1 /* Copyright (C) 1997 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
)
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 (void)
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
)
99 return NSS_STATUS_TRYAGAIN
;
100 tablename_len
= strlen (tablename_val
);
102 return NSS_STATUS_SUCCESS
;
107 _nss_nisplus_setetherent (void)
109 enum nss_status status
;
111 status
= NSS_STATUS_SUCCESS
;
113 __libc_lock_lock (lock
);
116 nis_freeresult (result
);
119 if (_nss_create_tablename () != NSS_STATUS_SUCCESS
)
120 status
= NSS_STATUS_UNAVAIL
;
122 __libc_lock_unlock (lock
);
124 return NSS_STATUS_SUCCESS
;
128 _nss_nisplus_endetherent (void)
130 __libc_lock_lock (lock
);
133 nis_freeresult (result
);
136 __libc_lock_unlock (lock
);
138 return NSS_STATUS_SUCCESS
;
141 static enum nss_status
142 internal_nisplus_getetherent_r (struct etherent
*ether
, char *buffer
,
147 if (tablename_val
== NULL
)
148 if (_nss_create_tablename () != NSS_STATUS_SUCCESS
)
149 return NSS_STATUS_UNAVAIL
;
151 /* Get the next entry until we found a correct one. */
154 nis_result
*saved_result
;
159 result
= nis_first_entry(tablename_val
);
160 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
161 return niserr2nss (result
->status
);
167 res2
= nis_next_entry(tablename_val
, &result
->cookie
);
168 saved_result
= result
;
170 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
172 nis_freeresult (saved_result
);
173 return niserr2nss (result
->status
);
177 if ((parse_res
= _nss_nisplus_parse_etherent (result
, ether
, buffer
,
180 nis_freeresult (result
);
181 result
= saved_result
;
182 return NSS_STATUS_TRYAGAIN
;
186 if (saved_result
!= NULL
)
187 nis_freeresult (saved_result
);
190 } while (!parse_res
);
192 return NSS_STATUS_SUCCESS
;
196 _nss_nisplus_getetherent_r (struct etherent
*result
, char *buffer
,
201 __libc_lock_lock (lock
);
203 status
= internal_nisplus_getetherent_r (result
, buffer
, buflen
);
205 __libc_lock_unlock (lock
);
211 _nss_nisplus_gethostton_r (const char *name
, struct etherent
*eth
,
212 char *buffer
, size_t buflen
)
216 if (tablename_val
== NULL
)
217 if (_nss_create_tablename () != NSS_STATUS_SUCCESS
)
218 return NSS_STATUS_UNAVAIL
;
223 char buf
[strlen (name
) + 40 + tablename_len
];
225 sprintf(buf
, "[name=%s],%s", name
, tablename_val
);
227 result
= nis_list(buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
229 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
231 enum nss_status status
= niserr2nss (result
->status
);
232 nis_freeresult (result
);
236 if ((parse_res
= _nss_nisplus_parse_etherent (result
, eth
, buffer
,
239 nis_freeresult (result
);
240 return NSS_STATUS_TRYAGAIN
;
244 return NSS_STATUS_SUCCESS
;
246 return NSS_STATUS_NOTFOUND
;
250 _nss_nisplus_getntohost_r (const struct ether_addr
*addr
,
251 struct etherent
*eth
,
252 char *buffer
, size_t buflen
)
254 if (tablename_val
== NULL
)
255 if (_nss_create_tablename () != NSS_STATUS_SUCCESS
)
256 return NSS_STATUS_UNAVAIL
;
260 __set_errno (EINVAL
);
261 return NSS_STATUS_UNAVAIL
;
267 char buf
[255 + tablename_len
];
269 memset (&buf
, '\0', sizeof (buf
));
270 sprintf(buf
, "[addr=%x:%x:%x:%x:%x:%x],ethers.org_dir",
271 addr
->ether_addr_octet
[0], addr
->ether_addr_octet
[1],
272 addr
->ether_addr_octet
[2], addr
->ether_addr_octet
[3],
273 addr
->ether_addr_octet
[4], addr
->ether_addr_octet
[5]);
275 result
= nis_list(buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
277 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
279 enum nss_status status
= niserr2nss (result
->status
);
280 nis_freeresult (result
);
284 if ((parse_res
= _nss_nisplus_parse_etherent (result
, eth
, buffer
,
287 nis_freeresult (result
);
288 return NSS_STATUS_TRYAGAIN
;
292 return NSS_STATUS_SUCCESS
;
294 return NSS_STATUS_NOTFOUND
;