ctdb-scripts: Improve update and listing code
[samba4-gss.git] / python / samba / netcmd / user / password.py
blob3b26b62000d9b136188beaa917d1bdbde566507d
1 # user management
3 # user password
5 # Copyright Jelmer Vernooij 2010 <jelmer@samba.org>
6 # Copyright Theresa Halloran 2011 <theresahalloran@gmail.com>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 from getpass import getpass
24 import samba.getopt as options
25 from samba.net import Net
26 from samba.netcmd import Command, CommandError, Option
29 class cmd_user_password(Command):
30 """Change password for a user account (the one provided in authentication).
31 """
33 synopsis = "%prog [options]"
35 takes_options = [
36 Option("--newpassword", help="New password", type=str),
39 takes_optiongroups = {
40 "sambaopts": options.SambaOptions,
41 "credopts": options.CredentialsOptions,
42 "versionopts": options.VersionOptions,
45 def run(self, credopts=None, sambaopts=None, versionopts=None,
46 newpassword=None):
48 lp = sambaopts.get_loadparm()
49 creds = credopts.get_credentials(lp)
51 # get old password now, to get the password prompts in the right order
52 old_password = creds.get_password()
54 net = Net(creds, lp, server=credopts.ipaddress)
56 password = newpassword
57 while True:
58 if password is not None and password != '':
59 break
60 password = getpass("New Password: ")
61 passwordverify = getpass("Retype Password: ")
62 if not password == passwordverify:
63 password = None
64 self.outf.write("Sorry, passwords do not match.\n")
66 try:
67 if not isinstance(password, str):
68 password = password.decode('utf8')
69 net.change_password(password)
70 except Exception as msg:
71 # FIXME: catch more specific exception
72 raise CommandError("Failed to change password : %s" % msg)
73 self.outf.write("Changed password OK\n")