2008-04-01 02:17:00 +04:00
# Unix SMB/CIFS implementation.
# Copyright (C) 2008 Kai Blin <kai@samba.org>
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
""" Convenience functions for using the idmap database. """
2008-05-22 19:42:18 +04:00
__docformat__ = " restructuredText "
2009-11-22 19:50:31 +03:00
import ldb
2008-04-01 02:17:00 +04:00
import samba
class IDmapDB ( samba . Ldb ) :
""" The IDmap database. """
# Mappings for ID_TYPE_UID, ID_TYPE_GID and ID_TYPE_BOTH
TYPE_UID = 1
TYPE_GID = 2
TYPE_BOTH = 3
2009-08-15 17:20:09 +04:00
def __init__ ( self , url = None , lp = None , modules_dir = None , session_info = None ,
credentials = None , flags = 0 , options = None ) :
2011-11-04 20:34:47 +04:00
""" Opens the IDMap Database.
2009-08-15 17:20:09 +04:00
For parameter meanings see the super class ( samba . Ldb )
2008-04-01 02:17:00 +04:00
"""
2008-04-14 13:51:02 +04:00
self . lp = lp
2009-08-15 17:20:09 +04:00
if url is None :
2011-11-04 20:34:47 +04:00
url = lp . private_path ( " idmap.ldb " )
2008-04-14 13:51:02 +04:00
2009-08-15 17:20:09 +04:00
super ( IDmapDB , self ) . __init__ ( url = url , lp = lp , modules_dir = modules_dir ,
session_info = session_info , credentials = credentials , flags = flags ,
options = options )
2008-04-01 02:17:00 +04:00
2009-08-15 17:20:09 +04:00
def connect ( self , url = None , flags = 0 , options = None ) :
super ( IDmapDB , self ) . connect ( url = self . lp . private_path ( url ) , flags = flags ,
options = options )
2008-04-01 02:17:00 +04:00
2009-11-22 19:50:31 +03:00
def increment_xid ( self ) :
""" Increment xidNumber, if not present it create and assign it to the lowerBound
: return xid can that be used for SID / unixid mapping
"""
2011-10-25 22:10:30 +04:00
res = self . search ( expression = " distinguishedName=CN=CONFIG " , base = " " ,
2010-06-11 01:12:53 +04:00
scope = ldb . SCOPE_SUBTREE )
id = res [ 0 ] . get ( " xidNumber " )
flag = ldb . FLAG_MOD_REPLACE
if id is None :
id = res [ 0 ] . get ( " lowerBound " )
2009-11-22 19:50:31 +03:00
flag = ldb . FLAG_MOD_ADD
newid = int ( str ( id ) ) + 1
msg = ldb . Message ( )
2010-06-11 01:12:53 +04:00
msg . dn = ldb . Dn ( self , " CN=CONFIG " )
msg [ " xidNumber " ] = ldb . MessageElement ( str ( newid ) , flag , " xidNumber " )
2009-11-22 19:50:31 +03:00
self . modify ( msg )
return id
def setup_name_mapping ( self , sid , type , unixid = None ) :
2008-04-01 02:17:00 +04:00
""" Setup a mapping between a sam name and a unix name.
: param sid : SID of the NT - side of the mapping .
2009-11-22 19:50:31 +03:00
: param unixname : Unix id to map to , if none supplied the next one will be selected
2008-04-01 02:17:00 +04:00
"""
2010-06-11 01:12:53 +04:00
if unixid is None :
2009-11-22 19:50:31 +03:00
unixid = self . increment_xid ( )
2008-04-01 02:17:00 +04:00
type_string = " "
if type == self . TYPE_UID :
type_string = " ID_TYPE_UID "
elif type == self . TYPE_GID :
type_string = " ID_TYPE_GID "
elif type == self . TYPE_BOTH :
type_string = " ID_TYPE_BOTH "
else :
return
mod = """
dn : CN = % s
xidNumber : % s
objectSid : % s
objectClass : sidMap
type : % s
cn : % s
""" % (sid, unixid, sid, type_string, sid)
2018-05-04 13:22:43 +03:00
self . add ( next ( self . parse_ldif ( mod ) ) [ 1 ] )