(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nis / nss_nis / nis-rpc.c
blobd1ab94371a685b403ba53953537d7942e2bcbd12
1 /* Copyright (C) 1996-1998,2000,2002,2003,2004 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
18 02111-1307 USA. */
20 #include <nss.h>
21 #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 rpcent
33 #define EXTERN_PARSER
34 #include <nss/nss_files/files-parse.c>
36 __libc_lock_define_initialized (static, lock)
38 struct response_t
40 struct response_t *next;
41 char val[0];
44 struct intern_t
46 struct response_t *start;
47 struct response_t *next;
49 typedef struct intern_t intern_t;
51 static intern_t intern = {NULL, NULL};
53 static int
54 saveit (int instatus, char *inkey, int inkeylen, char *inval,
55 int invallen, char *indata)
57 intern_t *intern = (intern_t *)indata;
59 if (instatus != YP_TRUE)
60 return 1;
62 if (inkey && inkeylen > 0 && inval && invallen > 0)
64 struct response_t *newp = malloc (sizeof (struct response_t)
65 + invallen + 1);
66 if (newp == NULL)
67 return 1; /* We have no error code for out of memory */
69 if (intern->start == NULL)
70 intern->start = newp;
71 else
72 intern->next->next = newp;
73 intern->next = newp;
75 newp->next = NULL;
76 *((char *) mempcpy (newp->val, inval, invallen)) = '\0';
79 return 0;
82 static void
83 internal_nis_endrpcent (intern_t *intern)
85 while (intern->start != NULL)
87 intern->next = intern->start;
88 intern->start = intern->start->next;
89 free (intern->next);
93 static enum nss_status
94 internal_nis_setrpcent (intern_t *intern)
96 char *domainname;
97 struct ypall_callback ypcb;
98 enum nss_status status;
100 if (yp_get_default_domain (&domainname))
101 return NSS_STATUS_UNAVAIL;
103 internal_nis_endrpcent (intern);
105 ypcb.foreach = saveit;
106 ypcb.data = (char *)intern;
107 status = yperr2nss (yp_all(domainname, "rpc.bynumber", &ypcb));
108 intern->next = intern->start;
110 return status;
113 enum nss_status
114 _nss_nis_setrpcent (int stayopen)
116 enum nss_status status;
118 __libc_lock_lock (lock);
120 status = internal_nis_setrpcent (&intern);
122 __libc_lock_unlock (lock);
124 return status;
127 enum nss_status
128 _nss_nis_endrpcent (void)
130 __libc_lock_lock (lock);
132 internal_nis_endrpcent (&intern);
134 __libc_lock_unlock (lock);
136 return NSS_STATUS_SUCCESS;
139 static enum nss_status
140 internal_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
141 int *errnop, intern_t *data)
143 struct parser_data *pdata = (void *) buffer;
144 int parse_res;
145 char *p;
147 if (data->start == NULL)
148 internal_nis_setrpcent (data);
150 /* Get the next entry until we found a correct one. */
153 if (data->next == NULL)
154 return NSS_STATUS_NOTFOUND;
156 p = strncpy (buffer, data->next->val, buflen);
157 while (isspace (*p))
158 ++p;
160 parse_res = _nss_files_parse_rpcent (p, rpc, pdata, buflen, errnop);
161 if (parse_res == -1)
162 return NSS_STATUS_TRYAGAIN;
163 data->next = data->next->next;
165 while (!parse_res);
167 return NSS_STATUS_SUCCESS;
170 enum nss_status
171 _nss_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
172 int *errnop)
174 enum nss_status status;
176 __libc_lock_lock (lock);
178 status = internal_nis_getrpcent_r (rpc, buffer, buflen, errnop, &intern);
180 __libc_lock_unlock (lock);
182 return status;
185 enum nss_status
186 _nss_nis_getrpcbyname_r (const char *name, struct rpcent *rpc,
187 char *buffer, size_t buflen, int *errnop)
189 intern_t data = {NULL, NULL};
190 enum nss_status status;
191 int found;
193 if (name == NULL)
195 *errnop = EINVAL;
196 return NSS_STATUS_UNAVAIL;
199 status = internal_nis_setrpcent (&data);
200 if (status != NSS_STATUS_SUCCESS)
201 return status;
203 found = 0;
204 while (!found &&
205 ((status = internal_nis_getrpcent_r (rpc, buffer, buflen, errnop,
206 &data)) == NSS_STATUS_SUCCESS))
208 if (strcmp (rpc->r_name, name) == 0)
209 found = 1;
210 else
212 int i = 0;
214 while (rpc->r_aliases[i] != NULL)
216 if (strcmp (rpc->r_aliases[i], name) == 0)
218 found = 1;
219 break;
221 else
222 ++i;
227 internal_nis_endrpcent (&data);
229 if (!found && status == NSS_STATUS_SUCCESS)
230 return NSS_STATUS_NOTFOUND;
231 else
232 return status;
235 enum nss_status
236 _nss_nis_getrpcbynumber_r (int number, struct rpcent *rpc,
237 char *buffer, size_t buflen, int *errnop)
239 struct parser_data *data = (void *) buffer;
240 enum nss_status retval;
241 char *domain, *result, *p;
242 int len, nlen, parse_res;
243 char buf[32];
245 if (yp_get_default_domain (&domain))
246 return NSS_STATUS_UNAVAIL;
248 nlen = sprintf (buf, "%d", number);
250 retval = yperr2nss (yp_match (domain, "rpc.bynumber", buf,
251 nlen, &result, &len));
253 if (retval != NSS_STATUS_SUCCESS)
255 if (retval == NSS_STATUS_TRYAGAIN)
256 *errnop = errno;
257 return retval;
260 if ((size_t) (len + 1) > buflen)
262 free (result);
263 *errnop = ERANGE;
264 return NSS_STATUS_TRYAGAIN;
267 p = strncpy (buffer, result, len);
268 buffer[len] = '\0';
269 while (isspace (*p))
270 ++p;
271 free (result);
273 parse_res = _nss_files_parse_rpcent (p, rpc, data, buflen, errnop);
275 if (parse_res < 1)
277 if (parse_res == -1)
278 return NSS_STATUS_TRYAGAIN;
279 else
280 return NSS_STATUS_NOTFOUND;
282 else
283 return NSS_STATUS_SUCCESS;