xref: /csrg-svn/usr.sbin/sendmail/src/alias.c (revision 36928)
122694Sdist /*
235073Sbostic  * Copyright (c) 1983 Eric P. Allman
333728Sbostic  * Copyright (c) 1988 Regents of the University of California.
433728Sbostic  * All rights reserved.
533728Sbostic  *
633728Sbostic  * Redistribution and use in source and binary forms are permitted
735073Sbostic  * provided that the above copyright notice and this paragraph are
835073Sbostic  * duplicated in all such forms and that any documentation,
935073Sbostic  * advertising materials, and other materials related to such
1035073Sbostic  * distribution and use acknowledge that the software was developed
1135073Sbostic  * by the University of California, Berkeley.  The name of the
1235073Sbostic  * University may not be used to endorse or promote products derived
1335073Sbostic  * from this software without specific prior written permission.
1435073Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1535073Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1635073Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1733728Sbostic  */
1822694Sdist 
1933728Sbostic #ifndef lint
2033728Sbostic #ifdef DBM
21*36928Sbostic static char sccsid[] = "@(#)alias.c	5.18 (Berkeley) 02/27/89 (with DBM)";
2233728Sbostic #else
23*36928Sbostic static char sccsid[] = "@(#)alias.c	5.18 (Berkeley) 02/27/89 (without DBM)";
2433728Sbostic #endif
2533728Sbostic #endif /* not lint */
2633728Sbostic 
274212Seric # include <sys/types.h>
284212Seric # include <sys/stat.h>
298437Seric # include <signal.h>
3019784Seric # include <errno.h>
313309Seric # include "sendmail.h"
3219784Seric # include <sys/file.h>
33*36928Sbostic # include <pwd.h>
34292Seric 
35292Seric /*
36292Seric **  ALIAS -- Compute aliases.
37292Seric **
389368Seric **	Scans the alias file for an alias for the given address.
399368Seric **	If found, it arranges to deliver to the alias list instead.
409368Seric **	Uses libdbm database if -DDBM.
41292Seric **
42292Seric **	Parameters:
434097Seric **		a -- address to alias.
444999Seric **		sendq -- a pointer to the head of the send queue
454999Seric **			to put the aliases in.
46292Seric **
47292Seric **	Returns:
48292Seric **		none
49292Seric **
50292Seric **	Side Effects:
513185Seric **		Aliases found are expanded.
52292Seric **
53292Seric **	Notes:
54292Seric **		If NoAlias (the "-n" flag) is set, no aliasing is
55292Seric **			done.
56292Seric **
57292Seric **	Deficiencies:
58292Seric **		It should complain about names that are aliased to
59292Seric **			nothing.
60292Seric */
61292Seric 
62292Seric 
631503Smark #ifdef DBM
642966Seric typedef struct
652966Seric {
662966Seric 	char	*dptr;
674157Seric 	int	dsize;
684157Seric } DATUM;
694157Seric extern DATUM fetch();
701503Smark #endif DBM
71292Seric 
724999Seric alias(a, sendq)
734097Seric 	register ADDRESS *a;
744999Seric 	ADDRESS **sendq;
75292Seric {
764081Seric 	register char *p;
775701Seric 	extern char *aliaslookup();
78292Seric 
797671Seric 	if (tTd(27, 1))
804098Seric 		printf("alias(%s)\n", a->q_paddr);
81292Seric 
824098Seric 	/* don't realias already aliased names */
834098Seric 	if (bitset(QDONTSEND, a->q_flags))
844098Seric 		return;
854098Seric 
866898Seric 	CurEnv->e_to = a->q_paddr;
874098Seric 
884314Seric 	/*
894314Seric 	**  Look up this name
904314Seric 	*/
914314Seric 
9224944Seric 	if (NoAlias)
9324944Seric 		p = NULL;
9424944Seric 	else
9524944Seric 		p = aliaslookup(a->q_user);
964098Seric 	if (p == NULL)
974098Seric 		return;
98292Seric 
99292Seric 	/*
1004098Seric 	**  Match on Alias.
1014098Seric 	**	Deliver to the target list.
1021515Seric 	*/
1031515Seric 
1047671Seric 	if (tTd(27, 1))
1054098Seric 		printf("%s (%s, %s) aliased to %s\n",
1064098Seric 		    a->q_paddr, a->q_host, a->q_user, p);
1077051Seric 	message(Arpa_Info, "aliased to %s", p);
1084098Seric 	AliasLevel++;
1099614Seric 	sendtolist(p, a, sendq);
1104098Seric 	AliasLevel--;
1114098Seric }
1124098Seric /*
1135701Seric **  ALIASLOOKUP -- look up a name in the alias file.
1145701Seric **
1155701Seric **	Parameters:
1165701Seric **		name -- the name to look up.
1175701Seric **
1185701Seric **	Returns:
1195701Seric **		the value of name.
1205701Seric **		NULL if unknown.
1215701Seric **
1225701Seric **	Side Effects:
1235701Seric **		none.
1245701Seric **
1255701Seric **	Warnings:
1265701Seric **		The return value will be trashed across calls.
1275701Seric */
1285701Seric 
1295701Seric char *
1305701Seric aliaslookup(name)
1315701Seric 	char *name;
1325701Seric {
1335701Seric # ifdef DBM
1345701Seric 	DATUM rhs, lhs;
1355701Seric 
1365701Seric 	/* create a key for fetch */
1375701Seric 	lhs.dptr = name;
1385701Seric 	lhs.dsize = strlen(name) + 1;
1395701Seric 	rhs = fetch(lhs);
1405701Seric 	return (rhs.dptr);
1415701Seric # else DBM
1425701Seric 	register STAB *s;
1435701Seric 
1445701Seric 	s = stab(name, ST_ALIAS, ST_FIND);
1455701Seric 	if (s == NULL)
1465701Seric 		return (NULL);
1475701Seric 	return (s->s_alias);
1485701Seric # endif DBM
1495701Seric }
1505701Seric /*
1514098Seric **  INITALIASES -- initialize for aliasing
1524098Seric **
1534098Seric **	Very different depending on whether we are running DBM or not.
1544098Seric **
1554098Seric **	Parameters:
1564098Seric **		aliasfile -- location of aliases.
1574157Seric **		init -- if set and if DBM, initialize the DBM files.
1584098Seric **
1594098Seric **	Returns:
1604098Seric **		none.
1614098Seric **
1624098Seric **	Side Effects:
1634098Seric **		initializes aliases:
1644098Seric **		if DBM:  opens the database.
1654098Seric **		if ~DBM: reads the aliases into the symbol table.
1664098Seric */
1674098Seric 
1684157Seric # define DBMMODE	0666
1694157Seric 
1704157Seric initaliases(aliasfile, init)
1714098Seric 	char *aliasfile;
1724157Seric 	bool init;
1734098Seric {
1749368Seric #ifdef DBM
1758437Seric 	int atcnt;
17625522Seric 	time_t modtime;
17725522Seric 	bool automatic = FALSE;
1784322Seric 	char buf[MAXNAME];
1799368Seric #endif DBM
1809368Seric 	struct stat stb;
18127176Seric 	static bool initialized = FALSE;
1824322Seric 
18327176Seric 	if (initialized)
18427176Seric 		return;
18527176Seric 	initialized = TRUE;
18627176Seric 
18717984Seric 	if (aliasfile == NULL || stat(aliasfile, &stb) < 0)
1888437Seric 	{
18925522Seric 		if (aliasfile != NULL && init)
19025522Seric 			syserr("Cannot open %s", aliasfile);
1918437Seric 		NoAlias = TRUE;
19211937Seric 		errno = 0;
1938437Seric 		return;
1948437Seric 	}
1958437Seric 
1968928Seric # ifdef DBM
1974322Seric 	/*
1988437Seric 	**  Check to see that the alias file is complete.
1998437Seric 	**	If not, we will assume that someone died, and it is up
2008437Seric 	**	to us to rebuild it.
2018437Seric 	*/
2028437Seric 
20325689Seric 	if (!init)
20425689Seric 		dbminit(aliasfile);
20517471Seric 	atcnt = SafeAlias * 2;
20617471Seric 	if (atcnt > 0)
20717471Seric 	{
20817471Seric 		while (!init && atcnt-- >= 0 && aliaslookup("@") == NULL)
20925689Seric 		{
21025689Seric 			/*
21125689Seric 			**  Reinitialize alias file in case the new
21225689Seric 			**  one is mv'ed in instead of cp'ed in.
21325689Seric 			**
21425689Seric 			**	Only works with new DBM -- old one will
21525689Seric 			**	just consume file descriptors forever.
21625689Seric 			**	If you have a dbmclose() it can be
21725689Seric 			**	added before the sleep(30).
21825689Seric 			*/
21925689Seric 
22017471Seric 			sleep(30);
22125689Seric # ifdef NDBM
22225689Seric 			dbminit(aliasfile);
22325689Seric # endif NDBM
22425689Seric 		}
22517471Seric 	}
22617471Seric 	else
22717471Seric 		atcnt = 1;
2288437Seric 
2298437Seric 	/*
2304322Seric 	**  See if the DBM version of the file is out of date with
2314322Seric 	**  the text version.  If so, go into 'init' mode automatically.
2324322Seric 	**	This only happens if our effective userid owns the DBM
2334325Seric 	**	version or if the mode of the database is 666 -- this
2344322Seric 	**	is an attempt to avoid protection problems.  Note the
2354322Seric 	**	unpalatable hack to see if the stat succeeded.
2364322Seric 	*/
2374322Seric 
2384322Seric 	modtime = stb.st_mtime;
2394322Seric 	(void) strcpy(buf, aliasfile);
2404322Seric 	(void) strcat(buf, ".pag");
2414322Seric 	stb.st_ino = 0;
24219039Seric 	if (!init && (stat(buf, &stb) < 0 || stb.st_mtime < modtime || atcnt < 0))
2434322Seric 	{
24411937Seric 		errno = 0;
2459150Seric 		if (AutoRebuild && stb.st_ino != 0 &&
2469368Seric 		    ((stb.st_mode & 0777) == 0666 || stb.st_uid == geteuid()))
2474322Seric 		{
2484322Seric 			init = TRUE;
24925522Seric 			automatic = TRUE;
2507051Seric 			message(Arpa_Info, "rebuilding alias database");
25124944Seric #ifdef LOG
25224944Seric 			if (LogLevel >= 7)
25324944Seric 				syslog(LOG_INFO, "rebuilding alias database");
25424944Seric #endif LOG
2554322Seric 		}
2564322Seric 		else
2574322Seric 		{
25819039Seric #ifdef LOG
25924944Seric 			if (LogLevel >= 7)
26024944Seric 				syslog(LOG_INFO, "alias database out of date");
26119039Seric #endif LOG
2624322Seric 			message(Arpa_Info, "Warning: alias database out of date");
2634322Seric 		}
2644322Seric 	}
2654322Seric 
2664322Seric 
2674322Seric 	/*
2688437Seric 	**  If necessary, load the DBM file.
2694322Seric 	**	If running without DBM, load the symbol table.
2704322Seric 	*/
2714322Seric 
2724157Seric 	if (init)
2738437Seric 	{
27425522Seric #ifdef LOG
27525522Seric 		if (LogLevel >= 6)
27625522Seric 		{
27725522Seric 			extern char *username();
27825522Seric 
27925522Seric 			syslog(LOG_NOTICE, "alias database %srebuilt by %s",
28025522Seric 				automatic ? "auto" : "", username());
28125522Seric 		}
28225522Seric #endif LOG
2834157Seric 		readaliases(aliasfile, TRUE);
2848437Seric 	}
2854098Seric # else DBM
2864157Seric 	readaliases(aliasfile, init);
2874157Seric # endif DBM
2884157Seric }
2894157Seric /*
2904157Seric **  READALIASES -- read and process the alias file.
2914157Seric **
2924157Seric **	This routine implements the part of initaliases that occurs
2934157Seric **	when we are not going to use the DBM stuff.
2944157Seric **
2954157Seric **	Parameters:
2964157Seric **		aliasfile -- the pathname of the alias file master.
2974157Seric **		init -- if set, initialize the DBM stuff.
2984157Seric **
2994157Seric **	Returns:
3004157Seric **		none.
3014157Seric **
3024157Seric **	Side Effects:
3034157Seric **		Reads aliasfile into the symbol table.
3044157Seric **		Optionally, builds the .dir & .pag files.
3054157Seric */
3064157Seric 
3074157Seric static
3084157Seric readaliases(aliasfile, init)
3094157Seric 	char *aliasfile;
3104157Seric 	bool init;
3114157Seric {
3124098Seric 	register char *p;
3134098Seric 	char *rhs;
3144098Seric 	bool skipping;
3159368Seric 	int naliases, bytes, longest;
3169368Seric 	FILE *af;
31719784Seric 	int (*oldsigint)();
3184098Seric 	ADDRESS al, bl;
3194106Seric 	register STAB *s;
3209368Seric 	char line[BUFSIZ];
3214098Seric 
3224098Seric 	if ((af = fopen(aliasfile, "r")) == NULL)
3231515Seric 	{
3247671Seric 		if (tTd(27, 1))
3254106Seric 			printf("Can't open %s\n", aliasfile);
3264098Seric 		errno = 0;
3274098Seric 		NoAlias++;
3284098Seric 		return;
3294098Seric 	}
3304314Seric 
33119784Seric # ifdef DBM
33219784Seric 	/* see if someone else is rebuilding the alias file already */
33319784Seric 	if (flock(fileno(af), LOCK_EX | LOCK_NB) < 0 && errno == EWOULDBLOCK)
33419784Seric 	{
33519784Seric 		/* yes, they are -- wait until done and then return */
33619784Seric 		message(Arpa_Info, "Alias file is already being rebuilt");
33719784Seric 		if (OpMode != MD_INITALIAS)
33819784Seric 		{
33919784Seric 			/* wait for other rebuild to complete */
34019784Seric 			(void) flock(fileno(af), LOCK_EX);
34119784Seric 		}
34223108Seric 		(void) fclose(af);
34319784Seric 		errno = 0;
34419784Seric 		return;
34519784Seric 	}
34619784Seric # endif DBM
34719784Seric 
3484314Seric 	/*
34919784Seric 	**  If initializing, create the new DBM files.
35019784Seric 	*/
35119784Seric 
35219784Seric 	if (init)
35319784Seric 	{
35419784Seric 		oldsigint = signal(SIGINT, SIG_IGN);
35519784Seric 		(void) strcpy(line, aliasfile);
35619784Seric 		(void) strcat(line, ".dir");
35719784Seric 		if (close(creat(line, DBMMODE)) < 0)
35819784Seric 		{
35919784Seric 			syserr("cannot make %s", line);
36019784Seric 			(void) signal(SIGINT, oldsigint);
36119784Seric 			return;
36219784Seric 		}
36319784Seric 		(void) strcpy(line, aliasfile);
36419784Seric 		(void) strcat(line, ".pag");
36519784Seric 		if (close(creat(line, DBMMODE)) < 0)
36619784Seric 		{
36719784Seric 			syserr("cannot make %s", line);
36819784Seric 			(void) signal(SIGINT, oldsigint);
36919784Seric 			return;
37019784Seric 		}
37126501Seric 		dbminit(aliasfile);
37219784Seric 	}
37319784Seric 
37419784Seric 	/*
3754314Seric 	**  Read and interpret lines
3764314Seric 	*/
3774314Seric 
3789368Seric 	FileName = aliasfile;
3799368Seric 	LineNumber = 0;
3804322Seric 	naliases = bytes = longest = 0;
3814098Seric 	skipping = FALSE;
3824098Seric 	while (fgets(line, sizeof (line), af) != NULL)
3834098Seric 	{
3844322Seric 		int lhssize, rhssize;
3854322Seric 
3869368Seric 		LineNumber++;
38725278Seric 		p = index(line, '\n');
38825278Seric 		if (p != NULL)
38925278Seric 			*p = '\0';
3904098Seric 		switch (line[0])
3914098Seric 		{
3924098Seric 		  case '#':
3934098Seric 		  case '\0':
3944098Seric 			skipping = FALSE;
3954098Seric 			continue;
3964065Seric 
3974098Seric 		  case ' ':
3984098Seric 		  case '\t':
3994098Seric 			if (!skipping)
4009368Seric 				syserr("Non-continuation line starts with space");
4014098Seric 			skipping = TRUE;
4024097Seric 			continue;
4034098Seric 		}
4044098Seric 		skipping = FALSE;
4051874Seric 
4064314Seric 		/*
4074314Seric 		**  Process the LHS
4084314Seric 		**	Find the final colon, and parse the address.
40916898Seric 		**	It should resolve to a local name -- this will
41016898Seric 		**	be checked later (we want to optionally do
41116898Seric 		**	parsing of the RHS first to maximize error
41216898Seric 		**	detection).
4134314Seric 		*/
4144314Seric 
4154098Seric 		for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++)
4164097Seric 			continue;
41716898Seric 		if (*p++ != ':')
4184098Seric 		{
4199368Seric 			syserr("missing colon");
4204097Seric 			continue;
4214098Seric 		}
42216898Seric 		if (parseaddr(line, &al, 1, ':') == NULL)
4234098Seric 		{
42416898Seric 			syserr("illegal alias name");
42516898Seric 			continue;
4264098Seric 		}
42716898Seric 		loweraddr(&al);
4284314Seric 
4294314Seric 		/*
4304314Seric 		**  Process the RHS.
4314314Seric 		**	'al' is the internal form of the LHS address.
4324314Seric 		**	'p' points to the text of the RHS.
4334314Seric 		*/
4344314Seric 
4354098Seric 		rhs = p;
4364098Seric 		for (;;)
4374098Seric 		{
4384098Seric 			register char c;
4391515Seric 
44025821Seric 			if (init && CheckAliases)
4414098Seric 			{
4424157Seric 				/* do parsing & compression of addresses */
44325278Seric 				while (*p != '\0')
4444098Seric 				{
44525278Seric 					extern char *DelimChar;
44625278Seric 
44725278Seric 					while (isspace(*p) || *p == ',')
4484157Seric 						p++;
44925278Seric 					if (*p == '\0')
45025278Seric 						break;
45125278Seric 					if (parseaddr(p, &bl, -1, ',') == NULL)
45225278Seric 						usrerr("%s... bad address", p);
45325278Seric 					p = DelimChar;
4544098Seric 				}
4554098Seric 			}
4564157Seric 			else
45715769Seric 			{
45816898Seric 				p = &p[strlen(p)];
45916898Seric 				if (p[-1] == '\n')
46016898Seric 					*--p = '\0';
46115769Seric 			}
4621515Seric 
4634098Seric 			/* see if there should be a continuation line */
4644106Seric 			c = fgetc(af);
4654106Seric 			if (!feof(af))
4664314Seric 				(void) ungetc(c, af);
4674106Seric 			if (c != ' ' && c != '\t')
4684098Seric 				break;
4694098Seric 
4704098Seric 			/* read continuation line */
4714098Seric 			if (fgets(p, sizeof line - (p - line), af) == NULL)
4724098Seric 				break;
4739368Seric 			LineNumber++;
4744098Seric 		}
47516898Seric 		if (al.q_mailer != LocalMailer)
47616898Seric 		{
47716898Seric 			syserr("cannot alias non-local names");
47816898Seric 			continue;
47916898Seric 		}
4804314Seric 
4814314Seric 		/*
4824314Seric 		**  Insert alias into symbol table or DBM file
4834314Seric 		*/
4844314Seric 
48516898Seric 		lhssize = strlen(al.q_user) + 1;
4864322Seric 		rhssize = strlen(rhs) + 1;
4874322Seric 
4884157Seric # ifdef DBM
4894157Seric 		if (init)
4904157Seric 		{
4914157Seric 			DATUM key, content;
4924157Seric 
4934322Seric 			key.dsize = lhssize;
4944157Seric 			key.dptr = al.q_user;
4954322Seric 			content.dsize = rhssize;
4964157Seric 			content.dptr = rhs;
4974157Seric 			store(key, content);
4984157Seric 		}
4994157Seric 		else
5004157Seric # endif DBM
5014157Seric 		{
5024157Seric 			s = stab(al.q_user, ST_ALIAS, ST_ENTER);
5034157Seric 			s->s_alias = newstr(rhs);
5044157Seric 		}
5054322Seric 
5064322Seric 		/* statistics */
5074322Seric 		naliases++;
5084322Seric 		bytes += lhssize + rhssize;
5094322Seric 		if (rhssize > longest)
5104322Seric 			longest = rhssize;
5111515Seric 	}
51219784Seric 
51319784Seric # ifdef DBM
51419784Seric 	if (init)
51519784Seric 	{
51619784Seric 		/* add the distinquished alias "@" */
51719784Seric 		DATUM key;
51819784Seric 
51919784Seric 		key.dsize = 2;
52019784Seric 		key.dptr = "@";
52119784Seric 		store(key, key);
52219784Seric 
52319784Seric 		/* restore the old signal */
52419784Seric 		(void) signal(SIGINT, oldsigint);
52519784Seric 	}
52619784Seric # endif DBM
52719784Seric 
52819784Seric 	/* closing the alias file drops the lock */
5294098Seric 	(void) fclose(af);
5306898Seric 	CurEnv->e_to = NULL;
5319368Seric 	FileName = NULL;
5327051Seric 	message(Arpa_Info, "%d aliases, longest %d bytes, %d bytes total",
5334322Seric 			naliases, longest, bytes);
53424944Seric # ifdef LOG
53524944Seric 	if (LogLevel >= 8)
53624944Seric 		syslog(LOG_INFO, "%d aliases, longest %d bytes, %d bytes total",
53724944Seric 			naliases, longest, bytes);
53824944Seric # endif LOG
539292Seric }
540292Seric /*
541292Seric **  FORWARD -- Try to forward mail
542292Seric **
543292Seric **	This is similar but not identical to aliasing.
544292Seric **
545292Seric **	Parameters:
5464314Seric **		user -- the name of the user who's mail we would like
5474314Seric **			to forward to.  It must have been verified --
5484314Seric **			i.e., the q_home field must have been filled
5494314Seric **			in.
5504999Seric **		sendq -- a pointer to the head of the send queue to
5514999Seric **			put this user's aliases in.
552292Seric **
553292Seric **	Returns:
5544098Seric **		none.
555292Seric **
556292Seric **	Side Effects:
5573185Seric **		New names are added to send queues.
558292Seric */
559292Seric 
5604999Seric forward(user, sendq)
5612966Seric 	ADDRESS *user;
5624999Seric 	ADDRESS **sendq;
563292Seric {
5644078Seric 	char buf[60];
5654536Seric 	extern bool safefile();
5664069Seric 
5677671Seric 	if (tTd(27, 1))
5684098Seric 		printf("forward(%s)\n", user->q_paddr);
5694098Seric 
5704594Seric 	if (user->q_mailer != LocalMailer || bitset(QBADADDR, user->q_flags))
5714098Seric 		return;
5724314Seric 	if (user->q_home == NULL)
5734314Seric 		syserr("forward: no home");
5744069Seric 
5754069Seric 	/* good address -- look for .forward file in home */
5769368Seric 	define('z', user->q_home, CurEnv);
57716154Seric 	expand("\001z/.forward", buf, &buf[sizeof buf - 1], CurEnv);
5784536Seric 	if (!safefile(buf, user->q_uid, S_IREAD))
5794098Seric 		return;
5804069Seric 
5814069Seric 	/* we do have an address to forward to -- do it */
5824999Seric 	include(buf, "forwarding", user, sendq);
583292Seric }
584