-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.c
1419 lines (1354 loc) · 61.2 KB
/
config.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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017 J Cody Baker. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <yaml.h>
#include <pthread.h>
#include <pwd.h>
#include "config.h"
#include "log.h"
#include "snip.h"
#include "net_util.h"
#include "sys_util.h"
#include <event2/util.h>
// Local defs.
/**
* Cleanup a route object by freeing children.
* @param route
*/
void
snip_config_route_clean(snip_config_route_t *route);
/**
* Cleanup and free a snip_config_route_list_t object.
* @param route_list[in,out]
*/
void
snip_config_route_list_destroy(snip_config_route_list_t *route_list);
/**
* Create a snip_config_t object.
* @return
*/
snip_config_t *
snip_config_create() {
snip_config_t *config = malloc(sizeof(snip_config_t));
memset(config, '\0', sizeof(snip_config_t));
pthread_mutex_init(&(config->lock), 0);
config->user_id = -1;
config->group_id = -1;
config->default_route.action = snip_route_action_tls_fatal_unrecognized_name;
config->no_sni_route.action = snip_route_action_tls_fatal_unrecognized_name;
config->tls_error_route.action = snip_route_action_tls_fatal_decode_error;
// TODO - If we see something that looks like HTTP we could try to redirect them or display an error.
const char *http_fallback_message =
"HTTP/1.1 400 Bad Request\r\n"
"Connection: close\r\n"
"Context-Type: test/html;\r\n"
"\r\n"
""
"<html><head></head><body><h1>You must use HTTPS to access this service.</h1></body></html>\n";
const size_t http_fallback_message_length = strlen(http_fallback_message);
config->http_fallback_route.send_text = malloc(strlen(http_fallback_message) + 1);
memset((void*)config->http_fallback_route.send_text, '\0', strlen(http_fallback_message) + 1);
memcpy((void*)config->http_fallback_route.send_text, http_fallback_message, http_fallback_message_length);
config->http_fallback_route.action = snip_route_action_send_text;
config->proxy_connect_failure_route.action = snip_route_action_tls_fatal_internal_error;
return config;
}
/**
* Cleanup a listener object by freeing children.
* @param listener
*/
void
snip_config_listener_clean(snip_config_listener_t *listener) {
if(listener->routes) {
snip_config_route_list_destroy(listener->routes);
listener->routes = NULL;
}
snip_config_route_clean(&listener->default_route);
snip_config_route_clean(&listener->no_sni_route);
snip_config_route_clean(&listener->tls_error_route);
snip_config_route_clean(&listener->http_fallback_route);
snip_config_route_clean(&listener->proxy_connect_failure_route);
}
/**
* Create a listener linked-list item.
* @return
*/
snip_config_listener_list_t *
snip_config_listener_list_create() {
snip_config_listener_list_t *list = malloc(sizeof(snip_config_listener_list_t));
memset(list, '\0', sizeof(snip_config_listener_list_t));
return list;
}
/**
* Recursively destroy a list of snip_config_listeners.
* @param list[in,out]
*/
void
snip_config_listener_list_destroy(snip_config_listener_list_t *list) {
if(list->next) {
snip_config_listener_list_destroy(list->next);
}
snip_config_listener_clean(&list->value);
free(list);
}
/**
* Allocate and initialize a new snip_config_route object.
* @return The new snip_config_route_list_t *
*/
snip_config_route_list_t *
snip_config_route_list_create() {
snip_config_route_list_t *route_list = malloc(sizeof(snip_config_route_list_t));
memset(route_list, '\0', sizeof(snip_config_route_list_t));
return route_list;
}
/**
* Cleanup a route object by freeing children.
* @param route
*/
void
snip_config_route_clean(snip_config_route_t *route) {
if(route->dest_hostname) {
free((void*) route->dest_hostname);
}
if(route->sni_hostname) {
free((void*) route->sni_hostname);
}
if(route->send_file) {
free((void*) route->send_file);
}
if(route->send_text) {
free((void*) route->send_text);
}
}
/**
* Cleanup and free a snip_config_route_list_t object.
* @param route_list[in,out]
*/
void
snip_config_route_list_destroy(snip_config_route_list_t *route_list) {
if(route_list->next) {
snip_config_route_list_destroy(route_list->next);
}
snip_config_route_clean(&route_list->value);
free(route_list);
}
/**
* Display the help information and exit the application.
* @param name[in] - The name of the command.
*/
void
snip_config_display_help_and_exit(const char *name) {
const char *better_name = strrchr(name, '/') == NULL ? name : strrchr(name, '/') + 1;
printf(
"snip - TLS SNI Proxy v%s\n"
"\n"
"usage: %s [arguments]\n"
"\n"
"Arguments:\n"
" -c FILE, --conf FILE Specify an alternative config file.\n"
" Default: %s\n"
" -t, --test-config Evaluate the config file and then exit. Exit code 0 for success.\n"
" -h, --help Display this help message.\n"
"\n"
"Exit codes with special meanings:\n"
" %d - Configuration error\n"
" %d - Error establishing a listener socket\n"
"\n",
SNIP_VERSION,
better_name,
SNIP_INSTALL_CONF_PATH,
SNIP_EXIT_ERROR_INVALID_CONFIG,
SNIP_EXIT_ERROR_SOCKET
);
exit(0);
}
/**
* Parse the command line arguments and drop them into a configuration.
* @param config[in,out]
* @param argc[in]
* @param argv[in]
*/
void
snip_config_parse_args(snip_config_t *config, int argc, char **argv) {
int index = 0;
int rv = 0;
const struct option arguments[] = {
{
"conf",
required_argument,
NULL,
'c'
},
{
"test-config",
no_argument,
NULL,
't'
},
{
"help",
no_argument,
NULL,
'h'
},
{
0,
0,
0,
0
}
};
optind = 0;
do {
rv = getopt_long(argc, argv, "c:ht", arguments, &index);
switch(rv) {
case 'c':
config->config_path = optarg;
break;
case 'h':
snip_config_display_help_and_exit(argc ? argv[0] : "snip");
break;
case 't':
config->just_test_config = TRUE;
break;
case -1:
break;
default:
exit(SNIP_EXIT_ERROR_INVALID_CONFIG);
break;
}
} while (rv >= 0);
}
/**
* Log a configuration event with location references within the configuration file.
* @param config
* @param event
* @param level - Log severity level. If SNIP_LOG_LEVEL_FATAL, we will shutdown and exit.
* @param msg_format - Single-line description of the condition we want to log. printf style format string populated
* with variadic arguments provided in args.
* @param ... - List of arguments for populating the format string msg_format.
*/
void
snip_log_config(snip_config_t *config, yaml_event_t *event, snip_log_level_t level, const char *msg_format, ...) {
const char *config_error_msg = "%sin configuration file '%s' between %d:%d and %d:%d.";
size_t buffer_max = 1 + (size_t) snprintf(NULL,
0,
config_error_msg,
msg_format,
config->config_path,
event->start_mark.line,
event->start_mark.column,
event->end_mark.line,
event->end_mark.column);
char *buffer = malloc(buffer_max);
memset(buffer, '\0', buffer_max);
snprintf(buffer,
buffer_max,
config_error_msg,
msg_format,
config->config_path,
event->start_mark.line,
event->start_mark.column,
event->end_mark.line,
event->end_mark.column);
va_list args;
va_start(args, msg_format);
if(level == SNIP_LOG_LEVEL_FATAL) {
snip_vlog_fatal(SNIP_EXIT_ERROR_INVALID_CONFIG, buffer, args);
}
else {
snip_vlog(level, buffer, args);
}
va_end(args);
free(buffer);
}
/**
* Parse a YAML bool tag.
* @param event
* @param value Pointer to a location where we can store the value.
* @return TRUE if the value was parsed properly, FALSE otherwise.
*
* @see http://yaml.org/type/bool.html
*/
SNIP_BOOLEAN
snip_parse_boolean(yaml_event_t *event, SNIP_BOOLEAN *value) {
if(event->type != YAML_SCALAR_EVENT) {
return FALSE;
}
if(!evutil_ascii_strcasecmp((const char *) event->data.scalar.value, "TRUE") ||
!evutil_ascii_strcasecmp((const char *) event->data.scalar.value, "YES") ||
!evutil_ascii_strcasecmp((const char *) event->data.scalar.value, "ON") ||
!evutil_ascii_strcasecmp((const char *) event->data.scalar.value, "Y")) {
*value = TRUE;
return TRUE;
}
else if(!evutil_ascii_strcasecmp((const char *) event->data.scalar.value, "FALSE") ||
!evutil_ascii_strcasecmp((const char *) event->data.scalar.value, "NO") ||
!evutil_ascii_strcasecmp((const char *) event->data.scalar.value, "Off") ||
!evutil_ascii_strcasecmp((const char *) event->data.scalar.value, "N")) {
*value = FALSE;
return TRUE;
}
else {
return FALSE;
}
}
/**
* Given a string of digits (ex. "12345") parse it into a port and set *port to the value. It may NOT be prefaced or
* suffixed by any extra characters, must be a valid 16-bit number, and must only contain digits.
* @param port_string[in] A NULL terminated string of at 1 to 5 digits.
* @param port[out] Pointer to a uint16_t where the port value should be stored.
* @return True if the port is valid and was parsed properly. False otherwise.
*/
SNIP_BOOLEAN
snip_parse_port(const char *port_string, uint16_t *port) {
const char *port_end = port_string;
while(1) {
if((port_end - port_string) > 5) {
return FALSE;
}
if(*port_end == '\0') {
break;
}
if((*port_end < '0') || (*port_end > '9')) {
return FALSE; // Return false if the port contains a non-digit character.
}
port_end += 1;
}
if(port_string == port_end) {
// A colon was found, but no port was found after it.
return FALSE;
}
unsigned long port_ul = strtoul(port_string, NULL, 10);
if(port_ul > 0xFFFF) { // port must be a 16-bit number.
return FALSE;
}
*port = (uint16_t) port_ul;
return TRUE;
}
/**
* Given a string of format "www.example.com:12345", parse the hostname and port, and allocate a new string for the
* separated hostname.
*
* @param target[in] - A target string of the format "www.example.com:12345" or "www.example.com".
* @param hostname[out] - A place where we can store a pointer to the hostname string.
* @param port[out] - Address where we can store the port. We set 0 if the port isn't specified.
* @return true (1) if the parse was successful, false (0) otherwise.
*/
SNIP_BOOLEAN
snip_parse_target(const char *target, const char **hostname, uint16_t *port) {
*hostname = NULL;
const char *colon = strchr(target, ':');
size_t hostname_length;
if(colon) {
hostname_length = colon - target;
if(!snip_parse_port(colon + 1, port)) {
return 0;
}
*hostname = malloc(hostname_length + 1);
memset((void*) *hostname, '\0', hostname_length + 1);
memcpy((void*) *hostname, target, hostname_length);
return 1;
}
else {
hostname_length = strlen(target) + 1;
*hostname = malloc(hostname_length);
memset((void*) *hostname, '\0', hostname_length);
strcpy((void*) *hostname, target);
*port = 0;
return 1;
}
}
/**
* Verify the configuration is valid and can be executed.
* @param config
* @return
*/
SNIP_BOOLEAN
snip_validate_config_file(snip_config_t *config) {
if(!config->listeners) {
snip_log_fatal(SNIP_EXIT_ERROR_INVALID_CONFIG, "Config file '%s' had no listeners.", config->config_path);
return FALSE;
}
if(config->user_id == -1 && config->group_id != -1) {
snip_log_fatal(SNIP_EXIT_ERROR_INVALID_CONFIG,
"Config file '%s' specified a 'group' parameter, but no 'user' parameter.",
config->config_path);
return FALSE;
}
if(config->disable_ipv4 && config->disable_ipv6) {
snip_log_fatal(SNIP_EXIT_ERROR_INVALID_CONFIG,
"Config file '%s' disabled IPv4 and IPv6.",
config->config_path);
return FALSE;
}
return TRUE;
}
/**
* Read the configuration file and apply it to the specified config structure.
* @param config[in,out]
*/
SNIP_BOOLEAN
snip_parse_config_file(snip_config_t *config) {
FILE *config_file = fopen(config->config_path, "r");
if(!config_file) {
snip_log_fatal(SNIP_EXIT_ERROR_INVALID_CONFIG, "Could not read config file '%s'.", config->config_path);
return FALSE;
}
yaml_parser_t parser;
yaml_event_t event;
yaml_parser_initialize(&parser);
yaml_parser_set_input_file(&parser, config_file);
typedef enum snip_config_parse_state_e {
snip_config_parse_state_initial = 0,
snip_config_parse_state_root_map,
snip_config_parse_state_listener_bind_rvalue,
snip_config_parse_state_routes_rvalue,
snip_config_parse_state_user_rvalue,
snip_config_parse_state_group_rvalue,
snip_config_parse_state_disable_ipv4_rvalue,
snip_config_parse_state_disable_ipv6_rvalue,
snip_config_parse_state_default_route_rvalue,
snip_config_parse_state_no_sni_route_rvalue,
snip_config_parse_state_tls_error_route_rvalue,
snip_config_parse_state_http_fallback_route_rvalue,
snip_config_parse_state_proxy_connect_failure_route_rvalue,
snip_config_parse_state_routes_list,
snip_config_parse_state_route_verbose_map,
snip_config_parse_state_route_verbose_map_sni_hostname_rvalue,
snip_config_parse_state_route_verbose_target_rvalue,
snip_config_parse_state_route_verbose_action_rvalue,
snip_config_parse_state_route_verbose_send_file_rvalue,
snip_config_parse_state_route_verbose_send_text_rvalue,
snip_config_parse_state_routes_map,
snip_config_parse_state_routes_map_value,
snip_config_parse_state_skipping_unexpected_key_value,
// Right now we always treat these as fatal errors, but we might change the behavior for SIGHUP config reloads.
snip_config_parse_state_error,
snip_config_parse_state_listeners_rvalue,
snip_config_parse_state_listeners_in_list,
snip_config_parse_state_listener_item_map,
snip_config_parse_success
} snip_config_parse_state_t;
snip_config_parse_state_t state = snip_config_parse_state_initial;
snip_config_parse_state_t state_after_skip = snip_config_parse_state_initial;
snip_config_listener_list_t *current_listener_item = NULL;
snip_config_route_list_t *current_route_item = NULL;
snip_config_route_t *current_route = NULL;
// We skip unknown keys (though we do warn). If the associated value is a map or sequence we need to discard
// all children until we complete that value.
int current_depth = 0;
int skip_rvalue_depth = 0; // This gets set when we enter the skip state.
if(event.type == YAML_SEQUENCE_START_EVENT || event.type == YAML_MAPPING_START_EVENT) {
current_depth += 1;
}
else if (event.type == YAML_SEQUENCE_END_EVENT || event.type == YAML_MAPPING_END_EVENT) {
current_depth -= 1;
}
while(1) {
if (!yaml_parser_parse(&parser, &event)) {
snip_log_config(config, &event, SNIP_LOG_LEVEL_FATAL, "Error parsing ");
}
/*
switch(event.type) {
case YAML_NO_EVENT:
//printf("YAML_NO_EVENT\n");
break;
case YAML_STREAM_START_EVENT:
printf("YAML_STREAM_START_EVENT - %d\n", event.data.stream_start.encoding);
break;
case YAML_DOCUMENT_START_EVENT:
printf("YAML_DOCUMENT_START_EVENT\n");
break;
case YAML_ALIAS_EVENT:
printf("YAML_ALIAS_EVENT\n");
break;
case YAML_SCALAR_EVENT:
printf("YAML_SCALAR_EVENT");
if(event.data.scalar.tag) {
printf(" tag: '%s'", event.data.scalar.tag);
}
if(event.data.scalar.value) {
printf(" value: '%s'", event.data.scalar.value);
}
puts("");
break;
case YAML_SEQUENCE_START_EVENT:
printf("YAML_SEQUENCE_START_EVENT\n");
break;
case YAML_SEQUENCE_END_EVENT:
printf("YAML_SEQUENCE_END_EVENT\n");
break;
case YAML_MAPPING_START_EVENT:
printf("YAML_MAPPING_START_EVENT\n");
break;
case YAML_MAPPING_END_EVENT:
printf("YAML_MAPPING_END_EVENT\n");
break;
case YAML_DOCUMENT_END_EVENT:
printf("YAML_DOCUMENT_END_EVENT\n");
break;
case YAML_STREAM_END_EVENT:
printf("YAML_STREAM_END_EVENT\n");
break;
}
*/
if(event.type == YAML_STREAM_END_EVENT) {
if(state != snip_config_parse_success) {
snip_log_config(config, &event, SNIP_LOG_LEVEL_FATAL, "Unexpected end of configuration ");
}
break;
}
else if(state == snip_config_parse_state_initial)
{
// New document. Make sure it starts with a map, but otherwise, nothing fancy.
if(event.type == YAML_MAPPING_START_EVENT) {
state = snip_config_parse_state_root_map;
}
else if ((event.type == YAML_MAPPING_END_EVENT) ||
(event.type == YAML_SEQUENCE_START_EVENT) ||
(event.type == YAML_SEQUENCE_END_EVENT))
{
snip_log_config(config, &event, SNIP_LOG_LEVEL_FATAL, "The root element must be a key/value map ");
state = snip_config_parse_state_error;
}
}
else if(state == snip_config_parse_state_root_map) {
// We're parsing a new key in the root dictionary of the configuration file.
if(event.type == YAML_SCALAR_EVENT) {
if(!strcmp((const char *) event.data.scalar.value, "listeners")) {
state = snip_config_parse_state_listeners_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "routes")) {
state = snip_config_parse_state_routes_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "group")) {
state = snip_config_parse_state_group_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "user")) {
state = snip_config_parse_state_user_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "disable_ipv4")) {
state = snip_config_parse_state_disable_ipv4_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "disable_ipv6")) {
state = snip_config_parse_state_disable_ipv6_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "default_route")) {
state = snip_config_parse_state_default_route_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "no_sni_route")) {
state = snip_config_parse_state_no_sni_route_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "tls_error_route")) {
state = snip_config_parse_state_tls_error_route_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "http_fallback_route")) {
state = snip_config_parse_state_http_fallback_route_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "proxy_connect_failure")) {
state = snip_config_parse_state_proxy_connect_failure_route_rvalue;
}
else {
// We don't recognize this key. We log it as a warning, and make plans to skip the associated value.
state_after_skip = state;
state = snip_config_parse_state_skipping_unexpected_key_value;
skip_rvalue_depth = current_depth;
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_WARNING,
"Unexpected key '%s' ",
event.data.scalar.value
);
}
}
else if(event.type == YAML_MAPPING_END_EVENT) {
// Verify the config before we try to run it.
if(snip_validate_config_file(config)) {
state = snip_config_parse_success;
}
else {
state = snip_config_parse_state_error;
}
}
else if(event.type != YAML_NO_EVENT) {
snip_log_config(config, &event, SNIP_LOG_LEVEL_FATAL, "Key had unexpected type ");
state = snip_config_parse_state_error;
}
}
else if(state == snip_config_parse_state_listeners_rvalue) {
// We're in root dictionary, and we have the key "listeners". The value MUST be a list.
if(event.type == YAML_SEQUENCE_START_EVENT) {
state = snip_config_parse_state_listeners_in_list;
}
else if (event.type != YAML_NO_EVENT) {
snip_log_config(config, &event, SNIP_LOG_LEVEL_FATAL, "'listeners' section was not a list ");
state = snip_config_parse_state_error;
}
}
else if(state == snip_config_parse_state_listeners_in_list) {
// We're in the list of listeners, ready to start parsing the next listener. That listener must be a map.
if(event.type == YAML_MAPPING_START_EVENT) {
// Ok, time to create a new listener.
current_listener_item = snip_config_listener_list_create();
current_listener_item->value.config = config;
if(!config->listeners) {
config->listeners = current_listener_item;
}
else {
// We want the new listener at the end;
snip_config_listener_list_t *listener_item = config->listeners;
while(listener_item->next) {
listener_item = listener_item->next;
}
listener_item->next = current_listener_item;
}
state = snip_config_parse_state_listener_item_map;
}
else if(event.type == YAML_SEQUENCE_END_EVENT) {
current_listener_item = NULL;
state = snip_config_parse_state_root_map;
}
else if (event.type != YAML_NO_EVENT) {
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_FATAL,
"The 'listeners' section must be a list of key/value dictionaries ");
state = snip_config_parse_state_error;
}
}
else if(state == snip_config_parse_state_listener_item_map) {
// We are now parsing a specific listener, and are ready to examine a key.
if(event.type == YAML_SCALAR_EVENT) {
if(!strcmp((const char *) event.data.scalar.value, "routes")) {
state = snip_config_parse_state_routes_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "bind")) {
state = snip_config_parse_state_listener_bind_rvalue;
}
else {
// We don't recognize this key. We log it as a warning, and make plans to skip the associated value.
state_after_skip = state;
state = snip_config_parse_state_skipping_unexpected_key_value;
skip_rvalue_depth = current_depth;
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_WARNING,
"Unexpected key '%s' ",
event.data.scalar.value
);
}
}
else if(event.type == YAML_MAPPING_END_EVENT) {
// This listener is finishing. Evaluate it for completeness.
if(!current_listener_item->value.bind_address_string[0]) {
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_FATAL,
"'listener' items MUST include a valid 'bind' parameter ");
}
current_listener_item = NULL;
state = snip_config_parse_state_listeners_in_list;
}
else if (event.type != YAML_NO_EVENT) {
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_FATAL,
"The 'listeners' section must be a list of key/value dictionaries ");
state = snip_config_parse_state_error;
}
}
else if(state == snip_config_parse_state_user_rvalue) {
// The "routes" key is valid in both the root (as a global default) and within listener config objects.
// With the key, we now need to parse the value. We accept two formats: a list of dictionaries, and a
// shortcut dictionary format. We figure out which format we have here.
if(event.type == YAML_SCALAR_EVENT) {
if(get_uid_gid_for_username((const char *) event.data.scalar.value,
&config->user_id,
config->group_id == -1 ? &config->group_id : NULL)) // don't overwrite group
{
state = snip_config_parse_state_root_map;
}
else
{
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_FATAL,
"Unable to resolve uid for user '%s' specified in config 'user' value.",
event.data.scalar.value
);
state = snip_config_parse_state_error;
}
}
else if(event.type != YAML_NO_EVENT) {
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_FATAL,
"The 'user' property must be a string or number.");
state = snip_config_parse_state_error;
}
}
else if(state == snip_config_parse_state_group_rvalue) {
// The "routes" key is valid in both the root (as a global default) and within listener config objects.
// With the key, we now need to parse the value. We accept two formats: a list of dictionaries, and a
// shortcut dictionary format. We figure out which format we have here.
if(event.type == YAML_SCALAR_EVENT) {
if((config->group_id = get_gid_for_group_name((const char *) event.data.scalar.value)) >= 0)
{
state = snip_config_parse_state_root_map;
}
else
{
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_FATAL,
"Unable to resolve gid for group '%s' specified in config 'group' value.",
event.data.scalar.value
);
state = snip_config_parse_state_error;
}
}
else if(event.type != YAML_NO_EVENT) {
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_FATAL,
"The 'user' property must be a string or number.");
state = snip_config_parse_state_error;
}
}
else if(state == snip_config_parse_state_disable_ipv4_rvalue) {
if(event.type == YAML_SCALAR_EVENT && snip_parse_boolean(&event, &(config->disable_ipv4))) {
state = snip_config_parse_state_root_map;
}
else if(event.type != YAML_NO_EVENT) {
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_FATAL,
"The 'disable_ipv4' property must be a boolean.");
state = snip_config_parse_state_error;
}
}
else if(state == snip_config_parse_state_disable_ipv6_rvalue) {
if(event.type == YAML_SCALAR_EVENT && snip_parse_boolean(&event, &(config->disable_ipv6))) {
state = snip_config_parse_state_root_map;
}
else if(event.type != YAML_NO_EVENT) {
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_FATAL,
"The 'disable_ipv6' property must be a boolean.");
state = snip_config_parse_state_error;
}
}
else if(state == snip_config_parse_state_default_route_rvalue) {
if(event.type == YAML_MAPPING_START_EVENT) {
state = snip_config_parse_state_route_verbose_map;
current_route_item = NULL;
current_route = current_listener_item ?
¤t_listener_item->value.default_route :
&config->default_route;
}
else if(event.type != YAML_NO_EVENT) {
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_FATAL,
"The 'default_route' property must be a boolean.");
state = snip_config_parse_state_error;
}
}
else if(state == snip_config_parse_state_routes_rvalue) {
// The "routes" key is valid in both the root (as a global default) and within listener config objects.
// With the key, we now need to parse the value. We accept two formats: a list of dictionaries, and a
// shortcut dictionary format. We figure out which format we have here.
if(event.type == YAML_MAPPING_START_EVENT) {
state = snip_config_parse_state_routes_map;
}
else if(event.type == YAML_SEQUENCE_START_EVENT) {
state = snip_config_parse_state_routes_list;
}
else if(event.type != YAML_NO_EVENT) {
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_FATAL,
"The 'routes' section must be a list or dictionary ");
state = snip_config_parse_state_error;
}
}
else if(state == snip_config_parse_state_routes_map) {
// We're inside the value of a 'routes' configuration. These routes are defined with the shortcut
// dictionary format. The key is the pattern, the value the destination. We're going to examine the key,
// which should be the SNI-hostname-pattern we're trying to match. We might also get an event indicating
// the end of the list.
if(event.type == YAML_SCALAR_EVENT) {
state = snip_config_parse_state_routes_map_value;
current_route_item = snip_config_route_list_create();
current_route = ¤t_route_item->value;
current_route->action = snip_route_action_proxy;
// If we have a current_listener_item this route belongs to a listener, otherwise its global default.
snip_config_route_list_t **routes_first = current_listener_item ?
&(current_listener_item->value.routes) :
&(config->routes);
if(!(*(routes_first))) {
*(routes_first) = current_route_item;
}
else {
// skip to the end and add it there.
snip_config_route_list_t *route_item = *(routes_first);
while(route_item->next) {
route_item = route_item->next;
}
route_item->next = current_route_item;
}
current_route->sni_hostname = malloc(event.data.scalar.length + 1);
memset((void*) current_route->sni_hostname, '\0', event.data.scalar.length + 1);
memcpy((void*) current_route->sni_hostname, event.data.scalar.value, event.data.scalar.length);
}
else if(event.type == YAML_MAPPING_END_EVENT) {
// End of the dictionary.
current_route_item = NULL;
current_route = NULL;
state = current_listener_item ?
snip_config_parse_state_listener_item_map : snip_config_parse_state_root_map;
}
else if(event.type != YAML_NO_EVENT) {
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_FATAL,
"The 'routes' section must be a list or dictionary ");
state = snip_config_parse_state_error;
}
}
else if(state == snip_config_parse_state_routes_map_value) {
// We're in the shortcut dictionary version of a routes section. We have the SNI-hostname key, and are now
// looking at the value. It must be a string.
if(event.type == YAML_SCALAR_EVENT) {
state = snip_config_parse_state_routes_map;
snip_parse_target((const char *) event.data.scalar.value,
&(current_route->dest_hostname),
&(current_route->port));
}
else if(event.type != YAML_NO_EVENT) {
snip_log_config(
config,
&event,
SNIP_LOG_LEVEL_FATAL,
"The 'routes' section must either be a string:string dictionary, or a list of dictionaries ");
state = snip_config_parse_state_error;
}
}
else if(state == snip_config_parse_state_routes_list) {
// We're currently parsing a 'routes' section which is defined in the more verbose list-of-dictionaries
// format. We expect to find either the start of a new dictionary, or an end-of-list event.
if(event.type == YAML_MAPPING_START_EVENT) {
state = snip_config_parse_state_route_verbose_map;
current_route_item = snip_config_route_list_create();
current_route = ¤t_route_item->value;
current_route->action = snip_route_action_proxy;
// If we have a current_listener_item this route belongs to a listener, otherwise its global default.
snip_config_route_list_t **routes_first = current_listener_item ?
&(current_listener_item->value.routes) :
&(config->routes);
if(!(*(routes_first))) {
*(routes_first) = current_route_item;
}
else {
// skip to the end and add it there.
snip_config_route_list_t *route_item = *(routes_first);
while(route_item->next) {
route_item = route_item->next;
}
route_item->next = current_route_item;
}
}
else if(event.type == YAML_SEQUENCE_END_EVENT) {
// End of the list. If we were examining a listener, go back to parsing it, otherwise goto the root.
state = current_listener_item ?
snip_config_parse_state_listener_item_map : snip_config_parse_state_root_map;
}
else if(event.type != YAML_NO_EVENT) {
snip_log_config(
config,
&event,
SNIP_LOG_LEVEL_FATAL,
"The 'routes' section must either be a string:string dictionary, or a list of dictionaries ");
state = snip_config_parse_state_error;
}
}
else if(state == snip_config_parse_state_route_verbose_map) {
// We're inside a route-definition that uses the more verbose list-of-dictionary format. We're in that
// dictionary looking at the key.
if(event.type == YAML_SCALAR_EVENT) {
if(!strcmp((const char *) event.data.scalar.value, "sni_hostname")) {
state = snip_config_parse_state_route_verbose_map_sni_hostname_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "target")) {
state = snip_config_parse_state_route_verbose_target_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "action")) {
state = snip_config_parse_state_route_verbose_action_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "send_file")) {
state = snip_config_parse_state_route_verbose_send_file_rvalue;
}
else if(!strcmp((const char *) event.data.scalar.value, "send_text")) {
state = snip_config_parse_state_route_verbose_send_text_rvalue;
}
else {
// We don't recognize this key. We log it as a warning, and make plans to skip the associated value.
state_after_skip = state;
state = snip_config_parse_state_skipping_unexpected_key_value;
skip_rvalue_depth = current_depth;
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_WARNING,
"Unexpected key '%s' ",
event.data.scalar.value
);
}
}
else if(event.type == YAML_MAPPING_END_EVENT) {
if(current_route->action == snip_route_action_undefined) {
if(current_route->dest_hostname) {
current_route->action = snip_route_action_proxy;
}
else {
current_route->action = snip_route_action_hangup;
}
}
if(current_route->action == snip_route_action_proxy && !current_route->dest_hostname) {
snip_log_config(config,
&event,
SNIP_LOG_LEVEL_FATAL,
"Route items with 'action'='proxy' must specify a 'target' parameter ");
}
if(current_route_item) {
state = snip_config_parse_state_routes_list;
}
else if(current_listener_item) {
state = snip_config_parse_state_listener_item_map;
}
else {
if(current_route->sni_hostname) {
snip_log_config(
config,
&event,
SNIP_LOG_LEVEL_FATAL,
"Exception routes (default_route, no_sni_route, etc.) must NOT include the "
"\"sni_hostname\" property ");
}
state = snip_config_parse_state_root_map;
}
}
else if(event.type != YAML_NO_EVENT) {
snip_log_config(config, &event, SNIP_LOG_LEVEL_FATAL, "Key had unexpected type ");
state = snip_config_parse_state_error;
}
}
else if (state == snip_config_parse_state_route_verbose_map_sni_hostname_rvalue) {
if(event.type == YAML_SCALAR_EVENT) {
current_route->sni_hostname = malloc(event.data.scalar.length + 1);
memset((void*) current_route->sni_hostname, '\0', event.data.scalar.length + 1);