Browse code

auth_xkeys: rpc commands to manage the keys

Daniel-Constantin Mierla authored on 22/04/2015 08:16:32
Showing 3 changed files
... ...
@@ -36,6 +36,8 @@
36 36
 #include "../../parser/msg_parser.h"
37 37
 #include "../../lib/srutils/shautils.h"
38 38
 #include "../../lib/kcore/cmpapi.h"
39
+#include "../../rpc.h"
40
+#include "../../rpc_lookup.h"
39 41
 
40 42
 #include "auth_xkeys.h"
41 43
 
... ...
@@ -201,7 +203,7 @@ int auth_xkeys_add(sip_msg_t* msg, str *hdr, str *key,
201 203
 	char xout[SHA512_DIGEST_STRING_LENGTH];
202 204
 	struct lump* anchor;
203 205
 
204
-	if(_auth_xkeys_list==NULL || _auth_xkeys_list==NULL) {
206
+	if(_auth_xkeys_list==NULL || *_auth_xkeys_list==NULL) {
205 207
 		LM_ERR("no stored keys\n");
206 208
 		return -1;
207 209
 	}
... ...
@@ -283,7 +285,7 @@ int auth_xkeys_check(sip_msg_t* msg, str *hdr, str *key,
283 285
 	char xout[SHA512_DIGEST_STRING_LENGTH];
284 286
 	str hbody;
285 287
 
286
-	if(_auth_xkeys_list==NULL || _auth_xkeys_list==NULL) {
288
+	if(_auth_xkeys_list==NULL || *_auth_xkeys_list==NULL) {
287 289
 		LM_ERR("no stored keys\n");
288 290
 		return -1;
289 291
 	}
... ...
@@ -379,3 +381,117 @@ int auth_xkeys_check(sip_msg_t* msg, str *hdr, str *key,
379 381
 	return -1;
380 382
 }
381 383
 
384
+
385
+static const char* auth_xkeys_rpc_list_doc[2] = {
386
+	"List existing keys",
387
+	0
388
+};
389
+
390
+/*
391
+ * RPC command to list the keys
392
+ */
393
+static void auth_xkeys_rpc_list(rpc_t* rpc, void* ctx)
394
+{
395
+	void* th;
396
+	void* ih;
397
+	void* vh;
398
+	auth_xkey_t *itc;
399
+	auth_xkey_t *itd;
400
+
401
+	if(_auth_xkeys_list==NULL || *_auth_xkeys_list==NULL) {
402
+		rpc->fault(ctx, 500, "No keys");
403
+		return;
404
+	}
405
+	/* add entry node */
406
+	if (rpc->add(ctx, "{", &th) < 0) {
407
+		rpc->fault(ctx, 500, "Internal error root reply");
408
+		return;
409
+	}
410
+	for(itc = *_auth_xkeys_list; itc; itc = itc->next_id) {
411
+		if(rpc->struct_add(th, "S[",
412
+					"KID", &itc->kid,
413
+					"KEYS",  &ih)<0) {
414
+			rpc->fault(ctx, 500, "Internal error keys array");
415
+			return;
416
+		}
417
+
418
+		for(itd=itc; itd; itd = itd->next) {
419
+			if(rpc->struct_add(ih, "{",
420
+						"KEY", &vh)<0) {
421
+				rpc->fault(ctx, 500, "Internal error creating keys data");
422
+				return;
423
+			}
424
+			if(rpc->struct_add(vh, "SDd",
425
+						"NAME",  &itd->kname,
426
+						"VALUE", &itd->kvalue,
427
+						"EXPIRES", itd->kexpires)<0)
428
+			{
429
+				rpc->fault(ctx, 500, "Internal error creating dest struct");
430
+				return;
431
+			}
432
+		}
433
+	}
434
+	return;
435
+}
436
+
437
+static const char* auth_xkeys_rpc_set_doc[2] = {
438
+	"Set expires of existing key or add a new key",
439
+	0
440
+};
441
+
442
+/*
443
+ * RPC command to set the expires of a key or add a new key
444
+ */
445
+static void auth_xkeys_rpc_set(rpc_t* rpc, void* ctx)
446
+{
447
+	auth_xkey_t tmp;
448
+	auth_xkey_t *itc;
449
+
450
+	memset(&tmp, 0, sizeof(auth_xkey_t));
451
+
452
+	if(rpc->scan(ctx, ".SSSd", &tmp.kid, &tmp.kname,
453
+				&tmp.kvalue, &tmp.kexpires)<4)
454
+	{
455
+		rpc->fault(ctx, 500, "Invalid Parameters");
456
+		return;
457
+	}
458
+	for(itc = *_auth_xkeys_list; itc; itc = itc->next_id) {
459
+		if(itc->kid.len==tmp.kid.len
460
+				&& strncasecmp(itc->kid.s, tmp.kid.s, tmp.kid.len)==0)
461
+			break;
462
+	}
463
+	if(itc==NULL) {
464
+		LM_DBG("no key chain id [%.*s]\n", tmp.kid.len, tmp.kid.s);
465
+		/* add one */
466
+		if(authx_xkey_insert(&tmp)<0) {
467
+			LM_ERR("unable to insert the key [%.*s:%.*s]\n",
468
+				tmp.kid.len, tmp.kid.s, tmp.kname.len, tmp.kname.s);
469
+			rpc->fault(ctx, 500, "Insert failure");
470
+			return;
471
+		}
472
+		return;
473
+	}
474
+	itc->kexpires = time(NULL) + tmp.kexpires;
475
+	return;
476
+}
477
+
478
+rpc_export_t auth_xkeys_rpc_cmds[] = {
479
+	{"auth_xkeys_.list",   auth_xkeys_rpc_list,
480
+		auth_xkeys_rpc_list_doc,   0},
481
+	{"auth_xkeys_.set",   auth_xkeys_rpc_set,
482
+		auth_xkeys_rpc_set_doc,   0},
483
+	{0, 0, 0, 0}
484
+};
485
+
486
+/**
487
+ *
488
+ */
489
+int auth_xkeys_init_rpc(void)
490
+{
491
+	if (rpc_register_array(auth_xkeys_rpc_cmds)!=0)
492
+	{
493
+		LM_ERR("failed to register RPC commands\n");
494
+		return -1;
495
+	}
496
+	return 0;
497
+}
... ...
@@ -30,5 +30,6 @@ int auth_xkeys_add(sip_msg_t* msg, str *hdr, str *key,
30 30
 		str *alg, str *data);
31 31
 int auth_xkeys_check(sip_msg_t* msg, str *hdr, str *key,
32 32
 		str *alg, str *data);
33
+int auth_xkeys_init_rpc(void);
33 34
 
34 35
 #endif
... ...
@@ -93,6 +93,11 @@ struct module_exports exports = {
93 93
  */
94 94
 static int mod_init(void)
95 95
 {
96
+	if(auth_xkeys_init_rpc()<0)
97
+	{
98
+		LM_ERR("failed to register RPC commands\n");
99
+		return -1;
100
+	}
96 101
 	return 0;
97 102
 }
98 103