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