Update.
[glibc.git] / sunrpc / svc.c
blobda97098addac1734b3afc3ba6d6bdf5810e30d73
1 /* @(#)svc.c 2.4 88/08/11 4.0 RPCSRC; from 1.44 88/02/08 SMI */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)svc.c 1.41 87/10/13 Copyr 1984 Sun Micro";
32 #endif
35 * svc.c, Server-side remote procedure call interface.
37 * There are two sets of procedures here. The xprt routines are
38 * for handling transport handles. The svc routines handle the
39 * list of service routines.
41 * Copyright (C) 1984, Sun Microsystems, Inc.
44 #include <errno.h>
45 #include <rpc/rpc.h>
46 #include <rpc/svc.h>
47 #include <rpc/pmap_clnt.h>
49 #ifndef errno
50 extern int errno;
51 #endif
53 #ifdef FD_SETSIZE
54 static SVCXPRT **xports;
55 #else
56 #define NOFILE 32
58 static SVCXPRT *xports[NOFILE];
59 #endif /* def FD_SETSIZE */
61 #define NULL_SVC ((struct svc_callout *)0)
62 #define RQCRED_SIZE 400 /* this size is excessive */
65 * The services list
66 * Each entry represents a set of procedures (an rpc program).
67 * The dispatch routine takes request structs and runs the
68 * appropriate procedure.
70 static struct svc_callout
72 struct svc_callout *sc_next;
73 u_long sc_prog;
74 u_long sc_vers;
75 void (*sc_dispatch) (struct svc_req *, SVCXPRT *);
77 *svc_head;
79 static struct svc_callout *svc_find (u_long, u_long, struct svc_callout **);
81 /* *************** SVCXPRT related stuff **************** */
84 * Activate a transport handle.
86 void
87 xprt_register (SVCXPRT *xprt)
89 register int sock = xprt->xp_sock;
91 #ifdef FD_SETSIZE
92 if (xports == NULL)
94 xports = (SVCXPRT **)
95 mem_alloc (FD_SETSIZE * sizeof (SVCXPRT *));
97 if (sock < _rpc_dtablesize ())
99 xports[sock] = xprt;
100 FD_SET (sock, &svc_fdset);
102 #else
103 if (sock < NOFILE)
105 xports[sock] = xprt;
106 svc_fds |= (1 << sock);
108 #endif /* def FD_SETSIZE */
113 * De-activate a transport handle.
115 void
116 xprt_unregister (xprt)
117 SVCXPRT *xprt;
119 register int sock = xprt->xp_sock;
121 #ifdef FD_SETSIZE
122 if ((sock < _rpc_dtablesize ()) && (xports[sock] == xprt))
124 xports[sock] = (SVCXPRT *) 0;
125 FD_CLR (sock, &svc_fdset);
127 #else
128 if ((sock < NOFILE) && (xports[sock] == xprt))
130 xports[sock] = (SVCXPRT *) 0;
131 svc_fds &= ~(1 << sock);
133 #endif /* def FD_SETSIZE */
137 /* ********************** CALLOUT list related stuff ************* */
140 * Add a service program to the callout list.
141 * The dispatch routine will be called when a rpc request for this
142 * program number comes in.
144 bool_t
145 svc_register (SVCXPRT *xprt, u_long prog, u_long vers,
146 void (*dispatch) (struct svc_req *, SVCXPRT *), u_long protocol)
148 struct svc_callout *prev;
149 register struct svc_callout *s;
151 if ((s = svc_find (prog, vers, &prev)) != NULL_SVC)
153 if (s->sc_dispatch == dispatch)
154 goto pmap_it; /* he is registering another xptr */
155 return FALSE;
157 s = (struct svc_callout *) mem_alloc (sizeof (struct svc_callout));
158 if (s == (struct svc_callout *) 0)
160 return FALSE;
162 s->sc_prog = prog;
163 s->sc_vers = vers;
164 s->sc_dispatch = dispatch;
165 s->sc_next = svc_head;
166 svc_head = s;
167 pmap_it:
168 /* now register the information with the local binder service */
169 if (protocol)
171 return pmap_set (prog, vers, protocol, xprt->xp_port);
173 return TRUE;
177 * Remove a service program from the callout list.
179 void
180 svc_unregister (prog, vers)
181 u_long prog;
182 u_long vers;
184 struct svc_callout *prev;
185 register struct svc_callout *s;
187 if ((s = svc_find (prog, vers, &prev)) == NULL_SVC)
188 return;
189 if (prev == NULL_SVC)
191 svc_head = s->sc_next;
193 else
195 prev->sc_next = s->sc_next;
197 s->sc_next = NULL_SVC;
198 mem_free ((char *) s, (u_int) sizeof (struct svc_callout));
199 /* now unregister the information with the local binder service */
200 (void) pmap_unset (prog, vers);
204 * Search the callout list for a program number, return the callout
205 * struct.
207 static struct svc_callout *
208 svc_find (u_long prog, u_long vers, struct svc_callout **prev)
210 register struct svc_callout *s, *p;
212 p = NULL_SVC;
213 for (s = svc_head; s != NULL_SVC; s = s->sc_next)
215 if ((s->sc_prog == prog) && (s->sc_vers == vers))
216 goto done;
217 p = s;
219 done:
220 *prev = p;
221 return s;
224 /* ******************* REPLY GENERATION ROUTINES ************ */
227 * Send a reply to an rpc request
229 bool_t
230 svc_sendreply (xprt, xdr_results, xdr_location)
231 register SVCXPRT *xprt;
232 xdrproc_t xdr_results;
233 caddr_t xdr_location;
235 struct rpc_msg rply;
237 rply.rm_direction = REPLY;
238 rply.rm_reply.rp_stat = MSG_ACCEPTED;
239 rply.acpted_rply.ar_verf = xprt->xp_verf;
240 rply.acpted_rply.ar_stat = SUCCESS;
241 rply.acpted_rply.ar_results.where = xdr_location;
242 rply.acpted_rply.ar_results.proc = xdr_results;
243 return SVC_REPLY (xprt, &rply);
247 * No procedure error reply
249 void
250 svcerr_noproc (xprt)
251 register SVCXPRT *xprt;
253 struct rpc_msg rply;
255 rply.rm_direction = REPLY;
256 rply.rm_reply.rp_stat = MSG_ACCEPTED;
257 rply.acpted_rply.ar_verf = xprt->xp_verf;
258 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
259 SVC_REPLY (xprt, &rply);
263 * Can't decode args error reply
265 void
266 svcerr_decode (xprt)
267 register SVCXPRT *xprt;
269 struct rpc_msg rply;
271 rply.rm_direction = REPLY;
272 rply.rm_reply.rp_stat = MSG_ACCEPTED;
273 rply.acpted_rply.ar_verf = xprt->xp_verf;
274 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
275 SVC_REPLY (xprt, &rply);
279 * Some system error
281 void
282 svcerr_systemerr (xprt)
283 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 = SYSTEM_ERR;
291 SVC_REPLY (xprt, &rply);
295 * Authentication error reply
297 void
298 svcerr_auth (xprt, why)
299 SVCXPRT *xprt;
300 enum auth_stat why;
302 struct rpc_msg rply;
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);
312 * Auth too weak error reply
314 void
315 svcerr_weakauth (xprt)
316 SVCXPRT *xprt;
319 svcerr_auth (xprt, AUTH_TOOWEAK);
323 * Program unavailable error reply
325 void
326 svcerr_noprog (xprt)
327 register SVCXPRT *xprt;
329 struct rpc_msg rply;
331 rply.rm_direction = REPLY;
332 rply.rm_reply.rp_stat = MSG_ACCEPTED;
333 rply.acpted_rply.ar_verf = xprt->xp_verf;
334 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
335 SVC_REPLY (xprt, &rply);
339 * Program version mismatch error reply
341 void
342 svcerr_progvers (xprt, low_vers, high_vers)
343 register SVCXPRT *xprt;
344 u_long low_vers;
345 u_long high_vers;
347 struct rpc_msg rply;
349 rply.rm_direction = REPLY;
350 rply.rm_reply.rp_stat = MSG_ACCEPTED;
351 rply.acpted_rply.ar_verf = xprt->xp_verf;
352 rply.acpted_rply.ar_stat = PROG_MISMATCH;
353 rply.acpted_rply.ar_vers.low = low_vers;
354 rply.acpted_rply.ar_vers.high = high_vers;
355 SVC_REPLY (xprt, &rply);
358 /* ******************* SERVER INPUT STUFF ******************* */
361 * Get server side input from some transport.
363 * Statement of authentication parameters management:
364 * This function owns and manages all authentication parameters, specifically
365 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
366 * the "cooked" credentials (rqst->rq_clntcred).
367 * However, this function does not know the structure of the cooked
368 * credentials, so it make the following assumptions:
369 * a) the structure is contiguous (no pointers), and
370 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
371 * In all events, all three parameters are freed upon exit from this routine.
372 * The storage is trivially management on the call stack in user land, but
373 * is mallocated in kernel land.
376 void
377 svc_getreq (int rdfds)
379 #ifdef FD_SETSIZE
380 fd_set readfds;
382 FD_ZERO (&readfds);
383 readfds.fds_bits[0] = rdfds;
384 svc_getreqset (&readfds);
385 #else
386 int readfds = rdfds & svc_fds;
388 svc_getreqset (&readfds);
389 #endif /* def FD_SETSIZE */
392 void
393 svc_getreqset (readfds)
394 #ifdef FD_SETSIZE
395 fd_set *readfds;
397 #else
398 int *readfds;
400 int readfds_local = *readfds;
401 #endif /* def FD_SETSIZE */
402 enum xprt_stat stat;
403 struct rpc_msg msg;
404 int prog_found;
405 u_long low_vers;
406 u_long high_vers;
407 struct svc_req r;
408 register SVCXPRT *xprt;
409 register u_long mask;
410 register int bit;
411 register u_int32_t *maskp;
412 register int setsize;
413 register int sock;
414 char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
415 msg.rm_call.cb_cred.oa_base = cred_area;
416 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
417 r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
420 #ifdef FD_SETSIZE
421 setsize = _rpc_dtablesize ();
422 maskp = (u_int32_t *) readfds->fds_bits;
423 for (sock = 0; sock < setsize; sock += 32)
425 for (mask = *maskp++; (bit = ffs (mask)); mask ^= (1 << (bit - 1)))
427 /* sock has input waiting */
428 xprt = xports[sock + bit - 1];
429 #else
430 for (sock = 0; readfds_local != 0; sock++, readfds_local >>= 1)
432 if ((readfds_local & 1) != 0)
434 /* sock has input waiting */
435 xprt = xports[sock];
436 #endif /* def FD_SETSIZE */
437 if (xprt == NULL)
438 /* But do we control sock? */
439 continue;
441 /* now receive msgs from xprtprt (support batch calls) */
444 if (SVC_RECV (xprt, &msg))
447 /* now find the exported program and call it */
448 register struct svc_callout *s;
449 enum auth_stat why;
451 r.rq_xprt = xprt;
452 r.rq_prog = msg.rm_call.cb_prog;
453 r.rq_vers = msg.rm_call.cb_vers;
454 r.rq_proc = msg.rm_call.cb_proc;
455 r.rq_cred = msg.rm_call.cb_cred;
456 /* first authenticate the message */
457 if ((why = _authenticate (&r, &msg)) != AUTH_OK)
459 svcerr_auth (xprt, why);
460 goto call_done;
462 /* now match message with a registered service */
463 prog_found = FALSE;
464 low_vers = 0 - 1;
465 high_vers = 0;
466 for (s = svc_head; s != NULL_SVC; s = s->sc_next)
468 if (s->sc_prog == r.rq_prog)
470 if (s->sc_vers == r.rq_vers)
472 (*s->sc_dispatch) (&r, xprt);
473 goto call_done;
474 } /* found correct version */
475 prog_found = TRUE;
476 if (s->sc_vers < low_vers)
477 low_vers = s->sc_vers;
478 if (s->sc_vers > high_vers)
479 high_vers = s->sc_vers;
480 } /* found correct program */
483 * if we got here, the program or version
484 * is not served ...
486 if (prog_found)
487 svcerr_progvers (xprt, low_vers, high_vers);
488 else
489 svcerr_noprog (xprt);
490 /* Fall through to ... */
492 call_done:
493 if ((stat = SVC_STAT (xprt)) == XPRT_DIED)
495 SVC_DESTROY (xprt);
496 break;
499 while (stat == XPRT_MOREREQS);