Skip to content

Commit

Permalink
Add support for XLATs on the LHS of update sections
Browse files Browse the repository at this point in the history
  • Loading branch information
arr2036 committed Jan 16, 2015
1 parent d5e619f commit 8a2cca8
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/main/map.c
Expand Up @@ -826,10 +826,63 @@ int map_to_request(REQUEST *request, value_pair_map_t const *map, radius_map_get
TALLOC_CTX *parent;
vp_cursor_t dst_list, src_list;

value_pair_map_t exp_map;
value_pair_tmpl_t exp_lhs;

VERIFY_MAP(map);
rad_assert(map->lhs != NULL);
rad_assert(map->rhs != NULL);

/*
* Preprocessing of the LHS of the map.
*/
switch (map->lhs->type) {
/*
* Already in the correct form.
*/
case TMPL_TYPE_LIST:
case TMPL_TYPE_ATTR:
break;

/*
* Everything else gets expanded, then re-parsed as an
* attribute reference.
*/
case TMPL_TYPE_XLAT:
case TMPL_TYPE_XLAT_STRUCT:
case TMPL_TYPE_EXEC:
{
char *attr;
ssize_t slen;

slen = tmpl_expand(request, &attr, request, map->lhs);
if (slen <= 0) {
REDEBUG("Left side \"%.*s\" of map failed expansion", (int)map->lhs->len, map->lhs->name);
rad_assert(!attr);
return -1;
}

slen = tmpl_from_attr_str(&exp_lhs, attr, REQUEST_CURRENT, PAIR_LIST_REQUEST, false, false) ;
if (slen <= 0) {
REDEBUG("Left side \"%.*s\" expansion not an attribute reference: %s",
(int)map->lhs->len, map->lhs->name, fr_strerror());
talloc_free(attr);
return -1;
}
rad_assert((exp_lhs.type == TMPL_TYPE_ATTR) || (exp_lhs.type == TMPL_TYPE_LIST));

memcpy(&exp_map, map, sizeof(exp_map));
exp_map.lhs = &exp_lhs;
map = &exp_map;
}
break;

default:
rad_assert(0);
break;
}


/*
* Sanity check inputs. We can have a list or attribute
* as a destination.
Expand Down
61 changes: 61 additions & 0 deletions src/tests/keywords/update-xlat
@@ -0,0 +1,61 @@
#
# PRE: update
#
# Form attribute references with xlats
#
update {
control:Cleartext-Password := 'hello'
reply:Filter-Id := 'filter'
}


update request {
Tmp-String-0 := 'Tmp-String-1'
}

#
# Shouldn't update Tmp-String-0, should instead update Tmp-String-1
# ... maybe this is what Alan meant when he was talking about people
# doing stupid things with this feature.
#
update request {
"%{Tmp-String-0}" := 'hello'
}

if (&Tmp-String-1 != 'hello') {
update reply {
Filter-Id += 'Fail 0'
}
}

if (&Tmp-String-0 == 'hello') {
update reply {
Filter-Id += 'Fail 1'
}
}

#
# Try updating an attribute specified by an OID string
#
update {
Tmp-Integer-0 := 11344
}
update {
"Vendor-%{Tmp-Integer-0}-Attr-1" := 127.0.0.1
}

if (&FreeRADIUS-Proxied-To != 127.0.0.1) {
update reply {
Filter-Id += 'Fail 2'
}
}

update {
"Attr-%{attr_num:Tmp-String-1}" := 'hello2'
}

if (&Tmp-String-1 != 'hello2') {
update reply {
Filter-Id += 'Fail 3'
}
}

0 comments on commit 8a2cca8

Please sign in to comment.