1 /* Copyright (C) 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>, 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. */
24 #include <rpc/pmap_clnt.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <rpcsvc/nis.h>
32 #include <bits/libc-lock.h>
34 #include "nis_intern.h"
36 extern void get_myaddress (struct sockaddr_in *addr);
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;
60 typedef struct cback_data cback_data;
64 __libc_lock_define_initialized (static, callback)
66 static bool_t xdr_cback_data (XDR *, cback_data *);
69 cb_prog_1 (struct svc_req *rqstp, SVCXPRT *transp)
73 cback_data cbproc_receive_1_arg;
74 nis_error cbproc_error_1_arg;
78 xdrproc_t xdr_argument, xdr_result;
81 switch (rqstp->rq_proc)
84 (void) svc_sendreply (transp, (xdrproc_t) xdr_void, (char *) NULL);
89 char name[NIS_MAXNAMELEN + 1];
92 xdr_argument = (xdrproc_t) xdr_cback_data;
93 xdr_result = (xdrproc_t) xdr_bool;
94 memset (&argument, 0, sizeof (argument));
95 if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
97 svcerr_decode (transp);
101 for (i = 0; i < argument.cbproc_receive_1_arg.entries.entries_len; ++i)
103 snprintf (name, NIS_MAXNAMELEN, "%s.%s",
104 argument.cbproc_receive_1_arg.entries.entries_val[i]->zo_name,
105 argument.cbproc_receive_1_arg.entries.entries_val[i]->zo_domain);
108 (name, argument.cbproc_receive_1_arg.entries.entries_val[i],
113 data->result = NIS_SUCCESS;
117 result = (char *) &bool_result;
121 xdr_argument = (xdrproc_t) xdr_void;
122 xdr_result = (xdrproc_t) xdr_void;
123 memset (&argument, 0, sizeof (argument));
124 if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
126 svcerr_decode (transp);
130 data->result = NIS_SUCCESS;
131 bool_result = TRUE; /* to make gcc happy, not necessary */
132 result = (char *) &bool_result;
135 xdr_argument = (xdrproc_t) xdr_nis_error;
136 xdr_result = (xdrproc_t) xdr_void;
137 memset (&argument, 0, sizeof (argument));
138 if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
140 svcerr_decode (transp);
144 data->result = argument.cbproc_error_1_arg;
145 bool_result = TRUE; /* to make gcc happy, not necessary */
146 result = (char *) &bool_result;
149 svcerr_noproc (transp);
152 if (result != NULL && !svc_sendreply (transp, xdr_result, result))
153 svcerr_systemerr (transp);
154 if (!svc_freeargs (transp, xdr_argument, (caddr_t) & argument))
156 fputs (_ ("unable to free arguments"), stderr);
163 xdr_obj_p (XDR * xdrs, obj_p *objp)
165 if (!xdr_pointer (xdrs, (char **) objp, sizeof (nis_object),
166 (xdrproc_t) xdr_nis_object))
172 xdr_cback_data (XDR *xdrs, cback_data *objp)
174 if (!xdr_array (xdrs, (char **) &objp->entries.entries_val,
175 (u_int *) & objp->entries.entries_len, ~0, sizeof (obj_p),
176 (xdrproc_t) xdr_obj_p))
182 internal_nis_do_callback (struct dir_binding *bptr, netobj *cookie,
185 /* Default timeout can be changed using clnt_control() */
186 static struct timeval TIMEOUT = {25, 0};
191 #endif /* def FD_SETSIZE */
193 bool_t cb_is_running = FALSE;
203 #endif /* def FD_SETSIZE */
206 switch (select (_rpc_dtablesize (), &readfds, NULL, NULL, &tv))
213 /* See if callback 'thread' in the server is still alive. */
214 memset ((char *) &cb_is_running, 0, sizeof (cb_is_running));
215 if (clnt_call (bptr->clnt, NIS_CALLBACK, (xdrproc_t) xdr_netobj,
216 (caddr_t) cookie, (xdrproc_t) xdr_bool,
217 (caddr_t) & cb_is_running, TIMEOUT) != RPC_SUCCESS)
218 cb_is_running = FALSE;
220 if (cb_is_running == FALSE)
222 syslog (LOG_ERR, "NIS+: callback timed out");
227 svc_getreqset (&readfds);
235 __nis_do_callback (struct dir_binding *bptr, netobj *cookie,
240 __libc_lock_lock (callback);
242 result = internal_nis_do_callback (bptr, cookie, cb);
244 __libc_lock_unlock (callback);
250 __nis_create_callback (int (*callback) (const_nis_name, const nis_object *,
252 const void *userdata, u_long flags)
255 int sock = RPC_ANYSOCK;
256 struct sockaddr_in sin;
257 int len = sizeof (struct sockaddr_in);
258 char addr[NIS_MAXNAMELEN + 1];
261 cb = (struct nis_cb *) calloc (1, sizeof (struct nis_cb));
264 syslog (LOG_ERR, "NIS+: out of memory allocating callback");
268 cb->serv = (nis_server *) calloc (1, sizeof (nis_server));
269 if (cb->serv == NULL)
272 syslog (LOG_ERR, "NIS+: out of memory allocating callback");
275 cb->serv->name = strdup (nis_local_host ());
276 cb->serv->ep.ep_val = (endpoint *) calloc (2, sizeof (endpoint));
277 cb->serv->ep.ep_len = 1;
278 cb->serv->ep.ep_val[0].family = strdup ("inet");
279 cb->callback = callback;
280 cb->userdata = userdata;
282 /* XXX Sometimes, we should add the public key of the user here ! */
283 cb->serv->key_type = NIS_PK_NONE;
284 cb->serv->pkey.n_bytes = NULL;
285 cb->serv->pkey.n_len = 0;
287 if (flags & USE_DGRAM)
289 cb->serv->ep.ep_val[0].proto = strdup ("udp");
290 cb->xprt = svcudp_bufcreate (sock, 100, 8192);
294 cb->serv->ep.ep_val[0].proto = strdup ("tcp");
295 cb->xprt = svctcp_create (sock, 100, 8192);
297 cb->sock = cb->xprt->xp_sock;
298 if (!svc_register (cb->xprt, CB_PROG, CB_VERS, cb_prog_1, 0))
300 xprt_unregister (cb->xprt);
301 svc_destroy (cb->xprt);
302 xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);
305 syslog (LOG_ERR, "NIS+: failed to register callback dispatcher");
309 if (getsockname (cb->sock, (struct sockaddr *) &sin, &len) == -1)
311 xprt_unregister (cb->xprt);
312 svc_destroy (cb->xprt);
313 xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);
316 syslog (LOG_ERR, "NIS+: failed to read local socket info");
320 get_myaddress (&sin);
321 snprintf (addr, sizeof (addr), "%s.%d.%d", inet_ntoa (sin.sin_addr),
322 port & 0x00FF, (port & 0xFF00) >> 8);
323 cb->serv->ep.ep_val[0].uaddr = strdup (addr);
329 __nis_destroy_callback (struct nis_cb *cb)
331 xprt_unregister (cb->xprt);
332 svc_destroy (cb->xprt);
334 xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);