Merge from vendor branch GDB:
[dragonfly.git] / contrib / amd / amd / info_hesiod.c
1 /*
2  * Copyright (c) 1997-1999 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 acknowledgment:
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.5 1999/02/04 07:24:15 ezk Exp $
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 */
60 extern char **hesiod_resolve(void *context, const char *name, const char *type);
61 extern int hesiod_init(void **context);
62 static voidp hesiod_context;
63 #endif /* HAVE_HESIOD_INIT */
64
65 /* forward declarations */
66 int amu_hesiod_init(mnt_map *m, char *map, time_t *tp);
67 int hesiod_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp);
68 int hesiod_isup(mnt_map *m, char *map);
69
70 /*
71  * No easy way to probe the server - check the map name begins with "hesiod."
72  * Note: this name includes 'amu_' so as to not conflict with libhesiod's
73  * hesiod_init() function.
74  */
75 int
76 amu_hesiod_init(mnt_map *m, char *map, time_t *tp)
77 {
78 #ifdef DEBUG
79   dlog("amu_hesiod_init(%s)", map);
80 #endif /* DEBUG */
81   *tp = 0;
82
83 #ifdef HAVE_HESIOD_INIT
84   if(!hesiod_context && hesiod_init(&hesiod_context) != 0)
85     return ENOENT;
86 #endif /* HAVE_HESIOD_INIT */
87
88   return NSTREQ(map, HES_PREFIX, HES_PREFLEN) ? 0 : ENOENT;
89 }
90
91
92 /*
93  * Do a Hesiod nameserver call.
94  * Modify time is ignored by Hesiod - XXX
95  */
96 int
97 hesiod_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
98 {
99   char hes_key[MAXPATHLEN];
100   char **rvec;
101 #ifndef HAVE_HESIOD_INIT
102   int error;
103 #endif /* not HAVE_HESIOD_INIT */
104
105 #ifdef DEBUG
106   dlog("hesiod_search(m=%lx, map=%s, key=%s, pval=%lx tp=%lx)",
107        (unsigned long) m, map, key, (unsigned long) pval, (unsigned long) tp);
108 #endif /* DEBUG */
109
110   sprintf(hes_key, "%s.%s", key, map + HES_PREFLEN);
111
112   /*
113    * Call the resolver
114    */
115 #ifdef DEBUG
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 (debug_flags & D_INFO)
119     _res.options |= RES_DEBUG;
120 #endif /* DEBUG */
121
122 #ifdef HAVE_HESIOD_INIT
123   /* new style hesiod */
124   rvec = hesiod_resolve(hesiod_context, hes_key, gopt.hesiod_base);
125 #else /* not HAVE_HESIOD_INIT */
126   rvec = hes_resolve(hes_key, gopt.hesiod_base);
127 #endif /* not HAVE_HESIOD_INIT */
128
129   /*
130    * If a reply was forthcoming then return
131    * it (and free subsequent replies)
132    */
133   if (rvec && *rvec) {
134     *pval = *rvec;
135     while (*++rvec)
136       XFREE(*rvec);
137     return 0;
138   }
139
140 #ifdef HAVE_HESIOD_INIT
141   /* new style hesiod */
142   return errno;
143 #else /* not HAVE_HESIOD_INIT */
144   /*
145    * Otherwise reflect the hesiod error into a Un*x error
146    */
147 # ifdef DEBUG
148   dlog("hesiod_search: Error: %d", hes_error());
149 # endif /* DEBUG */
150   switch (hes_error()) {
151   case HES_ER_NOTFOUND:
152     error = ENOENT;
153     break;
154   case HES_ER_CONFIG:
155     error = EIO;
156     break;
157   case HES_ER_NET:
158     error = ETIMEDOUT;
159     break;
160   default:
161     error = EINVAL;
162     break;
163   }
164 # ifdef DEBUG
165   dlog("hesiod_search: Returning: %d", error);
166 # endif /* DEBUG */
167   return error;
168 #endif /* not HAVE_HESIOD_INIT */
169 }
170
171
172 /*
173  * Check if Hesiod is up, so we can determine if to clear the map or not.
174  * Test it by querying for /defaults.
175  * Returns: 0 if Hesiod is down, 1 if it is up.
176  */
177 int
178 hesiod_isup(mnt_map *m, char *map)
179 {
180   int error;
181   char *val;
182   time_t mtime;
183   static int last_status = 1;   /* assume up by default */
184
185   error = hesiod_search(m, map, "/defaults", &val, &mtime);
186 #ifdef DEBUG
187   dlog("hesiod_isup(%s): %s", map, strerror(error));
188 #endif /* DEBUG */
189   if (error != 0 && error != ENOENT) {
190     plog(XLOG_ERROR,
191          "hesiod_isup: error getting `/defaults' entry in map %s: %m", map);
192     last_status = 0;
193     return 0;                   /* Hesiod is down */
194   }
195   if (last_status == 0) {       /* if was down before */
196     plog(XLOG_INFO, "hesiod_isup: Hesiod came back up for map %s", map);
197     last_status = 1;
198   }
199   return 1;                     /* Hesiod is up */
200 }