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