]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nis/nis-rpc.c
Update.
[thirdparty/glibc.git] / nis / nss_nis / nis-rpc.c
1 /* Copyright (C) 1996, 1997, 1998, 2000, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
4
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.
9
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.
14
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. */
19
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>
28
29 #include "nss-nis.h"
30
31 /* Get the declaration of the parser function. */
32 #define ENTNAME rpcent
33 #define EXTERN_PARSER
34 #include <nss/nss_files/files-parse.c>
35
36 __libc_lock_define_initialized (static, lock)
37
38 struct response_t
39 {
40 char *val;
41 struct response_t *next;
42 };
43
44 struct intern_t
45 {
46 struct response_t *start;
47 struct response_t *next;
48 };
49 typedef struct intern_t intern_t;
50
51 static intern_t intern = {NULL, NULL};
52
53 static int
54 saveit (int instatus, char *inkey, int inkeylen, char *inval,
55 int invallen, char *indata)
56 {
57 intern_t *intern = (intern_t *)indata;
58
59 if (instatus != YP_TRUE)
60 return instatus;
61
62 if (inkey && inkeylen > 0 && inval && invallen > 0)
63 {
64 if (intern->start == NULL)
65 {
66 intern->start = malloc (sizeof (struct response_t));
67 if (intern->start == NULL)
68 return YP_FALSE;
69 intern->next = intern->start;
70 }
71 else
72 {
73 intern->next->next = malloc (sizeof (struct response_t));
74 if (intern->next->next == NULL)
75 return YP_FALSE;
76 intern->next = intern->next->next;
77 }
78 intern->next->next = NULL;
79 intern->next->val = malloc (invallen + 1);
80 if (intern->next->val == NULL)
81 return YP_FALSE;
82 strncpy (intern->next->val, inval, invallen);
83 intern->next->val[invallen] = '\0';
84 }
85
86 return 0;
87 }
88
89 static enum nss_status
90 internal_nis_setrpcent (intern_t *intern)
91 {
92 char *domainname;
93 struct ypall_callback ypcb;
94 enum nss_status status;
95
96 if (yp_get_default_domain (&domainname))
97 return NSS_STATUS_UNAVAIL;
98
99 while (intern->start != NULL)
100 {
101 if (intern->start->val != NULL)
102 free (intern->start->val);
103 intern->next = intern->start;
104 intern->start = intern->start->next;
105 free (intern->next);
106 }
107 intern->start = NULL;
108
109 ypcb.foreach = saveit;
110 ypcb.data = (char *)intern;
111 status = yperr2nss (yp_all(domainname, "rpc.bynumber", &ypcb));
112 intern->next = intern->start;
113
114 return status;
115 }
116
117 enum nss_status
118 _nss_nis_setrpcent (int stayopen)
119 {
120 enum nss_status status;
121
122 __libc_lock_lock (lock);
123
124 status = internal_nis_setrpcent (&intern);
125
126 __libc_lock_unlock (lock);
127
128 return status;
129 }
130
131 static enum nss_status
132 internal_nis_endrpcent (intern_t *intern)
133 {
134 while (intern->start != NULL)
135 {
136 if (intern->start->val != NULL)
137 free (intern->start->val);
138 intern->next = intern->start;
139 intern->start = intern->start->next;
140 free (intern->next);
141 }
142 intern->start = NULL;
143
144 return NSS_STATUS_SUCCESS;
145 }
146
147 enum nss_status
148 _nss_nis_endrpcent (void)
149 {
150 enum nss_status status;
151
152 __libc_lock_lock (lock);
153
154 status = internal_nis_endrpcent (&intern);
155
156 __libc_lock_unlock (lock);
157
158 return status;
159 }
160
161 static enum nss_status
162 internal_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
163 int *errnop, intern_t *data)
164 {
165 struct parser_data *pdata = (void *) buffer;
166 int parse_res;
167 char *p;
168
169 if (data->start == NULL)
170 internal_nis_setrpcent (data);
171
172 /* Get the next entry until we found a correct one. */
173 do
174 {
175 if (data->next == NULL)
176 return NSS_STATUS_NOTFOUND;
177
178 p = strncpy (buffer, data->next->val, buflen);
179 while (isspace (*p))
180 ++p;
181
182 parse_res = _nss_files_parse_rpcent (p, rpc, pdata, buflen, errnop);
183 if (parse_res == -1)
184 return NSS_STATUS_TRYAGAIN;
185 data->next = data->next->next;
186 }
187 while (!parse_res);
188
189 return NSS_STATUS_SUCCESS;
190 }
191
192 enum nss_status
193 _nss_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
194 int *errnop)
195 {
196 enum nss_status status;
197
198 __libc_lock_lock (lock);
199
200 status = internal_nis_getrpcent_r (rpc, buffer, buflen, errnop, &intern);
201
202 __libc_lock_unlock (lock);
203
204 return status;
205 }
206
207 enum nss_status
208 _nss_nis_getrpcbyname_r (const char *name, struct rpcent *rpc,
209 char *buffer, size_t buflen, int *errnop)
210 {
211 intern_t data = {NULL, NULL};
212 enum nss_status status;
213 int found;
214
215 if (name == NULL)
216 {
217 *errnop = EINVAL;
218 return NSS_STATUS_UNAVAIL;
219 }
220
221 status = internal_nis_setrpcent (&data);
222 if (status != NSS_STATUS_SUCCESS)
223 return status;
224
225 found = 0;
226 while (!found &&
227 ((status = internal_nis_getrpcent_r (rpc, buffer, buflen, errnop,
228 &data)) == NSS_STATUS_SUCCESS))
229 {
230 if (strcmp (rpc->r_name, name) == 0)
231 found = 1;
232 else
233 {
234 int i = 0;
235
236 while (rpc->r_aliases[i] != NULL)
237 {
238 if (strcmp (rpc->r_aliases[i], name) == 0)
239 {
240 found = 1;
241 break;
242 }
243 else
244 ++i;
245 }
246 }
247 }
248
249 internal_nis_endrpcent (&data);
250
251 if (!found && status == NSS_STATUS_SUCCESS)
252 return NSS_STATUS_NOTFOUND;
253 else
254 return status;
255 }
256
257 enum nss_status
258 _nss_nis_getrpcbynumber_r (int number, struct rpcent *rpc,
259 char *buffer, size_t buflen, int *errnop)
260 {
261 struct parser_data *data = (void *) buffer;
262 enum nss_status retval;
263 char *domain, *result, *p;
264 int len, nlen, parse_res;
265 char buf[32];
266
267 if (yp_get_default_domain (&domain))
268 return NSS_STATUS_UNAVAIL;
269
270 nlen = sprintf (buf, "%d", number);
271
272 retval = yperr2nss (yp_match (domain, "rpc.bynumber", buf,
273 nlen, &result, &len));
274
275 if (retval != NSS_STATUS_SUCCESS)
276 {
277 if (retval == NSS_STATUS_TRYAGAIN)
278 *errnop = errno;
279 return retval;
280 }
281
282 if ((size_t) (len + 1) > buflen)
283 {
284 free (result);
285 *errnop = ERANGE;
286 return NSS_STATUS_TRYAGAIN;
287 }
288
289 p = strncpy (buffer, result, len);
290 buffer[len] = '\0';
291 while (isspace (*p))
292 ++p;
293 free (result);
294
295 parse_res = _nss_files_parse_rpcent (p, rpc, data, buflen, errnop);
296
297 if (parse_res < 1)
298 {
299 if (parse_res == -1)
300 return NSS_STATUS_TRYAGAIN;
301 else
302 return NSS_STATUS_NOTFOUND;
303 }
304 else
305 return NSS_STATUS_SUCCESS;
306 }