Example #1
0
DWORD CIniFile::GetSectionNames(wchar_t *const pwchSections, const DWORD dwSectionsSize)
{
	assert(NULL != pwchSections);
	assert(dwSectionsSize > 0);

	return GetPrivateProfileSectionNamesW(pwchSections, dwSectionsSize, m_wchPath);
}
Example #2
0
void ndisks_load() {
    wchar_t tmp[SECTIONS_BUFFER_SIZE], sTmp[SECTIONS_BUFFER_SIZE], *pTmp, rTmp[SECTIONS_BUFFER_SIZE];
    size_t i = 0, j = 0;
    NDisk disk;
    if(available_disks == NULL) {
        return;
    }
    wcslcpy(tmp, my_dir, SECTIONS_BUFFER_SIZE);
    wcscat_s(tmp, SECTIONS_BUFFER_SIZE, _config_file);
    GetPrivateProfileSectionNamesW(sTmp, SECTIONS_BUFFER_SIZE, tmp);
    for(i = 0; i < SECTIONS_BUFFER_SIZE; i++) {
        if(sTmp[0] == '\0') {
            break;
        }
        if(sTmp[i] == '\0') {
            pTmp = sTmp + j;
            if(!pTmp || wcscmp(pTmp, L"") == 0) {
                continue;
            }
            j = i + 1;
            memset(&disk, 0, sizeof(NDisk));
            GetPrivateProfileStringW(pTmp, L"type", NULL, rTmp, SECTIONS_BUFFER_SIZE, tmp);
            if(!rTmp || wcscmp(rTmp, L"") == 0) {
                continue;
            }
            disk.type = _wcsdup(rTmp);
            GetPrivateProfileStringW(pTmp, L"username", NULL, rTmp, SECTIONS_BUFFER_SIZE, tmp);
            if(!rTmp || wcscmp(rTmp, L"") == 0) {
                free(disk.type);
                continue;
            }
            disk.username = _wcsdup(rTmp);
            disk.nickname = _wcsdup(pTmp);
            GetPrivateProfileStringW(pTmp, L"token", NULL, rTmp, SECTIONS_BUFFER_SIZE, tmp);
            disk.token = _wcsdup(rTmp);
            GetPrivateProfileStringW(pTmp, L"secret", NULL, rTmp, SECTIONS_BUFFER_SIZE, tmp);
            disk.secret = _wcsdup(rTmp);
            dict_set_element_s(available_disks, pTmp, &disk, sizeof(NDisk), ndisk_destroy_s);
        }
    }
}
Example #3
0
static
BOOL
MAIN_LoadSettings(VOID)
{
    LPWSTR lpszTmp;
    LPWSTR lpszSection;
    LONG lRet;
    WCHAR dummy[2];
    LPWSTR lpszKeyValue;
    const LPCWSTR lpszIniFile = L"progman.ini";
    WCHAR szWinDir[MAX_PATH];
    LPWSTR lpszKey;
    DWORD Value;
    HKEY hKey;
    BOOL bIsIniMigrated;
    DWORD dwSize;
    LPWSTR lpszSections;
    LPWSTR lpszData;
    DWORD dwRet;
    DWORD dwType;
    LPWSTR lpszValue;

    bIsIniMigrated = FALSE;
    lpszSections = NULL;
    lpszData = NULL;

    /* Try to create/open the Program Manager user key */
    if (RegCreateKeyExW(HKEY_CURRENT_USER,
                        L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Program Manager",
                        0,
                        NULL,
                        REG_OPTION_NON_VOLATILE,
                        KEY_READ | KEY_WRITE,
                        NULL,
                        &Globals.hKeyProgMan,
                        NULL) != ERROR_SUCCESS)
    {
        return FALSE;
    }

    /*
     * TODO: Add the explanation for the migration...
     */
    dwSize = sizeof(Value);
    lRet = RegQueryValueExW(Globals.hKeyProgMan, L"IniMigrated", NULL, &dwType, (LPBYTE)&Value, &dwSize);
    if (lRet != ERROR_SUCCESS || dwType != REG_DWORD)
        Value = 0;
    bIsIniMigrated = !!Value;

    if (bIsIniMigrated)
    {
        /* The migration was already done, just load the settings */
        goto LoadSettings;
    }

    /* Perform the migration */

    bIsIniMigrated = TRUE;
    dwSize = ARRAYSIZE(dummy);
    SetLastError(0);
    GetPrivateProfileSectionW(L"Settings", dummy, dwSize, lpszIniFile);
    if (GetLastError() == ERROR_FILE_NOT_FOUND)
        goto MigrationDone;

    SetLastError(0);
    GetPrivateProfileSectionW(L"Groups", dummy, dwSize, lpszIniFile);
    if (GetLastError() == ERROR_FILE_NOT_FOUND)
        goto MigrationDone;

    GetWindowsDirectoryW(szWinDir, ARRAYSIZE(szWinDir));
    // NOTE: GCC complains we cannot use the "\u2022" (UNICODE Code Point) notation for specifying the bullet character,
    // because it's only available in C++ or in C99. On the contrary MSVC is fine with it.
    // Instead we use a hex specification for the character: "\x2022".
    // Note also that the character "\x07" gives also a bullet, but a larger one.
    PrintString(
        L"The Program Manager has detected the presence of a legacy settings file PROGMAN.INI in the directory '%s' "
        L"and is going to migrate its contents into the current-user Program Manager settings registry key:\n"
        L"HKCU\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Program Manager"
        L"\n\n"
        L"\x2022 The migration operation will potentially overwrite all the existing current-user Program Manager settings in the registry by those stored in the PROGMAN.INI file.\n"
        L"\n"
        L"\x2022 The migration is done once, so that, at the next launch of the Program Manager, the new migrated settings are directly used.\n"
        L"\n"
        L"\x2022 It is possible to trigger later the migration by manually deleting the registry value \"IniMigrated\" under the current-user Program Manager settings registry key (specified above).\n"
        L"\n"
        L"Would you like to migrate its contents into the registry?",
        szWinDir);

    for (dwSize = BUFFER_SIZE; ; dwSize += BUFFER_SIZE)
    {
        lpszSections = Alloc(0, dwSize * sizeof(WCHAR));
        dwRet = GetPrivateProfileSectionNamesW(lpszSections, dwSize, lpszIniFile);
        if (dwRet < dwSize - 2)
            break;
        Free(lpszSections);
    }
    lpszSection = lpszSections;
    while (*lpszSection)
    {
        lRet = RegCreateKeyExW(Globals.hKeyProgMan,
                                lpszSection,
                                0,
                                NULL,
                                REG_OPTION_NON_VOLATILE,
                                KEY_WRITE,
                                NULL,
                                &hKey,
                                NULL);
        if (lRet == ERROR_SUCCESS)
        {
            for (dwSize = BUFFER_SIZE; ; dwSize += BUFFER_SIZE)
            {
                lpszData = Alloc(0, dwSize * sizeof(WCHAR));
                dwRet = GetPrivateProfileSectionW(lpszSection, lpszData, dwSize, lpszIniFile);
                if (dwRet < dwSize - 2)
                    break;
                Free(lpszData);
            }
            lpszKeyValue = lpszData;
            while (*lpszKeyValue)
            {
                lpszKey = lpszKeyValue;
                lpszValue = wcschr(lpszKeyValue, L'=');
                lpszKeyValue += (wcslen(lpszKeyValue) + 1);
                if (lpszValue)
                {
                    *lpszValue = '\0';
                    ++lpszValue;
                    Value = wcstoul(lpszValue, &lpszTmp, 0);
                    if (lpszTmp - lpszValue >= wcslen(lpszValue))
                    {
                        lpszValue = (LPWSTR)&Value;
                        dwSize = sizeof(Value);
                        dwType = REG_DWORD;
                    }
                    else
                    {
                        dwSize = wcslen(lpszValue) * sizeof(WCHAR);
                        dwType = REG_SZ;
                    }
                }
                else
                {
                    dwSize = 0;
                    dwType = REG_DWORD;
                }
                lRet = RegSetValueExW(hKey, lpszKey, 0, dwType, (LPBYTE)lpszValue, dwSize);
            }
            Free(lpszData);
            RegCloseKey(hKey);
            lpszSection += (wcslen(lpszSection) + 1);
        }
    }
    Free(lpszSections);

MigrationDone:
    Value = TRUE;
    RegSetValueExW(Globals.hKeyProgMan, L"IniMigrated", 0, REG_DWORD, (LPBYTE)&Value, sizeof(Value));


LoadSettings:
    /* Create the necessary registry keys for the Program Manager and load its settings from the registry */

    lRet = RegCreateKeyExW(Globals.hKeyProgMan,
                           L"Settings",
                           0,
                           NULL,
                           REG_OPTION_NON_VOLATILE,
                           KEY_READ | KEY_WRITE,
                           NULL,
                           &Globals.hKeyPMSettings,
                           NULL);

    lRet = RegCreateKeyExW(Globals.hKeyProgMan,
                           L"Common Groups",
                           0,
                           NULL,
                           REG_OPTION_NON_VOLATILE,
                           KEY_READ | KEY_WRITE,
                           NULL,
                           &Globals.hKeyPMCommonGroups,
                           NULL);

    lRet = RegCreateKeyExW(Globals.hKeyProgMan,
                           L"Groups",
                           0,
                           NULL,
                           REG_OPTION_NON_VOLATILE,
                           KEY_READ | KEY_WRITE,
                           NULL,
                           &Globals.hKeyPMAnsiGroups,
                           NULL);

    lRet = RegCreateKeyExW(Globals.hKeyProgMan,
                           L"UNICODE Groups",
                           0,
                           NULL,
                           REG_OPTION_NON_VOLATILE,
                           KEY_READ | KEY_WRITE,
                           NULL,
                           &Globals.hKeyPMUnicodeGroups,
                           NULL);

    lRet = RegCreateKeyExW(HKEY_CURRENT_USER,
                           L"Program Groups",
                           0,
                           NULL,
                           REG_OPTION_NON_VOLATILE,
                           KEY_READ | KEY_WRITE,
                           NULL,
                           &Globals.hKeyAnsiGroups,
                           NULL);

    lRet = RegCreateKeyExW(HKEY_CURRENT_USER,
                           L"UNICODE Program Groups",
                           0,
                           NULL,
                           REG_OPTION_NON_VOLATILE,
                           KEY_READ | KEY_WRITE,
                           NULL,
                           &Globals.hKeyUnicodeGroups,
                           NULL);

    lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
                           L"SOFTWARE\\Program Groups",
                           0,
                           NULL,
                           REG_OPTION_NON_VOLATILE,
                           KEY_READ | KEY_WRITE,
                           NULL,
                           &Globals.hKeyCommonGroups,
                           NULL);

    dwSize = sizeof(Globals.bAutoArrange);
    RegQueryValueExW(Globals.hKeyPMSettings, L"AutoArrange", NULL, &dwType, (LPBYTE)&Globals.bAutoArrange, &dwSize);

    dwSize = sizeof(Globals.bMinOnRun);
    RegQueryValueExW(Globals.hKeyPMSettings, L"MinOnRun", NULL, &dwType, (LPBYTE)&Globals.bMinOnRun, &dwSize);

    dwSize = sizeof(Globals.bSaveSettings);
    RegQueryValueExW(Globals.hKeyPMSettings, L"SaveSettings", NULL, &dwType, (LPBYTE)&Globals.bSaveSettings, &dwSize);

    return TRUE;
}
Example #4
0
INT_PTR CALLBACK NSudoDlgCallBack(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	HWND hUserName = GetDlgItem(hDlg, IDC_UserName);
	HWND hTokenPrivilege = GetDlgItem(hDlg, IDC_TokenPrivilege);
	HWND hMandatoryLabel = GetDlgItem(hDlg, IDC_MandatoryLabel);
	HWND hszPath = GetDlgItem(hDlg, IDC_szPath);

	wchar_t szCMDLine[260], szUser[260], szPrivilege[260], szMandatory[260], szBuffer[260];

	switch (message)
	{
	case WM_INITDIALOG:
		
		// Show NSudo Logo
		SendMessageW(
			GetDlgItem(hDlg, IDC_NSudoLogo),
			STM_SETIMAGE,
			IMAGE_ICON,
			LPARAM(LoadImageW(GetModuleHandleW(NULL), MAKEINTRESOURCE(IDI_NSUDO), IMAGE_ICON, 0, 0, LR_COPYFROMRESOURCE)));

		//Show Warning Icon
		SendMessageW(
			GetDlgItem(hDlg, IDC_Icon),
			STM_SETIMAGE,
			IMAGE_ICON,
			LPARAM(LoadIconW(NULL, IDI_WARNING)));

		SendMessageW(hUserName, CB_INSERTSTRING, 0, (LPARAM)TextRes.NSudo_Text_TI);
		SendMessageW(hUserName, CB_INSERTSTRING, 0, (LPARAM)TextRes.NSudo_Text_Sys);
		SendMessageW(hUserName, CB_INSERTSTRING, 0, (LPARAM)TextRes.NSudo_Text_CP);
		SendMessageW(hUserName, CB_INSERTSTRING, 0, (LPARAM)TextRes.NSudo_Text_CU);
		SendMessageW(hUserName, CB_INSERTSTRING, 0, (LPARAM)TextRes.NSudo_Text_CPD);
		SendMessageW(hUserName, CB_SETCURSEL, 4, 0); //设置默认项"TrustedInstaller"
		
		SendMessageW(hTokenPrivilege, CB_INSERTSTRING, 0, (LPARAM)TextRes.NSudo_Text_Default);
		SendMessageW(hTokenPrivilege, CB_INSERTSTRING, 0, (LPARAM)TextRes.NSudo_Text_EnableAll);
		SendMessageW(hTokenPrivilege, CB_INSERTSTRING, 0, (LPARAM)TextRes.NSudo_Text_DisableAll);
		SendMessageW(hTokenPrivilege, CB_SETCURSEL, 2, 0); //设置默认项"默认"

		SendMessageW(hMandatoryLabel, CB_INSERTSTRING, 0, (LPARAM)TextRes.NSudo_Text_Low);
		SendMessageW(hMandatoryLabel, CB_INSERTSTRING, 0, (LPARAM)TextRes.NSudo_Text_Medium);
		SendMessageW(hMandatoryLabel, CB_INSERTSTRING, 0, (LPARAM)TextRes.NSudo_Text_High);
		SendMessageW(hMandatoryLabel, CB_INSERTSTRING, 0, (LPARAM)TextRes.NSudo_Text_System);
		SendMessageW(hMandatoryLabel, CB_INSERTSTRING, 0, (LPARAM)TextRes.NSudo_Text_Default);
		SendMessageW(hMandatoryLabel, CB_SETCURSEL, 0, 0); //设置默认项"默认"

		{
			wchar_t szItem[260], szBuffer[32768];
			DWORD dwLength = GetPrivateProfileSectionNamesW(szBuffer, 32768, szShortCutListPath);

			for (DWORD i = 0, j = 0; i < dwLength; i++,j++)
			{
				if (szBuffer[i] != NULL)
				{
					szItem[j] = szBuffer[i];
				}
				else
				{
					szItem[j] = NULL;
					SendMessageW(hszPath, CB_INSERTSTRING, 0, (LPARAM)szItem);
					j=-1;
				}
			}
		}
		return (INT_PTR)TRUE;
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDC_Run:
			GetDlgItemTextW(hDlg, IDC_UserName, szUser, sizeof(szUser));
			GetDlgItemTextW(hDlg, IDC_TokenPrivilege, szPrivilege, sizeof(szPrivilege));
			GetDlgItemTextW(hDlg, IDC_MandatoryLabel, szMandatory, sizeof(szMandatory));
			GetDlgItemTextW(hDlg, IDC_szPath, szCMDLine, sizeof(szCMDLine));

			NSudo_Run(hDlg,szUser, szPrivilege, szMandatory, szCMDLine);
			break;
		case IDC_About:
			NSudoReturnMessage(TextRes.NSudo_AboutText);
			break;
		case IDC_Browse:
			wcscpy_s(szBuffer, 260, L"");
			NSudoBrowseDialog(hDlg, szBuffer);
			SetDlgItemTextW(hDlg, IDC_szPath, szBuffer);
			break;
		}
		break;
	case WM_SYSCOMMAND:
		switch (LOWORD(wParam))
		{
		case SC_CLOSE:
			PostQuitMessage(0);
			break;
		}
		break;
	}

	return 0;
}
Example #5
0
static void test_profile_sections_names(void)
{
    HANDLE h;
    int ret;
    DWORD count;
    char buf[100];
    WCHAR bufW[100];
    static const char content[]="[section1]\r\n[section2]\r\n[section3]\r\n";
    static const char testfile3[]=".\\testwine3.ini";
    static const WCHAR testfile3W[]={ '.','\\','t','e','s','t','w','i','n','e','3','.','i','n','i',0 };
    static const WCHAR not_here[] = {'.','\\','n','o','t','_','h','e','r','e','.','i','n','i',0};
    DeleteFileA( testfile3 );
    h = CreateFileA( testfile3, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL);
    ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", testfile3);
    if( h == INVALID_HANDLE_VALUE) return;
    WriteFile( h, content, sizeof(content), &count, NULL);
    CloseHandle( h);

    /* Test with sufficiently large buffer */
    ret = GetPrivateProfileSectionNamesA( buf, 29, testfile3 );
    ok( ret == 27, "expected return size 27, got %d\n", ret );
    ok( buf[ret-1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
    
    /* Test with exactly fitting buffer */
    ret = GetPrivateProfileSectionNamesA( buf, 28, testfile3 );
    ok( ret == 26, "expected return size 26, got %d\n", ret );
    ok( buf[ret+1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
    
    /* Test with a buffer too small */
    ret = GetPrivateProfileSectionNamesA( buf, 27, testfile3 );
    ok( ret == 25, "expected return size 25, got %d\n", ret );
    ok( buf[ret+1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
    
    /* Tests on nonexistent file */
    memset(buf, 0xcc, sizeof(buf));
    ret = GetPrivateProfileSectionNamesA( buf, 10, ".\\not_here.ini" );
    ok( ret == 0, "expected return size 0, got %d\n", ret );
    ok( buf[0] == 0, "returned buffer not terminated with null\n" );
    ok( buf[1] != 0, "returned buffer terminated with double-null\n" );
    
    /* Test with sufficiently large buffer */
    SetLastError(0xdeadbeef);
    ret = GetPrivateProfileSectionNamesW( bufW, 29, testfile3W );
    if (ret == 0 && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
    {
        skip("GetPrivateProfileSectionNamesW is not implemented\n");
        DeleteFileA( testfile3 );
        return;
    }
    ok( ret == 27, "expected return size 27, got %d\n", ret );
    ok( bufW[ret-1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
    
    /* Test with exactly fitting buffer */
    ret = GetPrivateProfileSectionNamesW( bufW, 28, testfile3W );
    ok( ret == 26, "expected return size 26, got %d\n", ret );
    ok( bufW[ret+1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
    
    /* Test with a buffer too small */
    ret = GetPrivateProfileSectionNamesW( bufW, 27, testfile3W );
    ok( ret == 25, "expected return size 25, got %d\n", ret );
    ok( bufW[ret+1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
    
    DeleteFileA( testfile3 );

    /* Tests on nonexistent file */
    memset(bufW, 0xcc, sizeof(bufW));
    ret = GetPrivateProfileSectionNamesW( bufW, 10, not_here );
    ok( ret == 0, "expected return size 0, got %d\n", ret );
    ok( bufW[0] == 0, "returned buffer not terminated with null\n" );
    ok( bufW[1] != 0, "returned buffer terminated with double-null\n" );
}