xref: /original-bsd/usr.sbin/sendmail/src/udb.c (revision 0997b878)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #include "sendmail.h"
10 
11 #ifndef lint
12 #ifdef USERDB
13 static char sccsid [] = "@(#)udb.c	8.12 (Berkeley) 11/22/94 (with USERDB)";
14 #else
15 static char sccsid [] = "@(#)udb.c	8.12 (Berkeley) 11/22/94 (without USERDB)";
16 #endif
17 #endif
18 
19 #ifdef USERDB
20 
21 #include <errno.h>
22 #include <netdb.h>
23 #include <db.h>
24 
25 #ifdef HESIOD
26 #include <hesiod.h>
27 #endif /* HESIOD */
28 
29 /*
30 **  UDB.C -- interface between sendmail and Berkeley User Data Base.
31 **
32 **	This depends on the 4.4BSD db package.
33 */
34 
35 
36 struct udbent
37 {
38 	char	*udb_spec;		/* string version of spec */
39 	int	udb_type;		/* type of entry */
40 	char	*udb_default;		/* default host for outgoing mail */
41 	union
42 	{
43 		/* type UE_REMOTE -- do remote call for lookup */
44 		struct
45 		{
46 			struct sockaddr_in _udb_addr;	/* address */
47 			int		_udb_timeout;	/* timeout */
48 		} udb_remote;
49 #define udb_addr	udb_u.udb_remote._udb_addr
50 #define udb_timeout	udb_u.udb_remote._udb_timeout
51 
52 		/* type UE_FORWARD -- forward message to remote */
53 		struct
54 		{
55 			char	*_udb_fwdhost;	/* name of forward host */
56 		} udb_forward;
57 #define udb_fwdhost	udb_u.udb_forward._udb_fwdhost
58 
59 		/* type UE_FETCH -- lookup in local database */
60 		struct
61 		{
62 			char	*_udb_dbname;	/* pathname of database */
63 			DB	*_udb_dbp;	/* open database ptr */
64 		} udb_lookup;
65 #define udb_dbname	udb_u.udb_lookup._udb_dbname
66 #define udb_dbp		udb_u.udb_lookup._udb_dbp
67 	} udb_u;
68 };
69 
70 #define UDB_EOLIST	0	/* end of list */
71 #define UDB_SKIP	1	/* skip this entry */
72 #define UDB_REMOTE	2	/* look up in remote database */
73 #define UDB_DBFETCH	3	/* look up in local database */
74 #define UDB_FORWARD	4	/* forward to remote host */
75 #define UDB_HESIOD	5	/* look up via hesiod */
76 
77 #define MAXUDBENT	10	/* maximum number of UDB entries */
78 
79 
80 struct option
81 {
82 	char	*name;
83 	char	*val;
84 };
85 /*
86 **  UDBEXPAND -- look up user in database and expand
87 **
88 **	Parameters:
89 **		a -- address to expand.
90 **		sendq -- pointer to head of sendq to put the expansions in.
91 **		aliaslevel -- the current alias nesting depth.
92 **		e -- the current envelope.
93 **
94 **	Returns:
95 **		EX_TEMPFAIL -- if something "odd" happened -- probably due
96 **			to accessing a file on an NFS server that is down.
97 **		EX_OK -- otherwise.
98 **
99 **	Side Effects:
100 **		Modifies sendq.
101 */
102 
103 int	UdbPort = 1616;
104 int	UdbTimeout = 10;
105 
106 struct udbent	UdbEnts[MAXUDBENT + 1];
107 int		UdbSock = -1;
108 bool		UdbInitialized = FALSE;
109 
110 int
111 udbexpand(a, sendq, aliaslevel, e)
112 	register ADDRESS *a;
113 	ADDRESS **sendq;
114 	int aliaslevel;
115 	register ENVELOPE *e;
116 {
117 	int i;
118 	register char *p;
119 	DBT key;
120 	DBT info;
121 	bool breakout;
122 	register struct udbent *up;
123 	int keylen;
124 	int naddrs;
125 	char keybuf[MAXKEY];
126 	char buf[BUFSIZ];
127 
128 	if (tTd(28, 1))
129 		printf("udbexpand(%s)\n", a->q_paddr);
130 
131 	/* make certain we are supposed to send to this address */
132 	if (bitset(QDONTSEND|QVERIFIED, a->q_flags))
133 		return EX_OK;
134 	e->e_to = a->q_paddr;
135 
136 	/* on first call, locate the database */
137 	if (!UdbInitialized)
138 	{
139 		extern int _udbx_init();
140 
141 		if (_udbx_init() == EX_TEMPFAIL)
142 			return EX_TEMPFAIL;
143 	}
144 
145 	/* short circuit the process if no chance of a match */
146 	if (UdbSpec == NULL || UdbSpec[0] == '\0')
147 		return EX_OK;
148 
149 	/* short circuit name begins with '\\' since it can't possibly match */
150 	if (a->q_user[0] == '\\')
151 		return EX_OK;
152 
153 	/* if name is too long, assume it won't match */
154 	if (strlen(a->q_user) > sizeof keybuf - 12)
155 		return EX_OK;
156 
157 	/* if name begins with a colon, it indicates our metadata */
158 	if (a->q_user[0] == ':')
159 		return EX_OK;
160 
161 	/* build actual database key */
162 	(void) strcpy(keybuf, a->q_user);
163 	(void) strcat(keybuf, ":maildrop");
164 	keylen = strlen(keybuf);
165 
166 	breakout = FALSE;
167 	for (up = UdbEnts; !breakout; up++)
168 	{
169 		char *user;
170 
171 		/*
172 		**  Select action based on entry type.
173 		**
174 		**	On dropping out of this switch, "class" should
175 		**	explain the type of the data, and "user" should
176 		**	contain the user information.
177 		*/
178 
179 		switch (up->udb_type)
180 		{
181 		  case UDB_DBFETCH:
182 			key.data = keybuf;
183 			key.size = keylen;
184 			if (tTd(28, 80))
185 				printf("udbexpand: trying %s (%d) via db\n",
186 					keybuf, keylen);
187 			i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_CURSOR);
188 			if (i > 0 || info.size <= 0)
189 			{
190 				if (tTd(28, 2))
191 					printf("udbexpand: no match on %s (%d)\n",
192 						keybuf, keylen);
193 				continue;
194 			}
195 			if (tTd(28, 80))
196 				printf("udbexpand: match %.*s: %.*s\n",
197 					key.size, key.data, info.size, info.data);
198 
199 			naddrs = 0;
200 			a->q_flags &= ~QSELFREF;
201 			while (i == 0 && key.size == keylen &&
202 					bcmp(key.data, keybuf, keylen) == 0)
203 			{
204 				if (bitset(EF_VRFYONLY, e->e_flags))
205 				{
206 					a->q_flags |= QVERIFIED;
207 					e->e_nrcpts++;
208 					return EX_OK;
209 				}
210 
211 				breakout = TRUE;
212 				if (info.size < sizeof buf)
213 					user = buf;
214 				else
215 					user = xalloc(info.size + 1);
216 				bcopy(info.data, user, info.size);
217 				user[info.size] = '\0';
218 
219 				message("expanded to %s", user);
220 #ifdef LOG
221 				if (LogLevel >= 10)
222 					syslog(LOG_INFO, "%s: expand %s => %s",
223 						e->e_id, e->e_to, user);
224 #endif
225 				naddrs += sendtolist(user, a, sendq, aliaslevel + 1, e);
226 
227 				if (user != buf)
228 					free(user);
229 
230 				/* get the next record */
231 				i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_NEXT);
232 			}
233 
234 			/* if nothing ever matched, try next database */
235 			if (!breakout)
236 				continue;
237 
238 			if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
239 			{
240 				if (tTd(28, 5))
241 				{
242 					printf("udbexpand: QDONTSEND ");
243 					printaddr(a, FALSE);
244 				}
245 				a->q_flags |= QDONTSEND;
246 			}
247 			if (i < 0)
248 			{
249 				syserr("udbexpand: db-get %.*s stat %d",
250 					key.size, key.data, i);
251 				return EX_TEMPFAIL;
252 			}
253 
254 			/*
255 			**  If this address has a -request address, reflect
256 			**  it into the envelope.
257 			*/
258 
259 			(void) strcpy(keybuf, a->q_user);
260 			(void) strcat(keybuf, ":mailsender");
261 			keylen = strlen(keybuf);
262 			key.data = keybuf;
263 			key.size = keylen;
264 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
265 			if (i != 0 || info.size <= 0)
266 				break;
267 			a->q_owner = xalloc(info.size + 1);
268 			bcopy(info.data, a->q_owner, info.size);
269 			a->q_owner[info.size] = '\0';
270 
271 			/* announce delivery; NORECEIPT bit set later */
272 			if (e->e_xfp != NULL)
273 			{
274 				fprintf(e->e_xfp,
275 					"Message delivered to mailing list %s\n",
276 					a->q_paddr);
277 				e->e_flags |= EF_SENDRECEIPT;
278 			}
279 			break;
280 
281 #ifdef HESIOD
282 		  case UDB_HESIOD:
283 			key.data = keybuf;
284 			key.size = keylen;
285 			if (tTd(28, 80))
286 				printf("udbexpand: trying %s (%d) via hesiod\n",
287 					keybuf, keylen);
288 			/* look up the key via hesiod */
289 			i = hes_udb_get(&key, &info);
290 			if (i > 0 || info.size <= 0)
291 			{
292 				if (tTd(28, 2))
293 				printf("udbexpand: no match on %s (%d)\n",
294 					keybuf, keylen);
295 				continue;
296 			}
297 			if (tTd(28, 80))
298 				printf("udbexpand: match %.*s: %.*s\n",
299 					key.size, key.data, info.size, info.data);
300 			a->q_flags &= ~QSELFREF;
301 
302 			if (bitset(EF_VRFYONLY, e->e_flags))
303 			{
304 				a->q_flags |= QVERIFIED;
305 				e->e_nrcpts++;
306 				free(info.data);
307 				return EX_OK;
308 			}
309 
310 			breakout = TRUE;
311 			if (info.size < sizeof buf)
312 				user = buf;
313 			else
314 				user = xalloc(info.size + 1);
315 			bcopy(info.data, user, info.size);
316 			user[info.size] = '\0';
317 			free(info.data);
318 
319 			message("hesioded to %s", user);
320 #ifdef LOG
321 			if (LogLevel >= 10)
322 				syslog(LOG_INFO, "%s: hesiod %s => %s",
323 					e->e_id, e->e_to, user);
324 #endif
325 			naddrs = sendtolist(user, a, sendq, aliaslevel + 1, e);
326 
327 			if (user != buf)
328 				free(user);
329 
330 			if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
331 			{
332 				if (tTd(28, 5))
333 				{
334 					printf("udbexpand: QDONTSEND ");
335 					printaddr(a, FALSE);
336 				}
337 				a->q_flags |= QDONTSEND;
338 			}
339 			if (i < 0)
340 			{
341 				syserr("udbexpand: hesiod-get %.*s stat %d",
342 					key.size, key.data, i);
343 				return EX_TEMPFAIL;
344 			}
345 
346 			/*
347 			**  If this address has a -request address, reflect
348 			**  it into the envelope.
349 			*/
350 
351 			(void) strcpy(keybuf, a->q_user);
352 			(void) strcat(keybuf, ":mailsender");
353 			keylen = strlen(keybuf);
354 			key.data = keybuf;
355 			key.size = keylen;
356 			i = hes_udb_get(&key, &info);
357 			if (i != 0 || info.size <= 0)
358 				break;
359 			a->q_owner = xalloc(info.size + 1);
360 			bcopy(info.data, a->q_owner, info.size);
361 			a->q_owner[info.size] = '\0';
362 			free(info.data);
363 			break;
364 #endif /* HESIOD */
365 
366 		  case UDB_REMOTE:
367 			/* not yet implemented */
368 			continue;
369 
370 		  case UDB_FORWARD:
371 			if (bitset(EF_VRFYONLY, e->e_flags))
372 				return EX_OK;
373 			i = strlen(up->udb_fwdhost) + strlen(a->q_user) + 1;
374 			if (i < sizeof buf)
375 				user = buf;
376 			else
377 				user = xalloc(i + 1);
378 			(void) sprintf(user, "%s@%s", a->q_user, up->udb_fwdhost);
379 			message("expanded to %s", user);
380 			a->q_flags &= ~QSELFREF;
381 			naddrs = sendtolist(user, a, sendq, aliaslevel + 1, e);
382 			if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
383 			{
384 				if (tTd(28, 5))
385 				{
386 					printf("udbexpand: QDONTSEND ");
387 					printaddr(a, FALSE);
388 				}
389 				a->q_flags |= QDONTSEND;
390 			}
391 			if (user != buf)
392 				free(user);
393 			breakout = TRUE;
394 			break;
395 
396 		  case UDB_EOLIST:
397 			breakout = TRUE;
398 			continue;
399 
400 		  default:
401 			/* unknown entry type */
402 			continue;
403 		}
404 	}
405 	return EX_OK;
406 }
407 /*
408 **  UDBSENDER -- return canonical external name of sender, given local name
409 **
410 **	Parameters:
411 **		sender -- the name of the sender on the local machine.
412 **
413 **	Returns:
414 **		The external name for this sender, if derivable from the
415 **			database.
416 **		NULL -- if nothing is changed from the database.
417 **
418 **	Side Effects:
419 **		none.
420 */
421 
422 char *
423 udbsender(sender)
424 	char *sender;
425 {
426 	extern char *udbmatch();
427 
428 	return udbmatch(sender, "mailname");
429 }
430 
431 
432 char *
433 udbmatch(user, field)
434 	char *user;
435 	char *field;
436 {
437 	register char *p;
438 	register struct udbent *up;
439 	int i;
440 	int keylen;
441 	DBT key, info;
442 	char keybuf[MAXKEY];
443 
444 	if (tTd(28, 1))
445 		printf("udbmatch(%s, %s)\n", user, field);
446 
447 	if (!UdbInitialized)
448 	{
449 		if (_udbx_init() == EX_TEMPFAIL)
450 			return NULL;
451 	}
452 
453 	/* short circuit if no spec */
454 	if (UdbSpec == NULL || UdbSpec[0] == '\0')
455 		return NULL;
456 
457 	/* short circuit name begins with '\\' since it can't possibly match */
458 	if (user[0] == '\\')
459 		return NULL;
460 
461 	/* long names can never match and are a pain to deal with */
462 	if ((strlen(user) + strlen(field)) > sizeof keybuf - 4)
463 		return NULL;
464 
465 	/* names beginning with colons indicate metadata */
466 	if (user[0] == ':')
467 		return NULL;
468 
469 	/* build database key */
470 	(void) strcpy(keybuf, user);
471 	(void) strcat(keybuf, ":");
472 	(void) strcat(keybuf, field);
473 	keylen = strlen(keybuf);
474 
475 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
476 	{
477 		/*
478 		**  Select action based on entry type.
479 		*/
480 
481 		switch (up->udb_type)
482 		{
483 		  case UDB_DBFETCH:
484 			key.data = keybuf;
485 			key.size = keylen;
486 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
487 			if (i != 0 || info.size <= 0)
488 			{
489 				if (tTd(28, 2))
490 					printf("udbmatch: no match on %s (%d) via db\n",
491 							keybuf, keylen);
492 				continue;
493 			}
494 
495 			p = xalloc(info.size + 1);
496 			bcopy(info.data, p, info.size);
497 			p[info.size] = '\0';
498 			if (tTd(28, 1))
499 				printf("udbmatch ==> %s\n", p);
500 			return p;
501 			break;
502 
503 #ifdef HESIOD
504 		  case UDB_HESIOD:
505 			key.data = keybuf;
506 			key.size = keylen;
507 			i = hes_udb_get(&key, &info);
508 			if (i != 0 || info.size <= 0)
509 			{
510 				if (tTd(28, 2))
511 					printf("udbmatch: no match on %s (%d) via hesiod\n",
512 							keybuf, keylen);
513 				continue;
514 			}
515 
516 			p = xalloc(info.size + 1);
517 			bcopy(info.data, p, info.size);
518 			p[info.size] = '\0';
519 			free(info.data);
520 			if (tTd(28, 1))
521 				printf("udbmatch ==> %s\n", p);
522 			return p;
523 			break;
524 #endif /* HESIOD */
525 		}
526 	}
527 
528 	if (strcmp(field, "mailname") != 0)
529 		return NULL;
530 
531 	/*
532 	**  Nothing yet.  Search again for a default case.  But only
533 	**  use it if we also have a forward (:maildrop) pointer already
534 	**  in the database.
535 	*/
536 
537 	/* build database key */
538 	(void) strcpy(keybuf, user);
539 	(void) strcat(keybuf, ":maildrop");
540 	keylen = strlen(keybuf);
541 
542 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
543 	{
544 		switch (up->udb_type)
545 		{
546 		  case UDB_DBFETCH:
547 			/* get the default case for this database */
548 			if (up->udb_default == NULL)
549 			{
550 				key.data = ":default:mailname";
551 				key.size = strlen(key.data);
552 				i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
553 				if (i != 0 || info.size <= 0)
554 				{
555 					/* no default case */
556 					up->udb_default = "";
557 					continue;
558 				}
559 
560 				/* save the default case */
561 				up->udb_default = xalloc(info.size + 1);
562 				bcopy(info.data, up->udb_default, info.size);
563 				up->udb_default[info.size] = '\0';
564 			}
565 			else if (up->udb_default[0] == '\0')
566 				continue;
567 
568 			/* we have a default case -- verify user:maildrop */
569 			key.data = keybuf;
570 			key.size = keylen;
571 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
572 			if (i != 0 || info.size <= 0)
573 			{
574 				/* nope -- no aliasing for this user */
575 				continue;
576 			}
577 
578 			/* they exist -- build the actual address */
579 			p = xalloc(strlen(user) + strlen(up->udb_default) + 2);
580 			(void) strcpy(p, user);
581 			(void) strcat(p, "@");
582 			(void) strcat(p, up->udb_default);
583 			if (tTd(28, 1))
584 				printf("udbmatch ==> %s\n", p);
585 			return p;
586 			break;
587 
588 #ifdef HESIOD
589 		  case UDB_HESIOD:
590 			/* get the default case for this database */
591 			if (up->udb_default == NULL)
592 			{
593 				key.data = ":default:mailname";
594 				key.size = strlen(key.data);
595 				i = hes_udb_get(&key, &info);
596 
597 				if (i != 0 || info.size <= 0)
598 				{
599 					/* no default case */
600 					up->udb_default = "";
601 					continue;
602 				}
603 
604 				/* save the default case */
605 				up->udb_default = xalloc(info.size + 1);
606 				bcopy(info.data, up->udb_default, info.size);
607 				up->udb_default[info.size] = '\0';
608 				free(info.data);
609 			}
610 			else if (up->udb_default[0] == '\0')
611 				continue;
612 
613 			/* we have a default case -- verify user:maildrop */
614 			key.data = keybuf;
615 			key.size = keylen;
616 			i = hes_udb_get(&key, &info);
617 			if (i != 0 || info.size <= 0)
618 			{
619 				/* nope -- no aliasing for this user */
620 				continue;
621 			}
622 
623 			free(info.data);
624 			/* they exist -- build the actual address */
625 			p = xalloc(strlen(user) + strlen(up->udb_default) + 2);
626 			(void) strcpy(p, user);
627 			(void) strcat(p, "@");
628 			(void) strcat(p, up->udb_default);
629 			if (tTd(28, 1))
630 				printf("udbmatch ==> %s\n", p);
631 			return p;
632 			break;
633 #endif /* HESIOD */
634 		}
635 	}
636 
637 	/* still nothing....  too bad */
638 	return NULL;
639 }
640 /*
641 **  _UDBX_INIT -- parse the UDB specification, opening any valid entries.
642 **
643 **	Parameters:
644 **		none.
645 **
646 **	Returns:
647 **		EX_TEMPFAIL -- if it appeared it couldn't get hold of a
648 **			database due to a host being down or some similar
649 **			(recoverable) situation.
650 **		EX_OK -- otherwise.
651 **
652 **	Side Effects:
653 **		Fills in the UdbEnts structure from UdbSpec.
654 */
655 
656 #define MAXUDBOPTS	27
657 
658 int
659 _udbx_init()
660 {
661 	register char *p;
662 	int i;
663 	register struct udbent *up;
664 	char buf[BUFSIZ];
665 
666 	if (UdbInitialized)
667 		return EX_OK;
668 
669 # ifdef UDB_DEFAULT_SPEC
670 	if (UdbSpec == NULL)
671 		UdbSpec = UDB_DEFAULT_SPEC;
672 # endif
673 
674 	p = UdbSpec;
675 	up = UdbEnts;
676 	while (p != NULL)
677 	{
678 		char *spec;
679 		auto int rcode;
680 		int nopts;
681 		int nmx;
682 		register struct hostent *h;
683 		char *mxhosts[MAXMXHOSTS + 1];
684 		struct option opts[MAXUDBOPTS + 1];
685 
686 		while (*p == ' ' || *p == '\t' || *p == ',')
687 			p++;
688 		if (*p == '\0')
689 			break;
690 		spec = p;
691 		p = strchr(p, ',');
692 		if (p != NULL)
693 			*p++ = '\0';
694 
695 		/* extract options */
696 		nopts = _udb_parsespec(spec, opts, MAXUDBOPTS);
697 
698 		/*
699 		**  Decode database specification.
700 		**
701 		**	In the sendmail tradition, the leading character
702 		**	defines the semantics of the rest of the entry.
703 		**
704 		**	+hostname --	send a datagram to the udb server
705 		**			on host "hostname" asking for the
706 		**			home mail server for this user.
707 		**	*hostname --	similar to +hostname, except that the
708 		**			hostname is searched as an MX record;
709 		**			resulting hosts are searched as for
710 		**			+mxhostname.  If no MX host is found,
711 		**			this is the same as +hostname.
712 		**	@hostname --	forward email to the indicated host.
713 		**			This should be the last in the list,
714 		**			since it always matches the input.
715 		**	/dbname	 --	search the named database on the local
716 		**			host using the Berkeley db package.
717 		*/
718 
719 		switch (*spec)
720 		{
721 		  case '+':	/* search remote database */
722 		  case '*':	/* search remote database (expand MX) */
723 			if (*spec == '*')
724 			{
725 #if NAMED_BIND
726 				nmx = getmxrr(spec + 1, mxhosts, FALSE, &rcode);
727 #else
728 				mxhosts[0] = spec + 1;
729 				nmx = 1;
730 				rcode = 0;
731 #endif
732 				if (tTd(28, 16))
733 				{
734 					int i;
735 
736 					printf("getmxrr(%s): %d", spec + 1, nmx);
737 					for (i = 0; i <= nmx; i++)
738 						printf(" %s", mxhosts[i]);
739 					printf("\n");
740 				}
741 			}
742 			else
743 			{
744 				nmx = 1;
745 				mxhosts[0] = spec + 1;
746 			}
747 
748 			for (i = 0; i < nmx; i++)
749 			{
750 				h = gethostbyname(mxhosts[i]);
751 				if (h == NULL)
752 					continue;
753 				up->udb_type = UDB_REMOTE;
754 				up->udb_addr.sin_family = h->h_addrtype;
755 				bcopy(h->h_addr_list[0],
756 				      (char *) &up->udb_addr.sin_addr,
757 				      INADDRSZ);
758 				up->udb_addr.sin_port = UdbPort;
759 				up->udb_timeout = UdbTimeout;
760 				up++;
761 			}
762 
763 			/* set up a datagram socket */
764 			if (UdbSock < 0)
765 			{
766 				UdbSock = socket(AF_INET, SOCK_DGRAM, 0);
767 				(void) fcntl(UdbSock, F_SETFD, 1);
768 			}
769 			break;
770 
771 		  case '@':	/* forward to remote host */
772 			up->udb_type = UDB_FORWARD;
773 			up->udb_fwdhost = spec + 1;
774 			up++;
775 			break;
776 
777 		  case 'h':	/* use hesiod */
778 		  case 'H':
779 #ifdef HESIOD
780 			if (strcasecmp(spec, "hesiod") != 0)
781 				break;
782 			up->udb_type = UDB_HESIOD;
783 			up++;
784 #endif /* HESIOD */
785 			break;
786 
787 		  case '/':	/* look up remote name */
788 			up->udb_dbname = spec;
789 			errno = 0;
790 			up->udb_dbp = dbopen(spec, O_RDONLY, 0644, DB_BTREE, NULL);
791 			if (up->udb_dbp == NULL)
792 			{
793 				if (tTd(28, 1))
794 				{
795 					int saveerrno = errno;
796 
797 					printf("dbopen(%s): %s",
798 						spec, errstring(errno));
799 					errno = saveerrno;
800 				}
801 				if (errno != ENOENT && errno != EACCES)
802 				{
803 #ifdef LOG
804 					if (LogLevel > 2)
805 						syslog(LOG_ERR, "dbopen(%s): %s",
806 							spec, errstring(errno));
807 #endif
808 					up->udb_type = UDB_EOLIST;
809 					goto tempfail;
810 				}
811 				break;
812 			}
813 			up->udb_type = UDB_DBFETCH;
814 			up++;
815 			break;
816 		}
817 	}
818 	up->udb_type = UDB_EOLIST;
819 
820 	if (tTd(28, 4))
821 	{
822 		for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
823 		{
824 			switch (up->udb_type)
825 			{
826 			  case UDB_REMOTE:
827 				printf("REMOTE: addr %s, timeo %d\n",
828 					anynet_ntoa((SOCKADDR *) &up->udb_addr),
829 					up->udb_timeout);
830 				break;
831 
832 			  case UDB_DBFETCH:
833 				printf("FETCH: file %s\n",
834 					up->udb_dbname);
835 				break;
836 
837 			  case UDB_FORWARD:
838 				printf("FORWARD: host %s\n",
839 					up->udb_fwdhost);
840 				break;
841 
842 			  case UDB_HESIOD:
843 				printf("HESIOD\n");
844 				break;
845 
846 			  default:
847 				printf("UNKNOWN\n");
848 				break;
849 			}
850 		}
851 	}
852 
853 	UdbInitialized = TRUE;
854 	errno = 0;
855 	return EX_OK;
856 
857 	/*
858 	**  On temporary failure, back out anything we've already done
859 	*/
860 
861   tempfail:
862 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
863 	{
864 		if (up->udb_type == UDB_DBFETCH)
865 		{
866 			(*up->udb_dbp->close)(up->udb_dbp);
867 		}
868 	}
869 	return EX_TEMPFAIL;
870 }
871 
872 int
873 _udb_parsespec(udbspec, opt, maxopts)
874 	char *udbspec;
875 	struct option opt[];
876 	int maxopts;
877 {
878 	register char *spec;
879 	register char *spec_end;
880 	register int optnum;
881 
882 	spec_end = strchr(udbspec, ':');
883 	for (optnum = 0; optnum < maxopts && (spec = spec_end) != NULL; optnum++)
884 	{
885 		register char *p;
886 
887 		while (isascii(*spec) && isspace(*spec))
888 			spec++;
889 		spec_end = strchr(spec, ':');
890 		if (spec_end != NULL)
891 			*spec_end++ = '\0';
892 
893 		opt[optnum].name = spec;
894 		opt[optnum].val = NULL;
895 		p = strchr(spec, '=');
896 		if (p != NULL)
897 			opt[optnum].val = ++p;
898 	}
899 	return optnum;
900 }
901 
902 #ifdef HESIOD
903 
904 int
905 hes_udb_get(key, info)
906 	DBT *key;
907 	DBT *info;
908 {
909 	char *name, *type;
910 	char *p, **hp;
911 
912 	name = key->data;
913 	type = strchr(name, ':');
914 	if (type == NULL)
915 		return 1;
916 
917 	*type++ = '\0';
918 
919 	if (tTd(28, 1))
920 		printf("hes_udb_get(%s, %s)\n", name, type);
921 
922 	/* make the hesiod query */
923 	hp = hes_resolve(name, type);
924 	if (hp == NULL)
925 	{
926 		/* network problem or timeout */
927 		if (hes_error() == HES_ER_NET)
928 			return -1;
929 
930 		return 1;
931 	}
932 	else
933 	{
934 		/*
935 		**  If there are multiple matches, just return the
936 		**  first one and free the others.
937 		**
938 		**  XXX These should really be returned; for example,
939 		**  XXX it is legal for :maildrop to be multi-valued.
940 		*/
941 
942 		for (p = hp[1]; p; p++)
943 			free(p);
944 
945 		info->data = hp[0];
946 		info->size = (size_t) strlen(info->data);
947 	}
948 
949 	return 0;
950 }
951 #endif /* HESIOD */
952 
953 #else /* not USERDB */
954 
955 int
956 udbexpand(a, sendq, aliaslevel, e)
957 	ADDRESS *a;
958 	ADDRESS **sendq;
959 	int aliaslevel;
960 	ENVELOPE *e;
961 {
962 	return EX_OK;
963 }
964 
965 #endif /* USERDB */
966