Annotation of src/usr.bin/mail/names.c, Revision 1.17
1.17 ! millert 1: /* $OpenBSD: names.c,v 1.16 2001/11/21 20:41:55 millert Exp $ */
1.2 deraadt 2: /* $NetBSD: names.c,v 1.5 1996/06/08 19:48:32 christos Exp $ */
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.17 ! 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: #ifndef lint
1.2 deraadt 34: #if 0
1.14 millert 35: static const char sccsid[] = "@(#)names.c 8.1 (Berkeley) 6/6/93";
1.2 deraadt 36: #else
1.17 ! millert 37: static const char rcsid[] = "$OpenBSD: names.c,v 1.16 2001/11/21 20:41:55 millert Exp $";
1.2 deraadt 38: #endif
1.1 deraadt 39: #endif /* not lint */
40:
41: /*
42: * Mail -- a mail program
43: *
44: * Handle name lists.
45: */
46:
47: #include "rcv.h"
48: #include <fcntl.h>
49: #include "extern.h"
50:
51: /*
52: * Allocate a single element of a name list,
53: * initialize its name field to the passed
54: * name and return it.
55: */
56: struct name *
1.14 millert 57: nalloc(char *str, int ntype)
1.1 deraadt 58: {
1.9 millert 59: struct name *np;
1.1 deraadt 60:
1.5 millert 61: np = (struct name *)salloc(sizeof(*np));
1.14 millert 62: np->n_flink = NULL;
63: np->n_blink = NULL;
1.1 deraadt 64: np->n_type = ntype;
65: np->n_name = savestr(str);
66: return(np);
67: }
68:
69: /*
70: * Find the tail of a list and return it.
71: */
72: struct name *
1.14 millert 73: tailof(struct name *name)
1.1 deraadt 74: {
1.9 millert 75: struct name *np;
1.1 deraadt 76:
77: np = name;
1.14 millert 78: if (np == NULL)
79: return(NULL);
80: while (np->n_flink != NULL)
1.1 deraadt 81: np = np->n_flink;
82: return(np);
83: }
84:
85: /*
86: * Extract a list of names from a line,
87: * and make a list of names from it.
1.14 millert 88: * Return the list or NULL if none found.
1.1 deraadt 89: */
90: struct name *
1.14 millert 91: extract(char *line, int ntype)
1.1 deraadt 92: {
1.9 millert 93: char *cp;
94: struct name *top, *np, *t;
1.8 millert 95: char *nbuf;
1.1 deraadt 96:
1.6 millert 97: if (line == NULL || *line == '\0')
1.14 millert 98: return(NULL);
1.8 millert 99: if ((nbuf = (char *)malloc(strlen(line) + 1)) == NULL)
1.9 millert 100: errx(1, "Out of memory");
1.14 millert 101: top = NULL;
102: np = NULL;
1.1 deraadt 103: cp = line;
1.6 millert 104: while ((cp = yankword(cp, nbuf)) != NULL) {
1.1 deraadt 105: t = nalloc(nbuf, ntype);
1.14 millert 106: if (top == NULL)
1.1 deraadt 107: top = t;
108: else
109: np->n_flink = t;
110: t->n_blink = np;
111: np = t;
112: }
1.8 millert 113: (void)free(nbuf);
1.4 millert 114: return(top);
1.1 deraadt 115: }
116:
117: /*
118: * Turn a list of names into a string of the same names.
119: */
120: char *
1.14 millert 121: detract(struct name *np, int ntype)
1.1 deraadt 122: {
1.9 millert 123: int s, comma;
124: char *cp, *top;
125: struct name *p;
1.1 deraadt 126:
127: comma = ntype & GCOMMA;
1.14 millert 128: if (np == NULL)
1.6 millert 129: return(NULL);
1.1 deraadt 130: ntype &= ~GCOMMA;
131: s = 0;
132: if (debug && comma)
1.4 millert 133: fputs("detract asked to insert commas\n", stderr);
1.14 millert 134: for (p = np; p != NULL; p = p->n_flink) {
1.1 deraadt 135: if (ntype && (p->n_type & GMASK) != ntype)
136: continue;
137: s += strlen(p->n_name) + 1;
138: if (comma)
139: s++;
140: }
141: if (s == 0)
1.6 millert 142: return(NULL);
1.1 deraadt 143: s += 2;
144: top = salloc(s);
145: cp = top;
1.14 millert 146: for (p = np; p != NULL; p = p->n_flink) {
1.1 deraadt 147: if (ntype && (p->n_type & GMASK) != ntype)
148: continue;
149: cp = copy(p->n_name, cp);
1.14 millert 150: if (comma && p->n_flink != NULL)
1.1 deraadt 151: *cp++ = ',';
152: *cp++ = ' ';
153: }
154: *--cp = 0;
155: if (comma && *--cp == ',')
156: *cp = 0;
157: return(top);
158: }
159:
160: /*
161: * Grab a single word (liberal word)
162: * Throw away things between ()'s, and take anything between <>.
163: */
164: char *
1.14 millert 165: yankword(char *ap, char *wbuf)
1.1 deraadt 166: {
1.9 millert 167: char *cp, *cp2;
1.1 deraadt 168:
169: cp = ap;
170: for (;;) {
171: if (*cp == '\0')
1.6 millert 172: return(NULL);
1.1 deraadt 173: if (*cp == '(') {
1.9 millert 174: int nesting = 0;
1.1 deraadt 175:
176: while (*cp != '\0') {
177: switch (*cp++) {
178: case '(':
179: nesting++;
180: break;
181: case ')':
182: --nesting;
183: break;
184: }
185: if (nesting <= 0)
186: break;
187: }
188: } else if (*cp == ' ' || *cp == '\t' || *cp == ',')
189: cp++;
190: else
191: break;
192: }
193: if (*cp == '<')
194: for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';)
195: ;
196: else
1.3 millert 197: for (cp2 = wbuf; *cp && !strchr(" \t,(", *cp); *cp2++ = *cp++)
1.1 deraadt 198: ;
199: *cp2 = '\0';
1.4 millert 200: return(cp);
1.1 deraadt 201: }
202:
203: /*
204: * For each recipient in the passed name list with a /
205: * in the name, append the message to the end of the named file
206: * and remove him from the recipient list.
207: *
208: * Recipients whose name begins with | are piped through the given
209: * program and removed.
210: */
211: struct name *
1.14 millert 212: outof(struct name *names, FILE *fo, struct header *hp)
1.1 deraadt 213: {
1.9 millert 214: int c, ispipe;
215: struct name *np, *top;
1.2 deraadt 216: time_t now;
217: char *date, *fname;
1.1 deraadt 218: FILE *fout, *fin;
219:
220: top = names;
221: np = names;
1.5 millert 222: (void)time(&now);
1.1 deraadt 223: date = ctime(&now);
1.14 millert 224: while (np != NULL) {
1.1 deraadt 225: if (!isfileaddr(np->n_name) && np->n_name[0] != '|') {
226: np = np->n_flink;
227: continue;
228: }
229: ispipe = np->n_name[0] == '|';
230: if (ispipe)
231: fname = np->n_name+1;
232: else
233: fname = expand(np->n_name);
234:
235: /*
236: * See if we have copied the complete message out yet.
237: * If not, do so.
238: */
239: if (image < 0) {
1.7 millert 240: int fd;
241: char tempname[PATHSIZE];
242:
243: (void)snprintf(tempname, sizeof(tempname),
244: "%s/mail.ReXXXXXXXXXX", tmpdir);
245: if ((fd = mkstemp(tempname)) == -1 ||
246: (fout = Fdopen(fd, "a")) == NULL) {
1.11 millert 247: warn("%s", tempname);
1.1 deraadt 248: senderr++;
249: goto cant;
250: }
1.7 millert 251: image = open(tempname, O_RDWR);
252: (void)rm(tempname);
1.1 deraadt 253: if (image < 0) {
1.11 millert 254: warn("%s", tempname);
1.1 deraadt 255: senderr++;
1.4 millert 256: (void)Fclose(fout);
1.1 deraadt 257: goto cant;
258: }
1.5 millert 259: (void)fcntl(image, F_SETFD, 1);
1.1 deraadt 260: fprintf(fout, "From %s %s", myname, date);
261: puthead(hp, fout, GTO|GSUBJECT|GCC|GNL);
262: while ((c = getc(fo)) != EOF)
1.5 millert 263: (void)putc(c, fout);
1.1 deraadt 264: rewind(fo);
1.5 millert 265: (void)putc('\n', fout);
266: (void)fflush(fout);
1.1 deraadt 267: if (ferror(fout))
1.11 millert 268: warn("%s", tempname);
1.4 millert 269: (void)Fclose(fout);
1.1 deraadt 270: }
271:
272: /*
273: * Now either copy "image" to the desired file
274: * or give it as the standard input to the desired
275: * program as appropriate.
276: */
277: if (ispipe) {
1.14 millert 278: pid_t pid;
1.1 deraadt 279: char *shell;
1.2 deraadt 280: sigset_t nset;
1.1 deraadt 281:
282: /*
283: * XXX
284: * We can't really reuse the same image file,
285: * because multiple piped recipients will
286: * share the same lseek location and trample
287: * on one another.
288: */
1.13 millert 289: shell = value("SHELL");
1.2 deraadt 290: sigemptyset(&nset);
291: sigaddset(&nset, SIGHUP);
292: sigaddset(&nset, SIGINT);
293: sigaddset(&nset, SIGQUIT);
294: pid = start_command(shell, &nset,
1.6 millert 295: image, -1, "-c", fname, NULL);
1.1 deraadt 296: if (pid < 0) {
297: senderr++;
298: goto cant;
299: }
300: free_child(pid);
301: } else {
302: int f;
303: if ((fout = Fopen(fname, "a")) == NULL) {
1.11 millert 304: warn("%s", fname);
1.1 deraadt 305: senderr++;
306: goto cant;
307: }
308: if ((f = dup(image)) < 0) {
1.4 millert 309: warn("dup");
1.1 deraadt 310: fin = NULL;
311: } else
312: fin = Fdopen(f, "r");
313: if (fin == NULL) {
1.4 millert 314: fputs("Can't reopen image\n", stderr);
315: (void)Fclose(fout);
1.1 deraadt 316: senderr++;
317: goto cant;
318: }
319: rewind(fin);
320: while ((c = getc(fin)) != EOF)
1.5 millert 321: (void)putc(c, fout);
1.4 millert 322: if (ferror(fout)) {
323: senderr++;
1.11 millert 324: warn("%s", fname);
1.4 millert 325: }
326: (void)Fclose(fout);
327: (void)Fclose(fin);
1.1 deraadt 328: }
329: cant:
330: /*
331: * In days of old we removed the entry from the
332: * the list; now for sake of header expansion
333: * we leave it in and mark it as deleted.
334: */
335: np->n_type |= GDEL;
336: np = np->n_flink;
337: }
338: if (image >= 0) {
1.4 millert 339: (void)close(image);
1.1 deraadt 340: image = -1;
341: }
342: return(top);
343: }
344:
345: /*
346: * Determine if the passed address is a local "send to file" address.
347: * If any of the network metacharacters precedes any slashes, it can't
348: * be a filename. We cheat with .'s to allow path names like ./...
349: */
350: int
1.14 millert 351: isfileaddr(char *name)
1.1 deraadt 352: {
1.9 millert 353: char *cp;
1.1 deraadt 354:
355: if (*name == '+')
1.4 millert 356: return(1);
1.1 deraadt 357: for (cp = name; *cp; cp++) {
358: if (*cp == '!' || *cp == '%' || *cp == '@')
1.4 millert 359: return(0);
1.1 deraadt 360: if (*cp == '/')
1.4 millert 361: return(1);
1.1 deraadt 362: }
1.4 millert 363: return(0);
1.1 deraadt 364: }
365:
366: /*
367: * Map all of the aliased users in the invoker's mailrc
368: * file and insert them into the list.
369: * Changed after all these months of service to recursively
370: * expand names (2/14/80).
371: */
372: struct name *
1.14 millert 373: usermap(struct name *names)
1.1 deraadt 374: {
1.9 millert 375: struct name *new, *np, *cp;
1.1 deraadt 376: struct grouphead *gh;
1.9 millert 377: int metoo;
1.1 deraadt 378:
1.14 millert 379: new = NULL;
1.1 deraadt 380: np = names;
1.6 millert 381: metoo = (value("metoo") != NULL);
1.14 millert 382: while (np != NULL) {
1.1 deraadt 383: if (np->n_name[0] == '\\') {
384: cp = np->n_flink;
385: new = put(new, np);
386: np = cp;
387: continue;
388: }
389: gh = findgroup(np->n_name);
390: cp = np->n_flink;
1.14 millert 391: if (gh != NULL)
1.1 deraadt 392: new = gexpand(new, gh, metoo, np->n_type);
393: else
394: new = put(new, np);
395: np = cp;
396: }
397: return(new);
398: }
399:
400: /*
401: * Recursively expand a group name. We limit the expansion to some
402: * fixed level to keep things from going haywire.
403: * Direct recursion is not expanded for convenience.
404: */
405: struct name *
1.14 millert 406: gexpand(struct name *nlist, struct grouphead *gh, int metoo, int ntype)
1.1 deraadt 407: {
408: struct group *gp;
409: struct grouphead *ngh;
410: struct name *np;
411: static int depth;
412: char *cp;
413:
414: if (depth > MAXEXP) {
415: printf("Expanding alias to depth larger than %d\n", MAXEXP);
416: return(nlist);
417: }
418: depth++;
1.14 millert 419: for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) {
1.1 deraadt 420: cp = gp->ge_name;
421: if (*cp == '\\')
422: goto quote;
423: if (strcmp(cp, gh->g_name) == 0)
424: goto quote;
1.14 millert 425: if ((ngh = findgroup(cp)) != NULL) {
1.1 deraadt 426: nlist = gexpand(nlist, ngh, metoo, ntype);
427: continue;
428: }
429: quote:
430: np = nalloc(cp, ntype);
431: /*
432: * At this point should allow to expand
433: * to self if only person in group
434: */
1.14 millert 435: if (gp == gh->g_list && gp->ge_link == NULL)
1.1 deraadt 436: goto skip;
437: if (!metoo && strcmp(cp, myname) == 0)
438: np->n_type |= GDEL;
439: skip:
440: nlist = put(nlist, np);
441: }
442: depth--;
443: return(nlist);
444: }
445:
446: /*
447: * Concatenate the two passed name lists, return the result.
448: */
449: struct name *
1.14 millert 450: cat(struct name *n1, struct name *n2)
1.1 deraadt 451: {
1.9 millert 452: struct name *tail;
1.1 deraadt 453:
1.14 millert 454: if (n1 == NULL)
1.1 deraadt 455: return(n2);
1.14 millert 456: if (n2 == NULL)
1.1 deraadt 457: return(n1);
458: tail = tailof(n1);
459: tail->n_flink = n2;
460: n2->n_blink = tail;
461: return(n1);
462: }
463:
464: /*
465: * Unpack the name list onto a vector of strings.
466: * Return an error if the name list won't fit.
467: */
468: char **
1.15 millert 469: unpack(struct name *sm, struct name *np)
1.1 deraadt 470: {
1.9 millert 471: char **ap, **top;
1.1 deraadt 472: int t, extra, metoo, verbose;
473:
1.12 mickey 474: if ((t = count(np)) == 0)
1.9 millert 475: errx(1, "No names to unpack");
1.12 mickey 476: t += count(sm);
477:
1.1 deraadt 478: /*
479: * Compute the number of extra arguments we will need.
1.10 millert 480: * We need at least four extra -- one for "send-mail", one for the
481: * "-i" flag, one for the "--" to signal end of command line
482: * arguments, and one for the terminating 0 pointer.
1.1 deraadt 483: */
1.10 millert 484: extra = 4;
1.6 millert 485: metoo = value("metoo") != NULL;
1.1 deraadt 486: if (metoo)
487: extra++;
1.6 millert 488: verbose = value("verbose") != NULL;
1.1 deraadt 489: if (verbose)
490: extra++;
1.5 millert 491: top = (char **)salloc((t + extra) * sizeof(*top));
1.1 deraadt 492: ap = top;
493: *ap++ = "send-mail";
494: *ap++ = "-i";
495: if (metoo)
496: *ap++ = "-m";
497: if (verbose)
498: *ap++ = "-v";
1.14 millert 499: for (; sm != NULL; sm = sm->n_flink)
1.12 mickey 500: if ((sm->n_type & GDEL) == 0)
501: *ap++ = sm->n_name;
1.10 millert 502: *ap++ = "--";
1.14 millert 503: for (; np != NULL; np = np->n_flink)
1.12 mickey 504: if ((np->n_type & GDEL) == 0)
505: *ap++ = np->n_name;
1.6 millert 506: *ap = NULL;
1.1 deraadt 507: return(top);
508: }
509:
510: /*
511: * Remove all of the duplicates from the passed name list by
512: * insertion sorting them, then checking for dups.
513: * Return the head of the new list.
514: */
515: struct name *
1.14 millert 516: elide(struct name *names)
1.1 deraadt 517: {
1.9 millert 518: struct name *np, *t, *new;
1.1 deraadt 519: struct name *x;
520:
1.14 millert 521: if (names == NULL)
522: return(NULL);
1.1 deraadt 523: new = names;
524: np = names;
525: np = np->n_flink;
1.14 millert 526: if (np != NULL)
527: np->n_blink = NULL;
528: new->n_flink = NULL;
529: while (np != NULL) {
1.1 deraadt 530: t = new;
531: while (strcasecmp(t->n_name, np->n_name) < 0) {
1.14 millert 532: if (t->n_flink == NULL)
1.1 deraadt 533: break;
534: t = t->n_flink;
535: }
536:
537: /*
538: * If we ran out of t's, put the new entry after
539: * the current value of t.
540: */
541: if (strcasecmp(t->n_name, np->n_name) < 0) {
542: t->n_flink = np;
543: np->n_blink = t;
544: t = np;
545: np = np->n_flink;
1.14 millert 546: t->n_flink = NULL;
1.1 deraadt 547: continue;
548: }
549:
550: /*
551: * Otherwise, put the new entry in front of the
552: * current t. If at the front of the list,
553: * the new guy becomes the new head of the list.
554: */
555: if (t == new) {
556: t = np;
557: np = np->n_flink;
558: t->n_flink = new;
559: new->n_blink = t;
1.14 millert 560: t->n_blink = NULL;
1.1 deraadt 561: new = t;
562: continue;
563: }
564:
565: /*
566: * The normal case -- we are inserting into the
567: * middle of the list.
568: */
569: x = np;
570: np = np->n_flink;
571: x->n_flink = t;
572: x->n_blink = t->n_blink;
573: t->n_blink->n_flink = x;
574: t->n_blink = x;
575: }
576:
577: /*
578: * Now the list headed up by new is sorted.
579: * Go through it and remove duplicates.
580: */
581: np = new;
1.14 millert 582: while (np != NULL) {
1.1 deraadt 583: t = np;
1.14 millert 584: while (t->n_flink != NULL &&
1.1 deraadt 585: strcasecmp(np->n_name, t->n_flink->n_name) == 0)
586: t = t->n_flink;
1.14 millert 587: if (t == np || t == NULL) {
1.1 deraadt 588: np = np->n_flink;
589: continue;
590: }
591:
592: /*
593: * Now t points to the last entry with the same name
594: * as np. Make np point beyond t.
595: */
596: np->n_flink = t->n_flink;
1.14 millert 597: if (t->n_flink != NULL)
1.1 deraadt 598: t->n_flink->n_blink = np;
599: np = np->n_flink;
600: }
601: return(new);
602: }
603:
604: /*
605: * Put another node onto a list of names and return
606: * the list.
607: */
608: struct name *
1.14 millert 609: put(struct name *list, struct name *node)
1.1 deraadt 610: {
611: node->n_flink = list;
1.14 millert 612: node->n_blink = NULL;
613: if (list != NULL)
1.1 deraadt 614: list->n_blink = node;
615: return(node);
616: }
617:
618: /*
619: * Determine the number of undeleted elements in
620: * a name list and return it.
621: */
622: int
1.14 millert 623: count(struct name *np)
1.1 deraadt 624: {
1.9 millert 625: int c;
1.1 deraadt 626:
1.14 millert 627: for (c = 0; np != NULL; np = np->n_flink)
1.1 deraadt 628: if ((np->n_type & GDEL) == 0)
629: c++;
1.4 millert 630: return(c);
1.1 deraadt 631: }
632:
633: /*
634: * Delete the given name from a namelist.
635: */
636: struct name *
1.14 millert 637: delname(struct name *np, char *name)
1.1 deraadt 638: {
1.9 millert 639: struct name *p;
1.1 deraadt 640:
1.14 millert 641: for (p = np; p != NULL; p = p->n_flink)
1.13 millert 642: if ((strcasecmp(p->n_name, name) == 0) ||
643: (value("allnet") &&
644: strncasecmp(p->n_name, name, strlen(name)) == 0 &&
645: *(p->n_name+strlen(name)) == '@')) {
1.14 millert 646: if (p->n_blink == NULL) {
647: if (p->n_flink != NULL)
648: p->n_flink->n_blink = NULL;
1.1 deraadt 649: np = p->n_flink;
650: continue;
651: }
1.14 millert 652: if (p->n_flink == NULL) {
653: if (p->n_blink != NULL)
654: p->n_blink->n_flink = NULL;
1.1 deraadt 655: continue;
656: }
657: p->n_blink->n_flink = p->n_flink;
658: p->n_flink->n_blink = p->n_blink;
659: }
1.4 millert 660: return(np);
1.1 deraadt 661: }
662:
663: /*
664: * Pretty print a name list
665: * Uncomment it if you need it.
666: */
1.14 millert 667: #if 0
1.1 deraadt 668: void
1.16 millert 669: prettyprint(struct name *name)
1.1 deraadt 670: {
1.9 millert 671: struct name *np;
1.1 deraadt 672:
673: np = name;
1.14 millert 674: while (np != NULL) {
1.1 deraadt 675: fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
676: np = np->n_flink;
677: }
1.4 millert 678: putc('\n', stderr);
1.1 deraadt 679: }
1.14 millert 680: #endif