[SAMLIB]
[reactos.git] / reactos / dll / win32 / samlib / samlib.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2004 ReactOS Team
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 /* $Id$
20 *
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS system libraries
23 * PURPOSE: SAM interface library
24 * FILE: lib/samlib/samlib.c
25 * PROGRAMER: Eric Kohl
26 */
27
28 /* INCLUDES *****************************************************************/
29
30 #define NDEBUG
31 #include "precomp.h"
32
33
34 /* GLOBALS *******************************************************************/
35
36
37 /* FUNCTIONS *****************************************************************/
38
39
40 static BOOL
41 CreateBuiltinAliases (HKEY hAliasesKey)
42 {
43 return TRUE;
44 }
45
46
47 static BOOL
48 CreateBuiltinGroups (HKEY hGroupsKey)
49 {
50 return TRUE;
51 }
52
53
54 static BOOL
55 CreateBuiltinUsers (HKEY hUsersKey)
56 {
57 return TRUE;
58 }
59
60
61 BOOL WINAPI
62 SamInitializeSAM (VOID)
63 {
64 DWORD dwDisposition;
65 HKEY hSamKey;
66 HKEY hDomainsKey;
67 HKEY hAccountKey;
68 HKEY hBuiltinKey;
69 HKEY hAliasesKey;
70 HKEY hGroupsKey;
71 HKEY hUsersKey;
72
73 DPRINT("SamInitializeSAM() called\n");
74
75 if (RegCreateKeyExW (HKEY_LOCAL_MACHINE,
76 L"SAM\\SAM",
77 0,
78 NULL,
79 REG_OPTION_NON_VOLATILE,
80 KEY_ALL_ACCESS,
81 NULL,
82 &hSamKey,
83 &dwDisposition))
84 {
85 DPRINT1 ("Failed to create 'Sam' key! (Error %lu)\n", GetLastError());
86 return FALSE;
87 }
88
89 if (RegCreateKeyExW (hSamKey,
90 L"Domains",
91 0,
92 NULL,
93 REG_OPTION_NON_VOLATILE,
94 KEY_ALL_ACCESS,
95 NULL,
96 &hDomainsKey,
97 &dwDisposition))
98 {
99 DPRINT1 ("Failed to create 'Domains' key! (Error %lu)\n", GetLastError());
100 RegCloseKey (hSamKey);
101 return FALSE;
102 }
103
104 RegCloseKey (hSamKey);
105
106 /* Create the 'Domains\\Account' key */
107 if (RegCreateKeyExW (hDomainsKey,
108 L"Account",
109 0,
110 NULL,
111 REG_OPTION_NON_VOLATILE,
112 KEY_ALL_ACCESS,
113 NULL,
114 &hAccountKey,
115 &dwDisposition))
116 {
117 DPRINT1 ("Failed to create 'Domains\\Account' key! (Error %lu)\n", GetLastError());
118 RegCloseKey (hDomainsKey);
119 return FALSE;
120 }
121
122
123 /* Create the 'Account\Aliases' key */
124 if (RegCreateKeyExW (hAccountKey,
125 L"Aliases",
126 0,
127 NULL,
128 REG_OPTION_NON_VOLATILE,
129 KEY_ALL_ACCESS,
130 NULL,
131 &hAliasesKey,
132 &dwDisposition))
133 {
134 DPRINT1 ("Failed to create 'Account\\Aliases' key! (Error %lu)\n", GetLastError());
135 RegCloseKey (hAccountKey);
136 RegCloseKey (hDomainsKey);
137 return FALSE;
138 }
139
140 RegCloseKey (hAliasesKey);
141
142
143 /* Create the 'Account\Groups' key */
144 if (RegCreateKeyExW (hAccountKey,
145 L"Groups",
146 0,
147 NULL,
148 REG_OPTION_NON_VOLATILE,
149 KEY_ALL_ACCESS,
150 NULL,
151 &hGroupsKey,
152 &dwDisposition))
153 {
154 DPRINT1 ("Failed to create 'Account\\Groups' key! (Error %lu)\n", GetLastError());
155 RegCloseKey (hAccountKey);
156 RegCloseKey (hDomainsKey);
157 return FALSE;
158 }
159
160 RegCloseKey (hGroupsKey);
161
162
163 /* Create the 'Account\Users' key */
164 if (RegCreateKeyExW (hAccountKey,
165 L"Users",
166 0,
167 NULL,
168 REG_OPTION_NON_VOLATILE,
169 KEY_ALL_ACCESS,
170 NULL,
171 &hUsersKey,
172 &dwDisposition))
173 {
174 DPRINT1 ("Failed to create 'Account\\Users' key! (Error %lu)\n", GetLastError());
175 RegCloseKey (hAccountKey);
176 RegCloseKey (hDomainsKey);
177 return FALSE;
178 }
179
180 RegCloseKey (hUsersKey);
181
182 RegCloseKey (hAccountKey);
183
184
185 /* Create the 'Domains\\Builtin' */
186 if (RegCreateKeyExW (hDomainsKey,
187 L"Builtin",
188 0,
189 NULL,
190 REG_OPTION_NON_VOLATILE,
191 KEY_ALL_ACCESS,
192 NULL,
193 &hBuiltinKey,
194 &dwDisposition))
195 {
196 DPRINT1 ("Failed to create Builtin key! (Error %lu)\n", GetLastError());
197 RegCloseKey (hDomainsKey);
198 return FALSE;
199 }
200
201
202 /* Create the 'Builtin\Aliases' key */
203 if (RegCreateKeyExW (hBuiltinKey,
204 L"Aliases",
205 0,
206 NULL,
207 REG_OPTION_NON_VOLATILE,
208 KEY_ALL_ACCESS,
209 NULL,
210 &hAliasesKey,
211 &dwDisposition))
212 {
213 DPRINT1 ("Failed to create 'Builtin\\Aliases' key! (Error %lu)\n", GetLastError());
214 RegCloseKey (hBuiltinKey);
215 RegCloseKey (hDomainsKey);
216 return FALSE;
217 }
218
219 /* Create builtin aliases */
220 if (!CreateBuiltinAliases (hAliasesKey))
221 {
222 DPRINT1 ("Failed to create builtin aliases!\n");
223 RegCloseKey (hAliasesKey);
224 RegCloseKey (hBuiltinKey);
225 RegCloseKey (hDomainsKey);
226 return FALSE;
227 }
228
229 RegCloseKey (hAliasesKey);
230
231
232 /* Create the 'Builtin\Groups' key */
233 if (RegCreateKeyExW (hBuiltinKey,
234 L"Groups",
235 0,
236 NULL,
237 REG_OPTION_NON_VOLATILE,
238 KEY_ALL_ACCESS,
239 NULL,
240 &hGroupsKey,
241 &dwDisposition))
242 {
243 DPRINT1 ("Failed to create 'Builtin\\Groups' key! (Error %lu)\n", GetLastError());
244 RegCloseKey (hBuiltinKey);
245 RegCloseKey (hDomainsKey);
246 return FALSE;
247 }
248
249 /* Create builtin groups */
250 if (!CreateBuiltinGroups (hGroupsKey))
251 {
252 DPRINT1 ("Failed to create builtin groups!\n");
253 RegCloseKey (hGroupsKey);
254 RegCloseKey (hBuiltinKey);
255 RegCloseKey (hDomainsKey);
256 return FALSE;
257 }
258
259 RegCloseKey (hGroupsKey);
260
261
262 /* Create the 'Builtin\Users' key */
263 if (RegCreateKeyExW (hBuiltinKey,
264 L"Users",
265 0,
266 NULL,
267 REG_OPTION_NON_VOLATILE,
268 KEY_ALL_ACCESS,
269 NULL,
270 &hUsersKey,
271 &dwDisposition))
272 {
273 DPRINT1 ("Failed to create 'Builtin\\Users' key! (Error %lu)\n", GetLastError());
274 RegCloseKey (hBuiltinKey);
275 RegCloseKey (hDomainsKey);
276 return FALSE;
277 }
278
279 /* Create builtin users */
280 if (!CreateBuiltinUsers (hUsersKey))
281 {
282 DPRINT1 ("Failed to create builtin users!\n");
283 RegCloseKey (hUsersKey);
284 RegCloseKey (hBuiltinKey);
285 RegCloseKey (hDomainsKey);
286 return FALSE;
287 }
288
289 RegCloseKey (hUsersKey);
290
291 RegCloseKey (hBuiltinKey);
292
293 RegCloseKey (hDomainsKey);
294
295 DPRINT ("SamInitializeSAM() done\n");
296
297 return TRUE;
298 }
299
300
301 BOOL WINAPI
302 SamGetDomainSid (PSID *Sid)
303 {
304 DPRINT ("SamGetDomainSid() called\n");
305
306 return FALSE;
307 }
308
309
310 BOOL WINAPI
311 SamSetDomainSid (PSID Sid)
312 {
313 HKEY hAccountKey;
314
315 DPRINT ("SamSetDomainSid() called\n");
316
317 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
318 L"SAM\\SAM\\Domains\\Account",
319 0,
320 KEY_ALL_ACCESS,
321 &hAccountKey))
322 {
323 DPRINT1 ("Failed to open the Account key! (Error %lu)\n", GetLastError());
324 return FALSE;
325 }
326
327 if (RegSetValueExW (hAccountKey,
328 L"Sid",
329 0,
330 REG_BINARY,
331 (LPBYTE)Sid,
332 RtlLengthSid (Sid)))
333 {
334 DPRINT1 ("Failed to set Domain-SID value! (Error %lu)\n", GetLastError());
335 RegCloseKey (hAccountKey);
336 return FALSE;
337 }
338
339 RegCloseKey (hAccountKey);
340
341 DPRINT ("SamSetDomainSid() called\n");
342
343 return TRUE;
344 }
345
346
347 /*
348 * ERROR_USER_EXISTS
349 */
350 BOOL WINAPI
351 SamCreateUser (PWSTR UserName,
352 PWSTR UserPassword,
353 PSID UserSid)
354 {
355 DWORD dwDisposition;
356 HKEY hUsersKey;
357 HKEY hUserKey;
358
359 DPRINT ("SamCreateUser() called\n");
360
361 /* FIXME: Check whether the SID is a real user sid */
362
363 /* Open the Users key */
364 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
365 L"SAM\\SAM\\Domains\\Account\\Users",
366 0,
367 KEY_ALL_ACCESS,
368 &hUsersKey))
369 {
370 DPRINT1 ("Failed to open Account key! (Error %lu)\n", GetLastError());
371 return FALSE;
372 }
373
374 /* Create user name key */
375 if (RegCreateKeyExW (hUsersKey,
376 UserName,
377 0,
378 NULL,
379 REG_OPTION_NON_VOLATILE,
380 KEY_ALL_ACCESS,
381 NULL,
382 &hUserKey,
383 &dwDisposition))
384 {
385 DPRINT1 ("Failed to create/open the user key! (Error %lu)\n", GetLastError());
386 RegCloseKey (hUsersKey);
387 return FALSE;
388 }
389
390 RegCloseKey (hUsersKey);
391
392 if (dwDisposition == REG_OPENED_EXISTING_KEY)
393 {
394 DPRINT1 ("User already exists!\n");
395 RegCloseKey (hUserKey);
396 SetLastError (ERROR_USER_EXISTS);
397 return FALSE;
398 }
399
400
401 /* Set 'Name' value */
402 if (RegSetValueExW (hUserKey,
403 L"Name",
404 0,
405 REG_SZ,
406 (LPBYTE)UserName,
407 (wcslen (UserName) + 1) * sizeof (WCHAR)))
408 {
409 DPRINT1 ("Failed to set the user name value! (Error %lu)\n", GetLastError());
410 RegCloseKey (hUserKey);
411 return FALSE;
412 }
413
414 /* Set 'Password' value */
415 if (RegSetValueExW (hUserKey,
416 L"Password",
417 0,
418 REG_SZ,
419 (LPBYTE)UserPassword,
420 (wcslen (UserPassword) + 1) * sizeof (WCHAR)))
421 {
422 DPRINT1 ("Failed to set the user name value! (Error %lu)\n", GetLastError());
423 RegCloseKey (hUserKey);
424 return FALSE;
425 }
426
427 /* Set 'Sid' value */
428 if (RegSetValueExW (hUserKey,
429 L"Sid",
430 0,
431 REG_BINARY,
432 (LPBYTE)UserSid,
433 RtlLengthSid (UserSid)))
434 {
435 DPRINT1 ("Failed to set the user SID value! (Error %lu)\n", GetLastError());
436 RegCloseKey (hUserKey);
437 return FALSE;
438 }
439
440 RegCloseKey (hUserKey);
441
442 DPRINT ("SamCreateUser() done\n");
443
444 return TRUE;
445 }
446
447
448 /*
449 * ERROR_WRONG_PASSWORD
450 * ERROR_NO_SUCH_USER
451 */
452 BOOL WINAPI
453 SamCheckUserPassword (PWSTR UserName,
454 PWSTR UserPassword)
455 {
456 WCHAR szPassword[256];
457 DWORD dwLength;
458 HKEY hUsersKey;
459 HKEY hUserKey;
460
461 DPRINT ("SamCheckUserPassword() called\n");
462
463 /* Open the Users key */
464 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
465 L"SAM\\SAM\\Domains\\Account\\Users",
466 0,
467 KEY_READ,
468 &hUsersKey))
469 {
470 DPRINT1 ("Failed to open Users key! (Error %lu)\n", GetLastError());
471 return FALSE;
472 }
473
474 /* Open the user key */
475 if (RegOpenKeyExW (hUsersKey,
476 UserName,
477 0,
478 KEY_READ,
479 &hUserKey))
480 {
481 if (GetLastError () == ERROR_FILE_NOT_FOUND)
482 {
483 DPRINT1 ("Invalid user name!\n");
484 SetLastError (ERROR_NO_SUCH_USER);
485 }
486 else
487 {
488 DPRINT1 ("Failed to open user key! (Error %lu)\n", GetLastError());
489 }
490
491 RegCloseKey (hUsersKey);
492 return FALSE;
493 }
494
495 RegCloseKey (hUsersKey);
496
497 /* Get the password */
498 dwLength = 256 * sizeof(WCHAR);
499 if (RegQueryValueExW (hUserKey,
500 L"Password",
501 NULL,
502 NULL,
503 (LPBYTE)szPassword,
504 &dwLength))
505 {
506 DPRINT1 ("Failed to read the password! (Error %lu)\n", GetLastError());
507 RegCloseKey (hUserKey);
508 return FALSE;
509 }
510
511 RegCloseKey (hUserKey);
512
513 /* Compare passwords */
514 if ((wcslen (szPassword) != wcslen (UserPassword)) ||
515 (wcscmp (szPassword, UserPassword) != 0))
516 {
517 DPRINT1 ("Wrong password!\n");
518 SetLastError (ERROR_WRONG_PASSWORD);
519 return FALSE;
520 }
521
522 DPRINT ("SamCheckUserPassword() done\n");
523
524 return TRUE;
525 }
526
527
528 BOOL WINAPI
529 SamGetUserSid (PWSTR UserName,
530 PSID *Sid)
531 {
532 PSID lpSid;
533 DWORD dwLength;
534 HKEY hUsersKey;
535 HKEY hUserKey;
536
537 DPRINT ("SamGetUserSid() called\n");
538
539 if (Sid != NULL)
540 *Sid = NULL;
541
542 /* Open the Users key */
543 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
544 L"SAM\\SAM\\Domains\\Account\\Users",
545 0,
546 KEY_READ,
547 &hUsersKey))
548 {
549 DPRINT1 ("Failed to open Users key! (Error %lu)\n", GetLastError());
550 return FALSE;
551 }
552
553 /* Open the user key */
554 if (RegOpenKeyExW (hUsersKey,
555 UserName,
556 0,
557 KEY_READ,
558 &hUserKey))
559 {
560 if (GetLastError () == ERROR_FILE_NOT_FOUND)
561 {
562 DPRINT1 ("Invalid user name!\n");
563 SetLastError (ERROR_NO_SUCH_USER);
564 }
565 else
566 {
567 DPRINT1 ("Failed to open user key! (Error %lu)\n", GetLastError());
568 }
569
570 RegCloseKey (hUsersKey);
571 return FALSE;
572 }
573
574 RegCloseKey (hUsersKey);
575
576 /* Get SID size */
577 dwLength = 0;
578 if (RegQueryValueExW (hUserKey,
579 L"Sid",
580 NULL,
581 NULL,
582 NULL,
583 &dwLength))
584 {
585 DPRINT1 ("Failed to read the SID size! (Error %lu)\n", GetLastError());
586 RegCloseKey (hUserKey);
587 return FALSE;
588 }
589
590 /* Allocate sid buffer */
591 DPRINT ("Required SID buffer size: %lu\n", dwLength);
592 lpSid = (PSID)RtlAllocateHeap (RtlGetProcessHeap (),
593 0,
594 dwLength);
595 if (lpSid == NULL)
596 {
597 DPRINT1 ("Failed to allocate SID buffer!\n");
598 RegCloseKey (hUserKey);
599 return FALSE;
600 }
601
602 /* Read sid */
603 if (RegQueryValueExW (hUserKey,
604 L"Sid",
605 NULL,
606 NULL,
607 (LPBYTE)lpSid,
608 &dwLength))
609 {
610 DPRINT1 ("Failed to read the SID! (Error %lu)\n", GetLastError());
611 RtlFreeHeap (RtlGetProcessHeap (),
612 0,
613 lpSid);
614 RegCloseKey (hUserKey);
615 return FALSE;
616 }
617
618 RegCloseKey (hUserKey);
619
620 *Sid = lpSid;
621
622 DPRINT ("SamGetUserSid() done\n");
623
624 return TRUE;
625 }
626
627 /* EOF */