Update.
[glibc.git] / nis / nss_nisplus / nisplus-ethers.c
blob8ca6935cc7e1645ed84f2467a00a200ad8383ee8
1 /* Copyright (C) 1997 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)
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 __set_errno (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 (void)
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)
99 return NSS_STATUS_TRYAGAIN;
100 tablename_len = strlen (tablename_val);
102 return NSS_STATUS_SUCCESS;
106 enum nss_status
107 _nss_nisplus_setetherent (void)
109 enum nss_status status;
111 status = NSS_STATUS_SUCCESS;
113 __libc_lock_lock (lock);
115 if (result)
116 nis_freeresult (result);
117 result = NULL;
119 if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
120 status = NSS_STATUS_UNAVAIL;
122 __libc_lock_unlock (lock);
124 return NSS_STATUS_SUCCESS;
127 enum nss_status
128 _nss_nisplus_endetherent (void)
130 __libc_lock_lock (lock);
132 if (result)
133 nis_freeresult (result);
134 result = NULL;
136 __libc_lock_unlock (lock);
138 return NSS_STATUS_SUCCESS;
141 static enum nss_status
142 internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
143 size_t buflen)
145 int parse_res;
147 if (tablename_val == NULL)
148 if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
149 return NSS_STATUS_UNAVAIL;
151 /* Get the next entry until we found a correct one. */
154 nis_result *saved_result;
156 if (result == NULL)
158 saved_result = NULL;
159 result = nis_first_entry(tablename_val);
160 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
161 return niserr2nss (result->status);
163 else
165 nis_result *res2;
167 res2 = nis_next_entry(tablename_val, &result->cookie);
168 saved_result = result;
169 result = res2;
170 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
172 nis_freeresult (saved_result);
173 return niserr2nss (result->status);
177 if ((parse_res = _nss_nisplus_parse_etherent (result, ether, buffer,
178 buflen)) == -1)
180 nis_freeresult (result);
181 result = saved_result;
182 return NSS_STATUS_TRYAGAIN;
184 else
186 if (saved_result != NULL)
187 nis_freeresult (saved_result);
190 } while (!parse_res);
192 return NSS_STATUS_SUCCESS;
195 enum nss_status
196 _nss_nisplus_getetherent_r (struct etherent *result, char *buffer,
197 size_t buflen)
199 int status;
201 __libc_lock_lock (lock);
203 status = internal_nisplus_getetherent_r (result, buffer, buflen);
205 __libc_lock_unlock (lock);
207 return status;
210 enum nss_status
211 _nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
212 char *buffer, size_t buflen)
214 int parse_res;
216 if (tablename_val == NULL)
217 if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
218 return NSS_STATUS_UNAVAIL;
220 if (name != NULL)
222 nis_result *result;
223 char buf[strlen (name) + 40 + tablename_len];
225 sprintf(buf, "[name=%s],%s", name, tablename_val);
227 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
229 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
231 enum nss_status status = niserr2nss (result->status);
232 nis_freeresult (result);
233 return status;
236 if ((parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
237 buflen)) == -1)
239 nis_freeresult (result);
240 return NSS_STATUS_TRYAGAIN;
243 if (parse_res)
244 return NSS_STATUS_SUCCESS;
246 return NSS_STATUS_NOTFOUND;
249 enum nss_status
250 _nss_nisplus_getntohost_r (const struct ether_addr *addr,
251 struct etherent *eth,
252 char *buffer, size_t buflen)
254 if (tablename_val == NULL)
255 if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
256 return NSS_STATUS_UNAVAIL;
258 if (addr == NULL)
260 __set_errno (EINVAL);
261 return NSS_STATUS_UNAVAIL;
263 else
265 int parse_res;
266 nis_result *result;
267 char buf[255 + tablename_len];
269 memset (&buf, '\0', sizeof (buf));
270 sprintf(buf, "[addr=%x:%x:%x:%x:%x:%x],ethers.org_dir",
271 addr->ether_addr_octet[0], addr->ether_addr_octet[1],
272 addr->ether_addr_octet[2], addr->ether_addr_octet[3],
273 addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
275 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
277 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
279 enum nss_status status = niserr2nss (result->status);
280 nis_freeresult (result);
281 return status;
284 if ((parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
285 buflen)) == -1)
287 nis_freeresult (result);
288 return NSS_STATUS_TRYAGAIN;
291 if (parse_res)
292 return NSS_STATUS_SUCCESS;
294 return NSS_STATUS_NOTFOUND;