]> 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 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.
4
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.
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 Library General Public License for more details.
14
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. */
19
20 #include <nss.h>
21 #include <netdb.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <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 intern->next = intern->start;
68 }
69 else
70 {
71 intern->next->next = malloc (sizeof (struct response_t));
72 intern->next = intern->next->next;
73 }
74 intern->next->next = NULL;
75 intern->next->val = malloc (invallen + 1);
76 strncpy (intern->next->val, inval, invallen);
77 intern->next->val[invallen] = '\0';
78 }
79
80 return 0;
81 }
82
83 static enum nss_status
84 internal_nis_setrpcent (intern_t *intern)
85 {
86 char *domainname;
87 struct ypall_callback ypcb;
88
89 if (yp_get_default_domain (&domainname))
90 return NSS_STATUS_UNAVAIL;
91
92 while (intern->start != NULL)
93 {
94 if (intern->start->val != NULL)
95 free (intern->start->val);
96 intern->next = intern->start;
97 intern->start = intern->start->next;
98 free (intern->next);
99 }
100 intern->start = NULL;
101
102 ypcb.foreach = saveit;
103 ypcb.data = (char *)intern;
104 yp_all(domainname, "rpc.bynumber", &ypcb);
105 intern->next = intern->start;
106
107 return NSS_STATUS_SUCCESS;
108 }
109
110 enum nss_status
111 _nss_nis_setrpcent (void)
112 {
113 enum nss_status status;
114
115 __libc_lock_lock (lock);
116
117 status = internal_nis_setrpcent (&intern);
118
119 __libc_lock_unlock (lock);
120
121 return status;
122 }
123
124 static enum nss_status
125 internal_nis_endrpcent (intern_t *intern)
126 {
127 while (intern->start != NULL)
128 {
129 if (intern->start->val != NULL)
130 free (intern->start->val);
131 intern->next = intern->start;
132 intern->start = intern->start->next;
133 free (intern->next);
134 }
135 intern->start = NULL;
136
137 return NSS_STATUS_SUCCESS;
138 }
139
140 enum nss_status
141 _nss_nis_endrpcent (void)
142 {
143 enum nss_status status;
144
145 __libc_lock_lock (lock);
146
147 status = internal_nis_endrpcent (&intern);
148
149 __libc_lock_unlock (lock);
150
151 return status;
152 }
153
154 static enum nss_status
155 internal_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
156 intern_t *data)
157 {
158 struct parser_data *pdata = (void *) buffer;
159 int parse_res;
160 char *p;
161
162 if (data->start == NULL)
163 internal_nis_setrpcent (data);
164
165 /* Get the next entry until we found a correct one. */
166 do
167 {
168 if (data->next == NULL)
169 return NSS_STATUS_NOTFOUND;
170 p = strcpy (buffer, data->next->val);
171 data->next = data->next->next;
172 while (isspace (*p))
173 ++p;
174
175 parse_res = _nss_files_parse_rpcent (p, rpc, pdata, buflen);
176 if (!parse_res && errno == ERANGE)
177 return NSS_STATUS_TRYAGAIN;
178 }
179 while (!parse_res);
180
181 return NSS_STATUS_SUCCESS;
182 }
183
184 enum nss_status
185 _nss_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen)
186 {
187 enum nss_status status;
188
189 __libc_lock_lock (lock);
190
191 status = internal_nis_getrpcent_r (rpc, buffer, buflen, &intern);
192
193 __libc_lock_unlock (lock);
194
195 return status;
196 }
197
198 enum nss_status
199 _nss_nis_getrpcbyname_r (const char *name, struct rpcent *rpc,
200 char *buffer, size_t buflen)
201 {
202 intern_t data = {NULL, NULL};
203 enum nss_status status;
204 int found;
205
206 if (name == NULL)
207 {
208 __set_errno (EINVAL);
209 return NSS_STATUS_UNAVAIL;
210 }
211
212 status = internal_nis_setrpcent (&data);
213 if (status != NSS_STATUS_SUCCESS)
214 return status;
215
216 found = 0;
217 while (!found &&
218 ((status = internal_nis_getrpcent_r (rpc, buffer, buflen, &data))
219 == NSS_STATUS_SUCCESS))
220 {
221 if (strcmp (rpc->r_name, name) == 0)
222 found = 1;
223 else
224 {
225 int i = 0;
226
227 while (rpc->r_aliases[i] != NULL)
228 {
229 if (strcmp (rpc->r_aliases[i], name) == 0)
230 {
231 found = 1;
232 break;
233 }
234 else
235 ++i;
236 }
237 }
238 }
239
240 internal_nis_endrpcent (&data);
241
242 if (!found && status == NSS_STATUS_SUCCESS)
243 return NSS_STATUS_NOTFOUND;
244 else
245 return status;
246 }
247
248 enum nss_status
249 _nss_nis_getrpcbynumber_r (int number, struct rpcent *rpc,
250 char *buffer, size_t buflen)
251 {
252 struct parser_data *data = (void *) buffer;
253 enum nss_status retval;
254 char *domain, *result, *p;
255 int len, nlen, parse_res;
256 char buf[32];
257
258 if (yp_get_default_domain (&domain))
259 return NSS_STATUS_UNAVAIL;
260
261 nlen = sprintf (buf, "%d", number);
262
263 retval = yperr2nss (yp_match (domain, "rpc.bynumber", buf,
264 nlen, &result, &len));
265
266 if (retval != NSS_STATUS_SUCCESS)
267 {
268 if (retval == NSS_STATUS_TRYAGAIN)
269 __set_errno (EAGAIN);
270 return retval;
271 }
272
273 if ((size_t) (len + 1) > buflen)
274 {
275 free (result);
276 __set_errno (ERANGE);
277 return NSS_STATUS_TRYAGAIN;
278 }
279
280 p = strncpy (buffer, result, len);
281 buffer[len] = '\0';
282 while (isspace (*p))
283 ++p;
284 free (result);
285
286 parse_res = _nss_files_parse_rpcent (p, rpc, data, buflen);
287
288 if (!parse_res)
289 {
290 if (errno == ERANGE)
291 return NSS_STATUS_TRYAGAIN;
292 else
293 return NSS_STATUS_NOTFOUND;
294 }
295 else
296 return NSS_STATUS_SUCCESS;
297 }