summaryrefslogtreecommitdiffstats
path: root/src/sh_dbIO.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sh_dbIO.c')
-rw-r--r--src/sh_dbIO.c137
1 files changed, 118 insertions, 19 deletions
diff --git a/src/sh_dbIO.c b/src/sh_dbIO.c
index 0ac9194..1f9b152 100644
--- a/src/sh_dbIO.c
+++ b/src/sh_dbIO.c
@@ -30,7 +30,7 @@
#include "sh_dbIO_int.h"
#include "sh_hash.h"
#include "sh_dbIO.h"
-#include "sh_gpg.h"
+#include "sh_sig.h"
#include "sh_tiger.h"
#include "sh_xfer.h"
#include "sh_pthread.h"
@@ -151,6 +151,8 @@ static int sh_dbIO_setdataent (SL_TICKET fd, char * line, int size,
SL_RETURN( retval, _("sh_dbIO_setdataent"));
}
+/* Seek to [SOF] and truncate remainder of file
+ */
static int sh_dbIO_setdataent_old (SL_TICKET fd, char * line, int size,
const char * file)
{
@@ -224,12 +226,10 @@ static unsigned short * swap_short (unsigned short * iptr)
else
{
/* alignment problem */
- unsigned char swap;
- static unsigned short ooop;
- unsigned char * ii;
- ooop = *iptr;
- ii = (unsigned char *) &ooop;
- swap = ii[0]; ii[0] = ii[1]; ii[1] = swap;
+ static unsigned short ooop = *iptr;
+ unsigned short hi = (ooop & 0xff00);
+ unsigned short lo = (ooop & 0xff);
+ ooop = (lo << 8) | (hi >> 8);
return &ooop;
}
return iptr;
@@ -378,10 +378,87 @@ static char * unquote_path(char * line, long i)
(void) sl_strlcpy (path, tmp, len);
if (tmp)
SH_FREE(tmp);
+ /* do not strip newline twice...
if (len > 1) {
if (path[len-2] == '\n')
path[len-2] = '\0';
}
+ ****/
+ return path;
+}
+
+/******************************************************************
+ *
+ * Use different init rootfs (patch by Kemal H.)
+ *
+ ******************************************************************/
+
+static char * sh_dbIO_rootfs = NULL;
+static size_t sh_dbIO_rootfs_len = 0;
+
+int sh_dbIO_init_rootfs (const char * rootfs)
+{
+ if (NULL == sh_dbIO_rootfs)
+ {
+ sh_dbIO_rootfs = sh_util_strdup (rootfs);
+ sh_dbIO_rootfs_len = sl_strlen(sh_dbIO_rootfs);
+ return 0;
+ }
+ return -1;
+}
+
+size_t sh_dbIO_get_rootfs_len()
+{
+ return sh_dbIO_rootfs_len;
+}
+
+/* Prepend rootfs when reading from config file ('path' must be allocated with sufficient space).
+ */
+char * sh_dbIO_rootfs_prepend(char * path)
+{
+ if (0 == sh_dbIO_rootfs_len)
+ return path;
+
+ memmove (path + sh_dbIO_rootfs_len, path, sl_strlen(path) + 1);
+ memcpy (path, sh_dbIO_rootfs, sh_dbIO_rootfs_len);
+
+ return path;
+}
+
+
+/* Strip rootfs when writing to database file.
+ */
+char * sh_dbIO_rootfs_strip(char * path)
+{
+ if (sh_dbIO_rootfs_len == 0)
+ {
+ return path;
+ }
+ else
+ {
+ size_t len = sl_strlen(path);
+
+ memmove (path, path + sh_dbIO_rootfs_len, len + 1 - sh_dbIO_rootfs_len);
+ if(path[0] != '/')
+ {
+ path[0]='/';
+ path[1]='\0';
+ }
+ }
+
+ return path;
+}
+
+char * sh_dbIO_rootfs_strip_link(char * path)
+{
+ if (sh_dbIO_rootfs_len == 0)
+ return path;
+ if (strstr(path, sh_dbIO_rootfs) == path)
+ {
+ size_t len = sl_strlen(path);
+
+ memmove (path, path + sh_dbIO_rootfs_len, len + 1 - sh_dbIO_rootfs_len);
+ }
return path;
}
@@ -406,7 +483,7 @@ static void corrupt_record(char * file, int line, const char * filepath)
static void wrong_version(char * file, int line, const char * filepath)
{
dlog(1, file, line,
- _("There is a record with a bad version number in the file signature database: %s\n"),
+ _("There is a record with a bad version number in the file signature database: %s\nThis may be caused by using '-t init' repeatedly to initialise the database, without (re)moving the database file.\n"),
(NULL == filepath) ? _("(null)") : filepath);
sh_error_handle((-1), file, line, 0, MSG_E_SUBGPATH,
_("Record with bad version number in file signature database"),
@@ -457,7 +534,7 @@ static size_t dbIO_fread_struct (sh_filestore_t * ptr, FILE *stream,
if (1 == 0)
hexdump((unsigned char *)&old_struct, sizeof(old_struct));
- memset(&try_struct, '\0', sizeof(try_struct));
+ memset(&try_struct, 0, sizeof(try_struct));
if (!memcmp(&old_struct, &try_struct, sizeof(try_struct)))
return 0; /* NULL read */
if (1 != fread (try, sizeof(try), 1, stream))
@@ -580,7 +657,7 @@ static sh_file_t * sh_dbIO_getdataent (char * line, int size,
}
fullpath = unquote_path(line, i);
-
+
/* Read next record -- Part Three -- Linkpath
*/
i = sh_dbIO_getline (sh_fin_fd, line, size);
@@ -774,12 +851,12 @@ static SL_TICKET load_data_from_disk(const char * filepath)
static SL_TICKET verify_data (SL_TICKET fd)
{
-#if defined(WITH_GPG) || defined(WITH_PGP)
+#if defined(WITH_SIG)
SL_TICKET fdTmp;
/* extract the data and copy to temporary file
*/
- fdTmp = sh_gpg_extract_signed(fd);
+ fdTmp = sh_sig_extract_signed(fd);
if (sig_termfast == 1) /* SIGTERM */
{
@@ -793,12 +870,16 @@ static SL_TICKET verify_data (SL_TICKET fd)
/* Validate signature of open file.
*/
- if (0 != sh_gpg_check_sign (fd, SIG_DATA))
+ if (0 != sh_sig_check_signature (fd, SIG_DATA))
{
sl_close(fd);
return -1;
}
sl_rewind (fd);
+
+ fdTmp = sh_sig_extract_signed_data(fd);
+ sl_close(fd);
+ fd = fdTmp;
#endif
return fd;
@@ -1116,15 +1197,19 @@ static void seek_writeout_data(SL_TICKET fd, const char * path)
return;
}
+/* Seek to [SOF] and truncate remainder
+ */
static int seek_writeout_data_old(SL_TICKET fd, const char * path)
{
char * line = SH_ALLOC(MAX_PATH_STORE+1);
+ /* This will do an ftruncate() after the sof marker
+ */
if (SL_ISERROR(sh_dbIO_setdataent_old (fd, line, MAX_PATH_STORE, path)))
{
SH_FREE(line);
sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
- _("Failed to seek to end of baseline database"),
+ _("Failed to seek to start of baseline database"),
_("seek_writeout_data_old"),
path);
aud_exit(FIL__, __LINE__, EXIT_FAILURE);
@@ -1329,6 +1414,7 @@ static void write_record(SL_TICKET fd, sh_filestore_t * p,
if (!linkpath || 0 == sl_strlen(linkpath))
lpath = ll;
else
+ /* cppcheck-suppress uninitvar */
lpath = linkpath;
if (pushdata_stdout == S_FALSE)
@@ -1413,6 +1499,7 @@ static void sh_dbIO_data_write_int (file_type * buf, char * fileHash,
else
{
pushdata_fd = open_writeout_data(outpath);
+ /* Seek to eof */
seek_writeout_data(pushdata_fd, outpath);
}
}
@@ -1423,13 +1510,13 @@ static void sh_dbIO_data_write_int (file_type * buf, char * fileHash,
{
TPT((0, FIL__, __LINE__, _("msg=<Update.>\n")));
pushdata_fd = open_writeout_data(outpath);
+ /* Seek to sof and truncate */
seek_writeout_data_old(pushdata_fd, outpath);
}
}
-
- if (!buf) {
- memset(&p, '\0', sizeof(sh_filestore_t));
- }
+
+ /* unconditionally initialize the structure */
+ memset(&p, 0, sizeof(sh_filestore_t));
if (buf != NULL)
{
@@ -1478,7 +1565,19 @@ static void sh_dbIO_data_write_int (file_type * buf, char * fileHash,
{
if (sh.flag.checkSum != SH_CHECK_INIT || (buf == NULL && fileHash == NULL))
{
- sl_close (pushdata_fd);
+ if (SL_ISERROR(sl_close (pushdata_fd)))
+ {
+ char * tmp = sh_util_safe_name(outpath);
+ sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
+ _("Failed to close baseline database"),
+ _("sh_dbIO_data_write_int"),
+ tmp);
+ SH_FREE(tmp);
+ }
+ else {
+ if (sh.flag.checkSum == SH_CHECK_INIT)
+ sh_error_handle((-1), FIL__, __LINE__, 0, MSG_DCLOSE);
+ }
pushdata_fd = -1;
}
}