Update.
[platform/upstream/glibc.git] / nis / nis_callback.c
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.
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 <rpc/key_prot.h>
33 #include <rpcsvc/nis.h>
34 #include <rpcsvc/nis_callback.h>
35 #include <bits/libc-lock.h>
36
37 #include "nis_xdr.h"
38 #include "nis_intern.h"
39
40 /* Sorry, we are not able to make this threadsafe. Stupid. But some
41    functions doesn't send us a nis_result obj, so we don't have a
42    cookie. Maybe we could use keys for threads ? Have to learn more
43    about pthreads -- kukuk@vt.uni-paderborn.de */
44
45 static nis_cb *data;
46
47 __libc_lock_define_initialized (static, callback)
48
49
50 #if 0
51 static char *
52 __nis_getpkey(const char *sname)
53 {
54   char buf[(strlen (sname) + 1) * 2 + 40];
55   char pkey[HEXKEYBYTES + 1];
56   char *cp, *domain;
57   nis_result *res;
58   u_int len = 0;
59
60   domain = strchr (sname, '.');
61   if (domain == NULL)
62     return NULL;
63
64   /* Remove prefixing dot */
65   ++domain;
66
67   cp = stpcpy (buf, "[cname=");
68   cp = stpcpy (cp, sname);
69   cp = stpcpy (cp, ",auth_type=DES],cred.org_dir.");
70   cp = stpcpy (cp, domain);
71
72   res = nis_list (buf, USE_DGRAM|NO_AUTHINFO|FOLLOW_LINKS|FOLLOW_PATH,
73                   NULL, NULL);
74
75   if (res == NULL)
76     return NULL;
77
78   if (NIS_RES_STATUS (res) != NIS_SUCCESS)
79     {
80       nis_freeresult (res);
81       return NULL;
82     }
83
84   len = ENTRY_LEN(NIS_RES_OBJECT(res), 3);
85   strncpy (pkey, ENTRY_VAL(NIS_RES_OBJECT(res), 3), len);
86   pkey[len] = '\0';
87   cp = strchr (pkey, ':');
88   if (cp != NULL)
89     *cp = '\0';
90
91   nis_freeresult (res);
92
93   return strdup (pkey);
94 }
95 #endif
96
97 static void
98 cb_prog_1 (struct svc_req *rqstp, SVCXPRT *transp)
99 {
100   union
101     {
102       cback_data cbproc_receive_1_arg;
103       nis_error cbproc_error_1_arg;
104     }
105   argument;
106   char *result;
107   xdrproc_t xdr_argument, xdr_result;
108   bool_t bool_result;
109
110   switch (rqstp->rq_proc)
111     {
112     case NULLPROC:
113       svc_sendreply (transp, (xdrproc_t) xdr_void, (char *) NULL);
114       return;
115
116     case CBPROC_RECEIVE:
117       {
118         u_long i;
119
120         xdr_argument = (xdrproc_t) xdr_cback_data;
121         xdr_result = (xdrproc_t) xdr_bool;
122         memset (&argument, 0, sizeof (argument));
123         if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
124           {
125             svcerr_decode (transp);
126             return;
127           }
128         bool_result = FALSE;
129         for (i = 0; i < argument.cbproc_receive_1_arg.entries.entries_len; ++i)
130           {
131 #define cbproc_entry(a) argument.cbproc_receive_1_arg.entries.entries_val[a]
132             char name[strlen (cbproc_entry(i)->zo_name) +
133                       strlen (cbproc_entry(i)->zo_domain) + 3];
134             char *cp;
135
136             cp = stpcpy (name, cbproc_entry(i)->zo_name);
137             *cp++ = '.';
138             cp = stpcpy (cp, cbproc_entry(i)->zo_domain);
139
140             if ((data->callback) (name, cbproc_entry(i), data->userdata))
141               {
142                 bool_result = TRUE;
143                 data->nomore = 1;
144                 data->result = NIS_SUCCESS;
145                 break;
146               }
147           }
148         result = (char *) &bool_result;
149       }
150       break;
151     case CBPROC_FINISH:
152       xdr_argument = (xdrproc_t) xdr_void;
153       xdr_result = (xdrproc_t) xdr_void;
154       memset (&argument, 0, sizeof (argument));
155       if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
156         {
157           svcerr_decode (transp);
158           return;
159         }
160       data->nomore = 1;
161       data->result = NIS_SUCCESS;
162       bool_result = TRUE;       /* to make gcc happy, not necessary */
163       result = (char *) &bool_result;
164       break;
165     case CBPROC_ERROR:
166       xdr_argument = (xdrproc_t) _xdr_nis_error;
167       xdr_result = (xdrproc_t) xdr_void;
168       memset (&argument, 0, sizeof (argument));
169       if (!svc_getargs (transp, xdr_argument, (caddr_t) & argument))
170         {
171           svcerr_decode (transp);
172           return;
173         }
174       data->nomore = 1;
175       data->result = argument.cbproc_error_1_arg;
176       bool_result = TRUE;       /* to make gcc happy, not necessary */
177       result = (char *) &bool_result;
178       break;
179     default:
180       svcerr_noproc (transp);
181       return;
182     }
183   if (result != NULL && !svc_sendreply (transp, xdr_result, result))
184     svcerr_systemerr (transp);
185   if (!svc_freeargs (transp, xdr_argument, (caddr_t) & argument))
186     {
187       fputs (_ ("unable to free arguments"), stderr);
188       exit (1);
189     }
190   return;
191 }
192
193 static nis_error
194 internal_nis_do_callback (struct dir_binding *bptr, netobj *cookie,
195                           struct nis_cb *cb)
196 {
197   /* Default timeout can be changed using clnt_control() */
198   static struct timeval TIMEOUT = {25, 0};
199 #ifdef FD_SETSIZE
200   fd_set readfds;
201 #else
202   int readfds;
203 #endif /* def FD_SETSIZE */
204   struct timeval tv;
205   bool_t cb_is_running = FALSE;
206
207   data = cb;
208
209   for (;;)
210     {
211 #ifdef FD_SETSIZE
212       readfds = svc_fdset;
213 #else
214       readfds = svc_fds;
215 #endif /* def FD_SETSIZE */
216       tv.tv_sec = 25;
217       tv.tv_usec = 0;
218       switch (select (_rpc_dtablesize (), &readfds, NULL, NULL, &tv))
219         {
220         case -1:
221           if (errno == EINTR)
222             continue;
223           return NIS_CBERROR;
224         case 0:
225           /* See if callback 'thread' in the server is still alive. */
226           memset ((char *) &cb_is_running, 0, sizeof (cb_is_running));
227           if (clnt_call (bptr->clnt, NIS_CALLBACK, (xdrproc_t) xdr_netobj,
228                          (caddr_t) cookie, (xdrproc_t) xdr_bool,
229                          (caddr_t) & cb_is_running, TIMEOUT) != RPC_SUCCESS)
230             cb_is_running = FALSE;
231
232           if (cb_is_running == FALSE)
233             {
234               syslog (LOG_ERR, "NIS+: callback timed out");
235               return NIS_CBERROR;
236             }
237           break;
238         default:
239           svc_getreqset (&readfds);
240           if (data->nomore)
241             return data->result;
242         }
243     }
244 }
245
246 nis_error
247 __nis_do_callback (struct dir_binding *bptr, netobj *cookie,
248                    struct nis_cb *cb)
249 {
250   nis_error result;
251
252   __libc_lock_lock (callback);
253
254   result = internal_nis_do_callback (bptr, cookie, cb);
255
256   __libc_lock_unlock (callback);
257
258   return result;
259 }
260
261 struct nis_cb *
262 __nis_create_callback (int (*callback) (const_nis_name, const nis_object *,
263                                         const void *),
264                        const void *userdata, u_long flags)
265 {
266   struct nis_cb *cb;
267   int sock = RPC_ANYSOCK;
268   struct sockaddr_in sin;
269   int len = sizeof (struct sockaddr_in);
270   char addr[NIS_MAXNAMELEN + 1];
271   unsigned short port;
272
273   cb = (struct nis_cb *) calloc (1, sizeof (struct nis_cb));
274   if (cb == NULL)
275     {
276       syslog (LOG_ERR, "NIS+: out of memory allocating callback");
277       return NULL;
278     }
279
280   cb->serv = (nis_server *) calloc (1, sizeof (nis_server));
281   if (cb->serv == NULL)
282     {
283       free (cb);
284       syslog (LOG_ERR, "NIS+: out of memory allocating callback");
285       return NULL;
286     }
287   cb->serv->name = strdup (nis_local_principal ());
288   cb->serv->ep.ep_val = (endpoint *) calloc (2, sizeof (endpoint));
289   cb->serv->ep.ep_len = 1;
290   cb->serv->ep.ep_val[0].family = strdup ("inet");
291   cb->callback = callback;
292   cb->userdata = userdata;
293
294   if ((flags & NO_AUTHINFO) || !key_secretkey_is_set ())
295     {
296       cb->serv->key_type = NIS_PK_NONE;
297       cb->serv->pkey.n_bytes = NULL;
298       cb->serv->pkey.n_len = 0;
299     }
300   else
301     {
302 #if 0
303       if ((cb->serv->pkey.n_bytes = __nis_getpkey (cb->serv->name)) == NULL)
304         {
305           cb->serv->pkey.n_len = 0;
306           cb->serv->key_type = NIS_PK_NONE;
307         }
308       else
309         {
310           cb->serv->key_type = NIS_PK_DH;
311           cb->serv->pkey.n_len = strlen(cb->serv->pkey.n_bytes);
312         }
313 #else
314       cb->serv->pkey.n_len =0;
315       cb->serv->pkey.n_bytes = NULL;
316       cb->serv->key_type = NIS_PK_DH;
317 #endif
318     }
319
320   if (flags & USE_DGRAM)
321     {
322       cb->serv->ep.ep_val[0].proto = strdup ("udp");
323       cb->xprt = svcudp_bufcreate (sock, 100, 8192);
324     }
325   else
326     {
327       cb->serv->ep.ep_val[0].proto = strdup ("tcp");
328       cb->xprt = svctcp_create (sock, 100, 8192);
329     }
330   cb->sock = cb->xprt->xp_sock;
331   if (!svc_register (cb->xprt, CB_PROG, CB_VERS, cb_prog_1, 0))
332     {
333       xprt_unregister (cb->xprt);
334       svc_destroy (cb->xprt);
335       xdr_free ((xdrproc_t) _xdr_nis_server, (char *) cb->serv);
336       free (cb->serv);
337       free (cb);
338       syslog (LOG_ERR, "NIS+: failed to register callback dispatcher");
339       return NULL;
340     }
341
342   if (getsockname (cb->sock, (struct sockaddr *) &sin, &len) == -1)
343     {
344       xprt_unregister (cb->xprt);
345       svc_destroy (cb->xprt);
346       xdr_free ((xdrproc_t) _xdr_nis_server, (char *) cb->serv);
347       free (cb->serv);
348       free (cb);
349       syslog (LOG_ERR, "NIS+: failed to read local socket info");
350       return NULL;
351     }
352   port = sin.sin_port;
353   get_myaddress (&sin);
354   snprintf (addr, sizeof (addr), "%s.%d.%d", inet_ntoa (sin.sin_addr),
355             port & 0x00FF, (port & 0xFF00) >> 8);
356   cb->serv->ep.ep_val[0].uaddr = strdup (addr);
357
358   return cb;
359 }
360
361 nis_error
362 __nis_destroy_callback (struct nis_cb *cb)
363 {
364   xprt_unregister (cb->xprt);
365   svc_destroy (cb->xprt);
366   close (cb->sock);
367   xdr_free ((xdrproc_t) _xdr_nis_server, (char *) cb->serv);
368   free (cb->serv);
369   free (cb);
370
371   return NIS_SUCCESS;
372 }