2008-03-29 03:42:06 +03:00
/*
Unix SMB / CIFS implementation .
Winbind client library .
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/>.
*/
# include "includes.h"
2010-09-03 14:33:17 +04:00
# include <tevent.h>
2016-02-09 11:30:09 +03:00
# include "nsswitch/winbind_client.h"
2008-03-29 03:42:06 +03:00
# include "libcli/wbclient/wbclient.h"
2014-01-30 23:05:09 +04:00
# include "libcli/security/dom_sid.h"
2016-02-09 11:30:09 +03:00
# include "nsswitch/libwbclient/wbclient.h"
2014-01-30 23:05:09 +04:00
2016-09-18 15:03:33 +03:00
NTSTATUS wbc_sids_to_xids ( struct id_map * ids , uint32_t count )
2014-01-30 23:05:09 +04:00
{
TALLOC_CTX * mem_ctx ;
uint32_t i ;
2016-02-09 11:30:09 +03:00
struct wbcDomainSid * sids ;
struct wbcUnixId * xids ;
wbcErr result ;
bool wb_off ;
2014-01-30 23:05:09 +04:00
mem_ctx = talloc_new ( NULL ) ;
if ( mem_ctx = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
2016-02-09 11:30:09 +03:00
sids = talloc_array ( mem_ctx , struct wbcDomainSid , count ) ;
2014-01-30 23:05:09 +04:00
if ( sids = = NULL ) {
TALLOC_FREE ( mem_ctx ) ;
return NT_STATUS_NO_MEMORY ;
}
2016-02-09 11:30:09 +03:00
xids = talloc_array ( mem_ctx , struct wbcUnixId , count ) ;
if ( xids = = NULL ) {
TALLOC_FREE ( mem_ctx ) ;
return NT_STATUS_NO_MEMORY ;
}
2014-01-30 23:05:09 +04:00
for ( i = 0 ; i < count ; i + + ) {
2016-02-09 11:30:09 +03:00
memcpy ( & sids [ i ] , ids [ i ] . sid , sizeof ( struct dom_sid ) ) ;
2014-01-30 23:05:09 +04:00
}
2016-02-09 11:30:09 +03:00
wb_off = winbind_env_set ( ) ;
if ( wb_off ) {
( void ) winbind_on ( ) ;
}
2014-01-30 23:05:09 +04:00
2016-02-09 11:30:09 +03:00
result = wbcSidsToUnixIds ( sids , count , xids ) ;
2014-01-30 23:05:09 +04:00
2016-02-09 11:30:09 +03:00
if ( wb_off ) {
( void ) winbind_off ( ) ;
2014-01-30 23:05:09 +04:00
}
2016-02-09 11:30:09 +03:00
if ( ! WBC_ERROR_IS_OK ( result ) ) {
TALLOC_FREE ( mem_ctx ) ;
2014-04-07 06:06:21 +04:00
return NT_STATUS_INTERNAL_ERROR ;
}
2014-01-30 23:05:09 +04:00
for ( i = 0 ; i < count ; i + + ) {
2016-02-09 11:30:09 +03:00
struct wbcUnixId * xid = & xids [ i ] ;
2014-01-30 23:05:09 +04:00
struct unixid * id = & ids [ i ] . xid ;
2016-02-09 11:30:09 +03:00
switch ( xid - > type ) {
case WBC_ID_TYPE_UID :
2014-01-30 23:05:09 +04:00
id - > type = ID_TYPE_UID ;
2016-02-09 11:30:09 +03:00
id - > id = xid - > id . uid ;
2014-01-30 23:05:09 +04:00
break ;
2016-02-09 11:30:09 +03:00
case WBC_ID_TYPE_GID :
2014-01-30 23:05:09 +04:00
id - > type = ID_TYPE_GID ;
2016-02-09 11:30:09 +03:00
id - > id = xid - > id . gid ;
2014-01-30 23:05:09 +04:00
break ;
2016-02-09 11:30:09 +03:00
case WBC_ID_TYPE_BOTH :
2014-01-30 23:05:09 +04:00
id - > type = ID_TYPE_BOTH ;
2016-02-09 11:30:09 +03:00
id - > id = xid - > id . uid ;
2014-01-30 23:05:09 +04:00
break ;
2016-02-09 11:30:09 +03:00
case WBC_ID_TYPE_NOT_SPECIFIED :
2014-01-30 23:05:09 +04:00
id - > type = ID_TYPE_NOT_SPECIFIED ;
id - > id = UINT32_MAX ;
break ;
}
2016-02-09 11:30:09 +03:00
ids [ i ] . status = ID_MAPPED ;
2014-02-01 23:46:28 +04:00
}
2016-02-09 11:30:09 +03:00
TALLOC_FREE ( mem_ctx ) ;
2014-02-01 23:46:28 +04:00
2016-02-09 11:30:09 +03:00
return NT_STATUS_OK ;
2014-02-01 23:46:28 +04:00
}
2016-09-18 15:06:24 +03:00
NTSTATUS wbc_xids_to_sids ( struct id_map * ids , uint32_t count )
2014-02-01 23:46:28 +04:00
{
2016-02-09 11:30:09 +03:00
TALLOC_CTX * mem_ctx ;
uint32_t i ;
struct wbcDomainSid * sids ;
struct wbcUnixId * xids ;
wbcErr result ;
bool wb_off ;
2014-02-01 23:46:28 +04:00
2016-02-09 11:30:09 +03:00
mem_ctx = talloc_new ( NULL ) ;
if ( mem_ctx = = NULL ) {
return NT_STATUS_NO_MEMORY ;
2014-02-01 23:46:28 +04:00
}
2016-02-09 11:30:09 +03:00
sids = talloc_array ( mem_ctx , struct wbcDomainSid , count ) ;
if ( sids = = NULL ) {
TALLOC_FREE ( mem_ctx ) ;
return NT_STATUS_NO_MEMORY ;
2014-02-01 23:46:28 +04:00
}
2016-02-09 11:30:09 +03:00
xids = talloc_array ( mem_ctx , struct wbcUnixId , count ) ;
if ( xids = = NULL ) {
TALLOC_FREE ( mem_ctx ) ;
return NT_STATUS_NO_MEMORY ;
2014-02-01 23:46:28 +04:00
}
2016-02-09 11:30:09 +03:00
for ( i = 0 ; i < count ; i + + ) {
struct id_map * id = & ids [ i ] ;
struct wbcUnixId * xid = & xids [ i ] ;
switch ( id - > xid . type ) {
case ID_TYPE_UID :
* xid = ( struct wbcUnixId ) {
. type = WBC_ID_TYPE_UID ,
. id . uid = id - > xid . id
} ;
break ;
case ID_TYPE_GID :
* xid = ( struct wbcUnixId ) {
. type = WBC_ID_TYPE_GID ,
. id . uid = id - > xid . id
} ;
break ;
default :
TALLOC_FREE ( mem_ctx ) ;
return NT_STATUS_NOT_FOUND ;
2014-02-01 23:46:28 +04:00
}
}
2016-02-09 11:30:09 +03:00
wb_off = winbind_env_set ( ) ;
if ( wb_off ) {
( void ) winbind_on ( ) ;
2014-02-01 23:46:28 +04:00
}
2016-02-09 11:30:09 +03:00
result = wbcUnixIdsToSids ( xids , count , sids ) ;
2014-02-01 23:46:28 +04:00
2016-02-09 11:30:09 +03:00
if ( wb_off ) {
( void ) winbind_off ( ) ;
2014-02-01 23:46:28 +04:00
}
2016-02-09 11:30:09 +03:00
if ( ! WBC_ERROR_IS_OK ( result ) ) {
TALLOC_FREE ( mem_ctx ) ;
return NT_STATUS_INTERNAL_ERROR ;
2014-02-01 23:46:28 +04:00
}
2016-02-09 11:30:09 +03:00
for ( i = 0 ; i < count ; i + + ) {
struct wbcDomainSid * sid = & sids [ i ] ;
struct wbcDomainSid null_sid = { 0 } ;
struct id_map * id = & ids [ i ] ;
if ( memcmp ( sid , & null_sid , sizeof ( * sid ) ) ! = 0 ) {
struct dom_sid domsid ;
id - > status = ID_MAPPED ;
memcpy ( & domsid , sid , sizeof ( struct dom_sid ) ) ;
id - > sid = dom_sid_dup ( ids , & domsid ) ;
if ( id - > sid = = NULL ) {
TALLOC_FREE ( mem_ctx ) ;
return NT_STATUS_NO_MEMORY ;
}
} else {
id - > status = ID_UNMAPPED ;
id - > sid = NULL ;
}
2014-02-01 23:46:28 +04:00
}
2016-02-09 11:30:09 +03:00
TALLOC_FREE ( mem_ctx ) ;
return NT_STATUS_OK ;
2014-02-01 23:46:28 +04:00
}