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. */
25 #include <rpc/pmap_clnt.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;
60 typedef struct cback_data cback_data;
64 __libc_lock_define_initialized (static, callback)
68 __nis_getpkey(const char *sname)
70 char buf[(strlen (sname) + 1) * 2 + 40];
71 char pkey[HEXKEYBYTES + 1];
76 domain = strchr (sname, '.');
80 /* Remove prefixing dot */
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,
94 if (res->status != NIS_SUCCESS)
100 len = ENTRY_LEN(NIS_RES_OBJECT(res), 3);
101 strncpy (pkey, ENTRY_VAL(NIS_RES_OBJECT(res), 3), len);
103 cp = strchr (pkey, ':');
107 nis_freeresult (res);
109 return strdup (pkey);
113 static bool_t xdr_cback_data (XDR *, cback_data *);
116 cb_prog_1 (struct svc_req *rqstp, SVCXPRT *transp)
120 cback_data cbproc_receive_1_arg;
121 nis_error cbproc_error_1_arg;
125 xdrproc_t xdr_argument, xdr_result;
128 switch (rqstp->rq_proc)
131 svc_sendreply (transp, (xdrproc_t) xdr_void, (char *) NULL);
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);
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];
154 cp = stpcpy (name, cbproc_entry(i)->zo_name);
156 cp = stpcpy (cp, cbproc_entry(i)->zo_domain);
158 if ((data->callback) (name, cbproc_entry(i), data->userdata))
162 data->result = NIS_SUCCESS;
166 result = (char *) &bool_result;
170 xdr_argument = (xdrproc_t) xdr_void;
171 xdr_result = (xdrproc_t) xdr_void;
172 memset (&argument, 0, sizeof (argument));
173 if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
175 svcerr_decode (transp);
179 data->result = NIS_SUCCESS;
180 bool_result = TRUE; /* to make gcc happy, not necessary */
181 result = (char *) &bool_result;
184 xdr_argument = (xdrproc_t) xdr_nis_error;
185 xdr_result = (xdrproc_t) xdr_void;
186 memset (&argument, 0, sizeof (argument));
187 if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
189 svcerr_decode (transp);
193 data->result = argument.cbproc_error_1_arg;
194 bool_result = TRUE; /* to make gcc happy, not necessary */
195 result = (char *) &bool_result;
198 svcerr_noproc (transp);
201 if (result != NULL && !svc_sendreply (transp, xdr_result, result))
202 svcerr_systemerr (transp);
203 if (!svc_freeargs (transp, xdr_argument, (caddr_t) & argument))
205 fputs (_ ("unable to free arguments"), stderr);
212 xdr_obj_p (XDR * xdrs, obj_p *objp)
214 if (!xdr_pointer (xdrs, (char **) objp, sizeof (nis_object),
215 (xdrproc_t) xdr_nis_object))
221 xdr_cback_data (XDR *xdrs, cback_data *objp)
223 if (!xdr_array (xdrs, (char **) &objp->entries.entries_val,
224 (u_int *) & objp->entries.entries_len, ~0, sizeof (obj_p),
225 (xdrproc_t) xdr_obj_p))
231 internal_nis_do_callback (struct dir_binding *bptr, netobj *cookie,
234 /* Default timeout can be changed using clnt_control() */
235 static struct timeval TIMEOUT = {25, 0};
240 #endif /* def FD_SETSIZE */
242 bool_t cb_is_running = FALSE;
252 #endif /* def FD_SETSIZE */
255 switch (select (_rpc_dtablesize (), &readfds, NULL, NULL, &tv))
262 /* See if callback 'thread' in the server is still alive. */
263 memset ((char *) &cb_is_running, 0, sizeof (cb_is_running));
264 if (clnt_call (bptr->clnt, NIS_CALLBACK, (xdrproc_t) xdr_netobj,
265 (caddr_t) cookie, (xdrproc_t) xdr_bool,
266 (caddr_t) & cb_is_running, TIMEOUT) != RPC_SUCCESS)
267 cb_is_running = FALSE;
269 if (cb_is_running == FALSE)
271 syslog (LOG_ERR, "NIS+: callback timed out");
276 svc_getreqset (&readfds);
284 __nis_do_callback (struct dir_binding *bptr, netobj *cookie,
289 __libc_lock_lock (callback);
291 result = internal_nis_do_callback (bptr, cookie, cb);
293 __libc_lock_unlock (callback);
299 __nis_create_callback (int (*callback) (const_nis_name, const nis_object *,
301 const void *userdata, u_long flags)
304 int sock = RPC_ANYSOCK;
305 struct sockaddr_in sin;
306 int len = sizeof (struct sockaddr_in);
307 char addr[NIS_MAXNAMELEN + 1];
310 cb = (struct nis_cb *) calloc (1, sizeof (struct nis_cb));
313 syslog (LOG_ERR, "NIS+: out of memory allocating callback");
317 cb->serv = (nis_server *) calloc (1, sizeof (nis_server));
318 if (cb->serv == NULL)
321 syslog (LOG_ERR, "NIS+: out of memory allocating callback");
324 cb->serv->name = strdup (nis_local_principal ());
325 cb->serv->ep.ep_val = (endpoint *) calloc (2, sizeof (endpoint));
326 cb->serv->ep.ep_len = 1;
327 cb->serv->ep.ep_val[0].family = strdup ("inet");
328 cb->callback = callback;
329 cb->userdata = userdata;
331 if ((flags & NO_AUTHINFO) && key_secretkey_is_set ())
333 cb->serv->key_type = NIS_PK_NONE;
334 cb->serv->pkey.n_bytes = NULL;
335 cb->serv->pkey.n_len = 0;
339 if ((cb->serv->pkey.n_bytes = __nis_getpkey (cb->serv->name)) == NULL)
341 cb->serv->pkey.n_len = 0;
342 cb->serv->key_type = NIS_PK_NONE;
346 cb->serv->key_type = NIS_PK_DH;
347 cb->serv->pkey.n_len = strlen(cb->serv->pkey.n_bytes);
351 if (flags & USE_DGRAM)
353 cb->serv->ep.ep_val[0].proto = strdup ("udp");
354 cb->xprt = svcudp_bufcreate (sock, 100, 8192);
358 cb->serv->ep.ep_val[0].proto = strdup ("tcp");
359 cb->xprt = svctcp_create (sock, 100, 8192);
361 cb->sock = cb->xprt->xp_sock;
362 if (!svc_register (cb->xprt, CB_PROG, CB_VERS, cb_prog_1, 0))
364 xprt_unregister (cb->xprt);
365 svc_destroy (cb->xprt);
366 xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);
369 syslog (LOG_ERR, "NIS+: failed to register callback dispatcher");
373 if (getsockname (cb->sock, (struct sockaddr *) &sin, &len) == -1)
375 xprt_unregister (cb->xprt);
376 svc_destroy (cb->xprt);
377 xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);
380 syslog (LOG_ERR, "NIS+: failed to read local socket info");
384 get_myaddress (&sin);
385 snprintf (addr, sizeof (addr), "%s.%d.%d", inet_ntoa (sin.sin_addr),
386 port & 0x00FF, (port & 0xFF00) >> 8);
387 cb->serv->ep.ep_val[0].uaddr = strdup (addr);
393 __nis_destroy_callback (struct nis_cb *cb)
395 xprt_unregister (cb->xprt);
396 svc_destroy (cb->xprt);
398 xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);