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.2.1 (2.11BSD) 1997/10/2"; 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: int rexecoptions; 21: 22: rexec(ahost, rport, name, pass, cmd, fd2p) 23: char **ahost; 24: int rport; 25: char *name, *pass, *cmd; 26: int *fd2p; 27: { 28: int s, timo = 1, s3; 29: struct sockaddr_in sin, sin2, from; 30: char c; 31: u_short port; 32: struct hostent *hp; 33: 34: hp = gethostbyname(*ahost); 35: if (hp == 0) { 36: fprintf(stderr, "%s: unknown host\n", *ahost); 37: return (-1); 38: } 39: *ahost = hp->h_name; 40: ruserpass(hp->h_name, &name, &pass); 41: retry: 42: s = socket(AF_INET, SOCK_STREAM, 0); 43: if (s < 0) { 44: perror("rexec: socket"); 45: return (-1); 46: } 47: sin.sin_family = hp->h_addrtype; 48: sin.sin_port = rport; 49: bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length); 50: if (connect(s, &sin, sizeof(sin)) < 0) { 51: if (errno == ECONNREFUSED && timo <= 16) { 52: (void) close(s); 53: sleep(timo); 54: timo *= 2; 55: goto retry; 56: } 57: perror(hp->h_name); 58: return (-1); 59: } 60: if (fd2p == 0) { 61: (void) write(s, "", 1); 62: port = 0; 63: } else { 64: char num[8]; 65: int s2, sin2len; 66: 67: s2 = socket(AF_INET, SOCK_STREAM, 0); 68: if (s2 < 0) { 69: (void) close(s); 70: return (-1); 71: } 72: listen(s2, 1); 73: sin2len = sizeof (sin2); 74: if (getsockname(s2, (char *)&sin2, &sin2len) < 0 || 75: sin2len != sizeof (sin2)) { 76: perror("getsockname"); 77: (void) close(s2); 78: goto bad; 79: } 80: port = ntohs((u_short)sin2.sin_port); 81: (void) sprintf(num, "%u", port); 82: (void) write(s, num, strlen(num)+1); 83: { int len = sizeof (from); 84: s3 = accept(s2, &from, &len, 0); 85: close(s2); 86: if (s3 < 0) { 87: perror("accept"); 88: port = 0; 89: goto bad; 90: } 91: } 92: *fd2p = s3; 93: } 94: (void) write(s, name, strlen(name) + 1); 95: /* should public key encypt the password here */ 96: (void) write(s, pass, strlen(pass) + 1); 97: (void) write(s, cmd, strlen(cmd) + 1); 98: if (read(s, &c, 1) != 1) { 99: perror(*ahost); 100: goto bad; 101: } 102: if (c != 0) { 103: while (read(s, &c, 1) == 1) { 104: (void) write(2, &c, 1); 105: if (c == '\n') 106: break; 107: } 108: goto bad; 109: } 110: return (s); 111: bad: 112: if (port) 113: (void) close(*fd2p); 114: (void) close(s); 115: return (-1); 116: }