openldap/libraries/libldap/getdn.c

270 lines
4.7 KiB
C
Raw Normal View History

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
#include "ldap-int.h"
1998-08-09 08:43:13 +08:00
1998-11-05 07:28:51 +08:00
static char **explode_name( char *name, int notypes, int is_dn );
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 *
ldap_dn2ufn( char *dn )
{
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';
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 **
ldap_explode_dns( char *dn )
{
int ncomps, maxcomps;
char *s;
char **rdns;
char *tok_r;
1998-08-09 08:43:13 +08:00
if ( (rdns = (char **) malloc( 8 * sizeof(char *) )) == NULL ) {
return( NULL );
}
maxcomps = 8;
ncomps = 0;
1999-01-15 22:49:03 +08:00
for ( s = ldap_pvt_strtok( dn, "@.", &tok_r ); s != NULL;
s = ldap_pvt_strtok( NULL, "@.", &tok_r ) ) {
1998-08-09 08:43:13 +08:00
if ( ncomps == maxcomps ) {
maxcomps *= 2;
if ( (rdns = (char **) realloc( rdns, maxcomps *
sizeof(char *) )) == NULL ) {
return( NULL );
}
}
1999-01-15 22:49:03 +08:00
rdns[ncomps++] = strdup( s );
1998-08-09 08:43:13 +08:00
}
rdns[ncomps] = NULL;
return( rdns );
}
char **
ldap_explode_dn( char *dn, int notypes )
{
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 **
ldap_explode_rdn( char *rdn, int notypes )
{
Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
return explode_name( rdn, notypes, 0 );
}
static char **
explode_name( char *name, int notypes, int is_dn )
{
char *p, *q, **parts = NULL;
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 ) {
if (( parts = (char **)malloc( 8
1998-08-09 08:43:13 +08:00
* sizeof( char *))) == NULL )
return( NULL );
} else if ( count >= 8 ) {
1998-11-05 07:28:51 +08:00
if (( parts = (char **)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;
if (( parts[ count-1 ] = (char *)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
ldap_is_dns_dn( char *dn )
{
return( dn[ 0 ] != '\0' && strchr( dn, '=' ) == NULL &&
strchr( dn, ',' ) == NULL );
}