Update.
[glibc.git] / nis / nis_callback.c
blob7dfab8ff2508a0433d0c5eca363e18056e8b1409
1 /* Copyright (C) 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>, 1997.
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 <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <rpc/rpc.h>
25 #include <rpc/pmap_clnt.h>
26 #include <string.h>
27 #include <memory.h>
28 #include <syslog.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <rpc/key_prot.h>
33 #include <rpcsvc/nis.h>
34 #include <bits/libc-lock.h>
36 #include "nis_intern.h"
38 /* Sorry, we are not able to make this threadsafe. Stupid. But some
39 functions doesn't send us a nis_result obj, so we don't have a
40 cookie. Maybe we could use keys for threads ? Have to learn more
41 about pthreads -- kukuk@vt.uni-paderborn.de */
43 #define CB_PROG ((u_long)100302)
44 #define CB_VERS ((u_long)1)
45 #define CBPROC_RECEIVE ((u_long)1)
46 #define CBPROC_FINISH ((u_long)2)
47 #define CBPROC_ERROR ((u_long)3)
49 typedef nis_object *obj_p;
51 struct cback_data
53 struct
55 u_int entries_len;
56 obj_p *entries_val;
58 entries;
60 typedef struct cback_data cback_data;
62 static nis_cb *data;
64 __libc_lock_define_initialized (static, callback)
67 static char *
68 __nis_getpkey(const char *sname)
70 char buf[(strlen (sname) + 1) * 2 + 40];
71 char pkey[HEXKEYBYTES + 1];
72 char *cp, *domain;
73 nis_result *res;
74 u_int len = 0;
76 domain = strchr (sname, '.');
77 if (domain == NULL)
78 return NULL;
80 /* Remove prefixing dot */
81 ++domain;
83 cp = stpcpy (buf, "[cname=");
84 cp = stpcpy (cp, sname);
85 cp = stpcpy (cp, ",auth_type=DES],cred.org_dir.");
86 cp = stpcpy (cp, domain);
88 res = nis_list (buf, USE_DGRAM|NO_AUTHINFO|FOLLOW_LINKS|FOLLOW_PATH,
89 NULL, NULL);
91 if (res == NULL)
92 return NULL;
94 if (res->status != NIS_SUCCESS)
96 nis_freeresult (res);
97 return NULL;
100 len = ENTRY_LEN(NIS_RES_OBJECT(res), 3);
101 strncpy (pkey, ENTRY_VAL(NIS_RES_OBJECT(res), 3), len);
102 pkey[len] = '\0';
103 cp = strchr (pkey, ':');
104 if (cp != NULL)
105 *cp = '\0';
107 nis_freeresult (res);
109 return strdup (pkey);
113 static bool_t xdr_cback_data (XDR *, cback_data *);
115 static void
116 cb_prog_1 (struct svc_req *rqstp, SVCXPRT *transp)
118 union
120 cback_data cbproc_receive_1_arg;
121 nis_error cbproc_error_1_arg;
123 argument;
124 char *result;
125 xdrproc_t xdr_argument, xdr_result;
126 bool_t bool_result;
128 switch (rqstp->rq_proc)
130 case NULLPROC:
131 svc_sendreply (transp, (xdrproc_t) xdr_void, (char *) NULL);
132 return;
134 case CBPROC_RECEIVE:
136 u_long i;
138 xdr_argument = (xdrproc_t) xdr_cback_data;
139 xdr_result = (xdrproc_t) xdr_bool;
140 memset (&argument, 0, sizeof (argument));
141 if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
143 svcerr_decode (transp);
144 return;
146 bool_result = FALSE;
147 for (i = 0; i < argument.cbproc_receive_1_arg.entries.entries_len; ++i)
149 #define cbproc_entry(a) argument.cbproc_receive_1_arg.entries.entries_val[a]
150 char name[strlen (cbproc_entry(i)->zo_name) +
151 strlen (cbproc_entry(i)->zo_domain) + 3];
152 char *cp;
154 cp = stpcpy (name, cbproc_entry(i)->zo_name);
155 *cp++ = '.';
156 cp = stpcpy (cp, cbproc_entry(i)->zo_domain);
158 fprintf (stderr, "name=%s\n", name);
160 if ((data->callback) (name, cbproc_entry(i), data->userdata))
162 bool_result = TRUE;
163 data->nomore = 1;
164 data->result = NIS_SUCCESS;
165 break;
168 result = (char *) &bool_result;
170 break;
171 case CBPROC_FINISH:
172 xdr_argument = (xdrproc_t) xdr_void;
173 xdr_result = (xdrproc_t) xdr_void;
174 memset (&argument, 0, sizeof (argument));
175 if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
177 svcerr_decode (transp);
178 return;
180 data->nomore = 1;
181 data->result = NIS_SUCCESS;
182 bool_result = TRUE; /* to make gcc happy, not necessary */
183 result = (char *) &bool_result;
184 break;
185 case CBPROC_ERROR:
186 xdr_argument = (xdrproc_t) xdr_nis_error;
187 xdr_result = (xdrproc_t) xdr_void;
188 memset (&argument, 0, sizeof (argument));
189 if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
191 svcerr_decode (transp);
192 return;
194 data->nomore = 1;
195 data->result = argument.cbproc_error_1_arg;
196 bool_result = TRUE; /* to make gcc happy, not necessary */
197 result = (char *) &bool_result;
198 break;
199 default:
200 svcerr_noproc (transp);
201 return;
203 if (result != NULL && !svc_sendreply (transp, xdr_result, result))
204 svcerr_systemerr (transp);
205 if (!svc_freeargs (transp, xdr_argument, (caddr_t) & argument))
207 fputs (_ ("unable to free arguments"), stderr);
208 exit (1);
210 return;
213 static bool_t
214 xdr_obj_p (XDR * xdrs, obj_p *objp)
216 if (!xdr_pointer (xdrs, (char **) objp, sizeof (nis_object),
217 (xdrproc_t) xdr_nis_object))
218 return FALSE;
219 return TRUE;
222 static bool_t
223 xdr_cback_data (XDR *xdrs, cback_data *objp)
225 if (!xdr_array (xdrs, (char **) &objp->entries.entries_val,
226 (u_int *) & objp->entries.entries_len, ~0, sizeof (obj_p),
227 (xdrproc_t) xdr_obj_p))
228 return FALSE;
229 return TRUE;
232 static nis_error
233 internal_nis_do_callback (struct dir_binding *bptr, netobj *cookie,
234 struct nis_cb *cb)
236 /* Default timeout can be changed using clnt_control() */
237 static struct timeval TIMEOUT = {25, 0};
238 #ifdef FD_SETSIZE
239 fd_set readfds;
240 #else
241 int readfds;
242 #endif /* def FD_SETSIZE */
243 struct timeval tv;
244 bool_t cb_is_running = FALSE;
246 data = cb;
248 for (;;)
250 #ifdef FD_SETSIZE
251 readfds = svc_fdset;
252 #else
253 readfds = svc_fds;
254 #endif /* def FD_SETSIZE */
255 tv.tv_sec = 25;
256 tv.tv_usec = 0;
257 switch (select (_rpc_dtablesize (), &readfds, NULL, NULL, &tv))
259 case -1:
260 if (errno == EINTR)
261 continue;
262 return NIS_CBERROR;
263 case 0:
264 /* See if callback 'thread' in the server is still alive. */
265 memset ((char *) &cb_is_running, 0, sizeof (cb_is_running));
266 if (clnt_call (bptr->clnt, NIS_CALLBACK, (xdrproc_t) xdr_netobj,
267 (caddr_t) cookie, (xdrproc_t) xdr_bool,
268 (caddr_t) & cb_is_running, TIMEOUT) != RPC_SUCCESS)
269 cb_is_running = FALSE;
271 if (cb_is_running == FALSE)
273 syslog (LOG_ERR, "NIS+: callback timed out");
274 return NIS_CBERROR;
276 break;
277 default:
278 svc_getreqset (&readfds);
279 if (data->nomore)
280 return data->result;
285 nis_error
286 __nis_do_callback (struct dir_binding *bptr, netobj *cookie,
287 struct nis_cb *cb)
289 nis_error result;
291 __libc_lock_lock (callback);
293 result = internal_nis_do_callback (bptr, cookie, cb);
295 __libc_lock_unlock (callback);
297 return result;
300 struct nis_cb *
301 __nis_create_callback (int (*callback) (const_nis_name, const nis_object *,
302 const void *),
303 const void *userdata, u_long flags)
305 struct nis_cb *cb;
306 int sock = RPC_ANYSOCK;
307 struct sockaddr_in sin;
308 int len = sizeof (struct sockaddr_in);
309 char addr[NIS_MAXNAMELEN + 1];
310 unsigned short port;
312 cb = (struct nis_cb *) calloc (1, sizeof (struct nis_cb));
313 if (cb == NULL)
315 syslog (LOG_ERR, "NIS+: out of memory allocating callback");
316 return NULL;
319 cb->serv = (nis_server *) calloc (1, sizeof (nis_server));
320 if (cb->serv == NULL)
322 free (cb);
323 syslog (LOG_ERR, "NIS+: out of memory allocating callback");
324 return (NULL);
326 cb->serv->name = strdup (nis_local_principal ());
327 cb->serv->ep.ep_val = (endpoint *) calloc (2, sizeof (endpoint));
328 cb->serv->ep.ep_len = 1;
329 cb->serv->ep.ep_val[0].family = strdup ("inet");
330 cb->callback = callback;
331 cb->userdata = userdata;
333 if ((flags & NO_AUTHINFO) && key_secretkey_is_set ())
335 cb->serv->key_type = NIS_PK_NONE;
336 cb->serv->pkey.n_bytes = NULL;
337 cb->serv->pkey.n_len = 0;
339 else
341 if ((cb->serv->pkey.n_bytes = __nis_getpkey (cb->serv->name)) == NULL)
343 cb->serv->pkey.n_len = 0;
344 cb->serv->key_type = NIS_PK_NONE;
346 else
348 cb->serv->key_type = NIS_PK_DH;
349 cb->serv->pkey.n_len = strlen(cb->serv->pkey.n_bytes);
353 if (flags & USE_DGRAM)
355 cb->serv->ep.ep_val[0].proto = strdup ("udp");
356 cb->xprt = svcudp_bufcreate (sock, 100, 8192);
358 else
360 cb->serv->ep.ep_val[0].proto = strdup ("tcp");
361 cb->xprt = svctcp_create (sock, 100, 8192);
363 cb->sock = cb->xprt->xp_sock;
364 if (!svc_register (cb->xprt, CB_PROG, CB_VERS, cb_prog_1, 0))
366 xprt_unregister (cb->xprt);
367 svc_destroy (cb->xprt);
368 xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);
369 free (cb->serv);
370 free (cb);
371 syslog (LOG_ERR, "NIS+: failed to register callback dispatcher");
372 return NULL;
375 if (getsockname (cb->sock, (struct sockaddr *) &sin, &len) == -1)
377 xprt_unregister (cb->xprt);
378 svc_destroy (cb->xprt);
379 xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);
380 free (cb->serv);
381 free (cb);
382 syslog (LOG_ERR, "NIS+: failed to read local socket info");
383 return (NULL);
385 port = sin.sin_port;
386 get_myaddress (&sin);
387 snprintf (addr, sizeof (addr), "%s.%d.%d", inet_ntoa (sin.sin_addr),
388 port & 0x00FF, (port & 0xFF00) >> 8);
389 cb->serv->ep.ep_val[0].uaddr = strdup (addr);
391 return cb;
394 nis_error
395 __nis_destroy_callback (struct nis_cb *cb)
397 xprt_unregister (cb->xprt);
398 svc_destroy (cb->xprt);
399 close (cb->sock);
400 xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);
401 free (cb->serv);
402 free (cb);
404 return NIS_SUCCESS;