s4:pwsettings - Improve error handling and introduce "choice" type
authorMatthias Dieter Wallnöfer <mwallnoefer@yahoo.de>
Tue, 22 Sep 2009 12:09:37 +0000 (14:09 +0200)
committerMatthias Dieter Wallnöfer <mwallnoefer@yahoo.de>
Wed, 30 Sep 2009 14:00:29 +0000 (16:00 +0200)
- Improve the error handling according to Jelmer's suggestions
- Print out the error messages on "stderr"
- Add also here the "choice" type for arguments

source4/setup/pwsettings

index 0bb39d975761f7f5755acee3b4957eb89381723a..59ed5d29bf4b6f6a61182472e2aa1c0fa04b8e9a 100755 (executable)
@@ -41,8 +41,8 @@ credopts = options.CredentialsOptions(parser)
 parser.add_option_group(credopts)
 parser.add_option("-H", help="LDB URL for database or target server", type=str)
 parser.add_option("--quiet", help="Be quiet", action="store_true")
-parser.add_option("--complexity",
-  help="The password complexity (on | off | default). Default is 'on'", type=str)
+parser.add_option("--complexity", type="choice", choices=["on","off","default"],
+  help="The password complexity (on | off | default). Default is 'on'")
 parser.add_option("--history-length",
   help="The password history length (<integer> | default).  Default is 24.", type=str)
 parser.add_option("--min-pwd-length",
@@ -87,10 +87,10 @@ try:
        # ticks -> days
        min_pwd_age = int(abs(int(res[0]["minPwdAge"][0])) / (1e7 * 60 * 60 * 24))
        max_pwd_age = int(abs(int(res[0]["maxPwdAge"][0])) / (1e7 * 60 * 60 * 24))
-except:
-       print "ERROR: Could not retrieve password properties!"
+except KeyError:
+       print >>sys.stderr, "ERROR: Could not retrieve password properties!"
        if args[0] == "show":
-               print "So no settings can be displayed!"
+               print >>sys.stderr, "So no settings can be displayed!"
        sys.exit(1)
 
 if args[0] == "show":
@@ -118,9 +118,6 @@ elif args[0] == "set":
                elif opts.complexity == "off":
                        pwd_props = pwd_props & (~DOMAIN_PASSWORD_COMPLEX)
                        msgs.append("Password complexity deactivated!")
-               else:
-                       print "ERROR: Wrong argument '" + opts.complexity + "'!"
-                       sys.exit(1)
 
                m["pwdProperties"] = ldb.MessageElement(str(pwd_props),
                  ldb.FLAG_MOD_REPLACE, "pwdProperties")
@@ -132,7 +129,7 @@ elif args[0] == "set":
                        pwd_hist_len = int(opts.history_length)
 
                if pwd_hist_len < 0 or pwd_hist_len > 24:
-                       print "ERROR: Password history length must be in the range of 0 to 24!"
+                       print >>sys.stderr, "ERROR: Password history length must be in the range of 0 to 24!"
                        sys.exit(1)
 
                m["pwdHistoryLength"] = ldb.MessageElement(str(pwd_hist_len),
@@ -146,7 +143,7 @@ elif args[0] == "set":
                        min_pwd_len = int(opts.min_pwd_length)
 
                if min_pwd_len < 0 or min_pwd_len > 14:
-                       print "ERROR: Minimum password length must be in the range of 0 to 14!"
+                       print >>sys.stderr, "ERROR: Minimum password length must be in the range of 0 to 14!"
                        sys.exit(1)
 
                m["minPwdLength"] = ldb.MessageElement(str(min_pwd_len),
@@ -160,7 +157,7 @@ elif args[0] == "set":
                        min_pwd_age = int(opts.min_pwd_age)
 
                if min_pwd_age < 0 or min_pwd_age > 998:
-                       print "ERROR: Minimum password age must be in the range of 0 to 998!"
+                       print >>sys.stderr, "ERROR: Minimum password age must be in the range of 0 to 998!"
                        sys.exit(1)
 
                # days -> ticks
@@ -177,7 +174,7 @@ elif args[0] == "set":
                        max_pwd_age = int(opts.max_pwd_age)
 
                if max_pwd_age < 0 or max_pwd_age > 999:
-                       print "ERROR: Maximum password age must be in the range of 0 to 999!"
+                       print >>sys.stderr, "ERROR: Maximum password age must be in the range of 0 to 999!"
                        sys.exit(1)
 
                # days -> ticks
@@ -197,5 +194,5 @@ elif args[0] == "set":
 
        message("\n".join(msgs))
 else:
-       print "ERROR: Wrong argument '" + args[0] + "'!"
+       print >>sys.stderr, "ERROR: Wrong argument '" + args[0] + "'!"
        sys.exit(1)