svc.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 1.1 cgd * unrestricted use provided that this legend is included on all tape
4 1.1 cgd * media and as a part of the software program in whole or part. Users
5 1.1 cgd * may copy or modify Sun RPC without charge, but are not authorized
6 1.1 cgd * to license or distribute it to anyone else except as part of a product or
7 1.1 cgd * program developed by the user.
8 1.1 cgd *
9 1.1 cgd * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 1.1 cgd * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 1.1 cgd * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12 1.1 cgd *
13 1.1 cgd * Sun RPC is provided with no support and without any obligation on the
14 1.1 cgd * part of Sun Microsystems, Inc. to assist in its use, correction,
15 1.1 cgd * modification or enhancement.
16 1.1 cgd *
17 1.1 cgd * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 1.1 cgd * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 1.1 cgd * OR ANY PART THEREOF.
20 1.1 cgd *
21 1.1 cgd * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 1.1 cgd * or profits or other special, indirect and consequential damages, even if
23 1.1 cgd * Sun has been advised of the possibility of such damages.
24 1.1 cgd *
25 1.1 cgd * Sun Microsystems, Inc.
26 1.1 cgd * 2550 Garcia Avenue
27 1.1 cgd * Mountain View, California 94043
28 1.1 cgd */
29 1.1 cgd
30 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
31 1.1 cgd /*static char *sccsid = "from: @(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";*/
32 1.1 cgd /*static char *sccsid = "from: @(#)svc.c 2.4 88/08/11 4.0 RPCSRC";*/
33 1.1 cgd static char *rcsid = "$Id: svc.c,v 1.1 1993/10/07 07:30:14 cgd Exp $";
34 1.1 cgd #endif
35 1.1 cgd
36 1.1 cgd /*
37 1.1 cgd * svc.c, Server-side remote procedure call interface.
38 1.1 cgd *
39 1.1 cgd * There are two sets of procedures here. The xprt routines are
40 1.1 cgd * for handling transport handles. The svc routines handle the
41 1.1 cgd * list of service routines.
42 1.1 cgd *
43 1.1 cgd * Copyright (C) 1984, Sun Microsystems, Inc.
44 1.1 cgd */
45 1.1 cgd
46 1.1 cgd #include <sys/errno.h>
47 1.1 cgd #include <rpc/rpc.h>
48 1.1 cgd #include <rpc/pmap_clnt.h>
49 1.1 cgd
50 1.1 cgd extern int errno;
51 1.1 cgd
52 1.1 cgd #ifdef FD_SETSIZE
53 1.1 cgd static SVCXPRT **xports;
54 1.1 cgd #else
55 1.1 cgd #define NOFILE 32
56 1.1 cgd
57 1.1 cgd static SVCXPRT *xports[NOFILE];
58 1.1 cgd #endif /* def FD_SETSIZE */
59 1.1 cgd
60 1.1 cgd #define NULL_SVC ((struct svc_callout *)0)
61 1.1 cgd #define RQCRED_SIZE 400 /* this size is excessive */
62 1.1 cgd
63 1.1 cgd /*
64 1.1 cgd * The services list
65 1.1 cgd * Each entry represents a set of procedures (an rpc program).
66 1.1 cgd * The dispatch routine takes request structs and runs the
67 1.1 cgd * apropriate procedure.
68 1.1 cgd */
69 1.1 cgd static struct svc_callout {
70 1.1 cgd struct svc_callout *sc_next;
71 1.1 cgd u_long sc_prog;
72 1.1 cgd u_long sc_vers;
73 1.1 cgd void (*sc_dispatch)();
74 1.1 cgd } *svc_head;
75 1.1 cgd
76 1.1 cgd static struct svc_callout *svc_find();
77 1.1 cgd
78 1.1 cgd /* *************** SVCXPRT related stuff **************** */
79 1.1 cgd
80 1.1 cgd /*
81 1.1 cgd * Activate a transport handle.
82 1.1 cgd */
83 1.1 cgd void
84 1.1 cgd xprt_register(xprt)
85 1.1 cgd SVCXPRT *xprt;
86 1.1 cgd {
87 1.1 cgd register int sock = xprt->xp_sock;
88 1.1 cgd
89 1.1 cgd #ifdef FD_SETSIZE
90 1.1 cgd if (xports == NULL) {
91 1.1 cgd xports = (SVCXPRT **)
92 1.1 cgd mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
93 1.1 cgd }
94 1.1 cgd if (sock < _rpc_dtablesize()) {
95 1.1 cgd xports[sock] = xprt;
96 1.1 cgd FD_SET(sock, &svc_fdset);
97 1.1 cgd }
98 1.1 cgd #else
99 1.1 cgd if (sock < NOFILE) {
100 1.1 cgd xports[sock] = xprt;
101 1.1 cgd svc_fds |= (1 << sock);
102 1.1 cgd }
103 1.1 cgd #endif /* def FD_SETSIZE */
104 1.1 cgd
105 1.1 cgd }
106 1.1 cgd
107 1.1 cgd /*
108 1.1 cgd * De-activate a transport handle.
109 1.1 cgd */
110 1.1 cgd void
111 1.1 cgd xprt_unregister(xprt)
112 1.1 cgd SVCXPRT *xprt;
113 1.1 cgd {
114 1.1 cgd register int sock = xprt->xp_sock;
115 1.1 cgd
116 1.1 cgd #ifdef FD_SETSIZE
117 1.1 cgd if ((sock < _rpc_dtablesize()) && (xports[sock] == xprt)) {
118 1.1 cgd xports[sock] = (SVCXPRT *)0;
119 1.1 cgd FD_CLR(sock, &svc_fdset);
120 1.1 cgd }
121 1.1 cgd #else
122 1.1 cgd if ((sock < NOFILE) && (xports[sock] == xprt)) {
123 1.1 cgd xports[sock] = (SVCXPRT *)0;
124 1.1 cgd svc_fds &= ~(1 << sock);
125 1.1 cgd }
126 1.1 cgd #endif /* def FD_SETSIZE */
127 1.1 cgd }
128 1.1 cgd
129 1.1 cgd
130 1.1 cgd /* ********************** CALLOUT list related stuff ************* */
131 1.1 cgd
132 1.1 cgd /*
133 1.1 cgd * Add a service program to the callout list.
134 1.1 cgd * The dispatch routine will be called when a rpc request for this
135 1.1 cgd * program number comes in.
136 1.1 cgd */
137 1.1 cgd bool_t
138 1.1 cgd svc_register(xprt, prog, vers, dispatch, protocol)
139 1.1 cgd SVCXPRT *xprt;
140 1.1 cgd u_long prog;
141 1.1 cgd u_long vers;
142 1.1 cgd void (*dispatch)();
143 1.1 cgd int protocol;
144 1.1 cgd {
145 1.1 cgd struct svc_callout *prev;
146 1.1 cgd register struct svc_callout *s;
147 1.1 cgd
148 1.1 cgd if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
149 1.1 cgd if (s->sc_dispatch == dispatch)
150 1.1 cgd goto pmap_it; /* he is registering another xptr */
151 1.1 cgd return (FALSE);
152 1.1 cgd }
153 1.1 cgd s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
154 1.1 cgd if (s == (struct svc_callout *)0) {
155 1.1 cgd return (FALSE);
156 1.1 cgd }
157 1.1 cgd s->sc_prog = prog;
158 1.1 cgd s->sc_vers = vers;
159 1.1 cgd s->sc_dispatch = dispatch;
160 1.1 cgd s->sc_next = svc_head;
161 1.1 cgd svc_head = s;
162 1.1 cgd pmap_it:
163 1.1 cgd /* now register the information with the local binder service */
164 1.1 cgd if (protocol) {
165 1.1 cgd return (pmap_set(prog, vers, protocol, xprt->xp_port));
166 1.1 cgd }
167 1.1 cgd return (TRUE);
168 1.1 cgd }
169 1.1 cgd
170 1.1 cgd /*
171 1.1 cgd * Remove a service program from the callout list.
172 1.1 cgd */
173 1.1 cgd void
174 1.1 cgd svc_unregister(prog, vers)
175 1.1 cgd u_long prog;
176 1.1 cgd u_long vers;
177 1.1 cgd {
178 1.1 cgd struct svc_callout *prev;
179 1.1 cgd register struct svc_callout *s;
180 1.1 cgd
181 1.1 cgd if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
182 1.1 cgd return;
183 1.1 cgd if (prev == NULL_SVC) {
184 1.1 cgd svc_head = s->sc_next;
185 1.1 cgd } else {
186 1.1 cgd prev->sc_next = s->sc_next;
187 1.1 cgd }
188 1.1 cgd s->sc_next = NULL_SVC;
189 1.1 cgd mem_free((char *) s, (u_int) sizeof(struct svc_callout));
190 1.1 cgd /* now unregister the information with the local binder service */
191 1.1 cgd (void)pmap_unset(prog, vers);
192 1.1 cgd }
193 1.1 cgd
194 1.1 cgd /*
195 1.1 cgd * Search the callout list for a program number, return the callout
196 1.1 cgd * struct.
197 1.1 cgd */
198 1.1 cgd static struct svc_callout *
199 1.1 cgd svc_find(prog, vers, prev)
200 1.1 cgd u_long prog;
201 1.1 cgd u_long vers;
202 1.1 cgd struct svc_callout **prev;
203 1.1 cgd {
204 1.1 cgd register struct svc_callout *s, *p;
205 1.1 cgd
206 1.1 cgd p = NULL_SVC;
207 1.1 cgd for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
208 1.1 cgd if ((s->sc_prog == prog) && (s->sc_vers == vers))
209 1.1 cgd goto done;
210 1.1 cgd p = s;
211 1.1 cgd }
212 1.1 cgd done:
213 1.1 cgd *prev = p;
214 1.1 cgd return (s);
215 1.1 cgd }
216 1.1 cgd
217 1.1 cgd /* ******************* REPLY GENERATION ROUTINES ************ */
218 1.1 cgd
219 1.1 cgd /*
220 1.1 cgd * Send a reply to an rpc request
221 1.1 cgd */
222 1.1 cgd bool_t
223 1.1 cgd svc_sendreply(xprt, xdr_results, xdr_location)
224 1.1 cgd register SVCXPRT *xprt;
225 1.1 cgd xdrproc_t xdr_results;
226 1.1 cgd caddr_t xdr_location;
227 1.1 cgd {
228 1.1 cgd struct rpc_msg rply;
229 1.1 cgd
230 1.1 cgd rply.rm_direction = REPLY;
231 1.1 cgd rply.rm_reply.rp_stat = MSG_ACCEPTED;
232 1.1 cgd rply.acpted_rply.ar_verf = xprt->xp_verf;
233 1.1 cgd rply.acpted_rply.ar_stat = SUCCESS;
234 1.1 cgd rply.acpted_rply.ar_results.where = xdr_location;
235 1.1 cgd rply.acpted_rply.ar_results.proc = xdr_results;
236 1.1 cgd return (SVC_REPLY(xprt, &rply));
237 1.1 cgd }
238 1.1 cgd
239 1.1 cgd /*
240 1.1 cgd * No procedure error reply
241 1.1 cgd */
242 1.1 cgd void
243 1.1 cgd svcerr_noproc(xprt)
244 1.1 cgd register SVCXPRT *xprt;
245 1.1 cgd {
246 1.1 cgd struct rpc_msg rply;
247 1.1 cgd
248 1.1 cgd rply.rm_direction = REPLY;
249 1.1 cgd rply.rm_reply.rp_stat = MSG_ACCEPTED;
250 1.1 cgd rply.acpted_rply.ar_verf = xprt->xp_verf;
251 1.1 cgd rply.acpted_rply.ar_stat = PROC_UNAVAIL;
252 1.1 cgd SVC_REPLY(xprt, &rply);
253 1.1 cgd }
254 1.1 cgd
255 1.1 cgd /*
256 1.1 cgd * Can't decode args error reply
257 1.1 cgd */
258 1.1 cgd void
259 1.1 cgd svcerr_decode(xprt)
260 1.1 cgd register SVCXPRT *xprt;
261 1.1 cgd {
262 1.1 cgd struct rpc_msg rply;
263 1.1 cgd
264 1.1 cgd rply.rm_direction = REPLY;
265 1.1 cgd rply.rm_reply.rp_stat = MSG_ACCEPTED;
266 1.1 cgd rply.acpted_rply.ar_verf = xprt->xp_verf;
267 1.1 cgd rply.acpted_rply.ar_stat = GARBAGE_ARGS;
268 1.1 cgd SVC_REPLY(xprt, &rply);
269 1.1 cgd }
270 1.1 cgd
271 1.1 cgd /*
272 1.1 cgd * Some system error
273 1.1 cgd */
274 1.1 cgd void
275 1.1 cgd svcerr_systemerr(xprt)
276 1.1 cgd register SVCXPRT *xprt;
277 1.1 cgd {
278 1.1 cgd struct rpc_msg rply;
279 1.1 cgd
280 1.1 cgd rply.rm_direction = REPLY;
281 1.1 cgd rply.rm_reply.rp_stat = MSG_ACCEPTED;
282 1.1 cgd rply.acpted_rply.ar_verf = xprt->xp_verf;
283 1.1 cgd rply.acpted_rply.ar_stat = SYSTEM_ERR;
284 1.1 cgd SVC_REPLY(xprt, &rply);
285 1.1 cgd }
286 1.1 cgd
287 1.1 cgd /*
288 1.1 cgd * Authentication error reply
289 1.1 cgd */
290 1.1 cgd void
291 1.1 cgd svcerr_auth(xprt, why)
292 1.1 cgd SVCXPRT *xprt;
293 1.1 cgd enum auth_stat why;
294 1.1 cgd {
295 1.1 cgd struct rpc_msg rply;
296 1.1 cgd
297 1.1 cgd rply.rm_direction = REPLY;
298 1.1 cgd rply.rm_reply.rp_stat = MSG_DENIED;
299 1.1 cgd rply.rjcted_rply.rj_stat = AUTH_ERROR;
300 1.1 cgd rply.rjcted_rply.rj_why = why;
301 1.1 cgd SVC_REPLY(xprt, &rply);
302 1.1 cgd }
303 1.1 cgd
304 1.1 cgd /*
305 1.1 cgd * Auth too weak error reply
306 1.1 cgd */
307 1.1 cgd void
308 1.1 cgd svcerr_weakauth(xprt)
309 1.1 cgd SVCXPRT *xprt;
310 1.1 cgd {
311 1.1 cgd
312 1.1 cgd svcerr_auth(xprt, AUTH_TOOWEAK);
313 1.1 cgd }
314 1.1 cgd
315 1.1 cgd /*
316 1.1 cgd * Program unavailable error reply
317 1.1 cgd */
318 1.1 cgd void
319 1.1 cgd svcerr_noprog(xprt)
320 1.1 cgd register SVCXPRT *xprt;
321 1.1 cgd {
322 1.1 cgd struct rpc_msg rply;
323 1.1 cgd
324 1.1 cgd rply.rm_direction = REPLY;
325 1.1 cgd rply.rm_reply.rp_stat = MSG_ACCEPTED;
326 1.1 cgd rply.acpted_rply.ar_verf = xprt->xp_verf;
327 1.1 cgd rply.acpted_rply.ar_stat = PROG_UNAVAIL;
328 1.1 cgd SVC_REPLY(xprt, &rply);
329 1.1 cgd }
330 1.1 cgd
331 1.1 cgd /*
332 1.1 cgd * Program version mismatch error reply
333 1.1 cgd */
334 1.1 cgd void
335 1.1 cgd svcerr_progvers(xprt, low_vers, high_vers)
336 1.1 cgd register SVCXPRT *xprt;
337 1.1 cgd u_long low_vers;
338 1.1 cgd u_long high_vers;
339 1.1 cgd {
340 1.1 cgd struct rpc_msg rply;
341 1.1 cgd
342 1.1 cgd rply.rm_direction = REPLY;
343 1.1 cgd rply.rm_reply.rp_stat = MSG_ACCEPTED;
344 1.1 cgd rply.acpted_rply.ar_verf = xprt->xp_verf;
345 1.1 cgd rply.acpted_rply.ar_stat = PROG_MISMATCH;
346 1.1 cgd rply.acpted_rply.ar_vers.low = low_vers;
347 1.1 cgd rply.acpted_rply.ar_vers.high = high_vers;
348 1.1 cgd SVC_REPLY(xprt, &rply);
349 1.1 cgd }
350 1.1 cgd
351 1.1 cgd /* ******************* SERVER INPUT STUFF ******************* */
352 1.1 cgd
353 1.1 cgd /*
354 1.1 cgd * Get server side input from some transport.
355 1.1 cgd *
356 1.1 cgd * Statement of authentication parameters management:
357 1.1 cgd * This function owns and manages all authentication parameters, specifically
358 1.1 cgd * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
359 1.1 cgd * the "cooked" credentials (rqst->rq_clntcred).
360 1.1 cgd * However, this function does not know the structure of the cooked
361 1.1 cgd * credentials, so it make the following assumptions:
362 1.1 cgd * a) the structure is contiguous (no pointers), and
363 1.1 cgd * b) the cred structure size does not exceed RQCRED_SIZE bytes.
364 1.1 cgd * In all events, all three parameters are freed upon exit from this routine.
365 1.1 cgd * The storage is trivially management on the call stack in user land, but
366 1.1 cgd * is mallocated in kernel land.
367 1.1 cgd */
368 1.1 cgd
369 1.1 cgd void
370 1.1 cgd svc_getreq(rdfds)
371 1.1 cgd int rdfds;
372 1.1 cgd {
373 1.1 cgd #ifdef FD_SETSIZE
374 1.1 cgd fd_set readfds;
375 1.1 cgd
376 1.1 cgd FD_ZERO(&readfds);
377 1.1 cgd readfds.fds_bits[0] = rdfds;
378 1.1 cgd svc_getreqset(&readfds);
379 1.1 cgd #else
380 1.1 cgd int readfds = rdfds & svc_fds;
381 1.1 cgd
382 1.1 cgd svc_getreqset(&readfds);
383 1.1 cgd #endif /* def FD_SETSIZE */
384 1.1 cgd }
385 1.1 cgd
386 1.1 cgd void
387 1.1 cgd svc_getreqset(readfds)
388 1.1 cgd #ifdef FD_SETSIZE
389 1.1 cgd fd_set *readfds;
390 1.1 cgd {
391 1.1 cgd #else
392 1.1 cgd int *readfds;
393 1.1 cgd {
394 1.1 cgd int readfds_local = *readfds;
395 1.1 cgd #endif /* def FD_SETSIZE */
396 1.1 cgd enum xprt_stat stat;
397 1.1 cgd struct rpc_msg msg;
398 1.1 cgd int prog_found;
399 1.1 cgd u_long low_vers;
400 1.1 cgd u_long high_vers;
401 1.1 cgd struct svc_req r;
402 1.1 cgd register SVCXPRT *xprt;
403 1.1 cgd register u_long mask;
404 1.1 cgd register int bit;
405 1.1 cgd register u_long *maskp;
406 1.1 cgd register int setsize;
407 1.1 cgd register int sock;
408 1.1 cgd char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
409 1.1 cgd msg.rm_call.cb_cred.oa_base = cred_area;
410 1.1 cgd msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
411 1.1 cgd r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
412 1.1 cgd
413 1.1 cgd
414 1.1 cgd #ifdef FD_SETSIZE
415 1.1 cgd setsize = _rpc_dtablesize();
416 1.1 cgd maskp = (u_long *)readfds->fds_bits;
417 1.1 cgd for (sock = 0; sock < setsize; sock += NFDBITS) {
418 1.1 cgd for (mask = *maskp++; bit = ffs(mask); mask ^= (1 << (bit - 1))) {
419 1.1 cgd /* sock has input waiting */
420 1.1 cgd xprt = xports[sock + bit - 1];
421 1.1 cgd #else
422 1.1 cgd for (sock = 0; readfds_local != 0; sock++, readfds_local >>= 1) {
423 1.1 cgd if ((readfds_local & 1) != 0) {
424 1.1 cgd /* sock has input waiting */
425 1.1 cgd xprt = xports[sock];
426 1.1 cgd #endif /* def FD_SETSIZE */
427 1.1 cgd /* now receive msgs from xprtprt (support batch calls) */
428 1.1 cgd do {
429 1.1 cgd if (SVC_RECV(xprt, &msg)) {
430 1.1 cgd
431 1.1 cgd /* now find the exported program and call it */
432 1.1 cgd register struct svc_callout *s;
433 1.1 cgd enum auth_stat why;
434 1.1 cgd
435 1.1 cgd r.rq_xprt = xprt;
436 1.1 cgd r.rq_prog = msg.rm_call.cb_prog;
437 1.1 cgd r.rq_vers = msg.rm_call.cb_vers;
438 1.1 cgd r.rq_proc = msg.rm_call.cb_proc;
439 1.1 cgd r.rq_cred = msg.rm_call.cb_cred;
440 1.1 cgd /* first authenticate the message */
441 1.1 cgd if ((why= _authenticate(&r, &msg)) != AUTH_OK) {
442 1.1 cgd svcerr_auth(xprt, why);
443 1.1 cgd goto call_done;
444 1.1 cgd }
445 1.1 cgd /* now match message with a registered service*/
446 1.1 cgd prog_found = FALSE;
447 1.1 cgd low_vers = 0 - 1;
448 1.1 cgd high_vers = 0;
449 1.1 cgd for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
450 1.1 cgd if (s->sc_prog == r.rq_prog) {
451 1.1 cgd if (s->sc_vers == r.rq_vers) {
452 1.1 cgd (*s->sc_dispatch)(&r, xprt);
453 1.1 cgd goto call_done;
454 1.1 cgd } /* found correct version */
455 1.1 cgd prog_found = TRUE;
456 1.1 cgd if (s->sc_vers < low_vers)
457 1.1 cgd low_vers = s->sc_vers;
458 1.1 cgd if (s->sc_vers > high_vers)
459 1.1 cgd high_vers = s->sc_vers;
460 1.1 cgd } /* found correct program */
461 1.1 cgd }
462 1.1 cgd /*
463 1.1 cgd * if we got here, the program or version
464 1.1 cgd * is not served ...
465 1.1 cgd */
466 1.1 cgd if (prog_found)
467 1.1 cgd svcerr_progvers(xprt,
468 1.1 cgd low_vers, high_vers);
469 1.1 cgd else
470 1.1 cgd svcerr_noprog(xprt);
471 1.1 cgd /* Fall through to ... */
472 1.1 cgd }
473 1.1 cgd call_done:
474 1.1 cgd if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
475 1.1 cgd SVC_DESTROY(xprt);
476 1.1 cgd break;
477 1.1 cgd }
478 1.1 cgd } while (stat == XPRT_MOREREQS);
479 1.1 cgd }
480 1.1 cgd }
481 1.1 cgd }
482