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. */
24 #include <libc-lock.h>
26 #include <netinet/ether.h>
27 #include <rpcsvc/nis.h>
28 #include <rpcsvc/nislib.h>
29 #include <netinet/if_ether.h>
31 #include "nss-nisplus.h"
33 __libc_lock_define_initialized (static, lock)
35 static nis_result *result = NULL;
36 static nis_name *names = NULL;
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
44 struct ether_addr e_addr;
46 struct etherent_data {};
48 #define ENTNAME etherent
49 #define DATABASE "ethers"
50 #include "../../nss/nss_files/files-parse.c"
53 /* Read the ethernet address: 6 x 8bit hexadecimal number. */
57 for (cnt = 0; cnt < 6; ++cnt)
62 INT_FIELD (number, ISCOLON , 0, 16, (unsigned int))
64 INT_FIELD (number, isspace, 0, 16, (unsigned int))
68 result->e_addr.ether_addr_octet[cnt] = number;
71 STRING_FIELD (result->e_name, isspace, 1);
74 #define NISENTRYVAL(idx,col,res) \
75 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
77 #define NISENTRYLEN(idx,col,res) \
78 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
81 _nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether,
82 char *buffer, size_t buflen)
85 size_t room_left = buflen;
86 struct parser_data *data = (void *) buffer;
91 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
92 result->objects.objects_len != 1 ||
93 result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ ||
94 strcmp(result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type,
96 result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 2)
99 memset (p, '\0', room_left);
101 /* Generate the ether entry format and use the normal parser */
102 if (NISENTRYLEN (0, 0, result) +1 > room_left)
104 __set_errno (ERANGE);
107 strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result));
108 room_left -= (NISENTRYLEN (0, 0, result) +1);
110 if (NISENTRYLEN (0, 1, result) +1 > room_left)
112 __set_errno (ERANGE);
116 strncat (p, NISENTRYVAL (0, 1, result), NISENTRYLEN (0, 1, result));
117 room_left -= (NISENTRYLEN (0, 1, result) + 1);
119 return _nss_files_parse_etherent (p,ether, data, buflen);
123 _nss_nisplus_setetherent (void)
125 __libc_lock_lock (lock);
128 nis_freeresult (result);
132 nis_freenames (names);
136 __libc_lock_unlock (lock);
138 return NSS_STATUS_SUCCESS;
142 _nss_nisplus_endetherent (void)
144 __libc_lock_lock (lock);
147 nis_freeresult (result);
151 nis_freenames (names);
155 __libc_lock_unlock (lock);
157 return NSS_STATUS_SUCCESS;
160 static enum nss_status
161 internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
166 /* Get the next entry until we found a correct one. */
171 names = nis_getnames("ethers.org_dir");
172 if (names == NULL || names[0] == NULL)
173 return NSS_STATUS_UNAVAIL;
175 result = nis_first_entry(names[0]);
176 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
177 return niserr2nss (result->status);
183 res2 = nis_next_entry(names[0], &result->cookie);
184 nis_freeresult (result);
186 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
187 return niserr2nss (result->status);
190 parse_res = _nss_nisplus_parse_etherent (result, ether, buffer, buflen);
191 } while (!parse_res);
193 return NSS_STATUS_SUCCESS;
197 _nss_nisplus_getetherent_r (struct etherent *result, char *buffer,
202 __libc_lock_lock (lock);
204 status = internal_nisplus_getetherent_r (result, buffer, buflen);
206 __libc_lock_unlock (lock);
212 _nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
213 char *buffer, size_t buflen)
218 return NSS_STATUS_NOTFOUND;
222 char buf[strlen (name) + 255];
224 sprintf(buf, "[name=%s],ethers.org_dir", name);
226 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
228 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
229 return niserr2nss (result->status);
231 parse_res = _nss_nisplus_parse_etherent (result, eth, buffer, buflen);
234 return NSS_STATUS_SUCCESS;
236 if (!parse_res && errno == ERANGE)
237 return NSS_STATUS_TRYAGAIN;
239 return NSS_STATUS_NOTFOUND;
244 _nss_nisplus_getntohost_r (const struct ether_addr *addr,
245 struct etherent *eth,
246 char *buffer, size_t buflen)
254 __set_errno (EINVAL);
255 return NSS_STATUS_UNAVAIL;
258 memset (&buf, '\0', sizeof (buf));
259 snprintf(buf, sizeof (buf), "[addr=%x:%x:%x:%x:%x:%x],ethers.org_dir",
260 addr->ether_addr_octet[0], addr->ether_addr_octet[1],
261 addr->ether_addr_octet[2], addr->ether_addr_octet[3],
262 addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
264 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
266 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
267 return niserr2nss (result->status);
269 parse_res = _nss_nisplus_parse_etherent (result, eth, buffer, buflen);
272 return NSS_STATUS_SUCCESS;
274 if (!parse_res && errno == ERANGE)
275 return NSS_STATUS_TRYAGAIN;
277 return NSS_STATUS_NOTFOUND;