xref: /csrg-svn/usr.sbin/sendmail/src/alias.c (revision 58846)
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 
958332Seric # include "sendmail.h"
1050577Seric # include <sys/stat.h>
1150577Seric # include <signal.h>
1250577Seric # include <pwd.h>
1350577Seric 
1456845Seric # ifdef DBM
1556848Seric ERROR: DBM is no longer supported -- use NDBM instead.
1656845Seric # endif
1756845Seric 
1850577Seric # ifdef NEWDB
1950577Seric # include <db.h>
2050577Seric # endif
2150577Seric 
2256766Seric # ifdef NDBM
2356845Seric # include <ndbm.h>
2456766Seric # endif
2556766Seric 
2633728Sbostic #ifndef lint
2751756Seric #ifdef NEWDB
2857736Seric #ifdef NDBM
29*58846Seric static char sccsid[] = "@(#)alias.c	6.28 (Berkeley) 03/29/93 (with NEWDB and NDBM)";
3051756Seric #else
31*58846Seric static char sccsid[] = "@(#)alias.c	6.28 (Berkeley) 03/29/93 (with NEWDB)";
3257736Seric #endif
3357736Seric #else
3456845Seric #ifdef NDBM
35*58846Seric static char sccsid[] = "@(#)alias.c	6.28 (Berkeley) 03/29/93 (with NDBM)";
3633728Sbostic #else
37*58846Seric static char sccsid[] = "@(#)alias.c	6.28 (Berkeley) 03/29/93 (without NEWDB or NDBM)";
3833728Sbostic #endif
3950575Seric #endif
4033728Sbostic #endif /* not lint */
4151756Seric /*
42292Seric **  ALIAS -- Compute aliases.
43292Seric **
449368Seric **	Scans the alias file for an alias for the given address.
459368Seric **	If found, it arranges to deliver to the alias list instead.
469368Seric **	Uses libdbm database if -DDBM.
47292Seric **
48292Seric **	Parameters:
494097Seric **		a -- address to alias.
504999Seric **		sendq -- a pointer to the head of the send queue
514999Seric **			to put the aliases in.
5258092Seric **		e -- the current envelope.
53292Seric **
54292Seric **	Returns:
55292Seric **		none
56292Seric **
57292Seric **	Side Effects:
583185Seric **		Aliases found are expanded.
59292Seric **
60292Seric **	Notes:
61292Seric **		If NoAlias (the "-n" flag) is set, no aliasing is
62292Seric **			done.
63292Seric **
64292Seric **	Deficiencies:
65292Seric **		It should complain about names that are aliased to
66292Seric **			nothing.
67292Seric */
68292Seric 
69292Seric 
7053742Seric /*
7153742Seric **  Sun YP servers read the dbm files directly, so we have to build them
7253742Seric **  even if NEWDB
7353742Seric */
7453742Seric 
7556845Seric #ifdef NDBM
7653742Seric # ifndef NEWDB
7753742Seric #  define IF_MAKEDBMFILES
7853742Seric # else
7953742Seric #  ifdef YPCOMPAT
8053742Seric #   define IF_MAKEDBMFILES		if (makedbmfiles)
8153742Seric #  endif
8253742Seric # endif
8353742Seric #endif
8453742Seric 
8556845Seric typedef union
862966Seric {
8756845Seric #ifdef NDBM
8856845Seric 	datum	dbm;
8951756Seric #endif
9056845Seric #ifdef NEWDB
9156845Seric 	DBT	dbt;
9256845Seric #endif
9356845Seric 	struct
9456845Seric 	{
9556845Seric 		char	*data;
9656845Seric 		int	size;
9756845Seric 	} xx;
9856845Seric } DBdatum;
99292Seric 
10050575Seric #ifdef NEWDB
10150575Seric static DB	*AliasDBptr;
10250575Seric #endif
10356845Seric #ifdef NDBM
10456845Seric static DBM	*AliasDBMptr;
10556845Seric #endif
10650575Seric 
10755012Seric alias(a, sendq, e)
1084097Seric 	register ADDRESS *a;
1094999Seric 	ADDRESS **sendq;
11055012Seric 	register ENVELOPE *e;
111292Seric {
1124081Seric 	register char *p;
11358082Seric 	int naliases;
11458170Seric 	char *owner;
11558170Seric 	char obuf[MAXNAME + 6];
1165701Seric 	extern char *aliaslookup();
117292Seric 
1187671Seric 	if (tTd(27, 1))
1194098Seric 		printf("alias(%s)\n", a->q_paddr);
120292Seric 
1214098Seric 	/* don't realias already aliased names */
12258680Seric 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
1234098Seric 		return;
1244098Seric 
12555012Seric 	e->e_to = a->q_paddr;
1264098Seric 
1274314Seric 	/*
1284314Seric 	**  Look up this name
1294314Seric 	*/
1304314Seric 
13124944Seric 	if (NoAlias)
13224944Seric 		p = NULL;
13324944Seric 	else
13424944Seric 		p = aliaslookup(a->q_user);
1354098Seric 	if (p == NULL)
1364098Seric 		return;
137292Seric 
138292Seric 	/*
1394098Seric 	**  Match on Alias.
1404098Seric 	**	Deliver to the target list.
1411515Seric 	*/
1421515Seric 
1437671Seric 	if (tTd(27, 1))
1444098Seric 		printf("%s (%s, %s) aliased to %s\n",
1454098Seric 		    a->q_paddr, a->q_host, a->q_user, p);
14658092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
14758154Seric 	{
14858154Seric 		a->q_flags |= QVERIFIED;
14958092Seric 		return;
15058154Seric 	}
15158154Seric 	message("aliased to %s", p);
15257977Seric #ifdef LOG
15358020Seric 	if (LogLevel > 9)
15457977Seric 		syslog(LOG_INFO, "%s: alias %s => %s", e->e_id, a->q_paddr, p);
15557977Seric #endif
15658082Seric 	a->q_flags &= ~QSELFREF;
1574098Seric 	AliasLevel++;
15858082Seric 	naliases = sendtolist(p, a, sendq, e);
1594098Seric 	AliasLevel--;
16058082Seric 	if (naliases > 0 && !bitset(QSELFREF, a->q_flags))
16158065Seric 	{
16258065Seric 		if (tTd(27, 5))
16358065Seric 		{
16458065Seric 			printf("alias: QDONTSEND ");
16558065Seric 			printaddr(a, FALSE);
16658065Seric 		}
16758065Seric 		a->q_flags |= QDONTSEND;
16858065Seric 	}
16958170Seric 
17058170Seric 	/*
17158170Seric 	**  Look for owner of alias
17258170Seric 	*/
17358170Seric 
17458170Seric 	(void) strcpy(obuf, "owner-");
17558170Seric 	if (strncmp(a->q_user, "owner-", 6) == 0)
17658170Seric 		(void) strcat(obuf, "owner");
17758170Seric 	else
17858170Seric 		(void) strcat(obuf, a->q_user);
17958170Seric 	if (!bitnset(M_USR_UPPER, a->q_mailer->m_flags))
18058170Seric 		makelower(obuf);
18158170Seric 	owner = aliaslookup(obuf);
18258170Seric 	if (owner != NULL)
18358170Seric 	{
18458170Seric 		if (strchr(owner, ',') != NULL)
18558170Seric 			owner = obuf;
18658170Seric 		a->q_owner = newstr(owner);
18758170Seric 	}
1884098Seric }
1894098Seric /*
1905701Seric **  ALIASLOOKUP -- look up a name in the alias file.
1915701Seric **
1925701Seric **	Parameters:
1935701Seric **		name -- the name to look up.
1945701Seric **
1955701Seric **	Returns:
1965701Seric **		the value of name.
1975701Seric **		NULL if unknown.
1985701Seric **
1995701Seric **	Side Effects:
2005701Seric **		none.
2015701Seric **
2025701Seric **	Warnings:
2035701Seric **		The return value will be trashed across calls.
2045701Seric */
2055701Seric 
2065701Seric char *
2075701Seric aliaslookup(name)
2085701Seric 	char *name;
2095701Seric {
21057381Seric 	int i;
21157381Seric 	char keybuf[MAXNAME + 1];
21256845Seric # if defined(NEWDB) || defined(NDBM)
21356845Seric 	DBdatum rhs, lhs;
21450575Seric 	int s;
21557381Seric # else /* neither NEWDB nor NDBM */
21657381Seric 	register STAB *s;
21757381Seric # endif
2185701Seric 
2195701Seric 	/* create a key for fetch */
22057381Seric 	i = strlen(name) + 1;
22157381Seric 	if (i > sizeof keybuf)
22257381Seric 		i = sizeof keybuf;
22357381Seric 	bcopy(name, keybuf, i);
22457381Seric 	if (!bitnset(M_USR_UPPER, LocalMailer->m_flags))
22557381Seric 		makelower(keybuf);
22657381Seric 
22757381Seric # if defined(NEWDB) || defined(NDBM)
22857381Seric 	lhs.xx.size = i;
22957381Seric 	lhs.xx.data = keybuf;
23050575Seric # ifdef NEWDB
23151756Seric 	if (AliasDBptr != NULL)
23251756Seric 	{
23357381Seric 		i = AliasDBptr->get(AliasDBptr, &lhs.dbt, &rhs.dbt, 0);
23457381Seric 		if (i == 0)
23556845Seric 			return (rhs.dbt.data);
23651756Seric 	}
23756845Seric # ifdef NDBM
23857249Seric 	else if (AliasDBMptr != NULL)
23951756Seric 	{
24056845Seric 		rhs.dbm = dbm_fetch(AliasDBMptr, lhs.dbm);
24156845Seric 		return (rhs.dbm.dptr);
24251756Seric 	}
24356845Seric # endif /* NDBM */
24457530Seric 	return (NULL);
24556845Seric # else /* not NEWDB */
24656845Seric 	rhs.dbm = dbm_fetch(AliasDBMptr, lhs.dbm);
24756845Seric 	return (rhs.dbm.dptr);
24856845Seric # endif /* NEWDB */
24956845Seric # else /* neither NEWDB nor NDBM */
25057381Seric 	s = stab(keybuf, ST_ALIAS, ST_FIND);
25151756Seric 	if (s != NULL)
25251756Seric 		return (s->s_alias);
25357530Seric 	return (NULL);
25451756Seric # endif
2555701Seric }
2565701Seric /*
2574098Seric **  INITALIASES -- initialize for aliasing
2584098Seric **
25956845Seric **	Very different depending on whether we are running NDBM or not.
2604098Seric **
2614098Seric **	Parameters:
2624098Seric **		aliasfile -- location of aliases.
26356845Seric **		init -- if set and if NDBM, initialize the NDBM files.
2644098Seric **
2654098Seric **	Returns:
2664098Seric **		none.
2674098Seric **
2684098Seric **	Side Effects:
2694098Seric **		initializes aliases:
27056845Seric **		if NDBM:  opens the database.
27156845Seric **		if ~NDBM: reads the aliases into the symbol table.
2724098Seric */
2734098Seric 
27440559Sbostic # define DBMMODE	0644
2754157Seric 
27655012Seric initaliases(aliasfile, init, e)
2774098Seric 	char *aliasfile;
2784157Seric 	bool init;
27955012Seric 	register ENVELOPE *e;
2804098Seric {
28156845Seric #if defined(NDBM) || defined(NEWDB)
2828437Seric 	int atcnt;
28325522Seric 	time_t modtime;
28425522Seric 	bool automatic = FALSE;
2854322Seric 	char buf[MAXNAME];
28650575Seric #endif
2879368Seric 	struct stat stb;
28827176Seric 	static bool initialized = FALSE;
28946928Sbostic 	static int readaliases();
2904322Seric 
29127176Seric 	if (initialized)
29227176Seric 		return;
29327176Seric 	initialized = TRUE;
29427176Seric 
29517984Seric 	if (aliasfile == NULL || stat(aliasfile, &stb) < 0)
2968437Seric 	{
29725522Seric 		if (aliasfile != NULL && init)
29858151Seric 			syserr("554 Cannot open %s", aliasfile);
2998437Seric 		NoAlias = TRUE;
30011937Seric 		errno = 0;
3018437Seric 		return;
3028437Seric 	}
3038437Seric 
30456845Seric # if defined(NDBM) || defined(NEWDB)
3054322Seric 	/*
3068437Seric 	**  Check to see that the alias file is complete.
3078437Seric 	**	If not, we will assume that someone died, and it is up
3088437Seric 	**	to us to rebuild it.
3098437Seric 	*/
3108437Seric 
31125689Seric 	if (!init)
31250575Seric 	{
31350575Seric # ifdef NEWDB
31450575Seric 		(void) strcpy(buf, aliasfile);
31550575Seric 		(void) strcat(buf, ".db");
31651171Sbostic 		AliasDBptr = dbopen(buf, O_RDONLY, DBMMODE, DB_HASH, NULL);
31750576Seric 		if (AliasDBptr == NULL)
31850576Seric 		{
31956845Seric # ifdef NDBM
32056845Seric 			AliasDBMptr = dbm_open(aliasfile, O_RDONLY, DBMMODE);
32157249Seric 			if (AliasDBMptr == NULL)
32257249Seric 			{
32357249Seric 				syserr("initaliases: cannot open %s", buf);
32457249Seric 				NoAlias = TRUE;
32557249Seric 				return;
32657249Seric 			}
32751756Seric # else
32850576Seric 			syserr("initaliases: cannot open %s", buf);
32950576Seric 			NoAlias = TRUE;
33050576Seric 			return;
33151756Seric # endif
33250576Seric 		}
33350575Seric # else
33456845Seric 		AliasDBMptr = dbm_open(aliasfile, O_RDONLY, DBMMODE);
33557249Seric 		if (AliasDBMptr == NULL)
33657249Seric 		{
33757977Seric 			syserr("initaliases: cannot open DBM database %s.{pag,dir}",
33857977Seric 				aliasfile);
33957249Seric 			NoAlias = TRUE;
34057249Seric 			return;
34157249Seric 		}
34250575Seric # endif
34350575Seric 	}
34417471Seric 	atcnt = SafeAlias * 2;
34517471Seric 	if (atcnt > 0)
34617471Seric 	{
34717471Seric 		while (!init && atcnt-- >= 0 && aliaslookup("@") == NULL)
34825689Seric 		{
34925689Seric 			/*
35025689Seric 			**  Reinitialize alias file in case the new
35125689Seric 			**  one is mv'ed in instead of cp'ed in.
35225689Seric 			**
35325689Seric 			**	Only works with new DBM -- old one will
35425689Seric 			**	just consume file descriptors forever.
35525689Seric 			**	If you have a dbmclose() it can be
35625689Seric 			**	added before the sleep(30).
35725689Seric 			*/
35825689Seric 
35950575Seric # ifdef NEWDB
36051756Seric 			if (AliasDBptr != NULL)
36151756Seric 				AliasDBptr->close(AliasDBptr);
36250575Seric # endif
36356845Seric # ifdef NDBM
36456845Seric 			if (AliasDBMptr != NULL)
36556845Seric 				dbm_close(AliasDBMptr);
36656845Seric # endif
36750575Seric 
36817471Seric 			sleep(30);
36950575Seric # ifdef NEWDB
37050575Seric 			(void) strcpy(buf, aliasfile);
37150575Seric 			(void) strcat(buf, ".db");
37251171Sbostic 			AliasDBptr =
37351171Sbostic 			    dbopen(buf, O_RDONLY, DBMMODE, DB_HASH, NULL);
37450576Seric 			if (AliasDBptr == NULL)
37550576Seric 			{
37651756Seric # ifdef NDBM
37756845Seric 				AliasDBMptr = dbm_open(aliasfile, O_RDONLY, DBMMODE);
37851756Seric # else
37950576Seric 				syserr("initaliases: cannot open %s", buf);
38050576Seric 				NoAlias = TRUE;
38150576Seric 				return;
38251756Seric # endif
38350576Seric 			}
38450575Seric # else
38525689Seric # ifdef NDBM
38656845Seric 			AliasDBMptr = dbm_open(aliasfile, O_RDONLY, DBMMODE);
38757249Seric 			if (AliasDBMptr == NULL)
38857249Seric 			{
38958008Seric 				syserr("initaliases: cannot open DBM database %s.{pag,dir}",
39058008Seric 					aliasfile);
39157249Seric 				NoAlias = TRUE;
39257249Seric 				return;
39357249Seric 			}
39450575Seric # endif
39550575Seric # endif
39625689Seric 		}
39717471Seric 	}
39817471Seric 	else
39917471Seric 		atcnt = 1;
4008437Seric 
4018437Seric 	/*
40256845Seric 	**  See if the NDBM version of the file is out of date with
4034322Seric 	**  the text version.  If so, go into 'init' mode automatically.
40440559Sbostic 	**	This only happens if our effective userid owns the DBM.
40540559Sbostic 	**	Note the unpalatable hack to see if the stat succeeded.
4064322Seric 	*/
4074322Seric 
4084322Seric 	modtime = stb.st_mtime;
4094322Seric 	(void) strcpy(buf, aliasfile);
41050575Seric # ifdef NEWDB
41150575Seric 	(void) strcat(buf, ".db");
41250575Seric # else
4134322Seric 	(void) strcat(buf, ".pag");
41450575Seric # endif
4154322Seric 	stb.st_ino = 0;
41619039Seric 	if (!init && (stat(buf, &stb) < 0 || stb.st_mtime < modtime || atcnt < 0))
4174322Seric 	{
41811937Seric 		errno = 0;
41940559Sbostic 		if (AutoRebuild && stb.st_ino != 0 && stb.st_uid == geteuid())
4204322Seric 		{
4214322Seric 			init = TRUE;
42225522Seric 			automatic = TRUE;
42358151Seric 			message("rebuilding alias database");
42424944Seric #ifdef LOG
42558020Seric 			if (LogLevel > 14)
42624944Seric 				syslog(LOG_INFO, "rebuilding alias database");
42756795Seric #endif /* LOG */
4284322Seric 		}
4294322Seric 		else
4304322Seric 		{
43119039Seric #ifdef LOG
43258020Seric 			if (LogLevel > 3)
43324944Seric 				syslog(LOG_INFO, "alias database out of date");
43456795Seric #endif /* LOG */
43558151Seric 			message("Warning: alias database out of date");
4364322Seric 		}
4374322Seric 	}
4384322Seric 
4394322Seric 
4404322Seric 	/*
44156845Seric 	**  If necessary, load the NDBM file.
44256845Seric 	**	If running without NDBM, load the symbol table.
4434322Seric 	*/
4444322Seric 
4454157Seric 	if (init)
4468437Seric 	{
44725522Seric #ifdef LOG
44858020Seric 		if (LogLevel > 7)
44925522Seric 		{
45025522Seric 			extern char *username();
45125522Seric 
45225522Seric 			syslog(LOG_NOTICE, "alias database %srebuilt by %s",
45325522Seric 				automatic ? "auto" : "", username());
45425522Seric 		}
45556795Seric #endif /* LOG */
45655012Seric 		readaliases(aliasfile, TRUE, e);
4578437Seric 	}
45856845Seric # else /* NDBM */
45955012Seric 	readaliases(aliasfile, init, e);
46056845Seric # endif /* NDBM */
4614157Seric }
4624157Seric /*
4634157Seric **  READALIASES -- read and process the alias file.
4644157Seric **
4654157Seric **	This routine implements the part of initaliases that occurs
4664157Seric **	when we are not going to use the DBM stuff.
4674157Seric **
4684157Seric **	Parameters:
4694157Seric **		aliasfile -- the pathname of the alias file master.
47056845Seric **		init -- if set, initialize the NDBM stuff.
4714157Seric **
4724157Seric **	Returns:
4734157Seric **		none.
4744157Seric **
4754157Seric **	Side Effects:
4764157Seric **		Reads aliasfile into the symbol table.
4774157Seric **		Optionally, builds the .dir & .pag files.
4784157Seric */
4794157Seric 
4804157Seric static
48155012Seric readaliases(aliasfile, init, e)
4824157Seric 	char *aliasfile;
4834157Seric 	bool init;
48455012Seric 	register ENVELOPE *e;
4854157Seric {
4864098Seric 	register char *p;
4874098Seric 	char *rhs;
4884098Seric 	bool skipping;
4899368Seric 	int naliases, bytes, longest;
4909368Seric 	FILE *af;
49153742Seric 	bool makedbmfiles;
49240970Sbostic 	void (*oldsigint)();
4934098Seric 	ADDRESS al, bl;
4944106Seric 	register STAB *s;
49550575Seric # ifdef NEWDB
49650575Seric 	DB *dbp;
49750575Seric # endif
49856845Seric # ifdef NDBM
49956845Seric 	DBM *dbmp;
50056845Seric # endif
5019368Seric 	char line[BUFSIZ];
50258689Seric 	extern bool lockfile();
5034098Seric 
50451937Seric 	if ((af = fopen(aliasfile, "r+")) == NULL)
5051515Seric 	{
50657249Seric 		if (init)
50758151Seric 			syserr("554 Can't open %s", aliasfile);
50857249Seric 		else if (tTd(27, 1))
5094106Seric 			printf("Can't open %s\n", aliasfile);
5104098Seric 		errno = 0;
5114098Seric 		NoAlias++;
5124098Seric 		return;
5134098Seric 	}
5144314Seric 
51556845Seric # if defined(NDBM) || defined(NEWDB)
51619784Seric 	/* see if someone else is rebuilding the alias file already */
51758689Seric 	if (!lockfile(fileno(af), aliasfile, LOCK_EX|LOCK_NB))
51819784Seric 	{
51919784Seric 		/* yes, they are -- wait until done and then return */
52058151Seric 		message("Alias file is already being rebuilt");
52119784Seric 		if (OpMode != MD_INITALIAS)
52219784Seric 		{
52319784Seric 			/* wait for other rebuild to complete */
52458689Seric 			(void) lockfile(fileno(af), aliasfile, LOCK_EX);
52519784Seric 		}
52623108Seric 		(void) fclose(af);
52719784Seric 		errno = 0;
52819784Seric 		return;
52919784Seric 	}
53056845Seric # endif /* NDBM */
53119784Seric 
5324314Seric 	/*
53319784Seric 	**  If initializing, create the new DBM files.
53419784Seric 	*/
53519784Seric 
53619784Seric 	if (init)
53719784Seric 	{
53819784Seric 		oldsigint = signal(SIGINT, SIG_IGN);
53951756Seric # ifdef NEWDB
54051756Seric 		(void) strcpy(line, aliasfile);
54151756Seric 		(void) strcat(line, ".db");
54251756Seric 		dbp = dbopen(line,
54351756Seric 		    O_RDWR|O_CREAT|O_TRUNC, DBMMODE, DB_HASH, NULL);
54451756Seric 		if (dbp == NULL)
54551756Seric 		{
54651756Seric 			syserr("readaliases: cannot create %s", line);
54751756Seric 			(void) signal(SIGINT, oldsigint);
54851756Seric 			return;
54951756Seric 		}
55053742Seric # endif
55153742Seric # ifdef IF_MAKEDBMFILES
55253742Seric # ifdef NEWDB
55353742Seric 		makedbmfiles = access("/var/yp/Makefile", R_OK) == 0;
55453742Seric # endif
55553742Seric 		IF_MAKEDBMFILES
55619784Seric 		{
55756845Seric 			dbmp = dbm_open(aliasfile,
55856845Seric 					       O_RDWR|O_CREAT|O_TRUNC, DBMMODE);
55956845Seric 			if (dbmp == NULL)
56053742Seric 			{
56156845Seric 				syserr("readaliases: cannot create %s.{dir,pag}",
56256845Seric 					aliasfile);
56353742Seric 				(void) signal(SIGINT, oldsigint);
56453742Seric 				return;
56553742Seric 			}
56619784Seric 		}
56750575Seric # endif
56819784Seric 	}
56919784Seric 
57019784Seric 	/*
5714314Seric 	**  Read and interpret lines
5724314Seric 	*/
5734314Seric 
5749368Seric 	FileName = aliasfile;
5759368Seric 	LineNumber = 0;
5764322Seric 	naliases = bytes = longest = 0;
5774098Seric 	skipping = FALSE;
5784098Seric 	while (fgets(line, sizeof (line), af) != NULL)
5794098Seric 	{
5804322Seric 		int lhssize, rhssize;
5814322Seric 
5829368Seric 		LineNumber++;
58356795Seric 		p = strchr(line, '\n');
58425278Seric 		if (p != NULL)
58525278Seric 			*p = '\0';
5864098Seric 		switch (line[0])
5874098Seric 		{
5884098Seric 		  case '#':
5894098Seric 		  case '\0':
5904098Seric 			skipping = FALSE;
5914098Seric 			continue;
5924065Seric 
5934098Seric 		  case ' ':
5944098Seric 		  case '\t':
5954098Seric 			if (!skipping)
59658151Seric 				syserr("554 Non-continuation line starts with space");
5974098Seric 			skipping = TRUE;
5984097Seric 			continue;
5994098Seric 		}
6004098Seric 		skipping = FALSE;
6011874Seric 
6024314Seric 		/*
6034314Seric 		**  Process the LHS
60457736Seric 		**	Find the colon separator, and parse the address.
60516898Seric 		**	It should resolve to a local name -- this will
60616898Seric 		**	be checked later (we want to optionally do
60716898Seric 		**	parsing of the RHS first to maximize error
60816898Seric 		**	detection).
6094314Seric 		*/
6104314Seric 
6114098Seric 		for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++)
6124097Seric 			continue;
61316898Seric 		if (*p++ != ':')
6144098Seric 		{
61558151Seric 			syserr("554 missing colon");
6164097Seric 			continue;
6174098Seric 		}
61858333Seric 		if (parseaddr(line, &al, 1, ':', NULL, e) == NULL)
6194098Seric 		{
62058151Seric 			syserr("554 illegal alias name");
62116898Seric 			continue;
6224098Seric 		}
6234314Seric 
6244314Seric 		/*
6254314Seric 		**  Process the RHS.
6264314Seric 		**	'al' is the internal form of the LHS address.
6274314Seric 		**	'p' points to the text of the RHS.
6284314Seric 		*/
6294314Seric 
6304098Seric 		rhs = p;
6314098Seric 		for (;;)
6324098Seric 		{
6334098Seric 			register char c;
63458662Seric 			register char *nlp;
6351515Seric 
63658662Seric 			nlp = &p[strlen(p)];
63758662Seric 			if (nlp[-1] == '\n')
63858662Seric 				*--nlp = '\0';
63958662Seric 
64025821Seric 			if (init && CheckAliases)
6414098Seric 			{
6424157Seric 				/* do parsing & compression of addresses */
64325278Seric 				while (*p != '\0')
6444098Seric 				{
64558333Seric 					auto char *delimptr;
64625278Seric 
64758050Seric 					while ((isascii(*p) && isspace(*p)) ||
64858050Seric 								*p == ',')
6494157Seric 						p++;
65025278Seric 					if (*p == '\0')
65125278Seric 						break;
65258333Seric 					if (parseaddr(p, &bl, -1, ',', &delimptr, e) == NULL)
65358151Seric 						usrerr("553 %s... bad address", p);
65458333Seric 					p = delimptr;
6554098Seric 				}
6564098Seric 			}
6574157Seric 			else
65815769Seric 			{
65958662Seric 				p = nlp;
66015769Seric 			}
6611515Seric 
6624098Seric 			/* see if there should be a continuation line */
6634106Seric 			c = fgetc(af);
6644106Seric 			if (!feof(af))
6654314Seric 				(void) ungetc(c, af);
6664106Seric 			if (c != ' ' && c != '\t')
6674098Seric 				break;
6684098Seric 
6694098Seric 			/* read continuation line */
6704098Seric 			if (fgets(p, sizeof line - (p - line), af) == NULL)
6714098Seric 				break;
6729368Seric 			LineNumber++;
67357135Seric 
67457135Seric 			/* check for line overflow */
67557135Seric 			if (strchr(p, '\n') == NULL)
67657135Seric 			{
67758151Seric 				usrerr("554 alias too long");
67857135Seric 				break;
67957135Seric 			}
6804098Seric 		}
68116898Seric 		if (al.q_mailer != LocalMailer)
68216898Seric 		{
68358151Seric 			syserr("554 cannot alias non-local names");
68416898Seric 			continue;
68516898Seric 		}
6864314Seric 
6874314Seric 		/*
6884314Seric 		**  Insert alias into symbol table or DBM file
6894314Seric 		*/
6904314Seric 
69116898Seric 		lhssize = strlen(al.q_user) + 1;
69257381Seric 		if (!bitnset(M_USR_UPPER, al.q_mailer->m_flags))
69357381Seric 			makelower(al.q_user);
6944322Seric 		rhssize = strlen(rhs) + 1;
6954322Seric 
69656845Seric # if defined(NDBM) || defined(NEWDB)
6974157Seric 		if (init)
6984157Seric 		{
69956845Seric 			DBdatum key, content;
70057381Seric 			int putstat;
7014157Seric 
70256845Seric 			key.xx.size = lhssize;
70356845Seric 			key.xx.data = al.q_user;
70456845Seric 			content.xx.size = rhssize;
70556845Seric 			content.xx.data = rhs;
70651756Seric # ifdef NEWDB
70757381Seric 			putstat = dbp->put(dbp, &key.dbt, &content.dbt,
70857381Seric 					   R_NOOVERWRITE);
70957381Seric 			if (putstat > 0)
71057381Seric 			{
71157977Seric 				usrerr("050 Warning: duplicate alias name %s",
71257381Seric 					al.q_user);
71357381Seric 				putstat = dbp->put(dbp, &key.dbt,
71457381Seric 						   &content.dbt, 0);
71557381Seric 			}
71657381Seric 			if (putstat != 0)
71750576Seric 				syserr("readaliases: db put (%s)", al.q_user);
71850575Seric # endif
71953742Seric # ifdef IF_MAKEDBMFILES
72053742Seric 			IF_MAKEDBMFILES
72157381Seric 			{
72257381Seric 				putstat = dbm_store(dbmp, key.dbm, content.dbm,
72357381Seric 						    DBM_INSERT);
72457381Seric 				if (putstat > 0)
72557381Seric 				{
72657977Seric 					usrerr("050 Warning: duplicate alias name %s",
72757381Seric 						al.q_user);
72857381Seric 					putstat = dbm_store(dbmp, key.dbm,
72957381Seric 							content.dbm, DBM_REPLACE);
73057381Seric 				}
73157381Seric 				if (putstat != 0)
73256845Seric 					syserr("readaliases: dbm store (%s)",
73356845Seric 						al.q_user);
73457381Seric 			}
73553742Seric # endif
73657381Seric 			if (al.q_paddr != NULL)
73757381Seric 				free(al.q_paddr);
73857381Seric 			if (al.q_host != NULL)
73957381Seric 				free(al.q_host);
74057381Seric 			if (al.q_user != NULL)
74157381Seric 				free(al.q_user);
7424157Seric 		}
7434157Seric 		else
74456845Seric # endif /* NDBM */
7454157Seric 		{
7464157Seric 			s = stab(al.q_user, ST_ALIAS, ST_ENTER);
7474157Seric 			s->s_alias = newstr(rhs);
7484157Seric 		}
7494322Seric 
7504322Seric 		/* statistics */
7514322Seric 		naliases++;
7524322Seric 		bytes += lhssize + rhssize;
7534322Seric 		if (rhssize > longest)
7544322Seric 			longest = rhssize;
7551515Seric 	}
75619784Seric 
75756845Seric # if defined(NDBM) || defined(NEWDB)
75819784Seric 	if (init)
75919784Seric 	{
76019784Seric 		/* add the distinquished alias "@" */
76156845Seric 		DBdatum key;
76219784Seric 
76356845Seric 		key.xx.size = 2;
76456845Seric 		key.xx.data = "@";
76550575Seric # ifdef NEWDB
76650576Seric 		if (dbp->sync(dbp) != 0 ||
76756845Seric 		    dbp->put(dbp, &key.dbt, &key.dbt, 0) != 0 ||
76850576Seric 		    dbp->close(dbp) != 0)
76950576Seric 			syserr("readaliases: db close failure");
77053742Seric # endif
77156789Seric # ifdef IF_MAKEDBMFILES
77256793Seric 		IF_MAKEDBMFILES
77356845Seric 		{
77458059Seric #ifdef YPCOMPAT
775*58846Seric 			static void nis_magic P((DBM *dbmp));
776*58846Seric 
77758059Seric 			nis_magic(dbmp);
77858059Seric #endif
77956845Seric 			if (dbm_store(dbmp, key.dbm, key.dbm, DBM_REPLACE) != 0 ||
78056845Seric 			    dbm_error(dbmp))
78156845Seric 				syserr("readaliases: dbm close failure");
78256845Seric 			dbm_close(dbmp);
78356845Seric 		}
78450575Seric # endif
78519784Seric 
78619784Seric 		/* restore the old signal */
78719784Seric 		(void) signal(SIGINT, oldsigint);
78819784Seric 	}
78956845Seric # endif /* NDBM */
79019784Seric 
79119784Seric 	/* closing the alias file drops the lock */
7924098Seric 	(void) fclose(af);
79355012Seric 	e->e_to = NULL;
7949368Seric 	FileName = NULL;
79558151Seric 	message("%d aliases, longest %d bytes, %d bytes total",
7964322Seric 			naliases, longest, bytes);
79724944Seric # ifdef LOG
79858020Seric 	if (LogLevel > 7)
79924944Seric 		syslog(LOG_INFO, "%d aliases, longest %d bytes, %d bytes total",
80024944Seric 			naliases, longest, bytes);
80156795Seric # endif /* LOG */
802292Seric }
803292Seric /*
80458059Seric **  NIS_MAGIC -- Add NIS magic dbm data
80558059Seric **
80658059Seric **	This adds the magic entries needed by SunOS to make this a valid
80758059Seric **	NIS map.
80858059Seric **
80958059Seric **	Parameters:
81058059Seric **		dbmp -- a pointer to the DBM structure.
81158059Seric **
81258059Seric **	Returns:
81358059Seric **		none.
81458059Seric */
81558059Seric 
81658059Seric # ifdef YPCOMPAT
81758059Seric 
81858059Seric static void
81958059Seric nis_magic(dbmp)
82058059Seric 	DBM *dbmp;
82158059Seric {
82258059Seric 	int i;
82358059Seric 	static datum key[2] =
82458059Seric 	{
82558059Seric 		{ "YP_LAST_MODIFIED",	sizeof "YP_LAST_MODIFIED" - 1 },
82658059Seric 		{ "YP_MASTER_NAME",	sizeof "YP_MASTER_NAME" - 1 },
82758059Seric 	};
82858059Seric 	datum contents[2];
82958059Seric 	char tbuf[12];
83058059Seric 	char hbuf[MAXHOSTNAMELEN];
83158059Seric 
83258059Seric 	(void) sprintf(tbuf, "%010ld", curtime());
83358059Seric 	contents[0].dptr = tbuf;
83458059Seric 	contents[0].dsize = strlen(tbuf);
83558059Seric 
83658059Seric 	(void) myhostname(hbuf, sizeof hbuf);
83758059Seric 	contents[1].dptr = hbuf;
838*58846Seric 	contents[1].dsize = strlen(hbuf);
83958059Seric 
84058059Seric 	for (i = 0; i < sizeof key / sizeof *key; i++)
84158059Seric 	{
84258059Seric 		if (dbm_store(dbmp, key[i], contents[i], DBM_REPLACE) != 0 ||
84358059Seric 		    dbm_error(dbmp))
84458059Seric 			syserr("nis_magic: dbm_store failure");
84558059Seric 	}
84658059Seric }
84758059Seric 
84858059Seric # endif
84958059Seric /*
850292Seric **  FORWARD -- Try to forward mail
851292Seric **
852292Seric **	This is similar but not identical to aliasing.
853292Seric **
854292Seric **	Parameters:
8554314Seric **		user -- the name of the user who's mail we would like
8564314Seric **			to forward to.  It must have been verified --
8574314Seric **			i.e., the q_home field must have been filled
8584314Seric **			in.
8594999Seric **		sendq -- a pointer to the head of the send queue to
8604999Seric **			put this user's aliases in.
861292Seric **
862292Seric **	Returns:
8634098Seric **		none.
864292Seric **
865292Seric **	Side Effects:
8663185Seric **		New names are added to send queues.
867292Seric */
868292Seric 
86955012Seric forward(user, sendq, e)
8702966Seric 	ADDRESS *user;
8714999Seric 	ADDRESS **sendq;
87255012Seric 	register ENVELOPE *e;
873292Seric {
87457136Seric 	char *pp;
87557136Seric 	char *ep;
8764069Seric 
8777671Seric 	if (tTd(27, 1))
8784098Seric 		printf("forward(%s)\n", user->q_paddr);
8794098Seric 
8804594Seric 	if (user->q_mailer != LocalMailer || bitset(QBADADDR, user->q_flags))
8814098Seric 		return;
8824314Seric 	if (user->q_home == NULL)
88358059Seric 	{
88458151Seric 		syserr("554 forward: no home");
88558059Seric 		user->q_home = "/nosuchdirectory";
88658059Seric 	}
8874069Seric 
8884069Seric 	/* good address -- look for .forward file in home */
88955012Seric 	define('z', user->q_home, e);
89057136Seric 	define('u', user->q_user, e);
89157136Seric 	define('h', user->q_host, e);
89257136Seric 	if (ForwardPath == NULL)
89358050Seric 		ForwardPath = newstr("\201z/.forward");
89457136Seric 
89557136Seric 	for (pp = ForwardPath; pp != NULL; pp = ep)
89657136Seric 	{
89758247Seric 		int err;
89857232Seric 		char buf[MAXPATHLEN+1];
89957136Seric 
90057136Seric 		ep = strchr(pp, ':');
90157136Seric 		if (ep != NULL)
90257136Seric 			*ep = '\0';
90357136Seric 		expand(pp, buf, &buf[sizeof buf - 1], e);
90457136Seric 		if (ep != NULL)
90557136Seric 			*ep++ = ':';
90657136Seric 		if (tTd(27, 3))
90757136Seric 			printf("forward: trying %s\n", buf);
90858247Seric 		err = include(buf, TRUE, user, sendq, e);
90958247Seric 		if (err == 0)
91057136Seric 			break;
91158247Seric 		if (transienterror(err))
91258247Seric 		{
91358247Seric 			/* we have to suspend this message */
91458247Seric 			user->q_flags |= QQUEUEUP|QDONTSEND;
91558247Seric 			return;
91658247Seric 		}
91757136Seric 	}
918292Seric }
919