]> sourceware.org Git - glibc.git/blame - nis/nis_callback.c
Update.
[glibc.git] / nis / nis_callback.c
CommitLineData
bcccd5c0 1/* Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
650425ce 2 This file is part of the GNU C Library.
249fd241 3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
650425ce
UD
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>
4360eafd 24#include <libintl.h>
cc3fa755 25#include <rpc/rpc.h>
650425ce
UD
26#include <rpc/pmap_clnt.h>
27#include <string.h>
28#include <memory.h>
29#include <syslog.h>
249fd241 30#include <sys/poll.h>
650425ce
UD
31#include <sys/socket.h>
32#include <netinet/in.h>
33#include <arpa/inet.h>
26a60f90 34#include <rpc/key_prot.h>
650425ce 35#include <rpcsvc/nis.h>
91eee4dd 36#include <rpcsvc/nis_callback.h>
650425ce
UD
37#include <bits/libc-lock.h>
38
91eee4dd 39#include "nis_xdr.h"
650425ce
UD
40#include "nis_intern.h"
41
650425ce
UD
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 */
46
650425ce
UD
47static nis_cb *data;
48
49__libc_lock_define_initialized (static, callback)
50
26a60f90 51
91eee4dd 52#if 0
26a60f90
UD
53static char *
54__nis_getpkey(const char *sname)
55{
56 char buf[(strlen (sname) + 1) * 2 + 40];
57 char pkey[HEXKEYBYTES + 1];
58 char *cp, *domain;
59 nis_result *res;
a1129917 60 unsigned int len = 0;
26a60f90
UD
61
62 domain = strchr (sname, '.');
63 if (domain == NULL)
64 return NULL;
65
66 /* Remove prefixing dot */
67 ++domain;
68
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);
73
74 res = nis_list (buf, USE_DGRAM|NO_AUTHINFO|FOLLOW_LINKS|FOLLOW_PATH,
75 NULL, NULL);
76
77 if (res == NULL)
78 return NULL;
79
91eee4dd 80 if (NIS_RES_STATUS (res) != NIS_SUCCESS)
26a60f90
UD
81 {
82 nis_freeresult (res);
83 return NULL;
84 }
85
86 len = ENTRY_LEN(NIS_RES_OBJECT(res), 3);
87 strncpy (pkey, ENTRY_VAL(NIS_RES_OBJECT(res), 3), len);
88 pkey[len] = '\0';
89 cp = strchr (pkey, ':');
90 if (cp != NULL)
91 *cp = '\0';
92
93 nis_freeresult (res);
94
95 return strdup (pkey);
96}
91eee4dd 97#endif
650425ce
UD
98
99static void
100cb_prog_1 (struct svc_req *rqstp, SVCXPRT *transp)
101{
102 union
103 {
104 cback_data cbproc_receive_1_arg;
105 nis_error cbproc_error_1_arg;
106 }
107 argument;
108 char *result;
109 xdrproc_t xdr_argument, xdr_result;
110 bool_t bool_result;
111
112 switch (rqstp->rq_proc)
113 {
114 case NULLPROC:
26a60f90 115 svc_sendreply (transp, (xdrproc_t) xdr_void, (char *) NULL);
650425ce
UD
116 return;
117
118 case CBPROC_RECEIVE:
119 {
a1129917 120 unsigned int i;
650425ce
UD
121
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))
126 {
127 svcerr_decode (transp);
128 return;
129 }
130 bool_result = FALSE;
131 for (i = 0; i < argument.cbproc_receive_1_arg.entries.entries_len; ++i)
132 {
26a60f90
UD
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];
136 char *cp;
137
138 cp = stpcpy (name, cbproc_entry(i)->zo_name);
139 *cp++ = '.';
140 cp = stpcpy (cp, cbproc_entry(i)->zo_domain);
650425ce 141
26a60f90 142 if ((data->callback) (name, cbproc_entry(i), data->userdata))
650425ce
UD
143 {
144 bool_result = TRUE;
145 data->nomore = 1;
146 data->result = NIS_SUCCESS;
147 break;
148 }
149 }
150 result = (char *) &bool_result;
151 }
152 break;
153 case CBPROC_FINISH:
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))
158 {
159 svcerr_decode (transp);
160 return;
161 }
162 data->nomore = 1;
163 data->result = NIS_SUCCESS;
164 bool_result = TRUE; /* to make gcc happy, not necessary */
165 result = (char *) &bool_result;
166 break;
167 case CBPROC_ERROR:
91eee4dd 168 xdr_argument = (xdrproc_t) _xdr_nis_error;
650425ce
UD
169 xdr_result = (xdrproc_t) xdr_void;
170 memset (&argument, 0, sizeof (argument));
171 if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
172 {
173 svcerr_decode (transp);
174 return;
175 }
176 data->nomore = 1;
177 data->result = argument.cbproc_error_1_arg;
178 bool_result = TRUE; /* to make gcc happy, not necessary */
179 result = (char *) &bool_result;
180 break;
181 default:
182 svcerr_noproc (transp);
183 return;
184 }
185 if (result != NULL && !svc_sendreply (transp, xdr_result, result))
186 svcerr_systemerr (transp);
187 if (!svc_freeargs (transp, xdr_argument, (caddr_t) & argument))
188 {
189 fputs (_ ("unable to free arguments"), stderr);
190 exit (1);
191 }
192 return;
193}
194
650425ce
UD
195static nis_error
196internal_nis_do_callback (struct dir_binding *bptr, netobj *cookie,
197 struct nis_cb *cb)
198{
249fd241 199 struct timeval TIMEOUT = {25, 0};
650425ce
UD
200 bool_t cb_is_running = FALSE;
201
202 data = cb;
203
204 for (;;)
205 {
249fd241
UD
206 struct pollfd *my_pollfd;
207 int i;
208
6d5728c8 209 if (svc_max_pollfd == 0 && svc_pollfd == NULL)
249fd241
UD
210 return NIS_CBERROR;
211
6d5728c8
UD
212 my_pollfd = malloc (sizeof (struct pollfd) * svc_max_pollfd);
213 if (__builtin_expect (my_pollfd == NULL, 0))
214 return NIS_NOMEMORY;
bcccd5c0 215
249fd241
UD
216 for (i = 0; i < svc_max_pollfd; ++i)
217 {
218 my_pollfd[i].fd = svc_pollfd[i].fd;
219 my_pollfd[i].events = svc_pollfd[i].events;
220 my_pollfd[i].revents = 0;
221 }
222
6d5728c8 223 switch (i = __poll (my_pollfd, svc_max_pollfd, 25*1000))
249fd241 224 {
650425ce 225 case -1:
6d5728c8 226 free (my_pollfd);
650425ce
UD
227 if (errno == EINTR)
228 continue;
229 return NIS_CBERROR;
230 case 0:
6d5728c8 231 free (my_pollfd);
650425ce
UD
232 /* See if callback 'thread' in the server is still alive. */
233 memset ((char *) &cb_is_running, 0, sizeof (cb_is_running));
234 if (clnt_call (bptr->clnt, NIS_CALLBACK, (xdrproc_t) xdr_netobj,
235 (caddr_t) cookie, (xdrproc_t) xdr_bool,
236 (caddr_t) & cb_is_running, TIMEOUT) != RPC_SUCCESS)
237 cb_is_running = FALSE;
238
239 if (cb_is_running == FALSE)
240 {
241 syslog (LOG_ERR, "NIS+: callback timed out");
242 return NIS_CBERROR;
243 }
244 break;
245 default:
249fd241 246 svc_getreq_poll (my_pollfd, i);
6d5728c8 247 free (my_pollfd);
650425ce
UD
248 if (data->nomore)
249 return data->result;
250 }
251 }
252}
253
254nis_error
255__nis_do_callback (struct dir_binding *bptr, netobj *cookie,
256 struct nis_cb *cb)
257{
258 nis_error result;
259
260 __libc_lock_lock (callback);
261
262 result = internal_nis_do_callback (bptr, cookie, cb);
263
264 __libc_lock_unlock (callback);
265
266 return result;
267}
268
269struct nis_cb *
270__nis_create_callback (int (*callback) (const_nis_name, const nis_object *,
271 const void *),
a1129917 272 const void *userdata, unsigned int flags)
650425ce
UD
273{
274 struct nis_cb *cb;
275 int sock = RPC_ANYSOCK;
276 struct sockaddr_in sin;
277 int len = sizeof (struct sockaddr_in);
278 char addr[NIS_MAXNAMELEN + 1];
279 unsigned short port;
280
281 cb = (struct nis_cb *) calloc (1, sizeof (struct nis_cb));
316518d6
UD
282 if (__builtin_expect (cb == NULL, 0))
283 goto failed;
650425ce 284 cb->serv = (nis_server *) calloc (1, sizeof (nis_server));
bcccd5c0 285 if (__builtin_expect (cb->serv == NULL, 0))
316518d6 286 goto failed;
26a60f90 287 cb->serv->name = strdup (nis_local_principal ());
bcccd5c0 288 if (__builtin_expect (cb->serv->name == NULL, 0))
316518d6 289 goto failed;
650425ce 290 cb->serv->ep.ep_val = (endpoint *) calloc (2, sizeof (endpoint));
bcccd5c0 291 if (__builtin_expect (cb->serv->ep.ep_val == NULL, 0))
316518d6 292 goto failed;
650425ce
UD
293 cb->serv->ep.ep_len = 1;
294 cb->serv->ep.ep_val[0].family = strdup ("inet");
bcccd5c0 295 if (__builtin_expect (cb->serv->ep.ep_val[0].family == NULL, 0))
316518d6 296 goto failed;
650425ce
UD
297 cb->callback = callback;
298 cb->userdata = userdata;
299
91eee4dd 300 if ((flags & NO_AUTHINFO) || !key_secretkey_is_set ())
26a60f90
UD
301 {
302 cb->serv->key_type = NIS_PK_NONE;
303 cb->serv->pkey.n_bytes = NULL;
304 cb->serv->pkey.n_len = 0;
305 }
306 else
307 {
91eee4dd 308#if 0
26a60f90
UD
309 if ((cb->serv->pkey.n_bytes = __nis_getpkey (cb->serv->name)) == NULL)
310 {
311 cb->serv->pkey.n_len = 0;
312 cb->serv->key_type = NIS_PK_NONE;
313 }
314 else
315 {
316 cb->serv->key_type = NIS_PK_DH;
317 cb->serv->pkey.n_len = strlen(cb->serv->pkey.n_bytes);
318 }
91eee4dd
UD
319#else
320 cb->serv->pkey.n_len =0;
321 cb->serv->pkey.n_bytes = NULL;
a53bad16 322 cb->serv->key_type = NIS_PK_NONE;
91eee4dd 323#endif
26a60f90 324 }
650425ce 325
316518d6 326 cb->serv->ep.ep_val[0].proto = strdup ((flags & USE_DGRAM) ? "udp" : "tcp");
bcccd5c0 327 if (__builtin_expect (cb->serv->ep.ep_val[0].proto == NULL, 0))
316518d6
UD
328 goto failed;
329 cb->xprt = (flags & USE_DGRAM) ? svcudp_bufcreate (sock, 100, 8192) :
330 svctcp_create (sock, 100, 8192);
650425ce
UD
331 cb->sock = cb->xprt->xp_sock;
332 if (!svc_register (cb->xprt, CB_PROG, CB_VERS, cb_prog_1, 0))
333 {
334 xprt_unregister (cb->xprt);
335 svc_destroy (cb->xprt);
91eee4dd 336 xdr_free ((xdrproc_t) _xdr_nis_server, (char *) cb->serv);
650425ce
UD
337 free (cb->serv);
338 free (cb);
339 syslog (LOG_ERR, "NIS+: failed to register callback dispatcher");
340 return NULL;
341 }
342
343 if (getsockname (cb->sock, (struct sockaddr *) &sin, &len) == -1)
344 {
345 xprt_unregister (cb->xprt);
346 svc_destroy (cb->xprt);
91eee4dd 347 xdr_free ((xdrproc_t) _xdr_nis_server, (char *) cb->serv);
650425ce
UD
348 free (cb->serv);
349 free (cb);
350 syslog (LOG_ERR, "NIS+: failed to read local socket info");
91eee4dd 351 return NULL;
650425ce 352 }
d6f6ffa1 353 port = ntohs (sin.sin_port);
650425ce
UD
354 get_myaddress (&sin);
355 snprintf (addr, sizeof (addr), "%s.%d.%d", inet_ntoa (sin.sin_addr),
d6f6ffa1 356 (port & 0xFF00) >> 8, port & 0x00FF);
650425ce
UD
357 cb->serv->ep.ep_val[0].uaddr = strdup (addr);
358
359 return cb;
316518d6
UD
360
361 failed:
362 if (cb)
363 {
364 if (cb->serv)
365 {
366 xdr_free ((xdrproc_t) _xdr_nis_server, (char *) cb->serv);
367 free (cb->serv);
368 }
369 free (cb);
370 }
371 syslog (LOG_ERR, "NIS+: out of memory allocating callback");
372 return NULL;
650425ce
UD
373}
374
375nis_error
376__nis_destroy_callback (struct nis_cb *cb)
377{
378 xprt_unregister (cb->xprt);
379 svc_destroy (cb->xprt);
380 close (cb->sock);
91eee4dd 381 xdr_free ((xdrproc_t) _xdr_nis_server, (char *) cb->serv);
650425ce
UD
382 free (cb->serv);
383 free (cb);
384
385 return NIS_SUCCESS;
386}
This page took 0.178517 seconds and 5 git commands to generate.