1 /* @(#)svc.c 2.4 88/08/11 4.0 RPCSRC; from 1.44 88/02/08 SMI */
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.
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";
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.
47 #include <rpc/pmap_clnt.h>
54 static SVCXPRT
**xports
;
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 */
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
;
75 void (*sc_dispatch
) (struct svc_req
*, SVCXPRT
*);
79 static struct svc_callout
*svc_find (u_long
, u_long
, struct svc_callout
**);
81 /* *************** SVCXPRT related stuff **************** */
84 * Activate a transport handle.
87 xprt_register (SVCXPRT
*xprt
)
89 register int sock
= xprt
->xp_sock
;
95 mem_alloc (FD_SETSIZE
* sizeof (SVCXPRT
*));
97 if (sock
< _rpc_dtablesize ())
100 FD_SET (sock
, &svc_fdset
);
106 svc_fds
|= (1 << sock
);
108 #endif /* def FD_SETSIZE */
113 * De-activate a transport handle.
116 xprt_unregister (xprt
)
119 register int sock
= xprt
->xp_sock
;
122 if ((sock
< _rpc_dtablesize ()) && (xports
[sock
] == xprt
))
124 xports
[sock
] = (SVCXPRT
*) 0;
125 FD_CLR (sock
, &svc_fdset
);
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.
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 */
157 s
= (struct svc_callout
*) mem_alloc (sizeof (struct svc_callout
));
158 if (s
== (struct svc_callout
*) 0)
164 s
->sc_dispatch
= dispatch
;
165 s
->sc_next
= svc_head
;
168 /* now register the information with the local binder service */
171 return pmap_set (prog
, vers
, protocol
, xprt
->xp_port
);
177 * Remove a service program from the callout list.
180 svc_unregister (prog
, vers
)
184 struct svc_callout
*prev
;
185 register struct svc_callout
*s
;
187 if ((s
= svc_find (prog
, vers
, &prev
)) == NULL_SVC
)
189 if (prev
== NULL_SVC
)
191 svc_head
= s
->sc_next
;
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
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
;
213 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
)
215 if ((s
->sc_prog
== prog
) && (s
->sc_vers
== vers
))
224 /* ******************* REPLY GENERATION ROUTINES ************ */
227 * Send a reply to an rpc request
230 svc_sendreply (xprt
, xdr_results
, xdr_location
)
231 register SVCXPRT
*xprt
;
232 xdrproc_t xdr_results
;
233 caddr_t xdr_location
;
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
251 register SVCXPRT
*xprt
;
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
267 register SVCXPRT
*xprt
;
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
);
282 svcerr_systemerr (xprt
)
283 register SVCXPRT
*xprt
;
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
298 svcerr_auth (xprt
, why
)
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
315 svcerr_weakauth (xprt
)
319 svcerr_auth (xprt
, AUTH_TOOWEAK
);
323 * Program unavailable error reply
327 register SVCXPRT
*xprt
;
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
342 svcerr_progvers (xprt
, low_vers
, high_vers
)
343 register SVCXPRT
*xprt
;
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.
377 svc_getreq (int rdfds
)
383 readfds
.fds_bits
[0] = rdfds
;
384 svc_getreqset (&readfds
);
386 int readfds
= rdfds
& svc_fds
;
388 svc_getreqset (&readfds
);
389 #endif /* def FD_SETSIZE */
393 svc_getreqset (readfds
)
400 int readfds_local
= *readfds
;
401 #endif /* def FD_SETSIZE */
408 register SVCXPRT
*xprt
;
409 register u_long mask
;
411 register u_int32_t
*maskp
;
412 register int setsize
;
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
]);
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];
430 for (sock
= 0; readfds_local
!= 0; sock
++, readfds_local
>>= 1)
432 if ((readfds_local
& 1) != 0)
434 /* sock has input waiting */
436 #endif /* def FD_SETSIZE */
438 /* But do we control sock? */
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
;
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
);
462 /* now match message with a registered service */
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
);
474 } /* found correct version */
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
487 svcerr_progvers (xprt
, low_vers
, high_vers
);
489 svcerr_noprog (xprt
);
490 /* Fall through to ... */
493 if ((stat
= SVC_STAT (xprt
)) == XPRT_DIED
)
499 while (stat
== XPRT_MOREREQS
);