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