1 /* Copyright (C) 1996-2017 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, see
17 <http://www.gnu.org/licenses/>. */
24 #include <libc-lock.h>
25 #include <rpcsvc/yp.h>
26 #include <rpcsvc/ypclnt.h>
30 /* Get the declaration of the parser function. */
31 #define ENTNAME protoent
33 #include <nss/nss_files/files-parse.c>
35 __libc_lock_define_initialized (static, lock
)
39 struct response
*next
;
43 static struct response
*start
;
44 static struct response
*next
;
47 saveit (int instatus
, char *inkey
, int inkeylen
, char *inval
,
48 int invallen
, char *indata
)
50 if (instatus
!= YP_TRUE
)
53 if (inkey
&& inkeylen
> 0 && inval
&& invallen
> 0)
55 struct response
*newp
= malloc (sizeof (struct response
) + invallen
+ 1);
57 return 1; /* We have no error code for out of memory */
66 *((char *) mempcpy (newp
->val
, inval
, invallen
)) = '\0';
73 internal_nis_endprotoent (void)
83 static enum nss_status
84 internal_nis_setprotoent (void)
87 struct ypall_callback ypcb
;
88 enum nss_status status
;
90 yp_get_default_domain (&domainname
);
92 internal_nis_endprotoent ();
94 ypcb
.foreach
= saveit
;
96 status
= yperr2nss (yp_all (domainname
, "protocols.bynumber", &ypcb
));
103 _nss_nis_setprotoent (int stayopen
)
105 enum nss_status status
;
107 __libc_lock_lock (lock
);
109 status
= internal_nis_setprotoent ();
111 __libc_lock_unlock (lock
);
117 _nss_nis_endprotoent (void)
119 __libc_lock_lock (lock
);
121 internal_nis_endprotoent ();
124 __libc_lock_unlock (lock
);
126 return NSS_STATUS_SUCCESS
;
129 static enum nss_status
130 internal_nis_getprotoent_r (struct protoent
*proto
,
131 char *buffer
, size_t buflen
, int *errnop
)
133 struct parser_data
*data
= (void *) buffer
;
137 internal_nis_setprotoent ();
139 /* Get the next entry until we found a correct one. */
145 return NSS_STATUS_NOTFOUND
;
147 p
= strncpy (buffer
, next
->val
, buflen
);
152 parse_res
= _nss_files_parse_protoent (p
, proto
, data
, buflen
, errnop
);
154 return NSS_STATUS_TRYAGAIN
;
159 return NSS_STATUS_SUCCESS
;
163 _nss_nis_getprotoent_r (struct protoent
*proto
, char *buffer
, size_t buflen
,
166 enum nss_status status
;
168 __libc_lock_lock (lock
);
170 status
= internal_nis_getprotoent_r (proto
, buffer
, buflen
, errnop
);
172 __libc_lock_unlock (lock
);
178 _nss_nis_getprotobyname_r (const char *name
, struct protoent
*proto
,
179 char *buffer
, size_t buflen
, int *errnop
)
184 return NSS_STATUS_UNAVAIL
;
188 if (__glibc_unlikely (yp_get_default_domain (&domain
)))
189 return NSS_STATUS_UNAVAIL
;
193 int yperr
= yp_match (domain
, "protocols.byname", name
, strlen (name
),
196 if (__glibc_unlikely (yperr
!= YPERR_SUCCESS
))
198 enum nss_status retval
= yperr2nss (yperr
);
200 if (retval
== NSS_STATUS_TRYAGAIN
)
205 if (__glibc_unlikely ((size_t) (len
+ 1) > buflen
))
209 return NSS_STATUS_TRYAGAIN
;
212 char *p
= strncpy (buffer
, result
, len
);
218 int parse_res
= _nss_files_parse_protoent (p
, proto
, (void *) buffer
, buflen
,
220 if (__glibc_unlikely (parse_res
< 1))
223 return NSS_STATUS_TRYAGAIN
;
225 return NSS_STATUS_NOTFOUND
;
227 return NSS_STATUS_SUCCESS
;
231 _nss_nis_getprotobynumber_r (int number
, struct protoent
*proto
,
232 char *buffer
, size_t buflen
, int *errnop
)
235 if (__glibc_unlikely (yp_get_default_domain (&domain
)))
236 return NSS_STATUS_UNAVAIL
;
239 int nlen
= snprintf (buf
, sizeof (buf
), "%d", number
);
243 int yperr
= yp_match (domain
, "protocols.bynumber", buf
, nlen
, &result
,
246 if (__glibc_unlikely (yperr
!= YPERR_SUCCESS
))
248 enum nss_status retval
= yperr2nss (yperr
);
250 if (retval
== NSS_STATUS_TRYAGAIN
)
255 if (__glibc_unlikely ((size_t) (len
+ 1) > buflen
))
259 return NSS_STATUS_TRYAGAIN
;
262 char *p
= strncpy (buffer
, result
, len
);
268 int parse_res
= _nss_files_parse_protoent (p
, proto
, (void *) buffer
, buflen
,
270 if (__glibc_unlikely (parse_res
< 1))
273 return NSS_STATUS_TRYAGAIN
;
275 return NSS_STATUS_NOTFOUND
;
277 return NSS_STATUS_SUCCESS
;