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