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"
26 class IDmapDB(samba
.Ldb
):
27 """The IDmap database."""
29 # Mappings for ID_TYPE_UID, ID_TYPE_GID and ID_TYPE_BOTH
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)
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
,
48 def connect(self
, url
=None, flags
=0, options
=None):
49 super(IDmapDB
, self
).connect(url
=self
.lp
.private_path(url
), flags
=flags
,
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
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
62 id = res
[0].get("lowerBound")
63 flag
= ldb
.FLAG_MOD_ADD
64 newid
= int(str(id)) + 1
66 msg
.dn
= ldb
.Dn(self
, "CN=CONFIG")
67 msg
["xidNumber"] = ldb
.MessageElement(str(newid
), flag
, "xidNumber")
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
78 unixid
= self
.increment_xid()
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"
97 """ % (sid
, unixid
, sid
, type_string
, sid
)
98 self
.add(self
.parse_ldif(mod
).next()[1])