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. */
25 #include <rpc/pmap_clnt.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <rpcsvc/nis.h>
33 #include <bits/libc-lock.h>
35 #include "nis_intern.h"
37 extern void get_myaddress (struct sockaddr_in
*addr
);
39 /* Sorry, we are not able to make this threadsafe. Stupid. But some
40 functions doesn't send us a nis_result obj, so we don't have a
41 cookie. Maybe we could use keys for threads ? Have to learn more
42 about pthreads -- kukuk@vt.uni-paderborn.de */
44 #define CB_PROG ((u_long)100302)
45 #define CB_VERS ((u_long)1)
46 #define CBPROC_RECEIVE ((u_long)1)
47 #define CBPROC_FINISH ((u_long)2)
48 #define CBPROC_ERROR ((u_long)3)
50 typedef nis_object
*obj_p
;
61 typedef struct cback_data cback_data
;
65 __libc_lock_define_initialized (static, callback
)
67 static bool_t
xdr_cback_data (XDR
*, cback_data
*);
70 cb_prog_1 (struct svc_req
*rqstp
, SVCXPRT
*transp
)
74 cback_data cbproc_receive_1_arg
;
75 nis_error cbproc_error_1_arg
;
79 xdrproc_t xdr_argument
, xdr_result
;
82 switch (rqstp
->rq_proc
)
85 (void) svc_sendreply (transp
, (xdrproc_t
) xdr_void
, (char *) NULL
);
90 char name
[NIS_MAXNAMELEN
+ 1];
93 xdr_argument
= (xdrproc_t
) xdr_cback_data
;
94 xdr_result
= (xdrproc_t
) xdr_bool
;
95 memset (&argument
, 0, sizeof (argument
));
96 if (!svc_getargs (transp
, xdr_argument
, (caddr_t
) & argument
))
98 svcerr_decode (transp
);
102 for (i
= 0; i
< argument
.cbproc_receive_1_arg
.entries
.entries_len
; ++i
)
104 snprintf (name
, NIS_MAXNAMELEN
, "%s.%s",
105 argument
.cbproc_receive_1_arg
.entries
.entries_val
[i
]->zo_name
,
106 argument
.cbproc_receive_1_arg
.entries
.entries_val
[i
]->zo_domain
);
109 (name
, argument
.cbproc_receive_1_arg
.entries
.entries_val
[i
],
114 data
->result
= NIS_SUCCESS
;
118 result
= (char *) &bool_result
;
122 xdr_argument
= (xdrproc_t
) xdr_void
;
123 xdr_result
= (xdrproc_t
) xdr_void
;
124 memset (&argument
, 0, sizeof (argument
));
125 if (!svc_getargs (transp
, xdr_argument
, (caddr_t
) & argument
))
127 svcerr_decode (transp
);
131 data
->result
= NIS_SUCCESS
;
132 bool_result
= TRUE
; /* to make gcc happy, not necessary */
133 result
= (char *) &bool_result
;
136 xdr_argument
= (xdrproc_t
) xdr_nis_error
;
137 xdr_result
= (xdrproc_t
) xdr_void
;
138 memset (&argument
, 0, sizeof (argument
));
139 if (!svc_getargs (transp
, xdr_argument
, (caddr_t
) & argument
))
141 svcerr_decode (transp
);
145 data
->result
= argument
.cbproc_error_1_arg
;
146 bool_result
= TRUE
; /* to make gcc happy, not necessary */
147 result
= (char *) &bool_result
;
150 svcerr_noproc (transp
);
153 if (result
!= NULL
&& !svc_sendreply (transp
, xdr_result
, result
))
154 svcerr_systemerr (transp
);
155 if (!svc_freeargs (transp
, xdr_argument
, (caddr_t
) & argument
))
157 fputs (_ ("unable to free arguments"), stderr
);
164 xdr_obj_p (XDR
* xdrs
, obj_p
*objp
)
166 if (!xdr_pointer (xdrs
, (char **) objp
, sizeof (nis_object
),
167 (xdrproc_t
) xdr_nis_object
))
173 xdr_cback_data (XDR
*xdrs
, cback_data
*objp
)
175 if (!xdr_array (xdrs
, (char **) &objp
->entries
.entries_val
,
176 (u_int
*) & objp
->entries
.entries_len
, ~0, sizeof (obj_p
),
177 (xdrproc_t
) xdr_obj_p
))
183 internal_nis_do_callback (struct dir_binding
*bptr
, netobj
*cookie
,
186 /* Default timeout can be changed using clnt_control() */
187 static struct timeval TIMEOUT
= {25, 0};
192 #endif /* def FD_SETSIZE */
194 bool_t cb_is_running
= FALSE
;
204 #endif /* def FD_SETSIZE */
207 switch (select (_rpc_dtablesize (), &readfds
, NULL
, NULL
, &tv
))
214 /* See if callback 'thread' in the server is still alive. */
215 memset ((char *) &cb_is_running
, 0, sizeof (cb_is_running
));
216 if (clnt_call (bptr
->clnt
, NIS_CALLBACK
, (xdrproc_t
) xdr_netobj
,
217 (caddr_t
) cookie
, (xdrproc_t
) xdr_bool
,
218 (caddr_t
) & cb_is_running
, TIMEOUT
) != RPC_SUCCESS
)
219 cb_is_running
= FALSE
;
221 if (cb_is_running
== FALSE
)
223 syslog (LOG_ERR
, "NIS+: callback timed out");
228 svc_getreqset (&readfds
);
236 __nis_do_callback (struct dir_binding
*bptr
, netobj
*cookie
,
241 __libc_lock_lock (callback
);
243 result
= internal_nis_do_callback (bptr
, cookie
, cb
);
245 __libc_lock_unlock (callback
);
251 __nis_create_callback (int (*callback
) (const_nis_name
, const nis_object
*,
253 const void *userdata
, u_long flags
)
256 int sock
= RPC_ANYSOCK
;
257 struct sockaddr_in sin
;
258 int len
= sizeof (struct sockaddr_in
);
259 char addr
[NIS_MAXNAMELEN
+ 1];
262 cb
= (struct nis_cb
*) calloc (1, sizeof (struct nis_cb
));
265 syslog (LOG_ERR
, "NIS+: out of memory allocating callback");
269 cb
->serv
= (nis_server
*) calloc (1, sizeof (nis_server
));
270 if (cb
->serv
== NULL
)
273 syslog (LOG_ERR
, "NIS+: out of memory allocating callback");
276 cb
->serv
->name
= strdup (nis_local_host ());
277 cb
->serv
->ep
.ep_val
= (endpoint
*) calloc (2, sizeof (endpoint
));
278 cb
->serv
->ep
.ep_len
= 1;
279 cb
->serv
->ep
.ep_val
[0].family
= strdup ("inet");
280 cb
->callback
= callback
;
281 cb
->userdata
= userdata
;
283 /* XXX Sometimes, we should add the public key of the user here ! */
284 cb
->serv
->key_type
= NIS_PK_NONE
;
285 cb
->serv
->pkey
.n_bytes
= NULL
;
286 cb
->serv
->pkey
.n_len
= 0;
288 if (flags
& USE_DGRAM
)
290 cb
->serv
->ep
.ep_val
[0].proto
= strdup ("udp");
291 cb
->xprt
= svcudp_bufcreate (sock
, 100, 8192);
295 cb
->serv
->ep
.ep_val
[0].proto
= strdup ("tcp");
296 cb
->xprt
= svctcp_create (sock
, 100, 8192);
298 cb
->sock
= cb
->xprt
->xp_sock
;
299 if (!svc_register (cb
->xprt
, CB_PROG
, CB_VERS
, cb_prog_1
, 0))
301 xprt_unregister (cb
->xprt
);
302 svc_destroy (cb
->xprt
);
303 xdr_free ((xdrproc_t
) xdr_nis_server
, (char *) cb
->serv
);
306 syslog (LOG_ERR
, "NIS+: failed to register callback dispatcher");
310 if (getsockname (cb
->sock
, (struct sockaddr
*) &sin
, &len
) == -1)
312 xprt_unregister (cb
->xprt
);
313 svc_destroy (cb
->xprt
);
314 xdr_free ((xdrproc_t
) xdr_nis_server
, (char *) cb
->serv
);
317 syslog (LOG_ERR
, "NIS+: failed to read local socket info");
321 get_myaddress (&sin
);
322 snprintf (addr
, sizeof (addr
), "%s.%d.%d", inet_ntoa (sin
.sin_addr
),
323 port
& 0x00FF, (port
& 0xFF00) >> 8);
324 cb
->serv
->ep
.ep_val
[0].uaddr
= strdup (addr
);
330 __nis_destroy_callback (struct nis_cb
*cb
)
332 xprt_unregister (cb
->xprt
);
333 svc_destroy (cb
->xprt
);
335 xdr_free ((xdrproc_t
) xdr_nis_server
, (char *) cb
->serv
);