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