Skip to content

Commit

Permalink
Fix modifyVendorAttribute not applied (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Mauchle committed Jul 31, 2020
1 parent 36353af commit f91c070
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 26 deletions.
6 changes: 3 additions & 3 deletions rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down
53 changes: 50 additions & 3 deletions tests/t_rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../rewrite.h"
#include "../radmsg.h"
#include "../debug.h"

static void printescape(uint8_t *v, uint8_t l) {
int i;
for(i=0; i<l; i++) {
if(isprint(v[i])) printf("%c", v[i]);
else printf("\\x%02x", v[i]);
}
}

/*origattrs and expectedattrs as struct tlv*/
/*return 0 if expected; 1 otherwise or error*/
static int
_check_rewrite(struct list *origattrs, struct rewrite *rewrite, struct list *expectedattrs, int shouldfail) {
struct radmsg msg;
struct list_node *n,*m;
int i = 1;

msg.attrs = origattrs;

Expand All @@ -31,11 +41,20 @@ _check_rewrite(struct list *origattrs, struct rewrite *rewrite, struct list *exp
}
m=list_first(origattrs);
for(n=list_first(expectedattrs); n; n=list_next(n)) {
if (!eqtlv((struct tlv *)n->data, (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;
}
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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 = &regex;
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);
Expand Down
73 changes: 53 additions & 20 deletions tests/t_rewrite_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit f91c070

Please sign in to comment.