provision: Set up id mappings in the idmap db, only map Administrator.
[vlendec/samba-autobuild/.git] / source4 / scripting / python / samba / idmap.py
1 #!/usr/bin/python
2
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) 2008 Kai Blin <kai@samba.org>
5 #
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 """Convenience functions for using the idmap database."""
22
23 import samba
24 import ldb
25
26 class IDmapDB(samba.Ldb):
27     """The IDmap database."""
28
29     # Mappings for ID_TYPE_UID, ID_TYPE_GID and ID_TYPE_BOTH
30     TYPE_UID = 1
31     TYPE_GID = 2
32     TYPE_BOTH = 3
33
34     def __init__(self, url=None, session_info=None, credentials=None,
35                  modules_dir=None, lp=None):
36         """Open the IDmap Database.
37
38         :param url: URL of the database.
39         """
40         super(IDmapDB, self).__init__(session_info=session_info, credentials=credentials,
41                                     modules_dir=modules_dir, lp=lp)
42         if url:
43             self.connect(url)
44
45
46     def setup_name_mapping(self, sid, type, unixid):
47         """Setup a mapping between a sam name and a unix name.
48
49         :param sid: SID of the NT-side of the mapping.
50         :param unixname: Unix name to map to.
51         """
52         type_string = ""
53         if type == self.TYPE_UID:
54             type_string = "ID_TYPE_UID"
55         elif type == self.TYPE_GID:
56             type_string = "ID_TYPE_GID"
57         elif type == self.TYPE_BOTH:
58             type_string = "ID_TYPE_BOTH"
59         else:
60             return
61
62         mod = """
63 dn: CN=%s
64 xidNumber: %s
65 objectSid: %s
66 objectClass: sidMap
67 type: %s
68 cn: %s
69
70 """ % (sid, unixid, sid, type_string, sid)
71         self.add(self.parse_ldif(mod).next()[1])
72
73