Update.
[glibc.git] / nis / nss_nis / nis-service.c
blobdfae9f7be2ebf7c073a285730b28f62cda8aa374
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.
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.
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.
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. */
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"
32 /* The parser is defined in a different module. */
33 extern int _nss_files_parse_servent (char *line, struct servent *result,
34 char *data, size_t datalen, int *errnop);
37 __libc_lock_define_initialized (static, lock)
39 struct response_t
41 char *val;
42 struct response_t *next;
45 struct intern_t
47 struct response_t *start;
48 struct response_t *next;
50 typedef struct intern_t intern_t;
52 static intern_t intern = { NULL, NULL };
54 static int
55 saveit (int instatus, char *inkey, int inkeylen, char *inval,
56 int invallen, char *indata)
58 intern_t *intern = (intern_t *) indata;
60 if (instatus != YP_TRUE)
61 return instatus;
63 if (inkey && inkeylen > 0 && inval && invallen > 0)
65 if (intern->start == NULL)
67 intern->start = malloc (sizeof (struct response_t));
68 intern->next = intern->start;
70 else
72 intern->next->next = malloc (sizeof (struct response_t));
73 intern->next = intern->next->next;
75 intern->next->next = NULL;
76 intern->next->val = malloc (invallen + 1);
77 strncpy (intern->next->val, inval, invallen);
78 intern->next->val[invallen] = '\0';
81 return 0;
84 static enum nss_status
85 internal_nis_setservent (intern_t *intern)
87 char *domainname;
88 struct ypall_callback ypcb;
89 enum nss_status status;
91 if (yp_get_default_domain (&domainname))
92 return NSS_STATUS_UNAVAIL;
94 while (intern->start != NULL)
96 if (intern->start->val != NULL)
97 free (intern->start->val);
98 intern->next = intern->start;
99 intern->start = intern->start->next;
100 free (intern->next);
102 intern->start = NULL;
104 ypcb.foreach = saveit;
105 ypcb.data = (char *) intern;
106 status = yperr2nss (yp_all (domainname, "services.byname", &ypcb));
107 intern->next = intern->start;
109 return status;
111 enum nss_status
112 _nss_nis_setservent (void)
114 enum nss_status status;
116 __libc_lock_lock (lock);
118 status = internal_nis_setservent (&intern);
120 __libc_lock_unlock (lock);
122 return status;
125 static enum nss_status
126 internal_nis_endservent (intern_t * intern)
128 while (intern->start != NULL)
130 if (intern->start->val != NULL)
131 free (intern->start->val);
132 intern->next = intern->start;
133 intern->start = intern->start->next;
134 free (intern->next);
136 intern->start = NULL;
138 return NSS_STATUS_SUCCESS;
141 enum nss_status
142 _nss_nis_endservent (void)
144 enum nss_status status;
146 __libc_lock_lock (lock);
148 status = internal_nis_endservent (&intern);
150 __libc_lock_unlock (lock);
152 return status;
155 static enum nss_status
156 internal_nis_getservent_r (struct servent *serv, char *buffer,
157 size_t buflen, int *errnop, intern_t *data)
159 int parse_res;
160 char *p;
162 if (data->start == NULL)
163 internal_nis_setservent (data);
165 /* Get the next entry until we found a correct one. */
168 if (data->next == NULL)
169 return NSS_STATUS_NOTFOUND;
170 p = strncpy (buffer, data->next->val, buflen);
171 while (isspace (*p))
172 ++p;
174 parse_res = _nss_files_parse_servent (p, serv, buffer, buflen, errnop);
175 if (parse_res == -1)
176 return NSS_STATUS_TRYAGAIN;
177 data->next = data->next->next;
179 while (!parse_res);
181 return NSS_STATUS_SUCCESS;
184 enum nss_status
185 _nss_nis_getservent_r (struct servent *serv, char *buffer, size_t buflen,
186 int *errnop)
188 enum nss_status status;
190 __libc_lock_lock (lock);
192 status = internal_nis_getservent_r (serv, buffer, buflen, errnop, &intern);
194 __libc_lock_unlock (lock);
196 return status;
199 enum nss_status
200 _nss_nis_getservbyname_r (const char *name, char *protocol,
201 struct servent *serv, char *buffer, size_t buflen,
202 int *errnop)
204 intern_t data = { NULL, NULL };
205 enum nss_status status;
206 int found;
208 if (name == NULL)
210 *errnop = EINVAL;
211 return NSS_STATUS_UNAVAIL;
214 status = internal_nis_setservent (&data);
215 if (status != NSS_STATUS_SUCCESS)
216 return status;
218 found = 0;
219 while (!found &&
220 ((status = internal_nis_getservent_r (serv, buffer, buflen, errnop,
221 &data)) == NSS_STATUS_SUCCESS))
223 if (protocol == NULL || strcmp (serv->s_proto, protocol) == 0)
225 char **cp;
227 if (strcmp (serv->s_name, name) == 0)
228 found = 1;
229 else
230 for (cp = serv->s_aliases; *cp; cp++)
231 if (strcmp (name, *cp) == 0)
232 found = 1;
236 internal_nis_endservent (&data);
238 if (!found && status == NSS_STATUS_SUCCESS)
239 return NSS_STATUS_NOTFOUND;
240 else
241 return status;
244 enum nss_status
245 _nss_nis_getservbyport_r (int port, char *protocol, struct servent *serv,
246 char *buffer, size_t buflen, int *errnop)
248 intern_t data = { NULL, NULL };
249 enum nss_status status;
250 int found;
252 if (protocol == NULL)
254 *errnop = EINVAL;
255 return NSS_STATUS_UNAVAIL;
258 status = internal_nis_setservent (&data);
259 if (status != NSS_STATUS_SUCCESS)
260 return status;
262 found = 0;
263 while (!found &&
264 ((status = internal_nis_getservent_r (serv, buffer, buflen, errnop,
265 &data)) == NSS_STATUS_SUCCESS))
266 if (htons (serv->s_port) == port
267 && strcmp (serv->s_proto, protocol) == 0)
268 found = 1;
270 internal_nis_endservent (&data);
272 if (!found && status == NSS_STATUS_SUCCESS)
273 return NSS_STATUS_NOTFOUND;
274 else
275 return status;