1 /*	$NetBSD: getentry.c,v 1.3 2021/08/14 16:14:56 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2021 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 <sys/cdefs.h>
22 __RCSID("$NetBSD: getentry.c,v 1.3 2021/08/14 16:14:56 christos Exp $");
23 
24 #include "portable.h"
25 
26 #include <stdio.h>
27 #include <ac/stdlib.h>
28 
29 #include <ac/socket.h>
30 #include <ac/string.h>
31 #include <ac/time.h>
32 
33 #include "ldap-int.h"
34 
35 /* ARGSUSED */
36 LDAPMessage *
ldap_first_entry(LDAP * ld,LDAPMessage * chain)37 ldap_first_entry( LDAP *ld, LDAPMessage *chain )
38 {
39 	assert( ld != NULL );
40 	assert( LDAP_VALID( ld ) );
41 	assert( chain != NULL );
42 
43 	return chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY
44 		? chain
45 		: ldap_next_entry( ld, chain );
46 }
47 
48 LDAPMessage *
ldap_next_entry(LDAP * ld,LDAPMessage * entry)49 ldap_next_entry( LDAP *ld, LDAPMessage *entry )
50 {
51 	assert( ld != NULL );
52 	assert( LDAP_VALID( ld ) );
53 	assert( entry != NULL );
54 
55 	for(
56 		entry = entry->lm_chain;
57 		entry != NULL;
58 		entry = entry->lm_chain )
59 	{
60 		if( entry->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
61 			return( entry );
62 		}
63 	}
64 
65 	return( NULL );
66 }
67 
68 int
ldap_count_entries(LDAP * ld,LDAPMessage * chain)69 ldap_count_entries( LDAP *ld, LDAPMessage *chain )
70 {
71 	int	i;
72 
73 	assert( ld != NULL );
74 	assert( LDAP_VALID( ld ) );
75 
76 	for ( i = 0; chain != NULL; chain = chain->lm_chain ) {
77 		if( chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
78 			i++;
79 		}
80 	}
81 
82 	return( i );
83 }
84 
85 int
ldap_get_entry_controls(LDAP * ld,LDAPMessage * entry,LDAPControl *** sctrls)86 ldap_get_entry_controls(
87 	LDAP *ld,
88 	LDAPMessage *entry,
89 	LDAPControl ***sctrls )
90 {
91 	int rc;
92 	BerElement be;
93 
94 	assert( ld != NULL );
95 	assert( LDAP_VALID( ld ) );
96 	assert( entry != NULL );
97 	assert( sctrls != NULL );
98 
99 	if ( entry->lm_msgtype != LDAP_RES_SEARCH_ENTRY ) {
100 		return LDAP_PARAM_ERROR;
101 	}
102 
103 	/* make a local copy of the BerElement */
104 	AC_MEMCPY(&be, entry->lm_ber, sizeof(be));
105 
106 	if ( ber_scanf( &be, "{xx" /*}*/ ) == LBER_ERROR ) {
107 		rc = LDAP_DECODING_ERROR;
108 		goto cleanup_and_return;
109 	}
110 
111 	rc = ldap_pvt_get_controls( &be, sctrls );
112 
113 cleanup_and_return:
114 	if( rc != LDAP_SUCCESS ) {
115 		ld->ld_errno = rc;
116 
117 		if( ld->ld_matched != NULL ) {
118 			LDAP_FREE( ld->ld_matched );
119 			ld->ld_matched = NULL;
120 		}
121 
122 		if( ld->ld_error != NULL ) {
123 			LDAP_FREE( ld->ld_error );
124 			ld->ld_error = NULL;
125 		}
126 	}
127 
128 	return rc;
129 }
130