[NETAPI32] Implement DsGetSiteNameA
[reactos.git] / dll / win32 / netapi32 / misc.c
1 /*
2 * PROJECT: NetAPI DLL
3 * LICENSE: GPL-2.0 (https://spdx.org/licenses/GPL-2.0)
4 * PURPOSE: Miscellaneous functions
5 * COPYRIGHT: Copyright 2017 Eric Kohl (eric.kohl@reactos.org)
6 */
7
8 /* INCLUDES ******************************************************************/
9
10 #include "netapi32.h"
11
12 #include <rpc.h>
13 #include "srvsvc_c.h"
14 #include "wkssvc_c.h"
15
16
17 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
18
19 /* FUNCTIONS *****************************************************************/
20
21 NET_API_STATUS
22 WINAPI
23 NetRegisterDomainNameChangeNotification(
24 _Out_ PHANDLE NotificationEventHandle)
25 {
26 HANDLE EventHandle;
27 NTSTATUS Status;
28
29 TRACE("NetRegisterDomainNameChangeNotification(%p)\n",
30 NotificationEventHandle);
31
32 if (NotificationEventHandle == NULL)
33 return ERROR_INVALID_PARAMETER;
34
35 EventHandle = CreateEventW(NULL, FALSE, FALSE, NULL);
36 if (EventHandle == NULL)
37 return GetLastError();
38
39 Status = LsaRegisterPolicyChangeNotification(PolicyNotifyDnsDomainInformation,
40 NotificationEventHandle);
41 if (!NT_SUCCESS(Status))
42 {
43 CloseHandle(EventHandle);
44 return NetpNtStatusToApiStatus(Status);
45 }
46
47 *NotificationEventHandle = EventHandle;
48
49 return NERR_Success;
50 }
51
52
53 NET_API_STATUS
54 WINAPI
55 NetStatisticsGet(
56 _In_ LPWSTR server,
57 _In_ LPWSTR service,
58 _In_ DWORD level,
59 _In_ DWORD options,
60 _Out_ LPBYTE *bufptr)
61 {
62 NET_API_STATUS status = ERROR_NOT_SUPPORTED;
63
64 TRACE("NetStatisticsGet(%s %s %lu %lu %p)\n",
65 debugstr_w(server), debugstr_w(service), level, options, bufptr);
66
67 *bufptr = NULL;
68
69 if (_wcsicmp(service, L"LanmanWorkstation") == 0)
70 {
71 if (level != 0)
72 return ERROR_INVALID_LEVEL;
73
74 if (options != 0)
75 return ERROR_INVALID_PARAMETER;
76
77 RpcTryExcept
78 {
79 status = NetrWorkstationStatisticsGet(server,
80 L"LanmanWorkstation",
81 level,
82 options,
83 (LPSTAT_WORKSTATION_0*)bufptr);
84 }
85 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
86 {
87 status = I_RpcMapWin32Status(RpcExceptionCode());
88 }
89 RpcEndExcept;
90 }
91 else if (_wcsicmp(service, L"LanmanServer") == 0)
92 {
93 if (level != 0)
94 return ERROR_INVALID_LEVEL;
95
96 if (options != 0)
97 return ERROR_INVALID_PARAMETER;
98
99 RpcTryExcept
100 {
101 status = NetrServerStatisticsGet(server,
102 L"LanmanServer",
103 level,
104 options,
105 (LPSTAT_SERVER_0 *)bufptr);
106 }
107 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
108 {
109 status = I_RpcMapWin32Status(RpcExceptionCode());
110 }
111 RpcEndExcept;
112 }
113
114 return status;
115 }
116
117
118 NET_API_STATUS
119 WINAPI
120 NetUnregisterDomainNameChangeNotification(
121 _In_ HANDLE NotificationEventHandle)
122 {
123 NTSTATUS Status;
124
125 TRACE("NetUnregisterDomainNameChangeNotification(%p)\n",
126 NotificationEventHandle);
127
128 if (NotificationEventHandle == NULL)
129 return ERROR_INVALID_PARAMETER;
130
131 Status = LsaUnregisterPolicyChangeNotification(PolicyNotifyDnsDomainInformation,
132 NotificationEventHandle);
133
134 return NetpNtStatusToApiStatus(Status);
135 }
136
137
138 PSTR
139 WINAPI
140 NetpAllocAnsiStrFromWStr(
141 _In_ PWSTR InString)
142 {
143 UNICODE_STRING UnicodeString;
144 ANSI_STRING AnsiString;
145 ULONG Size;
146 NET_API_STATUS NetStatus;
147 NTSTATUS Status;
148
149 RtlInitUnicodeString(&UnicodeString, InString);
150
151 Size = RtlUnicodeStringToAnsiSize(&UnicodeString);
152 NetStatus = NetApiBufferAllocate(Size,
153 (PVOID*)&AnsiString.Buffer);
154 if (NetStatus != NERR_Success)
155 return NULL;
156
157 Status = RtlUnicodeStringToAnsiString(&AnsiString,
158 &UnicodeString,
159 FALSE);
160 if (!NT_SUCCESS(Status))
161 {
162 NetApiBufferFree(AnsiString.Buffer);
163 return NULL;
164 }
165
166 return AnsiString.Buffer;
167 }
168
169
170 PWSTR
171 WINAPI
172 NetpAllocWStrFromAnsiStr(
173 _In_ PSTR InString)
174 {
175 ANSI_STRING AnsiString;
176 UNICODE_STRING UnicodeString;
177 ULONG Size;
178 NET_API_STATUS NetStatus;
179 NTSTATUS Status;
180
181 RtlInitAnsiString(&AnsiString, InString);
182
183 Size = RtlAnsiStringToUnicodeSize(&AnsiString);
184 NetStatus = NetApiBufferAllocate(Size,
185 (PVOID*)&UnicodeString.Buffer);
186 if (NetStatus != NERR_Success)
187 return NULL;
188
189 Status = RtlAnsiStringToUnicodeString(&UnicodeString,
190 &AnsiString,
191 FALSE);
192 if (!NT_SUCCESS(Status))
193 {
194 NetApiBufferFree(UnicodeString.Buffer);
195 return NULL;
196 }
197
198 return UnicodeString.Buffer;
199 }
200
201
202 PWSTR
203 WINAPI
204 NetpAllocWStrFromStr(
205 _In_ PSTR InString)
206 {
207 OEM_STRING OemString;
208 UNICODE_STRING UnicodeString;
209 ULONG Size;
210 NET_API_STATUS NetStatus;
211 NTSTATUS Status;
212
213 RtlInitAnsiString((PANSI_STRING)&OemString, InString);
214
215 Size = RtlOemStringToUnicodeSize(&OemString);
216 NetStatus = NetApiBufferAllocate(Size,
217 (PVOID*)&UnicodeString.Buffer);
218 if (NetStatus != NERR_Success)
219 return NULL;
220
221 Status = RtlOemStringToUnicodeString(&UnicodeString,
222 &OemString,
223 FALSE);
224 if (!NT_SUCCESS(Status))
225 {
226 NetApiBufferFree(UnicodeString.Buffer);
227 return NULL;
228 }
229
230 return UnicodeString.Buffer;
231 }
232
233
234 PWSTR
235 WINAPI
236 NetpAllocWStrFromWStr(
237 _In_ PWSTR InString)
238 {
239 PWSTR OutString;
240 ULONG Size;
241 NET_API_STATUS Status;
242
243 Size = (wcslen(InString) + 1) * sizeof(WCHAR);
244 Status = NetApiBufferAllocate(Size,
245 (PVOID*)&OutString);
246 if (Status != NERR_Success)
247 return NULL;
248
249 wcscpy(OutString, InString);
250
251 return OutString;
252 }
253
254
255 NET_API_STATUS
256 WINAPI
257 NetpNtStatusToApiStatus(
258 _In_ NTSTATUS Status)
259 {
260 NET_API_STATUS ApiStatus;
261
262 switch (Status)
263 {
264 case STATUS_SUCCESS:
265 ApiStatus = NERR_Success;
266 break;
267
268 case STATUS_INVALID_ACCOUNT_NAME:
269 ApiStatus = NERR_BadUsername;
270 break;
271
272 case STATUS_PASSWORD_RESTRICTION:
273 ApiStatus = NERR_PasswordTooShort;
274 break;
275
276 default:
277 ApiStatus = RtlNtStatusToDosError(Status);
278 break;
279 }
280
281 return ApiStatus;
282 }
283
284 /* EOF */