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