samba-tool: Add user password command to change user's own password
authorAmitay Isaacs <amitay@gmail.com>
Wed, 27 Jul 2011 08:41:56 +0000 (18:41 +1000)
committerAndrew Tridgell <tridge@samba.org>
Thu, 28 Jul 2011 05:20:53 +0000 (15:20 +1000)
This command is a user-level command and differs from setpassword
command which is administrator command.

Signed-off-by: Andrew Tridgell <tridge@samba.org>
source4/scripting/python/samba/netcmd/user.py

index b93996e15212320a314828365fd768b569e6dd89..b13bc0d6066cb7aca879c0e600f64c1ac545c3a4 100644 (file)
@@ -194,6 +194,38 @@ class cmd_user_setexpiry(Command):
 
 
 
+class cmd_user_password(Command):
+    """Change password for a user account (the one provided in authentication)"""
+
+    synopsis = "%prog user password [options]"
+
+    takes_options = [
+        Option("--newpassword", help="New password", type=str),
+        ]
+
+    def run(self, credopts=None, sambaopts=None, versionopts=None,
+                newpassword=None):
+
+        lp = sambaopts.get_loadparm()
+        creds = credopts.get_credentials(lp)
+
+        # FIXME: How to ensure user is authenticated before prompting for new password?
+        net = Net(creds, lp, server=credopts.ipaddress)
+
+        password = newpassword
+        while 1:
+            if password is not None and password is not '':
+                break
+            password = getpass("New Password: ")
+
+        try:
+            net.change_password(password)
+        except Exception, msg:
+            raise CommandError("Failed to change password : %s" % msg)
+        print "Changed password OK"
+
+
+
 class cmd_user_setpassword(Command):
     """(Re)sets the password of a user account"""
 
@@ -252,4 +284,5 @@ class cmd_user(SuperCommand):
     subcommands["delete"] = cmd_user_delete()
     subcommands["enable"] = cmd_user_enable()
     subcommands["setexpiry"] = cmd_user_setexpiry()
+    subcommands["password"] = cmd_user_password()
     subcommands["setpassword"] = cmd_user_setpassword()