Fix heap corruptions and memory leaks in dnsapi
[reactos.git] / reactos / dll / win32 / dnsapi / dnsapi / context.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/dnsapi/dnsapi/context.c
5 * PURPOSE: DNSAPI functions built on the ADNS library.
6 * PROGRAMER: Art Yerkes
7 * UPDATE HISTORY:
8 * 12/15/03 -- Created
9 */
10
11 #include "precomp.h"
12
13 #define NDEBUG
14 #include <debug.h>
15
16 /* DnsAcquireContextHandle *************
17 * Create a context handle that will allow us to open and retrieve queries.
18 *
19 * DWORD CredentialsFlags -- TRUE -- Unicode
20 * FALSE -- Ansi or UTF-8?
21 *
22 * PVOID Credentials -- Pointer to a SEC_WINNT_AUTH_IDENTITY
23 * TODO: Use it.
24 *
25 * PHANDLE ContextHandle -- Pointer to a HANDLE that will receive
26 * our context pointer.
27 *
28 * RETURNS:
29 * ERROR_SUCCESS or a failure code.
30 * TODO: Which ones area allowed?
31 */
32
33 extern DNS_STATUS WINAPI DnsAcquireContextHandle_UTF8
34 ( DWORD CredentialsFlags,
35 PVOID Credentials,
36 HANDLE *ContextHandle );
37
38 DNS_STATUS WINAPI DnsAcquireContextHandle_W
39 ( DWORD CredentialsFlags,
40 PVOID Credentials,
41 HANDLE *ContextHandle ) {
42 if( CredentialsFlags ) {
43 PWINDNS_CONTEXT Context;
44 int adns_status;
45
46 /* For now, don't worry about the user's identity. */
47 Context = (PWINDNS_CONTEXT)RtlAllocateHeap( RtlGetProcessHeap(), 0,
48 sizeof( WINDNS_CONTEXT ) );
49
50 if(!Context){
51 *ContextHandle = 0;
52 return ERROR_OUTOFMEMORY;
53 }
54
55 /* The real work here is to create an adns_state that will help us
56 * do what we want to later. */
57 adns_status = adns_init( &Context->State,
58 adns_if_noenv |
59 adns_if_noerrprint |
60 adns_if_noserverwarn,
61 0 );
62 if( adns_status != adns_s_ok ) {
63 *ContextHandle = 0;
64 RtlFreeHeap( RtlGetProcessHeap(), 0, Context );
65 return DnsIntTranslateAdnsToDNS_STATUS( adns_status );
66 } else {
67 *ContextHandle = (HANDLE)Context;
68 return ERROR_SUCCESS;
69 }
70 } else {
71 return DnsAcquireContextHandle_UTF8( CredentialsFlags,
72 Credentials,
73 ContextHandle );
74 }
75 }
76
77 DNS_STATUS WINAPI DnsAcquireContextHandle_UTF8
78 ( DWORD CredentialsFlags,
79 PVOID Credentials,
80 HANDLE *ContextHandle ) {
81 if( CredentialsFlags ) {
82 return DnsAcquireContextHandle_W( CredentialsFlags,
83 Credentials,
84 ContextHandle );
85 } else {
86 /* Convert to unicode, then call the _W version
87 * For now, there is no conversion */
88 DNS_STATUS Status;
89
90 Status = DnsAcquireContextHandle_W( TRUE,
91 Credentials, /* XXX arty */
92 ContextHandle );
93
94 /* Free the unicode credentials when they exist. */
95
96 return Status;
97 }
98 }
99
100 DNS_STATUS WINAPI DnsAcquireContextHandle_A
101 ( DWORD CredentialFlags,
102 PVOID Credentials,
103 HANDLE *ContextHandle ) {
104 if( CredentialFlags ) {
105 return DnsAcquireContextHandle_W( CredentialFlags,
106 Credentials,
107 ContextHandle );
108 } else {
109 return DnsAcquireContextHandle_UTF8( CredentialFlags,
110 Credentials,
111 ContextHandle );
112 }
113 }
114 /* DnsReleaseContextHandle *************
115 * Release a context handle, freeing all resources.
116 */
117
118 void WINAPI DnsReleaseContextHandle
119 ( HANDLE ContextHandle ) {
120 PWINDNS_CONTEXT Context = (PWINDNS_CONTEXT)ContextHandle;
121 adns_finish( Context->State );
122 RtlFreeHeap( RtlGetProcessHeap(), 0, Context );
123 }
124
125