-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.c
3771 lines (3299 loc) · 94.2 KB
/
init.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
/*
* OpenVPN -- An application to securely tunnel IP networks
* over a single TCP/UDP port, with support for SSL/TLS-based
* session authentication and key exchange,
* packet encryption, packet authentication, and
* packet compression.
*
* Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#elif defined(_MSC_VER)
#include "config-msvc.h"
#endif
#include "syshead.h"
#include "win32.h"
#include "init.h"
#include "sig.h"
#include "occ.h"
#include "list.h"
#include "otime.h"
#include "pool.h"
#include "gremlin.h"
#include "pkcs11.h"
#include "ps.h"
#include "lladdr.h"
#include "ping.h"
#include "mstats.h"
#include "memdbg.h"
#include "occ-inline.h"
static struct context *static_context; /* GLOBAL */
/*
* Crypto initialization flags
*/
#define CF_LOAD_PERSISTED_PACKET_ID (1<<0)
#define CF_INIT_TLS_MULTI (1<<1)
#define CF_INIT_TLS_AUTH_STANDALONE (1<<2)
static void do_init_first_time (struct context *c);
void
context_clear (struct context *c)
{
CLEAR (*c);
}
void
context_clear_1 (struct context *c)
{
CLEAR (c->c1);
}
void
context_clear_2 (struct context *c)
{
CLEAR (c->c2);
}
void
context_clear_all_except_first_time (struct context *c)
{
const bool first_time_save = c->first_time;
const struct context_persist cpsave = c->persist;
context_clear (c);
c->first_time = first_time_save;
c->persist = cpsave;
}
/*
* Should be called after options->ce is modified at the top
* of a SIGUSR1 restart.
*/
static void
update_options_ce_post (struct options *options)
{
#if P2MP
/*
* In pull mode, we usually import --ping/--ping-restart parameters from
* the server. However we should also set an initial default --ping-restart
* for the period of time before we pull the --ping-restart parameter
* from the server.
*/
if (options->pull
&& options->ping_rec_timeout_action == PING_UNDEF
&& proto_is_dgram(options->ce.proto))
{
options->ping_rec_timeout = PRE_PULL_INITIAL_PING_RESTART;
options->ping_rec_timeout_action = PING_RESTART;
}
#endif
}
#ifdef ENABLE_MANAGEMENT
static bool
management_callback_proxy_cmd (void *arg, const char **p)
{
struct context *c = arg;
struct connection_entry *ce = &c->options.ce;
struct gc_arena *gc = &c->c2.gc;
bool ret = false;
update_time();
if (streq (p[1], "NONE"))
ret = true;
else if (p[2] && p[3])
{
const int port = atoi(p[3]);
if (!legal_ipv4_port (port))
{
msg (M_WARN, "Bad proxy port number: %s", p[3]);
return false;
}
if (streq (p[1], "HTTP"))
{
#ifndef ENABLE_HTTP_PROXY
msg (M_WARN, "HTTP proxy support is not available");
#else
struct http_proxy_options *ho;
if (ce->proto != PROTO_TCPv4 && ce->proto != PROTO_TCPv4_CLIENT &&
ce->proto != PROTO_TCPv6 && ce->proto != PROTO_TCPv6_CLIENT)
{
msg (M_WARN, "HTTP proxy support only works for TCP based connections");
return false;
}
ho = init_http_proxy_options_once (&ce->http_proxy_options, gc);
ho->server = string_alloc (p[2], gc);
ho->port = port;
ho->retry = true;
ho->auth_retry = (p[4] && streq (p[4], "nct") ? PAR_NCT : PAR_ALL);
ret = true;
#endif
}
else if (streq (p[1], "SOCKS"))
{
#ifndef ENABLE_SOCKS
msg (M_WARN, "SOCKS proxy support is not available");
#else
ce->socks_proxy_server = string_alloc (p[2], gc);
ce->socks_proxy_port = port;
ret = true;
#endif
}
}
else
msg (M_WARN, "Bad proxy command");
ce->flags &= ~CE_MAN_QUERY_PROXY;
return ret;
}
static bool
ce_management_query_proxy (struct context *c)
{
const struct connection_list *l = c->options.connection_list;
struct connection_entry *ce = &c->options.ce;
struct gc_arena gc;
bool ret = true;
update_time();
if (management)
{
gc = gc_new ();
{
struct buffer out = alloc_buf_gc (256, &gc);
buf_printf (&out, ">PROXY:%u,%s,%s", (l ? l->current : 0) + 1,
(proto_is_udp (ce->proto) ? "UDP" : "TCP"), np (ce->remote));
management_notify_generic (management, BSTR (&out));
}
ce->flags |= CE_MAN_QUERY_PROXY;
while (ce->flags & CE_MAN_QUERY_PROXY)
{
management_event_loop_n_seconds (management, 1);
if (IS_SIG (c))
{
ret = false;
break;
}
}
gc_free (&gc);
}
return ret;
}
static bool
management_callback_remote_cmd (void *arg, const char **p)
{
struct context *c = (struct context *) arg;
struct connection_entry *ce = &c->options.ce;
int ret = false;
if (p[1] && ((ce->flags>>CE_MAN_QUERY_REMOTE_SHIFT)&CE_MAN_QUERY_REMOTE_MASK) == CE_MAN_QUERY_REMOTE_QUERY)
{
int flags = 0;
if (!strcmp(p[1], "ACCEPT"))
{
flags = CE_MAN_QUERY_REMOTE_ACCEPT;
ret = true;
}
else if (!strcmp(p[1], "SKIP"))
{
flags = CE_MAN_QUERY_REMOTE_SKIP;
ret = true;
}
else if (!strcmp(p[1], "MOD") && p[2] && p[3])
{
const int port = atoi(p[3]);
if (strlen(p[2]) < RH_HOST_LEN && legal_ipv4_port(port))
{
struct remote_host_store *rhs = c->options.rh_store;
if (!rhs)
{
ALLOC_OBJ_CLEAR_GC (rhs, struct remote_host_store, &c->options.gc);
c->options.rh_store = rhs;
}
strncpynt(rhs->host, p[2], RH_HOST_LEN);
ce->remote = rhs->host;
ce->remote_port = port;
flags = CE_MAN_QUERY_REMOTE_MOD;
ret = true;
}
}
if (ret)
{
ce->flags &= ~(CE_MAN_QUERY_REMOTE_MASK<<CE_MAN_QUERY_REMOTE_SHIFT);
ce->flags |= ((flags&CE_MAN_QUERY_REMOTE_MASK)<<CE_MAN_QUERY_REMOTE_SHIFT);
}
}
return ret;
}
static bool
ce_management_query_remote (struct context *c, const char *remote_ip_hint)
{
struct gc_arena gc = gc_new ();
volatile struct connection_entry *ce = &c->options.ce;
int ret = true;
update_time();
if (management)
{
struct buffer out = alloc_buf_gc (256, &gc);
buf_printf (&out, ">REMOTE:%s,%d,%s", np(ce->remote), ce->remote_port, proto2ascii(ce->proto, false));
management_notify_generic(management, BSTR (&out));
ce->flags &= ~(CE_MAN_QUERY_REMOTE_MASK<<CE_MAN_QUERY_REMOTE_SHIFT);
ce->flags |= (CE_MAN_QUERY_REMOTE_QUERY<<CE_MAN_QUERY_REMOTE_SHIFT);
while (((ce->flags>>CE_MAN_QUERY_REMOTE_SHIFT) & CE_MAN_QUERY_REMOTE_MASK) == CE_MAN_QUERY_REMOTE_QUERY)
{
management_event_loop_n_seconds (management, 1);
if (IS_SIG (c))
{
ret = false;
break;
}
}
}
{
const int flags = ((ce->flags>>CE_MAN_QUERY_REMOTE_SHIFT) & CE_MAN_QUERY_REMOTE_MASK);
if (flags == CE_MAN_QUERY_REMOTE_ACCEPT && remote_ip_hint)
ce->remote = remote_ip_hint;
ret = (flags != CE_MAN_QUERY_REMOTE_SKIP);
}
gc_free (&gc);
return ret;
}
#endif /* ENABLE_MANAGEMENT */
/*
* Initialize and possibly randomize connection list.
*/
static void
init_connection_list (struct context *c)
{
struct connection_list *l = c->options.connection_list;
if (l)
{
l->current = -1;
if (c->options.remote_random)
{
int i;
for (i = 0; i < l->len; ++i)
{
const int j = get_random () % l->len;
if (i != j)
{
struct connection_entry *tmp;
tmp = l->array[i];
l->array[i] = l->array[j];
l->array[j] = tmp;
}
}
}
}
}
/*
* Increment to next connection entry
*/
static void
next_connection_entry (struct context *c)
{
struct connection_list *l = c->options.connection_list;
if (l)
{
bool ce_defined;
struct connection_entry *ce;
int n_cycles = 0;
do {
const char *remote_ip_hint = NULL;
bool newcycle = false;
ce_defined = true;
if (l->no_advance && l->current >= 0)
{
l->no_advance = false;
}
else
{
if (++l->current >= l->len)
{
l->current = 0;
++l->n_cycles;
if (++n_cycles >= 2)
msg (M_FATAL, "No usable connection profiles are present");
}
if (l->current == 0)
newcycle = true;
}
ce = l->array[l->current];
if (c->options.remote_ip_hint && !l->n_cycles)
remote_ip_hint = c->options.remote_ip_hint;
if (ce->flags & CE_DISABLED)
ce_defined = false;
c->options.ce = *ce;
#ifdef ENABLE_MANAGEMENT
if (ce_defined && management && management_query_remote_enabled(management))
{
/* allow management interface to override connection entry details */
ce_defined = ce_management_query_remote(c, remote_ip_hint);
if (IS_SIG (c))
break;
}
else
#endif
if (remote_ip_hint)
c->options.ce.remote = remote_ip_hint;
#ifdef ENABLE_MANAGEMENT
if (ce_defined && management && management_query_proxy_enabled (management))
{
ce_defined = ce_management_query_proxy (c);
if (IS_SIG (c))
break;
}
#endif
} while (!ce_defined);
}
update_options_ce_post (&c->options);
}
/*
* Query for private key and auth-user-pass username/passwords
*/
void
init_query_passwords (const struct context *c)
{
#if defined(ENABLE_CRYPTO) && defined(ENABLE_SSL)
/* Certificate password input */
if (c->options.key_pass_file)
pem_password_setup (c->options.key_pass_file);
#endif
#if P2MP
/* Auth user/pass input */
if (c->options.auth_user_pass_file)
{
#ifdef ENABLE_CLIENT_CR
auth_user_pass_setup (c->options.auth_user_pass_file, &c->options.sc_info);
#else
auth_user_pass_setup (c->options.auth_user_pass_file, NULL);
#endif
}
#endif
}
/*
* Initialize/Uninitialize HTTP or SOCKS proxy
*/
#ifdef GENERAL_PROXY_SUPPORT
static int
proxy_scope (struct context *c)
{
return connection_list_defined (&c->options) ? 2 : 1;
}
static void
uninit_proxy_dowork (struct context *c)
{
#ifdef ENABLE_HTTP_PROXY
if (c->c1.http_proxy_owned && c->c1.http_proxy)
{
http_proxy_close (c->c1.http_proxy);
c->c1.http_proxy = NULL;
c->c1.http_proxy_owned = false;
}
#endif
#ifdef ENABLE_SOCKS
if (c->c1.socks_proxy_owned && c->c1.socks_proxy)
{
socks_proxy_close (c->c1.socks_proxy);
c->c1.socks_proxy = NULL;
c->c1.socks_proxy_owned = false;
}
#endif
}
static void
init_proxy_dowork (struct context *c)
{
#ifdef ENABLE_HTTP_PROXY
bool did_http = false;
#else
const bool did_http = false;
#endif
uninit_proxy_dowork (c);
#ifdef ENABLE_HTTP_PROXY
if (c->options.ce.http_proxy_options)
{
/* Possible HTTP proxy user/pass input */
c->c1.http_proxy = http_proxy_new (c->options.ce.http_proxy_options);
if (c->c1.http_proxy)
{
did_http = true;
c->c1.http_proxy_owned = true;
}
}
#endif
#ifdef ENABLE_SOCKS
if (!did_http && c->options.ce.socks_proxy_server)
{
c->c1.socks_proxy = socks_proxy_new (c->options.ce.socks_proxy_server,
c->options.ce.socks_proxy_port,
c->options.ce.socks_proxy_authfile,
c->options.ce.socks_proxy_retry);
if (c->c1.socks_proxy)
{
c->c1.socks_proxy_owned = true;
}
}
#endif
}
static void
init_proxy (struct context *c, const int scope)
{
if (scope == proxy_scope (c))
init_proxy_dowork (c);
}
static void
uninit_proxy (struct context *c)
{
if (c->sig->signal_received != SIGUSR1 || proxy_scope (c) == 2)
uninit_proxy_dowork (c);
}
#else
static inline void
init_proxy (struct context *c, const int scope)
{
}
static inline void
uninit_proxy (struct context *c)
{
}
#endif
void
context_init_1 (struct context *c)
{
context_clear_1 (c);
packet_id_persist_init (&c->c1.pid_persist);
init_connection_list (c);
#if defined(ENABLE_PKCS11)
if (c->first_time) {
int i;
pkcs11_initialize (true, c->options.pkcs11_pin_cache_period);
for (i=0;i<MAX_PARMS && c->options.pkcs11_providers[i] != NULL;i++)
pkcs11_addProvider (c->options.pkcs11_providers[i], c->options.pkcs11_protected_authentication[i],
c->options.pkcs11_private_mode[i], c->options.pkcs11_cert_private[i]);
}
#endif
#if 0 /* test get_user_pass with GET_USER_PASS_NEED_OK flag */
{
/*
* In the management interface, you can okay the request by entering "needok token-insertion-request ok"
*/
struct user_pass up;
CLEAR (up);
strcpy (up.username, "Please insert your cryptographic token"); /* put the high-level message in up.username */
get_user_pass (&up, NULL, "token-insertion-request", GET_USER_PASS_MANAGEMENT|GET_USER_PASS_NEED_OK);
msg (M_INFO, "RET:%s", up.password); /* will return the third argument to management interface
'needok' command, usually 'ok' or 'cancel'. */
}
#endif
/* initialize HTTP or SOCKS proxy object at scope level 1 */
init_proxy (c, 1);
}
void
context_gc_free (struct context *c)
{
gc_free (&c->c2.gc);
gc_free (&c->options.gc);
gc_free (&c->gc);
}
#if PORT_SHARE
static void
close_port_share (void)
{
if (port_share)
{
port_share_close (port_share);
port_share = NULL;
}
}
static void
init_port_share (struct context *c)
{
if (!port_share && (c->options.port_share_host && c->options.port_share_port))
{
port_share = port_share_open (c->options.port_share_host,
c->options.port_share_port,
MAX_RW_SIZE_LINK (&c->c2.frame),
c->options.port_share_journal_dir);
if (port_share == NULL)
msg (M_FATAL, "Fatal error: Port sharing failed");
}
}
#endif
bool
init_static (void)
{
/* configure_path (); */
#if defined(ENABLE_CRYPTO) && defined(DMALLOC)
crypto_init_dmalloc();
#endif
init_random_seed (); /* init random() function, only used as
source for weak random numbers */
error_reset (); /* initialize error.c */
reset_check_status (); /* initialize status check code in socket.c */
#ifdef WIN32
init_win32 ();
#endif
#ifdef OPENVPN_DEBUG_COMMAND_LINE
{
int i;
for (i = 0; i < argc; ++i)
msg (M_INFO, "argv[%d] = '%s'", i, argv[i]);
}
#endif
update_time ();
#ifdef ENABLE_CRYPTO
init_ssl_lib ();
/* init PRNG used for IV generation */
/* When forking, copy this to more places in the code to avoid fork
random-state predictability */
prng_init (NULL, 0);
#endif
#ifdef PID_TEST
packet_id_interactive_test (); /* test the sequence number code */
return false;
#endif
#ifdef SCHEDULE_TEST
schedule_test ();
return false;
#endif
#ifdef LIST_TEST
list_test ();
return false;
#endif
#ifdef IFCONFIG_POOL_TEST
ifconfig_pool_test (0x0A010004, 0x0A0100FF);
return false;
#endif
#ifdef CHARACTER_CLASS_DEBUG
character_class_debug ();
return false;
#endif
#ifdef EXTRACT_X509_FIELD_TEST
extract_x509_field_test ();
return false;
#endif
#ifdef TIME_TEST
time_test ();
return false;
#endif
#ifdef TEST_GET_DEFAULT_GATEWAY
{
struct route_gateway_info rgi;
get_default_gateway(&rgi);
print_default_gateway(M_INFO, &rgi);
return false;
}
#endif
#ifdef GEN_PATH_TEST
{
struct gc_arena gc = gc_new ();
const char *fn = gen_path ("foo",
"bar",
&gc);
printf ("%s\n", fn);
gc_free (&gc);
}
return false;
#endif
#ifdef STATUS_PRINTF_TEST
{
struct gc_arena gc = gc_new ();
const char *tmp_file = create_temp_file ("/tmp", "foo", &gc);
struct status_output *so = status_open (tmp_file, 0, -1, NULL, STATUS_OUTPUT_WRITE);
status_printf (so, "%s", "foo");
status_printf (so, "%s", "bar");
if (!status_close (so))
msg (M_WARN, "STATUS_PRINTF_TEST: %s: write error", tmp_file);
gc_free (&gc);
}
return false;
#endif
#ifdef ARGV_TEST
{
void argv_test (void);
argv_test ();
return false;
}
#endif
#ifdef PRNG_TEST
{
struct gc_arena gc = gc_new ();
uint8_t rndbuf[8];
int i;
prng_init ("sha1", 16);
/*prng_init (NULL, 0);*/
const int factor = 1;
for (i = 0; i < factor * 8; ++i)
{
#if 1
prng_bytes (rndbuf, sizeof (rndbuf));
#else
ASSERT(rand_bytes (rndbuf, sizeof (rndbuf)));
#endif
printf ("[%d] %s\n", i, format_hex (rndbuf, sizeof (rndbuf), 0, &gc));
}
gc_free (&gc);
prng_uninit ();
return false;
}
#endif
#ifdef BUFFER_LIST_AGGREGATE_TEST
/* test buffer_list_aggregate function */
{
static const char *text[] = {
"It was a bright cold day in April, ",
"and the clocks were striking ",
"thirteen. ",
"Winston Smith, ",
"his chin nuzzled into his breast in an ",
"effort to escape the vile wind, ",
"slipped quickly through the glass doors ",
"of Victory Mansions, though not quickly ",
"enough to prevent a swirl of gritty dust from ",
"entering along with him."
};
int iter, listcap;
for (listcap = 0; listcap < 12; ++listcap)
{
for (iter = 0; iter < 512; ++iter)
{
struct buffer_list *bl = buffer_list_new(listcap);
{
int i;
for (i = 0; i < SIZE(text); ++i)
buffer_list_push(bl, (unsigned char *)text[i]);
}
printf("[cap=%d i=%d] *************************\n", listcap, iter);
if (!(iter & 8))
buffer_list_aggregate(bl, iter/2);
if (!(iter & 16))
buffer_list_push(bl, (unsigned char *)"Even more text...");
buffer_list_aggregate(bl, iter);
if (!(iter & 1))
buffer_list_push(bl, (unsigned char *)"More text...");
{
struct buffer *buf;
while ((buf = buffer_list_peek(bl)))
{
int c;
printf ("'");
while ((c = buf_read_u8(buf)) >= 0)
putchar(c);
printf ("'\n");
buffer_list_advance(bl, 0);
}
}
buffer_list_free(bl);
}
}
return false;
}
#endif
#ifdef MSTATS_TEST
{
int i;
mstats_open("/dev/shm/mstats.dat");
for (i = 0; i < 30; ++i)
{
mmap_stats->n_clients += 1;
mmap_stats->link_write_bytes += 8;
mmap_stats->link_read_bytes += 16;
sleep(1);
}
mstats_close();
return false;
}
#endif
return true;
}
void
uninit_static (void)
{
#ifdef ENABLE_CRYPTO
free_ssl_lib ();
#endif
#ifdef ENABLE_PKCS11
pkcs11_terminate ();
#endif
#if PORT_SHARE
close_port_share ();
#endif
#if defined(MEASURE_TLS_HANDSHAKE_STATS) && defined(ENABLE_CRYPTO) && defined(ENABLE_SSL)
show_tls_performance_stats ();
#endif
}
void
init_verb_mute (struct context *c, unsigned int flags)
{
if (flags & IVM_LEVEL_1)
{
/* set verbosity and mute levels */
set_check_status (D_LINK_ERRORS, D_READ_WRITE);
set_debug_level (c->options.verbosity, SDL_CONSTRAIN);
set_mute_cutoff (c->options.mute);
}
/* special D_LOG_RW mode */
if (flags & IVM_LEVEL_2)
c->c2.log_rw = (check_debug_level (D_LOG_RW) && !check_debug_level (D_LOG_RW + 1));
}
/*
* Possibly set --dev based on --dev-node.
* For example, if --dev-node /tmp/foo/tun, and --dev undefined,
* set --dev to tun.
*/
void
init_options_dev (struct options *options)
{
if (!options->dev && options->dev_node) {
char *dev_node = strdup(options->dev_node); /* POSIX basename() implementaions may modify its arguments */
options->dev = basename (dev_node);
}
}
bool
print_openssl_info (const struct options *options)
{
/*
* OpenSSL info print mode?
*/
#ifdef ENABLE_CRYPTO
if (options->show_ciphers || options->show_digests || options->show_engines
#ifdef ENABLE_SSL
|| options->show_tls_ciphers
#endif
)
{
if (options->show_ciphers)
show_available_ciphers ();
if (options->show_digests)
show_available_digests ();
if (options->show_engines)
show_available_engines ();
#ifdef ENABLE_SSL
if (options->show_tls_ciphers)
show_available_tls_ciphers (options->cipher_list);
#endif
return true;
}
#endif
return false;
}
/*
* Static pre-shared key generation mode?
*/
bool
do_genkey (const struct options * options)
{
#ifdef ENABLE_CRYPTO
if (options->genkey)
{
int nbits_written;
notnull (options->shared_secret_file,
"shared secret output file (--secret)");
if (options->mlock) /* should we disable paging? */
platform_mlockall (true);
nbits_written = write_key_file (2, options->shared_secret_file);
msg (D_GENKEY | M_NOPREFIX,
"Randomly generated %d bit key written to %s", nbits_written,
options->shared_secret_file);
return true;
}
#endif
return false;
}
/*
* Persistent TUN/TAP device management mode?
*/
bool
do_persist_tuntap (const struct options *options)
{
if (options->persist_config)
{
/* sanity check on options for --mktun or --rmtun */
notnull (options->dev, "TUN/TAP device (--dev)");
if (options->ce.remote || options->ifconfig_local
|| options->ifconfig_remote_netmask
#ifdef ENABLE_CRYPTO
|| options->shared_secret_file
#ifdef ENABLE_SSL
|| options->tls_server || options->tls_client
#endif
#endif
)
msg (M_FATAL|M_OPTERR,
"options --mktun or --rmtun should only be used together with --dev");
#ifdef ENABLE_FEATURE_TUN_PERSIST
tuncfg (options->dev, options->dev_type, options->dev_node,
options->persist_mode,
options->username, options->groupname, &options->tuntap_options);
if (options->persist_mode && options->lladdr)
set_lladdr(options->dev, options->lladdr, NULL);
return true;
#else
msg( M_FATAL|M_OPTERR,
"options --mktun and --rmtun are not available on your operating "
"system. Please check 'man tun' (or 'tap'), whether your system "
"supports using 'ifconfig %s create' / 'destroy' to create/remove "
"persistant tunnel interfaces.", options->dev );
#endif
}
return false;
}
/*
* Should we become a daemon?
* Return true if we did it.
*/
bool
possibly_become_daemon (const struct options *options)
{
bool ret = false;
if (options->daemon)
{
ASSERT (!options->inetd);
/* Don't chdir immediately, but the end of the init sequence, if needed */
if (daemon (1, options->log) < 0)
msg (M_ERR, "daemon() failed or unsupported");
restore_signal_state ();
if (options->log)
set_std_files_to_null (true);
ret = true;
}
return ret;
}
/*
* Actually do UID/GID downgrade, chroot and SELinux context switching, if requested.
*/
static void
do_uid_gid_chroot (struct context *c, bool no_delay)
{
static const char why_not[] = "will be delayed because of --client, --pull, or --up-delay";
struct context_0 *c0 = c->c0;
if (c->first_time && c0 && !c0->uid_gid_set)
{
/* chroot if requested */
if (c->options.chroot_dir)
{
if (no_delay)
platform_chroot (c->options.chroot_dir);
else
msg (M_INFO, "NOTE: chroot %s", why_not);
}
/* set user and/or group that we want to setuid/setgid to */
if (no_delay)
{
platform_group_set (&c0->platform_state_group);
platform_user_set (&c0->platform_state_user);
c0->uid_gid_set = true;
}
else if (c0->uid_gid_specified)
{
msg (M_INFO, "NOTE: UID/GID downgrade %s", why_not);
}
#ifdef ENABLE_MEMSTATS
if (c->options.memstats_fn)