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
/* convert a single name to a sid in a domain */
NTSTATUS ads_name_to_sid ( ADS_STRUCT * ads ,
const char * name ,
DOM_SID * sid ,
enum SID_NAME_USE * type )
{
const char * attrs [ ] = { " objectSid " , " sAMAccountType " , NULL } ;
int count ;
ADS_STATUS rc ;
void * res = NULL ;
2003-06-30 05:42:15 +00:00
char * ldap_exp ;
2002-09-27 12:23:47 +00:00
uint32 t ;
NTSTATUS status = NT_STATUS_UNSUCCESSFUL ;
2003-02-19 12:31:16 +00:00
char * escaped_name = escape_ldap_string_alloc ( name ) ;
char * escaped_realm = escape_ldap_string_alloc ( ads - > config . realm ) ;
if ( ! escaped_name | | ! escaped_realm ) {
status = NT_STATUS_NO_MEMORY ;
goto done ;
}
2002-09-27 12:23:47 +00:00
2003-06-30 05:42:15 +00:00
if ( asprintf ( & ldap_exp , " (|(sAMAccountName=%s)(userPrincipalName=%s@%s)) " ,
2003-02-19 12:31:16 +00:00
escaped_name , escaped_name , escaped_realm ) = = - 1 ) {
2002-09-27 12:23:47 +00:00
DEBUG ( 1 , ( " ads_name_to_sid: asprintf failed! \n " ) ) ;
status = NT_STATUS_NO_MEMORY ;
goto done ;
}
2003-06-30 05:42:15 +00:00
rc = ads_search_retry ( ads , & res , ldap_exp , attrs ) ;
free ( ldap_exp ) ;
2002-09-27 12:23:47 +00:00
if ( ! ADS_ERR_OK ( rc ) ) {
DEBUG ( 1 , ( " name_to_sid ads_search: %s \n " , ads_errstr ( rc ) ) ) ;
goto done ;
}
count = ads_count_replies ( ads , res ) ;
if ( count ! = 1 ) {
DEBUG ( 1 , ( " name_to_sid: %s not found \n " , name ) ) ;
goto done ;
}
if ( ! ads_pull_sid ( ads , res , " objectSid " , sid ) ) {
DEBUG ( 1 , ( " No sid for %s !? \n " , name ) ) ;
goto done ;
}
if ( ! ads_pull_uint32 ( ads , res , " sAMAccountType " , & t ) ) {
DEBUG ( 1 , ( " No sAMAccountType for %s !? \n " , name ) ) ;
goto done ;
}
* type = ads_atype_map ( t ) ;
status = NT_STATUS_OK ;
DEBUG ( 3 , ( " ads name_to_sid mapped %s \n " , name ) ) ;
done :
if ( res ) ads_msgfree ( ads , res ) ;
2003-02-19 12:31:16 +00:00
SAFE_FREE ( escaped_name ) ;
SAFE_FREE ( escaped_realm ) ;
2002-09-27 12:23:47 +00:00
return status ;
}
/* convert a sid to a user or group name */
NTSTATUS ads_sid_to_name ( ADS_STRUCT * ads ,
TALLOC_CTX * mem_ctx ,
2002-09-28 12:27:04 +00:00
const DOM_SID * sid ,
2002-09-27 12:23:47 +00:00
char * * name ,
enum SID_NAME_USE * type )
{
const char * attrs [ ] = { " userPrincipalName " ,
" sAMAccountName " ,
" sAMAccountType " , NULL } ;
ADS_STATUS rc ;
void * msg = NULL ;
2003-06-30 05:42:15 +00:00
char * ldap_exp = NULL ;
2002-09-27 12:23:47 +00:00
char * sidstr = NULL ;
uint32 atype ;
NTSTATUS status = NT_STATUS_UNSUCCESSFUL ;
if ( ! ( sidstr = sid_binstring ( sid ) ) ) {
DEBUG ( 1 , ( " ads_sid_to_name: sid_binstring failed! \n " ) ) ;
status = NT_STATUS_NO_MEMORY ;
goto done ;
}
2003-06-30 05:42:15 +00:00
if ( asprintf ( & ldap_exp , " (objectSid=%s) " , sidstr ) = = - 1 ) {
2002-09-27 12:23:47 +00:00
DEBUG ( 1 , ( " ads_sid_to_name: asprintf failed! \n " ) ) ;
status = NT_STATUS_NO_MEMORY ;
goto done ;
}
2003-06-30 05:42:15 +00:00
rc = ads_search_retry ( ads , & msg , ldap_exp , attrs ) ;
2002-09-27 12:23:47 +00:00
if ( ! ADS_ERR_OK ( rc ) ) {
status = ads_ntstatus ( rc ) ;
DEBUG ( 1 , ( " ads_sid_to_name ads_search: %s \n " , ads_errstr ( rc ) ) ) ;
goto done ;
}
if ( ! ads_pull_uint32 ( ads , msg , " sAMAccountType " , & atype ) ) {
goto done ;
}
* name = ads_pull_username ( ads , mem_ctx , msg ) ;
if ( ! * name ) {
DEBUG ( 1 , ( " ads_sid_to_name: ads_pull_username retuned NULL! \n " ) ) ;
status = NT_STATUS_NO_MEMORY ;
goto done ;
}
* type = ads_atype_map ( atype ) ;
status = NT_STATUS_OK ;
DEBUG ( 3 , ( " ads sid_to_name mapped %s \n " , * name ) ) ;
done :
if ( msg ) ads_msgfree ( ads , msg ) ;
2003-06-30 05:42:15 +00:00
SAFE_FREE ( ldap_exp ) ;
2002-09-27 12:23:47 +00:00
SAFE_FREE ( sidstr ) ;
return status ;
}
2004-01-05 00:13:00 +00:00
/* convert a sid to a DN */
NTSTATUS ads_sid_to_dn ( ADS_STRUCT * ads ,
TALLOC_CTX * mem_ctx ,
const DOM_SID * sid ,
char * * dn )
{
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 ;
char * dn2 ;
NTSTATUS status = NT_STATUS_UNSUCCESSFUL ;
if ( ! ( sidstr = sid_binstring ( sid ) ) ) {
DEBUG ( 1 , ( " ads_sid_to_dn: sid_binstring failed! \n " ) ) ;
status = NT_STATUS_NO_MEMORY ;
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 00:13:00 +00:00
status = NT_STATUS_NO_MEMORY ;
goto done ;
}
2004-01-05 01:06:56 +00:00
rc = ads_search_retry ( ads , ( void * * ) & msg , ldap_exp , NULL ) ;
2004-01-05 00:13:00 +00:00
if ( ! ADS_ERR_OK ( rc ) ) {
status = ads_ntstatus ( 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 00:13:00 +00:00
status = NT_STATUS_UNSUCCESSFUL ;
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 ) {
status = NT_STATUS_NO_MEMORY ;
goto done ;
}
* dn = talloc_strdup ( mem_ctx , dn2 ) ;
if ( ! * dn ) {
SAFE_FREE ( dn2 ) ;
status = NT_STATUS_NO_MEMORY ;
goto done ;
}
status = NT_STATUS_OK ;
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 ) ;
SAFE_FREE ( sidstr ) ;
return status ;
}
2002-09-27 12:23:47 +00:00
# endif