xref: /csrg-svn/usr.sbin/sendmail/src/alias.c (revision 64564)
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*64564Seric static char sccsid[] = "@(#)alias.c	8.15 (Berkeley) 09/22/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--;
9764301Seric 	if (!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;
34964388Seric 	bool nolock = FALSE;
350*64564Seric 	sigfunc_t oldsigint;
3514322Seric 
35260207Seric 	if (!bitset(MCF_REBUILDABLE, map->map_class->map_cflags))
35359673Seric 		return;
3544322Seric 
35559673Seric 	/* try to lock the source file */
35660089Seric 	if ((af = fopen(map->map_file, "r+")) == NULL)
35759673Seric 	{
35864388Seric 		if (errno != EACCES || automatic ||
35964388Seric 		    (af = fopen(map->map_file, "r")) == NULL)
36064388Seric 		{
36164388Seric 			int saveerr = errno;
36264382Seric 
36364388Seric 			if (tTd(27, 1))
36464388Seric 				printf("Can't open %s: %s\n",
36564388Seric 					map->map_file, errstring(saveerr));
36664388Seric 			if (!automatic)
36764388Seric 				message("newaliases: cannot open %s: %s",
36864388Seric 					map->map_file, errstring(saveerr));
36964388Seric 			errno = 0;
37064388Seric 			return;
37164388Seric 		}
37264388Seric 		nolock = TRUE;
37364388Seric 		message("warning: cannot lock %s: %s",
37464388Seric 			map->map_file, errstring(errno));
3758437Seric 	}
37659673Seric 
37759673Seric 	/* see if someone else is rebuilding the alias file */
37864388Seric 	if (!nolock &&
37964388Seric 	    !lockfile(fileno(af), map->map_file, NULL, LOCK_EX|LOCK_NB))
38059673Seric 	{
38159673Seric 		/* yes, they are -- wait until done */
38259673Seric 		message("Alias file %s is already being rebuilt",
38360089Seric 			map->map_file);
38459673Seric 		if (OpMode != MD_INITALIAS)
38559673Seric 		{
38659673Seric 			/* wait for other rebuild to complete */
38764335Seric 			(void) lockfile(fileno(af), map->map_file, NULL,
38859673Seric 					LOCK_EX);
38959673Seric 		}
39059673Seric 		(void) fclose(af);
39159673Seric 		errno = 0;
39259673Seric 		return;
39359673Seric 	}
39459673Seric 
39564035Seric 	oldsigint = setsignal(SIGINT, SIG_IGN);
39659673Seric 
39760207Seric 	if (map->map_class->map_open(map, O_RDWR))
39860089Seric 	{
39964382Seric #ifdef LOG
40064382Seric 		if (LogLevel > 7)
40164382Seric 		{
40264382Seric 			syslog(LOG_NOTICE, "alias database %s %srebuilt by %s",
40364382Seric 				map->map_file, automatic ? "auto" : "",
40464382Seric 				username());
40564382Seric 		}
40664382Seric #endif /* LOG */
40760207Seric 		map->map_mflags |= MF_OPEN|MF_WRITABLE;
40860207Seric 		readaliases(map, af, automatic);
40960089Seric 	}
41060207Seric 	else
41160207Seric 	{
41260207Seric 		if (tTd(27, 1))
41360207Seric 			printf("Can't create database for %s: %s\n",
41460207Seric 				map->map_file, errstring(errno));
41560207Seric 		if (!automatic)
41660207Seric 			syserr("Cannot create database for alias file %s",
41760207Seric 				map->map_file);
41860207Seric 	}
41959673Seric 
42059673Seric 	/* close the file, thus releasing locks */
42159673Seric 	fclose(af);
42259673Seric 
42359673Seric 	/* add distinguished entries and close the database */
42460207Seric 	if (bitset(MF_OPEN, map->map_mflags))
42560089Seric 		map->map_class->map_close(map);
42659673Seric 
42759673Seric 	/* restore the old signal */
42864035Seric 	(void) setsignal(SIGINT, oldsigint);
4294157Seric }
4304157Seric /*
4314157Seric **  READALIASES -- read and process the alias file.
4324157Seric **
4334157Seric **	This routine implements the part of initaliases that occurs
4344157Seric **	when we are not going to use the DBM stuff.
4354157Seric **
4364157Seric **	Parameters:
43760089Seric **		map -- the alias database descriptor.
43859673Seric **		af -- file to read the aliases from.
43959733Seric **		automatic -- set if this was an automatic rebuild.
4404157Seric **
4414157Seric **	Returns:
4424157Seric **		none.
4434157Seric **
4444157Seric **	Side Effects:
4454157Seric **		Reads aliasfile into the symbol table.
4464157Seric **		Optionally, builds the .dir & .pag files.
4474157Seric */
4484157Seric 
44960207Seric readaliases(map, af, automatic)
45060089Seric 	register MAP *map;
45159673Seric 	FILE *af;
45259733Seric 	int automatic;
4534157Seric {
4544098Seric 	register char *p;
4554098Seric 	char *rhs;
4564098Seric 	bool skipping;
45759673Seric 	long naliases, bytes, longest;
4584098Seric 	ADDRESS al, bl;
4599368Seric 	char line[BUFSIZ];
4604098Seric 
4614314Seric 	/*
4624314Seric 	**  Read and interpret lines
4634314Seric 	*/
4644314Seric 
46560089Seric 	FileName = map->map_file;
4669368Seric 	LineNumber = 0;
4674322Seric 	naliases = bytes = longest = 0;
4684098Seric 	skipping = FALSE;
4694098Seric 	while (fgets(line, sizeof (line), af) != NULL)
4704098Seric 	{
4714322Seric 		int lhssize, rhssize;
4724322Seric 
4739368Seric 		LineNumber++;
47456795Seric 		p = strchr(line, '\n');
47525278Seric 		if (p != NULL)
47625278Seric 			*p = '\0';
4774098Seric 		switch (line[0])
4784098Seric 		{
4794098Seric 		  case '#':
4804098Seric 		  case '\0':
4814098Seric 			skipping = FALSE;
4824098Seric 			continue;
4834065Seric 
4844098Seric 		  case ' ':
4854098Seric 		  case '\t':
4864098Seric 			if (!skipping)
48758151Seric 				syserr("554 Non-continuation line starts with space");
4884098Seric 			skipping = TRUE;
4894097Seric 			continue;
4904098Seric 		}
4914098Seric 		skipping = FALSE;
4921874Seric 
4934314Seric 		/*
4944314Seric 		**  Process the LHS
49557736Seric 		**	Find the colon separator, and parse the address.
49616898Seric 		**	It should resolve to a local name -- this will
49716898Seric 		**	be checked later (we want to optionally do
49816898Seric 		**	parsing of the RHS first to maximize error
49916898Seric 		**	detection).
5004314Seric 		*/
5014314Seric 
5024098Seric 		for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++)
5034097Seric 			continue;
50416898Seric 		if (*p++ != ':')
5054098Seric 		{
50658151Seric 			syserr("554 missing colon");
5074097Seric 			continue;
5084098Seric 		}
50964284Seric 		if (parseaddr(line, &al, RF_COPYALL, ':', NULL, CurEnv) == NULL)
5104098Seric 		{
51164278Seric 			syserr("554 %s... illegal alias name", al.q_paddr);
51216898Seric 			continue;
5134098Seric 		}
5144314Seric 
5154314Seric 		/*
5164314Seric 		**  Process the RHS.
5174314Seric 		**	'al' is the internal form of the LHS address.
5184314Seric 		**	'p' points to the text of the RHS.
5194314Seric 		*/
5204314Seric 
52158914Seric 		while (isascii(*p) && isspace(*p))
52258914Seric 			p++;
5234098Seric 		rhs = p;
5244098Seric 		for (;;)
5254098Seric 		{
5264098Seric 			register char c;
52758662Seric 			register char *nlp;
5281515Seric 
52958662Seric 			nlp = &p[strlen(p)];
53058662Seric 			if (nlp[-1] == '\n')
53158662Seric 				*--nlp = '\0';
53258662Seric 
53359673Seric 			if (CheckAliases)
5344098Seric 			{
5354157Seric 				/* do parsing & compression of addresses */
53625278Seric 				while (*p != '\0')
5374098Seric 				{
53858333Seric 					auto char *delimptr;
53925278Seric 
54058050Seric 					while ((isascii(*p) && isspace(*p)) ||
54158050Seric 								*p == ',')
5424157Seric 						p++;
54325278Seric 					if (*p == '\0')
54425278Seric 						break;
54564284Seric 					if (parseaddr(p, &bl, RF_COPYNONE, ',',
54664284Seric 						      &delimptr, CurEnv) == NULL)
54758151Seric 						usrerr("553 %s... bad address", p);
54858333Seric 					p = delimptr;
5494098Seric 				}
5504098Seric 			}
5514157Seric 			else
55215769Seric 			{
55358662Seric 				p = nlp;
55415769Seric 			}
5551515Seric 
5564098Seric 			/* see if there should be a continuation line */
5574106Seric 			c = fgetc(af);
5584106Seric 			if (!feof(af))
5594314Seric 				(void) ungetc(c, af);
5604106Seric 			if (c != ' ' && c != '\t')
5614098Seric 				break;
5624098Seric 
5634098Seric 			/* read continuation line */
5644098Seric 			if (fgets(p, sizeof line - (p - line), af) == NULL)
5654098Seric 				break;
5669368Seric 			LineNumber++;
56757135Seric 
56857135Seric 			/* check for line overflow */
56957135Seric 			if (strchr(p, '\n') == NULL)
57057135Seric 			{
57158151Seric 				usrerr("554 alias too long");
57257135Seric 				break;
57357135Seric 			}
5744098Seric 		}
57516898Seric 		if (al.q_mailer != LocalMailer)
57616898Seric 		{
57764278Seric 			syserr("554 %s... cannot alias non-local names",
57864278Seric 				al.q_paddr);
57916898Seric 			continue;
58016898Seric 		}
5814314Seric 
5824314Seric 		/*
5834314Seric 		**  Insert alias into symbol table or DBM file
5844314Seric 		*/
5854314Seric 
58657381Seric 		if (!bitnset(M_USR_UPPER, al.q_mailer->m_flags))
58757381Seric 			makelower(al.q_user);
5884322Seric 
58959673Seric 		lhssize = strlen(al.q_user);
59059673Seric 		rhssize = strlen(rhs);
59160089Seric 		map->map_class->map_store(map, al.q_user, rhs);
5924157Seric 
59359673Seric 		if (al.q_paddr != NULL)
59459673Seric 			free(al.q_paddr);
59559673Seric 		if (al.q_host != NULL)
59659673Seric 			free(al.q_host);
59759673Seric 		if (al.q_user != NULL)
59859673Seric 			free(al.q_user);
5994322Seric 
6004322Seric 		/* statistics */
6014322Seric 		naliases++;
6024322Seric 		bytes += lhssize + rhssize;
6034322Seric 		if (rhssize > longest)
6044322Seric 			longest = rhssize;
6051515Seric 	}
60619784Seric 
60760207Seric 	CurEnv->e_to = NULL;
60859673Seric 	FileName = NULL;
60959733Seric 	if (Verbose || !automatic)
61059733Seric 		message("%s: %d aliases, longest %d bytes, %d bytes total",
61160089Seric 			map->map_file, naliases, longest, bytes);
61259673Seric # ifdef LOG
61359673Seric 	if (LogLevel > 7)
61459673Seric 		syslog(LOG_INFO, "%s: %d aliases, longest %d bytes, %d bytes total",
61560089Seric 			map->map_file, naliases, longest, bytes);
61659673Seric # endif /* LOG */
61759673Seric }
61859673Seric /*
619292Seric **  FORWARD -- Try to forward mail
620292Seric **
621292Seric **	This is similar but not identical to aliasing.
622292Seric **
623292Seric **	Parameters:
6244314Seric **		user -- the name of the user who's mail we would like
6254314Seric **			to forward to.  It must have been verified --
6264314Seric **			i.e., the q_home field must have been filled
6274314Seric **			in.
6284999Seric **		sendq -- a pointer to the head of the send queue to
6294999Seric **			put this user's aliases in.
630292Seric **
631292Seric **	Returns:
6324098Seric **		none.
633292Seric **
634292Seric **	Side Effects:
6353185Seric **		New names are added to send queues.
636292Seric */
637292Seric 
63855012Seric forward(user, sendq, e)
6392966Seric 	ADDRESS *user;
6404999Seric 	ADDRESS **sendq;
64155012Seric 	register ENVELOPE *e;
642292Seric {
64357136Seric 	char *pp;
64457136Seric 	char *ep;
6454069Seric 
6467671Seric 	if (tTd(27, 1))
6474098Seric 		printf("forward(%s)\n", user->q_paddr);
6484098Seric 
6494594Seric 	if (user->q_mailer != LocalMailer || bitset(QBADADDR, user->q_flags))
6504098Seric 		return;
6514314Seric 	if (user->q_home == NULL)
65258059Seric 	{
65358151Seric 		syserr("554 forward: no home");
65458059Seric 		user->q_home = "/nosuchdirectory";
65558059Seric 	}
6564069Seric 
6574069Seric 	/* good address -- look for .forward file in home */
65855012Seric 	define('z', user->q_home, e);
65957136Seric 	define('u', user->q_user, e);
66057136Seric 	define('h', user->q_host, e);
66157136Seric 	if (ForwardPath == NULL)
66258050Seric 		ForwardPath = newstr("\201z/.forward");
66357136Seric 
66457136Seric 	for (pp = ForwardPath; pp != NULL; pp = ep)
66557136Seric 	{
66658247Seric 		int err;
66757232Seric 		char buf[MAXPATHLEN+1];
66857136Seric 
66957136Seric 		ep = strchr(pp, ':');
67057136Seric 		if (ep != NULL)
67157136Seric 			*ep = '\0';
67257136Seric 		expand(pp, buf, &buf[sizeof buf - 1], e);
67357136Seric 		if (ep != NULL)
67457136Seric 			*ep++ = ':';
67557136Seric 		if (tTd(27, 3))
67657136Seric 			printf("forward: trying %s\n", buf);
67763753Seric 
67858247Seric 		err = include(buf, TRUE, user, sendq, e);
67958247Seric 		if (err == 0)
68057136Seric 			break;
68164325Seric 		else if (transienterror(err))
68258247Seric 		{
68358247Seric 			/* we have to suspend this message */
68459563Seric 			if (tTd(27, 2))
68559563Seric 				printf("forward: transient error on %s\n", buf);
68659563Seric #ifdef LOG
68759563Seric 			if (LogLevel > 2)
68859624Seric 				syslog(LOG_ERR, "%s: forward %s: transient error: %s",
68959624Seric 					e->e_id, buf, errstring(err));
69059563Seric #endif
69159611Seric 			message("%s: %s: message queued", buf, errstring(err));
69263853Seric 			user->q_flags |= QQUEUEUP;
69358247Seric 			return;
69458247Seric 		}
69557136Seric 	}
696292Seric }
697