+ /* parse headers, set up expansion variables */
+ while (mime_get_header(f, header))
+
+ /* look for interesting headers */
+ for (struct mime_header * mh = mime_header_list;
+ mh < mime_header_list + mime_header_list_size;
+ mh++) if (strncmpic(mh->name, header, mh->namelen) == 0)
+ {
+ uschar * p = header + mh->namelen;
+ uschar * q;
+
+ /* grab the value (normalize to lower case)
+ and copy to its corresponding expansion variable */
+
+ for (q = p; *q != ';' && *q; q++) ;
+ *mh->value = string_copynlc(p, q-p);
+ DEBUG(D_acl) debug_printf_indent("MIME: found %s header, value is '%s'\n",
+ mh->name, *mh->value);
+
+ if (*(p = q)) p++; /* jump past the ; */
+
+ {
+ uschar * mime_fname = NULL;
+ uschar * mime_fname_rfc2231 = NULL;
+ uschar * mime_filename_charset = NULL;
+ BOOL decoding_failed = FALSE;
+
+ /* grab all param=value tags on the remaining line,
+ check if they are interesting */
+
+ while (*p)
+ {
+ DEBUG(D_acl) debug_printf_indent("MIME: considering paramlist '%s'\n", p);
+
+ if ( !mime_filename
+ && strncmpic(CUS"content-disposition:", header, 20) == 0
+ && strncmpic(CUS"filename*", p, 9) == 0
+ )
+ { /* RFC 2231 filename */
+ uschar * q;
+
+ /* find value of the filename */
+ p += 9;
+ while(*p != '=' && *p) p++;
+ if (*p) p++; /* p is filename or NUL */
+ q = mime_param_val(&p); /* p now trailing ; or NUL */
+
+ if (q && *q)
+ {
+ uschar * temp_string, * err_msg;
+ int slen;
+
+ /* build up an un-decoded filename over successive
+ filename*= parameters (for use when 2047 decode fails) */
+
+ mime_fname_rfc2231 = string_sprintf("%#s%s",
+ mime_fname_rfc2231, q);
+
+ if (!decoding_failed)
+ {
+ int size;
+ if (!mime_filename_charset)
+ {
+ uschar * s = q;
+
+ /* look for a ' in the "filename" */
+ while(*s != '\'' && *s) s++; /* s is 1st ' or NUL */
+
+ if ((size = s-q) > 0)
+ mime_filename_charset = string_copyn(q, size);
+
+ if (*(p = s)) p++;
+ while(*p == '\'') p++; /* p is after 2nd ' */
+ }
+ else
+ p = q;
+
+ DEBUG(D_acl) debug_printf_indent("MIME: charset %s fname '%s'\n",
+ mime_filename_charset ? mime_filename_charset : US"<NULL>", p);
+
+ temp_string = rfc2231_to_2047(p, mime_filename_charset, &slen);
+ DEBUG(D_acl) debug_printf_indent("MIME: 2047-name %s\n", temp_string);
+
+ temp_string = rfc2047_decode(temp_string, FALSE, NULL, ' ',
+ NULL, &err_msg);
+ DEBUG(D_acl) debug_printf_indent("MIME: plain-name %s\n", temp_string);
+
+ if (!temp_string || (size = Ustrlen(temp_string)) == slen)
+ decoding_failed = TRUE;
+ else
+ /* build up a decoded filename over successive
+ filename*= parameters */
+
+ mime_filename = mime_fname = mime_fname
+ ? string_sprintf("%s%s", mime_fname, temp_string)
+ : temp_string;
+ }
+ }
+ }
+
+ else
+ /* look for interesting parameters */
+ for (mime_parameter * mp = mime_parameter_list;
+ mp < mime_parameter_list + nelem(mime_parameter_list);
+ mp++
+ ) if (strncmpic(mp->name, p, mp->namelen) == 0)
+ {
+ uschar * q;
+ uschar * dummy_errstr;
+
+ /* grab the value and copy to its expansion variable */
+ p += mp->namelen;
+ q = mime_param_val(&p); /* p now trailing ; or NUL */
+
+ *mp->value = q && *q
+ ? rfc2047_decode(q, check_rfc2047_length, NULL, 32, NULL,
+ &dummy_errstr)
+ : NULL;
+ DEBUG(D_acl) debug_printf_indent(
+ "MIME: found %s parameter in %s header, value '%s'\n",
+ mp->name, mh->name, *mp->value);
+
+ break; /* done matching param names */
+ }
+
+
+ /* There is something, but not one of our interesting parameters.
+ Advance past the next semicolon */
+ p = mime_next_semicolon(p);
+ if (*p) p++;
+ } /* param scan on line */
+
+ if (strncmpic(CUS"content-disposition:", header, 20) == 0)
+ {
+ if (decoding_failed) mime_filename = mime_fname_rfc2231;
+
+ DEBUG(D_acl) debug_printf_indent(
+ "MIME: found %s parameter in %s header, value is '%s'\n",
+ "filename", mh->name, mime_filename);
+ }
+ }
+ }