Use tevent_wakeup_send in open_socket_out_defer
[Samba.git] / source4 / setup / enableaccount
blob061997b804b21add347529e8ebdc7ed0c63085f2
1 #!/usr/bin/python
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
8 import os, sys
10 sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), "../bin/python"))
12 import samba.getopt as options
13 import optparse
14 import pwd
15 import ldb
17 from samba.auth import system_session
18 from samba.samdb import SamDB
20 parser = optparse.OptionParser("enableaccount [username] [options]")
21 sambaopts = options.SambaOptions(parser)
22 parser.add_option_group(sambaopts)
23 parser.add_option_group(options.VersionOptions(parser))
24 credopts = options.CredentialsOptions(parser)
25 parser.add_option_group(credopts)
26 parser.add_option("-H", help="LDB URL for database or target server", type=str)
27 parser.add_option("--base", help="Base DN to search for user under", type=str)
29 opts, args = parser.parse_args()
32 # print a message if quiet is not set
34 def message(text):
35 if not opts.quiet:
36 print text
38 if len(args) == 0:
39 parser.print_usage()
40 sys.exit(1)
42 username = args[0]
44 if username is None:
45 print "username must be specified"
47 lp = sambaopts.get_loadparm()
49 creds = credopts.get_credentials(lp)
51 if opts.H is not None:
52 url = opts.H
53 else:
54 url = lp.get("sam database")
56 samdb = SamDB(url=url, session_info=system_session(),
57 credentials=creds, lp=lp)
59 domain_dn = opts.base
60 if opts.base is None:
61 res = samdb.search("", scope=ldb.SCOPE_BASE,
62 expression="(defaultNamingContext=*)",
63 attrs=["defaultNamingContext"])
64 assert(len(res) == 1 and res[0]["defaultNamingContext"] is not None)
65 domain_dn = res[0]["defaultNamingContext"][0]
66 else:
67 domain_dn = opts.base
69 filter = "(&(objectClass=user)(samAccountName=%s))" % username
71 res = samdb.search(domain_dn, scope=ldb.SCOPE_SUBTREE,
72 expression=filter,
73 attrs=[])
74 assert(len(res) == 1)
75 user_dn = res[0].dn
77 samdb.enable_account(user_dn)