]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
27 * unrestricted use provided that this legend is included on all tape
28 * media and as a part of the software program in whole or part. Users
29 * may copy or modify Sun RPC without charge, but are not authorized
30 * to license or distribute it to anyone else except as part of a product or
31 * program developed by the user.
33 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
34 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
37 * Sun RPC is provided with no support and without any obligation on the
38 * part of Sun Microsystems, Inc. to assist in its use, correction,
39 * modification or enhancement.
41 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
42 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
43 * OR ANY PART THEREOF.
45 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
46 * or profits or other special, indirect and consequential damages, even if
47 * Sun has been advised of the possibility of such damages.
49 * Sun Microsystems, Inc.
51 * Mountain View, California 94043
54 #if defined(LIBC_SCCS) && !defined(lint)
55 /*static char *sccsid = "from: @(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";*/
56 /*static char *sccsid = "from: @(#)svc.c 2.4 88/08/11 4.0 RPCSRC";*/
57 static char *rcsid
= "$Id: svc.c,v 1.4 2002/02/19 20:36:24 epeyton Exp $";
61 * svc.c, Server-side remote procedure call interface.
63 * There are two sets of procedures here. The xprt routines are
64 * for handling transport handles. The svc routines handle the
65 * list of service routines.
67 * Copyright (C) 1984, Sun Microsystems, Inc.
72 #include <sys/errno.h>
74 #include <rpc/pmap_clnt.h>
78 static SVCXPRT
**xports
;
80 #define NULL_SVC ((struct svc_callout *)0)
81 #define RQCRED_SIZE 400 /* this size is excessive */
83 #define max(a, b) (a > b ? a : b)
87 * Each entry represents a set of procedures (an rpc program).
88 * The dispatch routine takes request structs and runs the
89 * apropriate procedure.
91 static struct svc_callout
{
92 struct svc_callout
*sc_next
;
95 void (*sc_dispatch
)();
98 static struct svc_callout
*svc_find();
100 /* *************** SVCXPRT related stuff **************** */
102 extern int svc_maxfd
;
105 * Activate a transport handle.
111 register int sock
= xprt
->xp_sock
;
113 if (xports
== NULL
) {
114 xports
= (SVCXPRT
**)
115 mem_alloc(FD_SETSIZE
* sizeof(SVCXPRT
*));
117 if (sock
< FD_SETSIZE
) {
119 FD_SET(sock
, &svc_fdset
);
120 svc_maxfd
= max(svc_maxfd
, sock
);
125 * De-activate a transport handle.
128 xprt_unregister(xprt
)
131 register int sock
= xprt
->xp_sock
;
133 if ((sock
< FD_SETSIZE
) && (xports
[sock
] == xprt
)) {
134 xports
[sock
] = (SVCXPRT
*)0;
135 FD_CLR(sock
, &svc_fdset
);
136 if (sock
== svc_maxfd
) {
137 for (svc_maxfd
--; svc_maxfd
>=0; svc_maxfd
--)
138 if (xports
[svc_maxfd
])
145 /* ********************** CALLOUT list related stuff ************* */
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.
153 svc_register(xprt
, prog
, vers
, dispatch
, protocol
)
160 struct svc_callout
*prev
;
161 register struct svc_callout
*s
;
163 if ((s
= svc_find(prog
, vers
, &prev
)) != NULL_SVC
) {
164 if (s
->sc_dispatch
== dispatch
)
165 goto pmap_it
; /* he is registering another xptr */
168 s
= (struct svc_callout
*)mem_alloc(sizeof(struct svc_callout
));
169 if (s
== (struct svc_callout
*)0) {
174 s
->sc_dispatch
= dispatch
;
175 s
->sc_next
= svc_head
;
178 /* now register the information with the local binder service */
180 return (pmap_set(prog
, vers
, protocol
, xprt
->xp_port
));
186 * Remove a service program from the callout list.
189 svc_unregister(prog
, vers
)
193 struct svc_callout
*prev
;
194 register struct svc_callout
*s
;
196 if ((s
= svc_find(prog
, vers
, &prev
)) == NULL_SVC
)
198 if (prev
== NULL_SVC
) {
199 svc_head
= s
->sc_next
;
201 prev
->sc_next
= s
->sc_next
;
203 s
->sc_next
= NULL_SVC
;
204 mem_free((char *) s
, (u_int
) sizeof(struct svc_callout
));
205 /* now unregister the information with the local binder service */
206 (void)pmap_unset(prog
, vers
);
210 * Search the callout list for a program number, return the callout
213 static struct svc_callout
*
214 svc_find(prog
, vers
, prev
)
217 struct svc_callout
**prev
;
219 register struct svc_callout
*s
, *p
;
222 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
) {
223 if ((s
->sc_prog
== prog
) && (s
->sc_vers
== vers
))
232 /* ******************* REPLY GENERATION ROUTINES ************ */
235 * Send a reply to an rpc request
238 svc_sendreply(xprt
, xdr_results
, xdr_location
)
239 register SVCXPRT
*xprt
;
240 xdrproc_t xdr_results
;
241 caddr_t xdr_location
;
245 rply
.rm_direction
= REPLY
;
246 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
247 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
248 rply
.acpted_rply
.ar_stat
= SUCCESS
;
249 rply
.acpted_rply
.ar_results
.where
= xdr_location
;
250 rply
.acpted_rply
.ar_results
.proc
= xdr_results
;
251 return (SVC_REPLY(xprt
, &rply
));
255 * No procedure error reply
259 register SVCXPRT
*xprt
;
263 rply
.rm_direction
= REPLY
;
264 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
265 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
266 rply
.acpted_rply
.ar_stat
= PROC_UNAVAIL
;
267 SVC_REPLY(xprt
, &rply
);
271 * Can't decode args error reply
275 register SVCXPRT
*xprt
;
279 rply
.rm_direction
= REPLY
;
280 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
281 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
282 rply
.acpted_rply
.ar_stat
= GARBAGE_ARGS
;
283 SVC_REPLY(xprt
, &rply
);
290 svcerr_systemerr(xprt
)
291 register SVCXPRT
*xprt
;
295 rply
.rm_direction
= REPLY
;
296 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
297 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
298 rply
.acpted_rply
.ar_stat
= SYSTEM_ERR
;
299 SVC_REPLY(xprt
, &rply
);
303 * Authentication error reply
306 svcerr_auth(xprt
, why
)
312 rply
.rm_direction
= REPLY
;
313 rply
.rm_reply
.rp_stat
= MSG_DENIED
;
314 rply
.rjcted_rply
.rj_stat
= AUTH_ERROR
;
315 rply
.rjcted_rply
.rj_why
= why
;
316 SVC_REPLY(xprt
, &rply
);
320 * Auth too weak error reply
323 svcerr_weakauth(xprt
)
327 svcerr_auth(xprt
, AUTH_TOOWEAK
);
331 * Program unavailable error reply
335 register SVCXPRT
*xprt
;
339 rply
.rm_direction
= REPLY
;
340 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
341 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
342 rply
.acpted_rply
.ar_stat
= PROG_UNAVAIL
;
343 SVC_REPLY(xprt
, &rply
);
347 * Program version mismatch error reply
350 svcerr_progvers(xprt
, low_vers
, high_vers
)
351 register SVCXPRT
*xprt
;
357 rply
.rm_direction
= REPLY
;
358 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
359 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
360 rply
.acpted_rply
.ar_stat
= PROG_MISMATCH
;
361 rply
.acpted_rply
.ar_vers
.low
= low_vers
;
362 rply
.acpted_rply
.ar_vers
.high
= high_vers
;
363 SVC_REPLY(xprt
, &rply
);
366 /* ******************* SERVER INPUT STUFF ******************* */
369 * Get server side input from some transport.
371 * Statement of authentication parameters management:
372 * This function owns and manages all authentication parameters, specifically
373 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
374 * the "cooked" credentials (rqst->rq_clntcred).
375 * However, this function does not know the structure of the cooked
376 * credentials, so it make the following assumptions:
377 * a) the structure is contiguous (no pointers), and
378 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
379 * In all events, all three parameters are freed upon exit from this routine.
380 * The storage is trivially management on the call stack in user land, but
381 * is mallocated in kernel land.
391 readfds
.fds_bits
[0] = rdfds
;
392 svc_getreqset(&readfds
);
396 svc_getreqset(readfds
)
405 register SVCXPRT
*xprt
;
406 register u_long mask
;
408 register u_long
*maskp
;
410 char cred_area
[2*MAX_AUTH_BYTES
+ RQCRED_SIZE
];
411 msg
.rm_call
.cb_cred
.oa_base
= cred_area
;
412 msg
.rm_call
.cb_verf
.oa_base
= &(cred_area
[MAX_AUTH_BYTES
]);
413 r
.rq_clntcred
= &(cred_area
[2*MAX_AUTH_BYTES
]);
416 maskp
= (u_long
*)readfds
->fds_bits
;
417 for (sock
= 0; sock
<= svc_maxfd
; sock
+= NFDBITS
) {
418 for (mask
= *maskp
++; (bit
= ffs(mask
)); mask
^= (1 << (bit
- 1))) {
419 if ((sock
+ bit
) > (svc_maxfd
+ 1))
420 /* if we're past our sockets */
422 /* sock has input waiting */
423 xprt
= xports
[sock
+ bit
- 1];
425 /* But do we control sock? */
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
);