Home | History | Annotate | Line # | Download | only in mail
names.c revision 1.20
      1 /*	$NetBSD: names.c,v 1.20 2006/06/15 13:03:29 ghen 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. 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 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)names.c	8.1 (Berkeley) 6/6/93";
     36 #else
     37 __RCSID("$NetBSD: names.c,v 1.20 2006/06/15 13:03:29 ghen Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 /*
     42  * Mail -- a mail program
     43  *
     44  * Handle name lists.
     45  */
     46 
     47 #include "rcv.h"
     48 #include "extern.h"
     49 
     50 
     51 /*
     52  * Allocate a single element of a name list,
     53  * initialize its name field to the passed
     54  * name and return it.
     55  */
     56 struct name *
     57 nalloc(char str[], int ntype)
     58 {
     59 	struct name *np;
     60 
     61 	np = salloc(sizeof *np);
     62 	np->n_flink = NULL;
     63 	np->n_blink = NULL;
     64 	np->n_type = ntype;
     65 	np->n_name = savestr(str);
     66 	return(np);
     67 }
     68 
     69 /*
     70  * Find the tail of a list and return it.
     71  */
     72 struct name *
     73 tailof(struct name *name)
     74 {
     75 	struct name *np;
     76 
     77 	np = name;
     78 	if (np == NULL)
     79 		return(NULL);
     80 	while (np->n_flink != NULL)
     81 		np = np->n_flink;
     82 	return(np);
     83 }
     84 
     85 /*
     86  * Extract a list of names from a line,
     87  * and make a list of names from it.
     88  * Return the list or NULL if none found.
     89  */
     90 struct name *
     91 extract(char line[], int ntype)
     92 {
     93 	char *cp;
     94 	struct name *begin, *np, *t;
     95 	char nbuf[BUFSIZ];
     96 
     97 	if (line == NULL || *line == '\0')
     98 		return NULL;
     99 	begin = NULL;
    100 	np = NULL;
    101 	cp = line;
    102 	while ((cp = yankword(cp, nbuf)) != NULL) {
    103 		t = nalloc(nbuf, ntype);
    104 		if (begin == NULL)
    105 			begin = t;
    106 		else
    107 			np->n_flink = t;
    108 		t->n_blink = np;
    109 		np = t;
    110 	}
    111 	return begin;
    112 }
    113 
    114 /*
    115  * Turn a list of names into a string of the same names.
    116  */
    117 char *
    118 detract(struct name *np, int ntype)
    119 {
    120 	size_t s;
    121 	char *cp, *begin;
    122 	struct name *p;
    123 	int comma;
    124 
    125 	comma = ntype & GCOMMA;
    126 	if (np == NULL)
    127 		return(NULL);
    128 	ntype &= ~GCOMMA;
    129 	s = 0;
    130 	if (debug && comma)
    131 		(void)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 	begin = salloc(s);
    143 	cp = begin;
    144 	for (p = np; p != NULL; p = p->n_flink) {
    145 		if (ntype && (p->n_type & GMASK) != ntype)
    146 			continue;
    147 		cp = copy(p->n_name, cp);
    148 		if (comma && p->n_flink != NULL)
    149 			*cp++ = ',';
    150 		*cp++ = ' ';
    151 	}
    152 	*--cp = 0;
    153 	if (comma && *--cp == ',')
    154 		*cp = 0;
    155 	return(begin);
    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 && !strchr(" \t,(", *cp); *cp2++ = *cp++)
    196 			;
    197 	*cp2 = '\0';
    198 	return cp;
    199 }
    200 
    201 /*
    202  * For each recipient in the passed name list with a /
    203  * in the name, append the message to the end of the named file
    204  * and remove him from the recipient list.
    205  *
    206  * Recipients whose name begins with | are piped through the given
    207  * program and removed.
    208  */
    209 struct name *
    210 outof(struct name *names, FILE *fo, struct header *hp)
    211 {
    212 	int c, fd;
    213 	struct name *np, *begin;
    214 	time_t now;
    215 	char *date;
    216 	const char *fname;
    217 	FILE *fout, *fin;
    218 	int ispipe;
    219 	char tempname[PATHSIZE];
    220 
    221 	begin = names;
    222 	np = names;
    223 	(void)time(&now);
    224 	date = ctime(&now);
    225 	while (np != NULL) {
    226 		if (!isfileaddr(np->n_name) && np->n_name[0] != '|') {
    227 			np = np->n_flink;
    228 			continue;
    229 		}
    230 		ispipe = np->n_name[0] == '|';
    231 		if (ispipe)
    232 			fname = np->n_name+1;
    233 		else
    234 			fname = expand(np->n_name);
    235 
    236 		/*
    237 		 * See if we have copied the complete message out yet.
    238 		 * If not, do so.
    239 		 */
    240 
    241 		if (image < 0) {
    242 			(void)snprintf(tempname, sizeof(tempname),
    243 			    "%s/mail.ReXXXXXXXXXXXX", tmpdir);
    244 			if ((fd = mkstemp(tempname)) == -1 ||
    245 			    (fout = Fdopen(fd, "a")) == NULL) {
    246 				if (fd != -1)
    247 					(void)close(fd);
    248 				warn("%s", tempname);
    249 				senderr++;
    250 				goto cant;
    251 			}
    252 			image = open(tempname, 2);
    253 			(void)unlink(tempname);
    254 			if (image < 0) {
    255 				warn("%s", tempname);
    256 				senderr++;
    257 				(void)Fclose(fout);
    258 				goto cant;
    259 			}
    260 			(void)fcntl(image, F_SETFD, 1);
    261 			(void)fprintf(fout, "From %s %s", myname, date);
    262 			(void)puthead(hp, fout, GTO|GSUBJECT|GCC|GNL);
    263 			while ((c = getc(fo)) != EOF)
    264 				(void)putc(c, fout);
    265 			rewind(fo);
    266 			(void)putc('\n', fout);
    267 			(void)fflush(fout);
    268 			if (ferror(fout)) {
    269 				warn("%s", tempname);
    270 				senderr++;
    271 				(void)Fclose(fout);
    272 				goto cant;
    273 			}
    274 			(void)Fclose(fout);
    275 		}
    276 
    277 		/*
    278 		 * Now either copy "image" to the desired file
    279 		 * or give it as the standard input to the desired
    280 		 * program as appropriate.
    281 		 */
    282 
    283 		if (ispipe) {
    284 			int pid;
    285 			const char *shellcmd;
    286 			sigset_t nset;
    287 
    288 			/*
    289 			 * XXX
    290 			 * We can't really reuse the same image file,
    291 			 * because multiple piped recipients will
    292 			 * share the same lseek location and trample
    293 			 * on one another.
    294 			 */
    295 			if ((shellcmd = value("SHELL")) == NULL)
    296 				shellcmd = _PATH_CSHELL;
    297 			(void)sigemptyset(&nset);
    298 			(void)sigaddset(&nset, SIGHUP);
    299 			(void)sigaddset(&nset, SIGINT);
    300 			(void)sigaddset(&nset, SIGQUIT);
    301 			pid = start_command(shellcmd, &nset,
    302 				image, -1, "-c", fname, NULL);
    303 			if (pid < 0) {
    304 				senderr++;
    305 				goto cant;
    306 			}
    307 			free_child(pid);
    308 		} else {
    309 			int f;
    310 			if ((fout = Fopen(fname, "a")) == NULL) {
    311 				warn("%s", fname);
    312 				senderr++;
    313 				goto cant;
    314 			}
    315 			if ((f = dup(image)) < 0) {
    316 				warn("dup");
    317 				fin = NULL;
    318 			} else
    319 				fin = Fdopen(f, "r");
    320 			if (fin == NULL) {
    321 				(void)fprintf(stderr, "Can't reopen image\n");
    322 				(void)Fclose(fout);
    323 				senderr++;
    324 				goto cant;
    325 			}
    326 			rewind(fin);
    327 			while ((c = getc(fin)) != EOF)
    328 				(void)putc(c, fout);
    329 			if (ferror(fout)) {
    330 				warn("%s", fname);
    331 				senderr++;
    332 				(void)Fclose(fout);
    333 				(void)Fclose(fin);
    334 				goto cant;
    335 			}
    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(begin);
    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(char *name)
    362 {
    363 	char *cp;
    364 
    365 	if (*name == '+')
    366 		return 1;
    367 	for (cp = name; *cp; cp++) {
    368 		if (*cp == '!' || *cp == '%' || *cp == '@')
    369 			return 0;
    370 		if (*cp == '/')
    371 			return 1;
    372 	}
    373 	return 0;
    374 }
    375 
    376 /*
    377  * Map all of the aliased users in the invoker's mailrc
    378  * file and insert them into the list.
    379  * Changed after all these months of service to recursively
    380  * expand names (2/14/80).
    381  */
    382 
    383 struct name *
    384 usermap(struct name *names)
    385 {
    386 	struct name *new, *np, *cp;
    387 	struct grouphead *gh;
    388 	int metoo;
    389 
    390 	new = NULL;
    391 	np = names;
    392 	metoo = (value("metoo") != NULL);
    393 	while (np != NULL) {
    394 		if (np->n_name[0] == '\\') {
    395 			cp = np->n_flink;
    396 			new = put(new, np);
    397 			np = cp;
    398 			continue;
    399 		}
    400 		gh = findgroup(np->n_name);
    401 		cp = np->n_flink;
    402 		if (gh != NULL)
    403 			new = gexpand(new, gh, metoo, np->n_type);
    404 		else
    405 			new = put(new, np);
    406 		np = cp;
    407 	}
    408 	return(new);
    409 }
    410 
    411 /*
    412  * Recursively expand a group name.  We limit the expansion to some
    413  * fixed level to keep things from going haywire.
    414  * Direct recursion is not expanded for convenience.
    415  */
    416 
    417 struct name *
    418 gexpand(struct name *nlist, struct grouphead *gh, int metoo, int ntype)
    419 {
    420 	struct group *gp;
    421 	struct grouphead *ngh;
    422 	struct name *np;
    423 	static int depth;
    424 	char *cp;
    425 
    426 	if (depth > MAXEXP) {
    427 		(void)printf("Expanding alias to depth larger than %d\n", MAXEXP);
    428 		return(nlist);
    429 	}
    430 	depth++;
    431 	for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) {
    432 		cp = gp->ge_name;
    433 		if (*cp == '\\')
    434 			goto quote;
    435 		if (strcmp(cp, gh->g_name) == 0)
    436 			goto quote;
    437 		if ((ngh = findgroup(cp)) != NULL) {
    438 			nlist = gexpand(nlist, ngh, metoo, ntype);
    439 			continue;
    440 		}
    441 quote:
    442 		np = nalloc(cp, ntype);
    443 		/*
    444 		 * At this point should allow to expand
    445 		 * to self if only person in group
    446 		 */
    447 		if (gp == gh->g_list && gp->ge_link == NULL)
    448 			goto skip;
    449 		if (!metoo && strcmp(cp, myname) == 0)
    450 			np->n_type |= GDEL;
    451 skip:
    452 		nlist = put(nlist, np);
    453 	}
    454 	depth--;
    455 	return(nlist);
    456 }
    457 
    458 /*
    459  * Concatenate the two passed name lists, return the result.
    460  */
    461 struct name *
    462 cat(struct name *n1, struct name *n2)
    463 {
    464 	struct name *tail;
    465 
    466 	if (n1 == NULL)
    467 		return(n2);
    468 	if (n2 == NULL)
    469 		return(n1);
    470 	tail = tailof(n1);
    471 	tail->n_flink = n2;
    472 	n2->n_blink = tail;
    473 	return(n1);
    474 }
    475 
    476 /*
    477  * Unpack the name list onto a vector of strings.
    478  * Return an error if the name list won't fit.
    479  */
    480 const char **
    481 unpack(struct name *np)
    482 {
    483 	const char **ap, **begin;
    484 	struct name *n;
    485 	int t, extra, metoo, verbose;
    486 
    487 	n = np;
    488 	if ((t = count(n)) == 0)
    489 		errx(1, "No names to unpack");
    490 	/*
    491 	 * Compute the number of extra arguments we will need.
    492 	 * We need at least two extra -- one for "mail" and one for
    493 	 * the terminating 0 pointer.  Additional spots may be needed
    494 	 * to pass along -f to the host mailer.
    495 	 */
    496 	extra = 2;
    497 	extra++;
    498 	metoo = value("metoo") != NULL;
    499 	if (metoo)
    500 		extra++;
    501 	verbose = value("verbose") != NULL;
    502 	if (verbose)
    503 		extra++;
    504 	begin = salloc((t + extra) * sizeof *begin);
    505 	ap = begin;
    506 	*ap++ = "sendmail";
    507 	*ap++ = "-i";
    508 	if (metoo)
    509 		*ap++ = "-m";
    510 	if (verbose)
    511 		*ap++ = "-v";
    512 	for (; n != NULL; n = n->n_flink)
    513 		if ((n->n_type & GDEL) == 0)
    514 			*ap++ = n->n_name;
    515 	*ap = NULL;
    516 	return(begin);
    517 }
    518 
    519 /*
    520  * Remove all of the duplicates from the passed name list by
    521  * insertion sorting them, then checking for dups.
    522  * Return the head of the new list.
    523  */
    524 struct name *
    525 elide(struct name *names)
    526 {
    527 	struct name *np, *t, *new;
    528 	struct name *x;
    529 
    530 	if (names == NULL)
    531 		return(NULL);
    532 	new = names;
    533 	np = names;
    534 	np = np->n_flink;
    535 	if (np != NULL)
    536 		np->n_blink = NULL;
    537 	new->n_flink = NULL;
    538 	while (np != NULL) {
    539 		t = new;
    540 		while (strcasecmp(t->n_name, np->n_name) < 0) {
    541 			if (t->n_flink == NULL)
    542 				break;
    543 			t = t->n_flink;
    544 		}
    545 
    546 		/*
    547 		 * If we ran out of t's, put the new entry after
    548 		 * the current value of t.
    549 		 */
    550 
    551 		if (strcasecmp(t->n_name, np->n_name) < 0) {
    552 			t->n_flink = np;
    553 			np->n_blink = t;
    554 			t = np;
    555 			np = np->n_flink;
    556 			t->n_flink = NULL;
    557 			continue;
    558 		}
    559 
    560 		/*
    561 		 * Otherwise, put the new entry in front of the
    562 		 * current t.  If at the front of the list,
    563 		 * the new guy becomes the new head of the list.
    564 		 */
    565 
    566 		if (t == new) {
    567 			t = np;
    568 			np = np->n_flink;
    569 			t->n_flink = new;
    570 			new->n_blink = t;
    571 			t->n_blink = NULL;
    572 			new = t;
    573 			continue;
    574 		}
    575 
    576 		/*
    577 		 * The normal case -- we are inserting into the
    578 		 * middle of the list.
    579 		 */
    580 
    581 		x = np;
    582 		np = np->n_flink;
    583 		x->n_flink = t;
    584 		x->n_blink = t->n_blink;
    585 		t->n_blink->n_flink = x;
    586 		t->n_blink = x;
    587 	}
    588 
    589 	/*
    590 	 * Now the list headed up by new is sorted.
    591 	 * Go through it and remove duplicates.
    592 	 */
    593 
    594 	np = new;
    595 	while (np != NULL) {
    596 		t = np;
    597 		while (t->n_flink != NULL &&
    598 		       strcasecmp(np->n_name, t->n_flink->n_name) == 0)
    599 			t = t->n_flink;
    600 		if (t == np || t == NULL) {
    601 			np = np->n_flink;
    602 			continue;
    603 		}
    604 
    605 		/*
    606 		 * Now t points to the last entry with the same name
    607 		 * as np.  Make np point beyond t.
    608 		 */
    609 
    610 		np->n_flink = t->n_flink;
    611 		if (t->n_flink != NULL)
    612 			t->n_flink->n_blink = np;
    613 		np = np->n_flink;
    614 	}
    615 	return(new);
    616 }
    617 
    618 /*
    619  * Put another node onto a list of names and return
    620  * the list.
    621  */
    622 struct name *
    623 put(struct name *list, struct name *node)
    624 {
    625 	node->n_flink = list;
    626 	node->n_blink = NULL;
    627 	if (list != NULL)
    628 		list->n_blink = node;
    629 	return(node);
    630 }
    631 
    632 /*
    633  * Determine the number of undeleted elements in
    634  * a name list and return it.
    635  */
    636 int
    637 count(struct name *np)
    638 {
    639 	int c;
    640 
    641 	for (c = 0; np != NULL; np = np->n_flink)
    642 		if ((np->n_type & GDEL) == 0)
    643 			c++;
    644 	return c;
    645 }
    646 
    647 /*
    648  * Delete the given name from a namelist.
    649  */
    650 struct name *
    651 delname(struct name *np, char name[])
    652 {
    653 	struct name *p;
    654 
    655 	for (p = np; p != NULL; p = p->n_flink)
    656 		if (strcasecmp(p->n_name, name) == 0) {
    657 			if (p->n_blink == NULL) {
    658 				if (p->n_flink != NULL)
    659 					p->n_flink->n_blink = NULL;
    660 				np = p->n_flink;
    661 				continue;
    662 			}
    663 			if (p->n_flink == NULL) {
    664 				if (p->n_blink != NULL)
    665 					p->n_blink->n_flink = NULL;
    666 				continue;
    667 			}
    668 			p->n_blink->n_flink = p->n_flink;
    669 			p->n_flink->n_blink = p->n_blink;
    670 		}
    671 	return np;
    672 }
    673 
    674 /*
    675  * Pretty print a name list
    676  * Uncomment it if you need it.
    677  */
    678 
    679 /*
    680 void
    681 prettyprint(name)
    682 	struct name *name;
    683 {
    684 	struct name *np;
    685 
    686 	np = name;
    687 	while (np != NULL) {
    688 		(void)fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
    689 		np = np->n_flink;
    690 	}
    691 	(void)fprintf(stderr, "\n");
    692 }
    693 */
    694