mirror of
				https://bitbucket.org/jsuto/piler.git
				synced 2025-11-04 04:02:26 +01:00 
			
		
		
		
	digesting fixes
This commit is contained in:
		@@ -48,7 +48,7 @@ libpiler.a: $(OBJS) $(MYSQL_OBJS)
 | 
			
		||||
	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)
 | 
			
		||||
 | 
			
		||||
pilerconf: pilerconf.c cfg.o misc.o tai.o
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										44
									
								
								src/digest.c
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								src/digest.c
									
									
									
									
									
								
							@@ -14,9 +14,27 @@
 | 
			
		||||
#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 i=0, n, fd, offset=3;
 | 
			
		||||
   char *p, *body=NULL;
 | 
			
		||||
   int i=0, n, fd, offset=3, hdr_len=0;
 | 
			
		||||
   char *body=NULL;
 | 
			
		||||
   unsigned char buf[BIGBUFSIZE], md[DIGEST_LENGTH], md2[DIGEST_LENGTH];
 | 
			
		||||
   SHA256_CTX context, context2;
 | 
			
		||||
 | 
			
		||||
@@ -25,6 +43,7 @@ int make_digests(struct session_data *sdata, struct __config *cfg){
 | 
			
		||||
   SHA256_Init(&context);
 | 
			
		||||
   SHA256_Init(&context2);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
   fd = open(sdata->ttmpfile, O_RDONLY);
 | 
			
		||||
   if(fd == -1) return -1;
 | 
			
		||||
 | 
			
		||||
@@ -36,22 +55,13 @@ int make_digests(struct session_data *sdata, struct __config *cfg){
 | 
			
		||||
 | 
			
		||||
      if(i == 0){
 | 
			
		||||
 | 
			
		||||
         p = strstr(body, "\n\r\n");
 | 
			
		||||
         if(!p){
 | 
			
		||||
            p = strstr(body, "\n\n");
 | 
			
		||||
            if(p){
 | 
			
		||||
               offset = 2;
 | 
			
		||||
         hdr_len = search_header_end(body, n);
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
         }
 | 
			
		||||
         if(hdr_len > 0){
 | 
			
		||||
            body += hdr_len;
 | 
			
		||||
            n -= hdr_len;
 | 
			
		||||
 | 
			
		||||
         if(p){
 | 
			
		||||
            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);
 | 
			
		||||
            if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: hdr_len: %d, offset: %d", sdata->ttmpfile, hdr_len, offset);
 | 
			
		||||
         }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
@@ -63,6 +73,8 @@ int make_digests(struct session_data *sdata, struct __config *cfg){
 | 
			
		||||
 | 
			
		||||
   close(fd);
 | 
			
		||||
 | 
			
		||||
   sdata->hdr_len = hdr_len;
 | 
			
		||||
 | 
			
		||||
   SHA256_Final(md, &context);
 | 
			
		||||
   SHA256_Final(md2, &context2);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -29,7 +29,6 @@ EVP_CIPHER_CTX ctx;
 | 
			
		||||
unsigned char *s=NULL;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void clean_exit(){
 | 
			
		||||
   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)))){
 | 
			
		||||
 | 
			
		||||
      if(!EVP_DecryptUpdate(&ctx, s+tlen, &olen, inbuf, n)){
 | 
			
		||||
         fprintf(stderr, "EVP_DecryptUpdate()\n");
 | 
			
		||||
         printf("EVP_DecryptUpdate()\n");
 | 
			
		||||
         clean_exit();
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
@@ -194,7 +193,7 @@ int retrieve_file_from_archive(char *filename, int mode, char **buffer, FILE *de
 | 
			
		||||
   close(fd);
 | 
			
		||||
 | 
			
		||||
   if(EVP_DecryptFinal(&ctx, s + tlen, &olen) != 1){
 | 
			
		||||
      fprintf(stderr, "EVP_DecryptFinal()\n");
 | 
			
		||||
      printf("EVP_DecryptFinal()\n");
 | 
			
		||||
      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];
 | 
			
		||||
 | 
			
		||||
   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;
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   attachments = query_attachments(sdata, &ptr_arr[0], cfg);
 | 
			
		||||
 | 
			
		||||
   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;
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
@@ -247,7 +246,6 @@ int retrieve_email_from_archive(struct session_data *sdata, FILE *dest, struct _
 | 
			
		||||
            p = strstr(buffer, pointer);
 | 
			
		||||
            if(p){
 | 
			
		||||
               *p = '\0';
 | 
			
		||||
               //printf("%s", buffer);
 | 
			
		||||
               fwrite(buffer, 1, p - buffer, dest);
 | 
			
		||||
               buffer = p + strlen(pointer);
 | 
			
		||||
 | 
			
		||||
@@ -261,7 +259,6 @@ int retrieve_email_from_archive(struct session_data *sdata, FILE *dest, struct _
 | 
			
		||||
         }
 | 
			
		||||
 | 
			
		||||
         if(buffer){
 | 
			
		||||
            //printf("%s", buffer);
 | 
			
		||||
            fwrite(buffer, 1, strlen(buffer), dest);
 | 
			
		||||
         }
 | 
			
		||||
 | 
			
		||||
@@ -362,7 +359,7 @@ int main(int argc, char **argv){
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
   if(argc < 2){
 | 
			
		||||
      fprintf(stderr, "usage: %s <piler-id>\n", argv[0]);
 | 
			
		||||
      printf("usage: %s <piler-id>\n", argv[0]);
 | 
			
		||||
      exit(1);
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
@@ -389,6 +386,8 @@ int main(int argc, char **argv){
 | 
			
		||||
 | 
			
		||||
   if(argv[1][0] == '-'){
 | 
			
		||||
 | 
			
		||||
      memset(sdata.ttmpfile, 0, sizeof(sdata.ttmpfile));
 | 
			
		||||
 | 
			
		||||
      while((rc = read(0, sdata.ttmpfile, RND_STR_LEN+1)) > 0){
 | 
			
		||||
         trimBuffer(sdata.ttmpfile);
 | 
			
		||||
 | 
			
		||||
@@ -401,18 +400,21 @@ int main(int argc, char **argv){
 | 
			
		||||
               rc = retrieve_email_from_archive(&sdata, f, &cfg);
 | 
			
		||||
               fclose(f);
 | 
			
		||||
 | 
			
		||||
               snprintf(sdata.ttmpfile, sizeof(sdata.ttmpfile)-1, "%s", filename);
 | 
			
		||||
               snprintf(sdata.ttmpfile, SMALLBUFSIZE-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("%s was not found in archive\n", sdata.ttmpfile);
 | 
			
		||||
 | 
			
		||||
         memset(sdata.ttmpfile, 0, sizeof(sdata.ttmpfile));
 | 
			
		||||
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
   }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user