2022-03-23 12:44:07 +01:00
/*
2004-05-01 09:45:56 +00:00
ldb database library
Copyright ( C ) Andrew Tridgell 2004
* * NOTE ! The following LGPL license applies to the ldb
* * library . This does NOT imply that all of Samba is released
* * under the LGPL
2022-03-23 12:44:07 +01:00
2004-05-01 09:45:56 +00:00
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation ; either
2007-07-10 02:46:15 +00:00
version 3 of the License , or ( at your option ) any later version .
2004-05-01 09:45:56 +00:00
This library 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
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public
2007-07-10 03:42:26 +00:00
License along with this library ; if not , see < http : //www.gnu.org/licenses/>.
2004-05-01 09:45:56 +00:00
*/
/*
* Name : ldb
*
* Component : ldb utf8 handling
*
* Description : case folding and case comparison for UTF8 strings
*
* Author : Andrew Tridgell
*/
2009-01-29 18:39:30 -05:00
# include "ldb_private.h"
2006-09-06 04:58:06 +00:00
# include "system/locale.h"
2004-05-01 09:45:56 +00:00
2006-02-04 00:38:48 +00:00
2004-05-01 09:45:56 +00:00
/*
2006-02-04 00:38:48 +00:00
this allow the user to pass in a caseless comparison
function to handle utf8 caseless comparisons
*/
void ldb_set_utf8_fns ( struct ldb_context * ldb ,
2008-08-22 17:36:56 +10:00
void * context ,
char * ( * casefold ) ( void * , void * , const char * , size_t ) )
2006-02-04 00:38:48 +00:00
{
if ( context )
ldb - > utf8_fns . context = context ;
if ( casefold )
ldb - > utf8_fns . casefold = casefold ;
}
/*
a simple case folding function
NOTE : does not handle UTF8
2004-05-01 09:45:56 +00:00
*/
2010-07-13 02:37:58 +03:00
char * ldb_casefold_default ( void * context , TALLOC_CTX * mem_ctx , const char * s , size_t n )
2004-05-01 09:45:56 +00:00
{
2009-11-06 18:35:17 +01:00
size_t i ;
2008-08-22 17:36:56 +10:00
char * ret = talloc_strndup ( mem_ctx , s , n ) ;
2004-05-01 09:45:56 +00:00
if ( ! s ) {
errno = ENOMEM ;
return NULL ;
}
for ( i = 0 ; ret [ i ] ; i + + ) {
2022-11-10 14:44:59 +01:00
ret [ i ] = ldb_ascii_toupper ( ( unsigned char ) ret [ i ] ) ;
2004-05-01 09:45:56 +00:00
}
return ret ;
}
2006-02-04 00:38:48 +00:00
void ldb_set_utf8_default ( struct ldb_context * ldb )
{
2006-02-04 18:30:30 +00:00
ldb_set_utf8_fns ( ldb , NULL , ldb_casefold_default ) ;
2006-02-04 00:38:48 +00:00
}
2010-07-13 02:37:58 +03:00
char * ldb_casefold ( struct ldb_context * ldb , TALLOC_CTX * mem_ctx , const char * s , size_t n )
2006-02-04 00:38:48 +00:00
{
2008-08-22 17:36:56 +10:00
return ldb - > utf8_fns . casefold ( ldb - > utf8_fns . context , mem_ctx , s , n ) ;
2006-02-04 00:38:48 +00:00
}
/*
check the attribute name is valid according to rfc2251
returns 1 if the name is ok
*/
int ldb_valid_attr_name ( const char * s )
2004-05-01 09:45:56 +00:00
{
2009-11-06 18:35:17 +01:00
size_t i ;
2006-02-04 00:38:48 +00:00
if ( ! s | | ! s [ 0 ] )
return 0 ;
/* handle special ldb_tdb wildcard */
if ( strcmp ( s , " * " ) = = 0 ) return 1 ;
for ( i = 0 ; s [ i ] ; i + + ) {
if ( ! isascii ( s [ i ] ) ) {
return 0 ;
}
if ( i = = 0 ) { /* first char must be an alpha (or our special '@' identifier) */
if ( ! ( isalpha ( s [ i ] ) | | ( s [ i ] = = ' @ ' ) ) ) {
return 0 ;
}
} else {
if ( ! ( isalnum ( s [ i ] ) | | ( s [ i ] = = ' - ' ) ) ) {
return 0 ;
}
}
}
return 1 ;
2004-05-01 09:45:56 +00:00
}
2010-07-13 02:37:58 +03:00
char * ldb_attr_casefold ( TALLOC_CTX * mem_ctx , const char * s )
2006-02-04 06:57:28 +00:00
{
2009-11-06 18:35:17 +01:00
size_t i ;
2006-02-04 06:57:28 +00:00
char * ret = talloc_strdup ( mem_ctx , s ) ;
2006-10-04 19:45:34 +00:00
if ( ! ret ) {
2006-02-04 06:57:28 +00:00
errno = ENOMEM ;
return NULL ;
}
for ( i = 0 ; ret [ i ] ; i + + ) {
2022-11-10 14:44:59 +01:00
ret [ i ] = ldb_ascii_toupper ( ( unsigned char ) ret [ i ] ) ;
2006-02-04 06:57:28 +00:00
}
return ret ;
}
2005-10-28 07:05:32 +00:00
/*
we accept either ' dn ' or ' distinguishedName ' for a distinguishedName
*/
int ldb_attr_dn ( const char * attr )
{
if ( ldb_attr_cmp ( attr , " dn " ) = = 0 | |
ldb_attr_cmp ( attr , " distinguishedName " ) = = 0 ) {
return 0 ;
}
return - 1 ;
}
2022-03-23 12:45:37 +01:00
_PRIVATE_ char ldb_ascii_toupper ( char c ) {
return ( ' a ' < = c & & c < = ' z ' ) ? c ^ 0x20 : toupper ( c ) ;
}