Annotation of src/usr.bin/mail/names.c, Revision 1.4
1.4 ! millert 1: /* $OpenBSD: names.c,v 1.3 1997/01/17 07:12:50 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.4 ! millert 41: static char rcsid[] = "$OpenBSD: names.c,v 1.3 1997/01/17 07:12:50 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.4 ! 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:
106: if (line == NOSTR || *line == '\0')
1.4 ! millert 107: return(NIL);
1.1 deraadt 108: top = NIL;
109: np = NIL;
110: cp = line;
111: while ((cp = yankword(cp, nbuf)) != NOSTR) {
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)
138: return(NOSTR);
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)
151: return(NOSTR);
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.4 ! millert 182: return(NOSTR);
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: extern char *tempEdit;
234:
235: top = names;
236: np = names;
237: (void) time(&now);
238: date = ctime(&now);
239: while (np != NIL) {
240: if (!isfileaddr(np->n_name) && np->n_name[0] != '|') {
241: np = np->n_flink;
242: continue;
243: }
244: ispipe = np->n_name[0] == '|';
245: if (ispipe)
246: fname = np->n_name+1;
247: else
248: fname = expand(np->n_name);
249:
250: /*
251: * See if we have copied the complete message out yet.
252: * If not, do so.
253: */
254:
255: if (image < 0) {
256: if ((fout = Fopen(tempEdit, "a")) == NULL) {
1.4 ! millert 257: warn(tempEdit);
1.1 deraadt 258: senderr++;
259: goto cant;
260: }
261: image = open(tempEdit, 2);
262: (void) unlink(tempEdit);
263: if (image < 0) {
1.4 ! millert 264: warn(tempEdit);
1.1 deraadt 265: senderr++;
1.4 ! millert 266: (void)Fclose(fout);
1.1 deraadt 267: goto cant;
268: }
269: (void) fcntl(image, F_SETFD, 1);
270: fprintf(fout, "From %s %s", myname, date);
271: puthead(hp, fout, GTO|GSUBJECT|GCC|GNL);
272: while ((c = getc(fo)) != EOF)
273: (void) putc(c, fout);
274: rewind(fo);
275: (void) putc('\n', fout);
276: (void) fflush(fout);
277: if (ferror(fout))
1.4 ! millert 278: warn(tempEdit);
! 279: (void)Fclose(fout);
1.1 deraadt 280: }
281:
282: /*
283: * Now either copy "image" to the desired file
284: * or give it as the standard input to the desired
285: * program as appropriate.
286: */
287:
288: if (ispipe) {
289: int pid;
290: char *shell;
1.2 deraadt 291: sigset_t nset;
1.1 deraadt 292:
293: /*
294: * XXX
295: * We can't really reuse the same image file,
296: * because multiple piped recipients will
297: * share the same lseek location and trample
298: * on one another.
299: */
300: if ((shell = value("SHELL")) == NOSTR)
301: shell = _PATH_CSHELL;
1.2 deraadt 302: sigemptyset(&nset);
303: sigaddset(&nset, SIGHUP);
304: sigaddset(&nset, SIGINT);
305: sigaddset(&nset, SIGQUIT);
306: pid = start_command(shell, &nset,
1.1 deraadt 307: image, -1, "-c", fname, NOSTR);
308: if (pid < 0) {
309: senderr++;
310: goto cant;
311: }
312: free_child(pid);
313: } else {
314: int f;
315: if ((fout = Fopen(fname, "a")) == NULL) {
1.4 ! millert 316: warn(fname);
1.1 deraadt 317: senderr++;
318: goto cant;
319: }
320: if ((f = dup(image)) < 0) {
1.4 ! millert 321: warn("dup");
1.1 deraadt 322: fin = NULL;
323: } else
324: fin = Fdopen(f, "r");
325: if (fin == NULL) {
1.4 ! millert 326: fputs("Can't reopen image\n", stderr);
! 327: (void)Fclose(fout);
1.1 deraadt 328: senderr++;
329: goto cant;
330: }
331: rewind(fin);
332: while ((c = getc(fin)) != EOF)
333: (void) putc(c, fout);
1.4 ! millert 334: if (ferror(fout)) {
! 335: senderr++;
! 336: warn(fname);
! 337: }
! 338: (void)Fclose(fout);
! 339: (void)Fclose(fin);
1.1 deraadt 340: }
341: cant:
342: /*
343: * In days of old we removed the entry from the
344: * the list; now for sake of header expansion
345: * we leave it in and mark it as deleted.
346: */
347: np->n_type |= GDEL;
348: np = np->n_flink;
349: }
350: if (image >= 0) {
1.4 ! millert 351: (void)close(image);
1.1 deraadt 352: image = -1;
353: }
354: return(top);
355: }
356:
357: /*
358: * Determine if the passed address is a local "send to file" address.
359: * If any of the network metacharacters precedes any slashes, it can't
360: * be a filename. We cheat with .'s to allow path names like ./...
361: */
362: int
363: isfileaddr(name)
364: char *name;
365: {
366: register char *cp;
367:
368: if (*name == '+')
1.4 ! millert 369: return(1);
1.1 deraadt 370: for (cp = name; *cp; cp++) {
371: if (*cp == '!' || *cp == '%' || *cp == '@')
1.4 ! millert 372: return(0);
1.1 deraadt 373: if (*cp == '/')
1.4 ! millert 374: return(1);
1.1 deraadt 375: }
1.4 ! millert 376: return(0);
1.1 deraadt 377: }
378:
379: /*
380: * Map all of the aliased users in the invoker's mailrc
381: * file and insert them into the list.
382: * Changed after all these months of service to recursively
383: * expand names (2/14/80).
384: */
385:
386: struct name *
387: usermap(names)
388: struct name *names;
389: {
390: register struct name *new, *np, *cp;
391: struct grouphead *gh;
392: register int metoo;
393:
394: new = NIL;
395: np = names;
396: metoo = (value("metoo") != NOSTR);
397: while (np != NIL) {
398: if (np->n_name[0] == '\\') {
399: cp = np->n_flink;
400: new = put(new, np);
401: np = cp;
402: continue;
403: }
404: gh = findgroup(np->n_name);
405: cp = np->n_flink;
406: if (gh != NOGRP)
407: new = gexpand(new, gh, metoo, np->n_type);
408: else
409: new = put(new, np);
410: np = cp;
411: }
412: return(new);
413: }
414:
415: /*
416: * Recursively expand a group name. We limit the expansion to some
417: * fixed level to keep things from going haywire.
418: * Direct recursion is not expanded for convenience.
419: */
420:
421: struct name *
422: gexpand(nlist, gh, metoo, ntype)
423: struct name *nlist;
424: struct grouphead *gh;
425: int metoo, ntype;
426: {
427: struct group *gp;
428: struct grouphead *ngh;
429: struct name *np;
430: static int depth;
431: char *cp;
432:
433: if (depth > MAXEXP) {
434: printf("Expanding alias to depth larger than %d\n", MAXEXP);
435: return(nlist);
436: }
437: depth++;
438: for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link) {
439: cp = gp->ge_name;
440: if (*cp == '\\')
441: goto quote;
442: if (strcmp(cp, gh->g_name) == 0)
443: goto quote;
444: if ((ngh = findgroup(cp)) != NOGRP) {
445: nlist = gexpand(nlist, ngh, metoo, ntype);
446: continue;
447: }
448: quote:
449: np = nalloc(cp, ntype);
450: /*
451: * At this point should allow to expand
452: * to self if only person in group
453: */
454: if (gp == gh->g_list && gp->ge_link == NOGE)
455: goto skip;
456: if (!metoo && strcmp(cp, myname) == 0)
457: np->n_type |= GDEL;
458: skip:
459: nlist = put(nlist, np);
460: }
461: depth--;
462: return(nlist);
463: }
464:
465: /*
466: * Concatenate the two passed name lists, return the result.
467: */
468: struct name *
469: cat(n1, n2)
470: struct name *n1, *n2;
471: {
472: register struct name *tail;
473:
474: if (n1 == NIL)
475: return(n2);
476: if (n2 == NIL)
477: return(n1);
478: tail = tailof(n1);
479: tail->n_flink = n2;
480: n2->n_blink = tail;
481: return(n1);
482: }
483:
484: /*
485: * Unpack the name list onto a vector of strings.
486: * Return an error if the name list won't fit.
487: */
488: char **
489: unpack(np)
490: struct name *np;
491: {
492: register char **ap, **top;
493: register struct name *n;
494: int t, extra, metoo, verbose;
495:
496: n = np;
497: if ((t = count(n)) == 0)
498: panic("No names to unpack");
499: /*
500: * Compute the number of extra arguments we will need.
501: * We need at least two extra -- one for "mail" and one for
502: * the terminating 0 pointer. Additional spots may be needed
503: * to pass along -f to the host mailer.
504: */
505: extra = 2;
506: extra++;
507: metoo = value("metoo") != NOSTR;
508: if (metoo)
509: extra++;
510: verbose = value("verbose") != NOSTR;
511: if (verbose)
512: extra++;
1.4 ! millert 513: top = (char **) salloc((t + extra) * sizeof(*top));
1.1 deraadt 514: ap = top;
515: *ap++ = "send-mail";
516: *ap++ = "-i";
517: if (metoo)
518: *ap++ = "-m";
519: if (verbose)
520: *ap++ = "-v";
521: for (; n != NIL; n = n->n_flink)
522: if ((n->n_type & GDEL) == 0)
523: *ap++ = n->n_name;
524: *ap = NOSTR;
525: return(top);
526: }
527:
528: /*
529: * Remove all of the duplicates from the passed name list by
530: * insertion sorting them, then checking for dups.
531: * Return the head of the new list.
532: */
533: struct name *
534: elide(names)
535: struct name *names;
536: {
537: register struct name *np, *t, *new;
538: struct name *x;
539:
540: if (names == NIL)
541: return(NIL);
542: new = names;
543: np = names;
544: np = np->n_flink;
545: if (np != NIL)
546: np->n_blink = NIL;
547: new->n_flink = NIL;
548: while (np != NIL) {
549: t = new;
550: while (strcasecmp(t->n_name, np->n_name) < 0) {
551: if (t->n_flink == NIL)
552: break;
553: t = t->n_flink;
554: }
555:
556: /*
557: * If we ran out of t's, put the new entry after
558: * the current value of t.
559: */
560:
561: if (strcasecmp(t->n_name, np->n_name) < 0) {
562: t->n_flink = np;
563: np->n_blink = t;
564: t = np;
565: np = np->n_flink;
566: t->n_flink = NIL;
567: continue;
568: }
569:
570: /*
571: * Otherwise, put the new entry in front of the
572: * current t. If at the front of the list,
573: * the new guy becomes the new head of the list.
574: */
575:
576: if (t == new) {
577: t = np;
578: np = np->n_flink;
579: t->n_flink = new;
580: new->n_blink = t;
581: t->n_blink = NIL;
582: new = t;
583: continue;
584: }
585:
586: /*
587: * The normal case -- we are inserting into the
588: * middle of the list.
589: */
590:
591: x = np;
592: np = np->n_flink;
593: x->n_flink = t;
594: x->n_blink = t->n_blink;
595: t->n_blink->n_flink = x;
596: t->n_blink = x;
597: }
598:
599: /*
600: * Now the list headed up by new is sorted.
601: * Go through it and remove duplicates.
602: */
603:
604: np = new;
605: while (np != NIL) {
606: t = np;
607: while (t->n_flink != NIL &&
608: strcasecmp(np->n_name, t->n_flink->n_name) == 0)
609: t = t->n_flink;
610: if (t == np || t == NIL) {
611: np = np->n_flink;
612: continue;
613: }
614:
615: /*
616: * Now t points to the last entry with the same name
617: * as np. Make np point beyond t.
618: */
619:
620: np->n_flink = t->n_flink;
621: if (t->n_flink != NIL)
622: t->n_flink->n_blink = np;
623: np = np->n_flink;
624: }
625: return(new);
626: }
627:
628: /*
629: * Put another node onto a list of names and return
630: * the list.
631: */
632: struct name *
633: put(list, node)
634: struct name *list, *node;
635: {
636: node->n_flink = list;
637: node->n_blink = NIL;
638: if (list != NIL)
639: list->n_blink = node;
640: return(node);
641: }
642:
643: /*
644: * Determine the number of undeleted elements in
645: * a name list and return it.
646: */
647: int
648: count(np)
649: register struct name *np;
650: {
651: register int c;
652:
653: for (c = 0; np != NIL; np = np->n_flink)
654: if ((np->n_type & GDEL) == 0)
655: c++;
1.4 ! millert 656: return(c);
1.1 deraadt 657: }
658:
659: /*
660: * Delete the given name from a namelist.
661: */
662: struct name *
663: delname(np, name)
664: register struct name *np;
665: char name[];
666: {
667: register struct name *p;
668:
669: for (p = np; p != NIL; p = p->n_flink)
670: if (strcasecmp(p->n_name, name) == 0) {
671: if (p->n_blink == NIL) {
672: if (p->n_flink != NIL)
673: p->n_flink->n_blink = NIL;
674: np = p->n_flink;
675: continue;
676: }
677: if (p->n_flink == NIL) {
678: if (p->n_blink != NIL)
679: p->n_blink->n_flink = NIL;
680: continue;
681: }
682: p->n_blink->n_flink = p->n_flink;
683: p->n_flink->n_blink = p->n_blink;
684: }
1.4 ! millert 685: return(np);
1.1 deraadt 686: }
687:
688: /*
689: * Pretty print a name list
690: * Uncomment it if you need it.
691: */
692:
693: /*
694: void
695: prettyprint(name)
696: struct name *name;
697: {
698: register struct name *np;
699:
700: np = name;
701: while (np != NIL) {
702: fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
703: np = np->n_flink;
704: }
1.4 ! millert 705: putc('\n', stderr);
1.1 deraadt 706: }
707: */