xref: /reactos/dll/win32/netapi32/misc.c (revision 9d3c3a75)
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 PSTR
171 WINAPI
172 NetpAllocStrFromWStr(
173     _In_ PWSTR InString)
174 {
175     UNICODE_STRING UnicodeString;
176     OEM_STRING OemString;
177     ULONG Size;
178     NET_API_STATUS NetStatus;
179     NTSTATUS Status;
180 
181     RtlInitUnicodeString(&UnicodeString, InString);
182 
183     Size = RtlUnicodeStringToOemSize(&UnicodeString);
184     NetStatus = NetApiBufferAllocate(Size,
185                                      (PVOID*)&OemString.Buffer);
186     if (NetStatus != NERR_Success)
187         return NULL;
188 
189     Status = RtlUnicodeStringToOemString(&OemString,
190                                          &UnicodeString,
191                                          FALSE);
192     if (!NT_SUCCESS(Status))
193     {
194         NetApiBufferFree(OemString.Buffer);
195         return NULL;
196     }
197 
198     return OemString.Buffer;
199 }
200 
201 
202 PWSTR
203 WINAPI
204 NetpAllocWStrFromAnsiStr(
205     _In_ PSTR InString)
206 {
207     ANSI_STRING AnsiString;
208     UNICODE_STRING UnicodeString;
209     ULONG Size;
210     NET_API_STATUS NetStatus;
211     NTSTATUS Status;
212 
213     RtlInitAnsiString(&AnsiString, InString);
214 
215     Size = RtlAnsiStringToUnicodeSize(&AnsiString);
216     NetStatus = NetApiBufferAllocate(Size,
217                                      (PVOID*)&UnicodeString.Buffer);
218     if (NetStatus != NERR_Success)
219         return NULL;
220 
221     Status = RtlAnsiStringToUnicodeString(&UnicodeString,
222                                           &AnsiString,
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 NetpAllocWStrFromStr(
237     _In_ PSTR InString)
238 {
239     OEM_STRING OemString;
240     UNICODE_STRING UnicodeString;
241     ULONG Size;
242     NET_API_STATUS NetStatus;
243     NTSTATUS Status;
244 
245     RtlInitAnsiString((PANSI_STRING)&OemString, InString);
246 
247     Size = RtlOemStringToUnicodeSize(&OemString);
248     NetStatus = NetApiBufferAllocate(Size,
249                                      (PVOID*)&UnicodeString.Buffer);
250     if (NetStatus != NERR_Success)
251         return NULL;
252 
253     Status = RtlOemStringToUnicodeString(&UnicodeString,
254                                          &OemString,
255                                          FALSE);
256     if (!NT_SUCCESS(Status))
257     {
258         NetApiBufferFree(UnicodeString.Buffer);
259         return NULL;
260     }
261 
262     return UnicodeString.Buffer;
263 }
264 
265 
266 PWSTR
267 WINAPI
268 NetpAllocWStrFromWStr(
269     _In_ PWSTR InString)
270 {
271     PWSTR OutString;
272     ULONG Size;
273     NET_API_STATUS Status;
274 
275     Size = (wcslen(InString) + 1) * sizeof(WCHAR);
276     Status = NetApiBufferAllocate(Size,
277                                   (PVOID*)&OutString);
278     if (Status != NERR_Success)
279         return NULL;
280 
281     wcscpy(OutString, InString);
282 
283     return OutString;
284 }
285 
286 
287 NET_API_STATUS
288 WINAPI
289 NetpNtStatusToApiStatus(
290     _In_ NTSTATUS Status)
291 {
292     NET_API_STATUS ApiStatus;
293 
294     switch (Status)
295     {
296         case STATUS_SUCCESS:
297             ApiStatus = NERR_Success;
298             break;
299 
300         case STATUS_INVALID_ACCOUNT_NAME:
301             ApiStatus = NERR_BadUsername;
302             break;
303 
304         case STATUS_USER_EXISTS:
305             ApiStatus = NERR_UserExists;
306             break;
307 
308         case STATUS_GROUP_EXISTS:
309             ApiStatus = NERR_GroupExists;
310             break;
311 
312         case STATUS_PASSWORD_RESTRICTION:
313             ApiStatus = NERR_PasswordTooShort;
314             break;
315 
316         default:
317             ApiStatus = RtlNtStatusToDosError(Status);
318             break;
319     }
320 
321     return ApiStatus;
322 }
323 
324 /* EOF */
325