1 | /* |
2 | * svc.c, Server-side remote procedure call interface. |
3 | * |
4 | * There are two sets of procedures here. The xprt routines are |
5 | * for handling transport handles. The svc routines handle the |
6 | * list of service routines. |
7 | * Copyright (C) 2002-2021 Free Software Foundation, Inc. |
8 | * This file is part of the GNU C Library. |
9 | * Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. |
10 | * |
11 | * The GNU C Library is free software; you can redistribute it and/or |
12 | * modify it under the terms of the GNU Lesser General Public |
13 | * License as published by the Free Software Foundation; either |
14 | * version 2.1 of the License, or (at your option) any later version. |
15 | * |
16 | * The GNU C Library is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19 | * Lesser General Public License for more details. |
20 | * |
21 | * You should have received a copy of the GNU Lesser General Public |
22 | * License along with the GNU C Library; if not, see |
23 | * <https://www.gnu.org/licenses/>. |
24 | * |
25 | * Copyright (c) 2010, Oracle America, Inc. |
26 | * |
27 | * Redistribution and use in source and binary forms, with or without |
28 | * modification, are permitted provided that the following conditions are |
29 | * met: |
30 | * |
31 | * * Redistributions of source code must retain the above copyright |
32 | * notice, this list of conditions and the following disclaimer. |
33 | * * Redistributions in binary form must reproduce the above |
34 | * copyright notice, this list of conditions and the following |
35 | * disclaimer in the documentation and/or other materials |
36 | * provided with the distribution. |
37 | * * Neither the name of the "Oracle America, Inc." nor the names of its |
38 | * contributors may be used to endorse or promote products derived |
39 | * from this software without specific prior written permission. |
40 | * |
41 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
42 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
43 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
44 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
45 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
46 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
48 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
49 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
50 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
51 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
52 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
53 | */ |
54 | |
55 | #include <errno.h> |
56 | #include <unistd.h> |
57 | #include <rpc/rpc.h> |
58 | #include <rpc/svc.h> |
59 | #include <rpc/pmap_clnt.h> |
60 | #include <sys/poll.h> |
61 | #include <time.h> |
62 | #include <shlib-compat.h> |
63 | |
64 | #define xports RPC_THREAD_VARIABLE(svc_xports_s) |
65 | |
66 | #define NULL_SVC ((struct svc_callout *)0) |
67 | #define RQCRED_SIZE 400 /* this size is excessive */ |
68 | |
69 | /* The services list |
70 | Each entry represents a set of procedures (an rpc program). |
71 | The dispatch routine takes request structs and runs the |
72 | appropriate procedure. */ |
73 | struct svc_callout { |
74 | struct svc_callout *sc_next; |
75 | rpcprog_t sc_prog; |
76 | rpcvers_t sc_vers; |
77 | void (*sc_dispatch) (struct svc_req *, SVCXPRT *); |
78 | bool_t sc_mapped; |
79 | }; |
80 | #define svc_head RPC_THREAD_VARIABLE(svc_head_s) |
81 | |
82 | /* *************** SVCXPRT related stuff **************** */ |
83 | |
84 | /* Activate a transport handle. */ |
85 | void |
86 | xprt_register (SVCXPRT *xprt) |
87 | { |
88 | register int sock = xprt->xp_sock; |
89 | register int i; |
90 | |
91 | if (xports == NULL) |
92 | { |
93 | xports = (SVCXPRT **) calloc (_rpc_dtablesize (), sizeof (SVCXPRT *)); |
94 | if (xports == NULL) /* Don't add handle */ |
95 | return; |
96 | } |
97 | |
98 | if (sock < _rpc_dtablesize ()) |
99 | { |
100 | struct pollfd *new_svc_pollfd; |
101 | |
102 | xports[sock] = xprt; |
103 | if (sock < FD_SETSIZE) |
104 | FD_SET (sock, &svc_fdset); |
105 | |
106 | /* Check if we have an empty slot */ |
107 | for (i = 0; i < svc_max_pollfd; ++i) |
108 | if (svc_pollfd[i].fd == -1) |
109 | { |
110 | svc_pollfd[i].fd = sock; |
111 | svc_pollfd[i].events = (POLLIN | POLLPRI | |
112 | POLLRDNORM | POLLRDBAND); |
113 | return; |
114 | } |
115 | |
116 | new_svc_pollfd = (struct pollfd *) realloc (svc_pollfd, |
117 | sizeof (struct pollfd) |
118 | * (svc_max_pollfd + 1)); |
119 | if (new_svc_pollfd == NULL) /* Out of memory */ |
120 | return; |
121 | svc_pollfd = new_svc_pollfd; |
122 | ++svc_max_pollfd; |
123 | |
124 | svc_pollfd[svc_max_pollfd - 1].fd = sock; |
125 | svc_pollfd[svc_max_pollfd - 1].events = (POLLIN | POLLPRI | |
126 | POLLRDNORM | POLLRDBAND); |
127 | } |
128 | } |
129 | libc_hidden_nolink_sunrpc (xprt_register, GLIBC_2_0) |
130 | |
131 | /* De-activate a transport handle. */ |
132 | void |
133 | xprt_unregister (SVCXPRT *xprt) |
134 | { |
135 | register int sock = xprt->xp_sock; |
136 | register int i; |
137 | |
138 | if ((sock < _rpc_dtablesize ()) && (xports[sock] == xprt)) |
139 | { |
140 | xports[sock] = (SVCXPRT *) 0; |
141 | |
142 | if (sock < FD_SETSIZE) |
143 | FD_CLR (sock, &svc_fdset); |
144 | |
145 | for (i = 0; i < svc_max_pollfd; ++i) |
146 | if (svc_pollfd[i].fd == sock) |
147 | svc_pollfd[i].fd = -1; |
148 | } |
149 | } |
150 | #ifdef EXPORT_RPC_SYMBOLS |
151 | libc_hidden_def (xprt_unregister) |
152 | #else |
153 | libc_hidden_nolink_sunrpc (xprt_unregister, GLIBC_2_0) |
154 | #endif |
155 | |
156 | |
157 | /* ********************** CALLOUT list related stuff ************* */ |
158 | |
159 | /* Search the callout list for a program number, return the callout |
160 | struct. */ |
161 | static struct svc_callout * |
162 | svc_find (rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev) |
163 | { |
164 | register struct svc_callout *s, *p; |
165 | |
166 | p = NULL_SVC; |
167 | for (s = svc_head; s != NULL_SVC; s = s->sc_next) |
168 | { |
169 | if ((s->sc_prog == prog) && (s->sc_vers == vers)) |
170 | goto done; |
171 | p = s; |
172 | } |
173 | done: |
174 | *prev = p; |
175 | return s; |
176 | } |
177 | |
178 | /* Add a service program to the callout list. |
179 | The dispatch routine will be called when a rpc request for this |
180 | program number comes in. */ |
181 | bool_t |
182 | svc_register (SVCXPRT * xprt, rpcprog_t prog, rpcvers_t vers, |
183 | void (*dispatch) (struct svc_req *, SVCXPRT *), |
184 | rpcproc_t protocol) |
185 | { |
186 | struct svc_callout *prev; |
187 | register struct svc_callout *s; |
188 | |
189 | if ((s = svc_find (prog, vers, &prev)) != NULL_SVC) |
190 | { |
191 | if (s->sc_dispatch == dispatch) |
192 | goto pmap_it; /* he is registering another xptr */ |
193 | return FALSE; |
194 | } |
195 | s = (struct svc_callout *) mem_alloc (sizeof (struct svc_callout)); |
196 | if (s == (struct svc_callout *) 0) |
197 | return FALSE; |
198 | |
199 | s->sc_prog = prog; |
200 | s->sc_vers = vers; |
201 | s->sc_dispatch = dispatch; |
202 | s->sc_next = svc_head; |
203 | s->sc_mapped = FALSE; |
204 | svc_head = s; |
205 | |
206 | pmap_it: |
207 | /* now register the information with the local binder service */ |
208 | if (protocol) |
209 | { |
210 | if (! pmap_set (prog, vers, protocol, xprt->xp_port)) |
211 | return FALSE; |
212 | |
213 | s->sc_mapped = TRUE; |
214 | } |
215 | |
216 | return TRUE; |
217 | } |
218 | #ifdef EXPORT_RPC_SYMBOLS |
219 | libc_hidden_def (svc_register) |
220 | #else |
221 | libc_hidden_nolink_sunrpc (svc_register, GLIBC_2_0) |
222 | #endif |
223 | |
224 | /* Remove a service program from the callout list. */ |
225 | void |
226 | svc_unregister (rpcprog_t prog, rpcvers_t vers) |
227 | { |
228 | struct svc_callout *prev; |
229 | register struct svc_callout *s; |
230 | |
231 | if ((s = svc_find (prog, vers, &prev)) == NULL_SVC) |
232 | return; |
233 | bool is_mapped = s->sc_mapped; |
234 | |
235 | if (prev == NULL_SVC) |
236 | svc_head = s->sc_next; |
237 | else |
238 | prev->sc_next = s->sc_next; |
239 | |
240 | s->sc_next = NULL_SVC; |
241 | mem_free ((char *) s, (u_int) sizeof (struct svc_callout)); |
242 | /* now unregister the information with the local binder service */ |
243 | if (is_mapped) |
244 | pmap_unset (prog, vers); |
245 | } |
246 | libc_hidden_nolink_sunrpc (svc_unregister, GLIBC_2_0) |
247 | |
248 | /* ******************* REPLY GENERATION ROUTINES ************ */ |
249 | |
250 | /* Send a reply to an rpc request */ |
251 | bool_t |
252 | svc_sendreply (register SVCXPRT *xprt, xdrproc_t xdr_results, |
253 | caddr_t xdr_location) |
254 | { |
255 | struct rpc_msg rply; |
256 | |
257 | rply.rm_direction = REPLY; |
258 | rply.rm_reply.rp_stat = MSG_ACCEPTED; |
259 | rply.acpted_rply.ar_verf = xprt->xp_verf; |
260 | rply.acpted_rply.ar_stat = SUCCESS; |
261 | rply.acpted_rply.ar_results.where = xdr_location; |
262 | rply.acpted_rply.ar_results.proc = xdr_results; |
263 | return SVC_REPLY (xprt, &rply); |
264 | } |
265 | #ifdef EXPORT_RPC_SYMBOLS |
266 | libc_hidden_def (svc_sendreply) |
267 | #else |
268 | libc_hidden_nolink_sunrpc (svc_sendreply, GLIBC_2_0) |
269 | #endif |
270 | |
271 | /* No procedure error reply */ |
272 | void |
273 | svcerr_noproc (register SVCXPRT *xprt) |
274 | { |
275 | struct rpc_msg rply; |
276 | |
277 | rply.rm_direction = REPLY; |
278 | rply.rm_reply.rp_stat = MSG_ACCEPTED; |
279 | rply.acpted_rply.ar_verf = xprt->xp_verf; |
280 | rply.acpted_rply.ar_stat = PROC_UNAVAIL; |
281 | SVC_REPLY (xprt, &rply); |
282 | } |
283 | #ifdef EXPORT_RPC_SYMBOLS |
284 | libc_hidden_def (svcerr_noproc) |
285 | #else |
286 | libc_hidden_nolink_sunrpc (svcerr_noproc, GLIBC_2_0) |
287 | #endif |
288 | |
289 | /* Can't decode args error reply */ |
290 | void |
291 | svcerr_decode (register SVCXPRT *xprt) |
292 | { |
293 | struct rpc_msg rply; |
294 | |
295 | rply.rm_direction = REPLY; |
296 | rply.rm_reply.rp_stat = MSG_ACCEPTED; |
297 | rply.acpted_rply.ar_verf = xprt->xp_verf; |
298 | rply.acpted_rply.ar_stat = GARBAGE_ARGS; |
299 | SVC_REPLY (xprt, &rply); |
300 | } |
301 | #ifdef EXPORT_RPC_SYMBOLS |
302 | libc_hidden_def (svcerr_decode) |
303 | #else |
304 | libc_hidden_nolink_sunrpc (svcerr_decode, GLIBC_2_0) |
305 | #endif |
306 | |
307 | /* Some system error */ |
308 | void |
309 | svcerr_systemerr (register SVCXPRT *xprt) |
310 | { |
311 | struct rpc_msg rply; |
312 | |
313 | rply.rm_direction = REPLY; |
314 | rply.rm_reply.rp_stat = MSG_ACCEPTED; |
315 | rply.acpted_rply.ar_verf = xprt->xp_verf; |
316 | rply.acpted_rply.ar_stat = SYSTEM_ERR; |
317 | SVC_REPLY (xprt, &rply); |
318 | } |
319 | #ifdef EXPORT_RPC_SYMBOLS |
320 | libc_hidden_def (svcerr_systemerr) |
321 | #else |
322 | libc_hidden_nolink_sunrpc (svcerr_systemerr, GLIBC_2_0) |
323 | #endif |
324 | |
325 | /* Authentication error reply */ |
326 | void |
327 | svcerr_auth (SVCXPRT *xprt, enum auth_stat why) |
328 | { |
329 | struct rpc_msg rply; |
330 | |
331 | rply.rm_direction = REPLY; |
332 | rply.rm_reply.rp_stat = MSG_DENIED; |
333 | rply.rjcted_rply.rj_stat = AUTH_ERROR; |
334 | rply.rjcted_rply.rj_why = why; |
335 | SVC_REPLY (xprt, &rply); |
336 | } |
337 | libc_hidden_nolink_sunrpc (svcerr_auth, GLIBC_2_0) |
338 | |
339 | /* Auth too weak error reply */ |
340 | void |
341 | svcerr_weakauth (SVCXPRT *xprt) |
342 | { |
343 | svcerr_auth (xprt, AUTH_TOOWEAK); |
344 | } |
345 | libc_hidden_nolink_sunrpc (svcerr_weakauth, GLIBC_2_0) |
346 | |
347 | /* Program unavailable error reply */ |
348 | void |
349 | svcerr_noprog (register SVCXPRT *xprt) |
350 | { |
351 | struct rpc_msg rply; |
352 | |
353 | rply.rm_direction = REPLY; |
354 | rply.rm_reply.rp_stat = MSG_ACCEPTED; |
355 | rply.acpted_rply.ar_verf = xprt->xp_verf; |
356 | rply.acpted_rply.ar_stat = PROG_UNAVAIL; |
357 | SVC_REPLY (xprt, &rply); |
358 | } |
359 | libc_hidden_nolink_sunrpc (svcerr_noprog, GLIBC_2_0) |
360 | |
361 | /* Program version mismatch error reply */ |
362 | void |
363 | svcerr_progvers (register SVCXPRT *xprt, rpcvers_t low_vers, |
364 | rpcvers_t high_vers) |
365 | { |
366 | struct rpc_msg rply; |
367 | |
368 | rply.rm_direction = REPLY; |
369 | rply.rm_reply.rp_stat = MSG_ACCEPTED; |
370 | rply.acpted_rply.ar_verf = xprt->xp_verf; |
371 | rply.acpted_rply.ar_stat = PROG_MISMATCH; |
372 | rply.acpted_rply.ar_vers.low = low_vers; |
373 | rply.acpted_rply.ar_vers.high = high_vers; |
374 | SVC_REPLY (xprt, &rply); |
375 | } |
376 | libc_hidden_nolink_sunrpc (svcerr_progvers, GLIBC_2_0) |
377 | |
378 | /* ******************* SERVER INPUT STUFF ******************* */ |
379 | |
380 | /* |
381 | * Get server side input from some transport. |
382 | * |
383 | * Statement of authentication parameters management: |
384 | * This function owns and manages all authentication parameters, specifically |
385 | * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and |
386 | * the "cooked" credentials (rqst->rq_clntcred). |
387 | * However, this function does not know the structure of the cooked |
388 | * credentials, so it make the following assumptions: |
389 | * a) the structure is contiguous (no pointers), and |
390 | * b) the cred structure size does not exceed RQCRED_SIZE bytes. |
391 | * In all events, all three parameters are freed upon exit from this routine. |
392 | * The storage is trivially management on the call stack in user land, but |
393 | * is mallocated in kernel land. |
394 | */ |
395 | |
396 | void |
397 | svc_getreq (int rdfds) |
398 | { |
399 | fd_set readfds; |
400 | |
401 | FD_ZERO (&readfds); |
402 | readfds.fds_bits[0] = rdfds; |
403 | svc_getreqset (&readfds); |
404 | } |
405 | libc_hidden_nolink_sunrpc (svc_getreq, GLIBC_2_0) |
406 | |
407 | void |
408 | svc_getreqset (fd_set *readfds) |
409 | { |
410 | register fd_mask mask; |
411 | register fd_mask *maskp; |
412 | register int setsize; |
413 | register int sock; |
414 | register int bit; |
415 | |
416 | setsize = _rpc_dtablesize (); |
417 | if (setsize > FD_SETSIZE) |
418 | setsize = FD_SETSIZE; |
419 | maskp = readfds->fds_bits; |
420 | for (sock = 0; sock < setsize; sock += NFDBITS) |
421 | for (mask = *maskp++; (bit = ffsl (mask)); mask ^= (1L << (bit - 1))) |
422 | svc_getreq_common (sock + bit - 1); |
423 | } |
424 | libc_hidden_nolink_sunrpc (svc_getreqset, GLIBC_2_0) |
425 | |
426 | void |
427 | svc_getreq_poll (struct pollfd *pfdp, int pollretval) |
428 | { |
429 | if (pollretval == 0) |
430 | return; |
431 | |
432 | register int fds_found; |
433 | for (int i = fds_found = 0; i < svc_max_pollfd; ++i) |
434 | { |
435 | register struct pollfd *p = &pfdp[i]; |
436 | |
437 | if (p->fd != -1 && p->revents) |
438 | { |
439 | /* fd has input waiting */ |
440 | if (p->revents & POLLNVAL) |
441 | xprt_unregister (xports[p->fd]); |
442 | else |
443 | svc_getreq_common (p->fd); |
444 | |
445 | if (++fds_found >= pollretval) |
446 | break; |
447 | } |
448 | } |
449 | } |
450 | #ifdef EXPORT_RPC_SYMBOLS |
451 | libc_hidden_def (svc_getreq_poll) |
452 | #else |
453 | libc_hidden_nolink_sunrpc (svc_getreq_poll, GLIBC_2_2) |
454 | #endif |
455 | |
456 | |
457 | void |
458 | svc_getreq_common (const int fd) |
459 | { |
460 | enum xprt_stat stat; |
461 | struct rpc_msg msg; |
462 | register SVCXPRT *xprt; |
463 | char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE]; |
464 | msg.rm_call.cb_cred.oa_base = cred_area; |
465 | msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]); |
466 | |
467 | xprt = xports[fd]; |
468 | /* Do we control fd? */ |
469 | if (xprt == NULL) |
470 | return; |
471 | |
472 | /* now receive msgs from xprtprt (support batch calls) */ |
473 | do |
474 | { |
475 | if (SVC_RECV (xprt, &msg)) |
476 | { |
477 | /* now find the exported program and call it */ |
478 | struct svc_callout *s; |
479 | struct svc_req r; |
480 | enum auth_stat why; |
481 | rpcvers_t low_vers; |
482 | rpcvers_t high_vers; |
483 | int prog_found; |
484 | |
485 | r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]); |
486 | r.rq_xprt = xprt; |
487 | r.rq_prog = msg.rm_call.cb_prog; |
488 | r.rq_vers = msg.rm_call.cb_vers; |
489 | r.rq_proc = msg.rm_call.cb_proc; |
490 | r.rq_cred = msg.rm_call.cb_cred; |
491 | |
492 | /* first authenticate the message */ |
493 | /* Check for null flavor and bypass these calls if possible */ |
494 | |
495 | if (msg.rm_call.cb_cred.oa_flavor == AUTH_NULL) |
496 | { |
497 | r.rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor; |
498 | r.rq_xprt->xp_verf.oa_length = 0; |
499 | } |
500 | else if ((why = _authenticate (&r, &msg)) != AUTH_OK) |
501 | { |
502 | svcerr_auth (xprt, why); |
503 | goto call_done; |
504 | } |
505 | |
506 | /* now match message with a registered service */ |
507 | prog_found = FALSE; |
508 | low_vers = 0 - 1; |
509 | high_vers = 0; |
510 | |
511 | for (s = svc_head; s != NULL_SVC; s = s->sc_next) |
512 | { |
513 | if (s->sc_prog == r.rq_prog) |
514 | { |
515 | if (s->sc_vers == r.rq_vers) |
516 | { |
517 | (*s->sc_dispatch) (&r, xprt); |
518 | goto call_done; |
519 | } |
520 | /* found correct version */ |
521 | prog_found = TRUE; |
522 | if (s->sc_vers < low_vers) |
523 | low_vers = s->sc_vers; |
524 | if (s->sc_vers > high_vers) |
525 | high_vers = s->sc_vers; |
526 | } |
527 | /* found correct program */ |
528 | } |
529 | /* if we got here, the program or version |
530 | is not served ... */ |
531 | if (prog_found) |
532 | svcerr_progvers (xprt, low_vers, high_vers); |
533 | else |
534 | svcerr_noprog (xprt); |
535 | /* Fall through to ... */ |
536 | } |
537 | call_done: |
538 | if ((stat = SVC_STAT (xprt)) == XPRT_DIED) |
539 | { |
540 | SVC_DESTROY (xprt); |
541 | break; |
542 | } |
543 | } |
544 | while (stat == XPRT_MOREREQS); |
545 | } |
546 | libc_hidden_nolink_sunrpc (svc_getreq_common, GLIBC_2_2) |
547 | |
548 | void |
549 | __svc_wait_on_error (void) |
550 | { |
551 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 50000000 }; |
552 | __nanosleep (&ts, NULL); |
553 | } |
554 | |
555 | /* If there are no file descriptors available, then accept will fail. |
556 | We want to delay here so the connection request can be dequeued; |
557 | otherwise we can bounce between polling and accepting, never giving the |
558 | request a chance to dequeue and eating an enormous amount of cpu time |
559 | in svc_run if we're polling on many file descriptors. */ |
560 | void |
561 | __svc_accept_failed (void) |
562 | { |
563 | if (errno == EMFILE) |
564 | { |
565 | __svc_wait_on_error (); |
566 | } |
567 | } |
568 | |
569 | |
570 | void |
571 | __rpc_thread_svc_cleanup (void) |
572 | { |
573 | struct svc_callout *svcp; |
574 | |
575 | while ((svcp = svc_head) != NULL) |
576 | svc_unregister (svcp->sc_prog, svcp->sc_vers); |
577 | } |
578 | |