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
10 sys
.path
.insert(0, os
.path
.join(os
.path
.dirname(sys
.argv
[0]), "../bin/python"))
12 import samba
.getopt
as options
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
45 print "username must be specified"
47 lp
= sambaopts
.get_loadparm()
49 creds
= credopts
.get_credentials(lp
)
51 if opts
.H
is not None:
54 url
= lp
.get("sam database")
56 samdb
= SamDB(url
=url
, session_info
=system_session(),
57 credentials
=creds
, lp
=lp
)
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]
69 filter = "(&(objectClass=user)(samAccountName=%s))" % username
71 res
= samdb
.search(domain_dn
, scope
=ldb
.SCOPE_SUBTREE
,
77 samdb
.enable_account(user_dn
)