1 /* Copyright (C) 1997,1998,2000-2003,2005,2006,2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <netinet/ether.h>
29 #include <netinet/if_ether.h>
30 #include <rpcsvc/nis.h>
31 #include <bits/libc-lock.h>
33 #include "nss-nisplus.h"
35 __libc_lock_define_initialized (static, lock
)
37 static nis_result
*result
;
38 static nis_name tablename_val
;
39 static u_long tablename_len
;
42 #define NISENTRYVAL(idx, col, res) \
43 (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
45 #define NISENTRYLEN(idx, col, res) \
46 (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
49 _nss_nisplus_parse_etherent (nis_result
*result
, struct etherent
*ether
,
50 char *buffer
, size_t buflen
, int *errnop
)
53 size_t room_left
= buflen
;
58 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
)
59 || NIS_RES_NUMOBJ (result
) != 1
60 || __type_of (NIS_RES_OBJECT (result
)) != NIS_ENTRY_OBJ
61 || strcmp (NIS_RES_OBJECT (result
)->EN_data
.en_type
,
63 || NIS_RES_OBJECT (result
)->EN_data
.en_cols
.en_cols_len
< 2)
66 /* Generate the ether entry format and use the normal parser */
67 if (NISENTRYLEN (0, 0, result
) + 1 > room_left
)
72 char *cp
= __stpncpy (p
, NISENTRYVAL (0, 0, result
),
73 NISENTRYLEN (0, 0, result
));
75 room_left
-= NISENTRYLEN (0, 0, result
) + 1;
78 struct ether_addr
*ea
= ether_aton (NISENTRYVAL (0, 1, result
));
90 static enum nss_status
91 _nss_create_tablename (int *errnop
)
93 if (tablename_val
== NULL
)
95 const char *local_dir
= nis_local_directory ();
96 size_t local_dir_len
= strlen (local_dir
);
97 static const char prefix
[] = "ethers.org_dir.";
99 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
103 return NSS_STATUS_TRYAGAIN
;
106 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
108 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
110 atomic_write_barrier ();
114 return NSS_STATUS_SUCCESS
;
119 _nss_nisplus_setetherent (int stayopen
)
121 enum nss_status status
;
124 status
= NSS_STATUS_SUCCESS
;
126 __libc_lock_lock (lock
);
130 nis_freeresult (result
);
134 if (_nss_create_tablename (&err
) != NSS_STATUS_SUCCESS
)
135 status
= NSS_STATUS_UNAVAIL
;
137 __libc_lock_unlock (lock
);
139 return NSS_STATUS_SUCCESS
;
143 _nss_nisplus_endetherent (void)
145 __libc_lock_lock (lock
);
149 nis_freeresult (result
);
153 __libc_lock_unlock (lock
);
155 return NSS_STATUS_SUCCESS
;
158 static enum nss_status
159 internal_nisplus_getetherent_r (struct etherent
*ether
, char *buffer
,
160 size_t buflen
, int *errnop
)
162 if (tablename_val
== NULL
)
164 enum nss_status status
= _nss_create_tablename (errnop
);
166 if (status
!= NSS_STATUS_SUCCESS
)
170 /* Get the next entry until we found a correct one. */
174 nis_result
*saved_result
;
179 result
= nis_first_entry (tablename_val
);
183 return NSS_STATUS_TRYAGAIN
;
185 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
186 return niserr2nss (result
->status
);
190 saved_result
= result
;
191 result
= nis_next_entry (tablename_val
, &result
->cookie
);
195 return NSS_STATUS_TRYAGAIN
;
197 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
199 nis_freeresult (saved_result
);
200 return niserr2nss (result
->status
);
204 parse_res
= _nss_nisplus_parse_etherent (result
, ether
, buffer
,
208 nis_freeresult (result
);
209 result
= saved_result
;
210 return NSS_STATUS_TRYAGAIN
;
213 if (saved_result
!= NULL
)
214 nis_freeresult (saved_result
);
219 return NSS_STATUS_SUCCESS
;
223 _nss_nisplus_getetherent_r (struct etherent
*result
, char *buffer
,
224 size_t buflen
, int *errnop
)
228 __libc_lock_lock (lock
);
230 status
= internal_nisplus_getetherent_r (result
, buffer
, buflen
, errnop
);
232 __libc_lock_unlock (lock
);
238 _nss_nisplus_gethostton_r (const char *name
, struct etherent
*eth
,
239 char *buffer
, size_t buflen
, int *errnop
)
241 if (tablename_val
== NULL
)
243 enum nss_status status
= _nss_create_tablename (errnop
);
245 if (status
!= NSS_STATUS_SUCCESS
)
252 return NSS_STATUS_UNAVAIL
;
255 char buf
[strlen (name
) + 9 + tablename_len
];
258 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
260 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
266 return NSS_STATUS_TRYAGAIN
;
269 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
271 enum nss_status status
= niserr2nss (result
->status
);
272 nis_freeresult (result
);
276 int parse_res
= _nss_nisplus_parse_etherent (result
, eth
, buffer
,
279 /* We do not need the lookup result anymore. */
280 nis_freeresult (result
);
282 if (__builtin_expect (parse_res
< 1, 0))
284 __set_errno (olderr
);
287 return NSS_STATUS_TRYAGAIN
;
289 return NSS_STATUS_NOTFOUND
;
292 return NSS_STATUS_SUCCESS
;
296 _nss_nisplus_getntohost_r (const struct ether_addr
*addr
, struct etherent
*eth
,
297 char *buffer
, size_t buflen
, int *errnop
)
299 if (tablename_val
== NULL
)
301 __libc_lock_lock (lock
);
303 enum nss_status status
= _nss_create_tablename (errnop
);
305 __libc_lock_unlock (lock
);
307 if (status
!= NSS_STATUS_SUCCESS
)
314 return NSS_STATUS_UNAVAIL
;
317 char buf
[26 + tablename_len
];
319 snprintf (buf
, sizeof (buf
),
320 "[addr=%" PRIx8
":%" PRIx8
":%" PRIx8
":%" PRIx8
":%" PRIx8
322 addr
->ether_addr_octet
[0], addr
->ether_addr_octet
[1],
323 addr
->ether_addr_octet
[2], addr
->ether_addr_octet
[3],
324 addr
->ether_addr_octet
[4], addr
->ether_addr_octet
[5],
327 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
333 return NSS_STATUS_TRYAGAIN
;
336 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
338 enum nss_status status
= niserr2nss (result
->status
);
339 nis_freeresult (result
);
343 int parse_res
= _nss_nisplus_parse_etherent (result
, eth
, buffer
,
346 /* We do not need the lookup result anymore. */
347 nis_freeresult (result
);
349 if (__builtin_expect (parse_res
< 1, 0))
352 return NSS_STATUS_TRYAGAIN
;
354 return NSS_STATUS_NOTFOUND
;
357 return NSS_STATUS_SUCCESS
;