3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
6 # Based on the original in EJS:
7 # Copyright (C) Andrew Tridgell <tridge@samba.org> 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/>.
23 """Convenience functions for using the SAM."""
29 class SamDB(samba.Ldb):
30 """The SAM database."""
31 def __init__(self, url=None, session_info=None, credentials=None,
32 modules_dir=None, lp=None):
33 """Open the Sam Database.
35 :param url: URL of the database.
37 super(SamDB, self).__init__(session_info=session_info, credentials=credentials,
38 modules_dir=modules_dir, lp=lp)
39 assert misc.dsdb_set_global_schema(self) == 0
43 def add_foreign(self, domaindn, sid, desc):
44 """Add a foreign security principle."""
46 dn: CN=%s,CN=ForeignSecurityPrincipals,%s
48 objectClass: foreignSecurityPrincipal
50 """ % (sid, domaindn, desc)
51 # deliberately ignore errors from this, as the records may
53 for msg in self.parse_ldif(add):
56 def enable_account(self, user_dn):
59 :param user_dn: Dn of the account to enable.
61 res = self.search(user_dn, ldb.SCOPE_BASE, None, ["userAccountControl"])
63 userAccountControl = res[0]["userAccountControl"][0]
64 userAccountControl = int(userAccountControl)
65 if (userAccountControl & 0x2):
66 userAccountControl = userAccountControl & ~0x2 # remove disabled bit
67 if (userAccountControl & 0x20):
68 userAccountControl = userAccountControl & ~0x20 # remove 'no password required' bit
73 replace: userAccountControl
74 userAccountControl: %u
75 """ % (user_dn, userAccountControl)
78 def newuser(self, username, unixname, password):
79 """add a new user record.
81 :param username: Name of the new user.
82 :param unixname: Name of the unix user to map to.
83 :param password: Password for the new user
86 self.transaction_start()
88 # find the DNs for the domain and the domain users group
89 res = self.search("", scope=ldb.SCOPE_BASE,
90 expression="(defaultNamingContext=*)",
91 attrs=["defaultNamingContext"])
92 assert(len(res) == 1 and res[0]["defaultNamingContext"] is not None)
93 domain_dn = res[0]["defaultNamingContext"][0]
94 assert(domain_dn 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
102 self.add({"dn": user_dn,
103 "sAMAccountName": username,
104 "unixName": unixname,
105 "sambaPassword": password,
106 "objectClass": "user"})
108 # modify the userAccountControl to remove the disabled bit
109 self.enable_account(user_dn)
110 self.transaction_commit()
112 def setpassword(self, filter, password):
113 """Set a password on a user record
115 :param filter: LDAP filter to find the user (eg samccountname=name)
116 :param password: Password for the user
119 self.transaction_start()
121 # find the DNs for the domain
122 res = self.search("", scope=ldb.SCOPE_BASE,
123 expression="(defaultNamingContext=*)",
124 attrs=["defaultNamingContext"])
125 assert(len(res) == 1 and res[0]["defaultNamingContext"] is not None)
126 domain_dn = res[0]["defaultNamingContext"][0]
127 assert(domain_dn is not None)
129 res = self.search(domain_dn, scope=ldb.SCOPE_SUBTREE,
132 assert(len(res) == 1)
138 replace: sambaPassword
140 """ % (user_dn, password)
142 self.modify_ldif(setpw)
144 # modify the userAccountControl to remove the disabled bit
145 self.enable_account(user_dn)
146 self.transaction_commit()
148 def set_domain_sid(self, sid):
149 """Change the domain SID used by this SamDB.
151 :param sid: The new domain sid to use.
153 misc.samdb_set_domain_sid(self, sid)
155 def attach_schema_from_ldif(self, pf, df):
156 misc.dsdb_attach_schema_from_ldif_file(self, pf, df)
158 def set_invocation_id(self, invocation_id):
159 """Set the invocation id for this SamDB handle.
161 :param invocation_id: GUID of the invocation id.
163 misc.dsdb_set_ntds_invocation_id(self, invocation_id)