|
|
1.1 root 1: /*
2: * Copyright (c) 1980 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that: (1) source distributions retain this entire copyright
7: * notice and comment, and (2) distributions including binaries display
8: * the following acknowledgement: ``This product includes software
9: * developed by the University of California, Berkeley and its contributors''
10: * in the documentation or other materials provided with the distribution
11: * and in all advertising materials mentioning features or use of this
12: * software. Neither the name of the University nor the names of its
13: * contributors may be used to endorse or promote products derived
14: * from this software without specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #if defined(LIBC_SCCS) && !defined(lint)
21: static char sccsid[] = "@(#)rexec.c 5.8 (Berkeley) 6/1/90";
22: #endif /* LIBC_SCCS and not lint */
23:
24: #include <sys/types.h>
25: #include <sys/socket.h>
26:
27: #include <netinet/in.h>
28:
29: #include <stdio.h>
30: #include <netdb.h>
31: #include <errno.h>
32:
33: extern errno;
34: char *index();
35: int rexecoptions;
36: char *getpass(), *getlogin();
37:
38: rexec(ahost, rport, name, pass, cmd, fd2p)
39: char **ahost;
40: int rport;
41: char *name, *pass, *cmd;
42: int *fd2p;
43: {
44: struct sockaddr_in sin, sin2, from;
45: struct hostent *hp;
46: u_short port;
47: int s, timo = 1, s3;
48: char c;
49:
50: hp = gethostbyname(*ahost);
51: if (hp == 0) {
52: herror(*ahost);
53: return (-1);
54: }
55: *ahost = hp->h_name;
56: ruserpass(hp->h_name, &name, &pass);
57: retry:
58: s = socket(AF_INET, SOCK_STREAM, 0);
59: if (s < 0) {
60: perror("rexec: socket");
61: return (-1);
62: }
63: sin.sin_family = hp->h_addrtype;
64: sin.sin_port = rport;
65: bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
66: if (connect(s, &sin, sizeof(sin)) < 0) {
67: if (errno == ECONNREFUSED && timo <= 16) {
68: (void) close(s);
69: sleep(timo);
70: timo *= 2;
71: goto retry;
72: }
73: perror(hp->h_name);
74: return (-1);
75: }
76: if (fd2p == 0) {
77: (void) write(s, "", 1);
78: port = 0;
79: } else {
80: char num[8];
81: int s2, sin2len;
82:
83: s2 = socket(AF_INET, SOCK_STREAM, 0);
84: if (s2 < 0) {
85: (void) close(s);
86: return (-1);
87: }
88: listen(s2, 1);
89: sin2len = sizeof (sin2);
90: if (getsockname(s2, (char *)&sin2, &sin2len) < 0 ||
91: sin2len != sizeof (sin2)) {
92: perror("getsockname");
93: (void) close(s2);
94: goto bad;
95: }
96: port = ntohs((u_short)sin2.sin_port);
97: (void) sprintf(num, "%u", port);
98: (void) write(s, num, strlen(num)+1);
99: { int len = sizeof (from);
100: s3 = accept(s2, &from, &len, 0);
101: close(s2);
102: if (s3 < 0) {
103: perror("accept");
104: port = 0;
105: goto bad;
106: }
107: }
108: *fd2p = s3;
109: }
110: (void) write(s, name, strlen(name) + 1);
111: /* should public key encypt the password here */
112: (void) write(s, pass, strlen(pass) + 1);
113: (void) write(s, cmd, strlen(cmd) + 1);
114: if (read(s, &c, 1) != 1) {
115: perror(*ahost);
116: goto bad;
117: }
118: if (c != 0) {
119: while (read(s, &c, 1) == 1) {
120: (void) write(2, &c, 1);
121: if (c == '\n')
122: break;
123: }
124: goto bad;
125: }
126: return (s);
127: bad:
128: if (port)
129: (void) close(*fd2p);
130: (void) close(s);
131: return (-1);
132: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.