xref: /csrg-svn/usr.sbin/sendmail/src/alias.c (revision 64035)
122694Sdist /*
235073Sbostic  * Copyright (c) 1983 Eric P. Allman
362522Sbostic  * Copyright (c) 1988, 1993
462522Sbostic  *	The Regents of the University of California.  All rights reserved.
533728Sbostic  *
642824Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822694Sdist 
958332Seric # include "sendmail.h"
1050577Seric # include <pwd.h>
1156766Seric 
1233728Sbostic #ifndef lint
13*64035Seric static char sccsid[] = "@(#)alias.c	8.6 (Berkeley) 07/26/93";
1433728Sbostic #endif /* not lint */
1559673Seric 
1659673Seric 
1760537Seric MAP	*AliasDB[MAXALIASDB + 1];	/* actual database list */
1859673Seric int	NAliasDBs;			/* number of alias databases */
1959673Seric /*
20292Seric **  ALIAS -- Compute aliases.
21292Seric **
229368Seric **	Scans the alias file for an alias for the given address.
239368Seric **	If found, it arranges to deliver to the alias list instead.
249368Seric **	Uses libdbm database if -DDBM.
25292Seric **
26292Seric **	Parameters:
274097Seric **		a -- address to alias.
284999Seric **		sendq -- a pointer to the head of the send queue
294999Seric **			to put the aliases in.
3058092Seric **		e -- the current envelope.
31292Seric **
32292Seric **	Returns:
33292Seric **		none
34292Seric **
35292Seric **	Side Effects:
363185Seric **		Aliases found are expanded.
37292Seric **
38292Seric **	Deficiencies:
39292Seric **		It should complain about names that are aliased to
40292Seric **			nothing.
41292Seric */
42292Seric 
4355012Seric alias(a, sendq, e)
444097Seric 	register ADDRESS *a;
454999Seric 	ADDRESS **sendq;
4655012Seric 	register ENVELOPE *e;
47292Seric {
484081Seric 	register char *p;
4958082Seric 	int naliases;
5058170Seric 	char *owner;
5158170Seric 	char obuf[MAXNAME + 6];
525701Seric 	extern char *aliaslookup();
53292Seric 
547671Seric 	if (tTd(27, 1))
554098Seric 		printf("alias(%s)\n", a->q_paddr);
56292Seric 
574098Seric 	/* don't realias already aliased names */
5858680Seric 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
594098Seric 		return;
604098Seric 
6159673Seric 	if (NoAlias)
6259673Seric 		return;
6359673Seric 
6455012Seric 	e->e_to = a->q_paddr;
654098Seric 
664314Seric 	/*
674314Seric 	**  Look up this name
684314Seric 	*/
694314Seric 
7059673Seric 	p = aliaslookup(a->q_user, e);
714098Seric 	if (p == NULL)
724098Seric 		return;
73292Seric 
74292Seric 	/*
754098Seric 	**  Match on Alias.
764098Seric 	**	Deliver to the target list.
771515Seric 	*/
781515Seric 
797671Seric 	if (tTd(27, 1))
804098Seric 		printf("%s (%s, %s) aliased to %s\n",
814098Seric 		    a->q_paddr, a->q_host, a->q_user, p);
8258092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
8358154Seric 	{
8458154Seric 		a->q_flags |= QVERIFIED;
8558884Seric 		e->e_nrcpts++;
8658092Seric 		return;
8758154Seric 	}
8858154Seric 	message("aliased to %s", p);
8957977Seric #ifdef LOG
9058020Seric 	if (LogLevel > 9)
9157977Seric 		syslog(LOG_INFO, "%s: alias %s => %s", e->e_id, a->q_paddr, p);
9257977Seric #endif
9358082Seric 	a->q_flags &= ~QSELFREF;
944098Seric 	AliasLevel++;
9558082Seric 	naliases = sendtolist(p, a, sendq, e);
964098Seric 	AliasLevel--;
9758082Seric 	if (naliases > 0 && !bitset(QSELFREF, a->q_flags))
9858065Seric 	{
9958065Seric 		if (tTd(27, 5))
10058065Seric 		{
10158065Seric 			printf("alias: QDONTSEND ");
10258065Seric 			printaddr(a, FALSE);
10358065Seric 		}
10458065Seric 		a->q_flags |= QDONTSEND;
10558065Seric 	}
10658170Seric 
10758170Seric 	/*
10858170Seric 	**  Look for owner of alias
10958170Seric 	*/
11058170Seric 
11158170Seric 	(void) strcpy(obuf, "owner-");
11258170Seric 	if (strncmp(a->q_user, "owner-", 6) == 0)
11358170Seric 		(void) strcat(obuf, "owner");
11458170Seric 	else
11558170Seric 		(void) strcat(obuf, a->q_user);
11658170Seric 	if (!bitnset(M_USR_UPPER, a->q_mailer->m_flags))
11758170Seric 		makelower(obuf);
11859673Seric 	owner = aliaslookup(obuf, e);
11958170Seric 	if (owner != NULL)
12058170Seric 	{
12158170Seric 		if (strchr(owner, ',') != NULL)
12258170Seric 			owner = obuf;
12358170Seric 		a->q_owner = newstr(owner);
12458170Seric 	}
1254098Seric }
1264098Seric /*
1275701Seric **  ALIASLOOKUP -- look up a name in the alias file.
1285701Seric **
1295701Seric **	Parameters:
1305701Seric **		name -- the name to look up.
1315701Seric **
1325701Seric **	Returns:
1335701Seric **		the value of name.
1345701Seric **		NULL if unknown.
1355701Seric **
1365701Seric **	Side Effects:
1375701Seric **		none.
1385701Seric **
1395701Seric **	Warnings:
1405701Seric **		The return value will be trashed across calls.
1415701Seric */
1425701Seric 
1435701Seric char *
14459673Seric aliaslookup(name, e)
1455701Seric 	char *name;
14659673Seric 	ENVELOPE *e;
1475701Seric {
14859673Seric 	register int dbno;
14960089Seric 	register MAP *map;
15059673Seric 	register char *p;
1515701Seric 
15259673Seric 	for (dbno = 0; dbno < NAliasDBs; dbno++)
15359673Seric 	{
15460089Seric 		auto int stat;
15560089Seric 
15660537Seric 		map = AliasDB[dbno];
15760207Seric 		if (!bitset(MF_OPEN, map->map_mflags))
15859673Seric 			continue;
15960207Seric 		p = (*map->map_class->map_lookup)(map, name, NULL, &stat);
16059673Seric 		if (p != NULL)
16159673Seric 			return p;
16259673Seric 	}
16359673Seric 	return NULL;
16459673Seric }
16559673Seric /*
16659673Seric **  SETALIAS -- set up an alias map
16759673Seric **
16859673Seric **	Called when reading configuration file.
16959673Seric **
17059673Seric **	Parameters:
17159673Seric **		spec -- the alias specification
17259673Seric **
17359673Seric **	Returns:
17459673Seric **		none.
17559673Seric */
17657381Seric 
17759673Seric setalias(spec)
17859673Seric 	char *spec;
17959673Seric {
18059673Seric 	register char *p;
18160089Seric 	register MAP *map;
18259673Seric 	char *class;
18359673Seric 	STAB *s;
18459673Seric 
18559697Seric 	if (tTd(27, 8))
18659697Seric 		printf("setalias(%s)\n", spec);
18759697Seric 
18859758Seric 	for (p = spec; p != NULL; )
18951756Seric 	{
19060537Seric 		char aname[50];
19160537Seric 
19259758Seric 		while (isspace(*p))
19359758Seric 			p++;
19460502Seric 		if (*p == '\0')
19559673Seric 			break;
19659673Seric 		spec = p;
19759673Seric 
19859758Seric 		if (NAliasDBs >= MAXALIASDB)
19959758Seric 		{
20059758Seric 			syserr("Too many alias databases defined, %d max", MAXALIASDB);
20159758Seric 			return;
20259758Seric 		}
20360537Seric 		(void) sprintf(aname, "Alias%d", NAliasDBs);
20460537Seric 		s = stab(aname, ST_MAP, ST_ENTER);
20560537Seric 		map = &s->s_map;
20660537Seric 		AliasDB[NAliasDBs] = map;
20760089Seric 		bzero(map, sizeof *map);
20859758Seric 
20959758Seric 		p = strpbrk(p, " ,/:");
21059758Seric 		if (p != NULL && *p == ':')
21159758Seric 		{
21260089Seric 			/* map name */
21359758Seric 			*p++ = '\0';
21459758Seric 			class = spec;
21559758Seric 			spec = p;
21659758Seric 		}
21759758Seric 		else
21859758Seric 		{
21959758Seric 			class = "implicit";
22060228Seric 			map->map_mflags = MF_OPTIONAL|MF_INCLNULL;
22159758Seric 		}
22259758Seric 
22359758Seric 		/* find end of spec */
22459758Seric 		if (p != NULL)
22559758Seric 			p = strchr(p, ',');
22659758Seric 		if (p != NULL)
22759758Seric 			*p++ = '\0';
22859758Seric 
22959758Seric 		/* look up class */
23060089Seric 		s = stab(class, ST_MAPCLASS, ST_FIND);
23159758Seric 		if (s == NULL)
23259758Seric 		{
23359758Seric 			if (tTd(27, 1))
23459758Seric 				printf("Unknown alias class %s\n", class);
23559758Seric 		}
23660207Seric 		else if (!bitset(MCF_ALIASOK, s->s_mapclass.map_cflags))
23760207Seric 		{
23860207Seric 			syserr("setalias: map class %s can't handle aliases",
23960207Seric 				class);
24060207Seric 		}
24159758Seric 		else
24259758Seric 		{
24360207Seric 			map->map_class = &s->s_mapclass;
24460089Seric 			if (map->map_class->map_parse(map, spec))
24560089Seric 			{
24660207Seric 				map->map_mflags |= MF_VALID|MF_ALIAS;
24760089Seric 				NAliasDBs++;
24860089Seric 			}
24959758Seric 		}
25059756Seric 	}
2515701Seric }
2525701Seric /*
25359673Seric **  ALIASWAIT -- wait for distinguished @:@ token to appear.
25459673Seric **
25559673Seric **	This can decide to reopen or rebuild the alias file
25659673Seric */
25759673Seric 
25860207Seric aliaswait(map, ext)
25960089Seric 	MAP *map;
26060207Seric 	char *ext;
26159673Seric {
26259673Seric 	int atcnt;
26359673Seric 	time_t mtime;
26459673Seric 	struct stat stb;
26559673Seric 	char buf[MAXNAME];
26659673Seric 
26759697Seric 	if (tTd(27, 3))
26860207Seric 		printf("aliaswait(%s:%s)\n",
26960207Seric 			map->map_class->map_cname, map->map_file);
27059697Seric 
27117471Seric 	atcnt = SafeAlias * 2;
27217471Seric 	if (atcnt > 0)
27317471Seric 	{
27460089Seric 		auto int st;
27560089Seric 
27659673Seric 		while (atcnt-- >= 0 &&
27760089Seric 		       map->map_class->map_lookup(map, "@", NULL, &st) == NULL)
27825689Seric 		{
27925689Seric 			/*
28059673Seric 			**  Close and re-open the alias database in case
28159673Seric 			**  the one is mv'ed instead of cp'ed in.
28225689Seric 			*/
28325689Seric 
28459697Seric 			if (tTd(27, 2))
28559697Seric 				printf("aliaswait: sleeping\n");
28659697Seric 
28760089Seric 			map->map_class->map_close(map);
28817471Seric 			sleep(30);
28960089Seric 			map->map_class->map_open(map, O_RDONLY);
29025689Seric 		}
29117471Seric 	}
2928437Seric 
29359673Seric 	/* see if we need to go into auto-rebuild mode */
29460207Seric 	if (!bitset(MCF_REBUILDABLE, map->map_class->map_cflags))
29560207Seric 	{
29660207Seric 		if (tTd(27, 3))
29760207Seric 			printf("aliaswait: not rebuildable\n");
29859673Seric 		return;
29960207Seric 	}
30060207Seric 	if (stat(map->map_file, &stb) < 0)
30160207Seric 	{
30260207Seric 		if (tTd(27, 3))
30360207Seric 			printf("aliaswait: no source file\n");
30460207Seric 		return;
30560207Seric 	}
30659673Seric 	mtime = stb.st_mtime;
30760089Seric 	(void) strcpy(buf, map->map_file);
30860207Seric 	if (ext != NULL)
30960207Seric 		(void) strcat(buf, ext);
31059673Seric 	if (stat(buf, &stb) < 0 || stb.st_mtime < mtime || atcnt < 0)
3114322Seric 	{
31259673Seric 		/* database is out of date */
31340559Sbostic 		if (AutoRebuild && stb.st_ino != 0 && stb.st_uid == geteuid())
3144322Seric 		{
31560089Seric 			message("auto-rebuilding alias database %s", buf);
31660207Seric 			rebuildaliases(map, TRUE);
3174322Seric 		}
3184322Seric 		else
3194322Seric 		{
32019039Seric #ifdef LOG
32158020Seric 			if (LogLevel > 3)
32259673Seric 				syslog(LOG_INFO, "alias database %s out of date",
32360089Seric 					buf);
32456795Seric #endif /* LOG */
32560089Seric 			message("Warning: alias database %s out of date", buf);
3264322Seric 		}
3274322Seric 	}
32859673Seric }
32959673Seric /*
33059673Seric **  REBUILDALIASES -- rebuild the alias database.
33159673Seric **
33259673Seric **	Parameters:
33360089Seric **		map -- the database to rebuild.
33459673Seric **		automatic -- set if this was automatically generated.
33559673Seric **
33659673Seric **	Returns:
33759673Seric **		none.
33859673Seric **
33959673Seric **	Side Effects:
34059673Seric **		Reads the text version of the database, builds the
34159673Seric **		DBM or DB version.
34259673Seric */
3434322Seric 
34460207Seric rebuildaliases(map, automatic)
34560089Seric 	register MAP *map;
34659673Seric 	bool automatic;
34759673Seric {
34859673Seric 	FILE *af;
34959673Seric 	void (*oldsigint)();
3504322Seric 
35160207Seric 	if (!bitset(MCF_REBUILDABLE, map->map_class->map_cflags))
35259673Seric 		return;
3534322Seric 
35459673Seric #ifdef LOG
35559673Seric 	if (LogLevel > 7)
3568437Seric 	{
35759673Seric 		syslog(LOG_NOTICE, "alias database %s %srebuilt by %s",
35860089Seric 			map->map_file, automatic ? "auto" : "", username());
35959673Seric 	}
36056795Seric #endif /* LOG */
36159673Seric 
36259673Seric 	/* try to lock the source file */
36360089Seric 	if ((af = fopen(map->map_file, "r+")) == NULL)
36459673Seric 	{
36559756Seric 		if (tTd(27, 1))
36659756Seric 			printf("Can't open %s: %s\n",
36760089Seric 				map->map_file, errstring(errno));
36859673Seric 		errno = 0;
36959673Seric 		return;
3708437Seric 	}
37159673Seric 
37259673Seric 	/* see if someone else is rebuilding the alias file */
37360089Seric 	if (!lockfile(fileno(af), map->map_file, LOCK_EX|LOCK_NB))
37459673Seric 	{
37559673Seric 		/* yes, they are -- wait until done */
37659673Seric 		message("Alias file %s is already being rebuilt",
37760089Seric 			map->map_file);
37859673Seric 		if (OpMode != MD_INITALIAS)
37959673Seric 		{
38059673Seric 			/* wait for other rebuild to complete */
38160089Seric 			(void) lockfile(fileno(af), map->map_file,
38259673Seric 					LOCK_EX);
38359673Seric 		}
38459673Seric 		(void) fclose(af);
38559673Seric 		errno = 0;
38659673Seric 		return;
38759673Seric 	}
38859673Seric 
389*64035Seric 	oldsigint = setsignal(SIGINT, SIG_IGN);
39059673Seric 
39160207Seric 	if (map->map_class->map_open(map, O_RDWR))
39260089Seric 	{
39360207Seric 		map->map_mflags |= MF_OPEN|MF_WRITABLE;
39460207Seric 		readaliases(map, af, automatic);
39560089Seric 	}
39660207Seric 	else
39760207Seric 	{
39860207Seric 		if (tTd(27, 1))
39960207Seric 			printf("Can't create database for %s: %s\n",
40060207Seric 				map->map_file, errstring(errno));
40160207Seric 		if (!automatic)
40260207Seric 			syserr("Cannot create database for alias file %s",
40360207Seric 				map->map_file);
40460207Seric 	}
40559673Seric 
40659673Seric 	/* close the file, thus releasing locks */
40759673Seric 	fclose(af);
40859673Seric 
40959673Seric 	/* add distinguished entries and close the database */
41060207Seric 	if (bitset(MF_OPEN, map->map_mflags))
41160089Seric 		map->map_class->map_close(map);
41259673Seric 
41359673Seric 	/* restore the old signal */
414*64035Seric 	(void) setsignal(SIGINT, oldsigint);
4154157Seric }
4164157Seric /*
4174157Seric **  READALIASES -- read and process the alias file.
4184157Seric **
4194157Seric **	This routine implements the part of initaliases that occurs
4204157Seric **	when we are not going to use the DBM stuff.
4214157Seric **
4224157Seric **	Parameters:
42360089Seric **		map -- the alias database descriptor.
42459673Seric **		af -- file to read the aliases from.
42559733Seric **		automatic -- set if this was an automatic rebuild.
4264157Seric **
4274157Seric **	Returns:
4284157Seric **		none.
4294157Seric **
4304157Seric **	Side Effects:
4314157Seric **		Reads aliasfile into the symbol table.
4324157Seric **		Optionally, builds the .dir & .pag files.
4334157Seric */
4344157Seric 
43560207Seric readaliases(map, af, automatic)
43660089Seric 	register MAP *map;
43759673Seric 	FILE *af;
43859733Seric 	int automatic;
4394157Seric {
4404098Seric 	register char *p;
4414098Seric 	char *rhs;
4424098Seric 	bool skipping;
44359673Seric 	long naliases, bytes, longest;
4444098Seric 	ADDRESS al, bl;
4459368Seric 	char line[BUFSIZ];
4464098Seric 
4474314Seric 	/*
4484314Seric 	**  Read and interpret lines
4494314Seric 	*/
4504314Seric 
45160089Seric 	FileName = map->map_file;
4529368Seric 	LineNumber = 0;
4534322Seric 	naliases = bytes = longest = 0;
4544098Seric 	skipping = FALSE;
4554098Seric 	while (fgets(line, sizeof (line), af) != NULL)
4564098Seric 	{
4574322Seric 		int lhssize, rhssize;
4584322Seric 
4599368Seric 		LineNumber++;
46056795Seric 		p = strchr(line, '\n');
46125278Seric 		if (p != NULL)
46225278Seric 			*p = '\0';
4634098Seric 		switch (line[0])
4644098Seric 		{
4654098Seric 		  case '#':
4664098Seric 		  case '\0':
4674098Seric 			skipping = FALSE;
4684098Seric 			continue;
4694065Seric 
4704098Seric 		  case ' ':
4714098Seric 		  case '\t':
4724098Seric 			if (!skipping)
47358151Seric 				syserr("554 Non-continuation line starts with space");
4744098Seric 			skipping = TRUE;
4754097Seric 			continue;
4764098Seric 		}
4774098Seric 		skipping = FALSE;
4781874Seric 
4794314Seric 		/*
4804314Seric 		**  Process the LHS
48157736Seric 		**	Find the colon separator, and parse the address.
48216898Seric 		**	It should resolve to a local name -- this will
48316898Seric 		**	be checked later (we want to optionally do
48416898Seric 		**	parsing of the RHS first to maximize error
48516898Seric 		**	detection).
4864314Seric 		*/
4874314Seric 
4884098Seric 		for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++)
4894097Seric 			continue;
49016898Seric 		if (*p++ != ':')
4914098Seric 		{
49258151Seric 			syserr("554 missing colon");
4934097Seric 			continue;
4944098Seric 		}
49560207Seric 		if (parseaddr(line, &al, 1, ':', NULL, CurEnv) == NULL)
4964098Seric 		{
49758151Seric 			syserr("554 illegal alias name");
49816898Seric 			continue;
4994098Seric 		}
5004314Seric 
5014314Seric 		/*
5024314Seric 		**  Process the RHS.
5034314Seric 		**	'al' is the internal form of the LHS address.
5044314Seric 		**	'p' points to the text of the RHS.
5054314Seric 		*/
5064314Seric 
50758914Seric 		while (isascii(*p) && isspace(*p))
50858914Seric 			p++;
5094098Seric 		rhs = p;
5104098Seric 		for (;;)
5114098Seric 		{
5124098Seric 			register char c;
51358662Seric 			register char *nlp;
5141515Seric 
51558662Seric 			nlp = &p[strlen(p)];
51658662Seric 			if (nlp[-1] == '\n')
51758662Seric 				*--nlp = '\0';
51858662Seric 
51959673Seric 			if (CheckAliases)
5204098Seric 			{
5214157Seric 				/* do parsing & compression of addresses */
52225278Seric 				while (*p != '\0')
5234098Seric 				{
52458333Seric 					auto char *delimptr;
52525278Seric 
52658050Seric 					while ((isascii(*p) && isspace(*p)) ||
52758050Seric 								*p == ',')
5284157Seric 						p++;
52925278Seric 					if (*p == '\0')
53025278Seric 						break;
53160207Seric 					if (parseaddr(p, &bl, -1, ',', &delimptr, CurEnv) == NULL)
53258151Seric 						usrerr("553 %s... bad address", p);
53358333Seric 					p = delimptr;
5344098Seric 				}
5354098Seric 			}
5364157Seric 			else
53715769Seric 			{
53858662Seric 				p = nlp;
53915769Seric 			}
5401515Seric 
5414098Seric 			/* see if there should be a continuation line */
5424106Seric 			c = fgetc(af);
5434106Seric 			if (!feof(af))
5444314Seric 				(void) ungetc(c, af);
5454106Seric 			if (c != ' ' && c != '\t')
5464098Seric 				break;
5474098Seric 
5484098Seric 			/* read continuation line */
5494098Seric 			if (fgets(p, sizeof line - (p - line), af) == NULL)
5504098Seric 				break;
5519368Seric 			LineNumber++;
55257135Seric 
55357135Seric 			/* check for line overflow */
55457135Seric 			if (strchr(p, '\n') == NULL)
55557135Seric 			{
55658151Seric 				usrerr("554 alias too long");
55757135Seric 				break;
55857135Seric 			}
5594098Seric 		}
56016898Seric 		if (al.q_mailer != LocalMailer)
56116898Seric 		{
56258151Seric 			syserr("554 cannot alias non-local names");
56316898Seric 			continue;
56416898Seric 		}
5654314Seric 
5664314Seric 		/*
5674314Seric 		**  Insert alias into symbol table or DBM file
5684314Seric 		*/
5694314Seric 
57057381Seric 		if (!bitnset(M_USR_UPPER, al.q_mailer->m_flags))
57157381Seric 			makelower(al.q_user);
5724322Seric 
57359673Seric 		lhssize = strlen(al.q_user);
57459673Seric 		rhssize = strlen(rhs);
57560089Seric 		map->map_class->map_store(map, al.q_user, rhs);
5764157Seric 
57759673Seric 		if (al.q_paddr != NULL)
57859673Seric 			free(al.q_paddr);
57959673Seric 		if (al.q_host != NULL)
58059673Seric 			free(al.q_host);
58159673Seric 		if (al.q_user != NULL)
58259673Seric 			free(al.q_user);
5834322Seric 
5844322Seric 		/* statistics */
5854322Seric 		naliases++;
5864322Seric 		bytes += lhssize + rhssize;
5874322Seric 		if (rhssize > longest)
5884322Seric 			longest = rhssize;
5891515Seric 	}
59019784Seric 
59160207Seric 	CurEnv->e_to = NULL;
59259673Seric 	FileName = NULL;
59359733Seric 	if (Verbose || !automatic)
59459733Seric 		message("%s: %d aliases, longest %d bytes, %d bytes total",
59560089Seric 			map->map_file, naliases, longest, bytes);
59659673Seric # ifdef LOG
59759673Seric 	if (LogLevel > 7)
59859673Seric 		syslog(LOG_INFO, "%s: %d aliases, longest %d bytes, %d bytes total",
59960089Seric 			map->map_file, naliases, longest, bytes);
60059673Seric # endif /* LOG */
60159673Seric }
60259673Seric /*
603292Seric **  FORWARD -- Try to forward mail
604292Seric **
605292Seric **	This is similar but not identical to aliasing.
606292Seric **
607292Seric **	Parameters:
6084314Seric **		user -- the name of the user who's mail we would like
6094314Seric **			to forward to.  It must have been verified --
6104314Seric **			i.e., the q_home field must have been filled
6114314Seric **			in.
6124999Seric **		sendq -- a pointer to the head of the send queue to
6134999Seric **			put this user's aliases in.
614292Seric **
615292Seric **	Returns:
6164098Seric **		none.
617292Seric **
618292Seric **	Side Effects:
6193185Seric **		New names are added to send queues.
620292Seric */
621292Seric 
62255012Seric forward(user, sendq, e)
6232966Seric 	ADDRESS *user;
6244999Seric 	ADDRESS **sendq;
62555012Seric 	register ENVELOPE *e;
626292Seric {
62757136Seric 	char *pp;
62857136Seric 	char *ep;
62963902Seric #ifdef HASSETREUID
63063753Seric 	register ADDRESS *ca;
63163753Seric 	uid_t saveduid, uid;
63263753Seric #endif
6334069Seric 
6347671Seric 	if (tTd(27, 1))
6354098Seric 		printf("forward(%s)\n", user->q_paddr);
6364098Seric 
6374594Seric 	if (user->q_mailer != LocalMailer || bitset(QBADADDR, user->q_flags))
6384098Seric 		return;
6394314Seric 	if (user->q_home == NULL)
64058059Seric 	{
64158151Seric 		syserr("554 forward: no home");
64258059Seric 		user->q_home = "/nosuchdirectory";
64358059Seric 	}
6444069Seric 
6454069Seric 	/* good address -- look for .forward file in home */
64655012Seric 	define('z', user->q_home, e);
64757136Seric 	define('u', user->q_user, e);
64857136Seric 	define('h', user->q_host, e);
64957136Seric 	if (ForwardPath == NULL)
65058050Seric 		ForwardPath = newstr("\201z/.forward");
65157136Seric 
65263902Seric #ifdef HASSETREUID
65363753Seric 	ca = getctladdr(user);
65463753Seric 	if (ca != NULL)
65563753Seric 		uid = ca->q_uid;
65663753Seric 	else
65763753Seric 		uid = DefUid;
65863753Seric #endif
65963753Seric 
66057136Seric 	for (pp = ForwardPath; pp != NULL; pp = ep)
66157136Seric 	{
66258247Seric 		int err;
66357232Seric 		char buf[MAXPATHLEN+1];
66457136Seric 
66557136Seric 		ep = strchr(pp, ':');
66657136Seric 		if (ep != NULL)
66757136Seric 			*ep = '\0';
66857136Seric 		expand(pp, buf, &buf[sizeof buf - 1], e);
66957136Seric 		if (ep != NULL)
67057136Seric 			*ep++ = ':';
67157136Seric 		if (tTd(27, 3))
67257136Seric 			printf("forward: trying %s\n", buf);
67363753Seric 
67463787Seric 		if (tTd(27, 9))
67563787Seric 			printf("forward: old uid = %d/%d\n", getuid(), geteuid());
67663787Seric 
67763902Seric #ifdef HASSETREUID
67863753Seric 		saveduid = geteuid();
67963753Seric 		if (saveduid == 0 && uid != 0)
68063902Seric 			(void) setreuid(0, uid);
68163753Seric #endif
68263753Seric 
68363787Seric 		if (tTd(27, 9))
68463787Seric 			printf("forward: new uid = %d/%d\n", getuid(), geteuid());
68563787Seric 
68658247Seric 		err = include(buf, TRUE, user, sendq, e);
68763753Seric 
68863902Seric #ifdef HASSETREUID
68963753Seric 		if (saveduid == 0 && uid != 0)
69063902Seric 			if (setreuid(-1, 0) < 0 || setreuid(RealUid, 0) < 0)
69163902Seric 				syserr("setreuid(%d, 0) failure (real=%d, eff=%d)",
69263902Seric 					RealUid, getuid(), geteuid());
69363753Seric #endif
69463753Seric 
69563787Seric 		if (tTd(27, 9))
69663787Seric 			printf("forward: reset uid = %d/%d\n", getuid(), geteuid());
69763787Seric 
69858247Seric 		if (err == 0)
69957136Seric 			break;
70058247Seric 		if (transienterror(err))
70158247Seric 		{
70258247Seric 			/* we have to suspend this message */
70359563Seric 			if (tTd(27, 2))
70459563Seric 				printf("forward: transient error on %s\n", buf);
70559563Seric #ifdef LOG
70659563Seric 			if (LogLevel > 2)
70759624Seric 				syslog(LOG_ERR, "%s: forward %s: transient error: %s",
70859624Seric 					e->e_id, buf, errstring(err));
70959563Seric #endif
71059611Seric 			message("%s: %s: message queued", buf, errstring(err));
71163853Seric 			user->q_flags |= QQUEUEUP;
71258247Seric 			return;
71358247Seric 		}
71457136Seric 	}
715292Seric }
716