digesting fixes

This commit is contained in:
SJ 2011-12-29 21:25:40 +01:00
parent 16abfa4266
commit 89a83df4fb
3 changed files with 40 additions and 26 deletions

View File

@ -48,7 +48,7 @@ libpiler.a: $(OBJS) $(MYSQL_OBJS)
ln -sf libpiler.so.$(LIBPILER_VERSION) libpiler.so.$(PILER_VERSION) ln -sf libpiler.so.$(LIBPILER_VERSION) libpiler.so.$(PILER_VERSION)
pilerget: pilerget.c cfg.o misc.o tai.o store.o attachment.o pilerget: pilerget.c cfg.o misc.o tai.o store.o attachment.o digest.o
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $^ $(LIBS) $(LIBDIR) $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $^ $(LIBS) $(LIBDIR)
pilerconf: pilerconf.c cfg.o misc.o tai.o pilerconf: pilerconf.c cfg.o misc.o tai.o

View File

@ -14,9 +14,27 @@
#include <openssl/evp.h> #include <openssl/evp.h>
#define MAX(m,n) m <= n ? m : n
int search_header_end(char *p, int n){
int hdr_len=0;
if(strlen(p) < 5) return hdr_len;
for(; *p; p++){
if(hdr_len < n-2 && *p == '\n' && *(p+1) == '\r' && *(p+2) == '\n'){ hdr_len += 3; return MAX(hdr_len, n); }
if(hdr_len < n-1 && *p == '\n' && *(p+1) == '\n'){ hdr_len += 2; return MAX(hdr_len, n); }
hdr_len++;
}
return 0;
}
int make_digests(struct session_data *sdata, struct __config *cfg){ int make_digests(struct session_data *sdata, struct __config *cfg){
int i=0, n, fd, offset=3; int i=0, n, fd, offset=3, hdr_len=0;
char *p, *body=NULL; char *body=NULL;
unsigned char buf[BIGBUFSIZE], md[DIGEST_LENGTH], md2[DIGEST_LENGTH]; unsigned char buf[BIGBUFSIZE], md[DIGEST_LENGTH], md2[DIGEST_LENGTH];
SHA256_CTX context, context2; SHA256_CTX context, context2;
@ -25,6 +43,7 @@ int make_digests(struct session_data *sdata, struct __config *cfg){
SHA256_Init(&context); SHA256_Init(&context);
SHA256_Init(&context2); SHA256_Init(&context2);
fd = open(sdata->ttmpfile, O_RDONLY); fd = open(sdata->ttmpfile, O_RDONLY);
if(fd == -1) return -1; if(fd == -1) return -1;
@ -36,22 +55,13 @@ int make_digests(struct session_data *sdata, struct __config *cfg){
if(i == 0){ if(i == 0){
p = strstr(body, "\n\r\n"); hdr_len = search_header_end(body, n);
if(!p){
p = strstr(body, "\n\n");
if(p){
offset = 2;
} if(hdr_len > 0){
} body += hdr_len;
n -= hdr_len;
if(p){ if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: hdr_len: %d, offset: %d", sdata->ttmpfile, hdr_len, offset);
sdata->hdr_len = p - body + offset;
body += sdata->hdr_len;
n -= sdata->hdr_len;
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: hdr_len: %d, offset: %d", sdata->ttmpfile, sdata->hdr_len, offset);
} }
} }
@ -63,6 +73,8 @@ int make_digests(struct session_data *sdata, struct __config *cfg){
close(fd); close(fd);
sdata->hdr_len = hdr_len;
SHA256_Final(md, &context); SHA256_Final(md, &context);
SHA256_Final(md2, &context2); SHA256_Final(md2, &context2);

View File

@ -29,7 +29,6 @@ EVP_CIPHER_CTX ctx;
unsigned char *s=NULL; unsigned char *s=NULL;
void clean_exit(){ void clean_exit(){
if(s) free(s); if(s) free(s);
@ -184,7 +183,7 @@ int retrieve_file_from_archive(char *filename, int mode, char **buffer, FILE *de
while((n = read(fd, inbuf, sizeof(inbuf)))){ while((n = read(fd, inbuf, sizeof(inbuf)))){
if(!EVP_DecryptUpdate(&ctx, s+tlen, &olen, inbuf, n)){ if(!EVP_DecryptUpdate(&ctx, s+tlen, &olen, inbuf, n)){
fprintf(stderr, "EVP_DecryptUpdate()\n"); printf("EVP_DecryptUpdate()\n");
clean_exit(); clean_exit();
} }
@ -194,7 +193,7 @@ int retrieve_file_from_archive(char *filename, int mode, char **buffer, FILE *de
close(fd); close(fd);
if(EVP_DecryptFinal(&ctx, s + tlen, &olen) != 1){ if(EVP_DecryptFinal(&ctx, s + tlen, &olen) != 1){
fprintf(stderr, "EVP_DecryptFinal()\n"); printf("EVP_DecryptFinal()\n");
clean_exit(); clean_exit();
} }
@ -219,14 +218,14 @@ int retrieve_email_from_archive(struct session_data *sdata, FILE *dest, struct _
struct ptr_array ptr_arr[MAX_ATTACHMENTS]; struct ptr_array ptr_arr[MAX_ATTACHMENTS];
if(strlen(sdata->ttmpfile) != RND_STR_LEN){ if(strlen(sdata->ttmpfile) != RND_STR_LEN){
fprintf(stderr, "invalid piler-id: %s\n", sdata->ttmpfile); printf("invalid piler-id: %s\n", sdata->ttmpfile);
return 1; return 1;
} }
attachments = query_attachments(sdata, &ptr_arr[0], cfg); attachments = query_attachments(sdata, &ptr_arr[0], cfg);
if(attachments == -1){ if(attachments == -1){
fprintf(stderr, "problem querying the attachment of %s\n", sdata->ttmpfile); printf("problem querying the attachment of %s\n", sdata->ttmpfile);
return 1; return 1;
} }
@ -247,7 +246,6 @@ int retrieve_email_from_archive(struct session_data *sdata, FILE *dest, struct _
p = strstr(buffer, pointer); p = strstr(buffer, pointer);
if(p){ if(p){
*p = '\0'; *p = '\0';
//printf("%s", buffer);
fwrite(buffer, 1, p - buffer, dest); fwrite(buffer, 1, p - buffer, dest);
buffer = p + strlen(pointer); buffer = p + strlen(pointer);
@ -261,7 +259,6 @@ int retrieve_email_from_archive(struct session_data *sdata, FILE *dest, struct _
} }
if(buffer){ if(buffer){
//printf("%s", buffer);
fwrite(buffer, 1, strlen(buffer), dest); fwrite(buffer, 1, strlen(buffer), dest);
} }
@ -362,7 +359,7 @@ int main(int argc, char **argv){
if(argc < 2){ if(argc < 2){
fprintf(stderr, "usage: %s <piler-id>\n", argv[0]); printf("usage: %s <piler-id>\n", argv[0]);
exit(1); exit(1);
} }
@ -389,6 +386,8 @@ int main(int argc, char **argv){
if(argv[1][0] == '-'){ if(argv[1][0] == '-'){
memset(sdata.ttmpfile, 0, sizeof(sdata.ttmpfile));
while((rc = read(0, sdata.ttmpfile, RND_STR_LEN+1)) > 0){ while((rc = read(0, sdata.ttmpfile, RND_STR_LEN+1)) > 0){
trimBuffer(sdata.ttmpfile); trimBuffer(sdata.ttmpfile);
@ -401,18 +400,21 @@ int main(int argc, char **argv){
rc = retrieve_email_from_archive(&sdata, f, &cfg); rc = retrieve_email_from_archive(&sdata, f, &cfg);
fclose(f); fclose(f);
snprintf(sdata.ttmpfile, sizeof(sdata.ttmpfile)-1, "%s", filename); snprintf(sdata.ttmpfile, SMALLBUFSIZE-1, "%s", filename);
make_digests(&sdata, &cfg); make_digests(&sdata, &cfg);
if(strcmp(digest, sdata.digest) == 0 && strcmp(bodydigest, sdata.bodydigest) == 0) if(strcmp(digest, sdata.digest) == 0 && strcmp(bodydigest, sdata.bodydigest) == 0)
printf("exported %s, verification: OK\n", sdata.ttmpfile); printf("exported %s, verification: OK\n", sdata.ttmpfile);
else else
printf("exported %s, verification: FAILED\n", sdata.ttmpfile); 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); else printf("%s was not found in archive\n", sdata.ttmpfile);
memset(sdata.ttmpfile, 0, sizeof(sdata.ttmpfile));
} }
} }