Annotation of src/usr.bin/mail/aux.c, Revision 1.31
1.31 ! deraadt 1: /* $OpenBSD: aux.c,v 1.30 2018/09/16 02:38:57 millert Exp $ */
1.5 millert 2: /* $NetBSD: aux.c,v 1.5 1997/05/13 06:15:52 mikel Exp $ */
1.2 deraadt 3:
1.1 deraadt 4: /*
5: * Copyright (c) 1980, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.21 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #include "rcv.h"
1.28 guenther 34: #include <fcntl.h>
1.1 deraadt 35: #include "extern.h"
36:
37: /*
38: * Mail -- a mail program
39: *
40: * Auxiliary functions.
41: */
1.20 millert 42: static char *save2str(char *, char *);
1.1 deraadt 43:
44: /*
45: * Return a pointer to a dynamic copy of the argument.
46: */
47: char *
1.30 millert 48: savestr(const char *str)
1.1 deraadt 49: {
50: char *new;
51: int size = strlen(str) + 1;
52:
1.7 millert 53: if ((new = salloc(size)) != NULL)
1.6 millert 54: (void)memcpy(new, str, size);
1.5 millert 55: return(new);
1.1 deraadt 56: }
57:
58: /*
59: * Make a copy of new argument incorporating old one.
60: */
1.2 deraadt 61: static char *
1.20 millert 62: save2str(char *str, char *old)
1.1 deraadt 63: {
64: char *new;
65: int newsize = strlen(str) + 1;
66: int oldsize = old ? strlen(old) + 1 : 0;
67:
1.7 millert 68: if ((new = salloc(newsize + oldsize)) != NULL) {
1.1 deraadt 69: if (oldsize) {
1.6 millert 70: (void)memcpy(new, old, oldsize);
1.1 deraadt 71: new[oldsize - 1] = ' ';
72: }
1.6 millert 73: (void)memcpy(new + oldsize, str, newsize);
1.1 deraadt 74: }
1.5 millert 75: return(new);
1.1 deraadt 76: }
77:
78: /*
79: * Touch the named message by setting its MTOUCH flag.
80: * Touched messages have the effect of not being sent
81: * back to the system mailbox on exit.
82: */
83: void
1.20 millert 84: touch(struct message *mp)
1.1 deraadt 85: {
86:
87: mp->m_flag |= MTOUCH;
88: if ((mp->m_flag & MREAD) == 0)
89: mp->m_flag |= MREAD|MSTATUS;
90: }
91:
92: /*
93: * Test to see if the passed file name is a directory.
94: * Return true if it is.
95: */
96: int
1.20 millert 97: isdir(char *name)
1.1 deraadt 98: {
99: struct stat sbuf;
100:
1.31 ! deraadt 101: if (stat(name, &sbuf) == -1)
1.1 deraadt 102: return(0);
1.16 millert 103: return(S_ISDIR(sbuf.st_mode));
1.1 deraadt 104: }
105:
106: /*
107: * Count the number of arguments in the given string raw list.
108: */
109: int
1.20 millert 110: argcount(char **argv)
1.1 deraadt 111: {
1.13 millert 112: char **ap;
1.1 deraadt 113:
1.7 millert 114: for (ap = argv; *ap++ != NULL;)
1.1 deraadt 115: ;
1.5 millert 116: return(ap - argv - 1);
1.1 deraadt 117: }
118:
119: /*
120: * Return the desired header line from the passed message
1.7 millert 121: * pointer (or NULL if the desired header field is not available).
1.1 deraadt 122: */
123: char *
1.20 millert 124: hfield(char *field, struct message *mp)
1.1 deraadt 125: {
1.13 millert 126: FILE *ibuf;
1.1 deraadt 127: char linebuf[LINESIZE];
1.13 millert 128: int lc;
129: char *hfield;
1.7 millert 130: char *colon, *oldhfield = NULL;
1.1 deraadt 131:
132: ibuf = setinput(mp);
133: if ((lc = mp->m_lines - 1) < 0)
1.7 millert 134: return(NULL);
1.19 millert 135: if (readline(ibuf, linebuf, LINESIZE, NULL) < 0)
1.7 millert 136: return(NULL);
1.1 deraadt 137: while (lc > 0) {
138: if ((lc = gethfield(ibuf, linebuf, lc, &colon)) < 0)
1.5 millert 139: return(oldhfield);
1.2 deraadt 140: if ((hfield = ishfield(linebuf, colon, field)) != NULL)
1.1 deraadt 141: oldhfield = save2str(hfield, oldhfield);
142: }
1.5 millert 143: return(oldhfield);
1.1 deraadt 144: }
145:
146: /*
147: * Return the next header field found in the given message.
148: * Return >= 0 if something found, < 0 elsewise.
149: * "colon" is set to point to the colon in the header.
150: * Must deal with \ continuations & other such fraud.
151: */
152: int
1.20 millert 153: gethfield(FILE *f, char *linebuf, int rem, char **colon)
1.1 deraadt 154: {
155: char line2[LINESIZE];
1.13 millert 156: char *cp, *cp2;
157: int c;
1.1 deraadt 158:
159: for (;;) {
160: if (--rem < 0)
1.5 millert 161: return(-1);
1.19 millert 162: if ((c = readline(f, linebuf, LINESIZE, NULL)) <= 0)
1.5 millert 163: return(-1);
1.26 okan 164: for (cp = linebuf;
165: isprint((unsigned char)*cp) && *cp != ' ' && *cp != ':';
166: cp++)
1.1 deraadt 167: ;
168: if (*cp != ':' || cp == linebuf)
169: continue;
170: /*
171: * I guess we got a headline.
172: * Handle wraparounding
173: */
174: *colon = cp;
175: cp = linebuf + c;
176: for (;;) {
177: while (--cp >= linebuf && (*cp == ' ' || *cp == '\t'))
178: ;
179: cp++;
180: if (rem <= 0)
181: break;
182: ungetc(c = getc(f), f);
183: if (c != ' ' && c != '\t')
184: break;
1.19 millert 185: if ((c = readline(f, line2, LINESIZE, NULL)) < 0)
1.1 deraadt 186: break;
187: rem--;
188: for (cp2 = line2; *cp2 == ' ' || *cp2 == '\t'; cp2++)
189: ;
190: c -= cp2 - line2;
191: if (cp + c >= linebuf + LINESIZE - 2)
192: break;
193: *cp++ = ' ';
1.6 millert 194: (void)memcpy(cp, cp2, c);
1.1 deraadt 195: cp += c;
196: }
197: *cp = 0;
1.5 millert 198: return(rem);
1.1 deraadt 199: }
200: /* NOTREACHED */
201: }
202:
203: /*
204: * Check whether the passed line is a header line of
205: * the desired breed. Return the field body, or 0.
206: */
207:
208: char*
1.20 millert 209: ishfield(char *linebuf, char *colon, char *field)
1.1 deraadt 210: {
1.13 millert 211: char *cp = colon;
1.1 deraadt 212:
213: *cp = 0;
214: if (strcasecmp(linebuf, field) != 0) {
215: *cp = ':';
1.5 millert 216: return(0);
1.1 deraadt 217: }
218: *cp = ':';
219: for (cp++; *cp == ' ' || *cp == '\t'; cp++)
220: ;
1.5 millert 221: return(cp);
1.1 deraadt 222: }
223:
224: /*
1.10 millert 225: * Copy a string, lowercasing it as we go. ``dsize'' should be
1.20 millert 226: * the real size (not len) of the dest string (guarantee NUL term).
1.1 deraadt 227: */
1.20 millert 228: size_t
229: istrlcpy(char *dst, const char *src, size_t dsize)
1.1 deraadt 230: {
1.20 millert 231: char *d = dst;
232: const char *s = src;
233: size_t n = dsize;
234:
235: /* Copy as many bytes as will fit */
236: if (n != 0 && --n != 0) {
237: do {
1.26 okan 238: if ((*d++ = tolower((unsigned char)*s++)) == 0)
1.20 millert 239: break;
240: } while (--n != 0);
241: }
1.1 deraadt 242:
1.20 millert 243: /* Not enough room in dst, add NUL and traverse rest of src */
244: if (n == 0) {
245: if (dsize != 0)
246: *d = '\0'; /* NUL-terminate dst */
247: while (*s++)
248: ;
1.10 millert 249: }
1.20 millert 250:
251: return(s - src - 1); /* count does not include NUL */
1.1 deraadt 252: }
253:
254: /*
255: * The following code deals with input stacking to do source
256: * commands. All but the current file pointer are saved on
257: * the stack.
258: */
259: static int ssp; /* Top of file stack */
260: struct sstack {
261: FILE *s_file; /* File we were in. */
262: int s_cond; /* Saved state of conditionals */
263: int s_loading; /* Loading .mailrc, etc. */
1.27 deraadt 264: } sstack[OPEN_MAX];
1.1 deraadt 265:
266: /*
267: * Pushdown current input file and switch to a new one.
268: * Set the global flag "sourcing" so that others will realize
269: * that they are no longer reading from a tty (in all probability).
270: */
271: int
1.20 millert 272: source(void *v)
1.1 deraadt 273: {
1.2 deraadt 274: char **arglist = v;
1.1 deraadt 275: FILE *fi;
276: char *cp;
277:
1.7 millert 278: if ((cp = expand(*arglist)) == NULL)
1.1 deraadt 279: return(1);
280: if ((fi = Fopen(cp, "r")) == NULL) {
1.15 millert 281: warn("%s", cp);
1.1 deraadt 282: return(1);
283: }
1.27 deraadt 284: if (ssp >= OPEN_MAX - 1) {
1.5 millert 285: puts("Too much \"sourcing\" going on.");
286: (void)Fclose(fi);
1.1 deraadt 287: return(1);
288: }
289: sstack[ssp].s_file = input;
290: sstack[ssp].s_cond = cond;
291: sstack[ssp].s_loading = loading;
292: ssp++;
293: loading = 0;
294: cond = CANY;
295: input = fi;
296: sourcing++;
297: return(0);
298: }
299:
300: /*
301: * Pop the current input back to the previous level.
302: * Update the "sourcing" flag as appropriate.
303: */
304: int
1.20 millert 305: unstack(void)
1.1 deraadt 306: {
1.20 millert 307:
1.1 deraadt 308: if (ssp <= 0) {
1.5 millert 309: puts("\"Source\" stack over-pop.");
1.1 deraadt 310: sourcing = 0;
311: return(1);
312: }
1.5 millert 313: (void)Fclose(input);
1.1 deraadt 314: if (cond != CANY)
1.5 millert 315: puts("Unmatched \"if\"");
1.1 deraadt 316: ssp--;
317: cond = sstack[ssp].s_cond;
318: loading = sstack[ssp].s_loading;
319: input = sstack[ssp].s_file;
320: if (ssp == 0)
321: sourcing = loading;
322: return(0);
323: }
324:
325: /*
326: * Touch the indicated file.
327: * This is nifty for the shell.
328: */
329: void
1.20 millert 330: alter(char *name)
1.1 deraadt 331: {
1.28 guenther 332: struct timespec ts[2];
1.1 deraadt 333:
1.28 guenther 334: clock_gettime(CLOCK_REALTIME, &ts[0]);
335: ts[0].tv_sec++;
336: ts[1].tv_nsec = UTIME_OMIT;
337: (void)utimensat(AT_FDCWD, name, ts, 0);
1.1 deraadt 338: }
339:
340: /*
341: * Examine the passed line buffer and
342: * return true if it is all blanks and tabs.
343: */
344: int
1.20 millert 345: blankline(char *linebuf)
1.1 deraadt 346: {
1.13 millert 347: char *cp;
1.1 deraadt 348:
349: for (cp = linebuf; *cp; cp++)
350: if (*cp != ' ' && *cp != '\t')
351: return(0);
352: return(1);
353: }
354:
355: /*
356: * Get sender's name from this message. If the message has
357: * a bunch of arpanet stuff in it, we may have to skin the name
358: * before returning it.
359: */
360: char *
1.20 millert 361: nameof(struct message *mp, int reptype)
1.1 deraadt 362: {
1.13 millert 363: char *cp, *cp2;
1.1 deraadt 364:
365: cp = skin(name1(mp, reptype));
366: if (reptype != 0 || charcount(cp, '!') < 2)
367: return(cp);
1.3 millert 368: cp2 = strrchr(cp, '!');
1.1 deraadt 369: cp2--;
370: while (cp2 > cp && *cp2 != '!')
371: cp2--;
372: if (*cp2 == '!')
373: return(cp2 + 1);
374: return(cp);
375: }
376:
377: /*
378: * Start of a "comment".
379: * Ignore it.
380: */
381: char *
1.20 millert 382: skip_comment(char *cp)
1.1 deraadt 383: {
1.13 millert 384: int nesting = 1;
1.1 deraadt 385:
386: for (; nesting > 0 && *cp; cp++) {
387: switch (*cp) {
388: case '\\':
389: if (cp[1])
390: cp++;
391: break;
392: case '(':
393: nesting++;
394: break;
395: case ')':
396: nesting--;
397: break;
398: }
399: }
1.5 millert 400: return(cp);
1.1 deraadt 401: }
402:
403: /*
404: * Skin an arpa net address according to the RFC 822 interpretation
405: * of "host-phrase."
406: */
407: char *
1.20 millert 408: skin(char *name)
1.1 deraadt 409: {
1.13 millert 410: char *nbuf, *bufend, *cp, *cp2;
411: int c, gotlt, lastsp;
1.1 deraadt 412:
1.7 millert 413: if (name == NULL)
414: return(NULL);
415: if (strchr(name, '(') == NULL && strchr(name, '<') == NULL
416: && strchr(name, ' ') == NULL)
1.1 deraadt 417: return(name);
1.11 millert 418:
419: /* We assume that length(input) <= length(output) */
1.29 mmcc 420: if ((nbuf = malloc(strlen(name) + 1)) == NULL)
421: err(1, "malloc");
1.1 deraadt 422: gotlt = 0;
423: lastsp = 0;
1.12 millert 424: bufend = nbuf;
1.26 okan 425: for (cp = name, cp2 = bufend; (c = (unsigned char)*cp++) != '\0'; ) {
1.1 deraadt 426: switch (c) {
427: case '(':
428: cp = skip_comment(cp);
429: lastsp = 0;
430: break;
431:
432: case '"':
433: /*
434: * Start of a "quoted-string".
435: * Copy it in its entirety.
436: */
1.26 okan 437: while ((c = (unsigned char)*cp) != '\0') {
1.1 deraadt 438: cp++;
439: if (c == '"')
440: break;
441: if (c != '\\')
442: *cp2++ = c;
1.26 okan 443: else if ((c = (unsigned char)*cp) != '\0') {
1.1 deraadt 444: *cp2++ = c;
445: cp++;
446: }
447: }
448: lastsp = 0;
449: break;
450:
451: case ' ':
1.24 martynas 452: if (strncmp(cp, "at ", 3) == 0)
1.1 deraadt 453: cp += 3, *cp2++ = '@';
454: else
1.24 martynas 455: if (strncmp(cp, "@ ", 2) == 0)
1.1 deraadt 456: cp += 2, *cp2++ = '@';
457: else
458: lastsp = 1;
459: break;
460:
461: case '<':
462: cp2 = bufend;
463: gotlt++;
464: lastsp = 0;
465: break;
466:
467: case '>':
468: if (gotlt) {
469: gotlt = 0;
1.26 okan 470: while ((c = (unsigned char)*cp) && c != ',') {
1.1 deraadt 471: cp++;
472: if (c == '(')
473: cp = skip_comment(cp);
474: else if (c == '"')
1.26 okan 475: while ((c = (unsigned char)*cp) != '\0') {
1.1 deraadt 476: cp++;
477: if (c == '"')
478: break;
479: if (c == '\\' && *cp)
480: cp++;
481: }
482: }
483: lastsp = 0;
484: break;
485: }
486: /* Fall into . . . */
487:
488: default:
489: if (lastsp) {
490: lastsp = 0;
491: *cp2++ = ' ';
492: }
493: *cp2++ = c;
1.17 millert 494: if (c == ',' && *cp == ' ' && !gotlt) {
1.1 deraadt 495: *cp2++ = ' ';
1.18 millert 496: while (*++cp == ' ')
1.1 deraadt 497: ;
498: lastsp = 0;
499: bufend = cp2;
500: }
501: }
502: }
503: *cp2 = 0;
504:
1.29 mmcc 505: if ((cp = realloc(nbuf, strlen(nbuf) + 1)) != NULL)
1.17 millert 506: nbuf = cp;
1.12 millert 507: return(nbuf);
1.1 deraadt 508: }
509:
510: /*
511: * Fetch the sender's name from the passed message.
512: * Reptype can be
513: * 0 -- get sender's name for display purposes
514: * 1 -- get sender's name for reply
515: * 2 -- get sender's name for Reply
516: */
517: char *
1.20 millert 518: name1(struct message *mp, int reptype)
1.1 deraadt 519: {
520: char namebuf[LINESIZE];
521: char linebuf[LINESIZE];
1.13 millert 522: char *cp, *cp2;
523: FILE *ibuf;
1.1 deraadt 524: int first = 1;
525:
1.7 millert 526: if ((cp = hfield("from", mp)) != NULL)
1.5 millert 527: return(cp);
1.7 millert 528: if (reptype == 0 && (cp = hfield("sender", mp)) != NULL)
1.5 millert 529: return(cp);
1.1 deraadt 530: ibuf = setinput(mp);
1.4 deraadt 531: namebuf[0] = '\0';
1.19 millert 532: if (readline(ibuf, linebuf, LINESIZE, NULL) < 0)
1.1 deraadt 533: return(savestr(namebuf));
534: newname:
535: for (cp = linebuf; *cp && *cp != ' '; cp++)
536: ;
537: for (; *cp == ' ' || *cp == '\t'; cp++)
538: ;
539: for (cp2 = &namebuf[strlen(namebuf)];
540: *cp && *cp != ' ' && *cp != '\t' && cp2 < namebuf + LINESIZE - 1;)
541: *cp2++ = *cp++;
542: *cp2 = '\0';
1.19 millert 543: if (readline(ibuf, linebuf, LINESIZE, NULL) < 0)
1.1 deraadt 544: return(savestr(namebuf));
1.3 millert 545: if ((cp = strchr(linebuf, 'F')) == NULL)
1.1 deraadt 546: return(savestr(namebuf));
547: if (strncmp(cp, "From", 4) != 0)
548: return(savestr(namebuf));
1.3 millert 549: while ((cp = strchr(cp, 'r')) != NULL) {
1.1 deraadt 550: if (strncmp(cp, "remote", 6) == 0) {
1.3 millert 551: if ((cp = strchr(cp, 'f')) == NULL)
1.1 deraadt 552: break;
553: if (strncmp(cp, "from", 4) != 0)
554: break;
1.3 millert 555: if ((cp = strchr(cp, ' ')) == NULL)
1.1 deraadt 556: break;
557: cp++;
558: if (first) {
1.5 millert 559: cp2 = namebuf;
1.1 deraadt 560: first = 0;
561: } else
1.5 millert 562: cp2 = strrchr(namebuf, '!') + 1;
1.20 millert 563: strlcpy(cp2, cp, sizeof(namebuf) - (cp2 - namebuf) - 1);
564: strlcat(namebuf, "!", sizeof(namebuf));
1.1 deraadt 565: goto newname;
566: }
567: cp++;
568: }
569: return(savestr(namebuf));
570: }
571:
572: /*
573: * Count the occurances of c in str
574: */
575: int
1.20 millert 576: charcount(char *str, int c)
1.1 deraadt 577: {
1.13 millert 578: char *cp;
579: int i;
1.1 deraadt 580:
581: for (i = 0, cp = str; *cp; cp++)
582: if (*cp == c)
583: i++;
584: return(i);
585: }
586:
587: /*
588: * Copy s1 to s2, return pointer to null in s2.
589: */
590: char *
1.20 millert 591: copy(char *s1, char *s2)
1.1 deraadt 592: {
593:
1.2 deraadt 594: while ((*s2++ = *s1++) != '\0')
1.1 deraadt 595: ;
1.5 millert 596: return(s2 - 1);
1.1 deraadt 597: }
598:
599: /*
600: * See if the given header field is supposed to be ignored.
601: */
602: int
1.20 millert 603: isign(char *field, struct ignoretab ignore[2])
1.1 deraadt 604: {
1.5 millert 605: char realfld[LINESIZE];
1.1 deraadt 606:
607: if (ignore == ignoreall)
1.5 millert 608: return(1);
1.1 deraadt 609: /*
610: * Lower-case the string, so that "Status" and "status"
611: * will hash to the same place.
612: */
1.20 millert 613: istrlcpy(realfld, field, sizeof(realfld));
1.1 deraadt 614: if (ignore[1].i_count > 0)
1.5 millert 615: return(!member(realfld, ignore + 1));
1.1 deraadt 616: else
1.5 millert 617: return(member(realfld, ignore));
1.1 deraadt 618: }
619:
620: int
1.20 millert 621: member(char *realfield, struct ignoretab *table)
1.1 deraadt 622: {
1.13 millert 623: struct ignore *igp;
1.1 deraadt 624:
625: for (igp = table->i_head[hash(realfield)]; igp != 0; igp = igp->i_link)
626: if (*igp->i_field == *realfield &&
627: equal(igp->i_field, realfield))
1.5 millert 628: return(1);
629: return(0);
1.14 millert 630: }
631:
632: void
1.20 millert 633: clearnew(void)
1.14 millert 634: {
635: struct message *mp;
636:
637: for (mp = &message[0]; mp < &message[msgCount]; mp++) {
638: if (mp->m_flag & MNEW) {
639: mp->m_flag &= ~MNEW;
640: mp->m_flag |= MSTATUS;
641: }
642: }
1.1 deraadt 643: }