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