xref: /csrg-svn/usr.sbin/sendmail/src/alias.c (revision 57977)
122694Sdist /*
235073Sbostic  * Copyright (c) 1983 Eric P. Allman
333728Sbostic  * Copyright (c) 1988 Regents of the University of California.
433728Sbostic  * All rights reserved.
533728Sbostic  *
642824Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822694Sdist 
950577Seric # include <sys/types.h>
1050577Seric # include <sys/stat.h>
1157948Seric # include <sys/file.h>
1250577Seric # include <signal.h>
1350577Seric # include "sendmail.h"
1457737Seric # include <fcntl.h>
1550577Seric # include <pwd.h>
1650577Seric 
1756845Seric # ifdef DBM
1856848Seric ERROR: DBM is no longer supported -- use NDBM instead.
1956845Seric # endif
2056845Seric 
2150577Seric # ifdef NEWDB
2250577Seric # include <db.h>
2350577Seric # endif
2450577Seric 
2556766Seric # ifdef NDBM
2656845Seric # include <ndbm.h>
2756766Seric # endif
2856766Seric 
2933728Sbostic #ifndef lint
3051756Seric #ifdef NEWDB
3157736Seric #ifdef NDBM
32*57977Seric static char sccsid[] = "@(#)alias.c	6.8 (Berkeley) 02/14/93 (with NEWDB and NDBM)";
3351756Seric #else
34*57977Seric static char sccsid[] = "@(#)alias.c	6.8 (Berkeley) 02/14/93 (with NEWDB)";
3557736Seric #endif
3657736Seric #else
3756845Seric #ifdef NDBM
38*57977Seric static char sccsid[] = "@(#)alias.c	6.8 (Berkeley) 02/14/93 (with NDBM)";
3933728Sbostic #else
40*57977Seric static char sccsid[] = "@(#)alias.c	6.8 (Berkeley) 02/14/93 (without NEWDB or NDBM)";
4133728Sbostic #endif
4250575Seric #endif
4333728Sbostic #endif /* not lint */
4451756Seric /*
45292Seric **  ALIAS -- Compute aliases.
46292Seric **
479368Seric **	Scans the alias file for an alias for the given address.
489368Seric **	If found, it arranges to deliver to the alias list instead.
499368Seric **	Uses libdbm database if -DDBM.
50292Seric **
51292Seric **	Parameters:
524097Seric **		a -- address to alias.
534999Seric **		sendq -- a pointer to the head of the send queue
544999Seric **			to put the aliases in.
55292Seric **
56292Seric **	Returns:
57292Seric **		none
58292Seric **
59292Seric **	Side Effects:
603185Seric **		Aliases found are expanded.
61292Seric **
62292Seric **	Notes:
63292Seric **		If NoAlias (the "-n" flag) is set, no aliasing is
64292Seric **			done.
65292Seric **
66292Seric **	Deficiencies:
67292Seric **		It should complain about names that are aliased to
68292Seric **			nothing.
69292Seric */
70292Seric 
71292Seric 
7253742Seric /*
7353742Seric **  Sun YP servers read the dbm files directly, so we have to build them
7453742Seric **  even if NEWDB
7553742Seric */
7653742Seric 
7756845Seric #ifdef NDBM
7853742Seric # ifndef NEWDB
7953742Seric #  define IF_MAKEDBMFILES
8053742Seric # else
8153742Seric #  ifdef YPCOMPAT
8253742Seric #   define IF_MAKEDBMFILES		if (makedbmfiles)
8353742Seric #  endif
8453742Seric # endif
8553742Seric #endif
8653742Seric 
8756845Seric typedef union
882966Seric {
8956845Seric #ifdef NDBM
9056845Seric 	datum	dbm;
9151756Seric #endif
9256845Seric #ifdef NEWDB
9356845Seric 	DBT	dbt;
9456845Seric #endif
9556845Seric 	struct
9656845Seric 	{
9756845Seric 		char	*data;
9856845Seric 		int	size;
9956845Seric 	} xx;
10056845Seric } DBdatum;
101292Seric 
10250575Seric #ifdef NEWDB
10350575Seric static DB	*AliasDBptr;
10450575Seric #endif
10556845Seric #ifdef NDBM
10656845Seric static DBM	*AliasDBMptr;
10756845Seric #endif
10850575Seric 
10955012Seric alias(a, sendq, e)
1104097Seric 	register ADDRESS *a;
1114999Seric 	ADDRESS **sendq;
11255012Seric 	register ENVELOPE *e;
113292Seric {
1144081Seric 	register char *p;
1155701Seric 	extern char *aliaslookup();
116292Seric 
1177671Seric 	if (tTd(27, 1))
1184098Seric 		printf("alias(%s)\n", a->q_paddr);
119292Seric 
1204098Seric 	/* don't realias already aliased names */
1214098Seric 	if (bitset(QDONTSEND, a->q_flags))
1224098Seric 		return;
1234098Seric 
12455012Seric 	e->e_to = a->q_paddr;
1254098Seric 
1264314Seric 	/*
1274314Seric 	**  Look up this name
1284314Seric 	*/
1294314Seric 
13024944Seric 	if (NoAlias)
13124944Seric 		p = NULL;
13224944Seric 	else
13324944Seric 		p = aliaslookup(a->q_user);
1344098Seric 	if (p == NULL)
1354098Seric 		return;
136292Seric 
137292Seric 	/*
1384098Seric 	**  Match on Alias.
1394098Seric 	**	Deliver to the target list.
1401515Seric 	*/
1411515Seric 
1427671Seric 	if (tTd(27, 1))
1434098Seric 		printf("%s (%s, %s) aliased to %s\n",
1444098Seric 		    a->q_paddr, a->q_host, a->q_user, p);
1457051Seric 	message(Arpa_Info, "aliased to %s", p);
146*57977Seric #ifdef LOG
147*57977Seric 	if (LogLevel >= 10)
148*57977Seric 		syslog(LOG_INFO, "%s: alias %s => %s", e->e_id, a->q_paddr, p);
149*57977Seric #endif
1504098Seric 	AliasLevel++;
15155012Seric 	sendtolist(p, a, sendq, e);
1524098Seric 	AliasLevel--;
1534098Seric }
1544098Seric /*
1555701Seric **  ALIASLOOKUP -- look up a name in the alias file.
1565701Seric **
1575701Seric **	Parameters:
1585701Seric **		name -- the name to look up.
1595701Seric **
1605701Seric **	Returns:
1615701Seric **		the value of name.
1625701Seric **		NULL if unknown.
1635701Seric **
1645701Seric **	Side Effects:
1655701Seric **		none.
1665701Seric **
1675701Seric **	Warnings:
1685701Seric **		The return value will be trashed across calls.
1695701Seric */
1705701Seric 
1715701Seric char *
1725701Seric aliaslookup(name)
1735701Seric 	char *name;
1745701Seric {
17557381Seric 	int i;
17657381Seric 	char keybuf[MAXNAME + 1];
17756845Seric # if defined(NEWDB) || defined(NDBM)
17856845Seric 	DBdatum rhs, lhs;
17950575Seric 	int s;
18057381Seric # else /* neither NEWDB nor NDBM */
18157381Seric 	register STAB *s;
18257381Seric # endif
1835701Seric 
1845701Seric 	/* create a key for fetch */
18557381Seric 	i = strlen(name) + 1;
18657381Seric 	if (i > sizeof keybuf)
18757381Seric 		i = sizeof keybuf;
18857381Seric 	bcopy(name, keybuf, i);
18957381Seric 	if (!bitnset(M_USR_UPPER, LocalMailer->m_flags))
19057381Seric 		makelower(keybuf);
19157381Seric 
19257381Seric # if defined(NEWDB) || defined(NDBM)
19357381Seric 	lhs.xx.size = i;
19457381Seric 	lhs.xx.data = keybuf;
19550575Seric # ifdef NEWDB
19651756Seric 	if (AliasDBptr != NULL)
19751756Seric 	{
19857381Seric 		i = AliasDBptr->get(AliasDBptr, &lhs.dbt, &rhs.dbt, 0);
19957381Seric 		if (i == 0)
20056845Seric 			return (rhs.dbt.data);
20151756Seric 	}
20256845Seric # ifdef NDBM
20357249Seric 	else if (AliasDBMptr != NULL)
20451756Seric 	{
20556845Seric 		rhs.dbm = dbm_fetch(AliasDBMptr, lhs.dbm);
20656845Seric 		return (rhs.dbm.dptr);
20751756Seric 	}
20856845Seric # endif /* NDBM */
20957530Seric 	return (NULL);
21056845Seric # else /* not NEWDB */
21156845Seric 	rhs.dbm = dbm_fetch(AliasDBMptr, lhs.dbm);
21256845Seric 	return (rhs.dbm.dptr);
21356845Seric # endif /* NEWDB */
21456845Seric # else /* neither NEWDB nor NDBM */
21557381Seric 	s = stab(keybuf, ST_ALIAS, ST_FIND);
21651756Seric 	if (s != NULL)
21751756Seric 		return (s->s_alias);
21857530Seric 	return (NULL);
21951756Seric # endif
2205701Seric }
2215701Seric /*
2224098Seric **  INITALIASES -- initialize for aliasing
2234098Seric **
22456845Seric **	Very different depending on whether we are running NDBM or not.
2254098Seric **
2264098Seric **	Parameters:
2274098Seric **		aliasfile -- location of aliases.
22856845Seric **		init -- if set and if NDBM, initialize the NDBM files.
2294098Seric **
2304098Seric **	Returns:
2314098Seric **		none.
2324098Seric **
2334098Seric **	Side Effects:
2344098Seric **		initializes aliases:
23556845Seric **		if NDBM:  opens the database.
23656845Seric **		if ~NDBM: reads the aliases into the symbol table.
2374098Seric */
2384098Seric 
23940559Sbostic # define DBMMODE	0644
2404157Seric 
24155012Seric initaliases(aliasfile, init, e)
2424098Seric 	char *aliasfile;
2434157Seric 	bool init;
24455012Seric 	register ENVELOPE *e;
2454098Seric {
24656845Seric #if defined(NDBM) || defined(NEWDB)
2478437Seric 	int atcnt;
24825522Seric 	time_t modtime;
24925522Seric 	bool automatic = FALSE;
2504322Seric 	char buf[MAXNAME];
25150575Seric #endif
2529368Seric 	struct stat stb;
25327176Seric 	static bool initialized = FALSE;
25446928Sbostic 	static int readaliases();
2554322Seric 
25627176Seric 	if (initialized)
25727176Seric 		return;
25827176Seric 	initialized = TRUE;
25927176Seric 
26017984Seric 	if (aliasfile == NULL || stat(aliasfile, &stb) < 0)
2618437Seric 	{
26225522Seric 		if (aliasfile != NULL && init)
26325522Seric 			syserr("Cannot open %s", aliasfile);
2648437Seric 		NoAlias = TRUE;
26511937Seric 		errno = 0;
2668437Seric 		return;
2678437Seric 	}
2688437Seric 
26956845Seric # if defined(NDBM) || defined(NEWDB)
2704322Seric 	/*
2718437Seric 	**  Check to see that the alias file is complete.
2728437Seric 	**	If not, we will assume that someone died, and it is up
2738437Seric 	**	to us to rebuild it.
2748437Seric 	*/
2758437Seric 
27625689Seric 	if (!init)
27750575Seric 	{
27850575Seric # ifdef NEWDB
27950575Seric 		(void) strcpy(buf, aliasfile);
28050575Seric 		(void) strcat(buf, ".db");
28151171Sbostic 		AliasDBptr = dbopen(buf, O_RDONLY, DBMMODE, DB_HASH, NULL);
28250576Seric 		if (AliasDBptr == NULL)
28350576Seric 		{
28456845Seric # ifdef NDBM
28556845Seric 			AliasDBMptr = dbm_open(aliasfile, O_RDONLY, DBMMODE);
28657249Seric 			if (AliasDBMptr == NULL)
28757249Seric 			{
28857249Seric 				syserr("initaliases: cannot open %s", buf);
28957249Seric 				NoAlias = TRUE;
29057249Seric 				return;
29157249Seric 			}
29251756Seric # else
29350576Seric 			syserr("initaliases: cannot open %s", buf);
29450576Seric 			NoAlias = TRUE;
29550576Seric 			return;
29651756Seric # endif
29750576Seric 		}
29850575Seric # else
29956845Seric 		AliasDBMptr = dbm_open(aliasfile, O_RDONLY, DBMMODE);
30057249Seric 		if (AliasDBMptr == NULL)
30157249Seric 		{
302*57977Seric 			syserr("initaliases: cannot open DBM database %s.{pag,dir}",
303*57977Seric 				aliasfile);
30457249Seric 			NoAlias = TRUE;
30557249Seric 			return;
30657249Seric 		}
30750575Seric # endif
30850575Seric 	}
30917471Seric 	atcnt = SafeAlias * 2;
31017471Seric 	if (atcnt > 0)
31117471Seric 	{
31217471Seric 		while (!init && atcnt-- >= 0 && aliaslookup("@") == NULL)
31325689Seric 		{
31425689Seric 			/*
31525689Seric 			**  Reinitialize alias file in case the new
31625689Seric 			**  one is mv'ed in instead of cp'ed in.
31725689Seric 			**
31825689Seric 			**	Only works with new DBM -- old one will
31925689Seric 			**	just consume file descriptors forever.
32025689Seric 			**	If you have a dbmclose() it can be
32125689Seric 			**	added before the sleep(30).
32225689Seric 			*/
32325689Seric 
32450575Seric # ifdef NEWDB
32551756Seric 			if (AliasDBptr != NULL)
32651756Seric 				AliasDBptr->close(AliasDBptr);
32750575Seric # endif
32856845Seric # ifdef NDBM
32956845Seric 			if (AliasDBMptr != NULL)
33056845Seric 				dbm_close(AliasDBMptr);
33156845Seric # endif
33250575Seric 
33317471Seric 			sleep(30);
33450575Seric # ifdef NEWDB
33550575Seric 			(void) strcpy(buf, aliasfile);
33650575Seric 			(void) strcat(buf, ".db");
33751171Sbostic 			AliasDBptr =
33851171Sbostic 			    dbopen(buf, O_RDONLY, DBMMODE, DB_HASH, NULL);
33950576Seric 			if (AliasDBptr == NULL)
34050576Seric 			{
34151756Seric # ifdef NDBM
34256845Seric 				AliasDBMptr = dbm_open(aliasfile, O_RDONLY, DBMMODE);
34351756Seric # else
34450576Seric 				syserr("initaliases: cannot open %s", buf);
34550576Seric 				NoAlias = TRUE;
34650576Seric 				return;
34751756Seric # endif
34850576Seric 			}
34950575Seric # else
35025689Seric # ifdef NDBM
35156845Seric 			AliasDBMptr = dbm_open(aliasfile, O_RDONLY, DBMMODE);
35257249Seric 			if (AliasDBMptr == NULL)
35357249Seric 			{
35457249Seric 				syserr("initaliases: cannot open %s", buf);
35557249Seric 				NoAlias = TRUE;
35657249Seric 				return;
35757249Seric 			}
35850575Seric # endif
35950575Seric # endif
36025689Seric 		}
36117471Seric 	}
36217471Seric 	else
36317471Seric 		atcnt = 1;
3648437Seric 
3658437Seric 	/*
36656845Seric 	**  See if the NDBM version of the file is out of date with
3674322Seric 	**  the text version.  If so, go into 'init' mode automatically.
36840559Sbostic 	**	This only happens if our effective userid owns the DBM.
36940559Sbostic 	**	Note the unpalatable hack to see if the stat succeeded.
3704322Seric 	*/
3714322Seric 
3724322Seric 	modtime = stb.st_mtime;
3734322Seric 	(void) strcpy(buf, aliasfile);
37450575Seric # ifdef NEWDB
37550575Seric 	(void) strcat(buf, ".db");
37650575Seric # else
3774322Seric 	(void) strcat(buf, ".pag");
37850575Seric # endif
3794322Seric 	stb.st_ino = 0;
38019039Seric 	if (!init && (stat(buf, &stb) < 0 || stb.st_mtime < modtime || atcnt < 0))
3814322Seric 	{
38211937Seric 		errno = 0;
38340559Sbostic 		if (AutoRebuild && stb.st_ino != 0 && stb.st_uid == geteuid())
3844322Seric 		{
3854322Seric 			init = TRUE;
38625522Seric 			automatic = TRUE;
3877051Seric 			message(Arpa_Info, "rebuilding alias database");
38824944Seric #ifdef LOG
38924944Seric 			if (LogLevel >= 7)
39024944Seric 				syslog(LOG_INFO, "rebuilding alias database");
39156795Seric #endif /* LOG */
3924322Seric 		}
3934322Seric 		else
3944322Seric 		{
39519039Seric #ifdef LOG
39624944Seric 			if (LogLevel >= 7)
39724944Seric 				syslog(LOG_INFO, "alias database out of date");
39856795Seric #endif /* LOG */
3994322Seric 			message(Arpa_Info, "Warning: alias database out of date");
4004322Seric 		}
4014322Seric 	}
4024322Seric 
4034322Seric 
4044322Seric 	/*
40556845Seric 	**  If necessary, load the NDBM file.
40656845Seric 	**	If running without NDBM, load the symbol table.
4074322Seric 	*/
4084322Seric 
4094157Seric 	if (init)
4108437Seric 	{
41125522Seric #ifdef LOG
41225522Seric 		if (LogLevel >= 6)
41325522Seric 		{
41425522Seric 			extern char *username();
41525522Seric 
41625522Seric 			syslog(LOG_NOTICE, "alias database %srebuilt by %s",
41725522Seric 				automatic ? "auto" : "", username());
41825522Seric 		}
41956795Seric #endif /* LOG */
42055012Seric 		readaliases(aliasfile, TRUE, e);
4218437Seric 	}
42256845Seric # else /* NDBM */
42355012Seric 	readaliases(aliasfile, init, e);
42456845Seric # endif /* NDBM */
4254157Seric }
4264157Seric /*
4274157Seric **  READALIASES -- read and process the alias file.
4284157Seric **
4294157Seric **	This routine implements the part of initaliases that occurs
4304157Seric **	when we are not going to use the DBM stuff.
4314157Seric **
4324157Seric **	Parameters:
4334157Seric **		aliasfile -- the pathname of the alias file master.
43456845Seric **		init -- if set, initialize the NDBM stuff.
4354157Seric **
4364157Seric **	Returns:
4374157Seric **		none.
4384157Seric **
4394157Seric **	Side Effects:
4404157Seric **		Reads aliasfile into the symbol table.
4414157Seric **		Optionally, builds the .dir & .pag files.
4424157Seric */
4434157Seric 
4444157Seric static
44555012Seric readaliases(aliasfile, init, e)
4464157Seric 	char *aliasfile;
4474157Seric 	bool init;
44855012Seric 	register ENVELOPE *e;
4494157Seric {
4504098Seric 	register char *p;
4514098Seric 	char *rhs;
4524098Seric 	bool skipping;
4539368Seric 	int naliases, bytes, longest;
4549368Seric 	FILE *af;
45553742Seric 	bool makedbmfiles;
45640970Sbostic 	void (*oldsigint)();
4574098Seric 	ADDRESS al, bl;
4584106Seric 	register STAB *s;
45950575Seric # ifdef NEWDB
46050575Seric 	DB *dbp;
46150575Seric # endif
46256845Seric # ifdef NDBM
46356845Seric 	DBM *dbmp;
46456845Seric # endif
46551937Seric # ifdef LOCKF
46651937Seric 	struct flock fld;
46751937Seric # endif
4689368Seric 	char line[BUFSIZ];
4694098Seric 
47051937Seric 	if ((af = fopen(aliasfile, "r+")) == NULL)
4711515Seric 	{
47257249Seric 		if (init)
47357249Seric 			syserr("Can't open %s", aliasfile);
47457249Seric 		else if (tTd(27, 1))
4754106Seric 			printf("Can't open %s\n", aliasfile);
4764098Seric 		errno = 0;
4774098Seric 		NoAlias++;
4784098Seric 		return;
4794098Seric 	}
4804314Seric 
48156845Seric # if defined(NDBM) || defined(NEWDB)
48219784Seric 	/* see if someone else is rebuilding the alias file already */
48351835Seric # ifdef LOCKF
48451937Seric 	fld.l_type = F_WRLCK;
48551937Seric 	fld.l_whence = fld.l_start = fld.l_len = 0;
48651937Seric 	if (fcntl(fileno(af), F_SETLK, &fld) < 0)
48751835Seric # else
48819784Seric 	if (flock(fileno(af), LOCK_EX | LOCK_NB) < 0 && errno == EWOULDBLOCK)
48951835Seric # endif
49019784Seric 	{
49119784Seric 		/* yes, they are -- wait until done and then return */
49219784Seric 		message(Arpa_Info, "Alias file is already being rebuilt");
49319784Seric 		if (OpMode != MD_INITALIAS)
49419784Seric 		{
49519784Seric 			/* wait for other rebuild to complete */
49651835Seric # ifdef LOCKF
49751937Seric 			(void) fcntl(fileno(af), F_SETLKW, &fld);
49851835Seric # else
49919784Seric 			(void) flock(fileno(af), LOCK_EX);
50051835Seric # endif
50119784Seric 		}
50223108Seric 		(void) fclose(af);
50319784Seric 		errno = 0;
50419784Seric 		return;
50519784Seric 	}
50656845Seric # endif /* NDBM */
50719784Seric 
5084314Seric 	/*
50919784Seric 	**  If initializing, create the new DBM files.
51019784Seric 	*/
51119784Seric 
51219784Seric 	if (init)
51319784Seric 	{
51419784Seric 		oldsigint = signal(SIGINT, SIG_IGN);
51551756Seric # ifdef NEWDB
51651756Seric 		(void) strcpy(line, aliasfile);
51751756Seric 		(void) strcat(line, ".db");
51851756Seric 		dbp = dbopen(line,
51951756Seric 		    O_RDWR|O_CREAT|O_TRUNC, DBMMODE, DB_HASH, NULL);
52051756Seric 		if (dbp == NULL)
52151756Seric 		{
52251756Seric 			syserr("readaliases: cannot create %s", line);
52351756Seric 			(void) signal(SIGINT, oldsigint);
52451756Seric 			return;
52551756Seric 		}
52653742Seric # endif
52753742Seric # ifdef IF_MAKEDBMFILES
52853742Seric # ifdef NEWDB
52953742Seric 		makedbmfiles = access("/var/yp/Makefile", R_OK) == 0;
53053742Seric # endif
53153742Seric 		IF_MAKEDBMFILES
53219784Seric 		{
53356845Seric 			dbmp = dbm_open(aliasfile,
53456845Seric 					       O_RDWR|O_CREAT|O_TRUNC, DBMMODE);
53556845Seric 			if (dbmp == NULL)
53653742Seric 			{
53756845Seric 				syserr("readaliases: cannot create %s.{dir,pag}",
53856845Seric 					aliasfile);
53953742Seric 				(void) signal(SIGINT, oldsigint);
54053742Seric 				return;
54153742Seric 			}
54219784Seric 		}
54350575Seric # endif
54419784Seric 	}
54519784Seric 
54619784Seric 	/*
5474314Seric 	**  Read and interpret lines
5484314Seric 	*/
5494314Seric 
5509368Seric 	FileName = aliasfile;
5519368Seric 	LineNumber = 0;
5524322Seric 	naliases = bytes = longest = 0;
5534098Seric 	skipping = FALSE;
5544098Seric 	while (fgets(line, sizeof (line), af) != NULL)
5554098Seric 	{
5564322Seric 		int lhssize, rhssize;
5574322Seric 
5589368Seric 		LineNumber++;
55956795Seric 		p = strchr(line, '\n');
56025278Seric 		if (p != NULL)
56125278Seric 			*p = '\0';
5624098Seric 		switch (line[0])
5634098Seric 		{
5644098Seric 		  case '#':
5654098Seric 		  case '\0':
5664098Seric 			skipping = FALSE;
5674098Seric 			continue;
5684065Seric 
5694098Seric 		  case ' ':
5704098Seric 		  case '\t':
5714098Seric 			if (!skipping)
5729368Seric 				syserr("Non-continuation line starts with space");
5734098Seric 			skipping = TRUE;
5744097Seric 			continue;
5754098Seric 		}
5764098Seric 		skipping = FALSE;
5771874Seric 
5784314Seric 		/*
5794314Seric 		**  Process the LHS
58057736Seric 		**	Find the colon separator, and parse the address.
58116898Seric 		**	It should resolve to a local name -- this will
58216898Seric 		**	be checked later (we want to optionally do
58316898Seric 		**	parsing of the RHS first to maximize error
58416898Seric 		**	detection).
5854314Seric 		*/
5864314Seric 
5874098Seric 		for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++)
5884097Seric 			continue;
58916898Seric 		if (*p++ != ':')
5904098Seric 		{
5919368Seric 			syserr("missing colon");
5924097Seric 			continue;
5934098Seric 		}
59455012Seric 		if (parseaddr(line, &al, 1, ':', e) == NULL)
5954098Seric 		{
59616898Seric 			syserr("illegal alias name");
59716898Seric 			continue;
5984098Seric 		}
59916898Seric 		loweraddr(&al);
6004314Seric 
6014314Seric 		/*
6024314Seric 		**  Process the RHS.
6034314Seric 		**	'al' is the internal form of the LHS address.
6044314Seric 		**	'p' points to the text of the RHS.
6054314Seric 		*/
6064314Seric 
6074098Seric 		rhs = p;
6084098Seric 		for (;;)
6094098Seric 		{
6104098Seric 			register char c;
6111515Seric 
61225821Seric 			if (init && CheckAliases)
6134098Seric 			{
6144157Seric 				/* do parsing & compression of addresses */
61525278Seric 				while (*p != '\0')
6164098Seric 				{
61725278Seric 					extern char *DelimChar;
61825278Seric 
61925278Seric 					while (isspace(*p) || *p == ',')
6204157Seric 						p++;
62125278Seric 					if (*p == '\0')
62225278Seric 						break;
62355012Seric 					if (parseaddr(p, &bl, -1, ',', e) == NULL)
62425278Seric 						usrerr("%s... bad address", p);
62525278Seric 					p = DelimChar;
6264098Seric 				}
6274098Seric 			}
6284157Seric 			else
62915769Seric 			{
63016898Seric 				p = &p[strlen(p)];
63116898Seric 				if (p[-1] == '\n')
63216898Seric 					*--p = '\0';
63315769Seric 			}
6341515Seric 
6354098Seric 			/* see if there should be a continuation line */
6364106Seric 			c = fgetc(af);
6374106Seric 			if (!feof(af))
6384314Seric 				(void) ungetc(c, af);
6394106Seric 			if (c != ' ' && c != '\t')
6404098Seric 				break;
6414098Seric 
6424098Seric 			/* read continuation line */
6434098Seric 			if (fgets(p, sizeof line - (p - line), af) == NULL)
6444098Seric 				break;
6459368Seric 			LineNumber++;
64657135Seric 
64757135Seric 			/* check for line overflow */
64857135Seric 			if (strchr(p, '\n') == NULL)
64957135Seric 			{
65057135Seric 				usrerr("alias too long");
65157135Seric 				break;
65257135Seric 			}
6534098Seric 		}
65416898Seric 		if (al.q_mailer != LocalMailer)
65516898Seric 		{
65616898Seric 			syserr("cannot alias non-local names");
65716898Seric 			continue;
65816898Seric 		}
6594314Seric 
6604314Seric 		/*
6614314Seric 		**  Insert alias into symbol table or DBM file
6624314Seric 		*/
6634314Seric 
66416898Seric 		lhssize = strlen(al.q_user) + 1;
66557381Seric 		if (!bitnset(M_USR_UPPER, al.q_mailer->m_flags))
66657381Seric 			makelower(al.q_user);
6674322Seric 		rhssize = strlen(rhs) + 1;
6684322Seric 
66956845Seric # if defined(NDBM) || defined(NEWDB)
6704157Seric 		if (init)
6714157Seric 		{
67256845Seric 			DBdatum key, content;
67357381Seric 			int putstat;
6744157Seric 
67556845Seric 			key.xx.size = lhssize;
67656845Seric 			key.xx.data = al.q_user;
67756845Seric 			content.xx.size = rhssize;
67856845Seric 			content.xx.data = rhs;
67951756Seric # ifdef NEWDB
68057381Seric 			putstat = dbp->put(dbp, &key.dbt, &content.dbt,
68157381Seric 					   R_NOOVERWRITE);
68257381Seric 			if (putstat > 0)
68357381Seric 			{
684*57977Seric 				usrerr("050 Warning: duplicate alias name %s",
68557381Seric 					al.q_user);
68657381Seric 				putstat = dbp->put(dbp, &key.dbt,
68757381Seric 						   &content.dbt, 0);
68857381Seric 			}
68957381Seric 			if (putstat != 0)
69050576Seric 				syserr("readaliases: db put (%s)", al.q_user);
69150575Seric # endif
69253742Seric # ifdef IF_MAKEDBMFILES
69353742Seric 			IF_MAKEDBMFILES
69457381Seric 			{
69557381Seric 				putstat = dbm_store(dbmp, key.dbm, content.dbm,
69657381Seric 						    DBM_INSERT);
69757381Seric 				if (putstat > 0)
69857381Seric 				{
699*57977Seric 					usrerr("050 Warning: duplicate alias name %s",
70057381Seric 						al.q_user);
70157381Seric 					putstat = dbm_store(dbmp, key.dbm,
70257381Seric 							content.dbm, DBM_REPLACE);
70357381Seric 				}
70457381Seric 				if (putstat != 0)
70556845Seric 					syserr("readaliases: dbm store (%s)",
70656845Seric 						al.q_user);
70757381Seric 			}
70853742Seric # endif
70957381Seric 			if (al.q_paddr != NULL)
71057381Seric 				free(al.q_paddr);
71157381Seric 			if (al.q_host != NULL)
71257381Seric 				free(al.q_host);
71357381Seric 			if (al.q_user != NULL)
71457381Seric 				free(al.q_user);
7154157Seric 		}
7164157Seric 		else
71756845Seric # endif /* NDBM */
7184157Seric 		{
7194157Seric 			s = stab(al.q_user, ST_ALIAS, ST_ENTER);
7204157Seric 			s->s_alias = newstr(rhs);
7214157Seric 		}
7224322Seric 
7234322Seric 		/* statistics */
7244322Seric 		naliases++;
7254322Seric 		bytes += lhssize + rhssize;
7264322Seric 		if (rhssize > longest)
7274322Seric 			longest = rhssize;
7281515Seric 	}
72919784Seric 
73056845Seric # if defined(NDBM) || defined(NEWDB)
73119784Seric 	if (init)
73219784Seric 	{
73319784Seric 		/* add the distinquished alias "@" */
73456845Seric 		DBdatum key;
73519784Seric 
73656845Seric 		key.xx.size = 2;
73756845Seric 		key.xx.data = "@";
73850575Seric # ifdef NEWDB
73950576Seric 		if (dbp->sync(dbp) != 0 ||
74056845Seric 		    dbp->put(dbp, &key.dbt, &key.dbt, 0) != 0 ||
74150576Seric 		    dbp->close(dbp) != 0)
74250576Seric 			syserr("readaliases: db close failure");
74353742Seric # endif
74456789Seric # ifdef IF_MAKEDBMFILES
74556793Seric 		IF_MAKEDBMFILES
74656845Seric 		{
74756845Seric 			if (dbm_store(dbmp, key.dbm, key.dbm, DBM_REPLACE) != 0 ||
74856845Seric 			    dbm_error(dbmp))
74956845Seric 				syserr("readaliases: dbm close failure");
75056845Seric 			dbm_close(dbmp);
75156845Seric 		}
75250575Seric # endif
75319784Seric 
75419784Seric 		/* restore the old signal */
75519784Seric 		(void) signal(SIGINT, oldsigint);
75619784Seric 	}
75756845Seric # endif /* NDBM */
75819784Seric 
75919784Seric 	/* closing the alias file drops the lock */
7604098Seric 	(void) fclose(af);
76155012Seric 	e->e_to = NULL;
7629368Seric 	FileName = NULL;
7637051Seric 	message(Arpa_Info, "%d aliases, longest %d bytes, %d bytes total",
7644322Seric 			naliases, longest, bytes);
76524944Seric # ifdef LOG
76624944Seric 	if (LogLevel >= 8)
76724944Seric 		syslog(LOG_INFO, "%d aliases, longest %d bytes, %d bytes total",
76824944Seric 			naliases, longest, bytes);
76956795Seric # endif /* LOG */
770292Seric }
771292Seric /*
772292Seric **  FORWARD -- Try to forward mail
773292Seric **
774292Seric **	This is similar but not identical to aliasing.
775292Seric **
776292Seric **	Parameters:
7774314Seric **		user -- the name of the user who's mail we would like
7784314Seric **			to forward to.  It must have been verified --
7794314Seric **			i.e., the q_home field must have been filled
7804314Seric **			in.
7814999Seric **		sendq -- a pointer to the head of the send queue to
7824999Seric **			put this user's aliases in.
783292Seric **
784292Seric **	Returns:
7854098Seric **		none.
786292Seric **
787292Seric **	Side Effects:
7883185Seric **		New names are added to send queues.
789292Seric */
790292Seric 
79155012Seric forward(user, sendq, e)
7922966Seric 	ADDRESS *user;
7934999Seric 	ADDRESS **sendq;
79455012Seric 	register ENVELOPE *e;
795292Seric {
79657136Seric 	char *pp;
79757136Seric 	char *ep;
7984536Seric 	extern bool safefile();
7994069Seric 
8007671Seric 	if (tTd(27, 1))
8014098Seric 		printf("forward(%s)\n", user->q_paddr);
8024098Seric 
8034594Seric 	if (user->q_mailer != LocalMailer || bitset(QBADADDR, user->q_flags))
8044098Seric 		return;
8054314Seric 	if (user->q_home == NULL)
8064314Seric 		syserr("forward: no home");
8074069Seric 
8084069Seric 	/* good address -- look for .forward file in home */
80955012Seric 	define('z', user->q_home, e);
81057136Seric 	define('u', user->q_user, e);
81157136Seric 	define('h', user->q_host, e);
81257136Seric 	if (ForwardPath == NULL)
81357136Seric 		ForwardPath = newstr("\001z/.forward");
81457136Seric 
81557136Seric 	for (pp = ForwardPath; pp != NULL; pp = ep)
81657136Seric 	{
81757232Seric 		char buf[MAXPATHLEN+1];
81857136Seric 
81957136Seric 		ep = strchr(pp, ':');
82057136Seric 		if (ep != NULL)
82157136Seric 			*ep = '\0';
82257136Seric 		expand(pp, buf, &buf[sizeof buf - 1], e);
82357136Seric 		if (ep != NULL)
82457136Seric 			*ep++ = ':';
82557136Seric 		if (tTd(27, 3))
82657136Seric 			printf("forward: trying %s\n", buf);
82757136Seric 		if (include(buf, TRUE, user, sendq, e) == 0)
82857136Seric 			break;
82957136Seric 	}
830292Seric }
831