144b6c6494e04949c795c9dd30e5ea11fad95fcf
[samba.git] / source4 / setup / enableaccount
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 import ldb
14
15 from auth import system_session
16 from samba.samdb import SamDB
17
18 parser = optparse.OptionParser("setpassword [username] [options]")
19 sambaopts = options.SambaOptions(parser)
20 parser.add_option_group(sambaopts)
21 parser.add_option_group(options.VersionOptions(parser))
22 credopts = options.CredentialsOptions(parser)
23 parser.add_option_group(credopts)
24 parser.add_option("-H", help="LDB URL for database or target server", type=str)
25 parser.add_option("--base", help="Base DN to search for user under", type=str)
26
27 opts, args = parser.parse_args()
28
29 #
30 #  print a message if quiet is not set
31 #
32 def message(text):
33         if not opts.quiet:
34                 print text
35
36 if len(args) == 0:
37         parser.print_usage()
38         sys.exit(1)
39
40 username = args[0]
41
42 if username is None:
43         print "username must be specified"
44
45 creds = credopts.get_credentials()
46
47 lp = sambaopts.get_loadparm()
48 if opts.H is not None:
49         url = opts.H
50 else:
51         url = lp.get("sam database")
52
53 samdb = SamDB(url=url, session_info=system_session(), 
54               credentials=creds, lp=lp)
55
56 domain_dn = opts.base
57 if opts.base is None:
58         res = samdb.search("", scope=ldb.SCOPE_BASE, 
59                           expression="(defaultNamingContext=*)", 
60                           attrs=["defaultNamingContext"])
61         assert(len(res) == 1 and res[0]["defaultNamingContext"] is not None)
62         domain_dn = res[0]["defaultNamingContext"][0]
63 else:
64         domain_dn = opts.base
65
66 filter = "(&(objectClass=user)(samAccountName=%s))" % username
67
68 res = samdb.search(domain_dn, scope=ldb.SCOPE_SUBTREE, 
69                   expression=filter,
70                   attrs=[])
71 assert(len(res) == 1)
72 user_dn = res[0].dn
73
74 samdb.enable_account(user_dn)