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