-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathio_nossl.c
389 lines (295 loc) · 10.4 KB
/
io_nossl.c
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
#include "common.h"
/***********************************************************************************************************************
*
* remote_read_plaintext()
*
* Input: A pointer to the buffer we want to fill, and the count of characters we need to read.
* We will also use the the global io struct.
* Output: The count of characters succesfully read, or -1 on error.
*
* Purpose: Fill our buffer.
*
**********************************************************************************************************************/
int remote_read_plaintext(void *buff, size_t count){
int retval;
int io_bytes;
char *tmp_ptr;
fd_set fd_select;
int seen = 0;
io_bytes = 0;
tmp_ptr = buff;
while(count){
/* Skip the select() statement the first time through, as the common case won't need it. */
if(seen){
FD_ZERO(&fd_select);
FD_SET(io->remote_fd, &fd_select);
if(select(io->remote_fd + 1, &fd_select, NULL, NULL, NULL) == -1){
report_error("remote_read_plaintext(): select(%d, %lx, NULL, NULL, NULL): %s", \
io->remote_fd + 1, (unsigned long) &fd_select, strerror(errno));
return(-1);
}
}else{
seen = 1;
}
retval = read(io->remote_fd, tmp_ptr, count);
if(!retval){
io->eof = 1;
return(-1);
}else if(retval == -1){
if(!(errno == EINTR || errno == EAGAIN)){
report_error("remote_read_plaintext(): read(%d, %lx, %d): %s", \
io->remote_fd, (unsigned long) &tmp_ptr, (int) count, strerror(errno));
return(-1);
}
}else{
count -= retval;
io_bytes += retval;
tmp_ptr += retval;
}
}
return(io_bytes);
}
/***********************************************************************************************************************
*
* remote_write_plaintext()
*
* Input: A pointer to the buffer we want to empty, and the count of characters we should write.
* We will also use the the global io struct.
* Output: The count of characters succesfully written, or -1 on error.
*
* Purpose: Empty our buffer.
*
**********************************************************************************************************************/
int remote_write_plaintext(void *buff, size_t count){
int retval;
int io_bytes;
char *tmp_ptr;
fd_set fd_select;
int seen = 0;
io_bytes = 0;
tmp_ptr = buff;
while(count){
/* Skip the select() statement the first time through, as the common case won't need it. */
if(seen){
FD_ZERO(&fd_select);
FD_SET(io->remote_fd, &fd_select);
if(select(io->remote_fd + 1, NULL, &fd_select, NULL, NULL) == -1){
report_error("remote_write_plaintext(): select(%d, NULL, %lx, NULL, NULL): %s", \
io->remote_fd + 1, (unsigned long) &fd_select, strerror(errno));
return(-1);
}
}else{
seen = 1;
}
retval = write(io->remote_fd, tmp_ptr, count);
if(retval == -1){
if(!(errno == EINTR || errno == EAGAIN)){
report_error("remote_write_plaintext(): read(%d, %lx, %d): %s", \
io->remote_fd, (unsigned long) &tmp_ptr, (int) count, strerror(errno));
return(-1);
}
}else{
count -= retval;
io_bytes += retval;
tmp_ptr += retval;
}
}
return(io_bytes);
}
/* The decision below to use gethostbyname() instead of getaddrinfo() is a design decision. */
/* If the application is being built without SSL, then the target host is probably a much older host with */
/* equally old network libraries. The "no SSL" case would better be considered here as the "deprecated best */
/* effort, only around for extreame backward compatability" case. */
/***********************************************************************************************************************
*
* init_io_control()
*
* Input: None. We will use the global io and config structs.
* Output: An int showing success (by returning the remote_fd) or failure (by returning -1).
*
* Purpose: To initialize the control nodes network io layer.
*
**********************************************************************************************************************/
int init_io_control(){
int tmp_sock;
int yes = 1;
struct sockaddr_in name;
struct hostent *host;
char *ip_address;
char *ip_port;
int ip_address_len;
struct sigaction act;
socklen_t len;
struct sockaddr_storage addr;
char ipstr[INET6_ADDRSTRLEN];
int port;
struct sockaddr_in *s;
struct sockaddr_in6 *s6;
/* In the no ssl build, there is no difference between a control in bindshell mode, and a target. */
/* As such, we'll just pass through to the other rather than repeat code. */
if(!io->target && config->bindshell){
return(init_io_target(config));
}
/* Initialize the structures we will be using. */
/* Set up our socket. */
ip_address_len = strlen(config->ip_addr);
if((ip_address = calloc(ip_address_len + 1, sizeof(char))) == NULL){
report_error("init_io_control(): calloc(%d, %d): %s", ip_address_len, (int) sizeof(char), strerror(errno));
return(-1);
}
memcpy(ip_address, config->ip_addr, ip_address_len);
if((ip_port = strchr(ip_address, ':')) == NULL){
report_error("init_io_control(): strchr(%s, ':'): Port not found!", ip_address);
return(-1);
}
*ip_port = '\0';
ip_port++;
if((host = gethostbyname(ip_address)) == NULL){
report_error("init_io_control(): gethostbyname(%s): %s", ip_address, strerror(errno));
return(-1);
}
memset(&name, 0, sizeof(name));
name.sin_family = AF_INET;
name.sin_port = htons(strtol(ip_port, NULL, 10));
free(ip_address);
if((tmp_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1){
report_error("init_io_control(): socket(AF_INET, SOCK_STREAM, 0): %s", strerror(errno));
return(-1);
}
if(setsockopt(tmp_sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1){
report_error("init_io_control(): setsockopt(%d, SOL_SOCKET, SO_REUSEADDR, %lx, %d): %s", \
tmp_sock, (unsigned long) &yes, (int) sizeof(yes), strerror(errno));
return(-1);
}
/* Seppuku if left alone too long. */
memset(&act, 0, sizeof(act));
act.sa_handler = seppuku;
if(sigaction(SIGALRM, &act, NULL) == -1){
report_error("init_io_control(): sigaction(%d, %lx, %p): %s", SIGALRM, (unsigned long) &act, NULL, strerror(errno));
return(-1);
}
if(verbose){
printf("Listening on %s...", config->ip_addr);
fflush(stdout);
}
report_log("Controller: Listening on %s.", config->ip_addr);
if(bind(tmp_sock, (struct sockaddr *) &name, sizeof(name)) == -1){
report_error("init_io_control(): bind(%d, %lx, %d): %s", \
tmp_sock, (unsigned long) &name, (int) sizeof(name), strerror(errno));
return(-1);
}
if(listen(tmp_sock, 1) == -1){
report_error("init_io_control(): listen(%d, 1): %s", tmp_sock, strerror(errno));
return(-1);
}
if((io->remote_fd = accept(tmp_sock, NULL, NULL)) == -1){
report_error("init_io_control(): accept(%d, NULL, NULL): %s", tmp_sock, strerror(errno));
return(-1);
}
if(close(tmp_sock) == -1){
report_error("init_io_control(): close(%d): %s", tmp_sock, strerror(errno));
return(-1);
}
len = sizeof addr;
if(getpeername(io->remote_fd, (struct sockaddr*) &addr, &len) == -1){
report_error("init_io_control(): getpeername(%d, %lx, %lx): %s", io->remote_fd, (unsigned long) &addr, &len, strerror(errno));
}
if(addr.ss_family == AF_INET){
s = (struct sockaddr_in *) &addr;
port = ntohs(s->sin_port);
inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr);
}else{
s6 = (struct sockaddr_in6 *) &addr;
port = ntohs(s6->sin6_port);
inet_ntop(AF_INET6, &s6->sin6_addr, ipstr, sizeof ipstr);
}
report_log("Controller: Connected from %s:%d.", ipstr, port);
return(io->remote_fd);
}
/***********************************************************************************************************************
*
* init_io_target()
*
* Input: None. We will use the global io and config structs.
* Output: An int showing success (by returning the remote_fd) or failure (by returning -1).
*
* Purpose: To initialize a target's network io layer.
*
**********************************************************************************************************************/
int init_io_target(){
int retval;
struct sockaddr_in name;
struct hostent *host;
char *ip_address;
char *ip_port;
int ip_address_len;
int tmp_sock;
struct sigaction act;
/* In the no ssl build, there is no difference between a target in bindshell mode, and the control node for networking. */
/* As such, we'll just pass through to the other rather than repeat code. */
if(io->target && config->bindshell){
return(init_io_control(config));
}
/* Initialize the structures we will need. */
/* Open our socket. */
ip_address_len = strlen(config->ip_addr);
if((ip_address = calloc(ip_address_len + 1, sizeof(char))) == NULL){
report_error("init_io_target(): calloc(%d, %d): %s", ip_address_len, (int) sizeof(char), strerror(errno));
return(-1);
}
memcpy(ip_address, config->ip_addr, ip_address_len);
if((ip_port = strchr(ip_address, ':')) == NULL){
report_error("init_io_target(): strchr(%s, ':'): Port not found!", ip_address);
return(-1);
}
*ip_port = '\0';
ip_port++;
if((host = gethostbyname(ip_address)) == NULL){
report_error("init_io_target(): gethostbyname(%s): %s", ip_address, strerror(errno));
return(-1);
}
memset(&name, 0, sizeof(name));
name.sin_family = AF_INET;
name.sin_port = htons(strtol(ip_port, NULL, 10));
free(ip_address);
if((tmp_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1){
report_error("init_io_target(): socket(AF_INET, SOCK_STREAM, 0): %s", strerror(errno));
return(-1);
}
/* Sepuku if left alone too long. */
memset(&act, 0, sizeof(act));
act.sa_handler = seppuku;
if(sigaction(SIGALRM, &act, NULL) == -1){
report_error("init_io_target(): sigaction(%d, %lx, %p): %s", SIGALRM, (unsigned long) &act, NULL, strerror(errno));
return(-1);
}
alarm(config->timeout);
if(verbose){
printf("Connecting to %s...", config->ip_addr);
fflush(stdout);
}
while((retval = connect(tmp_sock, (struct sockaddr *) &name, sizeof(name)))){
if(retval == -1){
report_error("init_io_target(): connect(%d, %lx, %d): %s", io->remote_fd, (unsigned long) &name, (int) sizeof(name), strerror(errno));
if((errno == ECONNREFUSED || errno == ETIMEDOUT)){
return(-2);
}
return(-1);
}
if(verbose){
printf("Connecting to %s...", config->ip_addr);
fflush(stdout);
}
}
io->remote_fd = tmp_sock;
act.sa_handler = SIG_DFL;
if(sigaction(SIGALRM, &act, NULL) == -1){
report_error("init_io_target(): sigaction(%d, %lx, %p): %s", SIGALRM, (unsigned long) &act, NULL, strerror(errno));
return(-1);
}
alarm(0);
if(verbose){
printf("\tConnected!\n");
}
return(io->remote_fd);
}