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