xref: /dragonfly/usr.bin/mail/names.c (revision 9bb2a92d)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)names.c	8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.bin/mail/names.c,v 1.4.6.3 2003/01/06 05:46:03 mikeh Exp $
35  * $DragonFly: src/usr.bin/mail/names.c,v 1.3 2003/10/04 20:36:48 hmp Exp $
36  */
37 
38 /*
39  * Mail -- a mail program
40  *
41  * Handle name lists.
42  */
43 
44 #include "rcv.h"
45 #include <fcntl.h>
46 #include "extern.h"
47 
48 /*
49  * Allocate a single element of a name list,
50  * initialize its name field to the passed
51  * name and return it.
52  */
53 struct name *
54 nalloc(char *str, int ntype)
55 {
56 	struct name *np;
57 
58 	np = (struct name *)salloc(sizeof(*np));
59 	np->n_flink = NULL;
60 	np->n_blink = NULL;
61 	np->n_type = ntype;
62 	np->n_name = savestr(str);
63 	return (np);
64 }
65 
66 /*
67  * Find the tail of a list and return it.
68  */
69 struct name *
70 tailof(struct name *name)
71 {
72 	struct name *np;
73 
74 	np = name;
75 	if (np == NULL)
76 		return (NULL);
77 	while (np->n_flink != NULL)
78 		np = np->n_flink;
79 	return (np);
80 }
81 
82 /*
83  * Extract a list of names from a line,
84  * and make a list of names from it.
85  * Return the list or NULL if none found.
86  */
87 struct name *
88 extract(char *line, int ntype)
89 {
90 	char *cp, *nbuf;
91 	struct name *top, *np, *t;
92 
93 	if (line == NULL || *line == '\0')
94 		return (NULL);
95 	if ((nbuf = malloc(strlen(line) + 1)) == NULL)
96 		err(1, "Out of memory");
97 	top = NULL;
98 	np = NULL;
99 	cp = line;
100 	while ((cp = yankword(cp, nbuf)) != NULL) {
101 		t = nalloc(nbuf, ntype);
102 		if (top == NULL)
103 			top = t;
104 		else
105 			np->n_flink = t;
106 		t->n_blink = np;
107 		np = t;
108 	}
109 	(void)free(nbuf);
110 	return (top);
111 }
112 
113 /*
114  * Turn a list of names into a string of the same names.
115  */
116 char *
117 detract(struct name *np, int ntype)
118 {
119 	int s, comma;
120 	char *cp, *top;
121 	struct name *p;
122 
123 	comma = ntype & GCOMMA;
124 	if (np == NULL)
125 		return (NULL);
126 	ntype &= ~GCOMMA;
127 	s = 0;
128 	if (debug && comma)
129 		fprintf(stderr, "detract asked to insert commas\n");
130 	for (p = np; p != NULL; p = p->n_flink) {
131 		if (ntype && (p->n_type & GMASK) != ntype)
132 			continue;
133 		s += strlen(p->n_name) + 1;
134 		if (comma)
135 			s++;
136 	}
137 	if (s == 0)
138 		return (NULL);
139 	s += 2;
140 	top = salloc(s);
141 	cp = top;
142 	for (p = np; p != NULL; p = p->n_flink) {
143 		if (ntype && (p->n_type & GMASK) != ntype)
144 			continue;
145 		cp += strlcpy(cp, p->n_name, strlen(p->n_name) + 1);
146 		if (comma && p->n_flink != NULL)
147 			*cp++ = ',';
148 		*cp++ = ' ';
149 	}
150 	*--cp = '\0';
151 	if (comma && *--cp == ',')
152 		*cp = '\0';
153 	return (top);
154 }
155 
156 /*
157  * Grab a single word (liberal word)
158  * Throw away things between ()'s, and take anything between <>.
159  */
160 char *
161 yankword(char *ap, char *wbuf)
162 {
163 	char *cp, *cp2;
164 
165 	cp = ap;
166 	for (;;) {
167 		if (*cp == '\0')
168 			return (NULL);
169 		if (*cp == '(') {
170 			int nesting = 0;
171 
172 			while (*cp != '\0') {
173 				switch (*cp++) {
174 				case '(':
175 					nesting++;
176 					break;
177 				case ')':
178 					--nesting;
179 					break;
180 				}
181 				if (nesting <= 0)
182 					break;
183 			}
184 		} else if (*cp == ' ' || *cp == '\t' || *cp == ',')
185 			cp++;
186 		else
187 			break;
188 	}
189 	if (*cp ==  '<')
190 		for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';)
191 			;
192 	else
193 		for (cp2 = wbuf; *cp != '\0' && strchr(" \t,(", *cp) == NULL;
194 		    *cp2++ = *cp++)
195 			;
196 	*cp2 = '\0';
197 	return (cp);
198 }
199 
200 /*
201  * For each recipient in the passed name list with a /
202  * in the name, append the message to the end of the named file
203  * and remove him from the recipient list.
204  *
205  * Recipients whose name begins with | are piped through the given
206  * program and removed.
207  */
208 struct name *
209 outof(struct name *names, FILE *fo, struct header *hp)
210 {
211 	int c, ispipe;
212 	struct name *np, *top;
213 	time_t now;
214 	char *date, *fname;
215 	FILE *fout, *fin;
216 
217 	top = names;
218 	np = names;
219 	(void)time(&now);
220 	date = ctime(&now);
221 	while (np != NULL) {
222 		if (!isfileaddr(np->n_name) && np->n_name[0] != '|') {
223 			np = np->n_flink;
224 			continue;
225 		}
226 		ispipe = np->n_name[0] == '|';
227 		if (ispipe)
228 			fname = np->n_name+1;
229 		else
230 			fname = expand(np->n_name);
231 
232 		/*
233 		 * See if we have copied the complete message out yet.
234 		 * If not, do so.
235 		 */
236 
237 		if (image < 0) {
238 			int fd;
239 			char tempname[PATHSIZE];
240 
241 			(void)snprintf(tempname, sizeof(tempname),
242 			    "%s/mail.ReXXXXXXXXXX", tmpdir);
243 			if ((fd = mkstemp(tempname)) == -1 ||
244 			    (fout = Fdopen(fd, "a")) == NULL) {
245 				warn("%s", tempname);
246 				senderr++;
247 				goto cant;
248 			}
249 			image = open(tempname, O_RDWR);
250 			(void)rm(tempname);
251 			if (image < 0) {
252 				warn("%s", tempname);
253 				senderr++;
254 				(void)Fclose(fout);
255 				goto cant;
256 			}
257 			(void)fcntl(image, F_SETFD, 1);
258 			fprintf(fout, "From %s %s", myname, date);
259 			puthead(hp, fout,
260 			    GTO|GSUBJECT|GCC|GREPLYTO|GINREPLYTO|GNL);
261 			while ((c = getc(fo)) != EOF)
262 				(void)putc(c, fout);
263 			rewind(fo);
264 			fprintf(fout, "\n");
265 			(void)fflush(fout);
266 			if (ferror(fout)) {
267 				warn("%s", tempname);
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 *sh;
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 ((sh = value("SHELL")) == NULL)
294 				sh = _PATH_CSHELL;
295 			(void)sigemptyset(&nset);
296 			(void)sigaddset(&nset, SIGHUP);
297 			(void)sigaddset(&nset, SIGINT);
298 			(void)sigaddset(&nset, SIGQUIT);
299 			pid = start_command(sh, &nset, image, -1, "-c", fname,
300 			    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 				warn("%s", fname);
310 				senderr++;
311 				goto cant;
312 			}
313 			if ((f = dup(image)) < 0) {
314 				warn("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 				warnx("%s", 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 (top);
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 != '\0'; 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 = NULL;
389 	np = names;
390 	metoo = (value("metoo") != NULL);
391 	while (np != NULL) {
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 != NULL)
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 != NULL; 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)) != NULL) {
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 == NULL)
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 == NULL)
465 		return (n2);
466 	if (n2 == NULL)
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, **top;
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 	top = (char **)salloc((t + extra) * sizeof(*top));
503 	ap = top;
504 	*ap++ = "send-mail";
505 	*ap++ = "-i";
506 	if (metoo)
507 		*ap++ = "-m";
508 	if (verbose)
509 		*ap++ = "-v";
510 	for (; n != NULL; n = n->n_flink)
511 		if ((n->n_type & GDEL) == 0)
512 			*ap++ = n->n_name;
513 	*ap = NULL;
514 	return (top);
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 == NULL)
529 		return (NULL);
530 	new = names;
531 	np = names;
532 	np = np->n_flink;
533 	if (np != NULL)
534 		np->n_blink = NULL;
535 	new->n_flink = NULL;
536 	while (np != NULL) {
537 		t = new;
538 		while (strcasecmp(t->n_name, np->n_name) < 0) {
539 			if (t->n_flink == NULL)
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 = NULL;
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 = NULL;
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 != NULL) {
594 		t = np;
595 		while (t->n_flink != NULL &&
596 		    strcasecmp(np->n_name, t->n_flink->n_name) == 0)
597 			t = t->n_flink;
598 		if (t == np || t == NULL) {
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 != NULL)
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 = NULL;
625 	if (list != NULL)
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 != NULL; 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 != NULL; p = p->n_flink)
654 		if (strcasecmp(p->n_name, name) == 0) {
655 			if (p->n_blink == NULL) {
656 				if (p->n_flink != NULL)
657 					p->n_flink->n_blink = NULL;
658 				np = p->n_flink;
659 				continue;
660 			}
661 			if (p->n_flink == NULL) {
662 				if (p->n_blink != NULL)
663 					p->n_blink->n_flink = NULL;
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(struct name *name)
680 {
681 	struct name *np;
682 
683 	np = name;
684 	while (np != NULL) {
685 		fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
686 		np = np->n_flink;
687 	}
688 	fprintf(stderr, "\n");
689 }
690 */
691