No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / am-utils / dist / amd / info_nisplus.c
blob03b04defcb1d2663672ee0c5a7dd95a0b9c00ad5
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_nisplus.c
47 * Get info from NIS+ (version 3) map
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 NISPLUS_KEY "key="
58 #define NISPLUS_ORGDIR ".org_dir"
60 struct nis_callback_data {
61 mnt_map *ncd_m;
62 char *ncd_map;
63 void (*ncd_fn)();
66 struct nisplus_search_callback_data {
67 nis_name key;
68 char *value;
72 static int
73 nisplus_callback(const nis_name key, const nis_object *value, voidp opaquedata)
75 char *kp = strnsave(ENTRY_VAL(value, 0), ENTRY_LEN(value, 0));
76 char *vp = strnsave(ENTRY_VAL(value, 1), ENTRY_LEN(value, 1));
77 struct nis_callback_data *data = (struct nis_callback_data *) opaquedata;
79 dlog("NISplus callback for <%s,%s>", kp, vp);
81 (*data->ncd_fn) (data->ncd_m, kp, vp);
84 * We want more ...
86 return FALSE;
90 int
91 nisplus_reload(mnt_map *m, char *map, void (*fn) ())
93 int error = 0;
94 struct nis_callback_data data;
95 nis_result *result;
96 char *org; /* if map does not have ".org_dir" then append it */
97 nis_name map_name;
98 size_t l;
100 org = strstr(map, NISPLUS_ORGDIR);
101 if (org == NULL)
102 org = NISPLUS_ORGDIR;
103 else
104 org = "";
106 /* make some room for the NIS map_name */
107 l = strlen(map) + sizeof(NISPLUS_ORGDIR);
108 map_name = xmalloc(l);
109 if (map_name == NULL) {
110 plog(XLOG_ERROR, "Unable to create map_name %s: %s",
111 map, strerror(ENOMEM));
112 return ENOMEM;
114 xsnprintf(map_name, l, "%s%s", map, org);
116 data.ncd_m = m;
117 data.ncd_map = map_name;
118 data.ncd_fn = fn;
120 dlog("NISplus reload for %s", map);
122 result = nis_list(map_name,
123 EXPAND_NAME | FOLLOW_LINKS | FOLLOW_PATH,
124 (int (*)()) nisplus_callback,
125 &data);
127 /* free off the NIS map_name */
128 XFREE(map_name);
130 if (result->status != NIS_SUCCESS && result->status != NIS_CBRESULTS)
131 error = 1;
133 if (error)
134 plog(XLOG_ERROR, "error grabbing nisplus map of %s: %s",
135 map,
136 nis_sperrno(result->status));
138 nis_freeresult(result);
139 return error;
143 static int
144 nisplus_search_callback(const nis_name key, const nis_object *value, voidp opaquedata)
146 struct nisplus_search_callback_data *data = (struct nisplus_search_callback_data *) opaquedata;
148 dlog("NISplus search callback for <%s>", ENTRY_VAL(value, 0));
149 dlog("NISplus search callback value <%s>", ENTRY_VAL(value, 1));
151 data->value = strnsave(ENTRY_VAL(value, 1), ENTRY_LEN(value, 1));
152 return TRUE;
157 * Try to locate a key using NIS+.
160 nisplus_search(mnt_map *m, char *map, char *key, char **val, time_t *tp)
162 nis_result *result;
163 int error = 0;
164 struct nisplus_search_callback_data data;
165 nis_name index;
166 char *org; /* if map does not have ".org_dir" then append it */
167 size_t l;
169 org = strstr(map, NISPLUS_ORGDIR);
170 if (org == NULL)
171 org = NISPLUS_ORGDIR;
172 else
173 org = "";
175 /* make some room for the NIS index */
176 l = sizeof('[') /* for opening selection criteria */
177 + sizeof(NISPLUS_KEY)
178 + strlen(key)
179 + sizeof(']') /* for closing selection criteria */
180 + sizeof(',') /* + 1 for , separator */
181 + strlen(map)
182 + sizeof(NISPLUS_ORGDIR);
183 index = xmalloc(l);
184 if (index == NULL) {
185 plog(XLOG_ERROR,
186 "Unable to create index %s: %s",
187 map,
188 strerror(ENOMEM));
189 return ENOMEM;
191 xsnprintf(index, l, "[%s%s],%s%s", NISPLUS_KEY, key, map, org);
193 data.key = key;
194 data.value = NULL;
196 dlog("NISplus search for %s", index);
198 result = nis_list(index,
199 EXPAND_NAME | FOLLOW_LINKS | FOLLOW_PATH,
200 (int (*)()) nisplus_search_callback,
201 &data);
203 /* free off the NIS index */
204 XFREE(index);
206 if (result == NULL) {
207 plog(XLOG_ERROR, "nisplus_search: %s: %s", map, strerror(ENOMEM));
208 return ENOMEM;
212 * Do something interesting with the return code
214 switch (result->status) {
215 case NIS_SUCCESS:
216 case NIS_CBRESULTS:
218 if (data.value == NULL) {
219 nis_object *value = result->objects.objects_val;
220 dlog("NISplus search found <nothing>");
221 dlog("NISplus search for %s: %s(%d)",
222 map, nis_sperrno(result->status), result->status);
224 if (value != NULL)
225 data.value = strnsave(ENTRY_VAL(value, 1), ENTRY_LEN(value, 1));
228 if (m->cfm && (m->cfm->cfm_flags & CFM_SUN_MAP_SYNTAX)) {
229 *val = sun_entry2amd(key, data.value);
230 XFREE(data.value); /* strnsave malloc'ed it above */
231 } else
232 *val = data.value;
234 if (*val) {
235 error = 0;
236 dlog("NISplus search found %s", *val);
237 } else {
238 error = ENOENT;
239 dlog("NISplus search found nothing");
242 *tp = 0;
243 break;
245 case NIS_NOSUCHNAME:
246 dlog("NISplus search returned %d", result->status);
247 error = ENOENT;
248 break;
250 default:
251 plog(XLOG_ERROR, "nisplus_search: %s: %s", map, nis_sperrno(result->status));
252 error = EIO;
253 break;
255 nis_freeresult(result);
257 return error;
262 nisplus_init(mnt_map *m, char *map, time_t *tp)
264 nis_result *result;
265 char *org; /* if map does not have ".org_dir" then append it */
266 nis_name map_name;
267 int error = 0;
268 size_t l;
270 org = strstr(map, NISPLUS_ORGDIR);
271 if (org == NULL)
272 org = NISPLUS_ORGDIR;
273 else
274 org = "";
276 /* make some room for the NIS map_name */
277 l = strlen(map) + sizeof(NISPLUS_ORGDIR);
278 map_name = xmalloc(l);
279 if (map_name == NULL) {
280 plog(XLOG_ERROR,
281 "Unable to create map_name %s: %s",
282 map,
283 strerror(ENOMEM));
284 return ENOMEM;
286 xsnprintf(map_name, l, "%s%s", map, org);
288 result = nis_lookup(map_name, (EXPAND_NAME | FOLLOW_LINKS | FOLLOW_PATH));
290 /* free off the NIS map_name */
291 XFREE(map_name);
293 if (result == NULL) {
294 plog(XLOG_ERROR, "NISplus init <%s>: %s", map, strerror(ENOMEM));
295 return ENOMEM;
298 if (result->status != NIS_SUCCESS) {
299 dlog("NISplus init <%s>: %s (%d)",
300 map, nis_sperrno(result->status), result->status);
302 error = ENOENT;
305 *tp = 0; /* no time */
306 nis_freeresult(result);
307 return error;
312 nisplus_mtime(mnt_map *m, char *map, time_t *tp)
314 return nisplus_init(m,map, tp);