[SAMSRV]
[reactos.git] / reactos / dll / win32 / samsrv / samsrv.c
1 /*
2 * SAM Server DLL
3 * Copyright (C) 2005 Eric Kohl
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 /* INCLUDES *****************************************************************/
21
22 #include "samsrv.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(samsrv);
25
26
27 /* GLOBALS *******************************************************************/
28
29 ENCRYPTED_NT_OWF_PASSWORD EmptyNtHash;
30 ENCRYPTED_LM_OWF_PASSWORD EmptyLmHash;
31 RTL_RESOURCE SampResource;
32
33
34 /* FUNCTIONS *****************************************************************/
35
36 static
37 NTSTATUS
38 SampInitHashes(VOID)
39 {
40 UNICODE_STRING EmptyNtPassword = {0, 0, NULL};
41 CHAR EmptyLmPassword[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
42 NTSTATUS Status;
43
44 /* Calculate the NT hash value of the empty password */
45 Status = SystemFunction007(&EmptyNtPassword,
46 (LPBYTE)&EmptyNtHash);
47 if (!NT_SUCCESS(Status))
48 {
49 ERR("Calculation of the empty NT hash failed (Status 0x%08lx)\n", Status);
50 return Status;
51 }
52
53 /* Calculate the LM hash value of the empty password */
54 Status = SystemFunction006(EmptyLmPassword,
55 (LPSTR)&EmptyLmHash);
56 if (!NT_SUCCESS(Status))
57 {
58 ERR("Calculation of the empty LM hash failed (Status 0x%08lx)\n", Status);
59 }
60
61 return Status;
62 }
63
64
65 NTSTATUS
66 NTAPI
67 SamIConnect(IN PSAMPR_SERVER_NAME ServerName,
68 OUT SAMPR_HANDLE *ServerHandle,
69 IN ACCESS_MASK DesiredAccess,
70 IN BOOLEAN Trusted)
71 {
72 PSAM_DB_OBJECT ServerObject;
73 NTSTATUS Status;
74
75 TRACE("SamIConnect(%p %p %lx %ld)\n",
76 ServerName, ServerHandle, DesiredAccess, Trusted);
77
78 /* Map generic access rights */
79 RtlMapGenericMask(&DesiredAccess,
80 pServerMapping);
81
82 /* Open the Server Object */
83 Status = SampOpenDbObject(NULL,
84 NULL,
85 L"SAM",
86 0,
87 SamDbServerObject,
88 DesiredAccess,
89 &ServerObject);
90 if (NT_SUCCESS(Status))
91 {
92 ServerObject->Trusted = Trusted;
93 *ServerHandle = (SAMPR_HANDLE)ServerObject;
94 }
95
96 TRACE("SamIConnect done (Status 0x%08lx)\n", Status);
97
98 return Status;
99 }
100
101
102 NTSTATUS
103 NTAPI
104 SamIInitialize(VOID)
105 {
106 NTSTATUS Status = STATUS_SUCCESS;
107
108 TRACE("SamIInitialize() called\n");
109
110 Status = SampInitHashes();
111 if (!NT_SUCCESS(Status))
112 return Status;
113
114 if (SampIsSetupRunning())
115 {
116 Status = SampInitializeRegistry();
117 if (!NT_SUCCESS(Status))
118 return Status;
119 }
120
121 RtlInitializeResource(&SampResource);
122
123 /* Initialize the SAM database */
124 Status = SampInitDatabase();
125 if (!NT_SUCCESS(Status))
126 return Status;
127
128 /* Start the RPC server */
129 SampStartRpcServer();
130
131 return Status;
132 }
133
134
135 NTSTATUS
136 NTAPI
137 SampInitializeRegistry(VOID)
138 {
139 TRACE("SampInitializeRegistry() called\n");
140
141 SampInitializeSAM();
142
143 return STATUS_SUCCESS;
144 }
145
146
147 VOID
148 NTAPI
149 SamIFree_SAMPR_ENUMERATION_BUFFER(PSAMPR_ENUMERATION_BUFFER Ptr)
150 {
151 ULONG i;
152
153 if (Ptr != NULL)
154 {
155 if (Ptr->Buffer != NULL)
156 {
157 for (i = 0; i < Ptr->EntriesRead; i++)
158 {
159 if (Ptr->Buffer[i].Name.Buffer != NULL)
160 MIDL_user_free(Ptr->Buffer[i].Name.Buffer);
161 }
162
163 MIDL_user_free(Ptr->Buffer);
164 }
165
166 MIDL_user_free(Ptr);
167 }
168 }
169
170
171 VOID
172 NTAPI
173 SamIFree_SAMPR_GET_GROUPS_BUFFER(PSAMPR_GET_GROUPS_BUFFER Ptr)
174 {
175 if (Ptr != NULL)
176 {
177 if (Ptr->Groups != NULL)
178 MIDL_user_free(Ptr->Groups);
179
180 MIDL_user_free(Ptr);
181 }
182 }
183
184
185 VOID
186 NTAPI
187 SamIFree_SAMPR_GET_MEMBERS_BUFFER(PSAMPR_GET_MEMBERS_BUFFER Ptr)
188 {
189 if (Ptr != NULL)
190 {
191 if (Ptr->Members != NULL)
192 MIDL_user_free(Ptr->Members);
193
194 if (Ptr->Attributes != NULL)
195 MIDL_user_free(Ptr->Attributes);
196
197 MIDL_user_free(Ptr);
198 }
199 }
200
201
202 VOID
203 NTAPI
204 SamIFree_SAMPR_PSID_ARRAY(PSAMPR_PSID_ARRAY Ptr)
205 {
206 if (Ptr != NULL)
207 {
208 if (Ptr->Sids != NULL)
209 {
210 MIDL_user_free(Ptr->Sids);
211 }
212 }
213 }
214
215
216 VOID
217 NTAPI
218 SamIFree_SAMPR_RETURNED_USTRING_ARRAY(PSAMPR_RETURNED_USTRING_ARRAY Ptr)
219 {
220 ULONG i;
221
222 if (Ptr != NULL)
223 {
224 if (Ptr->Element != NULL)
225 {
226 for (i = 0; i < Ptr->Count; i++)
227 {
228 if (Ptr->Element[i].Buffer != NULL)
229 MIDL_user_free(Ptr->Element[i].Buffer);
230 }
231
232 MIDL_user_free(Ptr->Element);
233 Ptr->Element = NULL;
234 Ptr->Count = 0;
235 }
236 }
237 }
238
239
240 VOID
241 NTAPI
242 SamIFree_SAMPR_SR_SECURITY_DESCRIPTOR(PSAMPR_SR_SECURITY_DESCRIPTOR Ptr)
243 {
244 if (Ptr != NULL)
245 {
246 if (Ptr->SecurityDescriptor != NULL)
247 MIDL_user_free(Ptr->SecurityDescriptor);
248
249 MIDL_user_free(Ptr);
250 }
251 }
252
253
254 VOID
255 NTAPI
256 SamIFree_SAMPR_ULONG_ARRAY(PSAMPR_ULONG_ARRAY Ptr)
257 {
258 if (Ptr != NULL)
259 {
260 if (Ptr->Element != NULL)
261 {
262 MIDL_user_free(Ptr->Element);
263 Ptr->Element = NULL;
264 Ptr->Count = 0;
265 }
266 }
267 }
268
269
270 VOID
271 NTAPI
272 SamIFree_SAMPR_USER_INFO_BUFFER(PSAMPR_USER_INFO_BUFFER Ptr,
273 USER_INFORMATION_CLASS InformationClass)
274 {
275 if (Ptr == NULL)
276 return;
277
278 switch (InformationClass)
279 {
280 case UserGeneralInformation:
281 if (Ptr->General.UserName.Buffer != NULL)
282 MIDL_user_free(Ptr->General.UserName.Buffer);
283
284 if (Ptr->General.FullName.Buffer != NULL)
285 MIDL_user_free(Ptr->General.FullName.Buffer);
286
287 if (Ptr->General.AdminComment.Buffer != NULL)
288 MIDL_user_free(Ptr->General.AdminComment.Buffer);
289
290 if (Ptr->General.UserComment.Buffer != NULL)
291 MIDL_user_free(Ptr->General.UserComment.Buffer);
292 break;
293
294 case UserPreferencesInformation:
295 if (Ptr->Preferences.UserComment.Buffer != NULL)
296 MIDL_user_free(Ptr->Preferences.UserComment.Buffer);
297
298 if (Ptr->Preferences.Reserved1.Buffer != NULL)
299 MIDL_user_free(Ptr->Preferences.Reserved1.Buffer);
300 break;
301
302 case UserLogonInformation:
303 if (Ptr->Logon.UserName.Buffer != NULL)
304 MIDL_user_free(Ptr->Logon.UserName.Buffer);
305
306 if (Ptr->Logon.FullName.Buffer != NULL)
307 MIDL_user_free(Ptr->Logon.FullName.Buffer);
308
309 if (Ptr->Logon.HomeDirectory.Buffer != NULL)
310 MIDL_user_free(Ptr->Logon.HomeDirectory.Buffer);
311
312 if (Ptr->Logon.HomeDirectoryDrive.Buffer != NULL)
313 MIDL_user_free(Ptr->Logon.HomeDirectoryDrive.Buffer);
314
315 if (Ptr->Logon.ScriptPath.Buffer != NULL)
316 MIDL_user_free(Ptr->Logon.ScriptPath.Buffer);
317
318 if (Ptr->Logon.ProfilePath.Buffer != NULL)
319 MIDL_user_free(Ptr->Logon.ProfilePath.Buffer);
320
321 if (Ptr->Logon.WorkStations.Buffer != NULL)
322 MIDL_user_free(Ptr->Logon.WorkStations.Buffer);
323
324 if (Ptr->Logon.LogonHours.LogonHours != NULL)
325 MIDL_user_free(Ptr->Logon.LogonHours.LogonHours);
326 break;
327
328 case UserLogonHoursInformation:
329 if (Ptr->LogonHours.LogonHours.LogonHours != NULL)
330 MIDL_user_free(Ptr->LogonHours.LogonHours.LogonHours);
331 break;
332
333 case UserAccountInformation:
334 if (Ptr->Account.UserName.Buffer != NULL)
335 MIDL_user_free(Ptr->Account.UserName.Buffer);
336
337 if (Ptr->Account.FullName.Buffer != NULL)
338 MIDL_user_free(Ptr->Account.FullName.Buffer);
339
340 if (Ptr->Account.HomeDirectory.Buffer != NULL)
341 MIDL_user_free(Ptr->Account.HomeDirectory.Buffer);
342
343 if (Ptr->Account.HomeDirectoryDrive.Buffer != NULL)
344 MIDL_user_free(Ptr->Account.HomeDirectoryDrive.Buffer);
345
346 if (Ptr->Account.ScriptPath.Buffer != NULL)
347 MIDL_user_free(Ptr->Account.ScriptPath.Buffer);
348
349 if (Ptr->Account.ProfilePath.Buffer != NULL)
350 MIDL_user_free(Ptr->Account.ProfilePath.Buffer);
351
352 if (Ptr->Account.AdminComment.Buffer != NULL)
353 MIDL_user_free(Ptr->Account.AdminComment.Buffer);
354
355 if (Ptr->Account.WorkStations.Buffer != NULL)
356 MIDL_user_free(Ptr->Account.WorkStations.Buffer);
357
358 if (Ptr->Account.LogonHours.LogonHours != NULL)
359 MIDL_user_free(Ptr->Account.LogonHours.LogonHours);
360 break;
361
362 case UserNameInformation:
363 if (Ptr->Name.UserName.Buffer != NULL)
364 MIDL_user_free(Ptr->Name.UserName.Buffer);
365
366 if (Ptr->Name.FullName.Buffer != NULL)
367 MIDL_user_free(Ptr->Name.FullName.Buffer);
368 break;
369
370 case UserAccountNameInformation:
371 if (Ptr->AccountName.UserName.Buffer != NULL)
372 MIDL_user_free(Ptr->AccountName.UserName.Buffer);
373 break;
374
375 case UserFullNameInformation:
376 if (Ptr->FullName.FullName.Buffer != NULL)
377 MIDL_user_free(Ptr->FullName.FullName.Buffer);
378 break;
379
380 case UserPrimaryGroupInformation:
381 break;
382
383 case UserHomeInformation:
384 if (Ptr->Home.HomeDirectory.Buffer != NULL)
385 MIDL_user_free(Ptr->Home.HomeDirectory.Buffer);
386
387 if (Ptr->Home.HomeDirectoryDrive.Buffer != NULL)
388 MIDL_user_free(Ptr->Home.HomeDirectoryDrive.Buffer);
389 break;
390
391 case UserScriptInformation:
392 if (Ptr->Script.ScriptPath.Buffer != NULL)
393 MIDL_user_free(Ptr->Script.ScriptPath.Buffer);
394
395 case UserProfileInformation:
396 if (Ptr->Profile.ProfilePath.Buffer != NULL)
397 MIDL_user_free(Ptr->Profile.ProfilePath.Buffer);
398
399 case UserAdminCommentInformation:
400 if (Ptr->AdminComment.AdminComment.Buffer != NULL)
401 MIDL_user_free(Ptr->AdminComment.AdminComment.Buffer);
402 break;
403
404 case UserWorkStationsInformation:
405 if (Ptr->WorkStations.WorkStations.Buffer != NULL)
406 MIDL_user_free(Ptr->WorkStations.WorkStations.Buffer);
407 break;
408
409 case UserSetPasswordInformation:
410 ERR("Information class UserSetPasswordInformation cannot be queried!\n");
411 break;
412
413 case UserControlInformation:
414 break;
415
416 case UserExpiresInformation:
417 break;
418
419 case UserInternal1Information:
420 break;
421
422 case UserInternal2Information:
423 break;
424
425 case UserParametersInformation:
426 if (Ptr->Parameters.Parameters.Buffer != NULL)
427 MIDL_user_free(Ptr->Parameters.Parameters.Buffer);
428 break;
429
430 case UserAllInformation:
431 if (Ptr->All.UserName.Buffer != NULL)
432 MIDL_user_free(Ptr->All.UserName.Buffer);
433
434 if (Ptr->All.FullName.Buffer != NULL)
435 MIDL_user_free(Ptr->All.FullName.Buffer);
436
437 if (Ptr->All.HomeDirectory.Buffer != NULL)
438 MIDL_user_free(Ptr->All.HomeDirectory.Buffer);
439
440 if (Ptr->All.HomeDirectoryDrive.Buffer != NULL)
441 MIDL_user_free(Ptr->All.HomeDirectoryDrive.Buffer);
442
443 if (Ptr->All.ScriptPath.Buffer != NULL)
444 MIDL_user_free(Ptr->All.ScriptPath.Buffer);
445
446 if (Ptr->All.ProfilePath.Buffer != NULL)
447 MIDL_user_free(Ptr->All.ProfilePath.Buffer);
448
449 if (Ptr->All.AdminComment.Buffer != NULL)
450 MIDL_user_free(Ptr->All.AdminComment.Buffer);
451
452 if (Ptr->All.WorkStations.Buffer != NULL)
453 MIDL_user_free(Ptr->All.WorkStations.Buffer);
454
455 if (Ptr->All.UserComment.Buffer != NULL)
456 MIDL_user_free(Ptr->All.UserComment.Buffer);
457
458 if (Ptr->All.Parameters.Buffer != NULL)
459 MIDL_user_free(Ptr->All.Parameters.Buffer);
460
461 if (Ptr->All.LmOwfPassword.Buffer != NULL)
462 MIDL_user_free(Ptr->All.LmOwfPassword.Buffer);
463
464 if (Ptr->All.NtOwfPassword.Buffer != NULL)
465 MIDL_user_free(Ptr->All.NtOwfPassword.Buffer);
466
467 if (Ptr->All.PrivateData.Buffer != NULL)
468 MIDL_user_free(Ptr->All.PrivateData.Buffer);
469
470 if (Ptr->All.SecurityDescriptor.SecurityDescriptor != NULL)
471 MIDL_user_free(Ptr->All.SecurityDescriptor.SecurityDescriptor);
472
473 if (Ptr->All.LogonHours.LogonHours != NULL)
474 MIDL_user_free(Ptr->All.LogonHours.LogonHours);
475 break;
476
477 default:
478 FIXME("Unsupported information class: %lu\n", InformationClass);
479 break;
480 }
481
482 MIDL_user_free(Ptr);
483 }
484
485 /* EOF */