openldap/servers/slapd/dn.c

493 lines
7.9 KiB
C
Raw Normal View History

1998-08-09 08:43:13 +08:00
/* dn.c - routines for dealing with distinguished names */
/* $OpenLDAP$ */
1999-08-07 07:07:46 +08:00
/*
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
1998-08-09 08:43:13 +08:00
#include "portable.h"
1998-10-25 09:41:42 +08:00
#include <stdio.h>
#include <ac/ctype.h>
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>
#include "ldap_pvt.h"
1998-08-09 08:43:13 +08:00
#include "slap.h"
#define B4LEADTYPE 0
#define B4TYPE 1
#define INOIDTYPE 2
#define INKEYTYPE 3
#define B4EQUAL 4
#define B4VALUE 5
#define INVALUE 6
#define INQUOTEDVALUE 7
#define B4SEPARATOR 8
1998-08-09 08:43:13 +08:00
/*
* dn_validate - validate and compress dn. the dn is
* compressed in place are returned if valid.
1998-08-09 08:43:13 +08:00
*/
char *
dn_validate( char *dn )
1998-08-09 08:43:13 +08:00
{
1999-04-02 11:45:33 +08:00
char *d, *s;
int state, gotesc;
1998-08-09 08:43:13 +08:00
gotesc = 0;
state = B4LEADTYPE;
1998-08-09 08:43:13 +08:00
for ( d = s = dn; *s; s++ ) {
switch ( state ) {
case B4LEADTYPE:
1998-08-09 08:43:13 +08:00
case B4TYPE:
if ( LEADOIDCHAR(*s) ) {
state = INOIDTYPE;
*d++ = *s;
} else if ( LEADKEYCHAR(*s) ) {
state = INKEYTYPE;
*d++ = *s;
} else if ( ! SPACE( *s ) ) {
dn = NULL;
state = INKEYTYPE;
1999-04-02 11:45:33 +08:00
*d++ = *s;
1998-08-09 08:43:13 +08:00
}
break;
case INOIDTYPE:
if ( OIDCHAR(*s) ) {
*d++ = *s;
} else if ( *s == '=' ) {
1998-08-09 08:43:13 +08:00
state = B4VALUE;
1999-04-02 11:45:33 +08:00
*d++ = *s;
1998-08-09 08:43:13 +08:00
} else if ( SPACE( *s ) ) {
state = B4EQUAL;
} else {
dn = NULL;
1999-04-02 11:45:33 +08:00
*d++ = *s;
1998-08-09 08:43:13 +08:00
}
break;
case INKEYTYPE:
if ( KEYCHAR(*s) ) {
*d++ = *s;
} else if ( *s == '=' ) {
state = B4VALUE;
*d++ = *s;
} else if ( SPACE( *s ) ) {
state = B4EQUAL;
} else {
dn = NULL;
*d++ = *s;
}
break;
1998-08-09 08:43:13 +08:00
case B4EQUAL:
if ( *s == '=' ) {
state = B4VALUE;
*d++ = *s;
} else if ( ! SPACE( *s ) ) {
/* not a valid dn - but what can we do here? */
1999-04-02 11:45:33 +08:00
*d++ = *s;
dn = NULL;
1998-08-09 08:43:13 +08:00
}
break;
1998-08-09 08:43:13 +08:00
case B4VALUE:
if ( *s == '"' ) {
state = INQUOTEDVALUE;
*d++ = *s;
} else if ( ! SPACE( *s ) ) {
state = INVALUE;
1999-04-02 11:45:33 +08:00
*d++ = *s;
1998-08-09 08:43:13 +08:00
}
break;
1998-08-09 08:43:13 +08:00
case INVALUE:
if ( !gotesc && SEPARATOR( *s ) ) {
while ( SPACE( *(d - 1) ) )
d--;
state = B4TYPE;
if ( *s == '+' ) {
*d++ = *s;
} else {
*d++ = ',';
}
} else if ( gotesc && !NEEDSESCAPE( *s ) &&
!SEPARATOR( *s ) ) {
1999-04-02 11:45:33 +08:00
*--d = *s;
1998-08-09 08:43:13 +08:00
d++;
} else {
1999-04-02 11:45:33 +08:00
*d++ = *s;
1998-08-09 08:43:13 +08:00
}
break;
1998-08-09 08:43:13 +08:00
case INQUOTEDVALUE:
if ( !gotesc && *s == '"' ) {
state = B4SEPARATOR;
*d++ = *s;
} else if ( gotesc && !NEEDSESCAPE( *s ) ) {
1999-04-02 11:45:33 +08:00
*--d = *s;
1998-08-09 08:43:13 +08:00
d++;
} else {
1999-04-02 11:45:33 +08:00
*d++ = *s;
1998-08-09 08:43:13 +08:00
}
break;
case B4SEPARATOR:
if ( SEPARATOR( *s ) ) {
state = B4TYPE;
*d++ = *s;
}
break;
default:
dn = NULL;
1998-08-09 08:43:13 +08:00
Debug( LDAP_DEBUG_ANY,
"dn_validate - unknown state %d\n", state, 0, 0 );
1998-08-09 08:43:13 +08:00
break;
}
if ( *s == '\\' ) {
gotesc = 1;
} else {
gotesc = 0;
}
}
*d = '\0';
if( gotesc ) {
/* shouldn't be left in escape */
dn = NULL;
}
/* check end state */
switch( state ) {
case B4LEADTYPE: /* looking for first type */
case B4SEPARATOR: /* looking for separator */
case INVALUE: /* inside value */
break;
default:
dn = NULL;
}
1998-08-09 08:43:13 +08:00
return( dn );
}
/*
* dn_normalize - put dn into a canonical form suitable for storing
1999-04-02 11:45:33 +08:00
* in a hash database. this involves normalizing the case as well as
* the format. the dn is normalized in place as well as returned if valid.
1998-08-09 08:43:13 +08:00
*/
1999-04-02 11:45:33 +08:00
char *
dn_normalize( char *dn )
1998-08-09 08:43:13 +08:00
{
ldap_pvt_str2upper( dn );
/* validate and compress dn */
dn = dn_validate( dn );
1998-08-09 08:43:13 +08:00
/* and upper case it */
return( dn );
1999-04-02 11:45:33 +08:00
}
1998-08-09 08:43:13 +08:00
/*
* dn_parent - return a copy of the dn of dn's parent
*/
char *
dn_parent(
Backend *be,
1999-08-21 03:00:44 +08:00
const char *dn
1998-08-09 08:43:13 +08:00
)
{
1999-08-21 03:00:44 +08:00
const char *s;
int inquote;
1998-08-09 08:43:13 +08:00
1999-01-24 03:34:02 +08:00
if( dn == NULL ) {
return NULL;
}
while(*dn && SPACE(*dn)) {
dn++;
}
if( *dn == '\0' ) {
return( NULL );
}
if ( be != NULL && be_issuffix( be, dn ) ) {
1998-08-09 08:43:13 +08:00
return( NULL );
}
/*
* else assume it is an X.500-style name, which looks like
* foo=bar,sha=baz,...
*/
inquote = 0;
for ( s = dn; *s; s++ ) {
if ( *s == '\\' ) {
1999-01-24 03:34:02 +08:00
if ( *(s + 1) ) {
1998-08-09 08:43:13 +08:00
s++;
1999-01-24 03:34:02 +08:00
}
1998-08-09 08:43:13 +08:00
continue;
}
if ( inquote ) {
1999-01-24 03:34:02 +08:00
if ( *s == '"' ) {
1998-08-09 08:43:13 +08:00
inquote = 0;
1999-01-24 03:34:02 +08:00
}
1998-08-09 08:43:13 +08:00
} else {
1999-01-24 03:34:02 +08:00
if ( *s == '"' ) {
1998-08-09 08:43:13 +08:00
inquote = 1;
1999-01-24 03:34:02 +08:00
} else if ( DNSEPARATOR( *s ) ) {
return( ch_strdup( &s[1] ) );
}
1998-08-09 08:43:13 +08:00
}
}
1999-01-24 03:34:02 +08:00
return( ch_strdup( "" ) );
1998-08-09 08:43:13 +08:00
}
char * dn_rdn(
Backend *be,
char *dn )
{
char *s;
int inquote;
if( dn == NULL ) {
return NULL;
}
while(*dn && SPACE(*dn)) {
dn++;
}
if( *dn == '\0' ) {
return( NULL );
}
if ( be != NULL && be_issuffix( be, dn ) ) {
return( NULL );
}
dn = ch_strdup( dn );
inquote = 0;
for ( s = dn; *s; s++ ) {
if ( *s == '\\' ) {
if ( *(s + 1) ) {
s++;
}
continue;
}
if ( inquote ) {
if ( *s == '"' ) {
inquote = 0;
}
} else {
if ( *s == '"' ) {
inquote = 1;
} else if ( DNSEPARATOR( *s ) ) {
*s = '\0';
return( dn );
}
}
}
return( dn );
}
/*
* return a charray of all subtrees to which the DN resides in
*/
char **dn_subtree(
Backend *be,
1999-08-21 03:00:44 +08:00
const char *dn )
{
char *child, *parent;
char **subtree = NULL;
1999-08-21 03:00:44 +08:00
child = ch_strdup( dn );
do {
charray_add( &subtree, child );
parent = dn_parent( be, child );
1999-08-21 03:00:44 +08:00
free( child );
child = parent;
} while ( child != NULL );
return subtree;
}
1998-08-09 08:43:13 +08:00
/*
* dn_issuffix - tells whether suffix is a suffix of dn. both dn
* and suffix must be normalized.
*/
int
dn_issuffix(
char *dn,
char *suffix
)
{
int dnlen, suffixlen;
if ( dn == NULL ) {
return( 0 );
}
suffixlen = strlen( suffix );
dnlen = strlen( dn );
if ( suffixlen > dnlen ) {
return( 0 );
}
1999-01-20 11:05:35 +08:00
return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
1998-08-09 08:43:13 +08:00
}
/*
* get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
* build_new_dn().
*
* Copyright 1999, Juan C. Gomez, All rights reserved.
* This software is not subject to any license of Silicon Graphics
* Inc. or Purdue University.
*
* Redistribution and use in source and binary forms are permitted
* without restriction or fee of any kind as long as this notice
* is preserved.
*
*/
/* get_next_substring:
*
* Gets next substring in s, using d (or the end of the string '\0') as a
* string delimiter, and places it in a duplicated memory space. Leading
* spaces are ignored. String s **must** be null-terminated.
*/
static char *
get_next_substring( char * s, char d )
{
char *str, *r;
r = str = ch_malloc( strlen(s) + 1 );
/* Skip leading spaces */
while ( *s && SPACE(*s) ) {
s++;
}
/* Copy word */
while ( *s && (*s != d) ) {
/* Don't stop when you see trailing spaces may be a multi-word
* string, i.e. name=John Doe!
*/
*str++ = *s++;
}
*str = '\0';
return r;
}
/* rdn_attr_type:
*
* Given a string (i.e. an rdn) of the form:
* "attribute_type = attribute_value"
* this function returns the type of an attribute, that is the
* string "attribute_type" which is placed in newly allocated
* memory. The returned string will be null-terminated.
*/
char * rdn_attr_type( char * s )
{
return get_next_substring( s, '=' );
}
/* rdn_attr_value:
*
* Given a string (i.e. an rdn) of the form:
* "attribute_type = attribute_value"
* this function returns "attribute_type" which is placed in newly allocated
* memory. The returned string will be null-terminated and may contain
* spaces (i.e. "John Doe\0").
*/
char *
rdn_attr_value( char * rdn )
{
char *str;
if ( (str = strchr( rdn, '=' )) != NULL ) {
return get_next_substring(++str, '\0');
}
return NULL;
}
int rdn_validate( const char * rdn )
{
/* just a simple check for now */
return strchr( rdn, '=' ) != NULL;
}
/* build_new_dn:
*
* Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
* renamed.
*
* new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
*/
void
1999-08-21 03:00:44 +08:00
build_new_dn( char ** new_dn,
const char *e_dn,
const char * p_dn,
const char * newrdn )
{
1999-03-25 05:45:15 +08:00
if ( p_dn == NULL ) {
*new_dn = ch_strdup( newrdn );
return;
}
*new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
strcpy( *new_dn, newrdn );
1999-03-25 05:45:15 +08:00
strcat( *new_dn, "," );
strcat( *new_dn, p_dn );
}