2003-02-01 08:20:11 +03:00
/*
Unix SMB / CIFS implementation .
ldap filter argument escaping
Copyright ( C ) 1998 , 1999 , 2000 Luke Howard < lukeh @ padl . com > ,
Copyright ( C ) 2003 Andrew Bartlett < abartlet @ samba . org >
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
2003-02-01 08:20:11 +03:00
( 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
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2003-02-01 08:20:11 +03:00
*/
# include "includes.h"
/**
* Escape a parameter to an LDAP filter string , so they cannot contain
2023-07-05 12:16:18 +03:00
* embedded ( ) * or \ chars which may cause it not to parse correctly .
2003-02-01 08:20:11 +03:00
*
* @ param s The input string
*
2017-04-23 19:47:25 +03:00
* @ return A string allocated with talloc ( ) , containing the escaped string ,
* and to be talloc_free ( ) ed by the caller .
2003-02-01 08:20:11 +03:00
* */
2009-07-10 00:03:52 +04:00
char * escape_ldap_string ( TALLOC_CTX * mem_ctx , const char * s )
2003-02-01 08:20:11 +03:00
{
size_t len = strlen ( s ) + 1 ;
2009-07-10 00:03:52 +04:00
char * output = talloc_array ( mem_ctx , char , len ) ;
2003-02-01 08:20:11 +03:00
const char * sub ;
int i = 0 ;
char * p = output ;
2006-06-12 16:45:06 +04:00
if ( output = = NULL ) {
return NULL ;
}
2009-07-10 00:03:52 +04:00
2003-02-01 08:20:11 +03:00
while ( * s )
{
switch ( * s )
{
case ' * ' :
sub = " \\ 2a " ;
break ;
case ' ( ' :
sub = " \\ 28 " ;
break ;
case ' ) ' :
sub = " \\ 29 " ;
break ;
case ' \\ ' :
sub = " \\ 5c " ;
break ;
default :
sub = NULL ;
break ;
}
2009-07-10 00:03:52 +04:00
2003-02-01 08:20:11 +03:00
if ( sub ) {
2009-07-10 00:03:52 +04:00
char * tmp ;
2003-02-01 08:20:11 +03:00
len = len + 3 ;
2009-07-10 00:03:52 +04:00
tmp = talloc_realloc ( mem_ctx , output , char , len ) ;
if ( tmp = = NULL ) {
TALLOC_FREE ( output ) ;
2003-02-01 08:20:11 +03:00
return NULL ;
}
2009-07-10 00:03:52 +04:00
output = tmp ;
2003-02-01 08:20:11 +03:00
p = & output [ i ] ;
2018-05-09 18:29:39 +03:00
memcpy ( p , sub , 3 ) ;
2003-02-01 08:20:11 +03:00
p + = 3 ;
i + = 3 ;
} else {
* p = * s ;
p + + ;
i + + ;
}
s + + ;
}
2009-07-10 00:03:52 +04:00
2003-02-01 08:20:11 +03:00
* p = ' \0 ' ;
return output ;
}
2007-03-01 03:49:28 +03:00
char * escape_rdn_val_string_alloc ( const char * s )
{
char * output , * p ;
/* The maximum size of the escaped string can be twice the actual size */
output = ( char * ) SMB_MALLOC ( 2 * strlen ( s ) + 1 ) ;
if ( output = = NULL ) {
return NULL ;
}
p = output ;
2009-07-10 00:03:52 +04:00
2007-03-01 03:49:28 +03:00
while ( * s )
{
switch ( * s )
{
case ' , ' :
case ' = ' :
case ' + ' :
case ' < ' :
case ' > ' :
case ' # ' :
case ' ; ' :
case ' \\ ' :
case ' \" ' :
* p + + = ' \\ ' ;
* p + + = * s ;
break ;
default :
* p = * s ;
p + + ;
}
2009-07-10 00:03:52 +04:00
2007-03-01 03:49:28 +03:00
s + + ;
}
2009-07-10 00:03:52 +04:00
2007-03-01 03:49:28 +03:00
* p = ' \0 ' ;
/* resize the string to the actual final size */
output = ( char * ) SMB_REALLOC ( output , strlen ( output ) + 1 ) ;
return output ;
}