1 /* $NetBSD: info_nisplus.c,v 1.1.1.3 2015/01/17 16:34:15 christos Exp $ */
2
3 /*
4 * Copyright (c) 1997-2014 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.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
12 *
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. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 *
38 * File: am-utils/amd/info_nisplus.c
39 *
40 */
41
42 /*
43 * Get info from NIS+ (version 3) map
44 */
45
46 #ifdef HAVE_CONFIG_H
47 # include <config.h>
48 #endif /* HAVE_CONFIG_H */
49 #include <am_defs.h>
50 #include <amd.h>
51 #include <sun_map.h>
52
53 #define NISPLUS_KEY "key="
54 #define NISPLUS_ORGDIR ".org_dir"
55
56 struct nis_callback_data {
57 mnt_map *ncd_m;
58 char *ncd_map;
59 void (*ncd_fn)();
60 };
61
62 struct nisplus_search_callback_data {
63 nis_name key;
64 char *value;
65 };
66
67
68 static int
nisplus_callback(const nis_name key,const nis_object * value,voidp opaquedata)69 nisplus_callback(const nis_name key, const nis_object *value, voidp opaquedata)
70 {
71 char *kp = strnsave(ENTRY_VAL(value, 0), ENTRY_LEN(value, 0));
72 char *vp = strnsave(ENTRY_VAL(value, 1), ENTRY_LEN(value, 1));
73 struct nis_callback_data *data = (struct nis_callback_data *) opaquedata;
74
75 dlog("NISplus callback for <%s,%s>", kp, vp);
76
77 (*data->ncd_fn) (data->ncd_m, kp, vp);
78
79 /*
80 * We want more ...
81 */
82 return FALSE;
83 }
84
85
86 int
nisplus_reload(mnt_map * m,char * map,void (* fn)())87 nisplus_reload(mnt_map *m, char *map, void (*fn) ())
88 {
89 int error = 0;
90 struct nis_callback_data data;
91 nis_result *result;
92 char *org; /* if map does not have ".org_dir" then append it */
93 nis_name map_name;
94 size_t l;
95
96 org = strstr(map, NISPLUS_ORGDIR);
97 if (org == NULL)
98 org = NISPLUS_ORGDIR;
99 else
100 org = "";
101
102 /* make some room for the NIS map_name */
103 l = strlen(map) + sizeof(NISPLUS_ORGDIR);
104 map_name = xmalloc(l);
105 if (map_name == NULL) {
106 plog(XLOG_ERROR, "Unable to create map_name %s: %s",
107 map, strerror(ENOMEM));
108 return ENOMEM;
109 }
110 xsnprintf(map_name, l, "%s%s", map, org);
111
112 data.ncd_m = m;
113 data.ncd_map = map_name;
114 data.ncd_fn = fn;
115
116 dlog("NISplus reload for %s", map);
117
118 result = nis_list(map_name,
119 EXPAND_NAME | FOLLOW_LINKS | FOLLOW_PATH,
120 (int (*)()) nisplus_callback,
121 &data);
122
123 /* free off the NIS map_name */
124 XFREE(map_name);
125
126 if (result->status != NIS_SUCCESS && result->status != NIS_CBRESULTS)
127 error = 1;
128
129 if (error)
130 plog(XLOG_ERROR, "error grabbing nisplus map of %s: %s",
131 map,
132 nis_sperrno(result->status));
133
134 nis_freeresult(result);
135 return error;
136 }
137
138
139 static int
nisplus_search_callback(const nis_name key,const nis_object * value,voidp opaquedata)140 nisplus_search_callback(const nis_name key, const nis_object *value, voidp opaquedata)
141 {
142 struct nisplus_search_callback_data *data = (struct nisplus_search_callback_data *) opaquedata;
143
144 dlog("NISplus search callback for <%s>", ENTRY_VAL(value, 0));
145 dlog("NISplus search callback value <%s>", ENTRY_VAL(value, 1));
146
147 data->value = strnsave(ENTRY_VAL(value, 1), ENTRY_LEN(value, 1));
148 return TRUE;
149 }
150
151
152 /*
153 * Try to locate a key using NIS+.
154 */
155 int
nisplus_search(mnt_map * m,char * map,char * key,char ** val,time_t * tp)156 nisplus_search(mnt_map *m, char *map, char *key, char **val, time_t *tp)
157 {
158 nis_result *result;
159 int error = 0;
160 struct nisplus_search_callback_data data;
161 nis_name index;
162 char *org; /* if map does not have ".org_dir" then append it */
163 size_t l;
164
165 org = strstr(map, NISPLUS_ORGDIR);
166 if (org == NULL)
167 org = NISPLUS_ORGDIR;
168 else
169 org = "";
170
171 /* make some room for the NIS index */
172 l = sizeof('[') /* for opening selection criteria */
173 + sizeof(NISPLUS_KEY)
174 + strlen(key)
175 + sizeof(']') /* for closing selection criteria */
176 + sizeof(',') /* + 1 for , separator */
177 + strlen(map)
178 + sizeof(NISPLUS_ORGDIR);
179 index = xmalloc(l);
180 if (index == NULL) {
181 plog(XLOG_ERROR,
182 "Unable to create index %s: %s",
183 map,
184 strerror(ENOMEM));
185 return ENOMEM;
186 }
187 xsnprintf(index, l, "[%s%s],%s%s", NISPLUS_KEY, key, map, org);
188
189 data.key = key;
190 data.value = NULL;
191
192 dlog("NISplus search for %s", index);
193
194 result = nis_list(index,
195 EXPAND_NAME | FOLLOW_LINKS | FOLLOW_PATH,
196 (int (*)()) nisplus_search_callback,
197 &data);
198
199 /* free off the NIS index */
200 XFREE(index);
201
202 if (result == NULL) {
203 plog(XLOG_ERROR, "nisplus_search: %s: %s", map, strerror(ENOMEM));
204 return ENOMEM;
205 }
206
207 /*
208 * Do something interesting with the return code
209 */
210 switch (result->status) {
211 case NIS_SUCCESS:
212 case NIS_CBRESULTS:
213
214 if (data.value == NULL) {
215 nis_object *value = result->objects.objects_val;
216 dlog("NISplus search found <nothing>");
217 dlog("NISplus search for %s: %s(%d)",
218 map, nis_sperrno(result->status), result->status);
219
220 if (value != NULL)
221 data.value = strnsave(ENTRY_VAL(value, 1), ENTRY_LEN(value, 1));
222 }
223
224 if (m->cfm && (m->cfm->cfm_flags & CFM_SUN_MAP_SYNTAX)) {
225 *val = sun_entry2amd(key, data.value);
226 XFREE(data.value); /* strnsave malloc'ed it above */
227 } else
228 *val = data.value;
229
230 if (*val) {
231 error = 0;
232 dlog("NISplus search found %s", *val);
233 } else {
234 error = ENOENT;
235 dlog("NISplus search found nothing");
236 }
237
238 *tp = 0;
239 break;
240
241 case NIS_NOSUCHNAME:
242 dlog("NISplus search returned %d", result->status);
243 error = ENOENT;
244 break;
245
246 default:
247 plog(XLOG_ERROR, "nisplus_search: %s: %s", map, nis_sperrno(result->status));
248 error = EIO;
249 break;
250 }
251 nis_freeresult(result);
252
253 return error;
254 }
255
256
257 int
nisplus_init(mnt_map * m,char * map,time_t * tp)258 nisplus_init(mnt_map *m, char *map, time_t *tp)
259 {
260 nis_result *result;
261 char *org; /* if map does not have ".org_dir" then append it */
262 nis_name map_name;
263 int error = 0;
264 size_t l;
265
266 org = strstr(map, NISPLUS_ORGDIR);
267 if (org == NULL)
268 org = NISPLUS_ORGDIR;
269 else
270 org = "";
271
272 /* make some room for the NIS map_name */
273 l = strlen(map) + sizeof(NISPLUS_ORGDIR);
274 map_name = xmalloc(l);
275 if (map_name == NULL) {
276 plog(XLOG_ERROR,
277 "Unable to create map_name %s: %s",
278 map,
279 strerror(ENOMEM));
280 return ENOMEM;
281 }
282 xsnprintf(map_name, l, "%s%s", map, org);
283
284 result = nis_lookup(map_name, (EXPAND_NAME | FOLLOW_LINKS | FOLLOW_PATH));
285
286 /* free off the NIS map_name */
287 XFREE(map_name);
288
289 if (result == NULL) {
290 plog(XLOG_ERROR, "NISplus init <%s>: %s", map, strerror(ENOMEM));
291 return ENOMEM;
292 }
293
294 if (result->status != NIS_SUCCESS) {
295 dlog("NISplus init <%s>: %s (%d)",
296 map, nis_sperrno(result->status), result->status);
297
298 error = ENOENT;
299 }
300
301 *tp = 0; /* no time */
302 nis_freeresult(result);
303 return error;
304 }
305
306
307 int
nisplus_mtime(mnt_map * m,char * map,time_t * tp)308 nisplus_mtime(mnt_map *m, char *map, time_t *tp)
309 {
310 return nisplus_init(m,map, tp);
311 }
312