1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  *
3*0Sstevel@tonic-gate  * Portions Copyright %G% Sun Microsystems, Inc.
4*0Sstevel@tonic-gate  * All Rights Reserved
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  */
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate /*
11*0Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
12*0Sstevel@tonic-gate  *  All rights reserved.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  *  getvalues.c
15*0Sstevel@tonic-gate  */
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate #ifndef lint
18*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
19*0Sstevel@tonic-gate #endif
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate #include <stdio.h>
22*0Sstevel@tonic-gate #include <ctype.h>
23*0Sstevel@tonic-gate #include <string.h>
24*0Sstevel@tonic-gate #include <stdlib.h> /* free() for Solaris */
25*0Sstevel@tonic-gate #ifdef MACOS
26*0Sstevel@tonic-gate #include <stdlib.h>
27*0Sstevel@tonic-gate #include "macos.h"
28*0Sstevel@tonic-gate #else /* MACOS */
29*0Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 )
30*0Sstevel@tonic-gate #include <malloc.h>
31*0Sstevel@tonic-gate #include "msdos.h"
32*0Sstevel@tonic-gate #else /* DOS */
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <sys/socket.h>
35*0Sstevel@tonic-gate #endif /* DOS */
36*0Sstevel@tonic-gate #endif /* MACOS */
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include "lber.h"
39*0Sstevel@tonic-gate #include "ldap.h"
40*0Sstevel@tonic-gate #include "ldap-private.h"
41*0Sstevel@tonic-gate #include "ldap-int.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate char **
44*0Sstevel@tonic-gate ldap_get_values( LDAP *ld, LDAPMessage *entry, char *target )
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate 	BerElement	ber;
47*0Sstevel@tonic-gate 	char		attr[LDAP_MAX_ATTR_LEN];
48*0Sstevel@tonic-gate 	int		found = 0;
49*0Sstevel@tonic-gate 	int		len;
50*0Sstevel@tonic-gate 	char		**vals;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 184, "ldap_get_values\n"), 0, 0, 0 );
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	ber = *entry->lm_ber;
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 	/* skip sequence, dn, sequence of, and snag the first attr */
57*0Sstevel@tonic-gate 	len = sizeof(attr);
58*0Sstevel@tonic-gate 	if ( ber_scanf( &ber, "{x{{s", attr, &len ) == LBER_ERROR ) {
59*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_DECODING_ERROR;
60*0Sstevel@tonic-gate 		return( NULL );
61*0Sstevel@tonic-gate 	}
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	if ( strcasecmp( target, attr ) == 0 )
64*0Sstevel@tonic-gate 		found = 1;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 	/* break out on success, return out on error */
67*0Sstevel@tonic-gate 	while ( ! found ) {
68*0Sstevel@tonic-gate 		len = sizeof(attr);
69*0Sstevel@tonic-gate 		if ( ber_scanf( &ber, "x}{s", attr, &len ) == LBER_ERROR ) {
70*0Sstevel@tonic-gate 			ld->ld_errno = LDAP_DECODING_ERROR;
71*0Sstevel@tonic-gate 			return( NULL );
72*0Sstevel@tonic-gate 		}
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 		if ( strcasecmp( target, attr ) == 0 )
75*0Sstevel@tonic-gate 			break;
76*0Sstevel@tonic-gate 	}
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	/*
79*0Sstevel@tonic-gate 	 * if we get this far, we've found the attribute and are sitting
80*0Sstevel@tonic-gate 	 * just before the set of values.
81*0Sstevel@tonic-gate 	 */
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	if ( ber_scanf( &ber, "[v]", &vals ) == LBER_ERROR ) {
84*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_DECODING_ERROR;
85*0Sstevel@tonic-gate 		return( NULL );
86*0Sstevel@tonic-gate 	}
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	return( vals );
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate struct berval **
92*0Sstevel@tonic-gate ldap_get_values_len( LDAP *ld, LDAPMessage *entry, char *target )
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	BerElement	ber;
95*0Sstevel@tonic-gate 	char		attr[LDAP_MAX_ATTR_LEN];
96*0Sstevel@tonic-gate 	int		found = 0;
97*0Sstevel@tonic-gate 	int		len;
98*0Sstevel@tonic-gate 	struct berval	**vals;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 185, "ldap_get_values_len\n"), 0, 0, 0 );
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	ber = *entry->lm_ber;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	/* skip sequence, dn, sequence of, and snag the first attr */
105*0Sstevel@tonic-gate 	len = sizeof(attr);
106*0Sstevel@tonic-gate 	if ( ber_scanf( &ber, "{x{{s", attr, &len ) == LBER_ERROR ) {
107*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_DECODING_ERROR;
108*0Sstevel@tonic-gate 		return( NULL );
109*0Sstevel@tonic-gate 	}
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	if ( strcasecmp( target, attr ) == 0 )
112*0Sstevel@tonic-gate 		found = 1;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	/* break out on success, return out on error */
115*0Sstevel@tonic-gate 	while ( ! found ) {
116*0Sstevel@tonic-gate 		len = sizeof(attr);
117*0Sstevel@tonic-gate 		if ( ber_scanf( &ber, "x}{s", attr, &len ) == LBER_ERROR ) {
118*0Sstevel@tonic-gate 			ld->ld_errno = LDAP_DECODING_ERROR;
119*0Sstevel@tonic-gate 			return( NULL );
120*0Sstevel@tonic-gate 		}
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 		if ( strcasecmp( target, attr ) == 0 )
123*0Sstevel@tonic-gate 			break;
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	/*
127*0Sstevel@tonic-gate 	 * if we get this far, we've found the attribute and are sitting
128*0Sstevel@tonic-gate 	 * just before the set of values.
129*0Sstevel@tonic-gate 	 */
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	if ( ber_scanf( &ber, "[V]", &vals ) == LBER_ERROR ) {
132*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_DECODING_ERROR;
133*0Sstevel@tonic-gate 		return( NULL );
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	return( vals );
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate int
140*0Sstevel@tonic-gate ldap_count_values( char **vals )
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate 	int	i;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	if ( vals == NULL )
145*0Sstevel@tonic-gate 		return( 0 );
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	for ( i = 0; vals[i] != NULL; i++ )
148*0Sstevel@tonic-gate 		;	/* NULL */
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	return( i );
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate int
154*0Sstevel@tonic-gate ldap_count_values_len( struct berval **vals )
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate 	return( ldap_count_values( (char **) vals ) );
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate void
160*0Sstevel@tonic-gate ldap_value_free( char **vals )
161*0Sstevel@tonic-gate {
162*0Sstevel@tonic-gate 	int	i;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	if ( vals == NULL )
165*0Sstevel@tonic-gate 		return;
166*0Sstevel@tonic-gate 	for ( i = 0; vals[i] != NULL; i++ )
167*0Sstevel@tonic-gate 		free( vals[i] );
168*0Sstevel@tonic-gate 	free( (char *) vals );
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate void
172*0Sstevel@tonic-gate ldap_value_free_len( struct berval **vals )
173*0Sstevel@tonic-gate {
174*0Sstevel@tonic-gate 	int	i;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	if ( vals == NULL )
177*0Sstevel@tonic-gate 		return;
178*0Sstevel@tonic-gate 	for ( i = 0; vals[i] != NULL; i++ ) {
179*0Sstevel@tonic-gate 		free( vals[i]->bv_val );
180*0Sstevel@tonic-gate 		free( vals[i] );
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 	free( (char *) vals );
183*0Sstevel@tonic-gate }
184