No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / openldap / dist / libraries / libldap / references.c
blobe1c3a6f55d0008dae8daf3f59df7ef8b43bdc6a9
1 /* references.c */
2 /* $OpenLDAP: pkg/ldap/libraries/libldap/references.c,v 1.24.2.3 2008/02/11 23:26:41 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 #include "portable.h"
19 #include <stdio.h>
21 #include <ac/stdlib.h>
23 #include <ac/socket.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
27 #include "ldap-int.h"
29 LDAPMessage *
30 ldap_first_reference( LDAP *ld, LDAPMessage *chain )
32 assert( ld != NULL );
33 assert( LDAP_VALID( ld ) );
34 assert( chain != NULL );
36 return chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE
37 ? chain
38 : ldap_next_reference( ld, chain );
41 LDAPMessage *
42 ldap_next_reference( LDAP *ld, LDAPMessage *ref )
44 assert( ld != NULL );
45 assert( LDAP_VALID( ld ) );
46 assert( ref != NULL );
48 for (
49 ref = ref->lm_chain;
50 ref != NULL;
51 ref = ref->lm_chain )
53 if( ref->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ) {
54 return( ref );
58 return( NULL );
61 int
62 ldap_count_references( LDAP *ld, LDAPMessage *chain )
64 int i;
66 assert( ld != NULL );
67 assert( LDAP_VALID( ld ) );
69 for ( i = 0; chain != NULL; chain = chain->lm_chain ) {
70 if( chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ) {
71 i++;
75 return( i );
78 int
79 ldap_parse_reference(
80 LDAP *ld,
81 LDAPMessage *ref,
82 char ***referralsp,
83 LDAPControl ***serverctrls,
84 int freeit)
86 BerElement be;
87 char **refs = NULL;
88 int rc;
90 assert( ld != NULL );
91 assert( LDAP_VALID( ld ) );
92 assert( ref != NULL );
94 if( ref->lm_msgtype != LDAP_RES_SEARCH_REFERENCE ) {
95 return LDAP_PARAM_ERROR;
98 /* make a private copy of BerElement */
99 AC_MEMCPY(&be, ref->lm_ber, sizeof(be));
101 if ( ber_scanf( &be, "{v" /*}*/, &refs ) == LBER_ERROR ) {
102 rc = LDAP_DECODING_ERROR;
103 goto free_and_return;
106 if ( serverctrls == NULL ) {
107 rc = LDAP_SUCCESS;
108 goto free_and_return;
111 if ( ber_scanf( &be, /*{*/ "}" ) == LBER_ERROR ) {
112 rc = LDAP_DECODING_ERROR;
113 goto free_and_return;
116 rc = ldap_pvt_get_controls( &be, serverctrls );
118 free_and_return:
120 if( referralsp != NULL ) {
121 /* provide references regradless of return code */
122 *referralsp = refs;
124 } else {
125 LDAP_VFREE( refs );
128 if( freeit ) {
129 ldap_msgfree( ref );
132 if( rc != LDAP_SUCCESS ) {
133 ld->ld_errno = rc;
135 if( ld->ld_matched != NULL ) {
136 LDAP_FREE( ld->ld_matched );
137 ld->ld_matched = NULL;
140 if( ld->ld_error != NULL ) {
141 LDAP_FREE( ld->ld_error );
142 ld->ld_error = NULL;
146 return rc;