1 /* Copyright (C) 1996-2000, 2001, 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
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
21 /* The following is an ugly trick to avoid a prototype declaration for
23 #define _nss_nis_endnetent _nss_nis_endnetent_XXX
25 #undef _nss_nis_endnetent
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <bits/libc-lock.h>
33 #include <rpcsvc/yp.h>
34 #include <rpcsvc/ypclnt.h>
38 /* Get the declaration of the parser function. */
39 #define ENTNAME netent
41 #include <nss/nss_files/files-parse.c>
43 __libc_lock_define_initialized (static, lock
)
45 static bool_t new_start
= 1;
50 _nss_nis_setnetent (int stayopen
)
52 __libc_lock_lock (lock
);
62 __libc_lock_unlock (lock
);
64 return NSS_STATUS_SUCCESS
;
66 /* Make _nss_nis_endnetent an alias of _nss_nis_setnetent. We do this
67 even though the prototypes don't match. The argument of setnetent
68 is not used so this makes no difference. */
69 strong_alias (_nss_nis_setnetent
, _nss_nis_endnetent
)
71 static enum nss_status
72 internal_nis_getnetent_r (struct netent
*net
, char *buffer
, size_t buflen
,
73 int *errnop
, int *herrnop
)
75 struct parser_data
*data
= (void *) buffer
;
76 char *domain
, *result
, *outkey
;
77 int len
, keylen
, parse_res
;
79 if (yp_get_default_domain (&domain
))
80 return NSS_STATUS_UNAVAIL
;
82 /* Get the next entry until we found a correct one. */
85 enum nss_status retval
;
89 retval
= yperr2nss (yp_first (domain
, "networks.byname",
90 &outkey
, &keylen
, &result
, &len
));
92 retval
= yperr2nss ( yp_next (domain
, "networks.byname",
94 &outkey
, &keylen
, &result
, &len
));
96 if (retval
!= NSS_STATUS_SUCCESS
)
98 if (retval
== NSS_STATUS_TRYAGAIN
)
100 *herrnop
= NETDB_INTERNAL
;
106 if ((size_t) (len
+ 1) > buflen
)
110 *herrnop
= NETDB_INTERNAL
;
111 return NSS_STATUS_TRYAGAIN
;
114 p
= strncpy (buffer
, result
, len
);
120 parse_res
= _nss_files_parse_netent (p
, net
, data
, buflen
, errnop
);
124 *herrnop
= NETDB_INTERNAL
;
126 return NSS_STATUS_TRYAGAIN
;
136 return NSS_STATUS_SUCCESS
;
140 _nss_nis_getnetent_r (struct netent
*net
, char *buffer
, size_t buflen
,
141 int *errnop
, int *herrnop
)
143 enum nss_status status
;
145 __libc_lock_lock (lock
);
147 status
= internal_nis_getnetent_r (net
, buffer
, buflen
, errnop
, herrnop
);
149 __libc_lock_unlock (lock
);
155 _nss_nis_getnetbyname_r (const char *name
, struct netent
*net
, char *buffer
,
156 size_t buflen
, int *errnop
, int *herrnop
)
158 enum nss_status retval
;
159 struct parser_data
*data
= (void *) buffer
;
160 char *domain
, *result
, *p
;
166 *herrnop
= NETDB_INTERNAL
;
167 return NSS_STATUS_UNAVAIL
;
170 if (yp_get_default_domain (&domain
))
171 return NSS_STATUS_UNAVAIL
;
173 if (buflen
< sizeof *data
+ 1)
175 *herrnop
= NETDB_INTERNAL
;
177 return NSS_STATUS_TRYAGAIN
;
181 /* Convert name to lowercase. */
182 size_t namlen
= strlen (name
);
183 char name2
[namlen
+ 1];
186 for (i
= 0; i
< namlen
; ++i
)
187 name2
[i
] = _tolower (name
[i
]);
190 retval
= yperr2nss (yp_match (domain
, "networks.byname", name2
,
191 namlen
, &result
, &len
));
195 if (retval
!= NSS_STATUS_SUCCESS
)
197 if (retval
== NSS_STATUS_TRYAGAIN
)
200 *herrnop
= NETDB_INTERNAL
;
205 if ((size_t) (len
+ 1) > buflen
)
209 *herrnop
= NETDB_INTERNAL
;
210 return NSS_STATUS_TRYAGAIN
;
213 p
= strncpy (buffer
, result
, len
);
219 parse_res
= _nss_files_parse_netent (p
, net
, data
, buflen
, errnop
);
223 *herrnop
= NETDB_INTERNAL
;
225 return NSS_STATUS_TRYAGAIN
;
227 return NSS_STATUS_NOTFOUND
;
230 return NSS_STATUS_SUCCESS
;
234 _nss_nis_getnetbyaddr_r (uint32_t addr
, int type
, struct netent
*net
,
235 char *buffer
, size_t buflen
, int *errnop
,
238 struct parser_data
*data
= (void *) buffer
;
247 if (yp_get_default_domain (&domain
))
248 return NSS_STATUS_UNAVAIL
;
250 in
= inet_makeaddr (addr
, 0);
251 strcpy (buf
, inet_ntoa (in
));
256 enum nss_status retval
;
259 retval
= yperr2nss (yp_match (domain
, "networks.byaddr", buf
,
260 strlen (buf
), &result
, &len
));
262 if (retval
!= NSS_STATUS_SUCCESS
)
264 if (retval
== NSS_STATUS_NOTFOUND
)
266 if (buf
[blen
- 2] == '.' && buf
[blen
- 1] == '0')
268 /* Try again, but with trailing dot(s)
269 removed (one by one) */
270 buf
[blen
- 2] = '\0';
275 return NSS_STATUS_NOTFOUND
;
279 if (retval
== NSS_STATUS_TRYAGAIN
)
285 if ((size_t) (len
+ 1) > buflen
)
289 *herrnop
= NETDB_INTERNAL
;
290 return NSS_STATUS_TRYAGAIN
;
293 p
= strncpy (buffer
, result
, len
);
299 parse_res
= _nss_files_parse_netent (p
, net
, data
, buflen
, errnop
);
303 *herrnop
= NETDB_INTERNAL
;
305 return NSS_STATUS_TRYAGAIN
;
307 return NSS_STATUS_NOTFOUND
;
310 return NSS_STATUS_SUCCESS
;