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