From 7c8b53a49ed5fc6d811eafe15ffd247d06404946 Mon Sep 17 00:00:00 2001 From: Giampaolo Lauria Date: Fri, 15 Jul 2011 15:23:36 -0400 Subject: samba-tool: added error handling for the user command Caught exception whenever possible, added new check for newpassword to make sure it contains some chars Signed-off-by: Andrew Tridgell --- source4/scripting/python/samba/netcmd/user.py | 31 ++++++++++++++++++--------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'source4/scripting') diff --git a/source4/scripting/python/samba/netcmd/user.py b/source4/scripting/python/samba/netcmd/user.py index f77af02064..ba85263634 100644 --- a/source4/scripting/python/samba/netcmd/user.py +++ b/source4/scripting/python/samba/netcmd/user.py @@ -47,9 +47,16 @@ class cmd_user_add(Command): lp = sambaopts.get_loadparm() creds = credopts.get_credentials(lp ) net = Net(creds, lp, server=credopts.ipaddress) - net.create_user(name) + try: + net.create_user(name) + except RuntimeError, msg: + raise CommandError("Failed to add user '%s': %s" % (name, msg)) + if password is not None: - net.set_password(name, creds.get_domain(), password, creds) + try: + net.set_password(name, creds.get_domain(), password, creds) + except RuntimeError, msg: + raise CommandError("Failed to set password '%s': %s" % (name, msg)) @@ -66,7 +73,7 @@ class cmd_user_delete(Command): try: net.delete_user(name) except RuntimeError, msg: - raise CommandError("Failed to delete user %s: %s" % (name, msg)) + raise CommandError("Failed to delete user '%s': %s" % (name, msg)) @@ -98,8 +105,8 @@ class cmd_user_enable(Command): try: samdb.enable_account(filter) except Exception, msg: - raise CommandError("Failed to enable user %s: %s" % (username or filter, msg)) - print("Enabled user %s" % (username or filter)) + raise CommandError("Failed to enable user '%s': %s" % (username or filter, msg)) + print("Enabled user '%s'" % (username or filter)) @@ -136,8 +143,9 @@ class cmd_user_setexpiry(Command): try: samdb.setexpiry(filter, days*24*3600, no_expiry_req=noexpiry) except Exception, msg: - raise CommandError("Failed to set expiry for user %s: %s" % (username or filter, msg)) - print("Set expiry for user %s to %u days" % (username or filter, days)) + raise CommandError("Failed to set expiry for user '%s': %s" % (username or filter, msg)) + print("Set expiry for user '%s' to %u days" % (username or filter, days)) + class cmd_user_setpassword(Command): @@ -163,7 +171,9 @@ class cmd_user_setpassword(Command): raise CommandError("Either the username or '--filter' must be specified!") password = newpassword - if password is None: + while 1: + if password is not None and password is not '': + break password = getpass("New Password: ") if filter is None: @@ -181,11 +191,12 @@ class cmd_user_setpassword(Command): samdb.setpassword(filter, password, force_change_at_next_login=must_change_at_next_login, username=username) - except Exception, e: - raise CommandError('Failed to set password for user "%s"' % username, e) + except Exception, msg: + raise CommandError("Failed to set password for user '%s': %s" % (username or filter, msg)) print "Changed password OK" + class cmd_user(SuperCommand): """User management [server connection needed]""" -- cgit