The branch, master has been updated via d9e94bf33681dda2b9fad5efa3226e5d818fab6b (commit) via f13895851fde13cefaa484cd9f90a9f0ad41d78e (commit) from 71221fded45c4f0fd0f48035045b0c79f5a90eff (commit)
http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master - Log ----------------------------------------------------------------- commit d9e94bf33681dda2b9fad5efa3226e5d818fab6b Author: Jelmer Vernooij <jel...@samba.org> Date: Wed Feb 11 18:17:00 2009 +0100 Make it possible to override the setup path. commit f13895851fde13cefaa484cd9f90a9f0ad41d78e Author: Jelmer Vernooij <jel...@samba.org> Date: Wed Feb 11 17:54:58 2009 +0100 Cancel transactions when exceptions are raised. ----------------------------------------------------------------------- Summary of changes: source4/scripting/python/samba/samdb.py | 175 +++++++++++++------------ source4/scripting/python/samba/tests/samdb.py | 21 ++-- 2 files changed, 104 insertions(+), 92 deletions(-) Changeset truncated at 500 lines: diff --git a/source4/scripting/python/samba/samdb.py b/source4/scripting/python/samba/samdb.py index 92b0bd7..947c460 100644 --- a/source4/scripting/python/samba/samdb.py +++ b/source4/scripting/python/samba/samdb.py @@ -104,41 +104,43 @@ userAccountControl: %u """ # connect to the sam self.transaction_start() - - domain_dn = self.domain_dn() - assert(domain_dn is not None) - user_dn = "CN=%s,CN=Users,%s" % (username, domain_dn) - - # - # the new user record. note the reliance on the samdb module to fill - # in a sid, guid etc - # - # now the real work - self.add({"dn": user_dn, - "sAMAccountName": username, - "userPassword": password, - "objectClass": "user"}) - - res = self.search(user_dn, scope=ldb.SCOPE_BASE, - expression="objectclass=*", - attrs=["objectSid"]) - assert(len(res) == 1) - user_sid = self.schema_format_value("objectSid", res[0]["objectSid"][0]) - - try: - idmap = IDmapDB(lp=self.lp) - - user = pwd.getpwnam(unixname) - # setup ID mapping for this UID + domain_dn = self.domain_dn() + assert(domain_dn is not None) + user_dn = "CN=%s,CN=Users,%s" % (username, domain_dn) + + # + # the new user record. note the reliance on the samdb module to + # fill in a sid, guid etc + # + # now the real work + self.add({"dn": user_dn, + "sAMAccountName": username, + "userPassword": password, + "objectClass": "user"}) + + res = self.search(user_dn, scope=ldb.SCOPE_BASE, + expression="objectclass=*", + attrs=["objectSid"]) + assert len(res) == 1 + user_sid = self.schema_format_value("objectSid", res[0]["objectSid"][0]) - idmap.setup_name_mapping(user_sid, idmap.TYPE_UID, user[2]) - - except KeyError: - pass - - # modify the userAccountControl to remove the disabled bit - self.enable_account(user_dn) + try: + idmap = IDmapDB(lp=self.lp) + + user = pwd.getpwnam(unixname) + # setup ID mapping for this UID + + idmap.setup_name_mapping(user_sid, idmap.TYPE_UID, user[2]) + + except KeyError: + pass + + # modify the userAccountControl to remove the disabled bit + self.enable_account(user_dn) + except: + self.transaction_cancel() + raise self.transaction_commit() def setpassword(self, filter, password): @@ -149,32 +151,35 @@ userAccountControl: %u """ # connect to the sam self.transaction_start() - - # find the DNs for the domain - res = self.search("", scope=ldb.SCOPE_BASE, - expression="(defaultNamingContext=*)", - attrs=["defaultNamingContext"]) - assert(len(res) == 1 and res[0]["defaultNamingContext"] is not None) - domain_dn = res[0]["defaultNamingContext"][0] - assert(domain_dn is not None) - - res = self.search(domain_dn, scope=ldb.SCOPE_SUBTREE, - expression=filter, - attrs=[]) - assert(len(res) == 1) - user_dn = res[0].dn - - setpw = """ -dn: %s -changetype: modify -replace: userPassword -userPassword: %s -""" % (user_dn, password) - - self.modify_ldif(setpw) - - # modify the userAccountControl to remove the disabled bit - self.enable_account(user_dn) + try: + # find the DNs for the domain + res = self.search("", scope=ldb.SCOPE_BASE, + expression="(defaultNamingContext=*)", + attrs=["defaultNamingContext"]) + assert(len(res) == 1 and res[0]["defaultNamingContext"] is not None) + domain_dn = res[0]["defaultNamingContext"][0] + assert(domain_dn is not None) + + res = self.search(domain_dn, scope=ldb.SCOPE_SUBTREE, + expression=filter, + attrs=[]) + assert(len(res) == 1) + user_dn = res[0].dn + + setpw = """ + dn: %s + changetype: modify + replace: userPassword + userPassword: %s + """ % (user_dn, password) + + self.modify_ldif(setpw) + + # modify the userAccountControl to remove the disabled bit + self.enable_account(user_dn) + except: + self.transaction_cancel() + raise self.transaction_commit() def set_domain_sid(self, sid): @@ -200,28 +205,32 @@ userPassword: %s :param expiry_seconds: expiry time from now in seconds :param noexpiry: if set, then don't expire password """ - self.transaction_start(); - res = self.search(base=self.domain_dn(), scope=ldb.SCOPE_SUBTREE, - expression=("(samAccountName=%s)" % user), - attrs=["userAccountControl", "accountExpires"]) - assert len(res) == 1 - userAccountControl = int(res[0]["userAccountControl"][0]) - accountExpires = int(res[0]["accountExpires"][0]) - if noexpiry: - userAccountControl = userAccountControl | 0x10000 - accountExpires = 0 - else: - userAccountControl = userAccountControl & ~0x10000 - accountExpires = glue.unix2nttime(expiry_seconds + int(time.time())) - - mod = """ -dn: %s -changetype: modify -replace: userAccountControl -userAccountControl: %u -replace: accountExpires -accountExpires: %u -""" % (res[0].dn, userAccountControl, accountExpires) - # now change the database - self.modify_ldif(mod) + self.transaction_start() + try: + res = self.search(base=self.domain_dn(), scope=ldb.SCOPE_SUBTREE, + expression=("(samAccountName=%s)" % user), + attrs=["userAccountControl", "accountExpires"]) + assert len(res) == 1 + userAccountControl = int(res[0]["userAccountControl"][0]) + accountExpires = int(res[0]["accountExpires"][0]) + if noexpiry: + userAccountControl = userAccountControl | 0x10000 + accountExpires = 0 + else: + userAccountControl = userAccountControl & ~0x10000 + accountExpires = glue.unix2nttime(expiry_seconds + int(time.time())) + + mod = """ + dn: %s + changetype: modify + replace: userAccountControl + userAccountControl: %u + replace: accountExpires + accountExpires: %u + """ % (res[0].dn, userAccountControl, accountExpires) + # now change the database + self.modify_ldif(mod) + except: + self.transaction_cancel() + raise self.transaction_commit(); diff --git a/source4/scripting/python/samba/tests/samdb.py b/source4/scripting/python/samba/tests/samdb.py index cce6ea8..8b8f178 100644 --- a/source4/scripting/python/samba/tests/samdb.py +++ b/source4/scripting/python/samba/tests/samdb.py @@ -28,6 +28,10 @@ import uuid from samba import param class SamDBTestCase(TestCaseInTempDir): + + def setup_path(self, relpath): + return os.path.join("setup", relpath) + def setUp(self): super(SamDBTestCase, self).setUp() invocationid = str(uuid.uuid4()) @@ -37,7 +41,6 @@ class SamDBTestCase(TestCaseInTempDir): schemadn = "CN=Schema," + configdn domainguid = str(uuid.uuid4()) policyguid = str(uuid.uuid4()) - setup_path = lambda x: os.path.join("setup", x) creds = Credentials() creds.set_anonymous() domainsid = security.random_sid() @@ -51,21 +54,21 @@ class SamDBTestCase(TestCaseInTempDir): serverrole="domain controller" smbconf = os.path.join(self.tempdir, "smb.conf") - make_smbconf(smbconf, setup_path, hostname, domain, dnsdomain, serverrole, - self.tempdir) + make_smbconf(smbconf, self.setup_path, hostname, domain, dnsdomain, + serverrole, self.tempdir) - lp = param.LoadParm() - lp.load(smbconf) + self.lp = param.LoadParm() + self.lp.load(smbconf) - names = guess_names(lp=lp, hostname=hostname, + names = guess_names(lp=self.lp, hostname=hostname, domain=domain, dnsdomain=dnsdomain, serverrole=serverrole, domaindn=self.domaindn, configdn=configdn, schemadn=schemadn) setup_templatesdb(os.path.join(self.tempdir, "templates.ldb"), - setup_path, session_info=session_info, - credentials=creds, lp=cmdline_loadparm) - self.samdb = setup_samdb(path, setup_path, session_info, creds, + self.setup_path, session_info=session_info, + credentials=creds, lp=self.lp) + self.samdb = setup_samdb(path, self.setup_path, session_info, creds, cmdline_loadparm, names, lambda x: None, domainsid, "# no aci", domainguid, -- Samba Shared Repository