1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #if defined(LIBC_SCCS) && !defined(lint) 8 static char sccsid[] = "@(#)rexec.c 5.3 (Berkeley) 10/22/87"; 9 #endif LIBC_SCCS and not lint 10 11 #include <sys/types.h> 12 #include <sys/socket.h> 13 14 #include <netinet/in.h> 15 16 #include <stdio.h> 17 #include <netdb.h> 18 #include <errno.h> 19 20 extern errno; 21 char *index(); 22 int rexecoptions; 23 char *getpass(), *getlogin(); 24 25 rexec(ahost, rport, name, pass, cmd, fd2p) 26 char **ahost; 27 int rport; 28 char *name, *pass, *cmd; 29 int *fd2p; 30 { 31 int s, timo = 1, s3; 32 struct sockaddr_in sin, sin2, from; 33 char c; 34 short port; 35 struct hostent *hp; 36 37 hp = gethostbyname(*ahost); 38 if (hp == 0) { 39 fprintf(stderr, "%s: unknown host\n", *ahost); 40 return (-1); 41 } 42 *ahost = hp->h_name; 43 ruserpass(hp->h_name, &name, &pass); 44 retry: 45 s = socket(AF_INET, SOCK_STREAM, 0); 46 if (s < 0) { 47 perror("rexec: socket"); 48 return (-1); 49 } 50 sin.sin_family = hp->h_addrtype; 51 sin.sin_port = rport; 52 bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length); 53 if (connect(s, &sin, sizeof(sin)) < 0) { 54 if (errno == ECONNREFUSED && timo <= 16) { 55 (void) close(s); 56 sleep(timo); 57 timo *= 2; 58 goto retry; 59 } 60 perror(hp->h_name); 61 return (-1); 62 } 63 if (fd2p == 0) { 64 (void) write(s, "", 1); 65 port = 0; 66 } else { 67 char num[8]; 68 int s2, sin2len; 69 70 s2 = socket(AF_INET, SOCK_STREAM, 0); 71 if (s2 < 0) { 72 (void) close(s); 73 return (-1); 74 } 75 listen(s2, 1); 76 sin2len = sizeof (sin2); 77 if (getsockname(s2, (char *)&sin2, &sin2len) < 0 || 78 sin2len != sizeof (sin2)) { 79 perror("getsockname"); 80 (void) close(s2); 81 goto bad; 82 } 83 port = ntohs((u_short)sin2.sin_port); 84 (void) sprintf(num, "%d", port); 85 (void) write(s, num, strlen(num)+1); 86 { int len = sizeof (from); 87 s3 = accept(s2, &from, &len, 0); 88 close(s2); 89 if (s3 < 0) { 90 perror("accept"); 91 port = 0; 92 goto bad; 93 } 94 } 95 *fd2p = s3; 96 } 97 (void) write(s, name, strlen(name) + 1); 98 /* should public key encypt the password here */ 99 (void) write(s, pass, strlen(pass) + 1); 100 (void) write(s, cmd, strlen(cmd) + 1); 101 if (read(s, &c, 1) != 1) { 102 perror(*ahost); 103 goto bad; 104 } 105 if (c != 0) { 106 while (read(s, &c, 1) == 1) { 107 (void) write(2, &c, 1); 108 if (c == '\n') 109 break; 110 } 111 goto bad; 112 } 113 return (s); 114 bad: 115 if (port) 116 (void) close(*fd2p); 117 (void) close(s); 118 return (-1); 119 } 120