1 /* $NetBSD: rpc_soc.c,v 1.6 2000/07/06 03:10:35 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2009, Sun Microsystems, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * - Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * - Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * - Neither the name of Sun Microsystems, Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 /* #ident "@(#)rpc_soc.c 1.17 94/04/24 SMI" */ 32 33 /* 34 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 35 * In addition, portions of such source code were derived from Berkeley 36 * 4.3 BSD under license from the Regents of the University of 37 * California. 38 */ 39 40 #if defined(LIBC_SCCS) && !defined(lint) 41 static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro"; 42 #endif 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #ifdef PORTMAP 47 /* 48 * rpc_soc.c 49 * 50 * The backward compatibility routines for the earlier implementation 51 * of RPC, where the only transports supported were tcp/ip and udp/ip. 52 * Based on berkeley socket abstraction, now implemented on the top 53 * of TLI/Streams 54 */ 55 56 #include "namespace.h" 57 #include "reentrant.h" 58 #include <sys/types.h> 59 #include <sys/socket.h> 60 #include <stdio.h> 61 #include <rpc/rpc.h> 62 #include <rpc/pmap_clnt.h> 63 #include <rpc/pmap_prot.h> 64 #include <rpc/nettype.h> 65 #include <syslog.h> 66 #include <netinet/in.h> 67 #include <netdb.h> 68 #include <errno.h> 69 #include <syslog.h> 70 #include <stdlib.h> 71 #include <string.h> 72 #include <unistd.h> 73 #include "un-namespace.h" 74 75 #include "rpc_com.h" 76 #include "mt_misc.h" 77 78 static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t, 79 int *, u_int, u_int, char *); 80 static SVCXPRT *svc_com_create(int, u_int, u_int, char *); 81 static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *); 82 83 /* XXX */ 84 #define IN4_LOCALHOST_STRING "127.0.0.1" 85 #define IN6_LOCALHOST_STRING "::1" 86 87 /* 88 * A common clnt create routine 89 */ 90 static CLIENT * 91 clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp) 92 struct sockaddr_in *raddr; 93 rpcprog_t prog; 94 rpcvers_t vers; 95 int *sockp; 96 u_int sendsz; 97 u_int recvsz; 98 char *tp; 99 { 100 CLIENT *cl; 101 int madefd = FALSE; 102 int fd = *sockp; 103 struct netconfig *nconf; 104 struct netbuf bindaddr; 105 106 mutex_lock(&rpcsoc_lock); 107 if ((nconf = __rpc_getconfip(tp)) == NULL) { 108 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 109 mutex_unlock(&rpcsoc_lock); 110 return (NULL); 111 } 112 if (fd == RPC_ANYSOCK) { 113 fd = __rpc_nconf2fd(nconf); 114 if (fd == -1) 115 goto syserror; 116 madefd = TRUE; 117 } 118 119 if (raddr->sin_port == 0) { 120 u_int proto; 121 u_short sport; 122 123 mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */ 124 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP; 125 sport = pmap_getport(raddr, (u_long)prog, (u_long)vers, 126 proto); 127 if (sport == 0) { 128 goto err; 129 } 130 raddr->sin_port = htons(sport); 131 mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */ 132 } 133 134 /* Transform sockaddr_in to netbuf */ 135 bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in); 136 bindaddr.buf = raddr; 137 138 bindresvport(fd, NULL); 139 cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers, 140 sendsz, recvsz); 141 if (cl) { 142 if (madefd == TRUE) { 143 /* 144 * The fd should be closed while destroying the handle. 145 */ 146 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL); 147 *sockp = fd; 148 } 149 (void) freenetconfigent(nconf); 150 mutex_unlock(&rpcsoc_lock); 151 return (cl); 152 } 153 goto err; 154 155 syserror: 156 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 157 rpc_createerr.cf_error.re_errno = errno; 158 159 err: if (madefd == TRUE) 160 (void)_close(fd); 161 (void) freenetconfigent(nconf); 162 mutex_unlock(&rpcsoc_lock); 163 return (NULL); 164 } 165 166 CLIENT * 167 clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz) 168 struct sockaddr_in *raddr; 169 u_long prog; 170 u_long vers; 171 struct timeval wait; 172 int *sockp; 173 u_int sendsz; 174 u_int recvsz; 175 { 176 CLIENT *cl; 177 178 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp, 179 sendsz, recvsz, "udp"); 180 if (cl == NULL) { 181 return (NULL); 182 } 183 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait); 184 return (cl); 185 } 186 187 CLIENT * 188 clntudp_create(raddr, program, version, wait, sockp) 189 struct sockaddr_in *raddr; 190 u_long program; 191 u_long version; 192 struct timeval wait; 193 int *sockp; 194 { 195 196 return clntudp_bufcreate(raddr, program, version, wait, sockp, 197 UDPMSGSIZE, UDPMSGSIZE); 198 } 199 200 CLIENT * 201 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz) 202 struct sockaddr_in *raddr; 203 u_long prog; 204 u_long vers; 205 int *sockp; 206 u_int sendsz; 207 u_int recvsz; 208 { 209 210 return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp, 211 sendsz, recvsz, "tcp"); 212 } 213 214 CLIENT * 215 clntraw_create(prog, vers) 216 u_long prog; 217 u_long vers; 218 { 219 220 return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers); 221 } 222 223 /* 224 * A common server create routine 225 */ 226 static SVCXPRT * 227 svc_com_create(fd, sendsize, recvsize, netid) 228 int fd; 229 u_int sendsize; 230 u_int recvsize; 231 char *netid; 232 { 233 struct netconfig *nconf; 234 SVCXPRT *svc; 235 int madefd = FALSE; 236 int port; 237 struct sockaddr_in sin; 238 239 if ((nconf = __rpc_getconfip(netid)) == NULL) { 240 (void) syslog(LOG_ERR, "Could not get %s transport", netid); 241 return (NULL); 242 } 243 if (fd == RPC_ANYSOCK) { 244 fd = __rpc_nconf2fd(nconf); 245 if (fd == -1) { 246 (void) freenetconfigent(nconf); 247 (void) syslog(LOG_ERR, 248 "svc%s_create: could not open connection", netid); 249 return (NULL); 250 } 251 madefd = TRUE; 252 } 253 254 memset(&sin, 0, sizeof sin); 255 sin.sin_family = AF_INET; 256 bindresvport(fd, &sin); 257 _listen(fd, SOMAXCONN); 258 svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize); 259 (void) freenetconfigent(nconf); 260 if (svc == NULL) { 261 if (madefd) 262 (void)_close(fd); 263 return (NULL); 264 } 265 port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port); 266 svc->xp_port = ntohs(port); 267 return (svc); 268 } 269 270 SVCXPRT * 271 svctcp_create(fd, sendsize, recvsize) 272 int fd; 273 u_int sendsize; 274 u_int recvsize; 275 { 276 277 return svc_com_create(fd, sendsize, recvsize, "tcp"); 278 } 279 280 SVCXPRT * 281 svcudp_bufcreate(fd, sendsz, recvsz) 282 int fd; 283 u_int sendsz, recvsz; 284 { 285 286 return svc_com_create(fd, sendsz, recvsz, "udp"); 287 } 288 289 SVCXPRT * 290 svcfd_create(fd, sendsize, recvsize) 291 int fd; 292 u_int sendsize; 293 u_int recvsize; 294 { 295 296 return svc_fd_create(fd, sendsize, recvsize); 297 } 298 299 300 SVCXPRT * 301 svcudp_create(fd) 302 int fd; 303 { 304 305 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp"); 306 } 307 308 SVCXPRT * 309 svcraw_create() 310 { 311 312 return svc_raw_create(); 313 } 314 315 int 316 get_myaddress(addr) 317 struct sockaddr_in *addr; 318 { 319 320 memset((void *) addr, 0, sizeof(*addr)); 321 addr->sin_family = AF_INET; 322 addr->sin_port = htons(PMAPPORT); 323 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK); 324 return (0); 325 } 326 327 /* 328 * For connectionless "udp" transport. Obsoleted by rpc_call(). 329 */ 330 int 331 callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out) 332 const char *host; 333 int prognum, versnum, procnum; 334 xdrproc_t inproc, outproc; 335 void *in, *out; 336 { 337 338 return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum, 339 (rpcproc_t)procnum, inproc, in, outproc, out, "udp"); 340 } 341 342 /* 343 * For connectionless kind of transport. Obsoleted by rpc_reg() 344 */ 345 int 346 registerrpc(prognum, versnum, procnum, progname, inproc, outproc) 347 int prognum, versnum, procnum; 348 char *(*progname)(char [UDPMSGSIZE]); 349 xdrproc_t inproc, outproc; 350 { 351 352 return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum, 353 (rpcproc_t)procnum, progname, inproc, outproc, "udp"); 354 } 355 356 /* 357 * All the following clnt_broadcast stuff is convulated; it supports 358 * the earlier calling style of the callback function 359 */ 360 static thread_key_t clnt_broadcast_key; 361 static resultproc_t clnt_broadcast_result_main; 362 static once_t clnt_broadcast_once = ONCE_INITIALIZER; 363 364 static void 365 clnt_broadcast_key_init(void) 366 { 367 368 thr_keycreate(&clnt_broadcast_key, free); 369 } 370 371 /* 372 * Need to translate the netbuf address into sockaddr_in address. 373 * Dont care about netid here. 374 */ 375 /* ARGSUSED */ 376 static bool_t 377 rpc_wrap_bcast(resultp, addr, nconf) 378 char *resultp; /* results of the call */ 379 struct netbuf *addr; /* address of the guy who responded */ 380 struct netconfig *nconf; /* Netconf of the transport */ 381 { 382 resultproc_t clnt_broadcast_result; 383 384 if (strcmp(nconf->nc_netid, "udp")) 385 return (FALSE); 386 if (thr_main()) 387 clnt_broadcast_result = clnt_broadcast_result_main; 388 else 389 clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key); 390 return (*clnt_broadcast_result)(resultp, 391 (struct sockaddr_in *)addr->buf); 392 } 393 394 /* 395 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast(). 396 */ 397 enum clnt_stat 398 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) 399 u_long prog; /* program number */ 400 u_long vers; /* version number */ 401 u_long proc; /* procedure number */ 402 xdrproc_t xargs; /* xdr routine for args */ 403 void *argsp; /* pointer to args */ 404 xdrproc_t xresults; /* xdr routine for results */ 405 void *resultsp; /* pointer to results */ 406 resultproc_t eachresult; /* call with each result obtained */ 407 { 408 409 if (thr_main()) 410 clnt_broadcast_result_main = eachresult; 411 else { 412 thr_once(&clnt_broadcast_once, clnt_broadcast_key_init); 413 thr_setspecific(clnt_broadcast_key, (void *) eachresult); 414 } 415 return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers, 416 (rpcproc_t)proc, xargs, argsp, xresults, resultsp, 417 (resultproc_t) rpc_wrap_bcast, "udp"); 418 } 419 420 /* 421 * Create the client des authentication object. Obsoleted by 422 * authdes_seccreate(). 423 */ 424 AUTH * 425 authdes_create(servername, window, syncaddr, ckey) 426 char *servername; /* network name of server */ 427 u_int window; /* time to live */ 428 struct sockaddr *syncaddr; /* optional hostaddr to sync with */ 429 des_block *ckey; /* optional conversation key to use */ 430 { 431 AUTH *dummy; 432 AUTH *nauth; 433 char hostname[NI_MAXHOST]; 434 435 if (syncaddr) { 436 /* 437 * Change addr to hostname, because that is the way 438 * new interface takes it. 439 */ 440 if (getnameinfo(syncaddr, syncaddr->sa_len, hostname, 441 sizeof hostname, NULL, 0, 0) != 0) 442 goto fallback; 443 444 nauth = authdes_seccreate(servername, window, hostname, ckey); 445 return (nauth); 446 } 447 fallback: 448 dummy = authdes_seccreate(servername, window, NULL, ckey); 449 return (dummy); 450 } 451 452 /* 453 * Create a client handle for a unix connection. Obsoleted by clnt_vc_create() 454 */ 455 CLIENT * 456 clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz) 457 struct sockaddr_un *raddr; 458 u_long prog; 459 u_long vers; 460 int *sockp; 461 u_int sendsz; 462 u_int recvsz; 463 { 464 struct netbuf *svcaddr; 465 struct netconfig *nconf; 466 CLIENT *cl; 467 int len; 468 469 cl = NULL; 470 nconf = NULL; 471 svcaddr = NULL; 472 if ((raddr->sun_len == 0) || 473 ((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) || 474 ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) { 475 if (svcaddr != NULL) 476 free(svcaddr); 477 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 478 rpc_createerr.cf_error.re_errno = errno; 479 return(cl); 480 } 481 if (*sockp < 0) { 482 *sockp = _socket(AF_LOCAL, SOCK_STREAM, 0); 483 len = raddr->sun_len = SUN_LEN(raddr); 484 if ((*sockp < 0) || (_connect(*sockp, 485 (struct sockaddr *)raddr, len) < 0)) { 486 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 487 rpc_createerr.cf_error.re_errno = errno; 488 if (*sockp != -1) 489 (void)_close(*sockp); 490 goto done; 491 } 492 } 493 svcaddr->buf = raddr; 494 svcaddr->len = raddr->sun_len; 495 svcaddr->maxlen = sizeof (struct sockaddr_un); 496 cl = clnt_vc_create(*sockp, svcaddr, prog, 497 vers, sendsz, recvsz); 498 done: 499 free(svcaddr->buf); 500 free(svcaddr); 501 return(cl); 502 } 503 504 /* 505 * Creates, registers, and returns a (rpc) unix based transporter. 506 * Obsoleted by svc_vc_create(). 507 */ 508 SVCXPRT * 509 svcunix_create(sock, sendsize, recvsize, path) 510 int sock; 511 u_int sendsize; 512 u_int recvsize; 513 char *path; 514 { 515 struct netconfig *nconf; 516 void *localhandle; 517 struct sockaddr_un sun; 518 struct sockaddr *sa; 519 struct t_bind taddr; 520 SVCXPRT *xprt; 521 int addrlen; 522 523 xprt = (SVCXPRT *)NULL; 524 localhandle = setnetconfig(); 525 while ((nconf = getnetconfig(localhandle)) != NULL) { 526 if (nconf->nc_protofmly != NULL && 527 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) 528 break; 529 } 530 if (nconf == NULL) 531 return(xprt); 532 533 if ((sock = __rpc_nconf2fd(nconf)) < 0) 534 goto done; 535 536 memset(&sun, 0, sizeof sun); 537 sun.sun_family = AF_LOCAL; 538 if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >= 539 sizeof(sun.sun_path)) 540 goto done; 541 sun.sun_len = SUN_LEN(&sun); 542 addrlen = sizeof (struct sockaddr_un); 543 sa = (struct sockaddr *)&sun; 544 545 if (_bind(sock, sa, addrlen) < 0) 546 goto done; 547 548 taddr.addr.len = taddr.addr.maxlen = addrlen; 549 taddr.addr.buf = malloc(addrlen); 550 if (taddr.addr.buf == NULL) 551 goto done; 552 memcpy(taddr.addr.buf, sa, addrlen); 553 554 if (nconf->nc_semantics != NC_TPI_CLTS) { 555 if (_listen(sock, SOMAXCONN) < 0) { 556 free(taddr.addr.buf); 557 goto done; 558 } 559 } 560 561 xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize); 562 563 done: 564 endnetconfig(localhandle); 565 return(xprt); 566 } 567 568 /* 569 * Like svunix_create(), except the routine takes any *open* UNIX file 570 * descriptor as its first input. Obsoleted by svc_fd_create(); 571 */ 572 SVCXPRT * 573 svcunixfd_create(fd, sendsize, recvsize) 574 int fd; 575 u_int sendsize; 576 u_int recvsize; 577 { 578 return (svc_fd_create(fd, sendsize, recvsize)); 579 } 580 581 #endif /* PORTMAP */ 582