xref: /csrg-svn/usr.sbin/sendmail/src/udb.c (revision 68603)
150581Seric /*
250581Seric  * Copyright (c) 1983 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*68603Seric static char sccsid [] = "@(#)udb.c	8.16 (Berkeley) 03/27/95 (with USERDB)";
1451360Seric #else
15*68603Seric static char sccsid [] = "@(#)udb.c	8.16 (Berkeley) 03/27/95 (without USERDB)";
1650581Seric #endif
1751360Seric #endif
1850581Seric 
1950581Seric #ifdef USERDB
2050581Seric 
2151923Seric #include <errno.h>
2251360Seric #include <netdb.h>
2350581Seric #include <db.h>
2450581Seric 
2566759Seric #ifdef HESIOD
2666759Seric #include <hesiod.h>
2766759Seric #endif /* HESIOD */
2866759Seric 
2950581Seric /*
3053654Seric **  UDB.C -- interface between sendmail and Berkeley User Data Base.
3150581Seric **
3251363Seric **	This depends on the 4.4BSD db package.
3350581Seric */
3450581Seric 
3551362Seric 
3651360Seric struct udbent
3751360Seric {
3851360Seric 	char	*udb_spec;		/* string version of spec */
3951360Seric 	int	udb_type;		/* type of entry */
4051951Seric 	char	*udb_default;		/* default host for outgoing mail */
4151360Seric 	union
4251360Seric 	{
4351360Seric 		/* type UE_REMOTE -- do remote call for lookup */
4451360Seric 		struct
4551360Seric 		{
4651360Seric 			struct sockaddr_in _udb_addr;	/* address */
4751360Seric 			int		_udb_timeout;	/* timeout */
4851360Seric 		} udb_remote;
4951360Seric #define udb_addr	udb_u.udb_remote._udb_addr
5051360Seric #define udb_timeout	udb_u.udb_remote._udb_timeout
5151360Seric 
5251360Seric 		/* type UE_FORWARD -- forward message to remote */
5351360Seric 		struct
5451360Seric 		{
5551360Seric 			char	*_udb_fwdhost;	/* name of forward host */
5651360Seric 		} udb_forward;
5751360Seric #define udb_fwdhost	udb_u.udb_forward._udb_fwdhost
5851360Seric 
5951951Seric 		/* type UE_FETCH -- lookup in local database */
6051360Seric 		struct
6151360Seric 		{
6251360Seric 			char	*_udb_dbname;	/* pathname of database */
6351360Seric 			DB	*_udb_dbp;	/* open database ptr */
6451360Seric 		} udb_lookup;
6551360Seric #define udb_dbname	udb_u.udb_lookup._udb_dbname
6651360Seric #define udb_dbp		udb_u.udb_lookup._udb_dbp
6751360Seric 	} udb_u;
6851360Seric };
6951360Seric 
7051360Seric #define UDB_EOLIST	0	/* end of list */
7151360Seric #define UDB_SKIP	1	/* skip this entry */
7251360Seric #define UDB_REMOTE	2	/* look up in remote database */
7351951Seric #define UDB_DBFETCH	3	/* look up in local database */
7451360Seric #define UDB_FORWARD	4	/* forward to remote host */
7566759Seric #define UDB_HESIOD	5	/* look up via hesiod */
7651360Seric 
7751360Seric #define MAXUDBENT	10	/* maximum number of UDB entries */
7851360Seric 
7951363Seric 
8051363Seric struct option
8151363Seric {
8251363Seric 	char	*name;
8351363Seric 	char	*val;
8451363Seric };
8551363Seric /*
8651363Seric **  UDBEXPAND -- look up user in database and expand
8751363Seric **
8851363Seric **	Parameters:
8951363Seric **		a -- address to expand.
9051363Seric **		sendq -- pointer to head of sendq to put the expansions in.
9167982Seric **		aliaslevel -- the current alias nesting depth.
9267982Seric **		e -- the current envelope.
9351363Seric **
9451363Seric **	Returns:
9551923Seric **		EX_TEMPFAIL -- if something "odd" happened -- probably due
9651923Seric **			to accessing a file on an NFS server that is down.
9751923Seric **		EX_OK -- otherwise.
9851363Seric **
9951363Seric **	Side Effects:
10051363Seric **		Modifies sendq.
10151363Seric */
10251363Seric 
10351363Seric int	UdbPort = 1616;
10451363Seric int	UdbTimeout = 10;
10551363Seric 
10651953Seric struct udbent	UdbEnts[MAXUDBENT + 1];
10751953Seric int		UdbSock = -1;
10851953Seric bool		UdbInitialized = FALSE;
10951360Seric 
11051923Seric int
11167982Seric udbexpand(a, sendq, aliaslevel, e)
11250581Seric 	register ADDRESS *a;
11350581Seric 	ADDRESS **sendq;
11467982Seric 	int aliaslevel;
11555012Seric 	register ENVELOPE *e;
11650581Seric {
11750581Seric 	int i;
11850581Seric 	register char *p;
11950581Seric 	DBT key;
12050581Seric 	DBT info;
12151360Seric 	bool breakout;
12251360Seric 	register struct udbent *up;
12351362Seric 	int keylen;
12458082Seric 	int naddrs;
12557232Seric 	char keybuf[MAXKEY];
12657232Seric 	char buf[BUFSIZ];
12750581Seric 
12850581Seric 	if (tTd(28, 1))
12958065Seric 		printf("udbexpand(%s)\n", a->q_paddr);
13050581Seric 
13150581Seric 	/* make certain we are supposed to send to this address */
13258154Seric 	if (bitset(QDONTSEND|QVERIFIED, a->q_flags))
13351923Seric 		return EX_OK;
13455012Seric 	e->e_to = a->q_paddr;
13550581Seric 
13651360Seric 	/* on first call, locate the database */
13751951Seric 	if (!UdbInitialized)
13850581Seric 	{
13951923Seric 		extern int _udbx_init();
14051362Seric 
14151923Seric 		if (_udbx_init() == EX_TEMPFAIL)
14251923Seric 			return EX_TEMPFAIL;
14351362Seric 	}
14450581Seric 
14551909Seric 	/* short circuit the process if no chance of a match */
14651909Seric 	if (UdbSpec == NULL || UdbSpec[0] == '\0')
14751923Seric 		return EX_OK;
14851909Seric 
14966759Seric 	/* short circuit name begins with '\\' since it can't possibly match */
15066759Seric 	if (a->q_user[0] == '\\')
15166759Seric 		return EX_OK;
15266759Seric 
15351362Seric 	/* if name is too long, assume it won't match */
15451362Seric 	if (strlen(a->q_user) > sizeof keybuf - 12)
15551923Seric 		return EX_OK;
15651360Seric 
15751362Seric 	/* if name begins with a colon, it indicates our metadata */
15851362Seric 	if (a->q_user[0] == ':')
15951923Seric 		return EX_OK;
16051360Seric 
16151362Seric 	/* build actual database key */
16251362Seric 	(void) strcpy(keybuf, a->q_user);
16351362Seric 	(void) strcat(keybuf, ":maildrop");
16451362Seric 	keylen = strlen(keybuf);
16551360Seric 
16651360Seric 	breakout = FALSE;
16751362Seric 	for (up = UdbEnts; !breakout; up++)
16850581Seric 	{
16951360Seric 		char *user;
17050581Seric 
17151360Seric 		/*
17251360Seric 		**  Select action based on entry type.
17351360Seric 		**
17451360Seric 		**	On dropping out of this switch, "class" should
17551360Seric 		**	explain the type of the data, and "user" should
17651360Seric 		**	contain the user information.
17751360Seric 		*/
17850581Seric 
17951360Seric 		switch (up->udb_type)
18051360Seric 		{
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 			}
278*68603Seric 			e->e_flags |= EF_SENDRECEIPT;
279*68603Seric 			a->q_flags |= QREPORT|QEXPLODED;
28051360Seric 			break;
28151360Seric 
28266759Seric #ifdef HESIOD
28366759Seric 		  case UDB_HESIOD:
28466759Seric 			key.data = keybuf;
28566759Seric 			key.size = keylen;
28666759Seric 			if (tTd(28, 80))
28766759Seric 				printf("udbexpand: trying %s (%d) via hesiod\n",
28866759Seric 					keybuf, keylen);
28966759Seric 			/* look up the key via hesiod */
29066759Seric 			i = hes_udb_get(&key, &info);
29166759Seric 			if (i > 0 || info.size <= 0)
29266759Seric 			{
29366759Seric 				if (tTd(28, 2))
29466759Seric 				printf("udbexpand: no match on %s (%d)\n",
29566759Seric 					keybuf, keylen);
29666759Seric 				continue;
29766759Seric 			}
29866759Seric 			if (tTd(28, 80))
29966759Seric 				printf("udbexpand: match %.*s: %.*s\n",
30066759Seric 					key.size, key.data, info.size, info.data);
30166759Seric 			a->q_flags &= ~QSELFREF;
30266759Seric 
30366759Seric 			if (bitset(EF_VRFYONLY, e->e_flags))
30466759Seric 			{
30566759Seric 				a->q_flags |= QVERIFIED;
30666759Seric 				e->e_nrcpts++;
30766759Seric 				free(info.data);
30866759Seric 				return EX_OK;
30966759Seric 			}
31066759Seric 
31166759Seric 			breakout = TRUE;
31266759Seric 			if (info.size < sizeof buf)
31366759Seric 				user = buf;
31466759Seric 			else
31566759Seric 				user = xalloc(info.size + 1);
31666759Seric 			bcopy(info.data, user, info.size);
31766759Seric 			user[info.size] = '\0';
31866759Seric 			free(info.data);
31966759Seric 
32066759Seric 			message("hesioded to %s", user);
32166759Seric #ifdef LOG
32266759Seric 			if (LogLevel >= 10)
32366759Seric 				syslog(LOG_INFO, "%s: hesiod %s => %s",
32466759Seric 					e->e_id, e->e_to, user);
32566759Seric #endif
32667982Seric 			naddrs = sendtolist(user, a, sendq, aliaslevel + 1, e);
32766759Seric 
32866759Seric 			if (user != buf)
32966759Seric 				free(user);
33066759Seric 
33166759Seric 			if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
33266759Seric 			{
33366759Seric 				if (tTd(28, 5))
33466759Seric 				{
33566759Seric 					printf("udbexpand: QDONTSEND ");
33666759Seric 					printaddr(a, FALSE);
33766759Seric 				}
33866759Seric 				a->q_flags |= QDONTSEND;
33966759Seric 			}
34066759Seric 			if (i < 0)
34166759Seric 			{
34266759Seric 				syserr("udbexpand: hesiod-get %.*s stat %d",
34366759Seric 					key.size, key.data, i);
34466759Seric 				return EX_TEMPFAIL;
34566759Seric 			}
34666759Seric 
34766759Seric 			/*
34866759Seric 			**  If this address has a -request address, reflect
34966759Seric 			**  it into the envelope.
35066759Seric 			*/
35166759Seric 
35266759Seric 			(void) strcpy(keybuf, a->q_user);
35366759Seric 			(void) strcat(keybuf, ":mailsender");
35466759Seric 			keylen = strlen(keybuf);
35566759Seric 			key.data = keybuf;
35666759Seric 			key.size = keylen;
35766759Seric 			i = hes_udb_get(&key, &info);
35866759Seric 			if (i != 0 || info.size <= 0)
35966759Seric 				break;
36066759Seric 			a->q_owner = xalloc(info.size + 1);
36166759Seric 			bcopy(info.data, a->q_owner, info.size);
36266759Seric 			a->q_owner[info.size] = '\0';
36366759Seric 			free(info.data);
36466759Seric 			break;
36566759Seric #endif /* HESIOD */
36666759Seric 
36751360Seric 		  case UDB_REMOTE:
36851741Seric 			/* not yet implemented */
36951741Seric 			continue;
37051362Seric 
37151360Seric 		  case UDB_FORWARD:
37258099Seric 			if (bitset(EF_VRFYONLY, e->e_flags))
37358099Seric 				return EX_OK;
37451360Seric 			i = strlen(up->udb_fwdhost) + strlen(a->q_user) + 1;
37551360Seric 			if (i < sizeof buf)
37651360Seric 				user = buf;
37751360Seric 			else
37851360Seric 				user = xalloc(i + 1);
37951360Seric 			(void) sprintf(user, "%s@%s", a->q_user, up->udb_fwdhost);
38058151Seric 			message("expanded to %s", user);
38158082Seric 			a->q_flags &= ~QSELFREF;
38267982Seric 			naddrs = sendtolist(user, a, sendq, aliaslevel + 1, e);
38358082Seric 			if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
38458065Seric 			{
38558065Seric 				if (tTd(28, 5))
38658065Seric 				{
38758065Seric 					printf("udbexpand: QDONTSEND ");
38858065Seric 					printaddr(a, FALSE);
38958065Seric 				}
39058065Seric 				a->q_flags |= QDONTSEND;
39158065Seric 			}
39251362Seric 			if (user != buf)
39351362Seric 				free(user);
39451362Seric 			breakout = TRUE;
39551360Seric 			break;
39651360Seric 
39751360Seric 		  case UDB_EOLIST:
39851360Seric 			breakout = TRUE;
39951360Seric 			continue;
40051360Seric 
40151360Seric 		  default:
40251360Seric 			/* unknown entry type */
40351360Seric 			continue;
40451360Seric 		}
40551362Seric 	}
40651923Seric 	return EX_OK;
40751362Seric }
40851951Seric /*
40951951Seric **  UDBSENDER -- return canonical external name of sender, given local name
41051951Seric **
41151951Seric **	Parameters:
41251951Seric **		sender -- the name of the sender on the local machine.
41351951Seric **
41451951Seric **	Returns:
41551951Seric **		The external name for this sender, if derivable from the
41651951Seric **			database.
41751951Seric **		NULL -- if nothing is changed from the database.
41851951Seric **
41951951Seric **	Side Effects:
42051951Seric **		none.
42151951Seric */
42251360Seric 
42351951Seric char *
42451951Seric udbsender(sender)
42551951Seric 	char *sender;
42651951Seric {
42764350Seric 	extern char *udbmatch();
42864350Seric 
42964350Seric 	return udbmatch(sender, "mailname");
43064350Seric }
43164350Seric 
43264350Seric 
43364350Seric char *
43464350Seric udbmatch(user, field)
43564350Seric 	char *user;
43664350Seric 	char *field;
43764350Seric {
43851951Seric 	register char *p;
43951951Seric 	register struct udbent *up;
44051951Seric 	int i;
44151951Seric 	int keylen;
44251951Seric 	DBT key, info;
44357232Seric 	char keybuf[MAXKEY];
44451951Seric 
44551951Seric 	if (tTd(28, 1))
44664350Seric 		printf("udbmatch(%s, %s)\n", user, field);
44751951Seric 
44851951Seric 	if (!UdbInitialized)
44951951Seric 	{
45051951Seric 		if (_udbx_init() == EX_TEMPFAIL)
45151951Seric 			return NULL;
45251951Seric 	}
45351951Seric 
45451951Seric 	/* short circuit if no spec */
45551951Seric 	if (UdbSpec == NULL || UdbSpec[0] == '\0')
45651951Seric 		return NULL;
45751951Seric 
45866759Seric 	/* short circuit name begins with '\\' since it can't possibly match */
45966759Seric 	if (user[0] == '\\')
46066759Seric 		return NULL;
46166759Seric 
46251951Seric 	/* long names can never match and are a pain to deal with */
46364350Seric 	if ((strlen(user) + strlen(field)) > sizeof keybuf - 4)
46451951Seric 		return NULL;
46551951Seric 
46651951Seric 	/* names beginning with colons indicate metadata */
46764350Seric 	if (user[0] == ':')
46851951Seric 		return NULL;
46951951Seric 
47051951Seric 	/* build database key */
47164350Seric 	(void) strcpy(keybuf, user);
47264350Seric 	(void) strcat(keybuf, ":");
47364350Seric 	(void) strcat(keybuf, field);
47451951Seric 	keylen = strlen(keybuf);
47551951Seric 
47651951Seric 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
47751951Seric 	{
47851951Seric 		/*
47951951Seric 		**  Select action based on entry type.
48051951Seric 		*/
48151951Seric 
48251951Seric 		switch (up->udb_type)
48351951Seric 		{
48451951Seric 		  case UDB_DBFETCH:
48551951Seric 			key.data = keybuf;
48651951Seric 			key.size = keylen;
48751951Seric 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
48851951Seric 			if (i != 0 || info.size <= 0)
48951951Seric 			{
49051951Seric 				if (tTd(28, 2))
49166759Seric 					printf("udbmatch: no match on %s (%d) via db\n",
49264067Seric 							keybuf, keylen);
49351951Seric 				continue;
49451951Seric 			}
49551951Seric 
49651951Seric 			p = xalloc(info.size + 1);
49751951Seric 			bcopy(info.data, p, info.size);
49851951Seric 			p[info.size] = '\0';
49951951Seric 			if (tTd(28, 1))
50064350Seric 				printf("udbmatch ==> %s\n", p);
50151951Seric 			return p;
50266759Seric 			break;
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 			free(info.data);
52166759Seric 			if (tTd(28, 1))
52266759Seric 				printf("udbmatch ==> %s\n", p);
52366759Seric 			return p;
52466759Seric 			break;
52566759Seric #endif /* HESIOD */
52651951Seric 		}
52751951Seric 	}
52851951Seric 
52964350Seric 	if (strcmp(field, "mailname") != 0)
53064350Seric 		return NULL;
53164350Seric 
53251951Seric 	/*
53351951Seric 	**  Nothing yet.  Search again for a default case.  But only
53451951Seric 	**  use it if we also have a forward (:maildrop) pointer already
53551951Seric 	**  in the database.
53651951Seric 	*/
53751951Seric 
53851951Seric 	/* build database key */
53964350Seric 	(void) strcpy(keybuf, user);
54051951Seric 	(void) strcat(keybuf, ":maildrop");
54151951Seric 	keylen = strlen(keybuf);
54251951Seric 
54351951Seric 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
54451951Seric 	{
54551951Seric 		switch (up->udb_type)
54651951Seric 		{
54751951Seric 		  case UDB_DBFETCH:
54851951Seric 			/* get the default case for this database */
54951951Seric 			if (up->udb_default == NULL)
55051951Seric 			{
55151951Seric 				key.data = ":default:mailname";
55251951Seric 				key.size = strlen(key.data);
55351951Seric 				i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
55451951Seric 				if (i != 0 || info.size <= 0)
55551951Seric 				{
55651951Seric 					/* no default case */
55751951Seric 					up->udb_default = "";
55851951Seric 					continue;
55951951Seric 				}
56051951Seric 
56151951Seric 				/* save the default case */
56251951Seric 				up->udb_default = xalloc(info.size + 1);
56351951Seric 				bcopy(info.data, up->udb_default, info.size);
56451951Seric 				up->udb_default[info.size] = '\0';
56551951Seric 			}
56651951Seric 			else if (up->udb_default[0] == '\0')
56751951Seric 				continue;
56851951Seric 
56951951Seric 			/* we have a default case -- verify user:maildrop */
57051951Seric 			key.data = keybuf;
57151951Seric 			key.size = keylen;
57251951Seric 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
57351951Seric 			if (i != 0 || info.size <= 0)
57451951Seric 			{
57551951Seric 				/* nope -- no aliasing for this user */
57651951Seric 				continue;
57751951Seric 			}
57851951Seric 
57951951Seric 			/* they exist -- build the actual address */
58064350Seric 			p = xalloc(strlen(user) + strlen(up->udb_default) + 2);
58164350Seric 			(void) strcpy(p, user);
58251951Seric 			(void) strcat(p, "@");
58351951Seric 			(void) strcat(p, up->udb_default);
58451951Seric 			if (tTd(28, 1))
58564350Seric 				printf("udbmatch ==> %s\n", p);
58651951Seric 			return p;
58766759Seric 			break;
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 				free(info.data);
61066759Seric 			}
61166759Seric 			else if (up->udb_default[0] == '\0')
61266759Seric 				continue;
61366759Seric 
61466759Seric 			/* we have a default case -- verify user:maildrop */
61566759Seric 			key.data = keybuf;
61666759Seric 			key.size = keylen;
61766759Seric 			i = hes_udb_get(&key, &info);
61866759Seric 			if (i != 0 || info.size <= 0)
61966759Seric 			{
62066759Seric 				/* nope -- no aliasing for this user */
62166759Seric 				continue;
62266759Seric 			}
62366759Seric 
62466759Seric 			free(info.data);
62566759Seric 			/* they exist -- build the actual address */
62666759Seric 			p = xalloc(strlen(user) + strlen(up->udb_default) + 2);
62766759Seric 			(void) strcpy(p, user);
62866759Seric 			(void) strcat(p, "@");
62966759Seric 			(void) strcat(p, up->udb_default);
63066759Seric 			if (tTd(28, 1))
63166759Seric 				printf("udbmatch ==> %s\n", p);
63266759Seric 			return p;
63366759Seric 			break;
63466759Seric #endif /* HESIOD */
63551951Seric 		}
63651951Seric 	}
63751951Seric 
63851951Seric 	/* still nothing....  too bad */
63951951Seric 	return NULL;
64051951Seric }
64151951Seric /*
64268521Seric **  UDB_MAP_LOOKUP -- look up arbitrary entry in user database map
64368521Seric **
64468521Seric **	Parameters:
64568521Seric **		map -- the map being queried.
64668521Seric **		name -- the name to look up.
64768521Seric **		av -- arguments to the map lookup.
64868521Seric **		statp -- to get any error status.
64968521Seric **
65068521Seric **	Returns:
65168521Seric **		NULL if name not found in map.
65268521Seric **		The rewritten name otherwise.
65368521Seric */
65468521Seric 
65568521Seric char *
65668521Seric udb_map_lookup(map, name, av, statp)
65768521Seric 	MAP *map;
65868521Seric 	char *name;
65968521Seric 	char **av;
66068521Seric 	int *statp;
66168521Seric {
66268521Seric 	char *val;
66368521Seric 
66468521Seric 	if (tTd(38, 20))
66568521Seric 		printf("udb_map_lookup(%s, %s)\n", map->map_mname, name);
66668521Seric 	val = udbmatch(name, map->map_file);
66768521Seric 	if (val == NULL)
66868521Seric 		return NULL;
66968521Seric 	if (bitset(MF_MATCHONLY, map->map_mflags))
67068521Seric 		return map_rewrite(map, name, strlen(name), NULL);
67168521Seric 	else
67268521Seric 		return map_rewrite(map, val, strlen(val), av);
67368521Seric }
67468521Seric /*
67551951Seric **  _UDBX_INIT -- parse the UDB specification, opening any valid entries.
67651951Seric **
67751951Seric **	Parameters:
67851951Seric **		none.
67951951Seric **
68051951Seric **	Returns:
68151951Seric **		EX_TEMPFAIL -- if it appeared it couldn't get hold of a
68251951Seric **			database due to a host being down or some similar
68351951Seric **			(recoverable) situation.
68451951Seric **		EX_OK -- otherwise.
68551951Seric **
68651951Seric **	Side Effects:
68751951Seric **		Fills in the UdbEnts structure from UdbSpec.
68851951Seric */
68951951Seric 
69051363Seric #define MAXUDBOPTS	27
69151363Seric 
69251953Seric int
69351362Seric _udbx_init()
69451362Seric {
69551362Seric 	register char *p;
69651362Seric 	int i;
69751362Seric 	register struct udbent *up;
69857232Seric 	char buf[BUFSIZ];
69951360Seric 
70051951Seric 	if (UdbInitialized)
70151951Seric 		return EX_OK;
70251951Seric 
70351908Seric # ifdef UDB_DEFAULT_SPEC
70451908Seric 	if (UdbSpec == NULL)
70551908Seric 		UdbSpec = UDB_DEFAULT_SPEC;
70651908Seric # endif
70751908Seric 
70851362Seric 	p = UdbSpec;
70951362Seric 	up = UdbEnts;
71051762Seric 	while (p != NULL)
71151362Seric 	{
71251362Seric 		char *spec;
71351362Seric 		auto int rcode;
71451363Seric 		int nopts;
71551362Seric 		int nmx;
71651362Seric 		register struct hostent *h;
71751362Seric 		char *mxhosts[MAXMXHOSTS + 1];
71851363Seric 		struct option opts[MAXUDBOPTS + 1];
71951362Seric 
72051362Seric 		while (*p == ' ' || *p == '\t' || *p == ',')
72151362Seric 			p++;
72251362Seric 		if (*p == '\0')
72351362Seric 			break;
72451362Seric 		spec = p;
72556795Seric 		p = strchr(p, ',');
72651761Seric 		if (p != NULL)
72751362Seric 			*p++ = '\0';
72851363Seric 
72951363Seric 		/* extract options */
73051363Seric 		nopts = _udb_parsespec(spec, opts, MAXUDBOPTS);
73151363Seric 
73251363Seric 		/*
73351363Seric 		**  Decode database specification.
73451363Seric 		**
73551363Seric 		**	In the sendmail tradition, the leading character
73651363Seric 		**	defines the semantics of the rest of the entry.
73751363Seric 		**
73851363Seric 		**	+hostname --	send a datagram to the udb server
73951363Seric 		**			on host "hostname" asking for the
74051363Seric 		**			home mail server for this user.
74151363Seric 		**	*hostname --	similar to +hostname, except that the
74251363Seric 		**			hostname is searched as an MX record;
74351363Seric 		**			resulting hosts are searched as for
74451363Seric 		**			+mxhostname.  If no MX host is found,
74551363Seric 		**			this is the same as +hostname.
74651363Seric 		**	@hostname --	forward email to the indicated host.
74751363Seric 		**			This should be the last in the list,
74851363Seric 		**			since it always matches the input.
74951363Seric 		**	/dbname	 --	search the named database on the local
75051363Seric 		**			host using the Berkeley db package.
75151363Seric 		*/
75251363Seric 
75351362Seric 		switch (*spec)
75451360Seric 		{
75551362Seric 		  case '+':	/* search remote database */
75651363Seric 		  case '*':	/* search remote database (expand MX) */
75751363Seric 			if (*spec == '*')
75851363Seric 			{
75966334Seric #if NAMED_BIND
76059273Seric 				nmx = getmxrr(spec + 1, mxhosts, FALSE, &rcode);
76157629Seric #else
76257629Seric 				mxhosts[0] = spec + 1;
76357629Seric 				nmx = 1;
76457629Seric 				rcode = 0;
76557629Seric #endif
76651363Seric 				if (tTd(28, 16))
76751363Seric 				{
76851363Seric 					int i;
76951362Seric 
77051363Seric 					printf("getmxrr(%s): %d", spec + 1, nmx);
77151363Seric 					for (i = 0; i <= nmx; i++)
77251363Seric 						printf(" %s", mxhosts[i]);
77351363Seric 					printf("\n");
77451363Seric 				}
77551363Seric 			}
77651363Seric 			else
77751362Seric 			{
77851363Seric 				nmx = 1;
77951363Seric 				mxhosts[0] = spec + 1;
78051362Seric 			}
78151362Seric 
78251362Seric 			for (i = 0; i < nmx; i++)
78351362Seric 			{
78451362Seric 				h = gethostbyname(mxhosts[i]);
78551362Seric 				if (h == NULL)
78651362Seric 					continue;
78751362Seric 				up->udb_type = UDB_REMOTE;
78851362Seric 				up->udb_addr.sin_family = h->h_addrtype;
78951362Seric 				bcopy(h->h_addr_list[0],
79051362Seric 				      (char *) &up->udb_addr.sin_addr,
79167421Seric 				      INADDRSZ);
79251362Seric 				up->udb_addr.sin_port = UdbPort;
79351362Seric 				up->udb_timeout = UdbTimeout;
79451362Seric 				up++;
79551362Seric 			}
79651362Seric 
79751362Seric 			/* set up a datagram socket */
79851362Seric 			if (UdbSock < 0)
79951362Seric 			{
80051362Seric 				UdbSock = socket(AF_INET, SOCK_DGRAM, 0);
80151362Seric 				(void) fcntl(UdbSock, F_SETFD, 1);
80251362Seric 			}
80351362Seric 			break;
80451362Seric 
80551362Seric 		  case '@':	/* forward to remote host */
80651362Seric 			up->udb_type = UDB_FORWARD;
80751362Seric 			up->udb_fwdhost = spec + 1;
80851362Seric 			up++;
80951362Seric 			break;
81051362Seric 
81166759Seric 		  case 'h':	/* use hesiod */
81266759Seric 		  case 'H':
81366759Seric #ifdef HESIOD
81466759Seric 			if (strcasecmp(spec, "hesiod") != 0)
81566759Seric 				break;
81666759Seric 			up->udb_type = UDB_HESIOD;
81766759Seric 			up++;
81866759Seric #endif /* HESIOD */
81966759Seric 			break;
82066759Seric 
82151362Seric 		  case '/':	/* look up remote name */
82251831Seric 			up->udb_dbname = spec;
82351923Seric 			errno = 0;
82451362Seric 			up->udb_dbp = dbopen(spec, O_RDONLY, 0644, DB_BTREE, NULL);
82551362Seric 			if (up->udb_dbp == NULL)
82651923Seric 			{
82767763Seric 				if (tTd(28, 1))
82867763Seric 				{
82967763Seric 					int saveerrno = errno;
83067763Seric 
83167763Seric 					printf("dbopen(%s): %s",
83267763Seric 						spec, errstring(errno));
83367763Seric 					errno = saveerrno;
83467763Seric 				}
83551923Seric 				if (errno != ENOENT && errno != EACCES)
83651951Seric 				{
83759615Seric #ifdef LOG
83859615Seric 					if (LogLevel > 2)
83959625Seric 						syslog(LOG_ERR, "dbopen(%s): %s",
84059625Seric 							spec, errstring(errno));
84159615Seric #endif
84251951Seric 					up->udb_type = UDB_EOLIST;
84351951Seric 					goto tempfail;
84451951Seric 				}
84551362Seric 				break;
84651923Seric 			}
84751951Seric 			up->udb_type = UDB_DBFETCH;
84851362Seric 			up++;
84951362Seric 			break;
85051360Seric 		}
85151362Seric 	}
85251362Seric 	up->udb_type = UDB_EOLIST;
85351360Seric 
85451362Seric 	if (tTd(28, 4))
85551362Seric 	{
85651951Seric 		for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
85751360Seric 		{
85851362Seric 			switch (up->udb_type)
85951362Seric 			{
86051362Seric 			  case UDB_REMOTE:
86151362Seric 				printf("REMOTE: addr %s, timeo %d\n",
86260494Seric 					anynet_ntoa((SOCKADDR *) &up->udb_addr),
86351362Seric 					up->udb_timeout);
86451362Seric 				break;
86551362Seric 
86651951Seric 			  case UDB_DBFETCH:
86751951Seric 				printf("FETCH: file %s\n",
86851830Seric 					up->udb_dbname);
86951362Seric 				break;
87051362Seric 
87151362Seric 			  case UDB_FORWARD:
87251362Seric 				printf("FORWARD: host %s\n",
87351362Seric 					up->udb_fwdhost);
87451362Seric 				break;
87551362Seric 
87666759Seric 			  case UDB_HESIOD:
87766759Seric 				printf("HESIOD\n");
87866759Seric 				break;
87966759Seric 
88051362Seric 			  default:
88151362Seric 				printf("UNKNOWN\n");
88251362Seric 				break;
88351362Seric 			}
88451360Seric 		}
88550581Seric 	}
88651951Seric 
88751951Seric 	UdbInitialized = TRUE;
88851955Seric 	errno = 0;
88951951Seric 	return EX_OK;
89051951Seric 
89151951Seric 	/*
89251951Seric 	**  On temporary failure, back out anything we've already done
89351951Seric 	*/
89451951Seric 
89551951Seric   tempfail:
89651951Seric 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
89751951Seric 	{
89851951Seric 		if (up->udb_type == UDB_DBFETCH)
89951951Seric 		{
90051951Seric 			(*up->udb_dbp->close)(up->udb_dbp);
90151951Seric 		}
90251951Seric 	}
90351951Seric 	return EX_TEMPFAIL;
90451360Seric }
90550581Seric 
90651363Seric int
90751363Seric _udb_parsespec(udbspec, opt, maxopts)
90851363Seric 	char *udbspec;
90951363Seric 	struct option opt[];
91051363Seric 	int maxopts;
91151363Seric {
91251363Seric 	register char *spec;
91351363Seric 	register char *spec_end;
91451363Seric 	register int optnum;
91551363Seric 
91656795Seric 	spec_end = strchr(udbspec, ':');
91751363Seric 	for (optnum = 0; optnum < maxopts && (spec = spec_end) != NULL; optnum++)
91851363Seric 	{
91951363Seric 		register char *p;
92051363Seric 
92158050Seric 		while (isascii(*spec) && isspace(*spec))
92251363Seric 			spec++;
92356795Seric 		spec_end = strchr(spec, ':');
92451363Seric 		if (spec_end != NULL)
92551363Seric 			*spec_end++ = '\0';
92651363Seric 
92751363Seric 		opt[optnum].name = spec;
92851363Seric 		opt[optnum].val = NULL;
92956795Seric 		p = strchr(spec, '=');
93051363Seric 		if (p != NULL)
93151363Seric 			opt[optnum].val = ++p;
93251363Seric 	}
93351363Seric 	return optnum;
93451363Seric }
93551363Seric 
93666759Seric #ifdef HESIOD
93766759Seric 
93866759Seric int
93966759Seric hes_udb_get(key, info)
94066759Seric 	DBT *key;
94166759Seric 	DBT *info;
94266759Seric {
94366759Seric 	char *name, *type;
94466759Seric 	char *p, **hp;
94568533Seric 	char kbuf[MAXKEY + 1];
94666759Seric 
94768533Seric 	strcpy(kbuf, key->data);
94868533Seric 	name = kbuf;
94966759Seric 	type = strchr(name, ':');
95068544Seric 	if (type == NULL)
95166759Seric 		return 1;
95266759Seric 	*type++ = '\0';
95366759Seric 
95466759Seric 	if (tTd(28, 1))
95566759Seric 		printf("hes_udb_get(%s, %s)\n", name, type);
95666759Seric 
95766759Seric 	/* make the hesiod query */
95866759Seric 	hp = hes_resolve(name, type);
95966759Seric 	if (hp == NULL)
96066759Seric 	{
96166759Seric 		/* network problem or timeout */
96266759Seric 		if (hes_error() == HES_ER_NET)
96366759Seric 			return -1;
96466759Seric 
96566759Seric 		return 1;
96666759Seric 	}
96766759Seric 	else
96866759Seric 	{
96966759Seric 		/*
97066759Seric 		**  If there are multiple matches, just return the
97166759Seric 		**  first one and free the others.
97266759Seric 		**
97366759Seric 		**  XXX These should really be returned; for example,
97466759Seric 		**  XXX it is legal for :maildrop to be multi-valued.
97566759Seric 		*/
97666759Seric 
97766759Seric 		for (p = hp[1]; p; p++)
97866759Seric 			free(p);
97966759Seric 
98066759Seric 		info->data = hp[0];
98166759Seric 		info->size = (size_t) strlen(info->data);
98266759Seric 	}
98366759Seric 
98466759Seric 	return 0;
98566759Seric }
98666759Seric #endif /* HESIOD */
98766759Seric 
98851360Seric #else /* not USERDB */
98951360Seric 
99051923Seric int
99167982Seric udbexpand(a, sendq, aliaslevel, e)
99251360Seric 	ADDRESS *a;
99351360Seric 	ADDRESS **sendq;
99467982Seric 	int aliaslevel;
99555012Seric 	ENVELOPE *e;
99651360Seric {
99751923Seric 	return EX_OK;
99850581Seric }
99950581Seric 
100050581Seric #endif /* USERDB */
1001