2002-09-27 12:23:47 +00:00
/*
Unix SMB / CIFS implementation .
Winbind ADS backend functions
Copyright ( C ) Andrew Tridgell 2001
Copyright ( C ) Andrew Bartlett 2002
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
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "includes.h"
# ifdef HAVE_LDAP
2004-01-05 00:13:00 +00:00
/* convert a sid to a DN */
2004-01-05 02:04:37 +00:00
ADS_STATUS ads_sid_to_dn ( ADS_STRUCT * ads ,
TALLOC_CTX * mem_ctx ,
const DOM_SID * sid ,
char * * dn )
2004-01-05 00:13:00 +00:00
{
ADS_STATUS rc ;
LDAPMessage * msg = NULL ;
LDAPMessage * entry = NULL ;
2004-01-05 01:06:56 +00:00
char * ldap_exp ;
2004-01-05 00:13:00 +00:00
char * sidstr = NULL ;
int count ;
2004-01-05 02:04:37 +00:00
char * dn2 = NULL ;
const char * attr [ ] = {
" dn " ,
NULL
} ;
2004-01-05 00:13:00 +00:00
if ( ! ( sidstr = sid_binstring ( sid ) ) ) {
DEBUG ( 1 , ( " ads_sid_to_dn: sid_binstring failed! \n " ) ) ;
2004-01-05 02:04:37 +00:00
rc = ADS_ERROR_NT ( NT_STATUS_NO_MEMORY ) ;
2004-01-05 00:13:00 +00:00
goto done ;
}
2004-01-05 01:06:56 +00:00
if ( ! ( ldap_exp = talloc_asprintf ( mem_ctx , " (objectSid=%s) " , sidstr ) ) ) {
DEBUG ( 1 , ( " ads_sid_to_dn: talloc_asprintf failed! \n " ) ) ;
2004-01-05 02:04:37 +00:00
rc = ADS_ERROR_NT ( NT_STATUS_NO_MEMORY ) ;
2004-01-05 00:13:00 +00:00
goto done ;
}
2004-01-05 02:04:37 +00:00
rc = ads_search_retry ( ads , ( void * * ) & msg , ldap_exp , attr ) ;
2004-01-05 00:13:00 +00:00
if ( ! ADS_ERR_OK ( rc ) ) {
DEBUG ( 1 , ( " ads_sid_to_dn ads_search: %s \n " , ads_errstr ( rc ) ) ) ;
goto done ;
}
2004-01-05 01:06:56 +00:00
if ( ( count = ads_count_replies ( ads , msg ) ) ! = 1 ) {
fstring sid_string ;
2004-01-05 00:13:00 +00:00
DEBUG ( 1 , ( " ads_sid_to_dn (sid=%s): Not found (count=%d) \n " ,
2004-01-05 01:06:56 +00:00
sid_to_string ( sid_string , sid ) , count ) ) ;
2004-01-05 02:04:37 +00:00
rc = ADS_ERROR_NT ( NT_STATUS_UNSUCCESSFUL ) ;
2004-01-05 00:13:00 +00:00
goto done ;
}
2004-01-05 01:06:56 +00:00
entry = ads_first_entry ( ads , msg ) ;
2004-01-05 00:13:00 +00:00
dn2 = ads_get_dn ( ads , entry ) ;
if ( ! dn2 ) {
2004-01-05 02:04:37 +00:00
rc = ADS_ERROR_NT ( NT_STATUS_NO_MEMORY ) ;
2004-01-05 00:13:00 +00:00
goto done ;
}
* dn = talloc_strdup ( mem_ctx , dn2 ) ;
if ( ! * dn ) {
2004-01-05 02:04:37 +00:00
ads_memfree ( ads , dn2 ) ;
rc = ADS_ERROR_NT ( NT_STATUS_NO_MEMORY ) ;
2004-01-05 00:13:00 +00:00
goto done ;
}
2004-01-05 02:04:37 +00:00
rc = ADS_ERROR_NT ( NT_STATUS_OK ) ;
2004-01-05 00:13:00 +00:00
2004-01-05 01:06:56 +00:00
DEBUG ( 3 , ( " ads sid_to_dn mapped %s \n " , dn2 ) ) ;
2004-01-05 00:13:00 +00:00
SAFE_FREE ( dn2 ) ;
done :
if ( msg ) ads_msgfree ( ads , msg ) ;
2004-01-05 02:04:37 +00:00
if ( dn2 ) ads_memfree ( ads , dn2 ) ;
2004-01-05 00:13:00 +00:00
SAFE_FREE ( sidstr ) ;
2004-01-05 02:04:37 +00:00
return rc ;
2004-01-05 00:13:00 +00:00
}
2002-09-27 12:23:47 +00:00
# endif