xref: /netbsd-src/external/bsd/am-utils/dist/amd/info_nisplus.c (revision 413d532bcc3f62d122e56d92e13ac64825a40baf)
1 /*	$NetBSD: info_nisplus.c,v 1.1.1.2 2009/03/20 20:26:49 christos Exp $	*/
2 
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.
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. 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.
28  *
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.
40  *
41  *
42  * File: am-utils/amd/info_nisplus.c
43  *
44  */
45 
46 /*
47  * Get info from NIS+ (version 3) map
48  */
49 
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>
56 
57 #define NISPLUS_KEY "key="
58 #define NISPLUS_ORGDIR ".org_dir"
59 
60 struct nis_callback_data {
61   mnt_map *ncd_m;
62   char *ncd_map;
63   void (*ncd_fn)();
64 };
65 
66 struct nisplus_search_callback_data {
67   nis_name key;
68   char *value;
69 };
70 
71 
72 static int
73 nisplus_callback(const nis_name key, const nis_object *value, voidp opaquedata)
74 {
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;
78 
79   dlog("NISplus callback for <%s,%s>", kp, vp);
80 
81   (*data->ncd_fn) (data->ncd_m, kp, vp);
82 
83   /*
84    * We want more ...
85    */
86   return FALSE;
87 }
88 
89 
90 int
91 nisplus_reload(mnt_map *m, char *map, void (*fn) ())
92 {
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;
99 
100   org = strstr(map, NISPLUS_ORGDIR);
101   if (org == NULL)
102     org = NISPLUS_ORGDIR;
103   else
104     org = "";
105 
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;
113   }
114   xsnprintf(map_name, l, "%s%s", map, org);
115 
116   data.ncd_m = m;
117   data.ncd_map = map_name;
118   data.ncd_fn = fn;
119 
120   dlog("NISplus reload for %s", map);
121 
122   result = nis_list(map_name,
123 		    EXPAND_NAME | FOLLOW_LINKS | FOLLOW_PATH,
124 		    (int (*)()) nisplus_callback,
125 		    &data);
126 
127   /* free off the NIS map_name */
128   XFREE(map_name);
129 
130   if (result->status != NIS_SUCCESS && result->status != NIS_CBRESULTS)
131     error = 1;
132 
133   if (error)
134     plog(XLOG_ERROR, "error grabbing nisplus map of %s: %s",
135 	 map,
136 	 nis_sperrno(result->status));
137 
138   nis_freeresult(result);
139   return error;
140 }
141 
142 
143 static int
144 nisplus_search_callback(const nis_name key, const nis_object *value, voidp opaquedata)
145 {
146   struct nisplus_search_callback_data *data = (struct nisplus_search_callback_data *) opaquedata;
147 
148   dlog("NISplus search callback for <%s>", ENTRY_VAL(value, 0));
149   dlog("NISplus search callback value <%s>", ENTRY_VAL(value, 1));
150 
151   data->value = strnsave(ENTRY_VAL(value, 1), ENTRY_LEN(value, 1));
152   return TRUE;
153 }
154 
155 
156 /*
157  * Try to locate a key using NIS+.
158  */
159 int
160 nisplus_search(mnt_map *m, char *map, char *key, char **val, time_t *tp)
161 {
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;
168 
169   org = strstr(map, NISPLUS_ORGDIR);
170   if (org == NULL)
171     org = NISPLUS_ORGDIR;
172   else
173     org = "";
174 
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;
190   }
191   xsnprintf(index, l, "[%s%s],%s%s", NISPLUS_KEY, key, map, org);
192 
193   data.key = key;
194   data.value = NULL;
195 
196   dlog("NISplus search for %s", index);
197 
198   result = nis_list(index,
199 		    EXPAND_NAME | FOLLOW_LINKS | FOLLOW_PATH,
200 		    (int (*)()) nisplus_search_callback,
201 		    &data);
202 
203   /* free off the NIS index */
204   XFREE(index);
205 
206   if (result == NULL) {
207     plog(XLOG_ERROR, "nisplus_search: %s: %s", map, strerror(ENOMEM));
208     return ENOMEM;
209   }
210 
211   /*
212    * Do something interesting with the return code
213    */
214   switch (result->status) {
215   case NIS_SUCCESS:
216   case NIS_CBRESULTS:
217 
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);
223 
224       if (value != NULL)
225 	data.value = strnsave(ENTRY_VAL(value, 1), ENTRY_LEN(value, 1));
226     }
227 
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;
233 
234     if (*val) {
235       error = 0;
236       dlog("NISplus search found %s", *val);
237     } else {
238       error = ENOENT;
239       dlog("NISplus search found nothing");
240     }
241 
242     *tp = 0;
243     break;
244 
245   case NIS_NOSUCHNAME:
246     dlog("NISplus search returned %d", result->status);
247     error = ENOENT;
248     break;
249 
250   default:
251     plog(XLOG_ERROR, "nisplus_search: %s: %s", map, nis_sperrno(result->status));
252     error = EIO;
253     break;
254   }
255   nis_freeresult(result);
256 
257   return error;
258 }
259 
260 
261 int
262 nisplus_init(mnt_map *m, char *map, time_t *tp)
263 {
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;
269 
270   org = strstr(map, NISPLUS_ORGDIR);
271   if (org == NULL)
272     org = NISPLUS_ORGDIR;
273   else
274     org = "";
275 
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;
285   }
286   xsnprintf(map_name, l, "%s%s", map, org);
287 
288   result = nis_lookup(map_name, (EXPAND_NAME | FOLLOW_LINKS | FOLLOW_PATH));
289 
290   /* free off the NIS map_name */
291   XFREE(map_name);
292 
293   if (result == NULL) {
294     plog(XLOG_ERROR, "NISplus init <%s>: %s", map, strerror(ENOMEM));
295     return ENOMEM;
296   }
297 
298   if (result->status != NIS_SUCCESS) {
299     dlog("NISplus init <%s>: %s (%d)",
300 	 map, nis_sperrno(result->status), result->status);
301 
302     error = ENOENT;
303   }
304 
305   *tp = 0;			/* no time */
306   nis_freeresult(result);
307   return error;
308 }
309 
310 
311 int
312 nisplus_mtime(mnt_map *m, char *map, time_t *tp)
313 {
314   return nisplus_init(m,map, tp);
315 }
316