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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/* FAt Remote SHell daemon by eeeee
* Public Domain */
#define _XOPEN_SOURCE 600
#include "config-common.h"
#include "config-farshd.h"
#include "common.h"
#include <signal.h>
#include <fcntl.h>
struct {
struct sockaddr_in6 a;
struct winsize ws;
u32 flags, notconnected;
s32 ptm, pid;
u32 lptime; /* time of last packet */
/* stream state */
u32 rcvd; /* recieved data bytes from client */
u32 rcvd_c; /* client version */
u32 rb_r; /* readed from ring buffer (recieved data by client) */
u32 rb_r_c; /* client version */
/* ring buffer */
u32 rb_w; /* written to ring buffer */
u32 rb_wm; /* written masked (mod RB_SZ) */
u32 rb_rm; /* readed masked */
u32 rb_free, rb_free_for_read;
u8 rb[RB_SZ];
} peer[MAX_PEERS];
u32 ccnt;
static void disconnect(s32 skt, s32 n){
u8 pkt[4 + NONCE_SZ + HMAC_SZ];
*(u32*)pkt = 0;
send_pkt(skt, &pkt, 4, (struct sockaddr_storage*) &peer[n].a);
kill(peer[n].pid, SIGKILL);
close(peer[n].ptm);
if (n != ccnt-1) memcpy(&peer[n], &peer[ccnt-1], sizeof(peer[0]));
ccnt--;
memset(&peer[ccnt], 0, sizeof(peer[0]));
return;
}
int main(int argc, char *argv[]){
u8 term[CONNECT];
u8 *home;
u16 *pkt;
u16 pos;
u16 port;
s32 skt, res, fdmax, pts, i;
s32 pkt_int = PKT_INT;
u32 arcv;
u32 rb_mask = RB_SZ - 1;
struct sockaddr_in6 c;
struct sigaction sa;
struct timeval slp;
struct timespec t1, t2;
struct termios attr;
fd_set rset;
/* check shell */
res = access(SHELL, F_OK); ERRDIE(res, SHELL);
res = access(SHELL, X_OK); ERRDIE(res, SHELL);
/* arguments */
if (argc != 2 && argc != 3){
fprintf(stderr, "Using:\n\t%s [address] <port>\n\n"
"Example:\n\t%s 2222\n\t%s 127.0.0.1 2222\n\n",
argv[0], argv[0], argv[0]);
exit(1);
}
memset(&c, 0, sizeof(c));
port = (u16)atoi(argv[argc-1]);
if (argc == 3){
res = inet_pton(AF_INET6, argv[1], c.sin6_addr.s6_addr);
if (res != 1){
res = inet_pton(AF_INET, argv[1], &arcv);
if (res != 1){
fprintf(stderr, "wrong addr: %s\n",argv[1]);
exit(1);
}
c.sin6_addr.s6_addr[10] = 0xFF;
c.sin6_addr.s6_addr[11] = 0xFF;
memcpy(&c.sin6_addr.s6_addr[12], &arcv, 4);
}
}
/* get term attrs */
res = tcgetattr(STDOUT_FILENO, &attr); ERRDIE(res, "tcgetattr");
/* echo off */
attr.c_lflag &= ~ECHO;
res = tcsetattr(STDOUT_FILENO, TCSANOW, &attr);
ERRDIE(res, "tcsetattr");
/* get password */
fputs("Password: ", stdout);
home = fgets(password, PASSWORD_BUF_SZ, stdin);
fputs("\n", stdout);
if (home == NULL)
fputs("can't read password\n", stderr), exit(1);
/* echo on */
attr.c_lflag |= ECHO;
res = tcsetattr(STDOUT_FILENO, TCSANOW, &attr);
ERRDIE(res, "tcsetattr");
/* password without '\r\n' */
res = 0;
while (password[res] != '\0'
&& password[res] != '\r'
&& password[res] != '\n') res++;
/* hash password */
sponge256_hash32(key, password, res);
memset(password, 0, PASSWORD_BUF_SZ);
/* feed prng with key and current time */
duplex257_prng_feed(prng_state, ((u32*)key)[0]);
duplex257_prng_feed(prng_state, ((u32*)key)[1]);
duplex257_prng_feed(prng_state, ((u32*)key)[2]);
duplex257_prng_feed(prng_state, ((u32*)key)[3]);
duplex257_prng_feed(prng_state, ((u32*)key)[4]);
duplex257_prng_feed(prng_state, ((u32*)key)[5]);
duplex257_prng_feed(prng_state, ((u32*)key)[6]);
duplex257_prng_feed(prng_state, ((u32*)key)[7]);
clock_gettime(CLOCK_MONOTONIC, &t1);
duplex257_prng_feed(prng_state, (u32)t1.tv_sec);
duplex257_prng_feed(prng_state, (u32)t1.tv_nsec);
/* daemonize: redirect std streams */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
res = open("/dev/null", O_RDONLY); ERRDIE(res, "open");
res = open(LOGFILE, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
ERRDIE(res, "open");
res = open(LOGFILE, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
ERRDIE(res, "open");
/* daemonize: fork 1 */
res = fork(); ERRDIE(res, "fork 1");
if (res != 0) exit(0);
/* daemonize: become session leader */
setsid();
/* daemonize: fork 2 */
res = fork(); ERRDIE(res, "fork 2");
if (res != 0) exit(0); /* kill session leader */
/* daemonized! */
/* chdir to HOME */
home = getenv("HOME");
if (home != NULL) chdir(home);
/* kill zombie */
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_NOCLDWAIT;
sigaction(SIGCHLD, &sa, NULL);
/* init some vars */
memset(peer, 0, sizeof(peer));
ccnt = 0;
/* cur pkt buf */
pkt = malloc(MAX_PKT_SZ);
if (pkt == NULL) fputs("pkt = malloc() failed\n",stderr), exit(1);
/* socket, setsockopt */
skt = socket(PF_INET6, SOCK_DGRAM, 0); ERRDIE(skt, "socket");
res = 1;
res = setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, &res, sizeof(res));
ERRDIE(res, "SO_REUSEADDR");
/* bind */
c.sin6_family = AF_INET6;
c.sin6_port = htons(port);
res = bind(skt, (struct sockaddr*) &c, sizeof(c));
ERRDIE(res, "bind");
inf_loop:
/* select */
fdmax = skt + 1;
FD_ZERO(&rset);
FD_SET(skt, &rset);
for (i=0; i < ccnt; i++){
if (peer[i].rb_free == 0) continue;
FD_SET(peer[i].ptm, &rset);
if (fdmax <= peer[i].ptm) fdmax = peer[i].ptm + 1;
}
slp.tv_sec = 0;
slp.tv_usec = pkt_int;
/* the contents of the slp structure after calling select() is
* undefined (not portable), so I manually count the sleep time */
clock_gettime(CLOCK_MONOTONIC, &t1);
select(fdmax, &rset, NULL, NULL, &slp);
/* recv packet from client */
if (FD_ISSET(skt, &rset)){
res = recv_pkt(skt, pkt, MAX_PKT_SZ,
(struct sockaddr_storage*) &c);
pkt[0] = ntohs(pkt[0]);
pkt[1] = ntohs(pkt[1]);
if (res < 4 || res != 4 + pkt[0] + pkt[1]) goto ignore_p;
for (i=0; i < ccnt; i++){
res = memcmp(peer[i].a.sin6_addr.s6_addr,
c.sin6_addr.s6_addr, 16);
if (res == 0 && c.sin6_port == peer[i].a.sin6_port)
break;
}
if (i == ccnt){
/* client not connected */
if (ccnt == MAX_PEERS || (pkt[0] & CONNECT) == 0)
goto ignore_p;
/* add peer to array */
memcpy(&peer[i].a, &c, sizeof(c));
/* peer is not fully connected yet */
peer[i].notconnected = NOT_CONNECTED;
/* init ring buffer */
peer[i].rb_free = peer[i].rb_free_for_read = RB_SZ;
/* pseudoterminal */
peer[i].ptm = posix_openpt(O_RDWR);
ERRDIE(peer[i].ptm, "posix_openpt");
res = grantpt (peer[i].ptm); ERRDIE(res,"grantpt");
res = unlockpt(peer[i].ptm); ERRDIE(res,"unlockpt");
pts = open(ptsname(peer[i].ptm), O_RDWR);
ERRDIE(pts, "pts = open()");
/* read TERM from packet */
memset(term, '\0', CONNECT);
memcpy(term, &pkt[2], CONNECT-1);
/* fork */
peer[i].pid = fork(); ERRDIE(peer[i].pid, "fork");
if (peer[i].pid == 0){ /* child */
close(peer[i].ptm);
close(0), close(1), close(2);
dup(pts), dup(pts), dup(pts);
close(pts);
setsid();
res = ioctl(0, TIOCSCTTY, 1);
ERRDIE(res, "TIOCSCTTY");
setenv("TERM", term, 1);
res = execlp(SHELL, SHELL, NULL);
ERRDIE(res, "execlp");
}
close(pts);
ccnt++;
}
/* client connected */
clock_gettime(CLOCK_MONOTONIC, &t2);
duplex257_prng_feed(prng_state, t2.tv_nsec);
peer[i].lptime = t2.tv_sec;
pos = 2;
if (pkt[0] & CONNECT){
if (!peer[i].notconnected){
/* fully connected client sent packet with
* CONNECT flag, disconnect him */
disconnect(skt, i);
goto ignore_p;
}
peer[i].notconnected = NOT_CONNECTED;
pos += CONNECT >> 1;
} else if (peer[i].notconnected) peer[i].notconnected--;
/* copy flags for responce */
peer[i].flags |= pkt[0];
/* ping, term sizes */
if (pkt[0] & PING){
peer[i].ws.ws_row = ntohs(pkt[pos++]);
peer[i].ws.ws_col = ntohs(pkt[pos++]);
ioctl(peer[i].ptm, TIOCSWINSZ, &peer[i].ws);
}
/* stream state */
if (pkt[0] & SSTATE){
peer[i].rb_r_c = ntohl(*(u32*)&pkt[pos]), pos += 2;
peer[i].rcvd_c = ntohl(*(u32*)&pkt[pos]), pos += 2;
/* rb_w can overflow, so I can't ignore a packet if
* rb_w < rb_r_c. That's why I do these checks */
if (RB_SZ < peer[i].rb_w - peer[i].rb_r_c ||
RB_SZ < peer[i].rb_r_c - peer[i].rb_r)
goto ignore_p;
/* rb_r_c is ok, update state of ring buffer */
peer[i].rb_r = peer[i].rb_r_c;
peer[i].rb_free = RB_SZ -
(peer[i].rb_w - peer[i].rb_r);
peer[i].rb_rm = peer[i].rb_r & rb_mask;
peer[i].rb_free_for_read = peer[i].rb_free;
if (peer[i].rb_free > peer[i].rb_rm)
peer[i].rb_free_for_read -= peer[i].rb_rm;
if (pkt[1]){ /* have data in packet */
/* arcv: already recieved amount of data*/
arcv = peer[i].rcvd - peer[i].rcvd_c;
if (arcv < pkt[1]){
/* packet also have data that is not
* recieved yet */
pos = (pos<<1) + arcv;
pkt[1] -= arcv;
write(peer[i].ptm, (u8*)pkt+pos,
pkt[1]);
peer[i].rcvd += pkt[1];
}
}
}
}
ignore_p:
/* read ptms to ring buffers */
for (i=0; i < ccnt; i++){
if (0 == FD_ISSET(peer[i].ptm, &rset)) continue;
res = read(peer[i].ptm, peer[i].rb + peer[i].rb_wm,
peer[i].rb_free_for_read);
if (res < 1){/* disconnect */
disconnect(skt, i);
if (i != ccnt-1) i--;
continue;
}
peer[i].rb_w += res;
peer[i].rb_wm = peer[i].rb_w & rb_mask;
peer[i].rb_free -= res;
peer[i].rb_free_for_read = peer[i].rb_free;
if (peer[i].rb_free > peer[i].rb_rm)
peer[i].rb_free_for_read -= peer[i].rb_rm;
}
/* send packets to clients every PKT_INT (+- MIN_SLEEP) microsecs */
clock_gettime(CLOCK_MONOTONIC, &t2);
pkt_int -= abs((s32)t2.tv_nsec - (s32)t1.tv_nsec) / 1000;
if (pkt_int < MIN_SLEEP)
for (pkt_int=PKT_INT, i=0; i < ccnt; i++){
/* make packet for peer i */
pos = 2;
pkt[0] = 0;
/* disconnect on timeout */
if (t2.tv_sec - peer[i].lptime >= TIMEOUT_D){
disconnect(skt, i);
continue;
}
/* set PING bit after getting packet with PING from peer */
if (peer[i].flags & PING){
pkt[0] |= PING;
pkt[pos++] = htons(peer[i].ws.ws_row);
pkt[pos++] = htons(peer[i].ws.ws_col);
}
/* add streams state and data if data exist */
pkt[1] = peer[i].rb_w - peer[i].rb_r;
if (pkt[1]) {
/* add state of streams */
pkt[0] |= SSTATE;
*(u32*)&pkt[pos] = htonl(peer[i].rb_r), pos += 2;
*(u32*)&pkt[pos] = htonl(peer[i].rcvd), pos += 2;
/* add data */
pos <<= 1; /* now it pos in array of u8 */
res = MAX_PKT_SZ - HMAC_SZ - NONCE_SZ - pos;
if (res < pkt[1]) pkt[1] = res;
memcpy((u8*)pkt + pos, peer[i].rb + peer[i].rb_rm,
pkt[1]);
pos += pkt[1];
} else { /* no data to send */
/* send streams state if it differ from peer state*/
if (peer[i].rb_r != peer[i].rb_r_c ||
peer[i].rcvd != peer[i].rcvd_c){
pkt[0] |= SSTATE;
*(u32*)&pkt[pos] = htonl(peer[i].rb_r);
pos += 2;
*(u32*)&pkt[pos] = htonl(peer[i].rcvd);
pos += 2;
/* send state only once */
peer[i].rb_r_c = peer[i].rb_r;
peer[i].rcvd_c = peer[i].rcvd;
}
pos <<= 1;
}
peer[i].flags = 0;
if (pkt[0]){
/* add CONNECT bit if peer is not fully connected */
if (peer[i].notconnected) pkt[0] |= CONNECT;
pkt[0] = htons(pkt[0]);
pkt[1] = htons(pkt[1]);
send_pkt(skt, pkt, pos,
(struct sockaddr_storage*) &peer[i].a);
}
}
goto inf_loop;
return 0;
}
|