1292Seric # include <stdio.h>
2292Seric # include <ctype.h>
3292Seric # include <pwd.h>
4292Seric # include "dlvrmail.h"
5292Seric 
6*569Seric static char SccsId[] = "@(#)alias.c	1.3	08/02/80";
7402Seric 
8292Seric /*
9292Seric **  ALIAS -- Compute aliases.
10292Seric **
11292Seric **	Scans the file /usr/lib/mailaliases for a set of aliases.
12292Seric **	If found, it arranges to deliver to them by inserting the
13292Seric **	new names onto the SendQ queue.
14292Seric **
15292Seric **	Parameters:
16292Seric **		none
17292Seric **
18292Seric **	Returns:
19292Seric **		none
20292Seric **
21292Seric **	Side Effects:
22292Seric **		Aliases found on SendQ are removed and put onto
23292Seric **		AliasQ; replacements are added to SendQ.  This is
24292Seric **		done until no such replacement occurs.
25292Seric **
26292Seric **	Defined Constants:
27292Seric **		MAXRCRSN -- the maximum recursion depth.
28292Seric **		ALIASFILE -- the pathname of the alias file.
29292Seric **
30292Seric **	Called By:
31292Seric **		main
32292Seric **
33292Seric **	Files:
34*569Seric **		ALIASFILE -- the mail aliases.  The format is
35*569Seric **			a series of lines of the form:
36*569Seric **				alias:name1,name2,name3,...
37*569Seric **			where 'alias' expands to all of
38*569Seric **			'name[i]'.  Continuations begin with
39*569Seric **			space or tab.
40292Seric **
41292Seric **	Notes:
42292Seric **		If NoAlias (the "-n" flag) is set, no aliasing is
43292Seric **			done.
44292Seric **
45292Seric **	Deficiencies:
46292Seric **		It should complain about names that are aliased to
47292Seric **			nothing.
48292Seric **		It is unsophisticated about line overflows.
49292Seric */
50292Seric 
51292Seric 
52292Seric # define ALIASFILE	"/usr/lib/mailaliases"
53292Seric # define MAXRCRSN	10
54292Seric 
55292Seric 
56292Seric alias()
57292Seric {
58292Seric 	register addrq *q;
59292Seric 	FILE *af;
60292Seric 	char line[MAXLINE+1];
61292Seric 	register char *p;
62292Seric 	extern int errno;
63292Seric 	bool didalias;
64292Seric 	bool gotmatch;
65292Seric 	auto addrq al;
66292Seric 	extern bool sameaddr();
67292Seric 	extern addrq *parse();
68292Seric 
69292Seric 	if (NoAlias)
70292Seric 		return;
71292Seric # ifdef DEBUG
72292Seric 	if (Debug)
73292Seric 		printf("--- alias ---\n");
74292Seric # endif
75292Seric 
76292Seric 	/* open alias file if not already open */
77292Seric # ifdef DEBUG
78292Seric 	if (Debug && (af = fopen("mailaliases", "r")) != NULL)
79292Seric 		printf(" [using local alias file]\n");
80292Seric 	else
81292Seric # endif
82292Seric 	if ((af = fopen(ALIASFILE, "r")) == NULL)
83292Seric 	{
84292Seric # ifdef DEBUG
85292Seric 		if (Debug)
86292Seric 			printf("Can't open %s\n", ALIASFILE);
87292Seric # endif
88292Seric 		errno = 0;
89292Seric 		return;
90292Seric 	}
91292Seric 
92292Seric 	/*
93292Seric 	**  Scan alias file.
94292Seric 	**	If we find any user that any line matches any user, we
95292Seric 	**	will send to the line rather than to the user.
96292Seric 	**
97292Seric 	**	We pass through the file several times.  Didalias tells
98292Seric 	**	us if we took some alias on this pass through the file;
99292Seric 	**	when it goes false at the top of the loop we don't have
100292Seric 	**	to scan any more.  Gotmatch tells the same thing, but
101292Seric 	**	on a line-by-line basis; it is used for processing
102292Seric 	**	continuation lines.
103292Seric 	*/
104292Seric 
105292Seric 	didalias = TRUE;
106292Seric 	while (didalias)
107292Seric 	{
108292Seric 		didalias = FALSE;
109292Seric 		gotmatch = FALSE;
110292Seric 		rewind(af);
111292Seric 		while (fgets(line, sizeof line, af) != NULL)
112292Seric 		{
113292Seric 			/* comments begin with `#' */
114292Seric 			if (line[0] == '#')
115292Seric 				continue;
116292Seric 
117292Seric 			/* check for continuation lines */
118292Seric 			if (isspace(line[0]))
119292Seric 			{
120292Seric 				if (gotmatch)
121292Seric 				{
122292Seric # ifdef DEBUG
123292Seric 					if (Debug)
124292Seric 						printf("   ... also aliased to %s", line);
125292Seric # endif
126292Seric 					sendto(line, 1);
127292Seric 				}
128292Seric 				continue;
129292Seric 			}
130292Seric 			gotmatch = FALSE;
131292Seric 
132292Seric 			/*
133292Seric 			**  Check to see if this pseudonym exists in SendQ.
134292Seric 			**	Turn the alias into canonical form.
135292Seric 			**	Then scan SendQ until you do (or do not)
136292Seric 			**	find that address.
137292Seric 			*/
138292Seric 
139292Seric 			/*  Get a canonical form for the alias. */
140292Seric 			for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++)
141292Seric 				continue;
142292Seric 			if (*p == '\0' || *p == '\n')
143292Seric 			{
144292Seric 			 syntaxerr:
145292Seric 				syserr("Bad alias line `%s'", line);
146292Seric 				continue;
147292Seric 			}
148292Seric 			*p++ = '\0';
149292Seric 			if (parse(line, &al, -1) == NULL)
150292Seric 			{
151292Seric 				*--p = ':';
152292Seric 				goto syntaxerr;
153292Seric 			}
154292Seric 
155292Seric 			/*  Scan SendQ for that canonical form. */
156292Seric 			for (q = &SendQ; (q = nxtinq(q)) != NULL; )
157292Seric 			{
158292Seric 				if (sameaddr(&al, q, TRUE))
159292Seric 					break;
160292Seric 			}
161292Seric 			if (q != NULL)
162292Seric 			{
163292Seric 				/*
164292Seric 				**  Match on Alias.
165292Seric 				**	Deliver to the target list.
166292Seric 				**	Remove the alias from the send queue
167292Seric 				**	  and put it on the Alias queue.
168292Seric 				*/
169292Seric 
170292Seric # ifdef DEBUG
171292Seric 				if (Debug)
172292Seric 					printf("%s (%s, %s) aliased to %s (%s,%s,%s)\n",
173292Seric 					    q->q_paddr, q->q_host, q->q_user,
174292Seric 					    p, al.q_paddr, al.q_host, al.q_user);
175292Seric # endif
176292Seric 				tkoffq(q, &SendQ);
177292Seric 				putonq(q, &AliasQ);
178292Seric 				didalias++;
179292Seric 				gotmatch++;
180292Seric 				sendto(p, 1);
181292Seric 			}
182292Seric 		}
183292Seric 	}
184292Seric 	fclose(af);
185292Seric }
186292Seric /*
187292Seric **  FORWARD -- Try to forward mail
188292Seric **
189292Seric **	This is similar but not identical to aliasing.
190292Seric **
191292Seric **	Currently it is undefined, until the protocol for userinfo
192292Seric **	databases is finalized.
193292Seric **
194292Seric **	Parameters:
195292Seric **		user -- the name of the user who's mail we
196292Seric **			would like to forward to.
197292Seric **
198292Seric **	Returns:
199292Seric **		TRUE -- we have forwarded it somewhere.
200292Seric **		FALSE -- not forwarded; go ahead & deliver.
201292Seric **
202292Seric **	Side Effects:
203292Seric **		New names are added to SendQ.
204292Seric **
205292Seric **	Called By:
206292Seric **		recipient
207292Seric */
208292Seric 
209292Seric bool
210292Seric forward(user)
211292Seric 	addrq *user;
212292Seric {
213292Seric 	return (FALSE);
214292Seric }
215