char *win_get_homedir(HANDLE user_token) { DWORD sz=4096; char *dir; GetUserProfileDirectory(user_token, NULL, &sz); dir = malloc(sz + 1); if (!GetUserProfileDirectory(user_token, dir, &sz)) { ERR(GetLastError(), "Could not get user profile directory"); return NULL; } return dir; }
std::string getHomeDir() { static char *home; if (home) return home; char buf[MAX_PATH] = {'\0'}; if (!home) { /* Try env variable first. */ GetEnvironmentVariable("HOME", buf, MAX_PATH); if (buf[0] != '\0') home = strdup(buf); } if (!home) { /* No `HOME' ENV; Try user profile */ HANDLE hToken = NULL; DWORD len = MAX_PATH; if (OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &hToken)) { GetUserProfileDirectory (hToken, buf, &len); CloseHandle(hToken); if (buf[0] != '\0') home = strdup(buf); } } if (home) regulatePath(home); return home ? home : ""; }
std::string home_directory() { TCHAR buf[10000]={0}; DWORD bufsize = sizeof(buf); HANDLE token = 0; OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token); GetUserProfileDirectory(token, buf, &bufsize); CloseHandle(token); return Unicode(buf).utf8(); }
BOOL getCurrentUserDir( char* buf, DWORD buflen ){ HANDLE hToken; if( !OpenProcessToken( GetCurrentProcess(), TOKEN_READ, &hToken )) return FALSE; if( !GetUserProfileDirectory( hToken, buf, &buflen )) return FALSE; CloseHandle( hToken ); return TRUE; }
BEGIN_NAMESPACE_XEVOL3D std::ds_wstring xOperationSys::getUserProfileFolder() { HANDLE hToken=0; BOOL bres1 = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken); DWORD size = 1024; wchar_t _strUsrDir[1024] = {0}; //SHGetFolderLocation(GetActiveWindow() , CLSI_APP_DATA , hToken ,) BOOL bres = GetUserProfileDirectory(hToken, _strUsrDir, &size); //得到当前进程用户的用户目录 return _strUsrDir; }
BOOL BMT_GetUserProfileDir (wchar_t* buf, DWORD* pdwLen) { HANDLE hToken; if (!OpenProcessToken (GetCurrentProcess (), TOKEN_READ, &hToken)) return FALSE; if (!GetUserProfileDirectory (hToken, buf, pdwLen)) return FALSE; CloseHandle (hToken); return TRUE; }
bool fs_userdir(char *userdir, size_t size) { HANDLE accessToken = NULL; HANDLE processHandle = GetCurrentProcess(); if (!OpenProcessToken(processHandle, TOKEN_QUERY, &accessToken)) { return false; } if (!GetUserProfileDirectory(accessToken, (LPSTR)userdir, (LPDWORD)&size)) { CloseHandle(accessToken); return false; } CloseHandle(accessToken); return true; }
void wmain(int argc, WCHAR *argv[]) { DWORD dwSize; HANDLE hToken; LPVOID lpvEnv; PROCESS_INFORMATION pi = {0}; STARTUPINFO si = {0}; WCHAR szUserProfile[256] = L""; si.cb = sizeof(STARTUPINFO); if (argc != 4) { /* wprintf(L"Usage: %s [user@domain] [password] [cmd]", argv[0]); wprintf(L"\n\n"); */ // return; } dwSize = sizeof(szUserProfile)/sizeof(WCHAR); OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken); //ShowLastError(L"OpenProcessToken"); //SetPrivilege(&hToken, SE_SYSTEMTIME_NAME,TRUE); // if(!SetPrivilege(hToken, SE_TCB_NAME , TRUE)) printf("Set Privilege Failed");// DisplayError(L"SetPrivilege"); //if (!LogonUser(argv[1], NULL , argv[2], LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &hToken)) DisplayError(L"LogonUser"); if (!CreateEnvironmentBlock(&lpvEnv, hToken, TRUE)) ShowLastError(L"CreateEnvironmentBlock"); dwSize = sizeof(szUserProfile)/sizeof(WCHAR); if (!GetUserProfileDirectory(hToken, szUserProfile, &dwSize)) ShowLastError(L"GetUserProfileDirectory"); // // TO DO: change NULL to '.' to use local account database // if (!CreateProcessWithLogonW(argv[1], NULL, argv[2], LOGON_WITH_PROFILE, NULL, argv[3], CREATE_UNICODE_ENVIRONMENT, lpvEnv, szUserProfile, &si, &pi)) ShowLastError(L"CreateProcessWithLogonW"); if (!DestroyEnvironmentBlock(lpvEnv)) ShowLastError(L"DestroyEnvironmentBlock"); CloseHandle(hToken); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); }
GetHomeDirectory () { #if defined(MIKTEX_WINDOWS) string homeDrive; string homePath; if (Utils::GetEnvironmentString("HOMEDRIVE", homeDrive) && Utils::GetEnvironmentString("HOMEPATH", homePath)) { return homeDrive + homePath; } #endif PathName ret; if (Utils::GetEnvironmentString("HOME", ret)) { return (ret); } #if defined(MIKTEX_WINDOWS) if (GetUserProfileDirectory(ret)) { return (ret); } wchar_t szWinDir[_MAX_PATH]; unsigned int n = GetWindowsDirectoryW(szWinDir, _MAX_PATH); if (n == 0) { FATAL_WINDOWS_ERROR ("GetWindowsDirectoryW", 0); } else if (n >= _MAX_PATH) { BUF_TOO_SMALL ("GetHomeDirectory"); } ret = szWinDir; return (ret); #else UNEXPECTED_CONDITION ("GetHomeDirectory"); #endif }
/* if it exits, this function returns the path to the root directory of the user's profile; otherwise, NULL. NOTE: Remember to delete the return value (using the delete [] form).*/ PSTR OwnerProfile::directory () { dprintf ( D_FULLDEBUG, "In OwnerProfile::directory()\n" ); priv_state priv = PRIV_UNKNOWN; DWORD size = MAX_PATH; PSTR buffer = NULL; BOOL ok = FALSE; __try { /* if we've already retrieved the profile's directory, then shortcut this operation by returning the one we have stashed away */ if ( profile_directory_ ) { buffer = profile_directory_; ok = TRUE; __leave; } /* we must do the following as the user or Condor */ priv = set_condor_priv (); /* if we are here, then either we a first-time visitor, or previous calls--heaven forbid--have failed so we will need to try and get the user's profile directory */ buffer = new CHAR[size]; ASSERT ( buffer ); if ( !GetUserProfileDirectory ( user_token_, buffer, &size ) ) { /* since we only allocated MAX_PATH CHARs, we may fail with at least ERROR_INSUFFICIENT_BUFFER, so we catch it and allocate the buffer size we were given by GetUserProfileDirectory() */ if ( ERROR_INSUFFICIENT_BUFFER == GetLastError () ) { delete [] buffer; /* kill the old buffer */ buffer = new CHAR[size]; ASSERT ( buffer ); if ( !GetUserProfileDirectory ( user_token_, buffer, &size ) ) { dprintf ( D_FULLDEBUG, "OwnerProfile::directory: could not get " "profile directory. (last-error = %u)\n", GetLastError () ); __leave; } } else { /* print the fact the user has no home buffer */ dprintf ( D_FULLDEBUG, "OwnerProfile::directory: this user has no " "profile directory.\n" ); __leave; } } /* if we made it this far, then we're rocking */ ok = TRUE; } __finally { if ( !ok ) { delete [] buffer; buffer = NULL; } /* return to previous privilege level */ set_priv ( priv ); } return buffer; }
BOOL CreateInteractiveProcess(DWORD dwSessionId, PWSTR pszCommandLine, BOOL fWait, DWORD dwTimeout, DWORD *pExitCode) { DWORD dwError = ERROR_SUCCESS; HANDLE hToken = NULL; LPVOID lpvEnv = NULL; wchar_t szUserProfileDir[MAX_PATH]; DWORD cchUserProfileDir = ARRAYSIZE(szUserProfileDir); STARTUPINFO si = { sizeof(si) }; PROCESS_INFORMATION pi = { 0 }; DWORD dwWaitResult; // Obtain the primary access token of the logged-on user specified by the // session ID. if (!WTSQueryUserToken(dwSessionId, &hToken)) { dwError = GetLastError(); goto Cleanup; } // Run the command line in the session that we found by using the default // values for working directory and desktop. // This creates the default environment block for the user. if (!CreateEnvironmentBlock(&lpvEnv, hToken, TRUE)) { dwError = GetLastError(); goto Cleanup; } // Retrieve the path to the root directory of the user's profile. if (!GetUserProfileDirectory(hToken, szUserProfileDir, &cchUserProfileDir)) { dwError = GetLastError(); goto Cleanup; } // Specify that the process runs in the interactive desktop. si.lpDesktop = L"winsta0\\default"; // Launch the process. if (!CreateProcessAsUser(hToken, NULL, pszCommandLine, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT, lpvEnv, szUserProfileDir, &si, &pi)) { dwError = GetLastError(); goto Cleanup; } if (fWait) { // Wait for the exit of the process. dwWaitResult = WaitForSingleObject(pi.hProcess, dwTimeout); if (dwWaitResult == WAIT_OBJECT_0) { // If the process exits before timeout, get the exit code. GetExitCodeProcess(pi.hProcess, pExitCode); } else if (dwWaitResult == WAIT_TIMEOUT) { // If it times out, terminiate the process. TerminateProcess(pi.hProcess, IDTIMEOUT); *pExitCode = IDTIMEOUT; } else { dwError = GetLastError(); goto Cleanup; } } else { *pExitCode = IDASYNC; } Cleanup: // Centralized cleanup for all allocated resources. if (hToken) { CloseHandle(hToken); hToken = NULL; } if (lpvEnv) { DestroyEnvironmentBlock(lpvEnv); lpvEnv = NULL; } if (pi.hProcess) { CloseHandle(pi.hProcess); pi.hProcess = NULL; } if (pi.hThread) { CloseHandle(pi.hThread); pi.hThread = NULL; } // Set the last error if something failed in the function. if (dwError != ERROR_SUCCESS) { SetLastError(dwError); return FALSE; } else { return TRUE; } }
VOID AFS_Logon_Event( PWLX_NOTIFICATION_INFO pInfo ) { TCHAR profileDir[1024] = TEXT(""); DWORD len = 1024; PTOKEN_USER tokenUser = NULL; DWORD retLen; WCHAR szUserW[128] = L""; char szUserA[128] = ""; char szClient[MAX_PATH]; char szPath[MAX_PATH] = ""; NETRESOURCE nr; DWORD res; DWORD dwSize; LogonOptions_t opt; /* Make sure the AFS Libraries are initialized */ AfsLogonInit(); DebugEvent0("AFS_Logon_Event - Start"); DebugEvent("AFS_Logon_Event Process ID: %d",GetCurrentProcessId()); memset(&opt, 0, sizeof(LogonOptions_t)); if (pInfo->UserName && pInfo->Domain) { char username[MAX_USERNAME_LENGTH] = ""; char domain[MAX_DOMAIN_LENGTH] = ""; size_t szlen = 0; DebugEvent0("AFS_Logon_Event - pInfo UserName and Domain"); StringCchLengthW(pInfo->UserName, MAX_USERNAME_LENGTH, &szlen); WideCharToMultiByte(CP_UTF8, 0, pInfo->UserName, szlen, username, sizeof(username), NULL, NULL); StringCchLengthW(pInfo->Domain, MAX_DOMAIN_LENGTH, &szlen); WideCharToMultiByte(CP_UTF8, 0, pInfo->Domain, szlen, domain, sizeof(domain), NULL, NULL); DebugEvent0("AFS_Logon_Event - Calling GetDomainLogonOptions"); GetDomainLogonOptions(NULL, username, domain, &opt); } else { if (!pInfo->UserName) DebugEvent0("AFS_Logon_Event - No pInfo->UserName"); if (!pInfo->Domain) DebugEvent0("AFS_Logon_Event - No pInfo->Domain"); } DebugEvent("AFS_Logon_Event - opt.LogonOption = %lX opt.flags = %lX", opt.LogonOption, opt.flags); if (!ISLOGONINTEGRATED(opt.LogonOption) || !ISREMOTE(opt.flags)) { DebugEvent0("AFS_Logon_Event - Logon is not integrated or not remote"); goto done_logon_event; } DebugEvent0("AFS_Logon_Event - Calling GetTokenInformation"); if (!GetTokenInformation(pInfo->hToken, TokenUser, NULL, 0, &retLen)) { if ( GetLastError() == ERROR_INSUFFICIENT_BUFFER ) { tokenUser = (PTOKEN_USER) LocalAlloc(LPTR, retLen); if (!GetTokenInformation(pInfo->hToken, TokenUser, tokenUser, retLen, &retLen)) { DebugEvent("AFS_Logon_Event - GetTokenInformation failed: GLE = %lX", GetLastError()); } } } /* We can't use pInfo->Domain for the domain since in the cross realm case * this is source domain and not the destination domain. */ if (tokenUser && QueryAdHomePathFromSid( profileDir, sizeof(profileDir), tokenUser->User.Sid, pInfo->Domain)) { WCHAR Domain[64]=L""; GetLocalShortDomain(Domain, sizeof(Domain)); if (QueryAdHomePathFromSid( profileDir, sizeof(profileDir), tokenUser->User.Sid, Domain)) { if (NetUserGetProfilePath(pInfo->Domain, pInfo->UserName, profileDir, len)) GetUserProfileDirectory(pInfo->hToken, profileDir, &len); } } if (strlen(profileDir)) { DebugEvent("AFS_Logon_Event - Profile Directory: %s", profileDir); } else { DebugEvent0("AFS_Logon_Event - Unable to load profile"); } done_logon_event: dwSize = sizeof(szUserA); if (!KFW_AFS_get_lsa_principal(szUserA, &dwSize)) { StringCbPrintfW(szUserW, sizeof(szUserW), L"%s\\%s", pInfo->Domain, pInfo->UserName); WideCharToMultiByte(CP_ACP, 0, szUserW, -1, szUserA, MAX_PATH, NULL, NULL); } if (szUserA[0]) { lana_GetNetbiosName(szClient, LANA_NETBIOS_NAME_FULL); StringCbPrintf(szPath, sizeof(szPath), "\\\\%s", szClient); DebugEvent("AFS_Logon_Event - Logon Name: %s", szUserA); memset (&nr, 0x00, sizeof(NETRESOURCE)); nr.dwType=RESOURCETYPE_DISK; nr.lpLocalName=0; nr.lpRemoteName=szPath; res = WNetAddConnection2(&nr,NULL,szUserA,0); if (res) DebugEvent("AFS_Logon_Event - WNetAddConnection2(%s,%s) failed: 0x%X", szPath, szUserA,res); else DebugEvent0("AFS_Logon_Event - WNetAddConnection2() succeeded"); } else DebugEvent("AFS_Logon_Event - User name conversion failed: GLE = 0x%X",GetLastError()); if ( tokenUser ) LocalFree(tokenUser); DebugEvent0("AFS_Logon_Event - End"); }
VOID AFS_Logoff_Event( PWLX_NOTIFICATION_INFO pInfo ) { DWORD code; TCHAR profileDir[1024] = TEXT(""); DWORD len = 1024; PTOKEN_USER tokenUser = NULL; DWORD retLen; DWORD LSPtype, LSPsize; HKEY NPKey; DWORD LogoffPreserveTokens = 0; LogonOptions_t opt; /* Make sure the AFS Libraries are initialized */ AfsLogonInit(); DebugEvent0("AFS_Logoff_Event - Start"); (void) RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY, 0, KEY_QUERY_VALUE, &NPKey); LSPsize=sizeof(LogoffPreserveTokens); RegQueryValueEx(NPKey, REG_CLIENT_LOGOFF_TOKENS_PARM, NULL, &LSPtype, (LPBYTE)&LogoffPreserveTokens, &LSPsize); RegCloseKey (NPKey); if (!LogoffPreserveTokens) { memset(&opt, 0, sizeof(LogonOptions_t)); if (pInfo->UserName && pInfo->Domain) { char username[MAX_USERNAME_LENGTH] = ""; char domain[MAX_DOMAIN_LENGTH] = ""; size_t szlen = 0; StringCchLengthW(pInfo->UserName, MAX_USERNAME_LENGTH, &szlen); WideCharToMultiByte(CP_UTF8, 0, pInfo->UserName, szlen, username, sizeof(username), NULL, NULL); StringCchLengthW(pInfo->Domain, MAX_DOMAIN_LENGTH, &szlen); WideCharToMultiByte(CP_UTF8, 0, pInfo->Domain, szlen, domain, sizeof(domain), NULL, NULL); GetDomainLogonOptions(NULL, username, domain, &opt); } if (ISREMOTE(opt.flags)) { if (!GetTokenInformation(pInfo->hToken, TokenUser, NULL, 0, &retLen)) { if ( GetLastError() == ERROR_INSUFFICIENT_BUFFER ) { tokenUser = (PTOKEN_USER) LocalAlloc(LPTR, retLen); if (!GetTokenInformation(pInfo->hToken, TokenUser, tokenUser, retLen, &retLen)) { DebugEvent("AFS_Logoff_Event - GetTokenInformation failed: GLE = %lX", GetLastError()); } } } /* We can't use pInfo->Domain for the domain since in the cross realm case * this is source domain and not the destination domain. */ if (tokenUser && QueryAdHomePathFromSid( profileDir, sizeof(profileDir), tokenUser->User.Sid, pInfo->Domain)) { WCHAR Domain[64]=L""; GetLocalShortDomain(Domain, sizeof(Domain)); if (QueryAdHomePathFromSid( profileDir, sizeof(profileDir), tokenUser->User.Sid, Domain)) { if (NetUserGetProfilePath(pInfo->Domain, pInfo->UserName, profileDir, len)) GetUserProfileDirectory(pInfo->hToken, profileDir, &len); } } if (strlen(profileDir)) { DebugEvent("AFS_Logoff_Event - Profile Directory: %s", profileDir); if (!IsPathInAfs(profileDir)) { if (code = ktc_ForgetAllTokens()) DebugEvent("AFS_Logoff_Event - ForgetAllTokens failed [%lX]",code); else DebugEvent0("AFS_Logoff_Event - ForgetAllTokens succeeded"); } else { DebugEvent0("AFS_Logoff_Event - Tokens left in place; profile in AFS"); } } else { DebugEvent0("AFS_Logoff_Event - Unable to load profile"); } if ( tokenUser ) LocalFree(tokenUser); } else { DebugEvent0("AFS_Logoff_Event - Local Logon"); if (code = ktc_ForgetAllTokens()) DebugEvent("AFS_Logoff_Event - ForgetAllTokens failed [%lX]",code); else DebugEvent0("AFS_Logoff_Event - ForgetAllTokens succeeded"); } } else { DebugEvent0("AFS_Logoff_Event - Preserving Tokens"); } DebugEvent0("AFS_Logoff_Event - End"); }