2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 static char sccsid[] = "@(#)names.c 8.1 (Berkeley) 6/6/93";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * Mail -- a mail program
51 * Allocate a single element of a name list,
52 * initialize its name field to the passed
56 nalloc(char str[], int ntype)
60 np = (struct name *)salloc(sizeof(*np));
64 np->n_name = savestr(str);
69 * Find the tail of a list and return it.
72 tailof(struct name *name)
79 while (np->n_flink != NULL)
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.
90 extract(char *line, int ntype)
93 struct name *top, *np, *t;
95 if (line == NULL || *line == '\0')
97 if ((nbuf = malloc(strlen(line) + 1)) == NULL)
98 err(1, "Out of memory");
102 while ((cp = yankword(cp, nbuf)) != NULL) {
103 t = nalloc(nbuf, ntype);
116 * Turn a list of names into a string of the same names.
119 detract(struct name *np, int ntype)
125 comma = ntype & GCOMMA;
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)
135 s += strlen(p->n_name) + 1;
144 for (p = np; p != NULL; p = p->n_flink) {
145 if (ntype && (p->n_type & GMASK) != ntype)
147 cp += strlcpy(cp, p->n_name, strlen(p->n_name) + 1);
148 if (comma && p->n_flink != NULL)
153 if (comma && *--cp == ',')
159 * Grab a single word (liberal word)
160 * Throw away things between ()'s, and take anything between <>.
163 yankword(char *ap, char *wbuf)
174 while (*cp != '\0') {
186 } else if (*cp == ' ' || *cp == '\t' || *cp == ',')
192 for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';)
195 for (cp2 = wbuf; *cp != '\0' && strchr(" \t,(", *cp) == NULL;
203 * Grab a single login name (liberal word)
204 * Throw away things between ()'s, take anything between <>,
205 * and look for words before metacharacters %, @, !.
208 yanklogin(char *ap, char *wbuf)
210 char *cp, *cp2, *cp_temp;
220 while (*cp != '\0') {
232 } else if (*cp == ' ' || *cp == '\t' || *cp == ',')
239 * Now, let's go forward till we meet the needed character,
240 * and step one word back.
243 /* First, remember current point. */
248 * Note that we look ahead in a cycle. This is safe, since
249 * non-end of string is checked first.
251 while(*cp != '\0' && strchr("@%!", *(cp + 1)) == NULL)
255 * Now, start stepping back to the first non-word character,
256 * while counting the number of symbols in a word.
258 while(cp != cp_temp && strchr(" \t,<>", *(cp - 1)) == NULL) {
263 /* Finally, grab the word forward. */
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.
279 * Recipients whose name begins with | are piped through the given
280 * program and removed.
283 outof(struct name *names, FILE *fo, struct header *hp)
286 struct name *np, *top;
296 if (!isfileaddr(np->n_name) && np->n_name[0] != '|') {
300 ispipe = np->n_name[0] == '|';
302 fname = np->n_name+1;
304 fname = expand(np->n_name);
307 * See if we have copied the complete message out yet.
313 char tempname[PATHSIZE];
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);
323 image = open(tempname, O_RDWR);
326 warn("%s", tempname);
331 (void)fcntl(image, F_SETFD, 1);
332 fprintf(fout, "From %s %s", myname, date);
334 GTO|GSUBJECT|GCC|GREPLYTO|GINREPLYTO|GNL);
335 while ((c = getc(fo)) != EOF)
341 warn("%s", tempname);
350 * Now either copy "image" to the desired file
351 * or give it as the standard input to the desired
352 * program as appropriate.
362 * We can't really reuse the same image file,
363 * because multiple piped recipients will
364 * share the same lseek location and trample
367 if ((sh = value("SHELL")) == NULL)
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,
382 if ((fout = Fopen(fname, "a")) == NULL) {
387 if ((f = dup(image)) < 0) {
391 fin = Fdopen(f, "r");
393 fprintf(stderr, "Can't reopen image\n");
399 while ((c = getc(fin)) != EOF)
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.
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 ./...
433 isfileaddr(char *name)
439 for (cp = name; *cp != '\0'; cp++) {
440 if (*cp == '!' || *cp == '%' || *cp == '@')
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).
456 usermap(struct name *names)
458 struct name *new, *np, *cp;
459 struct grouphead *gh;
464 metoo = (value("metoo") != NULL);
466 if (np->n_name[0] == '\\') {
472 gh = findgroup(np->n_name);
475 new = gexpand(new, gh, metoo, np->n_type);
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.
490 gexpand(struct name *nlist, struct grouphead *gh, int metoo, int ntype)
493 struct grouphead *ngh;
498 if (depth > MAXEXP) {
499 printf("Expanding alias to depth larger than %d\n", MAXEXP);
503 for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) {
507 if (strcmp(cp, gh->g_name) == 0)
509 if ((ngh = findgroup(cp)) != NULL) {
510 nlist = gexpand(nlist, ngh, metoo, ntype);
514 np = nalloc(cp, ntype);
516 * At this point should allow to expand
517 * to self if only person in group
519 if (gp == gh->g_list && gp->ge_link == NULL)
521 if (!metoo && strcmp(cp, myname) == 0)
524 nlist = put(nlist, np);
531 * Concatenate the two passed name lists, return the result.
534 cat(struct name *n1, struct name *n2)
549 * Unpack the name list onto a vector of strings.
550 * Return an error if the name list won't fit.
553 unpack(struct name *np)
557 int t, extra, metoo, verbose;
560 if ((t = count(n)) == 0)
561 errx(1, "No names to unpack");
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.
570 metoo = value("metoo") != NULL;
573 verbose = value("verbose") != NULL;
576 top = (char **)salloc((t + extra) * sizeof(*top));
584 for (; n != NULL; n = n->n_flink)
585 if ((n->n_type & GDEL) == 0)
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.
597 elide(struct name *names)
599 struct name *np, *t, *new;
612 while (strcasecmp(t->n_name, np->n_name) < 0) {
613 if (t->n_flink == NULL)
619 * If we ran out of t's, put the new entry after
620 * the current value of t.
623 if (strcasecmp(t->n_name, np->n_name) < 0) {
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.
649 * The normal case -- we are inserting into the
650 * middle of the list.
656 x->n_blink = t->n_blink;
657 t->n_blink->n_flink = x;
662 * Now the list headed up by new is sorted.
663 * Go through it and remove duplicates.
669 while (t->n_flink != NULL &&
670 strcasecmp(np->n_name, t->n_flink->n_name) == 0)
672 if (t == np || t == NULL) {
678 * Now t points to the last entry with the same name
679 * as np. Make np point beyond t.
682 np->n_flink = t->n_flink;
683 if (t->n_flink != NULL)
684 t->n_flink->n_blink = np;
691 * Put another node onto a list of names and return
695 put(struct name *list, struct name *node)
697 node->n_flink = list;
698 node->n_blink = NULL;
700 list->n_blink = node;
705 * Determine the number of undeleted elements in
706 * a name list and return it.
709 count(struct name *np)
713 for (c = 0; np != NULL; np = np->n_flink)
714 if ((np->n_type & GDEL) == 0)
720 * Delete the given name from a namelist.
723 delname(struct name *np, char name[])
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;
735 if (p->n_flink == NULL) {
736 if (p->n_blink != NULL)
737 p->n_blink->n_flink = NULL;
740 p->n_blink->n_flink = p->n_flink;
741 p->n_flink->n_blink = p->n_blink;
747 * Pretty print a name list
748 * Uncomment it if you need it.
753 prettyprint(struct name *name)
759 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
762 fprintf(stderr, "\n");