xref: /csrg-svn/usr.sbin/sendmail/src/alias.c (revision 25278)
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*25278Seric static char	SccsId[] = "@(#)alias.c	5.6 (Berkeley) 10/24/85	(with DBM)";
1422903Smiriam # else DBM
15*25278Seric static char	SccsId[] = "@(#)alias.c	5.6 (Berkeley) 10/24/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 
8924944Seric 	if (NoAlias)
9024944Seric 		p = NULL;
9124944Seric 	else
9224944Seric 		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");
22524944Seric #ifdef LOG
22624944Seric 			if (LogLevel >= 7)
22724944Seric 				syslog(LOG_INFO, "rebuilding alias database");
22824944Seric #endif LOG
2294322Seric 		}
2304322Seric 		else
2314322Seric 		{
23219039Seric #ifdef LOG
23324944Seric 			if (LogLevel >= 7)
23424944Seric 				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++;
356*25278Seric 		p = index(line, '\n');
357*25278Seric 		if (p != NULL)
358*25278Seric 			*p = '\0';
3594098Seric 		switch (line[0])
3604098Seric 		{
3614098Seric 		  case '#':
3624098Seric 		  case '\0':
3634098Seric 			skipping = FALSE;
3644098Seric 			continue;
3654065Seric 
3664098Seric 		  case ' ':
3674098Seric 		  case '\t':
3684098Seric 			if (!skipping)
3699368Seric 				syserr("Non-continuation line starts with space");
3704098Seric 			skipping = TRUE;
3714097Seric 			continue;
3724098Seric 		}
3734098Seric 		skipping = FALSE;
3741874Seric 
3754314Seric 		/*
3764314Seric 		**  Process the LHS
3774314Seric 		**	Find the final colon, and parse the address.
37816898Seric 		**	It should resolve to a local name -- this will
37916898Seric 		**	be checked later (we want to optionally do
38016898Seric 		**	parsing of the RHS first to maximize error
38116898Seric 		**	detection).
3824314Seric 		*/
3834314Seric 
3844098Seric 		for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++)
3854097Seric 			continue;
38616898Seric 		if (*p++ != ':')
3874098Seric 		{
3889368Seric 			syserr("missing colon");
3894097Seric 			continue;
3904098Seric 		}
39116898Seric 		if (parseaddr(line, &al, 1, ':') == NULL)
3924098Seric 		{
39316898Seric 			syserr("illegal alias name");
39416898Seric 			continue;
3954098Seric 		}
39616898Seric 		loweraddr(&al);
3974314Seric 
3984314Seric 		/*
3994314Seric 		**  Process the RHS.
4004314Seric 		**	'al' is the internal form of the LHS address.
4014314Seric 		**	'p' points to the text of the RHS.
4024314Seric 		*/
4034314Seric 
4044098Seric 		rhs = p;
4054098Seric 		for (;;)
4064098Seric 		{
4074098Seric 			register char c;
4081515Seric 
4094157Seric 			if (init)
4104098Seric 			{
4114157Seric 				/* do parsing & compression of addresses */
412*25278Seric 				while (*p != '\0')
4134098Seric 				{
414*25278Seric 					extern char *DelimChar;
415*25278Seric 
416*25278Seric 					while (isspace(*p) || *p == ',')
4174157Seric 						p++;
418*25278Seric 					if (*p == '\0')
419*25278Seric 						break;
420*25278Seric 					if (parseaddr(p, &bl, -1, ',') == NULL)
421*25278Seric 						usrerr("%s... bad address", p);
422*25278Seric 					p = DelimChar;
4234098Seric 				}
4244098Seric 			}
4254157Seric 			else
42615769Seric 			{
42716898Seric 				p = &p[strlen(p)];
42816898Seric 				if (p[-1] == '\n')
42916898Seric 					*--p = '\0';
43015769Seric 			}
4311515Seric 
4324098Seric 			/* see if there should be a continuation line */
4334106Seric 			c = fgetc(af);
4344106Seric 			if (!feof(af))
4354314Seric 				(void) ungetc(c, af);
4364106Seric 			if (c != ' ' && c != '\t')
4374098Seric 				break;
4384098Seric 
4394098Seric 			/* read continuation line */
4404098Seric 			if (fgets(p, sizeof line - (p - line), af) == NULL)
4414098Seric 				break;
4429368Seric 			LineNumber++;
4434098Seric 		}
44416898Seric 		if (al.q_mailer != LocalMailer)
44516898Seric 		{
44616898Seric 			syserr("cannot alias non-local names");
44716898Seric 			continue;
44816898Seric 		}
4494314Seric 
4504314Seric 		/*
4514314Seric 		**  Insert alias into symbol table or DBM file
4524314Seric 		*/
4534314Seric 
45416898Seric 		lhssize = strlen(al.q_user) + 1;
4554322Seric 		rhssize = strlen(rhs) + 1;
4564322Seric 
4574157Seric # ifdef DBM
4584157Seric 		if (init)
4594157Seric 		{
4604157Seric 			DATUM key, content;
4614157Seric 
4624322Seric 			key.dsize = lhssize;
4634157Seric 			key.dptr = al.q_user;
4644322Seric 			content.dsize = rhssize;
4654157Seric 			content.dptr = rhs;
4664157Seric 			store(key, content);
4674157Seric 		}
4684157Seric 		else
4694157Seric # endif DBM
4704157Seric 		{
4714157Seric 			s = stab(al.q_user, ST_ALIAS, ST_ENTER);
4724157Seric 			s->s_alias = newstr(rhs);
4734157Seric 		}
4744322Seric 
4754322Seric 		/* statistics */
4764322Seric 		naliases++;
4774322Seric 		bytes += lhssize + rhssize;
4784322Seric 		if (rhssize > longest)
4794322Seric 			longest = rhssize;
4801515Seric 	}
48119784Seric 
48219784Seric # ifdef DBM
48319784Seric 	if (init)
48419784Seric 	{
48519784Seric 		/* add the distinquished alias "@" */
48619784Seric 		DATUM key;
48719784Seric 
48819784Seric 		key.dsize = 2;
48919784Seric 		key.dptr = "@";
49019784Seric 		store(key, key);
49119784Seric 
49219784Seric 		/* restore the old signal */
49319784Seric 		(void) signal(SIGINT, oldsigint);
49419784Seric 	}
49519784Seric # endif DBM
49619784Seric 
49719784Seric 	/* closing the alias file drops the lock */
4984098Seric 	(void) fclose(af);
4996898Seric 	CurEnv->e_to = NULL;
5009368Seric 	FileName = NULL;
5017051Seric 	message(Arpa_Info, "%d aliases, longest %d bytes, %d bytes total",
5024322Seric 			naliases, longest, bytes);
50324944Seric # ifdef LOG
50424944Seric 	if (LogLevel >= 8)
50524944Seric 		syslog(LOG_INFO, "%d aliases, longest %d bytes, %d bytes total",
50624944Seric 			naliases, longest, bytes);
50724944Seric # endif LOG
508292Seric }
509292Seric /*
510292Seric **  FORWARD -- Try to forward mail
511292Seric **
512292Seric **	This is similar but not identical to aliasing.
513292Seric **
514292Seric **	Parameters:
5154314Seric **		user -- the name of the user who's mail we would like
5164314Seric **			to forward to.  It must have been verified --
5174314Seric **			i.e., the q_home field must have been filled
5184314Seric **			in.
5194999Seric **		sendq -- a pointer to the head of the send queue to
5204999Seric **			put this user's aliases in.
521292Seric **
522292Seric **	Returns:
5234098Seric **		none.
524292Seric **
525292Seric **	Side Effects:
5263185Seric **		New names are added to send queues.
527292Seric */
528292Seric 
5294999Seric forward(user, sendq)
5302966Seric 	ADDRESS *user;
5314999Seric 	ADDRESS **sendq;
532292Seric {
5334078Seric 	char buf[60];
5344536Seric 	extern bool safefile();
5354069Seric 
5364098Seric # ifdef DEBUG
5377671Seric 	if (tTd(27, 1))
5384098Seric 		printf("forward(%s)\n", user->q_paddr);
5394098Seric # endif DEBUG
5404098Seric 
5414594Seric 	if (user->q_mailer != LocalMailer || bitset(QBADADDR, user->q_flags))
5424098Seric 		return;
5434314Seric # ifdef DEBUG
5444314Seric 	if (user->q_home == NULL)
5454314Seric 		syserr("forward: no home");
5464314Seric # endif DEBUG
5474069Seric 
5484069Seric 	/* good address -- look for .forward file in home */
5499368Seric 	define('z', user->q_home, CurEnv);
55016154Seric 	expand("\001z/.forward", buf, &buf[sizeof buf - 1], CurEnv);
5514536Seric 	if (!safefile(buf, user->q_uid, S_IREAD))
5524098Seric 		return;
5534069Seric 
5544069Seric 	/* we do have an address to forward to -- do it */
5554999Seric 	include(buf, "forwarding", user, sendq);
556292Seric }
557