1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 /* 30 * Portions of this source code were derived from Berkeley 31 * 4.3 BSD under license from the Regents of the University of 32 * California. 33 */ 34 35 #include "mt.h" 36 #include "rpc_mt.h" 37 #include <stdio.h> 38 #include <errno.h> 39 #include <unistd.h> 40 #include <stdlib.h> 41 #include <rpc/rpc.h> 42 #include <rpc/nettype.h> 43 #include <netdir.h> 44 #include <string.h> 45 #include <syslog.h> 46 47 extern int __td_setnodelay(int); 48 extern bool_t __rpc_is_local_host(const char *); 49 extern bool_t __rpc_try_doors(const char *, bool_t *); 50 extern CLIENT *_clnt_vc_create_timed(int, struct netbuf *, rpcprog_t, 51 rpcvers_t, uint_t, uint_t, const struct timeval *); 52 53 CLIENT *_clnt_tli_create_timed(int, const struct netconfig *, struct netbuf *, 54 rpcprog_t, rpcvers_t, uint_t, uint_t, const struct timeval *); 55 56 #ifndef NETIDLEN 57 #define NETIDLEN 32 58 #endif 59 60 /* 61 * Generic client creation with version checking the value of 62 * vers_out is set to the highest server supported value 63 * vers_low <= vers_out <= vers_high AND an error results 64 * if this can not be done. 65 * 66 * It calls clnt_create_vers_timed() with a NULL value for the timeout 67 * pointer, which indicates that the default timeout should be used. 68 */ 69 CLIENT * 70 clnt_create_vers(const char *hostname, const rpcprog_t prog, 71 rpcvers_t *vers_out, const rpcvers_t vers_low, 72 const rpcvers_t vers_high, const char *nettype) 73 { 74 return (clnt_create_vers_timed(hostname, prog, vers_out, vers_low, 75 vers_high, nettype, NULL)); 76 } 77 78 /* 79 * This routine has the same definition as clnt_create_vers(), 80 * except it takes an additional timeout parameter - a pointer to 81 * a timeval structure. A NULL value for the pointer indicates 82 * that the default timeout value should be used. 83 */ 84 CLIENT * 85 clnt_create_vers_timed(const char *hostname, const rpcprog_t prog, 86 rpcvers_t *vers_out, const rpcvers_t vers_low, const rpcvers_t vers_high, 87 const char *nettype, const struct timeval *tp) 88 { 89 CLIENT *clnt; 90 struct timeval to; 91 enum clnt_stat rpc_stat; 92 struct rpc_err rpcerr; 93 rpcvers_t v_low, v_high; 94 95 clnt = clnt_create_timed(hostname, prog, vers_high, nettype, tp); 96 if (clnt == NULL) 97 return (NULL); 98 if (tp == NULL) { 99 to.tv_sec = 10; 100 to.tv_usec = 0; 101 } else 102 to = *tp; 103 104 rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void, 105 NULL, (xdrproc_t)xdr_void, NULL, to); 106 if (rpc_stat == RPC_SUCCESS) { 107 *vers_out = vers_high; 108 return (clnt); 109 } 110 v_low = vers_low; 111 v_high = vers_high; 112 while (rpc_stat == RPC_PROGVERSMISMATCH && v_high > v_low) { 113 unsigned int minvers, maxvers; 114 115 clnt_geterr(clnt, &rpcerr); 116 minvers = rpcerr.re_vers.low; 117 maxvers = rpcerr.re_vers.high; 118 if (maxvers < v_high) 119 v_high = maxvers; 120 else 121 v_high--; 122 if (minvers > v_low) 123 v_low = minvers; 124 if (v_low > v_high) { 125 goto error; 126 } 127 CLNT_CONTROL(clnt, CLSET_VERS, (char *)&v_high); 128 rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void, 129 NULL, (xdrproc_t)xdr_void, 130 NULL, to); 131 if (rpc_stat == RPC_SUCCESS) { 132 *vers_out = v_high; 133 return (clnt); 134 } 135 } 136 clnt_geterr(clnt, &rpcerr); 137 138 error: 139 rpc_createerr.cf_stat = rpc_stat; 140 rpc_createerr.cf_error = rpcerr; 141 clnt_destroy(clnt); 142 return (NULL); 143 } 144 145 /* 146 * Top level client creation routine. 147 * Generic client creation: takes (servers name, program-number, nettype) and 148 * returns client handle. Default options are set, which the user can 149 * change using the rpc equivalent of ioctl()'s. 150 * 151 * It tries for all the netids in that particular class of netid until 152 * it succeeds. 153 * XXX The error message in the case of failure will be the one 154 * pertaining to the last create error. 155 * 156 * It calls clnt_create_timed() with the default timeout. 157 */ 158 CLIENT * 159 clnt_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers, 160 const char *nettype) 161 { 162 return (clnt_create_timed(hostname, prog, vers, nettype, NULL)); 163 } 164 165 /* 166 * This the routine has the same definition as clnt_create(), 167 * except it takes an additional timeout parameter - a pointer to 168 * a timeval structure. A NULL value for the pointer indicates 169 * that the default timeout value should be used. 170 * 171 * This function calls clnt_tp_create_timed(). 172 */ 173 CLIENT * 174 clnt_create_timed(const char *hostname, const rpcprog_t prog, 175 const rpcvers_t vers, const char *netclass, const struct timeval *tp) 176 { 177 struct netconfig *nconf; 178 CLIENT *clnt = NULL; 179 void *handle; 180 enum clnt_stat save_cf_stat = RPC_SUCCESS; 181 struct rpc_err save_cf_error; 182 char nettype_array[NETIDLEN]; 183 char *nettype = &nettype_array[0]; 184 bool_t try_others; 185 186 if (netclass == NULL) 187 nettype = NULL; 188 else { 189 size_t len = strlen(netclass); 190 if (len >= sizeof (nettype_array)) { 191 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 192 return (NULL); 193 } 194 (void) strcpy(nettype, netclass); 195 } 196 197 /* 198 * Check to see if a rendezvous over doors should be attempted. 199 */ 200 if (__rpc_try_doors(nettype, &try_others)) { 201 /* 202 * Make sure this is the local host. 203 */ 204 if (__rpc_is_local_host(hostname)) { 205 if ((clnt = clnt_door_create(prog, vers, 0)) != NULL) 206 return (clnt); 207 else { 208 if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) 209 return (NULL); 210 save_cf_stat = rpc_createerr.cf_stat; 211 save_cf_error = rpc_createerr.cf_error; 212 } 213 } else { 214 save_cf_stat = rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 215 } 216 } 217 if (!try_others) 218 return (NULL); 219 220 if ((handle = __rpc_setconf((char *)nettype)) == NULL) { 221 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 222 return (NULL); 223 } 224 rpc_createerr.cf_stat = RPC_SUCCESS; 225 while (clnt == NULL) { 226 if ((nconf = __rpc_getconf(handle)) == NULL) { 227 if (rpc_createerr.cf_stat == RPC_SUCCESS) 228 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 229 break; 230 } 231 clnt = clnt_tp_create_timed(hostname, prog, vers, nconf, tp); 232 if (clnt) 233 break; 234 else { 235 /* 236 * Since we didn't get a name-to-address 237 * translation failure here, we remember 238 * this particular error. The object of 239 * this is to enable us to return to the 240 * caller a more-specific error than the 241 * unhelpful ``Name to address translation 242 * failed'' which might well occur if we 243 * merely returned the last error (because 244 * the local loopbacks are typically the 245 * last ones in /etc/netconfig and the most 246 * likely to be unable to translate a host 247 * name). We also check for a more 248 * meaningful error than ``unknown host 249 * name'' for the same reasons. 250 */ 251 if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) { 252 syslog(LOG_ERR, "clnt_create_timed: " 253 "RPC_SYSTEMERROR."); 254 break; 255 } 256 257 if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE && 258 rpc_createerr.cf_stat != RPC_UNKNOWNHOST) { 259 save_cf_stat = rpc_createerr.cf_stat; 260 save_cf_error = rpc_createerr.cf_error; 261 } 262 } 263 } 264 265 /* 266 * Attempt to return an error more specific than ``Name to address 267 * translation failed'' or ``unknown host name'' 268 */ 269 if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE || 270 rpc_createerr.cf_stat == RPC_UNKNOWNHOST) && 271 (save_cf_stat != RPC_SUCCESS)) { 272 rpc_createerr.cf_stat = save_cf_stat; 273 rpc_createerr.cf_error = save_cf_error; 274 } 275 __rpc_endconf(handle); 276 return (clnt); 277 } 278 279 /* 280 * Create a client handle for a well known service or a specific port on 281 * host. This routine bypasses rpcbind and can be use to construct a client 282 * handle to services that are not registered with rpcbind or where the remote 283 * rpcbind is not available, e.g., the remote rpcbind port is blocked by a 284 * firewall. We construct a client handle and then ping the service's NULL 285 * proc to see that the service is really available. If the caller supplies 286 * a non zero port number, the service name is ignored and the port will be 287 * used. A non-zero port number limits the protocol family to inet or inet6. 288 */ 289 290 CLIENT * 291 clnt_create_service_timed(const char *host, const char *service, 292 const rpcprog_t prog, const rpcvers_t vers, 293 const ushort_t port, const char *netclass, 294 const struct timeval *tmout) 295 { 296 int fd; 297 void *handle; 298 CLIENT *clnt = NULL; 299 struct netconfig *nconf; 300 struct nd_hostserv hs; 301 struct nd_addrlist *raddrs; 302 struct t_bind *tbind = NULL; 303 struct timeval to; 304 char nettype_array[NETIDLEN]; 305 char *nettype = &nettype_array[0]; 306 char *hostname, *serv; 307 bool_t try_others; 308 309 /* 310 * handle const of netclass 311 */ 312 if (netclass == NULL) 313 nettype = NULL; 314 else { 315 size_t len = strlen(netclass); 316 if (len >= sizeof (nettype_array)) { 317 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 318 return (NULL); 319 } 320 (void) strcpy(nettype, netclass); 321 } 322 323 if (tmout == NULL) { 324 to.tv_sec = 10; 325 to.tv_usec = 0; 326 } else 327 to = *tmout; 328 329 if ((handle = __rpc_setconf(nettype)) == NULL) { 330 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 331 return (NULL); 332 } 333 334 /* 335 * Sinct host, and service are const 336 */ 337 if (host == NULL || (hostname = strdup(host)) == NULL) { 338 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 339 rpc_createerr.cf_error.re_errno = (host ? errno : EINVAL); 340 rpc_createerr.cf_error.re_terrno = 0; 341 return (NULL); 342 } 343 344 if (service == NULL) 345 serv = NULL; 346 else if ((serv = strdup(service ? service : "")) == NULL) { 347 free(hostname); 348 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 349 rpc_createerr.cf_error.re_errno = errno; 350 rpc_createerr.cf_error.re_terrno = 0; 351 return (NULL); 352 } 353 354 hs.h_host = hostname; 355 hs.h_serv = port ? NULL : serv; 356 357 /* 358 * Check to see if a rendezvous over doors should be attempted. 359 */ 360 if (__rpc_try_doors(nettype, &try_others)) { 361 /* 362 * Make sure this is the local host. 363 */ 364 if (__rpc_is_local_host(hostname)) { 365 if ((clnt = clnt_door_create(prog, vers, 0)) != NULL) 366 goto done; 367 else if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) 368 goto done; 369 } else { 370 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 371 } 372 } 373 if (!try_others) 374 goto done; 375 376 rpc_createerr.cf_stat = RPC_SUCCESS; 377 while (clnt == NULL) { 378 tbind = NULL; 379 if ((nconf = __rpc_getconf(handle)) == NULL) { 380 if (rpc_createerr.cf_stat == RPC_SUCCESS) 381 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 382 break; 383 } 384 385 if (port) { 386 if (strcmp(nconf->nc_protofmly, NC_INET) != 0 && 387 strcmp(nconf->nc_protofmly, NC_INET6) != 0) 388 continue; 389 } 390 391 if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) { 392 rpc_createerr.cf_stat = RPC_TLIERROR; 393 rpc_createerr.cf_error.re_errno = errno; 394 rpc_createerr.cf_error.re_terrno = t_errno; 395 continue; 396 } 397 398 RPC_RAISEFD(fd); 399 400 __rpc_set_mac_options(fd, nconf, prog); 401 402 /* LINTED pointer cast */ 403 if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR)) 404 == NULL) { 405 (void) t_close(fd); 406 rpc_createerr.cf_stat = RPC_TLIERROR; 407 rpc_createerr.cf_error.re_errno = errno; 408 rpc_createerr.cf_error.re_terrno = t_errno; 409 continue; 410 } 411 412 if (netdir_getbyname(nconf, &hs, &raddrs) != ND_OK) { 413 if (rpc_createerr.cf_stat == RPC_SUCCESS) 414 rpc_createerr.cf_stat = RPC_UNKNOWNHOST; 415 if (tbind) 416 (void) t_free((char *)tbind, T_BIND); 417 (void) t_close(fd); 418 continue; 419 } 420 (void) memcpy(tbind->addr.buf, raddrs->n_addrs->buf, 421 raddrs->n_addrs->len); 422 tbind->addr.len = raddrs->n_addrs->len; 423 netdir_free((void *)raddrs, ND_ADDRLIST); 424 425 if (port) { 426 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) 427 /* LINTED pointer alignment */ 428 ((struct sockaddr_in *) 429 tbind->addr.buf)->sin_port = htons(port); 430 else if (strcmp(nconf->nc_protofmly, NC_INET6) == 0) 431 /* LINTED pointer alignment */ 432 ((struct sockaddr_in6 *) 433 tbind->addr.buf)->sin6_port = htons(port); 434 } 435 436 clnt = _clnt_tli_create_timed(fd, nconf, &tbind->addr, 437 prog, vers, 0, 0, &to); 438 439 if (clnt == NULL) { 440 if (tbind) 441 (void) t_free((char *)tbind, T_BIND); 442 (void) t_close(fd); 443 continue; 444 } 445 446 (void) CLNT_CONTROL(clnt, CLSET_FD_CLOSE, NULL); 447 448 /* 449 * Check if we can reach the server with this clnt handle 450 * Other clnt_create calls do a ping by contacting the 451 * remote rpcbind, here will just try to execute the service's 452 * NULL proc. 453 */ 454 455 rpc_createerr.cf_stat = clnt_call(clnt, NULLPROC, 456 xdr_void, 0, xdr_void, 0, to); 457 458 rpc_createerr.cf_error.re_errno = rpc_callerr.re_status; 459 rpc_createerr.cf_error.re_terrno = 0; 460 461 if (rpc_createerr.cf_stat != RPC_SUCCESS) { 462 clnt_destroy(clnt); 463 clnt = NULL; 464 if (tbind) 465 (void) t_free((char *)tbind, T_BIND); 466 continue; 467 } else 468 break; 469 } 470 471 __rpc_endconf(handle); 472 if (tbind) 473 (void) t_free((char *)tbind, T_BIND); 474 475 done: 476 if (hostname) 477 free(hostname); 478 if (serv) 479 free(serv); 480 481 return (clnt); 482 } 483 484 /* 485 * Generic client creation: takes (servers name, program-number, netconf) and 486 * returns client handle. Default options are set, which the user can 487 * change using the rpc equivalent of ioctl()'s : clnt_control() 488 * It finds out the server address from rpcbind and calls clnt_tli_create(). 489 * 490 * It calls clnt_tp_create_timed() with the default timeout. 491 */ 492 CLIENT * 493 clnt_tp_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers, 494 const struct netconfig *nconf) 495 { 496 return (clnt_tp_create_timed(hostname, prog, vers, nconf, NULL)); 497 } 498 499 /* 500 * This has the same definition as clnt_tp_create(), except it 501 * takes an additional parameter - a pointer to a timeval structure. 502 * A NULL value for the timeout pointer indicates that the default 503 * value for the timeout should be used. 504 */ 505 CLIENT * 506 clnt_tp_create_timed(const char *hostname, const rpcprog_t prog, 507 const rpcvers_t vers, const struct netconfig *nconf, 508 const struct timeval *tp) 509 { 510 struct netbuf *svcaddr; /* servers address */ 511 CLIENT *cl = NULL; /* client handle */ 512 extern struct netbuf *__rpcb_findaddr_timed(rpcprog_t, rpcvers_t, 513 struct netconfig *, char *, CLIENT **, struct timeval *); 514 515 if (nconf == NULL) { 516 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 517 return (NULL); 518 } 519 520 /* 521 * Get the address of the server 522 */ 523 if ((svcaddr = __rpcb_findaddr_timed(prog, vers, 524 (struct netconfig *)nconf, (char *)hostname, 525 &cl, (struct timeval *)tp)) == NULL) { 526 /* appropriate error number is set by rpcbind libraries */ 527 return (NULL); 528 } 529 if (cl == NULL) { 530 cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr, 531 prog, vers, 0, 0, tp); 532 } else { 533 /* Reuse the CLIENT handle and change the appropriate fields */ 534 if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) { 535 if (cl->cl_netid == NULL) { 536 cl->cl_netid = strdup(nconf->nc_netid); 537 if (cl->cl_netid == NULL) { 538 netdir_free((char *)svcaddr, ND_ADDR); 539 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 540 syslog(LOG_ERR, 541 "clnt_tp_create_timed: " 542 "strdup failed."); 543 return (NULL); 544 } 545 } 546 if (cl->cl_tp == NULL) { 547 cl->cl_tp = strdup(nconf->nc_device); 548 if (cl->cl_tp == NULL) { 549 netdir_free((char *)svcaddr, ND_ADDR); 550 if (cl->cl_netid) 551 free(cl->cl_netid); 552 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 553 syslog(LOG_ERR, 554 "clnt_tp_create_timed: " 555 "strdup failed."); 556 return (NULL); 557 } 558 } 559 (void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog); 560 (void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers); 561 } else { 562 CLNT_DESTROY(cl); 563 cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr, 564 prog, vers, 0, 0, tp); 565 } 566 } 567 netdir_free((char *)svcaddr, ND_ADDR); 568 return (cl); 569 } 570 571 /* 572 * Generic client creation: returns client handle. 573 * Default options are set, which the user can 574 * change using the rpc equivalent of ioctl()'s : clnt_control(). 575 * If fd is RPC_ANYFD, it will be opened using nconf. 576 * It will be bound if not so. 577 * If sizes are 0; appropriate defaults will be chosen. 578 */ 579 CLIENT * 580 clnt_tli_create(const int fd, const struct netconfig *nconf, 581 struct netbuf *svcaddr, const rpcprog_t prog, const rpcvers_t vers, 582 const uint_t sendsz, const uint_t recvsz) 583 { 584 return (_clnt_tli_create_timed(fd, nconf, svcaddr, prog, vers, sendsz, 585 recvsz, NULL)); 586 } 587 588 /* 589 * This has the same definition as clnt_tli_create(), except it 590 * takes an additional parameter - a pointer to a timeval structure. 591 * 592 * Not a public interface. This is for clnt_create_timed, 593 * clnt_create_vers_times, clnt_tp_create_timed to pass down the 594 * timeout value to COTS creation routine. 595 * (for bug 4049792: clnt_create_timed does not time out) 596 */ 597 CLIENT * 598 _clnt_tli_create_timed(int fd, const struct netconfig *nconf, 599 struct netbuf *svcaddr, rpcprog_t prog, rpcvers_t vers, uint_t sendsz, 600 uint_t recvsz, const struct timeval *tp) 601 { 602 CLIENT *cl; /* client handle */ 603 struct t_info tinfo; /* transport info */ 604 bool_t madefd; /* whether fd opened here */ 605 t_scalar_t servtype; 606 int retval; 607 608 if (fd == RPC_ANYFD) { 609 if (nconf == NULL) { 610 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 611 return (NULL); 612 } 613 614 fd = t_open(nconf->nc_device, O_RDWR, NULL); 615 if (fd == -1) 616 goto err; 617 RPC_RAISEFD(fd); 618 madefd = TRUE; 619 __rpc_set_mac_options(fd, nconf, prog); 620 if (t_bind(fd, NULL, NULL) == -1) 621 goto err; 622 switch (nconf->nc_semantics) { 623 case NC_TPI_CLTS: 624 servtype = T_CLTS; 625 break; 626 case NC_TPI_COTS: 627 servtype = T_COTS; 628 break; 629 case NC_TPI_COTS_ORD: 630 servtype = T_COTS_ORD; 631 break; 632 default: 633 if (t_getinfo(fd, &tinfo) == -1) 634 goto err; 635 servtype = tinfo.servtype; 636 break; 637 } 638 } else { 639 int state; /* Current state of provider */ 640 641 /* 642 * Sync the opened fd. 643 * Check whether bound or not, else bind it 644 */ 645 if (((state = t_sync(fd)) == -1) || 646 ((state == T_UNBND) && (t_bind(fd, NULL, NULL) == -1)) || 647 (t_getinfo(fd, &tinfo) == -1)) 648 goto err; 649 servtype = tinfo.servtype; 650 madefd = FALSE; 651 } 652 653 switch (servtype) { 654 case T_COTS: 655 cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz, 656 recvsz, tp); 657 break; 658 case T_COTS_ORD: 659 if (nconf && ((strcmp(nconf->nc_protofmly, NC_INET) == 0) || 660 (strcmp(nconf->nc_protofmly, NC_INET6) == 0))) { 661 retval = __td_setnodelay(fd); 662 if (retval == -1) 663 goto err; 664 } 665 cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz, 666 recvsz, tp); 667 break; 668 case T_CLTS: 669 cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz); 670 break; 671 default: 672 goto err; 673 } 674 675 if (cl == NULL) 676 goto err1; /* borrow errors from clnt_dg/vc creates */ 677 if (nconf) { 678 cl->cl_netid = strdup(nconf->nc_netid); 679 if (cl->cl_netid == NULL) { 680 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 681 rpc_createerr.cf_error.re_errno = errno; 682 rpc_createerr.cf_error.re_terrno = 0; 683 syslog(LOG_ERR, 684 "clnt_tli_create: strdup failed"); 685 goto err1; 686 } 687 cl->cl_tp = strdup(nconf->nc_device); 688 if (cl->cl_tp == NULL) { 689 if (cl->cl_netid) 690 free(cl->cl_netid); 691 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 692 rpc_createerr.cf_error.re_errno = errno; 693 rpc_createerr.cf_error.re_terrno = 0; 694 syslog(LOG_ERR, 695 "clnt_tli_create: strdup failed"); 696 goto err1; 697 } 698 } else { 699 struct netconfig *nc; 700 701 if ((nc = __rpcfd_to_nconf(fd, servtype)) != NULL) { 702 if (nc->nc_netid) { 703 cl->cl_netid = strdup(nc->nc_netid); 704 if (cl->cl_netid == NULL) { 705 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 706 rpc_createerr.cf_error.re_errno = errno; 707 rpc_createerr.cf_error.re_terrno = 0; 708 syslog(LOG_ERR, 709 "clnt_tli_create: " 710 "strdup failed"); 711 goto err1; 712 } 713 } 714 if (nc->nc_device) { 715 cl->cl_tp = strdup(nc->nc_device); 716 if (cl->cl_tp == NULL) { 717 if (cl->cl_netid) 718 free(cl->cl_netid); 719 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 720 rpc_createerr.cf_error.re_errno = errno; 721 rpc_createerr.cf_error.re_terrno = 0; 722 syslog(LOG_ERR, 723 "clnt_tli_create: " 724 "strdup failed"); 725 goto err1; 726 } 727 } 728 freenetconfigent(nc); 729 } 730 if (cl->cl_netid == NULL) 731 cl->cl_netid = ""; 732 if (cl->cl_tp == NULL) 733 cl->cl_tp = ""; 734 } 735 if (madefd) { 736 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL); 737 /* (void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, NULL); */ 738 }; 739 740 return (cl); 741 742 err: 743 rpc_createerr.cf_stat = RPC_TLIERROR; 744 rpc_createerr.cf_error.re_errno = errno; 745 rpc_createerr.cf_error.re_terrno = t_errno; 746 err1: if (madefd) 747 (void) t_close(fd); 748 return (NULL); 749 } 750 751 /* 752 * To avoid conflicts with the "magic" file descriptors (0, 1, and 2), 753 * we try to not use them. The __rpc_raise_fd() routine will dup 754 * a descriptor to a higher value. If we fail to do it, we continue 755 * to use the old one (and hope for the best). 756 */ 757 int 758 __rpc_raise_fd(int fd) 759 { 760 int nfd; 761 762 if ((nfd = fcntl(fd, F_DUPFD, RPC_MINFD)) == -1) 763 return (fd); 764 765 if (t_sync(nfd) == -1) { 766 (void) close(nfd); 767 return (fd); 768 } 769 770 if (t_close(fd) == -1) { 771 /* this is okay, we will syslog an error, then use the new fd */ 772 (void) syslog(LOG_ERR, 773 "could not t_close() fd %d; mem & fd leak", fd); 774 } 775 776 return (nfd); 777 } 778