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