xref: /csrg-svn/usr.sbin/sendmail/src/udb.c (revision 62532)
150581Seric /*
250581Seric  * Copyright (c) 1983 Eric P. Allman
3*62532Sbostic  * Copyright (c) 1988, 1993
4*62532Sbostic  *	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*62532Sbostic static char sccsid [] = "@(#)udb.c	8.1 (Berkeley) 06/07/93 (with USERDB)";
1451360Seric #else
15*62532Sbostic static char sccsid [] = "@(#)udb.c	8.1 (Berkeley) 06/07/93 (without USERDB)";
1650581Seric #endif
1751360Seric #endif
1850581Seric 
1950581Seric #ifdef USERDB
2050581Seric 
2151360Seric #include <sys/time.h>
2251923Seric #include <errno.h>
2351360Seric #include <netdb.h>
2450581Seric #include <db.h>
2550581Seric 
2650581Seric /*
2753654Seric **  UDB.C -- interface between sendmail and Berkeley User Data Base.
2850581Seric **
2951363Seric **	This depends on the 4.4BSD db package.
3050581Seric */
3150581Seric 
3251362Seric 
3351360Seric struct udbent
3451360Seric {
3551360Seric 	char	*udb_spec;		/* string version of spec */
3651360Seric 	int	udb_type;		/* type of entry */
3751951Seric 	char	*udb_default;		/* default host for outgoing mail */
3851360Seric 	union
3951360Seric 	{
4051360Seric 		/* type UE_REMOTE -- do remote call for lookup */
4151360Seric 		struct
4251360Seric 		{
4351360Seric 			struct sockaddr_in _udb_addr;	/* address */
4451360Seric 			int		_udb_timeout;	/* timeout */
4551360Seric 		} udb_remote;
4651360Seric #define udb_addr	udb_u.udb_remote._udb_addr
4751360Seric #define udb_timeout	udb_u.udb_remote._udb_timeout
4851360Seric 
4951360Seric 		/* type UE_FORWARD -- forward message to remote */
5051360Seric 		struct
5151360Seric 		{
5251360Seric 			char	*_udb_fwdhost;	/* name of forward host */
5351360Seric 		} udb_forward;
5451360Seric #define udb_fwdhost	udb_u.udb_forward._udb_fwdhost
5551360Seric 
5651951Seric 		/* type UE_FETCH -- lookup in local database */
5751360Seric 		struct
5851360Seric 		{
5951360Seric 			char	*_udb_dbname;	/* pathname of database */
6051360Seric 			DB	*_udb_dbp;	/* open database ptr */
6151360Seric 		} udb_lookup;
6251360Seric #define udb_dbname	udb_u.udb_lookup._udb_dbname
6351360Seric #define udb_dbp		udb_u.udb_lookup._udb_dbp
6451360Seric 	} udb_u;
6551360Seric };
6651360Seric 
6751360Seric #define UDB_EOLIST	0	/* end of list */
6851360Seric #define UDB_SKIP	1	/* skip this entry */
6951360Seric #define UDB_REMOTE	2	/* look up in remote database */
7051951Seric #define UDB_DBFETCH	3	/* look up in local database */
7151360Seric #define UDB_FORWARD	4	/* forward to remote host */
7251360Seric 
7351360Seric #define MAXUDBENT	10	/* maximum number of UDB entries */
7451360Seric 
7551363Seric 
7651363Seric struct option
7751363Seric {
7851363Seric 	char	*name;
7951363Seric 	char	*val;
8051363Seric };
8151363Seric /*
8251363Seric **  UDBEXPAND -- look up user in database and expand
8351363Seric **
8451363Seric **	Parameters:
8551363Seric **		a -- address to expand.
8651363Seric **		sendq -- pointer to head of sendq to put the expansions in.
8751363Seric **
8851363Seric **	Returns:
8951923Seric **		EX_TEMPFAIL -- if something "odd" happened -- probably due
9051923Seric **			to accessing a file on an NFS server that is down.
9151923Seric **		EX_OK -- otherwise.
9251363Seric **
9351363Seric **	Side Effects:
9451363Seric **		Modifies sendq.
9551363Seric */
9651363Seric 
9751363Seric int	UdbPort = 1616;
9851363Seric int	UdbTimeout = 10;
9951363Seric 
10051953Seric struct udbent	UdbEnts[MAXUDBENT + 1];
10151953Seric int		UdbSock = -1;
10251953Seric bool		UdbInitialized = FALSE;
10351360Seric 
10451923Seric int
10555012Seric udbexpand(a, sendq, e)
10650581Seric 	register ADDRESS *a;
10750581Seric 	ADDRESS **sendq;
10855012Seric 	register ENVELOPE *e;
10950581Seric {
11050581Seric 	int i;
11150581Seric 	register char *p;
11250581Seric 	DBT key;
11350581Seric 	DBT info;
11451360Seric 	bool breakout;
11551360Seric 	register struct udbent *up;
11651362Seric 	int keylen;
11758082Seric 	int naddrs;
11857232Seric 	char keybuf[MAXKEY];
11957232Seric 	char buf[BUFSIZ];
12050581Seric 
12150581Seric 	if (tTd(28, 1))
12258065Seric 		printf("udbexpand(%s)\n", a->q_paddr);
12350581Seric 
12450581Seric 	/* make certain we are supposed to send to this address */
12558154Seric 	if (bitset(QDONTSEND|QVERIFIED, a->q_flags))
12651923Seric 		return EX_OK;
12755012Seric 	e->e_to = a->q_paddr;
12850581Seric 
12951360Seric 	/* on first call, locate the database */
13051951Seric 	if (!UdbInitialized)
13150581Seric 	{
13251923Seric 		extern int _udbx_init();
13351362Seric 
13451923Seric 		if (_udbx_init() == EX_TEMPFAIL)
13551923Seric 			return EX_TEMPFAIL;
13651362Seric 	}
13750581Seric 
13851909Seric 	/* short circuit the process if no chance of a match */
13951909Seric 	if (UdbSpec == NULL || UdbSpec[0] == '\0')
14051923Seric 		return EX_OK;
14151909Seric 
14251362Seric 	/* if name is too long, assume it won't match */
14351362Seric 	if (strlen(a->q_user) > sizeof keybuf - 12)
14451923Seric 		return EX_OK;
14551360Seric 
14651362Seric 	/* if name begins with a colon, it indicates our metadata */
14751362Seric 	if (a->q_user[0] == ':')
14851923Seric 		return EX_OK;
14951360Seric 
15051362Seric 	/* build actual database key */
15151362Seric 	(void) strcpy(keybuf, a->q_user);
15251362Seric 	(void) strcat(keybuf, ":maildrop");
15351362Seric 	keylen = strlen(keybuf);
15451360Seric 
15551360Seric 	breakout = FALSE;
15651362Seric 	for (up = UdbEnts; !breakout; up++)
15750581Seric 	{
15851360Seric 		char *user;
15950581Seric 
16051360Seric 		/*
16151360Seric 		**  Select action based on entry type.
16251360Seric 		**
16351360Seric 		**	On dropping out of this switch, "class" should
16451360Seric 		**	explain the type of the data, and "user" should
16551360Seric 		**	contain the user information.
16651360Seric 		*/
16750581Seric 
16851360Seric 		switch (up->udb_type)
16951360Seric 		{
17051951Seric 		  case UDB_DBFETCH:
17151362Seric 			key.data = keybuf;
17251362Seric 			key.size = keylen;
17360990Seric 			if (tTd(28, 80))
17460990Seric 				printf("udbexpand: trying %s\n", keybuf);
17551362Seric 			i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_CURSOR);
17651923Seric 			if (i > 0 || info.size <= 0)
17751360Seric 			{
17851360Seric 				if (tTd(28, 2))
17958065Seric 					printf("udbexpand: no match on %s\n", keybuf);
18051360Seric 				continue;
18151360Seric 			}
18260990Seric 			if (tTd(28, 80))
18360990Seric 				printf("udbexpand: match %.*s: %.*s\n",
18460990Seric 					key.size, key.data, info.size, info.data);
18550581Seric 
18658082Seric 			naddrs = 0;
18758082Seric 			a->q_flags &= ~QSELFREF;
18851830Seric 			while (i == 0 && key.size == keylen &&
18951830Seric 					bcmp(key.data, keybuf, keylen) == 0)
19051362Seric 			{
19158099Seric 				if (bitset(EF_VRFYONLY, e->e_flags))
19258154Seric 				{
19358154Seric 					a->q_flags |= QVERIFIED;
19458884Seric 					e->e_nrcpts++;
19558099Seric 					return EX_OK;
19658154Seric 				}
19758099Seric 
19851830Seric 				breakout = TRUE;
19951362Seric 				if (info.size < sizeof buf)
20051362Seric 					user = buf;
20151362Seric 				else
20251362Seric 					user = xalloc(info.size + 1);
20351362Seric 				bcopy(info.data, user, info.size);
20451362Seric 				user[info.size] = '\0';
20550581Seric 
20658151Seric 				message("expanded to %s", user);
20757977Seric #ifdef LOG
20857977Seric 				if (LogLevel >= 10)
20957977Seric 					syslog(LOG_INFO, "%s: expand %s => %s",
21057977Seric 						e->e_id, e->e_to, user);
21157977Seric #endif
21251362Seric 				AliasLevel++;
21358082Seric 				naddrs += sendtolist(user, a, sendq, e);
21451362Seric 				AliasLevel--;
21551362Seric 
21651362Seric 				if (user != buf)
21751362Seric 					free(user);
21851362Seric 
21951362Seric 				/* get the next record */
22051362Seric 				i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_NEXT);
22151830Seric 			}
22260990Seric 
22360990Seric 			/* if nothing ever matched, try next database */
22460990Seric 			if (!breakout)
22560990Seric 				continue;
22660990Seric 
22758082Seric 			if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
22858065Seric 			{
22958065Seric 				if (tTd(28, 5))
23058065Seric 				{
23158065Seric 					printf("udbexpand: QDONTSEND ");
23258065Seric 					printaddr(a, FALSE);
23358065Seric 				}
23458065Seric 				a->q_flags |= QDONTSEND;
23558065Seric 			}
23651923Seric 			if (i < 0)
23751923Seric 			{
23858010Seric 				syserr("udbexpand: db-get %.*s stat %d",
23958010Seric 					key.size, key.data, i);
24051923Seric 				return EX_TEMPFAIL;
24151923Seric 			}
24259707Seric 
24359707Seric 			/*
24459707Seric 			**  If this address has a -request address, reflect
24559707Seric 			**  it into the envelope.
24659707Seric 			*/
24759707Seric 
24859707Seric 			(void) strcpy(keybuf, a->q_user);
24959707Seric 			(void) strcat(keybuf, ":mailsender");
25059707Seric 			keylen = strlen(keybuf);
25159707Seric 			key.data = keybuf;
25259707Seric 			key.size = keylen;
25359707Seric 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
25459707Seric 			if (i != 0 || info.size <= 0)
25559707Seric 				break;
25659707Seric 			a->q_owner = xalloc(info.size + 1);
25759707Seric 			bcopy(info.data, a->q_owner, info.size);
25859707Seric 			a->q_owner[info.size] = '\0';
25951360Seric 			break;
26051360Seric 
26151360Seric 		  case UDB_REMOTE:
26251741Seric 			/* not yet implemented */
26351741Seric 			continue;
26451362Seric 
26551360Seric 		  case UDB_FORWARD:
26658099Seric 			if (bitset(EF_VRFYONLY, e->e_flags))
26758099Seric 				return EX_OK;
26851360Seric 			i = strlen(up->udb_fwdhost) + strlen(a->q_user) + 1;
26951360Seric 			if (i < sizeof buf)
27051360Seric 				user = buf;
27151360Seric 			else
27251360Seric 				user = xalloc(i + 1);
27351360Seric 			(void) sprintf(user, "%s@%s", a->q_user, up->udb_fwdhost);
27458151Seric 			message("expanded to %s", user);
27558082Seric 			a->q_flags &= ~QSELFREF;
27651362Seric 			AliasLevel++;
27758082Seric 			naddrs = sendtolist(user, a, sendq, e);
27851362Seric 			AliasLevel--;
27958082Seric 			if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
28058065Seric 			{
28158065Seric 				if (tTd(28, 5))
28258065Seric 				{
28358065Seric 					printf("udbexpand: QDONTSEND ");
28458065Seric 					printaddr(a, FALSE);
28558065Seric 				}
28658065Seric 				a->q_flags |= QDONTSEND;
28758065Seric 			}
28851362Seric 			if (user != buf)
28951362Seric 				free(user);
29051362Seric 			breakout = TRUE;
29151360Seric 			break;
29251360Seric 
29351360Seric 		  case UDB_EOLIST:
29451360Seric 			breakout = TRUE;
29551360Seric 			continue;
29651360Seric 
29751360Seric 		  default:
29851360Seric 			/* unknown entry type */
29951360Seric 			continue;
30051360Seric 		}
30151362Seric 	}
30251923Seric 	return EX_OK;
30351362Seric }
30451951Seric /*
30551951Seric **  UDBSENDER -- return canonical external name of sender, given local name
30651951Seric **
30751951Seric **	Parameters:
30851951Seric **		sender -- the name of the sender on the local machine.
30951951Seric **
31051951Seric **	Returns:
31151951Seric **		The external name for this sender, if derivable from the
31251951Seric **			database.
31351951Seric **		NULL -- if nothing is changed from the database.
31451951Seric **
31551951Seric **	Side Effects:
31651951Seric **		none.
31751951Seric */
31851360Seric 
31951951Seric char *
32051951Seric udbsender(sender)
32151951Seric 	char *sender;
32251951Seric {
32351951Seric 	register char *p;
32451951Seric 	register struct udbent *up;
32551951Seric 	int i;
32651951Seric 	int keylen;
32751951Seric 	DBT key, info;
32857232Seric 	char keybuf[MAXKEY];
32951951Seric 
33051951Seric 	if (tTd(28, 1))
33151951Seric 		printf("udbsender(%s)\n", sender);
33251951Seric 
33351951Seric 	if (!UdbInitialized)
33451951Seric 	{
33551951Seric 		if (_udbx_init() == EX_TEMPFAIL)
33651951Seric 			return NULL;
33751951Seric 	}
33851951Seric 
33951951Seric 	/* short circuit if no spec */
34051951Seric 	if (UdbSpec == NULL || UdbSpec[0] == '\0')
34151951Seric 		return NULL;
34251951Seric 
34351951Seric 	/* long names can never match and are a pain to deal with */
34451951Seric 	if (strlen(sender) > sizeof keybuf - 12)
34551951Seric 		return NULL;
34651951Seric 
34751951Seric 	/* names beginning with colons indicate metadata */
34851951Seric 	if (sender[0] == ':')
34951951Seric 		return NULL;
35051951Seric 
35151951Seric 	/* build database key */
35251951Seric 	(void) strcpy(keybuf, sender);
35351951Seric 	(void) strcat(keybuf, ":mailname");
35451951Seric 	keylen = strlen(keybuf);
35551951Seric 
35651951Seric 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
35751951Seric 	{
35851951Seric 		/*
35951951Seric 		**  Select action based on entry type.
36051951Seric 		*/
36151951Seric 
36251951Seric 		switch (up->udb_type)
36351951Seric 		{
36451951Seric 		  case UDB_DBFETCH:
36551951Seric 			key.data = keybuf;
36651951Seric 			key.size = keylen;
36751951Seric 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
36851951Seric 			if (i != 0 || info.size <= 0)
36951951Seric 			{
37051951Seric 				if (tTd(28, 2))
37151951Seric 					printf("udbsender: no match on %s\n",
37251951Seric 							keybuf);
37351951Seric 				continue;
37451951Seric 			}
37551951Seric 
37651951Seric 			p = xalloc(info.size + 1);
37751951Seric 			bcopy(info.data, p, info.size);
37851951Seric 			p[info.size] = '\0';
37951951Seric 			if (tTd(28, 1))
38051951Seric 				printf("udbsender ==> %s\n", p);
38151951Seric 			return p;
38251951Seric 		}
38351951Seric 	}
38451951Seric 
38551951Seric 	/*
38651951Seric 	**  Nothing yet.  Search again for a default case.  But only
38751951Seric 	**  use it if we also have a forward (:maildrop) pointer already
38851951Seric 	**  in the database.
38951951Seric 	*/
39051951Seric 
39151951Seric 	/* build database key */
39251951Seric 	(void) strcpy(keybuf, sender);
39351951Seric 	(void) strcat(keybuf, ":maildrop");
39451951Seric 	keylen = strlen(keybuf);
39551951Seric 
39651951Seric 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
39751951Seric 	{
39851951Seric 		switch (up->udb_type)
39951951Seric 		{
40051951Seric 		  case UDB_DBFETCH:
40151951Seric 			/* get the default case for this database */
40251951Seric 			if (up->udb_default == NULL)
40351951Seric 			{
40451951Seric 				key.data = ":default:mailname";
40551951Seric 				key.size = strlen(key.data);
40651951Seric 				i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
40751951Seric 				if (i != 0 || info.size <= 0)
40851951Seric 				{
40951951Seric 					/* no default case */
41051951Seric 					up->udb_default = "";
41151951Seric 					continue;
41251951Seric 				}
41351951Seric 
41451951Seric 				/* save the default case */
41551951Seric 				up->udb_default = xalloc(info.size + 1);
41651951Seric 				bcopy(info.data, up->udb_default, info.size);
41751951Seric 				up->udb_default[info.size] = '\0';
41851951Seric 			}
41951951Seric 			else if (up->udb_default[0] == '\0')
42051951Seric 				continue;
42151951Seric 
42251951Seric 			/* we have a default case -- verify user:maildrop */
42351951Seric 			key.data = keybuf;
42451951Seric 			key.size = keylen;
42551951Seric 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
42651951Seric 			if (i != 0 || info.size <= 0)
42751951Seric 			{
42851951Seric 				/* nope -- no aliasing for this user */
42951951Seric 				continue;
43051951Seric 			}
43151951Seric 
43251951Seric 			/* they exist -- build the actual address */
43351951Seric 			p = xalloc(strlen(sender) + strlen(up->udb_default) + 2);
43451951Seric 			(void) strcpy(p, sender);
43551951Seric 			(void) strcat(p, "@");
43651951Seric 			(void) strcat(p, up->udb_default);
43751951Seric 			if (tTd(28, 1))
43851951Seric 				printf("udbsender ==> %s\n", p);
43951951Seric 			return p;
44051951Seric 		}
44151951Seric 	}
44251951Seric 
44351951Seric 	/* still nothing....  too bad */
44451951Seric 	return NULL;
44551951Seric }
44651951Seric /*
44751951Seric **  _UDBX_INIT -- parse the UDB specification, opening any valid entries.
44851951Seric **
44951951Seric **	Parameters:
45051951Seric **		none.
45151951Seric **
45251951Seric **	Returns:
45351951Seric **		EX_TEMPFAIL -- if it appeared it couldn't get hold of a
45451951Seric **			database due to a host being down or some similar
45551951Seric **			(recoverable) situation.
45651951Seric **		EX_OK -- otherwise.
45751951Seric **
45851951Seric **	Side Effects:
45951951Seric **		Fills in the UdbEnts structure from UdbSpec.
46051951Seric */
46151951Seric 
46251363Seric #define MAXUDBOPTS	27
46351363Seric 
46451953Seric int
46551362Seric _udbx_init()
46651362Seric {
46751362Seric 	register char *p;
46851362Seric 	int i;
46951362Seric 	register struct udbent *up;
47057232Seric 	char buf[BUFSIZ];
47151360Seric 
47251951Seric 	if (UdbInitialized)
47351951Seric 		return EX_OK;
47451951Seric 
47551908Seric # ifdef UDB_DEFAULT_SPEC
47651908Seric 	if (UdbSpec == NULL)
47751908Seric 		UdbSpec = UDB_DEFAULT_SPEC;
47851908Seric # endif
47951908Seric 
48051362Seric 	p = UdbSpec;
48151362Seric 	up = UdbEnts;
48251762Seric 	while (p != NULL)
48351362Seric 	{
48451362Seric 		char *spec;
48551362Seric 		auto int rcode;
48651363Seric 		int nopts;
48751362Seric 		int nmx;
48851362Seric 		register struct hostent *h;
48951362Seric 		char *mxhosts[MAXMXHOSTS + 1];
49051363Seric 		struct option opts[MAXUDBOPTS + 1];
49151362Seric 
49251362Seric 		while (*p == ' ' || *p == '\t' || *p == ',')
49351362Seric 			p++;
49451362Seric 		if (*p == '\0')
49551362Seric 			break;
49651362Seric 		spec = p;
49756795Seric 		p = strchr(p, ',');
49851761Seric 		if (p != NULL)
49951362Seric 			*p++ = '\0';
50051363Seric 
50151363Seric 		/* extract options */
50251363Seric 		nopts = _udb_parsespec(spec, opts, MAXUDBOPTS);
50351363Seric 
50451363Seric 		/*
50551363Seric 		**  Decode database specification.
50651363Seric 		**
50751363Seric 		**	In the sendmail tradition, the leading character
50851363Seric 		**	defines the semantics of the rest of the entry.
50951363Seric 		**
51051363Seric 		**	+hostname --	send a datagram to the udb server
51151363Seric 		**			on host "hostname" asking for the
51251363Seric 		**			home mail server for this user.
51351363Seric 		**	*hostname --	similar to +hostname, except that the
51451363Seric 		**			hostname is searched as an MX record;
51551363Seric 		**			resulting hosts are searched as for
51651363Seric 		**			+mxhostname.  If no MX host is found,
51751363Seric 		**			this is the same as +hostname.
51851363Seric 		**	@hostname --	forward email to the indicated host.
51951363Seric 		**			This should be the last in the list,
52051363Seric 		**			since it always matches the input.
52151363Seric 		**	/dbname	 --	search the named database on the local
52251363Seric 		**			host using the Berkeley db package.
52351363Seric 		*/
52451363Seric 
52551362Seric 		switch (*spec)
52651360Seric 		{
52751362Seric 		  case '+':	/* search remote database */
52851363Seric 		  case '*':	/* search remote database (expand MX) */
52951363Seric 			if (*spec == '*')
53051363Seric 			{
53157629Seric #ifdef NAMED_BIND
53259273Seric 				nmx = getmxrr(spec + 1, mxhosts, FALSE, &rcode);
53357629Seric #else
53457629Seric 				mxhosts[0] = spec + 1;
53557629Seric 				nmx = 1;
53657629Seric 				rcode = 0;
53757629Seric #endif
53851363Seric 				if (tTd(28, 16))
53951363Seric 				{
54051363Seric 					int i;
54151362Seric 
54251363Seric 					printf("getmxrr(%s): %d", spec + 1, nmx);
54351363Seric 					for (i = 0; i <= nmx; i++)
54451363Seric 						printf(" %s", mxhosts[i]);
54551363Seric 					printf("\n");
54651363Seric 				}
54751363Seric 			}
54851363Seric 			else
54951362Seric 			{
55051363Seric 				nmx = 1;
55151363Seric 				mxhosts[0] = spec + 1;
55251362Seric 			}
55351362Seric 
55451362Seric 			for (i = 0; i < nmx; i++)
55551362Seric 			{
55651362Seric 				h = gethostbyname(mxhosts[i]);
55751362Seric 				if (h == NULL)
55851362Seric 					continue;
55951362Seric 				up->udb_type = UDB_REMOTE;
56051362Seric 				up->udb_addr.sin_family = h->h_addrtype;
56151362Seric 				bcopy(h->h_addr_list[0],
56251362Seric 				      (char *) &up->udb_addr.sin_addr,
56351362Seric 				      h->h_length);
56451362Seric 				up->udb_addr.sin_port = UdbPort;
56551362Seric 				up->udb_timeout = UdbTimeout;
56651362Seric 				up++;
56751362Seric 			}
56851362Seric 
56951362Seric 			/* set up a datagram socket */
57051362Seric 			if (UdbSock < 0)
57151362Seric 			{
57251362Seric 				UdbSock = socket(AF_INET, SOCK_DGRAM, 0);
57351362Seric 				(void) fcntl(UdbSock, F_SETFD, 1);
57451362Seric 			}
57551362Seric 			break;
57651362Seric 
57751362Seric 		  case '@':	/* forward to remote host */
57851362Seric 			up->udb_type = UDB_FORWARD;
57951362Seric 			up->udb_fwdhost = spec + 1;
58051362Seric 			up++;
58151362Seric 			break;
58251362Seric 
58351362Seric 		  case '/':	/* look up remote name */
58451831Seric 			up->udb_dbname = spec;
58551923Seric 			errno = 0;
58651362Seric 			up->udb_dbp = dbopen(spec, O_RDONLY, 0644, DB_BTREE, NULL);
58751362Seric 			if (up->udb_dbp == NULL)
58851923Seric 			{
58951923Seric 				if (errno != ENOENT && errno != EACCES)
59051951Seric 				{
59159615Seric #ifdef LOG
59259615Seric 					if (LogLevel > 2)
59359625Seric 						syslog(LOG_ERR, "dbopen(%s): %s",
59459625Seric 							spec, errstring(errno));
59559615Seric #endif
59651951Seric 					up->udb_type = UDB_EOLIST;
59751951Seric 					goto tempfail;
59851951Seric 				}
59951362Seric 				break;
60051923Seric 			}
60151951Seric 			up->udb_type = UDB_DBFETCH;
60251362Seric 			up++;
60351362Seric 			break;
60451360Seric 		}
60551362Seric 	}
60651362Seric 	up->udb_type = UDB_EOLIST;
60751360Seric 
60851362Seric 	if (tTd(28, 4))
60951362Seric 	{
61051951Seric 		for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
61151360Seric 		{
61251362Seric 			switch (up->udb_type)
61351362Seric 			{
61451362Seric 			  case UDB_REMOTE:
61551362Seric 				printf("REMOTE: addr %s, timeo %d\n",
61660494Seric 					anynet_ntoa((SOCKADDR *) &up->udb_addr),
61751362Seric 					up->udb_timeout);
61851362Seric 				break;
61951362Seric 
62051951Seric 			  case UDB_DBFETCH:
62151951Seric 				printf("FETCH: file %s\n",
62251830Seric 					up->udb_dbname);
62351362Seric 				break;
62451362Seric 
62551362Seric 			  case UDB_FORWARD:
62651362Seric 				printf("FORWARD: host %s\n",
62751362Seric 					up->udb_fwdhost);
62851362Seric 				break;
62951362Seric 
63051362Seric 			  default:
63151362Seric 				printf("UNKNOWN\n");
63251362Seric 				break;
63351362Seric 			}
63451360Seric 		}
63550581Seric 	}
63651951Seric 
63751951Seric 	UdbInitialized = TRUE;
63851955Seric 	errno = 0;
63951951Seric 	return EX_OK;
64051951Seric 
64151951Seric 	/*
64251951Seric 	**  On temporary failure, back out anything we've already done
64351951Seric 	*/
64451951Seric 
64551951Seric   tempfail:
64651951Seric 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
64751951Seric 	{
64851951Seric 		if (up->udb_type == UDB_DBFETCH)
64951951Seric 		{
65051951Seric 			(*up->udb_dbp->close)(up->udb_dbp);
65151951Seric 		}
65251951Seric 	}
65351951Seric 	return EX_TEMPFAIL;
65451360Seric }
65550581Seric 
65651363Seric int
65751363Seric _udb_parsespec(udbspec, opt, maxopts)
65851363Seric 	char *udbspec;
65951363Seric 	struct option opt[];
66051363Seric 	int maxopts;
66151363Seric {
66251363Seric 	register char *spec;
66351363Seric 	register char *spec_end;
66451363Seric 	register int optnum;
66551363Seric 
66656795Seric 	spec_end = strchr(udbspec, ':');
66751363Seric 	for (optnum = 0; optnum < maxopts && (spec = spec_end) != NULL; optnum++)
66851363Seric 	{
66951363Seric 		register char *p;
67051363Seric 
67158050Seric 		while (isascii(*spec) && isspace(*spec))
67251363Seric 			spec++;
67356795Seric 		spec_end = strchr(spec, ':');
67451363Seric 		if (spec_end != NULL)
67551363Seric 			*spec_end++ = '\0';
67651363Seric 
67751363Seric 		opt[optnum].name = spec;
67851363Seric 		opt[optnum].val = NULL;
67956795Seric 		p = strchr(spec, '=');
68051363Seric 		if (p != NULL)
68151363Seric 			opt[optnum].val = ++p;
68251363Seric 	}
68351363Seric 	return optnum;
68451363Seric }
68551363Seric 
68651360Seric #else /* not USERDB */
68751360Seric 
68851923Seric int
68955012Seric udbexpand(a, sendq, e)
69051360Seric 	ADDRESS *a;
69151360Seric 	ADDRESS **sendq;
69255012Seric 	ENVELOPE *e;
69351360Seric {
69451923Seric 	return EX_OK;
69550581Seric }
69650581Seric 
69750581Seric #endif /* USERDB */
698