svc.c revision 1.26 1 1.26 christos /* $NetBSD: svc.c,v 1.26 2005/11/29 03:12:00 christos 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.26 christos __RCSID("$NetBSD: svc.c,v 1.26 2005/11/29 03:12:00 christos 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.20 fvdl #include "reentrant.h"
54 1.20 fvdl #include <sys/types.h>
55 1.20 fvdl #include <sys/poll.h>
56 1.17 lukem #include <assert.h>
57 1.14 lukem #include <errno.h>
58 1.4 cgd #include <stdlib.h>
59 1.8 pk #include <string.h>
60 1.4 cgd
61 1.1 cgd #include <rpc/rpc.h>
62 1.20 fvdl #ifdef PORTMAP
63 1.1 cgd #include <rpc/pmap_clnt.h>
64 1.20 fvdl #endif
65 1.20 fvdl
66 1.23 fvdl #include "rpc_internal.h"
67 1.11 jtc
68 1.11 jtc #ifdef __weak_alias
69 1.19 mycroft __weak_alias(svc_getreq,_svc_getreq)
70 1.19 mycroft __weak_alias(svc_getreqset,_svc_getreqset)
71 1.20 fvdl __weak_alias(svc_getreq_common,_svc_getreq_common)
72 1.19 mycroft __weak_alias(svc_register,_svc_register)
73 1.20 fvdl __weak_alias(svc_reg,_svc_reg)
74 1.20 fvdl __weak_alias(svc_unreg,_svc_unreg)
75 1.19 mycroft __weak_alias(svc_sendreply,_svc_sendreply)
76 1.19 mycroft __weak_alias(svc_unregister,_svc_unregister)
77 1.19 mycroft __weak_alias(svcerr_auth,_svcerr_auth)
78 1.19 mycroft __weak_alias(svcerr_decode,_svcerr_decode)
79 1.19 mycroft __weak_alias(svcerr_noproc,_svcerr_noproc)
80 1.19 mycroft __weak_alias(svcerr_noprog,_svcerr_noprog)
81 1.19 mycroft __weak_alias(svcerr_progvers,_svcerr_progvers)
82 1.19 mycroft __weak_alias(svcerr_systemerr,_svcerr_systemerr)
83 1.19 mycroft __weak_alias(svcerr_weakauth,_svcerr_weakauth)
84 1.19 mycroft __weak_alias(xprt_register,_xprt_register)
85 1.19 mycroft __weak_alias(xprt_unregister,_xprt_unregister)
86 1.23 fvdl __weak_alias(rpc_control,_rpc_control)
87 1.11 jtc #endif
88 1.1 cgd
89 1.23 fvdl SVCXPRT **__svc_xports;
90 1.23 fvdl int __svc_maxrec;
91 1.1 cgd
92 1.1 cgd #define RQCRED_SIZE 400 /* this size is excessive */
93 1.1 cgd
94 1.20 fvdl #define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */
95 1.20 fvdl #define version_keepquiet(xp) ((u_long)(xp)->xp_p3 & SVC_VERSQUIET)
96 1.20 fvdl
97 1.2 deraadt #define max(a, b) (a > b ? a : b)
98 1.2 deraadt
99 1.1 cgd /*
100 1.1 cgd * The services list
101 1.1 cgd * Each entry represents a set of procedures (an rpc program).
102 1.1 cgd * The dispatch routine takes request structs and runs the
103 1.1 cgd * apropriate procedure.
104 1.1 cgd */
105 1.1 cgd static struct svc_callout {
106 1.1 cgd struct svc_callout *sc_next;
107 1.20 fvdl rpcprog_t sc_prog;
108 1.20 fvdl rpcvers_t sc_vers;
109 1.20 fvdl char *sc_netid;
110 1.10 christos void (*sc_dispatch) __P((struct svc_req *, SVCXPRT *));
111 1.1 cgd } *svc_head;
112 1.1 cgd
113 1.24 thorpej #ifdef _REENTRANT
114 1.20 fvdl extern rwlock_t svc_lock;
115 1.20 fvdl extern rwlock_t svc_fd_lock;
116 1.20 fvdl #endif
117 1.20 fvdl
118 1.20 fvdl static struct svc_callout *svc_find __P((rpcprog_t, rpcvers_t,
119 1.20 fvdl struct svc_callout **, char *));
120 1.23 fvdl static void __xprt_do_unregister __P((SVCXPRT *xprt, bool_t dolock));
121 1.1 cgd
122 1.1 cgd /* *************** SVCXPRT related stuff **************** */
123 1.3 deraadt
124 1.1 cgd /*
125 1.1 cgd * Activate a transport handle.
126 1.1 cgd */
127 1.1 cgd void
128 1.1 cgd xprt_register(xprt)
129 1.1 cgd SVCXPRT *xprt;
130 1.1 cgd {
131 1.17 lukem int sock;
132 1.17 lukem
133 1.17 lukem _DIAGASSERT(xprt != NULL);
134 1.17 lukem
135 1.20 fvdl sock = xprt->xp_fd;
136 1.1 cgd
137 1.20 fvdl rwlock_wrlock(&svc_fd_lock);
138 1.23 fvdl if (__svc_xports == NULL) {
139 1.23 fvdl __svc_xports = (SVCXPRT **)
140 1.1 cgd mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
141 1.23 fvdl if (__svc_xports == NULL)
142 1.16 lukem return;
143 1.23 fvdl memset(__svc_xports, '\0', FD_SETSIZE * sizeof(SVCXPRT *));
144 1.1 cgd }
145 1.2 deraadt if (sock < FD_SETSIZE) {
146 1.23 fvdl __svc_xports[sock] = xprt;
147 1.1 cgd FD_SET(sock, &svc_fdset);
148 1.2 deraadt svc_maxfd = max(svc_maxfd, sock);
149 1.1 cgd }
150 1.20 fvdl rwlock_unlock(&svc_fd_lock);
151 1.1 cgd }
152 1.1 cgd
153 1.23 fvdl void
154 1.23 fvdl xprt_unregister(SVCXPRT *xprt)
155 1.23 fvdl {
156 1.23 fvdl __xprt_do_unregister(xprt, TRUE);
157 1.23 fvdl }
158 1.23 fvdl
159 1.23 fvdl void
160 1.23 fvdl __xprt_unregister_unlocked(SVCXPRT *xprt)
161 1.23 fvdl {
162 1.23 fvdl __xprt_do_unregister(xprt, FALSE);
163 1.23 fvdl }
164 1.23 fvdl
165 1.1 cgd /*
166 1.1 cgd * De-activate a transport handle.
167 1.1 cgd */
168 1.23 fvdl static void
169 1.23 fvdl __xprt_do_unregister(xprt, dolock)
170 1.1 cgd SVCXPRT *xprt;
171 1.23 fvdl bool_t dolock;
172 1.1 cgd {
173 1.17 lukem int sock;
174 1.17 lukem
175 1.17 lukem _DIAGASSERT(xprt != NULL);
176 1.17 lukem
177 1.20 fvdl sock = xprt->xp_fd;
178 1.1 cgd
179 1.23 fvdl if (dolock)
180 1.23 fvdl rwlock_wrlock(&svc_fd_lock);
181 1.23 fvdl if ((sock < FD_SETSIZE) && (__svc_xports[sock] == xprt)) {
182 1.23 fvdl __svc_xports[sock] = NULL;
183 1.1 cgd FD_CLR(sock, &svc_fdset);
184 1.20 fvdl if (sock >= svc_maxfd) {
185 1.3 deraadt for (svc_maxfd--; svc_maxfd>=0; svc_maxfd--)
186 1.23 fvdl if (__svc_xports[svc_maxfd])
187 1.3 deraadt break;
188 1.3 deraadt }
189 1.1 cgd }
190 1.23 fvdl if (dolock)
191 1.23 fvdl rwlock_unlock(&svc_fd_lock);
192 1.1 cgd }
193 1.1 cgd
194 1.20 fvdl /*
195 1.20 fvdl * Add a service program to the callout list.
196 1.20 fvdl * The dispatch routine will be called when a rpc request for this
197 1.20 fvdl * program number comes in.
198 1.20 fvdl */
199 1.20 fvdl bool_t
200 1.20 fvdl svc_reg(xprt, prog, vers, dispatch, nconf)
201 1.20 fvdl SVCXPRT *xprt;
202 1.20 fvdl const rpcprog_t prog;
203 1.20 fvdl const rpcvers_t vers;
204 1.20 fvdl void (*dispatch) __P((struct svc_req *, SVCXPRT *));
205 1.20 fvdl const struct netconfig *nconf;
206 1.20 fvdl {
207 1.20 fvdl bool_t dummy;
208 1.20 fvdl struct svc_callout *prev;
209 1.21 christos struct svc_callout *s;
210 1.20 fvdl struct netconfig *tnconf;
211 1.21 christos char *netid = NULL;
212 1.20 fvdl int flag = 0;
213 1.20 fvdl
214 1.22 lukem _DIAGASSERT(xprt != NULL);
215 1.22 lukem /* XXX: dispatch may be NULL ??? */
216 1.22 lukem
217 1.20 fvdl /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
218 1.20 fvdl
219 1.20 fvdl if (xprt->xp_netid) {
220 1.20 fvdl netid = strdup(xprt->xp_netid);
221 1.20 fvdl flag = 1;
222 1.20 fvdl } else if (nconf && nconf->nc_netid) {
223 1.20 fvdl netid = strdup(nconf->nc_netid);
224 1.20 fvdl flag = 1;
225 1.20 fvdl } else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) {
226 1.20 fvdl netid = strdup(tnconf->nc_netid);
227 1.20 fvdl flag = 1;
228 1.20 fvdl freenetconfigent(tnconf);
229 1.20 fvdl } /* must have been created with svc_raw_create */
230 1.20 fvdl if ((netid == NULL) && (flag == 1)) {
231 1.20 fvdl return (FALSE);
232 1.20 fvdl }
233 1.20 fvdl
234 1.20 fvdl rwlock_wrlock(&svc_lock);
235 1.21 christos if ((s = svc_find(prog, vers, &prev, netid)) != NULL) {
236 1.20 fvdl if (netid)
237 1.20 fvdl free(netid);
238 1.20 fvdl if (s->sc_dispatch == dispatch)
239 1.20 fvdl goto rpcb_it; /* he is registering another xptr */
240 1.20 fvdl rwlock_unlock(&svc_lock);
241 1.20 fvdl return (FALSE);
242 1.20 fvdl }
243 1.21 christos s = mem_alloc(sizeof (struct svc_callout));
244 1.21 christos if (s == NULL) {
245 1.20 fvdl if (netid)
246 1.20 fvdl free(netid);
247 1.20 fvdl rwlock_unlock(&svc_lock);
248 1.20 fvdl return (FALSE);
249 1.20 fvdl }
250 1.20 fvdl
251 1.20 fvdl s->sc_prog = prog;
252 1.20 fvdl s->sc_vers = vers;
253 1.20 fvdl s->sc_dispatch = dispatch;
254 1.20 fvdl s->sc_netid = netid;
255 1.20 fvdl s->sc_next = svc_head;
256 1.20 fvdl svc_head = s;
257 1.20 fvdl
258 1.20 fvdl if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
259 1.20 fvdl ((SVCXPRT *) xprt)->xp_netid = strdup(netid);
260 1.20 fvdl
261 1.20 fvdl rpcb_it:
262 1.20 fvdl rwlock_unlock(&svc_lock);
263 1.20 fvdl /* now register the information with the local binder service */
264 1.20 fvdl if (nconf) {
265 1.26 christos dummy = rpcb_set(prog, vers, __UNCONST(nconf),
266 1.20 fvdl &((SVCXPRT *) xprt)->xp_ltaddr);
267 1.20 fvdl return (dummy);
268 1.20 fvdl }
269 1.20 fvdl return (TRUE);
270 1.20 fvdl }
271 1.20 fvdl
272 1.20 fvdl /*
273 1.20 fvdl * Remove a service program from the callout list.
274 1.20 fvdl */
275 1.20 fvdl void
276 1.20 fvdl svc_unreg(prog, vers)
277 1.20 fvdl const rpcprog_t prog;
278 1.20 fvdl const rpcvers_t vers;
279 1.20 fvdl {
280 1.20 fvdl struct svc_callout *prev;
281 1.21 christos struct svc_callout *s;
282 1.20 fvdl
283 1.20 fvdl /* unregister the information anyway */
284 1.20 fvdl (void) rpcb_unset(prog, vers, NULL);
285 1.20 fvdl rwlock_wrlock(&svc_lock);
286 1.21 christos while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) {
287 1.21 christos if (prev == NULL) {
288 1.20 fvdl svc_head = s->sc_next;
289 1.20 fvdl } else {
290 1.20 fvdl prev->sc_next = s->sc_next;
291 1.20 fvdl }
292 1.21 christos s->sc_next = NULL;
293 1.20 fvdl if (s->sc_netid)
294 1.21 christos mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
295 1.21 christos mem_free(s, sizeof (struct svc_callout));
296 1.20 fvdl }
297 1.20 fvdl rwlock_unlock(&svc_lock);
298 1.20 fvdl }
299 1.1 cgd
300 1.1 cgd /* ********************** CALLOUT list related stuff ************* */
301 1.1 cgd
302 1.20 fvdl #ifdef PORTMAP
303 1.1 cgd /*
304 1.1 cgd * Add a service program to the callout list.
305 1.1 cgd * The dispatch routine will be called when a rpc request for this
306 1.1 cgd * program number comes in.
307 1.1 cgd */
308 1.1 cgd bool_t
309 1.1 cgd svc_register(xprt, prog, vers, dispatch, protocol)
310 1.1 cgd SVCXPRT *xprt;
311 1.13 lukem u_long prog;
312 1.13 lukem u_long vers;
313 1.10 christos void (*dispatch) __P((struct svc_req *, SVCXPRT *));
314 1.1 cgd int protocol;
315 1.1 cgd {
316 1.1 cgd struct svc_callout *prev;
317 1.14 lukem struct svc_callout *s;
318 1.1 cgd
319 1.17 lukem _DIAGASSERT(xprt != NULL);
320 1.17 lukem _DIAGASSERT(dispatch != NULL);
321 1.17 lukem
322 1.21 christos if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) !=
323 1.21 christos NULL) {
324 1.1 cgd if (s->sc_dispatch == dispatch)
325 1.1 cgd goto pmap_it; /* he is registering another xptr */
326 1.1 cgd return (FALSE);
327 1.1 cgd }
328 1.21 christos s = mem_alloc(sizeof(struct svc_callout));
329 1.21 christos if (s == NULL) {
330 1.1 cgd return (FALSE);
331 1.1 cgd }
332 1.21 christos s->sc_prog = (rpcprog_t)prog;
333 1.21 christos s->sc_vers = (rpcvers_t)vers;
334 1.1 cgd s->sc_dispatch = dispatch;
335 1.1 cgd s->sc_next = svc_head;
336 1.1 cgd svc_head = s;
337 1.1 cgd pmap_it:
338 1.1 cgd /* now register the information with the local binder service */
339 1.1 cgd if (protocol) {
340 1.1 cgd return (pmap_set(prog, vers, protocol, xprt->xp_port));
341 1.1 cgd }
342 1.1 cgd return (TRUE);
343 1.1 cgd }
344 1.1 cgd
345 1.1 cgd /*
346 1.1 cgd * Remove a service program from the callout list.
347 1.1 cgd */
348 1.1 cgd void
349 1.1 cgd svc_unregister(prog, vers)
350 1.13 lukem u_long prog;
351 1.13 lukem u_long vers;
352 1.1 cgd {
353 1.1 cgd struct svc_callout *prev;
354 1.14 lukem struct svc_callout *s;
355 1.1 cgd
356 1.21 christos if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
357 1.21 christos NULL)
358 1.1 cgd return;
359 1.21 christos if (prev == NULL) {
360 1.1 cgd svc_head = s->sc_next;
361 1.1 cgd } else {
362 1.1 cgd prev->sc_next = s->sc_next;
363 1.1 cgd }
364 1.21 christos s->sc_next = NULL;
365 1.15 christos mem_free(s, sizeof(struct svc_callout));
366 1.1 cgd /* now unregister the information with the local binder service */
367 1.1 cgd (void)pmap_unset(prog, vers);
368 1.1 cgd }
369 1.20 fvdl #endif /* PORTMAP */
370 1.1 cgd
371 1.1 cgd /*
372 1.1 cgd * Search the callout list for a program number, return the callout
373 1.1 cgd * struct.
374 1.1 cgd */
375 1.1 cgd static struct svc_callout *
376 1.20 fvdl svc_find(prog, vers, prev, netid)
377 1.20 fvdl rpcprog_t prog;
378 1.20 fvdl rpcvers_t vers;
379 1.1 cgd struct svc_callout **prev;
380 1.20 fvdl char *netid;
381 1.1 cgd {
382 1.14 lukem struct svc_callout *s, *p;
383 1.1 cgd
384 1.17 lukem _DIAGASSERT(prev != NULL);
385 1.22 lukem /* netid is handled below */
386 1.17 lukem
387 1.21 christos p = NULL;
388 1.21 christos for (s = svc_head; s != NULL; s = s->sc_next) {
389 1.20 fvdl if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
390 1.20 fvdl ((netid == NULL) || (s->sc_netid == NULL) ||
391 1.20 fvdl (strcmp(netid, s->sc_netid) == 0)))
392 1.20 fvdl break;
393 1.1 cgd p = s;
394 1.1 cgd }
395 1.1 cgd *prev = p;
396 1.1 cgd return (s);
397 1.1 cgd }
398 1.1 cgd
399 1.1 cgd /* ******************* REPLY GENERATION ROUTINES ************ */
400 1.1 cgd
401 1.1 cgd /*
402 1.1 cgd * Send a reply to an rpc request
403 1.1 cgd */
404 1.1 cgd bool_t
405 1.1 cgd svc_sendreply(xprt, xdr_results, xdr_location)
406 1.14 lukem SVCXPRT *xprt;
407 1.1 cgd xdrproc_t xdr_results;
408 1.13 lukem caddr_t xdr_location;
409 1.1 cgd {
410 1.1 cgd struct rpc_msg rply;
411 1.1 cgd
412 1.17 lukem _DIAGASSERT(xprt != NULL);
413 1.17 lukem
414 1.1 cgd rply.rm_direction = REPLY;
415 1.1 cgd rply.rm_reply.rp_stat = MSG_ACCEPTED;
416 1.1 cgd rply.acpted_rply.ar_verf = xprt->xp_verf;
417 1.1 cgd rply.acpted_rply.ar_stat = SUCCESS;
418 1.1 cgd rply.acpted_rply.ar_results.where = xdr_location;
419 1.1 cgd rply.acpted_rply.ar_results.proc = xdr_results;
420 1.1 cgd return (SVC_REPLY(xprt, &rply));
421 1.1 cgd }
422 1.1 cgd
423 1.1 cgd /*
424 1.1 cgd * No procedure error reply
425 1.1 cgd */
426 1.1 cgd void
427 1.1 cgd svcerr_noproc(xprt)
428 1.14 lukem SVCXPRT *xprt;
429 1.1 cgd {
430 1.1 cgd struct rpc_msg rply;
431 1.1 cgd
432 1.17 lukem _DIAGASSERT(xprt != NULL);
433 1.17 lukem
434 1.1 cgd rply.rm_direction = REPLY;
435 1.1 cgd rply.rm_reply.rp_stat = MSG_ACCEPTED;
436 1.1 cgd rply.acpted_rply.ar_verf = xprt->xp_verf;
437 1.1 cgd rply.acpted_rply.ar_stat = PROC_UNAVAIL;
438 1.1 cgd SVC_REPLY(xprt, &rply);
439 1.1 cgd }
440 1.1 cgd
441 1.1 cgd /*
442 1.1 cgd * Can't decode args error reply
443 1.1 cgd */
444 1.1 cgd void
445 1.1 cgd svcerr_decode(xprt)
446 1.14 lukem SVCXPRT *xprt;
447 1.1 cgd {
448 1.1 cgd struct rpc_msg rply;
449 1.1 cgd
450 1.17 lukem _DIAGASSERT(xprt != NULL);
451 1.17 lukem
452 1.1 cgd rply.rm_direction = REPLY;
453 1.1 cgd rply.rm_reply.rp_stat = MSG_ACCEPTED;
454 1.1 cgd rply.acpted_rply.ar_verf = xprt->xp_verf;
455 1.1 cgd rply.acpted_rply.ar_stat = GARBAGE_ARGS;
456 1.1 cgd SVC_REPLY(xprt, &rply);
457 1.1 cgd }
458 1.1 cgd
459 1.1 cgd /*
460 1.1 cgd * Some system error
461 1.1 cgd */
462 1.1 cgd void
463 1.1 cgd svcerr_systemerr(xprt)
464 1.14 lukem SVCXPRT *xprt;
465 1.1 cgd {
466 1.1 cgd struct rpc_msg rply;
467 1.1 cgd
468 1.17 lukem _DIAGASSERT(xprt != NULL);
469 1.17 lukem
470 1.1 cgd rply.rm_direction = REPLY;
471 1.1 cgd rply.rm_reply.rp_stat = MSG_ACCEPTED;
472 1.1 cgd rply.acpted_rply.ar_verf = xprt->xp_verf;
473 1.1 cgd rply.acpted_rply.ar_stat = SYSTEM_ERR;
474 1.1 cgd SVC_REPLY(xprt, &rply);
475 1.1 cgd }
476 1.1 cgd
477 1.20 fvdl #if 0
478 1.20 fvdl /*
479 1.20 fvdl * Tell RPC package to not complain about version errors to the client. This
480 1.20 fvdl * is useful when revving broadcast protocols that sit on a fixed address.
481 1.20 fvdl * There is really one (or should be only one) example of this kind of
482 1.20 fvdl * protocol: the portmapper (or rpc binder).
483 1.20 fvdl */
484 1.20 fvdl void
485 1.20 fvdl __svc_versquiet_on(xprt)
486 1.21 christos SVCXPRT *xprt;
487 1.20 fvdl {
488 1.20 fvdl u_long tmp;
489 1.20 fvdl
490 1.22 lukem _DIAGASSERT(xprt != NULL);
491 1.22 lukem
492 1.20 fvdl tmp = ((u_long) xprt->xp_p3) | SVC_VERSQUIET;
493 1.20 fvdl xprt->xp_p3 = (caddr_t) tmp;
494 1.20 fvdl }
495 1.20 fvdl
496 1.20 fvdl void
497 1.20 fvdl __svc_versquiet_off(xprt)
498 1.21 christos SVCXPRT *xprt;
499 1.20 fvdl {
500 1.20 fvdl u_long tmp;
501 1.20 fvdl
502 1.22 lukem _DIAGASSERT(xprt != NULL);
503 1.22 lukem
504 1.20 fvdl tmp = ((u_long) xprt->xp_p3) & ~SVC_VERSQUIET;
505 1.20 fvdl xprt->xp_p3 = (caddr_t) tmp;
506 1.20 fvdl }
507 1.20 fvdl
508 1.20 fvdl void
509 1.20 fvdl svc_versquiet(xprt)
510 1.21 christos SVCXPRT *xprt;
511 1.20 fvdl {
512 1.20 fvdl __svc_versquiet_on(xprt);
513 1.20 fvdl }
514 1.20 fvdl
515 1.20 fvdl int
516 1.20 fvdl __svc_versquiet_get(xprt)
517 1.21 christos SVCXPRT *xprt;
518 1.20 fvdl {
519 1.22 lukem
520 1.22 lukem _DIAGASSERT(xprt != NULL);
521 1.22 lukem
522 1.20 fvdl return ((int) xprt->xp_p3) & SVC_VERSQUIET;
523 1.20 fvdl }
524 1.20 fvdl #endif
525 1.20 fvdl
526 1.1 cgd /*
527 1.1 cgd * Authentication error reply
528 1.1 cgd */
529 1.1 cgd void
530 1.1 cgd svcerr_auth(xprt, why)
531 1.1 cgd SVCXPRT *xprt;
532 1.1 cgd enum auth_stat why;
533 1.1 cgd {
534 1.1 cgd struct rpc_msg rply;
535 1.1 cgd
536 1.17 lukem _DIAGASSERT(xprt != NULL);
537 1.17 lukem
538 1.1 cgd rply.rm_direction = REPLY;
539 1.1 cgd rply.rm_reply.rp_stat = MSG_DENIED;
540 1.1 cgd rply.rjcted_rply.rj_stat = AUTH_ERROR;
541 1.1 cgd rply.rjcted_rply.rj_why = why;
542 1.1 cgd SVC_REPLY(xprt, &rply);
543 1.1 cgd }
544 1.1 cgd
545 1.1 cgd /*
546 1.1 cgd * Auth too weak error reply
547 1.1 cgd */
548 1.1 cgd void
549 1.1 cgd svcerr_weakauth(xprt)
550 1.1 cgd SVCXPRT *xprt;
551 1.1 cgd {
552 1.1 cgd
553 1.17 lukem _DIAGASSERT(xprt != NULL);
554 1.17 lukem
555 1.1 cgd svcerr_auth(xprt, AUTH_TOOWEAK);
556 1.1 cgd }
557 1.1 cgd
558 1.1 cgd /*
559 1.1 cgd * Program unavailable error reply
560 1.1 cgd */
561 1.1 cgd void
562 1.1 cgd svcerr_noprog(xprt)
563 1.14 lukem SVCXPRT *xprt;
564 1.1 cgd {
565 1.1 cgd struct rpc_msg rply;
566 1.1 cgd
567 1.17 lukem _DIAGASSERT(xprt != NULL);
568 1.17 lukem
569 1.1 cgd rply.rm_direction = REPLY;
570 1.1 cgd rply.rm_reply.rp_stat = MSG_ACCEPTED;
571 1.1 cgd rply.acpted_rply.ar_verf = xprt->xp_verf;
572 1.1 cgd rply.acpted_rply.ar_stat = PROG_UNAVAIL;
573 1.1 cgd SVC_REPLY(xprt, &rply);
574 1.1 cgd }
575 1.1 cgd
576 1.1 cgd /*
577 1.1 cgd * Program version mismatch error reply
578 1.1 cgd */
579 1.1 cgd void
580 1.1 cgd svcerr_progvers(xprt, low_vers, high_vers)
581 1.14 lukem SVCXPRT *xprt;
582 1.20 fvdl rpcvers_t low_vers;
583 1.20 fvdl rpcvers_t high_vers;
584 1.1 cgd {
585 1.1 cgd struct rpc_msg rply;
586 1.1 cgd
587 1.17 lukem _DIAGASSERT(xprt != NULL);
588 1.17 lukem
589 1.1 cgd rply.rm_direction = REPLY;
590 1.1 cgd rply.rm_reply.rp_stat = MSG_ACCEPTED;
591 1.1 cgd rply.acpted_rply.ar_verf = xprt->xp_verf;
592 1.1 cgd rply.acpted_rply.ar_stat = PROG_MISMATCH;
593 1.15 christos rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
594 1.15 christos rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
595 1.1 cgd SVC_REPLY(xprt, &rply);
596 1.1 cgd }
597 1.1 cgd
598 1.1 cgd /* ******************* SERVER INPUT STUFF ******************* */
599 1.1 cgd
600 1.1 cgd /*
601 1.1 cgd * Get server side input from some transport.
602 1.1 cgd *
603 1.1 cgd * Statement of authentication parameters management:
604 1.1 cgd * This function owns and manages all authentication parameters, specifically
605 1.1 cgd * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
606 1.1 cgd * the "cooked" credentials (rqst->rq_clntcred).
607 1.1 cgd * However, this function does not know the structure of the cooked
608 1.1 cgd * credentials, so it make the following assumptions:
609 1.1 cgd * a) the structure is contiguous (no pointers), and
610 1.1 cgd * b) the cred structure size does not exceed RQCRED_SIZE bytes.
611 1.1 cgd * In all events, all three parameters are freed upon exit from this routine.
612 1.1 cgd * The storage is trivially management on the call stack in user land, but
613 1.1 cgd * is mallocated in kernel land.
614 1.1 cgd */
615 1.1 cgd
616 1.1 cgd void
617 1.1 cgd svc_getreq(rdfds)
618 1.1 cgd int rdfds;
619 1.1 cgd {
620 1.1 cgd fd_set readfds;
621 1.1 cgd
622 1.1 cgd FD_ZERO(&readfds);
623 1.1 cgd readfds.fds_bits[0] = rdfds;
624 1.1 cgd svc_getreqset(&readfds);
625 1.1 cgd }
626 1.1 cgd
627 1.1 cgd void
628 1.1 cgd svc_getreqset(readfds)
629 1.1 cgd fd_set *readfds;
630 1.1 cgd {
631 1.20 fvdl int bit, fd;
632 1.15 christos int32_t mask, *maskp;
633 1.14 lukem int sock;
634 1.1 cgd
635 1.17 lukem _DIAGASSERT(readfds != NULL);
636 1.1 cgd
637 1.4 cgd maskp = readfds->fds_bits;
638 1.2 deraadt for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
639 1.10 christos for (mask = *maskp++; (bit = ffs(mask)) != 0;
640 1.10 christos mask ^= (1 << (bit - 1))) {
641 1.1 cgd /* sock has input waiting */
642 1.20 fvdl fd = sock + bit - 1;
643 1.20 fvdl svc_getreq_common(fd);
644 1.20 fvdl }
645 1.20 fvdl }
646 1.20 fvdl }
647 1.20 fvdl
648 1.20 fvdl void
649 1.20 fvdl svc_getreq_common(fd)
650 1.20 fvdl int fd;
651 1.20 fvdl {
652 1.20 fvdl SVCXPRT *xprt;
653 1.20 fvdl struct svc_req r;
654 1.20 fvdl struct rpc_msg msg;
655 1.20 fvdl int prog_found;
656 1.20 fvdl rpcvers_t low_vers;
657 1.20 fvdl rpcvers_t high_vers;
658 1.20 fvdl enum xprt_stat stat;
659 1.20 fvdl char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
660 1.20 fvdl
661 1.20 fvdl msg.rm_call.cb_cred.oa_base = cred_area;
662 1.20 fvdl msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
663 1.20 fvdl r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
664 1.20 fvdl
665 1.20 fvdl rwlock_rdlock(&svc_fd_lock);
666 1.23 fvdl xprt = __svc_xports[fd];
667 1.20 fvdl rwlock_unlock(&svc_fd_lock);
668 1.20 fvdl if (xprt == NULL)
669 1.20 fvdl /* But do we control sock? */
670 1.20 fvdl return;
671 1.20 fvdl /* now receive msgs from xprtprt (support batch calls) */
672 1.20 fvdl do {
673 1.20 fvdl if (SVC_RECV(xprt, &msg)) {
674 1.20 fvdl
675 1.20 fvdl /* now find the exported program and call it */
676 1.20 fvdl struct svc_callout *s;
677 1.20 fvdl enum auth_stat why;
678 1.20 fvdl
679 1.20 fvdl r.rq_xprt = xprt;
680 1.20 fvdl r.rq_prog = msg.rm_call.cb_prog;
681 1.20 fvdl r.rq_vers = msg.rm_call.cb_vers;
682 1.20 fvdl r.rq_proc = msg.rm_call.cb_proc;
683 1.20 fvdl r.rq_cred = msg.rm_call.cb_cred;
684 1.20 fvdl /* first authenticate the message */
685 1.20 fvdl if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
686 1.20 fvdl svcerr_auth(xprt, why);
687 1.20 fvdl goto call_done;
688 1.1 cgd }
689 1.20 fvdl /* now match message with a registered service*/
690 1.20 fvdl prog_found = FALSE;
691 1.20 fvdl low_vers = (rpcvers_t) -1L;
692 1.20 fvdl high_vers = (rpcvers_t) 0L;
693 1.21 christos for (s = svc_head; s != NULL; s = s->sc_next) {
694 1.20 fvdl if (s->sc_prog == r.rq_prog) {
695 1.20 fvdl if (s->sc_vers == r.rq_vers) {
696 1.20 fvdl (*s->sc_dispatch)(&r, xprt);
697 1.20 fvdl goto call_done;
698 1.20 fvdl } /* found correct version */
699 1.20 fvdl prog_found = TRUE;
700 1.20 fvdl if (s->sc_vers < low_vers)
701 1.20 fvdl low_vers = s->sc_vers;
702 1.20 fvdl if (s->sc_vers > high_vers)
703 1.20 fvdl high_vers = s->sc_vers;
704 1.20 fvdl } /* found correct program */
705 1.1 cgd }
706 1.20 fvdl /*
707 1.20 fvdl * if we got here, the program or version
708 1.20 fvdl * is not served ...
709 1.20 fvdl */
710 1.20 fvdl if (prog_found)
711 1.20 fvdl svcerr_progvers(xprt, low_vers, high_vers);
712 1.20 fvdl else
713 1.20 fvdl svcerr_noprog(xprt);
714 1.20 fvdl /* Fall through to ... */
715 1.20 fvdl }
716 1.20 fvdl /*
717 1.20 fvdl * Check if the xprt has been disconnected in a
718 1.20 fvdl * recursive call in the service dispatch routine.
719 1.20 fvdl * If so, then break.
720 1.20 fvdl */
721 1.20 fvdl rwlock_rdlock(&svc_fd_lock);
722 1.23 fvdl if (xprt != __svc_xports[fd]) {
723 1.20 fvdl rwlock_unlock(&svc_fd_lock);
724 1.20 fvdl break;
725 1.20 fvdl }
726 1.20 fvdl rwlock_unlock(&svc_fd_lock);
727 1.20 fvdl call_done:
728 1.20 fvdl if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
729 1.20 fvdl SVC_DESTROY(xprt);
730 1.20 fvdl break;
731 1.20 fvdl }
732 1.20 fvdl } while (stat == XPRT_MOREREQS);
733 1.20 fvdl }
734 1.20 fvdl
735 1.20 fvdl
736 1.20 fvdl void
737 1.20 fvdl svc_getreq_poll(pfdp, pollretval)
738 1.20 fvdl struct pollfd *pfdp;
739 1.20 fvdl int pollretval;
740 1.20 fvdl {
741 1.20 fvdl int i;
742 1.20 fvdl int fds_found;
743 1.22 lukem
744 1.22 lukem _DIAGASSERT(pfdp != NULL);
745 1.20 fvdl
746 1.20 fvdl for (i = fds_found = 0; fds_found < pollretval; i++) {
747 1.21 christos struct pollfd *p = &pfdp[i];
748 1.20 fvdl
749 1.20 fvdl if (p->revents) {
750 1.20 fvdl /* fd has input waiting */
751 1.20 fvdl fds_found++;
752 1.20 fvdl /*
753 1.20 fvdl * We assume that this function is only called
754 1.20 fvdl * via someone select()ing from svc_fdset or
755 1.25 christos * pollts()ing from svc_pollset[]. Thus it's safe
756 1.20 fvdl * to handle the POLLNVAL event by simply turning
757 1.20 fvdl * the corresponding bit off in svc_fdset. The
758 1.20 fvdl * svc_pollset[] array is derived from svc_fdset
759 1.20 fvdl * and so will also be updated eventually.
760 1.20 fvdl *
761 1.20 fvdl * XXX Should we do an xprt_unregister() instead?
762 1.20 fvdl */
763 1.20 fvdl if (p->revents & POLLNVAL) {
764 1.20 fvdl rwlock_wrlock(&svc_fd_lock);
765 1.20 fvdl FD_CLR(p->fd, &svc_fdset);
766 1.20 fvdl rwlock_unlock(&svc_fd_lock);
767 1.20 fvdl } else
768 1.20 fvdl svc_getreq_common(p->fd);
769 1.20 fvdl }
770 1.1 cgd }
771 1.23 fvdl }
772 1.23 fvdl
773 1.23 fvdl bool_t
774 1.23 fvdl rpc_control(int what, void *arg)
775 1.23 fvdl {
776 1.23 fvdl int val;
777 1.23 fvdl
778 1.23 fvdl switch (what) {
779 1.23 fvdl case RPC_SVC_CONNMAXREC_SET:
780 1.23 fvdl val = *(int *)arg;
781 1.23 fvdl if (val <= 0)
782 1.23 fvdl return FALSE;
783 1.23 fvdl __svc_maxrec = val;
784 1.23 fvdl return TRUE;
785 1.23 fvdl case RPC_SVC_CONNMAXREC_GET:
786 1.23 fvdl *(int *)arg = __svc_maxrec;
787 1.23 fvdl return TRUE;
788 1.23 fvdl default:
789 1.23 fvdl break;
790 1.23 fvdl }
791 1.23 fvdl return FALSE;
792 1.1 cgd }
793