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