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