Guest User

Untitled

a guest
May 26th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. // user_test.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "userenv.h"
  6. #include "lm.h"
  7. #pragma comment(lib, "userenv.lib")
  8. #pragma comment(lib, "Netapi32.lib")
  9.  
  10. int main()
  11. {
  12. USER_INFO_1 ui1;
  13. DWORD dwError;
  14. HANDLE hToken;
  15. PROFILEINFO pi;
  16.  
  17. // Set up the USER_INFO_1 structure that will be used to create the
  18. // new user account
  19. ZeroMemory(&ui1, sizeof(ui1));
  20. ui1.usri1_name = _T("testuser");
  21. ui1.usri1_password = _T("123456789");
  22. ui1.usri1_priv = USER_PRIV_USER;
  23. ui1.usri1_flags = UF_SCRIPT;
  24.  
  25. // Create the new user account
  26. dwError = NetUserAdd(
  27. NULL, // target computer name
  28. 1, // info level
  29. (LPBYTE)&ui1, // address of user info structure
  30. NULL); // index to invalid parameter
  31. if (dwError != NERR_Success) {
  32. _tprintf(_T("NetUserAdd() failed. Error %d\n"), dwError);
  33. dwError = ERROR_ACCESS_DENIED;
  34. return -1;
  35. }
  36.  
  37. // Do a network logon because most systems do not grant new users
  38. // the right to logon interactively (SE_INTERACTIVE_LOGON_NAME)
  39. // but they do grant the right to do a network logon
  40. // (SE_NETWORK_LOGON_NAME). A network logon has the added advantage
  41. // of being quicker.
  42.  
  43. // NOTE: To call LogonUser(), the current user must have the
  44. // SE_TCB_NAME privilege
  45. if (!LogonUser(
  46. ui1.usri1_name, // user name
  47. _T("."), // domain or server
  48. ui1.usri1_password, // password
  49. LOGON32_LOGON_NETWORK, // type of logon operation
  50. LOGON32_PROVIDER_DEFAULT, // logon provider
  51. &hToken)) { // pointer to token handle
  52. _tprintf(_T("LogonUser() failed. Error %d\n"), GetLastError());
  53. return -1;
  54. }
  55.  
  56. // Set up the PROFILEINFO structure that will be used to load the
  57. // new user's profile
  58. ZeroMemory(&pi, sizeof(pi));
  59. pi.dwSize = sizeof(pi);
  60.  
  61. pi.lpUserName = ui1.usri1_name;
  62. pi.dwFlags = PI_NOUI;
  63.  
  64. // Load the profile. Since it doesn't exist, it will be created
  65. if (!LoadUserProfile(
  66. hToken, // token for the user
  67. &pi)) { // pointer to PROFILEINFO structure
  68. _tprintf(_T("LoadUserProfile() failed. Error %d\n"),
  69. GetLastError());
  70. return -1;
  71. }
  72.  
  73. PROCESS_INFORMATION procInfo;
  74.  
  75. if (!CreateProcessAsUser(
  76. hToken,
  77. _T("git.exe"),
  78. _T("-version"),
  79. NULL,
  80. NULL,
  81. false,
  82. 0,
  83. NULL,
  84. NULL,
  85. NULL,
  86. &procInfo
  87. )) {
  88. _tprintf(_T("CreateProcessAsUser() failed. Error %d\n"),
  89. GetLastError());
  90. return -1;
  91. }
  92.  
  93. // Unload the profile when it is no longer needed
  94. if (!UnloadUserProfile(
  95. hToken, // token for the user
  96. pi.hProfile)) { // registry key handle
  97. _tprintf(_T("UnloadUserProfile() failed. Error %d\n"),
  98. GetLastError());
  99. return -1;
  100. }
  101. _tprintf(_T("I guess it worked\n"));
  102. return 0;
  103. }
Add Comment
Please, Sign In to add comment