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