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