2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1994 Regents of the University of Michigan.
18 #include <ac/socket.h>
19 #include <ac/string.h>
27 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
32 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
34 if ( entry == NULL ) {
35 ld->ld_errno = LDAP_PARAM_ERROR;
39 tmp = *entry->lm_ber; /* struct copy */
40 if ( ber_scanf( &tmp, "{a", &dn ) == LBER_ERROR ) {
41 ld->ld_errno = LDAP_DECODING_ERROR;
49 ldap_dn2ufn( char *dn )
54 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
56 if ( ldap_is_dns_dn( dn ) || ( p = strchr( dn, '=' )) == NULL )
57 return( ldap_strdup( dn ) );
59 ufn = ldap_strdup( ++p );
64 for ( p = ufn, r = ufn; *p; p++ ) {
75 if ( state == INQUOTE )
83 if ( state == OUTQUOTE )
89 if ( state == INQUOTE )
95 while ( !isspace( *r ) && *r != ';'
96 && *r != ',' && r > ufn )
100 if ( strcasecmp( r, "c" )
101 && strcasecmp( r, "o" )
102 && strcasecmp( r, "ou" )
103 && strcasecmp( r, "st" )
104 && strcasecmp( r, "l" )
105 && strcasecmp( r, "cn" ) ) {
122 ldap_explode_dns( char *dn )
124 int ncomps, maxcomps;
128 if ( (rdns = (char **) malloc( 8 * sizeof(char *) )) == NULL ) {
134 for ( s = strtok( dn, "@." ); s != NULL; s = strtok( NULL, "@." ) ) {
135 if ( ncomps == maxcomps ) {
137 if ( (rdns = (char **) realloc( rdns, maxcomps *
138 sizeof(char *) )) == NULL ) {
142 rdns[ncomps++] = ldap_strdup( s );
150 ldap_explode_dn( char *dn, int notypes )
152 char *p, *q, *rdnstart, **rdns = NULL;
153 int state, count = 0, endquote, len;
155 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
157 if ( ldap_is_dns_dn( dn ) ) {
158 return( ldap_explode_dns( dn ) );
174 if ( state == INQUOTE )
182 if ( state == OUTQUOTE ) {
184 if ( rdns == NULL ) {
185 if (( rdns = (char **)malloc( 8
186 * sizeof( char *))) == NULL )
188 } else if ( count >= 8 ) {
189 if (( rdns = (char **)realloc( rdns,
190 (count+1) * sizeof( char *)))
194 rdns[ count ] = NULL;
198 q < p && *q != '='; ++q ) {
204 if ( *rdnstart == '"' ) {
208 if ( *(p-1) == '"' ) {
215 if (( rdns[ count-1 ] = (char *)calloc( 1,
216 len + 1 )) != NULL ) {
217 SAFEMEMCPY( rdns[ count-1 ], rdnstart,
219 rdns[ count-1 ][ len ] = '\0';
223 * Don't forget to increment 'p' back to where
224 * it should be. If we don't, then we will
225 * never get past an "end quote."
230 rdnstart = *p ? p + 1 : p;
231 while ( isspace( *rdnstart ))
243 ldap_is_dns_dn( char *dn )
245 return( dn[ 0 ] != '\0' && strchr( dn, '=' ) == NULL &&
246 strchr( dn, ',' ) == NULL );