Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into 4-0-local
[amitay/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 samba.getopt as options
10 import optparse
11 import pwd
12 import sys
13 from getpass import getpass
14 from auth import system_session
15 from samba.samdb import SamDB
16
17 parser = optparse.OptionParser("setpassword [username] [options]")
18 sambaopts = options.SambaOptions(parser)
19 parser.add_option_group(sambaopts)
20 parser.add_option_group(options.VersionOptions(parser))
21 credopts = options.CredentialsOptions(parser)
22 parser.add_option_group(credopts)
23 parser.add_option("--filter", help="LDAP Filter to set password on", type=str)
24 parser.add_option("--newpassword", help="Set password", type=str)
25
26 opts, args = parser.parse_args()
27
28 #
29 #  print a message if quiet is not set
30 #
31 def message(text):
32         if not opts.quiet:
33                 print text
34
35 if len(args) == 0:
36         parser.print_usage()
37         sys.exit(1)
38
39 password = opts.password;
40 if password is None:
41         password = getpass("New Password: ")
42
43 filter = opts.filter
44
45 if filter is None:
46         username = args[0]
47         if username is None:
48                 print "Either username or --filter must be specified"
49
50         filter = "(&(objectclass=user)(samAccountName=" + username + "))"
51
52
53 creds = credopts.get_credentials()
54
55 lp = sambaopts.get_loadparm()
56 samdb = SamDB(url=lp.get("sam database"), session_info=system_session(), 
57               credentials=creds, lp=lp)
58 samdb.setpassword(filter, password)