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