ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

samlib.c
Go to the documentation of this file.
00001 /*
00002  *  ReactOS kernel
00003  *  Copyright (C) 2004 ReactOS Team
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License along
00016  *  with this program; if not, write to the Free Software Foundation, Inc.,
00017  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00018  */
00019 /* $Id: samlib.c 56648 2012-05-21 13:38:32Z ekohl $
00020  *
00021  * COPYRIGHT:         See COPYING in the top level directory
00022  * PROJECT:           ReactOS system libraries
00023  * PURPOSE:           SAM interface library
00024  * FILE:              lib/samlib/samlib.c
00025  * PROGRAMER:         Eric Kohl
00026  */
00027 
00028 /* INCLUDES *****************************************************************/
00029 
00030 #include "precomp.h"
00031 
00032 WINE_DEFAULT_DEBUG_CHANNEL(samlib);
00033 
00034 /* GLOBALS *******************************************************************/
00035 
00036 
00037 /* FUNCTIONS *****************************************************************/
00038 
00039 /*
00040  * ERROR_USER_EXISTS
00041  */
00042 BOOL WINAPI
00043 SamCreateUser (PWSTR UserName,
00044            PWSTR UserPassword,
00045            PSID UserSid)
00046 {
00047   DWORD dwDisposition;
00048   HKEY hUsersKey;
00049   HKEY hUserKey;
00050 
00051   TRACE("SamCreateUser() called\n");
00052 
00053   /* FIXME: Check whether the SID is a real user sid */
00054 
00055   /* Open the Users key */
00056   if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
00057              L"SAM\\SAM\\Domains\\Account\\Users",
00058              0,
00059              KEY_ALL_ACCESS,
00060              &hUsersKey))
00061     {
00062       ERR("Failed to open Account key! (Error %lu)\n", GetLastError());
00063       return FALSE;
00064     }
00065 
00066   /* Create user name key */
00067   if (RegCreateKeyExW (hUsersKey,
00068                UserName,
00069                0,
00070                NULL,
00071                REG_OPTION_NON_VOLATILE,
00072                KEY_ALL_ACCESS,
00073                NULL,
00074                &hUserKey,
00075                &dwDisposition))
00076     {
00077       ERR("Failed to create/open the user key! (Error %lu)\n", GetLastError());
00078       RegCloseKey (hUsersKey);
00079       return FALSE;
00080     }
00081 
00082   RegCloseKey (hUsersKey);
00083 
00084   if (dwDisposition == REG_OPENED_EXISTING_KEY)
00085     {
00086       ERR("User already exists!\n");
00087       RegCloseKey (hUserKey);
00088       SetLastError (ERROR_USER_EXISTS);
00089       return FALSE;
00090     }
00091 
00092 
00093   /* Set 'Name' value */
00094   if (RegSetValueExW (hUserKey,
00095               L"Name",
00096               0,
00097               REG_SZ,
00098               (LPBYTE)UserName,
00099               (wcslen (UserName) + 1) * sizeof (WCHAR)))
00100     {
00101       ERR("Failed to set the user name value! (Error %lu)\n", GetLastError());
00102       RegCloseKey (hUserKey);
00103       return FALSE;
00104     }
00105 
00106   /* Set 'Password' value */
00107   if (RegSetValueExW (hUserKey,
00108               L"Password",
00109               0,
00110               REG_SZ,
00111               (LPBYTE)UserPassword,
00112               (wcslen (UserPassword) + 1) * sizeof (WCHAR)))
00113     {
00114       ERR("Failed to set the user name value! (Error %lu)\n", GetLastError());
00115       RegCloseKey (hUserKey);
00116       return FALSE;
00117     }
00118 
00119   /* Set 'Sid' value */
00120   if (RegSetValueExW (hUserKey,
00121               L"Sid",
00122               0,
00123               REG_BINARY,
00124               (LPBYTE)UserSid,
00125               RtlLengthSid (UserSid)))
00126     {
00127       ERR("Failed to set the user SID value! (Error %lu)\n", GetLastError());
00128       RegCloseKey (hUserKey);
00129       return FALSE;
00130     }
00131 
00132   RegCloseKey (hUserKey);
00133 
00134   TRACE("SamCreateUser() done\n");
00135 
00136   return TRUE;
00137 }
00138 
00139 
00140 /*
00141  * ERROR_WRONG_PASSWORD
00142  * ERROR_NO_SUCH_USER
00143  */
00144 BOOL WINAPI
00145 SamCheckUserPassword (PWSTR UserName,
00146               PWSTR UserPassword)
00147 {
00148   WCHAR szPassword[256];
00149   DWORD dwLength;
00150   HKEY hUsersKey;
00151   HKEY hUserKey;
00152 
00153   TRACE("SamCheckUserPassword() called\n");
00154 
00155   /* Open the Users key */
00156   if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
00157              L"SAM\\SAM\\Domains\\Account\\Users",
00158              0,
00159              KEY_READ,
00160              &hUsersKey))
00161     {
00162       ERR("Failed to open Users key! (Error %lu)\n", GetLastError());
00163       return FALSE;
00164     }
00165 
00166   /* Open the user key */
00167   if (RegOpenKeyExW (hUsersKey,
00168              UserName,
00169              0,
00170              KEY_READ,
00171              &hUserKey))
00172     {
00173       if (GetLastError () == ERROR_FILE_NOT_FOUND)
00174     {
00175       ERR("Invalid user name!\n");
00176       SetLastError (ERROR_NO_SUCH_USER);
00177     }
00178       else
00179     {
00180       ERR("Failed to open user key! (Error %lu)\n", GetLastError());
00181     }
00182 
00183       RegCloseKey (hUsersKey);
00184       return FALSE;
00185     }
00186 
00187   RegCloseKey (hUsersKey);
00188 
00189   /* Get the password */
00190   dwLength = 256 * sizeof(WCHAR);
00191   if (RegQueryValueExW (hUserKey,
00192             L"Password",
00193             NULL,
00194             NULL,
00195             (LPBYTE)szPassword,
00196             &dwLength))
00197     {
00198       ERR("Failed to read the password! (Error %lu)\n", GetLastError());
00199       RegCloseKey (hUserKey);
00200       return FALSE;
00201     }
00202 
00203   RegCloseKey (hUserKey);
00204 
00205   /* Compare passwords */
00206   if ((wcslen (szPassword) != wcslen (UserPassword)) ||
00207       (wcscmp (szPassword, UserPassword) != 0))
00208     {
00209       ERR("Wrong password!\n");
00210       SetLastError (ERROR_WRONG_PASSWORD);
00211       return FALSE;
00212     }
00213 
00214   TRACE("SamCheckUserPassword() done\n");
00215 
00216   return TRUE;
00217 }
00218 
00219 
00220 BOOL WINAPI
00221 SamGetUserSid (PWSTR UserName,
00222            PSID *Sid)
00223 {
00224   PSID lpSid;
00225   DWORD dwLength;
00226   HKEY hUsersKey;
00227   HKEY hUserKey;
00228 
00229   TRACE("SamGetUserSid() called\n");
00230 
00231   if (Sid != NULL)
00232     *Sid = NULL;
00233 
00234   /* Open the Users key */
00235   if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
00236              L"SAM\\SAM\\Domains\\Account\\Users",
00237              0,
00238              KEY_READ,
00239              &hUsersKey))
00240     {
00241       ERR("Failed to open Users key! (Error %lu)\n", GetLastError());
00242       return FALSE;
00243     }
00244 
00245   /* Open the user key */
00246   if (RegOpenKeyExW (hUsersKey,
00247              UserName,
00248              0,
00249              KEY_READ,
00250              &hUserKey))
00251     {
00252       if (GetLastError () == ERROR_FILE_NOT_FOUND)
00253     {
00254       ERR("Invalid user name!\n");
00255       SetLastError (ERROR_NO_SUCH_USER);
00256     }
00257       else
00258     {
00259       ERR("Failed to open user key! (Error %lu)\n", GetLastError());
00260     }
00261 
00262       RegCloseKey (hUsersKey);
00263       return FALSE;
00264     }
00265 
00266   RegCloseKey (hUsersKey);
00267 
00268   /* Get SID size */
00269   dwLength = 0;
00270   if (RegQueryValueExW (hUserKey,
00271             L"Sid",
00272             NULL,
00273             NULL,
00274             NULL,
00275             &dwLength))
00276     {
00277       ERR("Failed to read the SID size! (Error %lu)\n", GetLastError());
00278       RegCloseKey (hUserKey);
00279       return FALSE;
00280     }
00281 
00282   /* Allocate sid buffer */
00283   TRACE("Required SID buffer size: %lu\n", dwLength);
00284   lpSid = (PSID)RtlAllocateHeap (RtlGetProcessHeap (),
00285                  0,
00286                  dwLength);
00287   if (lpSid == NULL)
00288     {
00289       ERR("Failed to allocate SID buffer!\n");
00290       RegCloseKey (hUserKey);
00291       return FALSE;
00292     }
00293 
00294   /* Read sid */
00295   if (RegQueryValueExW (hUserKey,
00296             L"Sid",
00297             NULL,
00298             NULL,
00299             (LPBYTE)lpSid,
00300             &dwLength))
00301     {
00302       ERR("Failed to read the SID! (Error %lu)\n", GetLastError());
00303       RtlFreeHeap (RtlGetProcessHeap (),
00304            0,
00305            lpSid);
00306       RegCloseKey (hUserKey);
00307       return FALSE;
00308     }
00309 
00310   RegCloseKey (hUserKey);
00311 
00312   *Sid = lpSid;
00313 
00314   TRACE("SamGetUserSid() done\n");
00315 
00316   return TRUE;
00317 }
00318 
00319 void __RPC_FAR * __RPC_USER midl_user_allocate(SIZE_T len)
00320 {
00321     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
00322 }
00323 
00324 
00325 void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
00326 {
00327     HeapFree(GetProcessHeap(), 0, ptr);
00328 }
00329 
00330 
00331 handle_t __RPC_USER
00332 PSAMPR_SERVER_NAME_bind(PSAMPR_SERVER_NAME pszSystemName)
00333 {
00334     handle_t hBinding = NULL;
00335     LPWSTR pszStringBinding;
00336     RPC_STATUS status;
00337 
00338     TRACE("PSAMPR_SERVER_NAME_bind() called\n");
00339 
00340     status = RpcStringBindingComposeW(NULL,
00341                                       L"ncacn_np",
00342                                       pszSystemName,
00343                                       L"\\pipe\\samr",
00344                                       NULL,
00345                                       &pszStringBinding);
00346     if (status)
00347     {
00348         TRACE("RpcStringBindingCompose returned 0x%x\n", status);
00349         return NULL;
00350     }
00351 
00352     /* Set the binding handle that will be used to bind to the server. */
00353     status = RpcBindingFromStringBindingW(pszStringBinding,
00354                                           &hBinding);
00355     if (status)
00356     {
00357         TRACE("RpcBindingFromStringBinding returned 0x%x\n", status);
00358     }
00359 
00360     status = RpcStringFreeW(&pszStringBinding);
00361     if (status)
00362     {
00363 //        TRACE("RpcStringFree returned 0x%x\n", status);
00364     }
00365 
00366     return hBinding;
00367 }
00368 
00369 
00370 void __RPC_USER
00371 PSAMPR_SERVER_NAME_unbind(PSAMPR_SERVER_NAME pszSystemName,
00372                           handle_t hBinding)
00373 {
00374     RPC_STATUS status;
00375 
00376     TRACE("PSAMPR_SERVER_NAME_unbind() called\n");
00377 
00378     status = RpcBindingFree(&hBinding);
00379     if (status)
00380     {
00381         TRACE("RpcBindingFree returned 0x%x\n", status);
00382     }
00383 }
00384 
00385 
00386 NTSTATUS
00387 NTAPI
00388 SamCloseHandle(IN SAM_HANDLE SamHandle)
00389 {
00390     return STATUS_NOT_IMPLEMENTED;
00391 }
00392 
00393 NTSTATUS
00394 NTAPI
00395 SamConnect(IN OUT PUNICODE_STRING ServerName,
00396            OUT PSAM_HANDLE ServerHandle,
00397            IN ACCESS_MASK DesiredAccess,
00398            IN POBJECT_ATTRIBUTES ObjectAttributes)
00399 {
00400     return STATUS_NOT_IMPLEMENTED;
00401 }
00402 
00403 NTSTATUS
00404 NTAPI
00405 SamShutdownSamServer(IN SAM_HANDLE ServerHandle)
00406 {
00407     return STATUS_NOT_IMPLEMENTED;
00408 }
00409 
00410 /* EOF */

Generated on Fri May 25 2012 04:24:18 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.