1 /* dn.c - routines for dealing with distinguished names */
10 #include <sys/socket.h>
13 static char **dn_explode();
15 #define DNSEPARATOR(c) (c == ',' || c == ';')
16 #define SEPARATOR(c) (c == ',' || c == ';' || c == '+')
17 #define SPACE(c) (c == ' ' || c == '\n')
18 #define NEEDSESCAPE(c) (c == '\\' || c == '"')
24 #define INQUOTEDVALUE 5
28 * dn_normalize - put dn into a canonical format. the dn is
29 * normalized in place, as well as returned.
33 dn_normalize( char *dn )
38 /* Debug( LDAP_DEBUG_TRACE, "=> dn_normalize \"%s\"\n", dn, 0, 0 ); */
42 for ( d = s = dn; *s; s++ ) {
45 if ( ! SPACE( *s ) ) {
54 } else if ( SPACE( *s ) ) {
64 } else if ( ! SPACE( *s ) ) {
65 /* not a valid dn - but what can we do here? */
71 state = INQUOTEDVALUE;
73 } else if ( ! SPACE( *s ) ) {
79 if ( !gotesc && SEPARATOR( *s ) ) {
80 while ( SPACE( *(d - 1) ) )
88 } else if ( gotesc && !NEEDSESCAPE( *s ) &&
97 if ( !gotesc && *s == '"' ) {
100 } else if ( gotesc && !NEEDSESCAPE( *s ) ) {
108 if ( SEPARATOR( *s ) ) {
114 Debug( LDAP_DEBUG_ANY,
115 "dn_normalize - unknown state %d\n", state, 0, 0 );
126 /* Debug( LDAP_DEBUG_TRACE, "<= dn_normalize \"%s\"\n", dn, 0, 0 ); */
131 * dn_normalize_case - put dn into a canonical form suitable for storing
132 * in a hash database. this involves normalizing the case as well as
133 * the format. the dn is normalized in place as well as returned.
137 dn_normalize_case( char *dn )
141 /* normalize format */
145 for ( s = dn; *s; s++ ) {
153 * dn_parent - return a copy of the dn of dn's parent
165 if ( dn == NULL || *dn == '\0' || be_issuffix( be, dn ) ) {
170 * no =, assume it is a dns name, like blah@some.domain.name
171 * if the blah@ part is there, return some.domain.name. if
172 * it's just some.domain.name, return domain.name.
174 if ( strchr( dn, '=' ) == NULL ) {
175 if ( (s = strchr( dn, '@' )) == NULL ) {
176 if ( (s = strchr( dn, '.' )) == NULL ) {
180 if ( *(s + 1) == '\0' ) {
183 return( strdup( s + 1 ) );
188 * else assume it is an X.500-style name, which looks like
189 * foo=bar,sha=baz,...
193 for ( s = dn; *s; s++ ) {
205 else if ( DNSEPARATOR( *s ) )
206 return( strdup( s + 1 ) );
210 return( strdup("") );
214 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
215 * and suffix must be normalized.
224 int dnlen, suffixlen;
230 suffixlen = strlen( suffix );
231 dnlen = strlen( dn );
233 if ( suffixlen > dnlen ) {
237 return( strcasecmp( dn + dnlen - suffixlen, suffix ) == 0 );
241 * dn_type - tells whether the given dn is an X.500 thing or DNS thing
242 * returns (defined in slap.h): DN_DNS dns-style thing
243 * DN_X500 x500-style thing
249 return( strchr( dn, '=' ) == NULL ? DN_DNS : DN_X500 );
253 dn_upcase( char *dn )
258 for ( s = dn; *s; s++ ) {