* sysdeps/unix/sysv/linux/sigaction.c: If WRAPPER_INCLUDE is defined,
[glibc.git] / nis / nss_nisplus / nisplus-ethers.c
blobfcc550e743b27b59923c5905a6da670526262a0b
1 /* Copyright (C) 1997,1998,2000-2003,2005 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 ((res)->objects.objects_val[(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 ((res)->objects.objects_val[(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 || result->objects.objects_len != 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 strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result));
72 room_left -= (NISENTRYLEN (0, 0, result) +1);
73 ether->e_name = p;
75 struct ether_addr *ea = ether_aton (NISENTRYVAL (0, 1, result));
76 if (ea == NULL)
78 *errnop = EINVAL;
79 return -2;
82 ether->e_addr = *ea;
84 return 1;
87 static enum nss_status
88 _nss_create_tablename (int *errnop)
90 if (tablename_val == NULL)
92 const char *local_dir = nis_local_directory ();
93 size_t local_dir_len = strlen (local_dir);
94 static const char prefix[] = "ethers.org_dir.";
96 char *p = malloc (sizeof (prefix) + local_dir_len);
97 if (tablename_val == NULL)
99 *errnop = errno;
100 return NSS_STATUS_TRYAGAIN;
103 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
105 tablename_len = sizeof (prefix) - 1 + local_dir_len;
107 atomic_write_barrier ();
109 tablename_val = p;
111 return NSS_STATUS_SUCCESS;
115 enum nss_status
116 _nss_nisplus_setetherent (int stayopen)
118 enum nss_status status;
119 int err;
121 status = NSS_STATUS_SUCCESS;
123 __libc_lock_lock (lock);
125 if (result != NULL)
127 nis_freeresult (result);
128 result = NULL;
131 if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
132 status = NSS_STATUS_UNAVAIL;
134 __libc_lock_unlock (lock);
136 return NSS_STATUS_SUCCESS;
139 enum nss_status
140 _nss_nisplus_endetherent (void)
142 __libc_lock_lock (lock);
144 if (result != NULL)
146 nis_freeresult (result);
147 result = NULL;
150 __libc_lock_unlock (lock);
152 return NSS_STATUS_SUCCESS;
155 static enum nss_status
156 internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
157 size_t buflen, int *errnop)
159 if (tablename_val == NULL)
161 enum nss_status status = _nss_create_tablename (errnop);
163 if (status != NSS_STATUS_SUCCESS)
164 return status;
167 /* Get the next entry until we found a correct one. */
168 int parse_res;
171 nis_result *saved_result;
173 if (result == NULL)
175 saved_result = NULL;
176 result = nis_first_entry (tablename_val);
177 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
178 return niserr2nss (result->status);
180 else
182 saved_result = result;
183 result = nis_next_entry (tablename_val, &result->cookie);
184 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
186 nis_freeresult (saved_result);
187 return niserr2nss (result->status);
191 parse_res = _nss_nisplus_parse_etherent (result, ether, buffer,
192 buflen, errnop);
193 if (parse_res == -1)
195 nis_freeresult (result);
196 result = saved_result;
197 return NSS_STATUS_TRYAGAIN;
200 if (saved_result != NULL)
201 nis_freeresult (saved_result);
204 while (!parse_res);
206 return NSS_STATUS_SUCCESS;
209 enum nss_status
210 _nss_nisplus_getetherent_r (struct etherent *result, char *buffer,
211 size_t buflen, int *errnop)
213 int status;
215 __libc_lock_lock (lock);
217 status = internal_nisplus_getetherent_r (result, buffer, buflen, errnop);
219 __libc_lock_unlock (lock);
221 return status;
224 enum nss_status
225 _nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
226 char *buffer, size_t buflen, int *errnop)
228 if (tablename_val == NULL)
230 enum nss_status status = _nss_create_tablename (errnop);
232 if (status != NSS_STATUS_SUCCESS)
233 return status;
236 if (name == NULL)
238 *errnop = EINVAL;
239 return NSS_STATUS_UNAVAIL;
242 char buf[strlen (name) + 9 + tablename_len];
243 int olderr = errno;
245 snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
247 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
249 if (result == NULL)
251 *errnop = ENOMEM;
252 return NSS_STATUS_TRYAGAIN;
255 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
257 enum nss_status status = niserr2nss (result->status);
258 nis_freeresult (result);
259 return status;
262 int parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
263 buflen, errnop);
264 if (__builtin_expect (parse_res < 1, 0))
266 __set_errno (olderr);
268 if (parse_res == -1)
270 nis_freeresult (result);
271 return NSS_STATUS_TRYAGAIN;
273 else
274 return NSS_STATUS_NOTFOUND;
277 return NSS_STATUS_SUCCESS;
280 enum nss_status
281 _nss_nisplus_getntohost_r (const struct ether_addr *addr, struct etherent *eth,
282 char *buffer, size_t buflen, int *errnop)
284 if (tablename_val == NULL)
286 __libc_lock_lock (lock);
288 enum nss_status status = _nss_create_tablename (errnop);
290 __libc_lock_unlock (lock);
292 if (status != NSS_STATUS_SUCCESS)
293 return status;
296 if (addr == NULL)
298 *errnop = EINVAL;
299 return NSS_STATUS_UNAVAIL;
302 char buf[26 + tablename_len];
304 snprintf (buf, sizeof (buf),
305 "[addr=%" PRIx8 ":%" PRIx8 ":%" PRIx8 ":%" PRIx8 ":%" PRIx8
306 ":%" PRIx8 "],%s",
307 addr->ether_addr_octet[0], addr->ether_addr_octet[1],
308 addr->ether_addr_octet[2], addr->ether_addr_octet[3],
309 addr->ether_addr_octet[4], addr->ether_addr_octet[5],
310 tablename_val);
312 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
314 if (result == NULL)
316 *errnop = ENOMEM;
317 return NSS_STATUS_TRYAGAIN;
320 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
322 enum nss_status status = niserr2nss (result->status);
323 nis_freeresult (result);
324 return status;
327 int parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
328 buflen, errnop);
329 if (__builtin_expect (parse_res < 1, 0))
331 if (parse_res == -1)
333 nis_freeresult (result);
334 return NSS_STATUS_TRYAGAIN;
337 return NSS_STATUS_NOTFOUND;
340 return NSS_STATUS_SUCCESS;