openldap/libraries/libldap/getdn.c
Kurt Zeilenga dc07e765f2 Vienna Bulk Commit
This commit includes many changes.  All changes compile under NT but
have not been tested under UNIX.

A Summary of changes (likely incomplete):

NT changes:
	Removed lint.
	Clean up configuration support for "Debug", "Release", "SDebug",
		and "SRelease" configurations.
	Share output directories for clients, libraries,
		and slapd.  (maybe they should be combined further
		and moved to build/{,S}{Debug,Release}).
	Enable threading when _MT is defined.
	Enable debuging when _DEBUG is defined.
	Disable setting of NDEBUG under Release/SRelease.  Asserts
		are disabled in <ac/assert.h> when LDAP_DEBUG is not
		defined.
	Added 'build/main.dsp' Master project.
	Removed non-slapd projects from slapd.dsp (see main.dsp).
	Removed replaced many uses of _WIN32 macro with feature based
		macros.

ldap_cdefs.h changes
	#define LDAP_CONST const
		(see below)
	#define LDAP_F(type) LDAP_F_PRE type LDAP_F_POST
		To allow specifiers to be added before and after
		the type declaration.  (For DLL handling)

LBER/LDAP changes
	Namespace changes:
		s/lber_/ber_/ for here and there.
		s/NAME_ERROR/LDAP_NAME_ERROR/g
	Deleted NULLMSG and other NULL* macros for namespace reasons.
	"const" libraries.  Installed headers (ie: lber.h, ldap.h)
		use LDAP_CONST macro.  Normally set to 'const' when
		__STDC__.  Can be set externally to enable/disable
		'constification' of external interface.  Internal
		interface always uses 'const'.  Did not fix warnings
		in -lldif (in lieu of new LDIF parser).

	Added _ext API implementations (excepting search and bind).
		Need to implement ldap_int_get_controls() for reponses
		with controls.

	Added numberous assert() checks.

LDAP_R
	_MT defines HAVE_NT_THREADS
	Added numberous assert() checks.
	Changed ldap_pthread_t back to unsigned long.  Used cast
	to HANDLE in _join().

LDBM
	Replaced _WIN32 with HAVE_SYSLOG

ud
	Added version string if MKVERSION is not defined.  (MKVERSION
		needs to be set under UNIX).

slapd
	Made connection sockbuf field a pointer to a sockbuf.  This
		removed slap.h dependency on lber-int.h.  lber-int.h now only
		included by those files needing to mess with the sockbuf.
	Used ber_* functions/macros to access sockbuf internals whenever
		possible.
	Added version string if MKVERSION is not defined.  (MKVERSION
		needs to be set under UNIX).
	Removed FD_SET unsigned lint

slapd/tools
	Used EXEEXT to added ".exe" to routines.  Need to define EXEEXT
		under UNIX.

ldappasswd
	Added ldappasswd.dsp.  Ported to NT.  Used getpid() to seed rand().

nt_debug
	Minor cleanup.  Added "portable.h" include and used <ac/*.h> where
	appropriate.  Added const to char* format argument.
1999-05-19 01:12:33 +00:00

285 lines
5.0 KiB
C

/*
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
/* Portions
* Copyright (c) 1994 Regents of the University of Michigan.
* All rights reserved.
*
* getdn.c
*/
#include "portable.h"
#include <stdio.h>
#include <stdlib.h>
#include <ac/ctype.h>
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>
#include "ldap-int.h"
static char **explode_name( LDAP_CONST char *name, int notypes, int is_dn );
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( LDAP_CONST 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 )
return( strdup( dn ) );
ufn = strdup( ++p );
#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 )
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( LDAP_CONST char *dn_in )
{
char *s;
char **rdns;
char *tok_r;
char *dn;
int ncomps;
int maxcomps = 8;
if ( (dn = strdup( dn_in )) == NULL ) {
return( NULL );
}
if ( (rdns = (char **) malloc( maxcomps * sizeof(char *) )) == NULL ) {
free( dn );
return( NULL );
}
ncomps = 0;
for ( s = ldap_pvt_strtok( dn, "@.", &tok_r ); s != NULL;
s = ldap_pvt_strtok( NULL, "@.", &tok_r ) )
{
if ( ncomps == maxcomps ) {
maxcomps *= 2;
if ( (rdns = (char **) realloc( rdns, maxcomps *
sizeof(char *) )) == NULL )
{
free( dn );
return NULL;
}
}
rdns[ncomps++] = strdup( s );
}
free(dn);
rdns[ncomps] = NULL;
/* trim rdns */
rdns = (char **) realloc( rdns, (ncomps+1) * sizeof(char*) );
return( rdns );
}
char **
ldap_explode_dn( LDAP_CONST 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 ) );
}
return explode_name( dn, notypes, 1 );
}
char **
ldap_explode_rdn( LDAP_CONST 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( LDAP_CONST char *name, int notypes, int is_dn )
{
const char *p, *q;
char **parts = NULL;
int state, count = 0, endquote, len;
p = name-1;
state = OUTQUOTE;
do {
++p;
switch ( *p ) {
case '\\':
if ( *++p == '\0' )
p--;
break;
case '"':
if ( state == INQUOTE )
state = OUTQUOTE;
else
state = INQUOTE;
break;
case '+':
if (!is_dn)
goto end_part;
break;
case ';':
case ',':
if (!is_dn)
break;
goto end_part;
case '\0':
end_part:
if ( state == OUTQUOTE ) {
++count;
if ( parts == NULL ) {
if (( parts = (char **)malloc( 8
* sizeof( char *))) == NULL )
return( NULL );
} else if ( count >= 8 ) {
if (( parts = (char **)realloc( parts,
(count+1) * sizeof( char *)))
== NULL )
return( NULL );
}
parts[ count ] = NULL;
endquote = 0;
if ( notypes ) {
for ( q = name;
q < p && *q != '='; ++q ) {
;
}
if ( q < p ) {
name = ++q;
}
if ( *name == '"' ) {
++name;
}
if ( *(p-1) == '"' ) {
endquote = 1;
--p;
}
}
len = p - name;
if (( parts[ count-1 ] = (char *)calloc( 1,
len + 1 )) != NULL ) {
SAFEMEMCPY( parts[ count-1 ], name,
len );
parts[ count-1 ][ len ] = '\0';
}
/*
* 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++;
name = *p ? p + 1 : p;
while ( isascii( *name ) && isspace( *name ) )
++name;
}
break;
}
} while ( *p );
return( parts );
}
int
ldap_is_dns_dn( LDAP_CONST char *dn )
{
return( dn[ 0 ] != '\0' && strchr( dn, '=' ) == NULL &&
strchr( dn, ',' ) == NULL );
}