]> git.sur5r.net Git - openldap/blob - libraries/libldap/getentry.c
Fix prev commit
[openldap] / libraries / libldap / getentry.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1990 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  getentry.c
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16 #include <ac/stdlib.h>
17
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/time.h>
21
22 #include "ldap-int.h"
23
24 /* ARGSUSED */
25 LDAPMessage *
26 ldap_first_entry( LDAP *ld, LDAPMessage *chain )
27 {
28         assert( ld != NULL );
29         assert( LDAP_VALID( ld ) );
30         assert( chain != NULL );
31
32         return chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY
33                 ? chain
34                 : ldap_next_entry( ld, chain );
35 }
36
37 LDAPMessage *
38 ldap_next_entry( LDAP *ld, LDAPMessage *entry )
39 {
40         assert( ld != NULL );
41         assert( LDAP_VALID( ld ) );
42         assert( entry != NULL );
43
44         for(
45                 entry = entry->lm_chain;
46                 entry != NULL;
47                 entry = entry->lm_chain )
48         {
49                 if( entry->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
50                         return( entry );
51                 }
52         }
53
54         return( NULL );
55 }
56
57 int
58 ldap_count_entries( LDAP *ld, LDAPMessage *chain )
59 {
60         int     i;
61
62         assert( ld != NULL );
63         assert( LDAP_VALID( ld ) );
64
65         for ( i = 0; chain != NULL; chain = chain->lm_chain ) {
66                 if( chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
67                         i++;
68                 }
69         }
70
71         return( i );
72 }
73
74 int
75 ldap_get_entry_controls(
76         LDAP *ld,
77         LDAPMessage *entry, 
78         LDAPControl ***sctrls )
79 {
80         int rc;
81         BerElement be;
82
83         assert( ld != NULL );
84         assert( LDAP_VALID( ld ) );
85         assert( entry != NULL );
86         assert( sctrls != NULL );
87
88         if ( entry->lm_msgtype != LDAP_RES_SEARCH_ENTRY ) {
89                 return LDAP_PARAM_ERROR;
90         }
91
92         /* make a local copy of the BerElement */
93         AC_MEMCPY(&be, entry->lm_ber, sizeof(be));
94
95         if ( ber_scanf( &be, "{xx" /*}*/ ) == LBER_ERROR ) {
96                 rc = LDAP_DECODING_ERROR;
97                 goto cleanup_and_return;
98         }
99
100         rc = ldap_int_get_controls( &be, sctrls );
101
102 cleanup_and_return:
103         if( rc != LDAP_SUCCESS ) {
104                 ld->ld_errno = rc;
105
106                 if( ld->ld_matched != NULL ) {
107                         LDAP_FREE( ld->ld_matched );
108                         ld->ld_matched = NULL;
109                 }
110
111                 if( ld->ld_error != NULL ) {
112                         LDAP_FREE( ld->ld_error );
113                         ld->ld_error = NULL;
114                 }
115         }
116
117         return rc;
118 }