Libinfo-278.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.4 2002/02/19 20:36:24 epeyton 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 <stdlib.h>
70 #include <string.h>
71 #include <sys/errno.h>
72 #include <rpc/rpc.h>
73 #include <rpc/pmap_clnt.h>
74
75 extern int errno;
76
77 static SVCXPRT **xports;
78
79 #define NULL_SVC ((struct svc_callout *)0)
80 #define RQCRED_SIZE 400 /* this size is excessive */
81
82 #define max(a, b) (a > b ? a : b)
83
84 /*
85 * The services list
86 * Each entry represents a set of procedures (an rpc program).
87 * The dispatch routine takes request structs and runs the
88 * apropriate procedure.
89 */
90 static struct svc_callout {
91 struct svc_callout *sc_next;
92 u_long sc_prog;
93 u_long sc_vers;
94 void (*sc_dispatch)();
95 } *svc_head;
96
97 static struct svc_callout *svc_find();
98
99 /* *************** SVCXPRT related stuff **************** */
100
101 extern int svc_maxfd;
102
103 /*
104 * Activate a transport handle.
105 */
106 void
107 xprt_register(xprt)
108 SVCXPRT *xprt;
109 {
110 register int sock = xprt->xp_sock;
111
112 if (xports == NULL) {
113 xports = (SVCXPRT **)
114 mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
115 }
116 if (sock < FD_SETSIZE) {
117 xports[sock] = xprt;
118 FD_SET(sock, &svc_fdset);
119 svc_maxfd = max(svc_maxfd, sock);
120 }
121 }
122
123 /*
124 * De-activate a transport handle.
125 */
126 void
127 xprt_unregister(xprt)
128 SVCXPRT *xprt;
129 {
130 register int sock = xprt->xp_sock;
131
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])
138 break;
139 }
140 }
141 }
142
143
144 /* ********************** CALLOUT list related stuff ************* */
145
146 /*
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.
150 */
151 bool_t
152 svc_register(xprt, prog, vers, dispatch, protocol)
153 #ifdef __LP64__
154 SVCXPRT *xprt;
155 uint32_t prog;
156 uint32_t vers;
157 void (*dispatch)();
158 int32_t protocol;
159 #else
160 SVCXPRT *xprt;
161 u_long prog;
162 u_long vers;
163 void (*dispatch)();
164 int protocol;
165 #endif
166 {
167 struct svc_callout *prev;
168 register struct svc_callout *s;
169
170 if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
171 if (s->sc_dispatch == dispatch)
172 goto pmap_it; /* he is registering another xptr */
173 return (FALSE);
174 }
175 s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
176 if (s == (struct svc_callout *)0) {
177 return (FALSE);
178 }
179 s->sc_prog = prog;
180 s->sc_vers = vers;
181 s->sc_dispatch = dispatch;
182 s->sc_next = svc_head;
183 svc_head = s;
184 pmap_it:
185 /* now register the information with the local binder service */
186 if (protocol) {
187 return (pmap_set(prog, vers, protocol, xprt->xp_port));
188 }
189 return (TRUE);
190 }
191
192 /*
193 * Remove a service program from the callout list.
194 */
195 void
196 svc_unregister(prog, vers)
197 #ifdef __LP64__
198 uint32_t prog;
199 uint32_t vers;
200 #else
201 u_long prog;
202 u_long vers;
203 #endif
204 {
205 struct svc_callout *prev;
206 register struct svc_callout *s;
207
208 if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
209 return;
210 if (prev == NULL_SVC) {
211 svc_head = s->sc_next;
212 } else {
213 prev->sc_next = s->sc_next;
214 }
215 s->sc_next = NULL_SVC;
216 mem_free((char *) s, (u_int) sizeof(struct svc_callout));
217 /* now unregister the information with the local binder service */
218 (void)pmap_unset(prog, vers);
219 }
220
221 /*
222 * Search the callout list for a program number, return the callout
223 * struct.
224 */
225 static struct svc_callout *
226 svc_find(prog, vers, prev)
227 u_long prog;
228 u_long vers;
229 struct svc_callout **prev;
230 {
231 register struct svc_callout *s, *p;
232
233 p = NULL_SVC;
234 for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
235 if ((s->sc_prog == prog) && (s->sc_vers == vers))
236 goto done;
237 p = s;
238 }
239 done:
240 *prev = p;
241 return (s);
242 }
243
244 /* ******************* REPLY GENERATION ROUTINES ************ */
245
246 /*
247 * Send a reply to an rpc request
248 */
249 bool_t
250 svc_sendreply(xprt, xdr_results, xdr_location)
251 register SVCXPRT *xprt;
252 xdrproc_t xdr_results;
253 caddr_t xdr_location;
254 {
255 struct rpc_msg rply;
256
257 rply.rm_direction = REPLY;
258 rply.rm_reply.rp_stat = MSG_ACCEPTED;
259 rply.acpted_rply.ar_verf = xprt->xp_verf;
260 rply.acpted_rply.ar_stat = SUCCESS;
261 rply.acpted_rply.ar_results.where = xdr_location;
262 rply.acpted_rply.ar_results.proc = xdr_results;
263 return (SVC_REPLY(xprt, &rply));
264 }
265
266 /*
267 * No procedure error reply
268 */
269 void
270 svcerr_noproc(xprt)
271 register SVCXPRT *xprt;
272 {
273 struct rpc_msg rply;
274
275 rply.rm_direction = REPLY;
276 rply.rm_reply.rp_stat = MSG_ACCEPTED;
277 rply.acpted_rply.ar_verf = xprt->xp_verf;
278 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
279 SVC_REPLY(xprt, &rply);
280 }
281
282 /*
283 * Can't decode args error reply
284 */
285 void
286 svcerr_decode(xprt)
287 register SVCXPRT *xprt;
288 {
289 struct rpc_msg rply;
290
291 rply.rm_direction = REPLY;
292 rply.rm_reply.rp_stat = MSG_ACCEPTED;
293 rply.acpted_rply.ar_verf = xprt->xp_verf;
294 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
295 SVC_REPLY(xprt, &rply);
296 }
297
298 /*
299 * Some system error
300 */
301 void
302 svcerr_systemerr(xprt)
303 register SVCXPRT *xprt;
304 {
305 struct rpc_msg rply;
306
307 rply.rm_direction = REPLY;
308 rply.rm_reply.rp_stat = MSG_ACCEPTED;
309 rply.acpted_rply.ar_verf = xprt->xp_verf;
310 rply.acpted_rply.ar_stat = SYSTEM_ERR;
311 SVC_REPLY(xprt, &rply);
312 }
313
314 /*
315 * Authentication error reply
316 */
317 void
318 svcerr_auth(xprt, why)
319 SVCXPRT *xprt;
320 enum auth_stat why;
321 {
322 struct rpc_msg rply;
323
324 rply.rm_direction = REPLY;
325 rply.rm_reply.rp_stat = MSG_DENIED;
326 rply.rjcted_rply.rj_stat = AUTH_ERROR;
327 rply.rjcted_rply.rj_why = why;
328 SVC_REPLY(xprt, &rply);
329 }
330
331 /*
332 * Auth too weak error reply
333 */
334 void
335 svcerr_weakauth(xprt)
336 SVCXPRT *xprt;
337 {
338
339 svcerr_auth(xprt, AUTH_TOOWEAK);
340 }
341
342 /*
343 * Program unavailable error reply
344 */
345 void
346 svcerr_noprog(xprt)
347 register SVCXPRT *xprt;
348 {
349 struct rpc_msg rply;
350
351 rply.rm_direction = REPLY;
352 rply.rm_reply.rp_stat = MSG_ACCEPTED;
353 rply.acpted_rply.ar_verf = xprt->xp_verf;
354 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
355 SVC_REPLY(xprt, &rply);
356 }
357
358 /*
359 * Program version mismatch error reply
360 */
361 void
362 svcerr_progvers(xprt, low_vers, high_vers)
363 #ifdef __LP64__
364 register SVCXPRT *xprt;
365 uint32_t low_vers;
366 uint32_t high_vers;
367 #else
368 register SVCXPRT *xprt;
369 u_long low_vers;
370 u_long high_vers;
371 #endif
372 {
373 struct rpc_msg rply;
374
375 rply.rm_direction = REPLY;
376 rply.rm_reply.rp_stat = MSG_ACCEPTED;
377 rply.acpted_rply.ar_verf = xprt->xp_verf;
378 rply.acpted_rply.ar_stat = PROG_MISMATCH;
379 rply.acpted_rply.ar_vers.low = low_vers;
380 rply.acpted_rply.ar_vers.high = high_vers;
381 SVC_REPLY(xprt, &rply);
382 }
383
384 /* ******************* SERVER INPUT STUFF ******************* */
385
386 /*
387 * Get server side input from some transport.
388 *
389 * Statement of authentication parameters management:
390 * This function owns and manages all authentication parameters, specifically
391 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
392 * the "cooked" credentials (rqst->rq_clntcred).
393 * However, this function does not know the structure of the cooked
394 * credentials, so it make the following assumptions:
395 * a) the structure is contiguous (no pointers), and
396 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
397 * In all events, all three parameters are freed upon exit from this routine.
398 * The storage is trivially management on the call stack in user land, but
399 * is mallocated in kernel land.
400 */
401
402 void
403 svc_getreq(rdfds)
404 int rdfds;
405 {
406 fd_set readfds;
407
408 FD_ZERO(&readfds);
409 readfds.fds_bits[0] = rdfds;
410 svc_getreqset(&readfds);
411 }
412
413 void
414 svc_getreqset(readfds)
415 fd_set *readfds;
416 {
417 enum xprt_stat stat;
418 struct rpc_msg msg;
419 int prog_found;
420 u_long low_vers;
421 u_long high_vers;
422 struct svc_req r;
423 register SVCXPRT *xprt;
424 register u_long mask;
425 register int bit;
426 register u_long *maskp;
427 register int sock;
428 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
429 msg.rm_call.cb_cred.oa_base = cred_area;
430 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
431 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
432
433
434 maskp = (u_long *)readfds->fds_bits;
435 for (sock = 0; sock <= svc_maxfd; sock += NFDBITS) {
436 for (mask = *maskp++; (bit = ffs(mask)); mask ^= (1 << (bit - 1))) {
437 if ((sock + bit) > (svc_maxfd + 1))
438 /* if we're past our sockets */
439 return;
440 /* sock has input waiting */
441 xprt = xports[sock + bit - 1];
442 if (xprt == NULL)
443 /* But do we control sock? */
444 continue;
445 /* now receive msgs from xprtprt (support batch calls) */
446 do {
447 if (SVC_RECV(xprt, &msg)) {
448
449 /* now find the exported program and call it */
450 register struct svc_callout *s;
451 enum auth_stat why;
452
453 r.rq_xprt = xprt;
454 r.rq_prog = msg.rm_call.cb_prog;
455 r.rq_vers = msg.rm_call.cb_vers;
456 r.rq_proc = msg.rm_call.cb_proc;
457 r.rq_cred = msg.rm_call.cb_cred;
458 /* first authenticate the message */
459 if ((why= _authenticate(&r, &msg)) != AUTH_OK) {
460 svcerr_auth(xprt, why);
461 goto call_done;
462 }
463 /* now match message with a registered service*/
464 prog_found = FALSE;
465 low_vers = 0 - 1;
466 high_vers = 0;
467 for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
468 if (s->sc_prog == r.rq_prog) {
469 if (s->sc_vers == r.rq_vers) {
470 (*s->sc_dispatch)(&r, xprt);
471 goto call_done;
472 } /* found correct version */
473 prog_found = TRUE;
474 if (s->sc_vers < low_vers)
475 low_vers = s->sc_vers;
476 if (s->sc_vers > high_vers)
477 high_vers = s->sc_vers;
478 } /* found correct program */
479 }
480 /*
481 * if we got here, the program or version
482 * is not served ...
483 */
484 if (prog_found)
485 svcerr_progvers(xprt,
486 low_vers, high_vers);
487 else
488 svcerr_noprog(xprt);
489 /* Fall through to ... */
490 }
491 call_done:
492 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
493 SVC_DESTROY(xprt);
494 break;
495 }
496 } while (stat == XPRT_MOREREQS);
497 }
498 }
499 }