xref: /csrg-svn/usr.sbin/sendmail/src/alias.c (revision 24944)
122694Sdist /*
222694Sdist **  Sendmail
322694Sdist **  Copyright (c) 1983  Eric P. Allman
422694Sdist **  Berkeley, California
522694Sdist **
622694Sdist **  Copyright (c) 1983 Regents of the University of California.
722694Sdist **  All rights reserved.  The Berkeley software License Agreement
822694Sdist **  specifies the terms and conditions for redistribution.
922694Sdist */
1022694Sdist 
1122694Sdist #ifndef lint
1222903Smiriam # ifdef DBM
13*24944Seric static char	SccsId[] = "@(#)alias.c	5.3.1.1 (Berkeley) 09/19/85	(with DBM)";
1422903Smiriam # else DBM
15*24944Seric static char	SccsId[] = "@(#)alias.c	5.3.1.1 (Berkeley) 09/19/85	(without DBM)";
1622903Smiriam # endif DBM
1722694Sdist #endif not lint
1822694Sdist 
19292Seric # include <pwd.h>
204212Seric # include <sys/types.h>
214212Seric # include <sys/stat.h>
228437Seric # include <signal.h>
2319784Seric # include <errno.h>
243309Seric # include "sendmail.h"
2519784Seric # ifdef FLOCK
2619784Seric # include <sys/file.h>
2719784Seric # endif FLOCK
28292Seric 
29402Seric 
30292Seric /*
31292Seric **  ALIAS -- Compute aliases.
32292Seric **
339368Seric **	Scans the alias file for an alias for the given address.
349368Seric **	If found, it arranges to deliver to the alias list instead.
359368Seric **	Uses libdbm database if -DDBM.
36292Seric **
37292Seric **	Parameters:
384097Seric **		a -- address to alias.
394999Seric **		sendq -- a pointer to the head of the send queue
404999Seric **			to put the aliases in.
41292Seric **
42292Seric **	Returns:
43292Seric **		none
44292Seric **
45292Seric **	Side Effects:
463185Seric **		Aliases found are expanded.
47292Seric **
48292Seric **	Notes:
49292Seric **		If NoAlias (the "-n" flag) is set, no aliasing is
50292Seric **			done.
51292Seric **
52292Seric **	Deficiencies:
53292Seric **		It should complain about names that are aliased to
54292Seric **			nothing.
55292Seric */
56292Seric 
57292Seric 
581503Smark #ifdef DBM
592966Seric typedef struct
602966Seric {
612966Seric 	char	*dptr;
624157Seric 	int	dsize;
634157Seric } DATUM;
644157Seric extern DATUM fetch();
651503Smark #endif DBM
66292Seric 
674999Seric alias(a, sendq)
684097Seric 	register ADDRESS *a;
694999Seric 	ADDRESS **sendq;
70292Seric {
714081Seric 	register char *p;
725701Seric 	extern char *aliaslookup();
73292Seric 
74292Seric # ifdef DEBUG
757671Seric 	if (tTd(27, 1))
764098Seric 		printf("alias(%s)\n", a->q_paddr);
77292Seric # endif
78292Seric 
794098Seric 	/* don't realias already aliased names */
804098Seric 	if (bitset(QDONTSEND, a->q_flags))
814098Seric 		return;
824098Seric 
836898Seric 	CurEnv->e_to = a->q_paddr;
844098Seric 
854314Seric 	/*
864314Seric 	**  Look up this name
874314Seric 	*/
884314Seric 
89*24944Seric 	if (NoAlias)
90*24944Seric 		p = NULL;
91*24944Seric 	else
92*24944Seric 		p = aliaslookup(a->q_user);
934098Seric 	if (p == NULL)
944098Seric 		return;
95292Seric 
96292Seric 	/*
974098Seric 	**  Match on Alias.
984098Seric 	**	Deliver to the target list.
991515Seric 	*/
1001515Seric 
1014098Seric # ifdef DEBUG
1027671Seric 	if (tTd(27, 1))
1034098Seric 		printf("%s (%s, %s) aliased to %s\n",
1044098Seric 		    a->q_paddr, a->q_host, a->q_user, p);
1054098Seric # endif
1067051Seric 	message(Arpa_Info, "aliased to %s", p);
1074098Seric 	AliasLevel++;
1089614Seric 	sendtolist(p, a, sendq);
1094098Seric 	AliasLevel--;
1104098Seric }
1114098Seric /*
1125701Seric **  ALIASLOOKUP -- look up a name in the alias file.
1135701Seric **
1145701Seric **	Parameters:
1155701Seric **		name -- the name to look up.
1165701Seric **
1175701Seric **	Returns:
1185701Seric **		the value of name.
1195701Seric **		NULL if unknown.
1205701Seric **
1215701Seric **	Side Effects:
1225701Seric **		none.
1235701Seric **
1245701Seric **	Warnings:
1255701Seric **		The return value will be trashed across calls.
1265701Seric */
1275701Seric 
1285701Seric char *
1295701Seric aliaslookup(name)
1305701Seric 	char *name;
1315701Seric {
1325701Seric # ifdef DBM
1335701Seric 	DATUM rhs, lhs;
1345701Seric 
1355701Seric 	/* create a key for fetch */
1365701Seric 	lhs.dptr = name;
1375701Seric 	lhs.dsize = strlen(name) + 1;
1385701Seric 	rhs = fetch(lhs);
1395701Seric 	return (rhs.dptr);
1405701Seric # else DBM
1415701Seric 	register STAB *s;
1425701Seric 
1435701Seric 	s = stab(name, ST_ALIAS, ST_FIND);
1445701Seric 	if (s == NULL)
1455701Seric 		return (NULL);
1465701Seric 	return (s->s_alias);
1475701Seric # endif DBM
1485701Seric }
1495701Seric /*
1504098Seric **  INITALIASES -- initialize for aliasing
1514098Seric **
1524098Seric **	Very different depending on whether we are running DBM or not.
1534098Seric **
1544098Seric **	Parameters:
1554098Seric **		aliasfile -- location of aliases.
1564157Seric **		init -- if set and if DBM, initialize the DBM files.
1574098Seric **
1584098Seric **	Returns:
1594098Seric **		none.
1604098Seric **
1614098Seric **	Side Effects:
1624098Seric **		initializes aliases:
1634098Seric **		if DBM:  opens the database.
1644098Seric **		if ~DBM: reads the aliases into the symbol table.
1654098Seric */
1664098Seric 
1674157Seric # define DBMMODE	0666
1684157Seric 
1694157Seric initaliases(aliasfile, init)
1704098Seric 	char *aliasfile;
1714157Seric 	bool init;
1724098Seric {
1739368Seric #ifdef DBM
1748437Seric 	int atcnt;
1754322Seric 	char buf[MAXNAME];
1764322Seric 	time_t modtime;
1779368Seric #endif DBM
1789368Seric 	struct stat stb;
1794322Seric 
18017984Seric 	if (aliasfile == NULL || stat(aliasfile, &stb) < 0)
1818437Seric 	{
1828437Seric 		NoAlias = TRUE;
18311937Seric 		errno = 0;
1848437Seric 		return;
1858437Seric 	}
1868437Seric 
1878928Seric # ifdef DBM
1884322Seric 	/*
1898437Seric 	**  Check to see that the alias file is complete.
1908437Seric 	**	If not, we will assume that someone died, and it is up
1918437Seric 	**	to us to rebuild it.
1928437Seric 	*/
1938437Seric 
1948437Seric 	dbminit(aliasfile);
19517471Seric 	atcnt = SafeAlias * 2;
19617471Seric 	if (atcnt > 0)
19717471Seric 	{
19817471Seric 		while (!init && atcnt-- >= 0 && aliaslookup("@") == NULL)
19917471Seric 			sleep(30);
20017471Seric 	}
20117471Seric 	else
20217471Seric 		atcnt = 1;
2038437Seric 
2048437Seric 	/*
2054322Seric 	**  See if the DBM version of the file is out of date with
2064322Seric 	**  the text version.  If so, go into 'init' mode automatically.
2074322Seric 	**	This only happens if our effective userid owns the DBM
2084325Seric 	**	version or if the mode of the database is 666 -- this
2094322Seric 	**	is an attempt to avoid protection problems.  Note the
2104322Seric 	**	unpalatable hack to see if the stat succeeded.
2114322Seric 	*/
2124322Seric 
2134322Seric 	modtime = stb.st_mtime;
2144322Seric 	(void) strcpy(buf, aliasfile);
2154322Seric 	(void) strcat(buf, ".pag");
2164322Seric 	stb.st_ino = 0;
21719039Seric 	if (!init && (stat(buf, &stb) < 0 || stb.st_mtime < modtime || atcnt < 0))
2184322Seric 	{
21911937Seric 		errno = 0;
2209150Seric 		if (AutoRebuild && stb.st_ino != 0 &&
2219368Seric 		    ((stb.st_mode & 0777) == 0666 || stb.st_uid == geteuid()))
2224322Seric 		{
2234322Seric 			init = TRUE;
2247051Seric 			message(Arpa_Info, "rebuilding alias database");
225*24944Seric #ifdef LOG
226*24944Seric 			if (LogLevel >= 7)
227*24944Seric 				syslog(LOG_INFO, "rebuilding alias database");
228*24944Seric #endif LOG
2294322Seric 		}
2304322Seric 		else
2314322Seric 		{
23219039Seric #ifdef LOG
233*24944Seric 			if (LogLevel >= 7)
234*24944Seric 				syslog(LOG_INFO, "alias database out of date");
23519039Seric #endif LOG
2364322Seric 			message(Arpa_Info, "Warning: alias database out of date");
2374322Seric 		}
2384322Seric 	}
2394322Seric 
2404322Seric 
2414322Seric 	/*
2428437Seric 	**  If necessary, load the DBM file.
2434322Seric 	**	If running without DBM, load the symbol table.
2444322Seric 	*/
2454322Seric 
2464157Seric 	if (init)
2478437Seric 	{
2484157Seric 		readaliases(aliasfile, TRUE);
2498437Seric 	}
2504098Seric # else DBM
2514157Seric 	readaliases(aliasfile, init);
2524157Seric # endif DBM
2534157Seric }
2544157Seric /*
2554157Seric **  READALIASES -- read and process the alias file.
2564157Seric **
2574157Seric **	This routine implements the part of initaliases that occurs
2584157Seric **	when we are not going to use the DBM stuff.
2594157Seric **
2604157Seric **	Parameters:
2614157Seric **		aliasfile -- the pathname of the alias file master.
2624157Seric **		init -- if set, initialize the DBM stuff.
2634157Seric **
2644157Seric **	Returns:
2654157Seric **		none.
2664157Seric **
2674157Seric **	Side Effects:
2684157Seric **		Reads aliasfile into the symbol table.
2694157Seric **		Optionally, builds the .dir & .pag files.
2704157Seric */
2714157Seric 
2724157Seric static
2734157Seric readaliases(aliasfile, init)
2744157Seric 	char *aliasfile;
2754157Seric 	bool init;
2764157Seric {
2774098Seric 	register char *p;
2784098Seric 	char *p2;
2794098Seric 	char *rhs;
2804098Seric 	bool skipping;
2819368Seric 	int naliases, bytes, longest;
2829368Seric 	FILE *af;
28319784Seric 	int (*oldsigint)();
2844098Seric 	ADDRESS al, bl;
2854106Seric 	register STAB *s;
2869368Seric 	char line[BUFSIZ];
2874098Seric 
2884098Seric 	if ((af = fopen(aliasfile, "r")) == NULL)
2891515Seric 	{
2904098Seric # ifdef DEBUG
2917671Seric 		if (tTd(27, 1))
2924106Seric 			printf("Can't open %s\n", aliasfile);
2934098Seric # endif
2944098Seric 		errno = 0;
2954098Seric 		NoAlias++;
2964098Seric 		return;
2974098Seric 	}
2984314Seric 
29919784Seric # ifdef DBM
30019784Seric # ifdef FLOCK
30119784Seric 	/* see if someone else is rebuilding the alias file already */
30219784Seric 	if (flock(fileno(af), LOCK_EX | LOCK_NB) < 0 && errno == EWOULDBLOCK)
30319784Seric 	{
30419784Seric 		/* yes, they are -- wait until done and then return */
30519784Seric 		message(Arpa_Info, "Alias file is already being rebuilt");
30619784Seric 		if (OpMode != MD_INITALIAS)
30719784Seric 		{
30819784Seric 			/* wait for other rebuild to complete */
30919784Seric 			(void) flock(fileno(af), LOCK_EX);
31019784Seric 		}
31123108Seric 		(void) fclose(af);
31219784Seric 		errno = 0;
31319784Seric 		return;
31419784Seric 	}
31519784Seric # endif FLOCK
31619784Seric # endif DBM
31719784Seric 
3184314Seric 	/*
31919784Seric 	**  If initializing, create the new DBM files.
32019784Seric 	*/
32119784Seric 
32219784Seric 	if (init)
32319784Seric 	{
32419784Seric 		oldsigint = signal(SIGINT, SIG_IGN);
32519784Seric 		(void) strcpy(line, aliasfile);
32619784Seric 		(void) strcat(line, ".dir");
32719784Seric 		if (close(creat(line, DBMMODE)) < 0)
32819784Seric 		{
32919784Seric 			syserr("cannot make %s", line);
33019784Seric 			(void) signal(SIGINT, oldsigint);
33119784Seric 			return;
33219784Seric 		}
33319784Seric 		(void) strcpy(line, aliasfile);
33419784Seric 		(void) strcat(line, ".pag");
33519784Seric 		if (close(creat(line, DBMMODE)) < 0)
33619784Seric 		{
33719784Seric 			syserr("cannot make %s", line);
33819784Seric 			(void) signal(SIGINT, oldsigint);
33919784Seric 			return;
34019784Seric 		}
34119784Seric 	}
34219784Seric 
34319784Seric 	/*
3444314Seric 	**  Read and interpret lines
3454314Seric 	*/
3464314Seric 
3479368Seric 	FileName = aliasfile;
3489368Seric 	LineNumber = 0;
3494322Seric 	naliases = bytes = longest = 0;
3504098Seric 	skipping = FALSE;
3514098Seric 	while (fgets(line, sizeof (line), af) != NULL)
3524098Seric 	{
3534322Seric 		int lhssize, rhssize;
3544322Seric 
3559368Seric 		LineNumber++;
3564098Seric 		switch (line[0])
3574098Seric 		{
3584098Seric 		  case '#':
3594098Seric 		  case '\n':
3604098Seric 		  case '\0':
3614098Seric 			skipping = FALSE;
3624098Seric 			continue;
3634065Seric 
3644098Seric 		  case ' ':
3654098Seric 		  case '\t':
3664098Seric 			if (!skipping)
3679368Seric 				syserr("Non-continuation line starts with space");
3684098Seric 			skipping = TRUE;
3694097Seric 			continue;
3704098Seric 		}
3714098Seric 		skipping = FALSE;
3721874Seric 
3734314Seric 		/*
3744314Seric 		**  Process the LHS
3754314Seric 		**	Find the final colon, and parse the address.
37616898Seric 		**	It should resolve to a local name -- this will
37716898Seric 		**	be checked later (we want to optionally do
37816898Seric 		**	parsing of the RHS first to maximize error
37916898Seric 		**	detection).
3804314Seric 		*/
3814314Seric 
3824098Seric 		for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++)
3834097Seric 			continue;
38416898Seric 		if (*p++ != ':')
3854098Seric 		{
3869368Seric 			syserr("missing colon");
3874097Seric 			continue;
3884098Seric 		}
38916898Seric 		if (parseaddr(line, &al, 1, ':') == NULL)
3904098Seric 		{
39116898Seric 			syserr("illegal alias name");
39216898Seric 			continue;
3934098Seric 		}
39416898Seric 		loweraddr(&al);
3954314Seric 
3964314Seric 		/*
3974314Seric 		**  Process the RHS.
3984314Seric 		**	'al' is the internal form of the LHS address.
3994314Seric 		**	'p' points to the text of the RHS.
4004314Seric 		*/
4014314Seric 
4024098Seric 		rhs = p;
4034098Seric 		for (;;)
4044098Seric 		{
4054098Seric 			register char c;
4061515Seric 
4074157Seric 			if (init)
4084098Seric 			{
4094157Seric 				/* do parsing & compression of addresses */
4104098Seric 				c = *p;
4114157Seric 				while (c != '\0')
4124098Seric 				{
4134157Seric 					p2 = p;
4144157Seric 					while (*p != '\n' && *p != ',' && *p != '\0')
4154157Seric 						p++;
4164157Seric 					c = *p;
4174322Seric 					if (c == '\n')
4184322Seric 						c = '\0';
41917199Seric 					*p = '\0';
420*24944Seric 					if (*p2 != '\0' &&
421*24944Seric 					    parseaddr(p2, &bl, -1, ',') == NULL)
422*24944Seric 					{
423*24944Seric 						usrerr("%s... bad address");
424*24944Seric 					}
42517199Seric 					if (c != '\0')
42617199Seric 						*p++ = c;
4274098Seric 				}
4284098Seric 			}
4294157Seric 			else
43015769Seric 			{
43116898Seric 				p = &p[strlen(p)];
43216898Seric 				if (p[-1] == '\n')
43316898Seric 					*--p = '\0';
43415769Seric 			}
4351515Seric 
4364098Seric 			/* see if there should be a continuation line */
4374106Seric 			c = fgetc(af);
4384106Seric 			if (!feof(af))
4394314Seric 				(void) ungetc(c, af);
4404106Seric 			if (c != ' ' && c != '\t')
4414098Seric 				break;
4424098Seric 
4434098Seric 			/* read continuation line */
4444098Seric 			if (fgets(p, sizeof line - (p - line), af) == NULL)
4454098Seric 				break;
4469368Seric 			LineNumber++;
4474098Seric 		}
44816898Seric 		if (al.q_mailer != LocalMailer)
44916898Seric 		{
45016898Seric 			syserr("cannot alias non-local names");
45116898Seric 			continue;
45216898Seric 		}
4534314Seric 
4544314Seric 		/*
4554314Seric 		**  Insert alias into symbol table or DBM file
4564314Seric 		*/
4574314Seric 
45816898Seric 		lhssize = strlen(al.q_user) + 1;
4594322Seric 		rhssize = strlen(rhs) + 1;
4604322Seric 
4614157Seric # ifdef DBM
4624157Seric 		if (init)
4634157Seric 		{
4644157Seric 			DATUM key, content;
4654157Seric 
4664322Seric 			key.dsize = lhssize;
4674157Seric 			key.dptr = al.q_user;
4684322Seric 			content.dsize = rhssize;
4694157Seric 			content.dptr = rhs;
4704157Seric 			store(key, content);
4714157Seric 		}
4724157Seric 		else
4734157Seric # endif DBM
4744157Seric 		{
4754157Seric 			s = stab(al.q_user, ST_ALIAS, ST_ENTER);
4764157Seric 			s->s_alias = newstr(rhs);
4774157Seric 		}
4784322Seric 
4794322Seric 		/* statistics */
4804322Seric 		naliases++;
4814322Seric 		bytes += lhssize + rhssize;
4824322Seric 		if (rhssize > longest)
4834322Seric 			longest = rhssize;
4841515Seric 	}
48519784Seric 
48619784Seric # ifdef DBM
48719784Seric 	if (init)
48819784Seric 	{
48919784Seric 		/* add the distinquished alias "@" */
49019784Seric 		DATUM key;
49119784Seric 
49219784Seric 		key.dsize = 2;
49319784Seric 		key.dptr = "@";
49419784Seric 		store(key, key);
49519784Seric 
49619784Seric 		/* restore the old signal */
49719784Seric 		(void) signal(SIGINT, oldsigint);
49819784Seric 	}
49919784Seric # endif DBM
50019784Seric 
50119784Seric 	/* closing the alias file drops the lock */
5024098Seric 	(void) fclose(af);
5036898Seric 	CurEnv->e_to = NULL;
5049368Seric 	FileName = NULL;
5057051Seric 	message(Arpa_Info, "%d aliases, longest %d bytes, %d bytes total",
5064322Seric 			naliases, longest, bytes);
507*24944Seric # ifdef LOG
508*24944Seric 	if (LogLevel >= 8)
509*24944Seric 		syslog(LOG_INFO, "%d aliases, longest %d bytes, %d bytes total",
510*24944Seric 			naliases, longest, bytes);
511*24944Seric # endif LOG
512292Seric }
513292Seric /*
514292Seric **  FORWARD -- Try to forward mail
515292Seric **
516292Seric **	This is similar but not identical to aliasing.
517292Seric **
518292Seric **	Parameters:
5194314Seric **		user -- the name of the user who's mail we would like
5204314Seric **			to forward to.  It must have been verified --
5214314Seric **			i.e., the q_home field must have been filled
5224314Seric **			in.
5234999Seric **		sendq -- a pointer to the head of the send queue to
5244999Seric **			put this user's aliases in.
525292Seric **
526292Seric **	Returns:
5274098Seric **		none.
528292Seric **
529292Seric **	Side Effects:
5303185Seric **		New names are added to send queues.
531292Seric */
532292Seric 
5334999Seric forward(user, sendq)
5342966Seric 	ADDRESS *user;
5354999Seric 	ADDRESS **sendq;
536292Seric {
5374078Seric 	char buf[60];
5384536Seric 	extern bool safefile();
5394069Seric 
5404098Seric # ifdef DEBUG
5417671Seric 	if (tTd(27, 1))
5424098Seric 		printf("forward(%s)\n", user->q_paddr);
5434098Seric # endif DEBUG
5444098Seric 
5454594Seric 	if (user->q_mailer != LocalMailer || bitset(QBADADDR, user->q_flags))
5464098Seric 		return;
5474314Seric # ifdef DEBUG
5484314Seric 	if (user->q_home == NULL)
5494314Seric 		syserr("forward: no home");
5504314Seric # endif DEBUG
5514069Seric 
5524069Seric 	/* good address -- look for .forward file in home */
5539368Seric 	define('z', user->q_home, CurEnv);
55416154Seric 	expand("\001z/.forward", buf, &buf[sizeof buf - 1], CurEnv);
5554536Seric 	if (!safefile(buf, user->q_uid, S_IREAD))
5564098Seric 		return;
5574069Seric 
5584069Seric 	/* we do have an address to forward to -- do it */
5594999Seric 	include(buf, "forwarding", user, sendq);
560292Seric }
561