From: Herwin Weststrate Date: Thu, 17 Dec 2015 19:28:55 +0000 (+0100) Subject: Allow strings as operator in rlm_python X-Git-Tag: release_3_0_11~31^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8d4893d3850dc7a2647f72a4b1e75e1f67a5537a;p=thirdparty%2Ffreeradius-server.git Allow strings as operator in rlm_python Because ('Tmp-String-0', '!*', 'ANY') is just so more readable than ('Tmp-String-0', 21, 'ANY'). Plain integers still work for backwards compatibility. As a bonus, we get rid of the OP table in radiusd.py: this module was not supposed to be included in scripts running from FreeRADIUS, but was still referenced from prepaid.py. As a bonus, we get rid of a table that was no longer in sync with the definitions in tokens.h. --- diff --git a/src/modules/rlm_python/prepaid.py b/src/modules/rlm_python/prepaid.py index f8fb7c8ae5e..c3cbf57b8fc 100644 --- a/src/modules/rlm_python/prepaid.py +++ b/src/modules/rlm_python/prepaid.py @@ -161,15 +161,14 @@ def authorize(authData): # If you want to use different operators # you can do # return (radiusd.RLM_MODULE_UPDATED, - # radiusd.resolve( - # 'Session-Timeout := %s' % str(sessionTimeout), - # 'Some-other-option -= Value', + # ( + # ('Session-Timeout', ':=', str(sessionTimeout)), + # ('Some-other-option', '-=', Value'), + # ), + # ( + # ('Auth-Type', ':=', 'python'), # ), - # radiusd.resolve( - # 'Auth-Type := python' - # ) # ) - # Edit operators you need in OP_TRY in radiusd.py def authenticate(p): p = p diff --git a/src/modules/rlm_python/radiusd.py b/src/modules/rlm_python/radiusd.py index 40247b34fd8..e12bbd642b6 100644 --- a/src/modules/rlm_python/radiusd.py +++ b/src/modules/rlm_python/radiusd.py @@ -32,22 +32,6 @@ L_ERR = 4 L_PROXY = 5 L_CONS = 128 -OP={ '{':2, '}':3, '(':4, ')':5, ',':6, ';':7, '+=':8, '-=':9, ':=':10, - '=':11, '!=':12, '>=':13, '>':14, '<=':15, '<':16, '=~':17, '!~':18, '=*':19, '!*':20, - '==':21 , '#':22 } - -OP_TRY = (':=', '+=', '-=', '=' ) - -def resolve(*lines): - tuples = [] - for line in lines: - for op in OP_TRY: - arr = line.rsplit(op) - if len(arr)==2: - tuples.append((str(arr[0].strip()),OP[op],str(arr[1].strip()))) - break - return tuple(tuples) - # log function def radlog(level, msg): import sys diff --git a/src/modules/rlm_python/rlm_python.c b/src/modules/rlm_python/rlm_python.c index 7ac7b757f86..d7ce86f0c8c 100644 --- a/src/modules/rlm_python/rlm_python.c +++ b/src/modules/rlm_python/rlm_python.c @@ -315,7 +315,7 @@ static void mod_vptuple(TALLOC_CTX *ctx, REQUEST *request, VALUE_PAIR **vps, PyO int pairsize; char const *s1; char const *s2; - FR_TOKEN op; + FR_TOKEN op = T_OP_EQ; if (!PyTuple_CheckExact(pTupleElement)) { ERROR("rlm_python:%s: tuple element %d of %s is not a tuple", funcname, i, list_name); @@ -332,12 +332,24 @@ static void mod_vptuple(TALLOC_CTX *ctx, REQUEST *request, VALUE_PAIR **vps, PyO if (pairsize == 2) { pStr1 = PyTuple_GET_ITEM(pTupleElement, 0); pStr2 = PyTuple_GET_ITEM(pTupleElement, 1); - op = T_OP_EQ; } else { pStr1 = PyTuple_GET_ITEM(pTupleElement, 0); pStr2 = PyTuple_GET_ITEM(pTupleElement, 2); - pOp = PyTuple_GET_ITEM(pTupleElement, 1); - op = PyInt_AsLong(pOp); + pOp = PyTuple_GET_ITEM(pTupleElement, 1); + if (PyInt_Check(pOp)) { + op = PyInt_AsLong(pOp); + if (!fr_int2str(fr_tokens, op, NULL)) { + ERROR("rlm_python:%s: Invalid operator '%i', falling back to '='", funcname, op); + op = T_OP_EQ; + } + } else if (PyString_CheckExact(pOp)) { + if (!(op = fr_str2int(fr_tokens, PyString_AsString(pOp), 0))) { + ERROR("rlm_python:%s: Invalid operator '%s', falling back to '='", funcname, PyString_AsString(pOp)); + op = T_OP_EQ; + } + } else { + ERROR("rlm_python:%s: Invalid operator type, using default '='", funcname); + } } if ((!PyString_CheckExact(pStr1)) || (!PyString_CheckExact(pStr2))) {