1 /*
2  * Copyright (c) 2001 by Sun Microsystems, Inc.
3  * All rights reserved.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * The contents of this file are subject to the Netscape Public
10  * License Version 1.1 (the "License"); you may not use this file
11  * except in compliance with the License. You may obtain a copy of
12  * the License at http://www.mozilla.org/NPL/
13  *
14  * Software distributed under the License is distributed on an "AS
15  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
16  * implied. See the License for the specific language governing
17  * rights and limitations under the License.
18  *
19  * The Original Code is Mozilla Communicator client code, released
20  * March 31, 1998.
21  *
22  * The Initial Developer of the Original Code is Netscape
23  * Communications Corporation. Portions created by Netscape are
24  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
25  * Rights Reserved.
26  *
27  * Contributor(s):
28  */
29 /*
30  *  Copyright (c) 1990 Regents of the University of Michigan.
31  *  All rights reserved.
32  */
33 /*
34  *  getattr.c
35  */
36 
37 #if 0
38 #ifndef lint
39 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
40 #endif
41 #endif
42 
43 #include "ldap-int.h"
44 
45 
46 static ber_len_t
bytes_remaining(BerElement * ber)47 bytes_remaining( BerElement *ber )
48 {
49 	ber_len_t	len;
50 
51 	if ( ber_get_option( ber, LBER_OPT_REMAINING_BYTES, &len ) != 0 ) {
52 		return( 0 );	/* not sure what else to do.... */
53 	}
54 	return( len );
55 }
56 
57 
58 char *
59 LDAP_CALL
ldap_first_attribute(LDAP * ld,LDAPMessage * entry,BerElement ** ber)60 ldap_first_attribute( LDAP *ld, LDAPMessage *entry, BerElement **ber )
61 {
62 	char	*attr;
63 	int	err;
64 	ber_int_t	seqlength;
65 
66 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_first_attribute\n", 0, 0, 0 );
67 
68 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
69 		return( NULL );		/* punt */
70 	}
71 
72 	if ( ber == NULL || !NSLDAPI_VALID_LDAPMESSAGE_ENTRY_POINTER( entry )) {
73 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
74 		return( NULL );
75 	}
76 
77 	if ( nsldapi_alloc_ber_with_options( ld, ber ) != LDAP_SUCCESS ) {
78 		return( NULL );
79 	}
80 
81 	**ber = *entry->lm_ber;
82 
83 	attr = NULL;			/* pessimistic */
84 	err = LDAP_DECODING_ERROR;	/* ditto */
85 
86 	/*
87 	 * Skip past the sequence, dn, and sequence of sequence.
88 	 * Reset number of bytes remaining so we confine the rest of our
89 	 * decoding to the current sequence.
90 	 */
91 	if ( ber_scanf( *ber, "{xl{", &seqlength ) != LBER_ERROR &&
92 	     ber_set_option( *ber, LBER_OPT_REMAINING_BYTES, &seqlength )
93 	    == 0 ) {
94 		/* snarf the attribute type, and skip the set of values,
95 		 * leaving us positioned right before the next attribute
96 		 * type/value sequence.
97 		 */
98 		if ( ber_scanf( *ber, "{ax}", &attr ) != LBER_ERROR ||
99 		    bytes_remaining( *ber ) == 0 ) {
100 			err = LDAP_SUCCESS;
101 		}
102 	}
103 
104 	LDAP_SET_LDERRNO( ld, err, NULL, NULL );
105 	if ( attr == NULL || err != LDAP_SUCCESS ) {
106 		ber_free( *ber, 0 );
107 		*ber = NULL;
108 	}
109 	return( attr );
110 }
111 
112 /* ARGSUSED */
113 char *
114 LDAP_CALL
ldap_next_attribute(LDAP * ld,LDAPMessage * entry,BerElement * ber)115 ldap_next_attribute( LDAP *ld, LDAPMessage *entry, BerElement *ber )
116 {
117 	char	*attr;
118 	int	err;
119 
120 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_next_attribute\n", 0, 0, 0 );
121 
122 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
123 		return( NULL );		/* punt */
124 	}
125 
126 	if ( ber == NULL || !NSLDAPI_VALID_LDAPMESSAGE_ENTRY_POINTER( entry )) {
127 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
128 		return( NULL );
129 	}
130 
131 	attr = NULL;			/* pessimistic */
132 	err = LDAP_DECODING_ERROR;	/* ditto */
133 
134 	/* skip sequence, snarf attribute type, skip values */
135 	if ( ber_scanf( ber, "{ax}", &attr ) != LBER_ERROR ||
136 	    bytes_remaining( ber ) == 0 ) {
137 		err = LDAP_SUCCESS;
138 	}
139 
140 	LDAP_SET_LDERRNO( ld, err, NULL, NULL );
141 	return( attr );
142 }
143