>From fe00dc59845b741e14194370715ad9dee246a253 Mon Sep 17 00:00:00 2001 From: Rowland Penny Date: Thu, 21 Jan 2016 15:41:08 +0000 Subject: [PATCH] samba-tool: make 'samba-tool user password' a bit more user and script friendly Signed-off-by: Rowland Penny --- python/samba/netcmd/user.py | 56 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/python/samba/netcmd/user.py b/python/samba/netcmd/user.py index cf640b0..f16aa36 100644 --- a/python/samba/netcmd/user.py +++ b/python/samba/netcmd/user.py @@ -480,13 +480,47 @@ Example4 shows how to set the account expiration so that it will never expire. class cmd_user_password(Command): - """Change password for a user account (the one provided in authentication). + """Change the password of a user account. + +This command changes the logon password for a user account. + +As it is expected to be run by a user, the username must not be given + +If the users old password is not specified with the --old-password parameter, +the user will be prompted for the old password to be entered through the +command line. + +If the new password is not specified with the --newpassword parameter, +the user will be prompted for the new password to be entered through the +command line (twice). + +Example1: +samba-tool user password + +Example1 shows how a user can change their password, the user will be prompted +for their old password and then for the new password, they will be prompted +twice for the new password. + +Example2: +samba-tool user password --oldpassword=passw0rd + +Example2 shows how a user can change their password by supplying their old +password on the command line. The user will be prompted for their new password, +they will be prompted twice for the new password. + +Example3: +samba-tool user password --oldpassword=passw0rd --newpassword=newpassw0rd + +Example3 shows how a user can change their password by supplied their old +and new passwords on the command line. The user will not be prompted for any +passwords. """ synopsis = "%prog [options]" takes_options = [ - Option("--newpassword", help="New password", type=str), + Option("--oldpassword", help="The users password now", type=str), + Option("--newpassword", help="Users new password", type=str), ] takes_optiongroups = { @@ -496,13 +530,21 @@ class cmd_user_password(Command): } def run(self, credopts=None, sambaopts=None, versionopts=None, - newpassword=None): + oldpassword=None, newpassword=None): lp = sambaopts.get_loadparm() creds = credopts.get_credentials(lp) # get old password now, to get the password prompts in the right order - old_password = creds.get_password() + old_password = oldpassword + if old_password is not None: + old_password = creds.set_password(old_password) + + while True: + if old_password is not None and old_password is not '': + break + # get old password, to get the password prompts in the right order + old_password = creds.get_password() net = Net(creds, lp, server=credopts.ipaddress) @@ -512,9 +554,11 @@ class cmd_user_password(Command): break password = getpass("New Password: ") passwordverify = getpass("Retype Password: ") - if not password == passwordverify: + if password == passwordverify: + break + else: password = None - self.outf.write("Sorry, passwords do not match.\n") + self.outf.write("Sorry, passwords do not match. Try again.\n") try: net.change_password(password) -- 1.7.10.4