2001-11-29 06:21:56 +00:00
/*
2002-01-30 06:08:46 +00:00
Unix SMB / CIFS implementation .
2001-11-29 06:21:56 +00:00
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"
2002-01-16 02:22:30 +00:00
/* return a ldap dn path from a string, given separators and field name
2001-12-19 12:21:12 +00:00
caller must free
*/
2002-01-16 02:22:30 +00:00
char * ads_build_path ( const char * realm , const char * sep , const char * field , int reverse )
2001-11-29 06:21:56 +00:00
{
char * p , * r ;
2002-01-16 02:22:30 +00:00
int numbits = 0 ;
2001-11-29 06:21:56 +00:00
char * ret ;
int len ;
r = strdup ( realm ) ;
2003-02-04 23:44:28 +00:00
if ( ! r | | ! * r )
return r ;
2001-11-29 06:21:56 +00:00
2003-02-04 23:44:28 +00:00
for ( p = r ; * p ; p + + )
if ( strchr ( sep , * p ) )
numbits + + ;
2001-11-29 06:21:56 +00:00
2002-01-16 02:22:30 +00:00
len = ( numbits + 1 ) * ( strlen ( field ) + 1 ) + strlen ( r ) + 1 ;
2001-11-29 06:21:56 +00:00
ret = malloc ( len ) ;
2003-02-04 23:44:28 +00:00
if ( ! ret )
return NULL ;
2002-01-16 02:22:30 +00:00
strlcpy ( ret , field , len ) ;
p = strtok ( r , sep ) ;
2001-11-29 06:21:56 +00:00
strlcat ( ret , p , len ) ;
2002-01-16 02:22:30 +00:00
while ( ( p = strtok ( NULL , sep ) ) ) {
char * s ;
2003-02-04 23:44:28 +00:00
if ( reverse )
2002-01-16 02:22:30 +00:00
asprintf ( & s , " %s%s,%s " , field , p , ret ) ;
2003-02-04 23:44:28 +00:00
else
2002-01-16 02:22:30 +00:00
asprintf ( & s , " %s,%s%s " , ret , field , p ) ;
free ( ret ) ;
ret = s ;
2001-11-29 06:21:56 +00:00
}
free ( r ) ;
return ret ;
}
2002-01-16 02:22:30 +00:00
/* return a dn of the form "dc=AA,dc=BB,dc=CC" from a
realm of the form AA . BB . CC
caller must free
*/
char * ads_build_dn ( const char * realm )
{
return ads_build_path ( realm , " . " , " dc= " , 0 ) ;
}
2001-11-29 06:21:56 +00:00
2001-11-29 06:38:54 +00:00
# ifndef LDAP_PORT
# define LDAP_PORT 389
# endif
2001-11-29 06:21:56 +00:00
/*
initialise a ADS_STRUCT , ready for some ads_ ops
*/
ADS_STRUCT * ads_init ( const char * realm ,
2002-07-15 10:35:28 +00:00
const char * workgroup ,
2002-08-17 17:00:51 +00:00
const char * ldap_server )
2001-11-29 06:21:56 +00:00
{
ADS_STRUCT * ads ;
ads = ( ADS_STRUCT * ) smb_xmalloc ( sizeof ( * ads ) ) ;
2001-12-05 06:26:56 +00:00
ZERO_STRUCTP ( ads ) ;
2001-11-29 06:21:56 +00:00
2002-08-17 17:00:51 +00:00
ads - > server . realm = realm ? strdup ( realm ) : NULL ;
ads - > server . workgroup = workgroup ? strdup ( workgroup ) : NULL ;
ads - > server . ldap_server = ldap_server ? strdup ( ldap_server ) : NULL ;
2003-06-25 17:41:05 +00:00
/* we need to know if this is a foreign realm */
2003-10-22 23:38:20 +00:00
if ( realm & & * realm & & ! strequal ( lp_realm ( ) , realm ) ) {
2002-08-17 17:00:51 +00:00
ads - > server . foreign = 1 ;
2002-07-15 10:35:28 +00:00
}
2003-10-22 23:38:20 +00:00
if ( workgroup & & * workgroup & & ! strequal ( lp_workgroup ( ) , workgroup ) ) {
2002-08-17 17:00:51 +00:00
ads - > server . foreign = 1 ;
2001-11-29 06:21:56 +00:00
}
return ads ;
}
2002-07-15 10:35:28 +00:00
/* a simpler ads_init() interface using all defaults */
ADS_STRUCT * ads_init_simple ( void )
{
2002-08-17 17:00:51 +00:00
return ads_init ( NULL , NULL , NULL ) ;
2002-07-15 10:35:28 +00:00
}
2001-11-29 06:21:56 +00:00
/*
free the memory used by the ADS structure initialized with ' ads_init ( . . . ) '
*/
void ads_destroy ( ADS_STRUCT * * ads )
{
2001-12-05 06:26:56 +00:00
if ( ads & & * ads ) {
2001-12-05 10:35:25 +00:00
# if HAVE_LDAP
2001-11-29 06:21:56 +00:00
if ( ( * ads ) - > ld ) ldap_unbind ( ( * ads ) - > ld ) ;
2001-12-05 10:35:25 +00:00
# endif
2002-08-17 17:00:51 +00:00
SAFE_FREE ( ( * ads ) - > server . realm ) ;
SAFE_FREE ( ( * ads ) - > server . workgroup ) ;
SAFE_FREE ( ( * ads ) - > server . ldap_server ) ;
2002-10-01 18:26:00 +00:00
SAFE_FREE ( ( * ads ) - > server . ldap_uri ) ;
2002-08-17 17:00:51 +00:00
SAFE_FREE ( ( * ads ) - > auth . realm ) ;
SAFE_FREE ( ( * ads ) - > auth . password ) ;
SAFE_FREE ( ( * ads ) - > auth . user_name ) ;
SAFE_FREE ( ( * ads ) - > auth . kdc_server ) ;
SAFE_FREE ( ( * ads ) - > config . realm ) ;
SAFE_FREE ( ( * ads ) - > config . bind_path ) ;
SAFE_FREE ( ( * ads ) - > config . ldap_server_name ) ;
2001-11-29 06:21:56 +00:00
ZERO_STRUCTP ( * ads ) ;
SAFE_FREE ( * ads ) ;
}
}