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.
45 char *udb_spec; /* string version of spec */
46 int udb_type; /* type of entry */
47 pid_t udb_pid; /* PID of process which opened db */
48 char *udb_default; /* default host for outgoing mail */
51 # if NETINET || NETINET6
52 /* type UE_REMOTE -- do remote call for lookup */
55 SOCKADDR _udb_addr; /* address */
56 int _udb_timeout; /* timeout */
58 # define udb_addr udb_u.udb_remote._udb_addr
59 # define udb_timeout udb_u.udb_remote._udb_timeout
60 # endif /* NETINET || NETINET6 */
62 /* type UE_FORWARD -- forward message to remote */
65 char *_udb_fwdhost; /* name of forward host */
67 # define udb_fwdhost udb_u.udb_forward._udb_fwdhost
70 /* type UE_FETCH -- look up in local database */
73 char *_udb_dbname; /* pathname of database */
74 DB *_udb_dbp; /* open database ptr */
76 # define udb_dbname udb_u.udb_lookup._udb_dbname
77 # define udb_dbp udb_u.udb_lookup._udb_dbp
82 # define UDB_EOLIST 0 /* end of list */
83 # define UDB_SKIP 1 /* skip this entry */
84 # define UDB_REMOTE 2 /* look up in remote database */
85 # define UDB_DBFETCH 3 /* look up in local database */
86 # define UDB_FORWARD 4 /* forward to remote host */
87 # define UDB_HESIOD 5 /* look up via hesiod */
89 # define MAXUDBENT 10 /* maximum number of UDB entries */
98 static int hes_udb_get __P((DBT *, DBT *));
100 static char *udbmatch __P((char *, char *, SM_RPOOL_T *));
101 static int _udbx_init __P((ENVELOPE *));
102 static int _udb_parsespec __P((char *, struct udb_option [], int));
105 ** UDBEXPAND -- look up user in database and expand
108 ** a -- address to expand.
109 ** sendq -- pointer to head of sendq to put the expansions in.
110 ** aliaslevel -- the current alias nesting depth.
111 ** e -- the current envelope.
114 ** EX_TEMPFAIL -- if something "odd" happened -- probably due
115 ** to accessing a file on an NFS server that is down.
116 ** EX_OK -- otherwise.
122 static struct udbent UdbEnts[MAXUDBENT + 1];
123 static bool UdbInitialized = false;
126 udbexpand(a, sendq, aliaslevel, e)
130 register ENVELOPE *e;
136 register struct udbent *up;
140 char keybuf[MAXUDBKEY];
142 memset(&key, '\0', sizeof(key));
143 memset(&info, '\0', sizeof(info));
146 sm_dprintf("udbexpand(%s)\n", a->q_paddr);
148 /* make certain we are supposed to send to this address */
149 if (!QS_IS_SENDABLE(a->q_state))
151 e->e_to = a->q_paddr;
153 /* on first call, locate the database */
156 if (_udbx_init(e) == EX_TEMPFAIL)
160 /* short circuit the process if no chance of a match */
161 if (SM_IS_EMPTY(UdbSpec))
164 /* extract user to do userdb matching on */
167 /* short circuit name begins with '\\' since it can't possibly match */
168 /* (might want to treat this as unquoted instead) */
172 /* if name begins with a colon, it indicates our metadata */
176 keylen = sm_strlcpyn(keybuf, sizeof(keybuf), 2, user, ":maildrop");
178 /* if name is too long, assume it won't match */
179 if (keylen >= sizeof(keybuf))
182 /* build actual database key */
185 for (up = UdbEnts; !breakout; up++)
191 char userbuf[MEMCHUNKSIZE];
192 # if HESIOD && HES_GETMAILHOST
193 char pobuf[MAXNAME]; /* EAI:should be ok, no UTF8? */
195 # if defined(NEWDB) && DB_VERSION_MAJOR > 1
201 usersize = sizeof(userbuf);
203 userleft = sizeof(userbuf) - 1;
207 ** Select action based on entry type.
209 ** On dropping out of this switch, "class" should
210 ** explain the type of the data, and "user" should
211 ** contain the user information.
214 switch (up->udb_type)
221 sm_dprintf("udbexpand: trying %s (%d) via db\n",
223 # if DB_VERSION_MAJOR < 2
224 i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_CURSOR);
225 # else /* DB_VERSION_MAJOR < 2 */
228 # if DB_VERSION_MAJOR > 2 || DB_VERSION_MINOR >= 6
229 (errno = (*up->udb_dbp->cursor)(up->udb_dbp,
230 NULL, &dbc, 0)) != 0)
231 # else /* DB_VERSION_MAJOR > 2 || DB_VERSION_MINOR >= 6 */
232 (errno = (*up->udb_dbp->cursor)(up->udb_dbp,
234 # endif /* DB_VERSION_MAJOR > 2 || DB_VERSION_MINOR >= 6 */
236 if (i != 0 || dbc == NULL ||
237 (errno = dbc->c_get(dbc, &key,
238 &info, DB_SET)) != 0)
240 # endif /* DB_VERSION_MAJOR < 2 */
241 if (i > 0 || info.size <= 0)
244 sm_dprintf("udbexpand: no match on %s (%d)\n",
246 # if DB_VERSION_MAJOR > 1
249 (void) dbc->c_close(dbc);
252 # endif /* DB_VERSION_MAJOR > 1 */
256 sm_dprintf("udbexpand: match %.*s: %.*s\n",
257 (int) key.size, (char *) key.data,
258 (int) info.size, (char *) info.data);
260 a->q_flags &= ~QSELFREF;
261 while (i == 0 && key.size == keylen &&
262 memcmp(key.data, keybuf, keylen) == 0)
266 if (bitset(EF_VRFYONLY, e->e_flags))
268 a->q_state = QS_VERIFIED;
269 # if DB_VERSION_MAJOR > 1
272 (void) dbc->c_close(dbc);
275 # endif /* DB_VERSION_MAJOR > 1 */
280 if (info.size >= userleft - 1)
283 int size = MEMCHUNKSIZE;
285 if (info.size > MEMCHUNKSIZE)
287 nuser = sm_malloc_x(usersize + size);
289 memmove(nuser, user, usersize);
291 sm_free(user); /* XXX */
296 p = &user[strlen(user)];
302 memmove(p, info.data, info.size);
304 userleft -= info.size;
306 /* get the next record */
307 # if DB_VERSION_MAJOR < 2
308 i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_NEXT);
309 # else /* DB_VERSION_MAJOR < 2 */
311 if ((errno = dbc->c_get(dbc, &key,
312 &info, DB_NEXT)) != 0)
314 # endif /* DB_VERSION_MAJOR < 2 */
317 # if DB_VERSION_MAJOR > 1
320 (void) dbc->c_close(dbc);
323 # endif /* DB_VERSION_MAJOR > 1 */
325 /* if nothing ever matched, try next database */
329 message("expanded to %s", user);
331 sm_syslog(LOG_INFO, e->e_id,
332 "expand %.100s => %s",
334 shortenstring(user, MAXSHORTSTR));
335 naddrs = sendtolist(user, a, sendq, aliaslevel + 1, e);
336 if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
340 sm_dprintf("udbexpand: QS_EXPANDED ");
341 printaddr(sm_debug_file(), a, false);
343 a->q_state = QS_EXPANDED;
347 syserr("udbexpand: db-get %.*s stat %d",
348 (int) key.size, (char *) key.data, i);
353 ** If this address has a -request address, reflect
354 ** it into the envelope.
357 memset(&key, '\0', sizeof(key));
358 memset(&info, '\0', sizeof(info));
359 (void) sm_strlcpyn(keybuf, sizeof(keybuf), 2, a->q_user,
361 keylen = strlen(keybuf);
365 # if DB_VERSION_MAJOR < 2
366 i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
368 i = errno = (*up->udb_dbp->get)(up->udb_dbp, NULL,
371 if (i != 0 || info.size <= 0)
373 a->q_owner = sm_rpool_malloc_x(e->e_rpool,
375 memmove(a->q_owner, info.data, info.size);
376 a->q_owner[info.size] = '\0';
378 /* announce delivery; NORECEIPT bit set later */
379 if (e->e_xfp != NULL)
381 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
382 "Message delivered to mailing list %s\n",
385 e->e_flags |= EF_SENDRECEIPT;
386 a->q_flags |= QDELIVERED|QEXPANDED;
395 sm_dprintf("udbexpand: trying %s (%d) via hesiod\n",
397 /* look up the key via hesiod */
398 i = hes_udb_get(&key, &info);
401 syserr("udbexpand: hesiod-get %.*s stat %d",
402 (int) key.size, (char *) key.data, i);
405 else if (i > 0 || info.size <= 0)
408 struct hes_postoffice *hp;
412 sm_dprintf("udbexpand: no match on %s (%d)\n",
413 (char *) keybuf, (int) keylen);
416 sm_dprintf(" ... trying hes_getmailhost(%s)\n",
418 hp = hes_getmailhost(a->q_user);
421 if (hes_error() == HES_ER_NET)
423 syserr("udbexpand: hesiod-getmail %s stat %d",
424 a->q_user, hes_error());
428 sm_dprintf("hes_getmailhost(%s): %d\n",
429 a->q_user, hes_error());
432 if (strlen(hp->po_name) + strlen(hp->po_host) >
436 sm_dprintf("hes_getmailhost(%s): expansion too long: %.30s@%.30s\n",
443 (void) sm_snprintf(pobuf, sizeof(pobuf),
444 "%s@%s", hp->po_name, hp->po_host);
445 info.size = strlen(info.data);
446 # else /* HES_GETMAILHOST */
448 # endif /* HES_GETMAILHOST */
451 sm_dprintf("udbexpand: match %.*s: %.*s\n",
452 (int) key.size, (char *) key.data,
453 (int) info.size, (char *) info.data);
454 a->q_flags &= ~QSELFREF;
456 if (bitset(EF_VRFYONLY, e->e_flags))
458 a->q_state = QS_VERIFIED;
463 if (info.size >= usersize)
464 user = sm_malloc_x(info.size + 1);
465 memmove(user, info.data, info.size);
466 user[info.size] = '\0';
468 message("hesioded to %s", user);
470 sm_syslog(LOG_INFO, e->e_id,
471 "hesiod %.100s => %s",
473 shortenstring(user, MAXSHORTSTR));
474 naddrs = sendtolist(user, a, sendq, aliaslevel + 1, e);
476 if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
480 sm_dprintf("udbexpand: QS_EXPANDED ");
481 printaddr(sm_debug_file(), a, false);
483 a->q_state = QS_EXPANDED;
487 ** If this address has a -request address, reflect
488 ** it into the envelope.
491 (void) sm_strlcpyn(keybuf, sizeof(keybuf), 2, a->q_user,
493 keylen = strlen(keybuf);
496 i = hes_udb_get(&key, &info);
497 if (i != 0 || info.size <= 0)
499 a->q_owner = sm_rpool_malloc_x(e->e_rpool,
501 memmove(a->q_owner, info.data, info.size);
502 a->q_owner[info.size] = '\0';
507 /* not yet implemented */
511 if (bitset(EF_VRFYONLY, e->e_flags))
513 a->q_state = QS_VERIFIED;
516 i = strlen(up->udb_fwdhost) + strlen(a->q_user) + 1;
520 user = sm_malloc_x(usersize);
522 (void) sm_strlcpyn(user, usersize, 3,
523 a->q_user, "@", up->udb_fwdhost);
524 message("expanded to %s", user);
525 a->q_flags &= ~QSELFREF;
526 naddrs = sendtolist(user, a, sendq, aliaslevel + 1, e);
527 if (naddrs > 0 && !bitset(QSELFREF, a->q_flags))
531 sm_dprintf("udbexpand: QS_EXPANDED ");
532 printaddr(sm_debug_file(), a, false);
534 a->q_state = QS_EXPANDED;
544 /* unknown entry type */
547 /* XXX if an exception occurs, there is a storage leak */
549 sm_free(user); /* XXX */
554 ** UDBSENDER -- return canonical external name of sender, given local name
557 ** sender -- the name of the sender on the local machine.
558 ** rpool -- resource pool from which to allocate result
561 ** The external name for this sender, if derivable from the
562 ** database. Storage allocated from rpool.
563 ** NULL -- if nothing is changed from the database.
570 udbsender(sender, rpool)
574 return udbmatch(sender, "mailname", rpool);
577 ** UDBMATCH -- match user in field, return result of lookup.
580 ** user -- the name of the user.
581 ** field -- the field to look up.
582 ** rpool -- resource pool from which to allocate result
585 ** The external name for this sender, if derivable from the
586 ** database. Storage allocated from rpool.
587 ** NULL -- if nothing is changed from the database.
594 udbmatch(user, field, rpool)
599 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 (SM_IS_EMPTY(UdbSpec))
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);
639 keylen = strlen(keybuf);
642 for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
645 ** Select action based on entry type.
648 switch (up->udb_type)
652 memset(&key, '\0', sizeof(key));
653 memset(&info, '\0', sizeof(info));
656 # if DB_VERSION_MAJOR < 2
657 i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
659 i = errno = (*up->udb_dbp->get)(up->udb_dbp, NULL,
662 if (i != 0 || info.size <= 0)
665 sm_dprintf("udbmatch: no match on %s (%d) via db\n",
670 p = sm_rpool_malloc_x(rpool, info.size + 1);
671 memmove(p, info.data, info.size);
674 sm_dprintf("udbmatch ==> %s\n", p);
682 i = hes_udb_get(&key, &info);
683 if (i != 0 || info.size <= 0)
686 sm_dprintf("udbmatch: no match on %s (%d) via hesiod\n",
691 p = sm_rpool_malloc_x(rpool, info.size + 1);
692 memmove(p, info.data, info.size);
695 sm_dprintf("udbmatch ==> %s\n", p);
701 if (strcmp(field, "mailname") != 0)
705 ** Nothing yet. Search again for a default case. But only
706 ** use it if we also have a forward (:maildrop) pointer already
710 /* build database key */
711 (void) sm_strlcpyn(keybuf, sizeof(keybuf), 2, user, ":maildrop");
713 keylen = strlen(keybuf);
716 for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
718 switch (up->udb_type)
722 /* get the default case for this database */
723 if (up->udb_default == NULL)
725 memset(&key, '\0', sizeof(key));
726 memset(&info, '\0', sizeof(info));
727 key.data = ":default:mailname";
728 key.size = strlen(key.data);
729 # if DB_VERSION_MAJOR < 2
730 i = (*up->udb_dbp->get)(up->udb_dbp,
732 # else /* DB_VERSION_MAJOR < 2 */
733 i = errno = (*up->udb_dbp->get)(up->udb_dbp,
736 # endif /* DB_VERSION_MAJOR < 2 */
737 if (i != 0 || info.size <= 0)
739 /* no default case */
740 up->udb_default = "";
744 /* save the default case */
745 up->udb_default = sm_pmalloc_x(info.size + 1);
746 memmove(up->udb_default, info.data, info.size);
747 up->udb_default[info.size] = '\0';
749 else if (up->udb_default[0] == '\0')
752 /* we have a default case -- verify user:maildrop */
753 memset(&key, '\0', sizeof(key));
754 memset(&info, '\0', sizeof(info));
757 # if DB_VERSION_MAJOR < 2
758 i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
760 i = errno = (*up->udb_dbp->get)(up->udb_dbp, NULL,
763 if (i != 0 || info.size <= 0)
765 /* nope -- no aliasing for this user */
769 /* they exist -- build the actual address */
770 i = strlen(user) + strlen(up->udb_default) + 2;
771 p = sm_rpool_malloc_x(rpool, i);
772 (void) sm_strlcpyn(p, i, 3, user, "@", up->udb_default);
774 sm_dprintf("udbmatch ==> %s\n", p);
780 /* get the default case for this database */
781 if (up->udb_default == NULL)
783 key.data = ":default:mailname";
784 key.size = strlen(key.data);
785 i = hes_udb_get(&key, &info);
787 if (i != 0 || info.size <= 0)
789 /* no default case */
790 up->udb_default = "";
794 /* save the default case */
795 up->udb_default = sm_pmalloc_x(info.size + 1);
796 memmove(up->udb_default, info.data, info.size);
797 up->udb_default[info.size] = '\0';
799 else if (up->udb_default[0] == '\0')
802 /* we have a default case -- verify user:maildrop */
805 i = hes_udb_get(&key, &info);
806 if (i != 0 || info.size <= 0)
808 /* nope -- no aliasing for this user */
812 /* they exist -- build the actual address */
813 i = strlen(user) + strlen(up->udb_default) + 2;
814 p = sm_rpool_malloc_x(rpool, i);
815 (void) sm_strlcpyn(p, i, 3, user, "@", up->udb_default);
817 sm_dprintf("udbmatch ==> %s\n", p);
824 /* still nothing.... too bad */
828 ** UDB_MAP_LOOKUP -- look up arbitrary entry in user database map
831 ** map -- the map being queried.
832 ** name -- the name to look up.
833 ** av -- arguments to the map lookup.
834 ** statp -- to get any error status.
837 ** NULL if name not found in map.
838 ** The rewritten name otherwise.
843 udb_map_lookup(map, name, av, statp)
851 char *SM_NONVOLATILE result = NULL;
852 char keybuf[MAXNAME + 1]; /* EAI:ok */
854 if (tTd(28, 20) || tTd(38, 20))
855 sm_dprintf("udb_map_lookup(%s, %s)\n", map->map_mname, name);
857 if (bitset(MF_NOFOLDCASE, map->map_mflags))
861 int keysize = strlen(name);
863 if (keysize > sizeof(keybuf) - 1)
864 keysize = sizeof(keybuf) - 1;
865 memmove(keybuf, name, keysize);
866 keybuf[keysize] = '\0';
867 makelower_buf(keybuf, keybuf, sizeof(keybuf));
870 val = udbmatch(key, map->map_file, NULL);
874 if (bitset(MF_MATCHONLY, map->map_mflags))
875 result = map_rewrite(map, name, strlen(name), NULL);
877 result = map_rewrite(map, val, strlen(val), av);
881 if (key != name && key != keybuf)
886 ** _UDBX_INIT -- parse the UDB specification, opening any valid entries.
889 ** e -- the current envelope.
892 ** EX_TEMPFAIL -- if it appeared it couldn't get hold of a
893 ** database due to a host being down or some similar
894 ** (recoverable) situation.
895 ** EX_OK -- otherwise.
898 ** Fills in the UdbEnts structure from UdbSpec.
901 # define MAXUDBOPTS 27
909 register struct udbent *up;
914 # ifdef UDB_DEFAULT_SPEC
916 UdbSpec = UDB_DEFAULT_SPEC;
927 struct udb_option opts[MAXUDBOPTS + 1];
929 while (*p == ' ' || *p == '\t' || *p == ',')
938 if (ents >= MAXUDBENT)
940 syserr("Maximum number of UDB entries exceeded");
944 /* extract options */
945 (void) _udb_parsespec(spec, opts, MAXUDBOPTS);
948 ** Decode database specification.
950 ** In the sendmail tradition, the leading character
951 ** defines the semantics of the rest of the entry.
953 ** @hostname -- forward email to the indicated host.
954 ** This should be the last in the list,
955 ** since it always matches the input.
956 ** /dbname -- search the named database on the local
957 ** host using the Berkeley db package.
958 ** Hesiod -- search the named database with BIND
959 ** using the MIT Hesiod package.
964 case '@': /* forward to remote host */
965 up->udb_type = UDB_FORWARD;
966 up->udb_pid = CurrentPid;
967 up->udb_fwdhost = spec + 1;
973 case 'h': /* use hesiod */
975 if (!SM_STRCASEEQ(spec, "hesiod"))
977 up->udb_type = UDB_HESIOD;
978 up->udb_pid = CurrentPid;
985 case '/': /* look up remote name */
987 if (l > 3 && strcmp(&spec[l - 3], ".db") == 0)
989 up->udb_dbname = spec;
993 up->udb_dbname = sm_pmalloc_x(l + 4);
994 (void) sm_strlcpyn(up->udb_dbname, l + 4, 2,
998 # if DB_VERSION_MAJOR < 2
999 up->udb_dbp = dbopen(up->udb_dbname, O_RDONLY,
1000 0644, DB_BTREE, NULL);
1001 # else /* DB_VERSION_MAJOR < 2 */
1003 int flags = DB_RDONLY;
1004 # if DB_VERSION_MAJOR > 2
1006 # endif /* DB_VERSION_MAJOR > 2 */
1008 SM_DB_FLAG_ADD(flags);
1010 # if DB_VERSION_MAJOR > 2
1011 ret = db_create(&up->udb_dbp, NULL, 0);
1014 (void) up->udb_dbp->close(up->udb_dbp,
1020 ret = up->udb_dbp->open(up->udb_dbp,
1029 # ifdef DB_OLD_VERSION
1030 if (ret == DB_OLD_VERSION)
1033 (void) up->udb_dbp->close(up->udb_dbp, 0);
1038 # else /* DB_VERSION_MAJOR > 2 */
1039 errno = db_open(up->udb_dbname, DB_BTREE,
1041 NULL, &up->udb_dbp);
1042 # endif /* DB_VERSION_MAJOR > 2 */
1044 # endif /* DB_VERSION_MAJOR < 2 */
1045 if (up->udb_dbp == NULL)
1049 int save_errno = errno;
1051 # if DB_VERSION_MAJOR < 2
1052 sm_dprintf("dbopen(%s): %s\n",
1053 # else /* DB_VERSION_MAJOR < 2 */
1054 sm_dprintf("db_open(%s): %s\n",
1055 # endif /* DB_VERSION_MAJOR < 2 */
1057 sm_errstring(errno));
1060 if (errno != ENOENT && errno != EACCES)
1063 sm_syslog(LOG_ERR, e->e_id,
1064 # if DB_VERSION_MAJOR < 2
1066 # else /* DB_VERSION_MAJOR < 2 */
1068 # endif /* DB_VERSION_MAJOR < 2 */
1070 sm_errstring(errno));
1071 up->udb_type = UDB_EOLIST;
1072 if (up->udb_dbname != spec)
1073 sm_free(up->udb_dbname); /* XXX */
1076 if (up->udb_dbname != spec)
1077 sm_free(up->udb_dbname); /* XXX */
1082 # if DB_VERSION_MAJOR < 2
1083 sm_dprintf("_udbx_init: dbopen(%s)\n",
1084 # else /* DB_VERSION_MAJOR < 2 */
1085 sm_dprintf("_udbx_init: db_open(%s)\n",
1086 # endif /* DB_VERSION_MAJOR < 2 */
1089 up->udb_type = UDB_DBFETCH;
1090 up->udb_pid = CurrentPid;
1100 syserr("Unknown UDB spec %s", spec);
1104 up->udb_type = UDB_EOLIST;
1108 for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
1110 switch (up->udb_type)
1113 sm_dprintf("REMOTE: addr %s, timeo %d\n",
1114 anynet_ntoa((SOCKADDR *) &up->udb_addr),
1120 sm_dprintf("FETCH: file %s\n", up->udb_dbname);
1122 sm_dprintf("FETCH\n");
1127 sm_dprintf("FORWARD: host %s\n",
1132 sm_dprintf("HESIOD\n");
1136 sm_dprintf("UNKNOWN\n");
1142 UdbInitialized = true;
1147 ** On temporary failure, back out anything we've already done
1152 for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
1154 if (up->udb_type == UDB_DBFETCH)
1156 # if DB_VERSION_MAJOR < 2
1157 (*up->udb_dbp->close)(up->udb_dbp);
1159 errno = (*up->udb_dbp->close)(up->udb_dbp, 0);
1162 sm_dprintf("_udbx_init: db->close(%s)\n",
1171 _udb_parsespec(udbspec, opt, maxopts)
1173 struct udb_option opt[];
1176 register char *spec;
1177 register char *spec_end;
1178 register int optnum;
1180 spec_end = strchr(udbspec, ':');
1181 for (optnum = 0; optnum < maxopts && (spec = spec_end) != NULL; optnum++)
1185 while (SM_ISSPACE(*spec))
1187 spec_end = strchr(spec, ':');
1188 if (spec_end != NULL)
1191 opt[optnum].udbo_name = spec;
1192 opt[optnum].udbo_val = NULL;
1193 p = strchr(spec, '=');
1195 opt[optnum].udbo_val = ++p;
1200 ** _UDBX_CLOSE -- close all file based UDB entries.
1213 if (!UdbInitialized)
1216 for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
1218 if (up->udb_pid != CurrentPid)
1222 if (up->udb_type == UDB_DBFETCH)
1224 # if DB_VERSION_MAJOR < 2
1225 (*up->udb_dbp->close)(up->udb_dbp);
1227 errno = (*up->udb_dbp->close)(up->udb_dbp, 0);
1231 sm_dprintf("_udbx_close: db->close(%s)\n",
1240 hes_udb_get(key, info)
1246 char kbuf[MAXUDBKEY + 1];
1248 if (sm_strlcpy(kbuf, key->data, sizeof(kbuf)) >= sizeof(kbuf))
1251 type = strrchr(name, ':');
1255 if (strchr(name, '@') != NULL)
1259 sm_dprintf("hes_udb_get(%s, %s)\n", name, type);
1261 /* make the hesiod query */
1263 if (HesiodContext == NULL && hesiod_init(&HesiodContext) != 0)
1265 hp = hesiod_resolve(HesiodContext, name, type);
1267 hp = hes_resolve(name, type);
1275 hesiod_free_list(HesiodContext, hp);
1276 if (errno == ECONNREFUSED || errno == EMSGSIZE)
1280 # else /* HESIOD_INIT */
1281 if (SM_IS_EMPTY(hp))
1283 /* network problem or timeout */
1284 if (hes_error() == HES_ER_NET)
1289 # endif /* HESIOD_INIT */
1293 ** If there are multiple matches, just return the
1296 ** XXX These should really be returned; for example,
1297 ** XXX it is legal for :maildrop to be multi-valued.
1301 info->size = (size_t) strlen(info->data);
1305 sm_dprintf("hes_udb_get => %s\n", *hp);
1309 # endif /* HESIOD */
1314 udbexpand(a, sendq, aliaslevel, e)