]>
Commit | Line | Data |
---|---|---|
bfff8b1b | 1 | /* Copyright (C) 1997-2017 Free Software Foundation, Inc. |
e61abf83 | 2 | This file is part of the GNU C Library. |
ed073f0e | 3 | Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997. |
e61abf83 UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
e61abf83 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
e61abf83 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
e61abf83 | 18 | |
636e689e | 19 | #include <atomic.h> |
a334319f | 20 | #include <ctype.h> |
636e689e UD |
21 | #include <errno.h> |
22 | #include <inttypes.h> | |
a334319f | 23 | #include <netdb.h> |
636e689e UD |
24 | #include <nss.h> |
25 | #include <string.h> | |
e61abf83 | 26 | #include <netinet/ether.h> |
a334319f | 27 | #include <netinet/if_ether.h> |
636e689e | 28 | #include <rpcsvc/nis.h> |
ec999b8e | 29 | #include <libc-lock.h> |
e61abf83 UD |
30 | |
31 | #include "nss-nisplus.h" | |
32 | ||
33 | __libc_lock_define_initialized (static, lock) | |
34 | ||
fc9f33e3 UD |
35 | static nis_result *result; |
36 | static nis_name tablename_val; | |
37 | static u_long tablename_len; | |
e61abf83 | 38 | |
e61abf83 | 39 | |
1620084c | 40 | #define NISENTRYVAL(idx, col, res) \ |
1a544854 | 41 | (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_val) |
e61abf83 | 42 | |
1620084c | 43 | #define NISENTRYLEN(idx, col, res) \ |
1a544854 | 44 | (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_len) |
e61abf83 UD |
45 | |
46 | static int | |
47 | _nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether, | |
6b083d46 | 48 | char *buffer, size_t buflen, int *errnop) |
e61abf83 UD |
49 | { |
50 | char *p = buffer; | |
51 | size_t room_left = buflen; | |
e61abf83 UD |
52 | |
53 | if (result == NULL) | |
26dee9c4 | 54 | return 0; |
e61abf83 | 55 | |
d71b808a | 56 | if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) |
1620084c | 57 | || NIS_RES_NUMOBJ (result) != 1 |
d71b808a UD |
58 | || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ |
59 | || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type, | |
60 | "ethers_tbl") != 0 | |
61 | || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 2) | |
26dee9c4 | 62 | return 0; |
e61abf83 | 63 | |
e61abf83 | 64 | /* Generate the ether entry format and use the normal parser */ |
1620084c | 65 | if (NISENTRYLEN (0, 0, result) + 1 > room_left) |
e61abf83 | 66 | { |
d71b808a | 67 | *errnop = ERANGE; |
60c96635 | 68 | return -1; |
e61abf83 | 69 | } |
1620084c UD |
70 | char *cp = __stpncpy (p, NISENTRYVAL (0, 0, result), |
71 | NISENTRYLEN (0, 0, result)); | |
72 | *cp = '\0'; | |
73 | room_left -= NISENTRYLEN (0, 0, result) + 1; | |
2d7da676 UD |
74 | ether->e_name = p; |
75 | ||
636e689e UD |
76 | struct ether_addr *ea = ether_aton (NISENTRYVAL (0, 1, result)); |
77 | if (ea == NULL) | |
78 | { | |
79 | *errnop = EINVAL; | |
80 | return -2; | |
81 | } | |
82 | ||
83 | ether->e_addr = *ea; | |
2d7da676 UD |
84 | |
85 | return 1; | |
86 | } | |
e61abf83 | 87 | |
2d7da676 | 88 | static enum nss_status |
d71b808a | 89 | _nss_create_tablename (int *errnop) |
2d7da676 UD |
90 | { |
91 | if (tablename_val == NULL) | |
e61abf83 | 92 | { |
636e689e UD |
93 | const char *local_dir = nis_local_directory (); |
94 | size_t local_dir_len = strlen (local_dir); | |
95 | static const char prefix[] = "ethers.org_dir."; | |
e61abf83 | 96 | |
636e689e | 97 | char *p = malloc (sizeof (prefix) + local_dir_len); |
c8e82b4a | 98 | if (p == NULL) |
d71b808a UD |
99 | { |
100 | *errnop = errno; | |
101 | return NSS_STATUS_TRYAGAIN; | |
102 | } | |
636e689e UD |
103 | |
104 | memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1); | |
105 | ||
106 | tablename_len = sizeof (prefix) - 1 + local_dir_len; | |
107 | ||
108 | atomic_write_barrier (); | |
109 | ||
110 | tablename_val = p; | |
2d7da676 UD |
111 | } |
112 | return NSS_STATUS_SUCCESS; | |
e61abf83 UD |
113 | } |
114 | ||
2d7da676 | 115 | |
e61abf83 | 116 | enum nss_status |
ed073f0e | 117 | _nss_nisplus_setetherent (int stayopen) |
e61abf83 | 118 | { |
2d7da676 | 119 | enum nss_status status; |
d71b808a | 120 | int err; |
2d7da676 UD |
121 | |
122 | status = NSS_STATUS_SUCCESS; | |
123 | ||
e61abf83 UD |
124 | __libc_lock_lock (lock); |
125 | ||
636e689e UD |
126 | if (result != NULL) |
127 | { | |
128 | nis_freeresult (result); | |
129 | result = NULL; | |
130 | } | |
2d7da676 | 131 | |
d71b808a | 132 | if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS) |
2d7da676 | 133 | status = NSS_STATUS_UNAVAIL; |
e61abf83 UD |
134 | |
135 | __libc_lock_unlock (lock); | |
136 | ||
1a544854 | 137 | return status; |
e61abf83 UD |
138 | } |
139 | ||
140 | enum nss_status | |
141 | _nss_nisplus_endetherent (void) | |
142 | { | |
143 | __libc_lock_lock (lock); | |
144 | ||
636e689e UD |
145 | if (result != NULL) |
146 | { | |
147 | nis_freeresult (result); | |
148 | result = NULL; | |
149 | } | |
e61abf83 UD |
150 | |
151 | __libc_lock_unlock (lock); | |
152 | ||
153 | return NSS_STATUS_SUCCESS; | |
154 | } | |
155 | ||
156 | static enum nss_status | |
157 | internal_nisplus_getetherent_r (struct etherent *ether, char *buffer, | |
d71b808a | 158 | size_t buflen, int *errnop) |
e61abf83 | 159 | { |
2d7da676 | 160 | if (tablename_val == NULL) |
d71b808a UD |
161 | { |
162 | enum nss_status status = _nss_create_tablename (errnop); | |
163 | ||
164 | if (status != NSS_STATUS_SUCCESS) | |
165 | return status; | |
166 | } | |
2d7da676 | 167 | |
e61abf83 | 168 | /* Get the next entry until we found a correct one. */ |
8e64faef | 169 | int parse_res; |
e61abf83 UD |
170 | do |
171 | { | |
60c96635 | 172 | nis_result *saved_result; |
8f2ece69 | 173 | |
e61abf83 UD |
174 | if (result == NULL) |
175 | { | |
60c96635 | 176 | saved_result = NULL; |
d71b808a | 177 | result = nis_first_entry (tablename_val); |
487609e3 UD |
178 | if (result == NULL) |
179 | { | |
180 | *errnop = errno; | |
181 | return NSS_STATUS_TRYAGAIN; | |
182 | } | |
e61abf83 UD |
183 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) |
184 | return niserr2nss (result->status); | |
185 | } | |
186 | else | |
187 | { | |
60c96635 | 188 | saved_result = result; |
8e64faef | 189 | result = nis_next_entry (tablename_val, &result->cookie); |
487609e3 UD |
190 | if (result == NULL) |
191 | { | |
192 | *errnop = errno; | |
193 | return NSS_STATUS_TRYAGAIN; | |
194 | } | |
e61abf83 | 195 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) |
60c96635 UD |
196 | { |
197 | nis_freeresult (saved_result); | |
198 | return niserr2nss (result->status); | |
199 | } | |
e61abf83 UD |
200 | } |
201 | ||
d71b808a UD |
202 | parse_res = _nss_nisplus_parse_etherent (result, ether, buffer, |
203 | buflen, errnop); | |
204 | if (parse_res == -1) | |
60c96635 UD |
205 | { |
206 | nis_freeresult (result); | |
207 | result = saved_result; | |
208 | return NSS_STATUS_TRYAGAIN; | |
209 | } | |
8f2ece69 | 210 | |
8e64faef UD |
211 | if (saved_result != NULL) |
212 | nis_freeresult (saved_result); | |
213 | ||
214 | } | |
215 | while (!parse_res); | |
e61abf83 UD |
216 | |
217 | return NSS_STATUS_SUCCESS; | |
218 | } | |
219 | ||
220 | enum nss_status | |
221 | _nss_nisplus_getetherent_r (struct etherent *result, char *buffer, | |
d71b808a | 222 | size_t buflen, int *errnop) |
e61abf83 UD |
223 | { |
224 | int status; | |
225 | ||
226 | __libc_lock_lock (lock); | |
227 | ||
d71b808a | 228 | status = internal_nisplus_getetherent_r (result, buffer, buflen, errnop); |
e61abf83 UD |
229 | |
230 | __libc_lock_unlock (lock); | |
231 | ||
232 | return status; | |
233 | } | |
234 | ||
235 | enum nss_status | |
236 | _nss_nisplus_gethostton_r (const char *name, struct etherent *eth, | |
d71b808a | 237 | char *buffer, size_t buflen, int *errnop) |
e61abf83 | 238 | { |
2d7da676 | 239 | if (tablename_val == NULL) |
d71b808a UD |
240 | { |
241 | enum nss_status status = _nss_create_tablename (errnop); | |
242 | ||
243 | if (status != NSS_STATUS_SUCCESS) | |
244 | return status; | |
245 | } | |
2d7da676 | 246 | |
4b1fef84 | 247 | if (name == NULL) |
ac9f45cf UD |
248 | { |
249 | *errnop = EINVAL; | |
250 | return NSS_STATUS_UNAVAIL; | |
251 | } | |
0ecb606c | 252 | |
8e64faef UD |
253 | char buf[strlen (name) + 9 + tablename_len]; |
254 | int olderr = errno; | |
0ecb606c | 255 | |
8e64faef | 256 | snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val); |
34816665 | 257 | |
697d37b1 JJ |
258 | nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM, |
259 | NULL, NULL); | |
0ecb606c | 260 | |
8e64faef UD |
261 | if (result == NULL) |
262 | { | |
263 | *errnop = ENOMEM; | |
264 | return NSS_STATUS_TRYAGAIN; | |
265 | } | |
266 | ||
a1ffb40e | 267 | if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS)) |
8e64faef UD |
268 | { |
269 | enum nss_status status = niserr2nss (result->status); | |
270 | nis_freeresult (result); | |
271 | return status; | |
272 | } | |
273 | ||
274 | int parse_res = _nss_nisplus_parse_etherent (result, eth, buffer, | |
a334319f | 275 | buflen, errnop); |
0292b0dd UD |
276 | |
277 | /* We do not need the lookup result anymore. */ | |
278 | nis_freeresult (result); | |
279 | ||
a1ffb40e | 280 | if (__glibc_unlikely (parse_res < 1)) |
8e64faef UD |
281 | { |
282 | __set_errno (olderr); | |
0ecb606c | 283 | |
8e64faef | 284 | if (parse_res == -1) |
0292b0dd UD |
285 | return NSS_STATUS_TRYAGAIN; |
286 | ||
287 | return NSS_STATUS_NOTFOUND; | |
e61abf83 | 288 | } |
8e64faef UD |
289 | |
290 | return NSS_STATUS_SUCCESS; | |
e61abf83 UD |
291 | } |
292 | ||
293 | enum nss_status | |
8e64faef | 294 | _nss_nisplus_getntohost_r (const struct ether_addr *addr, struct etherent *eth, |
d71b808a | 295 | char *buffer, size_t buflen, int *errnop) |
e61abf83 | 296 | { |
2d7da676 | 297 | if (tablename_val == NULL) |
d71b808a | 298 | { |
636e689e UD |
299 | __libc_lock_lock (lock); |
300 | ||
d71b808a UD |
301 | enum nss_status status = _nss_create_tablename (errnop); |
302 | ||
636e689e UD |
303 | __libc_lock_unlock (lock); |
304 | ||
d71b808a UD |
305 | if (status != NSS_STATUS_SUCCESS) |
306 | return status; | |
307 | } | |
e61abf83 UD |
308 | |
309 | if (addr == NULL) | |
310 | { | |
d71b808a | 311 | *errnop = EINVAL; |
e61abf83 UD |
312 | return NSS_STATUS_UNAVAIL; |
313 | } | |
e61abf83 | 314 | |
636e689e | 315 | char buf[26 + tablename_len]; |
0ecb606c | 316 | |
636e689e UD |
317 | snprintf (buf, sizeof (buf), |
318 | "[addr=%" PRIx8 ":%" PRIx8 ":%" PRIx8 ":%" PRIx8 ":%" PRIx8 | |
319 | ":%" PRIx8 "],%s", | |
320 | addr->ether_addr_octet[0], addr->ether_addr_octet[1], | |
321 | addr->ether_addr_octet[2], addr->ether_addr_octet[3], | |
322 | addr->ether_addr_octet[4], addr->ether_addr_octet[5], | |
323 | tablename_val); | |
0ecb606c | 324 | |
697d37b1 JJ |
325 | nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM, |
326 | NULL, NULL); | |
636e689e UD |
327 | |
328 | if (result == NULL) | |
329 | { | |
330 | *errnop = ENOMEM; | |
331 | return NSS_STATUS_TRYAGAIN; | |
332 | } | |
333 | ||
a1ffb40e | 334 | if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS)) |
636e689e UD |
335 | { |
336 | enum nss_status status = niserr2nss (result->status); | |
337 | nis_freeresult (result); | |
338 | return status; | |
339 | } | |
340 | ||
8e64faef UD |
341 | int parse_res = _nss_nisplus_parse_etherent (result, eth, buffer, |
342 | buflen, errnop); | |
0292b0dd UD |
343 | |
344 | /* We do not need the lookup result anymore. */ | |
345 | nis_freeresult (result); | |
346 | ||
a1ffb40e | 347 | if (__glibc_unlikely (parse_res < 1)) |
636e689e UD |
348 | { |
349 | if (parse_res == -1) | |
0292b0dd | 350 | return NSS_STATUS_TRYAGAIN; |
0ecb606c | 351 | |
636e689e | 352 | return NSS_STATUS_NOTFOUND; |
2d7da676 | 353 | } |
636e689e UD |
354 | |
355 | return NSS_STATUS_SUCCESS; | |
e61abf83 | 356 | } |