xref: /csrg-svn/usr.sbin/sendmail/src/udb.c (revision 64067)
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*64067Seric static char sccsid [] = "@(#)udb.c	8.2 (Berkeley) 07/27/93 (with USERDB)";
1451360Seric #else
15*64067Seric static char sccsid [] = "@(#)udb.c	8.2 (Berkeley) 07/27/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))
174*64067Seric 				printf("udbexpand: trying %s (%d)\n",
175*64067Seric 					keybuf, keylen);
17651362Seric 			i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_CURSOR);
17751923Seric 			if (i > 0 || info.size <= 0)
17851360Seric 			{
17951360Seric 				if (tTd(28, 2))
180*64067Seric 					printf("udbexpand: no match on %s (%d)\n",
181*64067Seric 						keybuf, keylen);
18251360Seric 				continue;
18351360Seric 			}
18460990Seric 			if (tTd(28, 80))
18560990Seric 				printf("udbexpand: match %.*s: %.*s\n",
18660990Seric 					key.size, key.data, info.size, info.data);
18750581Seric 
18858082Seric 			naddrs = 0;
18958082Seric 			a->q_flags &= ~QSELFREF;
19051830Seric 			while (i == 0 && key.size == keylen &&
19151830Seric 					bcmp(key.data, keybuf, keylen) == 0)
19251362Seric 			{
19358099Seric 				if (bitset(EF_VRFYONLY, e->e_flags))
19458154Seric 				{
19558154Seric 					a->q_flags |= QVERIFIED;
19658884Seric 					e->e_nrcpts++;
19758099Seric 					return EX_OK;
19858154Seric 				}
19958099Seric 
20051830Seric 				breakout = TRUE;
20151362Seric 				if (info.size < sizeof buf)
20251362Seric 					user = buf;
20351362Seric 				else
20451362Seric 					user = xalloc(info.size + 1);
20551362Seric 				bcopy(info.data, user, info.size);
20651362Seric 				user[info.size] = '\0';
20750581Seric 
20858151Seric 				message("expanded to %s", user);
20957977Seric #ifdef LOG
21057977Seric 				if (LogLevel >= 10)
21157977Seric 					syslog(LOG_INFO, "%s: expand %s => %s",
21257977Seric 						e->e_id, e->e_to, user);
21357977Seric #endif
21451362Seric 				AliasLevel++;
21558082Seric 				naddrs += sendtolist(user, a, sendq, e);
21651362Seric 				AliasLevel--;
21751362Seric 
21851362Seric 				if (user != buf)
21951362Seric 					free(user);
22051362Seric 
22151362Seric 				/* get the next record */
22251362Seric 				i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_NEXT);
22351830Seric 			}
22460990Seric 
22560990Seric 			/* if nothing ever matched, try next database */
22660990Seric 			if (!breakout)
22760990Seric 				continue;
22860990Seric 
22958082Seric 			if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
23058065Seric 			{
23158065Seric 				if (tTd(28, 5))
23258065Seric 				{
23358065Seric 					printf("udbexpand: QDONTSEND ");
23458065Seric 					printaddr(a, FALSE);
23558065Seric 				}
23658065Seric 				a->q_flags |= QDONTSEND;
23758065Seric 			}
23851923Seric 			if (i < 0)
23951923Seric 			{
24058010Seric 				syserr("udbexpand: db-get %.*s stat %d",
24158010Seric 					key.size, key.data, i);
24251923Seric 				return EX_TEMPFAIL;
24351923Seric 			}
24459707Seric 
24559707Seric 			/*
24659707Seric 			**  If this address has a -request address, reflect
24759707Seric 			**  it into the envelope.
24859707Seric 			*/
24959707Seric 
25059707Seric 			(void) strcpy(keybuf, a->q_user);
25159707Seric 			(void) strcat(keybuf, ":mailsender");
25259707Seric 			keylen = strlen(keybuf);
25359707Seric 			key.data = keybuf;
25459707Seric 			key.size = keylen;
25559707Seric 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
25659707Seric 			if (i != 0 || info.size <= 0)
25759707Seric 				break;
25859707Seric 			a->q_owner = xalloc(info.size + 1);
25959707Seric 			bcopy(info.data, a->q_owner, info.size);
26059707Seric 			a->q_owner[info.size] = '\0';
26151360Seric 			break;
26251360Seric 
26351360Seric 		  case UDB_REMOTE:
26451741Seric 			/* not yet implemented */
26551741Seric 			continue;
26651362Seric 
26751360Seric 		  case UDB_FORWARD:
26858099Seric 			if (bitset(EF_VRFYONLY, e->e_flags))
26958099Seric 				return EX_OK;
27051360Seric 			i = strlen(up->udb_fwdhost) + strlen(a->q_user) + 1;
27151360Seric 			if (i < sizeof buf)
27251360Seric 				user = buf;
27351360Seric 			else
27451360Seric 				user = xalloc(i + 1);
27551360Seric 			(void) sprintf(user, "%s@%s", a->q_user, up->udb_fwdhost);
27658151Seric 			message("expanded to %s", user);
27758082Seric 			a->q_flags &= ~QSELFREF;
27851362Seric 			AliasLevel++;
27958082Seric 			naddrs = sendtolist(user, a, sendq, e);
28051362Seric 			AliasLevel--;
28158082Seric 			if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
28258065Seric 			{
28358065Seric 				if (tTd(28, 5))
28458065Seric 				{
28558065Seric 					printf("udbexpand: QDONTSEND ");
28658065Seric 					printaddr(a, FALSE);
28758065Seric 				}
28858065Seric 				a->q_flags |= QDONTSEND;
28958065Seric 			}
29051362Seric 			if (user != buf)
29151362Seric 				free(user);
29251362Seric 			breakout = TRUE;
29351360Seric 			break;
29451360Seric 
29551360Seric 		  case UDB_EOLIST:
29651360Seric 			breakout = TRUE;
29751360Seric 			continue;
29851360Seric 
29951360Seric 		  default:
30051360Seric 			/* unknown entry type */
30151360Seric 			continue;
30251360Seric 		}
30351362Seric 	}
30451923Seric 	return EX_OK;
30551362Seric }
30651951Seric /*
30751951Seric **  UDBSENDER -- return canonical external name of sender, given local name
30851951Seric **
30951951Seric **	Parameters:
31051951Seric **		sender -- the name of the sender on the local machine.
31151951Seric **
31251951Seric **	Returns:
31351951Seric **		The external name for this sender, if derivable from the
31451951Seric **			database.
31551951Seric **		NULL -- if nothing is changed from the database.
31651951Seric **
31751951Seric **	Side Effects:
31851951Seric **		none.
31951951Seric */
32051360Seric 
32151951Seric char *
32251951Seric udbsender(sender)
32351951Seric 	char *sender;
32451951Seric {
32551951Seric 	register char *p;
32651951Seric 	register struct udbent *up;
32751951Seric 	int i;
32851951Seric 	int keylen;
32951951Seric 	DBT key, info;
33057232Seric 	char keybuf[MAXKEY];
33151951Seric 
33251951Seric 	if (tTd(28, 1))
33351951Seric 		printf("udbsender(%s)\n", sender);
33451951Seric 
33551951Seric 	if (!UdbInitialized)
33651951Seric 	{
33751951Seric 		if (_udbx_init() == EX_TEMPFAIL)
33851951Seric 			return NULL;
33951951Seric 	}
34051951Seric 
34151951Seric 	/* short circuit if no spec */
34251951Seric 	if (UdbSpec == NULL || UdbSpec[0] == '\0')
34351951Seric 		return NULL;
34451951Seric 
34551951Seric 	/* long names can never match and are a pain to deal with */
34651951Seric 	if (strlen(sender) > sizeof keybuf - 12)
34751951Seric 		return NULL;
34851951Seric 
34951951Seric 	/* names beginning with colons indicate metadata */
35051951Seric 	if (sender[0] == ':')
35151951Seric 		return NULL;
35251951Seric 
35351951Seric 	/* build database key */
35451951Seric 	(void) strcpy(keybuf, sender);
35551951Seric 	(void) strcat(keybuf, ":mailname");
35651951Seric 	keylen = strlen(keybuf);
35751951Seric 
35851951Seric 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
35951951Seric 	{
36051951Seric 		/*
36151951Seric 		**  Select action based on entry type.
36251951Seric 		*/
36351951Seric 
36451951Seric 		switch (up->udb_type)
36551951Seric 		{
36651951Seric 		  case UDB_DBFETCH:
36751951Seric 			key.data = keybuf;
36851951Seric 			key.size = keylen;
36951951Seric 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
37051951Seric 			if (i != 0 || info.size <= 0)
37151951Seric 			{
37251951Seric 				if (tTd(28, 2))
373*64067Seric 					printf("udbsender: no match on %s (%d)\n",
374*64067Seric 							keybuf, keylen);
37551951Seric 				continue;
37651951Seric 			}
37751951Seric 
37851951Seric 			p = xalloc(info.size + 1);
37951951Seric 			bcopy(info.data, p, info.size);
38051951Seric 			p[info.size] = '\0';
38151951Seric 			if (tTd(28, 1))
38251951Seric 				printf("udbsender ==> %s\n", p);
38351951Seric 			return p;
38451951Seric 		}
38551951Seric 	}
38651951Seric 
38751951Seric 	/*
38851951Seric 	**  Nothing yet.  Search again for a default case.  But only
38951951Seric 	**  use it if we also have a forward (:maildrop) pointer already
39051951Seric 	**  in the database.
39151951Seric 	*/
39251951Seric 
39351951Seric 	/* build database key */
39451951Seric 	(void) strcpy(keybuf, sender);
39551951Seric 	(void) strcat(keybuf, ":maildrop");
39651951Seric 	keylen = strlen(keybuf);
39751951Seric 
39851951Seric 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
39951951Seric 	{
40051951Seric 		switch (up->udb_type)
40151951Seric 		{
40251951Seric 		  case UDB_DBFETCH:
40351951Seric 			/* get the default case for this database */
40451951Seric 			if (up->udb_default == NULL)
40551951Seric 			{
40651951Seric 				key.data = ":default:mailname";
40751951Seric 				key.size = strlen(key.data);
40851951Seric 				i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
40951951Seric 				if (i != 0 || info.size <= 0)
41051951Seric 				{
41151951Seric 					/* no default case */
41251951Seric 					up->udb_default = "";
41351951Seric 					continue;
41451951Seric 				}
41551951Seric 
41651951Seric 				/* save the default case */
41751951Seric 				up->udb_default = xalloc(info.size + 1);
41851951Seric 				bcopy(info.data, up->udb_default, info.size);
41951951Seric 				up->udb_default[info.size] = '\0';
42051951Seric 			}
42151951Seric 			else if (up->udb_default[0] == '\0')
42251951Seric 				continue;
42351951Seric 
42451951Seric 			/* we have a default case -- verify user:maildrop */
42551951Seric 			key.data = keybuf;
42651951Seric 			key.size = keylen;
42751951Seric 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
42851951Seric 			if (i != 0 || info.size <= 0)
42951951Seric 			{
43051951Seric 				/* nope -- no aliasing for this user */
43151951Seric 				continue;
43251951Seric 			}
43351951Seric 
43451951Seric 			/* they exist -- build the actual address */
43551951Seric 			p = xalloc(strlen(sender) + strlen(up->udb_default) + 2);
43651951Seric 			(void) strcpy(p, sender);
43751951Seric 			(void) strcat(p, "@");
43851951Seric 			(void) strcat(p, up->udb_default);
43951951Seric 			if (tTd(28, 1))
44051951Seric 				printf("udbsender ==> %s\n", p);
44151951Seric 			return p;
44251951Seric 		}
44351951Seric 	}
44451951Seric 
44551951Seric 	/* still nothing....  too bad */
44651951Seric 	return NULL;
44751951Seric }
44851951Seric /*
44951951Seric **  _UDBX_INIT -- parse the UDB specification, opening any valid entries.
45051951Seric **
45151951Seric **	Parameters:
45251951Seric **		none.
45351951Seric **
45451951Seric **	Returns:
45551951Seric **		EX_TEMPFAIL -- if it appeared it couldn't get hold of a
45651951Seric **			database due to a host being down or some similar
45751951Seric **			(recoverable) situation.
45851951Seric **		EX_OK -- otherwise.
45951951Seric **
46051951Seric **	Side Effects:
46151951Seric **		Fills in the UdbEnts structure from UdbSpec.
46251951Seric */
46351951Seric 
46451363Seric #define MAXUDBOPTS	27
46551363Seric 
46651953Seric int
46751362Seric _udbx_init()
46851362Seric {
46951362Seric 	register char *p;
47051362Seric 	int i;
47151362Seric 	register struct udbent *up;
47257232Seric 	char buf[BUFSIZ];
47351360Seric 
47451951Seric 	if (UdbInitialized)
47551951Seric 		return EX_OK;
47651951Seric 
47751908Seric # ifdef UDB_DEFAULT_SPEC
47851908Seric 	if (UdbSpec == NULL)
47951908Seric 		UdbSpec = UDB_DEFAULT_SPEC;
48051908Seric # endif
48151908Seric 
48251362Seric 	p = UdbSpec;
48351362Seric 	up = UdbEnts;
48451762Seric 	while (p != NULL)
48551362Seric 	{
48651362Seric 		char *spec;
48751362Seric 		auto int rcode;
48851363Seric 		int nopts;
48951362Seric 		int nmx;
49051362Seric 		register struct hostent *h;
49151362Seric 		char *mxhosts[MAXMXHOSTS + 1];
49251363Seric 		struct option opts[MAXUDBOPTS + 1];
49351362Seric 
49451362Seric 		while (*p == ' ' || *p == '\t' || *p == ',')
49551362Seric 			p++;
49651362Seric 		if (*p == '\0')
49751362Seric 			break;
49851362Seric 		spec = p;
49956795Seric 		p = strchr(p, ',');
50051761Seric 		if (p != NULL)
50151362Seric 			*p++ = '\0';
50251363Seric 
50351363Seric 		/* extract options */
50451363Seric 		nopts = _udb_parsespec(spec, opts, MAXUDBOPTS);
50551363Seric 
50651363Seric 		/*
50751363Seric 		**  Decode database specification.
50851363Seric 		**
50951363Seric 		**	In the sendmail tradition, the leading character
51051363Seric 		**	defines the semantics of the rest of the entry.
51151363Seric 		**
51251363Seric 		**	+hostname --	send a datagram to the udb server
51351363Seric 		**			on host "hostname" asking for the
51451363Seric 		**			home mail server for this user.
51551363Seric 		**	*hostname --	similar to +hostname, except that the
51651363Seric 		**			hostname is searched as an MX record;
51751363Seric 		**			resulting hosts are searched as for
51851363Seric 		**			+mxhostname.  If no MX host is found,
51951363Seric 		**			this is the same as +hostname.
52051363Seric 		**	@hostname --	forward email to the indicated host.
52151363Seric 		**			This should be the last in the list,
52251363Seric 		**			since it always matches the input.
52351363Seric 		**	/dbname	 --	search the named database on the local
52451363Seric 		**			host using the Berkeley db package.
52551363Seric 		*/
52651363Seric 
52751362Seric 		switch (*spec)
52851360Seric 		{
52951362Seric 		  case '+':	/* search remote database */
53051363Seric 		  case '*':	/* search remote database (expand MX) */
53151363Seric 			if (*spec == '*')
53251363Seric 			{
53357629Seric #ifdef NAMED_BIND
53459273Seric 				nmx = getmxrr(spec + 1, mxhosts, FALSE, &rcode);
53557629Seric #else
53657629Seric 				mxhosts[0] = spec + 1;
53757629Seric 				nmx = 1;
53857629Seric 				rcode = 0;
53957629Seric #endif
54051363Seric 				if (tTd(28, 16))
54151363Seric 				{
54251363Seric 					int i;
54351362Seric 
54451363Seric 					printf("getmxrr(%s): %d", spec + 1, nmx);
54551363Seric 					for (i = 0; i <= nmx; i++)
54651363Seric 						printf(" %s", mxhosts[i]);
54751363Seric 					printf("\n");
54851363Seric 				}
54951363Seric 			}
55051363Seric 			else
55151362Seric 			{
55251363Seric 				nmx = 1;
55351363Seric 				mxhosts[0] = spec + 1;
55451362Seric 			}
55551362Seric 
55651362Seric 			for (i = 0; i < nmx; i++)
55751362Seric 			{
55851362Seric 				h = gethostbyname(mxhosts[i]);
55951362Seric 				if (h == NULL)
56051362Seric 					continue;
56151362Seric 				up->udb_type = UDB_REMOTE;
56251362Seric 				up->udb_addr.sin_family = h->h_addrtype;
56351362Seric 				bcopy(h->h_addr_list[0],
56451362Seric 				      (char *) &up->udb_addr.sin_addr,
56551362Seric 				      h->h_length);
56651362Seric 				up->udb_addr.sin_port = UdbPort;
56751362Seric 				up->udb_timeout = UdbTimeout;
56851362Seric 				up++;
56951362Seric 			}
57051362Seric 
57151362Seric 			/* set up a datagram socket */
57251362Seric 			if (UdbSock < 0)
57351362Seric 			{
57451362Seric 				UdbSock = socket(AF_INET, SOCK_DGRAM, 0);
57551362Seric 				(void) fcntl(UdbSock, F_SETFD, 1);
57651362Seric 			}
57751362Seric 			break;
57851362Seric 
57951362Seric 		  case '@':	/* forward to remote host */
58051362Seric 			up->udb_type = UDB_FORWARD;
58151362Seric 			up->udb_fwdhost = spec + 1;
58251362Seric 			up++;
58351362Seric 			break;
58451362Seric 
58551362Seric 		  case '/':	/* look up remote name */
58651831Seric 			up->udb_dbname = spec;
58751923Seric 			errno = 0;
58851362Seric 			up->udb_dbp = dbopen(spec, O_RDONLY, 0644, DB_BTREE, NULL);
58951362Seric 			if (up->udb_dbp == NULL)
59051923Seric 			{
59151923Seric 				if (errno != ENOENT && errno != EACCES)
59251951Seric 				{
59359615Seric #ifdef LOG
59459615Seric 					if (LogLevel > 2)
59559625Seric 						syslog(LOG_ERR, "dbopen(%s): %s",
59659625Seric 							spec, errstring(errno));
59759615Seric #endif
59851951Seric 					up->udb_type = UDB_EOLIST;
59951951Seric 					goto tempfail;
60051951Seric 				}
60151362Seric 				break;
60251923Seric 			}
60351951Seric 			up->udb_type = UDB_DBFETCH;
60451362Seric 			up++;
60551362Seric 			break;
60651360Seric 		}
60751362Seric 	}
60851362Seric 	up->udb_type = UDB_EOLIST;
60951360Seric 
61051362Seric 	if (tTd(28, 4))
61151362Seric 	{
61251951Seric 		for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
61351360Seric 		{
61451362Seric 			switch (up->udb_type)
61551362Seric 			{
61651362Seric 			  case UDB_REMOTE:
61751362Seric 				printf("REMOTE: addr %s, timeo %d\n",
61860494Seric 					anynet_ntoa((SOCKADDR *) &up->udb_addr),
61951362Seric 					up->udb_timeout);
62051362Seric 				break;
62151362Seric 
62251951Seric 			  case UDB_DBFETCH:
62351951Seric 				printf("FETCH: file %s\n",
62451830Seric 					up->udb_dbname);
62551362Seric 				break;
62651362Seric 
62751362Seric 			  case UDB_FORWARD:
62851362Seric 				printf("FORWARD: host %s\n",
62951362Seric 					up->udb_fwdhost);
63051362Seric 				break;
63151362Seric 
63251362Seric 			  default:
63351362Seric 				printf("UNKNOWN\n");
63451362Seric 				break;
63551362Seric 			}
63651360Seric 		}
63750581Seric 	}
63851951Seric 
63951951Seric 	UdbInitialized = TRUE;
64051955Seric 	errno = 0;
64151951Seric 	return EX_OK;
64251951Seric 
64351951Seric 	/*
64451951Seric 	**  On temporary failure, back out anything we've already done
64551951Seric 	*/
64651951Seric 
64751951Seric   tempfail:
64851951Seric 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
64951951Seric 	{
65051951Seric 		if (up->udb_type == UDB_DBFETCH)
65151951Seric 		{
65251951Seric 			(*up->udb_dbp->close)(up->udb_dbp);
65351951Seric 		}
65451951Seric 	}
65551951Seric 	return EX_TEMPFAIL;
65651360Seric }
65750581Seric 
65851363Seric int
65951363Seric _udb_parsespec(udbspec, opt, maxopts)
66051363Seric 	char *udbspec;
66151363Seric 	struct option opt[];
66251363Seric 	int maxopts;
66351363Seric {
66451363Seric 	register char *spec;
66551363Seric 	register char *spec_end;
66651363Seric 	register int optnum;
66751363Seric 
66856795Seric 	spec_end = strchr(udbspec, ':');
66951363Seric 	for (optnum = 0; optnum < maxopts && (spec = spec_end) != NULL; optnum++)
67051363Seric 	{
67151363Seric 		register char *p;
67251363Seric 
67358050Seric 		while (isascii(*spec) && isspace(*spec))
67451363Seric 			spec++;
67556795Seric 		spec_end = strchr(spec, ':');
67651363Seric 		if (spec_end != NULL)
67751363Seric 			*spec_end++ = '\0';
67851363Seric 
67951363Seric 		opt[optnum].name = spec;
68051363Seric 		opt[optnum].val = NULL;
68156795Seric 		p = strchr(spec, '=');
68251363Seric 		if (p != NULL)
68351363Seric 			opt[optnum].val = ++p;
68451363Seric 	}
68551363Seric 	return optnum;
68651363Seric }
68751363Seric 
68851360Seric #else /* not USERDB */
68951360Seric 
69051923Seric int
69155012Seric udbexpand(a, sendq, e)
69251360Seric 	ADDRESS *a;
69351360Seric 	ADDRESS **sendq;
69455012Seric 	ENVELOPE *e;
69551360Seric {
69651923Seric 	return EX_OK;
69750581Seric }
69850581Seric 
69950581Seric #endif /* USERDB */
700