1 /* Copyright (C) 1997-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.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 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.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
26 #include <netinet/ether.h>
27 #include <netinet/if_ether.h>
28 #include <rpcsvc/nis.h>
29 #include <bits/libc-lock.h>
31 #include "nss-nisplus.h"
33 __libc_lock_define_initialized (static, lock
)
35 static nis_result
*result
;
36 static nis_name tablename_val
;
37 static u_long tablename_len
;
40 #define NISENTRYVAL(idx, col, res) \
41 (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
43 #define NISENTRYLEN(idx, col, res) \
44 (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
47 _nss_nisplus_parse_etherent (nis_result
*result
, struct etherent
*ether
,
48 char *buffer
, size_t buflen
, int *errnop
)
51 size_t room_left
= buflen
;
56 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
)
57 || NIS_RES_NUMOBJ (result
) != 1
58 || __type_of (NIS_RES_OBJECT (result
)) != NIS_ENTRY_OBJ
59 || strcmp (NIS_RES_OBJECT (result
)->EN_data
.en_type
,
61 || NIS_RES_OBJECT (result
)->EN_data
.en_cols
.en_cols_len
< 2)
64 /* Generate the ether entry format and use the normal parser */
65 if (NISENTRYLEN (0, 0, result
) + 1 > room_left
)
70 char *cp
= __stpncpy (p
, NISENTRYVAL (0, 0, result
),
71 NISENTRYLEN (0, 0, result
));
73 room_left
-= NISENTRYLEN (0, 0, result
) + 1;
76 struct ether_addr
*ea
= ether_aton (NISENTRYVAL (0, 1, result
));
88 static enum nss_status
89 _nss_create_tablename (int *errnop
)
91 if (tablename_val
== NULL
)
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.";
97 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
101 return NSS_STATUS_TRYAGAIN
;
104 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
106 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
108 atomic_write_barrier ();
112 return NSS_STATUS_SUCCESS
;
117 _nss_nisplus_setetherent (int stayopen
)
119 enum nss_status status
;
122 status
= NSS_STATUS_SUCCESS
;
124 __libc_lock_lock (lock
);
128 nis_freeresult (result
);
132 if (_nss_create_tablename (&err
) != NSS_STATUS_SUCCESS
)
133 status
= NSS_STATUS_UNAVAIL
;
135 __libc_lock_unlock (lock
);
141 _nss_nisplus_endetherent (void)
143 __libc_lock_lock (lock
);
147 nis_freeresult (result
);
151 __libc_lock_unlock (lock
);
153 return NSS_STATUS_SUCCESS
;
156 static enum nss_status
157 internal_nisplus_getetherent_r (struct etherent
*ether
, char *buffer
,
158 size_t buflen
, int *errnop
)
160 if (tablename_val
== NULL
)
162 enum nss_status status
= _nss_create_tablename (errnop
);
164 if (status
!= NSS_STATUS_SUCCESS
)
168 /* Get the next entry until we found a correct one. */
172 nis_result
*saved_result
;
177 result
= nis_first_entry (tablename_val
);
181 return NSS_STATUS_TRYAGAIN
;
183 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
184 return niserr2nss (result
->status
);
188 saved_result
= result
;
189 result
= nis_next_entry (tablename_val
, &result
->cookie
);
193 return NSS_STATUS_TRYAGAIN
;
195 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
197 nis_freeresult (saved_result
);
198 return niserr2nss (result
->status
);
202 parse_res
= _nss_nisplus_parse_etherent (result
, ether
, buffer
,
206 nis_freeresult (result
);
207 result
= saved_result
;
208 return NSS_STATUS_TRYAGAIN
;
211 if (saved_result
!= NULL
)
212 nis_freeresult (saved_result
);
217 return NSS_STATUS_SUCCESS
;
221 _nss_nisplus_getetherent_r (struct etherent
*result
, char *buffer
,
222 size_t buflen
, int *errnop
)
226 __libc_lock_lock (lock
);
228 status
= internal_nisplus_getetherent_r (result
, buffer
, buflen
, errnop
);
230 __libc_lock_unlock (lock
);
236 _nss_nisplus_gethostton_r (const char *name
, struct etherent
*eth
,
237 char *buffer
, size_t buflen
, int *errnop
)
239 if (tablename_val
== NULL
)
241 enum nss_status status
= _nss_create_tablename (errnop
);
243 if (status
!= NSS_STATUS_SUCCESS
)
250 return NSS_STATUS_UNAVAIL
;
253 char buf
[strlen (name
) + 9 + tablename_len
];
256 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
258 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
264 return NSS_STATUS_TRYAGAIN
;
267 if (__glibc_unlikely (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
))
269 enum nss_status status
= niserr2nss (result
->status
);
270 nis_freeresult (result
);
274 int parse_res
= _nss_nisplus_parse_etherent (result
, eth
, buffer
,
277 /* We do not need the lookup result anymore. */
278 nis_freeresult (result
);
280 if (__glibc_unlikely (parse_res
< 1))
282 __set_errno (olderr
);
285 return NSS_STATUS_TRYAGAIN
;
287 return NSS_STATUS_NOTFOUND
;
290 return NSS_STATUS_SUCCESS
;
294 _nss_nisplus_getntohost_r (const struct ether_addr
*addr
, struct etherent
*eth
,
295 char *buffer
, size_t buflen
, int *errnop
)
297 if (tablename_val
== NULL
)
299 __libc_lock_lock (lock
);
301 enum nss_status status
= _nss_create_tablename (errnop
);
303 __libc_lock_unlock (lock
);
305 if (status
!= NSS_STATUS_SUCCESS
)
312 return NSS_STATUS_UNAVAIL
;
315 char buf
[26 + tablename_len
];
317 snprintf (buf
, sizeof (buf
),
318 "[addr=%" PRIx8
":%" PRIx8
":%" PRIx8
":%" PRIx8
":%" PRIx8
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],
325 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
331 return NSS_STATUS_TRYAGAIN
;
334 if (__glibc_unlikely (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
))
336 enum nss_status status
= niserr2nss (result
->status
);
337 nis_freeresult (result
);
341 int parse_res
= _nss_nisplus_parse_etherent (result
, eth
, buffer
,
344 /* We do not need the lookup result anymore. */
345 nis_freeresult (result
);
347 if (__glibc_unlikely (parse_res
< 1))
350 return NSS_STATUS_TRYAGAIN
;
352 return NSS_STATUS_NOTFOUND
;
355 return NSS_STATUS_SUCCESS
;