2002-08-01 Roland McGrath <roland@redhat.com>
[platform/upstream/glibc.git] / sunrpc / svc.c
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29 /*
30  * svc.c, Server-side remote procedure call interface.
31  *
32  * There are two sets of procedures here.  The xprt routines are
33  * for handling transport handles.  The svc routines handle the
34  * list of service routines.
35  *
36  * Copyright (C) 1984, Sun Microsystems, Inc.
37  */
38
39 #include <errno.h>
40 #include <unistd.h>
41 #include <rpc/rpc.h>
42 #include <rpc/svc.h>
43 #include <rpc/pmap_clnt.h>
44 #include <sys/poll.h>
45
46 #ifdef _RPC_THREAD_SAFE_
47 #define xports ((SVCXPRT **)RPC_THREAD_VARIABLE(svc_xports_s))
48 #else
49 static SVCXPRT **xports;
50 #endif
51
52 #define NULL_SVC ((struct svc_callout *)0)
53 #define RQCRED_SIZE     400     /* this size is excessive */
54
55 /* The services list
56    Each entry represents a set of procedures (an rpc program).
57    The dispatch routine takes request structs and runs the
58    appropriate procedure. */
59 struct svc_callout {
60   struct svc_callout *sc_next;
61   rpcprog_t sc_prog;
62   rpcvers_t sc_vers;
63   void (*sc_dispatch) (struct svc_req *, SVCXPRT *);
64 };
65 #ifdef _RPC_THREAD_SAFE_
66 #define svc_head ((struct svc_callout *)RPC_THREAD_VARIABLE(svc_head_s))
67 #else
68 static struct svc_callout *svc_head;
69 #endif
70
71 /* ***************  SVCXPRT related stuff **************** */
72
73 /* Activate a transport handle. */
74 void
75 xprt_register (SVCXPRT *xprt)
76 {
77   register int sock = xprt->xp_sock;
78   register int i;
79
80   if (xports == NULL)
81     {
82       xports = (SVCXPRT **) malloc (_rpc_dtablesize () * sizeof (SVCXPRT *));
83       if (xports == NULL) /* DonĀ“t add handle */
84         return;
85     }
86
87   if (sock < _rpc_dtablesize ())
88     {
89       struct pollfd *new_svc_pollfd;
90
91       xports[sock] = xprt;
92       if (sock < FD_SETSIZE)
93         FD_SET (sock, &svc_fdset);
94
95       /* Check if we have an empty slot */
96       for (i = 0; i < svc_max_pollfd; ++i)
97         if (svc_pollfd[i].fd == -1)
98           {
99             svc_pollfd[i].fd = sock;
100             svc_pollfd[i].events = (POLLIN | POLLPRI |
101                                     POLLRDNORM | POLLRDBAND);
102             return;
103           }
104
105       new_svc_pollfd = (struct pollfd *) realloc (svc_pollfd,
106                                                   sizeof (struct pollfd)
107                                                   * (svc_max_pollfd + 1));
108       if (new_svc_pollfd == NULL) /* Out of memory */
109         return;
110       svc_pollfd = new_svc_pollfd;
111       ++svc_max_pollfd;
112
113       svc_pollfd[svc_max_pollfd - 1].fd = sock;
114       svc_pollfd[svc_max_pollfd - 1].events = (POLLIN | POLLPRI |
115                                                POLLRDNORM | POLLRDBAND);
116     }
117 }
118
119 /* De-activate a transport handle. */
120 void
121 xprt_unregister (SVCXPRT *xprt)
122 {
123   register int sock = xprt->xp_sock;
124   register int i;
125
126   if ((sock < _rpc_dtablesize ()) && (xports[sock] == xprt))
127     {
128       xports[sock] = (SVCXPRT *) 0;
129
130       if (sock < FD_SETSIZE)
131         FD_CLR (sock, &svc_fdset);
132
133       for (i = 0; i < svc_max_pollfd; ++i)
134         if (svc_pollfd[i].fd == sock)
135           svc_pollfd[i].fd = -1;
136     }
137 }
138
139
140 /* ********************** CALLOUT list related stuff ************* */
141
142 /* Search the callout list for a program number, return the callout
143    struct. */
144 static struct svc_callout *
145 svc_find (rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev)
146 {
147   register struct svc_callout *s, *p;
148
149   p = NULL_SVC;
150   for (s = svc_head; s != NULL_SVC; s = s->sc_next)
151     {
152       if ((s->sc_prog == prog) && (s->sc_vers == vers))
153         goto done;
154       p = s;
155     }
156 done:
157   *prev = p;
158   return s;
159 }
160
161 /* Add a service program to the callout list.
162    The dispatch routine will be called when a rpc request for this
163    program number comes in. */
164 bool_t
165 svc_register (SVCXPRT * xprt, rpcprog_t prog, rpcvers_t vers,
166               void (*dispatch) (struct svc_req *, SVCXPRT *),
167               rpcproc_t protocol)
168 {
169   struct svc_callout *prev;
170   register struct svc_callout *s;
171
172   if ((s = svc_find (prog, vers, &prev)) != NULL_SVC)
173     {
174       if (s->sc_dispatch == dispatch)
175         goto pmap_it;           /* he is registering another xptr */
176       return FALSE;
177     }
178   s = (struct svc_callout *) mem_alloc (sizeof (struct svc_callout));
179   if (s == (struct svc_callout *) 0)
180     return FALSE;
181
182   s->sc_prog = prog;
183   s->sc_vers = vers;
184   s->sc_dispatch = dispatch;
185   s->sc_next = svc_head;
186   svc_head = s;
187
188 pmap_it:
189   /* now register the information with the local binder service */
190   if (protocol)
191     return pmap_set (prog, vers, protocol, xprt->xp_port);
192
193   return TRUE;
194 }
195 INTDEF (svc_register)
196
197 /* Remove a service program from the callout list. */
198 void
199 svc_unregister (rpcprog_t prog, rpcvers_t vers)
200 {
201   struct svc_callout *prev;
202   register struct svc_callout *s;
203
204   if ((s = svc_find (prog, vers, &prev)) == NULL_SVC)
205     return;
206
207   if (prev == NULL_SVC)
208     svc_head = s->sc_next;
209   else
210     prev->sc_next = s->sc_next;
211
212   s->sc_next = NULL_SVC;
213   mem_free ((char *) s, (u_int) sizeof (struct svc_callout));
214   /* now unregister the information with the local binder service */
215   pmap_unset (prog, vers);
216 }
217 INTDEF (svc_unregister)
218
219 /* ******************* REPLY GENERATION ROUTINES  ************ */
220
221 /* Send a reply to an rpc request */
222 bool_t
223 svc_sendreply (register SVCXPRT *xprt, xdrproc_t xdr_results,
224                caddr_t xdr_location)
225 {
226   struct rpc_msg rply;
227
228   rply.rm_direction = REPLY;
229   rply.rm_reply.rp_stat = MSG_ACCEPTED;
230   rply.acpted_rply.ar_verf = xprt->xp_verf;
231   rply.acpted_rply.ar_stat = SUCCESS;
232   rply.acpted_rply.ar_results.where = xdr_location;
233   rply.acpted_rply.ar_results.proc = xdr_results;
234   return SVC_REPLY (xprt, &rply);
235 }
236 INTDEF (svc_sendreply)
237
238 /* No procedure error reply */
239 void
240 svcerr_noproc (register SVCXPRT *xprt)
241 {
242   struct rpc_msg rply;
243
244   rply.rm_direction = REPLY;
245   rply.rm_reply.rp_stat = MSG_ACCEPTED;
246   rply.acpted_rply.ar_verf = xprt->xp_verf;
247   rply.acpted_rply.ar_stat = PROC_UNAVAIL;
248   SVC_REPLY (xprt, &rply);
249 }
250
251 /* Can't decode args error reply */
252 void
253 svcerr_decode (register SVCXPRT *xprt)
254 {
255   struct rpc_msg rply;
256
257   rply.rm_direction = REPLY;
258   rply.rm_reply.rp_stat = MSG_ACCEPTED;
259   rply.acpted_rply.ar_verf = xprt->xp_verf;
260   rply.acpted_rply.ar_stat = GARBAGE_ARGS;
261   SVC_REPLY (xprt, &rply);
262 }
263 INTDEF (svcerr_decode)
264
265 /* Some system error */
266 void
267 svcerr_systemerr (register SVCXPRT *xprt)
268 {
269   struct rpc_msg rply;
270
271   rply.rm_direction = REPLY;
272   rply.rm_reply.rp_stat = MSG_ACCEPTED;
273   rply.acpted_rply.ar_verf = xprt->xp_verf;
274   rply.acpted_rply.ar_stat = SYSTEM_ERR;
275   SVC_REPLY (xprt, &rply);
276 }
277
278 /* Authentication error reply */
279 void
280 svcerr_auth (SVCXPRT *xprt, enum auth_stat why)
281 {
282   struct rpc_msg rply;
283
284   rply.rm_direction = REPLY;
285   rply.rm_reply.rp_stat = MSG_DENIED;
286   rply.rjcted_rply.rj_stat = AUTH_ERROR;
287   rply.rjcted_rply.rj_why = why;
288   SVC_REPLY (xprt, &rply);
289 }
290
291 /* Auth too weak error reply */
292 void
293 svcerr_weakauth (SVCXPRT *xprt)
294 {
295   svcerr_auth (xprt, AUTH_TOOWEAK);
296 }
297
298 /* Program unavailable error reply */
299 void
300 svcerr_noprog (register SVCXPRT *xprt)
301 {
302   struct rpc_msg rply;
303
304   rply.rm_direction = REPLY;
305   rply.rm_reply.rp_stat = MSG_ACCEPTED;
306   rply.acpted_rply.ar_verf = xprt->xp_verf;
307   rply.acpted_rply.ar_stat = PROG_UNAVAIL;
308   SVC_REPLY (xprt, &rply);
309 }
310
311 /* Program version mismatch error reply */
312 void
313 svcerr_progvers (register SVCXPRT *xprt, rpcvers_t low_vers,
314                  rpcvers_t high_vers)
315 {
316   struct rpc_msg rply;
317
318   rply.rm_direction = REPLY;
319   rply.rm_reply.rp_stat = MSG_ACCEPTED;
320   rply.acpted_rply.ar_verf = xprt->xp_verf;
321   rply.acpted_rply.ar_stat = PROG_MISMATCH;
322   rply.acpted_rply.ar_vers.low = low_vers;
323   rply.acpted_rply.ar_vers.high = high_vers;
324   SVC_REPLY (xprt, &rply);
325 }
326
327 /* ******************* SERVER INPUT STUFF ******************* */
328
329 /*
330  * Get server side input from some transport.
331  *
332  * Statement of authentication parameters management:
333  * This function owns and manages all authentication parameters, specifically
334  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
335  * the "cooked" credentials (rqst->rq_clntcred).
336  * However, this function does not know the structure of the cooked
337  * credentials, so it make the following assumptions:
338  *   a) the structure is contiguous (no pointers), and
339  *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
340  * In all events, all three parameters are freed upon exit from this routine.
341  * The storage is trivially management on the call stack in user land, but
342  * is mallocated in kernel land.
343  */
344
345 void
346 svc_getreq (int rdfds)
347 {
348   fd_set readfds;
349
350   FD_ZERO (&readfds);
351   readfds.fds_bits[0] = rdfds;
352   INTUSE(svc_getreqset) (&readfds);
353 }
354 INTDEF (svc_getreq)
355
356 void
357 svc_getreqset (fd_set *readfds)
358 {
359   register u_int32_t mask;
360   register u_int32_t *maskp;
361   register int setsize;
362   register int sock;
363   register int bit;
364
365   setsize = _rpc_dtablesize ();
366   maskp = (u_int32_t *) readfds->fds_bits;
367   for (sock = 0; sock < setsize; sock += 32)
368     for (mask = *maskp++; (bit = ffs (mask)); mask ^= (1 << (bit - 1)))
369       INTUSE(svc_getreq_common) (sock + bit - 1);
370 }
371 INTDEF (svc_getreqset)
372
373 void
374 svc_getreq_poll (struct pollfd *pfdp, int pollretval)
375 {
376   register int i;
377   register int fds_found;
378
379   for (i = fds_found = 0; i < svc_max_pollfd && fds_found < pollretval; ++i)
380     {
381       register struct pollfd *p = &pfdp[i];
382
383       if (p->fd != -1 && p->revents)
384         {
385           /* fd has input waiting */
386           ++fds_found;
387
388           if (p->revents & POLLNVAL)
389             xprt_unregister (xports[p->fd]);
390           else
391             INTUSE(svc_getreq_common) (p->fd);
392         }
393     }
394 }
395 INTDEF (svc_getreq_poll)
396
397
398 void
399 svc_getreq_common (const int fd)
400 {
401   enum xprt_stat stat;
402   struct rpc_msg msg;
403   register SVCXPRT *xprt;
404   char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
405   msg.rm_call.cb_cred.oa_base = cred_area;
406   msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
407
408   xprt = xports[fd];
409   /* Do we control fd? */
410   if (xprt == NULL)
411      return;
412
413   /* now receive msgs from xprtprt (support batch calls) */
414   do
415     {
416       if (SVC_RECV (xprt, &msg))
417         {
418           /* now find the exported program and call it */
419           struct svc_callout *s;
420           struct svc_req r;
421           enum auth_stat why;
422           rpcvers_t low_vers;
423           rpcvers_t high_vers;
424           int prog_found;
425
426           r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
427           r.rq_xprt = xprt;
428           r.rq_prog = msg.rm_call.cb_prog;
429           r.rq_vers = msg.rm_call.cb_vers;
430           r.rq_proc = msg.rm_call.cb_proc;
431           r.rq_cred = msg.rm_call.cb_cred;
432
433           /* first authenticate the message */
434           /* Check for null flavor and bypass these calls if possible */
435
436           if (msg.rm_call.cb_cred.oa_flavor == AUTH_NULL)
437             {
438               r.rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
439               r.rq_xprt->xp_verf.oa_length = 0;
440             }
441           else if ((why = _authenticate (&r, &msg)) != AUTH_OK)
442             {
443               svcerr_auth (xprt, why);
444               goto call_done;
445             }
446
447           /* now match message with a registered service */
448           prog_found = FALSE;
449           low_vers = 0 - 1;
450           high_vers = 0;
451
452           for (s = svc_head; s != NULL_SVC; s = s->sc_next)
453             {
454               if (s->sc_prog == r.rq_prog)
455                 {
456                   if (s->sc_vers == r.rq_vers)
457                     {
458                       (*s->sc_dispatch) (&r, xprt);
459                       goto call_done;
460                     }
461                   /* found correct version */
462                   prog_found = TRUE;
463                   if (s->sc_vers < low_vers)
464                     low_vers = s->sc_vers;
465                   if (s->sc_vers > high_vers)
466                     high_vers = s->sc_vers;
467                 }
468               /* found correct program */
469             }
470           /* if we got here, the program or version
471              is not served ... */
472           if (prog_found)
473             svcerr_progvers (xprt, low_vers, high_vers);
474           else
475             svcerr_noprog (xprt);
476           /* Fall through to ... */
477         }
478     call_done:
479       if ((stat = SVC_STAT (xprt)) == XPRT_DIED)
480         {
481           SVC_DESTROY (xprt);
482           break;
483         }
484     }
485   while (stat == XPRT_MOREREQS);
486 }
487 INTDEF (svc_getreq_common)
488
489 #ifdef _RPC_THREAD_SAFE_
490
491 void
492 __rpc_thread_svc_cleanup (void)
493 {
494   struct svc_callout *svcp;
495
496   while ((svcp = svc_head) != NULL)
497     svc_unregister (svcp->sc_prog, svcp->sc_vers);
498 }
499
500 #endif /* _RPC_THREAD_SAFE_ */