3 * Copyright (C) 2004 ReactOS Team
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.
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.
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.
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
28 /* INCLUDES *****************************************************************/
29 #define WIN32_NO_STATUS
32 #define NTOS_MODE_USER
33 #include <ndk/ntndk.h>
39 /* GLOBALS *******************************************************************/
42 /* FUNCTIONS *****************************************************************/
46 CreateBuiltinAliases (HKEY hAliasesKey
)
53 CreateBuiltinGroups (HKEY hGroupsKey
)
60 CreateBuiltinUsers (HKEY hUsersKey
)
67 SamInitializeSAM (VOID
)
78 DPRINT("SamInitializeSAM() called\n");
80 if (RegCreateKeyExW (HKEY_LOCAL_MACHINE
,
84 REG_OPTION_NON_VOLATILE
,
90 DPRINT1 ("Failed to create 'Sam' key! (Error %lu)\n", GetLastError());
94 if (RegCreateKeyExW (hSamKey
,
98 REG_OPTION_NON_VOLATILE
,
104 DPRINT1 ("Failed to create 'Domains' key! (Error %lu)\n", GetLastError());
105 RegCloseKey (hSamKey
);
109 RegCloseKey (hSamKey
);
111 /* Create the 'Domains\\Account' key */
112 if (RegCreateKeyExW (hDomainsKey
,
116 REG_OPTION_NON_VOLATILE
,
122 DPRINT1 ("Failed to create 'Domains\\Account' key! (Error %lu)\n", GetLastError());
123 RegCloseKey (hDomainsKey
);
128 /* Create the 'Account\Aliases' key */
129 if (RegCreateKeyExW (hAccountKey
,
133 REG_OPTION_NON_VOLATILE
,
139 DPRINT1 ("Failed to create 'Account\\Aliases' key! (Error %lu)\n", GetLastError());
140 RegCloseKey (hAccountKey
);
141 RegCloseKey (hDomainsKey
);
145 RegCloseKey (hAliasesKey
);
148 /* Create the 'Account\Groups' key */
149 if (RegCreateKeyExW (hAccountKey
,
153 REG_OPTION_NON_VOLATILE
,
159 DPRINT1 ("Failed to create 'Account\\Groups' key! (Error %lu)\n", GetLastError());
160 RegCloseKey (hAccountKey
);
161 RegCloseKey (hDomainsKey
);
165 RegCloseKey (hGroupsKey
);
168 /* Create the 'Account\Users' key */
169 if (RegCreateKeyExW (hAccountKey
,
173 REG_OPTION_NON_VOLATILE
,
179 DPRINT1 ("Failed to create 'Account\\Users' key! (Error %lu)\n", GetLastError());
180 RegCloseKey (hAccountKey
);
181 RegCloseKey (hDomainsKey
);
185 RegCloseKey (hUsersKey
);
187 RegCloseKey (hAccountKey
);
190 /* Create the 'Domains\\Builtin' */
191 if (RegCreateKeyExW (hDomainsKey
,
195 REG_OPTION_NON_VOLATILE
,
201 DPRINT1 ("Failed to create Builtin key! (Error %lu)\n", GetLastError());
202 RegCloseKey (hDomainsKey
);
207 /* Create the 'Builtin\Aliases' key */
208 if (RegCreateKeyExW (hBuiltinKey
,
212 REG_OPTION_NON_VOLATILE
,
218 DPRINT1 ("Failed to create 'Builtin\\Aliases' key! (Error %lu)\n", GetLastError());
219 RegCloseKey (hBuiltinKey
);
220 RegCloseKey (hDomainsKey
);
224 /* Create builtin aliases */
225 if (!CreateBuiltinAliases (hAliasesKey
))
227 DPRINT1 ("Failed to create builtin aliases!\n");
228 RegCloseKey (hAliasesKey
);
229 RegCloseKey (hBuiltinKey
);
230 RegCloseKey (hDomainsKey
);
234 RegCloseKey (hAliasesKey
);
237 /* Create the 'Builtin\Groups' key */
238 if (RegCreateKeyExW (hBuiltinKey
,
242 REG_OPTION_NON_VOLATILE
,
248 DPRINT1 ("Failed to create 'Builtin\\Groups' key! (Error %lu)\n", GetLastError());
249 RegCloseKey (hBuiltinKey
);
250 RegCloseKey (hDomainsKey
);
254 /* Create builtin groups */
255 if (!CreateBuiltinGroups (hGroupsKey
))
257 DPRINT1 ("Failed to create builtin groups!\n");
258 RegCloseKey (hGroupsKey
);
259 RegCloseKey (hBuiltinKey
);
260 RegCloseKey (hDomainsKey
);
264 RegCloseKey (hGroupsKey
);
267 /* Create the 'Builtin\Users' key */
268 if (RegCreateKeyExW (hBuiltinKey
,
272 REG_OPTION_NON_VOLATILE
,
278 DPRINT1 ("Failed to create 'Builtin\\Users' key! (Error %lu)\n", GetLastError());
279 RegCloseKey (hBuiltinKey
);
280 RegCloseKey (hDomainsKey
);
284 /* Create builtin users */
285 if (!CreateBuiltinUsers (hUsersKey
))
287 DPRINT1 ("Failed to create builtin users!\n");
288 RegCloseKey (hUsersKey
);
289 RegCloseKey (hBuiltinKey
);
290 RegCloseKey (hDomainsKey
);
294 RegCloseKey (hUsersKey
);
296 RegCloseKey (hBuiltinKey
);
298 RegCloseKey (hDomainsKey
);
300 DPRINT ("SamInitializeSAM() done\n");
307 SamGetDomainSid (PSID
*Sid
)
309 DPRINT ("SamGetDomainSid() called\n");
316 SamSetDomainSid (PSID Sid
)
320 DPRINT ("SamSetDomainSid() called\n");
322 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
323 L
"SAM\\SAM\\Domains\\Account",
328 DPRINT1 ("Failed to open the Account key! (Error %lu)\n", GetLastError());
332 if (RegSetValueExW (hAccountKey
,
339 DPRINT1 ("Failed to set Domain-SID value! (Error %lu)\n", GetLastError());
340 RegCloseKey (hAccountKey
);
344 RegCloseKey (hAccountKey
);
346 DPRINT ("SamSetDomainSid() called\n");
356 SamCreateUser (PWSTR UserName
,
364 DPRINT ("SamCreateUser() called\n");
366 /* FIXME: Check whether the SID is a real user sid */
368 /* Open the Users key */
369 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
370 L
"SAM\\SAM\\Domains\\Account\\Users",
375 DPRINT1 ("Failed to open Account key! (Error %lu)\n", GetLastError());
379 /* Create user name key */
380 if (RegCreateKeyExW (hUsersKey
,
384 REG_OPTION_NON_VOLATILE
,
390 DPRINT1 ("Failed to create/open the user key! (Error %lu)\n", GetLastError());
391 RegCloseKey (hUsersKey
);
395 RegCloseKey (hUsersKey
);
397 if (dwDisposition
== REG_OPENED_EXISTING_KEY
)
399 DPRINT1 ("User already exists!\n");
400 RegCloseKey (hUserKey
);
401 SetLastError (ERROR_USER_EXISTS
);
406 /* Set 'Name' value */
407 if (RegSetValueExW (hUserKey
,
412 (wcslen (UserName
) + 1) * sizeof (WCHAR
)))
414 DPRINT1 ("Failed to set the user name value! (Error %lu)\n", GetLastError());
415 RegCloseKey (hUserKey
);
419 /* Set 'Password' value */
420 if (RegSetValueExW (hUserKey
,
424 (LPBYTE
)UserPassword
,
425 (wcslen (UserPassword
) + 1) * sizeof (WCHAR
)))
427 DPRINT1 ("Failed to set the user name value! (Error %lu)\n", GetLastError());
428 RegCloseKey (hUserKey
);
432 /* Set 'Sid' value */
433 if (RegSetValueExW (hUserKey
,
438 RtlLengthSid (UserSid
)))
440 DPRINT1 ("Failed to set the user SID value! (Error %lu)\n", GetLastError());
441 RegCloseKey (hUserKey
);
445 RegCloseKey (hUserKey
);
447 DPRINT ("SamCreateUser() done\n");
454 * ERROR_WRONG_PASSWORD
458 SamCheckUserPassword (PWSTR UserName
,
461 WCHAR szPassword
[256];
466 DPRINT ("SamCheckUserPassword() called\n");
468 /* Open the Users key */
469 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
470 L
"SAM\\SAM\\Domains\\Account\\Users",
475 DPRINT1 ("Failed to open Users key! (Error %lu)\n", GetLastError());
479 /* Open the user key */
480 if (RegOpenKeyExW (hUsersKey
,
486 if (GetLastError () == ERROR_FILE_NOT_FOUND
)
488 DPRINT1 ("Invalid user name!\n");
489 SetLastError (ERROR_NO_SUCH_USER
);
493 DPRINT1 ("Failed to open user key! (Error %lu)\n", GetLastError());
496 RegCloseKey (hUsersKey
);
500 RegCloseKey (hUsersKey
);
502 /* Get the password */
503 dwLength
= 256 * sizeof(WCHAR
);
504 if (RegQueryValueExW (hUserKey
,
511 DPRINT1 ("Failed to read the password! (Error %lu)\n", GetLastError());
512 RegCloseKey (hUserKey
);
516 RegCloseKey (hUserKey
);
518 /* Compare passwords */
519 if ((wcslen (szPassword
) != wcslen (UserPassword
)) ||
520 (wcscmp (szPassword
, UserPassword
) != 0))
522 DPRINT1 ("Wrong password!\n");
523 SetLastError (ERROR_WRONG_PASSWORD
);
527 DPRINT ("SamCheckUserPassword() done\n");
534 SamGetUserSid (PWSTR UserName
,
542 DPRINT ("SamGetUserSid() called\n");
547 /* Open the Users key */
548 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
549 L
"SAM\\SAM\\Domains\\Account\\Users",
554 DPRINT1 ("Failed to open Users key! (Error %lu)\n", GetLastError());
558 /* Open the user key */
559 if (RegOpenKeyExW (hUsersKey
,
565 if (GetLastError () == ERROR_FILE_NOT_FOUND
)
567 DPRINT1 ("Invalid user name!\n");
568 SetLastError (ERROR_NO_SUCH_USER
);
572 DPRINT1 ("Failed to open user key! (Error %lu)\n", GetLastError());
575 RegCloseKey (hUsersKey
);
579 RegCloseKey (hUsersKey
);
583 if (RegQueryValueExW (hUserKey
,
590 DPRINT1 ("Failed to read the SID size! (Error %lu)\n", GetLastError());
591 RegCloseKey (hUserKey
);
595 /* Allocate sid buffer */
596 DPRINT ("Required SID buffer size: %lu\n", dwLength
);
597 lpSid
= (PSID
)RtlAllocateHeap (RtlGetProcessHeap (),
602 DPRINT1 ("Failed to allocate SID buffer!\n");
603 RegCloseKey (hUserKey
);
608 if (RegQueryValueExW (hUserKey
,
615 DPRINT1 ("Failed to read the SID! (Error %lu)\n", GetLastError());
616 RtlFreeHeap (RtlGetProcessHeap (),
619 RegCloseKey (hUserKey
);
623 RegCloseKey (hUserKey
);
627 DPRINT ("SamGetUserSid() done\n");