[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[glibc.git] / nis / nss_nisplus / nisplus-ethers.c
blob8d69ad93738c8f4bdaf54287f2fa7fa4a21e1088
1 /* Copyright (C) 1997,1998,2000-2003,2005,2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <atomic.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <inttypes.h>
24 #include <netdb.h>
25 #include <nss.h>
26 #include <string.h>
27 #include <netinet/ether.h>
28 #include <netinet/if_ether.h>
29 #include <rpcsvc/nis.h>
30 #include <bits/libc-lock.h>
32 #include "nss-nisplus.h"
34 __libc_lock_define_initialized (static, lock)
36 static nis_result *result;
37 static nis_name tablename_val;
38 static u_long tablename_len;
41 #define NISENTRYVAL(idx, col, res) \
42 (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
44 #define NISENTRYLEN(idx, col, res) \
45 (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
47 static int
48 _nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether,
49 char *buffer, size_t buflen, int *errnop)
51 char *p = buffer;
52 size_t room_left = buflen;
54 if (result == NULL)
55 return 0;
57 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
58 || NIS_RES_NUMOBJ (result) != 1
59 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
60 || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
61 "ethers_tbl") != 0
62 || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 2)
63 return 0;
65 /* Generate the ether entry format and use the normal parser */
66 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
68 *errnop = ERANGE;
69 return -1;
71 char *cp = __stpncpy (p, NISENTRYVAL (0, 0, result),
72 NISENTRYLEN (0, 0, result));
73 *cp = '\0';
74 room_left -= NISENTRYLEN (0, 0, result) + 1;
75 ether->e_name = p;
77 struct ether_addr *ea = ether_aton (NISENTRYVAL (0, 1, result));
78 if (ea == NULL)
80 *errnop = EINVAL;
81 return -2;
84 ether->e_addr = *ea;
86 return 1;
89 static enum nss_status
90 _nss_create_tablename (int *errnop)
92 if (tablename_val == NULL)
94 const char *local_dir = nis_local_directory ();
95 size_t local_dir_len = strlen (local_dir);
96 static const char prefix[] = "ethers.org_dir.";
98 char *p = malloc (sizeof (prefix) + local_dir_len);
99 if (p == NULL)
101 *errnop = errno;
102 return NSS_STATUS_TRYAGAIN;
105 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
107 tablename_len = sizeof (prefix) - 1 + local_dir_len;
109 atomic_write_barrier ();
111 tablename_val = p;
113 return NSS_STATUS_SUCCESS;
117 enum nss_status
118 _nss_nisplus_setetherent (int stayopen)
120 enum nss_status status;
121 int err;
123 status = NSS_STATUS_SUCCESS;
125 __libc_lock_lock (lock);
127 if (result != NULL)
129 nis_freeresult (result);
130 result = NULL;
133 if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
134 status = NSS_STATUS_UNAVAIL;
136 __libc_lock_unlock (lock);
138 return NSS_STATUS_SUCCESS;
141 enum nss_status
142 _nss_nisplus_endetherent (void)
144 __libc_lock_lock (lock);
146 if (result != NULL)
148 nis_freeresult (result);
149 result = NULL;
152 __libc_lock_unlock (lock);
154 return NSS_STATUS_SUCCESS;
157 static enum nss_status
158 internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
159 size_t buflen, int *errnop)
161 if (tablename_val == NULL)
163 enum nss_status status = _nss_create_tablename (errnop);
165 if (status != NSS_STATUS_SUCCESS)
166 return status;
169 /* Get the next entry until we found a correct one. */
170 int parse_res;
173 nis_result *saved_result;
175 if (result == NULL)
177 saved_result = NULL;
178 result = nis_first_entry (tablename_val);
179 if (result == NULL)
181 *errnop = errno;
182 return NSS_STATUS_TRYAGAIN;
184 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
185 return niserr2nss (result->status);
187 else
189 saved_result = result;
190 result = nis_next_entry (tablename_val, &result->cookie);
191 if (result == NULL)
193 *errnop = errno;
194 return NSS_STATUS_TRYAGAIN;
196 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
198 nis_freeresult (saved_result);
199 return niserr2nss (result->status);
203 parse_res = _nss_nisplus_parse_etherent (result, ether, buffer,
204 buflen, errnop);
205 if (parse_res == -1)
207 nis_freeresult (result);
208 result = saved_result;
209 return NSS_STATUS_TRYAGAIN;
212 if (saved_result != NULL)
213 nis_freeresult (saved_result);
216 while (!parse_res);
218 return NSS_STATUS_SUCCESS;
221 enum nss_status
222 _nss_nisplus_getetherent_r (struct etherent *result, char *buffer,
223 size_t buflen, int *errnop)
225 int status;
227 __libc_lock_lock (lock);
229 status = internal_nisplus_getetherent_r (result, buffer, buflen, errnop);
231 __libc_lock_unlock (lock);
233 return status;
236 enum nss_status
237 _nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
238 char *buffer, size_t buflen, int *errnop)
240 if (tablename_val == NULL)
242 enum nss_status status = _nss_create_tablename (errnop);
244 if (status != NSS_STATUS_SUCCESS)
245 return status;
248 if (name == NULL)
250 *errnop = EINVAL;
251 return NSS_STATUS_UNAVAIL;
254 char buf[strlen (name) + 9 + tablename_len];
255 int olderr = errno;
257 snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
259 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
261 if (result == NULL)
263 *errnop = ENOMEM;
264 return NSS_STATUS_TRYAGAIN;
267 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
269 enum nss_status status = niserr2nss (result->status);
270 nis_freeresult (result);
271 return status;
274 int parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
275 buflen, errnop);
277 /* We do not need the lookup result anymore. */
278 nis_freeresult (result);
280 if (__builtin_expect (parse_res < 1, 0))
282 __set_errno (olderr);
284 if (parse_res == -1)
285 return NSS_STATUS_TRYAGAIN;
287 return NSS_STATUS_NOTFOUND;
290 return NSS_STATUS_SUCCESS;
293 enum nss_status
294 _nss_nisplus_getntohost_r (const struct ether_addr *addr, struct etherent *eth,
295 char *buffer, size_t buflen, int *errnop)
297 if (tablename_val == NULL)
299 __libc_lock_lock (lock);
301 enum nss_status status = _nss_create_tablename (errnop);
303 __libc_lock_unlock (lock);
305 if (status != NSS_STATUS_SUCCESS)
306 return status;
309 if (addr == NULL)
311 *errnop = EINVAL;
312 return NSS_STATUS_UNAVAIL;
315 char buf[26 + tablename_len];
317 snprintf (buf, sizeof (buf),
318 "[addr=%" PRIx8 ":%" PRIx8 ":%" PRIx8 ":%" PRIx8 ":%" PRIx8
319 ":%" PRIx8 "],%s",
320 addr->ether_addr_octet[0], addr->ether_addr_octet[1],
321 addr->ether_addr_octet[2], addr->ether_addr_octet[3],
322 addr->ether_addr_octet[4], addr->ether_addr_octet[5],
323 tablename_val);
325 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
327 if (result == NULL)
329 *errnop = ENOMEM;
330 return NSS_STATUS_TRYAGAIN;
333 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
335 enum nss_status status = niserr2nss (result->status);
336 nis_freeresult (result);
337 return status;
340 int parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
341 buflen, errnop);
343 /* We do not need the lookup result anymore. */
344 nis_freeresult (result);
346 if (__builtin_expect (parse_res < 1, 0))
348 if (parse_res == -1)
349 return NSS_STATUS_TRYAGAIN;
351 return NSS_STATUS_NOTFOUND;
354 return NSS_STATUS_SUCCESS;