Commit b7c6921e authored by Per Cederqvist's avatar Per Cederqvist

Check the magic cookie when opening database files.

* src/server/ram-io.c (dbfile_open_read): New argument:
wanted_magic.  Log a message and return NULL if the file contains
the wrong magic cookie.  All callers updated to supply "CLEAN" as
the magic cookie.
parent 7f11c20f
From: Per Cederqvist <ceder@lysator.liu.se>
Subject: Check the magic cookie when opening database files.
* src/server/ram-io.c (dbfile_open_read): New argument:
wanted_magic. Log a message and return NULL if the file contains
the wrong magic cookie. All callers updated to supply "CLEAN" as
the magic cookie.
......@@ -40,6 +40,14 @@
That construct worked in texinfo.tex version 2003-11-16.09, but
support was removed in 2003-11-29.14.
2010-04-01 Per Cederqvist <ceder@lysator.liu.se>
Check the magic cookie when opening database files.
* src/server/ram-io.c (dbfile_open_read): New argument:
wanted_magic. Log a message and return NULL if the file contains
the wrong magic cookie. All callers updated to supply "CLEAN" as
the magic cookie.
2010-03-31 Per Cederqvist <ceder@lysator.liu.se>
Avoid an eternal loop in the test suite.
......
......@@ -845,7 +845,7 @@ init_cache(void)
if ( is_clean(param.datafile_name) )
{
if ((fp = dbfile_open_read(param.datafile_name)) == NULL)
if ((fp = dbfile_open_read(param.datafile_name, "CLEAN")) == NULL)
{
kom_log("WARNING: init_cache: can't open datafile.\n");
return FAILURE;
......@@ -854,7 +854,8 @@ init_cache(void)
}
else if ( is_clean(param.backupfile_name) )
{
if ((fp = dbfile_open_read(param.backupfile_name)) == NULL)
if ((fp = dbfile_open_read(param.backupfile_name, "CLEAN"))
== NULL)
{
kom_log("WARNING: init_cache: can't open backupfile.\n");
return FAILURE;
......
......@@ -29,6 +29,7 @@
#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "timewrap.h"
#include "server/smalloc.h"
......@@ -109,7 +110,8 @@ get_version(struct dbfile *fp)
}
struct dbfile *
dbfile_open_read(const char *filename)
dbfile_open_read(const char *filename,
const char *wanted_magic)
{
struct dbfile *res = dbfile_new();
res->fp = i_fopen(filename, "rb");
......@@ -120,6 +122,28 @@ dbfile_open_read(const char *filename)
errno = saved_errno;
return NULL;
}
if (wanted_magic != NULL)
{
char file_magic[6];
if (fread(file_magic, 5, 1, res->fp) != 1)
{
kom_log("Failed to read magic from %s: %s.\n",
filename, strerror(errno));
dbfile_delete(res);
return NULL;
}
file_magic[5] = '\0';
if (strcmp(file_magic, wanted_magic) != 0)
{
kom_log("Expected magic %s in file %s, but got %s.\n",
wanted_magic, filename, file_magic);
dbfile_delete(res);
return NULL;
}
}
++nr_dbfile_files;
res->format = get_version(res);
switch(res->format)
......
......@@ -55,9 +55,17 @@ dbfile_change_magic(struct dbfile *fp,
const char *magic);
/* Open the file for reading. The file format version will be read
from the file. */
from the file.
wanted_magic should be a NUL-terminated string of five characters.
If the file contains something else an error will be logged and
NULL returned. wanted_magic can also be NULL, to open the file
regardless of the magic string. However, all files are expected to
have a valid file header.
*/
extern struct dbfile *
dbfile_open_read(const char *filename);
dbfile_open_read(const char *filename,
const char *wanted_magic);
/* Dump information about the number of existing dbfile structs and
open dbfile file descriptors to the specified file. */
......
......@@ -1919,7 +1919,7 @@ pre_sync(void)
}
#ifdef FASTSAVE
if ((file_b_r = dbfile_open_read(param.datafile_name)) == NULL)
if ((file_b_r = dbfile_open_read(param.datafile_name, "CLEAN")) == NULL)
{
dbfile_delete(file_b);
file_b = NULL;
......@@ -2244,7 +2244,7 @@ post_sync(void)
else
dbfile_delete(file_a);
if ((file_a = dbfile_open_read(param.datafile_name)) == NULL)
if ((file_a = dbfile_open_read(param.datafile_name, "CLEAN")) == NULL)
{
kom_log("post_sync: can't open the file I just saved.\n");
sync_state = sync_wait;
......@@ -2432,7 +2432,7 @@ save_one_text(void)
file_b_r = NULL;
#endif
file_b = dbfile_open_read(param.datafile_name);
file_b = dbfile_open_read(param.datafile_name, "CLEAN");
if (file_b == NULL)
{
kom_log("save_one_text(): failed to reopen file.\n");
......@@ -2661,7 +2661,7 @@ init_cache(void)
if (is_clean(param.datafile_name))
{
if ((file_a = dbfile_open_read(param.datafile_name)) == NULL)
if ((file_a = dbfile_open_read(param.datafile_name, "CLEAN")) == NULL)
{
kom_log("WARNING: init_cache: can't open datafile.\n");
kom_errno = KOM_INTERNAL_ERROR;
......@@ -2673,7 +2673,8 @@ init_cache(void)
}
else if (is_clean(param.backupfile_name))
{
if ((file_a = dbfile_open_read(param.backupfile_name)) == NULL)
if ((file_a = dbfile_open_read(param.backupfile_name,
"CLEAN")) == NULL)
{
kom_log("WARNING: init_cache: can't open backupfile.\n");
kom_errno = KOM_INTERNAL_ERROR;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment