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