Update.
[glibc.git] / nis / nss_nis / nis-service.c
blob0aa35cc34b7dceb32324bdfeb78cceeff2bb2068
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);
38 __libc_lock_define_initialized (static, lock)
40 struct response_t
42 char *val;
43 struct response_t *next;
46 struct intern_t
48 struct response_t *start;
49 struct response_t *next;
51 typedef struct intern_t intern_t;
53 static intern_t intern = { NULL, NULL };
55 static int
56 saveit (int instatus, char *inkey, int inkeylen, char *inval,
57 int invallen, char *indata)
59 intern_t *intern = (intern_t *) indata;
61 if (instatus != YP_TRUE)
62 return instatus;
64 if (inkey && inkeylen > 0 && inval && invallen > 0)
66 if (intern->start == NULL)
68 intern->start = malloc (sizeof (struct response_t));
69 intern->next = intern->start;
71 else
73 intern->next->next = malloc (sizeof (struct response_t));
74 intern->next = intern->next->next;
76 intern->next->next = NULL;
77 intern->next->val = malloc (invallen + 1);
78 strncpy (intern->next->val, inval, invallen);
79 intern->next->val[invallen] = '\0';
82 return 0;
85 static enum nss_status
86 internal_nis_setservent (intern_t *intern)
88 char *domainname;
89 struct ypall_callback ypcb;
90 enum nss_status status;
92 if (yp_get_default_domain (&domainname))
93 return NSS_STATUS_UNAVAIL;
95 while (intern->start != NULL)
97 if (intern->start->val != NULL)
98 free (intern->start->val);
99 intern->next = intern->start;
100 intern->start = intern->start->next;
101 free (intern->next);
103 intern->start = NULL;
105 ypcb.foreach = saveit;
106 ypcb.data = (char *) intern;
107 status = yperr2nss (yp_all (domainname, "services.byname", &ypcb));
108 intern->next = intern->start;
110 return status;
112 enum nss_status
113 _nss_nis_setservent (void)
115 enum nss_status status;
117 __libc_lock_lock (lock);
119 status = internal_nis_setservent (&intern);
121 __libc_lock_unlock (lock);
123 return status;
126 static enum nss_status
127 internal_nis_endservent (intern_t * intern)
129 while (intern->start != NULL)
131 if (intern->start->val != NULL)
132 free (intern->start->val);
133 intern->next = intern->start;
134 intern->start = intern->start->next;
135 free (intern->next);
137 intern->start = NULL;
139 return NSS_STATUS_SUCCESS;
142 enum nss_status
143 _nss_nis_endservent (void)
145 enum nss_status status;
147 __libc_lock_lock (lock);
149 status = internal_nis_endservent (&intern);
151 __libc_lock_unlock (lock);
153 return status;
156 static enum nss_status
157 internal_nis_getservent_r (struct servent *serv, char *buffer,
158 size_t buflen, int *errnop, intern_t *data)
160 int parse_res;
161 char *p;
163 if (data->start == NULL)
164 internal_nis_setservent (data);
166 /* Get the next entry until we found a correct one. */
169 if (data->next == NULL)
170 return NSS_STATUS_NOTFOUND;
171 p = strncpy (buffer, data->next->val, buflen);
172 while (isspace (*p))
173 ++p;
175 parse_res = _nss_files_parse_servent (p, serv, buffer, buflen, errnop);
176 if (parse_res == -1)
177 return NSS_STATUS_TRYAGAIN;
178 data->next = data->next->next;
180 while (!parse_res);
182 return NSS_STATUS_SUCCESS;
185 enum nss_status
186 _nss_nis_getservent_r (struct servent *serv, char *buffer, size_t buflen,
187 int *errnop)
189 enum nss_status status;
191 __libc_lock_lock (lock);
193 status = internal_nis_getservent_r (serv, buffer, buflen, errnop, &intern);
195 __libc_lock_unlock (lock);
197 return status;
200 enum nss_status
201 _nss_nis_getservbyname_r (const char *name, char *protocol,
202 struct servent *serv, char *buffer, size_t buflen,
203 int *errnop)
205 intern_t data = { NULL, NULL };
206 enum nss_status status;
207 int found;
209 if (name == NULL)
211 __set_errno (EINVAL);
212 return NSS_STATUS_UNAVAIL;
215 status = internal_nis_setservent (&data);
216 if (status != NSS_STATUS_SUCCESS)
217 return status;
219 found = 0;
220 while (!found &&
221 ((status = internal_nis_getservent_r (serv, buffer, buflen, errnop,
222 &data)) == NSS_STATUS_SUCCESS))
224 if (protocol == NULL || strcmp (serv->s_proto, protocol) == 0)
226 char **cp;
228 if (strcmp (serv->s_name, name) == 0)
229 found = 1;
230 else
231 for (cp = serv->s_aliases; *cp; cp++)
232 if (strcmp (name, *cp) == 0)
233 found = 1;
237 internal_nis_endservent (&data);
239 if (!found && status == NSS_STATUS_SUCCESS)
240 return NSS_STATUS_NOTFOUND;
241 else
242 return status;
245 enum nss_status
246 _nss_nis_getservbyport_r (int port, char *protocol, struct servent *serv,
247 char *buffer, size_t buflen, int *errnop)
249 intern_t data = { NULL, NULL };
250 enum nss_status status;
251 int found;
253 if (protocol == NULL)
255 __set_errno (EINVAL);
256 return NSS_STATUS_UNAVAIL;
259 status = internal_nis_setservent (&data);
260 if (status != NSS_STATUS_SUCCESS)
261 return status;
263 found = 0;
264 while (!found &&
265 ((status = internal_nis_getservent_r (serv, buffer, buflen, errnop,
266 &data)) == NSS_STATUS_SUCCESS))
267 if (htons (serv->s_port) == port
268 && strcmp (serv->s_proto, protocol) == 0)
269 found = 1;
271 internal_nis_endservent (&data);
273 if (!found && status == NSS_STATUS_SUCCESS)
274 return NSS_STATUS_NOTFOUND;
275 else
276 return status;