]> git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/svc.c
Libinfo-78.2.tar.gz
[apple/libinfo.git] / rpc.subproj / svc.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
12 * this file.
13 *
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
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
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.
31 *
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.
35 *
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.
39 *
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.
43 *
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.
47 *
48 * Sun Microsystems, Inc.
49 * 2550 Garcia Avenue
50 * Mountain View, California 94043
51 */
52
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 $";
57 #endif
58
59 /*
60 * svc.c, Server-side remote procedure call interface.
61 *
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.
65 *
66 * Copyright (C) 1984, Sun Microsystems, Inc.
67 */
68
69 #include <sys/errno.h>
70 #include <rpc/rpc.h>
71 #include <rpc/pmap_clnt.h>
72
73 extern int errno;
74
75 static SVCXPRT **xports;
76
77 #define NULL_SVC ((struct svc_callout *)0)
78 #define RQCRED_SIZE 400 /* this size is excessive */
79
80 #define max(a, b) (a > b ? a : b)
81
82 /*
83 * The services list
84 * Each entry represents a set of procedures (an rpc program).
85 * The dispatch routine takes request structs and runs the
86 * apropriate procedure.
87 */
88 static struct svc_callout {
89 struct svc_callout *sc_next;
90 u_long sc_prog;
91 u_long sc_vers;
92 void (*sc_dispatch)();
93 } *svc_head;
94
95 static struct svc_callout *svc_find();
96
97 /* *************** SVCXPRT related stuff **************** */
98
99 extern int svc_maxfd;
100
101 /*
102 * Activate a transport handle.
103 */
104 void
105 xprt_register(xprt)
106 SVCXPRT *xprt;
107 {
108 register int sock = xprt->xp_sock;
109
110 if (xports == NULL) {
111 xports = (SVCXPRT **)
112 mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
113 }
114 if (sock < FD_SETSIZE) {
115 xports[sock] = xprt;
116 FD_SET(sock, &svc_fdset);
117 svc_maxfd = max(svc_maxfd, sock);
118 }
119 }
120
121 /*
122 * De-activate a transport handle.
123 */
124 void
125 xprt_unregister(xprt)
126 SVCXPRT *xprt;
127 {
128 register int sock = xprt->xp_sock;
129
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])
136 break;
137 }
138 }
139 }
140
141
142 /* ********************** CALLOUT list related stuff ************* */
143
144 /*
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.
148 */
149 bool_t
150 svc_register(xprt, prog, vers, dispatch, protocol)
151 SVCXPRT *xprt;
152 u_long prog;
153 u_long vers;
154 void (*dispatch)();
155 int protocol;
156 {
157 struct svc_callout *prev;
158 register struct svc_callout *s;
159
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 */
163 return (FALSE);
164 }
165 s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
166 if (s == (struct svc_callout *)0) {
167 return (FALSE);
168 }
169 s->sc_prog = prog;
170 s->sc_vers = vers;
171 s->sc_dispatch = dispatch;
172 s->sc_next = svc_head;
173 svc_head = s;
174 pmap_it:
175 /* now register the information with the local binder service */
176 if (protocol) {
177 return (pmap_set(prog, vers, protocol, xprt->xp_port));
178 }
179 return (TRUE);
180 }
181
182 /*
183 * Remove a service program from the callout list.
184 */
185 void
186 svc_unregister(prog, vers)
187 u_long prog;
188 u_long vers;
189 {
190 struct svc_callout *prev;
191 register struct svc_callout *s;
192
193 if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
194 return;
195 if (prev == NULL_SVC) {
196 svc_head = s->sc_next;
197 } else {
198 prev->sc_next = s->sc_next;
199 }
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);
204 }
205
206 /*
207 * Search the callout list for a program number, return the callout
208 * struct.
209 */
210 static struct svc_callout *
211 svc_find(prog, vers, prev)
212 u_long prog;
213 u_long vers;
214 struct svc_callout **prev;
215 {
216 register struct svc_callout *s, *p;
217
218 p = NULL_SVC;
219 for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
220 if ((s->sc_prog == prog) && (s->sc_vers == vers))
221 goto done;
222 p = s;
223 }
224 done:
225 *prev = p;
226 return (s);
227 }
228
229 /* ******************* REPLY GENERATION ROUTINES ************ */
230
231 /*
232 * Send a reply to an rpc request
233 */
234 bool_t
235 svc_sendreply(xprt, xdr_results, xdr_location)
236 register SVCXPRT *xprt;
237 xdrproc_t xdr_results;
238 caddr_t xdr_location;
239 {
240 struct rpc_msg rply;
241
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));
249 }
250
251 /*
252 * No procedure error reply
253 */
254 void
255 svcerr_noproc(xprt)
256 register SVCXPRT *xprt;
257 {
258 struct rpc_msg rply;
259
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);
265 }
266
267 /*
268 * Can't decode args error reply
269 */
270 void
271 svcerr_decode(xprt)
272 register SVCXPRT *xprt;
273 {
274 struct rpc_msg rply;
275
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);
281 }
282
283 /*
284 * Some system error
285 */
286 void
287 svcerr_systemerr(xprt)
288 register SVCXPRT *xprt;
289 {
290 struct rpc_msg rply;
291
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);
297 }
298
299 /*
300 * Authentication error reply
301 */
302 void
303 svcerr_auth(xprt, why)
304 SVCXPRT *xprt;
305 enum auth_stat why;
306 {
307 struct rpc_msg rply;
308
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);
314 }
315
316 /*
317 * Auth too weak error reply
318 */
319 void
320 svcerr_weakauth(xprt)
321 SVCXPRT *xprt;
322 {
323
324 svcerr_auth(xprt, AUTH_TOOWEAK);
325 }
326
327 /*
328 * Program unavailable error reply
329 */
330 void
331 svcerr_noprog(xprt)
332 register SVCXPRT *xprt;
333 {
334 struct rpc_msg rply;
335
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);
341 }
342
343 /*
344 * Program version mismatch error reply
345 */
346 void
347 svcerr_progvers(xprt, low_vers, high_vers)
348 register SVCXPRT *xprt;
349 u_long low_vers;
350 u_long high_vers;
351 {
352 struct rpc_msg rply;
353
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);
361 }
362
363 /* ******************* SERVER INPUT STUFF ******************* */
364
365 /*
366 * Get server side input from some transport.
367 *
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.
379 */
380
381 void
382 svc_getreq(rdfds)
383 int rdfds;
384 {
385 fd_set readfds;
386
387 FD_ZERO(&readfds);
388 readfds.fds_bits[0] = rdfds;
389 svc_getreqset(&readfds);
390 }
391
392 void
393 svc_getreqset(readfds)
394 fd_set *readfds;
395 {
396 enum xprt_stat stat;
397 struct rpc_msg msg;
398 int prog_found;
399 u_long low_vers;
400 u_long high_vers;
401 struct svc_req r;
402 register SVCXPRT *xprt;
403 register u_long mask;
404 register int bit;
405 register u_long *maskp;
406 register int sock;
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]);
411
412
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];
418 if (xprt == NULL)
419 /* But do we control sock? */
420 continue;
421 /* now receive msgs from xprtprt (support batch calls) */
422 do {
423 if (SVC_RECV(xprt, &msg)) {
424
425 /* now find the exported program and call it */
426 register struct svc_callout *s;
427 enum auth_stat why;
428
429 r.rq_xprt = xprt;
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);
437 goto call_done;
438 }
439 /* now match message with a registered service*/
440 prog_found = FALSE;
441 low_vers = 0 - 1;
442 high_vers = 0;
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);
447 goto call_done;
448 } /* found correct version */
449 prog_found = TRUE;
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 */
455 }
456 /*
457 * if we got here, the program or version
458 * is not served ...
459 */
460 if (prog_found)
461 svcerr_progvers(xprt,
462 low_vers, high_vers);
463 else
464 svcerr_noprog(xprt);
465 /* Fall through to ... */
466 }
467 call_done:
468 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
469 SVC_DESTROY(xprt);
470 break;
471 }
472 } while (stat == XPRT_MOREREQS);
473 }
474 }
475 }