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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 *****************************************************************/
40 /* GLOBALS *******************************************************************/
43 /* FUNCTIONS *****************************************************************/
47 CreateBuiltinAliases (HKEY hAliasesKey
)
54 CreateBuiltinGroups (HKEY hGroupsKey
)
61 CreateBuiltinUsers (HKEY hUsersKey
)
68 SamInitializeSAM (VOID
)
79 DPRINT("SamInitializeSAM() called\n");
81 if (RegCreateKeyExW (HKEY_LOCAL_MACHINE
,
85 REG_OPTION_NON_VOLATILE
,
91 DPRINT1 ("Failed to create 'Sam' key! (Error %lu)\n", GetLastError());
95 if (RegCreateKeyExW (hSamKey
,
99 REG_OPTION_NON_VOLATILE
,
105 DPRINT1 ("Failed to create 'Domains' key! (Error %lu)\n", GetLastError());
106 RegCloseKey (hSamKey
);
110 RegCloseKey (hSamKey
);
112 /* Create the 'Domains\\Account' key */
113 if (RegCreateKeyExW (hDomainsKey
,
117 REG_OPTION_NON_VOLATILE
,
123 DPRINT1 ("Failed to create 'Domains\\Account' key! (Error %lu)\n", GetLastError());
124 RegCloseKey (hDomainsKey
);
129 /* Create the 'Account\Aliases' key */
130 if (RegCreateKeyExW (hAccountKey
,
134 REG_OPTION_NON_VOLATILE
,
140 DPRINT1 ("Failed to create 'Account\\Aliases' key! (Error %lu)\n", GetLastError());
141 RegCloseKey (hAccountKey
);
142 RegCloseKey (hDomainsKey
);
146 RegCloseKey (hAliasesKey
);
149 /* Create the 'Account\Groups' key */
150 if (RegCreateKeyExW (hAccountKey
,
154 REG_OPTION_NON_VOLATILE
,
160 DPRINT1 ("Failed to create 'Account\\Groups' key! (Error %lu)\n", GetLastError());
161 RegCloseKey (hAccountKey
);
162 RegCloseKey (hDomainsKey
);
166 RegCloseKey (hGroupsKey
);
169 /* Create the 'Account\Users' key */
170 if (RegCreateKeyExW (hAccountKey
,
174 REG_OPTION_NON_VOLATILE
,
180 DPRINT1 ("Failed to create 'Account\\Users' key! (Error %lu)\n", GetLastError());
181 RegCloseKey (hAccountKey
);
182 RegCloseKey (hDomainsKey
);
186 RegCloseKey (hUsersKey
);
188 RegCloseKey (hAccountKey
);
191 /* Create the 'Domains\\Builtin' */
192 if (RegCreateKeyExW (hDomainsKey
,
196 REG_OPTION_NON_VOLATILE
,
202 DPRINT1 ("Failed to create Builtin key! (Error %lu)\n", GetLastError());
203 RegCloseKey (hDomainsKey
);
208 /* Create the 'Builtin\Aliases' key */
209 if (RegCreateKeyExW (hBuiltinKey
,
213 REG_OPTION_NON_VOLATILE
,
219 DPRINT1 ("Failed to create 'Builtin\\Aliases' key! (Error %lu)\n", GetLastError());
220 RegCloseKey (hBuiltinKey
);
221 RegCloseKey (hDomainsKey
);
225 /* Create builtin aliases */
226 if (!CreateBuiltinAliases (hAliasesKey
))
228 DPRINT1 ("Failed to create builtin aliases!\n");
229 RegCloseKey (hAliasesKey
);
230 RegCloseKey (hBuiltinKey
);
231 RegCloseKey (hDomainsKey
);
235 RegCloseKey (hAliasesKey
);
238 /* Create the 'Builtin\Groups' key */
239 if (RegCreateKeyExW (hBuiltinKey
,
243 REG_OPTION_NON_VOLATILE
,
249 DPRINT1 ("Failed to create 'Builtin\\Groups' key! (Error %lu)\n", GetLastError());
250 RegCloseKey (hBuiltinKey
);
251 RegCloseKey (hDomainsKey
);
255 /* Create builtin groups */
256 if (!CreateBuiltinGroups (hGroupsKey
))
258 DPRINT1 ("Failed to create builtin groups!\n");
259 RegCloseKey (hGroupsKey
);
260 RegCloseKey (hBuiltinKey
);
261 RegCloseKey (hDomainsKey
);
265 RegCloseKey (hGroupsKey
);
268 /* Create the 'Builtin\Users' key */
269 if (RegCreateKeyExW (hBuiltinKey
,
273 REG_OPTION_NON_VOLATILE
,
279 DPRINT1 ("Failed to create 'Builtin\\Users' key! (Error %lu)\n", GetLastError());
280 RegCloseKey (hBuiltinKey
);
281 RegCloseKey (hDomainsKey
);
285 /* Create builtin users */
286 if (!CreateBuiltinUsers (hUsersKey
))
288 DPRINT1 ("Failed to create builtin users!\n");
289 RegCloseKey (hUsersKey
);
290 RegCloseKey (hBuiltinKey
);
291 RegCloseKey (hDomainsKey
);
295 RegCloseKey (hUsersKey
);
297 RegCloseKey (hBuiltinKey
);
299 RegCloseKey (hDomainsKey
);
301 DPRINT ("SamInitializeSAM() done\n");
308 SamGetDomainSid (PSID
*Sid
)
310 DPRINT ("SamGetDomainSid() called\n");
317 SamSetDomainSid (PSID Sid
)
321 DPRINT ("SamSetDomainSid() called\n");
323 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
324 L
"SAM\\SAM\\Domains\\Account",
329 DPRINT1 ("Failed to open the Account key! (Error %lu)\n", GetLastError());
333 if (RegSetValueExW (hAccountKey
,
340 DPRINT1 ("Failed to set Domain-SID value! (Error %lu)\n", GetLastError());
341 RegCloseKey (hAccountKey
);
345 RegCloseKey (hAccountKey
);
347 DPRINT ("SamSetDomainSid() called\n");
357 SamCreateUser (PWSTR UserName
,
365 DPRINT ("SamCreateUser() called\n");
367 /* FIXME: Check whether the SID is a real user sid */
369 /* Open the Users key */
370 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
371 L
"SAM\\SAM\\Domains\\Account\\Users",
376 DPRINT1 ("Failed to open Account key! (Error %lu)\n", GetLastError());
380 /* Create user name key */
381 if (RegCreateKeyExW (hUsersKey
,
385 REG_OPTION_NON_VOLATILE
,
391 DPRINT1 ("Failed to create/open the user key! (Error %lu)\n", GetLastError());
392 RegCloseKey (hUsersKey
);
396 RegCloseKey (hUsersKey
);
398 if (dwDisposition
== REG_OPENED_EXISTING_KEY
)
400 DPRINT1 ("User already exists!\n");
401 RegCloseKey (hUserKey
);
402 SetLastError (ERROR_USER_EXISTS
);
407 /* Set 'Name' value */
408 if (RegSetValueExW (hUserKey
,
413 (wcslen (UserName
) + 1) * sizeof (WCHAR
)))
415 DPRINT1 ("Failed to set the user name value! (Error %lu)\n", GetLastError());
416 RegCloseKey (hUserKey
);
420 /* Set 'Password' value */
421 if (RegSetValueExW (hUserKey
,
425 (LPBYTE
)UserPassword
,
426 (wcslen (UserPassword
) + 1) * sizeof (WCHAR
)))
428 DPRINT1 ("Failed to set the user name value! (Error %lu)\n", GetLastError());
429 RegCloseKey (hUserKey
);
433 /* Set 'Sid' value */
434 if (RegSetValueExW (hUserKey
,
439 RtlLengthSid (UserSid
)))
441 DPRINT1 ("Failed to set the user SID value! (Error %lu)\n", GetLastError());
442 RegCloseKey (hUserKey
);
446 RegCloseKey (hUserKey
);
448 DPRINT ("SamCreateUser() done\n");
455 * ERROR_WRONG_PASSWORD
459 SamCheckUserPassword (PWSTR UserName
,
462 WCHAR szPassword
[256];
467 DPRINT ("SamCheckUserPassword() called\n");
469 /* Open the Users key */
470 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
471 L
"SAM\\SAM\\Domains\\Account\\Users",
476 DPRINT1 ("Failed to open Users key! (Error %lu)\n", GetLastError());
480 /* Open the user key */
481 if (RegOpenKeyExW (hUsersKey
,
487 if (GetLastError () == ERROR_FILE_NOT_FOUND
)
489 DPRINT1 ("Invalid user name!\n");
490 SetLastError (ERROR_NO_SUCH_USER
);
494 DPRINT1 ("Failed to open user key! (Error %lu)\n", GetLastError());
497 RegCloseKey (hUsersKey
);
501 RegCloseKey (hUsersKey
);
503 /* Get the password */
504 dwLength
= 256 * sizeof(WCHAR
);
505 if (RegQueryValueExW (hUserKey
,
512 DPRINT1 ("Failed to read the password! (Error %lu)\n", GetLastError());
513 RegCloseKey (hUserKey
);
517 RegCloseKey (hUserKey
);
519 /* Compare passwords */
520 if ((wcslen (szPassword
) != wcslen (UserPassword
)) ||
521 (wcscmp (szPassword
, UserPassword
) != 0))
523 DPRINT1 ("Wrong password!\n");
524 SetLastError (ERROR_WRONG_PASSWORD
);
528 DPRINT ("SamCheckUserPassword() done\n");
535 SamGetUserSid (PWSTR UserName
,
543 DPRINT ("SamGetUserSid() called\n");
548 /* Open the Users key */
549 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
550 L
"SAM\\SAM\\Domains\\Account\\Users",
555 DPRINT1 ("Failed to open Users key! (Error %lu)\n", GetLastError());
559 /* Open the user key */
560 if (RegOpenKeyExW (hUserKey
,
566 if (GetLastError () == ERROR_FILE_NOT_FOUND
)
568 DPRINT1 ("Invalid user name!\n");
569 SetLastError (ERROR_NO_SUCH_USER
);
573 DPRINT1 ("Failed to open user key! (Error %lu)\n", GetLastError());
576 RegCloseKey (hUsersKey
);
580 RegCloseKey (hUsersKey
);
584 if (RegQueryValueExW (hUserKey
,
591 DPRINT1 ("Failed to read the SID size! (Error %lu)\n", GetLastError());
592 RegCloseKey (hUserKey
);
596 /* Allocate sid buffer */
597 DPRINT ("Required SID buffer size: %lu\n", dwLength
);
598 lpSid
= (PSID
)RtlAllocateHeap (RtlGetProcessHeap (),
603 DPRINT1 ("Failed to allocate SID buffer!\n");
604 RegCloseKey (hUserKey
);
609 if (RegQueryValueExW (hUserKey
,
616 DPRINT1 ("Failed to read the SID! (Error %lu)\n", GetLastError());
617 RtlFreeHeap (RtlGetProcessHeap (),
620 RegCloseKey (hUserKey
);
624 RegCloseKey (hUserKey
);
628 DPRINT ("SamGetUserSid() done\n");