1 /* Copyright (C) 1997, 1998, 2000, 2001, 2002 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
24 #include <bits/libc-lock.h>
26 #include <netinet/ether.h>
27 #include <rpcsvc/nis.h>
28 #include <netinet/if_ether.h>
30 #include "nss-nisplus.h"
32 __libc_lock_define_initialized (static, lock
)
34 static nis_result
*result
;
35 static nis_name tablename_val
;
36 static u_long tablename_len
;
39 #define NISENTRYVAL(idx,col,res) \
40 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
42 #define NISENTRYLEN(idx,col,res) \
43 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
46 _nss_nisplus_parse_etherent (nis_result
*result
, struct etherent
*ether
,
47 char *buffer
, size_t buflen
, int *errnop
)
50 size_t room_left
= buflen
;
55 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
)
56 || result
->objects
.objects_len
!= 1
57 || __type_of (NIS_RES_OBJECT (result
)) != NIS_ENTRY_OBJ
58 || strcmp (NIS_RES_OBJECT (result
)->EN_data
.en_type
,
60 || NIS_RES_OBJECT (result
)->EN_data
.en_cols
.en_cols_len
< 2)
63 /* Generate the ether entry format and use the normal parser */
64 if (NISENTRYLEN (0, 0, result
) +1 > room_left
)
69 strncpy (p
, NISENTRYVAL (0, 0, result
), NISENTRYLEN (0, 0, result
));
70 room_left
-= (NISENTRYLEN (0, 0, result
) +1);
73 ether
->e_addr
= *ether_aton (NISENTRYVAL (0, 1, result
));
78 static enum nss_status
79 _nss_create_tablename (int *errnop
)
81 if (tablename_val
== NULL
)
83 char buf
[40 + strlen (nis_local_directory ())];
86 p
= __stpcpy (buf
, "ethers.org_dir.");
87 p
= __stpcpy (p
, nis_local_directory ());
88 tablename_val
= __strdup (buf
);
89 if (tablename_val
== NULL
)
92 return NSS_STATUS_TRYAGAIN
;
94 tablename_len
= strlen (tablename_val
);
96 return NSS_STATUS_SUCCESS
;
101 _nss_nisplus_setetherent (int stayopen
)
103 enum nss_status status
;
106 status
= NSS_STATUS_SUCCESS
;
108 __libc_lock_lock (lock
);
111 nis_freeresult (result
);
114 if (_nss_create_tablename (&err
) != NSS_STATUS_SUCCESS
)
115 status
= NSS_STATUS_UNAVAIL
;
117 __libc_lock_unlock (lock
);
119 return NSS_STATUS_SUCCESS
;
123 _nss_nisplus_endetherent (void)
125 __libc_lock_lock (lock
);
128 nis_freeresult (result
);
131 __libc_lock_unlock (lock
);
133 return NSS_STATUS_SUCCESS
;
136 static enum nss_status
137 internal_nisplus_getetherent_r (struct etherent
*ether
, char *buffer
,
138 size_t buflen
, int *errnop
)
142 if (tablename_val
== NULL
)
144 enum nss_status status
= _nss_create_tablename (errnop
);
146 if (status
!= NSS_STATUS_SUCCESS
)
150 /* Get the next entry until we found a correct one. */
153 nis_result
*saved_result
;
158 result
= nis_first_entry (tablename_val
);
159 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
160 return niserr2nss (result
->status
);
166 res2
= nis_next_entry(tablename_val
, &result
->cookie
);
167 saved_result
= result
;
169 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
171 nis_freeresult (saved_result
);
172 return niserr2nss (result
->status
);
176 parse_res
= _nss_nisplus_parse_etherent (result
, ether
, buffer
,
180 nis_freeresult (result
);
182 result
= saved_result
;
183 return NSS_STATUS_TRYAGAIN
;
187 if (saved_result
!= NULL
)
188 nis_freeresult (saved_result
);
191 } while (!parse_res
);
193 return NSS_STATUS_SUCCESS
;
197 _nss_nisplus_getetherent_r (struct etherent
*result
, char *buffer
,
198 size_t buflen
, int *errnop
)
202 __libc_lock_lock (lock
);
204 status
= internal_nisplus_getetherent_r (result
, buffer
, buflen
, errnop
);
206 __libc_lock_unlock (lock
);
212 _nss_nisplus_gethostton_r (const char *name
, struct etherent
*eth
,
213 char *buffer
, size_t buflen
, int *errnop
)
217 if (tablename_val
== NULL
)
219 enum nss_status status
= _nss_create_tablename (errnop
);
221 if (status
!= NSS_STATUS_SUCCESS
)
228 return NSS_STATUS_UNAVAIL
;
233 char buf
[strlen (name
) + 40 + tablename_len
];
236 sprintf (buf
, "[name=%s],%s", name
, tablename_val
);
238 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
240 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
242 enum nss_status status
= niserr2nss (result
->status
);
243 nis_freeresult (result
);
247 parse_res
= _nss_nisplus_parse_etherent (result
, eth
, buffer
,
251 __set_errno (olderr
);
255 nis_freeresult (result
);
257 return NSS_STATUS_TRYAGAIN
;
260 return NSS_STATUS_NOTFOUND
;
262 return NSS_STATUS_SUCCESS
;
267 _nss_nisplus_getntohost_r (const struct ether_addr
*addr
,
268 struct etherent
*eth
,
269 char *buffer
, size_t buflen
, int *errnop
)
271 if (tablename_val
== NULL
)
273 enum nss_status status
= _nss_create_tablename (errnop
);
275 if (status
!= NSS_STATUS_SUCCESS
)
282 return NSS_STATUS_UNAVAIL
;
288 char buf
[255 + tablename_len
];
290 sprintf (buf
, "[addr=%x:%x:%x:%x:%x:%x],ethers.org_dir",
291 addr
->ether_addr_octet
[0], addr
->ether_addr_octet
[1],
292 addr
->ether_addr_octet
[2], addr
->ether_addr_octet
[3],
293 addr
->ether_addr_octet
[4], addr
->ether_addr_octet
[5]);
295 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
297 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
299 enum nss_status status
= niserr2nss (result
->status
);
300 nis_freeresult (result
);
304 parse_res
= _nss_nisplus_parse_etherent (result
, eth
, buffer
,
310 nis_freeresult (result
);
312 return NSS_STATUS_TRYAGAIN
;
315 return NSS_STATUS_NOTFOUND
;
317 return NSS_STATUS_SUCCESS
;