provision: Set up id mappings in the idmap db, only map Administrator.
[ira/wip.git] / source4 / scripting / python / samba / samdb.py
1 #!/usr/bin/python
2
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
5 #
6 # Based on the original in EJS:
7 # Copyright (C) Andrew Tridgell <tridge@samba.org> 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 """Convenience functions for using the SAM."""
24
25 import samba
26 import misc
27 import ldb
28
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.
34
35         :param url: URL of the database.
36         """
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
40         if url:
41             self.connect(url)
42
43     def add_foreign(self, domaindn, sid, desc):
44         """Add a foreign security principle."""
45         add = """
46 dn: CN=%s,CN=ForeignSecurityPrincipals,%s
47 objectClass: top
48 objectClass: foreignSecurityPrincipal
49 description: %s
50         """ % (sid, domaindn, desc)
51         # deliberately ignore errors from this, as the records may
52         # already exist
53         for msg in self.parse_ldif(add):
54             self.add(msg[1])
55
56     def enable_account(self, user_dn):
57         """Enable an account.
58         
59         :param user_dn: Dn of the account to enable.
60         """
61         res = self.search(user_dn, ldb.SCOPE_BASE, None, ["userAccountControl"])
62         assert len(res) == 1
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
69
70         mod = """
71 dn: %s
72 changetype: modify
73 replace: userAccountControl
74 userAccountControl: %u
75 """ % (user_dn, userAccountControl)
76         self.modify_ldif(mod)
77
78     def newuser(self, username, unixname, password):
79         """add a new user record.
80         
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
84         """
85         # connect to the sam 
86         self.transaction_start()
87
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)
96
97         #
98         #  the new user record. note the reliance on the samdb module to fill
99         #  in a sid, guid etc
100         #
101         #  now the real work
102         self.add({"dn": user_dn, 
103             "sAMAccountName": username,
104             "unixName": unixname,
105             "sambaPassword": password,
106             "objectClass": "user"})
107
108         #  modify the userAccountControl to remove the disabled bit
109         self.enable_account(user_dn)
110         self.transaction_commit()
111
112     def setpassword(self, filter, password):
113         """Set a password on a user record
114         
115         :param filter: LDAP filter to find the user (eg samccountname=name)
116         :param password: Password for the user
117         """
118         # connect to the sam 
119         self.transaction_start()
120
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)
128
129         res = self.search(domain_dn, scope=ldb.SCOPE_SUBTREE, 
130                           expression=filter,
131                           attrs=[])
132         assert(len(res) == 1)
133         user_dn = res[0].dn
134
135         setpw = """
136 dn: %s
137 changetype: modify
138 replace: sambaPassword
139 sambaPassword: %s
140 """ % (user_dn, password)
141
142         self.modify_ldif(setpw)
143
144         #  modify the userAccountControl to remove the disabled bit
145         self.enable_account(user_dn)
146         self.transaction_commit()
147
148     def set_domain_sid(self, sid):
149         """Change the domain SID used by this SamDB.
150
151         :param sid: The new domain sid to use.
152         """
153         misc.samdb_set_domain_sid(self, sid)
154
155     def attach_schema_from_ldif(self, pf, df):
156         misc.dsdb_attach_schema_from_ldif_file(self, pf, df)
157
158     def set_invocation_id(self, invocation_id):
159         """Set the invocation id for this SamDB handle.
160         
161         :param invocation_id: GUID of the invocation id.
162         """
163         misc.dsdb_set_ntds_invocation_id(self, invocation_id)