From: Metasov Date: Fri, 14 Oct 2011 14:49:23 +0000 (+0300) Subject: Add different operators handle to rlm_python X-Git-Tag: release_2_2_1~254 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5a143e2bab9e5cf99334153660f7e730889defb2;p=thirdparty%2Ffreeradius-server.git Add different operators handle to rlm_python --- diff --git a/src/modules/rlm_python/prepaid.py b/src/modules/rlm_python/prepaid.py index aae1c83d0ff..c08876ab4a7 100644 --- a/src/modules/rlm_python/prepaid.py +++ b/src/modules/rlm_python/prepaid.py @@ -158,8 +158,18 @@ def authorize(authData): return (radiusd.RLM_MODULE_UPDATED, (('Session-Timeout', str(sessionTimeout)),), (('Auth-Type', 'python'),)) - - + # 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', + # ), + # 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 08a1aa02db9..87c9b64dafe 100644 --- a/src/modules/rlm_python/radiusd.py +++ b/src/modules/rlm_python/radiusd.py @@ -32,6 +32,21 @@ 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): @@ -39,7 +54,5 @@ def radlog(level, msg): sys.stdout.write(msg + '\n') level = level - - diff --git a/src/modules/rlm_python/rlm_python.c b/src/modules/rlm_python/rlm_python.c index 7030323aca3..5d8f0b217fe 100644 --- a/src/modules/rlm_python/rlm_python.c +++ b/src/modules/rlm_python/rlm_python.c @@ -271,29 +271,42 @@ static void python_vptuple(VALUE_PAIR **vpp, PyObject *pValue, PyObject *pTupleElement = PyTuple_GET_ITEM(pValue, i); PyObject *pStr1; PyObject *pStr2; + PyObject *pOp; int pairsize; const char *s1; const char *s2; + long Op; if (!PyTuple_CheckExact(pTupleElement)) { radlog(L_ERR, "rlm_python:%s: tuple element %d is not a tuple", funcname, i); continue; } /* Check if it's a pair */ - if ((pairsize = PyTuple_GET_SIZE(pTupleElement)) != 2) { - radlog(L_ERR, "rlm_python:%s: tuple element %d is a tuple of size %d. Must be 2", funcname, i, pairsize); + + pairsize = PyTuple_GET_SIZE(pTupleElement); + if ((pairsize < 2) || (pairsize > 3)) { + radlog(L_ERR, "rlm_python:%s: tuple element %d is a tuple of size %d. Must be 2 or 3.", funcname, i, pairsize); continue; } + + 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); + } + if ((!PyString_CheckExact(pStr1)) || (!PyString_CheckExact(pStr2))) { radlog(L_ERR, "rlm_python:%s: tuple element %d must be as (str, str)", funcname, i); continue; } s1 = PyString_AsString(pStr1); s2 = PyString_AsString(pStr2); - /* xxx Might need to support other T_OP */ - vp = pairmake(s1, s2, T_OP_EQ); + vp = pairmake(s1, s2, Op); if (vp != NULL) { pairadd(vpp, vp); radlog(L_DBG, "rlm_python:%s: '%s' = '%s'", funcname, s1, s2);