7 #include <sys/socket.h>
8 #include <netinet/in.h>
11 int rcmd(char **remote_hostname, int remote_port,
12 char *local_user, char *remote_user,
13 char *command, int zero)
15 struct hostent *remote_hp;
16 struct hostent *local_hp;
17 struct sockaddr_in remote_isa;
18 struct sockaddr_in local_isa;
19 char local_hostname[80];
25 remote_hp = gethostbyname(*remote_hostname);
28 perror("couldn't get remote host address");
32 /* Copy remote IP address into socket address structure */
33 remote_isa.sin_family = AF_INET;
34 remote_isa.sin_port = htons(remote_port);
35 memcpy(&remote_isa.sin_addr, remote_hp->h_addr, sizeof(remote_isa.sin_addr));
37 gethostname(local_hostname, 80);
38 local_hp = gethostbyname(local_hostname);
41 perror("couldn't get local host address");
45 /* Copy local IP address into socket address structure */
46 local_isa.sin_family = AF_INET;
47 memcpy(&local_isa.sin_addr, local_hp->h_addr, sizeof(local_isa.sin_addr));
49 /* Create the local socket */
50 s = socket(AF_INET, SOCK_STREAM, 0);
53 perror("socket failed\n");
57 /* Bind local socket with a port from IPPORT_RESERVED/2 to IPPORT_RESERVED - 1
58 this requires the OPER privilege under VMS -- to allow communication with
59 a stock rshd under UNIX */
61 for(local_port = IPPORT_RESERVED - 1; local_port >= IPPORT_RESERVED/2; local_port--)
63 local_isa.sin_port = htons(local_port);
64 rs = bind(s, (struct sockaddr *)&local_isa, sizeof(local_isa));
69 /* Bind local socket to an unprivileged port. A normal rshd will drop the
70 connection; you must be running a patched rshd invoked through inetd for
71 this connection method to work */
74 for(local_port = IPPORT_USERRESERVED - 1;
75 local_port > IPPORT_RESERVED;
78 local_isa.sin_port = htons(local_port);
79 rs = bind(s, (struct sockaddr *)&local_isa, sizeof(local_isa));
84 rs = connect(s, (struct sockaddr *) &remote_isa, sizeof(remote_isa));
87 fprintf(stderr, "connect: errno = %d\n", errno);
92 /* Now supply authentication information */
94 /* Auxiliary port number for error messages, we don't use it */
98 write(s, local_user, strlen(local_user) + 1);
100 /* Who do we want to be */
101 write(s, remote_user, strlen(remote_user) + 1);
103 /* What do we want to run */
104 write(s, command, strlen(command) + 1);
106 /* NUL is sent back to us if information is acceptable */