1 /* Copyright (C) 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
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
= NULL
;
35 static nis_name tablename_val
= NULL
;
36 static u_long tablename_len
= 0;
38 /* Because the `ethers' lookup does not fit so well in the scheme so
39 we define a dummy struct here which helps us to use the available
44 struct ether_addr e_addr
;
46 struct etherent_data
{};
48 #define NISENTRYVAL(idx,col,res) \
49 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
51 #define NISENTRYLEN(idx,col,res) \
52 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
55 _nss_nisplus_parse_etherent (nis_result
*result
, struct etherent
*ether
,
56 char *buffer
, size_t buflen
, int *errnop
)
59 size_t room_left
= buflen
;
64 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
)
65 || result
->objects
.objects_len
!= 1
66 || __type_of (NIS_RES_OBJECT (result
)) != NIS_ENTRY_OBJ
67 || strcmp (NIS_RES_OBJECT (result
)->EN_data
.en_type
,
69 || NIS_RES_OBJECT (result
)->EN_data
.en_cols
.en_cols_len
< 2)
72 /* Generate the ether entry format and use the normal parser */
73 if (NISENTRYLEN (0, 0, result
) +1 > room_left
)
78 strncpy (p
, NISENTRYVAL (0, 0, result
), NISENTRYLEN (0, 0, result
));
79 room_left
-= (NISENTRYLEN (0, 0, result
) +1);
82 ether
->e_addr
= *ether_aton (NISENTRYVAL (0, 1, result
));
87 static enum nss_status
88 _nss_create_tablename (int *errnop
)
90 if (tablename_val
== NULL
)
92 char buf
[40 + strlen (nis_local_directory ())];
95 p
= __stpcpy (buf
, "ethers.org_dir.");
96 p
= __stpcpy (p
, nis_local_directory ());
97 tablename_val
= __strdup (buf
);
98 if (tablename_val
== NULL
)
101 return NSS_STATUS_TRYAGAIN
;
103 tablename_len
= strlen (tablename_val
);
105 return NSS_STATUS_SUCCESS
;
110 _nss_nisplus_setetherent (void)
112 enum nss_status status
;
115 status
= NSS_STATUS_SUCCESS
;
117 __libc_lock_lock (lock
);
120 nis_freeresult (result
);
123 if (_nss_create_tablename (&err
) != NSS_STATUS_SUCCESS
)
124 status
= NSS_STATUS_UNAVAIL
;
126 __libc_lock_unlock (lock
);
128 return NSS_STATUS_SUCCESS
;
132 _nss_nisplus_endetherent (void)
134 __libc_lock_lock (lock
);
137 nis_freeresult (result
);
140 __libc_lock_unlock (lock
);
142 return NSS_STATUS_SUCCESS
;
145 static enum nss_status
146 internal_nisplus_getetherent_r (struct etherent
*ether
, char *buffer
,
147 size_t buflen
, int *errnop
)
151 if (tablename_val
== NULL
)
153 enum nss_status status
= _nss_create_tablename (errnop
);
155 if (status
!= NSS_STATUS_SUCCESS
)
159 /* Get the next entry until we found a correct one. */
162 nis_result
*saved_result
;
167 result
= nis_first_entry (tablename_val
);
168 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
169 return niserr2nss (result
->status
);
175 res2
= nis_next_entry(tablename_val
, &result
->cookie
);
176 saved_result
= result
;
178 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
180 nis_freeresult (saved_result
);
181 return niserr2nss (result
->status
);
185 parse_res
= _nss_nisplus_parse_etherent (result
, ether
, buffer
,
189 nis_freeresult (result
);
191 result
= saved_result
;
192 return NSS_STATUS_TRYAGAIN
;
196 if (saved_result
!= NULL
)
197 nis_freeresult (saved_result
);
200 } while (!parse_res
);
202 return NSS_STATUS_SUCCESS
;
206 _nss_nisplus_getetherent_r (struct etherent
*result
, char *buffer
,
207 size_t buflen
, int *errnop
)
211 __libc_lock_lock (lock
);
213 status
= internal_nisplus_getetherent_r (result
, buffer
, buflen
, errnop
);
215 __libc_lock_unlock (lock
);
221 _nss_nisplus_gethostton_r (const char *name
, struct etherent
*eth
,
222 char *buffer
, size_t buflen
, int *errnop
)
226 if (tablename_val
== NULL
)
228 enum nss_status status
= _nss_create_tablename (errnop
);
230 if (status
!= NSS_STATUS_SUCCESS
)
237 return NSS_STATUS_UNAVAIL
;
242 char buf
[strlen (name
) + 40 + tablename_len
];
244 sprintf (buf
, "[name=%s],%s", name
, tablename_val
);
246 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
248 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
250 enum nss_status status
= niserr2nss (result
->status
);
251 nis_freeresult (result
);
255 parse_res
= _nss_nisplus_parse_etherent (result
, eth
, buffer
,
261 nis_freeresult (result
);
263 return NSS_STATUS_TRYAGAIN
;
266 return NSS_STATUS_NOTFOUND
;
268 return NSS_STATUS_SUCCESS
;
273 _nss_nisplus_getntohost_r (const struct ether_addr
*addr
,
274 struct etherent
*eth
,
275 char *buffer
, size_t buflen
, int *errnop
)
277 if (tablename_val
== NULL
)
279 enum nss_status status
= _nss_create_tablename (errnop
);
281 if (status
!= NSS_STATUS_SUCCESS
)
288 return NSS_STATUS_UNAVAIL
;
294 char buf
[255 + tablename_len
];
296 sprintf (buf
, "[addr=%x:%x:%x:%x:%x:%x],ethers.org_dir",
297 addr
->ether_addr_octet
[0], addr
->ether_addr_octet
[1],
298 addr
->ether_addr_octet
[2], addr
->ether_addr_octet
[3],
299 addr
->ether_addr_octet
[4], addr
->ether_addr_octet
[5]);
301 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
303 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
305 enum nss_status status
= niserr2nss (result
->status
);
306 nis_freeresult (result
);
310 parse_res
= _nss_nisplus_parse_etherent (result
, eth
, buffer
,
316 nis_freeresult (result
);
318 return NSS_STATUS_TRYAGAIN
;
321 return NSS_STATUS_NOTFOUND
;
323 return NSS_STATUS_SUCCESS
;