(internal_nis_getservent_r): Use strncpy instead of strcpy for
[glibc/history.git] / nis / nss_nis / nis-service.c
blob1100c1d5d0dede53844dae4109be1d72671c5e7c
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 <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);
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, 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 data->next = data->next->next;
173 while (isspace (*p))
174 ++p;
176 parse_res = _nss_files_parse_servent (p, serv, buffer, buflen);
177 if (!parse_res && errno == ERANGE)
178 return NSS_STATUS_TRYAGAIN;
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)
188 enum nss_status status;
190 __libc_lock_lock (lock);
192 status = internal_nis_getservent_r (serv, buffer, buflen, &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)
203 intern_t data = { NULL, NULL };
204 enum nss_status status;
205 int found;
207 if (name == NULL)
209 __set_errno (EINVAL);
210 return NSS_STATUS_UNAVAIL;
213 status = internal_nis_setservent (&data);
214 if (status != NSS_STATUS_SUCCESS)
215 return status;
217 found = 0;
218 while (!found &&
219 ((status = internal_nis_getservent_r (serv, buffer, buflen, &data))
220 == NSS_STATUS_SUCCESS))
222 if (protocol == NULL || strcmp (serv->s_proto, protocol) == 0)
224 char **cp;
226 if (strcmp (serv->s_name, name) == 0)
227 found = 1;
228 else
229 for (cp = serv->s_aliases; *cp; cp++)
230 if (strcmp (name, *cp) == 0)
231 found = 1;
235 internal_nis_endservent (&data);
237 if (!found && status == NSS_STATUS_SUCCESS)
238 return NSS_STATUS_NOTFOUND;
239 else
240 return status;
243 enum nss_status
244 _nss_nis_getservbyport_r (int port, char *protocol, struct servent *serv,
245 char *buffer, size_t buflen)
247 intern_t data = { NULL, NULL };
248 enum nss_status status;
249 int found;
251 if (protocol == NULL)
253 __set_errno (EINVAL);
254 return NSS_STATUS_UNAVAIL;
257 status = internal_nis_setservent (&data);
258 if (status != NSS_STATUS_SUCCESS)
259 return status;
261 found = 0;
262 while (!found &&
263 ((status = internal_nis_getservent_r (serv, buffer, buflen, &data))
264 == NSS_STATUS_SUCCESS))
266 if (htons (serv->s_port) == port)
268 if (strcmp (serv->s_proto, protocol) == 0)
270 found = 1;
275 internal_nis_endservent (&data);
277 if (!found && status == NSS_STATUS_SUCCESS)
278 return NSS_STATUS_NOTFOUND;
279 else
280 return status;