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. 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
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
49 * Allocate a single element of a name list,
50 * initialize its name field to the passed
54 nalloc(char *str
, int ntype
)
58 np
= (struct name
*)salloc(sizeof(*np
));
62 np
->n_name
= savestr(str
);
67 * Find the tail of a list and return it.
70 tailof(struct name
*name
)
77 while (np
->n_flink
!= NULL
)
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.
88 extract(char *line
, int ntype
)
91 struct name
*top
, *np
, *t
;
93 if (line
== NULL
|| *line
== '\0')
95 if ((nbuf
= malloc(strlen(line
) + 1)) == NULL
)
96 err(1, "Out of memory");
100 while ((cp
= yankword(cp
, nbuf
)) != NULL
) {
101 t
= nalloc(nbuf
, ntype
);
114 * Turn a list of names into a string of the same names.
117 detract(struct name
*np
, int ntype
)
123 comma
= ntype
& GCOMMA
;
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
)
133 s
+= strlen(p
->n_name
) + 1;
142 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
143 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
145 cp
+= strlcpy(cp
, p
->n_name
, strlen(p
->n_name
) + 1);
146 if (comma
&& p
->n_flink
!= NULL
)
151 if (comma
&& *--cp
== ',')
157 * Grab a single word (liberal word)
158 * Throw away things between ()'s, and take anything between <>.
161 yankword(char *ap
, char *wbuf
)
172 while (*cp
!= '\0') {
184 } else if (*cp
== ' ' || *cp
== '\t' || *cp
== ',')
190 for (cp2
= wbuf
; *cp
&& (*cp2
++ = *cp
++) != '>';)
193 for (cp2
= wbuf
; *cp
!= '\0' && strchr(" \t,(", *cp
) == NULL
;
201 * Grab a single login name (liberal word).
202 * Throw away things between ()'s, take anything between <>,
203 * and look for words before metacharacters %, @, !.
206 yanklogin(char *ap
, char *wbuf
)
208 char *cp
, *cp2
, *cp_temp
;
218 while (*cp
!= '\0') {
230 } else if (*cp
== ' ' || *cp
== '\t' || *cp
== ',') {
238 * Now, let's go forward till we meet the needed character,
239 * and step one word back.
242 /* First, remember current point. */
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
)
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
) {
262 /* Finally, grab the word forward. */
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.
282 outof(struct name
*names
, FILE *fo
, struct header
*hp
)
285 struct name
*np
, *top
;
295 if (!isfileaddr(np
->n_name
) && np
->n_name
[0] != '|') {
299 ispipe
= np
->n_name
[0] == '|';
301 fname
= np
->n_name
+1;
303 fname
= expand(np
->n_name
);
306 * See if we have copied the complete message out yet.
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
);
322 image
= open(tempname
, O_RDWR
);
325 warn("%s", tempname
);
330 fcntl(image
, F_SETFD
, 1);
331 fprintf(fout
, "From %s %s", myname
, date
);
333 GTO
|GSUBJECT
|GCC
|GREPLYTO
|GINREPLYTO
|GNL
);
334 while ((c
= getc(fo
)) != EOF
)
340 warn("%s", tempname
);
349 * Now either copy "image" to the desired file
350 * or give it as the standard input to the desired
351 * program as appropriate.
361 * We can't really reuse the same image file,
362 * because multiple piped recipients will
363 * share the same lseek location and trample
366 if ((sh
= value("SHELL")) == NULL
)
369 sigaddset(&nset
, SIGHUP
);
370 sigaddset(&nset
, SIGINT
);
371 sigaddset(&nset
, SIGQUIT
);
372 pid
= start_command(sh
, &nset
, image
, -1, "-c", fname
,
381 if ((fout
= Fopen(fname
, "a")) == NULL
) {
386 if ((f
= dup(image
)) < 0) {
390 fin
= Fdopen(f
, "r");
392 fprintf(stderr
, "Can't reopen image\n");
398 while ((c
= getc(fin
)) != EOF
)
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.
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
)
438 for (cp
= name
; *cp
!= '\0'; cp
++) {
439 if (*cp
== '!' || *cp
== '%' || *cp
== '@')
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).
455 usermap(struct name
*names
)
457 struct name
*new, *np
, *cp
;
458 struct grouphead
*gh
;
463 metoo
= (value("metoo") != NULL
);
465 if (np
->n_name
[0] == '\\') {
471 gh
= findgroup(np
->n_name
);
474 new = gexpand(new, gh
, metoo
, np
->n_type
);
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.
489 gexpand(struct name
*nlist
, struct grouphead
*gh
, int metoo
, int ntype
)
492 struct grouphead
*ngh
;
497 if (depth
> MAXEXP
) {
498 printf("Expanding alias to depth larger than %d\n", MAXEXP
);
502 for (gp
= gh
->g_list
; gp
!= NULL
; gp
= gp
->ge_link
) {
506 if (strcmp(cp
, gh
->g_name
) == 0)
508 if ((ngh
= findgroup(cp
)) != NULL
) {
509 nlist
= gexpand(nlist
, ngh
, metoo
, ntype
);
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
)
520 if (!metoo
&& strcmp(cp
, myname
) == 0)
523 nlist
= put(nlist
, np
);
530 * Concatenate the two passed name lists, return the result.
533 cat(struct name
*n1
, struct name
*n2
)
548 * Unpack the name list onto a vector of strings.
549 * Return an error if the name list won't fit.
552 unpack(struct name
*np
)
556 int t
, extra
, metoo
, verbose
;
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.
569 metoo
= value("metoo") != NULL
;
572 verbose
= value("verbose") != NULL
;
575 top
= (char **)salloc((t
+ extra
) * sizeof(*top
));
583 for (; n
!= NULL
; n
= n
->n_flink
)
584 if ((n
->n_type
& GDEL
) == 0)
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.
596 elide(struct name
*names
)
598 struct name
*np
, *t
, *new;
611 while (strcasecmp(t
->n_name
, np
->n_name
) < 0) {
612 if (t
->n_flink
== NULL
)
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) {
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.
648 * The normal case -- we are inserting into the
649 * middle of the list.
655 x
->n_blink
= t
->n_blink
;
656 t
->n_blink
->n_flink
= x
;
661 * Now the list headed up by new is sorted.
662 * Go through it and remove duplicates.
668 while (t
->n_flink
!= NULL
&&
669 strcasecmp(np
->n_name
, t
->n_flink
->n_name
) == 0)
671 if (t
== np
|| t
== NULL
) {
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
;
690 * Put another node onto a list of names and return
694 put(struct name
*list
, struct name
*node
)
696 node
->n_flink
= list
;
697 node
->n_blink
= NULL
;
699 list
->n_blink
= node
;
704 * Determine the number of undeleted elements in
705 * a name list and return it.
708 count(struct name
*np
)
712 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
713 if ((np
->n_type
& GDEL
) == 0)
719 * Delete the given name from a namelist.
722 delname(struct name
*np
, char *name
)
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
;
734 if (p
->n_flink
== NULL
) {
735 if (p
->n_blink
!= NULL
)
736 p
->n_blink
->n_flink
= NULL
;
739 p
->n_blink
->n_flink
= p
->n_flink
;
740 p
->n_flink
->n_blink
= p
->n_blink
;
746 * Pretty print a name list
747 * Uncomment it if you need it.
752 prettyprint(struct name *name)
758 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
761 fprintf(stderr, "\n");