14091Seric # include <errno.h> 24091Seric # include "sendmail.h" 34091Seric 4*6980Seric SCCSID(@(#)headers.c 3.18.1.1 05/29/82); 54091Seric 64091Seric /* 74091Seric ** CHOMPHEADER -- process and save a header line. 84091Seric ** 94091Seric ** Called by collect and by readcf to deal with header lines. 104091Seric ** 114091Seric ** Parameters: 124091Seric ** line -- header as a text line. 134091Seric ** def -- if set, this is a default value. 144091Seric ** 154091Seric ** Returns: 164091Seric ** flags for this header. 174091Seric ** 184091Seric ** Side Effects: 194091Seric ** The header is saved on the header list. 204319Seric ** Contents of 'line' are destroyed. 214091Seric */ 224091Seric 234091Seric chompheader(line, def) 244091Seric char *line; 254091Seric bool def; 264091Seric { 274091Seric register char *p; 284091Seric register HDR *h; 294091Seric HDR **hp; 304091Seric extern bool isheader(); 314091Seric char *fname; 324091Seric char *fvalue; 334091Seric struct hdrinfo *hi; 344627Seric u_long mopts; 354627Seric extern u_long mfencode(); 364091Seric 374091Seric /* strip off trailing newline */ 384091Seric p = rindex(line, '\n'); 394091Seric if (p != NULL) 404091Seric *p = '\0'; 414091Seric 424627Seric /* strip off options */ 434627Seric mopts = 0; 444627Seric p = line; 454627Seric if (*p == '?') 464627Seric { 474627Seric /* have some */ 484627Seric register char *q = index(p + 1, *p); 494627Seric 504627Seric if (q != NULL) 514627Seric { 524627Seric *q++ = '\0'; 534627Seric mopts = mfencode(p + 1); 544627Seric p = q; 554627Seric } 564627Seric else 574627Seric syserr("chompheader: syntax error, line \"%s\"", line); 584627Seric } 594627Seric 604091Seric /* find canonical name */ 614627Seric fname = p; 624627Seric p = index(p, ':'); 634091Seric fvalue = &p[1]; 644091Seric while (isspace(*--p)) 654091Seric continue; 664091Seric *++p = '\0'; 674091Seric makelower(fname); 684091Seric 694091Seric /* strip field value on front */ 704091Seric if (*fvalue == ' ') 714091Seric fvalue++; 724091Seric 734372Seric /* hack, hack -- save From: line specially */ 746051Seric if (!def && !QueueRun && strcmp(fname, "from") == 0) 754372Seric { 766908Seric CurEnv->e_origfrom = newstr(fvalue); 774372Seric return (0); 784372Seric } 794372Seric 804091Seric /* search header list for this header */ 816908Seric for (hp = &CurEnv->e_header, h = CurEnv->e_header; h != NULL; hp = &h->h_link, h = h->h_link) 824091Seric { 835187Seric if (strcmp(fname, h->h_field) == 0 && bitset(H_DEFAULT, h->h_flags)) 844091Seric break; 854091Seric } 864091Seric 874091Seric /* see if it is a known type */ 884091Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 894091Seric { 904091Seric if (strcmp(hi->hi_field, fname) == 0) 914091Seric break; 924091Seric } 934091Seric 944091Seric /* if this means "end of header" quit now */ 954091Seric if (bitset(H_EOH, hi->hi_flags)) 964091Seric return (hi->hi_flags); 974091Seric 985187Seric /* don't put timestamps in every queue run */ 995187Seric if (QueueRun && h != NULL && bitset(H_FORCE, h->h_flags)) 1005187Seric return (h->h_flags); 1015187Seric 1024091Seric /* create/fill in a new node */ 1035187Seric if (h == NULL || bitset(H_FORCE, h->h_flags)) 1044091Seric { 1054091Seric /* create a new node */ 1065187Seric h = (HDR *) xalloc(sizeof *h); 1074091Seric h->h_field = newstr(fname); 1084091Seric h->h_value = NULL; 1095187Seric h->h_link = *hp; 1104091Seric h->h_flags = hi->hi_flags; 1114627Seric h->h_mflags = mopts | hi->hi_mflags; 1125187Seric *hp = h; 1134091Seric } 1144091Seric if (def) 1154091Seric h->h_flags |= H_DEFAULT; 1164627Seric else if (mopts == 0) 1174091Seric h->h_flags &= ~H_CHECK; 1184091Seric if (h->h_value != NULL) 1194091Seric free(h->h_value); 1204091Seric h->h_value = newstr(fvalue); 1215919Seric if (!def && GrabTo && bitset(H_RCPT, h->h_flags)) 1226908Seric sendto(h->h_value, 0, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 1234091Seric 1245937Seric /* hack to see if this is a new format message */ 1255937Seric if (bitset(H_RCPT, h->h_flags) && 1265937Seric (index(fvalue, ',') != NULL || index(fvalue, '(') != NULL || 1275937Seric index(fvalue, '<') != NULL)) 1286908Seric CurEnv->e_oldstyle = FALSE; 1295937Seric 1304091Seric return (h->h_flags); 1314091Seric } 1324091Seric /* 133*6980Seric ** ADDHEADER -- add a header entry to the end of the queue. 134*6980Seric ** 135*6980Seric ** This bypasses the special checking of chompheader. 136*6980Seric ** 137*6980Seric ** Parameters: 138*6980Seric ** field -- the name of the header field. 139*6980Seric ** value -- the value of the field. It must be lower-cased. 140*6980Seric ** e -- the envelope to add them to. 141*6980Seric ** 142*6980Seric ** Returns: 143*6980Seric ** none. 144*6980Seric ** 145*6980Seric ** Side Effects: 146*6980Seric ** adds the field on the list of headers for this envelope. 147*6980Seric */ 148*6980Seric 149*6980Seric addheader(field, value, e) 150*6980Seric char *field; 151*6980Seric char *value; 152*6980Seric ENVELOPE *e; 153*6980Seric { 154*6980Seric register HDR *h; 155*6980Seric register struct hdrinfo *hi; 156*6980Seric HDR **hp; 157*6980Seric 158*6980Seric /* find info struct */ 159*6980Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 160*6980Seric { 161*6980Seric if (strcmp(field, hi->hi_field) == 0) 162*6980Seric break; 163*6980Seric } 164*6980Seric 165*6980Seric /* find current place in list -- keep back pointer? */ 166*6980Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 167*6980Seric { 168*6980Seric if (strcmp(field, h->h_field) == 0) 169*6980Seric break; 170*6980Seric } 171*6980Seric 172*6980Seric /* allocate space for new header */ 173*6980Seric h = (HDR *) xalloc(sizeof *h); 174*6980Seric h->h_field = field; 175*6980Seric h->h_value = newstr(value); 176*6980Seric h->h_link = NULL; 177*6980Seric h->h_flags = hi->hi_flags | H_DEFAULT; 178*6980Seric h->h_mflags = hi->hi_mflags; 179*6980Seric *hp = h; 180*6980Seric } 181*6980Seric /* 1824091Seric ** HVALUE -- return value of a header. 1834091Seric ** 1844091Seric ** Only "real" fields (i.e., ones that have not been supplied 1854091Seric ** as a default) are used. 1864091Seric ** 1874091Seric ** Parameters: 1884091Seric ** field -- the field name. 1894091Seric ** 1904091Seric ** Returns: 1914091Seric ** pointer to the value part. 1924091Seric ** NULL if not found. 1934091Seric ** 1944091Seric ** Side Effects: 1954091Seric ** sets the H_USED bit in the header if found. 1964091Seric */ 1974091Seric 1984091Seric char * 1994091Seric hvalue(field) 2004091Seric char *field; 2014091Seric { 2024091Seric register HDR *h; 2034091Seric 2046908Seric for (h = CurEnv->e_header; h != NULL; h = h->h_link) 2054091Seric { 2064091Seric if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0) 2074091Seric { 2084091Seric h->h_flags |= H_USED; 2094091Seric return (h->h_value); 2104091Seric } 2114091Seric } 2124091Seric return (NULL); 2134091Seric } 2144091Seric /* 2154091Seric ** ISHEADER -- predicate telling if argument is a header. 2164091Seric ** 2174319Seric ** A line is a header if it has a single word followed by 2184319Seric ** optional white space followed by a colon. 2194319Seric ** 2204091Seric ** Parameters: 2214091Seric ** s -- string to check for possible headerness. 2224091Seric ** 2234091Seric ** Returns: 2244091Seric ** TRUE if s is a header. 2254091Seric ** FALSE otherwise. 2264091Seric ** 2274091Seric ** Side Effects: 2284091Seric ** none. 2294319Seric ** 2304319Seric ** Bugs: 2314319Seric ** According to RFC733, there should be a newline 2324319Seric ** permitted after the word but before the colon. 2334319Seric ** We don't seem to support that..... 2344091Seric */ 2354091Seric 2364091Seric bool 2374091Seric isheader(s) 2384091Seric register char *s; 2394091Seric { 2404091Seric if (!isalnum(*s)) 2414091Seric return (FALSE); 2424091Seric while (!isspace(*s) && *s != ':') 2434091Seric s++; 2444091Seric while (isspace(*s)) 2454091Seric s++; 2464091Seric return (*s == ':'); 2474091Seric } 2485919Seric /* 2495919Seric ** GETXPART -- extract the "signature" part of an address line. 2505919Seric ** 2515919Seric ** Try to extract the full name from a general address 2525919Seric ** field. We take anything which is a comment as a 2535919Seric ** first choice. Failing in that, we see if there is 2545919Seric ** a "machine readable" name (in <angle brackets>); if 2555919Seric ** so we take anything preceeding that clause. 2565919Seric ** 2575919Seric ** If we blow it here it's not all that serious. 2585919Seric ** 2595919Seric ** Parameters: 2605919Seric ** p -- line to crack. 2615919Seric ** 2625919Seric ** Returns: 2635919Seric ** signature part. 2645919Seric ** NULL if no signature part. 2655919Seric ** 2665919Seric ** Side Effects: 2675919Seric ** none. 2685919Seric */ 2695919Seric 2705919Seric char * 2715919Seric getxpart(p) 2725919Seric register char *p; 2735919Seric { 2745919Seric register char *q; 2755919Seric register char *rval = NULL; 2765919Seric 2775919Seric q = index(p, '('); 2785919Seric if (q != NULL) 2795919Seric { 2805919Seric int parenlev = 0; 2815919Seric 2825919Seric for (p = q; *p != '\0'; p++) 2835919Seric { 2845919Seric if (*p == '(') 2855919Seric parenlev++; 2865919Seric else if (*p == ')' && --parenlev <= 0) 2875919Seric break; 2885919Seric } 2895919Seric if (*p == ')') 2905919Seric { 2915919Seric *p = '\0'; 2925919Seric if (*++q != '\0') 2935919Seric rval = newstr(q); 2945919Seric *p = ')'; 2955919Seric } 2965919Seric } 2975919Seric else if ((q = index(p, '<')) != NULL) 2985919Seric { 2995919Seric char savec; 3005919Seric 3015919Seric while (*--q == ' ') 3025919Seric continue; 3035919Seric while (isspace(*p)) 3045919Seric p++; 3055919Seric savec = *++q; 3065919Seric *q = '\0'; 3075919Seric if (*p != '\0') 3085919Seric rval = newstr(p); 3095919Seric *q = savec; 3105919Seric } 3115919Seric 3125919Seric return (rval); 3135919Seric } 314