xref: /original-bsd/usr.sbin/sendmail/src/alias.c (revision cf2061f8)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 # include "sendmail.h"
10 # include <pwd.h>
11 
12 #ifndef lint
13 static char sccsid[] = "@(#)alias.c	8.16 (Berkeley) 09/25/93";
14 #endif /* not lint */
15 
16 
17 MAP	*AliasDB[MAXALIASDB + 1];	/* actual database list */
18 int	NAliasDBs;			/* number of alias databases */
19 /*
20 **  ALIAS -- Compute aliases.
21 **
22 **	Scans the alias file for an alias for the given address.
23 **	If found, it arranges to deliver to the alias list instead.
24 **	Uses libdbm database if -DDBM.
25 **
26 **	Parameters:
27 **		a -- address to alias.
28 **		sendq -- a pointer to the head of the send queue
29 **			to put the aliases in.
30 **		e -- the current envelope.
31 **
32 **	Returns:
33 **		none
34 **
35 **	Side Effects:
36 **		Aliases found are expanded.
37 **
38 **	Deficiencies:
39 **		It should complain about names that are aliased to
40 **			nothing.
41 */
42 
43 alias(a, sendq, e)
44 	register ADDRESS *a;
45 	ADDRESS **sendq;
46 	register ENVELOPE *e;
47 {
48 	register char *p;
49 	int naliases;
50 	char *owner;
51 	char obuf[MAXNAME + 6];
52 	extern char *aliaslookup();
53 
54 	if (tTd(27, 1))
55 		printf("alias(%s)\n", a->q_paddr);
56 
57 	/* don't realias already aliased names */
58 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
59 		return;
60 
61 	if (NoAlias)
62 		return;
63 
64 	e->e_to = a->q_paddr;
65 
66 	/*
67 	**  Look up this name
68 	*/
69 
70 	p = aliaslookup(a->q_user, e);
71 	if (p == NULL)
72 		return;
73 
74 	/*
75 	**  Match on Alias.
76 	**	Deliver to the target list.
77 	*/
78 
79 	if (tTd(27, 1))
80 		printf("%s (%s, %s) aliased to %s\n",
81 		    a->q_paddr, a->q_host, a->q_user, p);
82 	if (bitset(EF_VRFYONLY, e->e_flags))
83 	{
84 		a->q_flags |= QVERIFIED;
85 		e->e_nrcpts++;
86 		return;
87 	}
88 	message("aliased to %s", p);
89 #ifdef LOG
90 	if (LogLevel > 9)
91 		syslog(LOG_INFO, "%s: alias %s => %s", e->e_id, a->q_paddr, p);
92 #endif
93 	a->q_flags &= ~QSELFREF;
94 	AliasLevel++;
95 	naliases = sendtolist(p, a, sendq, e);
96 	AliasLevel--;
97 	if (!bitset(QSELFREF, a->q_flags))
98 	{
99 		if (tTd(27, 5))
100 		{
101 			printf("alias: QDONTSEND ");
102 			printaddr(a, FALSE);
103 		}
104 		a->q_flags |= QDONTSEND;
105 	}
106 
107 	/*
108 	**  Look for owner of alias
109 	*/
110 
111 	(void) strcpy(obuf, "owner-");
112 	if (strncmp(a->q_user, "owner-", 6) == 0)
113 		(void) strcat(obuf, "owner");
114 	else
115 		(void) strcat(obuf, a->q_user);
116 	if (!bitnset(M_USR_UPPER, a->q_mailer->m_flags))
117 		makelower(obuf);
118 	owner = aliaslookup(obuf, e);
119 	if (owner != NULL)
120 	{
121 		if (strchr(owner, ',') != NULL)
122 			owner = obuf;
123 		a->q_owner = newstr(owner);
124 	}
125 }
126 /*
127 **  ALIASLOOKUP -- look up a name in the alias file.
128 **
129 **	Parameters:
130 **		name -- the name to look up.
131 **
132 **	Returns:
133 **		the value of name.
134 **		NULL if unknown.
135 **
136 **	Side Effects:
137 **		none.
138 **
139 **	Warnings:
140 **		The return value will be trashed across calls.
141 */
142 
143 char *
144 aliaslookup(name, e)
145 	char *name;
146 	ENVELOPE *e;
147 {
148 	register int dbno;
149 	register MAP *map;
150 	register char *p;
151 
152 	for (dbno = 0; dbno < NAliasDBs; dbno++)
153 	{
154 		auto int stat;
155 
156 		map = AliasDB[dbno];
157 		if (!bitset(MF_OPEN, map->map_mflags))
158 			continue;
159 		p = (*map->map_class->map_lookup)(map, name, NULL, &stat);
160 		if (p != NULL)
161 			return p;
162 	}
163 	return NULL;
164 }
165 /*
166 **  SETALIAS -- set up an alias map
167 **
168 **	Called when reading configuration file.
169 **
170 **	Parameters:
171 **		spec -- the alias specification
172 **
173 **	Returns:
174 **		none.
175 */
176 
177 setalias(spec)
178 	char *spec;
179 {
180 	register char *p;
181 	register MAP *map;
182 	char *class;
183 	STAB *s;
184 
185 	if (tTd(27, 8))
186 		printf("setalias(%s)\n", spec);
187 
188 	for (p = spec; p != NULL; )
189 	{
190 		char aname[50];
191 
192 		while (isspace(*p))
193 			p++;
194 		if (*p == '\0')
195 			break;
196 		spec = p;
197 
198 		if (NAliasDBs >= MAXALIASDB)
199 		{
200 			syserr("Too many alias databases defined, %d max", MAXALIASDB);
201 			return;
202 		}
203 		(void) sprintf(aname, "Alias%d", NAliasDBs);
204 		s = stab(aname, ST_MAP, ST_ENTER);
205 		map = &s->s_map;
206 		AliasDB[NAliasDBs] = map;
207 		bzero(map, sizeof *map);
208 
209 		p = strpbrk(p, " ,/:");
210 		if (p != NULL && *p == ':')
211 		{
212 			/* map name */
213 			*p++ = '\0';
214 			class = spec;
215 			spec = p;
216 		}
217 		else
218 		{
219 			class = "implicit";
220 			map->map_mflags = MF_OPTIONAL|MF_INCLNULL;
221 		}
222 
223 		/* find end of spec */
224 		if (p != NULL)
225 			p = strchr(p, ',');
226 		if (p != NULL)
227 			*p++ = '\0';
228 
229 		/* look up class */
230 		s = stab(class, ST_MAPCLASS, ST_FIND);
231 		if (s == NULL)
232 		{
233 			if (tTd(27, 1))
234 				printf("Unknown alias class %s\n", class);
235 		}
236 		else if (!bitset(MCF_ALIASOK, s->s_mapclass.map_cflags))
237 		{
238 			syserr("setalias: map class %s can't handle aliases",
239 				class);
240 		}
241 		else
242 		{
243 			map->map_class = &s->s_mapclass;
244 			if (map->map_class->map_parse(map, spec))
245 			{
246 				map->map_mflags |= MF_VALID|MF_ALIAS;
247 				NAliasDBs++;
248 			}
249 		}
250 	}
251 }
252 /*
253 **  ALIASWAIT -- wait for distinguished @:@ token to appear.
254 **
255 **	This can decide to reopen or rebuild the alias file
256 */
257 
258 aliaswait(map, ext)
259 	MAP *map;
260 	char *ext;
261 {
262 	int atcnt;
263 	time_t mtime;
264 	struct stat stb;
265 	char buf[MAXNAME];
266 
267 	if (tTd(27, 3))
268 		printf("aliaswait(%s:%s)\n",
269 			map->map_class->map_cname, map->map_file);
270 	if (bitset(MF_ALIASWAIT, map->map_mflags))
271 		return;
272 	map->map_mflags |= MF_ALIASWAIT;
273 
274 	atcnt = SafeAlias * 2;
275 	if (atcnt > 0)
276 	{
277 		auto int st;
278 
279 		while (atcnt-- >= 0 &&
280 		       map->map_class->map_lookup(map, "@", NULL, &st) == NULL)
281 		{
282 			/*
283 			**  Close and re-open the alias database in case
284 			**  the one is mv'ed instead of cp'ed in.
285 			*/
286 
287 			if (tTd(27, 2))
288 				printf("aliaswait: sleeping\n");
289 
290 			map->map_class->map_close(map);
291 			sleep(30);
292 			map->map_class->map_open(map, O_RDONLY);
293 		}
294 	}
295 
296 	/* see if we need to go into auto-rebuild mode */
297 	if (!bitset(MCF_REBUILDABLE, map->map_class->map_cflags))
298 	{
299 		if (tTd(27, 3))
300 			printf("aliaswait: not rebuildable\n");
301 		map->map_mflags &= ~MF_ALIASWAIT;
302 		return;
303 	}
304 	if (stat(map->map_file, &stb) < 0)
305 	{
306 		if (tTd(27, 3))
307 			printf("aliaswait: no source file\n");
308 		map->map_mflags &= ~MF_ALIASWAIT;
309 		return;
310 	}
311 	mtime = stb.st_mtime;
312 	(void) strcpy(buf, map->map_file);
313 	if (ext != NULL)
314 		(void) strcat(buf, ext);
315 	if (stat(buf, &stb) < 0 || stb.st_mtime < mtime || atcnt < 0)
316 	{
317 		/* database is out of date */
318 		if (AutoRebuild && stb.st_ino != 0 && stb.st_uid == geteuid())
319 		{
320 			message("auto-rebuilding alias database %s", buf);
321 			rebuildaliases(map, TRUE);
322 		}
323 		else
324 		{
325 #ifdef LOG
326 			if (LogLevel > 3)
327 				syslog(LOG_INFO, "alias database %s out of date",
328 					buf);
329 #endif /* LOG */
330 			message("Warning: alias database %s out of date", buf);
331 		}
332 	}
333 	map->map_mflags &= ~MF_ALIASWAIT;
334 }
335 /*
336 **  REBUILDALIASES -- rebuild the alias database.
337 **
338 **	Parameters:
339 **		map -- the database to rebuild.
340 **		automatic -- set if this was automatically generated.
341 **
342 **	Returns:
343 **		none.
344 **
345 **	Side Effects:
346 **		Reads the text version of the database, builds the
347 **		DBM or DB version.
348 */
349 
350 rebuildaliases(map, automatic)
351 	register MAP *map;
352 	bool automatic;
353 {
354 	FILE *af;
355 	bool nolock = FALSE;
356 	sigfunc_t oldsigint;
357 
358 	if (!bitset(MCF_REBUILDABLE, map->map_class->map_cflags))
359 		return;
360 
361 	/* try to lock the source file */
362 	if ((af = fopen(map->map_file, "r+")) == NULL)
363 	{
364 		if (errno != EACCES || automatic ||
365 		    (af = fopen(map->map_file, "r")) == NULL)
366 		{
367 			int saveerr = errno;
368 
369 			if (tTd(27, 1))
370 				printf("Can't open %s: %s\n",
371 					map->map_file, errstring(saveerr));
372 			if (!automatic)
373 				message("newaliases: cannot open %s: %s",
374 					map->map_file, errstring(saveerr));
375 			errno = 0;
376 			return;
377 		}
378 		nolock = TRUE;
379 		message("warning: cannot lock %s: %s",
380 			map->map_file, errstring(errno));
381 	}
382 
383 	/* see if someone else is rebuilding the alias file */
384 	if (!nolock &&
385 	    !lockfile(fileno(af), map->map_file, NULL, LOCK_EX|LOCK_NB))
386 	{
387 		/* yes, they are -- wait until done */
388 		message("Alias file %s is already being rebuilt",
389 			map->map_file);
390 		if (OpMode != MD_INITALIAS)
391 		{
392 			/* wait for other rebuild to complete */
393 			(void) lockfile(fileno(af), map->map_file, NULL,
394 					LOCK_EX);
395 		}
396 		(void) fclose(af);
397 		errno = 0;
398 		return;
399 	}
400 
401 	oldsigint = setsignal(SIGINT, SIG_IGN);
402 
403 	if (map->map_class->map_open(map, O_RDWR))
404 	{
405 #ifdef LOG
406 		if (LogLevel > 7)
407 		{
408 			syslog(LOG_NOTICE, "alias database %s %srebuilt by %s",
409 				map->map_file, automatic ? "auto" : "",
410 				username());
411 		}
412 #endif /* LOG */
413 		map->map_mflags |= MF_OPEN|MF_WRITABLE;
414 		readaliases(map, af, automatic);
415 	}
416 	else
417 	{
418 		if (tTd(27, 1))
419 			printf("Can't create database for %s: %s\n",
420 				map->map_file, errstring(errno));
421 		if (!automatic)
422 			syserr("Cannot create database for alias file %s",
423 				map->map_file);
424 	}
425 
426 	/* close the file, thus releasing locks */
427 	fclose(af);
428 
429 	/* add distinguished entries and close the database */
430 	if (bitset(MF_OPEN, map->map_mflags))
431 		map->map_class->map_close(map);
432 
433 	/* restore the old signal */
434 	(void) setsignal(SIGINT, oldsigint);
435 }
436 /*
437 **  READALIASES -- read and process the alias file.
438 **
439 **	This routine implements the part of initaliases that occurs
440 **	when we are not going to use the DBM stuff.
441 **
442 **	Parameters:
443 **		map -- the alias database descriptor.
444 **		af -- file to read the aliases from.
445 **		automatic -- set if this was an automatic rebuild.
446 **
447 **	Returns:
448 **		none.
449 **
450 **	Side Effects:
451 **		Reads aliasfile into the symbol table.
452 **		Optionally, builds the .dir & .pag files.
453 */
454 
455 readaliases(map, af, automatic)
456 	register MAP *map;
457 	FILE *af;
458 	int automatic;
459 {
460 	register char *p;
461 	char *rhs;
462 	bool skipping;
463 	long naliases, bytes, longest;
464 	ADDRESS al, bl;
465 	char line[BUFSIZ];
466 
467 	/*
468 	**  Read and interpret lines
469 	*/
470 
471 	FileName = map->map_file;
472 	LineNumber = 0;
473 	naliases = bytes = longest = 0;
474 	skipping = FALSE;
475 	while (fgets(line, sizeof (line), af) != NULL)
476 	{
477 		int lhssize, rhssize;
478 
479 		LineNumber++;
480 		p = strchr(line, '\n');
481 		if (p != NULL)
482 			*p = '\0';
483 		switch (line[0])
484 		{
485 		  case '#':
486 		  case '\0':
487 			skipping = FALSE;
488 			continue;
489 
490 		  case ' ':
491 		  case '\t':
492 			if (!skipping)
493 				syserr("554 Non-continuation line starts with space");
494 			skipping = TRUE;
495 			continue;
496 		}
497 		skipping = FALSE;
498 
499 		/*
500 		**  Process the LHS
501 		**	Find the colon separator, and parse the address.
502 		**	It should resolve to a local name -- this will
503 		**	be checked later (we want to optionally do
504 		**	parsing of the RHS first to maximize error
505 		**	detection).
506 		*/
507 
508 		for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++)
509 			continue;
510 		if (*p++ != ':')
511 		{
512 			syserr("554 missing colon");
513 			continue;
514 		}
515 		if (parseaddr(line, &al, RF_COPYALL, ':', NULL, CurEnv) == NULL)
516 		{
517 			syserr("554 %s... illegal alias name", al.q_paddr);
518 			continue;
519 		}
520 
521 		/*
522 		**  Process the RHS.
523 		**	'al' is the internal form of the LHS address.
524 		**	'p' points to the text of the RHS.
525 		*/
526 
527 		while (isascii(*p) && isspace(*p))
528 			p++;
529 		rhs = p;
530 		for (;;)
531 		{
532 			register char c;
533 			register char *nlp;
534 
535 			nlp = &p[strlen(p)];
536 			if (nlp[-1] == '\n')
537 				*--nlp = '\0';
538 
539 			if (CheckAliases)
540 			{
541 				/* do parsing & compression of addresses */
542 				while (*p != '\0')
543 				{
544 					auto char *delimptr;
545 
546 					while ((isascii(*p) && isspace(*p)) ||
547 								*p == ',')
548 						p++;
549 					if (*p == '\0')
550 						break;
551 					if (parseaddr(p, &bl, RF_COPYNONE, ',',
552 						      &delimptr, CurEnv) == NULL)
553 						usrerr("553 %s... bad address", p);
554 					p = delimptr;
555 				}
556 			}
557 			else
558 			{
559 				p = nlp;
560 			}
561 
562 			/* see if there should be a continuation line */
563 			c = fgetc(af);
564 			if (!feof(af))
565 				(void) ungetc(c, af);
566 			if (c != ' ' && c != '\t')
567 				break;
568 
569 			/* read continuation line */
570 			if (fgets(p, sizeof line - (p - line), af) == NULL)
571 				break;
572 			LineNumber++;
573 
574 			/* check for line overflow */
575 			if (strchr(p, '\n') == NULL)
576 			{
577 				usrerr("554 alias too long");
578 				break;
579 			}
580 		}
581 		if (al.q_mailer != LocalMailer)
582 		{
583 			syserr("554 %s... cannot alias non-local names",
584 				al.q_paddr);
585 			continue;
586 		}
587 
588 		/*
589 		**  Insert alias into symbol table or DBM file
590 		*/
591 
592 		if (!bitnset(M_USR_UPPER, al.q_mailer->m_flags))
593 			makelower(al.q_user);
594 
595 		lhssize = strlen(al.q_user);
596 		rhssize = strlen(rhs);
597 		map->map_class->map_store(map, al.q_user, rhs);
598 
599 		if (al.q_paddr != NULL)
600 			free(al.q_paddr);
601 		if (al.q_host != NULL)
602 			free(al.q_host);
603 		if (al.q_user != NULL)
604 			free(al.q_user);
605 
606 		/* statistics */
607 		naliases++;
608 		bytes += lhssize + rhssize;
609 		if (rhssize > longest)
610 			longest = rhssize;
611 	}
612 
613 	CurEnv->e_to = NULL;
614 	FileName = NULL;
615 	if (Verbose || !automatic)
616 		message("%s: %d aliases, longest %d bytes, %d bytes total",
617 			map->map_file, naliases, longest, bytes);
618 # ifdef LOG
619 	if (LogLevel > 7)
620 		syslog(LOG_INFO, "%s: %d aliases, longest %d bytes, %d bytes total",
621 			map->map_file, naliases, longest, bytes);
622 # endif /* LOG */
623 }
624 /*
625 **  FORWARD -- Try to forward mail
626 **
627 **	This is similar but not identical to aliasing.
628 **
629 **	Parameters:
630 **		user -- the name of the user who's mail we would like
631 **			to forward to.  It must have been verified --
632 **			i.e., the q_home field must have been filled
633 **			in.
634 **		sendq -- a pointer to the head of the send queue to
635 **			put this user's aliases in.
636 **
637 **	Returns:
638 **		none.
639 **
640 **	Side Effects:
641 **		New names are added to send queues.
642 */
643 
644 forward(user, sendq, e)
645 	ADDRESS *user;
646 	ADDRESS **sendq;
647 	register ENVELOPE *e;
648 {
649 	char *pp;
650 	char *ep;
651 
652 	if (tTd(27, 1))
653 		printf("forward(%s)\n", user->q_paddr);
654 
655 	if (user->q_mailer != LocalMailer || bitset(QBADADDR, user->q_flags))
656 		return;
657 	if (user->q_home == NULL)
658 	{
659 		syserr("554 forward: no home");
660 		user->q_home = "/nosuchdirectory";
661 	}
662 
663 	/* good address -- look for .forward file in home */
664 	define('z', user->q_home, e);
665 	define('u', user->q_user, e);
666 	define('h', user->q_host, e);
667 	if (ForwardPath == NULL)
668 		ForwardPath = newstr("\201z/.forward");
669 
670 	for (pp = ForwardPath; pp != NULL; pp = ep)
671 	{
672 		int err;
673 		char buf[MAXPATHLEN+1];
674 
675 		ep = strchr(pp, ':');
676 		if (ep != NULL)
677 			*ep = '\0';
678 		expand(pp, buf, &buf[sizeof buf - 1], e);
679 		if (ep != NULL)
680 			*ep++ = ':';
681 		if (tTd(27, 3))
682 			printf("forward: trying %s\n", buf);
683 
684 		err = include(buf, TRUE, user, sendq, e);
685 		if (err == 0)
686 			break;
687 		else if (transienterror(err))
688 		{
689 			/* we have to suspend this message */
690 			if (tTd(27, 2))
691 				printf("forward: transient error on %s\n", buf);
692 #ifdef LOG
693 			if (LogLevel > 2)
694 				syslog(LOG_ERR, "%s: forward %s: transient error: %s",
695 					e->e_id, buf, errstring(err));
696 #endif
697 			message("%s: %s: message queued", buf, errstring(err));
698 			user->q_flags |= QQUEUEUP;
699 			return;
700 		}
701 	}
702 }
703