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"
27 class IDmapDB(samba
.Ldb
):
28 """The IDmap database."""
30 # Mappings for ID_TYPE_UID, ID_TYPE_GID and ID_TYPE_BOTH
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.
39 For parameter meanings see the super class (samba.Ldb)
43 url
= lp
.private_path("idmap.ldb")
45 super().__init
__(url
=url
, lp
=lp
, modules_dir
=modules_dir
,
46 session_info
=session_info
, credentials
=credentials
, flags
=flags
,
49 def connect(self
, url
=None, flags
=0, options
=None):
50 super().connect(url
=self
.lp
.private_path(url
), flags
=flags
,
53 def increment_xid(self
):
54 """Increment xidNumber, if not present it create and assign it to the lowerBound
56 :return xid can that be used for SID/unixid mapping
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
63 id = res
[0].get("lowerBound")
64 flag
= ldb
.FLAG_MOD_ADD
65 newid
= int(str(id)) + 1
67 msg
.dn
= ldb
.Dn(self
, "CN=CONFIG")
68 msg
["xidNumber"] = ldb
.MessageElement(str(newid
), flag
, "xidNumber")
72 def setup_name_mapping(self
, sid
, type, unixid
=None):
73 """Setup a mapping between a sam name and a unix name.
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
79 unixid
= self
.increment_xid()
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"
98 """ % (sid
, unixid
, sid
, type_string
, sid
)
99 self
.add(next(self
.parse_ldif(mod
))[1])