1 /* $NetBSD: rpc_soc.c,v 1.6 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 /* #ident "@(#)rpc_soc.c 1.17 94/04/24 SMI" */ 33 34 /* 35 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 36 * In addition, portions of such source code were derived from Berkeley 37 * 4.3 BSD under license from the Regents of the University of 38 * California. 39 */ 40 41 #if defined(LIBC_SCCS) && !defined(lint) 42 static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro"; 43 #endif 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #ifdef PORTMAP 48 /* 49 * rpc_soc.c 50 * 51 * The backward compatibility routines for the earlier implementation 52 * of RPC, where the only transports supported were tcp/ip and udp/ip. 53 * Based on berkeley socket abstraction, now implemented on the top 54 * of TLI/Streams 55 */ 56 57 #include "namespace.h" 58 #include "reentrant.h" 59 #include <sys/types.h> 60 #include <sys/socket.h> 61 #include <stdio.h> 62 #include <rpc/rpc.h> 63 #include <rpc/pmap_clnt.h> 64 #include <rpc/pmap_prot.h> 65 #include <rpc/nettype.h> 66 #include <syslog.h> 67 #include <netinet/in.h> 68 #include <netdb.h> 69 #include <errno.h> 70 #include <syslog.h> 71 #include <stdlib.h> 72 #include <string.h> 73 #include <unistd.h> 74 #include "un-namespace.h" 75 76 #include "rpc_com.h" 77 78 extern mutex_t rpcsoc_lock; 79 80 static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t, 81 int *, u_int, u_int, char *); 82 static SVCXPRT *svc_com_create(int, u_int, u_int, char *); 83 static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *); 84 85 /* XXX */ 86 #define IN4_LOCALHOST_STRING "127.0.0.1" 87 #define IN6_LOCALHOST_STRING "::1" 88 89 /* 90 * A common clnt create routine 91 */ 92 static CLIENT * 93 clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp) 94 struct sockaddr_in *raddr; 95 rpcprog_t prog; 96 rpcvers_t vers; 97 int *sockp; 98 u_int sendsz; 99 u_int recvsz; 100 char *tp; 101 { 102 CLIENT *cl; 103 int madefd = FALSE; 104 int fd = *sockp; 105 struct netconfig *nconf; 106 struct netbuf bindaddr; 107 108 mutex_lock(&rpcsoc_lock); 109 if ((nconf = __rpc_getconfip(tp)) == NULL) { 110 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 111 mutex_unlock(&rpcsoc_lock); 112 return (NULL); 113 } 114 if (fd == RPC_ANYSOCK) { 115 fd = __rpc_nconf2fd(nconf); 116 if (fd == -1) 117 goto syserror; 118 madefd = TRUE; 119 } 120 121 if (raddr->sin_port == 0) { 122 u_int proto; 123 u_short sport; 124 125 mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */ 126 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP; 127 sport = pmap_getport(raddr, (u_long)prog, (u_long)vers, 128 proto); 129 if (sport == 0) { 130 goto err; 131 } 132 raddr->sin_port = htons(sport); 133 mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */ 134 } 135 136 /* Transform sockaddr_in to netbuf */ 137 bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in); 138 bindaddr.buf = raddr; 139 140 bindresvport(fd, NULL); 141 cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers, 142 sendsz, recvsz); 143 if (cl) { 144 if (madefd == TRUE) { 145 /* 146 * The fd should be closed while destroying the handle. 147 */ 148 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL); 149 *sockp = fd; 150 } 151 (void) freenetconfigent(nconf); 152 mutex_unlock(&rpcsoc_lock); 153 return (cl); 154 } 155 goto err; 156 157 syserror: 158 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 159 rpc_createerr.cf_error.re_errno = errno; 160 161 err: if (madefd == TRUE) 162 (void)_close(fd); 163 (void) freenetconfigent(nconf); 164 mutex_unlock(&rpcsoc_lock); 165 return (NULL); 166 } 167 168 CLIENT * 169 clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz) 170 struct sockaddr_in *raddr; 171 u_long prog; 172 u_long vers; 173 struct timeval wait; 174 int *sockp; 175 u_int sendsz; 176 u_int recvsz; 177 { 178 CLIENT *cl; 179 180 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp, 181 sendsz, recvsz, "udp"); 182 if (cl == NULL) { 183 return (NULL); 184 } 185 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait); 186 return (cl); 187 } 188 189 CLIENT * 190 clntudp_create(raddr, program, version, wait, sockp) 191 struct sockaddr_in *raddr; 192 u_long program; 193 u_long version; 194 struct timeval wait; 195 int *sockp; 196 { 197 198 return clntudp_bufcreate(raddr, program, version, wait, sockp, 199 UDPMSGSIZE, UDPMSGSIZE); 200 } 201 202 CLIENT * 203 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz) 204 struct sockaddr_in *raddr; 205 u_long prog; 206 u_long vers; 207 int *sockp; 208 u_int sendsz; 209 u_int recvsz; 210 { 211 212 return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp, 213 sendsz, recvsz, "tcp"); 214 } 215 216 CLIENT * 217 clntraw_create(prog, vers) 218 u_long prog; 219 u_long vers; 220 { 221 222 return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers); 223 } 224 225 /* 226 * A common server create routine 227 */ 228 static SVCXPRT * 229 svc_com_create(fd, sendsize, recvsize, netid) 230 int fd; 231 u_int sendsize; 232 u_int recvsize; 233 char *netid; 234 { 235 struct netconfig *nconf; 236 SVCXPRT *svc; 237 int madefd = FALSE; 238 int port; 239 struct sockaddr_in sin; 240 241 if ((nconf = __rpc_getconfip(netid)) == NULL) { 242 (void) syslog(LOG_ERR, "Could not get %s transport", netid); 243 return (NULL); 244 } 245 if (fd == RPC_ANYSOCK) { 246 fd = __rpc_nconf2fd(nconf); 247 if (fd == -1) { 248 (void) freenetconfigent(nconf); 249 (void) syslog(LOG_ERR, 250 "svc%s_create: could not open connection", netid); 251 return (NULL); 252 } 253 madefd = TRUE; 254 } 255 256 memset(&sin, 0, sizeof sin); 257 sin.sin_family = AF_INET; 258 bindresvport(fd, &sin); 259 _listen(fd, SOMAXCONN); 260 svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize); 261 (void) freenetconfigent(nconf); 262 if (svc == NULL) { 263 if (madefd) 264 (void)_close(fd); 265 return (NULL); 266 } 267 port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port); 268 svc->xp_port = ntohs(port); 269 return (svc); 270 } 271 272 SVCXPRT * 273 svctcp_create(fd, sendsize, recvsize) 274 int fd; 275 u_int sendsize; 276 u_int recvsize; 277 { 278 279 return svc_com_create(fd, sendsize, recvsize, "tcp"); 280 } 281 282 SVCXPRT * 283 svcudp_bufcreate(fd, sendsz, recvsz) 284 int fd; 285 u_int sendsz, recvsz; 286 { 287 288 return svc_com_create(fd, sendsz, recvsz, "udp"); 289 } 290 291 SVCXPRT * 292 svcfd_create(fd, sendsize, recvsize) 293 int fd; 294 u_int sendsize; 295 u_int recvsize; 296 { 297 298 return svc_fd_create(fd, sendsize, recvsize); 299 } 300 301 302 SVCXPRT * 303 svcudp_create(fd) 304 int fd; 305 { 306 307 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp"); 308 } 309 310 SVCXPRT * 311 svcraw_create() 312 { 313 314 return svc_raw_create(); 315 } 316 317 int 318 get_myaddress(addr) 319 struct sockaddr_in *addr; 320 { 321 322 memset((void *) addr, 0, sizeof(*addr)); 323 addr->sin_family = AF_INET; 324 addr->sin_port = htons(PMAPPORT); 325 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK); 326 return (0); 327 } 328 329 /* 330 * For connectionless "udp" transport. Obsoleted by rpc_call(). 331 */ 332 int 333 callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out) 334 const char *host; 335 int prognum, versnum, procnum; 336 xdrproc_t inproc, outproc; 337 void *in, *out; 338 { 339 340 return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum, 341 (rpcproc_t)procnum, inproc, in, outproc, out, "udp"); 342 } 343 344 /* 345 * For connectionless kind of transport. Obsoleted by rpc_reg() 346 */ 347 int 348 registerrpc(prognum, versnum, procnum, progname, inproc, outproc) 349 int prognum, versnum, procnum; 350 char *(*progname)(char [UDPMSGSIZE]); 351 xdrproc_t inproc, outproc; 352 { 353 354 return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum, 355 (rpcproc_t)procnum, progname, inproc, outproc, "udp"); 356 } 357 358 /* 359 * All the following clnt_broadcast stuff is convulated; it supports 360 * the earlier calling style of the callback function 361 */ 362 static thread_key_t clnt_broadcast_key; 363 static resultproc_t clnt_broadcast_result_main; 364 365 /* 366 * Need to translate the netbuf address into sockaddr_in address. 367 * Dont care about netid here. 368 */ 369 /* ARGSUSED */ 370 static bool_t 371 rpc_wrap_bcast(resultp, addr, nconf) 372 char *resultp; /* results of the call */ 373 struct netbuf *addr; /* address of the guy who responded */ 374 struct netconfig *nconf; /* Netconf of the transport */ 375 { 376 resultproc_t clnt_broadcast_result; 377 378 if (strcmp(nconf->nc_netid, "udp")) 379 return (FALSE); 380 if (thr_main()) 381 clnt_broadcast_result = clnt_broadcast_result_main; 382 else 383 clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key); 384 return (*clnt_broadcast_result)(resultp, 385 (struct sockaddr_in *)addr->buf); 386 } 387 388 /* 389 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast(). 390 */ 391 enum clnt_stat 392 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) 393 u_long prog; /* program number */ 394 u_long vers; /* version number */ 395 u_long proc; /* procedure number */ 396 xdrproc_t xargs; /* xdr routine for args */ 397 void *argsp; /* pointer to args */ 398 xdrproc_t xresults; /* xdr routine for results */ 399 void *resultsp; /* pointer to results */ 400 resultproc_t eachresult; /* call with each result obtained */ 401 { 402 extern mutex_t tsd_lock; 403 404 if (thr_main()) 405 clnt_broadcast_result_main = eachresult; 406 else { 407 if (clnt_broadcast_key == 0) { 408 mutex_lock(&tsd_lock); 409 if (clnt_broadcast_key == 0) 410 thr_keycreate(&clnt_broadcast_key, free); 411 mutex_unlock(&tsd_lock); 412 } 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