2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)names.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.bin/mail/names.c,v 1.4.6.3 2003/01/06 05:46:03 mikeh Exp $
34 * Mail -- a mail program
44 * Allocate a single element of a name list,
45 * initialize its name field to the passed
49 nalloc(char *str, int ntype)
53 np = (struct name *)salloc(sizeof(*np));
57 np->n_name = savestr(str);
62 * Find the tail of a list and return it.
65 tailof(struct name *name)
72 while (np->n_flink != NULL)
78 * Extract a list of names from a line,
79 * and make a list of names from it.
80 * Return the list or NULL if none found.
83 extract(char *line, int ntype)
86 struct name *top, *np, *t;
88 if (line == NULL || *line == '\0')
90 if ((nbuf = malloc(strlen(line) + 1)) == NULL)
91 err(1, "Out of memory");
95 while ((cp = yankword(cp, nbuf)) != NULL) {
96 t = nalloc(nbuf, ntype);
109 * Turn a list of names into a string of the same names.
112 detract(struct name *np, int ntype)
118 comma = ntype & GCOMMA;
124 fprintf(stderr, "detract asked to insert commas\n");
125 for (p = np; p != NULL; p = p->n_flink) {
126 if (ntype && (p->n_type & GMASK) != ntype)
128 s += strlen(p->n_name) + 1;
137 for (p = np; p != NULL; p = p->n_flink) {
138 if (ntype && (p->n_type & GMASK) != ntype)
140 cp += strlcpy(cp, p->n_name, strlen(p->n_name) + 1);
141 if (comma && p->n_flink != NULL)
146 if (comma && *--cp == ',')
152 * Grab a single word (liberal word)
153 * Throw away things between ()'s, and take anything between <>.
156 yankword(char *ap, char *wbuf)
167 while (*cp != '\0') {
179 } else if (*cp == ' ' || *cp == '\t' || *cp == ',')
185 for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';)
188 for (cp2 = wbuf; *cp != '\0' && strchr(" \t,(", *cp) == NULL;
196 * Grab a single login name (liberal word).
197 * Throw away things between ()'s, take anything between <>,
198 * and look for words before metacharacters %, @, !.
201 yanklogin(char *ap, char *wbuf)
203 char *cp, *cp2, *cp_temp;
213 while (*cp != '\0') {
225 } else if (*cp == ' ' || *cp == '\t' || *cp == ',') {
233 * Now, let's go forward till we meet the needed character,
234 * and step one word back.
237 /* First, remember current point. */
242 * Note that we look ahead in a cycle. This is safe, since
243 * non-end of string is checked first.
245 while (*cp != '\0' && strchr("@%!", *(cp + 1)) == NULL)
249 * Now, start stepping back to the first non-word character,
250 * while counting the number of symbols in a word.
252 while (cp != cp_temp && strchr(" \t,<>", *(cp - 1)) == NULL) {
257 /* Finally, grab the word forward. */
269 * For each recipient in the passed name list with a /
270 * in the name, append the message to the end of the named file
271 * and remove him from the recipient list.
273 * Recipients whose name begins with | are piped through the given
274 * program and removed.
277 outof(struct name *names, FILE *fo, struct header *hp)
280 struct name *np, *top;
290 if (!isfileaddr(np->n_name) && np->n_name[0] != '|') {
294 ispipe = np->n_name[0] == '|';
296 fname = np->n_name+1;
298 fname = expand(np->n_name);
301 * See if we have copied the complete message out yet.
307 char tempname[PATHSIZE];
309 snprintf(tempname, sizeof(tempname),
310 "%s/mail.ReXXXXXXXXXX", tmpdir);
311 if ((fd = mkstemp(tempname)) == -1 ||
312 (fout = Fdopen(fd, "a")) == NULL) {
313 warn("%s", tempname);
317 image = open(tempname, O_RDWR);
320 warn("%s", tempname);
325 fcntl(image, F_SETFD, 1);
326 fprintf(fout, "From %s %s", myname, date);
328 GTO|GSUBJECT|GCC|GREPLYTO|GINREPLYTO|GNL);
329 while ((c = getc(fo)) != EOF)
335 warn("%s", tempname);
344 * Now either copy "image" to the desired file
345 * or give it as the standard input to the desired
346 * program as appropriate.
356 * We can't really reuse the same image file,
357 * because multiple piped recipients will
358 * share the same lseek location and trample
361 if ((sh = value("SHELL")) == NULL)
364 sigaddset(&nset, SIGHUP);
365 sigaddset(&nset, SIGINT);
366 sigaddset(&nset, SIGQUIT);
367 pid = start_command(sh, &nset, image, -1, "-c", fname,
376 if ((fout = Fopen(fname, "a")) == NULL) {
381 if ((f = dup(image)) < 0) {
385 fin = Fdopen(f, "r");
387 fprintf(stderr, "Can't reopen image\n");
393 while ((c = getc(fin)) != EOF)
407 * In days of old we removed the entry from the
408 * the list; now for sake of header expansion
409 * we leave it in and mark it as deleted.
422 * Determine if the passed address is a local "send to file" address.
423 * If any of the network metacharacters precedes any slashes, it can't
424 * be a filename. We cheat with .'s to allow path names like ./...
427 isfileaddr(char *name)
433 for (cp = name; *cp != '\0'; cp++) {
434 if (*cp == '!' || *cp == '%' || *cp == '@')
443 * Map all of the aliased users in the invoker's mailrc
444 * file and insert them into the list.
445 * Changed after all these months of service to recursively
446 * expand names (2/14/80).
450 usermap(struct name *names)
452 struct name *new, *np, *cp;
453 struct grouphead *gh;
458 metoo = (value("metoo") != NULL);
460 if (np->n_name[0] == '\\') {
466 gh = findgroup(np->n_name);
469 new = gexpand(new, gh, metoo, np->n_type);
478 * Recursively expand a group name. We limit the expansion to some
479 * fixed level to keep things from going haywire.
480 * Direct recursion is not expanded for convenience.
484 gexpand(struct name *nlist, struct grouphead *gh, int metoo, int ntype)
487 struct grouphead *ngh;
492 if (depth > MAXEXP) {
493 printf("Expanding alias to depth larger than %d\n", MAXEXP);
497 for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) {
501 if (strcmp(cp, gh->g_name) == 0)
503 if ((ngh = findgroup(cp)) != NULL) {
504 nlist = gexpand(nlist, ngh, metoo, ntype);
508 np = nalloc(cp, ntype);
510 * At this point should allow to expand
511 * to self if only person in group
513 if (gp == gh->g_list && gp->ge_link == NULL)
515 if (!metoo && strcmp(cp, myname) == 0)
518 nlist = put(nlist, np);
525 * Concatenate the two passed name lists, return the result.
528 cat(struct name *n1, struct name *n2)
543 * Unpack the name list onto a vector of strings.
544 * Return an error if the name list won't fit.
547 unpack(struct name *np)
551 int t, extra, metoo, verbose;
554 if ((t = count(n)) == 0)
555 errx(1, "No names to unpack");
557 * Compute the number of extra arguments we will need.
558 * We need at least two extra -- one for "mail" and one for
559 * the terminating 0 pointer. Additional spots may be needed
560 * to pass along -f to the host mailer.
564 metoo = value("metoo") != NULL;
567 verbose = value("verbose") != NULL;
570 top = (char **)salloc((t + extra) * sizeof(*top));
578 for (; n != NULL; n = n->n_flink)
579 if ((n->n_type & GDEL) == 0)
586 * Remove all of the duplicates from the passed name list by
587 * insertion sorting them, then checking for dups.
588 * Return the head of the new list.
591 elide(struct name *names)
593 struct name *np, *t, *new;
606 while (strcasecmp(t->n_name, np->n_name) < 0) {
607 if (t->n_flink == NULL)
613 * If we ran out of t's, put the new entry after
614 * the current value of t.
617 if (strcasecmp(t->n_name, np->n_name) < 0) {
627 * Otherwise, put the new entry in front of the
628 * current t. If at the front of the list,
629 * the new guy becomes the new head of the list.
643 * The normal case -- we are inserting into the
644 * middle of the list.
650 x->n_blink = t->n_blink;
651 t->n_blink->n_flink = x;
656 * Now the list headed up by new is sorted.
657 * Go through it and remove duplicates.
663 while (t->n_flink != NULL &&
664 strcasecmp(np->n_name, t->n_flink->n_name) == 0)
666 if (t == np || t == NULL) {
672 * Now t points to the last entry with the same name
673 * as np. Make np point beyond t.
676 np->n_flink = t->n_flink;
677 if (t->n_flink != NULL)
678 t->n_flink->n_blink = np;
685 * Put another node onto a list of names and return
689 put(struct name *list, struct name *node)
691 node->n_flink = list;
692 node->n_blink = NULL;
694 list->n_blink = node;
699 * Determine the number of undeleted elements in
700 * a name list and return it.
703 count(struct name *np)
707 for (c = 0; np != NULL; np = np->n_flink)
708 if ((np->n_type & GDEL) == 0)
714 * Delete the given name from a namelist.
717 delname(struct name *np, char *name)
721 for (p = np; p != NULL; p = p->n_flink)
722 if (strcasecmp(p->n_name, name) == 0) {
723 if (p->n_blink == NULL) {
724 if (p->n_flink != NULL)
725 p->n_flink->n_blink = NULL;
729 if (p->n_flink == NULL) {
730 if (p->n_blink != NULL)
731 p->n_blink->n_flink = NULL;
734 p->n_blink->n_flink = p->n_flink;
735 p->n_flink->n_blink = p->n_blink;
741 * Pretty print a name list
742 * Uncomment it if you need it.
747 prettyprint(struct name *name)
753 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
756 fprintf(stderr, "\n");