2 * Copyright (c) 1994 Regents of the University of Michigan.
11 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
18 #include <ac/socket.h>
19 #include <ac/string.h>
24 static char **explode_name( char *name, int notypes, int is_dn );
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( strdup( dn ));
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++] = strdup( s );
150 ldap_explode_dn( char *dn, int notypes )
152 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
154 if ( ldap_is_dns_dn( dn ) ) {
155 return( ldap_explode_dns( dn ) );
157 return explode_name( dn, notypes, 1 );
161 ldap_explode_rdn( char *rdn, int notypes )
163 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
164 return explode_name( rdn, notypes, 0 );
168 explode_name( char *name, int notypes, int is_dn )
170 char *p, *q, **parts = NULL;
171 int state, count = 0, endquote, len;
185 if ( state == INQUOTE )
201 if ( state == OUTQUOTE ) {
203 if ( parts == NULL ) {
204 if (( parts = (char **)malloc( 8
205 * sizeof( char *))) == NULL )
207 } else if ( count >= 8 ) {
208 if (( parts = (char **)realloc( parts,
209 (count+1) * sizeof( char *)))
213 parts[ count ] = NULL;
217 q < p && *q != '='; ++q ) {
223 if ( *name == '"' ) {
227 if ( *(p-1) == '"' ) {
234 if (( parts[ count-1 ] = (char *)calloc( 1,
235 len + 1 )) != NULL ) {
236 SAFEMEMCPY( parts[ count-1 ], name,
238 parts[ count-1 ][ len ] = '\0';
242 * Don't forget to increment 'p' back to where
243 * it should be. If we don't, then we will
244 * never get past an "end quote."
249 name = *p ? p + 1 : p;
250 while ( isascii( *name ) && isspace( *name ) )
262 ldap_is_dns_dn( char *dn )
264 return( dn[ 0 ] != '\0' && strchr( dn, '=' ) == NULL &&
265 strchr( dn, ',' ) == NULL );