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.
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
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
42 * File: am-utils/amd/info_nisplus.c
47 * Get info from NIS+ (version 3) map
52 #endif /* HAVE_CONFIG_H */
57 #define NISPLUS_KEY "key="
58 #define NISPLUS_ORGDIR ".org_dir"
60 struct nis_callback_data
{
66 struct nisplus_search_callback_data
{
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
);
91 nisplus_reload(mnt_map
*m
, char *map
, void (*fn
) ())
94 struct nis_callback_data data
;
96 char *org
; /* if map does not have ".org_dir" then append it */
100 org
= strstr(map
, NISPLUS_ORGDIR
);
102 org
= NISPLUS_ORGDIR
;
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
));
114 xsnprintf(map_name
, l
, "%s%s", map
, org
);
117 data
.ncd_map
= map_name
;
120 dlog("NISplus reload for %s", map
);
122 result
= nis_list(map_name
,
123 EXPAND_NAME
| FOLLOW_LINKS
| FOLLOW_PATH
,
124 (int (*)()) nisplus_callback
,
127 /* free off the NIS map_name */
130 if (result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_CBRESULTS
)
134 plog(XLOG_ERROR
, "error grabbing nisplus map of %s: %s",
136 nis_sperrno(result
->status
));
138 nis_freeresult(result
);
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));
157 * Try to locate a key using NIS+.
160 nisplus_search(mnt_map
*m
, char *map
, char *key
, char **val
, time_t *tp
)
164 struct nisplus_search_callback_data data
;
166 char *org
; /* if map does not have ".org_dir" then append it */
169 org
= strstr(map
, NISPLUS_ORGDIR
);
171 org
= NISPLUS_ORGDIR
;
175 /* make some room for the NIS index */
176 l
= sizeof('[') /* for opening selection criteria */
177 + sizeof(NISPLUS_KEY
)
179 + sizeof(']') /* for closing selection criteria */
180 + sizeof(',') /* + 1 for , separator */
182 + sizeof(NISPLUS_ORGDIR
);
186 "Unable to create index %s: %s",
191 xsnprintf(index
, l
, "[%s%s],%s%s", NISPLUS_KEY
, key
, map
, org
);
196 dlog("NISplus search for %s", index
);
198 result
= nis_list(index
,
199 EXPAND_NAME
| FOLLOW_LINKS
| FOLLOW_PATH
,
200 (int (*)()) nisplus_search_callback
,
203 /* free off the NIS index */
206 if (result
== NULL
) {
207 plog(XLOG_ERROR
, "nisplus_search: %s: %s", map
, strerror(ENOMEM
));
212 * Do something interesting with the return code
214 switch (result
->status
) {
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
);
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 */
236 dlog("NISplus search found %s", *val
);
239 dlog("NISplus search found nothing");
246 dlog("NISplus search returned %d", result
->status
);
251 plog(XLOG_ERROR
, "nisplus_search: %s: %s", map
, nis_sperrno(result
->status
));
255 nis_freeresult(result
);
262 nisplus_init(mnt_map
*m
, char *map
, time_t *tp
)
265 char *org
; /* if map does not have ".org_dir" then append it */
270 org
= strstr(map
, NISPLUS_ORGDIR
);
272 org
= NISPLUS_ORGDIR
;
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
) {
281 "Unable to create map_name %s: %s",
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 */
293 if (result
== NULL
) {
294 plog(XLOG_ERROR
, "NISplus init <%s>: %s", map
, strerror(ENOMEM
));
298 if (result
->status
!= NIS_SUCCESS
) {
299 dlog("NISplus init <%s>: %s (%d)",
300 map
, nis_sperrno(result
->status
), result
->status
);
305 *tp
= 0; /* no time */
306 nis_freeresult(result
);
312 nisplus_mtime(mnt_map
*m
, char *map
, time_t *tp
)
314 return nisplus_init(m
,map
, tp
);