This repository has been archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathcontrol-server.c
867 lines (721 loc) · 26.4 KB
/
control-server.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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <netdb.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#ifdef __LINUX__
#include <alloca.h>
#endif
#ifdef __FREEBSD__
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "control-server.h"
#include "core.h"
#include "socket.h"
#include "utils.h"
#include "plugin.h"
#define BUFSIZE 4096
#define ERR_INVALID_PLUGIN_ID(x) {proxenet_write(x, "{\"error\": \"Invalid plugin id\"}", 30);}
#define ERR_INVALID_SYNTAX_JSON(x) {proxenet_write(x, "{\"error\": \"Invalid syntax\"}", 27);}
#define ERR_MISSING_ARGUMENT_JSON(x) {proxenet_write(x, "{\"error\": \"Missing argument\"}", 29);}
#define ERR_INVALID_ACTION(x) {proxenet_write(x, "{\"error\": \"Invalid action\"}", 27);}
#define COMMAND(NAME, HAS_ARG, DESC) { #NAME, HAS_ARG, &NAME##_cmd, "Command '" #NAME "':" DESC}
#define COMMAND_SIGNATURE(x) static void x##_cmd(sock_t, char*, unsigned int)
COMMAND_SIGNATURE(quit);
COMMAND_SIGNATURE(restart);
COMMAND_SIGNATURE(help);
COMMAND_SIGNATURE(info);
COMMAND_SIGNATURE(reload);
COMMAND_SIGNATURE(threads);
COMMAND_SIGNATURE(plugin);
COMMAND_SIGNATURE(config);
COMMAND_SIGNATURE(version);
static struct command_t known_commands[] = {
COMMAND(quit, 0, "Make "PROGNAME" leave kindly"),
COMMAND(restart, 0, "Make "PROGNAME" restart"),
COMMAND(help, 0, "Show this menu"),
COMMAND(info, 0, "Display information about environment"),
COMMAND(reload, 0, "Reload the plugins"),
COMMAND(threads, 1, "Command the threads"),
COMMAND(plugin, 1, "Get/Set info about plugin"),
COMMAND(config, 1, "Edit configuration at runtime"),
COMMAND(version, 0, "Show proxenet version"),
{ NULL, 0, NULL, NULL}
};
/**
* Send detailed information to socket
*
* @param fd the socket to send data to
*/
static void send_threads_list_as_json(sock_t fd)
{
char msg[BUFSIZE] = {0,};
int i, n;
bool first_item = true;
n = proxenet_xsnprintf(msg, sizeof(msg),
"\"Threads\":{"
"\"Max threads\": %d,"
"\"Running threads\": %d,",
cfg->nb_threads, get_active_threads_size());
proxenet_write(fd, msg, n);
proxenet_write(fd, "\"Details\": {", 12);
for (i=0; i < cfg->nb_threads; i++) {
if (!is_thread_active(i)) continue;
if (first_item) first_item=false;
else proxenet_write(fd, ",", 1);
memset(msg, 0, sizeof(msg));
#if defined __LINUX__
n = proxenet_xsnprintf(msg, sizeof(msg), "\"Thread-%d\": %lu", i, threads[i]);
#elif defined __FREEBSD__ || defined __DARWIN__
n = proxenet_xsnprintf(msg, sizeof(msg), "\"Thread-%d\": %p", i, threads[i]);
#endif
proxenet_write(fd, (void*)msg, n);
}
proxenet_write(fd, "}}", 2);
return;
}
/**
* Enumerate all plugins loaded
*/
static int send_plugin_list_as_json(sock_t fd, bool only_loaded)
{
plugin_t *p;
bool first_iter=true;
char msg[BUFSIZE] = {0, };
int n;
proxenet_write(fd, "{", 1);
for (p=plugins_list; p; p=p->next) {
if (only_loaded && p->state!=INACTIVE)
continue;
if(first_iter) first_iter=false;
else proxenet_write(fd, ",", 1);
memset(msg, 0, sizeof(msg));
n = proxenet_xsnprintf(msg, sizeof(msg),
"\"%s\": {"
"\"id\": %d,"
"\"priority\": %d,"
"\"type\": \"%s [0x%x]\","
"\"state\": \"%sACTIVE\"}",
p->name,
p->id,
p->priority,
supported_plugins_str[p->type],
p->type,
(p->state==ACTIVE?"":"IN"));
proxenet_write(fd, msg, n);
}
proxenet_write(fd, "}", 1);
return 0;
}
/**
* This command will try to kill gracefully the running process of proxenet
*/
static void quit_cmd(sock_t fd, char *options, unsigned int nb_options)
{
char *msg = "{\"retcode\": 0, \"message\": \"Leaving gracefully\n\"}";
/* happy compiler means karma++ */
(void) options;
(void) nb_options;
proxenet_write(fd, (void*)msg, strlen(msg));
proxy_state = INACTIVE;
return;
}
/**
* This command will restart proxenet. Restarting is the only way to unload and reload
* plugins safely.
*/
static void restart_cmd(sock_t fd, char *options, unsigned int nb_options)
{
(void) options;
(void) nb_options;
char *msg = "{\"retcode\": 0, \"message\": \"Restarting "PROGNAME"\n\"}";
proxenet_write(fd, msg, strlen(msg));
cfg->do_restart = true;
proxy_state = INACTIVE;
return;
}
/**
* The famous help menu
*/
static void help_cmd(sock_t fd, char *options, unsigned int nb_options)
{
struct command_t *cmd;
char msg[BUFSIZE];
int n;
bool first_iter = true;
(void) options;
(void) nb_options;
proxenet_write(fd, "{\"Command list\":{", 17);
for (cmd=known_commands; cmd && cmd->name; cmd++) {
if(first_iter) first_iter=false;
else proxenet_write(fd, ",", 1);
proxenet_xzero(msg, sizeof(msg));
n = proxenet_xsnprintf(msg, sizeof(msg), "\"%s\": \"%s\"", cmd->name, cmd->desc);
proxenet_write(fd, msg, n);
}
proxenet_write(fd, "}}", 2);
return;
}
/**
* Show proxenet version
*/
static void version_cmd(sock_t fd, char *options, unsigned int nb_options)
{
char *msg = "{"
"\"name\": \""PROGNAME"\", "
"\"author\": \""AUTHOR"\", "
"\"version\": \""VERSION"\", "
"\"compilation\": \""CC" for "SYSTEM" using MBedTLS "_MBEDTLS_VERSION_"\", "
"\"vms\": [ "
#ifdef _C_PLUGIN
"\""_C_VERSION_"\","
#endif
#ifdef _PYTHON_PLUGIN
"\""_PYTHON_VERSION_"\","
#endif
#ifdef _RUBY_PLUGIN
"\""_RUBY_VERSION_"\","
#endif
#ifdef _LUA_PLUGIN
"\""_LUA_VERSION_"\","
#endif
#ifdef _JAVA_PLUGIN
"\""_JAVA_VERSION_"\","
#endif
#ifdef _TCL_PLUGIN
"\""_TCL_VERSION_"\","
#endif
#ifdef _PERL_PLUGIN
"\""_PERL_VERSION_"\","
#endif
"\"\"],"
"\"license\": \""LICENSE"\" "
"}";
/* happy compiler means karma++ */
(void) options;
(void) nb_options;
proxenet_write(fd, (void*)msg, strlen(msg));
return;
}
/**
* Get information about proxenet state.
*/
static void info_cmd(sock_t fd, char *options, unsigned int nb_options)
{
char msg[BUFSIZE] = {0, };
int n;
(void) options;
(void) nb_options;
/* generic info */
n = proxenet_xsnprintf(msg, sizeof(msg),
"{\"info\":"
" {\"Information\":{"
" \"Listening interface\": \"%s/%s\","
" \"Supported IP version\": \"%s\","
" \"Logging file\": \"%s\","
" \"SSL private key\": \"%s\","
" \"SSL certificate\": \"%s\","
" \"Proxy\": \"%s [%s]\","
" \"Plugins directory\": \"%s\","
" \"Autoloading plugins directory\": \"%s\","
" \"Number of requests treated\":\"%lu\""
" },",
cfg->iface, cfg->port,
(cfg->ip_version==AF_INET)? "IPv4": (cfg->ip_version==AF_INET6)?"IPv6": "ANY",
(cfg->logfile)?cfg->logfile:"stdout",
cfg->keyfile,
cfg->cafile,
cfg->proxy.host ? cfg->proxy.host : "None",
cfg->proxy.host ? cfg->proxy.port : "direct",
cfg->plugins_path,
cfg->autoload_path,
(request_id-1)
);
proxenet_write(fd, (void*)msg, n);
/* threads info */
send_threads_list_as_json(fd);
proxenet_write(fd, ",", 1);
/* plugins info */
proxenet_write(fd, "\"Plugins\": ", 11);
send_plugin_list_as_json(fd, false);
proxenet_write(fd, "}}", 2);
return;
}
/**
* Reload proxenet
*/
static void reload_cmd(sock_t fd, char *options, unsigned int nb_options)
{
char *msg;
(void) options;
(void) nb_options;
if (get_active_threads_size() > 0) {
msg = "{\"error\": \"Threads still active, cannot reload\"}";
proxenet_write(fd, (void*)msg, strlen(msg));
return;
}
proxy_state = SLEEPING;
proxenet_destroy_plugins_vm();
proxenet_free_all_plugins();
if( proxenet_initialize_plugins_list() < 0) {
msg = "{\"error\": \"Failed to reinitilize plugins\"}";
proxenet_write(fd, (void*)msg, strlen(msg));
proxy_state = INACTIVE;
return;
}
proxenet_initialize_plugins();
proxy_state = ACTIVE;
msg = "{\"success\": \"Plugins list successfully reloaded\"}";
proxenet_write(fd, (void*)msg, strlen(msg));
return;
}
/**
* Get information about the threads
*/
static void threads_cmd(sock_t fd, char *options, unsigned int nb_options)
{
char msg[BUFSIZE] = {0, };
char *static_msg;
char *ptr;
int n, res;
long long tid;
(void) options;
(void) nb_options;
ptr = strtok(options, " \n");
if (!ptr){
proxenet_write(fd, "{", 1);
send_threads_list_as_json(fd);
proxenet_write(fd, "}", 1);
return;
}
if (strcmp(ptr, "inc")==0 && cfg->nb_threads<MAX_THREADS){
n = proxenet_xsnprintf(msg, BUFSIZE, "Nb threads level is now %d", ++cfg->nb_threads);
proxenet_write(fd, (void*)msg, n);
} else if (strcmp(ptr, "dec")==0 && cfg->nb_threads>1){
n = proxenet_xsnprintf(msg, BUFSIZE, "Nb threads level is now %d", --cfg->nb_threads);
proxenet_write(fd, (void*)msg, n);
} else if (strcmp(ptr, "kill")==0){
ptr = strtok(NULL, " \n");
if (!ptr){
static_msg = "Missing ThreadId";
proxenet_write(fd, (void*)static_msg, strlen(static_msg));
return;
}
tid = atoll(ptr);
if(tid<=0){
static_msg = "Invalid ThreadId value";
proxenet_write(fd, (void*)static_msg, strlen(static_msg));
return;
}
res = proxenet_kill_thread((pthread_t)tid);
if(res==0){
n = proxenet_xsnprintf(msg, BUFSIZE, "Thread %lu killed successfully", tid);
} else {
n = proxenet_xsnprintf(msg, BUFSIZE, "Failed to kill thread %lu: retcode=%d", tid, res);
}
proxenet_write(fd, (void*)msg, n);
} else {
static_msg = "Invalid action: must be in (inc|dec)";
proxenet_write(fd, (void*)static_msg, strlen(static_msg));
}
return;
}
/**
* Enumerate all plugins available in the plugins directory
*/
static int plugin_cmd_list_available(sock_t fd)
{
struct dirent *dir_ptr=NULL;
DIR *dir = NULL;
char* name = NULL;
char msg[2048] = {0, };
int n, type;
bool first_iter = true;
dir = opendir(cfg->plugins_path);
if (dir == NULL) {
xlog(LOG_ERROR, "Failed to open '%s': %s\n", cfg->plugins_path, strerror(errno));
n = proxenet_xsnprintf(msg, sizeof(msg), "{\"Error\": \"Cannot open '%s': %s\"}",
cfg->plugins_path, strerror(errno));
proxenet_write(fd, msg, n);
return -1;
}
n = proxenet_xsnprintf(msg, sizeof(msg),"{\"Plugins available in '%s'\": {", cfg->plugins_path);
proxenet_write(fd, msg, n);
while ((dir_ptr=readdir(dir))) {
name = dir_ptr->d_name;
if (!strcmp(name,".") || !strcmp(name,"..")) continue;
type = proxenet_get_plugin_type(name);
if (type < 0) continue;
/* this is only to avoid breaking json syntax */
if (!first_iter)
proxenet_write(fd, ",", 1);
else
first_iter = false;
proxenet_xzero(msg, sizeof(msg));
n = proxenet_xsnprintf(msg, sizeof(msg), "\"%s\": [\"%s\", %d]",
name, supported_plugins_str[type],
proxenet_is_plugin_loaded(name)?1:0);
proxenet_write(fd, msg, n);
}
proxenet_write(fd, "}}", 2);
if (closedir(dir) < 0){
xlog(LOG_ERROR, "Failed to close '%s': %s\n", cfg->plugins_path, strerror(errno));
n = proxenet_xsnprintf(msg, sizeof(msg), "{\"Error\": \"Cannot close dir '%s': %s\"",
cfg->plugins_path, strerror(errno));
proxenet_write(fd, msg, n);
return -1;
}
return 0;
}
/**
* Set plugin properties
*/
static int plugin_cmd_set(sock_t fd, char *options)
{
int ret, n;
char msg[BUFSIZE] = {0, };
char *ptr;
unsigned int plugin_id;
plugin_id = (unsigned int)atoi(options);
if ( proxenet_get_plugin_by_id( plugin_id )==NULL ) {
ERR_INVALID_PLUGIN_ID(fd);
return -1;
}
ptr = strtok(NULL, " \n");
if(!ptr){
n = proxenet_xsnprintf(msg, BUFSIZE, "{\"error\": \"Invalid syntax: plugin set %d <command>\"}", plugin_id);
proxenet_write(fd, (void*)msg, n);
return -1;
}
if (!strcmp(ptr, "toggle")){
ret = proxenet_toggle_plugin(plugin_id);
if (ret < 0){
n = proxenet_xsnprintf(msg, BUFSIZE, "{\"error\": \"Failed to toggle plugin %d\"}", plugin_id);
proxenet_write(fd, (void*)msg, n);
return -1;
}
n = proxenet_xsnprintf(msg, BUFSIZE, "{\"success\": \"Plugin %d is now %sACTIVE\"}", plugin_id, ret?"":"IN");
proxenet_write(fd, (void*)msg, n);
return 0;
}
if (!strcmp(ptr, "priority")){
ptr = strtok(NULL, " \n");
if(!ptr){
n = proxenet_xsnprintf(msg, BUFSIZE,
"{\"error\": \"Invalid syntax: plugin set %d prority <prio>\"}",
plugin_id);
proxenet_write(fd, (void*)msg, n);
return -1;
}
n = atoi(ptr);
if (n==0){
proxenet_write(fd, "{\"error\": \"Invalid priority\"}", 29);
return -1;
}
if (proxenet_plugin_set_prority(plugin_id, n) < 0){
n = proxenet_xsnprintf(msg, BUFSIZE,
"{\"error\": \"An error occured during priority update for plugin %d\"}",
plugin_id);
proxenet_write(fd, (void*)msg, n);
return -1;
}
n = proxenet_xsnprintf(msg, BUFSIZE, "{\"success\": \"Plugin %d priority is now %d\"}", plugin_id, n);
proxenet_write(fd, (void*)msg, n);
return 0;
}
n = proxenet_xsnprintf(msg, BUFSIZE, "{\"error\": \"Unknown action '%s' for plugin %d\"}", ptr, plugin_id);
proxenet_write(fd, (void*)msg, n);
return -1;
}
/**
* Load a plugin during runtime
*/
static int plugin_cmd_load(sock_t fd, char *options)
{
char* plugin_name = NULL;
int ret, n;
char msg[BUFSIZE] = {0, };
char *ptr;
size_t l;
ptr = strtok(options, " \n");
if (!ptr){
proxenet_write(fd, "{\"error\": \"Missing plugin name\"}", 32);
return -1;
}
l = strlen(ptr);
plugin_name = alloca(l+1);
proxenet_xzero(plugin_name, l+1);
memcpy(plugin_name, ptr, l);
ret = proxenet_add_new_plugins(cfg->plugins_path, plugin_name);
if(ret < 0){
n = proxenet_xsnprintf(msg, sizeof(msg)-1, "{\"error\": \"Error while loading plugin '%s'\"}", plugin_name);
proxenet_write(fd, (void*)msg, n);
return -1;
}
if(ret)
n = proxenet_xsnprintf(msg, sizeof(msg)-1, "{\"success\": \"Plugin '%s' added successfully\"}", plugin_name);
else
n = proxenet_xsnprintf(msg, sizeof(msg)-1, "{\"error\": \"File '%s' has not been added\"}", plugin_name);
if (n>0)
proxenet_write(fd, (void*)msg, n);
else
return -1;
/* proxenet_initialize_plugins() is called to try to activate INACTIVE status */
if(ret) {
proxenet_initialize_plugins();
return 0;
}
return -1;
}
/**
*
*/
static int plugin_cmd_change_all_status(proxenet_state new_state)
{
plugin_t *p;
proxenet_state old_state;
int retcode;
switch (new_state){
case INACTIVE:
old_state = ACTIVE;
break;
case ACTIVE:
old_state = INACTIVE;
break;
default:
xlog(LOG_ERROR, "Invalid state %d\n", new_state);
return -1;
}
for (p=plugins_list; p!=NULL; p=p->next) {
if (p->state != old_state)
continue;
retcode = proxenet_plugin_set_state(p, new_state);
if (retcode < 0){
xlog(LOG_ERROR, "Failed to set state %ud to plugin %ud\n", new_state, p->id);
}
}
return 0;
}
/**
* Handle plugins (list/activate/deactivate/load)
*/
static void plugin_cmd(sock_t fd, char *options, unsigned int nb_options)
{
char *ptr;
int res;
(void) options;
(void) nb_options;
/* usage */
ptr = strtok(options, " \n");
if (!ptr){
ERR_INVALID_ACTION(fd);
return;
}
if (strcmp(ptr, "list") == 0) {
send_plugin_list_as_json(fd, false);
return;
}
if (strcmp(ptr, "list-all") == 0) {
plugin_cmd_list_available(fd);
return;
}
if (strcmp(ptr, "enable-all") == 0) {
plugin_cmd_change_all_status(ACTIVE);
return;
}
if (strcmp(ptr, "disable-all") == 0) {
plugin_cmd_change_all_status(INACTIVE);
return;
}
if (strcmp(ptr, "set") == 0) {
/* shift argument */
ptr = strtok(NULL, " \n");
if (!ptr){
xlog(LOG_ERROR, "%s\n", "Failed to get 'set' argument");
return;
}
res = plugin_cmd_set(fd, ptr);
if(res < 0){
xlog(LOG_ERROR, "%s\n", "An error occured during plugin setting");
}
return;
}
if (strcmp(ptr, "load") == 0) {
/* shift argument */
ptr = strtok(NULL, " \n");
if (!ptr){
xlog(LOG_ERROR, "%s\n", "Failed to get 'load' argument");
return;
}
res = plugin_cmd_load(fd, ptr);
if(res < 0){
xlog(LOG_ERROR, "%s\n", "An error occured during plugin loading");
}
return;
}
}
/**
* List configuration settings
*/
static void plugin_config_cmd_list(sock_t fd)
{
char msg[BUFSIZE];
int n;
n = proxenet_xsnprintf(msg, BUFSIZE,
"{\"Configuration parameters\":"
"{"
" \"verbose\": {\"value\": %d, \"type\": \"int\" },"
" \"state\": {\"value\": \"%s\", \"type\": \"int\" },"
" \"logfile\": {\"value\": \"%s\", \"type\": \"None\" },"
" \"intercept_pattern\": {\"value\": \"%s\", \"type\": \"str\" },"
" \"ssl_intercept\": {\"value\": \"%s\", \"type\": \"bool\" }"
"}"
"}"
,
cfg->verbose,
proxy_state==SLEEPING?"SLEEPING":"ACTIVE",
(cfg->logfile)?cfg->logfile:"stdout",
cfg->intercept_pattern,
cfg->ssl_intercept?"true":"false"
);
proxenet_write(fd, msg, n);
return;
}
/**
* View or edit configuration
*/
static void config_cmd(sock_t fd, char *options, unsigned int nb_options)
{
char *ptr;
char msg[BUFSIZE]={0,};
int n;
(void) options;
(void) nb_options;
/* usage */
ptr = strtok(options, " \n");
if (!ptr){
ERR_INVALID_SYNTAX_JSON(fd);
return;
}
if (strcmp(ptr, "list")==0)
return plugin_config_cmd_list(fd);
if (strcmp(ptr, "set")!=0 && strcmp(ptr, "get")!=0){
ERR_INVALID_SYNTAX_JSON(fd);
return;
}
ptr = strtok(NULL, " \n");
if (!ptr){ ERR_MISSING_ARGUMENT_JSON(fd); return; }
if (!strcmp(ptr, "intercept_pattern")){
ptr = strtok(NULL, " \n");
if (!ptr){ ERR_MISSING_ARGUMENT_JSON(fd); return; }
proxenet_xfree( cfg->intercept_pattern );
cfg->intercept_pattern = proxenet_xstrdup2(ptr);
n = proxenet_xsnprintf(msg, sizeof(msg), "{\"success\": \"Intercept pattern is now '%s'\" }", cfg->intercept_pattern);
proxenet_write(fd, msg, n);
return;
}
if (!strcmp(ptr, "verbose")){
ptr = strtok(NULL, " \n");
if (!ptr){ ERR_MISSING_ARGUMENT_JSON(fd); return; }
cfg->verbose = atoi(ptr);
n = proxenet_xsnprintf(msg, sizeof(msg), "{\"success\": \"Verbose is now '%d'\" }", cfg->verbose);
proxenet_write(fd, msg, n);
return;
}
if (!strcmp(ptr, "ssl_intercept")){
ptr = strtok(NULL, " \n");
if (!ptr){ ERR_MISSING_ARGUMENT_JSON(fd); return; }
cfg->ssl_intercept = strcasecmp(ptr,"true")==0?true:false;
n = proxenet_xsnprintf(msg, sizeof(msg), "{\"success\": \"SSL Intercept is set to %d\" }", cfg->ssl_intercept);
proxenet_write(fd, msg, n);
return;
}
if (!strcmp(ptr, "state")){
ptr = strtok(NULL, " \n");
if (!ptr){ ERR_MISSING_ARGUMENT_JSON(fd); return; }
if (strcasecmp(ptr, "sleeping")==0 && proxy_state==ACTIVE){
proxy_state = SLEEPING;
n = proxenet_xsnprintf(msg, sizeof(msg), "{\"success\": \""PROGNAME" is paused\" }");
proxenet_write(fd, msg, n);
return;
} else if (strcasecmp(ptr, "active")==0 && proxy_state==SLEEPING){
proxy_state = ACTIVE;
n = proxenet_xsnprintf(msg, sizeof(msg), "{\"success\": \""PROGNAME" is unpaused\" }");
proxenet_write(fd, msg, n);
return;
} else {
n = proxenet_xsnprintf(msg, sizeof(msg), "{\"success\": \"nothing to do\" }");
proxenet_write(fd, msg, n);
return;
}
}
}
/**
* Parse an incoming command
*
* @param name is a pointer to the buffer of the command to parse
* @return pointer to valid struct command_t if `name` is found as known_commands[]
* @return NULL otherwise
*/
struct command_t* get_command(char *name)
{
struct command_t *cmd;
char *buf = name;
char c;
do {
c = *buf;
if (c == '\0')
return NULL;
if (c=='\n' || c==' ') {
*buf = '\0';
break;
}
buf++;
} while (1);
for (cmd=known_commands; cmd && cmd->name; cmd++) {
if (strcmp(name, cmd->name) == 0) {
*buf = c;
return cmd;
}
}
return NULL;
}
/**
* Main handler for new control command
*/
int proxenet_handle_control_event(sock_t* sock) {
char read_buf[BUFSIZE] = {0, };
char *ptr = NULL;
int retcode = -1;
struct command_t *cmd = NULL;
retcode = proxenet_read(*sock, read_buf, BUFSIZE-1);
if (retcode < 0) {
xlog(LOG_ERROR, "Failed to read control command: %s\n", strerror(errno));
return -1;
}
if (retcode == 0) {
return -1;
}
if (read_buf[0] == '\n') {
goto cmd_end;
}
if ( (cmd=get_command(read_buf)) == NULL ) {
proxenet_write(*sock, CONTROL_INVALID, strlen(CONTROL_INVALID));
goto cmd_end;
}
if(cfg->verbose > 1)
xlog(LOG_INFO, "Receiving control command: \"%s\" \n", cmd->name);
ptr = &read_buf[strlen(cmd->name)];
cmd->func(*sock, ptr, cmd->nb_opt_max);
cmd_end:
proxenet_write(*sock, CONTROL_PROMPT, strlen(CONTROL_PROMPT));
return 0;
}