Update.
[glibc.git] / nis / nss_nisplus / nisplus-ethers.c
blobddf6f7f1282813bed65ca7cc33c74c3ed4d5a54f
1 /* Copyright (C) 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 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 = NULL;
35 static nis_name tablename_val = NULL;
36 static u_long tablename_len = 0;
38 /* Because the `ethers' lookup does not fit so well in the scheme so
39 we define a dummy struct here which helps us to use the available
40 functions. */
41 struct etherent
43 const char *e_name;
44 struct ether_addr e_addr;
46 struct etherent_data {};
48 #define NISENTRYVAL(idx,col,res) \
49 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
51 #define NISENTRYLEN(idx,col,res) \
52 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
54 static int
55 _nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether,
56 char *buffer, size_t buflen, int *errnop)
58 char *p = buffer;
59 size_t room_left = buflen;
61 if (result == NULL)
62 return 0;
64 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
65 || result->objects.objects_len != 1
66 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
67 || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
68 "ethers_tbl") != 0
69 || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 2)
70 return 0;
72 /* Generate the ether entry format and use the normal parser */
73 if (NISENTRYLEN (0, 0, result) +1 > room_left)
75 *errnop = ERANGE;
76 return -1;
78 strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result));
79 room_left -= (NISENTRYLEN (0, 0, result) +1);
80 ether->e_name = p;
82 ether->e_addr = *ether_aton (NISENTRYVAL (0, 1, result));
84 return 1;
87 static enum nss_status
88 _nss_create_tablename (int *errnop)
90 if (tablename_val == NULL)
92 char buf [40 + strlen (nis_local_directory ())];
93 char *p;
95 p = __stpcpy (buf, "ethers.org_dir.");
96 p = __stpcpy (p, nis_local_directory ());
97 tablename_val = __strdup (buf);
98 if (tablename_val == NULL)
100 *errnop = errno;
101 return NSS_STATUS_TRYAGAIN;
103 tablename_len = strlen (tablename_val);
105 return NSS_STATUS_SUCCESS;
109 enum nss_status
110 _nss_nisplus_setetherent (void)
112 enum nss_status status;
113 int err;
115 status = NSS_STATUS_SUCCESS;
117 __libc_lock_lock (lock);
119 if (result)
120 nis_freeresult (result);
121 result = NULL;
123 if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
124 status = NSS_STATUS_UNAVAIL;
126 __libc_lock_unlock (lock);
128 return NSS_STATUS_SUCCESS;
131 enum nss_status
132 _nss_nisplus_endetherent (void)
134 __libc_lock_lock (lock);
136 if (result)
137 nis_freeresult (result);
138 result = NULL;
140 __libc_lock_unlock (lock);
142 return NSS_STATUS_SUCCESS;
145 static enum nss_status
146 internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
147 size_t buflen, int *errnop)
149 int parse_res;
151 if (tablename_val == NULL)
153 enum nss_status status = _nss_create_tablename (errnop);
155 if (status != NSS_STATUS_SUCCESS)
156 return status;
159 /* Get the next entry until we found a correct one. */
162 nis_result *saved_result;
164 if (result == NULL)
166 saved_result = NULL;
167 result = nis_first_entry (tablename_val);
168 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
169 return niserr2nss (result->status);
171 else
173 nis_result *res2;
175 res2 = nis_next_entry(tablename_val, &result->cookie);
176 saved_result = result;
177 result = res2;
178 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
180 nis_freeresult (saved_result);
181 return niserr2nss (result->status);
185 parse_res = _nss_nisplus_parse_etherent (result, ether, buffer,
186 buflen, errnop);
187 if (parse_res == -1)
189 nis_freeresult (result);
190 *errnop = ERANGE;
191 result = saved_result;
192 return NSS_STATUS_TRYAGAIN;
194 else
196 if (saved_result != NULL)
197 nis_freeresult (saved_result);
200 } while (!parse_res);
202 return NSS_STATUS_SUCCESS;
205 enum nss_status
206 _nss_nisplus_getetherent_r (struct etherent *result, char *buffer,
207 size_t buflen, int *errnop)
209 int status;
211 __libc_lock_lock (lock);
213 status = internal_nisplus_getetherent_r (result, buffer, buflen, errnop);
215 __libc_lock_unlock (lock);
217 return status;
220 enum nss_status
221 _nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
222 char *buffer, size_t buflen, int *errnop)
224 int parse_res;
226 if (tablename_val == NULL)
228 enum nss_status status = _nss_create_tablename (errnop);
230 if (status != NSS_STATUS_SUCCESS)
231 return status;
234 if (name != NULL)
236 *errnop = EINVAL;
237 return NSS_STATUS_UNAVAIL;
239 else
241 nis_result *result;
242 char buf[strlen (name) + 40 + tablename_len];
244 sprintf (buf, "[name=%s],%s", name, tablename_val);
246 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
248 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
250 enum nss_status status = niserr2nss (result->status);
251 nis_freeresult (result);
252 return status;
255 parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
256 buflen, errnop);
257 if (parse_res < 1)
259 if (parse_res == -1)
261 nis_freeresult (result);
262 *errnop = ERANGE;
263 return NSS_STATUS_TRYAGAIN;
265 else
266 return NSS_STATUS_NOTFOUND;
268 return NSS_STATUS_SUCCESS;
272 enum nss_status
273 _nss_nisplus_getntohost_r (const struct ether_addr *addr,
274 struct etherent *eth,
275 char *buffer, size_t buflen, int *errnop)
277 if (tablename_val == NULL)
279 enum nss_status status = _nss_create_tablename (errnop);
281 if (status != NSS_STATUS_SUCCESS)
282 return status;
285 if (addr == NULL)
287 *errnop = EINVAL;
288 return NSS_STATUS_UNAVAIL;
290 else
292 int parse_res;
293 nis_result *result;
294 char buf[255 + tablename_len];
296 sprintf (buf, "[addr=%x:%x:%x:%x:%x:%x],ethers.org_dir",
297 addr->ether_addr_octet[0], addr->ether_addr_octet[1],
298 addr->ether_addr_octet[2], addr->ether_addr_octet[3],
299 addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
301 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
303 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
305 enum nss_status status = niserr2nss (result->status);
306 nis_freeresult (result);
307 return status;
310 parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
311 buflen, errnop);
312 if (parse_res < 1)
314 if (parse_res == -1)
316 nis_freeresult (result);
317 *errnop = ERANGE;
318 return NSS_STATUS_TRYAGAIN;
320 else
321 return NSS_STATUS_NOTFOUND;
323 return NSS_STATUS_SUCCESS;