Libinfo-173.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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
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.
32 *
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.
36 *
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.
40 *
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.
44 *
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.
48 *
49 * Sun Microsystems, Inc.
50 * 2550 Garcia Avenue
51 * Mountain View, California 94043
52 */
53
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 $";
58 #endif
59
60 /*
61 * svc.c, Server-side remote procedure call interface.
62 *
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.
66 *
67 * Copyright (C) 1984, Sun Microsystems, Inc.
68 */
69
70 #include <stdlib.h>
71 #include <string.h>
72 #include <sys/errno.h>
73 #include <rpc/rpc.h>
74 #include <rpc/pmap_clnt.h>
75
76 extern int errno;
77
78 static SVCXPRT **xports;
79
80 #define NULL_SVC ((struct svc_callout *)0)
81 #define RQCRED_SIZE 400 /* this size is excessive */
82
83 #define max(a, b) (a > b ? a : b)
84
85 /*
86 * The services list
87 * Each entry represents a set of procedures (an rpc program).
88 * The dispatch routine takes request structs and runs the
89 * apropriate procedure.
90 */
91 static struct svc_callout {
92 struct svc_callout *sc_next;
93 u_long sc_prog;
94 u_long sc_vers;
95 void (*sc_dispatch)();
96 } *svc_head;
97
98 static struct svc_callout *svc_find();
99
100 /* *************** SVCXPRT related stuff **************** */
101
102 extern int svc_maxfd;
103
104 /*
105 * Activate a transport handle.
106 */
107 void
108 xprt_register(xprt)
109 SVCXPRT *xprt;
110 {
111 register int sock = xprt->xp_sock;
112
113 if (xports == NULL) {
114 xports = (SVCXPRT **)
115 mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
116 }
117 if (sock < FD_SETSIZE) {
118 xports[sock] = xprt;
119 FD_SET(sock, &svc_fdset);
120 svc_maxfd = max(svc_maxfd, sock);
121 }
122 }
123
124 /*
125 * De-activate a transport handle.
126 */
127 void
128 xprt_unregister(xprt)
129 SVCXPRT *xprt;
130 {
131 register int sock = xprt->xp_sock;
132
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])
139 break;
140 }
141 }
142 }
143
144
145 /* ********************** CALLOUT list related stuff ************* */
146
147 /*
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.
151 */
152 bool_t
153 svc_register(xprt, prog, vers, dispatch, protocol)
154 SVCXPRT *xprt;
155 u_long prog;
156 u_long vers;
157 void (*dispatch)();
158 int protocol;
159 {
160 struct svc_callout *prev;
161 register struct svc_callout *s;
162
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 */
166 return (FALSE);
167 }
168 s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
169 if (s == (struct svc_callout *)0) {
170 return (FALSE);
171 }
172 s->sc_prog = prog;
173 s->sc_vers = vers;
174 s->sc_dispatch = dispatch;
175 s->sc_next = svc_head;
176 svc_head = s;
177 pmap_it:
178 /* now register the information with the local binder service */
179 if (protocol) {
180 return (pmap_set(prog, vers, protocol, xprt->xp_port));
181 }
182 return (TRUE);
183 }
184
185 /*
186 * Remove a service program from the callout list.
187 */
188 void
189 svc_unregister(prog, vers)
190 u_long prog;
191 u_long vers;
192 {
193 struct svc_callout *prev;
194 register struct svc_callout *s;
195
196 if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
197 return;
198 if (prev == NULL_SVC) {
199 svc_head = s->sc_next;
200 } else {
201 prev->sc_next = s->sc_next;
202 }
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);
207 }
208
209 /*
210 * Search the callout list for a program number, return the callout
211 * struct.
212 */
213 static struct svc_callout *
214 svc_find(prog, vers, prev)
215 u_long prog;
216 u_long vers;
217 struct svc_callout **prev;
218 {
219 register struct svc_callout *s, *p;
220
221 p = NULL_SVC;
222 for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
223 if ((s->sc_prog == prog) && (s->sc_vers == vers))
224 goto done;
225 p = s;
226 }
227 done:
228 *prev = p;
229 return (s);
230 }
231
232 /* ******************* REPLY GENERATION ROUTINES ************ */
233
234 /*
235 * Send a reply to an rpc request
236 */
237 bool_t
238 svc_sendreply(xprt, xdr_results, xdr_location)
239 register SVCXPRT *xprt;
240 xdrproc_t xdr_results;
241 caddr_t xdr_location;
242 {
243 struct rpc_msg rply;
244
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));
252 }
253
254 /*
255 * No procedure error reply
256 */
257 void
258 svcerr_noproc(xprt)
259 register SVCXPRT *xprt;
260 {
261 struct rpc_msg rply;
262
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);
268 }
269
270 /*
271 * Can't decode args error reply
272 */
273 void
274 svcerr_decode(xprt)
275 register SVCXPRT *xprt;
276 {
277 struct rpc_msg rply;
278
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);
284 }
285
286 /*
287 * Some system error
288 */
289 void
290 svcerr_systemerr(xprt)
291 register SVCXPRT *xprt;
292 {
293 struct rpc_msg rply;
294
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);
300 }
301
302 /*
303 * Authentication error reply
304 */
305 void
306 svcerr_auth(xprt, why)
307 SVCXPRT *xprt;
308 enum auth_stat why;
309 {
310 struct rpc_msg rply;
311
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);
317 }
318
319 /*
320 * Auth too weak error reply
321 */
322 void
323 svcerr_weakauth(xprt)
324 SVCXPRT *xprt;
325 {
326
327 svcerr_auth(xprt, AUTH_TOOWEAK);
328 }
329
330 /*
331 * Program unavailable error reply
332 */
333 void
334 svcerr_noprog(xprt)
335 register SVCXPRT *xprt;
336 {
337 struct rpc_msg rply;
338
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);
344 }
345
346 /*
347 * Program version mismatch error reply
348 */
349 void
350 svcerr_progvers(xprt, low_vers, high_vers)
351 register SVCXPRT *xprt;
352 u_long low_vers;
353 u_long high_vers;
354 {
355 struct rpc_msg rply;
356
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);
364 }
365
366 /* ******************* SERVER INPUT STUFF ******************* */
367
368 /*
369 * Get server side input from some transport.
370 *
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.
382 */
383
384 void
385 svc_getreq(rdfds)
386 int rdfds;
387 {
388 fd_set readfds;
389
390 FD_ZERO(&readfds);
391 readfds.fds_bits[0] = rdfds;
392 svc_getreqset(&readfds);
393 }
394
395 void
396 svc_getreqset(readfds)
397 fd_set *readfds;
398 {
399 enum xprt_stat stat;
400 struct rpc_msg msg;
401 int prog_found;
402 u_long low_vers;
403 u_long high_vers;
404 struct svc_req r;
405 register SVCXPRT *xprt;
406 register u_long mask;
407 register int bit;
408 register u_long *maskp;
409 register int sock;
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]);
414
415
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 */
421 return;
422 /* sock has input waiting */
423 xprt = xports[sock + bit - 1];
424 if (xprt == NULL)
425 /* But do we control sock? */
426 continue;
427 /* now receive msgs from xprtprt (support batch calls) */
428 do {
429 if (SVC_RECV(xprt, &msg)) {
430
431 /* now find the exported program and call it */
432 register struct svc_callout *s;
433 enum auth_stat why;
434
435 r.rq_xprt = xprt;
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);
443 goto call_done;
444 }
445 /* now match message with a registered service*/
446 prog_found = FALSE;
447 low_vers = 0 - 1;
448 high_vers = 0;
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);
453 goto call_done;
454 } /* found correct version */
455 prog_found = TRUE;
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 */
461 }
462 /*
463 * if we got here, the program or version
464 * is not served ...
465 */
466 if (prog_found)
467 svcerr_progvers(xprt,
468 low_vers, high_vers);
469 else
470 svcerr_noprog(xprt);
471 /* Fall through to ... */
472 }
473 call_done:
474 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
475 SVC_DESTROY(xprt);
476 break;
477 }
478 } while (stat == XPRT_MOREREQS);
479 }
480 }
481 }