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