]> git.sur5r.net Git - openldap/blob - libraries/libldap/references.c
176a72c9ec4628ada2c588114c5fc3e98feb0d0a
[openldap] / libraries / libldap / references.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*
6  *  references.c
7  */
8
9 #include "portable.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include <ac/ctype.h>
15 #include <ac/socket.h>
16 #include <ac/string.h>
17 #include <ac/time.h>
18
19 #include "ldap-int.h"
20
21 /* ARGSUSED */
22 LDAPMessage *
23 ldap_first_reference( LDAP *ld, LDAPMessage *chain )
24 {
25         if ( ld == NULL || chain == NULLMSG ) {
26                 return NULLMSG;
27         }
28
29         return chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE
30                 ? chain
31                 : ldap_next_reference( ld, chain );
32 }
33
34 /* ARGSUSED */
35 LDAPMessage *
36 ldap_next_reference( LDAP *ld, LDAPMessage *ref )
37 {
38         if ( ld == NULL || ref == NULLMSG ) {
39                 return NULLMSG;
40         }
41
42         for (
43                 ref = ref->lm_chain;
44                 ref != NULLMSG;
45                 ref = ref->lm_chain )
46         {
47                 if( ref->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ) {
48                         return( ref );
49                 }
50         }
51
52         return( NULLMSG );
53 }
54
55 /* ARGSUSED */
56 int
57 ldap_count_references( LDAP *ld, LDAPMessage *chain )
58 {
59         int     i;
60
61         if ( ld == NULL ) {
62                 return -1;
63         }
64
65         for ( i = 0; chain != NULL; chain = chain->lm_chain ) {
66                 if( chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ) {
67                         i++;
68                 }
69         }
70
71         return( i );
72 }
73
74 int
75 ldap_parse_reference( 
76         LDAP            *ld,    
77         LDAPMessage     *ref,
78         char            ***referralsp,
79         LDAPControl     ***serverctrls,
80         int             freeit)
81 {
82         BerElement be;
83         char **refs = NULL;
84         int rc;
85
86         if( ld == NULL || ref == NULL ||
87                 ref->lm_msgtype != LDAP_RES_SEARCH_REFERENCE )
88         {
89                 return LDAP_PARAM_ERROR;
90         }
91
92         /* make a private copy of BerElement */
93         SAFEMEMCPY(&be, ref->lm_ber, sizeof(be));
94         
95         if ( ber_scanf( &be, "{v" /*}*/, &refs ) == LBER_ERROR ) {
96                 rc = LDAP_DECODING_ERROR;
97                 goto free_and_return;
98         }
99
100         if ( serverctrls == NULL ) {
101                 rc = LDAP_SUCCESS;
102                 goto free_and_return;
103         }
104
105         if ( ber_scanf( &be, /*{*/ "}" ) == LBER_ERROR ) {
106                 rc = LDAP_DECODING_ERROR;
107                 goto free_and_return;
108         }
109
110         rc = ldap_int_get_controls( &be, serverctrls );
111
112 free_and_return:
113
114         if( referralsp != NULL ) {
115                 /* provide references regradless of return code */
116                 *referralsp = refs;
117
118         } else {
119                 ldap_value_free( refs );
120         }
121
122         if( freeit ) {
123                 ldap_msgfree( ref );
124         }
125
126         if( rc != LDAP_SUCCESS ) {
127                 ld->ld_errno = rc;
128
129                 if( ld->ld_matched != NULL )
130                         free( ld->ld_matched );
131
132                 ld->ld_matched = NULL;
133
134                 if( ld->ld_error != NULL )
135                         free( ld->ld_error );
136
137                 ld->ld_error = NULL;
138         }
139
140         return rc;
141 }