90a217fb6f714b709b9e1b59277b1b85ea63748d
[kai/samba.git] / source4 / setup / setpassword
1 #!/usr/bin/python
2 #
3 # Add a new user to 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 os, sys
24
25 # Find right directory when running from source tree
26 sys.path.insert(0, "bin/python")
27
28 import samba.getopt as options
29 import optparse
30 import pwd
31 import sys
32 from getpass import getpass
33 from samba.auth import system_session
34 from samba.samdb import SamDB
35
36 parser = optparse.OptionParser("setpassword [username] [options]")
37 sambaopts = options.SambaOptions(parser)
38 parser.add_option_group(sambaopts)
39 parser.add_option_group(options.VersionOptions(parser))
40 credopts = options.CredentialsOptions(parser)
41 parser.add_option_group(credopts)
42 parser.add_option("--filter", help="LDAP Filter to set password on", type=str)
43 parser.add_option("--newpassword", help="Set password", type=str)
44
45 opts, args = parser.parse_args()
46
47 #
48 #  print a message if quiet is not set
49 #
50 def message(text):
51         if not opts.quiet:
52                 print text
53
54 if len(args) == 0:
55         parser.print_usage()
56         sys.exit(1)
57
58 password = opts.newpassword;
59 if password is None:
60         password = getpass("New Password: ")
61
62 filter = opts.filter
63
64 if filter is None:
65         username = args[0]
66         if username is None:
67                 print "Either username or --filter must be specified"
68
69         filter = "(&(objectclass=user)(samAccountName=%s))" % (username)
70
71
72 lp = sambaopts.get_loadparm()
73 creds = credopts.get_credentials(lp)
74
75 samdb = SamDB(url=lp.get("sam database"), session_info=system_session(), 
76               credentials=creds, lp=lp)
77 samdb.setpassword(filter, password)