1/*
2 * Copyright (c) 1997-2014 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. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *
36 * File: am-utils/amd/info_hesiod.c
37 *
38 */
39
40/*
41 * Get info from Hesiod
42 */
43
44#ifdef HAVE_CONFIG_H
45# include <config.h>
46#endif /* HAVE_CONFIG_H */
47#include <am_defs.h>
48#include <amd.h>
49#include <sun_map.h>
50
51#define	HES_PREFIX	"hesiod."
52#define	HES_PREFLEN	7
53
54#ifdef HAVE_HESIOD_INIT
55/* bsdi3 does not define this extern in any header file */
56extern char **hesiod_resolve(void *context, const char *name, const char *type);
57extern int hesiod_init(void **context);
58static voidp hesiod_context;
59#endif /* HAVE_HESIOD_INIT */
60
61/* forward declarations */
62int amu_hesiod_init(mnt_map *m, char *map, time_t *tp);
63int hesiod_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp);
64int hesiod_isup(mnt_map *m, char *map);
65
66/*
67 * No easy way to probe the server - check the map name begins with "hesiod."
68 * Note: this name includes 'amu_' so as to not conflict with libhesiod's
69 * hesiod_init() function.
70 */
71int
72amu_hesiod_init(mnt_map *m, char *map, time_t *tp)
73{
74  dlog("amu_hesiod_init(%s)", map);
75  *tp = 0;
76
77#ifdef HAVE_HESIOD_INIT
78  if (!hesiod_context && hesiod_init(&hesiod_context) != 0)
79    return ENOENT;
80#endif /* HAVE_HESIOD_INIT */
81
82  return NSTREQ(map, HES_PREFIX, HES_PREFLEN) ? 0 : ENOENT;
83}
84
85
86/*
87 * Do a Hesiod nameserver call.
88 * Modify time is ignored by Hesiod - XXX
89 */
90int
91hesiod_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
92{
93  char hes_key[MAXPATHLEN];
94  char **rvec;
95#ifndef HAVE_HESIOD_INIT
96  int error;
97#endif /* not HAVE_HESIOD_INIT */
98
99  dlog("hesiod_search(m=%lx, map=%s, key=%s, pval=%lx tp=%lx)",
100       (unsigned long) m, map, key, (unsigned long) pval, (unsigned long) tp);
101
102  if (key[0] == '.')
103    return ENOENT;
104
105  xsnprintf(hes_key, sizeof(hes_key), "%s.%s", key, map + HES_PREFLEN);
106
107  /*
108   * Call the resolver
109   */
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 (amuDebug(D_INFO))
113    _res.options |= RES_DEBUG;
114
115#ifdef HAVE_HESIOD_INIT
116  /* new style hesiod */
117  rvec = hesiod_resolve(hesiod_context, hes_key, gopt.hesiod_base);
118#else /* not HAVE_HESIOD_INIT */
119  rvec = hes_resolve(hes_key, gopt.hesiod_base);
120#endif /* not HAVE_HESIOD_INIT */
121
122  /*
123   * If a reply was forthcoming then return
124   * it (and free subsequent replies)
125   */
126  if (rvec && *rvec) {
127    if (m->cfm && (m->cfm->cfm_flags & CFM_SUN_MAP_SYNTAX)) {
128      *pval = sun_entry2amd(key, *rvec);
129      XFREE(*rvec);
130    } else
131      *pval = *rvec;
132    while (*++rvec)
133      XFREE(*rvec);
134    return 0;
135  }
136
137#ifdef HAVE_HESIOD_INIT
138  /* new style hesiod */
139  return errno;
140#else /* not HAVE_HESIOD_INIT */
141  /*
142   * Otherwise reflect the hesiod error into a Un*x error
143   */
144  dlog("hesiod_search: Error: %d", hes_error());
145  switch (hes_error()) {
146  case HES_ER_NOTFOUND:
147    error = ENOENT;
148    break;
149  case HES_ER_CONFIG:
150    error = EIO;
151    break;
152  case HES_ER_NET:
153    error = ETIMEDOUT;
154    break;
155  default:
156    error = EINVAL;
157    break;
158  }
159  dlog("hesiod_search: Returning: %d", error);
160  return error;
161#endif /* not HAVE_HESIOD_INIT */
162}
163
164
165/*
166 * Check if Hesiod is up, so we can determine if to clear the map or not.
167 * Test it by querying for /defaults.
168 * Returns: 0 if Hesiod is down, 1 if it is up.
169 */
170int
171hesiod_isup(mnt_map *m, char *map)
172{
173  int error;
174  char *val;
175  time_t mtime;
176  static int last_status = 1;	/* assume up by default */
177
178  error = hesiod_search(m, map, "/defaults", &val, &mtime);
179  dlog("hesiod_isup(%s): %s", map, strerror(error));
180  if (error != 0 && error != ENOENT) {
181    plog(XLOG_ERROR,
182	 "hesiod_isup: error getting `/defaults' entry in map %s: %m", map);
183    last_status = 0;
184    return 0;			/* Hesiod is down */
185  }
186  if (last_status == 0) {	/* if was down before */
187    plog(XLOG_INFO, "hesiod_isup: Hesiod came back up for map %s", map);
188    last_status = 1;
189  }
190  return 1;			/* Hesiod is up */
191}
192