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