s4-python: python is not always in /usr/bin
[samba.git] / source4 / scripting / python / samba / netcmd / setpassword.py
1 #!/usr/bin/env python
2 #
3 # Sets a user password on a Samba4 server
4 # Copyright Jelmer Vernooij 2008
5 #
6 # Based on the original in EJS:
7 # Copyright Andrew Tridgell 2005
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22
23 import samba.getopt as options
24 from samba.netcmd import Command, CommandError, Option
25 from getpass import getpass
26 from samba.auth import system_session
27 from samba.samdb import SamDB
28
29 class cmd_setpassword(Command):
30     """(Re)sets the password on a user account"""
31
32     synopsis = "setpassword [username] [options]"
33
34     takes_optiongroups = {
35         "sambaopts": options.SambaOptions,
36         "versionopts": options.VersionOptions,
37         "credopts": options.CredentialsOptions,
38     }
39
40     takes_options = [
41         Option("-H", help="LDB URL for database or target server", type=str),
42         Option("--filter", help="LDAP Filter to set password on", type=str),
43         Option("--newpassword", help="Set password", type=str),
44         Option("--must-change-at-next-login",
45             help="Force password to be changed on next login",
46             action="store_true"),
47         ]
48
49     takes_args = ["username?"]
50
51     def run(self, username=None, filter=None, credopts=None, sambaopts=None,
52             versionopts=None, H=None, newpassword=None,
53             must_change_at_next_login=None):
54         if filter is None and username is None:
55             raise CommandError("Either the username or '--filter' must be specified!")
56
57         password = newpassword
58         if password is None:
59             password = getpass("New Password: ")
60
61         if filter is None:
62             filter = "(&(objectClass=user)(sAMAccountName=%s))" % (username)
63
64         lp = sambaopts.get_loadparm()
65         creds = credopts.get_credentials(lp)
66
67         samdb = SamDB(url=H, session_info=system_session(),
68                       credentials=creds, lp=lp)
69
70         try:
71             samdb.setpassword(filter, password,
72                               force_change_at_next_login=must_change_at_next_login,
73                               username=username)
74         except:
75             raise CommandError('Failed to set password for user "%s"' %
76                 username)