xref: /csrg-svn/usr.sbin/sendmail/src/udb.c (revision 69623)
150581Seric /*
268839Seric  * Copyright (c) 1983, 1995 Eric P. Allman
362532Sbostic  * Copyright (c) 1988, 1993
462532Sbostic  *	The Regents of the University of California.  All rights reserved.
550581Seric  *
650581Seric  * %sccs.include.redist.c%
750581Seric  */
850581Seric 
960567Seric #include "sendmail.h"
1060567Seric 
1150581Seric #ifndef lint
1251360Seric #ifdef USERDB
13*69623Seric static char sccsid [] = "@(#)udb.c	8.21 (Berkeley) 05/23/95 (with USERDB)";
1451360Seric #else
15*69623Seric static char sccsid [] = "@(#)udb.c	8.21 (Berkeley) 05/23/95 (without USERDB)";
1650581Seric #endif
1751360Seric #endif
1850581Seric 
1950581Seric #ifdef USERDB
2050581Seric 
2151923Seric #include <errno.h>
2250581Seric #include <db.h>
2350581Seric 
2466759Seric #ifdef HESIOD
2566759Seric #include <hesiod.h>
2666759Seric #endif /* HESIOD */
2766759Seric 
2850581Seric /*
2953654Seric **  UDB.C -- interface between sendmail and Berkeley User Data Base.
3050581Seric **
3151363Seric **	This depends on the 4.4BSD db package.
3250581Seric */
3350581Seric 
3451362Seric 
3551360Seric struct udbent
3651360Seric {
3751360Seric 	char	*udb_spec;		/* string version of spec */
3851360Seric 	int	udb_type;		/* type of entry */
3951951Seric 	char	*udb_default;		/* default host for outgoing mail */
4051360Seric 	union
4151360Seric 	{
4251360Seric 		/* type UE_REMOTE -- do remote call for lookup */
4351360Seric 		struct
4451360Seric 		{
4551360Seric 			struct sockaddr_in _udb_addr;	/* address */
4651360Seric 			int		_udb_timeout;	/* timeout */
4751360Seric 		} udb_remote;
4851360Seric #define udb_addr	udb_u.udb_remote._udb_addr
4951360Seric #define udb_timeout	udb_u.udb_remote._udb_timeout
5051360Seric 
5151360Seric 		/* type UE_FORWARD -- forward message to remote */
5251360Seric 		struct
5351360Seric 		{
5451360Seric 			char	*_udb_fwdhost;	/* name of forward host */
5551360Seric 		} udb_forward;
5651360Seric #define udb_fwdhost	udb_u.udb_forward._udb_fwdhost
5751360Seric 
5851951Seric 		/* type UE_FETCH -- lookup in local database */
5951360Seric 		struct
6051360Seric 		{
6151360Seric 			char	*_udb_dbname;	/* pathname of database */
6251360Seric 			DB	*_udb_dbp;	/* open database ptr */
6351360Seric 		} udb_lookup;
6451360Seric #define udb_dbname	udb_u.udb_lookup._udb_dbname
6551360Seric #define udb_dbp		udb_u.udb_lookup._udb_dbp
6651360Seric 	} udb_u;
6751360Seric };
6851360Seric 
6951360Seric #define UDB_EOLIST	0	/* end of list */
7051360Seric #define UDB_SKIP	1	/* skip this entry */
7151360Seric #define UDB_REMOTE	2	/* look up in remote database */
7251951Seric #define UDB_DBFETCH	3	/* look up in local database */
7351360Seric #define UDB_FORWARD	4	/* forward to remote host */
7466759Seric #define UDB_HESIOD	5	/* look up via hesiod */
7551360Seric 
7651360Seric #define MAXUDBENT	10	/* maximum number of UDB entries */
7751360Seric 
7851363Seric 
7951363Seric struct option
8051363Seric {
8151363Seric 	char	*name;
8251363Seric 	char	*val;
8351363Seric };
8451363Seric /*
8551363Seric **  UDBEXPAND -- look up user in database and expand
8651363Seric **
8751363Seric **	Parameters:
8851363Seric **		a -- address to expand.
8951363Seric **		sendq -- pointer to head of sendq to put the expansions in.
9067982Seric **		aliaslevel -- the current alias nesting depth.
9167982Seric **		e -- the current envelope.
9251363Seric **
9351363Seric **	Returns:
9451923Seric **		EX_TEMPFAIL -- if something "odd" happened -- probably due
9551923Seric **			to accessing a file on an NFS server that is down.
9651923Seric **		EX_OK -- otherwise.
9751363Seric **
9851363Seric **	Side Effects:
9951363Seric **		Modifies sendq.
10051363Seric */
10151363Seric 
10251363Seric int	UdbPort = 1616;
10351363Seric int	UdbTimeout = 10;
10451363Seric 
10551953Seric struct udbent	UdbEnts[MAXUDBENT + 1];
10651953Seric int		UdbSock = -1;
10751953Seric bool		UdbInitialized = FALSE;
10851360Seric 
10951923Seric int
11067982Seric udbexpand(a, sendq, aliaslevel, e)
11150581Seric 	register ADDRESS *a;
11250581Seric 	ADDRESS **sendq;
11367982Seric 	int aliaslevel;
11455012Seric 	register ENVELOPE *e;
11550581Seric {
11650581Seric 	int i;
11750581Seric 	register char *p;
11850581Seric 	DBT key;
11950581Seric 	DBT info;
12051360Seric 	bool breakout;
12151360Seric 	register struct udbent *up;
12251362Seric 	int keylen;
12358082Seric 	int naddrs;
12457232Seric 	char keybuf[MAXKEY];
12557232Seric 	char buf[BUFSIZ];
12650581Seric 
12750581Seric 	if (tTd(28, 1))
12858065Seric 		printf("udbexpand(%s)\n", a->q_paddr);
12950581Seric 
13050581Seric 	/* make certain we are supposed to send to this address */
13158154Seric 	if (bitset(QDONTSEND|QVERIFIED, a->q_flags))
13251923Seric 		return EX_OK;
13355012Seric 	e->e_to = a->q_paddr;
13450581Seric 
13551360Seric 	/* on first call, locate the database */
13651951Seric 	if (!UdbInitialized)
13750581Seric 	{
13851923Seric 		extern int _udbx_init();
13951362Seric 
14051923Seric 		if (_udbx_init() == EX_TEMPFAIL)
14151923Seric 			return EX_TEMPFAIL;
14251362Seric 	}
14350581Seric 
14451909Seric 	/* short circuit the process if no chance of a match */
14551909Seric 	if (UdbSpec == NULL || UdbSpec[0] == '\0')
14651923Seric 		return EX_OK;
14751909Seric 
14866759Seric 	/* short circuit name begins with '\\' since it can't possibly match */
14966759Seric 	if (a->q_user[0] == '\\')
15066759Seric 		return EX_OK;
15166759Seric 
15251362Seric 	/* if name is too long, assume it won't match */
15351362Seric 	if (strlen(a->q_user) > sizeof keybuf - 12)
15451923Seric 		return EX_OK;
15551360Seric 
15651362Seric 	/* if name begins with a colon, it indicates our metadata */
15751362Seric 	if (a->q_user[0] == ':')
15851923Seric 		return EX_OK;
15951360Seric 
16051362Seric 	/* build actual database key */
16151362Seric 	(void) strcpy(keybuf, a->q_user);
16251362Seric 	(void) strcat(keybuf, ":maildrop");
16351362Seric 	keylen = strlen(keybuf);
16451360Seric 
16551360Seric 	breakout = FALSE;
16651362Seric 	for (up = UdbEnts; !breakout; up++)
16750581Seric 	{
16851360Seric 		char *user;
16950581Seric 
17051360Seric 		/*
17151360Seric 		**  Select action based on entry type.
17251360Seric 		**
17351360Seric 		**	On dropping out of this switch, "class" should
17451360Seric 		**	explain the type of the data, and "user" should
17551360Seric 		**	contain the user information.
17651360Seric 		*/
17750581Seric 
17851360Seric 		switch (up->udb_type)
17951360Seric 		{
18068786Seric #ifdef NEWDB
18151951Seric 		  case UDB_DBFETCH:
18251362Seric 			key.data = keybuf;
18351362Seric 			key.size = keylen;
18460990Seric 			if (tTd(28, 80))
18566759Seric 				printf("udbexpand: trying %s (%d) via db\n",
18664067Seric 					keybuf, keylen);
18751362Seric 			i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_CURSOR);
18851923Seric 			if (i > 0 || info.size <= 0)
18951360Seric 			{
19051360Seric 				if (tTd(28, 2))
19164067Seric 					printf("udbexpand: no match on %s (%d)\n",
19264067Seric 						keybuf, keylen);
19351360Seric 				continue;
19451360Seric 			}
19560990Seric 			if (tTd(28, 80))
19660990Seric 				printf("udbexpand: match %.*s: %.*s\n",
19760990Seric 					key.size, key.data, info.size, info.data);
19850581Seric 
19958082Seric 			naddrs = 0;
20058082Seric 			a->q_flags &= ~QSELFREF;
20151830Seric 			while (i == 0 && key.size == keylen &&
20251830Seric 					bcmp(key.data, keybuf, keylen) == 0)
20351362Seric 			{
20458099Seric 				if (bitset(EF_VRFYONLY, e->e_flags))
20558154Seric 				{
20658154Seric 					a->q_flags |= QVERIFIED;
20758884Seric 					e->e_nrcpts++;
20858099Seric 					return EX_OK;
20958154Seric 				}
21058099Seric 
21151830Seric 				breakout = TRUE;
21251362Seric 				if (info.size < sizeof buf)
21351362Seric 					user = buf;
21451362Seric 				else
21551362Seric 					user = xalloc(info.size + 1);
21651362Seric 				bcopy(info.data, user, info.size);
21751362Seric 				user[info.size] = '\0';
21850581Seric 
21958151Seric 				message("expanded to %s", user);
22057977Seric #ifdef LOG
22157977Seric 				if (LogLevel >= 10)
22257977Seric 					syslog(LOG_INFO, "%s: expand %s => %s",
22357977Seric 						e->e_id, e->e_to, user);
22457977Seric #endif
22567982Seric 				naddrs += sendtolist(user, a, sendq, aliaslevel + 1, e);
22651362Seric 
22751362Seric 				if (user != buf)
22851362Seric 					free(user);
22951362Seric 
23051362Seric 				/* get the next record */
23151362Seric 				i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_NEXT);
23251830Seric 			}
23360990Seric 
23460990Seric 			/* if nothing ever matched, try next database */
23560990Seric 			if (!breakout)
23660990Seric 				continue;
23760990Seric 
23858082Seric 			if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
23958065Seric 			{
24058065Seric 				if (tTd(28, 5))
24158065Seric 				{
24258065Seric 					printf("udbexpand: QDONTSEND ");
24358065Seric 					printaddr(a, FALSE);
24458065Seric 				}
24558065Seric 				a->q_flags |= QDONTSEND;
24658065Seric 			}
24751923Seric 			if (i < 0)
24851923Seric 			{
24958010Seric 				syserr("udbexpand: db-get %.*s stat %d",
25058010Seric 					key.size, key.data, i);
25151923Seric 				return EX_TEMPFAIL;
25251923Seric 			}
25359707Seric 
25459707Seric 			/*
25559707Seric 			**  If this address has a -request address, reflect
25659707Seric 			**  it into the envelope.
25759707Seric 			*/
25859707Seric 
25959707Seric 			(void) strcpy(keybuf, a->q_user);
26059707Seric 			(void) strcat(keybuf, ":mailsender");
26159707Seric 			keylen = strlen(keybuf);
26259707Seric 			key.data = keybuf;
26359707Seric 			key.size = keylen;
26459707Seric 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
26559707Seric 			if (i != 0 || info.size <= 0)
26659707Seric 				break;
26759707Seric 			a->q_owner = xalloc(info.size + 1);
26859707Seric 			bcopy(info.data, a->q_owner, info.size);
26959707Seric 			a->q_owner[info.size] = '\0';
27066784Seric 
27166784Seric 			/* announce delivery; NORECEIPT bit set later */
27266784Seric 			if (e->e_xfp != NULL)
27366784Seric 			{
27466784Seric 				fprintf(e->e_xfp,
27566784Seric 					"Message delivered to mailing list %s\n",
27666784Seric 					a->q_paddr);
27766784Seric 			}
27868603Seric 			e->e_flags |= EF_SENDRECEIPT;
27968868Seric 			a->q_flags |= QDELIVERED|QEXPANDED;
28051360Seric 			break;
28168786Seric #endif
28251360Seric 
28366759Seric #ifdef HESIOD
28466759Seric 		  case UDB_HESIOD:
28566759Seric 			key.data = keybuf;
28666759Seric 			key.size = keylen;
28766759Seric 			if (tTd(28, 80))
28866759Seric 				printf("udbexpand: trying %s (%d) via hesiod\n",
28966759Seric 					keybuf, keylen);
29066759Seric 			/* look up the key via hesiod */
29166759Seric 			i = hes_udb_get(&key, &info);
292*69623Seric 			if (i < 0)
29366759Seric 			{
294*69623Seric 				syserr("udbexpand: hesiod-get %.*s stat %d",
295*69623Seric 					key.size, key.data, i);
296*69623Seric 				return EX_TEMPFAIL;
297*69623Seric 			}
298*69623Seric 			else if (i > 0 || info.size <= 0)
299*69623Seric 			{
30066759Seric 				if (tTd(28, 2))
30166759Seric 				printf("udbexpand: no match on %s (%d)\n",
30266759Seric 					keybuf, keylen);
30366759Seric 				continue;
30466759Seric 			}
30566759Seric 			if (tTd(28, 80))
30666759Seric 				printf("udbexpand: match %.*s: %.*s\n",
30766759Seric 					key.size, key.data, info.size, info.data);
30866759Seric 			a->q_flags &= ~QSELFREF;
30966759Seric 
31066759Seric 			if (bitset(EF_VRFYONLY, e->e_flags))
31166759Seric 			{
31266759Seric 				a->q_flags |= QVERIFIED;
31366759Seric 				e->e_nrcpts++;
31466759Seric 				return EX_OK;
31566759Seric 			}
31666759Seric 
31766759Seric 			breakout = TRUE;
31866759Seric 			if (info.size < sizeof buf)
31966759Seric 				user = buf;
32066759Seric 			else
32166759Seric 				user = xalloc(info.size + 1);
32266759Seric 			bcopy(info.data, user, info.size);
32366759Seric 			user[info.size] = '\0';
32466759Seric 
32566759Seric 			message("hesioded to %s", user);
32666759Seric #ifdef LOG
32766759Seric 			if (LogLevel >= 10)
32866759Seric 				syslog(LOG_INFO, "%s: hesiod %s => %s",
32966759Seric 					e->e_id, e->e_to, user);
33066759Seric #endif
33167982Seric 			naddrs = sendtolist(user, a, sendq, aliaslevel + 1, e);
33266759Seric 
33366759Seric 			if (user != buf)
33466759Seric 				free(user);
33566759Seric 
33666759Seric 			if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
33766759Seric 			{
33866759Seric 				if (tTd(28, 5))
33966759Seric 				{
34066759Seric 					printf("udbexpand: QDONTSEND ");
34166759Seric 					printaddr(a, FALSE);
34266759Seric 				}
34366759Seric 				a->q_flags |= QDONTSEND;
34466759Seric 			}
34566759Seric 
34666759Seric 			/*
34766759Seric 			**  If this address has a -request address, reflect
34866759Seric 			**  it into the envelope.
34966759Seric 			*/
35066759Seric 
35166759Seric 			(void) strcpy(keybuf, a->q_user);
35266759Seric 			(void) strcat(keybuf, ":mailsender");
35366759Seric 			keylen = strlen(keybuf);
35466759Seric 			key.data = keybuf;
35566759Seric 			key.size = keylen;
35666759Seric 			i = hes_udb_get(&key, &info);
35766759Seric 			if (i != 0 || info.size <= 0)
35866759Seric 				break;
35966759Seric 			a->q_owner = xalloc(info.size + 1);
36066759Seric 			bcopy(info.data, a->q_owner, info.size);
36166759Seric 			a->q_owner[info.size] = '\0';
36266759Seric 			break;
36366759Seric #endif /* HESIOD */
36466759Seric 
36551360Seric 		  case UDB_REMOTE:
36651741Seric 			/* not yet implemented */
36751741Seric 			continue;
36851362Seric 
36951360Seric 		  case UDB_FORWARD:
37058099Seric 			if (bitset(EF_VRFYONLY, e->e_flags))
37158099Seric 				return EX_OK;
37251360Seric 			i = strlen(up->udb_fwdhost) + strlen(a->q_user) + 1;
37351360Seric 			if (i < sizeof buf)
37451360Seric 				user = buf;
37551360Seric 			else
37651360Seric 				user = xalloc(i + 1);
37751360Seric 			(void) sprintf(user, "%s@%s", a->q_user, up->udb_fwdhost);
37858151Seric 			message("expanded to %s", user);
37958082Seric 			a->q_flags &= ~QSELFREF;
38067982Seric 			naddrs = sendtolist(user, a, sendq, aliaslevel + 1, e);
38158082Seric 			if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
38258065Seric 			{
38358065Seric 				if (tTd(28, 5))
38458065Seric 				{
38558065Seric 					printf("udbexpand: QDONTSEND ");
38658065Seric 					printaddr(a, FALSE);
38758065Seric 				}
38858065Seric 				a->q_flags |= QDONTSEND;
38958065Seric 			}
39051362Seric 			if (user != buf)
39151362Seric 				free(user);
39251362Seric 			breakout = TRUE;
39351360Seric 			break;
39451360Seric 
39551360Seric 		  case UDB_EOLIST:
39651360Seric 			breakout = TRUE;
39751360Seric 			continue;
39851360Seric 
39951360Seric 		  default:
40051360Seric 			/* unknown entry type */
40151360Seric 			continue;
40251360Seric 		}
40351362Seric 	}
40451923Seric 	return EX_OK;
40551362Seric }
40651951Seric /*
40751951Seric **  UDBSENDER -- return canonical external name of sender, given local name
40851951Seric **
40951951Seric **	Parameters:
41051951Seric **		sender -- the name of the sender on the local machine.
41151951Seric **
41251951Seric **	Returns:
41351951Seric **		The external name for this sender, if derivable from the
41451951Seric **			database.
41551951Seric **		NULL -- if nothing is changed from the database.
41651951Seric **
41751951Seric **	Side Effects:
41851951Seric **		none.
41951951Seric */
42051360Seric 
42151951Seric char *
42251951Seric udbsender(sender)
42351951Seric 	char *sender;
42451951Seric {
42564350Seric 	extern char *udbmatch();
42664350Seric 
42764350Seric 	return udbmatch(sender, "mailname");
42864350Seric }
42964350Seric 
43064350Seric 
43164350Seric char *
43264350Seric udbmatch(user, field)
43364350Seric 	char *user;
43464350Seric 	char *field;
43564350Seric {
43651951Seric 	register char *p;
43751951Seric 	register struct udbent *up;
43851951Seric 	int i;
43951951Seric 	int keylen;
44051951Seric 	DBT key, info;
44157232Seric 	char keybuf[MAXKEY];
44251951Seric 
44351951Seric 	if (tTd(28, 1))
44464350Seric 		printf("udbmatch(%s, %s)\n", user, field);
44551951Seric 
44651951Seric 	if (!UdbInitialized)
44751951Seric 	{
44851951Seric 		if (_udbx_init() == EX_TEMPFAIL)
44951951Seric 			return NULL;
45051951Seric 	}
45151951Seric 
45251951Seric 	/* short circuit if no spec */
45351951Seric 	if (UdbSpec == NULL || UdbSpec[0] == '\0')
45451951Seric 		return NULL;
45551951Seric 
45666759Seric 	/* short circuit name begins with '\\' since it can't possibly match */
45766759Seric 	if (user[0] == '\\')
45866759Seric 		return NULL;
45966759Seric 
46051951Seric 	/* long names can never match and are a pain to deal with */
46164350Seric 	if ((strlen(user) + strlen(field)) > sizeof keybuf - 4)
46251951Seric 		return NULL;
46351951Seric 
46451951Seric 	/* names beginning with colons indicate metadata */
46564350Seric 	if (user[0] == ':')
46651951Seric 		return NULL;
46751951Seric 
46851951Seric 	/* build database key */
46964350Seric 	(void) strcpy(keybuf, user);
47064350Seric 	(void) strcat(keybuf, ":");
47164350Seric 	(void) strcat(keybuf, field);
47251951Seric 	keylen = strlen(keybuf);
47351951Seric 
47451951Seric 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
47551951Seric 	{
47651951Seric 		/*
47751951Seric 		**  Select action based on entry type.
47851951Seric 		*/
47951951Seric 
48051951Seric 		switch (up->udb_type)
48151951Seric 		{
48268786Seric #ifdef NEWDB
48351951Seric 		  case UDB_DBFETCH:
48451951Seric 			key.data = keybuf;
48551951Seric 			key.size = keylen;
48651951Seric 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
48751951Seric 			if (i != 0 || info.size <= 0)
48851951Seric 			{
48951951Seric 				if (tTd(28, 2))
49066759Seric 					printf("udbmatch: no match on %s (%d) via db\n",
49164067Seric 							keybuf, keylen);
49251951Seric 				continue;
49351951Seric 			}
49451951Seric 
49551951Seric 			p = xalloc(info.size + 1);
49651951Seric 			bcopy(info.data, p, info.size);
49751951Seric 			p[info.size] = '\0';
49851951Seric 			if (tTd(28, 1))
49964350Seric 				printf("udbmatch ==> %s\n", p);
50051951Seric 			return p;
50166759Seric 			break;
50268786Seric #endif
50366759Seric 
50466759Seric #ifdef HESIOD
50566759Seric 		  case UDB_HESIOD:
50666759Seric 			key.data = keybuf;
50766759Seric 			key.size = keylen;
50866759Seric 			i = hes_udb_get(&key, &info);
50966759Seric 			if (i != 0 || info.size <= 0)
51066759Seric 			{
51166759Seric 				if (tTd(28, 2))
51266759Seric 					printf("udbmatch: no match on %s (%d) via hesiod\n",
51366759Seric 							keybuf, keylen);
51466759Seric 				continue;
51566759Seric 			}
51666759Seric 
51766759Seric 			p = xalloc(info.size + 1);
51866759Seric 			bcopy(info.data, p, info.size);
51966759Seric 			p[info.size] = '\0';
52066759Seric 			if (tTd(28, 1))
52166759Seric 				printf("udbmatch ==> %s\n", p);
52266759Seric 			return p;
52366759Seric #endif /* HESIOD */
52451951Seric 		}
52551951Seric 	}
52651951Seric 
52764350Seric 	if (strcmp(field, "mailname") != 0)
52864350Seric 		return NULL;
52964350Seric 
53051951Seric 	/*
53151951Seric 	**  Nothing yet.  Search again for a default case.  But only
53251951Seric 	**  use it if we also have a forward (:maildrop) pointer already
53351951Seric 	**  in the database.
53451951Seric 	*/
53551951Seric 
53651951Seric 	/* build database key */
53764350Seric 	(void) strcpy(keybuf, user);
53851951Seric 	(void) strcat(keybuf, ":maildrop");
53951951Seric 	keylen = strlen(keybuf);
54051951Seric 
54151951Seric 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
54251951Seric 	{
54351951Seric 		switch (up->udb_type)
54451951Seric 		{
54568786Seric #ifdef NEWDB
54651951Seric 		  case UDB_DBFETCH:
54751951Seric 			/* get the default case for this database */
54851951Seric 			if (up->udb_default == NULL)
54951951Seric 			{
55051951Seric 				key.data = ":default:mailname";
55151951Seric 				key.size = strlen(key.data);
55251951Seric 				i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
55351951Seric 				if (i != 0 || info.size <= 0)
55451951Seric 				{
55551951Seric 					/* no default case */
55651951Seric 					up->udb_default = "";
55751951Seric 					continue;
55851951Seric 				}
55951951Seric 
56051951Seric 				/* save the default case */
56151951Seric 				up->udb_default = xalloc(info.size + 1);
56251951Seric 				bcopy(info.data, up->udb_default, info.size);
56351951Seric 				up->udb_default[info.size] = '\0';
56451951Seric 			}
56551951Seric 			else if (up->udb_default[0] == '\0')
56651951Seric 				continue;
56751951Seric 
56851951Seric 			/* we have a default case -- verify user:maildrop */
56951951Seric 			key.data = keybuf;
57051951Seric 			key.size = keylen;
57151951Seric 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
57251951Seric 			if (i != 0 || info.size <= 0)
57351951Seric 			{
57451951Seric 				/* nope -- no aliasing for this user */
57551951Seric 				continue;
57651951Seric 			}
57751951Seric 
57851951Seric 			/* they exist -- build the actual address */
57964350Seric 			p = xalloc(strlen(user) + strlen(up->udb_default) + 2);
58064350Seric 			(void) strcpy(p, user);
58151951Seric 			(void) strcat(p, "@");
58251951Seric 			(void) strcat(p, up->udb_default);
58351951Seric 			if (tTd(28, 1))
58464350Seric 				printf("udbmatch ==> %s\n", p);
58551951Seric 			return p;
58666759Seric 			break;
58768786Seric #endif
58866759Seric 
58966759Seric #ifdef HESIOD
59066759Seric 		  case UDB_HESIOD:
59166759Seric 			/* get the default case for this database */
59266759Seric 			if (up->udb_default == NULL)
59366759Seric 			{
59466759Seric 				key.data = ":default:mailname";
59566759Seric 				key.size = strlen(key.data);
59666759Seric 				i = hes_udb_get(&key, &info);
59766759Seric 
59866759Seric 				if (i != 0 || info.size <= 0)
59966759Seric 				{
60066759Seric 					/* no default case */
60166759Seric 					up->udb_default = "";
60266759Seric 					continue;
60366759Seric 				}
60466759Seric 
60566759Seric 				/* save the default case */
60666759Seric 				up->udb_default = xalloc(info.size + 1);
60766759Seric 				bcopy(info.data, up->udb_default, info.size);
60866759Seric 				up->udb_default[info.size] = '\0';
60966759Seric 			}
61066759Seric 			else if (up->udb_default[0] == '\0')
61166759Seric 				continue;
61266759Seric 
61366759Seric 			/* we have a default case -- verify user:maildrop */
61466759Seric 			key.data = keybuf;
61566759Seric 			key.size = keylen;
61666759Seric 			i = hes_udb_get(&key, &info);
61766759Seric 			if (i != 0 || info.size <= 0)
61866759Seric 			{
61966759Seric 				/* nope -- no aliasing for this user */
62066759Seric 				continue;
62166759Seric 			}
62266759Seric 
62366759Seric 			/* they exist -- build the actual address */
62466759Seric 			p = xalloc(strlen(user) + strlen(up->udb_default) + 2);
62566759Seric 			(void) strcpy(p, user);
62666759Seric 			(void) strcat(p, "@");
62766759Seric 			(void) strcat(p, up->udb_default);
62866759Seric 			if (tTd(28, 1))
62966759Seric 				printf("udbmatch ==> %s\n", p);
63066759Seric 			return p;
63166759Seric 			break;
63266759Seric #endif /* HESIOD */
63351951Seric 		}
63451951Seric 	}
63551951Seric 
63651951Seric 	/* still nothing....  too bad */
63751951Seric 	return NULL;
63851951Seric }
63951951Seric /*
64068521Seric **  UDB_MAP_LOOKUP -- look up arbitrary entry in user database map
64168521Seric **
64268521Seric **	Parameters:
64368521Seric **		map -- the map being queried.
64468521Seric **		name -- the name to look up.
64568521Seric **		av -- arguments to the map lookup.
64668521Seric **		statp -- to get any error status.
64768521Seric **
64868521Seric **	Returns:
64968521Seric **		NULL if name not found in map.
65068521Seric **		The rewritten name otherwise.
65168521Seric */
65268521Seric 
65368521Seric char *
65468521Seric udb_map_lookup(map, name, av, statp)
65568521Seric 	MAP *map;
65668521Seric 	char *name;
65768521Seric 	char **av;
65868521Seric 	int *statp;
65968521Seric {
66068521Seric 	char *val;
66168521Seric 
66268521Seric 	if (tTd(38, 20))
66368521Seric 		printf("udb_map_lookup(%s, %s)\n", map->map_mname, name);
66468521Seric 	val = udbmatch(name, map->map_file);
66568521Seric 	if (val == NULL)
66668521Seric 		return NULL;
66768521Seric 	if (bitset(MF_MATCHONLY, map->map_mflags))
66868521Seric 		return map_rewrite(map, name, strlen(name), NULL);
66968521Seric 	else
67068521Seric 		return map_rewrite(map, val, strlen(val), av);
67168521Seric }
67268521Seric /*
67351951Seric **  _UDBX_INIT -- parse the UDB specification, opening any valid entries.
67451951Seric **
67551951Seric **	Parameters:
67651951Seric **		none.
67751951Seric **
67851951Seric **	Returns:
67951951Seric **		EX_TEMPFAIL -- if it appeared it couldn't get hold of a
68051951Seric **			database due to a host being down or some similar
68151951Seric **			(recoverable) situation.
68251951Seric **		EX_OK -- otherwise.
68351951Seric **
68451951Seric **	Side Effects:
68551951Seric **		Fills in the UdbEnts structure from UdbSpec.
68651951Seric */
68751951Seric 
68851363Seric #define MAXUDBOPTS	27
68951363Seric 
69051953Seric int
69151362Seric _udbx_init()
69251362Seric {
69351362Seric 	register char *p;
69451362Seric 	int i;
69551362Seric 	register struct udbent *up;
69657232Seric 	char buf[BUFSIZ];
69751360Seric 
69851951Seric 	if (UdbInitialized)
69951951Seric 		return EX_OK;
70051951Seric 
70151908Seric # ifdef UDB_DEFAULT_SPEC
70251908Seric 	if (UdbSpec == NULL)
70351908Seric 		UdbSpec = UDB_DEFAULT_SPEC;
70451908Seric # endif
70551908Seric 
70651362Seric 	p = UdbSpec;
70751362Seric 	up = UdbEnts;
70851762Seric 	while (p != NULL)
70951362Seric 	{
71051362Seric 		char *spec;
71151362Seric 		auto int rcode;
71251363Seric 		int nopts;
71351362Seric 		int nmx;
71451362Seric 		register struct hostent *h;
71551362Seric 		char *mxhosts[MAXMXHOSTS + 1];
71651363Seric 		struct option opts[MAXUDBOPTS + 1];
71751362Seric 
71851362Seric 		while (*p == ' ' || *p == '\t' || *p == ',')
71951362Seric 			p++;
72051362Seric 		if (*p == '\0')
72151362Seric 			break;
72251362Seric 		spec = p;
72356795Seric 		p = strchr(p, ',');
72451761Seric 		if (p != NULL)
72551362Seric 			*p++ = '\0';
72651363Seric 
72751363Seric 		/* extract options */
72851363Seric 		nopts = _udb_parsespec(spec, opts, MAXUDBOPTS);
72951363Seric 
73051363Seric 		/*
73151363Seric 		**  Decode database specification.
73251363Seric 		**
73351363Seric 		**	In the sendmail tradition, the leading character
73451363Seric 		**	defines the semantics of the rest of the entry.
73551363Seric 		**
73651363Seric 		**	+hostname --	send a datagram to the udb server
73751363Seric 		**			on host "hostname" asking for the
73851363Seric 		**			home mail server for this user.
73951363Seric 		**	*hostname --	similar to +hostname, except that the
74051363Seric 		**			hostname is searched as an MX record;
74151363Seric 		**			resulting hosts are searched as for
74251363Seric 		**			+mxhostname.  If no MX host is found,
74351363Seric 		**			this is the same as +hostname.
74451363Seric 		**	@hostname --	forward email to the indicated host.
74551363Seric 		**			This should be the last in the list,
74651363Seric 		**			since it always matches the input.
74751363Seric 		**	/dbname	 --	search the named database on the local
74851363Seric 		**			host using the Berkeley db package.
74951363Seric 		*/
75051363Seric 
75151362Seric 		switch (*spec)
75251360Seric 		{
75368786Seric #if 0
75451362Seric 		  case '+':	/* search remote database */
75551363Seric 		  case '*':	/* search remote database (expand MX) */
75651363Seric 			if (*spec == '*')
75751363Seric 			{
75866334Seric #if NAMED_BIND
75959273Seric 				nmx = getmxrr(spec + 1, mxhosts, FALSE, &rcode);
76057629Seric #else
76157629Seric 				mxhosts[0] = spec + 1;
76257629Seric 				nmx = 1;
76357629Seric 				rcode = 0;
76457629Seric #endif
76551363Seric 				if (tTd(28, 16))
76651363Seric 				{
76751363Seric 					int i;
76851362Seric 
76951363Seric 					printf("getmxrr(%s): %d", spec + 1, nmx);
77051363Seric 					for (i = 0; i <= nmx; i++)
77151363Seric 						printf(" %s", mxhosts[i]);
77251363Seric 					printf("\n");
77351363Seric 				}
77451363Seric 			}
77551363Seric 			else
77651362Seric 			{
77751363Seric 				nmx = 1;
77851363Seric 				mxhosts[0] = spec + 1;
77951362Seric 			}
78051362Seric 
78151362Seric 			for (i = 0; i < nmx; i++)
78251362Seric 			{
78368693Seric 				h = sm_gethostbyname(mxhosts[i]);
78451362Seric 				if (h == NULL)
78551362Seric 					continue;
78651362Seric 				up->udb_type = UDB_REMOTE;
78751362Seric 				up->udb_addr.sin_family = h->h_addrtype;
78851362Seric 				bcopy(h->h_addr_list[0],
78951362Seric 				      (char *) &up->udb_addr.sin_addr,
79067421Seric 				      INADDRSZ);
79151362Seric 				up->udb_addr.sin_port = UdbPort;
79251362Seric 				up->udb_timeout = UdbTimeout;
79351362Seric 				up++;
79451362Seric 			}
79551362Seric 
79651362Seric 			/* set up a datagram socket */
79751362Seric 			if (UdbSock < 0)
79851362Seric 			{
79951362Seric 				UdbSock = socket(AF_INET, SOCK_DGRAM, 0);
80051362Seric 				(void) fcntl(UdbSock, F_SETFD, 1);
80151362Seric 			}
80251362Seric 			break;
80368786Seric #endif
80451362Seric 
80551362Seric 		  case '@':	/* forward to remote host */
80651362Seric 			up->udb_type = UDB_FORWARD;
80751362Seric 			up->udb_fwdhost = spec + 1;
80851362Seric 			up++;
80951362Seric 			break;
81051362Seric 
81168786Seric #ifdef HESIOD
81266759Seric 		  case 'h':	/* use hesiod */
81366759Seric 		  case 'H':
81466759Seric 			if (strcasecmp(spec, "hesiod") != 0)
81568786Seric 				goto badspec;
81666759Seric 			up->udb_type = UDB_HESIOD;
81766759Seric 			up++;
81868786Seric 			break;
81966759Seric #endif /* HESIOD */
82066759Seric 
82168786Seric #ifdef NEWDB
82251362Seric 		  case '/':	/* look up remote name */
82351831Seric 			up->udb_dbname = spec;
82451923Seric 			errno = 0;
82551362Seric 			up->udb_dbp = dbopen(spec, O_RDONLY, 0644, DB_BTREE, NULL);
82651362Seric 			if (up->udb_dbp == NULL)
82751923Seric 			{
82867763Seric 				if (tTd(28, 1))
82967763Seric 				{
83067763Seric 					int saveerrno = errno;
83167763Seric 
83267763Seric 					printf("dbopen(%s): %s",
83367763Seric 						spec, errstring(errno));
83467763Seric 					errno = saveerrno;
83567763Seric 				}
83651923Seric 				if (errno != ENOENT && errno != EACCES)
83751951Seric 				{
83859615Seric #ifdef LOG
83959615Seric 					if (LogLevel > 2)
84059625Seric 						syslog(LOG_ERR, "dbopen(%s): %s",
84159625Seric 							spec, errstring(errno));
84259615Seric #endif
84351951Seric 					up->udb_type = UDB_EOLIST;
84451951Seric 					goto tempfail;
84551951Seric 				}
84651362Seric 				break;
84751923Seric 			}
84851951Seric 			up->udb_type = UDB_DBFETCH;
84951362Seric 			up++;
85051362Seric 			break;
85168786Seric #endif
85268786Seric 
85368786Seric 		  default:
85468786Seric badspec:
85568786Seric 			syserr("Unknown UDB spec %s", spec);
85668786Seric 			break;
85751360Seric 		}
85851362Seric 	}
85951362Seric 	up->udb_type = UDB_EOLIST;
86051360Seric 
86151362Seric 	if (tTd(28, 4))
86251362Seric 	{
86351951Seric 		for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
86451360Seric 		{
86551362Seric 			switch (up->udb_type)
86651362Seric 			{
86751362Seric 			  case UDB_REMOTE:
86851362Seric 				printf("REMOTE: addr %s, timeo %d\n",
86960494Seric 					anynet_ntoa((SOCKADDR *) &up->udb_addr),
87051362Seric 					up->udb_timeout);
87151362Seric 				break;
87251362Seric 
87351951Seric 			  case UDB_DBFETCH:
87451951Seric 				printf("FETCH: file %s\n",
87551830Seric 					up->udb_dbname);
87651362Seric 				break;
87751362Seric 
87851362Seric 			  case UDB_FORWARD:
87951362Seric 				printf("FORWARD: host %s\n",
88051362Seric 					up->udb_fwdhost);
88151362Seric 				break;
88251362Seric 
88366759Seric 			  case UDB_HESIOD:
88466759Seric 				printf("HESIOD\n");
88566759Seric 				break;
88666759Seric 
88751362Seric 			  default:
88851362Seric 				printf("UNKNOWN\n");
88951362Seric 				break;
89051362Seric 			}
89151360Seric 		}
89250581Seric 	}
89351951Seric 
89451951Seric 	UdbInitialized = TRUE;
89551955Seric 	errno = 0;
89651951Seric 	return EX_OK;
89751951Seric 
89851951Seric 	/*
89951951Seric 	**  On temporary failure, back out anything we've already done
90051951Seric 	*/
90151951Seric 
90251951Seric   tempfail:
90368786Seric #ifdef NEWDB
90451951Seric 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
90551951Seric 	{
90651951Seric 		if (up->udb_type == UDB_DBFETCH)
90751951Seric 		{
90851951Seric 			(*up->udb_dbp->close)(up->udb_dbp);
90951951Seric 		}
91051951Seric 	}
91168786Seric #endif
91251951Seric 	return EX_TEMPFAIL;
91351360Seric }
91450581Seric 
91551363Seric int
91651363Seric _udb_parsespec(udbspec, opt, maxopts)
91751363Seric 	char *udbspec;
91851363Seric 	struct option opt[];
91951363Seric 	int maxopts;
92051363Seric {
92151363Seric 	register char *spec;
92251363Seric 	register char *spec_end;
92351363Seric 	register int optnum;
92451363Seric 
92556795Seric 	spec_end = strchr(udbspec, ':');
92651363Seric 	for (optnum = 0; optnum < maxopts && (spec = spec_end) != NULL; optnum++)
92751363Seric 	{
92851363Seric 		register char *p;
92951363Seric 
93058050Seric 		while (isascii(*spec) && isspace(*spec))
93151363Seric 			spec++;
93256795Seric 		spec_end = strchr(spec, ':');
93351363Seric 		if (spec_end != NULL)
93451363Seric 			*spec_end++ = '\0';
93551363Seric 
93651363Seric 		opt[optnum].name = spec;
93751363Seric 		opt[optnum].val = NULL;
93856795Seric 		p = strchr(spec, '=');
93951363Seric 		if (p != NULL)
94051363Seric 			opt[optnum].val = ++p;
94151363Seric 	}
94251363Seric 	return optnum;
94351363Seric }
94451363Seric 
94566759Seric #ifdef HESIOD
94666759Seric 
94766759Seric int
94866759Seric hes_udb_get(key, info)
94966759Seric 	DBT *key;
95066759Seric 	DBT *info;
95166759Seric {
95266759Seric 	char *name, *type;
95366759Seric 	char *p, **hp;
95468533Seric 	char kbuf[MAXKEY + 1];
95566759Seric 
95668533Seric 	strcpy(kbuf, key->data);
95768533Seric 	name = kbuf;
95866759Seric 	type = strchr(name, ':');
95968544Seric 	if (type == NULL)
96066759Seric 		return 1;
96166759Seric 	*type++ = '\0';
96266759Seric 
96366759Seric 	if (tTd(28, 1))
96466759Seric 		printf("hes_udb_get(%s, %s)\n", name, type);
96566759Seric 
96666759Seric 	/* make the hesiod query */
96766759Seric 	hp = hes_resolve(name, type);
96866759Seric 	if (hp == NULL)
96966759Seric 	{
97066759Seric 		/* network problem or timeout */
97166759Seric 		if (hes_error() == HES_ER_NET)
97266759Seric 			return -1;
97366759Seric 
97466759Seric 		return 1;
97566759Seric 	}
97666759Seric 	else
97766759Seric 	{
97866759Seric 		/*
97966759Seric 		**  If there are multiple matches, just return the
980*69623Seric 		**  first one.
98166759Seric 		**
98266759Seric 		**  XXX These should really be returned; for example,
98366759Seric 		**  XXX it is legal for :maildrop to be multi-valued.
98466759Seric 		*/
98566759Seric 
98666759Seric 		info->data = hp[0];
98766759Seric 		info->size = (size_t) strlen(info->data);
98866759Seric 	}
98966759Seric 
99066759Seric 	return 0;
99166759Seric }
99266759Seric #endif /* HESIOD */
99366759Seric 
99451360Seric #else /* not USERDB */
99551360Seric 
99651923Seric int
99767982Seric udbexpand(a, sendq, aliaslevel, e)
99851360Seric 	ADDRESS *a;
99951360Seric 	ADDRESS **sendq;
100067982Seric 	int aliaslevel;
100155012Seric 	ENVELOPE *e;
100251360Seric {
100351923Seric 	return EX_OK;
100450581Seric }
100550581Seric 
100650581Seric #endif /* USERDB */
1007