-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsh.c
More file actions
153 lines (139 loc) · 3.33 KB
/
rsh.c
File metadata and controls
153 lines (139 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Allow shell commands to be remotely executed via a TCP socket.
* Usage: ./rsh (server|client host port 'shell command')
* Note that this program is quite insecure!
*/
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <netdb.h>
#include <sys/sendfile.h>
#define CMD_SZ 100
static void client(char *host, char *port, char *cmd) {
int sock;
size_t cmd_sz, nread;
struct sockaddr_storage addr;
struct addrinfo *res, *curs, hints;
char buf[100];
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
memset(&hints, 0, sizeof (hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
if (getaddrinfo(host, port, &hints, &res) != 0) {
perror("getaddrinfo");
exit(EXIT_FAILURE);
}
for (curs = res; curs != NULL; curs = curs->ai_next) {
if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
perror("connect");
continue;
} else {
break;
}
}
if (curs == NULL) {
perror("Couldn't connect");
exit(EXIT_FAILURE);
}
freeaddrinfo(res);
cmd_sz = strlen(cmd);
if (write(sock, cmd, cmd_sz) != cmd_sz) {
perror("write");
exit(EXIT_FAILURE);
}
shutdown(sock, SHUT_WR);
while ((nread = read(sock, buf, 100)) > 0) {
write(STDOUT_FILENO, buf, nread);
}
}
static void execmd(int fd, int sock, char *cmd) {
char *envp[1];
char *argv[4];
printf("exec command %s\n", cmd);
switch (fork()) {
case -1:
perror("fork");
break;
case 0:
close(sock);
close(STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
envp[0] = NULL;
argv[3] = NULL;
argv[0] = "sh";
argv[1] = "-c";
argv[2] = cmd;
execve("/bin/sh", argv, envp);
perror("execve");
exit(EXIT_FAILURE);
default:
break;
}
close(fd);
}
static void serv() {
int sock, client;
struct sockaddr_storage addr;
socklen_t addrlen;
char host[100], serv[100], command[CMD_SZ], c;
// Rely on an ephemeral port automatically being selected to avoid
// some socket setup code.
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
if (listen(sock, 10) == -1) {
perror("listen");
exit(EXIT_FAILURE);
}
addrlen = sizeof (addr);
if (getsockname(sock, (struct sockaddr*)&addr, &addrlen) == -1) {
perror("getsockname");
exit(EXIT_FAILURE);
}
if (getnameinfo((struct sockaddr*)&addr, addrlen, host, 100, serv,
100, NI_NUMERICHOST | NI_NUMERICSERV) == -1) {
perror("getnameinfo");
exit(EXIT_FAILURE);
}
printf("Server starting on %s:%s\n", host, serv);
openlog("rsh", LOG_PERROR, LOG_DAEMON);
while ((client = accept(sock, NULL, NULL)) != -1) {
puts("Accepting client");
memset(command, 0, CMD_SZ);
if (read(client, command, CMD_SZ - 1) == -1) {
close(client);
continue;
}
if (read(client, &c, 1) != 0) {
// Command too long.
close(client);
continue;
}
execmd(client, sock, command);
}
close(sock);
closelog();
}
int main(int argc, char **argv) {
if (argc > 1 && strcmp("server", argv[1]) == 0) {
serv();
} else if (argc > 4 && strcmp("client", argv[1]) == 0) {
client(argv[2], argv[3], argv[4]);
} else {
puts("Usage: ./rsh (server|client <host> <port> 'shell command')");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}