]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nss_nisplus/nisplus-ethers.c
Update.
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-ethers.c
CommitLineData
e61abf83
UD
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.
4
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.
9
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.
14
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. */
19
20#include <nss.h>
21#include <errno.h>
22#include <ctype.h>
23#include <string.h>
24#include <libc-lock.h>
25#include <netdb.h>
26#include <netinet/ether.h>
27#include <rpcsvc/nis.h>
28#include <rpcsvc/nislib.h>
29#include <netinet/if_ether.h>
30
31#include "nss-nisplus.h"
32
33__libc_lock_define_initialized (static, lock)
34
35static nis_result *result = NULL;
36static nis_name *names = NULL;
37
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. */
41struct etherent
42{
43 const char *e_name;
44 struct ether_addr e_addr;
45};
46struct etherent_data {};
47
48#define ENTNAME etherent
49#define DATABASE "ethers"
50#include "../../nss/nss_files/files-parse.c"
51LINE_PARSER
52("#",
53 /* Read the ethernet address: 6 x 8bit hexadecimal number. */
54 {
55 size_t cnt;
56
57 for (cnt = 0; cnt < 6; ++cnt)
58 {
59 unsigned int number;
60
61 if (cnt < 5)
62 INT_FIELD (number, ISCOLON , 0, 16, (unsigned int))
63 else
64 INT_FIELD (number, isspace, 0, 16, (unsigned int))
65
66 if (number > 0xff)
67 return 0;
68 result->e_addr.ether_addr_octet[cnt] = number;
69 }
70 };
71 STRING_FIELD (result->e_name, isspace, 1);
72 )
73
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)
76
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)
79
80static int
81_nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether,
82 char *buffer, size_t buflen)
83{
84 char *p = buffer;
85 size_t room_left = buflen;
86 struct parser_data *data = (void *) buffer;
87
88 if (result == NULL)
89 return -1;
90
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,
95 "ethers_tbl") != 0 ||
96 result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 2)
97 return -1;
98
99 memset (p, '\0', room_left);
100
101 /* Generate the ether entry format and use the normal parser */
102 if (NISENTRYLEN (0, 0, result) +1 > room_left)
103 {
104 __set_errno (ERANGE);
105 return -1;
106 }
107 strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result));
108 room_left -= (NISENTRYLEN (0, 0, result) +1);
109
110 if (NISENTRYLEN (0, 1, result) +1 > room_left)
111 {
112 __set_errno (ERANGE);
113 return -1;
114 }
115 strcat (p, "\t");
116 strncat (p, NISENTRYVAL (0, 1, result), NISENTRYLEN (0, 1, result));
117 room_left -= (NISENTRYLEN (0, 1, result) + 1);
118
119 return _nss_files_parse_etherent (p,ether, data, buflen);
120}
121
122enum nss_status
123_nss_nisplus_setetherent (void)
124{
125 __libc_lock_lock (lock);
126
127 if (result)
128 nis_freeresult (result);
129 result = NULL;
130 if (names)
131 {
132 nis_freenames (names);
133 names = NULL;
134 }
135
136 __libc_lock_unlock (lock);
137
138 return NSS_STATUS_SUCCESS;
139}
140
141enum nss_status
142_nss_nisplus_endetherent (void)
143{
144 __libc_lock_lock (lock);
145
146 if (result)
147 nis_freeresult (result);
148 result = NULL;
149 if (names)
150 {
151 nis_freenames (names);
152 names = NULL;
153 }
154
155 __libc_lock_unlock (lock);
156
157 return NSS_STATUS_SUCCESS;
158}
159
160static enum nss_status
161internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
162 size_t buflen)
163{
164 int parse_res;
165
166 /* Get the next entry until we found a correct one. */
167 do
168 {
169 if (result == NULL)
170 {
171 names = nis_getnames("ethers.org_dir");
172 if (names == NULL || names[0] == NULL)
173 return NSS_STATUS_UNAVAIL;
174
175 result = nis_first_entry(names[0]);
176 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
177 return niserr2nss (result->status);
178 }
179 else
180 {
181 nis_result *res2;
182
183 res2 = nis_next_entry(names[0], &result->cookie);
184 nis_freeresult (result);
185 result = res2;
186 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
187 return niserr2nss (result->status);
188 }
189
190 parse_res = _nss_nisplus_parse_etherent (result, ether, buffer, buflen);
191 } while (!parse_res);
192
193 return NSS_STATUS_SUCCESS;
194}
195
196enum nss_status
197_nss_nisplus_getetherent_r (struct etherent *result, char *buffer,
198 size_t buflen)
199{
200 int status;
201
202 __libc_lock_lock (lock);
203
204 status = internal_nisplus_getetherent_r (result, buffer, buflen);
205
206 __libc_lock_unlock (lock);
207
208 return status;
209}
210
211enum nss_status
212_nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
213 char *buffer, size_t buflen)
214{
215 int parse_res;
216
217 if (name == NULL)
218 return NSS_STATUS_NOTFOUND;
219 else
220 {
221 nis_result *result;
222 char buf[strlen (name) + 255];
223
224 sprintf(buf, "[name=%s],ethers.org_dir", name);
225
226 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
227
228 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
229 return niserr2nss (result->status);
230
231 parse_res = _nss_nisplus_parse_etherent (result, eth, buffer, buflen);
232
233 if (parse_res)
234 return NSS_STATUS_SUCCESS;
235
236 if (!parse_res && errno == ERANGE)
237 return NSS_STATUS_TRYAGAIN;
238 else
239 return NSS_STATUS_NOTFOUND;
240 }
241}
242
243enum nss_status
244_nss_nisplus_getntohost_r (const struct ether_addr *addr,
245 struct etherent *eth,
246 char *buffer, size_t buflen)
247{
248 int parse_res;
249 nis_result *result;
250 char buf[255];
251
252 if (addr == NULL)
253 {
254 __set_errno (EINVAL);
255 return NSS_STATUS_UNAVAIL;
256 }
257
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]);
263
264 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
265
266 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
267 return niserr2nss (result->status);
268
269 parse_res = _nss_nisplus_parse_etherent (result, eth, buffer, buflen);
270
271 if (parse_res)
272 return NSS_STATUS_SUCCESS;
273
274 if (!parse_res && errno == ERANGE)
275 return NSS_STATUS_TRYAGAIN;
276 else
277 return NSS_STATUS_NOTFOUND;
278}