(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nis / nss_nisplus / nisplus-ethers.c
blob028309c84114789a1b715d94cef813084841b179
1 /* Copyright (C) 1997,1998,2000,2001,2002,2003 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 (result == NULL)
242 *errnop = ENOMEM;
243 return NSS_STATUS_TRYAGAIN;
245 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
247 enum nss_status status = niserr2nss (result->status);
248 nis_freeresult (result);
249 return status;
252 parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
253 buflen, errnop);
254 if (parse_res < 1)
256 __set_errno (olderr);
258 if (parse_res == -1)
260 nis_freeresult (result);
261 *errnop = ERANGE;
262 return NSS_STATUS_TRYAGAIN;
264 else
265 return NSS_STATUS_NOTFOUND;
267 return NSS_STATUS_SUCCESS;
271 enum nss_status
272 _nss_nisplus_getntohost_r (const struct ether_addr *addr,
273 struct etherent *eth,
274 char *buffer, size_t buflen, int *errnop)
276 if (tablename_val == NULL)
278 enum nss_status status = _nss_create_tablename (errnop);
280 if (status != NSS_STATUS_SUCCESS)
281 return status;
284 if (addr == NULL)
286 *errnop = EINVAL;
287 return NSS_STATUS_UNAVAIL;
289 else
291 int parse_res;
292 nis_result *result;
293 char buf[255 + tablename_len];
295 sprintf (buf, "[addr=%x:%x:%x:%x:%x:%x],ethers.org_dir",
296 addr->ether_addr_octet[0], addr->ether_addr_octet[1],
297 addr->ether_addr_octet[2], addr->ether_addr_octet[3],
298 addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
300 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
302 if (result == NULL)
304 *errnop = ENOMEM;
305 return NSS_STATUS_TRYAGAIN;
307 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
309 enum nss_status status = niserr2nss (result->status);
310 nis_freeresult (result);
311 return status;
314 parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
315 buflen, errnop);
316 if (parse_res < 1)
318 if (parse_res == -1)
320 nis_freeresult (result);
321 *errnop = ERANGE;
322 return NSS_STATUS_TRYAGAIN;
324 else
325 return NSS_STATUS_NOTFOUND;
327 return NSS_STATUS_SUCCESS;