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