1 /* Copyright (C) 1997,1998,2000-2003,2005 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 ((res)->objects.objects_val[(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 ((res)->objects.objects_val[(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 || result
->objects
.objects_len
!= 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 strncpy (p
, NISENTRYVAL (0, 0, result
), NISENTRYLEN (0, 0, result
));
72 room_left
-= (NISENTRYLEN (0, 0, result
) +1);
75 struct ether_addr
*ea
= ether_aton (NISENTRYVAL (0, 1, result
));
87 static enum nss_status
88 _nss_create_tablename (int *errnop
)
90 if (tablename_val
== NULL
)
92 const char *local_dir
= nis_local_directory ();
93 size_t local_dir_len
= strlen (local_dir
);
94 static const char prefix
[] = "ethers.org_dir.";
96 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
97 if (tablename_val
== NULL
)
100 return NSS_STATUS_TRYAGAIN
;
103 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
105 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
107 atomic_write_barrier ();
111 return NSS_STATUS_SUCCESS
;
116 _nss_nisplus_setetherent (int stayopen
)
118 enum nss_status status
;
121 status
= NSS_STATUS_SUCCESS
;
123 __libc_lock_lock (lock
);
127 nis_freeresult (result
);
131 if (_nss_create_tablename (&err
) != NSS_STATUS_SUCCESS
)
132 status
= NSS_STATUS_UNAVAIL
;
134 __libc_lock_unlock (lock
);
136 return NSS_STATUS_SUCCESS
;
140 _nss_nisplus_endetherent (void)
142 __libc_lock_lock (lock
);
146 nis_freeresult (result
);
150 __libc_lock_unlock (lock
);
152 return NSS_STATUS_SUCCESS
;
155 static enum nss_status
156 internal_nisplus_getetherent_r (struct etherent
*ether
, char *buffer
,
157 size_t buflen
, int *errnop
)
159 if (tablename_val
== NULL
)
161 enum nss_status status
= _nss_create_tablename (errnop
);
163 if (status
!= NSS_STATUS_SUCCESS
)
167 /* Get the next entry until we found a correct one. */
171 nis_result
*saved_result
;
176 result
= nis_first_entry (tablename_val
);
177 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
178 return niserr2nss (result
->status
);
182 saved_result
= result
;
183 result
= nis_next_entry (tablename_val
, &result
->cookie
);
184 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
186 nis_freeresult (saved_result
);
187 return niserr2nss (result
->status
);
191 parse_res
= _nss_nisplus_parse_etherent (result
, ether
, buffer
,
195 nis_freeresult (result
);
196 result
= saved_result
;
197 return NSS_STATUS_TRYAGAIN
;
200 if (saved_result
!= NULL
)
201 nis_freeresult (saved_result
);
206 return NSS_STATUS_SUCCESS
;
210 _nss_nisplus_getetherent_r (struct etherent
*result
, char *buffer
,
211 size_t buflen
, int *errnop
)
215 __libc_lock_lock (lock
);
217 status
= internal_nisplus_getetherent_r (result
, buffer
, buflen
, errnop
);
219 __libc_lock_unlock (lock
);
225 _nss_nisplus_gethostton_r (const char *name
, struct etherent
*eth
,
226 char *buffer
, size_t buflen
, int *errnop
)
228 if (tablename_val
== NULL
)
230 enum nss_status status
= _nss_create_tablename (errnop
);
232 if (status
!= NSS_STATUS_SUCCESS
)
239 return NSS_STATUS_UNAVAIL
;
242 char buf
[strlen (name
) + 9 + tablename_len
];
245 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
247 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
252 return NSS_STATUS_TRYAGAIN
;
255 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
257 enum nss_status status
= niserr2nss (result
->status
);
258 nis_freeresult (result
);
262 int parse_res
= _nss_nisplus_parse_etherent (result
, eth
, buffer
,
264 if (__builtin_expect (parse_res
< 1, 0))
266 __set_errno (olderr
);
270 nis_freeresult (result
);
271 return NSS_STATUS_TRYAGAIN
;
274 return NSS_STATUS_NOTFOUND
;
277 return NSS_STATUS_SUCCESS
;
281 _nss_nisplus_getntohost_r (const struct ether_addr
*addr
, struct etherent
*eth
,
282 char *buffer
, size_t buflen
, int *errnop
)
284 if (tablename_val
== NULL
)
286 __libc_lock_lock (lock
);
288 enum nss_status status
= _nss_create_tablename (errnop
);
290 __libc_lock_unlock (lock
);
292 if (status
!= NSS_STATUS_SUCCESS
)
299 return NSS_STATUS_UNAVAIL
;
302 char buf
[26 + tablename_len
];
304 snprintf (buf
, sizeof (buf
),
305 "[addr=%" PRIx8
":%" PRIx8
":%" PRIx8
":%" PRIx8
":%" PRIx8
307 addr
->ether_addr_octet
[0], addr
->ether_addr_octet
[1],
308 addr
->ether_addr_octet
[2], addr
->ether_addr_octet
[3],
309 addr
->ether_addr_octet
[4], addr
->ether_addr_octet
[5],
312 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
317 return NSS_STATUS_TRYAGAIN
;
320 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
322 enum nss_status status
= niserr2nss (result
->status
);
323 nis_freeresult (result
);
327 int parse_res
= _nss_nisplus_parse_etherent (result
, eth
, buffer
,
329 if (__builtin_expect (parse_res
< 1, 0))
333 nis_freeresult (result
);
334 return NSS_STATUS_TRYAGAIN
;
337 return NSS_STATUS_NOTFOUND
;
340 return NSS_STATUS_SUCCESS
;