2003-03-07 23:47:59 +00:00
/*
Unix SMB / CIFS implementation .
Winbind ID Mapping
Copyright ( C ) Tim Potter 2000
Copyright ( C ) Anthony Liguori < aliguor @ us . ibm . com > 2003
Copyright ( C ) Simo Sorce 2003
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 2 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 , write to the Free Software
2003-04-02 10:36:02 +00:00
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA . */
2003-03-07 23:47:59 +00:00
# include "includes.h"
2003-03-08 00:49:18 +00:00
# undef DBGC_CLASS
# define DBGC_CLASS DBGC_IDMAP
2003-03-07 23:47:59 +00:00
static struct {
const char * name ;
/* Function to create a member of the idmap_methods list */
NTSTATUS ( * reg_meth ) ( struct idmap_methods * * methods ) ;
struct idmap_methods * methods ;
2003-03-08 17:29:40 +00:00
} remote_idmap_functions [ ] = {
2003-04-02 10:36:02 +00:00
{ " winbind " , idmap_reg_winbind , NULL } ,
2003-03-07 23:47:59 +00:00
{ NULL , NULL , NULL }
2003-03-08 17:29:40 +00:00
2003-03-07 23:47:59 +00:00
} ;
2003-04-02 10:36:02 +00:00
static struct idmap_methods * local_map ;
static struct idmap_methods * remote_map ;
2003-03-07 23:47:59 +00:00
2003-03-08 17:29:40 +00:00
static struct idmap_methods * get_methods ( const char * name )
2003-03-07 23:47:59 +00:00
{
int i = 0 ;
struct idmap_methods * ret = NULL ;
2003-03-08 17:29:40 +00:00
while ( remote_idmap_functions [ i ] . name & & strcmp ( remote_idmap_functions [ i ] . name , name ) ) {
2003-03-07 23:47:59 +00:00
i + + ;
}
2003-03-08 17:29:40 +00:00
if ( remote_idmap_functions [ i ] . name ) {
2003-03-07 23:47:59 +00:00
2003-03-08 17:29:40 +00:00
if ( ! remote_idmap_functions [ i ] . methods ) {
remote_idmap_functions [ i ] . reg_meth ( & remote_idmap_functions [ i ] . methods ) ;
2003-03-07 23:47:59 +00:00
}
2003-03-08 17:29:40 +00:00
ret = remote_idmap_functions [ i ] . methods ;
2003-03-07 23:47:59 +00:00
}
return ret ;
}
2003-04-02 10:36:02 +00:00
/* Initialize backend */
BOOL idmap_init ( const char * remote_backend )
2003-03-07 23:47:59 +00:00
{
2003-04-02 10:36:02 +00:00
if ( ! local_map ) {
idmap_reg_tdb ( & local_map ) ;
2003-04-06 16:01:18 +00:00
local_map - > init ( ) ;
2003-03-08 17:29:40 +00:00
}
2003-04-02 10:36:02 +00:00
if ( ! remote_map & & remote_backend & & * remote_backend ! = 0 ) {
DEBUG ( 3 , ( " load_methods: using '%s' as remote backend \n " , remote_backend ) ) ;
2003-03-07 23:47:59 +00:00
2003-04-02 10:36:02 +00:00
remote_map = get_methods ( remote_backend ) ;
if ( ! remote_map ) {
DEBUG ( 0 , ( " load_methods: could not load remote backend '%s' \n " , remote_backend ) ) ;
2003-03-07 23:47:59 +00:00
return False ;
}
2003-04-06 16:01:18 +00:00
remote_map - > init ( ) ;
2003-03-07 23:47:59 +00:00
}
2003-03-08 17:29:40 +00:00
2003-03-07 23:47:59 +00:00
return True ;
}
2003-04-02 10:36:02 +00:00
NTSTATUS idmap_set_mapping ( const DOM_SID * sid , unid_t id , int id_type )
2003-03-07 23:47:59 +00:00
{
NTSTATUS ret ;
2003-04-02 10:36:02 +00:00
ret = local_map - > set_mapping ( sid , id , id_type ) ;
2003-03-08 17:29:40 +00:00
if ( NT_STATUS_IS_ERR ( ret ) ) {
DEBUG ( 0 , ( " idmap_set_mapping: Error, unable to modify local cache! \n " ) ) ;
2003-04-16 10:24:24 +00:00
DEBUGADD ( 0 , ( " Error: %s " , nt_errstr ( ret ) ) ) ;
2003-03-08 17:29:40 +00:00
return ret ;
}
/* Being able to update the remote cache is seldomly right.
Generally this is a forbidden operation . */
2003-04-02 10:36:02 +00:00
if ( ! ( id_type & ID_CACHE ) & & ( remote_map ! = NULL ) ) {
remote_map - > set_mapping ( sid , id , id_type ) ;
2003-03-08 17:29:40 +00:00
if ( NT_STATUS_IS_ERR ( ret ) ) {
DEBUG ( 0 , ( " idmap_set_mapping: Error, unable to modify remote cache! \n " ) ) ;
2003-04-16 10:24:24 +00:00
DEBUGADD ( 0 , ( " Error: %s " , nt_errstr ( ret ) ) ) ;
2003-03-08 17:29:40 +00:00
}
}
return ret ;
}
2003-03-07 23:47:59 +00:00
/* Get ID from SID */
2003-04-02 10:36:02 +00:00
NTSTATUS idmap_get_id_from_sid ( unid_t * id , int * id_type , const DOM_SID * sid )
2003-03-07 23:47:59 +00:00
{
NTSTATUS ret ;
2003-03-08 17:29:40 +00:00
int loc_type ;
2003-03-07 23:47:59 +00:00
2003-03-08 17:29:40 +00:00
loc_type = * id_type ;
2003-04-02 10:36:02 +00:00
if ( remote_map ) { /* We have a central remote idmap */
2003-03-08 17:29:40 +00:00
loc_type | = ID_NOMAP ;
}
2003-04-02 10:36:02 +00:00
ret = local_map - > get_id_from_sid ( id , & loc_type , sid ) ;
2003-03-07 23:47:59 +00:00
if ( NT_STATUS_IS_ERR ( ret ) ) {
2003-04-02 10:36:02 +00:00
if ( remote_map ) {
ret = remote_map - > get_id_from_sid ( id , id_type , sid ) ;
2003-03-08 17:29:40 +00:00
if ( NT_STATUS_IS_ERR ( ret ) ) {
DEBUG ( 3 , ( " idmap_get_id_from_sid: error fetching id! \n " ) ) ;
2003-04-02 10:36:02 +00:00
return ret ;
2003-03-08 17:29:40 +00:00
} else {
loc_type | = ID_CACHE ;
idmap_set_mapping ( sid , * id , loc_type ) ;
}
}
} else {
* id_type = loc_type & ID_TYPEMASK ;
2003-03-07 23:47:59 +00:00
}
return ret ;
}
/* Get SID from ID */
2003-03-08 17:29:40 +00:00
NTSTATUS idmap_get_sid_from_id ( DOM_SID * sid , unid_t id , int id_type )
2003-03-07 23:47:59 +00:00
{
NTSTATUS ret ;
2003-03-08 17:29:40 +00:00
int loc_type ;
2003-03-07 23:47:59 +00:00
2003-03-08 17:29:40 +00:00
loc_type = id_type ;
2003-04-02 10:36:02 +00:00
if ( remote_map ) {
2003-03-08 17:29:40 +00:00
loc_type = id_type | ID_NOMAP ;
}
2003-04-02 10:36:02 +00:00
ret = local_map - > get_sid_from_id ( sid , id , loc_type ) ;
2003-03-07 23:47:59 +00:00
if ( NT_STATUS_IS_ERR ( ret ) ) {
2003-04-02 10:36:02 +00:00
if ( remote_map ) {
ret = remote_map - > get_sid_from_id ( sid , id , id_type ) ;
2003-03-08 17:29:40 +00:00
if ( NT_STATUS_IS_ERR ( ret ) ) {
DEBUG ( 3 , ( " idmap_get_sid_from_id: unable to fetch sid! \n " ) ) ;
2003-04-02 10:36:02 +00:00
return ret ;
2003-03-08 17:29:40 +00:00
} else {
loc_type | = ID_CACHE ;
idmap_set_mapping ( sid , id , loc_type ) ;
}
}
2003-03-07 23:47:59 +00:00
}
return ret ;
}
/* Close backend */
NTSTATUS idmap_close ( void )
{
NTSTATUS ret ;
2003-04-02 10:36:02 +00:00
ret = local_map - > close ( ) ;
2003-03-07 23:47:59 +00:00
if ( NT_STATUS_IS_ERR ( ret ) ) {
2003-03-08 17:29:40 +00:00
DEBUG ( 3 , ( " idmap_close: failed to close local cache! \n " ) ) ;
}
2003-04-02 10:36:02 +00:00
if ( remote_map ) {
ret = remote_map - > close ( ) ;
2003-03-08 17:29:40 +00:00
if ( NT_STATUS_IS_ERR ( ret ) ) {
DEBUG ( 3 , ( " idmap_close: failed to close remote idmap repository! \n " ) ) ;
}
2003-03-07 23:47:59 +00:00
}
return ret ;
}
/* Dump backend status */
void idmap_status ( void )
{
2003-04-02 10:36:02 +00:00
local_map - > status ( ) ;
if ( remote_map ) remote_map - > status ( ) ;
2003-03-07 23:47:59 +00:00
}