1 /* Copyright (C) 1997,1998,2000-2003,2005,2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #include <netinet/ether.h>
28 #include <netinet/if_ether.h>
29 #include <rpcsvc/nis.h>
30 #include <bits/libc-lock.h>
32 #include "nss-nisplus.h"
34 __libc_lock_define_initialized (static, lock
)
36 static nis_result
*result
;
37 static nis_name tablename_val
;
38 static u_long tablename_len
;
41 #define NISENTRYVAL(idx, col, res) \
42 (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
44 #define NISENTRYLEN(idx, col, res) \
45 (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
48 _nss_nisplus_parse_etherent (nis_result
*result
, struct etherent
*ether
,
49 char *buffer
, size_t buflen
, int *errnop
)
52 size_t room_left
= buflen
;
57 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
)
58 || NIS_RES_NUMOBJ (result
) != 1
59 || __type_of (NIS_RES_OBJECT (result
)) != NIS_ENTRY_OBJ
60 || strcmp (NIS_RES_OBJECT (result
)->EN_data
.en_type
,
62 || NIS_RES_OBJECT (result
)->EN_data
.en_cols
.en_cols_len
< 2)
65 /* Generate the ether entry format and use the normal parser */
66 if (NISENTRYLEN (0, 0, result
) + 1 > room_left
)
71 char *cp
= __stpncpy (p
, NISENTRYVAL (0, 0, result
),
72 NISENTRYLEN (0, 0, result
));
74 room_left
-= NISENTRYLEN (0, 0, result
) + 1;
77 struct ether_addr
*ea
= ether_aton (NISENTRYVAL (0, 1, result
));
89 static enum nss_status
90 _nss_create_tablename (int *errnop
)
92 if (tablename_val
== NULL
)
94 const char *local_dir
= nis_local_directory ();
95 size_t local_dir_len
= strlen (local_dir
);
96 static const char prefix
[] = "ethers.org_dir.";
98 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
102 return NSS_STATUS_TRYAGAIN
;
105 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
107 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
109 atomic_write_barrier ();
113 return NSS_STATUS_SUCCESS
;
118 _nss_nisplus_setetherent (int stayopen
)
120 enum nss_status status
;
123 status
= NSS_STATUS_SUCCESS
;
125 __libc_lock_lock (lock
);
129 nis_freeresult (result
);
133 if (_nss_create_tablename (&err
) != NSS_STATUS_SUCCESS
)
134 status
= NSS_STATUS_UNAVAIL
;
136 __libc_lock_unlock (lock
);
138 return NSS_STATUS_SUCCESS
;
142 _nss_nisplus_endetherent (void)
144 __libc_lock_lock (lock
);
148 nis_freeresult (result
);
152 __libc_lock_unlock (lock
);
154 return NSS_STATUS_SUCCESS
;
157 static enum nss_status
158 internal_nisplus_getetherent_r (struct etherent
*ether
, char *buffer
,
159 size_t buflen
, int *errnop
)
161 if (tablename_val
== NULL
)
163 enum nss_status status
= _nss_create_tablename (errnop
);
165 if (status
!= NSS_STATUS_SUCCESS
)
169 /* Get the next entry until we found a correct one. */
173 nis_result
*saved_result
;
178 result
= nis_first_entry (tablename_val
);
182 return NSS_STATUS_TRYAGAIN
;
184 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
185 return niserr2nss (result
->status
);
189 saved_result
= result
;
190 result
= nis_next_entry (tablename_val
, &result
->cookie
);
194 return NSS_STATUS_TRYAGAIN
;
196 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
198 nis_freeresult (saved_result
);
199 return niserr2nss (result
->status
);
203 parse_res
= _nss_nisplus_parse_etherent (result
, ether
, buffer
,
207 nis_freeresult (result
);
208 result
= saved_result
;
209 return NSS_STATUS_TRYAGAIN
;
212 if (saved_result
!= NULL
)
213 nis_freeresult (saved_result
);
218 return NSS_STATUS_SUCCESS
;
222 _nss_nisplus_getetherent_r (struct etherent
*result
, char *buffer
,
223 size_t buflen
, int *errnop
)
227 __libc_lock_lock (lock
);
229 status
= internal_nisplus_getetherent_r (result
, buffer
, buflen
, errnop
);
231 __libc_lock_unlock (lock
);
237 _nss_nisplus_gethostton_r (const char *name
, struct etherent
*eth
,
238 char *buffer
, size_t buflen
, int *errnop
)
240 if (tablename_val
== NULL
)
242 enum nss_status status
= _nss_create_tablename (errnop
);
244 if (status
!= NSS_STATUS_SUCCESS
)
251 return NSS_STATUS_UNAVAIL
;
254 char buf
[strlen (name
) + 9 + tablename_len
];
257 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
259 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
264 return NSS_STATUS_TRYAGAIN
;
267 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
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 (__builtin_expect (parse_res
< 1, 0))
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
, NULL
, NULL
);
330 return NSS_STATUS_TRYAGAIN
;
333 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
335 enum nss_status status
= niserr2nss (result
->status
);
336 nis_freeresult (result
);
340 int parse_res
= _nss_nisplus_parse_etherent (result
, eth
, buffer
,
343 /* We do not need the lookup result anymore. */
344 nis_freeresult (result
);
346 if (__builtin_expect (parse_res
< 1, 0))
349 return NSS_STATUS_TRYAGAIN
;
351 return NSS_STATUS_NOTFOUND
;
354 return NSS_STATUS_SUCCESS
;