Back to index

openldap  2.4.31
getentry.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 /* ARGSUSED */
00031 LDAPMessage *
00032 ldap_first_entry( LDAP *ld, LDAPMessage *chain )
00033 {
00034        assert( ld != NULL );
00035        assert( LDAP_VALID( ld ) );
00036        assert( chain != NULL );
00037 
00038        return chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY
00039               ? chain
00040               : ldap_next_entry( ld, chain );
00041 }
00042 
00043 LDAPMessage *
00044 ldap_next_entry( LDAP *ld, LDAPMessage *entry )
00045 {
00046        assert( ld != NULL );
00047        assert( LDAP_VALID( ld ) );
00048        assert( entry != NULL );
00049 
00050        for(
00051               entry = entry->lm_chain;
00052               entry != NULL;
00053               entry = entry->lm_chain )
00054        {
00055               if( entry->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
00056                      return( entry );
00057               }
00058        }
00059 
00060        return( NULL );
00061 }
00062 
00063 int
00064 ldap_count_entries( LDAP *ld, LDAPMessage *chain )
00065 {
00066        int    i;
00067 
00068        assert( ld != NULL );
00069        assert( LDAP_VALID( ld ) );
00070 
00071        for ( i = 0; chain != NULL; chain = chain->lm_chain ) {
00072               if( chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
00073                      i++;
00074               }
00075        }
00076 
00077        return( i );
00078 }
00079 
00080 int
00081 ldap_get_entry_controls(
00082        LDAP *ld,
00083        LDAPMessage *entry, 
00084        LDAPControl ***sctrls )
00085 {
00086        int rc;
00087        BerElement be;
00088 
00089        assert( ld != NULL );
00090        assert( LDAP_VALID( ld ) );
00091        assert( entry != NULL );
00092        assert( sctrls != NULL );
00093 
00094        if ( entry->lm_msgtype != LDAP_RES_SEARCH_ENTRY ) {
00095               return LDAP_PARAM_ERROR;
00096        }
00097 
00098        /* make a local copy of the BerElement */
00099        AC_MEMCPY(&be, entry->lm_ber, sizeof(be));
00100 
00101        if ( ber_scanf( &be, "{xx" /*}*/ ) == LBER_ERROR ) {
00102               rc = LDAP_DECODING_ERROR;
00103               goto cleanup_and_return;
00104        }
00105 
00106        rc = ldap_pvt_get_controls( &be, sctrls );
00107 
00108 cleanup_and_return:
00109        if( rc != LDAP_SUCCESS ) {
00110               ld->ld_errno = rc;
00111 
00112               if( ld->ld_matched != NULL ) {
00113                      LDAP_FREE( ld->ld_matched );
00114                      ld->ld_matched = NULL;
00115               }
00116 
00117               if( ld->ld_error != NULL ) {
00118                      LDAP_FREE( ld->ld_error );
00119                      ld->ld_error = NULL;
00120               }
00121        }
00122 
00123        return rc;
00124 }