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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 1989 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 /* from UCB 4.8 83/08/18 */ 29 30 #include <sys/types.h> 31 #include <sys/socket.h> 32 33 #include <netinet/in.h> 34 35 #include <stdio.h> 36 #include <netdb.h> 37 #include <errno.h> 38 39 extern errno; 40 char *index(); 41 char *getpass(), *getlogin(); 42 #ifndef S5EMUL 43 char *sprintf(); 44 #endif 45 46 int 47 rexec( 48 char **ahost, 49 unsigned short rport, 50 const char *name, 51 const char *pass, 52 const char *cmd, 53 int *fd2p) 54 { 55 int s, timo = 1, s3; 56 struct sockaddr_in sin, sin2, from; 57 char c; 58 u_short port; 59 struct hostent *hp; 60 61 hp = gethostbyname(*ahost); 62 if (hp == 0) { 63 fprintf(stderr, "%s: unknown host\n", *ahost); 64 return (-1); 65 } 66 *ahost = hp->h_name; 67 _ruserpass(hp->h_name, &name, &pass); 68 retry: 69 s = socket(AF_INET, SOCK_STREAM, 0); 70 if (s < 0) { 71 perror("rexec: socket"); 72 return (-1); 73 } 74 sin.sin_family = hp->h_addrtype; 75 sin.sin_port = rport; 76 bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length); 77 if (connect(s, &sin, sizeof(sin)) < 0) { 78 if (errno == ECONNREFUSED && timo <= 16) { 79 (void) close(s); 80 sleep(timo); 81 timo *= 2; 82 goto retry; 83 } 84 perror(hp->h_name); 85 (void) close(s); 86 return (-1); 87 } 88 if (fd2p == 0) { 89 (void) write(s, "", 1); 90 port = 0; 91 } else { 92 char num[8]; 93 int s2, sin2len; 94 95 s2 = socket(AF_INET, SOCK_STREAM, 0); 96 if (s2 < 0) { 97 (void) close(s); 98 return (-1); 99 } 100 listen(s2, 1); 101 sin2len = sizeof (sin2); 102 if (getsockname(s2, (char *)&sin2, &sin2len) < 0 || 103 sin2len != sizeof (sin2)) { 104 perror("getsockname"); 105 (void) close(s2); 106 goto bad; 107 } 108 port = ntohs((u_short)sin2.sin_port); 109 (void) sprintf(num, "%u", port); 110 (void) write(s, num, strlen(num)+1); 111 { int len = sizeof (from); 112 s3 = accept(s2, &from, &len); 113 close(s2); 114 if (s3 < 0) { 115 perror("accept"); 116 port = 0; 117 goto bad; 118 } 119 } 120 *fd2p = s3; 121 } 122 (void) write(s, name, strlen(name) + 1); 123 /* should public key encypt the password here */ 124 (void) write(s, pass, strlen(pass) + 1); 125 (void) write(s, cmd, strlen(cmd) + 1); 126 if (read(s, &c, 1) != 1) { 127 perror(*ahost); 128 goto bad; 129 } 130 if (c != 0) { 131 while (read(s, &c, 1) == 1) { 132 (void) write(2, &c, 1); 133 if (c == '\n') 134 break; 135 } 136 goto bad; 137 } 138 return (s); 139 bad: 140 if (port) 141 (void) close(*fd2p); 142 (void) close(s); 143 return (-1); 144 } 145