Browse code

tls: implemented tls hook for pre-mod init execution

- do openssl init at this callback

(cherry picked from commit f71243410d5e051def1a47cc73adb6caeeadd1cd)
(cherry picked from commit e6eca3e988226e3654c5c22d762048a5fff3491c)

Daniel-Constantin Mierla authored on 25/01/2015 09:59:07
Showing 3 changed files
... ...
@@ -61,6 +61,7 @@
61 61
 #include "tls_cfg.h"
62 62
 
63 63
 /* will be set to 1 when the TLS env is initialized to make destroy safe */
64
+static int tls_mod_preinitialized = 0;
64 65
 static int tls_mod_initialized = 0;
65 66
 
66 67
 #if OPENSSL_VERSION_NUMBER < 0x00907000L
... ...
@@ -456,6 +457,7 @@ end:
456 457
 
457 458
 /**
458 459
  * tls pre-init function
460
+ * - executed when module is loaded
459 461
  */
460 462
 int tls_pre_init(void)
461 463
 {
... ...
@@ -480,6 +482,23 @@ int tls_pre_init(void)
480 482
 	return 0;
481 483
 }
482 484
 
485
+/**
486
+ * tls mod pre-init function
487
+ * - executed before any mod_init()
488
+ */
489
+int tls_mod_pre_init_h(void)
490
+{
491
+	if(tls_mod_preinitialized==1) {
492
+		LM_DBG("already mod pre-initialized\n");
493
+		return 0;
494
+	}
495
+	DBG("============= :preparing tls env for modules initialization\n");
496
+	SSL_library_init();
497
+	SSL_load_error_strings();
498
+	tls_mod_preinitialized=1;
499
+	return 0;
500
+}
501
+
483 502
 /*
484 503
  * First step of TLS initialization
485 504
  */
... ...
@@ -498,6 +517,12 @@ int init_tls_h(void)
498 517
 	str s;
499 518
 	cfg_ctx_t* cfg_ctx;
500 519
 
520
+	if(tls_mod_initialized == 1) {
521
+		LM_DBG("already initialized\n");
522
+		return 0;
523
+	}
524
+	DBG("initializing tls system\n");
525
+
501 526
 #if OPENSSL_VERSION_NUMBER < 0x00907000L
502 527
 	WARN("You are using an old version of OpenSSL (< 0.9.7). Upgrade!\n");
503 528
 #endif
... ...
@@ -644,8 +669,6 @@ int init_tls_h(void)
644 669
 		}
645 670
 	}
646 671
 	
647
-	SSL_library_init();
648
-	SSL_load_error_strings();
649 672
 	init_ssl_methods();
650 673
 	tls_mod_initialized = 1;
651 674
 	return 0;
... ...
@@ -680,7 +703,7 @@ int tls_check_sockets(tls_domains_cfg_t* cfg)
680 703
 void destroy_tls_h(void)
681 704
 {
682 705
 	DBG("tls module final tls destroy\n");
683
-	if(tls_mod_initialized > 0)
706
+	if(tls_mod_preinitialized > 0)
684 707
 		ERR_free_strings();
685 708
 	/* TODO: free all the ctx'es */
686 709
 	tls_destroy_cfg();
... ...
@@ -52,8 +52,13 @@ extern const SSL_METHOD* ssl_methods[];
52 52
  */
53 53
 int tls_pre_init(void);
54 54
 
55
+/**
56
+ * just once, prepare for init of all modules
57
+ */
58
+int tls_mod_pre_init_h(void);
59
+
55 60
 /*
56
- * just once, initialize the tls subsystem 
61
+ * just once, initialize the tls subsystem after all mod inits
57 62
  */
58 63
 int init_tls_h(void);
59 64
 
... ...
@@ -231,7 +231,8 @@ static struct tls_hooks tls_h = {
231 231
 	tls_h_close,
232 232
 	tls_h_init_si,
233 233
 	init_tls_h,
234
-	destroy_tls_h
234
+	destroy_tls_h,
235
+	tls_mod_pre_init_h,
235 236
 };
236 237
 
237 238
 
... ...
@@ -253,12 +254,21 @@ static tls_domains_cfg_t* tls_use_modparams(void)
253 254
 
254 255
 int mod_register(char *path, int *dlflags, void *p1, void *p2)
255 256
 {
257
+	if (tls_disable) {
258
+		LOG(L_WARN, "tls support is disabled "
259
+				"(set enable_tls=1 in the config to enable it)\n");
260
+		return 0;
261
+	}
262
+
256 263
 	/* shm is used, be sure it is initialized */
257 264
 	if(!shm_initialized() && init_shm()<0)
258 265
 		return -1;
259 266
 
260 267
 	if(tls_pre_init()<0)
261 268
 		return -1;
269
+
270
+	register_tls_hooks(&tls_h);
271
+
262 272
 	return 0;
263 273
 }
264 274
 
... ...
@@ -267,7 +277,7 @@ static int mod_init(void)
267 277
 	int method;
268 278
 
269 279
 	if (tls_disable){
270
-		LOG(L_WARN, "WARNING: tls: mod_init: tls support is disabled "
280
+		LOG(L_WARN, "tls support is disabled "
271 281
 				"(set enable_tls=1 in the config to enable it)\n");
272 282
 		return 0;
273 283
 	}
... ...
@@ -306,7 +316,6 @@ static int mod_init(void)
306 316
 	}
307 317
 	*tls_domains_cfg = NULL;
308 318
 
309
-	register_tls_hooks(&tls_h);
310 319
 	register_select_table(tls_sel);
311 320
 	/* register the rpc interface */
312 321
 	if (rpc_register_array(tls_rpc)!=0) {