]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nis_callback.c
Update.
[thirdparty/glibc.git] / nis / nis_callback.c
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.
4
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.
9
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.
14
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. */
19
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 <rpcsvc/nis.h>
33 #include <bits/libc-lock.h>
34
35 #include "nis_intern.h"
36
37 extern void get_myaddress (struct sockaddr_in *addr);
38
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 */
43
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)
49
50 typedef nis_object *obj_p;
51
52 struct cback_data
53 {
54 struct
55 {
56 u_int entries_len;
57 obj_p *entries_val;
58 }
59 entries;
60 };
61 typedef struct cback_data cback_data;
62
63 static nis_cb *data;
64
65 __libc_lock_define_initialized (static, callback)
66
67 static bool_t xdr_cback_data (XDR *, cback_data *);
68
69 static void
70 cb_prog_1 (struct svc_req *rqstp, SVCXPRT *transp)
71 {
72 union
73 {
74 cback_data cbproc_receive_1_arg;
75 nis_error cbproc_error_1_arg;
76 }
77 argument;
78 char *result;
79 xdrproc_t xdr_argument, xdr_result;
80 bool_t bool_result;
81
82 switch (rqstp->rq_proc)
83 {
84 case NULLPROC:
85 (void) svc_sendreply (transp, (xdrproc_t) xdr_void, (char *) NULL);
86 return;
87
88 case CBPROC_RECEIVE:
89 {
90 char name[NIS_MAXNAMELEN + 1];
91 u_long i;
92
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))
97 {
98 svcerr_decode (transp);
99 return;
100 }
101 bool_result = FALSE;
102 for (i = 0; i < argument.cbproc_receive_1_arg.entries.entries_len; ++i)
103 {
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);
107
108 if ((data->callback)
109 (name, argument.cbproc_receive_1_arg.entries.entries_val[i],
110 data->userdata))
111 {
112 bool_result = TRUE;
113 data->nomore = 1;
114 data->result = NIS_SUCCESS;
115 break;
116 }
117 }
118 result = (char *) &bool_result;
119 }
120 break;
121 case CBPROC_FINISH:
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))
126 {
127 svcerr_decode (transp);
128 return;
129 }
130 data->nomore = 1;
131 data->result = NIS_SUCCESS;
132 bool_result = TRUE; /* to make gcc happy, not necessary */
133 result = (char *) &bool_result;
134 break;
135 case CBPROC_ERROR:
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))
140 {
141 svcerr_decode (transp);
142 return;
143 }
144 data->nomore = 1;
145 data->result = argument.cbproc_error_1_arg;
146 bool_result = TRUE; /* to make gcc happy, not necessary */
147 result = (char *) &bool_result;
148 break;
149 default:
150 svcerr_noproc (transp);
151 return;
152 }
153 if (result != NULL && !svc_sendreply (transp, xdr_result, result))
154 svcerr_systemerr (transp);
155 if (!svc_freeargs (transp, xdr_argument, (caddr_t) & argument))
156 {
157 fputs (_ ("unable to free arguments"), stderr);
158 exit (1);
159 }
160 return;
161 }
162
163 static bool_t
164 xdr_obj_p (XDR * xdrs, obj_p *objp)
165 {
166 if (!xdr_pointer (xdrs, (char **) objp, sizeof (nis_object),
167 (xdrproc_t) xdr_nis_object))
168 return FALSE;
169 return TRUE;
170 }
171
172 static bool_t
173 xdr_cback_data (XDR *xdrs, cback_data *objp)
174 {
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))
178 return FALSE;
179 return TRUE;
180 }
181
182 static nis_error
183 internal_nis_do_callback (struct dir_binding *bptr, netobj *cookie,
184 struct nis_cb *cb)
185 {
186 /* Default timeout can be changed using clnt_control() */
187 static struct timeval TIMEOUT = {25, 0};
188 #ifdef FD_SETSIZE
189 fd_set readfds;
190 #else
191 int readfds;
192 #endif /* def FD_SETSIZE */
193 struct timeval tv;
194 bool_t cb_is_running = FALSE;
195
196 data = cb;
197
198 for (;;)
199 {
200 #ifdef FD_SETSIZE
201 readfds = svc_fdset;
202 #else
203 readfds = svc_fds;
204 #endif /* def FD_SETSIZE */
205 tv.tv_sec = 25;
206 tv.tv_usec = 0;
207 switch (select (_rpc_dtablesize (), &readfds, NULL, NULL, &tv))
208 {
209 case -1:
210 if (errno == EINTR)
211 continue;
212 return NIS_CBERROR;
213 case 0:
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;
220
221 if (cb_is_running == FALSE)
222 {
223 syslog (LOG_ERR, "NIS+: callback timed out");
224 return NIS_CBERROR;
225 }
226 break;
227 default:
228 svc_getreqset (&readfds);
229 if (data->nomore)
230 return data->result;
231 }
232 }
233 }
234
235 nis_error
236 __nis_do_callback (struct dir_binding *bptr, netobj *cookie,
237 struct nis_cb *cb)
238 {
239 nis_error result;
240
241 __libc_lock_lock (callback);
242
243 result = internal_nis_do_callback (bptr, cookie, cb);
244
245 __libc_lock_unlock (callback);
246
247 return result;
248 }
249
250 struct nis_cb *
251 __nis_create_callback (int (*callback) (const_nis_name, const nis_object *,
252 const void *),
253 const void *userdata, u_long flags)
254 {
255 struct nis_cb *cb;
256 int sock = RPC_ANYSOCK;
257 struct sockaddr_in sin;
258 int len = sizeof (struct sockaddr_in);
259 char addr[NIS_MAXNAMELEN + 1];
260 unsigned short port;
261
262 cb = (struct nis_cb *) calloc (1, sizeof (struct nis_cb));
263 if (cb == NULL)
264 {
265 syslog (LOG_ERR, "NIS+: out of memory allocating callback");
266 return NULL;
267 }
268
269 cb->serv = (nis_server *) calloc (1, sizeof (nis_server));
270 if (cb->serv == NULL)
271 {
272 free (cb);
273 syslog (LOG_ERR, "NIS+: out of memory allocating callback");
274 return (NULL);
275 }
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;
282
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;
287
288 if (flags & USE_DGRAM)
289 {
290 cb->serv->ep.ep_val[0].proto = strdup ("udp");
291 cb->xprt = svcudp_bufcreate (sock, 100, 8192);
292 }
293 else
294 {
295 cb->serv->ep.ep_val[0].proto = strdup ("tcp");
296 cb->xprt = svctcp_create (sock, 100, 8192);
297 }
298 cb->sock = cb->xprt->xp_sock;
299 if (!svc_register (cb->xprt, CB_PROG, CB_VERS, cb_prog_1, 0))
300 {
301 xprt_unregister (cb->xprt);
302 svc_destroy (cb->xprt);
303 xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);
304 free (cb->serv);
305 free (cb);
306 syslog (LOG_ERR, "NIS+: failed to register callback dispatcher");
307 return NULL;
308 }
309
310 if (getsockname (cb->sock, (struct sockaddr *) &sin, &len) == -1)
311 {
312 xprt_unregister (cb->xprt);
313 svc_destroy (cb->xprt);
314 xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);
315 free (cb->serv);
316 free (cb);
317 syslog (LOG_ERR, "NIS+: failed to read local socket info");
318 return (NULL);
319 }
320 port = sin.sin_port;
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);
325
326 return cb;
327 }
328
329 nis_error
330 __nis_destroy_callback (struct nis_cb *cb)
331 {
332 xprt_unregister (cb->xprt);
333 svc_destroy (cb->xprt);
334 close (cb->sock);
335 xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);
336 free (cb->serv);
337 free (cb);
338
339 return NIS_SUCCESS;
340 }