1 /* dn.c - routines for dealing with distinguished names */
14 #define DNSEPARATOR(c) (c == ',' || c == ';')
15 #define SEPARATOR(c) (c == ',' || c == ';' || c == '+')
16 #define SPACE(c) (c == ' ' || c == '\n')
17 #define NEEDSESCAPE(c) (c == '\\' || c == '"')
23 #define INQUOTEDVALUE 5
27 * dn_normalize - put dn into a canonical format. the dn is
28 * normalized in place, as well as returned.
32 dn_normalize( char *dn )
37 /* Debug( LDAP_DEBUG_TRACE, "=> dn_normalize \"%s\"\n", dn, 0, 0 ); */
41 for ( d = s = dn; *s; s++ ) {
44 if ( ! SPACE( *s ) ) {
53 } else if ( SPACE( *s ) ) {
63 } else if ( ! SPACE( *s ) ) {
64 /* not a valid dn - but what can we do here? */
70 state = INQUOTEDVALUE;
72 } else if ( ! SPACE( *s ) ) {
78 if ( !gotesc && SEPARATOR( *s ) ) {
79 while ( SPACE( *(d - 1) ) )
87 } else if ( gotesc && !NEEDSESCAPE( *s ) &&
96 if ( !gotesc && *s == '"' ) {
99 } else if ( gotesc && !NEEDSESCAPE( *s ) ) {
107 if ( SEPARATOR( *s ) ) {
113 Debug( LDAP_DEBUG_ANY,
114 "dn_normalize - unknown state %d\n", state, 0, 0 );
125 /* Debug( LDAP_DEBUG_TRACE, "<= dn_normalize \"%s\"\n", dn, 0, 0 ); */
130 * dn_normalize_case - put dn into a canonical form suitable for storing
131 * in a hash database. this involves normalizing the case as well as
132 * the format. the dn is normalized in place as well as returned.
136 dn_normalize_case( char *dn )
140 /* normalize format */
144 for ( s = dn; *s; s++ ) {
152 * dn_parent - return a copy of the dn of dn's parent
164 if ( dn == NULL || *dn == '\0' || be_issuffix( be, dn ) ) {
169 * no =, assume it is a dns name, like blah@some.domain.name
170 * if the blah@ part is there, return some.domain.name. if
171 * it's just some.domain.name, return domain.name.
173 if ( strchr( dn, '=' ) == NULL ) {
174 if ( (s = strchr( dn, '@' )) == NULL ) {
175 if ( (s = strchr( dn, '.' )) == NULL ) {
179 if ( *(s + 1) == '\0' ) {
182 return( ch_strdup( s + 1 ) );
187 * else assume it is an X.500-style name, which looks like
188 * foo=bar,sha=baz,...
192 for ( s = dn; *s; s++ ) {
204 else if ( DNSEPARATOR( *s ) )
205 return( ch_strdup( s + 1 ) );
209 return( ch_strdup("") );
213 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
214 * and suffix must be normalized.
223 int dnlen, suffixlen;
229 suffixlen = strlen( suffix );
230 dnlen = strlen( dn );
232 if ( suffixlen > dnlen ) {
236 return( strcasecmp( dn + dnlen - suffixlen, suffix ) == 0 );
240 * dn_type - tells whether the given dn is an X.500 thing or DNS thing
241 * returns (defined in slap.h): DN_DNS dns-style thing
242 * DN_X500 x500-style thing
248 return( strchr( dn, '=' ) == NULL ? DN_DNS : DN_X500 );
252 dn_upcase( char *dn )
257 for ( s = dn; *s; s++ ) {