r26496: Move some provision functions to a new SamDB class, support setting session_i...
[ira/wip.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
25 class SamDB(samba.Ldb):
26     def add_foreign(self, domaindn, sid, desc):
27         """Add a foreign security principle."""
28         add = """
29 dn: CN=%s,CN=ForeignSecurityPrincipals,%s
30 objectClass: top
31 objectClass: foreignSecurityPrincipal
32 description: %s
33         """ % (sid, domaindn, desc)
34         # deliberately ignore errors from this, as the records may
35         # already exist
36         for msg in self.parse_ldif(add):
37             self.add(msg[1])
38
39     def setup_name_mapping(self, domaindn, sid, unixname):
40         """Setup a mapping between a sam name and a unix name."""
41         res = self.search(Dn(ldb, domaindn), SCOPE_SUBTREE, 
42                          "objectSid=%s" % sid, ["dn"])
43         assert len(res) == 1, "Failed to find record for objectSid %s" % sid
44
45         mod = """
46 dn: %s
47 changetype: modify
48 replace: unixName
49 unixName: %s
50 """ % (res[0].dn, unixname)
51         self.modify(self.parse_ldif(mod).next()[1])
52
53     def enable_account(self, user_dn):
54         """enable the account.
55         
56         :param user_dn: Dn of the account to enable.
57         """
58         res = self.search(user_dn, SCOPE_ONELEVEL, None, ["userAccountControl"])
59         assert len(res) == 1
60         userAccountControl = res[0].userAccountControl
61         userAccountControl = userAccountControl - 2 # remove disabled bit
62         mod = """
63 dn: %s
64 changetype: modify
65 replace: userAccountControl
66 userAccountControl: %u
67 """ % (user_dn, userAccountControl)
68         self.modify(mod)
69
70     def newuser(self, username, unixname, password, message):
71         """add a new user record"""
72         # connect to the sam 
73         self.transaction_start()
74
75         # find the DNs for the domain and the domain users group
76         res = self.search("", SCOPE_BASE, "defaultNamingContext=*", 
77                          ["defaultNamingContext"])
78         assert(len(res) == 1 and res[0].defaultNamingContext is not None)
79         domain_dn = res[0].defaultNamingContext
80         assert(domain_dn is not None)
81         dom_users = searchone(self, domain_dn, "name=Domain Users", "dn")
82         assert(dom_users is not None)
83
84         user_dn = "CN=%s,CN=Users,%s" % (username, domain_dn)
85
86         #
87         #  the new user record. note the reliance on the samdb module to fill
88         #  in a sid, guid etc
89         #
90         ldif = """
91 dn: %s
92 sAMAccountName: %s
93 unixName: %s
94 sambaPassword: %s
95 objectClass: user
96     """ % (user_dn, username, unixname, password)
97         #  add the user to the users group as well
98         modgroup = """
99 dn: %s
100 changetype: modify
101 add: member
102 member: %s
103 """ % (dom_users, user_dn)
104
105
106         #  now the real work
107         message("Adding user %s" % user_dn)
108         self.add(ldif)
109
110         message("Modifying group %s" % dom_users)
111         self.modify(modgroup)
112
113         #  modify the userAccountControl to remove the disabled bit
114         enable_account(self, user_dn)
115         self.transaction_commit()
116
117