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 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 /* 31 * University Copyright- Copyright (c) 1982, 1986, 1988 32 * The Regents of the University of California 33 * All Rights Reserved 34 * 35 * University Acknowledgment- Portions of this document are derived from 36 * software developed by the University of California, Berkeley, and its 37 * contributors. 38 */ 39 40 #include <sys/types.h> 41 #include <sys/socket.h> 42 43 #include <netinet/in.h> 44 45 #include <stdio.h> 46 #include <netdb.h> 47 #include <errno.h> 48 #include <unistd.h> 49 #include <string.h> 50 #include <stdlib.h> 51 #include <libintl.h> 52 53 #ifdef SYSV 54 #define bcopy(a, b, c) (void) memcpy((b), (a), (c)) 55 #endif 56 57 #define MAX_SHORTSTRLEN 6 58 59 void _ruserpass(const char *host, char **aname, char **apass); 60 61 int rexec(char **ahost, unsigned short rport, const char *name, 62 const char *pass, const char *cmd, int *fd2p) 63 { 64 return (rexec_af(ahost, rport, name, pass, cmd, fd2p, AF_INET)); 65 } 66 67 int rexec_af(char **ahost, unsigned short rport, const char *name, 68 const char *pass, const char *cmd, int *fd2p, int af) 69 { 70 int s, timo = 1, s3; 71 char c; 72 ushort_t port; 73 static char hostname[MAXHOSTNAMELEN]; 74 int rc; 75 struct addrinfo *res; 76 struct addrinfo hints; 77 char aport[MAX_SHORTSTRLEN]; 78 79 if (!(af == AF_INET || af == AF_INET6 || af == AF_UNSPEC)) { 80 (void) fprintf(stderr, 81 dgettext(TEXT_DOMAIN, "%d: Address family not " 82 "supported\n"), af); 83 errno = EAFNOSUPPORT; 84 return (-1); 85 } 86 memset(&hints, 0, sizeof (hints)); 87 (void) snprintf(aport, MAX_SHORTSTRLEN, "%u", ntohs(rport)); 88 hints.ai_flags = AI_CANONNAME|AI_ADDRCONFIG|AI_V4MAPPED; 89 hints.ai_socktype = SOCK_STREAM; 90 hints.ai_family = af; 91 rc = getaddrinfo(*ahost, aport, &hints, &res); 92 93 if (rc != 0) { 94 (void) fprintf(stderr, 95 dgettext(TEXT_DOMAIN, "%s: unknown host\n"), 96 *ahost); 97 return (-1); 98 } 99 (void) strlcpy(hostname, res->ai_canonname, MAXHOSTNAMELEN); 100 *ahost = hostname; 101 _ruserpass(res->ai_canonname, (char **)&name, (char **)&pass); 102 retry: 103 s = socket(res->ai_addr->sa_family, res->ai_socktype, res->ai_protocol); 104 if (s < 0) { 105 perror("rexec: socket"); 106 freeaddrinfo(res); 107 return (-1); 108 } 109 if (connect(s, res->ai_addr, res->ai_addrlen) != 0) { 110 if (errno == ECONNREFUSED && timo <= 16) { 111 (void) close(s); 112 (void) sleep(timo); 113 timo *= 2; 114 goto retry; 115 } 116 perror(*ahost); 117 (void) close(s); 118 freeaddrinfo(res); 119 return (-1); 120 } 121 if (fd2p == 0) { 122 (void) write(s, "", 1); 123 port = 0; 124 } else { 125 int s2; 126 socklen_t sin2len; 127 struct sockaddr_storage sin2, from; 128 129 s2 = socket(res->ai_family, SOCK_STREAM, 0); 130 if (s2 < 0) { 131 (void) close(s); 132 freeaddrinfo(res); 133 return (-1); 134 } 135 (void) listen(s2, 1); 136 sin2len = (socklen_t)sizeof (sin2); 137 if (getsockname(s2, (struct sockaddr *)&sin2, &sin2len) < 0) { 138 perror("getsockname"); 139 (void) close(s2); 140 goto bad; 141 } 142 if (res->ai_family == AF_INET6) { 143 port = ntohs(((struct sockaddr_in6 *)&sin2)->sin6_port); 144 } else { 145 port = ntohs(((struct sockaddr_in *)&sin2)->sin_port); 146 } 147 (void) snprintf(aport, MAX_SHORTSTRLEN, "%u", port); 148 (void) write(s, aport, strlen(aport)+1); 149 { 150 socklen_t len = (socklen_t)sizeof (from); 151 s3 = accept(s2, (struct sockaddr *)&from, &len); 152 (void) close(s2); 153 if (s3 < 0) { 154 perror("accept"); 155 port = 0; 156 goto bad; 157 } 158 } 159 *fd2p = s3; 160 } 161 (void) write(s, name, strlen(name) + 1); 162 /* should public key encypt the password here */ 163 (void) write(s, pass, strlen(pass) + 1); 164 (void) write(s, cmd, strlen(cmd) + 1); 165 if (read(s, &c, 1) != 1) { 166 perror(*ahost); 167 goto bad; 168 } 169 if (c != 0) { 170 while (read(s, &c, 1) == 1) { 171 (void) write(2, &c, 1); 172 if (c == '\n') 173 break; 174 } 175 goto bad; 176 } 177 freeaddrinfo(res); 178 return (s); 179 bad: 180 if (port) 181 (void) close(*fd2p); 182 (void) close(s); 183 freeaddrinfo(res); 184 return (-1); 185 } 186