commit ba0f67cc882c1d43fe963cded15c4f70b2b9367a Author: Damian Johnson atagar@torproject.org Date: Sun Sep 18 19:43:41 2011 -0700
fix: fixing mishandling of GETCONF/RESETCONF
Allowing for non-quoted GETCONF values and RESETCONF values by the interpretor (I wrote the previous handling when I didn't have an internet connection so couldn't consult the specs. --- src/util/torInterpretor.py | 26 +++++++++++++++----------- src/util/torTools.py | 16 +++++++++++----- 2 files changed, 26 insertions(+), 16 deletions(-)
diff --git a/src/util/torInterpretor.py b/src/util/torInterpretor.py index 6781b83..2424a29 100644 --- a/src/util/torInterpretor.py +++ b/src/util/torInterpretor.py @@ -651,8 +651,9 @@ class ControlInterpretor: outputEntry.append((response, OUTPUT_FORMAT)) except Exception, exc: outputEntry.append((str(exc), ERROR_FORMAT)) - elif cmd == "SETCONF": - # arguments can either be '<param>' or '<param>="<value>"' entries + elif cmd == "SETCONF" or cmd == "RESETCONF": + # arguments can either be '<param>', '<param>=<value>', or + # '<param>="<value>"' entries paramList = []
while arg: @@ -662,14 +663,21 @@ class ControlInterpretor: # echos back faithfully rather than being parsed) so leaving this # alone for now.
- m = re.match(r'^(\S+)="([^"]+)"', arg) + quotedMatch = re.match(r'^(\S+)="([^"]+)"', arg) + nonquotedMatch = re.match(r'^(\S+)=(\S+)', arg)
- if m: + if quotedMatch: # we're dealing with a '<param>="<value>"' entry - param, value = m.groups() + param, value = quotedMatch.groups()
paramList.append((param, value)) arg = arg[len(param) + len(value) + 3:].strip() + elif nonquotedMatch: + # we're dealing with a '<param>=<value>' entry + param, value = nonquotedMatch.groups() + + paramList.append((param, value)) + arg = arg[len(param) + len(value) + 1:].strip() else: # starts with just a param param = arg.split()[0] @@ -677,12 +685,8 @@ class ControlInterpretor: arg = arg[len(param):].strip()
try: - conn.setOptions(paramList) - except Exception, exc: - outputEntry.append((str(exc), ERROR_FORMAT)) - elif cmd == "RESETCONF": - try: - conn.setOptions([(param, None) for param in arg.split()]) + isReset = cmd == "RESETCONF" + conn.setOptions(paramList, isReset) except Exception, exc: outputEntry.append((str(exc), ERROR_FORMAT)) else: diff --git a/src/util/torTools.py b/src/util/torTools.py index fcd8247..f6d9d86 100644 --- a/src/util/torTools.py +++ b/src/util/torTools.py @@ -728,8 +728,8 @@ class Controller(TorCtl.PostEventListener): def setOption(self, param, value = None): """ Issues a SETCONF to set the given option/value pair. An exeptions raised - if it fails to be set. If no value is provided then this resets the - configuration option. + if it fails to be set. If no value is provided then this sets the option to + 0 or NULL.
Arguments: param - configuration option to be set @@ -739,16 +739,19 @@ class Controller(TorCtl.PostEventListener):
self.setOptions(((param, value),))
- def setOptions(self, paramList): + def setOptions(self, paramList, isReset = False): """ Issues a SETCONF to replace a set of configuration options. This takes a list of parameter/new value tuple pairs. Values can be... - a string to set a single value - a list of strings to set a series of values (for instance the ExitPolicy) - - None to reset the parameter + - None to set the value to 0 or NULL
Arguments: paramList - list of parameter/value tuple pairs + isReset - issues a RESETCONF instead of SETCONF, causing any None + mappings to revert the parameter to its default rather than + set it to 0 or NULL """
self.connLock.acquire() @@ -769,7 +772,10 @@ class Controller(TorCtl.PostEventListener): startTime, raisedExc = time.time(), None if self.isAlive(): try: - self.conn.sendAndRecv("SETCONF %s\r\n" % setConfStr) + if isReset: + self.conn.sendAndRecv("RESETCONF %s\r\n" % setConfStr) + else: + self.conn.sendAndRecv("SETCONF %s\r\n" % setConfStr)
# flushing cached values (needed until we can detect SETCONF calls) for param, _ in paramList:
tor-commits@lists.torproject.org