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