3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1994 Regents of the University of Michigan.
17 #include <ac/stdlib.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
26 static char **explode_name( const char *name, int notypes, int is_dn );
29 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
34 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
36 if ( entry == NULL ) {
37 ld->ld_errno = LDAP_PARAM_ERROR;
41 tmp = *entry->lm_ber; /* struct copy */
42 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
43 ld->ld_errno = LDAP_DECODING_ERROR;
51 ldap_dn2ufn( LDAP_CONST char *dn )
56 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
62 if ( ldap_is_dns_dn( dn ) ||
63 ( p = strchr( dn, '=' ) ) == NULL )
65 return( LDAP_STRDUP( dn ) );
69 ufn = LDAP_STRDUP( ++p );
74 for ( p = ufn, r = ufn; *p; p++ ) {
85 if ( state == INQUOTE )
93 if ( state == OUTQUOTE )
99 if ( state == INQUOTE )
105 while ( !isspace( (unsigned char) *r )
106 && *r != ';' && *r != ',' && r > ufn )
110 if ( strcasecmp( r, "c" )
111 && strcasecmp( r, "o" )
112 && strcasecmp( r, "ou" )
113 && strcasecmp( r, "st" )
114 && strcasecmp( r, "l" )
115 && strcasecmp( r, "cn" ) ) {
132 ldap_explode_dns( LDAP_CONST char *dn_in )
142 if ( (dn = LDAP_STRDUP( dn_in )) == NULL ) {
146 if ( (rdns = (char **) LDAP_MALLOC( maxcomps * sizeof(char *) )) == NULL ) {
152 for ( s = ldap_pvt_strtok( dn, "@.", &tok_r ); s != NULL;
153 s = ldap_pvt_strtok( NULL, "@.", &tok_r ) )
155 if ( ncomps == maxcomps ) {
157 if ( (rdns = (char **) LDAP_REALLOC( rdns, maxcomps *
158 sizeof(char *) )) == NULL )
164 rdns[ncomps++] = LDAP_STRDUP( s );
171 rdns = (char **) LDAP_REALLOC( rdns, (ncomps+1) * sizeof(char*) );
176 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
178 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
180 if ( ldap_is_dns_dn( dn ) ) {
181 return( ldap_explode_dns( dn ) );
183 return explode_name( dn, notypes, 1 );
187 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
189 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
190 return explode_name( rdn, notypes, 0 );
194 explode_name( const char *name, int notypes, int is_dn )
198 int state, count = 0, endquote, len;
212 if ( state == INQUOTE )
228 if ( state == OUTQUOTE ) {
230 if ( parts == NULL ) {
231 if (( parts = (char **)LDAP_MALLOC( 8
232 * sizeof( char *))) == NULL )
234 } else if ( count >= 8 ) {
235 if (( parts = (char **)LDAP_REALLOC( parts,
236 (count+1) * sizeof( char *)))
240 parts[ count ] = NULL;
244 q < p && *q != '='; ++q ) {
250 if ( *name == '"' ) {
254 if ( *(p-1) == '"' ) {
261 if (( parts[ count-1 ] = (char *)LDAP_CALLOC( 1,
262 len + 1 )) != NULL ) {
263 SAFEMEMCPY( parts[ count-1 ], name,
265 parts[ count-1 ][ len ] = '\0';
269 * Don't forget to increment 'p' back to where
270 * it should be. If we don't, then we will
271 * never get past an "end quote."
276 name = *p ? p + 1 : p;
277 while ( isascii( *name ) && isspace( *name ) )
289 ldap_is_dns_dn( LDAP_CONST char *dn )
291 return( dn[ 0 ] != '\0'
292 && strchr( dn, '=' ) == NULL
293 && strchr( dn, ',' ) == NULL
294 && strchr( dn, ';' ) == NULL );