* sysdeps/unix/sysv/linux/m68k/sysdep-cancel.h: Support cancellation
[glibc.git] / nis / nss_nisplus / nisplus-ethers.c
blob2fd152dfdbb14dd70f708d568ea89431e08c5ad4
1 /* Copyright (C) 1997, 1998, 2000, 2001, 2002 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 <nss.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <string.h>
24 #include <bits/libc-lock.h>
25 #include <netdb.h>
26 #include <netinet/ether.h>
27 #include <rpcsvc/nis.h>
28 #include <netinet/if_ether.h>
30 #include "nss-nisplus.h"
32 __libc_lock_define_initialized (static, lock)
34 static nis_result *result;
35 static nis_name tablename_val;
36 static u_long tablename_len;
39 #define NISENTRYVAL(idx,col,res) \
40 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
42 #define NISENTRYLEN(idx,col,res) \
43 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
45 static int
46 _nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether,
47 char *buffer, size_t buflen, int *errnop)
49 char *p = buffer;
50 size_t room_left = buflen;
52 if (result == NULL)
53 return 0;
55 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
56 || result->objects.objects_len != 1
57 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
58 || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
59 "ethers_tbl") != 0
60 || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 2)
61 return 0;
63 /* Generate the ether entry format and use the normal parser */
64 if (NISENTRYLEN (0, 0, result) +1 > room_left)
66 *errnop = ERANGE;
67 return -1;
69 strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result));
70 room_left -= (NISENTRYLEN (0, 0, result) +1);
71 ether->e_name = p;
73 ether->e_addr = *ether_aton (NISENTRYVAL (0, 1, result));
75 return 1;
78 static enum nss_status
79 _nss_create_tablename (int *errnop)
81 if (tablename_val == NULL)
83 char buf [40 + strlen (nis_local_directory ())];
84 char *p;
86 p = __stpcpy (buf, "ethers.org_dir.");
87 p = __stpcpy (p, nis_local_directory ());
88 tablename_val = __strdup (buf);
89 if (tablename_val == NULL)
91 *errnop = errno;
92 return NSS_STATUS_TRYAGAIN;
94 tablename_len = strlen (tablename_val);
96 return NSS_STATUS_SUCCESS;
100 enum nss_status
101 _nss_nisplus_setetherent (int stayopen)
103 enum nss_status status;
104 int err;
106 status = NSS_STATUS_SUCCESS;
108 __libc_lock_lock (lock);
110 if (result)
111 nis_freeresult (result);
112 result = NULL;
114 if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
115 status = NSS_STATUS_UNAVAIL;
117 __libc_lock_unlock (lock);
119 return NSS_STATUS_SUCCESS;
122 enum nss_status
123 _nss_nisplus_endetherent (void)
125 __libc_lock_lock (lock);
127 if (result)
128 nis_freeresult (result);
129 result = NULL;
131 __libc_lock_unlock (lock);
133 return NSS_STATUS_SUCCESS;
136 static enum nss_status
137 internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
138 size_t buflen, int *errnop)
140 int parse_res;
142 if (tablename_val == NULL)
144 enum nss_status status = _nss_create_tablename (errnop);
146 if (status != NSS_STATUS_SUCCESS)
147 return status;
150 /* Get the next entry until we found a correct one. */
153 nis_result *saved_result;
155 if (result == NULL)
157 saved_result = NULL;
158 result = nis_first_entry (tablename_val);
159 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
160 return niserr2nss (result->status);
162 else
164 nis_result *res2;
166 res2 = nis_next_entry(tablename_val, &result->cookie);
167 saved_result = result;
168 result = res2;
169 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
171 nis_freeresult (saved_result);
172 return niserr2nss (result->status);
176 parse_res = _nss_nisplus_parse_etherent (result, ether, buffer,
177 buflen, errnop);
178 if (parse_res == -1)
180 nis_freeresult (result);
181 *errnop = ERANGE;
182 result = saved_result;
183 return NSS_STATUS_TRYAGAIN;
185 else
187 if (saved_result != NULL)
188 nis_freeresult (saved_result);
191 } while (!parse_res);
193 return NSS_STATUS_SUCCESS;
196 enum nss_status
197 _nss_nisplus_getetherent_r (struct etherent *result, char *buffer,
198 size_t buflen, int *errnop)
200 int status;
202 __libc_lock_lock (lock);
204 status = internal_nisplus_getetherent_r (result, buffer, buflen, errnop);
206 __libc_lock_unlock (lock);
208 return status;
211 enum nss_status
212 _nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
213 char *buffer, size_t buflen, int *errnop)
215 int parse_res;
217 if (tablename_val == NULL)
219 enum nss_status status = _nss_create_tablename (errnop);
221 if (status != NSS_STATUS_SUCCESS)
222 return status;
225 if (name == NULL)
227 *errnop = EINVAL;
228 return NSS_STATUS_UNAVAIL;
230 else
232 nis_result *result;
233 char buf[strlen (name) + 40 + tablename_len];
234 int olderr = errno;
236 sprintf (buf, "[name=%s],%s", name, tablename_val);
238 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
240 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
242 enum nss_status status = niserr2nss (result->status);
243 nis_freeresult (result);
244 return status;
247 parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
248 buflen, errnop);
249 if (parse_res < 1)
251 __set_errno (olderr);
253 if (parse_res == -1)
255 nis_freeresult (result);
256 *errnop = ERANGE;
257 return NSS_STATUS_TRYAGAIN;
259 else
260 return NSS_STATUS_NOTFOUND;
262 return NSS_STATUS_SUCCESS;
266 enum nss_status
267 _nss_nisplus_getntohost_r (const struct ether_addr *addr,
268 struct etherent *eth,
269 char *buffer, size_t buflen, int *errnop)
271 if (tablename_val == NULL)
273 enum nss_status status = _nss_create_tablename (errnop);
275 if (status != NSS_STATUS_SUCCESS)
276 return status;
279 if (addr == NULL)
281 *errnop = EINVAL;
282 return NSS_STATUS_UNAVAIL;
284 else
286 int parse_res;
287 nis_result *result;
288 char buf[255 + tablename_len];
290 sprintf (buf, "[addr=%x:%x:%x:%x:%x:%x],ethers.org_dir",
291 addr->ether_addr_octet[0], addr->ether_addr_octet[1],
292 addr->ether_addr_octet[2], addr->ether_addr_octet[3],
293 addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
295 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
297 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
299 enum nss_status status = niserr2nss (result->status);
300 nis_freeresult (result);
301 return status;
304 parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
305 buflen, errnop);
306 if (parse_res < 1)
308 if (parse_res == -1)
310 nis_freeresult (result);
311 *errnop = ERANGE;
312 return NSS_STATUS_TRYAGAIN;
314 else
315 return NSS_STATUS_NOTFOUND;
317 return NSS_STATUS_SUCCESS;