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 *****************************************************************/
34 /* GLOBALS *******************************************************************/
37 /* FUNCTIONS *****************************************************************/
41 CreateBuiltinAliases (HKEY hAliasesKey
)
48 CreateBuiltinGroups (HKEY hGroupsKey
)
55 CreateBuiltinUsers (HKEY hUsersKey
)
62 SamInitializeSAM (VOID
)
73 DPRINT("SamInitializeSAM() called\n");
75 if (RegCreateKeyExW (HKEY_LOCAL_MACHINE
,
79 REG_OPTION_NON_VOLATILE
,
85 DPRINT1 ("Failed to create 'Sam' key! (Error %lu)\n", GetLastError());
89 if (RegCreateKeyExW (hSamKey
,
93 REG_OPTION_NON_VOLATILE
,
99 DPRINT1 ("Failed to create 'Domains' key! (Error %lu)\n", GetLastError());
100 RegCloseKey (hSamKey
);
104 RegCloseKey (hSamKey
);
106 /* Create the 'Domains\\Account' key */
107 if (RegCreateKeyExW (hDomainsKey
,
111 REG_OPTION_NON_VOLATILE
,
117 DPRINT1 ("Failed to create 'Domains\\Account' key! (Error %lu)\n", GetLastError());
118 RegCloseKey (hDomainsKey
);
123 /* Create the 'Account\Aliases' key */
124 if (RegCreateKeyExW (hAccountKey
,
128 REG_OPTION_NON_VOLATILE
,
134 DPRINT1 ("Failed to create 'Account\\Aliases' key! (Error %lu)\n", GetLastError());
135 RegCloseKey (hAccountKey
);
136 RegCloseKey (hDomainsKey
);
140 RegCloseKey (hAliasesKey
);
143 /* Create the 'Account\Groups' key */
144 if (RegCreateKeyExW (hAccountKey
,
148 REG_OPTION_NON_VOLATILE
,
154 DPRINT1 ("Failed to create 'Account\\Groups' key! (Error %lu)\n", GetLastError());
155 RegCloseKey (hAccountKey
);
156 RegCloseKey (hDomainsKey
);
160 RegCloseKey (hGroupsKey
);
163 /* Create the 'Account\Users' key */
164 if (RegCreateKeyExW (hAccountKey
,
168 REG_OPTION_NON_VOLATILE
,
174 DPRINT1 ("Failed to create 'Account\\Users' key! (Error %lu)\n", GetLastError());
175 RegCloseKey (hAccountKey
);
176 RegCloseKey (hDomainsKey
);
180 RegCloseKey (hUsersKey
);
182 RegCloseKey (hAccountKey
);
185 /* Create the 'Domains\\Builtin' */
186 if (RegCreateKeyExW (hDomainsKey
,
190 REG_OPTION_NON_VOLATILE
,
196 DPRINT1 ("Failed to create Builtin key! (Error %lu)\n", GetLastError());
197 RegCloseKey (hDomainsKey
);
202 /* Create the 'Builtin\Aliases' key */
203 if (RegCreateKeyExW (hBuiltinKey
,
207 REG_OPTION_NON_VOLATILE
,
213 DPRINT1 ("Failed to create 'Builtin\\Aliases' key! (Error %lu)\n", GetLastError());
214 RegCloseKey (hBuiltinKey
);
215 RegCloseKey (hDomainsKey
);
219 /* Create builtin aliases */
220 if (!CreateBuiltinAliases (hAliasesKey
))
222 DPRINT1 ("Failed to create builtin aliases!\n");
223 RegCloseKey (hAliasesKey
);
224 RegCloseKey (hBuiltinKey
);
225 RegCloseKey (hDomainsKey
);
229 RegCloseKey (hAliasesKey
);
232 /* Create the 'Builtin\Groups' key */
233 if (RegCreateKeyExW (hBuiltinKey
,
237 REG_OPTION_NON_VOLATILE
,
243 DPRINT1 ("Failed to create 'Builtin\\Groups' key! (Error %lu)\n", GetLastError());
244 RegCloseKey (hBuiltinKey
);
245 RegCloseKey (hDomainsKey
);
249 /* Create builtin groups */
250 if (!CreateBuiltinGroups (hGroupsKey
))
252 DPRINT1 ("Failed to create builtin groups!\n");
253 RegCloseKey (hGroupsKey
);
254 RegCloseKey (hBuiltinKey
);
255 RegCloseKey (hDomainsKey
);
259 RegCloseKey (hGroupsKey
);
262 /* Create the 'Builtin\Users' key */
263 if (RegCreateKeyExW (hBuiltinKey
,
267 REG_OPTION_NON_VOLATILE
,
273 DPRINT1 ("Failed to create 'Builtin\\Users' key! (Error %lu)\n", GetLastError());
274 RegCloseKey (hBuiltinKey
);
275 RegCloseKey (hDomainsKey
);
279 /* Create builtin users */
280 if (!CreateBuiltinUsers (hUsersKey
))
282 DPRINT1 ("Failed to create builtin users!\n");
283 RegCloseKey (hUsersKey
);
284 RegCloseKey (hBuiltinKey
);
285 RegCloseKey (hDomainsKey
);
289 RegCloseKey (hUsersKey
);
291 RegCloseKey (hBuiltinKey
);
293 RegCloseKey (hDomainsKey
);
295 DPRINT ("SamInitializeSAM() done\n");
302 SamGetDomainSid (PSID
*Sid
)
304 DPRINT ("SamGetDomainSid() called\n");
311 SamSetDomainSid (PSID Sid
)
315 DPRINT ("SamSetDomainSid() called\n");
317 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
318 L
"SAM\\SAM\\Domains\\Account",
323 DPRINT1 ("Failed to open the Account key! (Error %lu)\n", GetLastError());
327 if (RegSetValueExW (hAccountKey
,
334 DPRINT1 ("Failed to set Domain-SID value! (Error %lu)\n", GetLastError());
335 RegCloseKey (hAccountKey
);
339 RegCloseKey (hAccountKey
);
341 DPRINT ("SamSetDomainSid() called\n");
351 SamCreateUser (PWSTR UserName
,
359 DPRINT ("SamCreateUser() called\n");
361 /* FIXME: Check whether the SID is a real user sid */
363 /* Open the Users key */
364 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
365 L
"SAM\\SAM\\Domains\\Account\\Users",
370 DPRINT1 ("Failed to open Account key! (Error %lu)\n", GetLastError());
374 /* Create user name key */
375 if (RegCreateKeyExW (hUsersKey
,
379 REG_OPTION_NON_VOLATILE
,
385 DPRINT1 ("Failed to create/open the user key! (Error %lu)\n", GetLastError());
386 RegCloseKey (hUsersKey
);
390 RegCloseKey (hUsersKey
);
392 if (dwDisposition
== REG_OPENED_EXISTING_KEY
)
394 DPRINT1 ("User already exists!\n");
395 RegCloseKey (hUserKey
);
396 SetLastError (ERROR_USER_EXISTS
);
401 /* Set 'Name' value */
402 if (RegSetValueExW (hUserKey
,
407 (wcslen (UserName
) + 1) * sizeof (WCHAR
)))
409 DPRINT1 ("Failed to set the user name value! (Error %lu)\n", GetLastError());
410 RegCloseKey (hUserKey
);
414 /* Set 'Password' value */
415 if (RegSetValueExW (hUserKey
,
419 (LPBYTE
)UserPassword
,
420 (wcslen (UserPassword
) + 1) * sizeof (WCHAR
)))
422 DPRINT1 ("Failed to set the user name value! (Error %lu)\n", GetLastError());
423 RegCloseKey (hUserKey
);
427 /* Set 'Sid' value */
428 if (RegSetValueExW (hUserKey
,
433 RtlLengthSid (UserSid
)))
435 DPRINT1 ("Failed to set the user SID value! (Error %lu)\n", GetLastError());
436 RegCloseKey (hUserKey
);
440 RegCloseKey (hUserKey
);
442 DPRINT ("SamCreateUser() done\n");
449 * ERROR_WRONG_PASSWORD
453 SamCheckUserPassword (PWSTR UserName
,
456 WCHAR szPassword
[256];
461 DPRINT ("SamCheckUserPassword() called\n");
463 /* Open the Users key */
464 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
465 L
"SAM\\SAM\\Domains\\Account\\Users",
470 DPRINT1 ("Failed to open Users key! (Error %lu)\n", GetLastError());
474 /* Open the user key */
475 if (RegOpenKeyExW (hUsersKey
,
481 if (GetLastError () == ERROR_FILE_NOT_FOUND
)
483 DPRINT1 ("Invalid user name!\n");
484 SetLastError (ERROR_NO_SUCH_USER
);
488 DPRINT1 ("Failed to open user key! (Error %lu)\n", GetLastError());
491 RegCloseKey (hUsersKey
);
495 RegCloseKey (hUsersKey
);
497 /* Get the password */
498 dwLength
= 256 * sizeof(WCHAR
);
499 if (RegQueryValueExW (hUserKey
,
506 DPRINT1 ("Failed to read the password! (Error %lu)\n", GetLastError());
507 RegCloseKey (hUserKey
);
511 RegCloseKey (hUserKey
);
513 /* Compare passwords */
514 if ((wcslen (szPassword
) != wcslen (UserPassword
)) ||
515 (wcscmp (szPassword
, UserPassword
) != 0))
517 DPRINT1 ("Wrong password!\n");
518 SetLastError (ERROR_WRONG_PASSWORD
);
522 DPRINT ("SamCheckUserPassword() done\n");
529 SamGetUserSid (PWSTR UserName
,
537 DPRINT ("SamGetUserSid() called\n");
542 /* Open the Users key */
543 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
544 L
"SAM\\SAM\\Domains\\Account\\Users",
549 DPRINT1 ("Failed to open Users key! (Error %lu)\n", GetLastError());
553 /* Open the user key */
554 if (RegOpenKeyExW (hUsersKey
,
560 if (GetLastError () == ERROR_FILE_NOT_FOUND
)
562 DPRINT1 ("Invalid user name!\n");
563 SetLastError (ERROR_NO_SUCH_USER
);
567 DPRINT1 ("Failed to open user key! (Error %lu)\n", GetLastError());
570 RegCloseKey (hUsersKey
);
574 RegCloseKey (hUsersKey
);
578 if (RegQueryValueExW (hUserKey
,
585 DPRINT1 ("Failed to read the SID size! (Error %lu)\n", GetLastError());
586 RegCloseKey (hUserKey
);
590 /* Allocate sid buffer */
591 DPRINT ("Required SID buffer size: %lu\n", dwLength
);
592 lpSid
= (PSID
)RtlAllocateHeap (RtlGetProcessHeap (),
597 DPRINT1 ("Failed to allocate SID buffer!\n");
598 RegCloseKey (hUserKey
);
603 if (RegQueryValueExW (hUserKey
,
610 DPRINT1 ("Failed to read the SID! (Error %lu)\n", GetLastError());
611 RtlFreeHeap (RtlGetProcessHeap (),
614 RegCloseKey (hUserKey
);
618 RegCloseKey (hUserKey
);
622 DPRINT ("SamGetUserSid() done\n");