1 /* Copyright (C) 1996, 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>, 1996.
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. */
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <bits/libc-lock.h>
28 #include <rpcsvc/yp.h>
29 #include <rpcsvc/ypclnt.h>
33 /* Get the declaration of the parser function. */
34 #define ENTNAME netent
36 #include <nss/nss_files/files-parse.c>
38 __libc_lock_define_initialized (static, lock
)
40 static bool_t new_start
= 1;
41 static char *oldkey
= NULL
;
42 static int oldkeylen
= 0;
45 _nss_nis_setnetent (void)
47 __libc_lock_lock (lock
);
57 __libc_lock_unlock (lock
);
59 return NSS_STATUS_SUCCESS
;
63 _nss_nis_endnetent (void)
65 __libc_lock_lock (lock
);
75 __libc_lock_unlock (lock
);
77 return NSS_STATUS_SUCCESS
;
80 static enum nss_status
81 internal_nis_getnetent_r (struct netent
*net
, char *buffer
, size_t buflen
,
82 int *errnop
, int *herrnop
)
84 struct parser_data
*data
= (void *) buffer
;
85 char *domain
, *result
, *outkey
;
86 int len
, keylen
, parse_res
;
88 if (yp_get_default_domain (&domain
))
89 return NSS_STATUS_UNAVAIL
;
91 /* Get the next entry until we found a correct one. */
94 enum nss_status retval
;
98 retval
= yperr2nss (yp_first (domain
, "networks.byname",
99 &outkey
, &keylen
, &result
, &len
));
101 retval
= yperr2nss ( yp_next (domain
, "networks.byname",
103 &outkey
, &keylen
, &result
, &len
));
105 if (retval
!= NSS_STATUS_SUCCESS
)
107 if (retval
== NSS_STATUS_NOTFOUND
)
109 else if (retval
== NSS_STATUS_TRYAGAIN
)
111 *herrnop
= NETDB_INTERNAL
;
117 if ((size_t) (len
+ 1) > buflen
)
121 *herrnop
= NETDB_INTERNAL
;
122 return NSS_STATUS_TRYAGAIN
;
125 p
= strncpy (buffer
, result
, len
);
131 parse_res
= _nss_files_parse_netent (p
, net
, data
, buflen
, errnop
);
135 *herrnop
= NETDB_INTERNAL
;
137 return NSS_STATUS_TRYAGAIN
;
147 return NSS_STATUS_SUCCESS
;
151 _nss_nis_getnetent_r (struct netent
*net
, char *buffer
, size_t buflen
,
152 int *errnop
, int *herrnop
)
154 enum nss_status status
;
156 __libc_lock_lock (lock
);
158 status
= internal_nis_getnetent_r (net
, buffer
, buflen
, errnop
, herrnop
);
160 __libc_lock_unlock (lock
);
166 _nss_nis_getnetbyname_r (const char *name
, struct netent
*net
, char *buffer
,
167 size_t buflen
, int *errnop
, int *herrnop
)
169 enum nss_status retval
;
170 struct parser_data
*data
= (void *) buffer
;
171 char *domain
, *result
, *p
;
177 *herrnop
= NETDB_INTERNAL
;
178 return NSS_STATUS_UNAVAIL
;
181 if (yp_get_default_domain (&domain
))
182 return NSS_STATUS_UNAVAIL
;
184 if (buflen
< sizeof *data
+ 1)
186 *herrnop
= NETDB_INTERNAL
;
188 return NSS_STATUS_TRYAGAIN
;
192 /* Convert name to lowercase. */
193 size_t namlen
= strlen (name
);
194 char name2
[namlen
+ 1];
197 for (i
= 0; i
< namlen
; ++i
)
198 name2
[i
] = tolower (name
[i
]);
201 retval
= yperr2nss (yp_match (domain
, "networks.byname", name2
,
202 namlen
, &result
, &len
));
206 if (retval
!= NSS_STATUS_SUCCESS
)
208 if (retval
== NSS_STATUS_NOTFOUND
)
210 else if (retval
== NSS_STATUS_TRYAGAIN
)
213 *herrnop
= NETDB_INTERNAL
;
218 if ((size_t) (len
+ 1) > buflen
)
222 *herrnop
= NETDB_INTERNAL
;
223 return NSS_STATUS_TRYAGAIN
;
226 p
= strncpy (buffer
, result
, len
);
232 parse_res
= _nss_files_parse_netent (p
, net
, data
, buflen
, errnop
);
236 *herrnop
= NETDB_INTERNAL
;
238 return NSS_STATUS_TRYAGAIN
;
242 return NSS_STATUS_NOTFOUND
;
246 return NSS_STATUS_SUCCESS
;
250 _nss_nis_getnetbyaddr_r (unsigned long addr
, int type
, struct netent
*net
,
251 char *buffer
, size_t buflen
, int *errnop
,
254 struct parser_data
*data
= (void *) buffer
;
263 if (yp_get_default_domain (&domain
))
264 return NSS_STATUS_UNAVAIL
;
266 in
= inet_makeaddr (addr
, 0);
267 strcpy (buf
, inet_ntoa (in
));
272 enum nss_status retval
;
275 retval
= yperr2nss (yp_match (domain
, "networks.byaddr", buf
,
276 strlen (buf
), &result
, &len
));
278 if (retval
!= NSS_STATUS_SUCCESS
)
280 if (retval
== NSS_STATUS_NOTFOUND
)
282 if (buf
[blen
- 2] == '.' && buf
[blen
- 1] == '0')
284 /* Try again, but with trailing dot(s)
285 removed (one by one) */
286 buf
[blen
- 2] = '\0';
293 return NSS_STATUS_NOTFOUND
;
298 if (retval
== NSS_STATUS_TRYAGAIN
)
304 if ((size_t) (len
+ 1) > buflen
)
308 *herrnop
= NETDB_INTERNAL
;
309 return NSS_STATUS_TRYAGAIN
;
312 p
= strncpy (buffer
, result
, len
);
318 parse_res
= _nss_files_parse_netent (p
, net
, data
, buflen
, errnop
);
322 *herrnop
= NETDB_INTERNAL
;
324 return NSS_STATUS_TRYAGAIN
;
328 return NSS_STATUS_NOTFOUND
;
332 return NSS_STATUS_SUCCESS
;