info_hesiod.c revision 272461
11573Srgrimes/*
211681Sbde * Copyright (c) 1997-2006 Erez Zadok
31573Srgrimes * Copyright (c) 1989 Jan-Simon Pendry
41573Srgrimes * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
51573Srgrimes * Copyright (c) 1989 The Regents of the University of California.
61573Srgrimes * All rights reserved.
71573Srgrimes *
81573Srgrimes * This code is derived from software contributed to Berkeley by
91573Srgrimes * Jan-Simon Pendry at Imperial College, London.
101573Srgrimes *
111573Srgrimes * Redistribution and use in source and binary forms, with or without
121573Srgrimes * modification, are permitted provided that the following conditions
131573Srgrimes * are met:
141573Srgrimes * 1. Redistributions of source code must retain the above copyright
151573Srgrimes *    notice, this list of conditions and the following disclaimer.
161573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
171573Srgrimes *    notice, this list of conditions and the following disclaimer in the
181573Srgrimes *    documentation and/or other materials provided with the distribution.
191573Srgrimes * 3. All advertising materials mentioning features or use of this software
201573Srgrimes *    must display the following acknowledgment:
211573Srgrimes *      This product includes software developed by the University of
221573Srgrimes *      California, Berkeley and its contributors.
231573Srgrimes * 4. Neither the name of the University nor the names of its contributors
241573Srgrimes *    may be used to endorse or promote products derived from this software
251573Srgrimes *    without specific prior written permission.
261573Srgrimes *
271573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
281573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
291573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3084225Sdillon * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3184225Sdillon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3284225Sdillon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
331573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3428179Ssteve * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3511681Sbde * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3628179Ssteve * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
371573Srgrimes * SUCH DAMAGE.
381573Srgrimes *
391573Srgrimes *
4011681Sbde * File: am-utils/amd/info_hesiod.c
411573Srgrimes *
4211681Sbde */
4311681Sbde
441573Srgrimes/*
4511681Sbde * Get info from Hesiod
46116344Smarkm */
4711681Sbde
4811681Sbde#ifdef HAVE_CONFIG_H
491573Srgrimes# include <config.h>
501573Srgrimes#endif /* HAVE_CONFIG_H */
511573Srgrimes#include <am_defs.h>
52175352Sjhb#include <amd.h>
53175352Sjhb
54154835Scognet#define	HES_PREFIX	"hesiod."
55154835Scognet#define	HES_PREFLEN	7
56174818Sjhb
57154835Scognet#ifdef HAVE_HESIOD_INIT
58154835Scognet/* bsdi3 does not define this extern in any header file */
59183565Sedextern char **hesiod_resolve(void *context, const char *name, const char *type);
60154835Scognetextern int hesiod_init(void **context);
61154835Scognetstatic voidp hesiod_context;
62154835Scognet#endif /* HAVE_HESIOD_INIT */
63183565Sed
64183565Sed/* forward declarations */
65154835Scognetint amu_hesiod_init(mnt_map *m, char *map, time_t *tp);
66183565Sedint hesiod_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp);
67183565Sedint hesiod_isup(mnt_map *m, char *map);
68183565Sed
69174818Sjhb/*
70183565Sed * No easy way to probe the server - check the map name begins with "hesiod."
71183565Sed * Note: this name includes 'amu_' so as to not conflict with libhesiod's
72174818Sjhb * hesiod_init() function.
73183565Sed */
74183565Sedint
75174818Sjhbamu_hesiod_init(mnt_map *m, char *map, time_t *tp)
76174818Sjhb{
77183565Sed  dlog("amu_hesiod_init(%s)", map);
78183565Sed  *tp = 0;
79154835Scognet
80154835Scognet#ifdef HAVE_HESIOD_INIT
81154835Scognet  if (!hesiod_context && hesiod_init(&hesiod_context) != 0)
82154835Scognet    return ENOENT;
83154835Scognet#endif /* HAVE_HESIOD_INIT */
84175352Sjhb
85154835Scognet  return NSTREQ(map, HES_PREFIX, HES_PREFLEN) ? 0 : ENOENT;
86154835Scognet}
87154835Scognet
88154835Scognet
89154835Scognet/*
90154835Scognet * Do a Hesiod nameserver call.
91183565Sed * Modify time is ignored by Hesiod - XXX
92183565Sed */
93183565Sedint
94154835Scognethesiod_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
95154835Scognet{
9613137Speter  char hes_key[MAXPATHLEN];
97121193Smarkm  char **rvec;
981573Srgrimes#ifndef HAVE_HESIOD_INIT
991573Srgrimes  int error;
1001573Srgrimes#endif /* not HAVE_HESIOD_INIT */
1011573Srgrimes
1021573Srgrimes  dlog("hesiod_search(m=%lx, map=%s, key=%s, pval=%lx tp=%lx)",
1031573Srgrimes       (unsigned long) m, map, key, (unsigned long) pval, (unsigned long) tp);
1041573Srgrimes
1051573Srgrimes  if (key[0] == '.')
1061573Srgrimes    return ENOENT;
1078870Srgrimes
1081573Srgrimes  xsnprintf(hes_key, sizeof(hes_key), "%s.%s", key, map + HES_PREFLEN);
1091573Srgrimes
1101573Srgrimes  /*
1111573Srgrimes   * Call the resolver
1121573Srgrimes   */
1131573Srgrimes  dlog("Hesiod base is: %s\n", gopt.hesiod_base);
1141573Srgrimes  dlog("hesiod_search: hes_resolve(%s, %s)", hes_key, gopt.hesiod_base);
1151573Srgrimes  if (amuDebug(D_INFO))
1161573Srgrimes    _res.options |= RES_DEBUG;
1171573Srgrimes
1181573Srgrimes#ifdef HAVE_HESIOD_INIT
1191573Srgrimes  /* new style hesiod */
1201573Srgrimes  rvec = hesiod_resolve(hesiod_context, hes_key, gopt.hesiod_base);
121#else /* not HAVE_HESIOD_INIT */
122  rvec = hes_resolve(hes_key, gopt.hesiod_base);
123#endif /* not HAVE_HESIOD_INIT */
124
125  /*
126   * If a reply was forthcoming then return
127   * it (and free subsequent replies)
128   */
129  if (rvec && *rvec) {
130    *pval = *rvec;
131    while (*++rvec)
132      XFREE(*rvec);
133    return 0;
134  }
135
136#ifdef HAVE_HESIOD_INIT
137  /* new style hesiod */
138  return errno;
139#else /* not HAVE_HESIOD_INIT */
140  /*
141   * Otherwise reflect the hesiod error into a Un*x error
142   */
143  dlog("hesiod_search: Error: %d", hes_error());
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  dlog("hesiod_search: Returning: %d", error);
159  return error;
160#endif /* not HAVE_HESIOD_INIT */
161}
162
163
164/*
165 * Check if Hesiod is up, so we can determine if to clear the map or not.
166 * Test it by querying for /defaults.
167 * Returns: 0 if Hesiod is down, 1 if it is up.
168 */
169int
170hesiod_isup(mnt_map *m, char *map)
171{
172  int error;
173  char *val;
174  time_t mtime;
175  static int last_status = 1;	/* assume up by default */
176
177  error = hesiod_search(m, map, "/defaults", &val, &mtime);
178  dlog("hesiod_isup(%s): %s", map, strerror(error));
179  if (error != 0 && error != ENOENT) {
180    plog(XLOG_ERROR,
181	 "hesiod_isup: error getting `/defaults' entry in map %s: %m", map);
182    last_status = 0;
183    return 0;			/* Hesiod is down */
184  }
185  if (last_status == 0) {	/* if was down before */
186    plog(XLOG_INFO, "hesiod_isup: Hesiod came back up for map %s", map);
187    last_status = 1;
188  }
189  return 1;			/* Hesiod is up */
190}
191