X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=src%2Fsrc%2Fmime.c;h=45825950fcd119e77340d68abbda083e1b2b14ee;hb=8dce1a6f161d5f3617c38695029d485cc363fb43;hp=bece458c0ac8d30433ad760ede25154ddd986906;hpb=fb2274d4a2c4398a497fbec5cacebaab7d20a127;p=exim.git diff --git a/src/src/mime.c b/src/src/mime.c index bece458c0..45825950f 100644 --- a/src/src/mime.c +++ b/src/src/mime.c @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/mime.c,v 1.5 2005/03/08 15:32:02 tom Exp $ */ +/* $Cambridge: exim/src/src/mime.c,v 1.15 2006/09/05 15:34:41 ph10 Exp $ */ /************************************************* * Exim - an Internet mail transport agent * @@ -41,82 +41,42 @@ void mime_set_anomaly(int level, char *text) { 0-255 - char to write */ -unsigned int mime_qp_hstr_i(uschar *cptr) { - unsigned int i, j = 0; - while (cptr && *cptr && isxdigit(*cptr)) { - i = *cptr++ - '0'; - if (9 < i) i -= 7; - j <<= 4; - j |= (i & 0x0f); - } - return(j); -} - -uschar *mime_decode_qp_char(uschar *qp_p,int *c) { - uschar hex[] = {0,0,0}; - int nan = 0; +uschar *mime_decode_qp_char(uschar *qp_p, int *c) { uschar *initial_pos = qp_p; /* advance one char */ qp_p++; - REPEAT_FIRST: - if ( (*qp_p == '\t') || (*qp_p == ' ') || (*qp_p == '\r') ) { - /* tab or whitespace may follow - just ignore it, but remember - that this is not a valid hex - encoding any more */ - nan = 1; + /* Check for two hex digits and decode them */ + if (isxdigit(*qp_p) && isxdigit(qp_p[1])) { + /* Do hex conversion */ + if (isdigit(*qp_p)) {*c = *qp_p - '0';} + else {*c = toupper(*qp_p) - 'A' + 10;}; + *c <<= 4; + if (isdigit(qp_p[1])) {*c |= qp_p[1] - '0';} + else {*c |= toupper(qp_p[1]) - 'A' + 10;}; + return qp_p + 2; + }; + + /* tab or whitespace may follow just ignore it if it precedes \n */ + while (*qp_p == '\t' || *qp_p == ' ' || *qp_p == '\r') qp_p++; - goto REPEAT_FIRST; - } - else if ( (('0' <= *qp_p) && (*qp_p <= '9')) || (('A' <= *qp_p) && (*qp_p <= 'F')) || (('a' <= *qp_p) && (*qp_p <= 'f')) ) { - /* this is a valid hex char, if nan is unset */ - if (nan) { - /* this is illegal */ - *c = -2; - return initial_pos; - } - else { - hex[0] = *qp_p; - qp_p++; - }; - } - else if (*qp_p == '\n') { - /* hit soft line break already, continue */ + + if (*qp_p == '\n') { + /* hit soft line break */ *c = -1; return qp_p; - } - else { - /* illegal char here */ - *c = -2; - return initial_pos; }; - if ( (('0' <= *qp_p) && (*qp_p <= '9')) || (('A' <= *qp_p) && (*qp_p <= 'F')) || (('a' <= *qp_p) && (*qp_p <= 'f')) ) { - if (hex[0] > 0) { - hex[1] = *qp_p; - /* do hex conversion */ - *c = mime_qp_hstr_i(hex); - qp_p++; - return qp_p; - } - else { - /* huh ? */ - *c = -2; - return initial_pos; - }; - } - else { - /* illegal char */ - *c = -2; - return initial_pos; - }; + /* illegal char here */ + *c = -2; + return initial_pos; } + uschar *mime_parse_line(uschar *buffer, uschar *data, uschar *encoding, int *num_decoded) { - + if (encoding == NULL) { /* no encoding type at all */ NO_DECODING: @@ -159,7 +119,6 @@ uschar *mime_parse_line(uschar *buffer, uschar *data, uschar *encoding, int *num /* byte 0 ---------------------- */ if (*(p+1) == 255) { - mime_set_anomaly(MIME_ANOMALY_BROKEN_BASE64); break; } data[(*num_decoded)] = *p; @@ -171,7 +130,6 @@ uschar *mime_parse_line(uschar *buffer, uschar *data, uschar *encoding, int *num p++; /* byte 1 ---------------------- */ if (*(p+1) == 255) { - mime_set_anomaly(MIME_ANOMALY_BROKEN_BASE64); break; } data[(*num_decoded)] = *p; @@ -183,7 +141,6 @@ uschar *mime_parse_line(uschar *buffer, uschar *data, uschar *encoding, int *num p++; /* byte 2 ---------------------- */ if (*(p+1) == 255) { - mime_set_anomaly(MIME_ANOMALY_BROKEN_BASE64); break; } data[(*num_decoded)] = *p; @@ -243,11 +200,11 @@ FILE *mime_get_decode_file(uschar *pname, uschar *fname) { filename = (uschar *)malloc(2048); if ((pname != NULL) && (fname != NULL)) { - snprintf(CS filename, 2048, "%s/%s", pname, fname); - f = fopen(CS filename,"w+"); + (void)string_format(filename, 2048, "%s/%s", pname, fname); + f = modefopen(filename,"wb+",SPOOL_MODE); } else if (pname == NULL) { - f = fopen(CS fname,"w+"); + f = modefopen(fname,"wb+",SPOOL_MODE); } else if (fname == NULL) { int file_nr = 0; @@ -256,7 +213,7 @@ FILE *mime_get_decode_file(uschar *pname, uschar *fname) { /* must find first free sequential filename */ do { struct stat mystat; - snprintf(CS filename,2048,"%s/%s-%05u", pname, message_id, file_nr); + (void)string_format(filename,2048,"%s/%s-%05u", pname, message_id, file_nr); file_nr++; /* security break */ if (file_nr >= 1024) @@ -264,7 +221,7 @@ FILE *mime_get_decode_file(uschar *pname, uschar *fname) { result = stat(CS filename,&mystat); } while(result != -1); - f = fopen(CS filename,"w+"); + f = modefopen(filename,"wb+",SPOOL_MODE); }; /* set expansion variable */ @@ -292,7 +249,7 @@ int mime_decode(uschar **listptr) { f_pos = ftell(mime_stream); /* build default decode path (will exist since MBOX must be spooled up) */ - snprintf(CS decode_path,1024,"%s/scan/%s",spool_directory,message_id); + (void)string_format(decode_path,1024,"%s/scan/%s",spool_directory,message_id); /* reserve a line and decoder buffer to work in */ buffer = (uschar *)malloc(MIME_MAX_LINE_LENGTH+1); @@ -384,7 +341,7 @@ int mime_decode(uschar **listptr) { } - fclose(decode_file); + (void)fclose(decode_file); clearerr(mime_stream); fseek(mime_stream,f_pos,SEEK_SET); @@ -418,7 +375,7 @@ int mime_get_header(FILE *f, uschar *header) { c = fgetc(f); if (c == EOF) break; if ( (c == '\t') || (c == ' ') ) continue; - ungetc(c,f); + (void)ungetc(c,f); }; /* end of the header, terminate with ';' */ c = ';'; @@ -487,7 +444,7 @@ int mime_get_header(FILE *f, uschar *header) { }; }; - if (header[num_copied-1] != ';') { + if ((num_copied > 0) && (header[num_copied-1] != ';')) { header[num_copied-1] = ';'; }; @@ -502,8 +459,8 @@ int mime_get_header(FILE *f, uschar *header) { } -int mime_acl_check(FILE *f, struct mime_boundary_context *context, uschar - **user_msgptr, uschar **log_msgptr) { +int mime_acl_check(uschar *acl, FILE *f, struct mime_boundary_context *context, + uschar **user_msgptr, uschar **log_msgptr) { int rc = OK; uschar *header = NULL; struct mime_boundary_context nested_context; @@ -512,7 +469,7 @@ int mime_acl_check(FILE *f, struct mime_boundary_context *context, uschar header = (uschar *)malloc(MIME_MAX_HEADER_SIZE+1); if (header == NULL) { log_write(0, LOG_PANIC, - "acl_smtp_mime: can't allocate %d bytes of memory.", MIME_MAX_HEADER_SIZE+1); + "MIME ACL: can't allocate %d bytes of memory.", MIME_MAX_HEADER_SIZE+1); return DEFER; }; @@ -622,7 +579,7 @@ int mime_acl_check(FILE *f, struct mime_boundary_context *context, uschar memset(param_value,0,param_value_len+1); q = p + mime_parameter_list[j].namelen; Ustrncpy(param_value, q, param_value_len); - param_value = rfc2047_decode(param_value, TRUE, NULL, 32, ¶m_value_len, &q); + param_value = rfc2047_decode(param_value, check_rfc2047_length, NULL, 32, ¶m_value_len, &q); debug_printf("Found %s MIME parameter in %s header, value is '%s'\n", mime_parameter_list[j].name, mime_header_list[i].name, param_value); *((uschar **)(mime_parameter_list[j].value)) = param_value; p += (mime_parameter_list[j].namelen + param_value_len + 1); @@ -659,7 +616,7 @@ int mime_acl_check(FILE *f, struct mime_boundary_context *context, uschar mime_is_coverletter = !(context && context->context == MBC_ATTACHMENT); /* call ACL handling function */ - rc = acl_check(ACL_WHERE_MIME, NULL, acl_smtp_mime, user_msgptr, log_msgptr); + rc = acl_check(ACL_WHERE_MIME, NULL, acl, user_msgptr, log_msgptr); mime_stream = NULL; mime_current_boundary = NULL; @@ -680,7 +637,7 @@ int mime_acl_check(FILE *f, struct mime_boundary_context *context, uschar else nested_context.context = MBC_COVERLETTER_ONESHOT; - rc = mime_acl_check(f, &nested_context, user_msgptr, log_msgptr); + rc = mime_acl_check(acl, f, &nested_context, user_msgptr, log_msgptr); if (rc != OK) break; } else if ( (mime_content_type != NULL) && @@ -693,7 +650,7 @@ int mime_acl_check(FILE *f, struct mime_boundary_context *context, uschar /* must find first free sequential filename */ do { struct stat mystat; - snprintf(CS filename,2048,"%s/scan/%s/__rfc822_%05u", spool_directory, message_id, file_nr); + (void)string_format(filename,2048,"%s/scan/%s/__rfc822_%05u", spool_directory, message_id, file_nr); file_nr++; /* security break */ if (file_nr >= 128)