17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5b760ebbfSsp149894 * Common Development and Distribution License (the "License"). 6b760ebbfSsp149894 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21*7257d1b4Sraf 227c478bd9Sstevel@tonic-gate /* 23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 327c478bd9Sstevel@tonic-gate * The Regents of the University of California 337c478bd9Sstevel@tonic-gate * All Rights Reserved 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 377c478bd9Sstevel@tonic-gate * contributors. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <sys/types.h> 437c478bd9Sstevel@tonic-gate #include <sys/socket.h> 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate #include <netinet/in.h> 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate #include <stdio.h> 487c478bd9Sstevel@tonic-gate #include <netdb.h> 497c478bd9Sstevel@tonic-gate #include <errno.h> 507c478bd9Sstevel@tonic-gate #include <unistd.h> 517c478bd9Sstevel@tonic-gate #include <string.h> 527c478bd9Sstevel@tonic-gate #include <stdlib.h> 537c478bd9Sstevel@tonic-gate #include <libintl.h> 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate #ifdef SYSV 567c478bd9Sstevel@tonic-gate #define bcopy(a, b, c) (void) memcpy((b), (a), (c)) 577c478bd9Sstevel@tonic-gate #endif 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate #define MAX_SHORTSTRLEN 6 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate void _ruserpass(const char *host, char **aname, char **apass); 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate int rexec(char **ahost, unsigned short rport, const char *name, 647c478bd9Sstevel@tonic-gate const char *pass, const char *cmd, int *fd2p) 657c478bd9Sstevel@tonic-gate { 667c478bd9Sstevel@tonic-gate return (rexec_af(ahost, rport, name, pass, cmd, fd2p, AF_INET)); 677c478bd9Sstevel@tonic-gate } 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate int rexec_af(char **ahost, unsigned short rport, const char *name, 707c478bd9Sstevel@tonic-gate const char *pass, const char *cmd, int *fd2p, int af) 717c478bd9Sstevel@tonic-gate { 727c478bd9Sstevel@tonic-gate int s, timo = 1, s3; 737c478bd9Sstevel@tonic-gate char c; 747c478bd9Sstevel@tonic-gate ushort_t port; 757c478bd9Sstevel@tonic-gate static char hostname[MAXHOSTNAMELEN]; 767c478bd9Sstevel@tonic-gate int rc; 777c478bd9Sstevel@tonic-gate struct addrinfo *res; 787c478bd9Sstevel@tonic-gate struct addrinfo hints; 797c478bd9Sstevel@tonic-gate char aport[MAX_SHORTSTRLEN]; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate if (!(af == AF_INET || af == AF_INET6 || af == AF_UNSPEC)) { 827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 83*7257d1b4Sraf dgettext(TEXT_DOMAIN, "%d: Address family not " 847c478bd9Sstevel@tonic-gate "supported\n"), af); 857c478bd9Sstevel@tonic-gate errno = EAFNOSUPPORT; 867c478bd9Sstevel@tonic-gate return (-1); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate memset(&hints, 0, sizeof (hints)); 897c478bd9Sstevel@tonic-gate (void) snprintf(aport, MAX_SHORTSTRLEN, "%u", ntohs(rport)); 907c478bd9Sstevel@tonic-gate hints.ai_flags = AI_CANONNAME|AI_ADDRCONFIG|AI_V4MAPPED; 917c478bd9Sstevel@tonic-gate hints.ai_socktype = SOCK_STREAM; 927c478bd9Sstevel@tonic-gate hints.ai_family = af; 937c478bd9Sstevel@tonic-gate rc = getaddrinfo(*ahost, aport, &hints, &res); 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate if (rc != 0) { 967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 97*7257d1b4Sraf dgettext(TEXT_DOMAIN, "%s: unknown host\n"), 987c478bd9Sstevel@tonic-gate *ahost); 997c478bd9Sstevel@tonic-gate return (-1); 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate (void) strlcpy(hostname, res->ai_canonname, MAXHOSTNAMELEN); 1027c478bd9Sstevel@tonic-gate *ahost = hostname; 1037c478bd9Sstevel@tonic-gate _ruserpass(res->ai_canonname, (char **)&name, (char **)&pass); 1047c478bd9Sstevel@tonic-gate retry: 1057c478bd9Sstevel@tonic-gate s = socket(res->ai_addr->sa_family, res->ai_socktype, res->ai_protocol); 1067c478bd9Sstevel@tonic-gate if (s < 0) { 1077c478bd9Sstevel@tonic-gate perror("rexec: socket"); 1087c478bd9Sstevel@tonic-gate freeaddrinfo(res); 1097c478bd9Sstevel@tonic-gate return (-1); 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate if (connect(s, res->ai_addr, res->ai_addrlen) != 0) { 1127c478bd9Sstevel@tonic-gate if (errno == ECONNREFUSED && timo <= 16) { 1137c478bd9Sstevel@tonic-gate (void) close(s); 1147c478bd9Sstevel@tonic-gate (void) sleep(timo); 1157c478bd9Sstevel@tonic-gate timo *= 2; 1167c478bd9Sstevel@tonic-gate goto retry; 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate perror(*ahost); 1197c478bd9Sstevel@tonic-gate (void) close(s); 1207c478bd9Sstevel@tonic-gate freeaddrinfo(res); 1217c478bd9Sstevel@tonic-gate return (-1); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate if (fd2p == 0) { 1247c478bd9Sstevel@tonic-gate (void) write(s, "", 1); 1257c478bd9Sstevel@tonic-gate port = 0; 1267c478bd9Sstevel@tonic-gate } else { 1277c478bd9Sstevel@tonic-gate int s2; 1287c478bd9Sstevel@tonic-gate socklen_t sin2len; 1297c478bd9Sstevel@tonic-gate struct sockaddr_storage sin2, from; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate s2 = socket(res->ai_family, SOCK_STREAM, 0); 1327c478bd9Sstevel@tonic-gate if (s2 < 0) { 1337c478bd9Sstevel@tonic-gate (void) close(s); 1347c478bd9Sstevel@tonic-gate freeaddrinfo(res); 1357c478bd9Sstevel@tonic-gate return (-1); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate (void) listen(s2, 1); 1387c478bd9Sstevel@tonic-gate sin2len = (socklen_t)sizeof (sin2); 1397c478bd9Sstevel@tonic-gate if (getsockname(s2, (struct sockaddr *)&sin2, &sin2len) < 0) { 1407c478bd9Sstevel@tonic-gate perror("getsockname"); 1417c478bd9Sstevel@tonic-gate (void) close(s2); 1427c478bd9Sstevel@tonic-gate goto bad; 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate if (res->ai_family == AF_INET6) { 145b760ebbfSsp149894 port = ntohs(((struct sockaddr_in6 *)&sin2)->sin6_port); 1467c478bd9Sstevel@tonic-gate } else { 147b760ebbfSsp149894 port = ntohs(((struct sockaddr_in *)&sin2)->sin_port); 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate (void) snprintf(aport, MAX_SHORTSTRLEN, "%u", port); 1507c478bd9Sstevel@tonic-gate (void) write(s, aport, strlen(aport)+1); 1517c478bd9Sstevel@tonic-gate { 1527c478bd9Sstevel@tonic-gate socklen_t len = (socklen_t)sizeof (from); 1537c478bd9Sstevel@tonic-gate s3 = accept(s2, (struct sockaddr *)&from, &len); 1547c478bd9Sstevel@tonic-gate (void) close(s2); 1557c478bd9Sstevel@tonic-gate if (s3 < 0) { 1567c478bd9Sstevel@tonic-gate perror("accept"); 1577c478bd9Sstevel@tonic-gate port = 0; 1587c478bd9Sstevel@tonic-gate goto bad; 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate *fd2p = s3; 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate (void) write(s, name, strlen(name) + 1); 1647c478bd9Sstevel@tonic-gate /* should public key encypt the password here */ 1657c478bd9Sstevel@tonic-gate (void) write(s, pass, strlen(pass) + 1); 1667c478bd9Sstevel@tonic-gate (void) write(s, cmd, strlen(cmd) + 1); 1677c478bd9Sstevel@tonic-gate if (read(s, &c, 1) != 1) { 1687c478bd9Sstevel@tonic-gate perror(*ahost); 1697c478bd9Sstevel@tonic-gate goto bad; 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate if (c != 0) { 1727c478bd9Sstevel@tonic-gate while (read(s, &c, 1) == 1) { 1737c478bd9Sstevel@tonic-gate (void) write(2, &c, 1); 1747c478bd9Sstevel@tonic-gate if (c == '\n') 1757c478bd9Sstevel@tonic-gate break; 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate goto bad; 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate freeaddrinfo(res); 1807c478bd9Sstevel@tonic-gate return (s); 1817c478bd9Sstevel@tonic-gate bad: 1827c478bd9Sstevel@tonic-gate if (port) 1837c478bd9Sstevel@tonic-gate (void) close(*fd2p); 1847c478bd9Sstevel@tonic-gate (void) close(s); 1857c478bd9Sstevel@tonic-gate freeaddrinfo(res); 1867c478bd9Sstevel@tonic-gate return (-1); 1877c478bd9Sstevel@tonic-gate } 188