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