Home | History | Annotate | Line # | Download | only in libldap
getentry.c revision 1.1
      1  1.1  lukem /* $OpenLDAP: pkg/ldap/libraries/libldap/getentry.c,v 1.28.2.3 2008/02/11 23:26:41 kurt Exp $ */
      2  1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      3  1.1  lukem  *
      4  1.1  lukem  * Copyright 1998-2008 The OpenLDAP Foundation.
      5  1.1  lukem  * All rights reserved.
      6  1.1  lukem  *
      7  1.1  lukem  * Redistribution and use in source and binary forms, with or without
      8  1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
      9  1.1  lukem  * Public License.
     10  1.1  lukem  *
     11  1.1  lukem  * A copy of this license is available in the file LICENSE in the
     12  1.1  lukem  * top-level directory of the distribution or, alternatively, at
     13  1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     14  1.1  lukem  */
     15  1.1  lukem /* Portions Copyright (c) 1990 Regents of the University of Michigan.
     16  1.1  lukem  * All rights reserved.
     17  1.1  lukem  */
     18  1.1  lukem 
     19  1.1  lukem #include "portable.h"
     20  1.1  lukem 
     21  1.1  lukem #include <stdio.h>
     22  1.1  lukem #include <ac/stdlib.h>
     23  1.1  lukem 
     24  1.1  lukem #include <ac/socket.h>
     25  1.1  lukem #include <ac/string.h>
     26  1.1  lukem #include <ac/time.h>
     27  1.1  lukem 
     28  1.1  lukem #include "ldap-int.h"
     29  1.1  lukem 
     30  1.1  lukem /* ARGSUSED */
     31  1.1  lukem LDAPMessage *
     32  1.1  lukem ldap_first_entry( LDAP *ld, LDAPMessage *chain )
     33  1.1  lukem {
     34  1.1  lukem 	assert( ld != NULL );
     35  1.1  lukem 	assert( LDAP_VALID( ld ) );
     36  1.1  lukem 	assert( chain != NULL );
     37  1.1  lukem 
     38  1.1  lukem 	return chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY
     39  1.1  lukem 		? chain
     40  1.1  lukem 		: ldap_next_entry( ld, chain );
     41  1.1  lukem }
     42  1.1  lukem 
     43  1.1  lukem LDAPMessage *
     44  1.1  lukem ldap_next_entry( LDAP *ld, LDAPMessage *entry )
     45  1.1  lukem {
     46  1.1  lukem 	assert( ld != NULL );
     47  1.1  lukem 	assert( LDAP_VALID( ld ) );
     48  1.1  lukem 	assert( entry != NULL );
     49  1.1  lukem 
     50  1.1  lukem 	for(
     51  1.1  lukem 		entry = entry->lm_chain;
     52  1.1  lukem 		entry != NULL;
     53  1.1  lukem 		entry = entry->lm_chain )
     54  1.1  lukem 	{
     55  1.1  lukem 		if( entry->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
     56  1.1  lukem 			return( entry );
     57  1.1  lukem 		}
     58  1.1  lukem 	}
     59  1.1  lukem 
     60  1.1  lukem 	return( NULL );
     61  1.1  lukem }
     62  1.1  lukem 
     63  1.1  lukem int
     64  1.1  lukem ldap_count_entries( LDAP *ld, LDAPMessage *chain )
     65  1.1  lukem {
     66  1.1  lukem 	int	i;
     67  1.1  lukem 
     68  1.1  lukem 	assert( ld != NULL );
     69  1.1  lukem 	assert( LDAP_VALID( ld ) );
     70  1.1  lukem 
     71  1.1  lukem 	for ( i = 0; chain != NULL; chain = chain->lm_chain ) {
     72  1.1  lukem 		if( chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
     73  1.1  lukem 			i++;
     74  1.1  lukem 		}
     75  1.1  lukem 	}
     76  1.1  lukem 
     77  1.1  lukem 	return( i );
     78  1.1  lukem }
     79  1.1  lukem 
     80  1.1  lukem int
     81  1.1  lukem ldap_get_entry_controls(
     82  1.1  lukem 	LDAP *ld,
     83  1.1  lukem 	LDAPMessage *entry,
     84  1.1  lukem 	LDAPControl ***sctrls )
     85  1.1  lukem {
     86  1.1  lukem 	int rc;
     87  1.1  lukem 	BerElement be;
     88  1.1  lukem 
     89  1.1  lukem 	assert( ld != NULL );
     90  1.1  lukem 	assert( LDAP_VALID( ld ) );
     91  1.1  lukem 	assert( entry != NULL );
     92  1.1  lukem 	assert( sctrls != NULL );
     93  1.1  lukem 
     94  1.1  lukem 	if ( entry->lm_msgtype != LDAP_RES_SEARCH_ENTRY ) {
     95  1.1  lukem 		return LDAP_PARAM_ERROR;
     96  1.1  lukem 	}
     97  1.1  lukem 
     98  1.1  lukem 	/* make a local copy of the BerElement */
     99  1.1  lukem 	AC_MEMCPY(&be, entry->lm_ber, sizeof(be));
    100  1.1  lukem 
    101  1.1  lukem 	if ( ber_scanf( &be, "{xx" /*}*/ ) == LBER_ERROR ) {
    102  1.1  lukem 		rc = LDAP_DECODING_ERROR;
    103  1.1  lukem 		goto cleanup_and_return;
    104  1.1  lukem 	}
    105  1.1  lukem 
    106  1.1  lukem 	rc = ldap_pvt_get_controls( &be, sctrls );
    107  1.1  lukem 
    108  1.1  lukem cleanup_and_return:
    109  1.1  lukem 	if( rc != LDAP_SUCCESS ) {
    110  1.1  lukem 		ld->ld_errno = rc;
    111  1.1  lukem 
    112  1.1  lukem 		if( ld->ld_matched != NULL ) {
    113  1.1  lukem 			LDAP_FREE( ld->ld_matched );
    114  1.1  lukem 			ld->ld_matched = NULL;
    115  1.1  lukem 		}
    116  1.1  lukem 
    117  1.1  lukem 		if( ld->ld_error != NULL ) {
    118  1.1  lukem 			LDAP_FREE( ld->ld_error );
    119  1.1  lukem 			ld->ld_error = NULL;
    120  1.1  lukem 		}
    121  1.1  lukem 	}
    122  1.1  lukem 
    123  1.1  lukem 	return rc;
    124  1.1  lukem }
    125