Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / mail / names.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
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.
20  *
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.
32  *
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.2 2003/06/17 04:29:28 dillon Exp $
36  */
37
38 /*
39  * Mail -- a mail program
40  *
41  * Handle name lists.
42  */
43
44 #include "rcv.h"
45 #include <fcntl.h>
46 #include "extern.h"
47
48 /*
49  * Allocate a single element of a name list,
50  * initialize its name field to the passed
51  * name and return it.
52  */
53 struct name *
54 nalloc(str, ntype)
55         char str[];
56         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(name)
73         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(line, ntype)
92         char line[];
93         int ntype;
94 {
95         char *cp, *nbuf;
96         struct name *top, *np, *t;
97
98         if (line == NULL || *line == '\0')
99                 return (NULL);
100         if ((nbuf = malloc(strlen(line) + 1)) == NULL)
101                 err(1, "Out of memory");
102         top = NULL;
103         np = NULL;
104         cp = line;
105         while ((cp = yankword(cp, nbuf)) != NULL) {
106                 t = nalloc(nbuf, ntype);
107                 if (top == NULL)
108                         top = t;
109                 else
110                         np->n_flink = t;
111                 t->n_blink = np;
112                 np = t;
113         }
114         (void)free(nbuf);
115         return (top);
116 }
117
118 /*
119  * Turn a list of names into a string of the same names.
120  */
121 char *
122 detract(np, ntype)
123         struct name *np;
124         int ntype;
125 {
126         int s, comma;
127         char *cp, *top;
128         struct name *p;
129
130         comma = ntype & GCOMMA;
131         if (np == NULL)
132                 return (NULL);
133         ntype &= ~GCOMMA;
134         s = 0;
135         if (debug && comma)
136                 fprintf(stderr, "detract asked to insert commas\n");
137         for (p = np; p != NULL; p = p->n_flink) {
138                 if (ntype && (p->n_type & GMASK) != ntype)
139                         continue;
140                 s += strlen(p->n_name) + 1;
141                 if (comma)
142                         s++;
143         }
144         if (s == 0)
145                 return (NULL);
146         s += 2;
147         top = salloc(s);
148         cp = top;
149         for (p = np; p != NULL; p = p->n_flink) {
150                 if (ntype && (p->n_type & GMASK) != ntype)
151                         continue;
152                 cp += strlcpy(cp, p->n_name, strlen(p->n_name) + 1);
153                 if (comma && p->n_flink != NULL)
154                         *cp++ = ',';
155                 *cp++ = ' ';
156         }
157         *--cp = '\0';
158         if (comma && *--cp == ',')
159                 *cp = '\0';
160         return (top);
161 }
162
163 /*
164  * Grab a single word (liberal word)
165  * Throw away things between ()'s, and take anything between <>.
166  */
167 char *
168 yankword(ap, wbuf)
169         char *ap, wbuf[];
170 {
171         char *cp, *cp2;
172
173         cp = ap;
174         for (;;) {
175                 if (*cp == '\0')
176                         return (NULL);
177                 if (*cp == '(') {
178                         int nesting = 0;
179
180                         while (*cp != '\0') {
181                                 switch (*cp++) {
182                                 case '(':
183                                         nesting++;
184                                         break;
185                                 case ')':
186                                         --nesting;
187                                         break;
188                                 }
189                                 if (nesting <= 0)
190                                         break;
191                         }
192                 } else if (*cp == ' ' || *cp == '\t' || *cp == ',')
193                         cp++;
194                 else
195                         break;
196         }
197         if (*cp ==  '<')
198                 for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';)
199                         ;
200         else
201                 for (cp2 = wbuf; *cp != '\0' && strchr(" \t,(", *cp) == NULL;
202                     *cp2++ = *cp++)
203                         ;
204         *cp2 = '\0';
205         return (cp);
206 }
207
208 /*
209  * For each recipient in the passed name list with a /
210  * in the name, append the message to the end of the named file
211  * and remove him from the recipient list.
212  *
213  * Recipients whose name begins with | are piped through the given
214  * program and removed.
215  */
216 struct name *
217 outof(names, fo, hp)
218         struct name *names;
219         FILE *fo;
220         struct header *hp;
221 {
222         int c, ispipe;
223         struct name *np, *top;
224         time_t now;
225         char *date, *fname;
226         FILE *fout, *fin;
227
228         top = names;
229         np = names;
230         (void)time(&now);
231         date = ctime(&now);
232         while (np != NULL) {
233                 if (!isfileaddr(np->n_name) && np->n_name[0] != '|') {
234                         np = np->n_flink;
235                         continue;
236                 }
237                 ispipe = np->n_name[0] == '|';
238                 if (ispipe)
239                         fname = np->n_name+1;
240                 else
241                         fname = expand(np->n_name);
242
243                 /*
244                  * See if we have copied the complete message out yet.
245                  * If not, do so.
246                  */
247
248                 if (image < 0) {
249                         int fd;
250                         char tempname[PATHSIZE];
251
252                         (void)snprintf(tempname, sizeof(tempname),
253                             "%s/mail.ReXXXXXXXXXX", tmpdir);
254                         if ((fd = mkstemp(tempname)) == -1 ||
255                             (fout = Fdopen(fd, "a")) == NULL) {
256                                 warn("%s", tempname);
257                                 senderr++;
258                                 goto cant;
259                         }
260                         image = open(tempname, O_RDWR);
261                         (void)rm(tempname);
262                         if (image < 0) {
263                                 warn("%s", tempname);
264                                 senderr++;
265                                 (void)Fclose(fout);
266                                 goto cant;
267                         }
268                         (void)fcntl(image, F_SETFD, 1);
269                         fprintf(fout, "From %s %s", myname, date);
270                         puthead(hp, fout,
271                             GTO|GSUBJECT|GCC|GREPLYTO|GINREPLYTO|GNL);
272                         while ((c = getc(fo)) != EOF)
273                                 (void)putc(c, fout);
274                         rewind(fo);
275                         fprintf(fout, "\n");
276                         (void)fflush(fout);
277                         if (ferror(fout)) {
278                                 warn("%s", tempname);
279                                 senderr++;
280                                 (void)Fclose(fout);
281                                 goto cant;
282                         }
283                         (void)Fclose(fout);
284                 }
285
286                 /*
287                  * Now either copy "image" to the desired file
288                  * or give it as the standard input to the desired
289                  * program as appropriate.
290                  */
291
292                 if (ispipe) {
293                         int pid;
294                         char *sh;
295                         sigset_t nset;
296
297                         /*
298                          * XXX
299                          * We can't really reuse the same image file,
300                          * because multiple piped recipients will
301                          * share the same lseek location and trample
302                          * on one another.
303                          */
304                         if ((sh = value("SHELL")) == NULL)
305                                 sh = _PATH_CSHELL;
306                         (void)sigemptyset(&nset);
307                         (void)sigaddset(&nset, SIGHUP);
308                         (void)sigaddset(&nset, SIGINT);
309                         (void)sigaddset(&nset, SIGQUIT);
310                         pid = start_command(sh, &nset, image, -1, "-c", fname,
311                             NULL);
312                         if (pid < 0) {
313                                 senderr++;
314                                 goto cant;
315                         }
316                         free_child(pid);
317                 } else {
318                         int f;
319                         if ((fout = Fopen(fname, "a")) == NULL) {
320                                 warn("%s", fname);
321                                 senderr++;
322                                 goto cant;
323                         }
324                         if ((f = dup(image)) < 0) {
325                                 warn("dup");
326                                 fin = NULL;
327                         } else
328                                 fin = Fdopen(f, "r");
329                         if (fin == NULL) {
330                                 fprintf(stderr, "Can't reopen image\n");
331                                 (void)Fclose(fout);
332                                 senderr++;
333                                 goto cant;
334                         }
335                         rewind(fin);
336                         while ((c = getc(fin)) != EOF)
337                                 (void)putc(c, fout);
338                         if (ferror(fout)) {
339                                 warnx("%s", fname);
340                                 senderr++;
341                                 (void)Fclose(fout);
342                                 (void)Fclose(fin);
343                                 goto cant;
344                         }
345                         (void)Fclose(fout);
346                         (void)Fclose(fin);
347                 }
348 cant:
349                 /*
350                  * In days of old we removed the entry from the
351                  * the list; now for sake of header expansion
352                  * we leave it in and mark it as deleted.
353                  */
354                 np->n_type |= GDEL;
355                 np = np->n_flink;
356         }
357         if (image >= 0) {
358                 (void)close(image);
359                 image = -1;
360         }
361         return (top);
362 }
363
364 /*
365  * Determine if the passed address is a local "send to file" address.
366  * If any of the network metacharacters precedes any slashes, it can't
367  * be a filename.  We cheat with .'s to allow path names like ./...
368  */
369 int
370 isfileaddr(name)
371         char *name;
372 {
373         char *cp;
374
375         if (*name == '+')
376                 return (1);
377         for (cp = name; *cp != '\0'; cp++) {
378                 if (*cp == '!' || *cp == '%' || *cp == '@')
379                         return (0);
380                 if (*cp == '/')
381                         return (1);
382         }
383         return (0);
384 }
385
386 /*
387  * Map all of the aliased users in the invoker's mailrc
388  * file and insert them into the list.
389  * Changed after all these months of service to recursively
390  * expand names (2/14/80).
391  */
392
393 struct name *
394 usermap(names)
395         struct name *names;
396 {
397         struct name *new, *np, *cp;
398         struct grouphead *gh;
399         int metoo;
400
401         new = NULL;
402         np = names;
403         metoo = (value("metoo") != NULL);
404         while (np != NULL) {
405                 if (np->n_name[0] == '\\') {
406                         cp = np->n_flink;
407                         new = put(new, np);
408                         np = cp;
409                         continue;
410                 }
411                 gh = findgroup(np->n_name);
412                 cp = np->n_flink;
413                 if (gh != NULL)
414                         new = gexpand(new, gh, metoo, np->n_type);
415                 else
416                         new = put(new, np);
417                 np = cp;
418         }
419         return (new);
420 }
421
422 /*
423  * Recursively expand a group name.  We limit the expansion to some
424  * fixed level to keep things from going haywire.
425  * Direct recursion is not expanded for convenience.
426  */
427
428 struct name *
429 gexpand(nlist, gh, metoo, ntype)
430         struct name *nlist;
431         struct grouphead *gh;
432         int metoo, ntype;
433 {
434         struct group *gp;
435         struct grouphead *ngh;
436         struct name *np;
437         static int depth;
438         char *cp;
439
440         if (depth > MAXEXP) {
441                 printf("Expanding alias to depth larger than %d\n", MAXEXP);
442                 return (nlist);
443         }
444         depth++;
445         for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) {
446                 cp = gp->ge_name;
447                 if (*cp == '\\')
448                         goto quote;
449                 if (strcmp(cp, gh->g_name) == 0)
450                         goto quote;
451                 if ((ngh = findgroup(cp)) != NULL) {
452                         nlist = gexpand(nlist, ngh, metoo, ntype);
453                         continue;
454                 }
455 quote:
456                 np = nalloc(cp, ntype);
457                 /*
458                  * At this point should allow to expand
459                  * to self if only person in group
460                  */
461                 if (gp == gh->g_list && gp->ge_link == NULL)
462                         goto skip;
463                 if (!metoo && strcmp(cp, myname) == 0)
464                         np->n_type |= GDEL;
465 skip:
466                 nlist = put(nlist, np);
467         }
468         depth--;
469         return (nlist);
470 }
471
472 /*
473  * Concatenate the two passed name lists, return the result.
474  */
475 struct name *
476 cat(n1, n2)
477         struct name *n1, *n2;
478 {
479         struct name *tail;
480
481         if (n1 == NULL)
482                 return (n2);
483         if (n2 == NULL)
484                 return (n1);
485         tail = tailof(n1);
486         tail->n_flink = n2;
487         n2->n_blink = tail;
488         return (n1);
489 }
490
491 /*
492  * Unpack the name list onto a vector of strings.
493  * Return an error if the name list won't fit.
494  */
495 char **
496 unpack(np)
497         struct name *np;
498 {
499         char **ap, **top;
500         struct name *n;
501         int t, extra, metoo, verbose;
502
503         n = np;
504         if ((t = count(n)) == 0)
505                 errx(1, "No names to unpack");
506         /*
507          * Compute the number of extra arguments we will need.
508          * We need at least two extra -- one for "mail" and one for
509          * the terminating 0 pointer.  Additional spots may be needed
510          * to pass along -f to the host mailer.
511          */
512         extra = 2;
513         extra++;
514         metoo = value("metoo") != NULL;
515         if (metoo)
516                 extra++;
517         verbose = value("verbose") != NULL;
518         if (verbose)
519                 extra++;
520         top = (char **)salloc((t + extra) * sizeof(*top));
521         ap = top;
522         *ap++ = "send-mail";
523         *ap++ = "-i";
524         if (metoo)
525                 *ap++ = "-m";
526         if (verbose)
527                 *ap++ = "-v";
528         for (; n != NULL; n = n->n_flink)
529                 if ((n->n_type & GDEL) == 0)
530                         *ap++ = n->n_name;
531         *ap = NULL;
532         return (top);
533 }
534
535 /*
536  * Remove all of the duplicates from the passed name list by
537  * insertion sorting them, then checking for dups.
538  * Return the head of the new list.
539  */
540 struct name *
541 elide(names)
542         struct name *names;
543 {
544         struct name *np, *t, *new;
545         struct name *x;
546
547         if (names == NULL)
548                 return (NULL);
549         new = names;
550         np = names;
551         np = np->n_flink;
552         if (np != NULL)
553                 np->n_blink = NULL;
554         new->n_flink = NULL;
555         while (np != NULL) {
556                 t = new;
557                 while (strcasecmp(t->n_name, np->n_name) < 0) {
558                         if (t->n_flink == NULL)
559                                 break;
560                         t = t->n_flink;
561                 }
562
563                 /*
564                  * If we ran out of t's, put the new entry after
565                  * the current value of t.
566                  */
567
568                 if (strcasecmp(t->n_name, np->n_name) < 0) {
569                         t->n_flink = np;
570                         np->n_blink = t;
571                         t = np;
572                         np = np->n_flink;
573                         t->n_flink = NULL;
574                         continue;
575                 }
576
577                 /*
578                  * Otherwise, put the new entry in front of the
579                  * current t.  If at the front of the list,
580                  * the new guy becomes the new head of the list.
581                  */
582
583                 if (t == new) {
584                         t = np;
585                         np = np->n_flink;
586                         t->n_flink = new;
587                         new->n_blink = t;
588                         t->n_blink = NULL;
589                         new = t;
590                         continue;
591                 }
592
593                 /*
594                  * The normal case -- we are inserting into the
595                  * middle of the list.
596                  */
597
598                 x = np;
599                 np = np->n_flink;
600                 x->n_flink = t;
601                 x->n_blink = t->n_blink;
602                 t->n_blink->n_flink = x;
603                 t->n_blink = x;
604         }
605
606         /*
607          * Now the list headed up by new is sorted.
608          * Go through it and remove duplicates.
609          */
610
611         np = new;
612         while (np != NULL) {
613                 t = np;
614                 while (t->n_flink != NULL &&
615                     strcasecmp(np->n_name, t->n_flink->n_name) == 0)
616                         t = t->n_flink;
617                 if (t == np || t == NULL) {
618                         np = np->n_flink;
619                         continue;
620                 }
621
622                 /*
623                  * Now t points to the last entry with the same name
624                  * as np.  Make np point beyond t.
625                  */
626
627                 np->n_flink = t->n_flink;
628                 if (t->n_flink != NULL)
629                         t->n_flink->n_blink = np;
630                 np = np->n_flink;
631         }
632         return (new);
633 }
634
635 /*
636  * Put another node onto a list of names and return
637  * the list.
638  */
639 struct name *
640 put(list, node)
641         struct name *list, *node;
642 {
643         node->n_flink = list;
644         node->n_blink = NULL;
645         if (list != NULL)
646                 list->n_blink = node;
647         return (node);
648 }
649
650 /*
651  * Determine the number of undeleted elements in
652  * a name list and return it.
653  */
654 int
655 count(np)
656         struct name *np;
657 {
658         int c;
659
660         for (c = 0; np != NULL; np = np->n_flink)
661                 if ((np->n_type & GDEL) == 0)
662                         c++;
663         return (c);
664 }
665
666 /*
667  * Delete the given name from a namelist.
668  */
669 struct name *
670 delname(np, name)
671         struct name *np;
672         char name[];
673 {
674         struct name *p;
675
676         for (p = np; p != NULL; p = p->n_flink)
677                 if (strcasecmp(p->n_name, name) == 0) {
678                         if (p->n_blink == NULL) {
679                                 if (p->n_flink != NULL)
680                                         p->n_flink->n_blink = NULL;
681                                 np = p->n_flink;
682                                 continue;
683                         }
684                         if (p->n_flink == NULL) {
685                                 if (p->n_blink != NULL)
686                                         p->n_blink->n_flink = NULL;
687                                 continue;
688                         }
689                         p->n_blink->n_flink = p->n_flink;
690                         p->n_flink->n_blink = p->n_blink;
691                 }
692         return (np);
693 }
694
695 /*
696  * Pretty print a name list
697  * Uncomment it if you need it.
698  */
699
700 /*
701 void
702 prettyprint(name)
703         struct name *name;
704 {
705         struct name *np;
706
707         np = name;
708         while (np != NULL) {
709                 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
710                 np = np->n_flink;
711         }
712         fprintf(stderr, "\n");
713 }
714 */