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");
305 SamCreateUser (PWSTR UserName
,
313 DPRINT ("SamCreateUser() called\n");
315 /* FIXME: Check whether the SID is a real user sid */
317 /* Open the Users key */
318 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
319 L
"SAM\\SAM\\Domains\\Account\\Users",
324 DPRINT1 ("Failed to open Account key! (Error %lu)\n", GetLastError());
328 /* Create user name key */
329 if (RegCreateKeyExW (hUsersKey
,
333 REG_OPTION_NON_VOLATILE
,
339 DPRINT1 ("Failed to create/open the user key! (Error %lu)\n", GetLastError());
340 RegCloseKey (hUsersKey
);
344 RegCloseKey (hUsersKey
);
346 if (dwDisposition
== REG_OPENED_EXISTING_KEY
)
348 DPRINT1 ("User already exists!\n");
349 RegCloseKey (hUserKey
);
350 SetLastError (ERROR_USER_EXISTS
);
355 /* Set 'Name' value */
356 if (RegSetValueExW (hUserKey
,
361 (wcslen (UserName
) + 1) * sizeof (WCHAR
)))
363 DPRINT1 ("Failed to set the user name value! (Error %lu)\n", GetLastError());
364 RegCloseKey (hUserKey
);
368 /* Set 'Password' value */
369 if (RegSetValueExW (hUserKey
,
373 (LPBYTE
)UserPassword
,
374 (wcslen (UserPassword
) + 1) * sizeof (WCHAR
)))
376 DPRINT1 ("Failed to set the user name value! (Error %lu)\n", GetLastError());
377 RegCloseKey (hUserKey
);
381 /* Set 'Sid' value */
382 if (RegSetValueExW (hUserKey
,
387 RtlLengthSid (UserSid
)))
389 DPRINT1 ("Failed to set the user SID value! (Error %lu)\n", GetLastError());
390 RegCloseKey (hUserKey
);
394 RegCloseKey (hUserKey
);
396 DPRINT ("SamCreateUser() done\n");
403 * ERROR_WRONG_PASSWORD
407 SamCheckUserPassword (PWSTR UserName
,
410 WCHAR szPassword
[256];
415 DPRINT ("SamCheckUserPassword() called\n");
417 /* Open the Users key */
418 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
419 L
"SAM\\SAM\\Domains\\Account\\Users",
424 DPRINT1 ("Failed to open Users key! (Error %lu)\n", GetLastError());
428 /* Open the user key */
429 if (RegOpenKeyExW (hUsersKey
,
435 if (GetLastError () == ERROR_FILE_NOT_FOUND
)
437 DPRINT1 ("Invalid user name!\n");
438 SetLastError (ERROR_NO_SUCH_USER
);
442 DPRINT1 ("Failed to open user key! (Error %lu)\n", GetLastError());
445 RegCloseKey (hUsersKey
);
449 RegCloseKey (hUsersKey
);
451 /* Get the password */
452 dwLength
= 256 * sizeof(WCHAR
);
453 if (RegQueryValueExW (hUserKey
,
460 DPRINT1 ("Failed to read the password! (Error %lu)\n", GetLastError());
461 RegCloseKey (hUserKey
);
465 RegCloseKey (hUserKey
);
467 /* Compare passwords */
468 if ((wcslen (szPassword
) != wcslen (UserPassword
)) ||
469 (wcscmp (szPassword
, UserPassword
) != 0))
471 DPRINT1 ("Wrong password!\n");
472 SetLastError (ERROR_WRONG_PASSWORD
);
476 DPRINT ("SamCheckUserPassword() done\n");
483 SamGetUserSid (PWSTR UserName
,
491 DPRINT ("SamGetUserSid() called\n");
496 /* Open the Users key */
497 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
498 L
"SAM\\SAM\\Domains\\Account\\Users",
503 DPRINT1 ("Failed to open Users key! (Error %lu)\n", GetLastError());
507 /* Open the user key */
508 if (RegOpenKeyExW (hUsersKey
,
514 if (GetLastError () == ERROR_FILE_NOT_FOUND
)
516 DPRINT1 ("Invalid user name!\n");
517 SetLastError (ERROR_NO_SUCH_USER
);
521 DPRINT1 ("Failed to open user key! (Error %lu)\n", GetLastError());
524 RegCloseKey (hUsersKey
);
528 RegCloseKey (hUsersKey
);
532 if (RegQueryValueExW (hUserKey
,
539 DPRINT1 ("Failed to read the SID size! (Error %lu)\n", GetLastError());
540 RegCloseKey (hUserKey
);
544 /* Allocate sid buffer */
545 DPRINT ("Required SID buffer size: %lu\n", dwLength
);
546 lpSid
= (PSID
)RtlAllocateHeap (RtlGetProcessHeap (),
551 DPRINT1 ("Failed to allocate SID buffer!\n");
552 RegCloseKey (hUserKey
);
557 if (RegQueryValueExW (hUserKey
,
564 DPRINT1 ("Failed to read the SID! (Error %lu)\n", GetLastError());
565 RtlFreeHeap (RtlGetProcessHeap (),
568 RegCloseKey (hUserKey
);
572 RegCloseKey (hUserKey
);
576 DPRINT ("SamGetUserSid() done\n");
581 void __RPC_FAR
* __RPC_USER
midl_user_allocate(SIZE_T len
)
583 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
587 void __RPC_USER
midl_user_free(void __RPC_FAR
* ptr
)
589 HeapFree(GetProcessHeap(), 0, ptr
);
594 PSAMPR_SERVER_NAME_bind(PSAMPR_SERVER_NAME pszSystemName
)
596 handle_t hBinding
= NULL
;
597 LPWSTR pszStringBinding
;
600 // TRACE("PSAMPR_SERVER_NAME_bind() called\n");
602 status
= RpcStringBindingComposeW(NULL
,
610 // TRACE("RpcStringBindingCompose returned 0x%x\n", status);
614 /* Set the binding handle that will be used to bind to the server. */
615 status
= RpcBindingFromStringBindingW(pszStringBinding
,
619 // TRACE("RpcBindingFromStringBinding returned 0x%x\n", status);
622 status
= RpcStringFreeW(&pszStringBinding
);
625 // TRACE("RpcStringFree returned 0x%x\n", status);
633 PSAMPR_SERVER_NAME_unbind(PSAMPR_SERVER_NAME pszSystemName
,
638 // TRACE("PSAMPR_SERVER_NAME_unbind() called\n");
640 status
= RpcBindingFree(&hBinding
);
643 // TRACE("RpcBindingFree returned 0x%x\n", status);
650 SamCloseHandle(IN SAM_HANDLE SamHandle
)
652 return STATUS_NOT_IMPLEMENTED
;
657 SamConnect(IN OUT PUNICODE_STRING ServerName
,
658 OUT PSAM_HANDLE ServerHandle
,
659 IN ACCESS_MASK DesiredAccess
,
660 IN POBJECT_ATTRIBUTES ObjectAttributes
)
662 return STATUS_NOT_IMPLEMENTED
;
667 SamShutdownSamServer(IN SAM_HANDLE ServerHandle
)
669 return STATUS_NOT_IMPLEMENTED
;