Updated to fedora-glibc-20041207T1331
[glibc.git] / glibc-compat / nss_nis / nis-proto.c
blob8dff7d3687c7abff55b6fc7f5c1398d6291a639d
1 /* Copyright (C) 1996, 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>, 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. */
20 #include <nss.h>
21 #include <glibc-compat/include/netdb.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
29 #include "nss-nis.h"
31 /* Get the declaration of the parser function. */
32 #define ENTNAME protoent
33 #define EXTERN_PARSER
34 #include "../nss_files/files-parse.c"
36 __libc_lock_define_initialized (static, lock)
38 struct response
40 char *val;
41 struct response *next;
44 static struct response *start = NULL;
45 static struct response *next = NULL;
47 static int
48 saveit (int instatus, char *inkey, int inkeylen, char *inval,
49 int invallen, char *indata)
51 if (instatus != YP_TRUE)
52 return instatus;
54 if (inkey && inkeylen > 0 && inval && invallen > 0)
56 if (start == NULL)
58 start = malloc (sizeof (struct response));
59 next = start;
61 else
63 next->next = malloc (sizeof (struct response));
64 next = next->next;
66 next->next = NULL;
67 next->val = malloc (invallen + 1);
68 strncpy (next->val, inval, invallen);
69 next->val[invallen] = '\0';
72 return 0;
75 enum nss_status
76 internal_nis_setprotoent (void)
78 char *domainname;
79 struct ypall_callback ypcb;
80 enum nss_status status;
82 yp_get_default_domain (&domainname);
84 while (start != NULL)
86 if (start->val != NULL)
87 free (start->val);
88 next = start;
89 start = start->next;
90 free (next);
92 start = NULL;
94 ypcb.foreach = saveit;
95 ypcb.data = NULL;
96 status = yperr2nss (yp_all (domainname, "protocols.bynumber", &ypcb));
97 next = start;
99 return status;
102 enum nss_status
103 _nss_nis_setprotoent (void)
105 enum nss_status status;
107 __libc_lock_lock (lock);
109 status = internal_nis_setprotoent ();
111 __libc_lock_unlock (lock);
113 return status;
116 enum nss_status
117 _nss_nis_endprotoent (void)
119 __libc_lock_lock (lock);
121 while (start != NULL)
123 if (start->val != NULL)
124 free (start->val);
125 next = start;
126 start = start->next;
127 free (next);
129 start = NULL;
130 next = NULL;
132 __libc_lock_unlock (lock);
134 return NSS_STATUS_SUCCESS;
137 static enum nss_status
138 internal_nis_getprotoent_r (struct protoent *proto,
139 char *buffer, size_t buflen)
141 struct parser_data *data = (void *) buffer;
142 int parse_res;
144 if (start == NULL)
145 internal_nis_setprotoent ();
147 /* Get the next entry until we found a correct one. */
150 char *p;
152 if (next == NULL)
153 return NSS_STATUS_NOTFOUND;
154 p = strncpy (buffer, next->val, buflen);
155 next = next->next;
157 while (isspace (*p))
158 ++p;
160 parse_res = _nss_files_parse_protoent (p, proto, data, buflen);
161 if (parse_res == -1 && errno == ERANGE)
162 return NSS_STATUS_TRYAGAIN;
164 while (!parse_res);
166 return NSS_STATUS_SUCCESS;
169 enum nss_status
170 _nss_nis_getprotoent_r (struct protoent *proto, char *buffer, size_t buflen)
172 enum nss_status status;
174 __libc_lock_lock (lock);
176 status = internal_nis_getprotoent_r (proto, buffer, buflen);
178 __libc_lock_unlock (lock);
180 return status;
183 enum nss_status
184 _nss_nis_getprotobyname_r (const char *name, struct protoent *proto,
185 char *buffer, size_t buflen)
187 struct parser_data *data = (void *) buffer;
188 enum nss_status retval;
189 char *domain, *result, *p;
190 int len, parse_res;
192 if (name == NULL)
194 __set_errno (EINVAL);
195 return NSS_STATUS_UNAVAIL;
198 if (yp_get_default_domain (&domain))
199 return NSS_STATUS_UNAVAIL;
201 retval = yperr2nss (yp_match (domain, "protocols.byname", name,
202 strlen (name), &result, &len));
204 if (retval != NSS_STATUS_SUCCESS)
206 if (retval == NSS_STATUS_TRYAGAIN)
207 __set_errno (EAGAIN);
208 return retval;
211 if ((size_t) (len + 1) > buflen)
213 free (result);
214 __set_errno (ERANGE);
215 return NSS_STATUS_TRYAGAIN;
218 p = strncpy (buffer, result, len);
219 buffer[len] = '\0';
220 while (isspace (*p))
221 ++p;
222 free (result);
224 parse_res = _nss_files_parse_protoent (p, proto, data, buflen);
226 if (parse_res == -1 && errno == ERANGE)
227 return NSS_STATUS_TRYAGAIN;
228 else if (parse_res == 0)
229 return NSS_STATUS_NOTFOUND;
231 return NSS_STATUS_SUCCESS;
234 enum nss_status
235 _nss_nis_getprotobynumber_r (int number, struct protoent *proto,
236 char *buffer, size_t buflen)
238 struct parser_data *data = (void *) buffer;
239 enum nss_status retval;
240 char *domain, *result, *p;
241 int len, nlen, parse_res;
242 char buf[32];
244 if (yp_get_default_domain (&domain))
245 return NSS_STATUS_UNAVAIL;
247 nlen = sprintf (buf, "%d", number);
249 retval = yperr2nss (yp_match (domain, "protocols.bynumber", buf,
250 nlen, &result, &len));
252 if (retval != NSS_STATUS_SUCCESS)
254 if (retval == NSS_STATUS_TRYAGAIN)
255 __set_errno (EAGAIN);
256 return retval;
259 if ((size_t) (len + 1) > buflen)
261 free (result);
262 __set_errno (ERANGE);
263 return NSS_STATUS_TRYAGAIN;
266 p = strncpy (buffer, result, len);
267 buffer[len] = '\0';
268 while (isspace (*p))
269 ++p;
270 free (result);
272 parse_res = _nss_files_parse_protoent (p, proto, data, buflen);
274 if (parse_res == -1 && errno == ERANGE)
275 return NSS_STATUS_TRYAGAIN;
276 else if (parse_res == 0)
277 return NSS_STATUS_NOTFOUND;
279 return NSS_STATUS_SUCCESS;