]> www.infradead.org Git - users/dhowells/kafs-utils.git/commitdiff
Provide argument exception class and check argument length restrictions
authorDavid Howells <dhowells@redhat.com>
Mon, 14 Apr 2014 08:23:54 +0000 (09:23 +0100)
committerDavid Howells <dhowells@redhat.com>
Mon, 14 Apr 2014 08:23:54 +0000 (09:23 +0100)
Create an argument error class specifically for the indication of command line
argument problems and catch it in main.py.

Further, provide checking for string arguments that have length restrictions
in the protocol.

Signed-off-by: David Howells <dhowells@redhat.com>
19 files changed:
suite/argparse.py
suite/commands/bos/addhost.py
suite/commands/bos/adduser.py
suite/commands/bos/create.py
suite/commands/bos/delete.py
suite/commands/bos/getdate.py
suite/commands/bos/removeuser.py
suite/commands/bos/setcellname.py
suite/commands/bos/shutdown.py
suite/commands/bos/start.py
suite/commands/bos/startup.py
suite/commands/bos/status.py
suite/commands/bos/stop.py
suite/commands/vos/examine.py
suite/commands/vos/listvldb.py
suite/exception.py
suite/lib/partition.py
suite/lib/uuid.py
suite/main.py

index bcc2fa1ea4996121cd7068ae8c8d7361a16adff2..43a7aff61d845b600ce7c6aa507fedcd03c6783e 100644 (file)
@@ -13,6 +13,8 @@ as published by the Free Software Foundation; either version
 2 of the Licence, or (at your option) any later version.
 """
 
+from exception import AFSArgumentError
+
 def get_cell(switch, params):
     from afs.lib.cell import cell
     return cell(params[0])
@@ -129,7 +131,8 @@ def get_dummy(switch, params):
 #        another value for the preceding argument.
 #
 ###############################################################################
-def parse_arguments(args, available_arguments):
+def parse_arguments(args, available_arguments, argument_size_limits,
+                    cant_combine_arguments):
     result = {}
     need_switch = False
     i = 0       # Given arguments index
@@ -139,7 +142,7 @@ def parse_arguments(args, available_arguments):
 
     if len(args) == 0:
         if len(available_arguments) > 0 and available_arguments[0][0] == "r":
-            raise RuntimeError("Missing required parameters")
+            raise AFSArgumentError("Missing required parameters")
         return result
 
     # Process all the optional arguments or switch-based required arguments
@@ -150,13 +153,13 @@ def parse_arguments(args, available_arguments):
         if args[i][0] != "-":
             # Deal with positional arguments
             if need_switch:
-                raise RuntimeError("Need switch before argument " + i)
+                raise AFSArgumentError("Need switch before argument " + i)
             if av >= len(available_arguments):
-                raise RuntimeError("Unexpected positional argument")
+                raise AFSArgumentError("Unexpected positional argument")
             match = available_arguments[av]
             pattern = match[2]
             if pattern[0] == "f":
-                raise RuntimeError("Unexpected positional argument")
+                raise AFSArgumentError("Unexpected positional argument")
             av = av + 1
 
             params.append(args[i])
@@ -177,9 +180,9 @@ def parse_arguments(args, available_arguments):
             i = i + 1
 
             if switch == "help":
-                raise RuntimeError("Print help message")
+                raise AFSArgumentError("Print help message")
             if switch == "":
-                raise RuntimeError("Missing switch name")
+                raise AFSArgumentError("Missing switch name")
 
             # Look up the switch in the table of possible arguments and flags
             for j in available_arguments:
@@ -188,14 +191,14 @@ def parse_arguments(args, available_arguments):
                     break
                 if j[0].startswith(switch):
                     if match:
-                        raise RuntimeError("Ambiguous switch name abbreviation '-" + switch + "'")
+                        raise AFSArgumentError("Ambiguous switch name abbreviation '-" + switch + "'")
                     match = j
             if not match:
-                raise RuntimeError("Unsupported switch '-" + switch + "'")
+                raise AFSArgumentError("Unsupported switch '-" + switch + "'")
 
             # Reject repeat flags
             if match[0] in result:
-                raise RuntimeError("Duplicate switch '-" + switch + "' not permitted")
+                raise AFSArgumentError("Duplicate switch '-" + switch + "' not permitted")
 
             # Arrange the parameters associated with the switch into a list
             while i < len(args):
@@ -207,11 +210,18 @@ def parse_arguments(args, available_arguments):
         # Check that we have the number of arguments we're expecting
         pattern = match[2]
         if pattern[1] == "n" and len(params) != 0:
-            raise RuntimeError("Switch '-" + switch + "' expects no arguments")
+            raise AFSArgumentError("Switch '-" + switch + "' expects no arguments")
         if pattern[1] == "s" and len(params) != 1:
-            raise RuntimeError("Switch '-" + switch + "' expects one argument")
+            raise AFSArgumentError("Switch '-" + switch + "' expects one argument")
         if pattern[1] == "m" and len(params) < 1:
-            raise RuntimeError("Switch '-" + switch + "' expects one or more arguments")
+            raise AFSArgumentError("Switch '-" + switch + "' expects one or more arguments")
+
+        # Check that none of the arguments are too big
+        if match[0] in argument_size_limits:
+            limit = argument_size_limits[match[0]]
+            for j in params:
+                if len(j) > limit:
+                    raise AFSArgumentError("Switch '-" + switch + "' has an overlong argument")
 
         # Call the syntax checker
         syntax = match[1]
@@ -225,6 +235,11 @@ def parse_arguments(args, available_arguments):
         if j[2][0] != "r":
             break
         if switch not in result:
-            raise RuntimeError("Missing '-" + switch + "' argument")
+            raise AFSArgumentError("Missing '-" + switch + "' argument")
+
+    # Check for invalid argument combinations
+    for i in cant_combine_arguments:
+        if i[0] in params and i[1] in params:
+            raise AFSArgumentError("Can't combine -" + i[0], "with -" + i[1], file=sys.stderr)
 
     return result
index 7bc037d0a5ca465a085c28e1ff93a3938186abee..34ef6bbcf90e2e4f89b535e2b1e2d64fb6e82693 100644 (file)
@@ -42,6 +42,10 @@ cant_combine_arguments = [
     ( "noauth",         "localauth" ),
 ]
 
+argument_size_limits = {
+    "host"              : kafs.BOZO_BSSIZE,
+}
+
 description = r"""
 Add a database server machine to the CellServDB file
 """
index 907db1710e6a95b3e26b803e2a828bc7f4f8c99c..0fadb6340455e58151ed0dce62c1db7462254da5 100644 (file)
@@ -42,6 +42,10 @@ cant_combine_arguments = [
     ( "noauth",         "localauth" ),
 ]
 
+argument_size_limits = {
+    "user"              : kafs.BOZO_BSSIZE,
+}
+
 description = r"""
 Add a privileged user to the UserList file
 """
index df3e9c7ffb0cebb94e0b389a368283984d73f274..e5e5e0eb529d698f7e5d9e02b0c5952d6b135c94 100644 (file)
@@ -45,6 +45,13 @@ cant_combine_arguments = [
     ( "noauth",         "localauth" ),
 ]
 
+argument_size_limits = {
+    "instance"          : kafs.BOZO_BSSIZE,
+    "type"              : kafs.BOZO_BSSIZE,
+    "cmd"               : kafs.BOZO_BSSIZE,
+    "notifier"          : kafs.BOZO_BSSIZE,
+}
+
 description = r"""
 Define a new process in the BosConfig file and start it
 """
index b73965b290a29461c7aef7ca46a81eac13403f87..539e495b8bfb0c0c644467a935262158c7cf2636 100644 (file)
@@ -42,6 +42,10 @@ cant_combine_arguments = [
     ( "noauth",         "localauth" ),
 ]
 
+argument_size_limits = {
+    "instance"          : kafs.BOZO_BSSIZE,
+}
+
 description = r"""
 Delete a server process from the BosConfig file
 """
index 9b1fd96adb06ad2900ead026cee5ae27367fa7a5..cad020eb5d428dd6f8dba5069e3db6131ec57bae 100644 (file)
@@ -43,6 +43,11 @@ cant_combine_arguments = [
     ( "noauth",         "localauth" ),
 ]
 
+argument_size_limits = {
+    "file"              : kafs.BOZO_BSSIZE,
+    "dir"               : kafs.BOZO_BSSIZE,
+}
+
 description = r"""
 Display the time stamps on an AFS binary file installed on the server.
 """
index 1b2612ddf03556ce6b59e967ed2a62cd7b61140d..e144a3223d5c1b438a8326239773161bc0d3e307 100644 (file)
@@ -42,6 +42,10 @@ cant_combine_arguments = [
     ( "noauth",         "localauth" ),
 ]
 
+argument_size_limits = {
+    "user"              : kafs.BOZO_BSSIZE,
+}
+
 description = r"""
 Remove a privileged user from the UserList file
 """
index 073a180c2a578ed09c80ae1fff90f9eca12f76f9..69fa080b52aacc3315055635c98e65a062cde9cc 100644 (file)
@@ -41,6 +41,10 @@ cant_combine_arguments = [
     ( "noauth",         "localauth" ),
 ]
 
+argument_size_limits = {
+    "name"              : kafs.BOZO_BSSIZE,
+}
+
 description = r"""
 Display whether a bos server is restricted or not
 """
index 062d388be777a773f978098b3b48dc445fb4598e..2097d62d52631032ec0ab8e6cffa219f6029c49a 100644 (file)
@@ -43,6 +43,10 @@ cant_combine_arguments = [
     ( "noauth",         "localauth" ),
 ]
 
+argument_size_limits = {
+    "instance"          : kafs.BOZO_BSSIZE,
+}
+
 description = r"""
 Stop a process without changing its status flag
 """
index 8a775bc73ce0c622eb6da2e1bc2776b97adf5eae..7381f809227de6009311f9a7f90dbb8124065311 100644 (file)
@@ -42,6 +42,10 @@ cant_combine_arguments = [
     ( "noauth",         "localauth" ),
 ]
 
+argument_size_limits = {
+    "instance"          : kafs.BOZO_BSSIZE,
+}
+
 description = r"""
 Start a process after setting its status flag
 """
index db9fb7470324abd72d79f26c023904106c4d921c..45c601754fc5ff7fa693466b5be35bd2cd357321 100644 (file)
@@ -42,6 +42,10 @@ cant_combine_arguments = [
     ( "noauth",         "localauth" ),
 ]
 
+argument_size_limits = {
+    "instance"          : kafs.BOZO_BSSIZE,
+}
+
 description = r"""
 Start a process without changing its status flag
 """
index 789b45306ecb848fd3a19d556e66f5bd5907c548..1ab6243242f2e17d967208af6d92362b2b502690 100644 (file)
@@ -45,6 +45,10 @@ cant_combine_arguments = [
     ( "noauth",         "localauth" ),
 ]
 
+argument_size_limits = {
+    "instance"          : kafs.BOZO_BSSIZE,
+}
+
 description = r"""
 Display the status of server processes
 """
index e501f36c1c10d234a0c35e2babaf4bd2e22c3925..8cfdf197fbc3d924f581ce6c3b2a4a3d89cb0ad8 100644 (file)
@@ -43,6 +43,10 @@ cant_combine_arguments = [
     ( "noauth",         "localauth" ),
 ]
 
+argument_size_limits = {
+    "instance"          : kafs.BOZO_BSSIZE,
+}
+
 description = r"""
 Stop a process after changing its status flag
 """
index 86c6d5e1c09df739cabbe3157905192799d57163..3fb9a592c235496a9033eab06e7c15bce27f6787 100644 (file)
@@ -47,6 +47,10 @@ cant_combine_arguments = [
     ( "noauth",         "localauth" ),
 ]
 
+argument_size_limits = {
+    "id"                : kafs.VLDB_MAXNAMELEN,
+}
+
 description = r"""
 Show volume header and VLDB entry information for a volume
 """
index db310e9a08bc77d4a8df21d3e6083116fa3ea3e1..fa062c53d645a32112e0b95bf4a6860c719ec538 100644 (file)
@@ -50,6 +50,10 @@ cant_combine_arguments = [
     ( "name",           "locked" ),
 ]
 
+argument_size_limits = {
+    "name"              : kafs.VLDB_MAXNAMELEN,
+}
+
 description = r"""
 Displays a volume's VLDB entry
 """
index b031378fd4ac253c691bf8789d948d82656f3565..88bba149dee147e2a973d781459bb7a21d87066f 100644 (file)
@@ -1,3 +1,7 @@
 class AFSException(Exception):
     """Base class for all AFS Toolkit exceptions."""
     pass
+
+class AFSArgumentError(AFSException):
+    """Base class for all AFS Toolkit exceptions."""
+    pass
index 6af4e972bc850d43bbd31c015dc9c3b2ab6f8348..4463c33adb69b63208021cf8a48e9b673ff8c822 100644 (file)
@@ -21,28 +21,32 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 """
 
+from exception import AFSArgumentError
+
 def part2id(name):
     """Convert a partition name or number string into a numeric ID"""
     if name.isnumeric():
         n = int(name)
     else:
         if name.startswith("/vicep"):
-            name = name[6:]
+            a = name[6:]
         elif name.startswith("vicep"):
-            name = name[5:]
-
-        if len(name) == 1 and name >= "a" and name <= "z":
-            n = int(name, 36) - 10
-        elif (len(name) == 2 and
-              name[0] >= "a" and name[0] <= "z" and
-              name[1] >= "a" and name[1] <= "z"):
-            n = (int(name[0], 36) - 10) * 26 + (int(name[1], 36) - 10)
+            a = name[5:]
+        else:
+            a = name
+
+        if len(a) == 1 and a >= "a" and a <= "z":
+            n = int(a, 36) - 10
+        elif (len(a) == 2 and
+              a[0] >= "a" and a[0] <= "z" and
+              a[1] >= "a" and a[1] <= "z"):
+            n = (int(a[0], 36) - 10) * 26 + (int(a[1], 36) - 10)
             n += 26
         else:
-            raise RuntimeError("Unparseable partition ID '" + params[0] + "'")
+            raise AFSArgumentError("Unparseable partition ID '" + name + "'")
 
     if n < 0 or n > 255:
-        raise RuntimeError("Partition ID '" + params[0] + "' out of range")
+        raise AFSArgumentError("Partition ID '" + name + "' out of range")
     return n
 
 def id2part(n):
index 35db3ba3a9ea6dcf84418893d11a0e6e5f2c4918..82cf05f36fe957b7b5aabaeecf3aea176e0aff50 100644 (file)
@@ -21,6 +21,8 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 """
 
+from exception import AFSArgumentError
+
 def uuid2str(uuid):
     """Convert an afsUUID-class object into a UUID string"""
     s = "{:08x}-{:04x}-{:04x}-{:02x}-{:02x}-".format(uuid.time_low,
@@ -40,7 +42,7 @@ def str2uuid(s):
         s[18] != "-" or
         s[21] != "-" or
         s[24] != "-"):
-        raise RuntimeError("Invalid UUID format")
+        raise AFSArgumentError("Invalid UUID format")
 
     from kafs import afsUUID
     uuid = afsUUID()
index 113e03d621f3af284e2397b4fd489b0bbd7b250b..582fd1e937a961209419a4581b6ab3c1e8574c0b 100644 (file)
@@ -29,6 +29,7 @@ import sys, os, traceback
 
 import afs.commands
 from afs.lib.debug import debug, debugging_level
+from exception import AFSArgumentError
 
 #
 # The commands map
@@ -151,13 +152,20 @@ def _main():
         command = cmdsetmod.get_command(cmd)
         #print("MOD:", command)
 
-    # Parse the parameters
-    params = afs.argparse.parse_arguments(sys.argv[1:], command.command_arguments)
+    if hasattr(command, "argument_size_limits"):
+        argument_size_limits = command.argument_size_limits
+    else:
+        argument_size_limits = {}
 
-    for i in command.cant_combine_arguments:
-        if i[0] in params and i[1] in params:
-            print("Can't combine -" + i[0], "with -" + i[1], file=sys.stderr)
-            sys.exit(2)
+    # Parse the parameters
+    try:
+        params = afs.argparse.parse_arguments(sys.argv[1:],
+                                              command.command_arguments,
+                                              argument_size_limits,
+                                              command.cant_combine_arguments)
+    except AFSArgumentError as e:
+        print(prog + ":", e, file=sys.stderr)
+        sys.exit(2)
 
     # Stick in the default cell if there isn't one
     if "cell" not in params: