From f91c070829d82e2731835d66806e1630619de0c0 Mon Sep 17 00:00:00 2001 From: Fabian Mauchle Date: Thu, 23 Jul 2020 18:33:25 +0200 Subject: [PATCH] Fix modifyVendorAttribute not applied (#62) --- rewrite.c | 6 ++-- tests/t_rewrite.c | 53 +++++++++++++++++++++++++++-- tests/t_rewrite_config.c | 73 +++++++++++++++++++++++++++++----------- 3 files changed, 106 insertions(+), 26 deletions(-) diff --git a/rewrite.c b/rewrite.c index fa50f95..04dae00 100644 --- a/rewrite.c +++ b/rewrite.c @@ -147,7 +147,7 @@ struct modattr *extractmodvattr(char *nameval) { s = strchr(nameval, ':'); vendor = atoi(nameval); - if (!s || !vendor || !strchr(s,':')) + if (!s || !vendor || !strchr(s+1,':')) return NULL; modvattr = extractmodattr(s+1); if (modvattr) @@ -278,7 +278,7 @@ void addrewrite(char *value, uint8_t whitelist_mode, char **rmattrs, char **rmva freegconfmstr(supvattrs); } - if (rma || rmva || adda || moda || supa) { + if (rma || rmva || adda || moda || modva || supa) { rewrite = malloc(sizeof(struct rewrite)); if (!rewrite) debugx(1, DBG_ERR, "malloc failed"); @@ -499,7 +499,7 @@ int dorewritemodvattr(struct tlv *vendortlv, struct modattr *modvattr) { int dorewritemod(struct radmsg *msg, struct list *modattrs, struct list *modvattrs) { struct list_node *n, *m; uint32_t vendor; - + for (n = list_first(msg->attrs); n; n = list_next(n)) { struct tlv *attr = (struct tlv *)n->data; if (attr->t == RAD_Attr_Vendor_Specific) { diff --git a/tests/t_rewrite.c b/tests/t_rewrite.c index bbbe469..80d6bee 100644 --- a/tests/t_rewrite.c +++ b/tests/t_rewrite.c @@ -4,16 +4,26 @@ #include #include #include +#include #include "../rewrite.h" #include "../radmsg.h" #include "../debug.h" +static void printescape(uint8_t *v, uint8_t l) { + int i; + for(i=0; idata, (struct tlv *)m->data)) { - printf("attribute list not as expected\n"); + struct tlv *tlv_exp = (struct tlv *)n->data, *tlv_act = (struct tlv *)m->data; + if (!eqtlv(tlv_exp, tlv_act)) { + printf("attribute list at %d not as expected!\n", i); + printf(" expected type: %d, actual type: %d\n", tlv_exp->t, tlv_act->t); + printf(" expected length: %d, actual length: %d\n", tlv_exp->l, tlv_act->l); + printf(" expected value: "); + printescape(tlv_exp->v, tlv_exp->l); + printf(" actual value: "); + printescape(tlv_act->v, tlv_act->l); + printf("\n"); return 1; } m=list_next(m); + i++; } return 0; } @@ -65,7 +84,7 @@ void _reset_rewrite(struct rewrite *rewrite) { int main (int argc, char *argv[]) { - int testcount = 25; + int testcount = 26; struct list *origattrs, *expectedattrs; struct rewrite rewrite; char *username = "user@realm"; @@ -643,6 +662,34 @@ main (int argc, char *argv[]) _reset_rewrite(&rewrite); } + /* test issue #62 + rewrite 9:102:/^(h323-credit-time).*$/\1=86400/ + */ + { + char *value = "h323-credit-time=1846422"; + char *expect = "h323-credit-time=86400"; + struct modattr *mod = malloc(sizeof(struct modattr)); + regex_t regex; + + mod->t = 102; + mod->vendor = 9; + mod->regex = ®ex; + mod->replacement = "\\1=86400"; + regcomp(mod->regex, "^(h323-credit-time).*$", REG_ICASE | REG_EXTENDED); + + list_push(rewrite.modvattrs, mod); + list_push(origattrs, makevendortlv(9,maketlv(102,strlen(value), value))); + list_push(expectedattrs, makevendortlv(9,maketlv(102,strlen(expect), expect))); + + if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0)) + printf("not "); + printf("ok %d - issue #62\n", testcount++); + + _tlv_list_clear(origattrs); + _tlv_list_clear(expectedattrs); + _reset_rewrite(&rewrite); + } + list_destroy(origattrs); list_destroy(expectedattrs); list_destroy(rewrite.addattrs); diff --git a/tests/t_rewrite_config.c b/tests/t_rewrite_config.c index 2e0ac89..129bbbd 100644 --- a/tests/t_rewrite_config.c +++ b/tests/t_rewrite_config.c @@ -15,40 +15,73 @@ main (int argc, char *argv[]) struct rewrite *result; char *rewritename = "rewrite"; char **addattrs; - int numtests = 1, i; + int numtests = 2, i; struct tlv *tlv, *expected; uint8_t expectedvalue[] = {'1',0,0,'1','A','%','4','1'}; printf("1..%d\n", numtests); numtests = 1; - addattrs = malloc(2); - addattrs[0] = stringcopy("1:'1%00%001%41%2541", 0); - addattrs[1] = NULL; + { + addattrs = malloc(2); + addattrs[0] = stringcopy("1:'1%00%001%41%2541", 0); + addattrs[1] = NULL; - expected = maketlv(1,8,expectedvalue); + expected = maketlv(1,8,expectedvalue); - addrewrite(rewritename, 0, NULL, NULL, addattrs, - NULL, NULL, NULL, NULL, NULL); + addrewrite(rewritename, 0, NULL, NULL, addattrs, + NULL, NULL, NULL, NULL, NULL); - result = getrewrite(rewritename, NULL); + result = getrewrite(rewritename, NULL); - if (result->addattrs->first) { - tlv = (struct tlv *)result->addattrs->first->data; - if (!eqtlv(tlv, expected)) { - printf ("tlv value was: 0x"); - for (i = 0; i < tlv->l; i++) { - printf ("%x", *((tlv->v)+i)); + if (result->addattrs->first) { + tlv = (struct tlv *)result->addattrs->first->data; + if (!eqtlv(tlv, expected)) { + printf ("tlv value was: 0x"); + for (i = 0; i < tlv->l; i++) { + printf ("%x", *((tlv->v)+i)); + } + printf ("\n"); + printf ("not "); } - printf ("\n"); - printf ("not "); + printf("ok %d - rewrite config\n", numtests++); + } else { + printf("not ok %d - rewrite config\n", numtests++); } - printf("ok %d - rewrite config\n", numtests++); - } else { - printf("not ok %d - rewrite ocnfig\n", numtests++); + + freetlv(expected); } - freetlv(expected); + /* test issue #62 */ + { + char *expectreplace = "\\1=86400"; + char **modvattrs = malloc(2); + rewritename= "issue62"; + + modvattrs[0] = stringcopy("9:102:/^(h323-credit-time).*$/\\1=86400/",0); + modvattrs[1] = NULL; + + addrewrite(rewritename, 0, NULL, NULL, NULL, NULL, NULL, modvattrs, NULL, NULL); + result = getrewrite(rewritename, NULL); + + if (result && result->modvattrs && result->modvattrs->first) { + struct modattr *mod = (struct modattr *)result->modvattrs->first->data; + if (regexec(mod->regex,"h323-credit-time=1846422",0,NULL,0)) { + printf("not "); + } + if (strcmp(mod->replacement, expectreplace)) { + printf("not "); + } + else if (mod->t != 102 || mod->vendor != 9) { + printf("not "); + } + printf("ok %d - rewrite config issue #62\n", numtests++); + } else { + printf("not ok %d - rewrite config issue #62\n", numtests++); + } + + free(modvattrs); + } return 0; }