Annotation of src/usr.bin/mail/names.c, Revision 1.11
1.11 ! millert 1: /* $OpenBSD: names.c,v 1.10 2000/03/23 19:32:13 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.11 ! millert 41: static char rcsid[] = "$OpenBSD: names.c,v 1.10 2000/03/23 19:32:13 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: {
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.6 millert 306: if ((shell = value("SHELL")) == NULL)
1.1 deraadt 307: shell = _PATH_CSHELL;
1.2 deraadt 308: sigemptyset(&nset);
309: sigaddset(&nset, SIGHUP);
310: sigaddset(&nset, SIGINT);
311: sigaddset(&nset, SIGQUIT);
312: pid = start_command(shell, &nset,
1.6 millert 313: image, -1, "-c", fname, NULL);
1.1 deraadt 314: if (pid < 0) {
315: senderr++;
316: goto cant;
317: }
318: free_child(pid);
319: } else {
320: int f;
321: if ((fout = Fopen(fname, "a")) == NULL) {
1.11 ! millert 322: warn("%s", fname);
1.1 deraadt 323: senderr++;
324: goto cant;
325: }
326: if ((f = dup(image)) < 0) {
1.4 millert 327: warn("dup");
1.1 deraadt 328: fin = NULL;
329: } else
330: fin = Fdopen(f, "r");
331: if (fin == NULL) {
1.4 millert 332: fputs("Can't reopen image\n", stderr);
333: (void)Fclose(fout);
1.1 deraadt 334: senderr++;
335: goto cant;
336: }
337: rewind(fin);
338: while ((c = getc(fin)) != EOF)
1.5 millert 339: (void)putc(c, fout);
1.4 millert 340: if (ferror(fout)) {
341: senderr++;
1.11 ! millert 342: warn("%s", fname);
1.4 millert 343: }
344: (void)Fclose(fout);
345: (void)Fclose(fin);
1.1 deraadt 346: }
347: cant:
348: /*
349: * In days of old we removed the entry from the
350: * the list; now for sake of header expansion
351: * we leave it in and mark it as deleted.
352: */
353: np->n_type |= GDEL;
354: np = np->n_flink;
355: }
356: if (image >= 0) {
1.4 millert 357: (void)close(image);
1.1 deraadt 358: image = -1;
359: }
360: return(top);
361: }
362:
363: /*
364: * Determine if the passed address is a local "send to file" address.
365: * If any of the network metacharacters precedes any slashes, it can't
366: * be a filename. We cheat with .'s to allow path names like ./...
367: */
368: int
369: isfileaddr(name)
370: char *name;
371: {
1.9 millert 372: char *cp;
1.1 deraadt 373:
374: if (*name == '+')
1.4 millert 375: return(1);
1.1 deraadt 376: for (cp = name; *cp; cp++) {
377: if (*cp == '!' || *cp == '%' || *cp == '@')
1.4 millert 378: return(0);
1.1 deraadt 379: if (*cp == '/')
1.4 millert 380: return(1);
1.1 deraadt 381: }
1.4 millert 382: return(0);
1.1 deraadt 383: }
384:
385: /*
386: * Map all of the aliased users in the invoker's mailrc
387: * file and insert them into the list.
388: * Changed after all these months of service to recursively
389: * expand names (2/14/80).
390: */
391:
392: struct name *
393: usermap(names)
394: struct name *names;
395: {
1.9 millert 396: struct name *new, *np, *cp;
1.1 deraadt 397: struct grouphead *gh;
1.9 millert 398: int metoo;
1.1 deraadt 399:
400: new = NIL;
401: np = names;
1.6 millert 402: metoo = (value("metoo") != NULL);
1.1 deraadt 403: while (np != NIL) {
404: if (np->n_name[0] == '\\') {
405: cp = np->n_flink;
406: new = put(new, np);
407: np = cp;
408: continue;
409: }
410: gh = findgroup(np->n_name);
411: cp = np->n_flink;
412: if (gh != NOGRP)
413: new = gexpand(new, gh, metoo, np->n_type);
414: else
415: new = put(new, np);
416: np = cp;
417: }
418: return(new);
419: }
420:
421: /*
422: * Recursively expand a group name. We limit the expansion to some
423: * fixed level to keep things from going haywire.
424: * Direct recursion is not expanded for convenience.
425: */
426:
427: struct name *
428: gexpand(nlist, gh, metoo, ntype)
429: struct name *nlist;
430: struct grouphead *gh;
431: int metoo, ntype;
432: {
433: struct group *gp;
434: struct grouphead *ngh;
435: struct name *np;
436: static int depth;
437: char *cp;
438:
439: if (depth > MAXEXP) {
440: printf("Expanding alias to depth larger than %d\n", MAXEXP);
441: return(nlist);
442: }
443: depth++;
444: for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link) {
445: cp = gp->ge_name;
446: if (*cp == '\\')
447: goto quote;
448: if (strcmp(cp, gh->g_name) == 0)
449: goto quote;
450: if ((ngh = findgroup(cp)) != NOGRP) {
451: nlist = gexpand(nlist, ngh, metoo, ntype);
452: continue;
453: }
454: quote:
455: np = nalloc(cp, ntype);
456: /*
457: * At this point should allow to expand
458: * to self if only person in group
459: */
460: if (gp == gh->g_list && gp->ge_link == NOGE)
461: goto skip;
462: if (!metoo && strcmp(cp, myname) == 0)
463: np->n_type |= GDEL;
464: skip:
465: nlist = put(nlist, np);
466: }
467: depth--;
468: return(nlist);
469: }
470:
471: /*
472: * Concatenate the two passed name lists, return the result.
473: */
474: struct name *
475: cat(n1, n2)
476: struct name *n1, *n2;
477: {
1.9 millert 478: struct name *tail;
1.1 deraadt 479:
480: if (n1 == NIL)
481: return(n2);
482: if (n2 == NIL)
483: return(n1);
484: tail = tailof(n1);
485: tail->n_flink = n2;
486: n2->n_blink = tail;
487: return(n1);
488: }
489:
490: /*
491: * Unpack the name list onto a vector of strings.
492: * Return an error if the name list won't fit.
493: */
494: char **
495: unpack(np)
496: struct name *np;
497: {
1.9 millert 498: char **ap, **top;
499: struct name *n;
1.1 deraadt 500: int t, extra, metoo, verbose;
501:
502: n = np;
503: if ((t = count(n)) == 0)
1.9 millert 504: errx(1, "No names to unpack");
1.1 deraadt 505: /*
506: * Compute the number of extra arguments we will need.
1.10 millert 507: * We need at least four extra -- one for "send-mail", one for the
508: * "-i" flag, one for the "--" to signal end of command line
509: * arguments, and one for the terminating 0 pointer.
1.1 deraadt 510: */
1.10 millert 511: extra = 4;
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";
1.10 millert 526: *ap++ = "--";
1.1 deraadt 527: for (; n != NIL; n = n->n_flink)
528: if ((n->n_type & GDEL) == 0)
529: *ap++ = n->n_name;
1.6 millert 530: *ap = NULL;
1.1 deraadt 531: return(top);
532: }
533:
534: /*
535: * Remove all of the duplicates from the passed name list by
536: * insertion sorting them, then checking for dups.
537: * Return the head of the new list.
538: */
539: struct name *
540: elide(names)
541: struct name *names;
542: {
1.9 millert 543: struct name *np, *t, *new;
1.1 deraadt 544: struct name *x;
545:
546: if (names == NIL)
547: return(NIL);
548: new = names;
549: np = names;
550: np = np->n_flink;
551: if (np != NIL)
552: np->n_blink = NIL;
553: new->n_flink = NIL;
554: while (np != NIL) {
555: t = new;
556: while (strcasecmp(t->n_name, np->n_name) < 0) {
557: if (t->n_flink == NIL)
558: break;
559: t = t->n_flink;
560: }
561:
562: /*
563: * If we ran out of t's, put the new entry after
564: * the current value of t.
565: */
566:
567: if (strcasecmp(t->n_name, np->n_name) < 0) {
568: t->n_flink = np;
569: np->n_blink = t;
570: t = np;
571: np = np->n_flink;
572: t->n_flink = NIL;
573: continue;
574: }
575:
576: /*
577: * Otherwise, put the new entry in front of the
578: * current t. If at the front of the list,
579: * the new guy becomes the new head of the list.
580: */
581:
582: if (t == new) {
583: t = np;
584: np = np->n_flink;
585: t->n_flink = new;
586: new->n_blink = t;
587: t->n_blink = NIL;
588: new = t;
589: continue;
590: }
591:
592: /*
593: * The normal case -- we are inserting into the
594: * middle of the list.
595: */
596:
597: x = np;
598: np = np->n_flink;
599: x->n_flink = t;
600: x->n_blink = t->n_blink;
601: t->n_blink->n_flink = x;
602: t->n_blink = x;
603: }
604:
605: /*
606: * Now the list headed up by new is sorted.
607: * Go through it and remove duplicates.
608: */
609:
610: np = new;
611: while (np != NIL) {
612: t = np;
613: while (t->n_flink != NIL &&
614: strcasecmp(np->n_name, t->n_flink->n_name) == 0)
615: t = t->n_flink;
616: if (t == np || t == NIL) {
617: np = np->n_flink;
618: continue;
619: }
620:
621: /*
622: * Now t points to the last entry with the same name
623: * as np. Make np point beyond t.
624: */
625:
626: np->n_flink = t->n_flink;
627: if (t->n_flink != NIL)
628: t->n_flink->n_blink = np;
629: np = np->n_flink;
630: }
631: return(new);
632: }
633:
634: /*
635: * Put another node onto a list of names and return
636: * the list.
637: */
638: struct name *
639: put(list, node)
640: struct name *list, *node;
641: {
642: node->n_flink = list;
643: node->n_blink = NIL;
644: if (list != NIL)
645: list->n_blink = node;
646: return(node);
647: }
648:
649: /*
650: * Determine the number of undeleted elements in
651: * a name list and return it.
652: */
653: int
654: count(np)
1.9 millert 655: struct name *np;
1.1 deraadt 656: {
1.9 millert 657: int c;
1.1 deraadt 658:
659: for (c = 0; np != NIL; np = np->n_flink)
660: if ((np->n_type & GDEL) == 0)
661: c++;
1.4 millert 662: return(c);
1.1 deraadt 663: }
664:
665: /*
666: * Delete the given name from a namelist.
667: */
668: struct name *
669: delname(np, name)
1.9 millert 670: struct name *np;
1.1 deraadt 671: char name[];
672: {
1.9 millert 673: struct name *p;
1.1 deraadt 674:
675: for (p = np; p != NIL; p = p->n_flink)
676: if (strcasecmp(p->n_name, name) == 0) {
677: if (p->n_blink == NIL) {
678: if (p->n_flink != NIL)
679: p->n_flink->n_blink = NIL;
680: np = p->n_flink;
681: continue;
682: }
683: if (p->n_flink == NIL) {
684: if (p->n_blink != NIL)
685: p->n_blink->n_flink = NIL;
686: continue;
687: }
688: p->n_blink->n_flink = p->n_flink;
689: p->n_flink->n_blink = p->n_blink;
690: }
1.4 millert 691: return(np);
1.1 deraadt 692: }
693:
694: /*
695: * Pretty print a name list
696: * Uncomment it if you need it.
697: */
698:
699: /*
700: void
701: prettyprint(name)
702: struct name *name;
703: {
1.9 millert 704: struct name *np;
1.1 deraadt 705:
706: np = name;
707: while (np != NIL) {
708: fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
709: np = np->n_flink;
710: }
1.4 millert 711: putc('\n', stderr);
1.1 deraadt 712: }
713: */