From 16abfa42660873a3f10a347afcf79d3937c6b435 Mon Sep 17 00:00:00 2001 From: SJ Date: Thu, 29 Dec 2011 12:11:28 +0100 Subject: [PATCH] added verification to the bulk export --- src/digest.c | 17 +++++--- src/piler.h | 2 +- src/pilerget.c | 108 +++++++++++++++++++++++++++++++++++++++++++--- src/session.c | 3 +- src/test.c | 2 +- util/db-mysql.sql | 2 +- 6 files changed, 116 insertions(+), 18 deletions(-) diff --git a/src/digest.c b/src/digest.c index ae7e862c..9f63cfb6 100644 --- a/src/digest.c +++ b/src/digest.c @@ -14,22 +14,24 @@ #include -int make_body_digest(struct session_data *sdata, struct __config *cfg){ +int make_digests(struct session_data *sdata, struct __config *cfg){ int i=0, n, fd, offset=3; char *p, *body=NULL; - unsigned char buf[BIGBUFSIZE], md[DIGEST_LENGTH]; - SHA256_CTX context; - - //if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: digesting", sdata->ttmpfile); + unsigned char buf[BIGBUFSIZE], md[DIGEST_LENGTH], md2[DIGEST_LENGTH]; + SHA256_CTX context, context2; memset(sdata->bodydigest, 0, 2*DIGEST_LENGTH+1); + memset(sdata->digest, 0, 2*DIGEST_LENGTH+1); SHA256_Init(&context); + SHA256_Init(&context2); fd = open(sdata->ttmpfile, O_RDONLY); if(fd == -1) return -1; while((n = read(fd, buf, sizeof(buf))) > 0){ + SHA256_Update(&context2, buf, n); + body = (char *)&buf[0]; if(i == 0){ @@ -62,9 +64,12 @@ int make_body_digest(struct session_data *sdata, struct __config *cfg){ close(fd); SHA256_Final(md, &context); + SHA256_Final(md2, &context2); - for(i=0;ibodydigest + i*2, 2*DIGEST_LENGTH, "%02x", md[i]); + snprintf(sdata->digest + i*2, 2*DIGEST_LENGTH, "%02x", md2[i]); + } return 0; } diff --git a/src/piler.h b/src/piler.h index 13acd4b3..a0cc457e 100644 --- a/src/piler.h +++ b/src/piler.h @@ -30,7 +30,7 @@ int read_key(struct __config *cfg); int do_av_check(struct session_data *sdata, char *rcpttoemail, char *fromemail, char *virusinfo, struct __data *data, struct __config *cfg); -int make_body_digest(struct session_data *sdata, struct __config *cfg); +int make_digests(struct session_data *sdata, struct __config *cfg); void digest_file(char *filename, char *digest); int processMessage(struct session_data *sdata, struct _state *sstate, struct __config *cfg); diff --git a/src/pilerget.c b/src/pilerget.c index 8a16f591..8015193f 100644 --- a/src/pilerget.c +++ b/src/pilerget.c @@ -274,9 +274,88 @@ int retrieve_email_from_archive(struct session_data *sdata, FILE *dest, struct _ } +uint64 get_id_by_piler_id(struct session_data *sdata, char *digest, char *bodydigest, struct __config *cfg){ + char s[SMALLBUFSIZE]; + MYSQL_STMT *stmt; + MYSQL_BIND bind[3]; + unsigned long len=0; + uint64 id=0; + + memset(digest, 0, 2*DIGEST_LENGTH+1); + memset(bodydigest, 0, 2*DIGEST_LENGTH+1); + + stmt = mysql_stmt_init(&(sdata->mysql)); + if(!stmt){ + goto ENDE; + } + + snprintf(s, SMALLBUFSIZE-1, "SELECT `id`,`digest`,`bodydigest` FROM %s WHERE piler_id=?", SQL_METADATA_TABLE); + + + if(mysql_stmt_prepare(stmt, s, strlen(s))){ + goto ENDE; + } + + memset(bind, 0, sizeof(bind)); + + bind[0].buffer_type = MYSQL_TYPE_STRING; + bind[0].buffer = sdata->ttmpfile; + bind[0].is_null = 0; + len = strlen(sdata->ttmpfile); bind[0].length = &len; + + + if(mysql_stmt_bind_param(stmt, bind)){ + goto ENDE; + } + + + if(mysql_stmt_execute(stmt)){ + goto ENDE; + } + + + memset(bind, 0, sizeof(bind)); + + bind[0].buffer_type = MYSQL_TYPE_LONGLONG; + bind[0].buffer = (char *)&id; + bind[0].is_null = 0; + bind[0].length = 0; + + bind[1].buffer_type = MYSQL_TYPE_STRING; + bind[1].buffer = digest; + bind[1].buffer_length = 2*DIGEST_LENGTH+1; + bind[1].is_null = 0; + bind[1].length = &len; + + bind[2].buffer_type = MYSQL_TYPE_STRING; + bind[2].buffer = bodydigest; + bind[2].buffer_length = 2*DIGEST_LENGTH+1; + bind[2].is_null = 0; + bind[2].length = &len; + + if(mysql_stmt_bind_result(stmt, bind)){ + goto ENDE; + } + + + if(mysql_stmt_store_result(stmt)){ + goto ENDE; + } + + mysql_stmt_fetch(stmt); + + mysql_stmt_close(stmt); + +ENDE: + + return id; +} + + int main(int argc, char **argv){ int rc; - char filename[SMALLBUFSIZE]; + uint64 id; + char filename[SMALLBUFSIZE], digest[2*DIGEST_LENGTH+1], bodydigest[2*DIGEST_LENGTH+1]; FILE *f; struct session_data sdata; struct __config cfg; @@ -312,13 +391,28 @@ int main(int argc, char **argv){ while((rc = read(0, sdata.ttmpfile, RND_STR_LEN+1)) > 0){ trimBuffer(sdata.ttmpfile); - snprintf(filename, sizeof(filename)-1, "%s.eml", sdata.ttmpfile); - f = fopen(filename, "w"); - if(f){ - rc = retrieve_email_from_archive(&sdata, f, &cfg); - fclose(f); + + id = get_id_by_piler_id(&sdata, &digest[0], &bodydigest[0], &cfg); + + if(id > 0){ + snprintf(filename, sizeof(filename)-1, "%llu.eml", id); + f = fopen(filename, "w"); + if(f){ + rc = retrieve_email_from_archive(&sdata, f, &cfg); + fclose(f); + + snprintf(sdata.ttmpfile, sizeof(sdata.ttmpfile)-1, "%s", filename); + make_digests(&sdata, &cfg); + + if(strcmp(digest, sdata.digest) == 0 && strcmp(bodydigest, sdata.bodydigest) == 0) + printf("exported %s, verification: OK\n", sdata.ttmpfile); + else + printf("exported %s, verification: FAILED\n", sdata.ttmpfile); + } + else printf("cannot open: %s\n", filename); } - else printf("cannot open: %s\n", filename); + else printf("%s was not found in archive\n", sdata.ttmpfile); + } } diff --git a/src/session.c b/src/session.c index 2b5476e3..86479659 100644 --- a/src/session.c +++ b/src/session.c @@ -149,8 +149,7 @@ void handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){ sdata.need_scan = 1; - digest_file(sdata.ttmpfile, sdata.digest); - make_body_digest(&sdata, cfg); + make_digests(&sdata, cfg); #ifdef HAVE_ANTIVIRUS if(cfg->use_antivirus == 1){ diff --git a/src/test.c b/src/test.c index 535bffdb..9d4e716d 100644 --- a/src/test.c +++ b/src/test.c @@ -74,7 +74,7 @@ int main(int argc, char **argv){ printf("subject: *%s*\n", state.b_subject); //printf("body: *%s*\n", state.b_body); - make_body_digest(&sdata, &cfg); + make_digests(&sdata, &cfg); printf("hdr len: %d\n", sdata.hdr_len); diff --git a/util/db-mysql.sql b/util/db-mysql.sql index e230db43..7031420e 100644 --- a/util/db-mysql.sql +++ b/util/db-mysql.sql @@ -61,7 +61,7 @@ create index `rcpt_idx2` on `rcpt`(`to`); drop view if exists `messages`; -create view `messages` AS select `metadata`.`id` AS `id`,`metadata`.`piler_id` AS `piler_id`,`metadata`.`from` AS `from`,`rcpt`.`to` AS `to`,`metadata`.`subject` AS `subject`, `metadata`.`size` AS `size` from (`metadata` join `rcpt`) where (`metadata`.`id` = `rcpt`.`id`); +create view `messages` AS select `metadata`.`id` AS `id`,`metadata`.`piler_id` AS `piler_id`,`metadata`.`from` AS `from`,`rcpt`.`to` AS `to`,`metadata`.`subject` AS `subject`, `metadata`.`size` AS `size`, `metadata`.`arrived` AS `arrived` from (`metadata` join `rcpt`) where (`metadata`.`id` = `rcpt`.`id`); drop table if exists `attachment`; create table if not exists `attachment` (