Back to index

openldap  2.4.31
getattr.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
00016  * All rights reserved.
00017  */
00018 
00019 #include "portable.h"
00020 
00021 #include <stdio.h>
00022 #include <ac/stdlib.h>
00023 
00024 #include <ac/socket.h>
00025 #include <ac/string.h>
00026 #include <ac/time.h>
00027 
00028 #include "ldap-int.h"
00029 
00030 char *
00031 ldap_first_attribute( LDAP *ld, LDAPMessage *entry, BerElement **berout )
00032 {
00033        int rc;
00034        ber_tag_t tag;
00035        ber_len_t len = 0;
00036        char *attr;
00037        BerElement *ber;
00038 
00039        Debug( LDAP_DEBUG_TRACE, "ldap_first_attribute\n", 0, 0, 0 );
00040 
00041        assert( ld != NULL );
00042        assert( LDAP_VALID( ld ) );
00043        assert( entry != NULL );
00044        assert( berout != NULL );
00045 
00046        *berout = NULL;
00047 
00048        ber = ldap_alloc_ber_with_options( ld );
00049        if( ber == NULL ) {
00050               return NULL;
00051        }
00052 
00053        *ber = *entry->lm_ber;
00054 
00055        /* 
00056         * Skip past the sequence, dn, sequence of sequence leaving
00057         * us at the first attribute.
00058         */
00059 
00060        tag = ber_scanf( ber, "{xl{" /*}}*/, &len );
00061        if( tag == LBER_ERROR ) {
00062               ld->ld_errno = LDAP_DECODING_ERROR;
00063               ber_free( ber, 0 );
00064               return NULL;
00065        }
00066 
00067        /* set the length to avoid overrun */
00068        rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
00069        if( rc != LBER_OPT_SUCCESS ) {
00070               ld->ld_errno = LDAP_LOCAL_ERROR;
00071               ber_free( ber, 0 );
00072               return NULL;
00073        }
00074 
00075        if ( ber_pvt_ber_remaining( ber ) == 0 ) {
00076               assert( len == 0 );
00077               ber_free( ber, 0 );
00078               return NULL;
00079        }
00080        assert( len != 0 );
00081 
00082        /* snatch the first attribute */
00083        tag = ber_scanf( ber, "{ax}", &attr );
00084        if( tag == LBER_ERROR ) {
00085               ld->ld_errno = LDAP_DECODING_ERROR;
00086               ber_free( ber, 0 );
00087               return NULL;
00088        }
00089 
00090        *berout = ber;
00091        return attr;
00092 }
00093 
00094 /* ARGSUSED */
00095 char *
00096 ldap_next_attribute( LDAP *ld, LDAPMessage *entry, BerElement *ber )
00097 {
00098        ber_tag_t tag;
00099        char *attr;
00100 
00101        Debug( LDAP_DEBUG_TRACE, "ldap_next_attribute\n", 0, 0, 0 );
00102 
00103        assert( ld != NULL );
00104        assert( LDAP_VALID( ld ) );
00105        assert( entry != NULL );
00106        assert( ber != NULL );
00107 
00108        if ( ber_pvt_ber_remaining( ber ) == 0 ) {
00109               return NULL;
00110        }
00111 
00112        /* skip sequence, snarf attribute type, skip values */
00113        tag = ber_scanf( ber, "{ax}", &attr ); 
00114        if( tag == LBER_ERROR ) {
00115               ld->ld_errno = LDAP_DECODING_ERROR;
00116               return NULL;
00117        }
00118 
00119        return attr;
00120 }
00121 
00122 /* Fetch attribute type and optionally fetch values. The type
00123  * and values are referenced in-place from the BerElement, they are
00124  * not dup'd into malloc'd memory.
00125  */
00126 /* ARGSUSED */
00127 int
00128 ldap_get_attribute_ber( LDAP *ld, LDAPMessage *entry, BerElement *ber,
00129        BerValue *attr, BerVarray *vals )
00130 {
00131        ber_tag_t tag;
00132        int rc = LDAP_SUCCESS;
00133 
00134        Debug( LDAP_DEBUG_TRACE, "ldap_get_attribute_ber\n", 0, 0, 0 );
00135 
00136        assert( ld != NULL );
00137        assert( LDAP_VALID( ld ) );
00138        assert( entry != NULL );
00139        assert( ber != NULL );
00140        assert( attr != NULL );
00141 
00142        attr->bv_val = NULL;
00143        attr->bv_len = 0;
00144 
00145        if ( ber_pvt_ber_remaining( ber ) ) {
00146               ber_len_t siz = sizeof( BerValue );
00147 
00148               /* skip sequence, snarf attribute type */
00149               tag = ber_scanf( ber, vals ? "{mM}" : "{mx}", attr, vals,
00150                      &siz, 0 ); 
00151               if( tag == LBER_ERROR ) {
00152                      rc = ld->ld_errno = LDAP_DECODING_ERROR;
00153               }
00154        }
00155 
00156        return rc;
00157 }