forked from bumptech/stud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.c
1285 lines (1131 loc) · 36.8 KB
/
configuration.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
/**
* configuration.c
*
* Author: Brane F. Gracnar
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <getopt.h>
#include <libgen.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
#include <syslog.h>
#include "configuration.h"
#include "version.h"
#define ADDR_LEN 150
#define PORT_LEN 6
#define CFG_BOOL_ON "on"
// BEGIN: configuration parameters
#define CFG_CIPHERS "ciphers"
#define CFG_SSL_ENGINE "ssl-engine"
#define CFG_PREFER_SERVER_CIPHERS "prefer-server-ciphers"
#define CFG_BACKEND "backend"
#define CFG_FRONTEND "frontend"
#define CFG_WORKERS "workers"
#define CFG_BACKLOG "backlog"
#define CFG_KEEPALIVE "keepalive"
#define CFG_CHROOT "chroot"
#define CFG_USER "user"
#define CFG_GROUP "group"
#define CFG_QUIET "quiet"
#define CFG_SYSLOG "syslog"
#define CFG_SYSLOG_FACILITY "syslog-facility"
#define CFG_PARAM_SYSLOG_FACILITY 11015
#define CFG_DAEMON "daemon"
#define CFG_WRITE_IP "write-ip"
#define CFG_WRITE_PROXY "write-proxy"
#define CFG_PEM_FILE "pem-file"
#ifdef USE_SHARED_CACHE
#define CFG_SHARED_CACHE "shared-cache"
#define CFG_SHARED_CACHE_LISTEN "shared-cache-listen"
#define CFG_SHARED_CACHE_PEER "shared-cache-peer"
#define CFG_SHARED_CACHE_MCASTIF "shared-cache-if"
#endif
#ifndef NO_CONFIG_FILE
#define FMT_STR "%s = %s\n"
#define FMT_QSTR "%s = \"%s\"\n"
#define FMT_ISTR "%s = %d\n"
#define CONFIG_MAX_LINES 10000
#define CONFIG_BUF_SIZE 1024
#define CFG_PARAM_CFGFILE 10000
#define CFG_PARAM_DEFCFG 10001
#define CFG_CONFIG "config"
#define CFG_CONFIG_DEFAULT "default-config"
#endif
// END: configuration parameters
static char var_buf[CONFIG_BUF_SIZE];
static char val_buf[CONFIG_BUF_SIZE];
static char error_buf[CONFIG_BUF_SIZE];
static char tmp_buf[150];
// for testing configuration only
#include <openssl/ssl.h>
SSL_CTX * init_openssl();
static void config_error_set (char *fmt, ...) {
memset(error_buf, '\0', sizeof(error_buf));
va_list args;
va_start(args, fmt);
vsnprintf(error_buf, (sizeof(error_buf) - 1), fmt, args);
va_end(args);
}
char * config_error_get (void) {
return error_buf;
}
void config_die (char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
exit(1);
}
stud_config * config_new (void) {
stud_config *r = NULL;
r = malloc(sizeof(stud_config));
if (r == NULL) {
config_error_set("Unable to allocate memory for configuration structure: %s", strerror(errno));
return NULL;
}
// set default values
r->ETYPE = ENC_TLS;
r->PMODE = SSL_SERVER;
r->WRITE_IP_OCTET = 0;
r->WRITE_PROXY_LINE = 0;
r->CHROOT = NULL;
r->UID = 0;
r->GID = 0;
r->FRONT_IP = NULL;
r->FRONT_PORT = strdup("8443");
r->BACK_IP = strdup("127.0.0.1");
r->BACK_PORT = strdup("8000");
r->NCORES = 1;
r->CERT_FILE = NULL;
r->CIPHER_SUITE = NULL;
r->ENGINE = NULL;
r->BACKLOG = 100;
#ifdef USE_SHARED_CACHE
r->SHARED_CACHE = 0;
r->SHCUPD_IP = NULL;
r->SHCUPD_PORT = NULL;
for (int i = 0 ; i < MAX_SHCUPD_PEERS; i++)
memset(&r->SHCUPD_PEERS[i], 0, sizeof(shcupd_peer_opt));
r->SHCUPD_MCASTIF = NULL;
r->SHCUPD_MCASTTTL = NULL;
#endif
r->QUIET = 0;
r->SYSLOG = 0;
r->SYSLOG_FACILITY = LOG_DAEMON;
r->TCP_KEEPALIVE_TIME = 3600;
r->DAEMONIZE = 0;
r->PREFER_SERVER_CIPHERS = 0;
return r;
}
void config_destroy (stud_config *cfg) {
// printf("config_destroy() in pid %d: %p\n", getpid(), cfg);
if (cfg == NULL) return;
// free all members!
if (cfg->CHROOT != NULL) free(cfg->CHROOT);
if (cfg->FRONT_IP != NULL) free(cfg->FRONT_IP);
if (cfg->FRONT_PORT != NULL) free(cfg->FRONT_PORT);
if (cfg->BACK_IP != NULL) free(cfg->BACK_IP);
if (cfg->BACK_PORT != NULL) free(cfg->BACK_PORT);
if (cfg->CERT_FILE != NULL) free(cfg->CERT_FILE);
if (cfg->CIPHER_SUITE != NULL) free(cfg->CIPHER_SUITE);
if (cfg->ENGINE != NULL) free(cfg->ENGINE);
#ifdef USE_SHARED_CACHE
if (cfg->SHCUPD_IP != NULL) free(cfg->SHCUPD_IP);
if (cfg->SHCUPD_PORT != NULL) free(cfg->SHCUPD_PORT);
for (int i = 0; i < MAX_SHCUPD_PEERS; i++) {
if (cfg->SHCUPD_PEERS[i].ip != NULL)
free(cfg->SHCUPD_PEERS[i].ip);
if (cfg->SHCUPD_PEERS[i].port != NULL)
free(cfg->SHCUPD_PEERS[i].port);
}
if (cfg->SHCUPD_MCASTIF != NULL) free(cfg->SHCUPD_MCASTIF);
if (cfg->SHCUPD_MCASTTTL != NULL) free(cfg->SHCUPD_MCASTTTL);
#endif
free(cfg);
}
char * config_get_param (char *str) {
char *ptr;
int i;
if (str == NULL) return NULL;
/** empty string? */
if (strlen(str) < 1 || str[0] == '\n' || strcmp(str, "\r\n") == 0) return NULL;
ptr = str;
/** comments? */
if (str[0] == '#') return NULL;
/** first alpha character */
while (ptr != NULL && ! isalpha(*ptr))
ptr++;
/** overwrite alpha chars */
memset(var_buf, '\0', sizeof(var_buf));
i = 0;
while(ptr != NULL && (isalnum(*ptr) || *ptr == '-')) {
var_buf[i] = *ptr;
i++;
ptr++;
}
if (strlen(var_buf) < 1) return NULL;
return var_buf;
}
char * config_get_value (char *str) {
char *ptr;
int i = 0;
if (str == NULL) return NULL;
if (strlen(str) < 1) return NULL;
/** find '=' char */
ptr = str;
while (ptr != NULL && (*ptr) != '=')
ptr++;
ptr++;
/** skip whitespaces **/
while (ptr != NULL && ! isgraph(*ptr))
ptr++;
/** no value found? */
if (ptr == NULL) return NULL;
/** overwrite alpha chars */
memset(val_buf, '\0', sizeof(val_buf));
while(ptr != NULL && isgraph(*ptr)) {
val_buf[i++] = *ptr;
ptr++;
}
if (strlen(val_buf) < 1) return NULL;
return val_buf;
}
char * str_rtrim(char *str) {
char *ptr;
int len;
len = strlen(str);
ptr = str + len - 1;
while (ptr >= str && (isspace((int)*ptr ) || (char) *ptr == '"' || (char) *ptr == '\'')) --ptr;
ptr[1] = '\0';
return str;
}
char * str_ltrim(char *str) {
char *ptr;
int len;
for (ptr = str; (*ptr && (isspace((int)*ptr) || (char) *ptr == '"' || (char) *ptr == '\'')); ++ptr);
len = strlen(ptr);
memmove(str, ptr, len + 1);
return str;
}
char * str_trim(char *str) {
char *ptr;
ptr = str_rtrim(str);
str = str_ltrim(ptr);
return str;
}
char * config_assign_str (char **dst, char *v) {
if (*dst == NULL) {
if (v != NULL && strlen(v) > 0)
*dst = strdup(v);
} else {
if (v != NULL && strlen(v) > 0) {
// we assume that there is enough room for v in *dst
memset(*dst, '\0', strlen(v) + 1);
memcpy(*dst, v, strlen(v));
}
else
free(*dst);
}
return *dst;
}
int config_param_val_bool (char *val, int *res) {
if (val == NULL) return 0;
if (
strcasecmp(val, CFG_BOOL_ON) == 0 ||
strcasecmp(val, "yes") == 0 ||
strcasecmp(val, "y") == 0 ||
strcasecmp(val, "true") == 0 ||
strcasecmp(val, "t") == 0 ||
strcasecmp(val, "1") == 0) {
*res = 1;
}
return 1;
}
char * config_param_val_str (char *val) {
return strdup(val);
}
int config_param_host_port_wildcard (char *str, char **addr, char **port, int wildcard_okay) {
int len = (str != NULL) ? strlen(str) : 0;
if (str == NULL || ! len) {
config_error_set("Invalid/unset host/port string.");
return 0;
}
// address/port buffers
char port_buf[PORT_LEN];
char addr_buf[ADDR_LEN];
memset(port_buf, '\0', sizeof(port_buf));
memset(addr_buf, '\0', sizeof(addr_buf));
// NEW FORMAT: [address]:port
if (*str == '[') {
char *ptr = str + 1;
char *x = strrchr(ptr, ']');
if (x == NULL) {
config_error_set("Invalid address '%s'.", str);
return 0;
}
// address
memcpy(addr_buf, ptr, (x - ptr));
// port
x += 2;
memcpy(port_buf, x, sizeof(port_buf) - 1);
}
// OLD FORMAT: address,port
else {
char *x = strrchr(str, ',');
if (x == NULL) {
config_error_set("Invalid address string '%s'", str);
return 0;
}
// addr
int addr_len = x - str;
memcpy(addr_buf, str, addr_len);
// port
memcpy(port_buf, (++x), sizeof(port_buf));
}
// printf("PARSED ADDR '%s', PORT '%s'\n", addr_buf, port_buf);
// check port
int p = atoi(port_buf);
if (p < 1 || p > 65536) {
config_error_set("Invalid port number '%s'", port_buf);
return 0;
}
// write
if (strcmp(addr_buf, "*") == 0) {
if (wildcard_okay)
free(*addr);
else {
config_error_set("Invalid address: wildcards are not allowed.");
return 0;
}
} else {
//if (*addr != NULL) free(*addr);
*addr = strdup(addr_buf);
}
// if (**port != NULL) free(*port);
*port = strdup(port_buf);
// printf("ADDR FINAL: '%s', '%s'\n", *addr, *port);
return 1;
}
int config_param_host_port (char *str, char **addr, char **port) {
return config_param_host_port_wildcard(str, addr, port, 0);
}
int config_param_val_int (char *str, int *dst) {
*dst = (str != NULL) ? atoi(str) : 0;
return 1;
}
int config_param_val_int_pos (char *str, int *dst) {
int num = 0;
if (str != NULL)
num = atoi(str);
if (num < 1) {
config_error_set("Not a positive number.");
return 0;
}
*dst = num;
return 1;
}
int config_param_val_intl (char *str, long int *dst) {
*dst = (str != NULL) ? atol(str) : 0;
return 1;
}
int config_param_val_intl_pos (char *str, long int *dst) {
long int num = 0;
if (str != NULL)
num = atol(str);
if (num < 1) {
config_error_set("Not a positive number.");
return 0;
}
*dst = num;
return 1;
}
#ifdef USE_SHARED_CACHE
/* Parse mcast and ttl options */
int config_param_shcupd_mcastif (char *str, char **iface, char **ttl) {
char buf[150];
char *sp;
if (strlen(str) >= sizeof buf) {
config_error_set("Invalid option for IFACE[,TTL]");
return 0;
}
sp = strchr(str, ',');
if (!sp) {
if (!strcmp(str, "*"))
*iface = NULL;
else
*iface = str;
*ttl = NULL;
return 1;
}
else if (!strncmp(str, "*", sp - str)) {
*iface = NULL;
}
else {
*sp = 0;
*iface = str;
}
*ttl = sp + 1;
return 1;
}
int config_param_shcupd_peer (char *str, stud_config *cfg) {
if (cfg == NULL) {
config_error_set("Configuration pointer is NULL.");
return 0;
}
// parse result
int r = 1;
// find place for new peer
int offset = 0;
int i = 0;
for (i = 0; i < MAX_SHCUPD_PEERS; i++) {
if (cfg->SHCUPD_PEERS[i].ip == NULL && cfg->SHCUPD_PEERS[i].port == NULL) {
offset = i;
break;
}
}
if (i >= MAX_SHCUPD_PEERS) {
config_error_set(
"Reached maximum number of shared cache update peers (%d).",
MAX_SHCUPD_PEERS
);
return 0;
}
// create place for new peer
char *addr = malloc(ADDR_LEN);
if (addr == NULL) {
config_error_set(
"Unable to allocate memory for new shared cache update peer address: %s",
strerror(errno)
);
r = 0;
goto outta_parse_peer;
}
memset(addr, '\0', ADDR_LEN);
char *port = malloc(PORT_LEN);
if (port == NULL) {
config_error_set(
"Unable to allocate memory for new shared cache update peer port: %s",
strerror(errno)
);
r = 0;
goto outta_parse_peer;
}
memset(port, '\0', PORT_LEN);
// try to parse address
if (! config_param_host_port(str, &addr, &port)) {
r = 0;
goto outta_parse_peer;
}
outta_parse_peer:
if (! r) {
if (addr != NULL) free(addr);
if (port != NULL) free(port);
} else {
cfg->SHCUPD_PEERS[offset].ip = addr;
cfg->SHCUPD_PEERS[offset].port = port;
}
return r;
}
#endif /* USE_SHARED_CACHE */
void config_param_validate (char *k, char *v, stud_config *cfg, char *file, int line) {
int r = 1;
struct stat st;
if (strcmp(k, "tls") == 0) {
//cfg->ENC_TLS = 1;
}
else if (strcmp(k, "ssl") == 0) {
//cfg->ENC_TLS = 0;
}
else if (strcmp(k, CFG_CIPHERS) == 0) {
if (v != NULL && strlen(v) > 0) {
config_assign_str(&cfg->CIPHER_SUITE, v);
}
}
else if (strcmp(k, CFG_SSL_ENGINE) == 0) {
if (v != NULL && strlen(v) > 0) {
config_assign_str(&cfg->ENGINE, v);
}
}
else if (strcmp(k, CFG_PREFER_SERVER_CIPHERS) == 0) {
r = config_param_val_bool(v, &cfg->PREFER_SERVER_CIPHERS);
}
else if (strcmp(k, CFG_FRONTEND) == 0) {
r = config_param_host_port_wildcard(v, &cfg->FRONT_IP, &cfg->FRONT_PORT, 1);
}
else if (strcmp(k, CFG_BACKEND) == 0) {
r = config_param_host_port(v, &cfg->BACK_IP, &cfg->BACK_PORT);
}
else if (strcmp(k, CFG_WORKERS) == 0) {
r = config_param_val_intl_pos(v, &cfg->NCORES);
}
else if (strcmp(k, CFG_BACKLOG) == 0) {
r = config_param_val_int(v, &cfg->BACKLOG);
if (r && cfg->BACKLOG < -1) cfg->BACKLOG = -1;
}
else if (strcmp(k, CFG_KEEPALIVE) == 0) {
r = config_param_val_int_pos(v, &cfg->TCP_KEEPALIVE_TIME);
}
#ifdef USE_SHARED_CACHE
else if (strcmp(k, CFG_SHARED_CACHE) == 0) {
r = config_param_val_int(v, &cfg->SHARED_CACHE);
}
else if (strcmp(k, CFG_SHARED_CACHE_LISTEN) == 0) {
if (v != NULL && strlen(v) > 0)
r = config_param_host_port_wildcard(v, &cfg->SHCUPD_IP, &cfg->SHCUPD_PORT, 1);
}
else if (strcmp(k, CFG_SHARED_CACHE_PEER) == 0) {
r = config_param_shcupd_peer(v, cfg);
}
else if (strcmp(k, CFG_SHARED_CACHE_MCASTIF) == 0) {
r = config_param_shcupd_mcastif(v, &cfg->SHCUPD_MCASTIF, &cfg->SHCUPD_MCASTTTL);
}
#endif
else if (strcmp(k, CFG_CHROOT) == 0) {
if (v != NULL && strlen(v) > 0) {
// check directory
if (stat(v, &st) != 0) {
config_error_set("Unable to stat directory '%s': %s'.", v, strerror(errno));
r = 0;
} else {
if (! S_ISDIR(st.st_mode)) {
config_error_set("Bad chroot directory '%s': Not a directory.", v, strerror(errno));
r = 0;
} else {
config_assign_str(&cfg->CHROOT, v);
}
}
}
}
else if (strcmp(k, CFG_USER) == 0) {
if (v != NULL && strlen(v) > 0) {
struct passwd *passwd;
passwd = getpwnam(v);
if (!passwd) {
config_error_set("Invalid user '%s'.", v);
r = 0;
} else {
cfg->UID = passwd->pw_uid;
cfg->GID = passwd->pw_gid;
}
}
}
else if (strcmp(k, CFG_GROUP) == 0) {
if (v != NULL && strlen(v) > 0) {
struct group *grp;
grp = getgrnam(v);
if (!grp) {
config_error_set("Invalid group '%s'.", v);
r = 0;
} else {
cfg->GID = grp->gr_gid;
}
}
}
else if (strcmp(k, CFG_QUIET) == 0) {
r = config_param_val_bool(v, &cfg->QUIET);
}
else if (strcmp(k, CFG_SYSLOG) == 0) {
r = config_param_val_bool(v, &cfg->SYSLOG);
}
else if (strcmp(k, CFG_SYSLOG_FACILITY) == 0) {
r = 1;
if (!strcmp(v, "auth") || !strcmp(v, "authpriv"))
cfg->SYSLOG_FACILITY = LOG_AUTHPRIV;
else if (!strcmp(v, "cron"))
cfg->SYSLOG_FACILITY = LOG_CRON;
else if (!strcmp(v, "daemon"))
cfg->SYSLOG_FACILITY = LOG_DAEMON;
else if (!strcmp(v, "ftp"))
cfg->SYSLOG_FACILITY = LOG_FTP;
else if (!strcmp(v, "local0"))
cfg->SYSLOG_FACILITY = LOG_LOCAL0;
else if (!strcmp(v, "local1"))
cfg->SYSLOG_FACILITY = LOG_LOCAL1;
else if (!strcmp(v, "local2"))
cfg->SYSLOG_FACILITY = LOG_LOCAL2;
else if (!strcmp(v, "local3"))
cfg->SYSLOG_FACILITY = LOG_LOCAL3;
else if (!strcmp(v, "local4"))
cfg->SYSLOG_FACILITY = LOG_LOCAL4;
else if (!strcmp(v, "local5"))
cfg->SYSLOG_FACILITY = LOG_LOCAL5;
else if (!strcmp(v, "local6"))
cfg->SYSLOG_FACILITY = LOG_LOCAL6;
else if (!strcmp(v, "local7"))
cfg->SYSLOG_FACILITY = LOG_LOCAL7;
else if (!strcmp(v, "lpr"))
cfg->SYSLOG_FACILITY = LOG_LPR;
else if (!strcmp(v, "mail"))
cfg->SYSLOG_FACILITY = LOG_MAIL;
else if (!strcmp(v, "news"))
cfg->SYSLOG_FACILITY = LOG_NEWS;
else if (!strcmp(v, "user"))
cfg->SYSLOG_FACILITY = LOG_USER;
else if (!strcmp(v, "uucp"))
cfg->SYSLOG_FACILITY = LOG_UUCP;
else {
config_error_set("Invalid facility '%s'.", v);
r = 0;
}
}
else if (strcmp(k, CFG_DAEMON) == 0) {
r = config_param_val_bool(v, &cfg->DAEMONIZE);
}
else if (strcmp(k, CFG_WRITE_IP) == 0) {
r = config_param_val_bool(v, &cfg->WRITE_IP_OCTET);
}
else if (strcmp(k, CFG_WRITE_PROXY) == 0) {
r = config_param_val_bool(v, &cfg->WRITE_PROXY_LINE);
}
else if (strcmp(k, CFG_PEM_FILE) == 0) {
if (v != NULL && strlen(v) > 0) {
if (stat(v, &st) != 0) {
config_error_set("Unable to stat x509 certificate PEM file '%s': ", v, strerror(errno));
r = 0;
}
else if (! S_ISREG(st.st_mode)) {
config_error_set("Invalid x509 certificate PEM file '%s': Not a file.", v);
r = 0;
} else
config_assign_str(&cfg->CERT_FILE, v);
}
}
else {
fprintf(
stderr,
"Ignoring unknown configuration key '%s' in configuration file '%s', line %d\n",
k, file, line
);
}
if (! r) {
if (file != NULL)
config_die("Error in configuration file '%s', line %d: %s\n", file, line, config_error_get());
else
config_die("Invalid parameter '%s': %s", k, config_error_get());
}
}
#ifndef NO_CONFIG_FILE
int config_file_parse (char *file, stud_config *cfg) {
if (cfg == NULL)
config_die("Undefined stud options; THIS IS A BUG!\n");
char line[CONFIG_BUF_SIZE];
FILE *fd = NULL;
// should we read stdin?
if (file == NULL || strlen(file) < 1 || strcmp(file, "-") == 0) {
fd = stdin;
} else {
fd = fopen(file, "r");
}
if (fd == NULL)
config_die("Unable to open configuration file '%s': %s\n", file, strerror(errno));
// read config
int i = 0;
while (i < CONFIG_MAX_LINES) {
memset(line, '\0', sizeof(line));
if (fgets(line, (sizeof(line) - 1), fd) == NULL) break;
i++;
// get configuration key
char *key, *val;
key = config_get_param(line);
if (key == NULL) continue;
// get configuration key value...
val = config_get_value(line);
if (val == NULL) continue;
str_trim(val);
// printf("File '%s', line %d, key: '%s', value: '%s'\n", file, i, key, val);
// validate configuration key => value
config_param_validate(key, val, cfg, file, i);
}
fclose(fd);
return 1;
}
#endif /* NO_CONFIG_FILE */
char * config_disp_str (char *str) {
return (str == NULL) ? "" : str;
}
char * config_disp_bool (int v) {
return (v > 0) ? CFG_BOOL_ON : "off";
}
char * config_disp_uid (uid_t uid) {
memset(tmp_buf, '\0', sizeof(tmp_buf));
if (uid == 0 && geteuid() != 0) return tmp_buf;
struct passwd *pw = getpwuid(uid);
if (pw) {
memcpy(tmp_buf, pw->pw_name, strlen(pw->pw_name));
}
return tmp_buf;
}
char * config_disp_gid (gid_t gid) {
memset(tmp_buf, '\0', sizeof(tmp_buf));
if (gid == 0 && geteuid() != 0) return tmp_buf;
struct group *gr = getgrgid(gid);
if (gr) {
memcpy(tmp_buf, gr->gr_name, strlen(gr->gr_name));
}
return tmp_buf;
}
char * config_disp_hostport (char *host, char *port) {
memset(tmp_buf, '\0', sizeof(tmp_buf));
if (host == NULL && port == NULL)
return "";
strcat(tmp_buf, "[");
if (host == NULL)
strcat(tmp_buf, "*");
else {
strncat(tmp_buf, host, 40);
}
strcat(tmp_buf, "]:");
strncat(tmp_buf, port, 5);
return tmp_buf;
}
const char * config_disp_log_facility (int facility) {
switch (facility)
{
case LOG_AUTHPRIV:
return "authpriv";
case LOG_CRON:
return "cron";
case LOG_DAEMON:
return "daemon";
case LOG_FTP:
return "ftp";
case LOG_LOCAL0:
return "local0";
case LOG_LOCAL1:
return "local1";
case LOG_LOCAL2:
return "local2";
case LOG_LOCAL3:
return "local3";
case LOG_LOCAL4:
return "local4";
case LOG_LOCAL5:
return "local5";
case LOG_LOCAL6:
return "local6";
case LOG_LOCAL7:
return "local7";
case LOG_LPR:
return "lpr";
case LOG_MAIL:
return "mail";
case LOG_NEWS:
return "news";
case LOG_USER:
return "user";
case LOG_UUCP:
return "uucp";
default:
return "UNKNOWN";
}
}
void config_print_usage_fd (char *prog, stud_config *cfg, FILE *out) {
if (out == NULL) out = stderr;
fprintf(out, "Usage: %s [OPTIONS] PEM\n\n", basename(prog));
fprintf(out, "This is stud, The Scalable TLS Unwrapping Daemon.\n\n");
#ifndef NO_CONFIG_FILE
fprintf(out, "CONFIGURATION:\n");
fprintf(out, "\n");
fprintf(out, " --config=FILE Load configuration from specified file.\n");
fprintf(out, " --default-config Prints default configuration to stdout.\n");
fprintf(out, "\n");
#endif
fprintf(out, "ENCRYPTION METHODS:\n");
fprintf(out, "\n");
fprintf(out, " --tls TLSv1 (default)\n");
fprintf(out, " --ssl SSLv3 (implies no TLSv1)\n");
fprintf(out, " -c --ciphers=SUITE Sets allowed ciphers (Default: \"%s\")\n", config_disp_str(cfg->CIPHER_SUITE));
fprintf(out, " -e --ssl-engine=NAME Sets OpenSSL engine (Default: \"%s\")\n", config_disp_str(cfg->ENGINE));
fprintf(out, " -O --prefer-server-ciphers Prefer server list order\n");
fprintf(out, "\n");
fprintf(out, "SOCKET:\n");
fprintf(out, "\n");
fprintf(out, " --client Enable client proxy mode\n");
fprintf(out, " -b --backend=HOST,PORT Backend [connect] (default is \"%s\")\n", config_disp_hostport(cfg->BACK_IP, cfg->BACK_PORT));
fprintf(out, " -f --frontend=HOST,PORT Frontend [bind] (default is \"%s\")\n", config_disp_hostport(cfg->FRONT_IP, cfg->FRONT_PORT));
#ifdef USE_SHARED_CACHE
fprintf(out, "\n");
fprintf(out, " -U --shared-cache-listen=HOST,PORT\n");
fprintf(out, " Accept cache updates on UDP (Default: \"%s\")\n", config_disp_hostport(cfg->SHCUPD_IP, cfg->SHCUPD_PORT));
fprintf(out, " NOTE: This option requires enabled SSL session cache.\n");
fprintf(out, " -P --shared-cache-peer=HOST,PORT\n");
fprintf(out, " Send cache updates to specified peer\n");
fprintf(out, " NOTE: This option can be specified multiple times.\n");
fprintf(out, " -M --shared-cache-if=IFACE[,TTL]\n");
fprintf(out, " Force iface and ttl to receive and send multicast updates\n");
#endif
fprintf(out, "\n");
fprintf(out, "PERFORMANCE:\n");
fprintf(out, "\n");
fprintf(out, " -n --workers=NUM Number of worker processes (Default: %ld)\n", cfg->NCORES);
fprintf(out, " -B --backlog=NUM Set listen backlog size (Default: %d)\n", cfg->BACKLOG);
fprintf(out, " -k --keepalive=SECS TCP keepalive on client socket (Default: %d)\n", cfg->TCP_KEEPALIVE_TIME);
#ifdef USE_SHARED_CACHE
fprintf(out, " -C --session-cache=NUM Enable and set SSL session cache to specified number\n");
fprintf(out, " of sessions (Default: %d)\n", cfg->SHARED_CACHE);
#endif
fprintf(out, "\n");
fprintf(out, "SECURITY:\n");
fprintf(out, "\n");
fprintf(out, " -r --chroot=DIR Sets chroot directory (Default: \"%s\")\n", config_disp_str(cfg->CHROOT));
fprintf(out, " -u --user=USER Set uid/gid after binding the socket (Default: \"%s\")\n", config_disp_uid(cfg->UID));
fprintf(out, " -g --group=GROUP Set gid after binding the socket (Default: \"%s\")\n", config_disp_gid(cfg->GID));
fprintf(out, "\n");
fprintf(out, "LOGGING:\n");
fprintf(out, " -q --quiet Be quiet; emit only error messages\n");
fprintf(out, " -s --syslog Send log message to syslog in addition to stderr/stdout\n");
fprintf(out, " --syslog-facility=FACILITY Syslog facility to use (Default: \"%s\")\n", config_disp_log_facility(cfg->SYSLOG_FACILITY));
fprintf(out, "\n");
fprintf(out, "OTHER OPTIONS:\n");
fprintf(out, " --daemon Fork into background and become a daemon (Default: %s)\n", config_disp_bool(cfg->DAEMONIZE));
fprintf(out, " --write-ip Write 1 octet with the IP family followed by the IP\n");
fprintf(out, " address in 4 (IPv4) or 16 (IPv6) octets little-endian\n");
fprintf(out, " to backend before the actual data\n");
fprintf(out, " (Default: %s)\n", config_disp_bool(cfg->WRITE_IP_OCTET));
fprintf(out, " --write-proxy Write HaProxy's PROXY (IPv4 or IPv6) protocol line\n" );
fprintf(out, " before actual data\n");
fprintf(out, " (Default: %s)\n", config_disp_bool(cfg->WRITE_PROXY_LINE));
fprintf(out, "\n");
fprintf(out, " -t --test Test configuration and exit\n");
fprintf(out, " -V --version Print program version and exit\n");
fprintf(out, " -h --help This help message\n");
}
#ifndef NO_CONFIG_FILE
void config_print_default (FILE *fd, stud_config *cfg) {
if (fd == NULL) return;
fprintf(fd, "#\n");
fprintf(fd, "# stud(8), The Scalable TLS Unwrapping Daemon's configuration\n");
fprintf(fd, "#\n");
fprintf(fd, "\n");
fprintf(fd, "# NOTE: all config file parameters can be overriden\n");
fprintf(fd, "# from command line!\n");
fprintf(fd, "\n");
fprintf(fd, "# Listening address. REQUIRED.\n");
fprintf(fd, "#\n");
fprintf(fd, "# type: string\n");
fprintf(fd, "# syntax: [HOST]:PORT\n");
fprintf(fd, FMT_QSTR, CFG_FRONTEND, config_disp_hostport(cfg->FRONT_IP, cfg->FRONT_PORT));
fprintf(fd, "\n");
fprintf(fd, "# Upstream server address. REQUIRED.\n");
fprintf(fd, "#\n");
fprintf(fd, "# type: string\n");
fprintf(fd, "# syntax: [HOST]:PORT.\n");
fprintf(fd, FMT_QSTR, CFG_BACKEND, config_disp_hostport(cfg->BACK_IP, cfg->BACK_PORT));
fprintf(fd, "\n");
fprintf(fd, "# SSL x509 certificate file. REQUIRED.\n");
fprintf(fd, "#\n");
fprintf(fd, "# type: string\n");
fprintf(fd, FMT_QSTR, CFG_PEM_FILE, config_disp_str(cfg->CERT_FILE));
fprintf(fd, "\n");
fprintf(fd, "# SSL protocol.\n");
fprintf(fd, "#\n");
fprintf(fd, "# tls = on\n");
fprintf(fd, "# ssl = off\n");
fprintf(fd, "\n");
fprintf(fd, "# List of allowed SSL ciphers.\n");
fprintf(fd, "#\n");
fprintf(fd, "# Run openssl ciphers for list of available ciphers.\n");
fprintf(fd, "# type: string\n");
fprintf(fd, FMT_QSTR, CFG_CIPHERS, config_disp_str(cfg->CIPHER_SUITE));
fprintf(fd, "\n");
fprintf(fd, "# Enforce server cipher list order\n");
fprintf(fd, "#\n");
fprintf(fd, "# type: boolean\n");
fprintf(fd, FMT_STR, CFG_PREFER_SERVER_CIPHERS, config_disp_bool(cfg->PREFER_SERVER_CIPHERS));
fprintf(fd, "\n");
fprintf(fd, "# Use specified SSL engine\n");
fprintf(fd, "#\n");
fprintf(fd, "# type: string\n");
fprintf(fd, FMT_QSTR, CFG_SSL_ENGINE, config_disp_str(cfg->ENGINE));
fprintf(fd, "\n");
fprintf(fd, "# Number of worker processes\n");
fprintf(fd, "#\n");
fprintf(fd, "# type: integer\n");
fprintf(fd, FMT_ISTR, CFG_WORKERS, (int) cfg->NCORES);
fprintf(fd, "\n");
fprintf(fd, "# Listen backlog size\n");
fprintf(fd, "#\n");
fprintf(fd, "# type: integer\n");
fprintf(fd, FMT_ISTR, CFG_BACKLOG, cfg->BACKLOG);
fprintf(fd, "\n");
fprintf(fd, "# TCP socket keepalive interval in seconds\n");
fprintf(fd, "#\n");
fprintf(fd, "# type: integer\n");
fprintf(fd, FMT_ISTR, CFG_KEEPALIVE, cfg->TCP_KEEPALIVE_TIME);
fprintf(fd, "\n");
#ifdef USE_SHARED_CACHE
fprintf(fd, "# SSL session cache size\n");
fprintf(fd, "#\n");
fprintf(fd, "# type: integer\n");
fprintf(fd, FMT_ISTR, CFG_SHARED_CACHE, cfg->SHARED_CACHE);
fprintf(fd, "\n");
fprintf(fd, "# Accept shared SSL cache updates on specified listener.\n");
fprintf(fd, "#\n");
fprintf(fd, "# type: string\n");
fprintf(fd, "# syntax: [HOST]:PORT\n");
fprintf(fd, FMT_QSTR, CFG_SHARED_CACHE_LISTEN, config_disp_hostport(cfg->SHCUPD_IP, cfg->SHCUPD_PORT));