]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Allow strings as operator in rlm_python 1484/head
authorHerwin Weststrate <herwin@snt.utwente.nl>
Thu, 17 Dec 2015 19:28:55 +0000 (20:28 +0100)
committerHerwin Weststrate <herwin@snt.utwente.nl>
Wed, 6 Jan 2016 20:48:37 +0000 (21:48 +0100)
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.

src/modules/rlm_python/prepaid.py
src/modules/rlm_python/radiusd.py
src/modules/rlm_python/rlm_python.c

index f8fb7c8ae5ea6cd8577f8dbb948f4c93756a4589..c3cbf57b8fc99d6b57f1865308c2fec3719cab67 100644 (file)
@@ -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
index 40247b34fd8ff4bd72472d5ee9a306afdf429c5c..e12bbd642b63d87024dba9530c7778308cf0e3a4 100644 (file)
@@ -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
index 7ac7b757f868e615c6e52e736e3fa28a6d3da3f3..d7ce86f0c8c2735812b437636c4ea23002cd8d62 100644 (file)
@@ -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))) {