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