Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions
getdn.c File Reference
#include "ldap-int.h"

Go to the source code of this file.

Defines

#define INQUOTE   1
#define OUTQUOTE   2
#define LDAP_DN   1
#define LDAP_RDN   2

Functions

char *LDAP_CALL ldap_get_dn (LDAP *ld, LDAPMessage *entry)
char *LDAP_CALL ldap_dn2ufn (const char *dn)
char **LDAP_CALL ldap_explode_dns (const char *dn)
static char ** ldap_explode (const char *dn, const int notypes, const int nametype)
char **LDAP_CALL ldap_explode_dn (const char *dn, const int notypes)
char **LDAP_CALL ldap_explode_rdn (const char *rdn, const int notypes)
int LDAP_CALL ldap_is_dns_dn (const char *dn)

Define Documentation

#define INQUOTE   1
#define LDAP_DN   1

Definition at line 202 of file getdn.c.

#define LDAP_RDN   2

Definition at line 203 of file getdn.c.

#define OUTQUOTE   2

Function Documentation

char* LDAP_CALL ldap_dn2ufn ( const char *  dn)

Definition at line 82 of file getdn.c.

{
       char   *p, *ufn, *r;
       size_t plen;
       int    state;

       LDAPDebug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );

       if ( dn == NULL ) {
              dn = "";
       }

       if ( ldap_is_dns_dn( dn ) || ( p = strchr( dn, '=' )) == NULL )
              return( nsldapi_strdup( (char *)dn ));

       ufn = nsldapi_strdup( ++p );

#define INQUOTE             1
#define OUTQUOTE     2
       state = OUTQUOTE;
       for ( p = ufn, r = ufn; *p; p += plen ) {
           plen = 1;
              switch ( *p ) {
              case '\\':
                     if ( *++p == '\0' )
                            plen=0;
                     else {
                            *r++ = '\\';
                            r += (plen = LDAP_UTF8COPY(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;
                            LDAP_UTF8DEC(r);
                            *rsave = '\0';
                            while ( !ldap_utf8isspace( r ) && *r != ';'
                                && *r != ',' && r > ufn )
                                   LDAP_UTF8DEC(r);
                            LDAP_UTF8INC(r);

                            if ( strcasecmp( r, "c" )
                                && strcasecmp( r, "o" )
                                && strcasecmp( r, "ou" )
                                && strcasecmp( r, "st" )
                                && strcasecmp( r, "l" )
                                && strcasecmp( r, "dc" )
                                && strcasecmp( r, "uid" )
                                && strcasecmp( r, "cn" ) ) {
                                   r = rsave;
                                   *r++ = '=';
                            }
                     }
                     break;
              default:
                     r += (plen = LDAP_UTF8COPY(r,p));
                     break;
              }
       }
       *r = '\0';

       return( ufn );
}

Here is the call graph for this function:

Here is the caller graph for this function:

static char** ldap_explode ( const char *  dn,
const int  notypes,
const int  nametype 
) [static]

Definition at line 206 of file getdn.c.

{
       char   *p, *q, *rdnstart, **rdns = NULL;
       size_t plen = 0;
       int    state, count = 0, endquote, len, goteq;

       LDAPDebug( LDAP_DEBUG_TRACE, "ldap_explode\n", 0, 0, 0 );

       if ( dn == NULL ) {
              dn = "";
       }

#if 0
       if ( ldap_is_dns_dn( dn ) ) {
              return( ldap_explode_dns( dn ) );
       }
#endif

       while ( ldap_utf8isspace( (char *)dn )) { /* ignore leading spaces */
              ++dn;
       }

       p = rdnstart = (char *) dn;
       state = OUTQUOTE;
       goteq = 0;

       do {
              p += plen;
              plen = 1;
              switch ( *p ) {
              case '\\':
                     if ( *++p == '\0' )
                            p--;
                     else
                            plen = LDAP_UTF8LEN(p);
                     break;
              case '"':
                     if ( state == INQUOTE )
                            state = OUTQUOTE;
                     else
                            state = INQUOTE;
                     break;
              case '+': if ( nametype != LDAP_RDN ) break;
              case ';':
              case ',':
              case '\0':
                     if ( state == OUTQUOTE ) {
                            /*
                             * semicolon and comma are not valid RDN
                             * separators.
                             */
                            if ( nametype == LDAP_RDN && 
                                   ( *p == ';' || *p == ',' || !goteq)) {
                                   ldap_charray_free( rdns );
                                   return NULL;
                            }
                            if ( (*p == ',' || *p == ';') && !goteq ) {
                                   /* If we get here, we have a case similar
                                * to <attr>=<value>,<string>,<attr>=<value>
                                * This is not a valid dn */
                                ldap_charray_free( rdns );
                                return NULL;
                            }
                            goteq = 0;
                            ++count;
                            if ( rdns == NULL ) {
                                   if (( rdns = (char **)NSLDAPI_MALLOC( 8
                                           * sizeof( char *))) == NULL )
                                          return( NULL );
                            } else if ( count >= 8 ) {
                                   if (( rdns = (char **)NSLDAPI_REALLOC(
                                       rdns, (count+1) *
                                       sizeof( char *))) == NULL )
                                          return( NULL );
                            }
                            rdns[ count ] = NULL;
                            endquote = 0;
                            if ( notypes ) {
                                   for ( q = rdnstart;
                                       q < p && *q != '='; ++q ) {
                                          ;
                                   }
                                   if ( q < p ) { /* *q == '=' */
                                          rdnstart = ++q;
                                   }
                                   if ( *rdnstart == '"' ) {
                                          ++rdnstart;
                                   }
                                   
                                   if ( *(p-1) == '"' ) {
                                          endquote = 1;
                                          --p;
                                   }
                            }

                            len = p - rdnstart;
                            if (( rdns[ count-1 ] = (char *)NSLDAPI_CALLOC(
                                1, len + 1 )) != NULL ) {
                                   SAFEMEMCPY( rdns[ count-1 ], rdnstart,
                                       len );
                                   if ( !endquote ) {
                                          /* trim trailing spaces */
                                          while ( len > 0 &&
                                              ldap_utf8isspace(
                                              &rdns[count-1][len-1] )) {
                                                 --len;
                                          }
                                   }
                                   rdns[ 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++;

                            rdnstart = *p ? p + 1 : p;
                            while ( ldap_utf8isspace( rdnstart ))
                                   ++rdnstart;
                     }
                     break;
              case '=':
                     if ( state == OUTQUOTE ) {
                            goteq = 1;
                     }
                     /* FALL */
              default:
                     plen = LDAP_UTF8LEN(p);
                     break;
              }
       } while ( *p );

       return( rdns );
}

Here is the call graph for this function:

Here is the caller graph for this function:

char** LDAP_CALL ldap_explode_dn ( const char *  dn,
const int  notypes 
)

Definition at line 346 of file getdn.c.

{
       return( ldap_explode( dn, notypes, LDAP_DN ) );
}

Here is the call graph for this function:

Here is the caller graph for this function:

char** LDAP_CALL ldap_explode_dns ( const char *  dn)

Definition at line 164 of file getdn.c.

{
       int    ncomps, maxcomps;
       char   *s, *cpydn;
       char   **rdns;
#ifdef HAVE_STRTOK_R /* defined in portable.h */
       char   *lasts;
#endif

       if ( dn == NULL ) {
              dn = "";
       }

       if ( (rdns = (char **)NSLDAPI_MALLOC( 8 * sizeof(char *) )) == NULL ) {
              return( NULL );
       }

       maxcomps = 8;
       ncomps = 0;
       cpydn = nsldapi_strdup( (char *)dn );
       for ( s = STRTOK( cpydn, "@.", &lasts ); s != NULL;
           s = STRTOK( NULL, "@.", &lasts ) ) {
              if ( ncomps == maxcomps ) {
                     maxcomps *= 2;
                     if ( (rdns = (char **)NSLDAPI_REALLOC( rdns, maxcomps *
                         sizeof(char *) )) == NULL ) {
                            NSLDAPI_FREE( cpydn );
                            return( NULL );
                     }
              }
              rdns[ncomps++] = nsldapi_strdup( s );
       }
       rdns[ncomps] = NULL;
       NSLDAPI_FREE( cpydn );

       return( rdns );
}

Here is the call graph for this function:

Here is the caller graph for this function:

char** LDAP_CALL ldap_explode_rdn ( const char *  rdn,
const int  notypes 
)

Definition at line 353 of file getdn.c.

{
       return( ldap_explode( rdn, notypes, LDAP_RDN ) );
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* LDAP_CALL ldap_get_dn ( LDAP *  ld,
LDAPMessage *  entry 
)

Definition at line 55 of file getdn.c.

{
       char                 *dn;
       struct berelement    tmp;

       LDAPDebug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );

       if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
              return( NULL );             /* punt */
       }

       if ( !NSLDAPI_VALID_LDAPMESSAGE_ENTRY_POINTER( entry )) {
              LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
              return( NULL );
       }

       tmp = *entry->lm_ber;       /* struct copy */
       if ( ber_scanf( &tmp, "{a", &dn ) == LBER_ERROR ) {
              LDAP_SET_LDERRNO( ld, LDAP_DECODING_ERROR, NULL, NULL );
              return( NULL );
       }

       return( dn );
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 360 of file getdn.c.

{
       return( dn != NULL && dn[ 0 ] != '\0' && strchr( dn, '=' ) == NULL &&
           strchr( dn, ',' ) == NULL );
}

Here is the call graph for this function:

Here is the caller graph for this function: