Pre-2.0 release: Sync with HAMMER 64 - NFS and cross-device link fixes.
[dragonfly.git] / usr.bin / mail / names.c
blob044e0b2e3f5e349f77faf18ccea2568d594a273d
1 /*
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
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.
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.
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.5 2004/09/08 03:01:11 joerg Exp $
39 * Mail -- a mail program
41 * Handle name lists.
44 #include "rcv.h"
45 #include <fcntl.h>
46 #include "extern.h"
49 * Allocate a single element of a name list,
50 * initialize its name field to the passed
51 * name and return it.
53 struct name *
54 nalloc(char *str, int ntype)
56 struct name *np;
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);
67 * Find the tail of a list and return it.
69 struct name *
70 tailof(struct name *name)
72 struct name *np;
74 np = name;
75 if (np == NULL)
76 return (NULL);
77 while (np->n_flink != NULL)
78 np = np->n_flink;
79 return (np);
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.
87 struct name *
88 extract(char *line, int ntype)
90 char *cp, *nbuf;
91 struct name *top, *np, *t;
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;
109 free(nbuf);
110 return (top);
114 * Turn a list of names into a string of the same names.
116 char *
117 detract(struct name *np, int ntype)
119 int s, comma;
120 char *cp, *top;
121 struct name *p;
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++;
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++ = ' ';
150 *--cp = '\0';
151 if (comma && *--cp == ',')
152 *cp = '\0';
153 return (top);
157 * Grab a single word (liberal word)
158 * Throw away things between ()'s, and take anything between <>.
160 char *
161 yankword(char *ap, char *wbuf)
163 char *cp, *cp2;
165 cp = ap;
166 for (;;) {
167 if (*cp == '\0')
168 return (NULL);
169 if (*cp == '(') {
170 int nesting = 0;
172 while (*cp != '\0') {
173 switch (*cp++) {
174 case '(':
175 nesting++;
176 break;
177 case ')':
178 --nesting;
179 break;
181 if (nesting <= 0)
182 break;
184 } else if (*cp == ' ' || *cp == '\t' || *cp == ',')
185 cp++;
186 else
187 break;
189 if (*cp == '<')
190 for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';)
192 else
193 for (cp2 = wbuf; *cp != '\0' && strchr(" \t,(", *cp) == NULL;
194 *cp2++ = *cp++)
196 *cp2 = '\0';
197 return (cp);
201 * Grab a single login name (liberal word).
202 * Throw away things between ()'s, take anything between <>,
203 * and look for words before metacharacters %, @, !.
205 char *
206 yanklogin(char *ap, char *wbuf)
208 char *cp, *cp2, *cp_temp;
209 int n;
211 cp = ap;
212 for (;;) {
213 if (*cp == '\0')
214 return (NULL);
215 if (*cp == '(') {
216 int nesting = 0;
218 while (*cp != '\0') {
219 switch (*cp++) {
220 case '(':
221 nesting++;
222 break;
223 case ')':
224 nesting--;
225 break;
227 if (nesting <= 0)
228 break;
230 } else if (*cp == ' ' || *cp == '\t' || *cp == ',') {
231 cp++;
232 } else {
233 break;
238 * Now, let's go forward till we meet the needed character,
239 * and step one word back.
242 /* First, remember current point. */
243 cp_temp = cp;
244 n = 0;
247 * Note that we look ahead in a cycle. This is safe, since
248 * non-end of string is checked first.
250 while (*cp != '\0' && strchr("@%!", *(cp + 1)) == NULL)
251 cp ++;
254 * Now, start stepping back to the first non-word character,
255 * while counting the number of symbols in a word.
257 while (cp != cp_temp && strchr(" \t,<>", *(cp - 1)) == NULL) {
258 n++;
259 cp--;
262 /* Finally, grab the word forward. */
263 cp2 = wbuf;
264 while (n >= 0) {
265 *cp++ = *cp++;
266 n--;
269 *cp2 = '\0';
270 return (cp);
274 * For each recipient in the passed name list with a /
275 * in the name, append the message to the end of the named file
276 * and remove him from the recipient list.
278 * Recipients whose name begins with | are piped through the given
279 * program and removed.
281 struct name *
282 outof(struct name *names, FILE *fo, struct header *hp)
284 int c, ispipe;
285 struct name *np, *top;
286 time_t now;
287 char *date, *fname;
288 FILE *fout, *fin;
290 top = names;
291 np = names;
292 time(&now);
293 date = ctime(&now);
294 while (np != NULL) {
295 if (!isfileaddr(np->n_name) && np->n_name[0] != '|') {
296 np = np->n_flink;
297 continue;
299 ispipe = np->n_name[0] == '|';
300 if (ispipe)
301 fname = np->n_name+1;
302 else
303 fname = expand(np->n_name);
306 * See if we have copied the complete message out yet.
307 * If not, do so.
310 if (image < 0) {
311 int fd;
312 char tempname[PATHSIZE];
314 snprintf(tempname, sizeof(tempname),
315 "%s/mail.ReXXXXXXXXXX", tmpdir);
316 if ((fd = mkstemp(tempname)) == -1 ||
317 (fout = Fdopen(fd, "a")) == NULL) {
318 warn("%s", tempname);
319 senderr++;
320 goto cant;
322 image = open(tempname, O_RDWR);
323 rm(tempname);
324 if (image < 0) {
325 warn("%s", tempname);
326 senderr++;
327 Fclose(fout);
328 goto cant;
330 fcntl(image, F_SETFD, 1);
331 fprintf(fout, "From %s %s", myname, date);
332 puthead(hp, fout,
333 GTO|GSUBJECT|GCC|GREPLYTO|GINREPLYTO|GNL);
334 while ((c = getc(fo)) != EOF)
335 putc(c, fout);
336 rewind(fo);
337 fprintf(fout, "\n");
338 fflush(fout);
339 if (ferror(fout)) {
340 warn("%s", tempname);
341 senderr++;
342 Fclose(fout);
343 goto cant;
345 Fclose(fout);
349 * Now either copy "image" to the desired file
350 * or give it as the standard input to the desired
351 * program as appropriate.
354 if (ispipe) {
355 int pid;
356 char *sh;
357 sigset_t nset;
360 * XXX
361 * We can't really reuse the same image file,
362 * because multiple piped recipients will
363 * share the same lseek location and trample
364 * on one another.
366 if ((sh = value("SHELL")) == NULL)
367 sh = _PATH_CSHELL;
368 sigemptyset(&nset);
369 sigaddset(&nset, SIGHUP);
370 sigaddset(&nset, SIGINT);
371 sigaddset(&nset, SIGQUIT);
372 pid = start_command(sh, &nset, image, -1, "-c", fname,
373 NULL);
374 if (pid < 0) {
375 senderr++;
376 goto cant;
378 free_child(pid);
379 } else {
380 int f;
381 if ((fout = Fopen(fname, "a")) == NULL) {
382 warn("%s", fname);
383 senderr++;
384 goto cant;
386 if ((f = dup(image)) < 0) {
387 warn("dup");
388 fin = NULL;
389 } else
390 fin = Fdopen(f, "r");
391 if (fin == NULL) {
392 fprintf(stderr, "Can't reopen image\n");
393 Fclose(fout);
394 senderr++;
395 goto cant;
397 rewind(fin);
398 while ((c = getc(fin)) != EOF)
399 putc(c, fout);
400 if (ferror(fout)) {
401 warnx("%s", fname);
402 senderr++;
403 Fclose(fout);
404 Fclose(fin);
405 goto cant;
407 Fclose(fout);
408 Fclose(fin);
410 cant:
412 * In days of old we removed the entry from the
413 * the list; now for sake of header expansion
414 * we leave it in and mark it as deleted.
416 np->n_type |= GDEL;
417 np = np->n_flink;
419 if (image >= 0) {
420 close(image);
421 image = -1;
423 return (top);
427 * Determine if the passed address is a local "send to file" address.
428 * If any of the network metacharacters precedes any slashes, it can't
429 * be a filename. We cheat with .'s to allow path names like ./...
432 isfileaddr(char *name)
434 char *cp;
436 if (*name == '+')
437 return (1);
438 for (cp = name; *cp != '\0'; cp++) {
439 if (*cp == '!' || *cp == '%' || *cp == '@')
440 return (0);
441 if (*cp == '/')
442 return (1);
444 return (0);
448 * Map all of the aliased users in the invoker's mailrc
449 * file and insert them into the list.
450 * Changed after all these months of service to recursively
451 * expand names (2/14/80).
454 struct name *
455 usermap(struct name *names)
457 struct name *new, *np, *cp;
458 struct grouphead *gh;
459 int metoo;
461 new = NULL;
462 np = names;
463 metoo = (value("metoo") != NULL);
464 while (np != NULL) {
465 if (np->n_name[0] == '\\') {
466 cp = np->n_flink;
467 new = put(new, np);
468 np = cp;
469 continue;
471 gh = findgroup(np->n_name);
472 cp = np->n_flink;
473 if (gh != NULL)
474 new = gexpand(new, gh, metoo, np->n_type);
475 else
476 new = put(new, np);
477 np = cp;
479 return (new);
483 * Recursively expand a group name. We limit the expansion to some
484 * fixed level to keep things from going haywire.
485 * Direct recursion is not expanded for convenience.
488 struct name *
489 gexpand(struct name *nlist, struct grouphead *gh, int metoo, int ntype)
491 struct group *gp;
492 struct grouphead *ngh;
493 struct name *np;
494 static int depth;
495 char *cp;
497 if (depth > MAXEXP) {
498 printf("Expanding alias to depth larger than %d\n", MAXEXP);
499 return (nlist);
501 depth++;
502 for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) {
503 cp = gp->ge_name;
504 if (*cp == '\\')
505 goto quote;
506 if (strcmp(cp, gh->g_name) == 0)
507 goto quote;
508 if ((ngh = findgroup(cp)) != NULL) {
509 nlist = gexpand(nlist, ngh, metoo, ntype);
510 continue;
512 quote:
513 np = nalloc(cp, ntype);
515 * At this point should allow to expand
516 * to self if only person in group
518 if (gp == gh->g_list && gp->ge_link == NULL)
519 goto skip;
520 if (!metoo && strcmp(cp, myname) == 0)
521 np->n_type |= GDEL;
522 skip:
523 nlist = put(nlist, np);
525 depth--;
526 return (nlist);
530 * Concatenate the two passed name lists, return the result.
532 struct name *
533 cat(struct name *n1, struct name *n2)
535 struct name *tail;
537 if (n1 == NULL)
538 return (n2);
539 if (n2 == NULL)
540 return (n1);
541 tail = tailof(n1);
542 tail->n_flink = n2;
543 n2->n_blink = tail;
544 return (n1);
548 * Unpack the name list onto a vector of strings.
549 * Return an error if the name list won't fit.
551 char **
552 unpack(struct name *np)
554 char **ap, **top;
555 struct name *n;
556 int t, extra, metoo, verbose;
558 n = np;
559 if ((t = count(n)) == 0)
560 errx(1, "No names to unpack");
562 * Compute the number of extra arguments we will need.
563 * We need at least two extra -- one for "mail" and one for
564 * the terminating 0 pointer. Additional spots may be needed
565 * to pass along -f to the host mailer.
567 extra = 2;
568 extra++;
569 metoo = value("metoo") != NULL;
570 if (metoo)
571 extra++;
572 verbose = value("verbose") != NULL;
573 if (verbose)
574 extra++;
575 top = (char **)salloc((t + extra) * sizeof(*top));
576 ap = top;
577 *ap++ = "send-mail";
578 *ap++ = "-i";
579 if (metoo)
580 *ap++ = "-m";
581 if (verbose)
582 *ap++ = "-v";
583 for (; n != NULL; n = n->n_flink)
584 if ((n->n_type & GDEL) == 0)
585 *ap++ = n->n_name;
586 *ap = NULL;
587 return (top);
591 * Remove all of the duplicates from the passed name list by
592 * insertion sorting them, then checking for dups.
593 * Return the head of the new list.
595 struct name *
596 elide(struct name *names)
598 struct name *np, *t, *new;
599 struct name *x;
601 if (names == NULL)
602 return (NULL);
603 new = names;
604 np = names;
605 np = np->n_flink;
606 if (np != NULL)
607 np->n_blink = NULL;
608 new->n_flink = NULL;
609 while (np != NULL) {
610 t = new;
611 while (strcasecmp(t->n_name, np->n_name) < 0) {
612 if (t->n_flink == NULL)
613 break;
614 t = t->n_flink;
618 * If we ran out of t's, put the new entry after
619 * the current value of t.
622 if (strcasecmp(t->n_name, np->n_name) < 0) {
623 t->n_flink = np;
624 np->n_blink = t;
625 t = np;
626 np = np->n_flink;
627 t->n_flink = NULL;
628 continue;
632 * Otherwise, put the new entry in front of the
633 * current t. If at the front of the list,
634 * the new guy becomes the new head of the list.
637 if (t == new) {
638 t = np;
639 np = np->n_flink;
640 t->n_flink = new;
641 new->n_blink = t;
642 t->n_blink = NULL;
643 new = t;
644 continue;
648 * The normal case -- we are inserting into the
649 * middle of the list.
652 x = np;
653 np = np->n_flink;
654 x->n_flink = t;
655 x->n_blink = t->n_blink;
656 t->n_blink->n_flink = x;
657 t->n_blink = x;
661 * Now the list headed up by new is sorted.
662 * Go through it and remove duplicates.
665 np = new;
666 while (np != NULL) {
667 t = np;
668 while (t->n_flink != NULL &&
669 strcasecmp(np->n_name, t->n_flink->n_name) == 0)
670 t = t->n_flink;
671 if (t == np || t == NULL) {
672 np = np->n_flink;
673 continue;
677 * Now t points to the last entry with the same name
678 * as np. Make np point beyond t.
681 np->n_flink = t->n_flink;
682 if (t->n_flink != NULL)
683 t->n_flink->n_blink = np;
684 np = np->n_flink;
686 return (new);
690 * Put another node onto a list of names and return
691 * the list.
693 struct name *
694 put(struct name *list, struct name *node)
696 node->n_flink = list;
697 node->n_blink = NULL;
698 if (list != NULL)
699 list->n_blink = node;
700 return (node);
704 * Determine the number of undeleted elements in
705 * a name list and return it.
708 count(struct name *np)
710 int c;
712 for (c = 0; np != NULL; np = np->n_flink)
713 if ((np->n_type & GDEL) == 0)
714 c++;
715 return (c);
719 * Delete the given name from a namelist.
721 struct name *
722 delname(struct name *np, char *name)
724 struct name *p;
726 for (p = np; p != NULL; p = p->n_flink)
727 if (strcasecmp(p->n_name, name) == 0) {
728 if (p->n_blink == NULL) {
729 if (p->n_flink != NULL)
730 p->n_flink->n_blink = NULL;
731 np = p->n_flink;
732 continue;
734 if (p->n_flink == NULL) {
735 if (p->n_blink != NULL)
736 p->n_blink->n_flink = NULL;
737 continue;
739 p->n_blink->n_flink = p->n_flink;
740 p->n_flink->n_blink = p->n_blink;
742 return (np);
746 * Pretty print a name list
747 * Uncomment it if you need it.
751 void
752 prettyprint(struct name *name)
754 struct name *np;
756 np = name;
757 while (np != NULL) {
758 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
759 np = np->n_flink;
761 fprintf(stderr, "\n");