Annotation of src/usr.bin/mail/names.c, Revision 1.13
1.13 ! millert 1: /* $OpenBSD: names.c,v 1.12 2000/08/23 21:24:08 mickey 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.13 ! millert 41: static char rcsid[] = "$OpenBSD: names.c,v 1.12 2000/08/23 21:24:08 mickey 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: {
1.9 millert 65: struct name *np;
1.1 deraadt 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: {
1.9 millert 82: struct name *np;
1.1 deraadt 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: {
1.9 millert 102: char *cp;
103: struct name *top, *np, *t;
1.8 millert 104: char *nbuf;
1.1 deraadt 105:
1.6 millert 106: if (line == NULL || *line == '\0')
1.4 millert 107: return(NIL);
1.8 millert 108: if ((nbuf = (char *)malloc(strlen(line) + 1)) == NULL)
1.9 millert 109: errx(1, "Out of memory");
1.1 deraadt 110: top = NIL;
111: np = NIL;
112: cp = line;
1.6 millert 113: while ((cp = yankword(cp, nbuf)) != NULL) {
1.1 deraadt 114: t = nalloc(nbuf, ntype);
115: if (top == NIL)
116: top = t;
117: else
118: np->n_flink = t;
119: t->n_blink = np;
120: np = t;
121: }
1.8 millert 122: (void)free(nbuf);
1.4 millert 123: return(top);
1.1 deraadt 124: }
125:
126: /*
127: * Turn a list of names into a string of the same names.
128: */
129: char *
130: detract(np, ntype)
1.9 millert 131: struct name *np;
1.1 deraadt 132: int ntype;
133: {
1.9 millert 134: int s, comma;
135: char *cp, *top;
136: struct name *p;
1.1 deraadt 137:
138: comma = ntype & GCOMMA;
139: if (np == NIL)
1.6 millert 140: return(NULL);
1.1 deraadt 141: ntype &= ~GCOMMA;
142: s = 0;
143: if (debug && comma)
1.4 millert 144: fputs("detract asked to insert commas\n", stderr);
1.1 deraadt 145: for (p = np; p != NIL; p = p->n_flink) {
146: if (ntype && (p->n_type & GMASK) != ntype)
147: continue;
148: s += strlen(p->n_name) + 1;
149: if (comma)
150: s++;
151: }
152: if (s == 0)
1.6 millert 153: return(NULL);
1.1 deraadt 154: s += 2;
155: top = salloc(s);
156: cp = top;
157: for (p = np; p != NIL; p = p->n_flink) {
158: if (ntype && (p->n_type & GMASK) != ntype)
159: continue;
160: cp = copy(p->n_name, cp);
161: if (comma && p->n_flink != NIL)
162: *cp++ = ',';
163: *cp++ = ' ';
164: }
165: *--cp = 0;
166: if (comma && *--cp == ',')
167: *cp = 0;
168: return(top);
169: }
170:
171: /*
172: * Grab a single word (liberal word)
173: * Throw away things between ()'s, and take anything between <>.
174: */
175: char *
176: yankword(ap, wbuf)
177: char *ap, wbuf[];
178: {
1.9 millert 179: char *cp, *cp2;
1.1 deraadt 180:
181: cp = ap;
182: for (;;) {
183: if (*cp == '\0')
1.6 millert 184: return(NULL);
1.1 deraadt 185: if (*cp == '(') {
1.9 millert 186: int nesting = 0;
1.1 deraadt 187:
188: while (*cp != '\0') {
189: switch (*cp++) {
190: case '(':
191: nesting++;
192: break;
193: case ')':
194: --nesting;
195: break;
196: }
197: if (nesting <= 0)
198: break;
199: }
200: } else if (*cp == ' ' || *cp == '\t' || *cp == ',')
201: cp++;
202: else
203: break;
204: }
205: if (*cp == '<')
206: for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';)
207: ;
208: else
1.3 millert 209: for (cp2 = wbuf; *cp && !strchr(" \t,(", *cp); *cp2++ = *cp++)
1.1 deraadt 210: ;
211: *cp2 = '\0';
1.4 millert 212: return(cp);
1.1 deraadt 213: }
214:
215: /*
216: * For each recipient in the passed name list with a /
217: * in the name, append the message to the end of the named file
218: * and remove him from the recipient list.
219: *
220: * Recipients whose name begins with | are piped through the given
221: * program and removed.
222: */
223: struct name *
224: outof(names, fo, hp)
225: struct name *names;
226: FILE *fo;
227: struct header *hp;
228: {
1.9 millert 229: int c, ispipe;
230: struct name *np, *top;
1.2 deraadt 231: time_t now;
232: char *date, *fname;
1.1 deraadt 233: FILE *fout, *fin;
234:
235: top = names;
236: np = names;
1.5 millert 237: (void)time(&now);
1.1 deraadt 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) {
1.7 millert 256: int fd;
257: char tempname[PATHSIZE];
258:
259: (void)snprintf(tempname, sizeof(tempname),
260: "%s/mail.ReXXXXXXXXXX", tmpdir);
261: if ((fd = mkstemp(tempname)) == -1 ||
262: (fout = Fdopen(fd, "a")) == NULL) {
1.11 millert 263: warn("%s", tempname);
1.1 deraadt 264: senderr++;
265: goto cant;
266: }
1.7 millert 267: image = open(tempname, O_RDWR);
268: (void)rm(tempname);
1.1 deraadt 269: if (image < 0) {
1.11 millert 270: warn("%s", tempname);
1.1 deraadt 271: senderr++;
1.4 millert 272: (void)Fclose(fout);
1.1 deraadt 273: goto cant;
274: }
1.5 millert 275: (void)fcntl(image, F_SETFD, 1);
1.1 deraadt 276: fprintf(fout, "From %s %s", myname, date);
277: puthead(hp, fout, GTO|GSUBJECT|GCC|GNL);
278: while ((c = getc(fo)) != EOF)
1.5 millert 279: (void)putc(c, fout);
1.1 deraadt 280: rewind(fo);
1.5 millert 281: (void)putc('\n', fout);
282: (void)fflush(fout);
1.1 deraadt 283: if (ferror(fout))
1.11 millert 284: warn("%s", tempname);
1.4 millert 285: (void)Fclose(fout);
1.1 deraadt 286: }
287:
288: /*
289: * Now either copy "image" to the desired file
290: * or give it as the standard input to the desired
291: * program as appropriate.
292: */
293:
294: if (ispipe) {
295: int pid;
296: char *shell;
1.2 deraadt 297: sigset_t nset;
1.1 deraadt 298:
299: /*
300: * XXX
301: * We can't really reuse the same image file,
302: * because multiple piped recipients will
303: * share the same lseek location and trample
304: * on one another.
305: */
1.13 ! millert 306: shell = value("SHELL");
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.11 millert 321: warn("%s", 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++;
1.11 millert 341: warn("%s", fname);
1.4 millert 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: {
1.9 millert 371: char *cp;
1.1 deraadt 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: {
1.9 millert 395: struct name *new, *np, *cp;
1.1 deraadt 396: struct grouphead *gh;
1.9 millert 397: int metoo;
1.1 deraadt 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: {
1.9 millert 477: struct name *tail;
1.1 deraadt 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 **
1.12 mickey 494: unpack(sm, np)
495: struct name *np, *sm;
1.1 deraadt 496: {
1.9 millert 497: char **ap, **top;
1.1 deraadt 498: int t, extra, metoo, verbose;
499:
1.12 mickey 500: if ((t = count(np)) == 0)
1.9 millert 501: errx(1, "No names to unpack");
1.12 mickey 502: t += count(sm);
503:
1.1 deraadt 504: /*
505: * Compute the number of extra arguments we will need.
1.10 millert 506: * We need at least four extra -- one for "send-mail", one for the
507: * "-i" flag, one for the "--" to signal end of command line
508: * arguments, and one for the terminating 0 pointer.
1.1 deraadt 509: */
1.10 millert 510: extra = 4;
1.6 millert 511: metoo = value("metoo") != NULL;
1.1 deraadt 512: if (metoo)
513: extra++;
1.6 millert 514: verbose = value("verbose") != NULL;
1.1 deraadt 515: if (verbose)
516: extra++;
1.5 millert 517: top = (char **)salloc((t + extra) * sizeof(*top));
1.1 deraadt 518: ap = top;
519: *ap++ = "send-mail";
520: *ap++ = "-i";
521: if (metoo)
522: *ap++ = "-m";
523: if (verbose)
524: *ap++ = "-v";
1.12 mickey 525: for (; sm != NIL; sm = sm->n_flink)
526: if ((sm->n_type & GDEL) == 0)
527: *ap++ = sm->n_name;
1.10 millert 528: *ap++ = "--";
1.12 mickey 529: for (; np != NIL; np = np->n_flink)
530: if ((np->n_type & GDEL) == 0)
531: *ap++ = np->n_name;
1.6 millert 532: *ap = NULL;
1.1 deraadt 533: return(top);
534: }
535:
536: /*
537: * Remove all of the duplicates from the passed name list by
538: * insertion sorting them, then checking for dups.
539: * Return the head of the new list.
540: */
541: struct name *
542: elide(names)
543: struct name *names;
544: {
1.9 millert 545: struct name *np, *t, *new;
1.1 deraadt 546: struct name *x;
547:
548: if (names == NIL)
549: return(NIL);
550: new = names;
551: np = names;
552: np = np->n_flink;
553: if (np != NIL)
554: np->n_blink = NIL;
555: new->n_flink = NIL;
556: while (np != NIL) {
557: t = new;
558: while (strcasecmp(t->n_name, np->n_name) < 0) {
559: if (t->n_flink == NIL)
560: break;
561: t = t->n_flink;
562: }
563:
564: /*
565: * If we ran out of t's, put the new entry after
566: * the current value of t.
567: */
568:
569: if (strcasecmp(t->n_name, np->n_name) < 0) {
570: t->n_flink = np;
571: np->n_blink = t;
572: t = np;
573: np = np->n_flink;
574: t->n_flink = NIL;
575: continue;
576: }
577:
578: /*
579: * Otherwise, put the new entry in front of the
580: * current t. If at the front of the list,
581: * the new guy becomes the new head of the list.
582: */
583:
584: if (t == new) {
585: t = np;
586: np = np->n_flink;
587: t->n_flink = new;
588: new->n_blink = t;
589: t->n_blink = NIL;
590: new = t;
591: continue;
592: }
593:
594: /*
595: * The normal case -- we are inserting into the
596: * middle of the list.
597: */
598:
599: x = np;
600: np = np->n_flink;
601: x->n_flink = t;
602: x->n_blink = t->n_blink;
603: t->n_blink->n_flink = x;
604: t->n_blink = x;
605: }
606:
607: /*
608: * Now the list headed up by new is sorted.
609: * Go through it and remove duplicates.
610: */
611:
612: np = new;
613: while (np != NIL) {
614: t = np;
615: while (t->n_flink != NIL &&
616: strcasecmp(np->n_name, t->n_flink->n_name) == 0)
617: t = t->n_flink;
618: if (t == np || t == NIL) {
619: np = np->n_flink;
620: continue;
621: }
622:
623: /*
624: * Now t points to the last entry with the same name
625: * as np. Make np point beyond t.
626: */
627:
628: np->n_flink = t->n_flink;
629: if (t->n_flink != NIL)
630: t->n_flink->n_blink = np;
631: np = np->n_flink;
632: }
633: return(new);
634: }
635:
636: /*
637: * Put another node onto a list of names and return
638: * the list.
639: */
640: struct name *
641: put(list, node)
642: struct name *list, *node;
643: {
644: node->n_flink = list;
645: node->n_blink = NIL;
646: if (list != NIL)
647: list->n_blink = node;
648: return(node);
649: }
650:
651: /*
652: * Determine the number of undeleted elements in
653: * a name list and return it.
654: */
655: int
656: count(np)
1.9 millert 657: struct name *np;
1.1 deraadt 658: {
1.9 millert 659: int c;
1.1 deraadt 660:
661: for (c = 0; np != NIL; np = np->n_flink)
662: if ((np->n_type & GDEL) == 0)
663: c++;
1.4 millert 664: return(c);
1.1 deraadt 665: }
666:
667: /*
668: * Delete the given name from a namelist.
669: */
670: struct name *
671: delname(np, name)
1.9 millert 672: struct name *np;
1.1 deraadt 673: char name[];
674: {
1.9 millert 675: struct name *p;
1.1 deraadt 676:
677: for (p = np; p != NIL; p = p->n_flink)
1.13 ! millert 678: if ((strcasecmp(p->n_name, name) == 0) ||
! 679: (value("allnet") &&
! 680: strncasecmp(p->n_name, name, strlen(name)) == 0 &&
! 681: *(p->n_name+strlen(name)) == '@')) {
1.1 deraadt 682: if (p->n_blink == NIL) {
683: if (p->n_flink != NIL)
684: p->n_flink->n_blink = NIL;
685: np = p->n_flink;
686: continue;
687: }
688: if (p->n_flink == NIL) {
689: if (p->n_blink != NIL)
690: p->n_blink->n_flink = NIL;
691: continue;
692: }
693: p->n_blink->n_flink = p->n_flink;
694: p->n_flink->n_blink = p->n_blink;
695: }
1.4 millert 696: return(np);
1.1 deraadt 697: }
698:
699: /*
700: * Pretty print a name list
701: * Uncomment it if you need it.
702: */
703:
704: /*
705: void
706: prettyprint(name)
707: struct name *name;
708: {
1.9 millert 709: struct name *np;
1.1 deraadt 710:
711: np = name;
712: while (np != NIL) {
713: fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
714: np = np->n_flink;
715: }
1.4 millert 716: putc('\n', stderr);
1.1 deraadt 717: }
718: */