|
| 1 | +/********************************************************************************** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2018 Antoine Beauchamp |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + *********************************************************************************/ |
| 24 | + |
| 25 | +#include "ArgumentsHandler.h" |
| 26 | +#include "rapidassist/process.h" |
| 27 | +#include "rapidassist/environment.h" |
| 28 | +#include "rapidassist/filesystem.h" |
| 29 | + |
| 30 | +#ifndef WIN32_LEAN_AND_MEAN |
| 31 | +#define WIN32_LEAN_AND_MEAN 1 |
| 32 | +#endif |
| 33 | +#include <Windows.h> |
| 34 | +#undef GetEnvironmentVariable |
| 35 | +#undef DeleteFile |
| 36 | +#undef CreateDirectory |
| 37 | +#undef CopyFile |
| 38 | +#undef CreateFile |
| 39 | +#undef GetCurrentDirectory |
| 40 | + |
| 41 | + |
| 42 | +namespace shellanything |
| 43 | +{ |
| 44 | + |
| 45 | + //https://stackoverflow.com/questions/8046097/how-to-check-if-a-process-has-the-administrative-rights |
| 46 | + bool IsProcessElevated() |
| 47 | + { |
| 48 | + BOOL fRet = FALSE; |
| 49 | + HANDLE hToken = NULL; |
| 50 | + if( OpenProcessToken( GetCurrentProcess( ),TOKEN_QUERY,&hToken ) ) |
| 51 | + { |
| 52 | + TOKEN_ELEVATION Elevation; |
| 53 | + DWORD cbSize = sizeof( TOKEN_ELEVATION ); |
| 54 | + if( GetTokenInformation( hToken, TokenElevation, &Elevation, sizeof( Elevation ), &cbSize ) ) |
| 55 | + { |
| 56 | + fRet = Elevation.TokenIsElevated; |
| 57 | + } |
| 58 | + } |
| 59 | + if( hToken ) |
| 60 | + { |
| 61 | + CloseHandle( hToken ); |
| 62 | + } |
| 63 | + return (fRet == TRUE); |
| 64 | + } |
| 65 | + |
| 66 | + //http://blog.aaronballman.com/2011/08/how-to-check-access-rights/ |
| 67 | + bool CanAccessFolder( LPCTSTR folderName, DWORD genericAccessRights ) |
| 68 | + { |
| 69 | + bool bRet = false; |
| 70 | + DWORD length = 0; |
| 71 | + if (!::GetFileSecurity( folderName, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, NULL, NULL, &length ) && ERROR_INSUFFICIENT_BUFFER == ::GetLastError()) |
| 72 | + { |
| 73 | + PSECURITY_DESCRIPTOR security = static_cast< PSECURITY_DESCRIPTOR >( ::malloc( length ) ); |
| 74 | + if (security && ::GetFileSecurity( folderName, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, security, length, &length )) |
| 75 | + { |
| 76 | + HANDLE hToken = NULL; |
| 77 | + if (::OpenProcessToken( ::GetCurrentProcess(), TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE | STANDARD_RIGHTS_READ, &hToken )) |
| 78 | + { |
| 79 | + HANDLE hImpersonatedToken = NULL; |
| 80 | + if (::DuplicateToken( hToken, SecurityImpersonation, &hImpersonatedToken )) |
| 81 | + { |
| 82 | + GENERIC_MAPPING mapping = { 0xFFFFFFFF }; |
| 83 | + PRIVILEGE_SET privileges = { 0 }; |
| 84 | + DWORD grantedAccess = 0, privilegesLength = sizeof( privileges ); |
| 85 | + BOOL result = FALSE; |
| 86 | + |
| 87 | + mapping.GenericRead = FILE_GENERIC_READ; |
| 88 | + mapping.GenericWrite = FILE_GENERIC_WRITE; |
| 89 | + mapping.GenericExecute = FILE_GENERIC_EXECUTE; |
| 90 | + mapping.GenericAll = FILE_ALL_ACCESS; |
| 91 | + |
| 92 | + ::MapGenericMask( &genericAccessRights, &mapping ); |
| 93 | + if (::AccessCheck( security, hImpersonatedToken, genericAccessRights, &mapping, &privileges, &privilegesLength, &grantedAccess, &result )) |
| 94 | + { |
| 95 | + bRet = (result == TRUE); |
| 96 | + } |
| 97 | + ::CloseHandle( hImpersonatedToken ); |
| 98 | + } |
| 99 | + ::CloseHandle( hToken ); |
| 100 | + } |
| 101 | + ::free( security ); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return bRet; |
| 106 | + } |
| 107 | + |
| 108 | + int PrintStatistics() |
| 109 | + { |
| 110 | + std::string process_path = ra::process::GetCurrentProcessPath(); |
| 111 | + ra::process::processid_t process_id = ra::process::GetCurrentProcessId(); |
| 112 | + std::string process_current_directory = ra::filesystem::GetCurrentDirectory(); |
| 113 | + int process_architecture = (ra::environment::IsProcess64Bit() ? 64 : 86); |
| 114 | + int process_elevated = (IsProcessElevated() ? 1 : 0); |
| 115 | + const char * build_configuration = (ra::environment::IsConfigurationDebug() ? "debug" : "release"); |
| 116 | + std::string env_temp = ra::environment::GetEnvironmentVariable("TEMP"); |
| 117 | + bool has_windows_write_access = CanAccessFolder("C:\\Windows", GENERIC_WRITE); |
| 118 | + |
| 119 | + printf("process.path=%s\n", process_path.c_str()); |
| 120 | + printf("process.pid=%d\n", process_id); |
| 121 | + printf("process.current_dir=%s\n", process_current_directory.c_str()); |
| 122 | + printf("process.architecture=x%d\n", process_architecture); |
| 123 | + printf("process.elevated=%d\n", process_elevated); |
| 124 | + printf("build.configuration=%s\n", build_configuration); |
| 125 | + printf("env.temp=%s\n", env_temp.c_str()); |
| 126 | + printf("has_windows_write_access=%d\n", (int)has_windows_write_access); |
| 127 | + |
| 128 | + return 0; |
| 129 | + } |
| 130 | + |
| 131 | +} //namespace shellanything |
0 commit comments