Make invalid 'member' detection work again.
[kai/samba.git] / source4 / setup / setpassword
1 #!/usr/bin/python
2 #
3 #       add a new user to a Samba4 server
4 #       Copyright Andrew Tridgell 2005
5 #       Copyright Jelmer Vernooij 2008
6 #       Released under the GNU GPL version 3 or later
7 #
8
9 import os, sys
10
11 # Find right directory when running from source tree
12 sys.path.insert(0, "bin/python")
13
14 import samba.getopt as options
15 import optparse
16 import pwd
17 import sys
18 from getpass import getpass
19 from samba.auth import system_session
20 from samba.samdb import SamDB
21
22 parser = optparse.OptionParser("setpassword [username] [options]")
23 sambaopts = options.SambaOptions(parser)
24 parser.add_option_group(sambaopts)
25 parser.add_option_group(options.VersionOptions(parser))
26 credopts = options.CredentialsOptions(parser)
27 parser.add_option_group(credopts)
28 parser.add_option("--filter", help="LDAP Filter to set password on", type=str)
29 parser.add_option("--newpassword", help="Set password", type=str)
30
31 opts, args = parser.parse_args()
32
33 #
34 #  print a message if quiet is not set
35 #
36 def message(text):
37         if not opts.quiet:
38                 print text
39
40 if len(args) == 0:
41         parser.print_usage()
42         sys.exit(1)
43
44 password = opts.newpassword;
45 if password is None:
46         password = getpass("New Password: ")
47
48 filter = opts.filter
49
50 if filter is None:
51         username = args[0]
52         if username is None:
53                 print "Either username or --filter must be specified"
54
55         filter = "(&(objectclass=user)(samAccountName=%s))" % (username)
56
57
58 lp = sambaopts.get_loadparm()
59 creds = credopts.get_credentials(lp)
60
61 samdb = SamDB(url=lp.get("sam database"), session_info=system_session(), 
62               credentials=creds, lp=lp)
63 samdb.setpassword(filter, password)