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