gconv conversion module for DEC-MCS.
[glibc/history.git] / nis / nss_nis / nis-service.c
blobfac62a9679a8c893d4f7426e4b68da52b4dd437c
1 /* Copyright (C) 1996, 1997, 1998 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)
170 *errnop = ENOENT;
171 return NSS_STATUS_NOTFOUND;
173 p = strncpy (buffer, data->next->val, buflen);
174 while (isspace (*p))
175 ++p;
177 parse_res = _nss_files_parse_servent (p, serv, buffer, buflen, errnop);
178 if (parse_res == -1)
179 return NSS_STATUS_TRYAGAIN;
180 data->next = data->next->next;
182 while (!parse_res);
184 return NSS_STATUS_SUCCESS;
187 enum nss_status
188 _nss_nis_getservent_r (struct servent *serv, char *buffer, size_t buflen,
189 int *errnop)
191 enum nss_status status;
193 __libc_lock_lock (lock);
195 status = internal_nis_getservent_r (serv, buffer, buflen, errnop, &intern);
197 __libc_lock_unlock (lock);
199 return status;
202 enum nss_status
203 _nss_nis_getservbyname_r (const char *name, char *protocol,
204 struct servent *serv, char *buffer, size_t buflen,
205 int *errnop)
207 intern_t data = { NULL, NULL };
208 enum nss_status status;
209 int found;
211 if (name == NULL)
213 *errnop = EINVAL;
214 return NSS_STATUS_UNAVAIL;
217 status = internal_nis_setservent (&data);
218 if (status != NSS_STATUS_SUCCESS)
219 return status;
221 found = 0;
222 while (!found &&
223 ((status = internal_nis_getservent_r (serv, buffer, buflen, errnop,
224 &data)) == NSS_STATUS_SUCCESS))
226 if (protocol == NULL || strcmp (serv->s_proto, protocol) == 0)
228 char **cp;
230 if (strcmp (serv->s_name, name) == 0)
231 found = 1;
232 else
233 for (cp = serv->s_aliases; *cp; cp++)
234 if (strcmp (name, *cp) == 0)
235 found = 1;
239 internal_nis_endservent (&data);
241 if (!found && status == NSS_STATUS_SUCCESS)
243 *errnop = ENOENT;
244 return NSS_STATUS_NOTFOUND;
246 else
247 return status;
250 enum nss_status
251 _nss_nis_getservbyport_r (int port, char *protocol, struct servent *serv,
252 char *buffer, size_t buflen, int *errnop)
254 intern_t data = { NULL, NULL };
255 enum nss_status status;
256 int found;
258 if (protocol == NULL)
260 *errnop = EINVAL;
261 return NSS_STATUS_UNAVAIL;
264 status = internal_nis_setservent (&data);
265 if (status != NSS_STATUS_SUCCESS)
266 return status;
268 found = 0;
269 while (!found &&
270 ((status = internal_nis_getservent_r (serv, buffer, buflen, errnop,
271 &data)) == NSS_STATUS_SUCCESS))
272 if (htons (serv->s_port) == port
273 && strcmp (serv->s_proto, protocol) == 0)
274 found = 1;
276 internal_nis_endservent (&data);
278 if (!found && status == NSS_STATUS_SUCCESS)
280 *errnop = ENOENT;
281 return NSS_STATUS_NOTFOUND;
283 else
284 return status;