Document DN and error handling functions.
[wine] / dlls / wldap32 / value.c
1 /*
2  * WLDAP32 - LDAP support for Wine
3  *
4  * Copyright 2005 Hans Leidekker
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include "wine/port.h"
24 #include "wine/debug.h"
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winnls.h"
31
32 #ifdef HAVE_LDAP_H
33 #include <ldap.h>
34 #else
35 #define LDAP_SUCCESS        0x00
36 #define LDAP_NOT_SUPPORTED  0x5c
37 #endif
38
39 #include "winldap_private.h"
40 #include "wldap32.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
43
44 ULONG WLDAP32_ldap_count_values_len( struct WLDAP32_berval **vals )
45 {
46     ULONG ret = LDAP_NOT_SUPPORTED;
47 #ifdef HAVE_LDAP
48
49     TRACE( "(%p)\n", vals );
50     ret = ldap_count_values_len( (struct berval **)vals );
51
52 #endif
53     return ret;
54 }
55
56 ULONG ldap_count_valuesA( PCHAR *vals )
57 {
58     ULONG ret = LDAP_NOT_SUPPORTED;
59 #ifdef HAVE_LDAP
60     WCHAR **valsW = NULL;
61
62     TRACE( "(%p)\n", vals );
63
64     if (!vals) return 0;
65
66     valsW = strarrayAtoW( vals );
67     if (!valsW) return WLDAP32_LDAP_NO_MEMORY;
68
69     ret = ldap_count_valuesW( valsW );
70     strarrayfreeW( valsW );
71
72 #endif
73     return ret;
74 }
75
76 ULONG ldap_count_valuesW( PWCHAR *vals )
77 {
78     ULONG ret = LDAP_NOT_SUPPORTED;
79 #ifdef HAVE_LDAP
80     WCHAR **p = vals;
81
82     TRACE( "(%p)\n", vals );
83
84     if (!vals) return 0;
85
86     ret = 0;
87     while (*p)
88     {
89         ret++;
90         p++;
91     }
92
93 #endif
94     return ret;
95 }
96
97 PCHAR *ldap_get_valuesA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry, PCHAR attr )
98 {
99     PCHAR *ret = NULL;
100 #ifdef HAVE_LDAP
101     WCHAR *attrW = NULL, **retW;
102
103     TRACE( "(%p, %p, %s)\n", ld, entry, debugstr_a(attr) );
104
105     if (!ld || !entry || !attr) return NULL;
106
107     attrW = strAtoW( attr );
108     if (!attrW) return NULL;
109
110     retW = ldap_get_valuesW( ld, entry, attrW );
111
112     ret = strarrayWtoA( retW );
113     ldap_value_freeW( retW );
114     strfreeW( attrW );
115
116 #endif
117     return ret;
118 }
119
120 #ifdef HAVE_LDAP
121 static char *bv2str( struct berval *bv )
122 {
123     char *str = NULL;
124     unsigned int len = bv->bv_len;
125
126     str = HeapAlloc( GetProcessHeap(), 0, len + 1 );
127     if (str)
128     {
129         memcpy( str, bv->bv_val, len );
130         str[len] = '\0';
131     }
132     return str;
133 }
134
135 static char **bv2str_array( struct berval **bv )
136 {
137     unsigned int len = 0, i = 0;
138     struct berval **p = bv;
139     char **str;
140
141     while (*p)
142     {
143         len++;
144         p++;
145     }
146     str = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(char *) );
147     if (!str) return NULL;
148
149     p = bv;
150     while (*p)
151     {
152         str[i] = bv2str( *p );
153         if (!str[i])
154         {
155             for (--i; i >= 0; i--)
156                 HeapFree( GetProcessHeap(), 0, str[i] );
157
158             HeapFree( GetProcessHeap(), 0, str );
159             return NULL;
160         } 
161         i++;
162         p++; 
163     }
164     str[i] = NULL;
165     return str;
166 }
167 #endif
168
169 PWCHAR *ldap_get_valuesW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry, PWCHAR attr )
170 {
171     PWCHAR *ret = NULL;
172 #ifdef HAVE_LDAP
173     char *attrU = NULL, **retU;
174     struct berval **bv;
175
176     TRACE( "(%p, %p, %s)\n", ld, entry, debugstr_w(attr) );
177
178     if (!ld || !entry || !attr) return NULL;
179
180     attrU = strWtoU( attr );
181     if (!attrU) return NULL;
182
183     bv = ldap_get_values_len( ld, entry, attrU );
184
185     retU = bv2str_array( bv );
186     ret = strarrayUtoW( retU );
187
188     ldap_value_free_len( bv );
189     strarrayfreeU( retU );
190     strfreeU( attrU );
191
192 #endif
193     return ret;
194 }
195
196 struct WLDAP32_berval **ldap_get_values_lenA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *message,
197     PCHAR attr )
198 {
199 #ifdef HAVE_LDAP
200     WCHAR *attrW = NULL;
201     struct WLDAP32_berval **ret;
202
203     TRACE( "(%p, %p, %s)\n", ld, message, debugstr_a(attr) );
204
205     if (!ld || !message || !attr) return NULL;
206
207     attrW = strAtoW( attr );
208     if (!attrW) return NULL;
209
210     ret = ldap_get_values_lenW( ld, message, attrW );
211
212     strfreeW( attrW );
213     return ret;
214
215 #endif
216     return NULL;
217 }
218
219 struct WLDAP32_berval **ldap_get_values_lenW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *message,
220     PWCHAR attr )
221 {
222 #ifdef HAVE_LDAP
223     char *attrU = NULL;
224     struct berval **ret;
225
226     TRACE( "(%p, %p, %s)\n", ld, message, debugstr_w(attr) );
227
228     if (!ld || !message || !attr) return NULL;
229
230     attrU = strWtoU( attr );
231     if (!attrU) return NULL;
232
233     ret = ldap_get_values_len( ld, message, attrU );
234
235     strfreeU( attrU );
236     return (struct WLDAP32_berval **)ret;
237
238 #endif
239     return NULL;
240 }
241
242 ULONG WLDAP32_ldap_value_free_len( struct WLDAP32_berval **vals )
243 {
244 #ifdef HAVE_LDAP
245
246     TRACE( "(%p)\n", vals );
247     ldap_value_free_len( (struct berval **)vals );
248
249 #endif
250     return LDAP_SUCCESS;
251 }
252
253 ULONG ldap_value_freeA( PCHAR *vals )
254 {
255     TRACE( "(%p)\n", vals );
256
257     strarrayfreeA( vals );
258     return LDAP_SUCCESS;
259 }
260
261 ULONG ldap_value_freeW( PWCHAR *vals )
262 {
263     TRACE( "(%p)\n", vals );
264
265     strarrayfreeW( vals );
266     return LDAP_SUCCESS;
267 }