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 <bits/libc-lock.h>
35
36 #include "nis_intern.h"
37
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 */
42
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)
48
49 typedef nis_object *obj_p;
50
51 struct cback_data
52   {
53     struct
54       {
55         u_int entries_len;
56         obj_p *entries_val;
57       }
58     entries;
59   };
60 typedef struct cback_data cback_data;
61
62 static nis_cb *data;
63
64 __libc_lock_define_initialized (static, callback)
65
66
67 static char *
68 __nis_getpkey(const char *sname)
69 {
70   char buf[(strlen (sname) + 1) * 2 + 40];
71   char pkey[HEXKEYBYTES + 1];
72   char *cp, *domain;
73   nis_result *res;
74   u_int len = 0;
75
76   domain = strchr (sname, '.');
77   if (domain == NULL)
78     return NULL;
79
80   /* Remove prefixing dot */
81   ++domain;
82
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);
87
88   res = nis_list (buf, USE_DGRAM|NO_AUTHINFO|FOLLOW_LINKS|FOLLOW_PATH,
89                   NULL, NULL);
90
91   if (res == NULL)
92     return NULL;
93
94   if (res->status != NIS_SUCCESS)
95     {
96       nis_freeresult (res);
97       return NULL;
98     }
99
100   len = ENTRY_LEN(NIS_RES_OBJECT(res), 3);
101   strncpy (pkey, ENTRY_VAL(NIS_RES_OBJECT(res), 3), len);
102   pkey[len] = '\0';
103   cp = strchr (pkey, ':');
104   if (cp != NULL)
105     *cp = '\0';
106
107   nis_freeresult (res);
108
109   return strdup (pkey);
110 }
111
112
113 static bool_t xdr_cback_data (XDR *, cback_data *);
114
115 static void
116 cb_prog_1 (struct svc_req *rqstp, SVCXPRT *transp)
117 {
118   union
119     {
120       cback_data cbproc_receive_1_arg;
121       nis_error cbproc_error_1_arg;
122     }
123   argument;
124   char *result;
125   xdrproc_t xdr_argument, xdr_result;
126   bool_t bool_result;
127
128   switch (rqstp->rq_proc)
129     {
130     case NULLPROC:
131       svc_sendreply (transp, (xdrproc_t) xdr_void, (char *) NULL);
132       return;
133
134     case CBPROC_RECEIVE:
135       {
136         u_long i;
137
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))
142           {
143             svcerr_decode (transp);
144             return;
145           }
146         bool_result = FALSE;
147         for (i = 0; i < argument.cbproc_receive_1_arg.entries.entries_len; ++i)
148           {
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];
152             char *cp;
153
154             cp = stpcpy (name, cbproc_entry(i)->zo_name);
155             *cp++ = '.';
156             cp = stpcpy (cp, cbproc_entry(i)->zo_domain);
157
158             if ((data->callback) (name, cbproc_entry(i), data->userdata))
159               {
160                 bool_result = TRUE;
161                 data->nomore = 1;
162                 data->result = NIS_SUCCESS;
163                 break;
164               }
165           }
166         result = (char *) &bool_result;
167       }
168       break;
169     case CBPROC_FINISH:
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))
174         {
175           svcerr_decode (transp);
176           return;
177         }
178       data->nomore = 1;
179       data->result = NIS_SUCCESS;
180       bool_result = TRUE;       /* to make gcc happy, not necessary */
181       result = (char *) &bool_result;
182       break;
183     case CBPROC_ERROR:
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))
188         {
189           svcerr_decode (transp);
190           return;
191         }
192       data->nomore = 1;
193       data->result = argument.cbproc_error_1_arg;
194       bool_result = TRUE;       /* to make gcc happy, not necessary */
195       result = (char *) &bool_result;
196       break;
197     default:
198       svcerr_noproc (transp);
199       return;
200     }
201   if (result != NULL && !svc_sendreply (transp, xdr_result, result))
202     svcerr_systemerr (transp);
203   if (!svc_freeargs (transp, xdr_argument, (caddr_t) & argument))
204     {
205       fputs (_ ("unable to free arguments"), stderr);
206       exit (1);
207     }
208   return;
209 }
210
211 static bool_t
212 xdr_obj_p (XDR * xdrs, obj_p *objp)
213 {
214   if (!xdr_pointer (xdrs, (char **) objp, sizeof (nis_object),
215                     (xdrproc_t) xdr_nis_object))
216     return FALSE;
217   return TRUE;
218 }
219
220 static bool_t
221 xdr_cback_data (XDR *xdrs, cback_data *objp)
222 {
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))
226     return FALSE;
227   return TRUE;
228 }
229
230 static nis_error
231 internal_nis_do_callback (struct dir_binding *bptr, netobj *cookie,
232                           struct nis_cb *cb)
233 {
234   /* Default timeout can be changed using clnt_control() */
235   static struct timeval TIMEOUT = {25, 0};
236 #ifdef FD_SETSIZE
237   fd_set readfds;
238 #else
239   int readfds;
240 #endif /* def FD_SETSIZE */
241   struct timeval tv;
242   bool_t cb_is_running = FALSE;
243
244   data = cb;
245
246   for (;;)
247     {
248 #ifdef FD_SETSIZE
249       readfds = svc_fdset;
250 #else
251       readfds = svc_fds;
252 #endif /* def FD_SETSIZE */
253       tv.tv_sec = 25;
254       tv.tv_usec = 0;
255       switch (select (_rpc_dtablesize (), &readfds, NULL, NULL, &tv))
256         {
257         case -1:
258           if (errno == EINTR)
259             continue;
260           return NIS_CBERROR;
261         case 0:
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;
268
269           if (cb_is_running == FALSE)
270             {
271               syslog (LOG_ERR, "NIS+: callback timed out");
272               return NIS_CBERROR;
273             }
274           break;
275         default:
276           svc_getreqset (&readfds);
277           if (data->nomore)
278             return data->result;
279         }
280     }
281 }
282
283 nis_error
284 __nis_do_callback (struct dir_binding *bptr, netobj *cookie,
285                    struct nis_cb *cb)
286 {
287   nis_error result;
288
289   __libc_lock_lock (callback);
290
291   result = internal_nis_do_callback (bptr, cookie, cb);
292
293   __libc_lock_unlock (callback);
294
295   return result;
296 }
297
298 struct nis_cb *
299 __nis_create_callback (int (*callback) (const_nis_name, const nis_object *,
300                                         const void *),
301                        const void *userdata, u_long flags)
302 {
303   struct nis_cb *cb;
304   int sock = RPC_ANYSOCK;
305   struct sockaddr_in sin;
306   int len = sizeof (struct sockaddr_in);
307   char addr[NIS_MAXNAMELEN + 1];
308   unsigned short port;
309
310   cb = (struct nis_cb *) calloc (1, sizeof (struct nis_cb));
311   if (cb == NULL)
312     {
313       syslog (LOG_ERR, "NIS+: out of memory allocating callback");
314       return NULL;
315     }
316
317   cb->serv = (nis_server *) calloc (1, sizeof (nis_server));
318   if (cb->serv == NULL)
319     {
320       free (cb);
321       syslog (LOG_ERR, "NIS+: out of memory allocating callback");
322       return (NULL);
323     }
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;
330
331   if ((flags & NO_AUTHINFO) && key_secretkey_is_set ())
332     {
333       cb->serv->key_type = NIS_PK_NONE;
334       cb->serv->pkey.n_bytes = NULL;
335       cb->serv->pkey.n_len = 0;
336     }
337   else
338     {
339       if ((cb->serv->pkey.n_bytes = __nis_getpkey (cb->serv->name)) == NULL)
340         {
341           cb->serv->pkey.n_len = 0;
342           cb->serv->key_type = NIS_PK_NONE;
343         }
344       else
345         {
346           cb->serv->key_type = NIS_PK_DH;
347           cb->serv->pkey.n_len = strlen(cb->serv->pkey.n_bytes);
348         }
349     }
350
351   if (flags & USE_DGRAM)
352     {
353       cb->serv->ep.ep_val[0].proto = strdup ("udp");
354       cb->xprt = svcudp_bufcreate (sock, 100, 8192);
355     }
356   else
357     {
358       cb->serv->ep.ep_val[0].proto = strdup ("tcp");
359       cb->xprt = svctcp_create (sock, 100, 8192);
360     }
361   cb->sock = cb->xprt->xp_sock;
362   if (!svc_register (cb->xprt, CB_PROG, CB_VERS, cb_prog_1, 0))
363     {
364       xprt_unregister (cb->xprt);
365       svc_destroy (cb->xprt);
366       xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);
367       free (cb->serv);
368       free (cb);
369       syslog (LOG_ERR, "NIS+: failed to register callback dispatcher");
370       return NULL;
371     }
372
373   if (getsockname (cb->sock, (struct sockaddr *) &sin, &len) == -1)
374     {
375       xprt_unregister (cb->xprt);
376       svc_destroy (cb->xprt);
377       xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);
378       free (cb->serv);
379       free (cb);
380       syslog (LOG_ERR, "NIS+: failed to read local socket info");
381       return (NULL);
382     }
383   port = sin.sin_port;
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);
388
389   return cb;
390 }
391
392 nis_error
393 __nis_destroy_callback (struct nis_cb *cb)
394 {
395   xprt_unregister (cb->xprt);
396   svc_destroy (cb->xprt);
397   close (cb->sock);
398   xdr_free ((xdrproc_t) xdr_nis_server, (char *) cb->serv);
399   free (cb->serv);
400   free (cb);
401
402   return NIS_SUCCESS;
403 }