kcc: Use remote vertex instead of local vertex in getting bridgehead
[Samba.git] / python / samba / idmap.py
blob0cb729fbc29fb9824a4c9d91434eabe0f801a971
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) 2008 Kai Blin <kai@samba.org>
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.
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.
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/>.
19 """Convenience functions for using the idmap database."""
21 __docformat__ = "restructuredText"
23 import ldb
24 import samba
26 class IDmapDB(samba.Ldb):
27 """The IDmap database."""
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
34 def __init__(self, url=None, lp=None, modules_dir=None, session_info=None,
35 credentials=None, flags=0, options=None):
36 """Opens the IDMap Database.
38 For parameter meanings see the super class (samba.Ldb)
39 """
40 self.lp = lp
41 if url is None:
42 url = lp.private_path("idmap.ldb")
44 super(IDmapDB, self).__init__(url=url, lp=lp, modules_dir=modules_dir,
45 session_info=session_info, credentials=credentials, flags=flags,
46 options=options)
48 def connect(self, url=None, flags=0, options=None):
49 super(IDmapDB, self).connect(url=self.lp.private_path(url), flags=flags,
50 options=options)
52 def increment_xid(self):
53 """Increment xidNumber, if not present it create and assign it to the lowerBound
55 :return xid can that be used for SID/unixid mapping
56 """
57 res = self.search(expression="distinguishedName=CN=CONFIG", base="",
58 scope=ldb.SCOPE_SUBTREE)
59 id = res[0].get("xidNumber")
60 flag = ldb.FLAG_MOD_REPLACE
61 if id is None:
62 id = res[0].get("lowerBound")
63 flag = ldb.FLAG_MOD_ADD
64 newid = int(str(id)) + 1
65 msg = ldb.Message()
66 msg.dn = ldb.Dn(self, "CN=CONFIG")
67 msg["xidNumber"] = ldb.MessageElement(str(newid), flag, "xidNumber")
68 self.modify(msg)
69 return id
71 def setup_name_mapping(self, sid, type, unixid=None):
72 """Setup a mapping between a sam name and a unix name.
74 :param sid: SID of the NT-side of the mapping.
75 :param unixname: Unix id to map to, if none supplied the next one will be selected
76 """
77 if unixid is None:
78 unixid = self.increment_xid()
79 type_string = ""
80 if type == self.TYPE_UID:
81 type_string = "ID_TYPE_UID"
82 elif type == self.TYPE_GID:
83 type_string = "ID_TYPE_GID"
84 elif type == self.TYPE_BOTH:
85 type_string = "ID_TYPE_BOTH"
86 else:
87 return
89 mod = """
90 dn: CN=%s
91 xidNumber: %s
92 objectSid: %s
93 objectClass: sidMap
94 type: %s
95 cn: %s
97 """ % (sid, unixid, sid, type_string, sid)
98 self.add(self.parse_ldif(mod).next()[1])