info_hesiod.c revision 38494
1/*
2 * Copyright (c) 1997-1998 Erez Zadok
3 * Copyright (c) 1989 Jan-Simon Pendry
4 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1989 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *      This product includes software developed by the University of
22 *      California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 *      %W% (Berkeley) %G%
40 *
41 * $Id: info_hesiod.c,v 1.1 1997-1998/07/11 08:34:52 danny Exp danny $
42 *
43 */
44
45/*
46 * Get info from Hesiod
47 */
48
49#ifdef HAVE_CONFIG_H
50# include <config.h>
51#endif /* HAVE_CONFIG_H */
52#include <am_defs.h>
53#include <amd.h>
54
55#define	HES_PREFIX	"hesiod."
56#define	HES_PREFLEN	7
57
58#ifdef HAVE_HESIOD_INIT
59/* bsdi3 does not define this extern in any header file */
60extern char **hesiod_resolve(void *context, const char *name, const char *type);
61
62static voidp hesiod_context;
63#endif /* HAVE_HESIOD_INIT */
64
65/*
66 * No easy way to probe the server - check the map name begins with "hesiod."
67 * Note: this name includes 'amu_' so as to not conflict with libhesiod's
68 * hesiod_init() function.
69 */
70int
71amu_hesiod_init(mnt_map *m, char *map, time_t *tp)
72{
73#ifdef DEBUG
74  dlog("amu_hesiod_init(%s)", map);
75#endif /* DEBUG */
76  *tp = 0;
77
78#ifdef HAVE_HESIOD_INIT
79  if(!hesiod_context && hesiod_init(&hesiod_context) != 0)
80    return ENOENT;
81#endif /* HAVE_HESIOD_INIT */
82
83  return NSTREQ(map, HES_PREFIX, HES_PREFLEN) ? 0 : ENOENT;
84}
85
86
87/*
88 * Do a Hesiod nameserver call.
89 * Modify time is ignored by Hesiod - XXX
90 */
91int
92hesiod_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
93{
94  char hes_key[MAXPATHLEN];
95  char **rvec;
96#ifndef HAVE_HESIOD_INIT
97  int error;
98#endif /* not HAVE_HESIOD_INIT */
99
100#ifdef DEBUG
101  dlog("hesiod_search(m=%x, map=%s, key=%s, pval=%x tp=%x)", m, map, key, pval, tp);
102#endif /* DEBUG */
103
104  sprintf(hes_key, "%s.%s", key, map + HES_PREFLEN);
105
106  /*
107   * Call the resolver
108   */
109#ifdef DEBUG
110  dlog("Hesiod base is: %s\n", gopt.hesiod_base);
111  dlog("hesiod_search: hes_resolve(%s, %s)", hes_key, gopt.hesiod_base);
112  if (debug_flags & D_INFO)
113    _res.options |= RES_DEBUG;
114#endif /* DEBUG */
115
116#ifdef HAVE_HESIOD_INIT
117  /* new style hesiod */
118  rvec = hesiod_resolve(hesiod_context, hes_key, gopt.hesiod_base);
119#else /* not HAVE_HESIOD_INIT */
120  rvec = hes_resolve(hes_key, gopt.hesiod_base);
121#endif /* not HAVE_HESIOD_INIT */
122
123  /*
124   * If a reply was forthcoming then return
125   * it (and free subsequent replies)
126   */
127  if (rvec && *rvec) {
128    *pval = *rvec;
129    while (*++rvec)
130      XFREE(*rvec);
131    return 0;
132  }
133
134#ifdef HAVE_HESIOD_INIT
135  /* new style hesiod */
136  return errno;
137#else /* not HAVE_HESIOD_INIT */
138  /*
139   * Otherwise reflect the hesiod error into a Un*x error
140   */
141# ifdef DEBUG
142  dlog("hesiod_search: Error: %d", hes_error());
143# endif /* DEBUG */
144  switch (hes_error()) {
145  case HES_ER_NOTFOUND:
146    error = ENOENT;
147    break;
148  case HES_ER_CONFIG:
149    error = EIO;
150    break;
151  case HES_ER_NET:
152    error = ETIMEDOUT;
153    break;
154  default:
155    error = EINVAL;
156    break;
157  }
158# ifdef DEBUG
159  dlog("hesiod_search: Returning: %d", error);
160# endif /* DEBUG */
161  return error;
162#endif /* not HAVE_HESIOD_INIT */
163}
164