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