Fix "make xcheck" in sunrpc.
[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) 2010, Oracle America, 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 the "Oracle America, 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_nolink (xprt_register, GLIBC_2_0)
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 #ifdef EXPORT_RPC_SYMBOLS
140 libc_hidden_def (xprt_unregister)
141 #else
142 libc_hidden_nolink (xprt_unregister, GLIBC_2_0)
143 #endif
144
145
146 /* ********************** CALLOUT list related stuff ************* */
147
148 /* Search the callout list for a program number, return the callout
149    struct. */
150 static struct svc_callout *
151 svc_find (rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev)
152 {
153   register struct svc_callout *s, *p;
154
155   p = NULL_SVC;
156   for (s = svc_head; s != NULL_SVC; s = s->sc_next)
157     {
158       if ((s->sc_prog == prog) && (s->sc_vers == vers))
159         goto done;
160       p = s;
161     }
162 done:
163   *prev = p;
164   return s;
165 }
166
167
168 static bool_t
169 svc_is_mapped (rpcprog_t prog, rpcvers_t vers)
170 {
171   struct svc_callout *prev;
172   register struct svc_callout *s;
173   s = svc_find (prog, vers, &prev);
174   return s!= NULL_SVC && s->sc_mapped;
175 }
176
177
178 /* Add a service program to the callout list.
179    The dispatch routine will be called when a rpc request for this
180    program number comes in. */
181 bool_t
182 svc_register (SVCXPRT * xprt, rpcprog_t prog, rpcvers_t vers,
183               void (*dispatch) (struct svc_req *, SVCXPRT *),
184               rpcproc_t protocol)
185 {
186   struct svc_callout *prev;
187   register struct svc_callout *s;
188
189   if ((s = svc_find (prog, vers, &prev)) != NULL_SVC)
190     {
191       if (s->sc_dispatch == dispatch)
192         goto pmap_it;           /* he is registering another xptr */
193       return FALSE;
194     }
195   s = (struct svc_callout *) mem_alloc (sizeof (struct svc_callout));
196   if (s == (struct svc_callout *) 0)
197     return FALSE;
198
199   s->sc_prog = prog;
200   s->sc_vers = vers;
201   s->sc_dispatch = dispatch;
202   s->sc_next = svc_head;
203   s->sc_mapped = FALSE;
204   svc_head = s;
205
206 pmap_it:
207   /* now register the information with the local binder service */
208   if (protocol)
209     {
210       if (! pmap_set (prog, vers, protocol, xprt->xp_port))
211         return FALSE;
212
213       s->sc_mapped = TRUE;
214     }
215
216   return TRUE;
217 }
218 #ifdef EXPORT_RPC_SYMBOLS
219 libc_hidden_def (svc_register)
220 #else
221 libc_hidden_nolink (svc_register, GLIBC_2_0)
222 #endif
223
224 /* Remove a service program from the callout list. */
225 void
226 svc_unregister (rpcprog_t prog, rpcvers_t vers)
227 {
228   struct svc_callout *prev;
229   register struct svc_callout *s;
230
231   if ((s = svc_find (prog, vers, &prev)) == NULL_SVC)
232     return;
233
234   if (prev == NULL_SVC)
235     svc_head = s->sc_next;
236   else
237     prev->sc_next = s->sc_next;
238
239   s->sc_next = NULL_SVC;
240   mem_free ((char *) s, (u_int) sizeof (struct svc_callout));
241   /* now unregister the information with the local binder service */
242   if (! svc_is_mapped (prog, vers))
243     pmap_unset (prog, vers);
244 }
245 libc_hidden_nolink (svc_unregister, GLIBC_2_0)
246
247 /* ******************* REPLY GENERATION ROUTINES  ************ */
248
249 /* Send a reply to an rpc request */
250 bool_t
251 svc_sendreply (register SVCXPRT *xprt, xdrproc_t xdr_results,
252                caddr_t xdr_location)
253 {
254   struct rpc_msg rply;
255
256   rply.rm_direction = REPLY;
257   rply.rm_reply.rp_stat = MSG_ACCEPTED;
258   rply.acpted_rply.ar_verf = xprt->xp_verf;
259   rply.acpted_rply.ar_stat = SUCCESS;
260   rply.acpted_rply.ar_results.where = xdr_location;
261   rply.acpted_rply.ar_results.proc = xdr_results;
262   return SVC_REPLY (xprt, &rply);
263 }
264 #ifdef EXPORT_RPC_SYMBOLS
265 libc_hidden_def (svc_sendreply)
266 #else
267 libc_hidden_nolink (svc_sendreply, GLIBC_2_0)
268 #endif
269
270 /* No procedure error reply */
271 void
272 svcerr_noproc (register SVCXPRT *xprt)
273 {
274   struct rpc_msg rply;
275
276   rply.rm_direction = REPLY;
277   rply.rm_reply.rp_stat = MSG_ACCEPTED;
278   rply.acpted_rply.ar_verf = xprt->xp_verf;
279   rply.acpted_rply.ar_stat = PROC_UNAVAIL;
280   SVC_REPLY (xprt, &rply);
281 }
282 #ifdef EXPORT_RPC_SYMBOLS
283 libc_hidden_def (svcerr_noproc)
284 #else
285 libc_hidden_nolink (svcerr_noproc, GLIBC_2_0)
286 #endif
287
288 /* Can't decode args error reply */
289 void
290 svcerr_decode (register SVCXPRT *xprt)
291 {
292   struct rpc_msg rply;
293
294   rply.rm_direction = REPLY;
295   rply.rm_reply.rp_stat = MSG_ACCEPTED;
296   rply.acpted_rply.ar_verf = xprt->xp_verf;
297   rply.acpted_rply.ar_stat = GARBAGE_ARGS;
298   SVC_REPLY (xprt, &rply);
299 }
300 #ifdef EXPORT_RPC_SYMBOLS
301 libc_hidden_def (svcerr_decode)
302 #else
303 libc_hidden_nolink (svcerr_decode, GLIBC_2_0)
304 #endif
305
306 /* Some system error */
307 void
308 svcerr_systemerr (register SVCXPRT *xprt)
309 {
310   struct rpc_msg rply;
311
312   rply.rm_direction = REPLY;
313   rply.rm_reply.rp_stat = MSG_ACCEPTED;
314   rply.acpted_rply.ar_verf = xprt->xp_verf;
315   rply.acpted_rply.ar_stat = SYSTEM_ERR;
316   SVC_REPLY (xprt, &rply);
317 }
318 #ifdef EXPORT_RPC_SYMBOLS
319 libc_hidden_def (svcerr_systemerr)
320 #else
321 libc_hidden_nolink (svcerr_systemerr, GLIBC_2_0)
322 #endif
323
324 /* Authentication error reply */
325 void
326 svcerr_auth (SVCXPRT *xprt, enum auth_stat why)
327 {
328   struct rpc_msg rply;
329
330   rply.rm_direction = REPLY;
331   rply.rm_reply.rp_stat = MSG_DENIED;
332   rply.rjcted_rply.rj_stat = AUTH_ERROR;
333   rply.rjcted_rply.rj_why = why;
334   SVC_REPLY (xprt, &rply);
335 }
336 libc_hidden_nolink (svcerr_auth, GLIBC_2_0)
337
338 /* Auth too weak error reply */
339 void
340 svcerr_weakauth (SVCXPRT *xprt)
341 {
342   svcerr_auth (xprt, AUTH_TOOWEAK);
343 }
344 libc_hidden_nolink (svcerr_weakauth, GLIBC_2_0)
345
346 /* Program unavailable error reply */
347 void
348 svcerr_noprog (register SVCXPRT *xprt)
349 {
350   struct rpc_msg rply;
351
352   rply.rm_direction = REPLY;
353   rply.rm_reply.rp_stat = MSG_ACCEPTED;
354   rply.acpted_rply.ar_verf = xprt->xp_verf;
355   rply.acpted_rply.ar_stat = PROG_UNAVAIL;
356   SVC_REPLY (xprt, &rply);
357 }
358 libc_hidden_nolink (svcerr_noprog, GLIBC_2_0)
359
360 /* Program version mismatch error reply */
361 void
362 svcerr_progvers (register SVCXPRT *xprt, rpcvers_t low_vers,
363                  rpcvers_t high_vers)
364 {
365   struct rpc_msg rply;
366
367   rply.rm_direction = REPLY;
368   rply.rm_reply.rp_stat = MSG_ACCEPTED;
369   rply.acpted_rply.ar_verf = xprt->xp_verf;
370   rply.acpted_rply.ar_stat = PROG_MISMATCH;
371   rply.acpted_rply.ar_vers.low = low_vers;
372   rply.acpted_rply.ar_vers.high = high_vers;
373   SVC_REPLY (xprt, &rply);
374 }
375 libc_hidden_nolink (svcerr_progvers, GLIBC_2_0)
376
377 /* ******************* SERVER INPUT STUFF ******************* */
378
379 /*
380  * Get server side input from some transport.
381  *
382  * Statement of authentication parameters management:
383  * This function owns and manages all authentication parameters, specifically
384  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
385  * the "cooked" credentials (rqst->rq_clntcred).
386  * However, this function does not know the structure of the cooked
387  * credentials, so it make the following assumptions:
388  *   a) the structure is contiguous (no pointers), and
389  *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
390  * In all events, all three parameters are freed upon exit from this routine.
391  * The storage is trivially management on the call stack in user land, but
392  * is mallocated in kernel land.
393  */
394
395 void
396 svc_getreq (int rdfds)
397 {
398   fd_set readfds;
399
400   FD_ZERO (&readfds);
401   readfds.fds_bits[0] = rdfds;
402   svc_getreqset (&readfds);
403 }
404 libc_hidden_nolink (svc_getreq, GLIBC_2_0)
405
406 void
407 svc_getreqset (fd_set *readfds)
408 {
409   register fd_mask mask;
410   register fd_mask *maskp;
411   register int setsize;
412   register int sock;
413   register int bit;
414
415   setsize = _rpc_dtablesize ();
416   if (setsize > FD_SETSIZE)
417     setsize = FD_SETSIZE;
418   maskp = readfds->fds_bits;
419   for (sock = 0; sock < setsize; sock += NFDBITS)
420     for (mask = *maskp++; (bit = ffsl (mask)); mask ^= (1L << (bit - 1)))
421       svc_getreq_common (sock + bit - 1);
422 }
423 libc_hidden_nolink (svc_getreqset, GLIBC_2_0)
424
425 void
426 svc_getreq_poll (struct pollfd *pfdp, int pollretval)
427 {
428   if (pollretval == 0)
429     return;
430
431   register int fds_found;
432   for (int i = fds_found = 0; i < svc_max_pollfd; ++i)
433     {
434       register struct pollfd *p = &pfdp[i];
435
436       if (p->fd != -1 && p->revents)
437         {
438           /* fd has input waiting */
439           if (p->revents & POLLNVAL)
440             xprt_unregister (xports[p->fd]);
441           else
442             svc_getreq_common (p->fd);
443
444           if (++fds_found >= pollretval)
445             break;
446         }
447     }
448 }
449 #ifdef EXPORT_RPC_SYMBOLS
450 libc_hidden_def (svc_getreq_poll)
451 #else
452 libc_hidden_nolink (svc_getreq_poll, GLIBC_2_2)
453 #endif
454
455
456 void
457 svc_getreq_common (const int fd)
458 {
459   enum xprt_stat stat;
460   struct rpc_msg msg;
461   register SVCXPRT *xprt;
462   char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
463   msg.rm_call.cb_cred.oa_base = cred_area;
464   msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
465
466   xprt = xports[fd];
467   /* Do we control fd? */
468   if (xprt == NULL)
469      return;
470
471   /* now receive msgs from xprtprt (support batch calls) */
472   do
473     {
474       if (SVC_RECV (xprt, &msg))
475         {
476           /* now find the exported program and call it */
477           struct svc_callout *s;
478           struct svc_req r;
479           enum auth_stat why;
480           rpcvers_t low_vers;
481           rpcvers_t high_vers;
482           int prog_found;
483
484           r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
485           r.rq_xprt = xprt;
486           r.rq_prog = msg.rm_call.cb_prog;
487           r.rq_vers = msg.rm_call.cb_vers;
488           r.rq_proc = msg.rm_call.cb_proc;
489           r.rq_cred = msg.rm_call.cb_cred;
490
491           /* first authenticate the message */
492           /* Check for null flavor and bypass these calls if possible */
493
494           if (msg.rm_call.cb_cred.oa_flavor == AUTH_NULL)
495             {
496               r.rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
497               r.rq_xprt->xp_verf.oa_length = 0;
498             }
499           else if ((why = _authenticate (&r, &msg)) != AUTH_OK)
500             {
501               svcerr_auth (xprt, why);
502               goto call_done;
503             }
504
505           /* now match message with a registered service */
506           prog_found = FALSE;
507           low_vers = 0 - 1;
508           high_vers = 0;
509
510           for (s = svc_head; s != NULL_SVC; s = s->sc_next)
511             {
512               if (s->sc_prog == r.rq_prog)
513                 {
514                   if (s->sc_vers == r.rq_vers)
515                     {
516                       (*s->sc_dispatch) (&r, xprt);
517                       goto call_done;
518                     }
519                   /* found correct version */
520                   prog_found = TRUE;
521                   if (s->sc_vers < low_vers)
522                     low_vers = s->sc_vers;
523                   if (s->sc_vers > high_vers)
524                     high_vers = s->sc_vers;
525                 }
526               /* found correct program */
527             }
528           /* if we got here, the program or version
529              is not served ... */
530           if (prog_found)
531             svcerr_progvers (xprt, low_vers, high_vers);
532           else
533             svcerr_noprog (xprt);
534           /* Fall through to ... */
535         }
536     call_done:
537       if ((stat = SVC_STAT (xprt)) == XPRT_DIED)
538         {
539           SVC_DESTROY (xprt);
540           break;
541         }
542     }
543   while (stat == XPRT_MOREREQS);
544 }
545 libc_hidden_nolink (svc_getreq_common, GLIBC_2_2)
546
547 #ifdef _RPC_THREAD_SAFE_
548
549 void
550 __rpc_thread_svc_cleanup (void)
551 {
552   struct svc_callout *svcp;
553
554   while ((svcp = svc_head) != NULL)
555     svc_unregister (svcp->sc_prog, svcp->sc_vers);
556 }
557
558 #endif /* _RPC_THREAD_SAFE_ */