s4: Major rework of the LDB/SAMDB/IDMAP python bindings
[metze/samba/wip.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 __docformat__ = "restructuredText"
24
25 import samba
26
27 class IDmapDB(samba.Ldb):
28     """The IDmap database."""
29
30     # Mappings for ID_TYPE_UID, ID_TYPE_GID and ID_TYPE_BOTH
31     TYPE_UID = 1
32     TYPE_GID = 2
33     TYPE_BOTH = 3
34
35     def __init__(self, url=None, lp=None, modules_dir=None, session_info=None,
36                  credentials=None, flags=0, options=None):
37         """Opens the IDmap Database.
38         For parameter meanings see the super class (samba.Ldb)
39         """
40
41         self.lp = lp
42         if url is None:
43                 url = lp.get("idmap database")
44
45         super(IDmapDB, self).__init__(url=url, lp=lp, modules_dir=modules_dir,
46                 session_info=session_info, credentials=credentials, flags=flags,
47                 options=options)
48
49     def connect(self, url=None, flags=0, options=None):
50         super(IDmapDB, self).connect(url=self.lp.private_path(url), flags=flags,
51                 options=options)
52
53     def setup_name_mapping(self, sid, type, unixid):
54         """Setup a mapping between a sam name and a unix name.
55
56         :param sid: SID of the NT-side of the mapping.
57         :param unixname: Unix name to map to.
58         """
59         type_string = ""
60         if type == self.TYPE_UID:
61             type_string = "ID_TYPE_UID"
62         elif type == self.TYPE_GID:
63             type_string = "ID_TYPE_GID"
64         elif type == self.TYPE_BOTH:
65             type_string = "ID_TYPE_BOTH"
66         else:
67             return
68
69         mod = """
70 dn: CN=%s
71 xidNumber: %s
72 objectSid: %s
73 objectClass: sidMap
74 type: %s
75 cn: %s
76
77 """ % (sid, unixid, sid, type_string, sid)
78         self.add(self.parse_ldif(mod).next()[1])
79
80