r26505: Add python bindings for some samdb-related functions, improve provisioning...
[kai/samba.git] / source / scripting / python / samba / samdb.py
1 #!/usr/bin/python
2
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
5 #
6 # Based on the original in EJS:
7 # Copyright (C) Andrew Tridgell 2005
8 #   
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.
13 #   
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.
18 #   
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/>.
21 #
22
23 import samba
24 import misc
25
26 class SamDB(samba.Ldb):
27     def add_foreign(self, domaindn, sid, desc):
28         """Add a foreign security principle."""
29         add = """
30 dn: CN=%s,CN=ForeignSecurityPrincipals,%s
31 objectClass: top
32 objectClass: foreignSecurityPrincipal
33 description: %s
34         """ % (sid, domaindn, desc)
35         # deliberately ignore errors from this, as the records may
36         # already exist
37         for msg in self.parse_ldif(add):
38             self.add(msg[1])
39
40     def setup_name_mapping(self, domaindn, sid, unixname):
41         """Setup a mapping between a sam name and a unix name."""
42         res = self.search(Dn(ldb, domaindn), SCOPE_SUBTREE, 
43                          "objectSid=%s" % sid, ["dn"])
44         assert len(res) == 1, "Failed to find record for objectSid %s" % sid
45
46         mod = """
47 dn: %s
48 changetype: modify
49 replace: unixName
50 unixName: %s
51 """ % (res[0].dn, unixname)
52         self.modify(self.parse_ldif(mod).next()[1])
53
54     def enable_account(self, user_dn):
55         """enable the account.
56         
57         :param user_dn: Dn of the account to enable.
58         """
59         res = self.search(user_dn, SCOPE_ONELEVEL, None, ["userAccountControl"])
60         assert len(res) == 1
61         userAccountControl = res[0].userAccountControl
62         userAccountControl = userAccountControl - 2 # remove disabled bit
63         mod = """
64 dn: %s
65 changetype: modify
66 replace: userAccountControl
67 userAccountControl: %u
68 """ % (user_dn, userAccountControl)
69         self.modify(mod)
70
71     def newuser(self, username, unixname, password, message):
72         """add a new user record"""
73         # connect to the sam 
74         self.transaction_start()
75
76         # find the DNs for the domain and the domain users group
77         res = self.search("", SCOPE_BASE, "defaultNamingContext=*", 
78                          ["defaultNamingContext"])
79         assert(len(res) == 1 and res[0].defaultNamingContext is not None)
80         domain_dn = res[0].defaultNamingContext
81         assert(domain_dn is not None)
82         dom_users = searchone(self, domain_dn, "name=Domain Users", "dn")
83         assert(dom_users is not None)
84
85         user_dn = "CN=%s,CN=Users,%s" % (username, domain_dn)
86
87         #
88         #  the new user record. note the reliance on the samdb module to fill
89         #  in a sid, guid etc
90         #
91         ldif = """
92 dn: %s
93 sAMAccountName: %s
94 unixName: %s
95 sambaPassword: %s
96 objectClass: user
97     """ % (user_dn, username, unixname, password)
98         #  add the user to the users group as well
99         modgroup = """
100 dn: %s
101 changetype: modify
102 add: member
103 member: %s
104 """ % (dom_users, user_dn)
105
106
107         #  now the real work
108         message("Adding user %s" % user_dn)
109         self.add(ldif)
110
111         message("Modifying group %s" % dom_users)
112         self.modify(modgroup)
113
114         #  modify the userAccountControl to remove the disabled bit
115         enable_account(self, user_dn)
116         self.transaction_commit()
117
118     def set_domain_sid(self, sid):
119         misc.samdb_set_domain_sid(self, sid)
120
121     def attach_schema_from_ldif(self, pf, df):
122         misc.dsdb_attach_schema_from_ldif_file(self, pf, df)