2001-11-29 09:21:56 +03:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2001-11-29 09:21:56 +03:00
ads ( active directory ) utility library
Copyright ( C ) Andrew Tridgell 2001
Copyright ( C ) Andrew Bartlett 2001
2011-01-12 20:12:29 +03:00
2001-11-29 09:21:56 +03:00
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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2001-11-29 09:21:56 +03:00
( at your option ) any later version .
2011-01-12 20:12:29 +03:00
2001-11-29 09:21:56 +03:00
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 .
2011-01-12 20:12:29 +03:00
2001-11-29 09:21:56 +03:00
You should have received a copy of the GNU General Public License
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2001-11-29 09:21:56 +03:00
*/
# include "includes.h"
2010-07-02 02:32:52 +04:00
# include "ads.h"
2001-11-29 09:21:56 +03:00
2002-01-16 05:22:30 +03:00
/* return a ldap dn path from a string, given separators and field name
2001-12-19 15:21:12 +03:00
caller must free
*/
2002-01-16 05:22:30 +03:00
char * ads_build_path ( const char * realm , const char * sep , const char * field , int reverse )
2001-11-29 09:21:56 +03:00
{
char * p , * r ;
2002-01-16 05:22:30 +03:00
int numbits = 0 ;
2001-11-29 09:21:56 +03:00
char * ret ;
int len ;
2008-01-23 13:04:10 +03:00
char * saveptr ;
2004-12-07 21:25:53 +03:00
r = SMB_STRDUP ( realm ) ;
2001-11-29 09:21:56 +03:00
2008-01-16 17:51:52 +03:00
if ( ! r | | ! * r ) {
2003-02-05 02:44:28 +03:00
return r ;
2008-01-16 17:51:52 +03:00
}
2001-11-29 09:21:56 +03:00
2008-01-16 17:51:52 +03:00
for ( p = r ; * p ; p + + ) {
if ( strchr ( sep , * p ) ) {
2003-02-05 02:44:28 +03:00
numbits + + ;
2008-01-16 17:51:52 +03:00
}
}
2001-11-29 09:21:56 +03:00
2002-01-16 05:22:30 +03:00
len = ( numbits + 1 ) * ( strlen ( field ) + 1 ) + strlen ( r ) + 1 ;
2001-11-29 09:21:56 +03:00
2006-08-20 21:55:06 +04:00
ret = ( char * ) SMB_MALLOC ( len ) ;
2008-01-16 17:51:52 +03:00
if ( ! ret ) {
free ( r ) ;
2003-02-05 02:44:28 +03:00
return NULL ;
2008-01-16 17:51:52 +03:00
}
2003-02-05 02:44:28 +03:00
2012-03-30 04:13:07 +04:00
if ( strlcpy ( ret , field , len ) > = len ) {
/* Truncate ! */
free ( r ) ;
2014-01-09 18:06:14 +04:00
free ( ret ) ;
2012-03-30 04:13:07 +04:00
return NULL ;
}
2008-01-23 13:04:10 +03:00
p = strtok_r ( r , sep , & saveptr ) ;
2006-06-13 22:09:04 +04:00
if ( p ) {
2012-03-30 04:13:07 +04:00
if ( strlcat ( ret , p , len ) > = len ) {
free ( r ) ;
2014-01-09 18:06:14 +04:00
free ( ret ) ;
2012-03-30 04:13:07 +04:00
return NULL ;
}
2011-01-12 20:12:29 +03:00
2008-01-23 13:04:10 +03:00
while ( ( p = strtok_r ( NULL , sep , & saveptr ) ) ! = NULL ) {
2008-12-23 22:56:48 +03:00
int retval ;
char * s = NULL ;
2006-06-13 22:09:04 +04:00
if ( reverse )
2008-12-23 22:56:48 +03:00
retval = asprintf ( & s , " %s%s,%s " , field , p , ret ) ;
2006-06-13 22:09:04 +04:00
else
2008-12-23 22:56:48 +03:00
retval = asprintf ( & s , " %s,%s%s " , ret , field , p ) ;
2006-06-13 22:09:04 +04:00
free ( ret ) ;
2008-12-23 22:56:48 +03:00
if ( retval = = - 1 ) {
free ( r ) ;
return NULL ;
}
2008-01-16 17:51:52 +03:00
ret = SMB_STRDUP ( s ) ;
free ( s ) ;
2006-06-13 22:09:04 +04:00
}
2001-11-29 09:21:56 +03:00
}
free ( r ) ;
return ret ;
}
2002-01-16 05:22:30 +03: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 ) ;
}
2006-12-14 20:00:10 +03:00
/* return a DNS name in the for aa.bb.cc from the DN
" dc=AA,dc=BB,dc=CC " . caller must free
*/
char * ads_build_domain ( const char * dn )
{
char * dnsdomain = NULL ;
2011-01-12 20:12:29 +03:00
2006-12-14 20:00:10 +03:00
/* result should always be shorter than the DN */
if ( ( dnsdomain = SMB_STRDUP ( dn ) ) = = NULL ) {
DEBUG ( 0 , ( " ads_build_domain: malloc() failed! \n " ) ) ;
return NULL ;
}
2012-08-09 04:01:00 +04:00
if ( ! strlower_m ( dnsdomain ) ) {
SAFE_FREE ( dnsdomain ) ;
return NULL ;
}
2006-12-14 20:00:10 +03:00
all_string_sub ( dnsdomain , " dc= " , " " , 0 ) ;
all_string_sub ( dnsdomain , " , " , " . " , 0 ) ;
return dnsdomain ;
}
2001-11-29 09:21:56 +03:00
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 ,
2002-07-15 14:35:28 +04:00
const char * workgroup ,
2002-08-17 21:00:51 +04:00
const char * ldap_server )
2001-11-29 09:21:56 +03:00
{
ADS_STRUCT * ads ;
2007-07-30 12:22:45 +04:00
int wrap_flags ;
2011-01-12 20:12:29 +03:00
2004-12-07 21:25:53 +03:00
ads = SMB_XMALLOC_P ( ADS_STRUCT ) ;
2001-12-05 09:26:56 +03:00
ZERO_STRUCTP ( ads ) ;
2011-01-12 20:12:29 +03:00
2004-12-07 21:25:53 +03:00
ads - > server . realm = realm ? SMB_STRDUP ( realm ) : NULL ;
ads - > server . workgroup = workgroup ? SMB_STRDUP ( workgroup ) : NULL ;
ads - > server . ldap_server = ldap_server ? SMB_STRDUP ( ldap_server ) : NULL ;
2002-08-17 21:00:51 +04:00
2004-03-23 01:49:40 +03:00
/* the caller will own the memory by default */
ads - > is_mine = 1 ;
2001-11-29 09:21:56 +03:00
2007-07-30 12:22:45 +04:00
wrap_flags = lp_client_ldap_sasl_wrapping ( ) ;
if ( wrap_flags = = - 1 ) {
wrap_flags = 0 ;
}
ads - > auth . flags = wrap_flags ;
2016-01-14 03:25:34 +03:00
/* Start with the configured page size when the connection is new,
2012-05-18 16:01:14 +04:00
* we will drop it by half we get a timeout . */
2016-01-14 03:25:34 +03:00
ads - > config . ldap_page_size = lp_ldap_page_size ( ) ;
2012-05-18 16:01:14 +04:00
2004-03-23 01:49:40 +03:00
return ads ;
2002-07-15 14:35:28 +04:00
}
2009-09-02 23:16:45 +04:00
/****************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
bool ads_set_sasl_wrap_flags ( ADS_STRUCT * ads , int flags )
{
if ( ! ads ) {
return false ;
}
ads - > auth . flags = flags ;
return true ;
}
2001-11-29 09:21:56 +03:00
/*
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 ) {
2007-10-19 04:40:25 +04:00
bool is_mine ;
2004-03-23 01:49:40 +03:00
is_mine = ( * ads ) - > is_mine ;
2001-12-05 13:35:25 +03:00
# if HAVE_LDAP
2007-07-16 13:48:15 +04:00
ads_disconnect ( * ads ) ;
2001-12-05 13:35:25 +03:00
# endif
2002-08-17 21:00:51 +04:00
SAFE_FREE ( ( * ads ) - > server . realm ) ;
SAFE_FREE ( ( * ads ) - > server . workgroup ) ;
SAFE_FREE ( ( * ads ) - > server . ldap_server ) ;
SAFE_FREE ( ( * ads ) - > auth . realm ) ;
SAFE_FREE ( ( * ads ) - > auth . password ) ;
SAFE_FREE ( ( * ads ) - > auth . user_name ) ;
SAFE_FREE ( ( * ads ) - > auth . kdc_server ) ;
2012-09-07 22:14:08 +04:00
SAFE_FREE ( ( * ads ) - > auth . ccache_name ) ;
2002-08-17 21:00:51 +04:00
SAFE_FREE ( ( * ads ) - > config . realm ) ;
SAFE_FREE ( ( * ads ) - > config . bind_path ) ;
SAFE_FREE ( ( * ads ) - > config . ldap_server_name ) ;
2006-08-31 08:14:08 +04:00
SAFE_FREE ( ( * ads ) - > config . server_site_name ) ;
SAFE_FREE ( ( * ads ) - > config . client_site_name ) ;
2007-07-11 17:32:57 +04:00
SAFE_FREE ( ( * ads ) - > config . schema_path ) ;
SAFE_FREE ( ( * ads ) - > config . config_path ) ;
2011-01-12 20:12:29 +03:00
2001-11-29 09:21:56 +03:00
ZERO_STRUCTP ( * ads ) ;
2004-03-23 01:49:40 +03:00
if ( is_mine )
SAFE_FREE ( * ads ) ;
2001-11-29 09:21:56 +03:00
}
}