1 /* Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.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. */
26 #include <rpc/pmap_clnt.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <rpc/key_prot.h>
35 #include <rpcsvc/nis.h>
36 #include <rpcsvc/nis_callback.h>
37 #include <bits/libc-lock.h>
40 #include "nis_intern.h"
42 /* Sorry, we are not able to make this threadsafe. Stupid. But some
43 functions doesn't send us a nis_result obj, so we don't have a
44 cookie. Maybe we could use keys for threads ? Have to learn more
45 about pthreads -- kukuk@vt.uni-paderborn.de */
49 __libc_lock_define_initialized (static, callback)
54 __nis_getpkey(const char *sname)
56 char buf[(strlen (sname) + 1) * 2 + 40];
57 char pkey[HEXKEYBYTES + 1];
62 domain = strchr (sname, '.');
66 /* Remove prefixing dot */
69 cp = stpcpy (buf, "[cname=");
70 cp = stpcpy (cp, sname);
71 cp = stpcpy (cp, ",auth_type=DES],cred.org_dir.");
72 cp = stpcpy (cp, domain);
74 res = nis_list (buf, USE_DGRAM|NO_AUTHINFO|FOLLOW_LINKS|FOLLOW_PATH,
80 if (NIS_RES_STATUS (res) != NIS_SUCCESS)
86 len = ENTRY_LEN(NIS_RES_OBJECT(res), 3);
87 strncpy (pkey, ENTRY_VAL(NIS_RES_OBJECT(res), 3), len);
89 cp = strchr (pkey, ':');
100 cb_prog_1 (struct svc_req *rqstp, SVCXPRT *transp)
104 cback_data cbproc_receive_1_arg;
105 nis_error cbproc_error_1_arg;
109 xdrproc_t xdr_argument, xdr_result;
112 switch (rqstp->rq_proc)
115 svc_sendreply (transp, (xdrproc_t) xdr_void, (char *) NULL);
122 xdr_argument = (xdrproc_t) xdr_cback_data;
123 xdr_result = (xdrproc_t) xdr_bool;
124 memset (&argument, 0, sizeof (argument));
125 if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
127 svcerr_decode (transp);
131 for (i = 0; i < argument.cbproc_receive_1_arg.entries.entries_len; ++i)
133 #define cbproc_entry(a) argument.cbproc_receive_1_arg.entries.entries_val[a]
134 char name[strlen (cbproc_entry(i)->zo_name) +
135 strlen (cbproc_entry(i)->zo_domain) + 3];
138 cp = stpcpy (name, cbproc_entry(i)->zo_name);
140 cp = stpcpy (cp, cbproc_entry(i)->zo_domain);
142 if ((data->callback) (name, cbproc_entry(i), data->userdata))
146 data->result = NIS_SUCCESS;
150 result = (char *) &bool_result;
154 xdr_argument = (xdrproc_t) xdr_void;
155 xdr_result = (xdrproc_t) xdr_void;
156 memset (&argument, 0, sizeof (argument));
157 if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
159 svcerr_decode (transp);
163 data->result = NIS_SUCCESS;
164 bool_result = TRUE; /* to make gcc happy, not necessary */
165 result = (char *) &bool_result;
168 xdr_argument = (xdrproc_t) _xdr_nis_error;
169 xdr_result = (xdrproc_t) xdr_void;
170 memset (&argument, 0, sizeof (argument));
171 if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
173 svcerr_decode (transp);
177 data->result = argument.cbproc_error_1_arg;
178 bool_result = TRUE; /* to make gcc happy, not necessary */
179 result = (char *) &bool_result;
182 svcerr_noproc (transp);
185 if (result != NULL && !svc_sendreply (transp, xdr_result, result))
186 svcerr_systemerr (transp);
187 if (!svc_freeargs (transp, xdr_argument, (caddr_t) & argument))
189 fputs (_ ("unable to free arguments"), stderr);
196 internal_nis_do_callback (struct dir_binding *bptr, netobj *cookie,
199 struct timeval TIMEOUT = {25, 0};
200 bool_t cb_is_running = FALSE;
206 struct pollfd *my_pollfd;
209 if (svc_max_pollfd == 0 && svc_pollfd == NULL)
212 my_pollfd = malloc (sizeof (struct pollfd) * svc_max_pollfd);
213 for (i = 0; i < svc_max_pollfd; ++i)
215 my_pollfd[i].fd = svc_pollfd[i].fd;
216 my_pollfd[i].events = svc_pollfd[i].events;
217 my_pollfd[i].revents = 0;
220 switch (i = __poll (my_pollfd, svc_max_pollfd, 25*1000))
227 /* See if callback 'thread' in the server is still alive. */
228 memset ((char *) &cb_is_running, 0, sizeof (cb_is_running));
229 if (clnt_call (bptr->clnt, NIS_CALLBACK, (xdrproc_t) xdr_netobj,
230 (caddr_t) cookie, (xdrproc_t) xdr_bool,
231 (caddr_t) & cb_is_running, TIMEOUT) != RPC_SUCCESS)
232 cb_is_running = FALSE;
234 if (cb_is_running == FALSE)
236 syslog (LOG_ERR, "NIS+: callback timed out");
241 svc_getreq_poll (my_pollfd, i);
249 __nis_do_callback (struct dir_binding *bptr, netobj *cookie,
254 __libc_lock_lock (callback);
256 result = internal_nis_do_callback (bptr, cookie, cb);
258 __libc_lock_unlock (callback);
264 __nis_create_callback (int (*callback) (const_nis_name, const nis_object *,
266 const void *userdata, unsigned int flags)
269 int sock = RPC_ANYSOCK;
270 struct sockaddr_in sin;
271 int len = sizeof (struct sockaddr_in);
272 char addr[NIS_MAXNAMELEN + 1];
275 cb = (struct nis_cb *) calloc (1, sizeof (struct nis_cb));
278 syslog (LOG_ERR, "NIS+: out of memory allocating callback");
282 cb->serv = (nis_server *) calloc (1, sizeof (nis_server));
283 if (cb->serv == NULL)
286 syslog (LOG_ERR, "NIS+: out of memory allocating callback");
289 cb->serv->name = strdup (nis_local_principal ());
290 cb->serv->ep.ep_val = (endpoint *) calloc (2, sizeof (endpoint));
291 cb->serv->ep.ep_len = 1;
292 cb->serv->ep.ep_val[0].family = strdup ("inet");
293 cb->callback = callback;
294 cb->userdata = userdata;
296 if ((flags & NO_AUTHINFO) || !key_secretkey_is_set ())
298 cb->serv->key_type = NIS_PK_NONE;
299 cb->serv->pkey.n_bytes = NULL;
300 cb->serv->pkey.n_len = 0;
305 if ((cb->serv->pkey.n_bytes = __nis_getpkey (cb->serv->name)) == NULL)
307 cb->serv->pkey.n_len = 0;
308 cb->serv->key_type = NIS_PK_NONE;
312 cb->serv->key_type = NIS_PK_DH;
313 cb->serv->pkey.n_len = strlen(cb->serv->pkey.n_bytes);
316 cb->serv->pkey.n_len =0;
317 cb->serv->pkey.n_bytes = NULL;
318 cb->serv->key_type = NIS_PK_NONE;
322 if (flags & USE_DGRAM)
324 cb->serv->ep.ep_val[0].proto = strdup ("udp");
325 cb->xprt = svcudp_bufcreate (sock, 100, 8192);
329 cb->serv->ep.ep_val[0].proto = strdup ("tcp");
330 cb->xprt = svctcp_create (sock, 100, 8192);
332 cb->sock = cb->xprt->xp_sock;
333 if (!svc_register (cb->xprt, CB_PROG, CB_VERS, cb_prog_1, 0))
335 xprt_unregister (cb->xprt);
336 svc_destroy (cb->xprt);
337 xdr_free ((xdrproc_t) _xdr_nis_server, (char *) cb->serv);
340 syslog (LOG_ERR, "NIS+: failed to register callback dispatcher");
344 if (getsockname (cb->sock, (struct sockaddr *) &sin, &len) == -1)
346 xprt_unregister (cb->xprt);
347 svc_destroy (cb->xprt);
348 xdr_free ((xdrproc_t) _xdr_nis_server, (char *) cb->serv);
351 syslog (LOG_ERR, "NIS+: failed to read local socket info");
355 get_myaddress (&sin);
356 snprintf (addr, sizeof (addr), "%s.%d.%d", inet_ntoa (sin.sin_addr),
357 port & 0x00FF, (port & 0xFF00) >> 8);
358 cb->serv->ep.ep_val[0].uaddr = strdup (addr);
364 __nis_destroy_callback (struct nis_cb *cb)
366 xprt_unregister (cb->xprt);
367 svc_destroy (cb->xprt);
369 xdr_free ((xdrproc_t) _xdr_nis_server, (char *) cb->serv);