xref: /illumos-gate/usr/src/lib/libldap5/sources/ldap/common/getentry.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*
2  * The contents of this file are subject to the Netscape Public
3  * License Version 1.1 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of
5  * the License at http://www.mozilla.org/NPL/
6  *
7  * Software distributed under the License is distributed on an "AS
8  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9  * implied. See the License for the specific language governing
10  * rights and limitations under the License.
11  *
12  * The Original Code is Mozilla Communicator client code, released
13  * March 31, 1998.
14  *
15  * The Initial Developer of the Original Code is Netscape
16  * Communications Corporation. Portions created by Netscape are
17  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
18  * Rights Reserved.
19  *
20  * Contributor(s):
21  */
22 /*
23  *  Copyright (c) 1990 Regents of the University of Michigan.
24  *  All rights reserved.
25  */
26 /*
27  *  getentry.c
28  */
29 
30 #if 0
31 #ifndef lint
32 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
33 #endif
34 #endif
35 
36 #include "ldap-int.h"
37 
38 LDAPMessage *
39 LDAP_CALL
40 ldap_first_entry( LDAP *ld, LDAPMessage *chain )
41 {
42 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || chain == NULLMSG ) {
43 		return( NULLMSG );
44 	}
45 
46 	if ( chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
47 		return( chain );
48 	}
49 
50 	return( ldap_next_entry( ld, chain ));
51 }
52 
53 
54 LDAPMessage *
55 LDAP_CALL
56 ldap_next_entry( LDAP *ld, LDAPMessage *entry )
57 {
58 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || entry == NULLMSG ) {
59 		return( NULLMSG );
60 	}
61 
62 	for ( entry = entry->lm_chain; entry != NULLMSG;
63 	    entry = entry->lm_chain ) {
64 		if ( entry->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
65 			return( entry );
66 		}
67 	}
68 
69 	return( NULLMSG );
70 }
71 
72 int
73 LDAP_CALL
74 ldap_count_entries( LDAP *ld, LDAPMessage *chain )
75 {
76 	int	i;
77 
78 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
79 		return( -1 );
80 	}
81 
82 	for ( i = 0; chain != NULL; chain = chain->lm_chain ) {
83 		if ( chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
84 			++i;
85 		}
86 	}
87 
88 	return( i );
89 }
90 
91 
92 int
93 LDAP_CALL
94 ldap_get_entry_controls( LDAP *ld, LDAPMessage *entry,
95 	LDAPControl ***serverctrlsp )
96 {
97 	int		rc;
98 	BerElement	tmpber;
99 
100 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_get_entry_controls\n", 0, 0, 0 );
101 
102 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
103 		return( LDAP_PARAM_ERROR );
104 	}
105 
106 	if ( !NSLDAPI_VALID_LDAPMESSAGE_ENTRY_POINTER( entry )
107 	    || serverctrlsp == NULL ) {
108 		rc = LDAP_PARAM_ERROR;
109 		goto report_error_and_return;
110 	}
111 
112 	*serverctrlsp = NULL;
113 	tmpber = *entry->lm_ber;	/* struct copy */
114 
115 	/* skip past dn and entire attribute/value list */
116 	if ( ber_scanf( &tmpber, "{xx" ) == LBER_ERROR ) {
117 		rc = LDAP_DECODING_ERROR;
118 		goto report_error_and_return;
119 	}
120 
121 	rc = nsldapi_get_controls( &tmpber, serverctrlsp );
122 
123 report_error_and_return:
124 	LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
125 	return( rc );
126 }
127