xref: /reactos/dll/win32/wldap32/dn.c (revision c2c66aff)
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include "winldap_private.h"
22 
23 /***********************************************************************
24  *      ldap_dn2ufnA     (WLDAP32.@)
25  *
26  * See ldap_dn2ufnW.
27  */
28 PCHAR CDECL ldap_dn2ufnA( PCHAR dn )
29 {
30     PCHAR ret = NULL;
31 #ifdef HAVE_LDAP
32     WCHAR *dnW, *retW;
33 
34     TRACE( "(%s)\n", debugstr_a(dn) );
35 
36     dnW = strAtoW( dn );
37     if (!dnW) return NULL;
38 
39     retW = ldap_dn2ufnW( dnW );
40     ret = strWtoA( retW );
41 
42     strfreeW( dnW );
43     ldap_memfreeW( retW );
44 
45 #endif
46     return ret;
47 }
48 
49 /***********************************************************************
50  *      ldap_dn2ufnW     (WLDAP32.@)
51  *
52  * Convert a DN to a user-friendly name.
53  *
54  * PARAMS
55  *  dn  [I] DN to convert.
56  *
57  * RETURNS
58  *  Success: Pointer to a string containing the user-friendly name.
59  *  Failure: NULL
60  *
61  * NOTES
62  *  Free the string with ldap_memfree.
63  */
64 PWCHAR CDECL ldap_dn2ufnW( PWCHAR dn )
65 {
66     PWCHAR ret = NULL;
67 #ifdef HAVE_LDAP
68     char *dnU, *retU;
69 
70     TRACE( "(%s)\n", debugstr_w(dn) );
71 
72     dnU = strWtoU( dn );
73     if (!dnU) return NULL;
74 
75     retU = ldap_dn2ufn( dnU );
76     ret = strUtoW( retU );
77 
78     strfreeU( dnU );
79     ldap_memfree( retU );
80 
81 #endif
82     return ret;
83 }
84 
85 /***********************************************************************
86  *      ldap_explode_dnA     (WLDAP32.@)
87  *
88  * See ldap_explode_dnW.
89  */
90 PCHAR * CDECL ldap_explode_dnA( PCHAR dn, ULONG notypes )
91 {
92     PCHAR *ret = NULL;
93 #ifdef HAVE_LDAP
94     WCHAR *dnW, **retW;
95 
96     TRACE( "(%s, 0x%08x)\n", debugstr_a(dn), notypes );
97 
98     dnW = strAtoW( dn );
99     if (!dnW) return NULL;
100 
101     retW = ldap_explode_dnW( dnW, notypes );
102     ret = strarrayWtoA( retW );
103 
104     strfreeW( dnW );
105     ldap_value_freeW( retW );
106 
107 #endif
108     return ret;
109 }
110 
111 /***********************************************************************
112  *      ldap_explode_dnW     (WLDAP32.@)
113  *
114  * Break up a DN into its components.
115  *
116  * PARAMS
117  *  dn       [I] DN to break up.
118  *  notypes  [I] Remove attribute type information from the components.
119  *
120  * RETURNS
121  *  Success: Pointer to a NULL-terminated array that contains the DN
122  *           components.
123  *  Failure: NULL
124  *
125  * NOTES
126  *  Free the string array with ldap_value_free.
127  */
128 PWCHAR * CDECL ldap_explode_dnW( PWCHAR dn, ULONG notypes )
129 {
130     PWCHAR *ret = NULL;
131 #ifdef HAVE_LDAP
132     char *dnU, **retU;
133 
134     TRACE( "(%s, 0x%08x)\n", debugstr_w(dn), notypes );
135 
136     dnU = strWtoU( dn );
137     if (!dnU) return NULL;
138 
139     retU = ldap_explode_dn( dnU, notypes );
140     ret = strarrayUtoW( retU );
141 
142     strfreeU( dnU );
143     ldap_memvfree( (void **)retU );
144 
145 #endif
146     return ret;
147 }
148 
149 /***********************************************************************
150  *      ldap_get_dnA     (WLDAP32.@)
151  *
152  * See ldap_get_dnW.
153  */
154 PCHAR CDECL ldap_get_dnA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
155 {
156     PCHAR ret = NULL;
157 #ifdef HAVE_LDAP
158     PWCHAR retW;
159 
160     TRACE( "(%p, %p)\n", ld, entry );
161 
162     if (!ld || !entry) return NULL;
163 
164     retW = ldap_get_dnW( ld, entry );
165 
166     ret = strWtoA( retW );
167     ldap_memfreeW( retW );
168 
169 #endif
170     return ret;
171 }
172 
173 /***********************************************************************
174  *      ldap_get_dnW     (WLDAP32.@)
175  *
176  * Retrieve the DN from a given LDAP message.
177  *
178  * PARAMS
179  *  ld     [I] Pointer to an LDAP context.
180  *  entry  [I] LDAPMessage structure to retrieve the DN from.
181  *
182  * RETURNS
183  *  Success: Pointer to a string that contains the DN.
184  *  Failure: NULL
185  *
186  * NOTES
187  *  Free the string with ldap_memfree.
188  */
189 PWCHAR CDECL ldap_get_dnW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
190 {
191     PWCHAR ret = NULL;
192 #ifdef HAVE_LDAP
193     char *retU;
194 
195     TRACE( "(%p, %p)\n", ld, entry );
196 
197     if (!ld || !entry) return NULL;
198 
199     retU = ldap_get_dn( ld, entry );
200 
201     ret = strUtoW( retU );
202     ldap_memfree( retU );
203 
204 #endif
205     return ret;
206 }
207 
208 /***********************************************************************
209  *      ldap_ufn2dnA     (WLDAP32.@)
210  *
211  * See ldap_ufn2dnW.
212  */
213 ULONG CDECL ldap_ufn2dnA( PCHAR ufn, PCHAR *dn )
214 {
215     ULONG ret = WLDAP32_LDAP_SUCCESS;
216 #ifdef HAVE_LDAP
217     PWCHAR ufnW = NULL, dnW = NULL;
218 
219     TRACE( "(%s, %p)\n", debugstr_a(ufn), dn );
220 
221     if (!dn) return WLDAP32_LDAP_PARAM_ERROR;
222 
223     *dn = NULL;
224 
225     if (ufn) {
226         ufnW = strAtoW( ufn );
227         if (!ufnW) return WLDAP32_LDAP_NO_MEMORY;
228     }
229 
230     ret = ldap_ufn2dnW( ufnW, &dnW );
231 
232     if (dnW) {
233         *dn = strWtoA( dnW );
234         if (!*dn) ret = WLDAP32_LDAP_NO_MEMORY;
235     }
236 
237     strfreeW( ufnW );
238     ldap_memfreeW( dnW );
239 
240 #endif
241     return ret;
242 }
243 
244 /***********************************************************************
245  *      ldap_ufn2dnW     (WLDAP32.@)
246  *
247  * Convert a user-friendly name to a DN.
248  *
249  * PARAMS
250  *  ufn  [I] User-friendly name to convert.
251  *  dn   [O] Receives a pointer to a string containing the DN.
252  *
253  * RETURNS
254  *  Success: LDAP_SUCCESS
255  *  Failure: An LDAP error code.
256  *
257  * NOTES
258  *  Free the string with ldap_memfree.
259  */
260 ULONG CDECL ldap_ufn2dnW( PWCHAR ufn, PWCHAR *dn )
261 {
262     ULONG ret = WLDAP32_LDAP_SUCCESS;
263 #ifdef HAVE_LDAP
264     char *ufnU = NULL;
265 
266     TRACE( "(%s, %p)\n", debugstr_w(ufn), dn );
267 
268     if (!dn) return WLDAP32_LDAP_PARAM_ERROR;
269 
270     *dn = NULL;
271 
272     if (ufn) {
273         ufnU = strWtoU( ufn );
274         if (!ufnU) return WLDAP32_LDAP_NO_MEMORY;
275 
276         /* FIXME: do more than just a copy */
277         *dn = strUtoW( ufnU );
278         if (!*dn) ret = WLDAP32_LDAP_NO_MEMORY;
279     }
280 
281     strfreeU( ufnU );
282 
283 #endif
284     return ret;
285 }
286