2001-11-29 09:21:56 +03:00
/*
Unix SMB / Netbios implementation .
Version 3.0
ads ( active directory ) utility library
Copyright ( C ) Andrew Tridgell 2001
Copyright ( C ) Andrew Bartlett 2001
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"
2001-12-08 14:18:56 +03:00
char * ads_build_dn ( const char * realm )
2001-11-29 09:21:56 +03:00
{
char * p , * r ;
int numdots = 0 ;
char * ret ;
int len ;
r = strdup ( realm ) ;
if ( ! r | | ! * r ) return r ;
for ( p = r ; * p ; p + + ) {
if ( * p = = ' . ' ) numdots + + ;
}
len = ( numdots + 1 ) * 4 + strlen ( r ) + 1 ;
ret = malloc ( len ) ;
strlcpy ( ret , " dc= " , len ) ;
p = strtok ( r , " . " ) ;
strlcat ( ret , p , len ) ;
while ( ( p = strtok ( NULL , " . " ) ) ) {
strlcat ( ret , " ,dc= " , len ) ;
strlcat ( ret , p , len ) ;
}
free ( r ) ;
return ret ;
}
# ifdef HAVE_LDAP
/*
find the ldap server from DNS
*/
static char * find_ldap_server ( ADS_STRUCT * ads )
{
char * list = NULL ;
2001-12-13 14:29:49 +03:00
struct in_addr ip ;
2001-11-29 09:21:56 +03:00
2001-12-13 14:29:49 +03:00
if ( ads - > realm & &
ldap_domain2hostlist ( ads - > realm , & list ) = = LDAP_SUCCESS ) {
2001-11-29 09:21:56 +03:00
char * p ;
p = strchr ( list , ' : ' ) ;
if ( p ) * p = 0 ;
return list ;
}
2001-12-13 14:29:49 +03:00
/* get desperate, find the domain controller IP */
if ( resolve_name ( lp_workgroup ( ) , & ip , 0x1B ) ) {
return strdup ( inet_ntoa ( ip ) ) ;
}
2001-11-29 09:21:56 +03:00
return NULL ;
}
# else
static char * find_ldap_server ( ADS_STRUCT * ads )
{
/* Without LDAP this doesn't make much sense */
return NULL ;
}
# endif
2001-11-29 09:38:54 +03:00
# ifndef LDAP_PORT
# define LDAP_PORT 389
# endif
2001-11-29 09:21:56 +03:00
/*
initialise a ADS_STRUCT , ready for some ads_ ops
*/
ADS_STRUCT * ads_init ( const char * realm ,
const char * ldap_server ,
2001-12-05 12:46:53 +03:00
const char * bind_path ,
const char * password )
2001-11-29 09:21:56 +03:00
{
ADS_STRUCT * ads ;
ads = ( ADS_STRUCT * ) smb_xmalloc ( sizeof ( * ads ) ) ;
2001-12-05 09:26:56 +03:00
ZERO_STRUCTP ( ads ) ;
2001-11-29 09:21:56 +03:00
ads - > realm = realm ? strdup ( realm ) : NULL ;
ads - > ldap_server = ldap_server ? strdup ( ldap_server ) : NULL ;
ads - > bind_path = bind_path ? strdup ( bind_path ) : NULL ;
ads - > ldap_port = LDAP_PORT ;
2001-12-05 12:46:53 +03:00
if ( password ) ads - > password = strdup ( password ) ;
2001-11-29 09:21:56 +03:00
if ( ! ads - > realm ) {
2001-12-05 09:26:56 +03:00
ads - > realm = strdup ( lp_realm ( ) ) ;
2001-11-29 09:21:56 +03:00
if ( ! ads - > realm [ 0 ] ) {
2001-12-08 14:18:56 +03:00
SAFE_FREE ( ads - > realm ) ;
2001-11-29 09:21:56 +03:00
}
}
2001-12-08 14:18:56 +03:00
if ( ! ads - > bind_path & & ads - > realm ) {
2001-11-29 09:21:56 +03:00
ads - > bind_path = ads_build_dn ( ads - > realm ) ;
}
if ( ! ads - > ldap_server ) {
2001-12-05 09:26:56 +03:00
ads - > ldap_server = strdup ( lp_ads_server ( ) ) ;
2001-11-29 09:21:56 +03:00
if ( ! ads - > ldap_server [ 0 ] ) {
ads - > ldap_server = find_ldap_server ( ads ) ;
}
}
if ( ! ads - > kdc_server ) {
/* assume its the same as LDAP */
ads - > kdc_server = ads - > ldap_server ? strdup ( ads - > ldap_server ) : NULL ;
}
return ads ;
}
/*
free the memory used by the ADS structure initialized with ' ads_init ( . . . ) '
*/
void ads_destroy ( ADS_STRUCT * * ads )
{
2001-12-05 09:26:56 +03:00
if ( ads & & * ads ) {
2001-12-05 13:35:25 +03:00
# if HAVE_LDAP
2001-11-29 09:21:56 +03:00
if ( ( * ads ) - > ld ) ldap_unbind ( ( * ads ) - > ld ) ;
2001-12-05 13:35:25 +03:00
# endif
2001-11-29 09:21:56 +03:00
SAFE_FREE ( ( * ads ) - > realm ) ;
SAFE_FREE ( ( * ads ) - > ldap_server ) ;
2001-12-08 14:18:56 +03:00
SAFE_FREE ( ( * ads ) - > ldap_server_name ) ;
2001-11-29 09:21:56 +03:00
SAFE_FREE ( ( * ads ) - > kdc_server ) ;
SAFE_FREE ( ( * ads ) - > bind_path ) ;
2001-12-05 12:46:53 +03:00
SAFE_FREE ( ( * ads ) - > password ) ;
2001-12-08 14:18:56 +03:00
SAFE_FREE ( ( * ads ) - > user_name ) ;
2001-11-29 09:21:56 +03:00
ZERO_STRUCTP ( * ads ) ;
SAFE_FREE ( * ads ) ;
}
}
2001-12-19 12:58:52 +03:00
# if HAVE_KRB5
static void ads_display_status_helper ( const char * m , uint32 code , int type )
2001-12-19 11:44:23 +03:00
{
int maj_stat , min_stat ;
gss_buffer_desc msg ;
int msg_ctx ;
msg_ctx = 0 ;
while ( 1 ) {
maj_stat = gss_display_status ( & min_stat , code ,
type , GSS_C_NULL_OID ,
& msg_ctx , & msg ) ;
DEBUG ( 1 , ( " GSS-API error %s: %s \n " , m ,
( char * ) msg . value ) ) ;
( void ) gss_release_buffer ( & min_stat , & msg ) ;
if ( ! msg_ctx )
break ;
}
}
2001-12-19 12:58:52 +03:00
# endif
2001-12-19 11:44:23 +03:00
2001-12-19 12:58:52 +03:00
void ads_display_status ( const char * msg , int maj_stat , int min_stat )
2001-12-19 11:44:23 +03:00
{
2001-12-19 12:58:52 +03:00
# if HAVE_KRB5
2001-12-19 11:44:23 +03:00
ads_display_status_helper ( msg , maj_stat , GSS_C_GSS_CODE ) ;
ads_display_status_helper ( msg , min_stat , GSS_C_MECH_CODE ) ;
2001-12-19 12:58:52 +03:00
# endif
2001-12-19 11:44:23 +03:00
}