]>
Commit | Line | Data |
---|---|---|
26a60f90 | 1 | /* Copyright (C) 1997, 1998 Free Software Foundation, Inc. |
650425ce UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. | |
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 <errno.h> | |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <unistd.h> | |
4360eafd | 24 | #include <libintl.h> |
cc3fa755 | 25 | #include <rpc/rpc.h> |
650425ce UD |
26 | #include <rpc/pmap_clnt.h> |
27 | #include <string.h> | |
28 | #include <memory.h> | |
29 | #include <syslog.h> | |
30 | #include <sys/socket.h> | |
31 | #include <netinet/in.h> | |
32 | #include <arpa/inet.h> | |
26a60f90 | 33 | #include <rpc/key_prot.h> |
650425ce | 34 | #include <rpcsvc/nis.h> |
91eee4dd | 35 | #include <rpcsvc/nis_callback.h> |
650425ce UD |
36 | #include <bits/libc-lock.h> |
37 | ||
91eee4dd | 38 | #include "nis_xdr.h" |
650425ce UD |
39 | #include "nis_intern.h" |
40 | ||
650425ce UD |
41 | /* Sorry, we are not able to make this threadsafe. Stupid. But some |
42 | functions doesn't send us a nis_result obj, so we don't have a | |
43 | cookie. Maybe we could use keys for threads ? Have to learn more | |
44 | about pthreads -- kukuk@vt.uni-paderborn.de */ | |
45 | ||
650425ce UD |
46 | static nis_cb *data; |
47 | ||
48 | __libc_lock_define_initialized (static, callback) | |
49 | ||
26a60f90 | 50 | |
91eee4dd | 51 | #if 0 |
26a60f90 UD |
52 | static char * |
53 | __nis_getpkey(const char *sname) | |
54 | { | |
55 | char buf[(strlen (sname) + 1) * 2 + 40]; | |
56 | char pkey[HEXKEYBYTES + 1]; | |
57 | char *cp, *domain; | |
58 | nis_result *res; | |
a1129917 | 59 | unsigned int len = 0; |
26a60f90 UD |
60 | |
61 | domain = strchr (sname, '.'); | |
62 | if (domain == NULL) | |
63 | return NULL; | |
64 | ||
65 | /* Remove prefixing dot */ | |
66 | ++domain; | |
67 | ||
68 | cp = stpcpy (buf, "[cname="); | |
69 | cp = stpcpy (cp, sname); | |
70 | cp = stpcpy (cp, ",auth_type=DES],cred.org_dir."); | |
71 | cp = stpcpy (cp, domain); | |
72 | ||
73 | res = nis_list (buf, USE_DGRAM|NO_AUTHINFO|FOLLOW_LINKS|FOLLOW_PATH, | |
74 | NULL, NULL); | |
75 | ||
76 | if (res == NULL) | |
77 | return NULL; | |
78 | ||
91eee4dd | 79 | if (NIS_RES_STATUS (res) != NIS_SUCCESS) |
26a60f90 UD |
80 | { |
81 | nis_freeresult (res); | |
82 | return NULL; | |
83 | } | |
84 | ||
85 | len = ENTRY_LEN(NIS_RES_OBJECT(res), 3); | |
86 | strncpy (pkey, ENTRY_VAL(NIS_RES_OBJECT(res), 3), len); | |
87 | pkey[len] = '\0'; | |
88 | cp = strchr (pkey, ':'); | |
89 | if (cp != NULL) | |
90 | *cp = '\0'; | |
91 | ||
92 | nis_freeresult (res); | |
93 | ||
94 | return strdup (pkey); | |
95 | } | |
91eee4dd | 96 | #endif |
650425ce UD |
97 | |
98 | static void | |
99 | cb_prog_1 (struct svc_req *rqstp, SVCXPRT *transp) | |
100 | { | |
101 | union | |
102 | { | |
103 | cback_data cbproc_receive_1_arg; | |
104 | nis_error cbproc_error_1_arg; | |
105 | } | |
106 | argument; | |
107 | char *result; | |
108 | xdrproc_t xdr_argument, xdr_result; | |
109 | bool_t bool_result; | |
110 | ||
111 | switch (rqstp->rq_proc) | |
112 | { | |
113 | case NULLPROC: | |
26a60f90 | 114 | svc_sendreply (transp, (xdrproc_t) xdr_void, (char *) NULL); |
650425ce UD |
115 | return; |
116 | ||
117 | case CBPROC_RECEIVE: | |
118 | { | |
a1129917 | 119 | unsigned int i; |
650425ce UD |
120 | |
121 | xdr_argument = (xdrproc_t) xdr_cback_data; | |
122 | xdr_result = (xdrproc_t) xdr_bool; | |
123 | memset (&argument, 0, sizeof (argument)); | |
124 | if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument)) | |
125 | { | |
126 | svcerr_decode (transp); | |
127 | return; | |
128 | } | |
129 | bool_result = FALSE; | |
130 | for (i = 0; i < argument.cbproc_receive_1_arg.entries.entries_len; ++i) | |
131 | { | |
26a60f90 UD |
132 | #define cbproc_entry(a) argument.cbproc_receive_1_arg.entries.entries_val[a] |
133 | char name[strlen (cbproc_entry(i)->zo_name) + | |
134 | strlen (cbproc_entry(i)->zo_domain) + 3]; | |
135 | char *cp; | |
136 | ||
137 | cp = stpcpy (name, cbproc_entry(i)->zo_name); | |
138 | *cp++ = '.'; | |
139 | cp = stpcpy (cp, cbproc_entry(i)->zo_domain); | |
650425ce | 140 | |
26a60f90 | 141 | if ((data->callback) (name, cbproc_entry(i), data->userdata)) |
650425ce UD |
142 | { |
143 | bool_result = TRUE; | |
144 | data->nomore = 1; | |
145 | data->result = NIS_SUCCESS; | |
146 | break; | |
147 | } | |
148 | } | |
149 | result = (char *) &bool_result; | |
150 | } | |
151 | break; | |
152 | case CBPROC_FINISH: | |
153 | xdr_argument = (xdrproc_t) xdr_void; | |
154 | xdr_result = (xdrproc_t) xdr_void; | |
155 | memset (&argument, 0, sizeof (argument)); | |
156 | if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument)) | |
157 | { | |
158 | svcerr_decode (transp); | |
159 | return; | |
160 | } | |
161 | data->nomore = 1; | |
162 | data->result = NIS_SUCCESS; | |
163 | bool_result = TRUE; /* to make gcc happy, not necessary */ | |
164 | result = (char *) &bool_result; | |
165 | break; | |
166 | case CBPROC_ERROR: | |
91eee4dd | 167 | xdr_argument = (xdrproc_t) _xdr_nis_error; |
650425ce UD |
168 | xdr_result = (xdrproc_t) xdr_void; |
169 | memset (&argument, 0, sizeof (argument)); | |
170 | if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument)) | |
171 | { | |
172 | svcerr_decode (transp); | |
173 | return; | |
174 | } | |
175 | data->nomore = 1; | |
176 | data->result = argument.cbproc_error_1_arg; | |
177 | bool_result = TRUE; /* to make gcc happy, not necessary */ | |
178 | result = (char *) &bool_result; | |
179 | break; | |
180 | default: | |
181 | svcerr_noproc (transp); | |
182 | return; | |
183 | } | |
184 | if (result != NULL && !svc_sendreply (transp, xdr_result, result)) | |
185 | svcerr_systemerr (transp); | |
186 | if (!svc_freeargs (transp, xdr_argument, (caddr_t) & argument)) | |
187 | { | |
188 | fputs (_ ("unable to free arguments"), stderr); | |
189 | exit (1); | |
190 | } | |
191 | return; | |
192 | } | |
193 | ||
650425ce UD |
194 | static nis_error |
195 | internal_nis_do_callback (struct dir_binding *bptr, netobj *cookie, | |
196 | struct nis_cb *cb) | |
197 | { | |
198 | /* Default timeout can be changed using clnt_control() */ | |
199 | static struct timeval TIMEOUT = {25, 0}; | |
200 | #ifdef FD_SETSIZE | |
201 | fd_set readfds; | |
202 | #else | |
203 | int readfds; | |
204 | #endif /* def FD_SETSIZE */ | |
205 | struct timeval tv; | |
206 | bool_t cb_is_running = FALSE; | |
207 | ||
208 | data = cb; | |
209 | ||
210 | for (;;) | |
211 | { | |
212 | #ifdef FD_SETSIZE | |
213 | readfds = svc_fdset; | |
214 | #else | |
215 | readfds = svc_fds; | |
216 | #endif /* def FD_SETSIZE */ | |
217 | tv.tv_sec = 25; | |
218 | tv.tv_usec = 0; | |
219 | switch (select (_rpc_dtablesize (), &readfds, NULL, NULL, &tv)) | |
220 | { | |
221 | case -1: | |
222 | if (errno == EINTR) | |
223 | continue; | |
224 | return NIS_CBERROR; | |
225 | case 0: | |
226 | /* See if callback 'thread' in the server is still alive. */ | |
227 | memset ((char *) &cb_is_running, 0, sizeof (cb_is_running)); | |
228 | if (clnt_call (bptr->clnt, NIS_CALLBACK, (xdrproc_t) xdr_netobj, | |
229 | (caddr_t) cookie, (xdrproc_t) xdr_bool, | |
230 | (caddr_t) & cb_is_running, TIMEOUT) != RPC_SUCCESS) | |
231 | cb_is_running = FALSE; | |
232 | ||
233 | if (cb_is_running == FALSE) | |
234 | { | |
235 | syslog (LOG_ERR, "NIS+: callback timed out"); | |
236 | return NIS_CBERROR; | |
237 | } | |
238 | break; | |
239 | default: | |
240 | svc_getreqset (&readfds); | |
241 | if (data->nomore) | |
242 | return data->result; | |
243 | } | |
244 | } | |
245 | } | |
246 | ||
247 | nis_error | |
248 | __nis_do_callback (struct dir_binding *bptr, netobj *cookie, | |
249 | struct nis_cb *cb) | |
250 | { | |
251 | nis_error result; | |
252 | ||
253 | __libc_lock_lock (callback); | |
254 | ||
255 | result = internal_nis_do_callback (bptr, cookie, cb); | |
256 | ||
257 | __libc_lock_unlock (callback); | |
258 | ||
259 | return result; | |
260 | } | |
261 | ||
262 | struct nis_cb * | |
263 | __nis_create_callback (int (*callback) (const_nis_name, const nis_object *, | |
264 | const void *), | |
a1129917 | 265 | const void *userdata, unsigned int flags) |
650425ce UD |
266 | { |
267 | struct nis_cb *cb; | |
268 | int sock = RPC_ANYSOCK; | |
269 | struct sockaddr_in sin; | |
270 | int len = sizeof (struct sockaddr_in); | |
271 | char addr[NIS_MAXNAMELEN + 1]; | |
272 | unsigned short port; | |
273 | ||
274 | cb = (struct nis_cb *) calloc (1, sizeof (struct nis_cb)); | |
275 | if (cb == NULL) | |
276 | { | |
277 | syslog (LOG_ERR, "NIS+: out of memory allocating callback"); | |
278 | return NULL; | |
279 | } | |
280 | ||
281 | cb->serv = (nis_server *) calloc (1, sizeof (nis_server)); | |
282 | if (cb->serv == NULL) | |
283 | { | |
284 | free (cb); | |
285 | syslog (LOG_ERR, "NIS+: out of memory allocating callback"); | |
91eee4dd | 286 | return NULL; |
650425ce | 287 | } |
26a60f90 | 288 | cb->serv->name = strdup (nis_local_principal ()); |
650425ce UD |
289 | cb->serv->ep.ep_val = (endpoint *) calloc (2, sizeof (endpoint)); |
290 | cb->serv->ep.ep_len = 1; | |
291 | cb->serv->ep.ep_val[0].family = strdup ("inet"); | |
292 | cb->callback = callback; | |
293 | cb->userdata = userdata; | |
294 | ||
91eee4dd | 295 | if ((flags & NO_AUTHINFO) || !key_secretkey_is_set ()) |
26a60f90 UD |
296 | { |
297 | cb->serv->key_type = NIS_PK_NONE; | |
298 | cb->serv->pkey.n_bytes = NULL; | |
299 | cb->serv->pkey.n_len = 0; | |
300 | } | |
301 | else | |
302 | { | |
91eee4dd | 303 | #if 0 |
26a60f90 UD |
304 | if ((cb->serv->pkey.n_bytes = __nis_getpkey (cb->serv->name)) == NULL) |
305 | { | |
306 | cb->serv->pkey.n_len = 0; | |
307 | cb->serv->key_type = NIS_PK_NONE; | |
308 | } | |
309 | else | |
310 | { | |
311 | cb->serv->key_type = NIS_PK_DH; | |
312 | cb->serv->pkey.n_len = strlen(cb->serv->pkey.n_bytes); | |
313 | } | |
91eee4dd UD |
314 | #else |
315 | cb->serv->pkey.n_len =0; | |
316 | cb->serv->pkey.n_bytes = NULL; | |
a53bad16 | 317 | cb->serv->key_type = NIS_PK_NONE; |
91eee4dd | 318 | #endif |
26a60f90 | 319 | } |
650425ce UD |
320 | |
321 | if (flags & USE_DGRAM) | |
322 | { | |
323 | cb->serv->ep.ep_val[0].proto = strdup ("udp"); | |
324 | cb->xprt = svcudp_bufcreate (sock, 100, 8192); | |
325 | } | |
326 | else | |
327 | { | |
328 | cb->serv->ep.ep_val[0].proto = strdup ("tcp"); | |
329 | cb->xprt = svctcp_create (sock, 100, 8192); | |
330 | } | |
331 | cb->sock = cb->xprt->xp_sock; | |
332 | if (!svc_register (cb->xprt, CB_PROG, CB_VERS, cb_prog_1, 0)) | |
333 | { | |
334 | xprt_unregister (cb->xprt); | |
335 | svc_destroy (cb->xprt); | |
91eee4dd | 336 | xdr_free ((xdrproc_t) _xdr_nis_server, (char *) cb->serv); |
650425ce UD |
337 | free (cb->serv); |
338 | free (cb); | |
339 | syslog (LOG_ERR, "NIS+: failed to register callback dispatcher"); | |
340 | return NULL; | |
341 | } | |
342 | ||
343 | if (getsockname (cb->sock, (struct sockaddr *) &sin, &len) == -1) | |
344 | { | |
345 | xprt_unregister (cb->xprt); | |
346 | svc_destroy (cb->xprt); | |
91eee4dd | 347 | xdr_free ((xdrproc_t) _xdr_nis_server, (char *) cb->serv); |
650425ce UD |
348 | free (cb->serv); |
349 | free (cb); | |
350 | syslog (LOG_ERR, "NIS+: failed to read local socket info"); | |
91eee4dd | 351 | return NULL; |
650425ce UD |
352 | } |
353 | port = sin.sin_port; | |
354 | get_myaddress (&sin); | |
355 | snprintf (addr, sizeof (addr), "%s.%d.%d", inet_ntoa (sin.sin_addr), | |
356 | port & 0x00FF, (port & 0xFF00) >> 8); | |
357 | cb->serv->ep.ep_val[0].uaddr = strdup (addr); | |
358 | ||
359 | return cb; | |
360 | } | |
361 | ||
362 | nis_error | |
363 | __nis_destroy_callback (struct nis_cb *cb) | |
364 | { | |
365 | xprt_unregister (cb->xprt); | |
366 | svc_destroy (cb->xprt); | |
367 | close (cb->sock); | |
91eee4dd | 368 | xdr_free ((xdrproc_t) _xdr_nis_server, (char *) cb->serv); |
650425ce UD |
369 | free (cb->serv); |
370 | free (cb); | |
371 | ||
372 | return NIS_SUCCESS; | |
373 | } |