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