]> git.sur5r.net Git - openldap/blob - libraries/libldap/getvalues.c
Remove LDAP_PORT dependencies so that ldap.conf defaults take over.
[openldap] / libraries / libldap / getvalues.c
1 /*
2  *  Copyright (c) 1990 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  getvalues.c
6  */
7
8 #include "portable.h"
9
10 #ifndef lint 
11 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
12 #endif
13
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 #include <ac/ctype.h>
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/time.h>
21
22 #include "ldap-int.h"
23
24 char **
25 ldap_get_values( LDAP *ld, LDAPMessage *entry, char *target )
26 {
27         BerElement      ber;
28         char            attr[LDAP_MAX_ATTR_LEN];
29         int             found = 0;
30         long            len;
31         char            **vals;
32
33         Debug( LDAP_DEBUG_TRACE, "ldap_get_values\n", 0, 0, 0 );
34
35         ber = *entry->lm_ber;
36
37         /* skip sequence, dn, sequence of, and snag the first attr */
38         len = sizeof(attr);
39         if ( ber_scanf( &ber, "{x{{s", attr, &len ) == LBER_ERROR ) {
40                 ld->ld_errno = LDAP_DECODING_ERROR;
41                 return( NULL );
42         }
43
44         if ( strcasecmp( target, attr ) == 0 )
45                 found = 1;
46
47         /* break out on success, return out on error */
48         while ( ! found ) {
49                 len = sizeof(attr);
50                 if ( ber_scanf( &ber, "x}{s", attr, &len ) == LBER_ERROR ) {
51                         ld->ld_errno = LDAP_DECODING_ERROR;
52                         return( NULL );
53                 }
54
55                 if ( strcasecmp( target, attr ) == 0 )
56                         break;
57         }
58
59         /* 
60          * if we get this far, we've found the attribute and are sitting
61          * just before the set of values.
62          */
63
64         if ( ber_scanf( &ber, "[v]", &vals ) == LBER_ERROR ) {
65                 ld->ld_errno = LDAP_DECODING_ERROR;
66                 return( NULL );
67         }
68
69         return( vals );
70 }
71
72 struct berval **
73 ldap_get_values_len( LDAP *ld, LDAPMessage *entry, char *target )
74 {
75         BerElement      ber;
76         char            attr[LDAP_MAX_ATTR_LEN];
77         int             found = 0;
78         long            len;
79         struct berval   **vals;
80
81         Debug( LDAP_DEBUG_TRACE, "ldap_get_values_len\n", 0, 0, 0 );
82
83         ber = *entry->lm_ber;
84
85         /* skip sequence, dn, sequence of, and snag the first attr */
86         len = sizeof(attr);
87         if ( ber_scanf( &ber, "{x{{s", attr, &len ) == LBER_ERROR ) {
88                 ld->ld_errno = LDAP_DECODING_ERROR;
89                 return( NULL );
90         }
91
92         if ( strcasecmp( target, attr ) == 0 )
93                 found = 1;
94
95         /* break out on success, return out on error */
96         while ( ! found ) {
97                 len = sizeof(attr);
98                 if ( ber_scanf( &ber, "x}{s", attr, &len ) == LBER_ERROR ) {
99                         ld->ld_errno = LDAP_DECODING_ERROR;
100                         return( NULL );
101                 }
102
103                 if ( strcasecmp( target, attr ) == 0 )
104                         break;
105         }
106
107         /* 
108          * if we get this far, we've found the attribute and are sitting
109          * just before the set of values.
110          */
111
112         if ( ber_scanf( &ber, "[V]", &vals ) == LBER_ERROR ) {
113                 ld->ld_errno = LDAP_DECODING_ERROR;
114                 return( NULL );
115         }
116
117         return( vals );
118 }
119
120 int
121 ldap_count_values( char **vals )
122 {
123         int     i;
124
125         if ( vals == NULL )
126                 return( 0 );
127
128         for ( i = 0; vals[i] != NULL; i++ )
129                 ;       /* NULL */
130
131         return( i );
132 }
133
134 int
135 ldap_count_values_len( struct berval **vals )
136 {
137         return( ldap_count_values( (char **) vals ) );
138 }
139
140 void
141 ldap_value_free( char **vals )
142 {
143         int     i;
144
145         if ( vals == NULL )
146                 return;
147         for ( i = 0; vals[i] != NULL; i++ )
148                 free( vals[i] );
149         free( (char *) vals );
150 }
151
152 void
153 ldap_value_free_len( struct berval **vals )
154 {
155         int     i;
156
157         if ( vals == NULL )
158                 return;
159         for ( i = 0; vals[i] != NULL; i++ ) {
160                 free( vals[i]->bv_val );
161                 free( vals[i] );
162         }
163         free( (char *) vals );
164 }