Browse code

secsipid: new function secsipid_get_url(url, ovar)

- convenience function to get the content of an URL and store the result
in a variable, can be used to build caching of public keys with htable

Daniel-Constantin Mierla authored on 17/11/2020 10:39:10
Showing 1 changed files
... ...
@@ -29,6 +29,7 @@
29 29
 #include "../../core/dprint.h"
30 30
 #include "../../core/mod_fix.h"
31 31
 #include "../../core/data_lump.h"
32
+#include "../../core/lvalue.h"
32 33
 #include "../../core/kemi.h"
33 34
 
34 35
 #include "secsipid.h"
... ...
@@ -49,6 +50,7 @@ static int w_secsipid_check_identity(sip_msg_t *msg, char *pkeypath, char *str2)
49 50
 static int w_secsipid_check_identity_pubkey(sip_msg_t *msg, char *pkeyval, char *str2);
50 51
 static int w_secsipid_add_identity(sip_msg_t *msg, char *porigtn, char *pdesttn,
51 52
 			char *pattest, char *porigid, char *px5u, char *pkeypath);
53
+static int w_secsipid_get_url(sip_msg_t *msg, char *purl, char *pout);
52 54
 
53 55
 
54 56
 /* clang-format off */
... ...
@@ -59,6 +61,8 @@ static cmd_export_t cmds[]={
59 61
 		fixup_spve_null, fixup_free_spve_null, ANY_ROUTE},
60 62
 	{"secsipid_add_identity", (cmd_function)w_secsipid_add_identity, 6,
61 63
 		fixup_spve_all, fixup_free_spve_all, ANY_ROUTE},
64
+	{"secsipid_get_url", (cmd_function)w_secsipid_get_url, 2,
65
+		fixup_spve_pvar, fixup_free_spve_pvar, ANY_ROUTE},
62 66
 	{0, 0, 0, 0, 0, 0}
63 67
 };
64 68
 
... ...
@@ -316,6 +320,85 @@ static int w_secsipid_add_identity(sip_msg_t *msg, char *porigtn, char *pdesttn,
316 320
 			&attest, &origid, &x5u, &keypath);
317 321
 }
318 322
 
323
+/**
324
+ *
325
+ */
326
+static str _secsipid_get_url_val = STR_NULL;
327
+/**
328
+ *
329
+ */
330
+static sr_kemi_xval_t _sr_kemi_secsipid_xval = {0};
331
+
332
+/**
333
+ *
334
+ */
335
+static sr_kemi_xval_t* ki_secsipid_get_url(sip_msg_t *msg, str *surl)
336
+{
337
+	int r;
338
+
339
+	memset(&_sr_kemi_secsipid_xval, 0, sizeof(sr_kemi_xval_t));
340
+
341
+	if(msg==NULL) {
342
+		sr_kemi_xval_null(&_sr_kemi_secsipid_xval, SR_KEMI_XVAL_NULL_EMPTY);
343
+		return &_sr_kemi_secsipid_xval;
344
+	}
345
+
346
+	if(_secsipid_get_url_val.s != NULL) {
347
+		free(_secsipid_get_url_val.s);
348
+		_secsipid_get_url_val.len = 0;
349
+	}
350
+
351
+	r = SecSIPIDGetURLContent(surl->s, secsipid_timeout, &_secsipid_get_url_val.s,
352
+			&_secsipid_get_url_val.len);
353
+	if(r!=0) {
354
+		sr_kemi_xval_null(&_sr_kemi_secsipid_xval, SR_KEMI_XVAL_NULL_EMPTY);
355
+		return &_sr_kemi_secsipid_xval;
356
+	}
357
+
358
+	_sr_kemi_secsipid_xval.vtype = SR_KEMIP_STR;
359
+	_sr_kemi_secsipid_xval.v.s = _secsipid_get_url_val;
360
+
361
+	return &_sr_kemi_secsipid_xval;
362
+}
363
+
364
+/**
365
+ *
366
+ */
367
+static int w_secsipid_get_url(sip_msg_t *msg, char *purl, char *povar)
368
+{
369
+	int r;
370
+	pv_spec_t *ovar;
371
+	pv_value_t val;
372
+	str surl = {NULL, 0};
373
+
374
+	if(fixup_get_svalue(msg, (gparam_t*)purl, &surl)<0) {
375
+		LM_ERR("failed to get url parameter\n");
376
+		return -1;
377
+	}
378
+	if(_secsipid_get_url_val.s != NULL) {
379
+		free(_secsipid_get_url_val.s);
380
+		_secsipid_get_url_val.len = 0;
381
+	}
382
+
383
+	r = SecSIPIDGetURLContent(surl.s, secsipid_timeout, &_secsipid_get_url_val.s,
384
+			&_secsipid_get_url_val.len);
385
+	if(r!=0) {
386
+		return -1;
387
+	}
388
+	ovar = (pv_spec_t *)povar;
389
+
390
+	val.rs = _secsipid_get_url_val;
391
+	val.flags = PV_VAL_STR;
392
+	if(ovar->setf) {
393
+		ovar->setf(msg, &ovar->pvp, (int)EQ_T, &val);
394
+	} else {
395
+		LM_WARN("target pv is not writable\n");
396
+		return -1;
397
+	}
398
+
399
+	return 1;
400
+}
401
+
319 402
 /**
320 403
  *
321 404
  */
... ...
@@ -336,6 +419,11 @@ static sr_kemi_t sr_kemi_secsipid_exports[] = {
336 419
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
337 420
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
338 421
 	},
422
+	{ str_init("secsipid"), str_init("secsipid_get_url"),
423
+		SR_KEMIP_XVAL, ki_secsipid_get_url,
424
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
425
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
426
+	},
339 427
 
340 428
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
341 429
 };