]>
Commit | Line | Data |
---|---|---|
f166d865 | 1 | /* Copyright (C) 1996, 1997 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 | ||
f8b87ef0 UD |
31 | |
32 | /* The parser is defined in a different module. */ | |
33 | extern int _nss_files_parse_servent (char *line, struct servent *result, | |
d71b808a | 34 | char *data, size_t datalen, int *errnop); |
f8b87ef0 UD |
35 | |
36 | ||
37 | ||
6259ec0d UD |
38 | __libc_lock_define_initialized (static, lock) |
39 | ||
f166d865 UD |
40 | struct response_t |
41 | { | |
42 | char *val; | |
43 | struct response_t *next; | |
44 | }; | |
45 | ||
6259ec0d UD |
46 | struct intern_t |
47 | { | |
f166d865 UD |
48 | struct response_t *start; |
49 | struct response_t *next; | |
6259ec0d UD |
50 | }; |
51 | typedef struct intern_t intern_t; | |
52 | ||
0d8733c4 | 53 | static intern_t intern = { NULL, NULL }; |
f166d865 UD |
54 | |
55 | static int | |
0d8733c4 | 56 | saveit (int instatus, char *inkey, int inkeylen, char *inval, |
f166d865 UD |
57 | int invallen, char *indata) |
58 | { | |
0d8733c4 | 59 | intern_t *intern = (intern_t *) indata; |
f166d865 UD |
60 | |
61 | if (instatus != YP_TRUE) | |
62 | return instatus; | |
63 | ||
64 | if (inkey && inkeylen > 0 && inval && invallen > 0) | |
65 | { | |
66 | if (intern->start == NULL) | |
67 | { | |
68 | intern->start = malloc (sizeof (struct response_t)); | |
69 | intern->next = intern->start; | |
70 | } | |
71 | else | |
72 | { | |
73 | intern->next->next = malloc (sizeof (struct response_t)); | |
74 | intern->next = intern->next->next; | |
75 | } | |
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'; | |
80 | } | |
0d8733c4 | 81 | |
f166d865 UD |
82 | return 0; |
83 | } | |
6259ec0d UD |
84 | |
85 | static enum nss_status | |
f166d865 | 86 | internal_nis_setservent (intern_t *intern) |
6259ec0d | 87 | { |
f166d865 UD |
88 | char *domainname; |
89 | struct ypall_callback ypcb; | |
0d8733c4 UD |
90 | enum nss_status status; |
91 | ||
f166d865 UD |
92 | if (yp_get_default_domain (&domainname)) |
93 | return NSS_STATUS_UNAVAIL; | |
0d8733c4 | 94 | |
f166d865 | 95 | while (intern->start != NULL) |
6259ec0d | 96 | { |
f166d865 UD |
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); | |
6259ec0d | 102 | } |
f166d865 UD |
103 | intern->start = NULL; |
104 | ||
105 | ypcb.foreach = saveit; | |
0d8733c4 UD |
106 | ypcb.data = (char *) intern; |
107 | status = yperr2nss (yp_all (domainname, "services.byname", &ypcb)); | |
f166d865 UD |
108 | intern->next = intern->start; |
109 | ||
0d8733c4 | 110 | return status; |
6259ec0d UD |
111 | } |
112 | enum nss_status | |
113 | _nss_nis_setservent (void) | |
114 | { | |
115 | enum nss_status status; | |
116 | ||
117 | __libc_lock_lock (lock); | |
118 | ||
119 | status = internal_nis_setservent (&intern); | |
120 | ||
121 | __libc_lock_unlock (lock); | |
122 | ||
123 | return status; | |
124 | } | |
125 | ||
126 | static enum nss_status | |
127 | internal_nis_endservent (intern_t * intern) | |
128 | { | |
f166d865 | 129 | while (intern->start != NULL) |
6259ec0d | 130 | { |
f166d865 UD |
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); | |
6259ec0d | 136 | } |
f166d865 | 137 | intern->start = NULL; |
0d8733c4 | 138 | |
6259ec0d UD |
139 | return NSS_STATUS_SUCCESS; |
140 | } | |
141 | ||
142 | enum nss_status | |
143 | _nss_nis_endservent (void) | |
144 | { | |
145 | enum nss_status status; | |
146 | ||
147 | __libc_lock_lock (lock); | |
148 | ||
149 | status = internal_nis_endservent (&intern); | |
150 | ||
151 | __libc_lock_unlock (lock); | |
152 | ||
153 | return status; | |
154 | } | |
155 | ||
156 | static enum nss_status | |
157 | internal_nis_getservent_r (struct servent *serv, char *buffer, | |
d71b808a | 158 | size_t buflen, int *errnop, intern_t *data) |
6259ec0d | 159 | { |
f166d865 | 160 | int parse_res; |
6259ec0d UD |
161 | char *p; |
162 | ||
f166d865 UD |
163 | if (data->start == NULL) |
164 | internal_nis_setservent (data); | |
0d8733c4 | 165 | |
6259ec0d UD |
166 | /* Get the next entry until we found a correct one. */ |
167 | do | |
168 | { | |
f166d865 UD |
169 | if (data->next == NULL) |
170 | return NSS_STATUS_NOTFOUND; | |
9756dfe1 | 171 | p = strncpy (buffer, data->next->val, buflen); |
60c96635 | 172 | while (isspace (*p)) |
6259ec0d | 173 | ++p; |
0d8733c4 | 174 | |
d71b808a UD |
175 | parse_res = _nss_files_parse_servent (p, serv, buffer, buflen, errnop); |
176 | if (parse_res == -1) | |
6259ec0d | 177 | return NSS_STATUS_TRYAGAIN; |
60c96635 | 178 | data->next = data->next->next; |
6259ec0d UD |
179 | } |
180 | while (!parse_res); | |
181 | ||
182 | return NSS_STATUS_SUCCESS; | |
183 | } | |
184 | ||
185 | enum nss_status | |
d71b808a UD |
186 | _nss_nis_getservent_r (struct servent *serv, char *buffer, size_t buflen, |
187 | int *errnop) | |
6259ec0d UD |
188 | { |
189 | enum nss_status status; | |
190 | ||
191 | __libc_lock_lock (lock); | |
192 | ||
d71b808a | 193 | status = internal_nis_getservent_r (serv, buffer, buflen, errnop, &intern); |
6259ec0d UD |
194 | |
195 | __libc_lock_unlock (lock); | |
196 | ||
197 | return status; | |
198 | } | |
199 | ||
200 | enum nss_status | |
201 | _nss_nis_getservbyname_r (const char *name, char *protocol, | |
d71b808a UD |
202 | struct servent *serv, char *buffer, size_t buflen, |
203 | int *errnop) | |
6259ec0d | 204 | { |
0d8733c4 | 205 | intern_t data = { NULL, NULL }; |
6259ec0d UD |
206 | enum nss_status status; |
207 | int found; | |
208 | ||
e61abf83 | 209 | if (name == NULL) |
6259ec0d UD |
210 | { |
211 | __set_errno (EINVAL); | |
212 | return NSS_STATUS_UNAVAIL; | |
213 | } | |
214 | ||
215 | status = internal_nis_setservent (&data); | |
216 | if (status != NSS_STATUS_SUCCESS) | |
217 | return status; | |
218 | ||
219 | found = 0; | |
220 | while (!found && | |
d71b808a UD |
221 | ((status = internal_nis_getservent_r (serv, buffer, buflen, errnop, |
222 | &data)) == NSS_STATUS_SUCCESS)) | |
6259ec0d | 223 | { |
e61abf83 | 224 | if (protocol == NULL || strcmp (serv->s_proto, protocol) == 0) |
f166d865 UD |
225 | { |
226 | char **cp; | |
0d8733c4 | 227 | |
f166d865 UD |
228 | if (strcmp (serv->s_name, name) == 0) |
229 | found = 1; | |
230 | else | |
231 | for (cp = serv->s_aliases; *cp; cp++) | |
0d8733c4 | 232 | if (strcmp (name, *cp) == 0) |
f166d865 UD |
233 | found = 1; |
234 | } | |
6259ec0d | 235 | } |
0d8733c4 | 236 | |
6259ec0d UD |
237 | internal_nis_endservent (&data); |
238 | ||
239 | if (!found && status == NSS_STATUS_SUCCESS) | |
240 | return NSS_STATUS_NOTFOUND; | |
241 | else | |
242 | return status; | |
243 | } | |
244 | ||
245 | enum nss_status | |
246 | _nss_nis_getservbyport_r (int port, char *protocol, struct servent *serv, | |
d71b808a | 247 | char *buffer, size_t buflen, int *errnop) |
6259ec0d | 248 | { |
0d8733c4 | 249 | intern_t data = { NULL, NULL }; |
6259ec0d UD |
250 | enum nss_status status; |
251 | int found; | |
252 | ||
253 | if (protocol == NULL) | |
254 | { | |
255 | __set_errno (EINVAL); | |
256 | return NSS_STATUS_UNAVAIL; | |
257 | } | |
258 | ||
259 | status = internal_nis_setservent (&data); | |
260 | if (status != NSS_STATUS_SUCCESS) | |
261 | return status; | |
262 | ||
263 | found = 0; | |
264 | while (!found && | |
d71b808a UD |
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; | |
6259ec0d UD |
270 | |
271 | internal_nis_endservent (&data); | |
272 | ||
273 | if (!found && status == NSS_STATUS_SUCCESS) | |
274 | return NSS_STATUS_NOTFOUND; | |
275 | else | |
276 | return status; | |
277 | } |