2.9
[glibc/nacl-glibc.git] / nis / nss_nisplus / nisplus-ethers.c
blob298869f931d768aa4e559901c1611cee5c5b28f8
1 /* Copyright (C) 1997,1998,2000-2003,2005,2006,2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <atomic.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <inttypes.h>
25 #include <netdb.h>
26 #include <nss.h>
27 #include <string.h>
28 #include <netinet/ether.h>
29 #include <netinet/if_ether.h>
30 #include <rpcsvc/nis.h>
31 #include <bits/libc-lock.h>
33 #include "nss-nisplus.h"
35 __libc_lock_define_initialized (static, lock)
37 static nis_result *result;
38 static nis_name tablename_val;
39 static u_long tablename_len;
42 #define NISENTRYVAL(idx, col, res) \
43 (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
45 #define NISENTRYLEN(idx, col, res) \
46 (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
48 static int
49 _nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether,
50 char *buffer, size_t buflen, int *errnop)
52 char *p = buffer;
53 size_t room_left = buflen;
55 if (result == NULL)
56 return 0;
58 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
59 || NIS_RES_NUMOBJ (result) != 1
60 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
61 || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
62 "ethers_tbl") != 0
63 || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 2)
64 return 0;
66 /* Generate the ether entry format and use the normal parser */
67 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
69 *errnop = ERANGE;
70 return -1;
72 char *cp = __stpncpy (p, NISENTRYVAL (0, 0, result),
73 NISENTRYLEN (0, 0, result));
74 *cp = '\0';
75 room_left -= NISENTRYLEN (0, 0, result) + 1;
76 ether->e_name = p;
78 struct ether_addr *ea = ether_aton (NISENTRYVAL (0, 1, result));
79 if (ea == NULL)
81 *errnop = EINVAL;
82 return -2;
85 ether->e_addr = *ea;
87 return 1;
90 static enum nss_status
91 _nss_create_tablename (int *errnop)
93 if (tablename_val == NULL)
95 const char *local_dir = nis_local_directory ();
96 size_t local_dir_len = strlen (local_dir);
97 static const char prefix[] = "ethers.org_dir.";
99 char *p = malloc (sizeof (prefix) + local_dir_len);
100 if (p == NULL)
102 *errnop = errno;
103 return NSS_STATUS_TRYAGAIN;
106 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
108 tablename_len = sizeof (prefix) - 1 + local_dir_len;
110 atomic_write_barrier ();
112 tablename_val = p;
114 return NSS_STATUS_SUCCESS;
118 enum nss_status
119 _nss_nisplus_setetherent (int stayopen)
121 enum nss_status status;
122 int err;
124 status = NSS_STATUS_SUCCESS;
126 __libc_lock_lock (lock);
128 if (result != NULL)
130 nis_freeresult (result);
131 result = NULL;
134 if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
135 status = NSS_STATUS_UNAVAIL;
137 __libc_lock_unlock (lock);
139 return NSS_STATUS_SUCCESS;
142 enum nss_status
143 _nss_nisplus_endetherent (void)
145 __libc_lock_lock (lock);
147 if (result != NULL)
149 nis_freeresult (result);
150 result = NULL;
153 __libc_lock_unlock (lock);
155 return NSS_STATUS_SUCCESS;
158 static enum nss_status
159 internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
160 size_t buflen, int *errnop)
162 if (tablename_val == NULL)
164 enum nss_status status = _nss_create_tablename (errnop);
166 if (status != NSS_STATUS_SUCCESS)
167 return status;
170 /* Get the next entry until we found a correct one. */
171 int parse_res;
174 nis_result *saved_result;
176 if (result == NULL)
178 saved_result = NULL;
179 result = nis_first_entry (tablename_val);
180 if (result == NULL)
182 *errnop = errno;
183 return NSS_STATUS_TRYAGAIN;
185 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
186 return niserr2nss (result->status);
188 else
190 saved_result = result;
191 result = nis_next_entry (tablename_val, &result->cookie);
192 if (result == NULL)
194 *errnop = errno;
195 return NSS_STATUS_TRYAGAIN;
197 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
199 nis_freeresult (saved_result);
200 return niserr2nss (result->status);
204 parse_res = _nss_nisplus_parse_etherent (result, ether, buffer,
205 buflen, errnop);
206 if (parse_res == -1)
208 nis_freeresult (result);
209 result = saved_result;
210 return NSS_STATUS_TRYAGAIN;
213 if (saved_result != NULL)
214 nis_freeresult (saved_result);
217 while (!parse_res);
219 return NSS_STATUS_SUCCESS;
222 enum nss_status
223 _nss_nisplus_getetherent_r (struct etherent *result, char *buffer,
224 size_t buflen, int *errnop)
226 int status;
228 __libc_lock_lock (lock);
230 status = internal_nisplus_getetherent_r (result, buffer, buflen, errnop);
232 __libc_lock_unlock (lock);
234 return status;
237 enum nss_status
238 _nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
239 char *buffer, size_t buflen, int *errnop)
241 if (tablename_val == NULL)
243 enum nss_status status = _nss_create_tablename (errnop);
245 if (status != NSS_STATUS_SUCCESS)
246 return status;
249 if (name == NULL)
251 *errnop = EINVAL;
252 return NSS_STATUS_UNAVAIL;
255 char buf[strlen (name) + 9 + tablename_len];
256 int olderr = errno;
258 snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
260 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
261 NULL, NULL);
263 if (result == NULL)
265 *errnop = ENOMEM;
266 return NSS_STATUS_TRYAGAIN;
269 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
271 enum nss_status status = niserr2nss (result->status);
272 nis_freeresult (result);
273 return status;
276 int parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
277 buflen, errnop);
279 /* We do not need the lookup result anymore. */
280 nis_freeresult (result);
282 if (__builtin_expect (parse_res < 1, 0))
284 __set_errno (olderr);
286 if (parse_res == -1)
287 return NSS_STATUS_TRYAGAIN;
289 return NSS_STATUS_NOTFOUND;
292 return NSS_STATUS_SUCCESS;
295 enum nss_status
296 _nss_nisplus_getntohost_r (const struct ether_addr *addr, struct etherent *eth,
297 char *buffer, size_t buflen, int *errnop)
299 if (tablename_val == NULL)
301 __libc_lock_lock (lock);
303 enum nss_status status = _nss_create_tablename (errnop);
305 __libc_lock_unlock (lock);
307 if (status != NSS_STATUS_SUCCESS)
308 return status;
311 if (addr == NULL)
313 *errnop = EINVAL;
314 return NSS_STATUS_UNAVAIL;
317 char buf[26 + tablename_len];
319 snprintf (buf, sizeof (buf),
320 "[addr=%" PRIx8 ":%" PRIx8 ":%" PRIx8 ":%" PRIx8 ":%" PRIx8
321 ":%" PRIx8 "],%s",
322 addr->ether_addr_octet[0], addr->ether_addr_octet[1],
323 addr->ether_addr_octet[2], addr->ether_addr_octet[3],
324 addr->ether_addr_octet[4], addr->ether_addr_octet[5],
325 tablename_val);
327 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
328 NULL, NULL);
330 if (result == NULL)
332 *errnop = ENOMEM;
333 return NSS_STATUS_TRYAGAIN;
336 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
338 enum nss_status status = niserr2nss (result->status);
339 nis_freeresult (result);
340 return status;
343 int parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
344 buflen, errnop);
346 /* We do not need the lookup result anymore. */
347 nis_freeresult (result);
349 if (__builtin_expect (parse_res < 1, 0))
351 if (parse_res == -1)
352 return NSS_STATUS_TRYAGAIN;
354 return NSS_STATUS_NOTFOUND;
357 return NSS_STATUS_SUCCESS;