Remove various ABS macros and replace uses with fabs (or in one case abs)
[glibc.git] / nis / nss_nisplus / nisplus-ethers.c
blobb874ea826880b936f3a6ab2b2183d2ff753b82a4
1 /* Copyright (C) 1997-2015 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <atomic.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <inttypes.h>
23 #include <netdb.h>
24 #include <nss.h>
25 #include <string.h>
26 #include <netinet/ether.h>
27 #include <netinet/if_ether.h>
28 #include <rpcsvc/nis.h>
29 #include <bits/libc-lock.h>
31 #include "nss-nisplus.h"
33 __libc_lock_define_initialized (static, lock)
35 static nis_result *result;
36 static nis_name tablename_val;
37 static u_long tablename_len;
40 #define NISENTRYVAL(idx, col, res) \
41 (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
43 #define NISENTRYLEN(idx, col, res) \
44 (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
46 static int
47 _nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether,
48 char *buffer, size_t buflen, int *errnop)
50 char *p = buffer;
51 size_t room_left = buflen;
53 if (result == NULL)
54 return 0;
56 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
57 || NIS_RES_NUMOBJ (result) != 1
58 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
59 || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
60 "ethers_tbl") != 0
61 || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 2)
62 return 0;
64 /* Generate the ether entry format and use the normal parser */
65 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
67 *errnop = ERANGE;
68 return -1;
70 char *cp = __stpncpy (p, NISENTRYVAL (0, 0, result),
71 NISENTRYLEN (0, 0, result));
72 *cp = '\0';
73 room_left -= NISENTRYLEN (0, 0, result) + 1;
74 ether->e_name = p;
76 struct ether_addr *ea = ether_aton (NISENTRYVAL (0, 1, result));
77 if (ea == NULL)
79 *errnop = EINVAL;
80 return -2;
83 ether->e_addr = *ea;
85 return 1;
88 static enum nss_status
89 _nss_create_tablename (int *errnop)
91 if (tablename_val == NULL)
93 const char *local_dir = nis_local_directory ();
94 size_t local_dir_len = strlen (local_dir);
95 static const char prefix[] = "ethers.org_dir.";
97 char *p = malloc (sizeof (prefix) + local_dir_len);
98 if (p == NULL)
100 *errnop = errno;
101 return NSS_STATUS_TRYAGAIN;
104 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
106 tablename_len = sizeof (prefix) - 1 + local_dir_len;
108 atomic_write_barrier ();
110 tablename_val = p;
112 return NSS_STATUS_SUCCESS;
116 enum nss_status
117 _nss_nisplus_setetherent (int stayopen)
119 enum nss_status status;
120 int err;
122 status = NSS_STATUS_SUCCESS;
124 __libc_lock_lock (lock);
126 if (result != NULL)
128 nis_freeresult (result);
129 result = NULL;
132 if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
133 status = NSS_STATUS_UNAVAIL;
135 __libc_lock_unlock (lock);
137 return status;
140 enum nss_status
141 _nss_nisplus_endetherent (void)
143 __libc_lock_lock (lock);
145 if (result != NULL)
147 nis_freeresult (result);
148 result = NULL;
151 __libc_lock_unlock (lock);
153 return NSS_STATUS_SUCCESS;
156 static enum nss_status
157 internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
158 size_t buflen, int *errnop)
160 if (tablename_val == NULL)
162 enum nss_status status = _nss_create_tablename (errnop);
164 if (status != NSS_STATUS_SUCCESS)
165 return status;
168 /* Get the next entry until we found a correct one. */
169 int parse_res;
172 nis_result *saved_result;
174 if (result == NULL)
176 saved_result = NULL;
177 result = nis_first_entry (tablename_val);
178 if (result == NULL)
180 *errnop = errno;
181 return NSS_STATUS_TRYAGAIN;
183 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
184 return niserr2nss (result->status);
186 else
188 saved_result = result;
189 result = nis_next_entry (tablename_val, &result->cookie);
190 if (result == NULL)
192 *errnop = errno;
193 return NSS_STATUS_TRYAGAIN;
195 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
197 nis_freeresult (saved_result);
198 return niserr2nss (result->status);
202 parse_res = _nss_nisplus_parse_etherent (result, ether, buffer,
203 buflen, errnop);
204 if (parse_res == -1)
206 nis_freeresult (result);
207 result = saved_result;
208 return NSS_STATUS_TRYAGAIN;
211 if (saved_result != NULL)
212 nis_freeresult (saved_result);
215 while (!parse_res);
217 return NSS_STATUS_SUCCESS;
220 enum nss_status
221 _nss_nisplus_getetherent_r (struct etherent *result, char *buffer,
222 size_t buflen, int *errnop)
224 int status;
226 __libc_lock_lock (lock);
228 status = internal_nisplus_getetherent_r (result, buffer, buflen, errnop);
230 __libc_lock_unlock (lock);
232 return status;
235 enum nss_status
236 _nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
237 char *buffer, size_t buflen, int *errnop)
239 if (tablename_val == NULL)
241 enum nss_status status = _nss_create_tablename (errnop);
243 if (status != NSS_STATUS_SUCCESS)
244 return status;
247 if (name == NULL)
249 *errnop = EINVAL;
250 return NSS_STATUS_UNAVAIL;
253 char buf[strlen (name) + 9 + tablename_len];
254 int olderr = errno;
256 snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
258 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
259 NULL, NULL);
261 if (result == NULL)
263 *errnop = ENOMEM;
264 return NSS_STATUS_TRYAGAIN;
267 if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
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 (__glibc_unlikely (parse_res < 1))
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 | USE_DGRAM,
326 NULL, NULL);
328 if (result == NULL)
330 *errnop = ENOMEM;
331 return NSS_STATUS_TRYAGAIN;
334 if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
336 enum nss_status status = niserr2nss (result->status);
337 nis_freeresult (result);
338 return status;
341 int parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
342 buflen, errnop);
344 /* We do not need the lookup result anymore. */
345 nis_freeresult (result);
347 if (__glibc_unlikely (parse_res < 1))
349 if (parse_res == -1)
350 return NSS_STATUS_TRYAGAIN;
352 return NSS_STATUS_NOTFOUND;
355 return NSS_STATUS_SUCCESS;