Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   Программирование и базы данных (http://forum.oszone.net/forumdisplay.php?f=21)
-   -   [С++ Builder 2009] Проблемы с созданием пользователей (наверно все дело в строках) (http://forum.oszone.net/showthread.php?t=144265)

crashtuak 05-07-2009 12:41 1159309

[С++ Builder 2009] Проблемы с созданием пользователей (наверно все дело в строках)
 
Вот есть код:
Код:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Ntsecapi.h"
#include "shlwapi.h"
#include "lm.h"
#include "cstdlib"
#include "tchar.h"
#include "stdlib.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
DWORD  CreateNewUser(wchar_t *UserName, wchar_t *UserPass, wchar_t *UserDesc, wchar_t *UserGroup, bool IsAutoLogon);
LONG  SetDefaultPassword(LPCWSTR PasswordBuffer);
LONG  SetAutoLogon(LPCWSTR pszUserName, LPCWSTR pszPassword);
void  ClearAutoLogon(VOID);
void  SetDefAccount(LPCWSTR pszUser, LPCWSTR pszDomain);
void  MyRtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString);
WCHAR* GetGroupName(WCHAR* pszAdmins, DWORD cchAdmins, DWORD Group_Rid);
void  GetDefAccount(LPCWSTR pszUser, DWORD cbUser);

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
// registry information
const WCHAR c_szWinLogon[]          = L"Software\\Microsoft\\Windows NT\\CurrentVersion\\WinLogon";
const WCHAR c_szAutoLogon[]        = L"AutoAdminLogon";
const WCHAR c_szDefUserName[]      = L"DefaultUserName";
const WCHAR c_szDefDomain[]        = L"DefaultDomainName";
const WCHAR c_szDefPassword[]      = L"DefaultPassword";
const WCHAR c_szDefaultPwdKey[]    = L"DefaultPassword";

///////////////////////////////////////////////////////////////

DWORD CreateNewUser(wchar_t *UserName, wchar_t *UserPass, wchar_t *UserDesc, wchar_t *UserGroup, bool IsAutoLogon)
{
PWCHAR        pDomainName;
PSID          pUserSID;
DWORD          nStatus;
SID_NAME_USE  snu;
USER_INFO_1    UI;
DWORD          IndxErrorFild = 0;
DWORD          sid_size = 0;
DWORD          domain_size = 0;

UI.usri1_name                        = UserName;
UI.usri1_password                = UserPass;
UI.usri1_comment                = UserDesc;
UI.usri1_priv                        = USER_PRIV_USER;
UI.usri1_flags                        = UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
UI.usri1_password_age        = NULL;
UI.usri1_home_dir                = L"";
UI.usri1_script_path        = NULL;

nStatus = NetUserAdd(NULL, 1, (LPBYTE)&UI, &IndxErrorFild);
if(nStatus != NERR_Success) return nStatus;

LookupAccountNameW(NULL, UserName, NULL, &sid_size, NULL, &domain_size, &snu);

if((pDomainName = (PWCHAR)malloc(2*(domain_size+1))) == NULL)
{
        return -1;
}

if((pUserSID = (PSID)malloc(sid_size)) == NULL)
{
        if(pDomainName) free(pDomainName);
        return -1;
}

if(LookupAccountNameW(NULL, UserName, pUserSID, &sid_size, pDomainName, &domain_size, &snu))
{
        LOCALGROUP_MEMBERS_INFO_0 lmi0 = {pUserSID};
        nStatus = NetLocalGroupAddMembers(NULL, UserGroup, 0, (LPBYTE)&lmi0, 1);

        if((IsAutoLogon)&&(NERR_Success == nStatus)) nStatus = SetAutoLogon(UserName, UserPass);
}

if(pDomainName) free(pDomainName);
if(pUserSID)    free(pUserSID);

return nStatus;
}

////////////////////////////////////////////////////////////

BOOL _RegSetSZ(HKEY hk, LPCWSTR pszValueName, LPCWSTR pszValue)
{
        DWORD dwSize = lstrlenW(pszValue)*sizeof(WCHAR);
    return ERROR_SUCCESS == RegSetValueExW(hk, pszValueName, 0x0, REG_SZ, (BYTE *)pszValue, dwSize);
}

BOOL _RegSetDWORD(HKEY hk, LPCWSTR pszValueName, DWORD dwValue)
{
        DWORD dwSize = sizeof(dwValue);
    return ERROR_SUCCESS == RegSetValueExW(hk, pszValueName, 0x0, REG_DWORD, (BYTE *)&dwValue, dwSize);
}

BOOL _RegDelValue(HKEY hk, LPCWSTR pszValueName)
{
        return ERROR_SUCCESS == RegDeleteValueW(hk, pszValueName);
}

///////////////////////////////////////////////////////////////

LONG SetDefaultPassword(LPCWSTR PasswordBuffer)
{
        LONG Status;
        LSA_OBJECT_ATTRIBUTES ObjectAttributes;
    LSA_HANDLE LsaHandle = NULL;
        LSA_UNICODE_STRING SecretName;
    LSA_UNICODE_STRING SecretValue;

        ObjectAttributes.Attributes = NULL;
        ObjectAttributes.Length = sizeof(LSA_OBJECT_ATTRIBUTES);
        ObjectAttributes.ObjectName = NULL;
        ObjectAttributes.RootDirectory = NULL;
        ObjectAttributes.SecurityDescriptor = NULL;
        ObjectAttributes.SecurityQualityOfService = NULL;


        Status = LsaOpenPolicy(NULL, &ObjectAttributes, POLICY_CREATE_SECRET, &LsaHandle);
    if(Status != 0) return Status;

        MyRtlInitUnicodeString(&SecretName, c_szDefaultPwdKey);
        MyRtlInitUnicodeString(&SecretValue, PasswordBuffer);

    Status = LsaStorePrivateData(LsaHandle, &SecretName, &SecretValue);
    LsaClose(LsaHandle);

        return Status;
}

///////////////////////////////////////////////////////////////
// Clear the auto admin logon
///////////////////////////////////////////////////////////////
void ClearAutoLogon(VOID)
{
    HKEY hk;
    if(ERROR_SUCCESS == RegOpenKeyExW(HKEY_LOCAL_MACHINE, c_szWinLogon, 0x0, KEY_WRITE, &hk))
    {
        _RegSetSZ(hk, c_szAutoLogon, L"0");
                _RegDelValue(hk, c_szDefPassword);
        RegCloseKey(hk);
    }

    SetDefaultPassword(L"");
}

///////////////////////////////////////////////////////////////
// Set the default account in logon dialog
///////////////////////////////////////////////////////////////
void SetDefAccount(LPCWSTR pszUser, LPCWSTR pszDomain)
{
        HKEY hk;

        ClearAutoLogon();

    if(ERROR_SUCCESS == RegOpenKeyExW(HKEY_LOCAL_MACHINE, c_szWinLogon, 0x0, KEY_WRITE, &hk))
    {
                _RegSetSZ(hk, c_szDefUserName, pszUser);
        _RegSetSZ(hk, c_szDefDomain, pszDomain);
      RegCloseKey(hk);
    }
}

///////////////////////////////////////////////////////////////
// Set and clear auto logon for a particular
///////////////////////////////////////////////////////////////
LONG SetAutoLogon(LPCWSTR pszUserName, LPCWSTR pszPassword)
{
    WCHAR szComputerName[MAX_COMPUTERNAME_LENGTH+1];
        DWORD dwComputerName = ARRAYSIZE(szComputerName);
        HKEY hk;

        GetComputerNameW(szComputerName, &dwComputerName);
    SetDefAccount(pszUserName, szComputerName);

        if(ERROR_SUCCESS == RegOpenKeyExW(HKEY_LOCAL_MACHINE, c_szWinLogon, 0x0, KEY_WRITE, &hk))
    {
        _RegSetSZ(hk, c_szAutoLogon, L"1");
        _RegDelValue(hk, c_szDefPassword);
        RegCloseKey (hk);
    }

    return SetDefaultPassword(pszPassword);
}

///////////////////////////////////////////////////////////////
// RtlInitUnicodeString
///////////////////////////////////////////////////////////////
void MyRtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
{
    size_t Length;

    DestinationString->Buffer = (PWSTR)SourceString;
        if(SourceString)
                {
                        Length = wcslen(SourceString) * sizeof(WCHAR);
                        DestinationString->Length = (USHORT)Length;
                        DestinationString->MaximumLength = (USHORT)(Length + sizeof(UNICODE_NULL));
        }
        else
                {
                        DestinationString->MaximumLength = 0;
                        DestinationString->Length = 0;
                }
}

///////////////////////////////////////////////////////////////

WCHAR* GetGroupName(WCHAR* pszAdmins, DWORD cchAdmins, DWORD Group_Rid)
{
        PSID psid;
        SID_IDENTIFIER_AUTHORITY auth = SECURITY_NT_AUTHORITY;
        BOOL fSuccess = AllocateAndInitializeSid(
                &auth,
                2,
                SECURITY_BUILTIN_DOMAIN_RID,
                Group_Rid,
                0,0,0,0,0,0,
                &psid
                );

        if (fSuccess)
        {
                WCHAR szDomain[DNLEN + 1];
                DWORD cchDomain = ARRAYSIZE(szDomain);
                SID_NAME_USE sUse;

                fSuccess = LookupAccountSidW(NULL, psid, pszAdmins, &cchAdmins, szDomain, &cchDomain, &sUse);

                FreeSid(psid);

                if(fSuccess == TRUE) return pszAdmins;
        }

        return NULL;
}

///////////////////////////////////////////////////////////////

void GetDefAccount(LPCWSTR pszUser, DWORD cbUser)
{
        HKEY hk;
        DWORD dwSize = cbUser;

        if(ERROR_SUCCESS == RegOpenKeyExW(HKEY_LOCAL_MACHINE, c_szWinLogon, 0x0, KEY_READ, &hk))
        {
          RegQueryValueExW(hk, c_szDefUserName, NULL, NULL, (BYTE *)pszUser, &dwSize);
          RegCloseKey(hk);
        }
}








///////////////////////////////////////////////////////////////
void __fastcall TForm1::Button1Click(TObject *Sender)
{
DWORD qwqwqw;
qwqwqw = CreateNewUser(L"asdsdsd",NULL,NULL,L"Администраторы",True);
Edit1->Text= qwqwqw ;
}
//---------------------------------------------------------------------------

Возвращает Button1Click код 2224, подозреваю, что чтото не так с строками, а в частности с wchar_t *UserGroup. Кто поможет?

mingw 05-07-2009 22:58 1159702

Это Вам функция NetUserAdd "говорит" что пользователь уже есть.

В файле <C Builder install dir>/include/lmerr.h находим
Код:

#define NERR_BASE      2100
....
#define NERR_UserExists        (NERR_BASE+124) /* The account already exists. */
...



Время: 02:25.

Время: 02:25.
© OSzone.net 2001-