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.
44 #include <sys/errno.h>
46 #include <rpc/pmap_clnt.h>
53 static SVCXPRT
**xports
;
57 static SVCXPRT
*xports
[NOFILE
];
58 #endif /* def FD_SETSIZE */
60 #define NULL_SVC ((struct svc_callout *)0)
61 #define RQCRED_SIZE 400 /* this size is excessive */
65 * Each entry represents a set of procedures (an rpc program).
66 * The dispatch routine takes request structs and runs the
67 * appropriate procedure.
69 static struct svc_callout
{
70 struct svc_callout
*sc_next
;
73 void (*sc_dispatch
)();
76 static struct svc_callout
*svc_find();
78 /* *************** SVCXPRT related stuff **************** */
81 * Activate a transport handle.
87 register int sock
= xprt
->xp_sock
;
92 mem_alloc(FD_SETSIZE
* sizeof(SVCXPRT
*));
94 if (sock
< _rpc_dtablesize()) {
96 FD_SET(sock
, &svc_fdset
);
101 svc_fds
|= (1 << sock
);
103 #endif /* def FD_SETSIZE */
108 * De-activate a transport handle.
111 xprt_unregister(xprt
)
114 register int sock
= xprt
->xp_sock
;
117 if ((sock
< _rpc_dtablesize()) && (xports
[sock
] == xprt
)) {
118 xports
[sock
] = (SVCXPRT
*)0;
119 FD_CLR(sock
, &svc_fdset
);
122 if ((sock
< NOFILE
) && (xports
[sock
] == xprt
)) {
123 xports
[sock
] = (SVCXPRT
*)0;
124 svc_fds
&= ~(1 << sock
);
126 #endif /* def FD_SETSIZE */
130 /* ********************** CALLOUT list related stuff ************* */
133 * Add a service program to the callout list.
134 * The dispatch routine will be called when a rpc request for this
135 * program number comes in.
138 svc_register(xprt
, prog
, vers
, dispatch
, protocol
)
145 struct svc_callout
*prev
;
146 register struct svc_callout
*s
;
148 if ((s
= svc_find(prog
, vers
, &prev
)) != NULL_SVC
) {
149 if (s
->sc_dispatch
== dispatch
)
150 goto pmap_it
; /* he is registering another xptr */
153 s
= (struct svc_callout
*)mem_alloc(sizeof(struct svc_callout
));
154 if (s
== (struct svc_callout
*)0) {
159 s
->sc_dispatch
= dispatch
;
160 s
->sc_next
= svc_head
;
163 /* now register the information with the local binder service */
165 return (pmap_set(prog
, vers
, protocol
, xprt
->xp_port
));
171 * Remove a service program from the callout list.
174 svc_unregister(prog
, vers
)
178 struct svc_callout
*prev
;
179 register struct svc_callout
*s
;
181 if ((s
= svc_find(prog
, vers
, &prev
)) == NULL_SVC
)
183 if (prev
== NULL_SVC
) {
184 svc_head
= s
->sc_next
;
186 prev
->sc_next
= s
->sc_next
;
188 s
->sc_next
= NULL_SVC
;
189 mem_free((char *) s
, (u_int
) sizeof(struct svc_callout
));
190 /* now unregister the information with the local binder service */
191 (void)pmap_unset(prog
, vers
);
195 * Search the callout list for a program number, return the callout
198 static struct svc_callout
*
199 svc_find(prog
, vers
, prev
)
202 struct svc_callout
**prev
;
204 register struct svc_callout
*s
, *p
;
207 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
) {
208 if ((s
->sc_prog
== prog
) && (s
->sc_vers
== vers
))
217 /* ******************* REPLY GENERATION ROUTINES ************ */
220 * Send a reply to an rpc request
223 svc_sendreply(xprt
, xdr_results
, xdr_location
)
224 register SVCXPRT
*xprt
;
225 xdrproc_t xdr_results
;
226 caddr_t xdr_location
;
230 rply
.rm_direction
= REPLY
;
231 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
232 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
233 rply
.acpted_rply
.ar_stat
= SUCCESS
;
234 rply
.acpted_rply
.ar_results
.where
= xdr_location
;
235 rply
.acpted_rply
.ar_results
.proc
= xdr_results
;
236 return (SVC_REPLY(xprt
, &rply
));
240 * No procedure error reply
244 register SVCXPRT
*xprt
;
248 rply
.rm_direction
= REPLY
;
249 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
250 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
251 rply
.acpted_rply
.ar_stat
= PROC_UNAVAIL
;
252 SVC_REPLY(xprt
, &rply
);
256 * Can't decode args error reply
260 register SVCXPRT
*xprt
;
264 rply
.rm_direction
= REPLY
;
265 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
266 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
267 rply
.acpted_rply
.ar_stat
= GARBAGE_ARGS
;
268 SVC_REPLY(xprt
, &rply
);
275 svcerr_systemerr(xprt
)
276 register SVCXPRT
*xprt
;
280 rply
.rm_direction
= REPLY
;
281 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
282 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
283 rply
.acpted_rply
.ar_stat
= SYSTEM_ERR
;
284 SVC_REPLY(xprt
, &rply
);
288 * Authentication error reply
291 svcerr_auth(xprt
, why
)
297 rply
.rm_direction
= REPLY
;
298 rply
.rm_reply
.rp_stat
= MSG_DENIED
;
299 rply
.rjcted_rply
.rj_stat
= AUTH_ERROR
;
300 rply
.rjcted_rply
.rj_why
= why
;
301 SVC_REPLY(xprt
, &rply
);
305 * Auth too weak error reply
308 svcerr_weakauth(xprt
)
312 svcerr_auth(xprt
, AUTH_TOOWEAK
);
316 * Program unavailable error reply
320 register SVCXPRT
*xprt
;
324 rply
.rm_direction
= REPLY
;
325 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
326 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
327 rply
.acpted_rply
.ar_stat
= PROG_UNAVAIL
;
328 SVC_REPLY(xprt
, &rply
);
332 * Program version mismatch error reply
335 svcerr_progvers(xprt
, low_vers
, high_vers
)
336 register SVCXPRT
*xprt
;
342 rply
.rm_direction
= REPLY
;
343 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
344 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
345 rply
.acpted_rply
.ar_stat
= PROG_MISMATCH
;
346 rply
.acpted_rply
.ar_vers
.low
= low_vers
;
347 rply
.acpted_rply
.ar_vers
.high
= high_vers
;
348 SVC_REPLY(xprt
, &rply
);
351 /* ******************* SERVER INPUT STUFF ******************* */
354 * Get server side input from some transport.
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.
377 readfds
.fds_bits
[0] = rdfds
;
378 svc_getreqset(&readfds
);
380 int readfds
= rdfds
& svc_fds
;
382 svc_getreqset(&readfds
);
383 #endif /* def FD_SETSIZE */
387 svc_getreqset(readfds
)
394 int readfds_local
= *readfds
;
395 #endif /* def FD_SETSIZE */
402 register SVCXPRT
*xprt
;
403 register u_long mask
;
405 register u_int32_t
*maskp
;
406 register int setsize
;
408 char cred_area
[2*MAX_AUTH_BYTES
+ RQCRED_SIZE
];
409 msg
.rm_call
.cb_cred
.oa_base
= cred_area
;
410 msg
.rm_call
.cb_verf
.oa_base
= &(cred_area
[MAX_AUTH_BYTES
]);
411 r
.rq_clntcred
= &(cred_area
[2*MAX_AUTH_BYTES
]);
415 setsize
= _rpc_dtablesize();
416 maskp
= (u_int32_t
*)readfds
->fds_bits
;
417 for (sock
= 0; sock
< setsize
; sock
+= 32) {
418 for (mask
= *maskp
++; bit
= ffs(mask
); mask
^= (1 << (bit
- 1))) {
419 /* sock has input waiting */
420 xprt
= xports
[sock
+ bit
- 1];
422 for (sock
= 0; readfds_local
!= 0; sock
++, readfds_local
>>= 1) {
423 if ((readfds_local
& 1) != 0) {
424 /* sock has input waiting */
426 #endif /* def FD_SETSIZE */
427 /* now receive msgs from xprtprt (support batch calls) */
429 if (SVC_RECV(xprt
, &msg
)) {
431 /* now find the exported program and call it */
432 register struct svc_callout
*s
;
436 r
.rq_prog
= msg
.rm_call
.cb_prog
;
437 r
.rq_vers
= msg
.rm_call
.cb_vers
;
438 r
.rq_proc
= msg
.rm_call
.cb_proc
;
439 r
.rq_cred
= msg
.rm_call
.cb_cred
;
440 /* first authenticate the message */
441 if ((why
= _authenticate(&r
, &msg
)) != AUTH_OK
) {
442 svcerr_auth(xprt
, why
);
445 /* now match message with a registered service*/
449 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
) {
450 if (s
->sc_prog
== r
.rq_prog
) {
451 if (s
->sc_vers
== r
.rq_vers
) {
452 (*s
->sc_dispatch
)(&r
, xprt
);
454 } /* found correct version */
456 if (s
->sc_vers
< low_vers
)
457 low_vers
= s
->sc_vers
;
458 if (s
->sc_vers
> high_vers
)
459 high_vers
= s
->sc_vers
;
460 } /* found correct program */
463 * if we got here, the program or version
467 svcerr_progvers(xprt
,
468 low_vers
, high_vers
);
471 /* Fall through to ... */
474 if ((stat
= SVC_STAT(xprt
)) == XPRT_DIED
){
478 } while (stat
== XPRT_MOREREQS
);