3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
6 # Based on the original in EJS:
7 # Copyright (C) Andrew Tridgell 2005
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 class SamDB(samba.Ldb):
28 def __init__(self, url=None, session_info=None, credentials=None,
29 modules_dir=None, lp=None):
30 super(SamDB, self).__init__(session_info=session_info, credentials=credentials,
31 modules_dir=modules_dir, lp=lp)
32 misc.dsdb_set_global_schema(self)
33 misc.ldb_register_samba_handlers(self)
37 def add_foreign(self, domaindn, sid, desc):
38 """Add a foreign security principle."""
40 dn: CN=%s,CN=ForeignSecurityPrincipals,%s
42 objectClass: foreignSecurityPrincipal
44 """ % (sid, domaindn, desc)
45 # deliberately ignore errors from this, as the records may
47 for msg in self.parse_ldif(add):
50 def setup_name_mapping(self, domaindn, sid, unixname):
51 """Setup a mapping between a sam name and a unix name."""
52 res = self.search(ldb.Dn(self, domaindn), ldb.SCOPE_SUBTREE,
53 "objectSid=%s" % sid, ["dn"])
54 assert len(res) == 1, "Failed to find record for objectSid %s" % sid
61 """ % (res[0].dn, unixname)
62 self.modify(self.parse_ldif(mod).next()[1])
64 def enable_account(self, user_dn):
65 """enable the account.
67 :param user_dn: Dn of the account to enable.
69 res = self.search(user_dn, SCOPE_ONELEVEL, None, ["userAccountControl"])
71 userAccountControl = res[0].userAccountControl
72 userAccountControl = userAccountControl - 2 # remove disabled bit
76 replace: userAccountControl
77 userAccountControl: %u
78 """ % (user_dn, userAccountControl)
81 def newuser(self, username, unixname, password, message):
82 """add a new user record"""
84 self.transaction_start()
86 # find the DNs for the domain and the domain users group
87 res = self.search("", SCOPE_BASE, "defaultNamingContext=*",
88 ["defaultNamingContext"])
89 assert(len(res) == 1 and res[0].defaultNamingContext is not None)
90 domain_dn = res[0].defaultNamingContext
91 assert(domain_dn is not None)
92 dom_users = searchone(self, domain_dn, "name=Domain Users", "dn")
93 assert(dom_users is not None)
95 user_dn = "CN=%s,CN=Users,%s" % (username, domain_dn)
98 # the new user record. note the reliance on the samdb module to fill
107 """ % (user_dn, username, unixname, password)
108 # add the user to the users group as well
114 """ % (dom_users, user_dn)
118 message("Adding user %s" % user_dn)
121 message("Modifying group %s" % dom_users)
122 self.modify(modgroup)
124 # modify the userAccountControl to remove the disabled bit
125 enable_account(self, user_dn)
126 self.transaction_commit()
128 def set_domain_sid(self, sid):
129 misc.samdb_set_domain_sid(self, sid)
131 def attach_schema_from_ldif(self, pf, df):
132 misc.dsdb_attach_schema_from_ldif_file(self, pf, df)