No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / am-utils / dist / amd / info_hesiod.c
blobcc9d35578b35cae5c68f13b7696431ee198ae8d5
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 1997-2009 Erez Zadok
5 * Copyright (c) 1989 Jan-Simon Pendry
6 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
7 * Copyright (c) 1989 The Regents of the University of California.
8 * All rights reserved.
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgment:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
42 * File: am-utils/amd/info_hesiod.c
47 * Get info from Hesiod
50 #ifdef HAVE_CONFIG_H
51 # include <config.h>
52 #endif /* HAVE_CONFIG_H */
53 #include <am_defs.h>
54 #include <amd.h>
55 #include <sun_map.h>
57 #define HES_PREFIX "hesiod."
58 #define HES_PREFLEN 7
60 #ifdef HAVE_HESIOD_INIT
61 /* bsdi3 does not define this extern in any header file */
62 extern char **hesiod_resolve(void *context, const char *name, const char *type);
63 extern int hesiod_init(void **context);
64 static voidp hesiod_context;
65 #endif /* HAVE_HESIOD_INIT */
67 /* forward declarations */
68 int amu_hesiod_init(mnt_map *m, char *map, time_t *tp);
69 int hesiod_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp);
70 int hesiod_isup(mnt_map *m, char *map);
73 * No easy way to probe the server - check the map name begins with "hesiod."
74 * Note: this name includes 'amu_' so as to not conflict with libhesiod's
75 * hesiod_init() function.
77 int
78 amu_hesiod_init(mnt_map *m, char *map, time_t *tp)
80 dlog("amu_hesiod_init(%s)", map);
81 *tp = 0;
83 #ifdef HAVE_HESIOD_INIT
84 if (!hesiod_context && hesiod_init(&hesiod_context) != 0)
85 return ENOENT;
86 #endif /* HAVE_HESIOD_INIT */
88 return NSTREQ(map, HES_PREFIX, HES_PREFLEN) ? 0 : ENOENT;
93 * Do a Hesiod nameserver call.
94 * Modify time is ignored by Hesiod - XXX
96 int
97 hesiod_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
99 char hes_key[MAXPATHLEN];
100 char **rvec;
101 #ifndef HAVE_HESIOD_INIT
102 int error;
103 #endif /* not HAVE_HESIOD_INIT */
105 dlog("hesiod_search(m=%lx, map=%s, key=%s, pval=%lx tp=%lx)",
106 (unsigned long) m, map, key, (unsigned long) pval, (unsigned long) tp);
108 if (key[0] == '.')
109 return ENOENT;
111 xsnprintf(hes_key, sizeof(hes_key), "%s.%s", key, map + HES_PREFLEN);
114 * Call the resolver
116 dlog("Hesiod base is: %s\n", gopt.hesiod_base);
117 dlog("hesiod_search: hes_resolve(%s, %s)", hes_key, gopt.hesiod_base);
118 if (amuDebug(D_INFO))
119 _res.options |= RES_DEBUG;
121 #ifdef HAVE_HESIOD_INIT
122 /* new style hesiod */
123 rvec = hesiod_resolve(hesiod_context, hes_key, gopt.hesiod_base);
124 #else /* not HAVE_HESIOD_INIT */
125 rvec = hes_resolve(hes_key, gopt.hesiod_base);
126 #endif /* not HAVE_HESIOD_INIT */
129 * If a reply was forthcoming then return
130 * it (and free subsequent replies)
132 if (rvec && *rvec) {
133 if (m->cfm && (m->cfm->cfm_flags & CFM_SUN_MAP_SYNTAX)) {
134 *pval = sun_entry2amd(key, *rvec);
135 XFREE(*rvec);
136 } else
137 *pval = *rvec;
138 while (*++rvec)
139 XFREE(*rvec);
140 return 0;
143 #ifdef HAVE_HESIOD_INIT
144 /* new style hesiod */
145 return errno;
146 #else /* not HAVE_HESIOD_INIT */
148 * Otherwise reflect the hesiod error into a Un*x error
150 dlog("hesiod_search: Error: %d", hes_error());
151 switch (hes_error()) {
152 case HES_ER_NOTFOUND:
153 error = ENOENT;
154 break;
155 case HES_ER_CONFIG:
156 error = EIO;
157 break;
158 case HES_ER_NET:
159 error = ETIMEDOUT;
160 break;
161 default:
162 error = EINVAL;
163 break;
165 dlog("hesiod_search: Returning: %d", error);
166 return error;
167 #endif /* not HAVE_HESIOD_INIT */
172 * Check if Hesiod is up, so we can determine if to clear the map or not.
173 * Test it by querying for /defaults.
174 * Returns: 0 if Hesiod is down, 1 if it is up.
177 hesiod_isup(mnt_map *m, char *map)
179 int error;
180 char *val;
181 time_t mtime;
182 static int last_status = 1; /* assume up by default */
184 error = hesiod_search(m, map, "/defaults", &val, &mtime);
185 dlog("hesiod_isup(%s): %s", map, strerror(error));
186 if (error != 0 && error != ENOENT) {
187 plog(XLOG_ERROR,
188 "hesiod_isup: error getting `/defaults' entry in map %s: %m", map);
189 last_status = 0;
190 return 0; /* Hesiod is down */
192 if (last_status == 0) { /* if was down before */
193 plog(XLOG_INFO, "hesiod_isup: Hesiod came back up for map %s", map);
194 last_status = 1;
196 return 1; /* Hesiod is up */