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