]> sourceware.org Git - glibc.git/blame - nis/nss_nis/nis-proto.c
Update.
[glibc.git] / nis / nss_nis / nis-proto.c
CommitLineData
0c6cee5d 1/* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
6259ec0d
UD
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>
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
44static struct response *start = NULL;
45static struct response *next = NULL;
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));
59 next = start;
60 }
61 else
62 {
63 next->next = malloc (sizeof (struct response));
64 next = next->next;
65 }
66 next->next = NULL;
67 next->val = malloc (invallen + 1);
68 strncpy (next->val, inval, invallen);
69 next->val[invallen] = '\0';
70 }
cf29ffbe 71
f166d865
UD
72 return 0;
73}
74
900bec85 75static enum nss_status
f166d865
UD
76internal_nis_setprotoent (void)
77{
78 char *domainname;
79 struct ypall_callback ypcb;
0d8733c4 80 enum nss_status status;
cf29ffbe 81
f166d865 82 yp_get_default_domain (&domainname);
cf29ffbe 83
f166d865
UD
84 while (start != NULL)
85 {
86 if (start->val != NULL)
87 free (start->val);
88 next = start;
89 start = start->next;
90 free (next);
91 }
92 start = NULL;
cf29ffbe 93
f166d865
UD
94 ypcb.foreach = saveit;
95 ypcb.data = NULL;
0d8733c4 96 status = yperr2nss (yp_all (domainname, "protocols.bynumber", &ypcb));
f166d865 97 next = start;
cf29ffbe 98
0d8733c4 99 return status;
f166d865 100}
6259ec0d
UD
101
102enum nss_status
103_nss_nis_setprotoent (void)
104{
f166d865
UD
105 enum nss_status status;
106
6259ec0d
UD
107 __libc_lock_lock (lock);
108
f166d865 109 status = internal_nis_setprotoent ();
6259ec0d
UD
110
111 __libc_lock_unlock (lock);
112
f166d865 113 return status;
6259ec0d
UD
114}
115
116enum nss_status
117_nss_nis_endprotoent (void)
118{
119 __libc_lock_lock (lock);
120
f166d865 121 while (start != NULL)
6259ec0d 122 {
f166d865
UD
123 if (start->val != NULL)
124 free (start->val);
125 next = start;
126 start = start->next;
127 free (next);
6259ec0d 128 }
f166d865
UD
129 start = NULL;
130 next = NULL;
cf29ffbe 131
6259ec0d 132 __libc_lock_unlock (lock);
cf29ffbe 133
6259ec0d
UD
134 return NSS_STATUS_SUCCESS;
135}
136
137static enum nss_status
138internal_nis_getprotoent_r (struct protoent *proto,
d71b808a 139 char *buffer, size_t buflen, int *errnop)
6259ec0d 140{
7e3be507 141 struct parser_data *data = (void *) buffer;
f166d865 142 int parse_res;
6259ec0d 143
f166d865
UD
144 if (start == NULL)
145 internal_nis_setprotoent ();
6259ec0d
UD
146
147 /* Get the next entry until we found a correct one. */
148 do
149 {
6259ec0d 150 char *p;
cf29ffbe 151
f166d865 152 if (next == NULL)
0c6cee5d
UD
153 {
154 *errnop = ENOENT;
155 return NSS_STATUS_NOTFOUND;
156 }
9756dfe1 157 p = strncpy (buffer, next->val, buflen);
cf29ffbe 158
6259ec0d
UD
159 while (isspace (*p))
160 ++p;
6259ec0d 161
d71b808a
UD
162 parse_res = _nss_files_parse_protoent (p, proto, data, buflen, errnop);
163 if (parse_res == -1)
6259ec0d 164 return NSS_STATUS_TRYAGAIN;
60c96635 165 next = next->next;
6259ec0d
UD
166 }
167 while (!parse_res);
d71b808a 168
6259ec0d
UD
169 return NSS_STATUS_SUCCESS;
170}
171
172enum nss_status
d71b808a
UD
173_nss_nis_getprotoent_r (struct protoent *proto, char *buffer, size_t buflen,
174 int *errnop)
6259ec0d
UD
175{
176 enum nss_status status;
177
178 __libc_lock_lock (lock);
179
d71b808a 180 status = internal_nis_getprotoent_r (proto, buffer, buflen, errnop);
6259ec0d
UD
181
182 __libc_lock_unlock (lock);
183
184 return status;
185}
186
187enum nss_status
188_nss_nis_getprotobyname_r (const char *name, struct protoent *proto,
d71b808a 189 char *buffer, size_t buflen, int *errnop)
6259ec0d 190{
7e3be507 191 struct parser_data *data = (void *) buffer;
6259ec0d
UD
192 enum nss_status retval;
193 char *domain, *result, *p;
194 int len, parse_res;
195
196 if (name == NULL)
197 {
ac9f45cf 198 *errnop = EINVAL;
6259ec0d
UD
199 return NSS_STATUS_UNAVAIL;
200 }
201
202 if (yp_get_default_domain (&domain))
203 return NSS_STATUS_UNAVAIL;
204
205 retval = yperr2nss (yp_match (domain, "protocols.byname", name,
206 strlen (name), &result, &len));
207
208 if (retval != NSS_STATUS_SUCCESS)
209 {
0c6cee5d
UD
210 if (retval == NSS_STATUS_NOTFOUND)
211 *errnop = ENOENT;
212 else if (retval == NSS_STATUS_TRYAGAIN)
d71b808a 213 *errnop = errno;
6259ec0d
UD
214 return retval;
215 }
216
f8b87ef0 217 if ((size_t) (len + 1) > buflen)
6259ec0d
UD
218 {
219 free (result);
d71b808a 220 *errnop = ERANGE;
6259ec0d
UD
221 return NSS_STATUS_TRYAGAIN;
222 }
223
224 p = strncpy (buffer, result, len);
225 buffer[len] = '\0';
226 while (isspace (*p))
227 ++p;
228 free (result);
229
d71b808a 230 parse_res = _nss_files_parse_protoent (p, proto, data, buflen, errnop);
ac9f45cf
UD
231 if (parse_res < 1)
232 {
233 if (parse_res == -1)
234 return NSS_STATUS_TRYAGAIN;
235 else
0c6cee5d
UD
236 {
237 *errnop = ENOENT;
238 return NSS_STATUS_NOTFOUND;
239 }
ac9f45cf
UD
240 }
241 return NSS_STATUS_SUCCESS;
6259ec0d
UD
242}
243
244enum nss_status
245_nss_nis_getprotobynumber_r (int number, struct protoent *proto,
d71b808a 246 char *buffer, size_t buflen, int *errnop)
6259ec0d 247{
7e3be507 248 struct parser_data *data = (void *) buffer;
6259ec0d
UD
249 enum nss_status retval;
250 char *domain, *result, *p;
251 int len, nlen, parse_res;
252 char buf[32];
253
254 if (yp_get_default_domain (&domain))
255 return NSS_STATUS_UNAVAIL;
256
257 nlen = sprintf (buf, "%d", number);
258
259 retval = yperr2nss (yp_match (domain, "protocols.bynumber", buf,
260 nlen, &result, &len));
261
262 if (retval != NSS_STATUS_SUCCESS)
263 {
0c6cee5d
UD
264 if (retval == NSS_STATUS_NOTFOUND)
265 *errnop = ENOENT;
266 else if (retval == NSS_STATUS_TRYAGAIN)
d71b808a 267 *errnop = errno;
6259ec0d
UD
268 return retval;
269 }
270
f8b87ef0 271 if ((size_t) (len + 1) > buflen)
6259ec0d
UD
272 {
273 free (result);
d71b808a 274 *errnop = ERANGE;
6259ec0d
UD
275 return NSS_STATUS_TRYAGAIN;
276 }
277
278 p = strncpy (buffer, result, len);
279 buffer[len] = '\0';
280 while (isspace (*p))
281 ++p;
282 free (result);
283
d71b808a 284 parse_res = _nss_files_parse_protoent (p, proto, data, buflen, errnop);
ac9f45cf
UD
285 if (parse_res < 1)
286 {
287 if (parse_res == -1)
288 return NSS_STATUS_TRYAGAIN;
289 else
0c6cee5d
UD
290 {
291 *errnop = ENOENT;
292 return NSS_STATUS_NOTFOUND;
293 }
ac9f45cf
UD
294 }
295 return NSS_STATUS_SUCCESS;
6259ec0d 296}
This page took 0.077283 seconds and 5 git commands to generate.