python/samba: Fix py2/3 relative module import issue
[samba.git] / python / samba / idmap.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) 2008 Kai Blin <kai@samba.org>
3 #
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 """Convenience functions for using the idmap database."""
20
21 __docformat__ = "restructuredText"
22
23 import ldb
24 import samba
25
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
39         For parameter meanings see the super class (samba.Ldb)
40         """
41         self.lp = lp
42         if url is None:
43             url = lp.private_path("idmap.ldb")
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 increment_xid(self):
54         """Increment xidNumber, if not present it create and assign it to the lowerBound
55
56         :return xid can that be used for SID/unixid mapping
57         """
58         res = self.search(expression="distinguishedName=CN=CONFIG", base="",
59                           scope=ldb.SCOPE_SUBTREE)
60         id = res[0].get("xidNumber")
61         flag = ldb.FLAG_MOD_REPLACE
62         if id is None:
63             id = res[0].get("lowerBound")
64             flag = ldb.FLAG_MOD_ADD
65         newid = int(str(id)) + 1
66         msg = ldb.Message()
67         msg.dn = ldb.Dn(self, "CN=CONFIG")
68         msg["xidNumber"] = ldb.MessageElement(str(newid), flag, "xidNumber")
69         self.modify(msg)
70         return id
71
72     def setup_name_mapping(self, sid, type, unixid=None):
73         """Setup a mapping between a sam name and a unix name.
74
75         :param sid: SID of the NT-side of the mapping.
76         :param unixname: Unix id to map to, if none supplied the next one will be selected
77         """
78         if unixid is None:
79             unixid = self.increment_xid()
80         type_string = ""
81         if type == self.TYPE_UID:
82             type_string = "ID_TYPE_UID"
83         elif type == self.TYPE_GID:
84             type_string = "ID_TYPE_GID"
85         elif type == self.TYPE_BOTH:
86             type_string = "ID_TYPE_BOTH"
87         else:
88             return
89
90         mod = """
91 dn: CN=%s
92 xidNumber: %s
93 objectSid: %s
94 objectClass: sidMap
95 type: %s
96 cn: %s
97
98 """ % (sid, unixid, sid, type_string, sid)
99         self.add(next(self.parse_ldif(mod))[1])