2 * Copyright (c) 1998-2003, 2006 Proofpoint, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
18 SM_RCSID("@(#)$Id: udb.c,v 8.166 2013-11-22 20:51:57 ca Exp $ (with USERDB)")
20 SM_RCSID("@(#)$Id: udb.c,v 8.166 2013-11-22 20:51:57 ca Exp $ (without USERDB)")
25 #include <sm/sendmail.h>
29 # define DBT struct _data_base_thang_
32 void *data; /* pointer to data */
33 size_t size; /* length of data */
38 ** UDB.C -- interface between sendmail and Berkeley User Data Base.
40 ** This depends on the 4.4BSD db package.
46 char *udb_spec; /* string version of spec */
47 int udb_type; /* type of entry */
48 pid_t udb_pid; /* PID of process which opened db */
49 char *udb_default; /* default host for outgoing mail */
52 # if NETINET || NETINET6
53 /* type UE_REMOTE -- do remote call for lookup */
56 SOCKADDR _udb_addr; /* address */
57 int _udb_timeout; /* timeout */
59 # define udb_addr udb_u.udb_remote._udb_addr
60 # define udb_timeout udb_u.udb_remote._udb_timeout
61 # endif /* NETINET || NETINET6 */
63 /* type UE_FORWARD -- forward message to remote */
66 char *_udb_fwdhost; /* name of forward host */
68 # define udb_fwdhost udb_u.udb_forward._udb_fwdhost
71 /* type UE_FETCH -- lookup in local database */
74 char *_udb_dbname; /* pathname of database */
75 DB *_udb_dbp; /* open database ptr */
77 # define udb_dbname udb_u.udb_lookup._udb_dbname
78 # define udb_dbp udb_u.udb_lookup._udb_dbp
83 # define UDB_EOLIST 0 /* end of list */
84 # define UDB_SKIP 1 /* skip this entry */
85 # define UDB_REMOTE 2 /* look up in remote database */
86 # define UDB_DBFETCH 3 /* look up in local database */
87 # define UDB_FORWARD 4 /* forward to remote host */
88 # define UDB_HESIOD 5 /* look up via hesiod */
90 # define MAXUDBENT 10 /* maximum number of UDB entries */
100 static int hes_udb_get __P((DBT *, DBT *));
102 static char *udbmatch __P((char *, char *, SM_RPOOL_T *));
103 static int _udbx_init __P((ENVELOPE *));
104 static int _udb_parsespec __P((char *, struct udb_option [], int));
107 ** UDBEXPAND -- look up user in database and expand
110 ** a -- address to expand.
111 ** sendq -- pointer to head of sendq to put the expansions in.
112 ** aliaslevel -- the current alias nesting depth.
113 ** e -- the current envelope.
116 ** EX_TEMPFAIL -- if something "odd" happened -- probably due
117 ** to accessing a file on an NFS server that is down.
118 ** EX_OK -- otherwise.
124 static struct udbent UdbEnts[MAXUDBENT + 1];
125 static bool UdbInitialized = false;
128 udbexpand(a, sendq, aliaslevel, e)
132 register ENVELOPE *e;
138 register struct udbent *up;
142 char keybuf[MAXUDBKEY];
144 memset(&key, '\0', sizeof(key));
145 memset(&info, '\0', sizeof(info));
148 sm_dprintf("udbexpand(%s)\n", a->q_paddr);
150 /* make certain we are supposed to send to this address */
151 if (!QS_IS_SENDABLE(a->q_state))
153 e->e_to = a->q_paddr;
155 /* on first call, locate the database */
158 if (_udbx_init(e) == EX_TEMPFAIL)
162 /* short circuit the process if no chance of a match */
163 if (UdbSpec == NULL || UdbSpec[0] == '\0')
166 /* extract user to do userdb matching on */
169 /* short circuit name begins with '\\' since it can't possibly match */
170 /* (might want to treat this as unquoted instead) */
174 /* if name begins with a colon, it indicates our metadata */
178 keylen = sm_strlcpyn(keybuf, sizeof(keybuf), 2, user, ":maildrop");
180 /* if name is too long, assume it won't match */
181 if (keylen >= sizeof(keybuf))
184 /* build actual database key */
187 for (up = UdbEnts; !breakout; up++)
193 char userbuf[MEMCHUNKSIZE];
194 # if HESIOD && HES_GETMAILHOST
197 # if defined(NEWDB) && DB_VERSION_MAJOR > 1
203 usersize = sizeof(userbuf);
205 userleft = sizeof(userbuf) - 1;
209 ** Select action based on entry type.
211 ** On dropping out of this switch, "class" should
212 ** explain the type of the data, and "user" should
213 ** contain the user information.
216 switch (up->udb_type)
223 sm_dprintf("udbexpand: trying %s (%d) via db\n",
225 # if DB_VERSION_MAJOR < 2
226 i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_CURSOR);
227 # else /* DB_VERSION_MAJOR < 2 */
230 # if DB_VERSION_MAJOR > 2 || DB_VERSION_MINOR >= 6
231 (errno = (*up->udb_dbp->cursor)(up->udb_dbp,
232 NULL, &dbc, 0)) != 0)
233 # else /* DB_VERSION_MAJOR > 2 || DB_VERSION_MINOR >= 6 */
234 (errno = (*up->udb_dbp->cursor)(up->udb_dbp,
236 # endif /* DB_VERSION_MAJOR > 2 || DB_VERSION_MINOR >= 6 */
238 if (i != 0 || dbc == NULL ||
239 (errno = dbc->c_get(dbc, &key,
240 &info, DB_SET)) != 0)
242 # endif /* DB_VERSION_MAJOR < 2 */
243 if (i > 0 || info.size <= 0)
246 sm_dprintf("udbexpand: no match on %s (%d)\n",
248 # if DB_VERSION_MAJOR > 1
251 (void) dbc->c_close(dbc);
254 # endif /* DB_VERSION_MAJOR > 1 */
258 sm_dprintf("udbexpand: match %.*s: %.*s\n",
259 (int) key.size, (char *) key.data,
260 (int) info.size, (char *) info.data);
262 a->q_flags &= ~QSELFREF;
263 while (i == 0 && key.size == keylen &&
264 memcmp(key.data, keybuf, keylen) == 0)
268 if (bitset(EF_VRFYONLY, e->e_flags))
270 a->q_state = QS_VERIFIED;
271 # if DB_VERSION_MAJOR > 1
274 (void) dbc->c_close(dbc);
277 # endif /* DB_VERSION_MAJOR > 1 */
282 if (info.size >= userleft - 1)
285 int size = MEMCHUNKSIZE;
287 if (info.size > MEMCHUNKSIZE)
289 nuser = sm_malloc_x(usersize + size);
291 memmove(nuser, user, usersize);
293 sm_free(user); /* XXX */
298 p = &user[strlen(user)];
304 memmove(p, info.data, info.size);
306 userleft -= info.size;
308 /* get the next record */
309 # if DB_VERSION_MAJOR < 2
310 i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_NEXT);
311 # else /* DB_VERSION_MAJOR < 2 */
313 if ((errno = dbc->c_get(dbc, &key,
314 &info, DB_NEXT)) != 0)
316 # endif /* DB_VERSION_MAJOR < 2 */
319 # if DB_VERSION_MAJOR > 1
322 (void) dbc->c_close(dbc);
325 # endif /* DB_VERSION_MAJOR > 1 */
327 /* if nothing ever matched, try next database */
331 message("expanded to %s", user);
333 sm_syslog(LOG_INFO, e->e_id,
334 "expand %.100s => %s",
336 shortenstring(user, MAXSHORTSTR));
337 naddrs = sendtolist(user, a, sendq, aliaslevel + 1, e);
338 if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
342 sm_dprintf("udbexpand: QS_EXPANDED ");
343 printaddr(sm_debug_file(), a, false);
345 a->q_state = QS_EXPANDED;
349 syserr("udbexpand: db-get %.*s stat %d",
350 (int) key.size, (char *) key.data, i);
355 ** If this address has a -request address, reflect
356 ** it into the envelope.
359 memset(&key, '\0', sizeof(key));
360 memset(&info, '\0', sizeof(info));
361 (void) sm_strlcpyn(keybuf, sizeof(keybuf), 2, a->q_user,
363 keylen = strlen(keybuf);
367 # if DB_VERSION_MAJOR < 2
368 i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
370 i = errno = (*up->udb_dbp->get)(up->udb_dbp, NULL,
373 if (i != 0 || info.size <= 0)
375 a->q_owner = sm_rpool_malloc_x(e->e_rpool,
377 memmove(a->q_owner, info.data, info.size);
378 a->q_owner[info.size] = '\0';
380 /* announce delivery; NORECEIPT bit set later */
381 if (e->e_xfp != NULL)
383 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
384 "Message delivered to mailing list %s\n",
387 e->e_flags |= EF_SENDRECEIPT;
388 a->q_flags |= QDELIVERED|QEXPANDED;
397 sm_dprintf("udbexpand: trying %s (%d) via hesiod\n",
399 /* look up the key via hesiod */
400 i = hes_udb_get(&key, &info);
403 syserr("udbexpand: hesiod-get %.*s stat %d",
404 (int) key.size, (char *) key.data, i);
407 else if (i > 0 || info.size <= 0)
410 struct hes_postoffice *hp;
414 sm_dprintf("udbexpand: no match on %s (%d)\n",
415 (char *) keybuf, (int) keylen);
418 sm_dprintf(" ... trying hes_getmailhost(%s)\n",
420 hp = hes_getmailhost(a->q_user);
423 if (hes_error() == HES_ER_NET)
425 syserr("udbexpand: hesiod-getmail %s stat %d",
426 a->q_user, hes_error());
430 sm_dprintf("hes_getmailhost(%s): %d\n",
431 a->q_user, hes_error());
434 if (strlen(hp->po_name) + strlen(hp->po_host) >
438 sm_dprintf("hes_getmailhost(%s): expansion too long: %.30s@%.30s\n",
445 (void) sm_snprintf(pobuf, sizeof(pobuf),
446 "%s@%s", hp->po_name, hp->po_host);
447 info.size = strlen(info.data);
448 # else /* HES_GETMAILHOST */
450 # endif /* HES_GETMAILHOST */
453 sm_dprintf("udbexpand: match %.*s: %.*s\n",
454 (int) key.size, (char *) key.data,
455 (int) info.size, (char *) info.data);
456 a->q_flags &= ~QSELFREF;
458 if (bitset(EF_VRFYONLY, e->e_flags))
460 a->q_state = QS_VERIFIED;
465 if (info.size >= usersize)
466 user = sm_malloc_x(info.size + 1);
467 memmove(user, info.data, info.size);
468 user[info.size] = '\0';
470 message("hesioded to %s", user);
472 sm_syslog(LOG_INFO, e->e_id,
473 "hesiod %.100s => %s",
475 shortenstring(user, MAXSHORTSTR));
476 naddrs = sendtolist(user, a, sendq, aliaslevel + 1, e);
478 if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
482 sm_dprintf("udbexpand: QS_EXPANDED ");
483 printaddr(sm_debug_file(), a, false);
485 a->q_state = QS_EXPANDED;
489 ** If this address has a -request address, reflect
490 ** it into the envelope.
493 (void) sm_strlcpyn(keybuf, sizeof(keybuf), 2, a->q_user,
495 keylen = strlen(keybuf);
498 i = hes_udb_get(&key, &info);
499 if (i != 0 || info.size <= 0)
501 a->q_owner = sm_rpool_malloc_x(e->e_rpool,
503 memmove(a->q_owner, info.data, info.size);
504 a->q_owner[info.size] = '\0';
509 /* not yet implemented */
513 if (bitset(EF_VRFYONLY, e->e_flags))
515 a->q_state = QS_VERIFIED;
518 i = strlen(up->udb_fwdhost) + strlen(a->q_user) + 1;
522 user = sm_malloc_x(usersize);
524 (void) sm_strlcpyn(user, usersize, 3,
525 a->q_user, "@", up->udb_fwdhost);
526 message("expanded to %s", user);
527 a->q_flags &= ~QSELFREF;
528 naddrs = sendtolist(user, a, sendq, aliaslevel + 1, e);
529 if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
533 sm_dprintf("udbexpand: QS_EXPANDED ");
534 printaddr(sm_debug_file(), a, false);
536 a->q_state = QS_EXPANDED;
546 /* unknown entry type */
549 /* XXX if an exception occurs, there is a storage leak */
551 sm_free(user); /* XXX */
556 ** UDBSENDER -- return canonical external name of sender, given local name
559 ** sender -- the name of the sender on the local machine.
560 ** rpool -- resource pool from which to allocate result
563 ** The external name for this sender, if derivable from the
564 ** database. Storage allocated from rpool.
565 ** NULL -- if nothing is changed from the database.
572 udbsender(sender, rpool)
576 return udbmatch(sender, "mailname", rpool);
579 ** UDBMATCH -- match user in field, return result of lookup.
582 ** user -- the name of the user.
583 ** field -- the field to lookup.
584 ** rpool -- resource pool from which to allocate result
587 ** The external name for this sender, if derivable from the
588 ** database. Storage allocated from rpool.
589 ** NULL -- if nothing is changed from the database.
596 udbmatch(user, field, rpool)
602 register struct udbent *up;
606 char keybuf[MAXUDBKEY];
609 sm_dprintf("udbmatch(%s, %s)\n", user, field);
613 if (_udbx_init(CurEnv) == EX_TEMPFAIL)
617 /* short circuit if no spec */
618 if (UdbSpec == NULL || UdbSpec[0] == '\0')
621 /* short circuit name begins with '\\' since it can't possibly match */
625 /* long names can never match and are a pain to deal with */
627 if (i < sizeof("maildrop"))
628 i = sizeof("maildrop");
629 if ((strlen(user) + i) > sizeof(keybuf) - 4)
632 /* names beginning with colons indicate metadata */
636 /* build database key */
637 (void) sm_strlcpyn(keybuf, sizeof(keybuf), 3, user, ":", field);
638 keylen = strlen(keybuf);
640 for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
643 ** Select action based on entry type.
646 switch (up->udb_type)
650 memset(&key, '\0', sizeof(key));
651 memset(&info, '\0', sizeof(info));
654 # if DB_VERSION_MAJOR < 2
655 i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
657 i = errno = (*up->udb_dbp->get)(up->udb_dbp, NULL,
660 if (i != 0 || info.size <= 0)
663 sm_dprintf("udbmatch: no match on %s (%d) via db\n",
668 p = sm_rpool_malloc_x(rpool, info.size + 1);
669 memmove(p, info.data, info.size);
672 sm_dprintf("udbmatch ==> %s\n", p);
680 i = hes_udb_get(&key, &info);
681 if (i != 0 || info.size <= 0)
684 sm_dprintf("udbmatch: no match on %s (%d) via hesiod\n",
689 p = sm_rpool_malloc_x(rpool, info.size + 1);
690 memmove(p, info.data, info.size);
693 sm_dprintf("udbmatch ==> %s\n", p);
699 if (strcmp(field, "mailname") != 0)
703 ** Nothing yet. Search again for a default case. But only
704 ** use it if we also have a forward (:maildrop) pointer already
708 /* build database key */
709 (void) sm_strlcpyn(keybuf, sizeof(keybuf), 2, user, ":maildrop");
710 keylen = strlen(keybuf);
712 for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
714 switch (up->udb_type)
718 /* get the default case for this database */
719 if (up->udb_default == NULL)
721 memset(&key, '\0', sizeof(key));
722 memset(&info, '\0', sizeof(info));
723 key.data = ":default:mailname";
724 key.size = strlen(key.data);
725 # if DB_VERSION_MAJOR < 2
726 i = (*up->udb_dbp->get)(up->udb_dbp,
728 # else /* DB_VERSION_MAJOR < 2 */
729 i = errno = (*up->udb_dbp->get)(up->udb_dbp,
732 # endif /* DB_VERSION_MAJOR < 2 */
733 if (i != 0 || info.size <= 0)
735 /* no default case */
736 up->udb_default = "";
740 /* save the default case */
741 up->udb_default = sm_pmalloc_x(info.size + 1);
742 memmove(up->udb_default, info.data, info.size);
743 up->udb_default[info.size] = '\0';
745 else if (up->udb_default[0] == '\0')
748 /* we have a default case -- verify user:maildrop */
749 memset(&key, '\0', sizeof(key));
750 memset(&info, '\0', sizeof(info));
753 # if DB_VERSION_MAJOR < 2
754 i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
756 i = errno = (*up->udb_dbp->get)(up->udb_dbp, NULL,
759 if (i != 0 || info.size <= 0)
761 /* nope -- no aliasing for this user */
765 /* they exist -- build the actual address */
766 i = strlen(user) + strlen(up->udb_default) + 2;
767 p = sm_rpool_malloc_x(rpool, i);
768 (void) sm_strlcpyn(p, i, 3, user, "@", up->udb_default);
770 sm_dprintf("udbmatch ==> %s\n", p);
776 /* get the default case for this database */
777 if (up->udb_default == NULL)
779 key.data = ":default:mailname";
780 key.size = strlen(key.data);
781 i = hes_udb_get(&key, &info);
783 if (i != 0 || info.size <= 0)
785 /* no default case */
786 up->udb_default = "";
790 /* save the default case */
791 up->udb_default = sm_pmalloc_x(info.size + 1);
792 memmove(up->udb_default, info.data, info.size);
793 up->udb_default[info.size] = '\0';
795 else if (up->udb_default[0] == '\0')
798 /* we have a default case -- verify user:maildrop */
801 i = hes_udb_get(&key, &info);
802 if (i != 0 || info.size <= 0)
804 /* nope -- no aliasing for this user */
808 /* they exist -- build the actual address */
809 i = strlen(user) + strlen(up->udb_default) + 2;
810 p = sm_rpool_malloc_x(rpool, i);
811 (void) sm_strlcpyn(p, i, 3, user, "@", up->udb_default);
813 sm_dprintf("udbmatch ==> %s\n", p);
820 /* still nothing.... too bad */
824 ** UDB_MAP_LOOKUP -- look up arbitrary entry in user database map
827 ** map -- the map being queried.
828 ** name -- the name to look up.
829 ** av -- arguments to the map lookup.
830 ** statp -- to get any error status.
833 ** NULL if name not found in map.
834 ** The rewritten name otherwise.
839 udb_map_lookup(map, name, av, statp)
847 char *SM_NONVOLATILE result = NULL;
848 char keybuf[MAXNAME + 1];
850 if (tTd(28, 20) || tTd(38, 20))
851 sm_dprintf("udb_map_lookup(%s, %s)\n", map->map_mname, name);
853 if (bitset(MF_NOFOLDCASE, map->map_mflags))
859 int keysize = strlen(name);
861 if (keysize > sizeof(keybuf) - 1)
862 keysize = sizeof(keybuf) - 1;
863 memmove(keybuf, name, keysize);
864 keybuf[keysize] = '\0';
868 val = udbmatch(key, map->map_file, NULL);
872 if (bitset(MF_MATCHONLY, map->map_mflags))
873 result = map_rewrite(map, name, strlen(name), NULL);
875 result = map_rewrite(map, val, strlen(val), av);
882 ** _UDBX_INIT -- parse the UDB specification, opening any valid entries.
885 ** e -- the current envelope.
888 ** EX_TEMPFAIL -- if it appeared it couldn't get hold of a
889 ** database due to a host being down or some similar
890 ** (recoverable) situation.
891 ** EX_OK -- otherwise.
894 ** Fills in the UdbEnts structure from UdbSpec.
897 # define MAXUDBOPTS 27
905 register struct udbent *up;
910 # ifdef UDB_DEFAULT_SPEC
912 UdbSpec = UDB_DEFAULT_SPEC;
923 struct udb_option opts[MAXUDBOPTS + 1];
925 while (*p == ' ' || *p == '\t' || *p == ',')
934 if (ents >= MAXUDBENT)
936 syserr("Maximum number of UDB entries exceeded");
940 /* extract options */
941 (void) _udb_parsespec(spec, opts, MAXUDBOPTS);
944 ** Decode database specification.
946 ** In the sendmail tradition, the leading character
947 ** defines the semantics of the rest of the entry.
949 ** @hostname -- forward email to the indicated host.
950 ** This should be the last in the list,
951 ** since it always matches the input.
952 ** /dbname -- search the named database on the local
953 ** host using the Berkeley db package.
954 ** Hesiod -- search the named database with BIND
955 ** using the MIT Hesiod package.
960 case '@': /* forward to remote host */
961 up->udb_type = UDB_FORWARD;
962 up->udb_pid = CurrentPid;
963 up->udb_fwdhost = spec + 1;
969 case 'h': /* use hesiod */
971 if (sm_strcasecmp(spec, "hesiod") != 0)
973 up->udb_type = UDB_HESIOD;
974 up->udb_pid = CurrentPid;
981 case '/': /* look up remote name */
983 if (l > 3 && strcmp(&spec[l - 3], ".db") == 0)
985 up->udb_dbname = spec;
989 up->udb_dbname = sm_pmalloc_x(l + 4);
990 (void) sm_strlcpyn(up->udb_dbname, l + 4, 2,
994 # if DB_VERSION_MAJOR < 2
995 up->udb_dbp = dbopen(up->udb_dbname, O_RDONLY,
996 0644, DB_BTREE, NULL);
997 # else /* DB_VERSION_MAJOR < 2 */
999 int flags = DB_RDONLY;
1000 # if DB_VERSION_MAJOR > 2
1002 # endif /* DB_VERSION_MAJOR > 2 */
1004 SM_DB_FLAG_ADD(flags);
1006 # if DB_VERSION_MAJOR > 2
1007 ret = db_create(&up->udb_dbp, NULL, 0);
1010 (void) up->udb_dbp->close(up->udb_dbp,
1016 ret = up->udb_dbp->open(up->udb_dbp,
1025 #ifdef DB_OLD_VERSION
1026 if (ret == DB_OLD_VERSION)
1029 (void) up->udb_dbp->close(up->udb_dbp, 0);
1034 # else /* DB_VERSION_MAJOR > 2 */
1035 errno = db_open(up->udb_dbname, DB_BTREE,
1037 NULL, &up->udb_dbp);
1038 # endif /* DB_VERSION_MAJOR > 2 */
1040 # endif /* DB_VERSION_MAJOR < 2 */
1041 if (up->udb_dbp == NULL)
1045 int save_errno = errno;
1047 # if DB_VERSION_MAJOR < 2
1048 sm_dprintf("dbopen(%s): %s\n",
1049 # else /* DB_VERSION_MAJOR < 2 */
1050 sm_dprintf("db_open(%s): %s\n",
1051 # endif /* DB_VERSION_MAJOR < 2 */
1053 sm_errstring(errno));
1056 if (errno != ENOENT && errno != EACCES)
1059 sm_syslog(LOG_ERR, e->e_id,
1060 # if DB_VERSION_MAJOR < 2
1062 # else /* DB_VERSION_MAJOR < 2 */
1064 # endif /* DB_VERSION_MAJOR < 2 */
1066 sm_errstring(errno));
1067 up->udb_type = UDB_EOLIST;
1068 if (up->udb_dbname != spec)
1069 sm_free(up->udb_dbname); /* XXX */
1072 if (up->udb_dbname != spec)
1073 sm_free(up->udb_dbname); /* XXX */
1078 # if DB_VERSION_MAJOR < 2
1079 sm_dprintf("_udbx_init: dbopen(%s)\n",
1080 # else /* DB_VERSION_MAJOR < 2 */
1081 sm_dprintf("_udbx_init: db_open(%s)\n",
1082 # endif /* DB_VERSION_MAJOR < 2 */
1085 up->udb_type = UDB_DBFETCH;
1086 up->udb_pid = CurrentPid;
1096 syserr("Unknown UDB spec %s", spec);
1100 up->udb_type = UDB_EOLIST;
1104 for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
1106 switch (up->udb_type)
1109 sm_dprintf("REMOTE: addr %s, timeo %d\n",
1110 anynet_ntoa((SOCKADDR *) &up->udb_addr),
1116 sm_dprintf("FETCH: file %s\n", up->udb_dbname);
1118 sm_dprintf("FETCH\n");
1123 sm_dprintf("FORWARD: host %s\n",
1128 sm_dprintf("HESIOD\n");
1132 sm_dprintf("UNKNOWN\n");
1138 UdbInitialized = true;
1143 ** On temporary failure, back out anything we've already done
1148 for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
1150 if (up->udb_type == UDB_DBFETCH)
1152 # if DB_VERSION_MAJOR < 2
1153 (*up->udb_dbp->close)(up->udb_dbp);
1155 errno = (*up->udb_dbp->close)(up->udb_dbp, 0);
1158 sm_dprintf("_udbx_init: db->close(%s)\n",
1167 _udb_parsespec(udbspec, opt, maxopts)
1169 struct udb_option opt[];
1172 register char *spec;
1173 register char *spec_end;
1174 register int optnum;
1176 spec_end = strchr(udbspec, ':');
1177 for (optnum = 0; optnum < maxopts && (spec = spec_end) != NULL; optnum++)
1181 while (SM_ISSPACE(*spec))
1183 spec_end = strchr(spec, ':');
1184 if (spec_end != NULL)
1187 opt[optnum].udbo_name = spec;
1188 opt[optnum].udbo_val = NULL;
1189 p = strchr(spec, '=');
1191 opt[optnum].udbo_val = ++p;
1196 ** _UDBX_CLOSE -- close all file based UDB entries.
1209 if (!UdbInitialized)
1212 for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
1214 if (up->udb_pid != CurrentPid)
1218 if (up->udb_type == UDB_DBFETCH)
1220 # if DB_VERSION_MAJOR < 2
1221 (*up->udb_dbp->close)(up->udb_dbp);
1223 errno = (*up->udb_dbp->close)(up->udb_dbp, 0);
1227 sm_dprintf("_udbx_close: db->close(%s)\n",
1236 hes_udb_get(key, info)
1242 char kbuf[MAXUDBKEY + 1];
1244 if (sm_strlcpy(kbuf, key->data, sizeof(kbuf)) >= sizeof(kbuf))
1247 type = strrchr(name, ':');
1251 if (strchr(name, '@') != NULL)
1255 sm_dprintf("hes_udb_get(%s, %s)\n", name, type);
1257 /* make the hesiod query */
1259 if (HesiodContext == NULL && hesiod_init(&HesiodContext) != 0)
1261 hp = hesiod_resolve(HesiodContext, name, type);
1263 hp = hes_resolve(name, type);
1271 hesiod_free_list(HesiodContext, hp);
1272 if (errno == ECONNREFUSED || errno == EMSGSIZE)
1276 # else /* HESIOD_INIT */
1277 if (hp == NULL || hp[0] == NULL)
1279 /* network problem or timeout */
1280 if (hes_error() == HES_ER_NET)
1285 # endif /* HESIOD_INIT */
1289 ** If there are multiple matches, just return the
1292 ** XXX These should really be returned; for example,
1293 ** XXX it is legal for :maildrop to be multi-valued.
1297 info->size = (size_t) strlen(info->data);
1301 sm_dprintf("hes_udb_get => %s\n", *hp);
1305 # endif /* HESIOD */
1310 udbexpand(a, sendq, aliaslevel, e)