Reinitialize _create_xid state after fork.
[platform/upstream/glibc.git] / sunrpc / svc.c
1 /*
2  * svc.c, Server-side remote procedure call interface.
3  *
4  * There are two sets of procedures here.  The xprt routines are
5  * for handling transport handles.  The svc routines handle the
6  * list of service routines.
7  *
8  * Copyright (C) 1984, Sun Microsystems, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are
12  * met:
13  *
14  *     * Redistributions of source code must retain the above copyright
15  *       notice, this list of conditions and the following disclaimer.
16  *     * Redistributions in binary form must reproduce the above
17  *       copyright notice, this list of conditions and the following
18  *       disclaimer in the documentation and/or other materials
19  *       provided with the distribution.
20  *     * Neither the name of Sun Microsystems, Inc. nor the names of its
21  *       contributors may be used to endorse or promote products derived
22  *       from this software without specific prior written permission.
23  *
24  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
31  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include <errno.h>
39 #include <unistd.h>
40 #include <rpc/rpc.h>
41 #include <rpc/svc.h>
42 #include <rpc/pmap_clnt.h>
43 #include <sys/poll.h>
44
45 #ifdef _RPC_THREAD_SAFE_
46 #define xports RPC_THREAD_VARIABLE(svc_xports_s)
47 #else
48 static SVCXPRT **xports;
49 #endif
50
51 #define NULL_SVC ((struct svc_callout *)0)
52 #define RQCRED_SIZE     400     /* this size is excessive */
53
54 /* The services list
55    Each entry represents a set of procedures (an rpc program).
56    The dispatch routine takes request structs and runs the
57    appropriate procedure. */
58 struct svc_callout {
59   struct svc_callout *sc_next;
60   rpcprog_t sc_prog;
61   rpcvers_t sc_vers;
62   void (*sc_dispatch) (struct svc_req *, SVCXPRT *);
63   bool_t sc_mapped;
64 };
65 #ifdef _RPC_THREAD_SAFE_
66 #define svc_head 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 libc_hidden_def (xprt_register)
119
120 /* De-activate a transport handle. */
121 void
122 xprt_unregister (SVCXPRT *xprt)
123 {
124   register int sock = xprt->xp_sock;
125   register int i;
126
127   if ((sock < _rpc_dtablesize ()) && (xports[sock] == xprt))
128     {
129       xports[sock] = (SVCXPRT *) 0;
130
131       if (sock < FD_SETSIZE)
132         FD_CLR (sock, &svc_fdset);
133
134       for (i = 0; i < svc_max_pollfd; ++i)
135         if (svc_pollfd[i].fd == sock)
136           svc_pollfd[i].fd = -1;
137     }
138 }
139 libc_hidden_def (xprt_unregister)
140
141
142 /* ********************** CALLOUT list related stuff ************* */
143
144 /* Search the callout list for a program number, return the callout
145    struct. */
146 static struct svc_callout *
147 svc_find (rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev)
148 {
149   register struct svc_callout *s, *p;
150
151   p = NULL_SVC;
152   for (s = svc_head; s != NULL_SVC; s = s->sc_next)
153     {
154       if ((s->sc_prog == prog) && (s->sc_vers == vers))
155         goto done;
156       p = s;
157     }
158 done:
159   *prev = p;
160   return s;
161 }
162
163
164 static bool_t
165 svc_is_mapped (rpcprog_t prog, rpcvers_t vers)
166 {
167   struct svc_callout *prev;
168   register struct svc_callout *s;
169   s = svc_find (prog, vers, &prev);
170   return s!= NULL_SVC && s->sc_mapped;
171 }
172
173
174 /* Add a service program to the callout list.
175    The dispatch routine will be called when a rpc request for this
176    program number comes in. */
177 bool_t
178 svc_register (SVCXPRT * xprt, rpcprog_t prog, rpcvers_t vers,
179               void (*dispatch) (struct svc_req *, SVCXPRT *),
180               rpcproc_t protocol)
181 {
182   struct svc_callout *prev;
183   register struct svc_callout *s;
184
185   if ((s = svc_find (prog, vers, &prev)) != NULL_SVC)
186     {
187       if (s->sc_dispatch == dispatch)
188         goto pmap_it;           /* he is registering another xptr */
189       return FALSE;
190     }
191   s = (struct svc_callout *) mem_alloc (sizeof (struct svc_callout));
192   if (s == (struct svc_callout *) 0)
193     return FALSE;
194
195   s->sc_prog = prog;
196   s->sc_vers = vers;
197   s->sc_dispatch = dispatch;
198   s->sc_next = svc_head;
199   s->sc_mapped = FALSE;
200   svc_head = s;
201
202 pmap_it:
203   /* now register the information with the local binder service */
204   if (protocol)
205     {
206       if (! pmap_set (prog, vers, protocol, xprt->xp_port))
207         return FALSE;
208
209       s->sc_mapped = TRUE;
210     }
211
212   return TRUE;
213 }
214 libc_hidden_def (svc_register)
215
216 /* Remove a service program from the callout list. */
217 void
218 svc_unregister (rpcprog_t prog, rpcvers_t vers)
219 {
220   struct svc_callout *prev;
221   register struct svc_callout *s;
222
223   if ((s = svc_find (prog, vers, &prev)) == NULL_SVC)
224     return;
225
226   if (prev == NULL_SVC)
227     svc_head = s->sc_next;
228   else
229     prev->sc_next = s->sc_next;
230
231   s->sc_next = NULL_SVC;
232   mem_free ((char *) s, (u_int) sizeof (struct svc_callout));
233   /* now unregister the information with the local binder service */
234   if (! svc_is_mapped (prog, vers))
235     pmap_unset (prog, vers);
236 }
237 libc_hidden_def (svc_unregister)
238
239 /* ******************* REPLY GENERATION ROUTINES  ************ */
240
241 /* Send a reply to an rpc request */
242 bool_t
243 svc_sendreply (register SVCXPRT *xprt, xdrproc_t xdr_results,
244                caddr_t xdr_location)
245 {
246   struct rpc_msg rply;
247
248   rply.rm_direction = REPLY;
249   rply.rm_reply.rp_stat = MSG_ACCEPTED;
250   rply.acpted_rply.ar_verf = xprt->xp_verf;
251   rply.acpted_rply.ar_stat = SUCCESS;
252   rply.acpted_rply.ar_results.where = xdr_location;
253   rply.acpted_rply.ar_results.proc = xdr_results;
254   return SVC_REPLY (xprt, &rply);
255 }
256 INTDEF (svc_sendreply)
257
258 /* No procedure error reply */
259 void
260 svcerr_noproc (register SVCXPRT *xprt)
261 {
262   struct rpc_msg rply;
263
264   rply.rm_direction = REPLY;
265   rply.rm_reply.rp_stat = MSG_ACCEPTED;
266   rply.acpted_rply.ar_verf = xprt->xp_verf;
267   rply.acpted_rply.ar_stat = PROC_UNAVAIL;
268   SVC_REPLY (xprt, &rply);
269 }
270
271 /* Can't decode args error reply */
272 void
273 svcerr_decode (register SVCXPRT *xprt)
274 {
275   struct rpc_msg rply;
276
277   rply.rm_direction = REPLY;
278   rply.rm_reply.rp_stat = MSG_ACCEPTED;
279   rply.acpted_rply.ar_verf = xprt->xp_verf;
280   rply.acpted_rply.ar_stat = GARBAGE_ARGS;
281   SVC_REPLY (xprt, &rply);
282 }
283 INTDEF (svcerr_decode)
284
285 /* Some system error */
286 void
287 svcerr_systemerr (register SVCXPRT *xprt)
288 {
289   struct rpc_msg rply;
290
291   rply.rm_direction = REPLY;
292   rply.rm_reply.rp_stat = MSG_ACCEPTED;
293   rply.acpted_rply.ar_verf = xprt->xp_verf;
294   rply.acpted_rply.ar_stat = SYSTEM_ERR;
295   SVC_REPLY (xprt, &rply);
296 }
297
298 /* Authentication error reply */
299 void
300 svcerr_auth (SVCXPRT *xprt, enum auth_stat why)
301 {
302   struct rpc_msg rply;
303
304   rply.rm_direction = REPLY;
305   rply.rm_reply.rp_stat = MSG_DENIED;
306   rply.rjcted_rply.rj_stat = AUTH_ERROR;
307   rply.rjcted_rply.rj_why = why;
308   SVC_REPLY (xprt, &rply);
309 }
310 libc_hidden_def (svcerr_auth)
311
312 /* Auth too weak error reply */
313 void
314 svcerr_weakauth (SVCXPRT *xprt)
315 {
316   svcerr_auth (xprt, AUTH_TOOWEAK);
317 }
318
319 /* Program unavailable error reply */
320 void
321 svcerr_noprog (register SVCXPRT *xprt)
322 {
323   struct rpc_msg rply;
324
325   rply.rm_direction = REPLY;
326   rply.rm_reply.rp_stat = MSG_ACCEPTED;
327   rply.acpted_rply.ar_verf = xprt->xp_verf;
328   rply.acpted_rply.ar_stat = PROG_UNAVAIL;
329   SVC_REPLY (xprt, &rply);
330 }
331 libc_hidden_def (svcerr_noprog)
332
333 /* Program version mismatch error reply */
334 void
335 svcerr_progvers (register SVCXPRT *xprt, rpcvers_t low_vers,
336                  rpcvers_t high_vers)
337 {
338   struct rpc_msg rply;
339
340   rply.rm_direction = REPLY;
341   rply.rm_reply.rp_stat = MSG_ACCEPTED;
342   rply.acpted_rply.ar_verf = xprt->xp_verf;
343   rply.acpted_rply.ar_stat = PROG_MISMATCH;
344   rply.acpted_rply.ar_vers.low = low_vers;
345   rply.acpted_rply.ar_vers.high = high_vers;
346   SVC_REPLY (xprt, &rply);
347 }
348 libc_hidden_def (svcerr_progvers)
349
350 /* ******************* SERVER INPUT STUFF ******************* */
351
352 /*
353  * Get server side input from some transport.
354  *
355  * Statement of authentication parameters management:
356  * This function owns and manages all authentication parameters, specifically
357  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
358  * the "cooked" credentials (rqst->rq_clntcred).
359  * However, this function does not know the structure of the cooked
360  * credentials, so it make the following assumptions:
361  *   a) the structure is contiguous (no pointers), and
362  *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
363  * In all events, all three parameters are freed upon exit from this routine.
364  * The storage is trivially management on the call stack in user land, but
365  * is mallocated in kernel land.
366  */
367
368 void
369 svc_getreq (int rdfds)
370 {
371   fd_set readfds;
372
373   FD_ZERO (&readfds);
374   readfds.fds_bits[0] = rdfds;
375   INTUSE(svc_getreqset) (&readfds);
376 }
377 INTDEF (svc_getreq)
378
379 void
380 svc_getreqset (fd_set *readfds)
381 {
382   register fd_mask mask;
383   register fd_mask *maskp;
384   register int setsize;
385   register int sock;
386   register int bit;
387
388   setsize = _rpc_dtablesize ();
389   if (setsize > FD_SETSIZE)
390     setsize = FD_SETSIZE;
391   maskp = readfds->fds_bits;
392   for (sock = 0; sock < setsize; sock += NFDBITS)
393     for (mask = *maskp++; (bit = ffsl (mask)); mask ^= (1L << (bit - 1)))
394       INTUSE(svc_getreq_common) (sock + bit - 1);
395 }
396 INTDEF (svc_getreqset)
397
398 void
399 svc_getreq_poll (struct pollfd *pfdp, int pollretval)
400 {
401   if (pollretval == 0)
402     return;
403
404   register int fds_found;
405   for (int i = fds_found = 0; i < svc_max_pollfd; ++i)
406     {
407       register struct pollfd *p = &pfdp[i];
408
409       if (p->fd != -1 && p->revents)
410         {
411           /* fd has input waiting */
412           if (p->revents & POLLNVAL)
413             xprt_unregister (xports[p->fd]);
414           else
415             INTUSE(svc_getreq_common) (p->fd);
416
417           if (++fds_found >= pollretval)
418             break;
419         }
420     }
421 }
422 INTDEF (svc_getreq_poll)
423
424
425 void
426 svc_getreq_common (const int fd)
427 {
428   enum xprt_stat stat;
429   struct rpc_msg msg;
430   register SVCXPRT *xprt;
431   char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
432   msg.rm_call.cb_cred.oa_base = cred_area;
433   msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
434
435   xprt = xports[fd];
436   /* Do we control fd? */
437   if (xprt == NULL)
438      return;
439
440   /* now receive msgs from xprtprt (support batch calls) */
441   do
442     {
443       if (SVC_RECV (xprt, &msg))
444         {
445           /* now find the exported program and call it */
446           struct svc_callout *s;
447           struct svc_req r;
448           enum auth_stat why;
449           rpcvers_t low_vers;
450           rpcvers_t high_vers;
451           int prog_found;
452
453           r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
454           r.rq_xprt = xprt;
455           r.rq_prog = msg.rm_call.cb_prog;
456           r.rq_vers = msg.rm_call.cb_vers;
457           r.rq_proc = msg.rm_call.cb_proc;
458           r.rq_cred = msg.rm_call.cb_cred;
459
460           /* first authenticate the message */
461           /* Check for null flavor and bypass these calls if possible */
462
463           if (msg.rm_call.cb_cred.oa_flavor == AUTH_NULL)
464             {
465               r.rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
466               r.rq_xprt->xp_verf.oa_length = 0;
467             }
468           else if ((why = INTUSE(_authenticate) (&r, &msg)) != AUTH_OK)
469             {
470               svcerr_auth (xprt, why);
471               goto call_done;
472             }
473
474           /* now match message with a registered service */
475           prog_found = FALSE;
476           low_vers = 0 - 1;
477           high_vers = 0;
478
479           for (s = svc_head; s != NULL_SVC; s = s->sc_next)
480             {
481               if (s->sc_prog == r.rq_prog)
482                 {
483                   if (s->sc_vers == r.rq_vers)
484                     {
485                       (*s->sc_dispatch) (&r, xprt);
486                       goto call_done;
487                     }
488                   /* found correct version */
489                   prog_found = TRUE;
490                   if (s->sc_vers < low_vers)
491                     low_vers = s->sc_vers;
492                   if (s->sc_vers > high_vers)
493                     high_vers = s->sc_vers;
494                 }
495               /* found correct program */
496             }
497           /* if we got here, the program or version
498              is not served ... */
499           if (prog_found)
500             svcerr_progvers (xprt, low_vers, high_vers);
501           else
502             svcerr_noprog (xprt);
503           /* Fall through to ... */
504         }
505     call_done:
506       if ((stat = SVC_STAT (xprt)) == XPRT_DIED)
507         {
508           SVC_DESTROY (xprt);
509           break;
510         }
511     }
512   while (stat == XPRT_MOREREQS);
513 }
514 INTDEF (svc_getreq_common)
515
516 #ifdef _RPC_THREAD_SAFE_
517
518 void
519 __rpc_thread_svc_cleanup (void)
520 {
521   struct svc_callout *svcp;
522
523   while ((svcp = svc_head) != NULL)
524     svc_unregister (svcp->sc_prog, svcp->sc_vers);
525 }
526
527 #endif /* _RPC_THREAD_SAFE_ */