]>
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.2 1999/10/14 21:56:54 wsanchez 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.
69 #include <sys/errno.h>
71 #include <rpc/pmap_clnt.h>
75 static SVCXPRT
**xports
;
77 #define NULL_SVC ((struct svc_callout *)0)
78 #define RQCRED_SIZE 400 /* this size is excessive */
80 #define max(a, b) (a > b ? a : b)
84 * Each entry represents a set of procedures (an rpc program).
85 * The dispatch routine takes request structs and runs the
86 * apropriate procedure.
88 static struct svc_callout
{
89 struct svc_callout
*sc_next
;
92 void (*sc_dispatch
)();
95 static struct svc_callout
*svc_find();
97 /* *************** SVCXPRT related stuff **************** */
102 * Activate a transport handle.
108 register int sock
= xprt
->xp_sock
;
110 if (xports
== NULL
) {
111 xports
= (SVCXPRT
**)
112 mem_alloc(FD_SETSIZE
* sizeof(SVCXPRT
*));
114 if (sock
< FD_SETSIZE
) {
116 FD_SET(sock
, &svc_fdset
);
117 svc_maxfd
= max(svc_maxfd
, sock
);
122 * De-activate a transport handle.
125 xprt_unregister(xprt
)
128 register int sock
= xprt
->xp_sock
;
130 if ((sock
< FD_SETSIZE
) && (xports
[sock
] == xprt
)) {
131 xports
[sock
] = (SVCXPRT
*)0;
132 FD_CLR(sock
, &svc_fdset
);
133 if (sock
== svc_maxfd
) {
134 for (svc_maxfd
--; svc_maxfd
>=0; svc_maxfd
--)
135 if (xports
[svc_maxfd
])
142 /* ********************** CALLOUT list related stuff ************* */
145 * Add a service program to the callout list.
146 * The dispatch routine will be called when a rpc request for this
147 * program number comes in.
150 svc_register(xprt
, prog
, vers
, dispatch
, protocol
)
157 struct svc_callout
*prev
;
158 register struct svc_callout
*s
;
160 if ((s
= svc_find(prog
, vers
, &prev
)) != NULL_SVC
) {
161 if (s
->sc_dispatch
== dispatch
)
162 goto pmap_it
; /* he is registering another xptr */
165 s
= (struct svc_callout
*)mem_alloc(sizeof(struct svc_callout
));
166 if (s
== (struct svc_callout
*)0) {
171 s
->sc_dispatch
= dispatch
;
172 s
->sc_next
= svc_head
;
175 /* now register the information with the local binder service */
177 return (pmap_set(prog
, vers
, protocol
, xprt
->xp_port
));
183 * Remove a service program from the callout list.
186 svc_unregister(prog
, vers
)
190 struct svc_callout
*prev
;
191 register struct svc_callout
*s
;
193 if ((s
= svc_find(prog
, vers
, &prev
)) == NULL_SVC
)
195 if (prev
== NULL_SVC
) {
196 svc_head
= s
->sc_next
;
198 prev
->sc_next
= s
->sc_next
;
200 s
->sc_next
= NULL_SVC
;
201 mem_free((char *) s
, (u_int
) sizeof(struct svc_callout
));
202 /* now unregister the information with the local binder service */
203 (void)pmap_unset(prog
, vers
);
207 * Search the callout list for a program number, return the callout
210 static struct svc_callout
*
211 svc_find(prog
, vers
, prev
)
214 struct svc_callout
**prev
;
216 register struct svc_callout
*s
, *p
;
219 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
) {
220 if ((s
->sc_prog
== prog
) && (s
->sc_vers
== vers
))
229 /* ******************* REPLY GENERATION ROUTINES ************ */
232 * Send a reply to an rpc request
235 svc_sendreply(xprt
, xdr_results
, xdr_location
)
236 register SVCXPRT
*xprt
;
237 xdrproc_t xdr_results
;
238 caddr_t xdr_location
;
242 rply
.rm_direction
= REPLY
;
243 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
244 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
245 rply
.acpted_rply
.ar_stat
= SUCCESS
;
246 rply
.acpted_rply
.ar_results
.where
= xdr_location
;
247 rply
.acpted_rply
.ar_results
.proc
= xdr_results
;
248 return (SVC_REPLY(xprt
, &rply
));
252 * No procedure error reply
256 register SVCXPRT
*xprt
;
260 rply
.rm_direction
= REPLY
;
261 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
262 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
263 rply
.acpted_rply
.ar_stat
= PROC_UNAVAIL
;
264 SVC_REPLY(xprt
, &rply
);
268 * Can't decode args error reply
272 register SVCXPRT
*xprt
;
276 rply
.rm_direction
= REPLY
;
277 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
278 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
279 rply
.acpted_rply
.ar_stat
= GARBAGE_ARGS
;
280 SVC_REPLY(xprt
, &rply
);
287 svcerr_systemerr(xprt
)
288 register SVCXPRT
*xprt
;
292 rply
.rm_direction
= REPLY
;
293 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
294 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
295 rply
.acpted_rply
.ar_stat
= SYSTEM_ERR
;
296 SVC_REPLY(xprt
, &rply
);
300 * Authentication error reply
303 svcerr_auth(xprt
, why
)
309 rply
.rm_direction
= REPLY
;
310 rply
.rm_reply
.rp_stat
= MSG_DENIED
;
311 rply
.rjcted_rply
.rj_stat
= AUTH_ERROR
;
312 rply
.rjcted_rply
.rj_why
= why
;
313 SVC_REPLY(xprt
, &rply
);
317 * Auth too weak error reply
320 svcerr_weakauth(xprt
)
324 svcerr_auth(xprt
, AUTH_TOOWEAK
);
328 * Program unavailable error reply
332 register SVCXPRT
*xprt
;
336 rply
.rm_direction
= REPLY
;
337 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
338 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
339 rply
.acpted_rply
.ar_stat
= PROG_UNAVAIL
;
340 SVC_REPLY(xprt
, &rply
);
344 * Program version mismatch error reply
347 svcerr_progvers(xprt
, low_vers
, high_vers
)
348 register SVCXPRT
*xprt
;
354 rply
.rm_direction
= REPLY
;
355 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
356 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
357 rply
.acpted_rply
.ar_stat
= PROG_MISMATCH
;
358 rply
.acpted_rply
.ar_vers
.low
= low_vers
;
359 rply
.acpted_rply
.ar_vers
.high
= high_vers
;
360 SVC_REPLY(xprt
, &rply
);
363 /* ******************* SERVER INPUT STUFF ******************* */
366 * Get server side input from some transport.
368 * Statement of authentication parameters management:
369 * This function owns and manages all authentication parameters, specifically
370 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
371 * the "cooked" credentials (rqst->rq_clntcred).
372 * However, this function does not know the structure of the cooked
373 * credentials, so it make the following assumptions:
374 * a) the structure is contiguous (no pointers), and
375 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
376 * In all events, all three parameters are freed upon exit from this routine.
377 * The storage is trivially management on the call stack in user land, but
378 * is mallocated in kernel land.
388 readfds
.fds_bits
[0] = rdfds
;
389 svc_getreqset(&readfds
);
393 svc_getreqset(readfds
)
402 register SVCXPRT
*xprt
;
403 register u_long mask
;
405 register u_long
*maskp
;
407 char cred_area
[2*MAX_AUTH_BYTES
+ RQCRED_SIZE
];
408 msg
.rm_call
.cb_cred
.oa_base
= cred_area
;
409 msg
.rm_call
.cb_verf
.oa_base
= &(cred_area
[MAX_AUTH_BYTES
]);
410 r
.rq_clntcred
= &(cred_area
[2*MAX_AUTH_BYTES
]);
413 maskp
= (u_long
*)readfds
->fds_bits
;
414 for (sock
= 0; sock
< FD_SETSIZE
; sock
+= NFDBITS
) {
415 for (mask
= *maskp
++; bit
= ffs(mask
); mask
^= (1 << (bit
- 1))) {
416 /* sock has input waiting */
417 xprt
= xports
[sock
+ bit
- 1];
419 /* But do we control sock? */
421 /* now receive msgs from xprtprt (support batch calls) */
423 if (SVC_RECV(xprt
, &msg
)) {
425 /* now find the exported program and call it */
426 register struct svc_callout
*s
;
430 r
.rq_prog
= msg
.rm_call
.cb_prog
;
431 r
.rq_vers
= msg
.rm_call
.cb_vers
;
432 r
.rq_proc
= msg
.rm_call
.cb_proc
;
433 r
.rq_cred
= msg
.rm_call
.cb_cred
;
434 /* first authenticate the message */
435 if ((why
= _authenticate(&r
, &msg
)) != AUTH_OK
) {
436 svcerr_auth(xprt
, why
);
439 /* now match message with a registered service*/
443 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
) {
444 if (s
->sc_prog
== r
.rq_prog
) {
445 if (s
->sc_vers
== r
.rq_vers
) {
446 (*s
->sc_dispatch
)(&r
, xprt
);
448 } /* found correct version */
450 if (s
->sc_vers
< low_vers
)
451 low_vers
= s
->sc_vers
;
452 if (s
->sc_vers
> high_vers
)
453 high_vers
= s
->sc_vers
;
454 } /* found correct program */
457 * if we got here, the program or version
461 svcerr_progvers(xprt
,
462 low_vers
, high_vers
);
465 /* Fall through to ... */
468 if ((stat
= SVC_STAT(xprt
)) == XPRT_DIED
){
472 } while (stat
== XPRT_MOREREQS
);