File main.c changed (mode: 100644) (index 8faf16d..b1e89b6) |
... |
... |
char *remote_tox_id = NULL; |
31 |
31 |
/* Directory with config and tox save */ |
/* Directory with config and tox save */ |
32 |
32 |
char config_path[500] = "/etc/tuntox/"; |
char config_path[500] = "/etc/tuntox/"; |
33 |
33 |
|
|
|
34 |
|
/* Limit hostname and port in server */ |
|
35 |
|
int nrules = 0; |
|
36 |
|
enum rules_policy_enum rules_policy = NONE; |
|
37 |
|
rule *rules = NULL; |
|
38 |
|
|
34 |
39 |
/* Ports and hostname for port forwarding */ |
/* Ports and hostname for port forwarding */ |
35 |
40 |
int remote_port = 0; |
int remote_port = 0; |
36 |
41 |
char *remote_host = NULL; |
char *remote_host = NULL; |
|
... |
... |
int allowed_toxid_cmp(allowed_toxid *a, allowed_toxid *b) |
88 |
93 |
return memcmp(a->toxid, b->toxid, TOX_PUBLIC_KEY_SIZE); |
return memcmp(a->toxid, b->toxid, TOX_PUBLIC_KEY_SIZE); |
89 |
94 |
} |
} |
90 |
95 |
|
|
|
96 |
|
/* Comparison function for rule objects */ |
|
97 |
|
int rule_cmp(rule *a, rule *b) |
|
98 |
|
{ |
|
99 |
|
//log_printf(L_INFO, "Comparison result: %d %d\n", strcmp(a->host, b->host), (a->port == b->port)); |
|
100 |
|
if ((strcmp(a->host, b->host)==0) && (a->port == b->port)) |
|
101 |
|
return 0; |
|
102 |
|
else |
|
103 |
|
return -1; |
|
104 |
|
} |
|
105 |
|
|
91 |
106 |
void update_select_nfds(int fd) |
void update_select_nfds(int fd) |
92 |
107 |
{ |
{ |
93 |
108 |
/* TODO maybe replace with a scan every time to make select() more efficient in the long run? */ |
/* TODO maybe replace with a scan every time to make select() more efficient in the long run? */ |
|
... |
... |
int handle_request_tunnel_frame(protocol_frame *rcvd_frame) |
388 |
403 |
hostname[rcvd_frame->data_length] = '\0'; |
hostname[rcvd_frame->data_length] = '\0'; |
389 |
404 |
|
|
390 |
405 |
log_printf(L_INFO, "Got a request to forward data from %s:%d\n", hostname, port); |
log_printf(L_INFO, "Got a request to forward data from %s:%d\n", hostname, port); |
|
406 |
|
|
|
407 |
|
// check rules |
|
408 |
|
if (rules_policy == ENFORCE && nrules > 0) { |
|
409 |
|
// selects a random service |
|
410 |
|
int r = rand() % nrules; |
|
411 |
|
int i; |
|
412 |
|
rule * rtmp = rules; |
|
413 |
|
|
|
414 |
|
for (i = 0; i < r; i++) |
|
415 |
|
{ |
|
416 |
|
if (rtmp != NULL) |
|
417 |
|
rtmp = rtmp->next; |
|
418 |
|
} |
|
419 |
|
|
|
420 |
|
if (rtmp != NULL) { |
|
421 |
|
port = rtmp->port; |
|
422 |
|
hostname = strdup(rtmp->host); |
|
423 |
|
} else { |
|
424 |
|
log_printf(L_ERROR, "Could not find valid hostname/port. Dropping request.\n"); |
|
425 |
|
return -1; |
|
426 |
|
} |
|
427 |
|
|
|
428 |
|
log_printf(L_INFO, "ENFORCE policy enabled, using %s:%d\n", hostname, port); |
|
429 |
|
|
|
430 |
|
} else if (rules_policy == VALIDATE && nrules > 0 ) { |
|
431 |
|
|
|
432 |
|
|
|
433 |
|
// new implementatio |
|
434 |
|
|
|
435 |
|
rule rtmp, *found = NULL; |
|
436 |
|
rtmp.host = hostname; |
|
437 |
|
rtmp.port = port; |
|
438 |
|
|
|
439 |
|
LL_SEARCH(rules, found, &rtmp, rule_cmp); |
|
440 |
|
if(!found) |
|
441 |
|
{ |
|
442 |
|
log_printf(L_WARNING, "Rejected, request not in rules\n"); |
|
443 |
|
return -1; |
|
444 |
|
} |
|
445 |
|
|
|
446 |
|
} else { |
|
447 |
|
log_printf(L_WARNING, "Filter option active but no allowed ports!\n"); |
|
448 |
|
log_printf(L_WARNING, "All requests will be dropped.\n"); |
|
449 |
|
return -1; |
|
450 |
|
} |
|
451 |
|
|
|
452 |
|
|
391 |
453 |
|
|
392 |
454 |
tunnel_id = get_random_tunnel_id(); |
tunnel_id = get_random_tunnel_id(); |
393 |
455 |
log_printf(L_DEBUG, "Tunnel ID: %d\n", tunnel_id); |
log_printf(L_DEBUG, "Tunnel ID: %d\n", tunnel_id); |
|
... |
... |
static size_t load_save(uint8_t **out_data) |
706 |
768 |
} |
} |
707 |
769 |
} |
} |
708 |
770 |
|
|
|
771 |
|
void load_rules() |
|
772 |
|
{ |
|
773 |
|
char * ahost=NULL; |
|
774 |
|
int aport=0; |
|
775 |
|
char line[100 + 1] = ""; |
|
776 |
|
uint8_t path_tmp[512], path_real[512], *p; |
|
777 |
|
FILE *file = NULL; |
|
778 |
|
rule *rule_obj = NULL; |
|
779 |
|
|
|
780 |
|
|
|
781 |
|
strncpy(path_real, config_path, sizeof(config_path)); |
|
782 |
|
|
|
783 |
|
p = path_real + strlen(path_real); |
|
784 |
|
memcpy(p, "rules", sizeof("rules")); |
|
785 |
|
|
|
786 |
|
unsigned int path_len = (p - path_real) + sizeof("rules"); |
|
787 |
|
|
|
788 |
|
file = fopen((char *)path_real, "r"); |
|
789 |
|
|
|
790 |
|
if (file == NULL) { |
|
791 |
|
log_printf(L_WARNING, "Could not open rules file!\n"); |
|
792 |
|
return; |
|
793 |
|
} |
|
794 |
|
|
|
795 |
|
int linen = 0; |
|
796 |
|
while (fgets(line, sizeof(line), file)) { |
|
797 |
|
/* note that fgets don't strip the terminating \n, checking its |
|
798 |
|
presence would allow to handle lines longer that sizeof(line) */ |
|
799 |
|
if(line) |
|
800 |
|
{ |
|
801 |
|
// allow comments & white lines |
|
802 |
|
if (line[0]=='#'||line[0]=='\n') { |
|
803 |
|
continue; |
|
804 |
|
} |
|
805 |
|
if (parse_pipe_port_forward(line, &ahost, &aport) >= 0) { |
|
806 |
|
if (aport > 0 && aport < 65535) { |
|
807 |
|
|
|
808 |
|
rule_obj = (rule *)calloc(sizeof(rule), 1); |
|
809 |
|
if(!rule_obj) |
|
810 |
|
{ |
|
811 |
|
log_printf(L_ERROR, "Could not allocate memory for rule"); |
|
812 |
|
exit(1); |
|
813 |
|
} |
|
814 |
|
|
|
815 |
|
rule_obj->port = aport; |
|
816 |
|
rule_obj->host = strdup(ahost); |
|
817 |
|
|
|
818 |
|
LL_APPEND(rules, rule_obj); |
|
819 |
|
|
|
820 |
|
linen++; |
|
821 |
|
} else { |
|
822 |
|
log_printf(L_WARNING, "Invalid port in line: %s\n", line); |
|
823 |
|
} |
|
824 |
|
} else { |
|
825 |
|
log_printf(L_WARNING, "Could not parse line: %s\n", line); |
|
826 |
|
} |
|
827 |
|
} else { |
|
828 |
|
break; |
|
829 |
|
} |
|
830 |
|
} |
|
831 |
|
fclose(file); |
|
832 |
|
nrules = linen; |
|
833 |
|
|
|
834 |
|
log_printf(L_INFO, "Loaded %d rules\n", nrules); |
|
835 |
|
if (nrules==0 && |
|
836 |
|
(rules_policy == ENFORCE || rules_policy == VALIDATE)){ |
|
837 |
|
log_printf(L_WARNING, "No rules loaded! NO CONNECTIONS WILL BE ALLOWED!\n"); |
|
838 |
|
} |
|
839 |
|
} |
|
840 |
|
|
|
841 |
|
void clear_rules() |
|
842 |
|
{ |
|
843 |
|
int i; |
|
844 |
|
rule * elt, *tmp; |
|
845 |
|
/* now delete each element, use the safe iterator */ |
|
846 |
|
LL_FOREACH_SAFE(rules,elt,tmp) { |
|
847 |
|
LL_DELETE(rules,elt); |
|
848 |
|
free(elt->host); |
|
849 |
|
free(elt); |
|
850 |
|
} |
|
851 |
|
} |
|
852 |
|
|
709 |
853 |
void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, void *userdata) |
void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, void *userdata) |
710 |
854 |
{ |
{ |
711 |
855 |
unsigned char tox_printable_id[TOX_ADDRESS_SIZE * 2 + 1]; |
unsigned char tox_printable_id[TOX_ADDRESS_SIZE * 2 + 1]; |
|
... |
... |
int main(int argc, char *argv[]) |
1080 |
1224 |
|
|
1081 |
1225 |
log_init(); |
log_init(); |
1082 |
1226 |
|
|
1083 |
|
while ((oc = getopt(argc, argv, "L:pi:C:s:P:dqhSF:DU:")) != -1) |
|
|
1227 |
|
while ((oc = getopt(argc, argv, "L:pi:C:s:f:P:dqhSF:DU:")) != -1) |
1084 |
1228 |
{ |
{ |
1085 |
1229 |
switch(oc) |
switch(oc) |
1086 |
1230 |
{ |
{ |
|
... |
... |
int main(int argc, char *argv[]) |
1153 |
1297 |
} |
} |
1154 |
1298 |
load_saved_toxid_in_client_mode = 1; |
load_saved_toxid_in_client_mode = 1; |
1155 |
1299 |
break; |
break; |
|
1300 |
|
case 'f': |
|
1301 |
|
switch(optarg[0]) |
|
1302 |
|
{ |
|
1303 |
|
case 'E': |
|
1304 |
|
rules_policy = ENFORCE; |
|
1305 |
|
log_printf(L_INFO, "Filter policy set to ENFORCE\n"); |
|
1306 |
|
break; |
|
1307 |
|
case 'V': |
|
1308 |
|
rules_policy = VALIDATE; |
|
1309 |
|
log_printf(L_INFO, "Filter policy set to VALIDATE\n"); |
|
1310 |
|
break; |
|
1311 |
|
case 'N': |
|
1312 |
|
rules_policy = NONE; |
|
1313 |
|
log_printf(L_INFO, "Filter policy set to NONE\n"); |
|
1314 |
|
break; |
|
1315 |
|
default: |
|
1316 |
|
log_printf(L_WARNING, "Invalid filter policy, reverting to ENFORCE."); |
|
1317 |
|
rules_policy = ENFORCE; |
|
1318 |
|
} |
|
1319 |
|
break; |
1156 |
1320 |
case 's': |
case 's': |
1157 |
1321 |
/* Shared secret */ |
/* Shared secret */ |
1158 |
1322 |
use_shared_secret = 1; |
use_shared_secret = 1; |
|
... |
... |
int main(int argc, char *argv[]) |
1196 |
1360 |
{ |
{ |
1197 |
1361 |
log_printf(L_INFO, "Server in ToxID whitelisting mode - only clients listed with -i can connect"); |
log_printf(L_INFO, "Server in ToxID whitelisting mode - only clients listed with -i can connect"); |
1198 |
1362 |
} |
} |
|
1363 |
|
|
|
1364 |
|
if((!client_mode) && (rules_policy != NONE)) |
|
1365 |
|
{ |
|
1366 |
|
load_rules(); |
|
1367 |
|
} |
1199 |
1368 |
|
|
1200 |
1369 |
if(daemonize) |
if(daemonize) |
1201 |
1370 |
{ |
{ |
|
... |
... |
int main(int argc, char *argv[]) |
1280 |
1449 |
|
|
1281 |
1450 |
tox_callback_friend_request(tox, accept_friend_request, NULL); |
tox_callback_friend_request(tox, accept_friend_request, NULL); |
1282 |
1451 |
do_server_loop(); |
do_server_loop(); |
|
1452 |
|
clear_rules(); |
1283 |
1453 |
} |
} |
1284 |
1454 |
|
|
1285 |
1455 |
return 0; |
return 0; |