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