1 /* Copyright (C) 1996,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>, 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
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
31 /* Get the declaration of the parser function. */
32 #define ENTNAME protoent
34 #include <nss/nss_files/files-parse.c>
36 __libc_lock_define_initialized (static, lock
)
41 struct response
*next
;
44 static struct response
*start
;
45 static struct response
*next
;
48 saveit (int instatus
, char *inkey
, int inkeylen
, char *inval
,
49 int invallen
, char *indata
)
51 if (instatus
!= YP_TRUE
)
54 if (inkey
&& inkeylen
> 0 && inval
&& invallen
> 0)
58 start
= malloc (sizeof (struct response
));
65 next
->next
= malloc (sizeof (struct response
));
66 if (next
->next
== NULL
)
71 next
->val
= malloc (invallen
+ 1);
72 if (next
->val
== NULL
)
74 strncpy (next
->val
, inval
, invallen
);
75 next
->val
[invallen
] = '\0';
81 static enum nss_status
82 internal_nis_setprotoent (void)
85 struct ypall_callback ypcb
;
86 enum nss_status status
;
88 yp_get_default_domain (&domainname
);
92 if (start
->val
!= NULL
)
100 ypcb
.foreach
= saveit
;
102 status
= yperr2nss (yp_all (domainname
, "protocols.bynumber", &ypcb
));
109 _nss_nis_setprotoent (int stayopen
)
111 enum nss_status status
;
113 __libc_lock_lock (lock
);
115 status
= internal_nis_setprotoent ();
117 __libc_lock_unlock (lock
);
123 _nss_nis_endprotoent (void)
125 __libc_lock_lock (lock
);
127 while (start
!= NULL
)
129 if (start
->val
!= NULL
)
138 __libc_lock_unlock (lock
);
140 return NSS_STATUS_SUCCESS
;
143 static enum nss_status
144 internal_nis_getprotoent_r (struct protoent
*proto
,
145 char *buffer
, size_t buflen
, int *errnop
)
147 struct parser_data
*data
= (void *) buffer
;
151 internal_nis_setprotoent ();
153 /* Get the next entry until we found a correct one. */
159 return NSS_STATUS_NOTFOUND
;
161 p
= strncpy (buffer
, next
->val
, buflen
);
166 parse_res
= _nss_files_parse_protoent (p
, proto
, data
, buflen
, errnop
);
168 return NSS_STATUS_TRYAGAIN
;
173 return NSS_STATUS_SUCCESS
;
177 _nss_nis_getprotoent_r (struct protoent
*proto
, char *buffer
, size_t buflen
,
180 enum nss_status status
;
182 __libc_lock_lock (lock
);
184 status
= internal_nis_getprotoent_r (proto
, buffer
, buflen
, errnop
);
186 __libc_lock_unlock (lock
);
192 _nss_nis_getprotobyname_r (const char *name
, struct protoent
*proto
,
193 char *buffer
, size_t buflen
, int *errnop
)
195 struct parser_data
*data
= (void *) buffer
;
196 enum nss_status retval
;
197 char *domain
, *result
, *p
;
203 return NSS_STATUS_UNAVAIL
;
206 if (yp_get_default_domain (&domain
))
207 return NSS_STATUS_UNAVAIL
;
209 retval
= yperr2nss (yp_match (domain
, "protocols.byname", name
,
210 strlen (name
), &result
, &len
));
212 if (retval
!= NSS_STATUS_SUCCESS
)
214 if (retval
== NSS_STATUS_TRYAGAIN
)
219 if ((size_t) (len
+ 1) > buflen
)
223 return NSS_STATUS_TRYAGAIN
;
226 p
= strncpy (buffer
, result
, len
);
232 parse_res
= _nss_files_parse_protoent (p
, proto
, data
, buflen
, errnop
);
236 return NSS_STATUS_TRYAGAIN
;
238 return NSS_STATUS_NOTFOUND
;
240 return NSS_STATUS_SUCCESS
;
244 _nss_nis_getprotobynumber_r (int number
, struct protoent
*proto
,
245 char *buffer
, size_t buflen
, int *errnop
)
247 struct parser_data
*data
= (void *) buffer
;
248 enum nss_status retval
;
249 char *domain
, *result
, *p
;
250 int len
, nlen
, parse_res
;
253 if (yp_get_default_domain (&domain
))
254 return NSS_STATUS_UNAVAIL
;
256 nlen
= sprintf (buf
, "%d", number
);
258 retval
= yperr2nss (yp_match (domain
, "protocols.bynumber", buf
,
259 nlen
, &result
, &len
));
261 if (retval
!= NSS_STATUS_SUCCESS
)
263 if (retval
== NSS_STATUS_TRYAGAIN
)
268 if ((size_t) (len
+ 1) > buflen
)
272 return NSS_STATUS_TRYAGAIN
;
275 p
= strncpy (buffer
, result
, len
);
281 parse_res
= _nss_files_parse_protoent (p
, proto
, data
, buflen
, errnop
);
285 return NSS_STATUS_TRYAGAIN
;
287 return NSS_STATUS_NOTFOUND
;
289 return NSS_STATUS_SUCCESS
;