1998-08-09 08:43:13 +08:00
|
|
|
/*
|
1998-12-29 04:53:15 +08:00
|
|
|
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
/* Portions
|
1998-08-09 08:43:13 +08:00
|
|
|
* Copyright (c) 1994 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* getdn.c
|
|
|
|
*/
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1998-08-21 03:42:38 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include <ac/ctype.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-26 09:18:41 +08:00
|
|
|
#include "ldap-int.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
static char **explode_name( LDAP_CONST char *name, int notypes, int is_dn );
|
1998-11-05 07:28:51 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
char *
|
|
|
|
ldap_get_dn( LDAP *ld, LDAPMessage *entry )
|
|
|
|
{
|
|
|
|
char *dn;
|
|
|
|
BerElement tmp;
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
|
|
|
|
|
|
|
|
if ( entry == NULL ) {
|
|
|
|
ld->ld_errno = LDAP_PARAM_ERROR;
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = *entry->lm_ber; /* struct copy */
|
|
|
|
if ( ber_scanf( &tmp, "{a", &dn ) == LBER_ERROR ) {
|
|
|
|
ld->ld_errno = LDAP_DECODING_ERROR;
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( dn );
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
1999-05-19 09:12:33 +08:00
|
|
|
ldap_dn2ufn( LDAP_CONST char *dn )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
char *p, *ufn, *r;
|
|
|
|
int state;
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
|
|
|
|
|
|
|
|
if ( ldap_is_dns_dn( dn ) || ( p = strchr( dn, '=' )) == NULL )
|
1999-01-15 22:49:03 +08:00
|
|
|
return( strdup( dn ) );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-01-15 22:49:03 +08:00
|
|
|
ufn = strdup( ++p );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#define INQUOTE 1
|
|
|
|
#define OUTQUOTE 2
|
|
|
|
state = OUTQUOTE;
|
|
|
|
for ( p = ufn, r = ufn; *p; p++ ) {
|
|
|
|
switch ( *p ) {
|
|
|
|
case '\\':
|
|
|
|
if ( *++p == '\0' )
|
|
|
|
p--;
|
|
|
|
else {
|
|
|
|
*r++ = '\\';
|
|
|
|
*r++ = *p;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
if ( state == INQUOTE )
|
|
|
|
state = OUTQUOTE;
|
|
|
|
else
|
|
|
|
state = INQUOTE;
|
|
|
|
*r++ = *p;
|
|
|
|
break;
|
|
|
|
case ';':
|
|
|
|
case ',':
|
|
|
|
if ( state == OUTQUOTE )
|
|
|
|
*r++ = ',';
|
|
|
|
else
|
|
|
|
*r++ = *p;
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
if ( state == INQUOTE )
|
|
|
|
*r++ = *p;
|
|
|
|
else {
|
|
|
|
char *rsave = r;
|
|
|
|
|
|
|
|
*r-- = '\0';
|
1999-02-23 01:57:22 +08:00
|
|
|
while ( !isspace( (unsigned char) *r )
|
|
|
|
&& *r != ';' && *r != ',' && r > ufn )
|
1998-08-09 08:43:13 +08:00
|
|
|
r--;
|
|
|
|
r++;
|
|
|
|
|
|
|
|
if ( strcasecmp( r, "c" )
|
|
|
|
&& strcasecmp( r, "o" )
|
|
|
|
&& strcasecmp( r, "ou" )
|
|
|
|
&& strcasecmp( r, "st" )
|
|
|
|
&& strcasecmp( r, "l" )
|
|
|
|
&& strcasecmp( r, "cn" ) ) {
|
|
|
|
r = rsave;
|
|
|
|
*r++ = '=';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*r++ = *p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*r = '\0';
|
|
|
|
|
|
|
|
return( ufn );
|
|
|
|
}
|
|
|
|
|
|
|
|
char **
|
1999-05-19 09:12:33 +08:00
|
|
|
ldap_explode_dns( LDAP_CONST char *dn_in )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
char **rdns;
|
1998-12-19 09:27:20 +08:00
|
|
|
char *tok_r;
|
1999-05-19 09:12:33 +08:00
|
|
|
char *dn;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
int ncomps;
|
|
|
|
int maxcomps = 8;
|
|
|
|
|
|
|
|
if ( (dn = strdup( dn_in )) == NULL ) {
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
if ( (rdns = (char **) LDAP_MALLOC( maxcomps * sizeof(char *) )) == NULL ) {
|
|
|
|
LDAP_FREE( dn );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
ncomps = 0;
|
1999-01-15 22:49:03 +08:00
|
|
|
for ( s = ldap_pvt_strtok( dn, "@.", &tok_r ); s != NULL;
|
1999-05-19 09:12:33 +08:00
|
|
|
s = ldap_pvt_strtok( NULL, "@.", &tok_r ) )
|
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( ncomps == maxcomps ) {
|
|
|
|
maxcomps *= 2;
|
1999-05-29 09:19:14 +08:00
|
|
|
if ( (rdns = (char **) LDAP_REALLOC( rdns, maxcomps *
|
1999-05-19 09:12:33 +08:00
|
|
|
sizeof(char *) )) == NULL )
|
|
|
|
{
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( dn );
|
1999-05-19 09:12:33 +08:00
|
|
|
return NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
1999-01-15 22:49:03 +08:00
|
|
|
rdns[ncomps++] = strdup( s );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE(dn);
|
1999-05-19 09:12:33 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
rdns[ncomps] = NULL;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
/* trim rdns */
|
1999-05-29 09:19:14 +08:00
|
|
|
rdns = (char **) LDAP_REALLOC( rdns, (ncomps+1) * sizeof(char*) );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( rdns );
|
|
|
|
}
|
|
|
|
|
|
|
|
char **
|
1999-05-19 09:12:33 +08:00
|
|
|
ldap_explode_dn( LDAP_CONST char *dn, int notypes )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
|
|
|
|
|
|
|
|
if ( ldap_is_dns_dn( dn ) ) {
|
|
|
|
return( ldap_explode_dns( dn ) );
|
|
|
|
}
|
1998-11-05 07:28:51 +08:00
|
|
|
return explode_name( dn, notypes, 1 );
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-11-05 07:28:51 +08:00
|
|
|
char **
|
1999-05-19 09:12:33 +08:00
|
|
|
ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
|
1998-11-05 07:28:51 +08:00
|
|
|
{
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
|
|
|
|
return explode_name( rdn, notypes, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static char **
|
1999-05-19 09:12:33 +08:00
|
|
|
explode_name( LDAP_CONST char *name, int notypes, int is_dn )
|
1998-11-05 07:28:51 +08:00
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
const char *p, *q;
|
|
|
|
char **parts = NULL;
|
1998-11-05 07:28:51 +08:00
|
|
|
int state, count = 0, endquote, len;
|
|
|
|
|
|
|
|
p = name-1;
|
1998-08-09 08:43:13 +08:00
|
|
|
state = OUTQUOTE;
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
|
|
++p;
|
|
|
|
switch ( *p ) {
|
|
|
|
case '\\':
|
|
|
|
if ( *++p == '\0' )
|
|
|
|
p--;
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
if ( state == INQUOTE )
|
|
|
|
state = OUTQUOTE;
|
|
|
|
else
|
|
|
|
state = INQUOTE;
|
|
|
|
break;
|
1998-11-05 07:28:51 +08:00
|
|
|
case '+':
|
|
|
|
if (!is_dn)
|
|
|
|
goto end_part;
|
|
|
|
break;
|
1998-08-09 08:43:13 +08:00
|
|
|
case ';':
|
|
|
|
case ',':
|
1998-11-05 07:28:51 +08:00
|
|
|
if (!is_dn)
|
|
|
|
break;
|
|
|
|
goto end_part;
|
1998-08-09 08:43:13 +08:00
|
|
|
case '\0':
|
1998-11-05 07:28:51 +08:00
|
|
|
end_part:
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( state == OUTQUOTE ) {
|
|
|
|
++count;
|
1998-11-05 07:28:51 +08:00
|
|
|
if ( parts == NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
if (( parts = (char **)LDAP_MALLOC( 8
|
1998-08-09 08:43:13 +08:00
|
|
|
* sizeof( char *))) == NULL )
|
|
|
|
return( NULL );
|
|
|
|
} else if ( count >= 8 ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
if (( parts = (char **)LDAP_REALLOC( parts,
|
1998-08-09 08:43:13 +08:00
|
|
|
(count+1) * sizeof( char *)))
|
|
|
|
== NULL )
|
|
|
|
return( NULL );
|
|
|
|
}
|
1998-11-05 07:28:51 +08:00
|
|
|
parts[ count ] = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
endquote = 0;
|
|
|
|
if ( notypes ) {
|
1998-11-05 07:28:51 +08:00
|
|
|
for ( q = name;
|
1998-08-09 08:43:13 +08:00
|
|
|
q < p && *q != '='; ++q ) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
if ( q < p ) {
|
1998-11-05 07:28:51 +08:00
|
|
|
name = ++q;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1998-11-05 07:28:51 +08:00
|
|
|
if ( *name == '"' ) {
|
|
|
|
++name;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( *(p-1) == '"' ) {
|
|
|
|
endquote = 1;
|
|
|
|
--p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-11-05 07:28:51 +08:00
|
|
|
len = p - name;
|
1999-05-29 09:19:14 +08:00
|
|
|
if (( parts[ count-1 ] = (char *)LDAP_CALLOC( 1,
|
1998-08-09 08:43:13 +08:00
|
|
|
len + 1 )) != NULL ) {
|
1998-11-05 07:28:51 +08:00
|
|
|
SAFEMEMCPY( parts[ count-1 ], name,
|
1998-08-09 08:43:13 +08:00
|
|
|
len );
|
1998-11-05 07:28:51 +08:00
|
|
|
parts[ count-1 ][ len ] = '\0';
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't forget to increment 'p' back to where
|
|
|
|
* it should be. If we don't, then we will
|
|
|
|
* never get past an "end quote."
|
|
|
|
*/
|
|
|
|
if ( endquote == 1 )
|
|
|
|
p++;
|
|
|
|
|
1998-11-05 07:28:51 +08:00
|
|
|
name = *p ? p + 1 : p;
|
|
|
|
while ( isascii( *name ) && isspace( *name ) )
|
|
|
|
++name;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while ( *p );
|
|
|
|
|
1998-11-05 07:28:51 +08:00
|
|
|
return( parts );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1999-05-19 09:12:33 +08:00
|
|
|
ldap_is_dns_dn( LDAP_CONST char *dn )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
return( dn[ 0 ] != '\0' && strchr( dn, '=' ) == NULL &&
|
|
|
|
strchr( dn, ',' ) == NULL );
|
|
|
|
}
|
|
|
|
|