Update.
[glibc.git] / sunrpc / svc.c
blob4446692dc0c252609995b8a6f82a2015eb74c780
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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
30 * svc.c, Server-side remote procedure call interface.
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.
36 * Copyright (C) 1984, Sun Microsystems, Inc.
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>
46 static SVCXPRT **xports;
48 #define NULL_SVC ((struct svc_callout *)0)
49 #define RQCRED_SIZE 400 /* this size is excessive */
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;
62 /* *************** SVCXPRT related stuff **************** */
64 /* Activate a transport handle. */
65 void
66 xprt_register (SVCXPRT *xprt)
68 register int sock = xprt->xp_sock;
69 register int i;
71 if (xports == NULL)
73 xports = (SVCXPRT **) malloc (_rpc_dtablesize () * sizeof (SVCXPRT *));
74 if (xports == NULL) /* DonĀ“t add handle */
75 return;
78 if (sock < _rpc_dtablesize ())
80 xports[sock] = xprt;
81 if (sock < FD_SETSIZE)
82 FD_SET (sock, &svc_fdset);
84 /* Check if we have an empty slot */
85 for (i = 0; i < svc_max_pollfd; ++i)
86 if (svc_pollfd[i].fd == -1)
88 svc_pollfd[i].fd = sock;
89 svc_pollfd[i].events = (POLLIN | POLLPRI |
90 POLLRDNORM | POLLRDBAND);
91 return;
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;
100 svc_pollfd[svc_max_pollfd - 1].fd = sock;
101 svc_pollfd[svc_max_pollfd - 1].events = (POLLIN | POLLPRI |
102 POLLRDNORM | POLLRDBAND);
106 /* De-activate a transport handle. */
107 void
108 xprt_unregister (SVCXPRT *xprt)
110 register int sock = xprt->xp_sock;
111 register int i;
113 if ((sock < _rpc_dtablesize ()) && (xports[sock] == xprt))
115 xports[sock] = (SVCXPRT *) 0;
117 if (sock < FD_SETSIZE)
118 FD_CLR (sock, &svc_fdset);
120 for (i = 0; i < svc_max_pollfd; ++i)
121 if (svc_pollfd[i].fd == sock)
122 svc_pollfd[i].fd = -1;
127 /* ********************** CALLOUT list related stuff ************* */
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)
134 register struct svc_callout *s, *p;
136 p = NULL_SVC;
137 for (s = svc_head; s != NULL_SVC; s = s->sc_next)
139 if ((s->sc_prog == prog) && (s->sc_vers == vers))
140 goto done;
141 p = s;
143 done:
144 *prev = p;
145 return s;
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)
156 struct svc_callout *prev;
157 register struct svc_callout *s;
159 if ((s = svc_find (prog, vers, &prev)) != NULL_SVC)
161 if (s->sc_dispatch == dispatch)
162 goto pmap_it; /* he is registering another xptr */
163 return FALSE;
165 s = (struct svc_callout *) mem_alloc (sizeof (struct svc_callout));
166 if (s == (struct svc_callout *) 0)
167 return FALSE;
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;
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);
180 return TRUE;
183 /* Remove a service program from the callout list. */
184 void
185 svc_unregister (rpcprog_t prog, rpcvers_t vers)
187 struct svc_callout *prev;
188 register struct svc_callout *s;
190 if ((s = svc_find (prog, vers, &prev)) == NULL_SVC)
191 return;
193 if (prev == NULL_SVC)
194 svc_head = s->sc_next;
195 else
196 prev->sc_next = s->sc_next;
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);
204 /* ******************* REPLY GENERATION ROUTINES ************ */
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)
211 struct rpc_msg rply;
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);
222 /* No procedure error reply */
223 void
224 svcerr_noproc (register SVCXPRT *xprt)
226 struct rpc_msg rply;
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);
235 /* Can't decode args error reply */
236 void
237 svcerr_decode (register SVCXPRT *xprt)
239 struct rpc_msg rply;
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);
248 /* Some system error */
249 void
250 svcerr_systemerr (register SVCXPRT *xprt)
252 struct rpc_msg rply;
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);
261 /* Authentication error reply */
262 void
263 svcerr_auth (SVCXPRT *xprt, enum auth_stat why)
265 struct rpc_msg rply;
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);
274 /* Auth too weak error reply */
275 void
276 svcerr_weakauth (SVCXPRT *xprt)
278 svcerr_auth (xprt, AUTH_TOOWEAK);
281 /* Program unavailable error reply */
282 void
283 svcerr_noprog (register SVCXPRT *xprt)
285 struct rpc_msg rply;
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);
294 /* Program version mismatch error reply */
295 void
296 svcerr_progvers (register SVCXPRT *xprt, rpcvers_t low_vers,
297 rpcvers_t high_vers)
299 struct rpc_msg rply;
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);
310 /* ******************* SERVER INPUT STUFF ******************* */
313 * Get server side input from some transport.
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.
328 void
329 svc_getreq (int rdfds)
331 fd_set readfds;
333 FD_ZERO (&readfds);
334 readfds.fds_bits[0] = rdfds;
335 svc_getreqset (&readfds);
338 void
339 svc_getreqset (fd_set *readfds)
341 register u_int32_t mask;
342 register u_int32_t *maskp;
343 register int setsize;
344 register int sock;
345 register int bit;
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);
354 void
355 svc_getreq_poll (struct pollfd *pfdp, int pollretval)
357 register int i;
358 register int fds_found;
360 for (i = fds_found = 0; i < svc_max_pollfd && fds_found < pollretval; ++i)
362 register struct pollfd *p = &pfdp[i];
364 if (p->fd != -1 && p->revents)
366 /* fd has input waiting */
367 ++fds_found;
369 if (p->revents & POLLNVAL)
370 xprt_unregister (xports[p->fd]);
371 else
372 svc_getreq_common (p->fd);
378 void
379 svc_getreq_common (const int fd)
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]);
388 xprt = xports[fd];
389 /* Do we control fd? */
390 if (xprt == NULL)
391 return;
393 /* now receive msgs from xprtprt (support batch calls) */
396 if (SVC_RECV (xprt, &msg))
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;
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;
413 /* first authenticate the message */
414 /* Check for null flavor and bypass these calls if possible */
416 if (msg.rm_call.cb_cred.oa_flavor == AUTH_NULL)
418 r.rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
419 r.rq_xprt->xp_verf.oa_length = 0;
421 else if ((why = _authenticate (&r, &msg)) != AUTH_OK)
423 svcerr_auth (xprt, why);
424 goto call_done;
427 /* now match message with a registered service */
428 prog_found = FALSE;
429 low_vers = 0 - 1;
430 high_vers = 0;
432 for (s = svc_head; s != NULL_SVC; s = s->sc_next)
434 if (s->sc_prog == r.rq_prog)
436 if (s->sc_vers == r.rq_vers)
438 (*s->sc_dispatch) (&r, xprt);
439 goto call_done;
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;
448 /* found correct program */
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 ... */
458 call_done:
459 if ((stat = SVC_STAT (xprt)) == XPRT_DIED)
461 SVC_DESTROY (xprt);
462 break;
465 while (stat == XPRT_MOREREQS);