]>
git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/svc.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26 * unrestricted use provided that this legend is included on all tape
27 * media and as a part of the software program in whole or part. Users
28 * may copy or modify Sun RPC without charge, but are not authorized
29 * to license or distribute it to anyone else except as part of a product or
30 * program developed by the user.
32 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
33 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
36 * Sun RPC is provided with no support and without any obligation on the
37 * part of Sun Microsystems, Inc. to assist in its use, correction,
38 * modification or enhancement.
40 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
41 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
42 * OR ANY PART THEREOF.
44 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
45 * or profits or other special, indirect and consequential damages, even if
46 * Sun has been advised of the possibility of such damages.
48 * Sun Microsystems, Inc.
50 * Mountain View, California 94043
53 #if defined(LIBC_SCCS) && !defined(lint)
54 /*static char *sccsid = "from: @(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";*/
55 /*static char *sccsid = "from: @(#)svc.c 2.4 88/08/11 4.0 RPCSRC";*/
56 static char *rcsid
= "$Id: svc.c,v 1.4 2002/02/19 20:36:24 epeyton Exp $";
60 * svc.c, Server-side remote procedure call interface.
62 * There are two sets of procedures here. The xprt routines are
63 * for handling transport handles. The svc routines handle the
64 * list of service routines.
66 * Copyright (C) 1984, Sun Microsystems, Inc.
71 #include <sys/errno.h>
73 #include <rpc/pmap_clnt.h>
77 static SVCXPRT
**xports
;
79 #define NULL_SVC ((struct svc_callout *)0)
80 #define RQCRED_SIZE 400 /* this size is excessive */
82 #define max(a, b) (a > b ? a : b)
86 * Each entry represents a set of procedures (an rpc program).
87 * The dispatch routine takes request structs and runs the
88 * apropriate procedure.
90 static struct svc_callout
{
91 struct svc_callout
*sc_next
;
94 void (*sc_dispatch
)();
97 static struct svc_callout
*svc_find();
99 /* *************** SVCXPRT related stuff **************** */
101 extern int svc_maxfd
;
104 * Activate a transport handle.
110 register int sock
= xprt
->xp_sock
;
112 if (xports
== NULL
) {
113 xports
= (SVCXPRT
**)
114 mem_alloc(FD_SETSIZE
* sizeof(SVCXPRT
*));
116 if (sock
< FD_SETSIZE
) {
118 FD_SET(sock
, &svc_fdset
);
119 svc_maxfd
= max(svc_maxfd
, sock
);
124 * De-activate a transport handle.
127 xprt_unregister(xprt
)
130 register int sock
= xprt
->xp_sock
;
132 if ((sock
< FD_SETSIZE
) && (xports
[sock
] == xprt
)) {
133 xports
[sock
] = (SVCXPRT
*)0;
134 FD_CLR(sock
, &svc_fdset
);
135 if (sock
== svc_maxfd
) {
136 for (svc_maxfd
--; svc_maxfd
>=0; svc_maxfd
--)
137 if (xports
[svc_maxfd
])
144 /* ********************** CALLOUT list related stuff ************* */
147 * Add a service program to the callout list.
148 * The dispatch routine will be called when a rpc request for this
149 * program number comes in.
152 svc_register(xprt
, prog
, vers
, dispatch
, protocol
)
159 struct svc_callout
*prev
;
160 register struct svc_callout
*s
;
162 if ((s
= svc_find(prog
, vers
, &prev
)) != NULL_SVC
) {
163 if (s
->sc_dispatch
== dispatch
)
164 goto pmap_it
; /* he is registering another xptr */
167 s
= (struct svc_callout
*)mem_alloc(sizeof(struct svc_callout
));
168 if (s
== (struct svc_callout
*)0) {
173 s
->sc_dispatch
= dispatch
;
174 s
->sc_next
= svc_head
;
177 /* now register the information with the local binder service */
179 return (pmap_set(prog
, vers
, protocol
, xprt
->xp_port
));
185 * Remove a service program from the callout list.
188 svc_unregister(prog
, vers
)
192 struct svc_callout
*prev
;
193 register struct svc_callout
*s
;
195 if ((s
= svc_find(prog
, vers
, &prev
)) == NULL_SVC
)
197 if (prev
== NULL_SVC
) {
198 svc_head
= s
->sc_next
;
200 prev
->sc_next
= s
->sc_next
;
202 s
->sc_next
= NULL_SVC
;
203 mem_free((char *) s
, (u_int
) sizeof(struct svc_callout
));
204 /* now unregister the information with the local binder service */
205 (void)pmap_unset(prog
, vers
);
209 * Search the callout list for a program number, return the callout
212 static struct svc_callout
*
213 svc_find(prog
, vers
, prev
)
216 struct svc_callout
**prev
;
218 register struct svc_callout
*s
, *p
;
221 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
) {
222 if ((s
->sc_prog
== prog
) && (s
->sc_vers
== vers
))
231 /* ******************* REPLY GENERATION ROUTINES ************ */
234 * Send a reply to an rpc request
237 svc_sendreply(xprt
, xdr_results
, xdr_location
)
238 register SVCXPRT
*xprt
;
239 xdrproc_t xdr_results
;
240 caddr_t xdr_location
;
244 rply
.rm_direction
= REPLY
;
245 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
246 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
247 rply
.acpted_rply
.ar_stat
= SUCCESS
;
248 rply
.acpted_rply
.ar_results
.where
= xdr_location
;
249 rply
.acpted_rply
.ar_results
.proc
= xdr_results
;
250 return (SVC_REPLY(xprt
, &rply
));
254 * No procedure error reply
258 register SVCXPRT
*xprt
;
262 rply
.rm_direction
= REPLY
;
263 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
264 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
265 rply
.acpted_rply
.ar_stat
= PROC_UNAVAIL
;
266 SVC_REPLY(xprt
, &rply
);
270 * Can't decode args error reply
274 register SVCXPRT
*xprt
;
278 rply
.rm_direction
= REPLY
;
279 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
280 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
281 rply
.acpted_rply
.ar_stat
= GARBAGE_ARGS
;
282 SVC_REPLY(xprt
, &rply
);
289 svcerr_systemerr(xprt
)
290 register SVCXPRT
*xprt
;
294 rply
.rm_direction
= REPLY
;
295 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
296 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
297 rply
.acpted_rply
.ar_stat
= SYSTEM_ERR
;
298 SVC_REPLY(xprt
, &rply
);
302 * Authentication error reply
305 svcerr_auth(xprt
, why
)
311 rply
.rm_direction
= REPLY
;
312 rply
.rm_reply
.rp_stat
= MSG_DENIED
;
313 rply
.rjcted_rply
.rj_stat
= AUTH_ERROR
;
314 rply
.rjcted_rply
.rj_why
= why
;
315 SVC_REPLY(xprt
, &rply
);
319 * Auth too weak error reply
322 svcerr_weakauth(xprt
)
326 svcerr_auth(xprt
, AUTH_TOOWEAK
);
330 * Program unavailable error reply
334 register SVCXPRT
*xprt
;
338 rply
.rm_direction
= REPLY
;
339 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
340 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
341 rply
.acpted_rply
.ar_stat
= PROG_UNAVAIL
;
342 SVC_REPLY(xprt
, &rply
);
346 * Program version mismatch error reply
349 svcerr_progvers(xprt
, low_vers
, high_vers
)
350 register SVCXPRT
*xprt
;
356 rply
.rm_direction
= REPLY
;
357 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
358 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
359 rply
.acpted_rply
.ar_stat
= PROG_MISMATCH
;
360 rply
.acpted_rply
.ar_vers
.low
= low_vers
;
361 rply
.acpted_rply
.ar_vers
.high
= high_vers
;
362 SVC_REPLY(xprt
, &rply
);
365 /* ******************* SERVER INPUT STUFF ******************* */
368 * Get server side input from some transport.
370 * Statement of authentication parameters management:
371 * This function owns and manages all authentication parameters, specifically
372 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
373 * the "cooked" credentials (rqst->rq_clntcred).
374 * However, this function does not know the structure of the cooked
375 * credentials, so it make the following assumptions:
376 * a) the structure is contiguous (no pointers), and
377 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
378 * In all events, all three parameters are freed upon exit from this routine.
379 * The storage is trivially management on the call stack in user land, but
380 * is mallocated in kernel land.
390 readfds
.fds_bits
[0] = rdfds
;
391 svc_getreqset(&readfds
);
395 svc_getreqset(readfds
)
404 register SVCXPRT
*xprt
;
405 register u_long mask
;
407 register u_long
*maskp
;
409 char cred_area
[2*MAX_AUTH_BYTES
+ RQCRED_SIZE
];
410 msg
.rm_call
.cb_cred
.oa_base
= cred_area
;
411 msg
.rm_call
.cb_verf
.oa_base
= &(cred_area
[MAX_AUTH_BYTES
]);
412 r
.rq_clntcred
= &(cred_area
[2*MAX_AUTH_BYTES
]);
415 maskp
= (u_long
*)readfds
->fds_bits
;
416 for (sock
= 0; sock
<= svc_maxfd
; sock
+= NFDBITS
) {
417 for (mask
= *maskp
++; (bit
= ffs(mask
)); mask
^= (1 << (bit
- 1))) {
418 if ((sock
+ bit
) > (svc_maxfd
+ 1))
419 /* if we're past our sockets */
421 /* sock has input waiting */
422 xprt
= xports
[sock
+ bit
- 1];
424 /* But do we control sock? */
426 /* now receive msgs from xprtprt (support batch calls) */
428 if (SVC_RECV(xprt
, &msg
)) {
430 /* now find the exported program and call it */
431 register struct svc_callout
*s
;
435 r
.rq_prog
= msg
.rm_call
.cb_prog
;
436 r
.rq_vers
= msg
.rm_call
.cb_vers
;
437 r
.rq_proc
= msg
.rm_call
.cb_proc
;
438 r
.rq_cred
= msg
.rm_call
.cb_cred
;
439 /* first authenticate the message */
440 if ((why
= _authenticate(&r
, &msg
)) != AUTH_OK
) {
441 svcerr_auth(xprt
, why
);
444 /* now match message with a registered service*/
448 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
) {
449 if (s
->sc_prog
== r
.rq_prog
) {
450 if (s
->sc_vers
== r
.rq_vers
) {
451 (*s
->sc_dispatch
)(&r
, xprt
);
453 } /* found correct version */
455 if (s
->sc_vers
< low_vers
)
456 low_vers
= s
->sc_vers
;
457 if (s
->sc_vers
> high_vers
)
458 high_vers
= s
->sc_vers
;
459 } /* found correct program */
462 * if we got here, the program or version
466 svcerr_progvers(xprt
,
467 low_vers
, high_vers
);
470 /* Fall through to ... */
473 if ((stat
= SVC_STAT(xprt
)) == XPRT_DIED
){
477 } while (stat
== XPRT_MOREREQS
);