Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenmisc.c
Go to the documentation of this file.
00001 /* 00002 * WLDAP32 - LDAP support for Wine 00003 * 00004 * Copyright 2005 Hans Leidekker 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 00019 */ 00020 00021 #include "config.h" 00022 00023 #include "wine/port.h" 00024 #include "wine/debug.h" 00025 00026 #include <stdarg.h> 00027 #include <stdio.h> 00028 00029 #include "windef.h" 00030 #include "winbase.h" 00031 #include "winnls.h" 00032 00033 #ifdef HAVE_LDAP_H 00034 #include <ldap.h> 00035 #endif 00036 00037 #include "winldap_private.h" 00038 #include "wldap32.h" 00039 00040 WINE_DEFAULT_DEBUG_CHANNEL(wldap32); 00041 00042 /*********************************************************************** 00043 * ldap_abandon (WLDAP32.@) 00044 * 00045 * Cancel an asynchronous operation. 00046 * 00047 * PARAMS 00048 * ld [I] Pointer to an LDAP context. 00049 * msgid [I] ID of the operation to cancel. 00050 * 00051 * RETURNS 00052 * Success: LDAP_SUCCESS 00053 * Failure: An LDAP error code. 00054 */ 00055 ULONG CDECL WLDAP32_ldap_abandon( WLDAP32_LDAP *ld, ULONG msgid ) 00056 { 00057 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED; 00058 #ifdef HAVE_LDAP 00059 00060 TRACE( "(%p, 0x%08x)\n", ld, msgid ); 00061 00062 if (!ld) return ~0u; 00063 ret = map_error( ldap_abandon_ext( ld, msgid, NULL, NULL )); 00064 00065 #endif 00066 return ret; 00067 } 00068 00069 /*********************************************************************** 00070 * ldap_check_filterA (WLDAP32.@) 00071 * 00072 * See ldap_check_filterW. 00073 */ 00074 ULONG CDECL ldap_check_filterA( WLDAP32_LDAP *ld, PCHAR filter ) 00075 { 00076 ULONG ret; 00077 WCHAR *filterW = NULL; 00078 00079 TRACE( "(%p, %s)\n", ld, debugstr_a(filter) ); 00080 00081 if (!ld) return WLDAP32_LDAP_PARAM_ERROR; 00082 00083 if (filter) { 00084 filterW = strAtoW( filter ); 00085 if (!filterW) return WLDAP32_LDAP_NO_MEMORY; 00086 } 00087 00088 ret = ldap_check_filterW( ld, filterW ); 00089 00090 strfreeW( filterW ); 00091 return ret; 00092 } 00093 00094 /*********************************************************************** 00095 * ldap_check_filterW (WLDAP32.@) 00096 * 00097 * Check filter syntax. 00098 * 00099 * PARAMS 00100 * ld [I] Pointer to an LDAP context. 00101 * filter [I] Filter string. 00102 * 00103 * RETURNS 00104 * Success: LDAP_SUCCESS 00105 * Failure: An LDAP error code. 00106 */ 00107 ULONG CDECL ldap_check_filterW( WLDAP32_LDAP *ld, PWCHAR filter ) 00108 { 00109 TRACE( "(%p, %s)\n", ld, debugstr_w(filter) ); 00110 00111 if (!ld) return WLDAP32_LDAP_PARAM_ERROR; 00112 return WLDAP32_LDAP_SUCCESS; /* FIXME: do some checks */ 00113 } 00114 00115 /*********************************************************************** 00116 * ldap_cleanup (WLDAP32.@) 00117 */ 00118 ULONG CDECL ldap_cleanup( HANDLE instance ) 00119 { 00120 TRACE( "(%p)\n", instance ); 00121 return WLDAP32_LDAP_SUCCESS; 00122 } 00123 00124 /*********************************************************************** 00125 * ldap_conn_from_msg (WLDAP32.@) 00126 * 00127 * Get the LDAP context for a given message. 00128 * 00129 * PARAMS 00130 * ld [I] Pointer to an LDAP context. 00131 * res [I] LDAP message. 00132 * 00133 * RETURNS 00134 * Success: Pointer to an LDAP context. 00135 * Failure: NULL 00136 */ 00137 WLDAP32_LDAP * CDECL ldap_conn_from_msg( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res ) 00138 { 00139 TRACE( "(%p, %p)\n", ld, res ); 00140 00141 if (!ld || !res) return NULL; 00142 return ld; /* FIXME: not always correct */ 00143 } 00144 00145 /*********************************************************************** 00146 * ldap_count_entries (WLDAP32.@) 00147 * 00148 * Count the number of entries returned from a search. 00149 * 00150 * PARAMS 00151 * ld [I] Pointer to an LDAP context. 00152 * res [I] LDAP message. 00153 * 00154 * RETURNS 00155 * Success: The number of entries. 00156 * Failure: ~0u 00157 */ 00158 ULONG CDECL WLDAP32_ldap_count_entries( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res ) 00159 { 00160 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED; 00161 #ifdef HAVE_LDAP 00162 00163 TRACE( "(%p, %p)\n", ld, res ); 00164 00165 if (!ld) return ~0u; 00166 ret = ldap_count_entries( ld, res ); 00167 00168 #endif 00169 return ret; 00170 } 00171 00172 /*********************************************************************** 00173 * ldap_count_references (WLDAP32.@) 00174 * 00175 * Count the number of references returned from a search. 00176 * 00177 * PARAMS 00178 * ld [I] Pointer to an LDAP context. 00179 * res [I] LDAP message. 00180 * 00181 * RETURNS 00182 * Success: The number of references. 00183 * Failure: ~0u 00184 */ 00185 ULONG CDECL WLDAP32_ldap_count_references( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res ) 00186 { 00187 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED; 00188 #ifdef HAVE_LDAP_COUNT_REFERENCES 00189 00190 TRACE( "(%p, %p)\n", ld, res ); 00191 00192 if (!ld) return 0; 00193 ret = ldap_count_references( ld, res ); 00194 00195 #endif 00196 return ret; 00197 } 00198 00199 static ULONG get_escape_size( PCHAR src, ULONG srclen ) 00200 { 00201 ULONG i, size = 0; 00202 00203 if (src) 00204 { 00205 for (i = 0; i < srclen; i++) 00206 { 00207 if ((src[i] >= '0' && src[i] <= '9') || 00208 (src[i] >= 'A' && src[i] <= 'Z') || 00209 (src[i] >= 'a' && src[i] <= 'z')) 00210 size++; 00211 else 00212 size += 3; 00213 } 00214 } 00215 return size + 1; 00216 } 00217 00218 static void escape_filter_element( PCHAR src, ULONG srclen, PCHAR dst ) 00219 { 00220 ULONG i; 00221 static const char fmt[] = "\\%02X"; 00222 char *d = dst; 00223 00224 for (i = 0; i < srclen; i++) 00225 { 00226 if ((src[i] >= '0' && src[i] <= '9') || 00227 (src[i] >= 'A' && src[i] <= 'Z') || 00228 (src[i] >= 'a' && src[i] <= 'z')) 00229 *d++ = src[i]; 00230 else 00231 { 00232 sprintf( d, fmt, (unsigned char)src[i] ); 00233 d += 3; 00234 } 00235 } 00236 *++d = 0; 00237 } 00238 00239 /*********************************************************************** 00240 * ldap_escape_filter_elementA (WLDAP32.@) 00241 * 00242 * See ldap_escape_filter_elementW. 00243 */ 00244 ULONG CDECL ldap_escape_filter_elementA( PCHAR src, ULONG srclen, PCHAR dst, ULONG dstlen ) 00245 { 00246 ULONG len; 00247 00248 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src, srclen, dst, dstlen ); 00249 00250 len = get_escape_size( src, srclen ); 00251 if (!dst) return len; 00252 00253 if (!src || dstlen < len) 00254 return WLDAP32_LDAP_PARAM_ERROR; 00255 else 00256 { 00257 escape_filter_element( src, srclen, dst ); 00258 return WLDAP32_LDAP_SUCCESS; 00259 } 00260 } 00261 00262 /*********************************************************************** 00263 * ldap_escape_filter_elementW (WLDAP32.@) 00264 * 00265 * Escape binary data for safe passing in filters. 00266 * 00267 * PARAMS 00268 * src [I] Filter element to be escaped. 00269 * srclen [I] Length in bytes of the filter element. 00270 * dst [O] Destination buffer for the escaped filter element. 00271 * dstlen [I] Length in bytes of the destination buffer. 00272 * 00273 * RETURNS 00274 * Success: LDAP_SUCCESS 00275 * Failure: An LDAP error code. 00276 */ 00277 ULONG CDECL ldap_escape_filter_elementW( PCHAR src, ULONG srclen, PWCHAR dst, ULONG dstlen ) 00278 { 00279 ULONG len; 00280 00281 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src, srclen, dst, dstlen ); 00282 00283 len = get_escape_size( src, srclen ); 00284 if (!dst) return len; 00285 00286 /* no matter what you throw at it, this is what native returns */ 00287 return WLDAP32_LDAP_PARAM_ERROR; 00288 } 00289 00290 /*********************************************************************** 00291 * ldap_first_attributeA (WLDAP32.@) 00292 * 00293 * See ldap_first_attributeW. 00294 */ 00295 PCHAR CDECL ldap_first_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry, 00296 WLDAP32_BerElement** ptr ) 00297 { 00298 PCHAR ret = NULL; 00299 #ifdef HAVE_LDAP 00300 WCHAR *retW; 00301 00302 TRACE( "(%p, %p, %p)\n", ld, entry, ptr ); 00303 00304 if (!ld || !entry) return NULL; 00305 retW = ldap_first_attributeW( ld, entry, ptr ); 00306 00307 ret = strWtoA( retW ); 00308 ldap_memfreeW( retW ); 00309 00310 #endif 00311 return ret; 00312 } 00313 00314 /*********************************************************************** 00315 * ldap_first_attributeW (WLDAP32.@) 00316 * 00317 * Get the first attribute for a given entry. 00318 * 00319 * PARAMS 00320 * ld [I] Pointer to an LDAP context. 00321 * entry [I] Entry to retrieve attribute for. 00322 * ptr [O] Position pointer. 00323 * 00324 * RETURNS 00325 * Success: Name of the first attribute. 00326 * Failure: NULL 00327 * 00328 * NOTES 00329 * Use ldap_memfree to free the returned string. 00330 */ 00331 PWCHAR CDECL ldap_first_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry, 00332 WLDAP32_BerElement** ptr ) 00333 { 00334 PWCHAR ret = NULL; 00335 #ifdef HAVE_LDAP 00336 char *retU; 00337 00338 TRACE( "(%p, %p, %p)\n", ld, entry, ptr ); 00339 00340 if (!ld || !entry) return NULL; 00341 retU = ldap_first_attribute( ld, entry, ptr ); 00342 00343 ret = strUtoW( retU ); 00344 ldap_memfree( retU ); 00345 00346 #endif 00347 return ret; 00348 } 00349 00350 /*********************************************************************** 00351 * ldap_first_entry (WLDAP32.@) 00352 * 00353 * Get the first entry from a result message. 00354 * 00355 * PARAMS 00356 * ld [I] Pointer to an LDAP context. 00357 * res [I] Search result message. 00358 * 00359 * RETURNS 00360 * Success: The first entry. 00361 * Failure: NULL 00362 * 00363 * NOTES 00364 * The returned entry will be freed when the message is freed. 00365 */ 00366 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res ) 00367 { 00368 #ifdef HAVE_LDAP 00369 00370 TRACE( "(%p, %p)\n", ld, res ); 00371 00372 if (!ld || !res) return NULL; 00373 return ldap_first_entry( ld, res ); 00374 00375 #else 00376 return NULL; 00377 #endif 00378 } 00379 00380 /*********************************************************************** 00381 * ldap_first_reference (WLDAP32.@) 00382 * 00383 * Get the first reference from a result message. 00384 * 00385 * PARAMS 00386 * ld [I] Pointer to an LDAP context. 00387 * res [I] Search result message. 00388 * 00389 * RETURNS 00390 * Success: The first reference. 00391 * Failure: NULL 00392 */ 00393 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res ) 00394 { 00395 #ifdef HAVE_LDAP_FIRST_REFERENCE 00396 00397 TRACE( "(%p, %p)\n", ld, res ); 00398 00399 if (!ld) return NULL; 00400 return ldap_first_reference( ld, res ); 00401 00402 #else 00403 return NULL; 00404 #endif 00405 } 00406 00407 /*********************************************************************** 00408 * ldap_memfreeA (WLDAP32.@) 00409 * 00410 * See ldap_memfreeW. 00411 */ 00412 void CDECL ldap_memfreeA( PCHAR block ) 00413 { 00414 TRACE( "(%p)\n", block ); 00415 strfreeA( block ); 00416 } 00417 00418 /*********************************************************************** 00419 * ldap_memfreeW (WLDAP32.@) 00420 * 00421 * Free a block of memory. 00422 * 00423 * PARAMS 00424 * block [I] Pointer to memory block to be freed. 00425 */ 00426 void CDECL ldap_memfreeW( PWCHAR block ) 00427 { 00428 TRACE( "(%p)\n", block ); 00429 strfreeW( block ); 00430 } 00431 00432 /*********************************************************************** 00433 * ldap_msgfree (WLDAP32.@) 00434 * 00435 * Free a message. 00436 * 00437 * PARAMS 00438 * res [I] Message to be freed. 00439 */ 00440 ULONG CDECL WLDAP32_ldap_msgfree( WLDAP32_LDAPMessage *res ) 00441 { 00442 ULONG ret = WLDAP32_LDAP_SUCCESS; 00443 #ifdef HAVE_LDAP 00444 00445 TRACE( "(%p)\n", res ); 00446 ldap_msgfree( res ); 00447 00448 #endif 00449 return ret; 00450 } 00451 00452 /*********************************************************************** 00453 * ldap_next_attributeA (WLDAP32.@) 00454 * 00455 * See ldap_next_attributeW. 00456 */ 00457 PCHAR CDECL ldap_next_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry, 00458 WLDAP32_BerElement *ptr ) 00459 { 00460 PCHAR ret = NULL; 00461 #ifdef HAVE_LDAP 00462 WCHAR *retW; 00463 00464 TRACE( "(%p, %p, %p)\n", ld, entry, ptr ); 00465 00466 if (!ld || !entry || !ptr) return NULL; 00467 retW = ldap_next_attributeW( ld, entry, ptr ); 00468 00469 ret = strWtoA( retW ); 00470 ldap_memfreeW( retW ); 00471 00472 #endif 00473 return ret; 00474 } 00475 00476 /*********************************************************************** 00477 * ldap_next_attributeW (WLDAP32.@) 00478 * 00479 * Get the next attribute for a given entry. 00480 * 00481 * PARAMS 00482 * ld [I] Pointer to an LDAP context. 00483 * entry [I] Entry to retrieve attribute for. 00484 * ptr [I/O] Position pointer. 00485 * 00486 * RETURNS 00487 * Success: The name of the next attribute. 00488 * Failure: NULL 00489 * 00490 * NOTES 00491 * Free the returned string after each iteration with ldap_memfree. 00492 * When done iterating and when ptr != NULL, call ber_free( ptr, 0 ). 00493 */ 00494 PWCHAR CDECL ldap_next_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry, 00495 WLDAP32_BerElement *ptr ) 00496 { 00497 PWCHAR ret = NULL; 00498 #ifdef HAVE_LDAP 00499 char *retU; 00500 00501 TRACE( "(%p, %p, %p)\n", ld, entry, ptr ); 00502 00503 if (!ld || !entry || !ptr) return NULL; 00504 retU = ldap_next_attribute( ld, entry, ptr ); 00505 00506 ret = strUtoW( retU ); 00507 ldap_memfree( retU ); 00508 00509 #endif 00510 return ret; 00511 } 00512 00513 /*********************************************************************** 00514 * ldap_next_entry (WLDAP32.@) 00515 * 00516 * Get the next entry from a result message. 00517 * 00518 * PARAMS 00519 * ld [I] Pointer to an LDAP context. 00520 * entry [I] Entry returned by a previous call. 00521 * 00522 * RETURNS 00523 * Success: The next entry. 00524 * Failure: NULL 00525 * 00526 * NOTES 00527 * The returned entry will be freed when the message is freed. 00528 */ 00529 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry ) 00530 { 00531 #ifdef HAVE_LDAP 00532 00533 TRACE( "(%p, %p)\n", ld, entry ); 00534 00535 if (!ld || !entry) return NULL; 00536 return ldap_next_entry( ld, entry ); 00537 00538 #else 00539 return NULL; 00540 #endif 00541 } 00542 00543 /*********************************************************************** 00544 * ldap_next_reference (WLDAP32.@) 00545 * 00546 * Get the next reference from a result message. 00547 * 00548 * PARAMS 00549 * ld [I] Pointer to an LDAP context. 00550 * entry [I] Entry returned by a previous call. 00551 * 00552 * RETURNS 00553 * Success: The next reference. 00554 * Failure: NULL 00555 * 00556 * NOTES 00557 * The returned entry will be freed when the message is freed. 00558 */ 00559 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry ) 00560 { 00561 #ifdef HAVE_LDAP_NEXT_REFERENCE 00562 00563 TRACE( "(%p, %p)\n", ld, entry ); 00564 00565 if (!ld || !entry) return NULL; 00566 return ldap_next_reference( ld, entry ); 00567 00568 #else 00569 return NULL; 00570 #endif 00571 } 00572 00573 /*********************************************************************** 00574 * ldap_result (WLDAP32.@) 00575 * 00576 * Get the result of an asynchronous operation. 00577 * 00578 * PARAMS 00579 * ld [I] Pointer to an LDAP context. 00580 * msgid [I] Message ID of the operation. 00581 * all [I] How many results should be returned? 00582 * timeout [I] How long to wait for the results? 00583 * res [O] Result message for the operation. 00584 * 00585 * RETURNS 00586 * Success: One of the following values: 00587 * 00588 * LDAP_RES_ADD 00589 * LDAP_RES_BIND 00590 * LDAP_RES_COMPARE 00591 * LDAP_RES_DELETE 00592 * LDAP_RES_EXTENDED 00593 * LDAP_RES_MODIFY 00594 * LDAP_RES_MODRDN 00595 * LDAP_RES_REFERRAL 00596 * LDAP_RES_SEARCH_ENTRY 00597 * LDAP_RES_SEARCH_RESULT 00598 * 00599 * Failure: ~0u 00600 * 00601 * This function returns 0 when the timeout has expired. 00602 * 00603 * NOTES 00604 * A NULL timeout pointer causes the function to block waiting 00605 * for results to arrive. A timeout value of 0 causes the function 00606 * to immediately return any available results. Free returned results 00607 * with ldap_msgfree. 00608 */ 00609 ULONG CDECL WLDAP32_ldap_result( WLDAP32_LDAP *ld, ULONG msgid, ULONG all, 00610 struct l_timeval *timeout, WLDAP32_LDAPMessage **res ) 00611 { 00612 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED; 00613 #ifdef HAVE_LDAP 00614 00615 TRACE( "(%p, 0x%08x, 0x%08x, %p, %p)\n", ld, msgid, all, timeout, res ); 00616 00617 if (!ld || !res || msgid == ~0u) return ~0u; 00618 ret = ldap_result( ld, msgid, all, (struct timeval *)timeout, res ); 00619 00620 #endif 00621 return ret; 00622 } 00623 00624 /*********************************************************************** 00625 * LdapUnicodeToUTF8 (WLDAP32.@) 00626 * 00627 * Convert a wide character string to a UTF8 string. 00628 * 00629 * PARAMS 00630 * src [I] Wide character string to convert. 00631 * srclen [I] Size of string to convert, in characters. 00632 * dst [O] Pointer to a buffer that receives the converted string. 00633 * dstlen [I] Size of the destination buffer in characters. 00634 * 00635 * RETURNS 00636 * The number of characters written into the destination buffer. 00637 * 00638 * NOTES 00639 * Set dstlen to zero to ask for the required buffer size. 00640 */ 00641 int CDECL LdapUnicodeToUTF8( LPCWSTR src, int srclen, LPSTR dst, int dstlen ) 00642 { 00643 return WideCharToMultiByte( CP_UTF8, 0, src, srclen, dst, dstlen, NULL, NULL ); 00644 } 00645 00646 /*********************************************************************** 00647 * LdapUTF8ToUnicode (WLDAP32.@) 00648 * 00649 * Convert a UTF8 string to a wide character string. 00650 * 00651 * PARAMS 00652 * src [I] UTF8 string to convert. 00653 * srclen [I] Size of string to convert, in characters. 00654 * dst [O] Pointer to a buffer that receives the converted string. 00655 * dstlen [I] Size of the destination buffer in characters. 00656 * 00657 * RETURNS 00658 * The number of characters written into the destination buffer. 00659 * 00660 * NOTES 00661 * Set dstlen to zero to ask for the required buffer size. 00662 */ 00663 int CDECL LdapUTF8ToUnicode( LPCSTR src, int srclen, LPWSTR dst, int dstlen ) 00664 { 00665 return MultiByteToWideChar( CP_UTF8, 0, src, srclen, dst, dstlen ); 00666 } Generated on Sun May 27 2012 04:16:47 for ReactOS by
1.7.6.1
|