]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/getdn.c
Add comments to UTF-8 declarations.
[openldap] / libraries / libldap / getdn.c
index 1142c883643f4e0959864d0dd516862f40bfae37..f03be3b455d15a1c7039cc41e20f0bb1882eca6f 100644 (file)
@@ -1,4 +1,9 @@
+/* $OpenLDAP$ */
 /*
+ * 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.
  *
@@ -7,12 +12,9 @@
 
 #include "portable.h"
 
-#ifndef lint 
-static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
-#endif
-
 #include <stdio.h>
-#include <stdlib.h>
+
+#include <ac/stdlib.h>
 
 #include <ac/ctype.h>
 #include <ac/socket.h>
@@ -21,7 +23,11 @@ static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of
 
 #include "ldap-int.h"
 
-static char **explode_name( char *name, int notypes, int is_dn );
+#define DN_TYPE_LDAP_RDN       0
+#define DN_TYPE_LDAP_DN                1
+#define DN_TYPE_DCE_DN         2
+
+static char **explode_name( const char *name, int notypes, int is_dn );
 
 char *
 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
@@ -37,7 +43,7 @@ ldap_get_dn( LDAP *ld, LDAPMessage *entry )
        }
 
        tmp = *entry->lm_ber;   /* struct copy */
-       if ( ber_scanf( &tmp, "{a", &dn ) == LBER_ERROR ) {
+       if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
                ld->ld_errno = LDAP_DECODING_ERROR;
                return( NULL );
        }
@@ -46,17 +52,25 @@ ldap_get_dn( LDAP *ld, LDAPMessage *entry )
 }
 
 char *
-ldap_dn2ufn( char *dn )
+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( ldap_strdup( dn ) );
+       if( dn == NULL ) {
+               return NULL;
+       }
+
+       if ( ldap_is_dns_dn( dn ) ||
+               ( p = strchr( dn, '=' ) ) == NULL )
+       {
+               return( LDAP_STRDUP( dn ) );
+       }
 
-       ufn = ldap_strdup( ++p );
+
+       ufn = LDAP_STRDUP( ++p );
 
 #define INQUOTE                1
 #define OUTQUOTE       2
@@ -92,8 +106,8 @@ ldap_dn2ufn( char *dn )
                                char    *rsave = r;
 
                                *r-- = '\0';
-                               while ( !isspace( *r ) && *r != ';'
-                                   && *r != ',' && r > ufn )
+                               while ( !isspace( (unsigned char) *r )
+                                       && *r != ';' && *r != ',' && r > ufn )
                                        r--;
                                r++;
 
@@ -119,55 +133,156 @@ ldap_dn2ufn( char *dn )
 }
 
 char **
-ldap_explode_dns( char *dn )
+ldap_explode_dns( LDAP_CONST char *dn_in )
 {
-       int     ncomps, maxcomps;
        char    *s;
        char    **rdns;
+       char    *tok_r;
+       char    *dn;
+
+       int ncomps;
+       int maxcomps = 8;
+
+       if ( (dn = LDAP_STRDUP( dn_in )) == NULL ) {
+               return( NULL );
+       }
 
-       if ( (rdns = (char **) malloc( 8 * sizeof(char *) )) == NULL ) {
+       if ( (rdns = (char **) LDAP_MALLOC( maxcomps * sizeof(char *) )) == NULL ) {
+               LDAP_FREE( dn );
                return( NULL );
        }
 
-       maxcomps = 8;
        ncomps = 0;
-       for ( s = strtok( dn, "@." ); s != NULL; s = strtok( NULL, "@." ) ) {
+       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 ) {
-                               return( NULL );
+                       if ( (rdns = (char **) LDAP_REALLOC( rdns, maxcomps *
+                           sizeof(char *) )) == NULL )
+                       {
+                               LDAP_FREE( dn );
+                               return NULL;
                        }
                }
-               rdns[ncomps++] = ldap_strdup( s );
+               rdns[ncomps++] = LDAP_STRDUP( s );
        }
+       LDAP_FREE(dn);
+
        rdns[ncomps] = NULL;
 
+       /* trim rdns */
+       rdns = (char **) LDAP_REALLOC( rdns, (ncomps+1) * sizeof(char*) );
        return( rdns );
 }
 
 char **
-ldap_explode_dn( char *dn, int notypes )
+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 );
+       return explode_name( dn, notypes, DN_TYPE_LDAP_DN );
 }
 
 char **
-ldap_explode_rdn( char *rdn, int notypes )
+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 );
+       return explode_name( rdn, notypes, DN_TYPE_LDAP_RDN );
+}
+
+char *
+ldap_dn2dcedn( LDAP_CONST char *dn )
+{
+       char *dce, *q, **rdns, **p;
+       int len = 0;
+
+       Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
+
+       rdns = explode_name( dn, 0, DN_TYPE_LDAP_DN );
+       if ( rdns == NULL ) {
+               return NULL;
+       }
+       
+       for ( p = rdns; *p != NULL; p++ ) {
+               len += strlen( *p ) + 1;
+       }
+
+       q = dce = LDAP_MALLOC( len + 1 );
+       if ( dce == NULL ) {
+               return NULL;
+       }
+
+       p--; /* get back past NULL */
+
+       for ( ; p != rdns; p-- ) {
+               strcpy( q, "/" );
+               q++;
+               strcpy( q, *p );
+               q += strlen( *p );
+       }
+
+       strcpy( q, "/" );
+       q++;
+       strcpy( q, *p );
+       
+       return dce;
+}
+
+char *
+ldap_dcedn2dn( LDAP_CONST char *dce )
+{
+       char *dn, *q, **rdns, **p;
+       int len;
+
+       Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
+
+       rdns = explode_name( dce, 0, DN_TYPE_DCE_DN );
+       if ( rdns == NULL ) {
+               return NULL;
+       }
+
+       len = 0;
+
+       for ( p = rdns; *p != NULL; p++ ) {
+               len += strlen( *p ) + 1;
+       }
+
+       q = dn = LDAP_MALLOC( len );
+       if ( dn == NULL ) {
+               return NULL;
+       }
+
+       p--;
+
+       for ( ; p != rdns; p-- ) {
+               strcpy( q, *p );
+               q += strlen( *p );
+               strcpy( q, "," );
+               q++;
+       }
+
+       if ( *dce == '/' ) {
+               /* the name was fully qualified, thus the most-significant
+                * RDN was empty. trash the last comma */
+               q--;
+               *q = '\0';
+       } else {
+               /* the name was relative. copy the most significant RDN */
+               strcpy( q, *p );
+       }
+
+       return dn;
 }
 
 static char **
-explode_name( char *name, int notypes, int is_dn )
+explode_name( const char *name, int notypes, int is_dn )
 {
-       char    *p, *q, **parts = NULL;
+       const char *p, *q;
+       char **parts = NULL;
        int     state, count = 0, endquote, len;
 
        p = name-1;
@@ -188,24 +303,28 @@ explode_name( char *name, int notypes, int is_dn )
                                state = INQUOTE;
                        break;
                case '+':
-                       if (!is_dn)
+                       if (is_dn == DN_TYPE_LDAP_RDN)
+                               goto end_part;
+                       break;
+               case '/':
+                       if (is_dn == DN_TYPE_DCE_DN)
                                goto end_part;
                        break;
                case ';':
                case ',':
-                       if (!is_dn)
-                               break;
-                       goto end_part;
+                       if (is_dn == DN_TYPE_LDAP_DN)
+                               goto end_part;
+                       break;
                case '\0':
                end_part:
                        if ( state == OUTQUOTE ) {
                                ++count;
                                if ( parts == NULL ) {
-                                       if (( parts = (char **)malloc( 8
+                                       if (( parts = (char **)LDAP_MALLOC( 8
                                                 * sizeof( char *))) == NULL )
                                                return( NULL );
                                } else if ( count >= 8 ) {
-                                       if (( parts = (char **)realloc( parts,
+                                       if (( parts = (char **)LDAP_REALLOC( parts,
                                                (count+1) * sizeof( char *)))
                                                == NULL )
                                                return( NULL );
@@ -231,7 +350,7 @@ explode_name( char *name, int notypes, int is_dn )
                                }
 
                                len = p - name;
-                               if (( parts[ count-1 ] = (char *)calloc( 1,
+                               if (( parts[ count-1 ] = (char *)LDAP_CALLOC( 1,
                                    len + 1 )) != NULL ) {
                                        SAFEMEMCPY( parts[ count-1 ], name,
                                            len );
@@ -259,9 +378,11 @@ explode_name( char *name, int notypes, int is_dn )
 
 
 int
-ldap_is_dns_dn( char *dn )
+ldap_is_dns_dn( LDAP_CONST char *dn )
 {
-       return( dn[ 0 ] != '\0' && strchr( dn, '=' ) == NULL &&
-           strchr( dn, ',' ) == NULL );
+       return( dn[ 0 ] != '\0'
+               && strchr( dn, '=' ) == NULL
+               && strchr( dn, ',' ) == NULL
+               && strchr( dn, ';' ) == NULL );
 }