1 #ifndef lint 2 static char sccsid[] = "@(#)rexec.c 4.5 82/12/17"; 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 if (connect(s, &sin) < 0) { 48 if (errno == ECONNREFUSED && timo <= 16) { 49 (void) close(s); 50 sleep(timo); 51 timo *= 2; 52 goto retry; 53 } 54 perror(hp->h_name); 55 return (-1); 56 } 57 if (fd2p == 0) { 58 (void) write(s, "", 1); 59 port = 0; 60 } else { 61 char num[8]; 62 int s2; 63 64 s2 = socket(0, SOCK_STREAM, 0, 0); 65 if (s2 < 0) { 66 (void) close(s); 67 return (-1); 68 } 69 listen(s2, 1); 70 socketaddr(s2, &sin2); 71 port = ntohs((u_short)sin2.sin_port); 72 (void) sprintf(num, "%d", port); 73 (void) write(s, num, strlen(num)+1); 74 { int len = sizeof (from); 75 s3 = accept(s2, &from, &len, 0); 76 close(s2); 77 if (s3 < 0) { 78 perror("accept"); 79 port = 0; 80 goto bad; 81 } 82 } 83 *fd2p = s3; 84 } 85 (void) write(s, name, strlen(name) + 1); 86 /* should public key encypt the password here */ 87 (void) write(s, pass, strlen(pass) + 1); 88 (void) write(s, cmd, strlen(cmd) + 1); 89 if (read(s, &c, 1) != 1) { 90 perror(*ahost); 91 goto bad; 92 } 93 if (c != 0) { 94 while (read(s, &c, 1) == 1) { 95 (void) write(2, &c, 1); 96 if (c == '\n') 97 break; 98 } 99 goto bad; 100 } 101 return (s); 102 bad: 103 if (port) 104 (void) close(*fd2p); 105 (void) close(s); 106 return (-1); 107 } 108