AccountManage.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include <Windows.h>
  4. #include <LMACCESS.H>
  5. #include <LMERR.H>
  6. #include <LMAPIBUF.H>
  7. #include <string>
  8. #include <vector>
  9. #include <boost\format.hpp>
  10. #include <sddl.h> /* for ConvertSidToStringSid function */
  11. #include "AccountManage.h"
  12. #define SYSTEM_ON(a,b) system(a)
  13. #pragma comment(lib,"netapi32.lib")
  14. using namespace std;
  15. namespace AccountManage {
  16. string convertwStr2str(wstring wstr)
  17. {
  18. size_t size;
  19. char *psMultiByte;
  20. size = WideCharToMultiByte(CP_ACP,0,wstr.c_str(),-1,NULL,0,NULL,NULL);
  21. psMultiByte = new char[size + 1];
  22. WideCharToMultiByte(CP_ACP,0,wstr.c_str(),-1,psMultiByte,size,NULL,NULL);
  23. return psMultiByte;
  24. }
  25. wstring convertStr2wStr(string str)
  26. {
  27. size_t size;
  28. size = MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,NULL,0);
  29. wchar_t *pwsWideChar = new wchar_t[size + 1];
  30. MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,pwsWideChar,size);
  31. return pwsWideChar;
  32. }
  33. int AdjustACL(LPCWSTR pUserName,LPCWSTR pFile,DWORD dwAccassRights,ACCESS_MODE accessMode)
  34. {
  35. DWORD dwRes = 0;
  36. PACL pOldDACL =NULL,pNewDACL=NULL;
  37. PSECURITY_DESCRIPTOR pSD = NULL;
  38. EXPLICIT_ACCESS_W ea;
  39. dwRes = GetNamedSecurityInfoW(pFile,SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,NULL,NULL,&pOldDACL,NULL,&pSD);
  40. if(dwRes != ERROR_SUCCESS){
  41. Dbg("get named SecurityInfo FAIL!");
  42. goto ret;
  43. }
  44. ZeroMemory(&ea,sizeof(EXPLICIT_ACCESS));
  45. ea.grfAccessPermissions = dwAccassRights;
  46. ea.grfAccessMode = accessMode;
  47. ea.grfInheritance = NO_INHERITANCE;
  48. ea.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
  49. ea.Trustee.ptstrName = const_cast<LPWSTR>(pUserName);
  50. dwRes = SetEntriesInAclW(1,&ea,NULL,&pNewDACL);
  51. if(dwRes != ERROR_SUCCESS){
  52. Dbg("set entries ACL FAIL!");
  53. goto ret;
  54. }
  55. dwRes = SetNamedSecurityInfoW(const_cast<LPWSTR>(pFile),SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,NULL,NULL,pNewDACL,NULL);
  56. if(dwRes != ERROR_SUCCESS){
  57. Dbg("set named SecurityInfo FAIL!");
  58. goto ret;
  59. }
  60. Dbg("%s adjust ACL SUSS!",convertwStr2str(pUserName).c_str());
  61. return 0;
  62. ret:
  63. if (pSD!=NULL)
  64. {
  65. LocalFree((HLOCAL) pSD);
  66. }
  67. if(pNewDACL != NULL)
  68. {
  69. LocalFree((HLOCAL) pNewDACL);
  70. }
  71. return -1;
  72. }
  73. bool ComfirmUserPropertiy(LPCWSTR pUserName)
  74. {
  75. PUSER_INFO_1 pUsr = NULL;
  76. NET_API_STATUS netRet = 0;
  77. DWORD dwParamError = 0;
  78. netRet = NetUserGetInfo(NULL, pUserName, 1, (LPBYTE*)&pUsr);
  79. if(netRet == NERR_Success)
  80. {
  81. pUsr->usri1_flags = (pUsr->usri1_flags | UF_DONT_EXPIRE_PASSWD);
  82. pUsr->usri1_flags = (pUsr->usri1_flags & ((DWORD)(~UF_PASSWD_CANT_CHANGE)));
  83. netRet = NetUserSetInfo(NULL, pUserName, 1, (LPBYTE)pUsr, &dwParamError);
  84. NetApiBufferFree(pUsr);
  85. if(netRet == NERR_Success)
  86. {
  87. Dbg("Set user flag succeed.");
  88. return true;
  89. } else {
  90. Dbg("NetUserSetInfo failed: %d", netRet);
  91. }
  92. } else {
  93. Dbg("NetUserGetInfo failed: %d", netRet);
  94. }
  95. return false;
  96. }
  97. bool CheckAccount(const std::string accountName)
  98. {
  99. vector<string> sysAccounts;
  100. int errCode;
  101. string errStr;
  102. if (!AccountManage::GetAllAccount(sysAccounts, errCode))
  103. {
  104. Dbg("GetAllAccount errCode=%d",errCode);
  105. return false;
  106. }
  107. Dbg("sysAccounts.sizesize=%d", sysAccounts.size());
  108. for (int i=0;i<sysAccounts.size();i++)
  109. {
  110. Dbg("sysAccounts [%d]=%s", i,sysAccounts[i].c_str());
  111. if (accountName == sysAccounts[i])
  112. return true;
  113. }
  114. return false;
  115. }
  116. bool EnableAdministrator()
  117. {
  118. return (ERROR_SUCCESS == SYSTEM_ON("net user administrator /active:yes", true) && (ERROR_SUCCESS == SYSTEM_ON("net user administrator 11111111", true)));
  119. }
  120. bool GetAllAccount(vector<string> &AccountArr,int& errCode)
  121. {
  122. LPUSER_INFO_1 pBuf = NULL;
  123. LPUSER_INFO_1 pTmpBuf;
  124. DWORD dwLevel = 1;
  125. DWORD dwPrefMaxLen = -1;
  126. DWORD dwEntriesRead = 0;
  127. DWORD dwTotalEntries = 0;
  128. DWORD dwResumeHandle = 0;
  129. NET_API_STATUS nStatus;
  130. LPCWSTR pszServerName = NULL;
  131. nStatus = NetUserEnum((LPCWSTR)pszServerName, dwLevel, FILTER_NORMAL_ACCOUNT,
  132. (LPBYTE*)& pBuf, dwPrefMaxLen, &dwEntriesRead, &dwTotalEntries, &dwResumeHandle);
  133. if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
  134. {
  135. if ((pTmpBuf = pBuf) != NULL)
  136. {
  137. for (DWORD i = 0; i < dwEntriesRead; ++i)
  138. {
  139. string csFlag = (boost::format("%s,%ld") % pTmpBuf->usri1_name % pBuf->usri1_priv).str();
  140. //if (pTmpBuf->usri1_priv == USER_PRIV_ADMIN || pTmpBuf->usri1_priv == USER_PRIV_USER && (wstring(pTmpBuf->usri1_name) != L"ASPNET"))
  141. AccountArr.push_back(convertwStr2str(pTmpBuf->usri1_name));
  142. pTmpBuf++;
  143. }
  144. }
  145. }
  146. else
  147. {
  148. errCode = nStatus;
  149. return false;
  150. }
  151. if (pBuf != NULL)
  152. {
  153. NetApiBufferFree(pBuf);
  154. pBuf = NULL;
  155. }
  156. if (pBuf != NULL)
  157. NetApiBufferFree(pBuf);
  158. return TRUE;
  159. }
  160. bool GetAccountSid(const std::string accountName, std::string& accountSid)
  161. {
  162. LPUSER_INFO_4 pBuf = NULL;
  163. LPTSTR sStringSid = NULL;
  164. std::wstring t_accountName = convertStr2wStr(accountName);
  165. if (NERR_Success == NetUserGetInfo(NULL, t_accountName.c_str(), 4, (LPBYTE*)& pBuf))
  166. {
  167. if (ConvertSidToStringSid(pBuf->usri4_user_sid, &sStringSid))
  168. {
  169. accountSid = convertwStr2str(reinterpret_cast<wchar_t *>(sStringSid));
  170. LocalFree(sStringSid);
  171. }
  172. else
  173. wprintf(L"ConvertSidToSTringSid failed with error %d\n", GetLastError());
  174. }
  175. if (pBuf != NULL)
  176. NetApiBufferFree(pBuf);
  177. if (accountSid.length() > 0)
  178. return TRUE;
  179. else
  180. return FALSE;
  181. }
  182. bool AddNewAccount(std::string AccountName, std::string AccountPsw, int& errCode)
  183. {
  184. USER_INFO_1 ui;
  185. DWORD dwError = 0;
  186. wstring tempName = convertStr2wStr(AccountName), tempPwd = convertStr2wStr(AccountPsw);
  187. WCHAR comment[MAX_PATH] = L"";
  188. ui.usri1_name = const_cast<WCHAR*>(tempName.c_str());
  189. ui.usri1_password = const_cast<WCHAR*>(tempPwd.c_str());
  190. ui.usri1_priv = USER_PRIV_USER;
  191. ui.usri1_home_dir = NULL;
  192. ui.usri1_comment = comment;
  193. ui.usri1_flags = UF_SCRIPT | UF_DONT_EXPIRE_PASSWD;
  194. ui.usri1_script_path = NULL;
  195. NET_API_STATUS re = NetUserAdd(NULL, 1, (LPBYTE)& ui, &dwError);
  196. errCode = re;
  197. //if (re != NERR_Success && re != NERR_UserExists)
  198. if(re != NERR_Success)
  199. return FALSE;
  200. LOCALGROUP_MEMBERS_INFO_3 account;
  201. account.lgrmi3_domainandname = const_cast<WCHAR*>(tempName.c_str());
  202. re = NetLocalGroupAddMembers(NULL, L"Users", 3, (LPBYTE)& account, 1);
  203. if (re == NERR_Success || re == ERROR_MEMBER_IN_ALIAS)
  204. return TRUE;
  205. else
  206. return FALSE;
  207. }
  208. bool ExtendGroupAdd(std::string accountName, std::string groupName)
  209. {
  210. LOCALGROUP_MEMBERS_INFO_3 account;
  211. wstring tempName = convertStr2wStr(accountName), tempGroup = convertStr2wStr(groupName);
  212. account.lgrmi3_domainandname = const_cast<WCHAR*>(tempName.c_str());
  213. NET_API_STATUS re = NetLocalGroupAddMembers(NULL, tempGroup.c_str(), 3, (LPBYTE)& account, 1);
  214. if (re == NERR_Success || re == ERROR_MEMBER_IN_ALIAS)
  215. return TRUE;
  216. else
  217. return FALSE;
  218. }
  219. bool AddCmbUser()
  220. {
  221. int errCode;
  222. return AddNewAccount(CMBUSER_NAME, CMBUSER_TEMP_PSW, errCode);
  223. }
  224. bool AddClientUser()
  225. {
  226. int errCode;
  227. return AddNewAccount(CLIENTUSER_NAME, CLIENTUSER_TEMP_PSW, errCode);
  228. }
  229. bool RmAccount(std::string AccountName, int& errCode)
  230. {
  231. wstring tempName = convertStr2wStr(AccountName);
  232. errCode = NetUserDel(NULL, tempName.c_str());
  233. if (errCode == NERR_Success)
  234. return TRUE;
  235. else
  236. return FALSE;
  237. }
  238. bool InitUser(std::string AccountName, std::string AccountPwd, std::string processPath)
  239. {
  240. if (!startProcess(AccountName, "", AccountPwd, "c:\\windows\\explorer.exe"))
  241. return FALSE;
  242. if (0 == processPath.length())
  243. return TRUE;
  244. std::string exePath = (boost::format("%s\\%s") % processPath % INIT_PROCESS).str();
  245. if (!startProcess(AccountName, "", AccountPwd, exePath))
  246. return FALSE;
  247. return TRUE;
  248. }
  249. bool ModefyUserPsw(std::string AccountName, std::string oldPwd, std::string newPwd)
  250. {
  251. wstring newAccountName = convertStr2wStr(AccountName), oldAccountPwd = convertStr2wStr(oldPwd), newAccountPwd = convertStr2wStr(newPwd);
  252. int res= NetUserChangePassword(NULL, newAccountName.c_str(), oldAccountPwd.c_str(), newAccountPwd.c_str());
  253. Dbg("ModefyUserPsw res %d",res);
  254. if(res == ERROR_ACCESS_DENIED) {
  255. ComfirmUserPropertiy(newAccountName.c_str());
  256. }
  257. return NERR_Success == res;
  258. }
  259. bool startProcess(std::string userName, std::string domain, std::string password, std::string commandLine)
  260. {
  261. PROCESS_INFORMATION processInfo;
  262. STARTUPINFOW startupInfo;
  263. ZeroMemory(&processInfo, sizeof(processInfo));
  264. ZeroMemory(&startupInfo, sizeof(startupInfo));
  265. startupInfo.cb = sizeof(STARTUPINFO);
  266. startupInfo.lpTitle = NULL;
  267. startupInfo.dwFlags = STARTF_USECOUNTCHARS;
  268. startupInfo.dwYCountChars = 50;
  269. wstring cmdLine = convertStr2wStr(commandLine);
  270. BOOL retval = CreateProcessWithLogonW(convertStr2wStr(userName).c_str(), NULL, convertStr2wStr(password).c_str(),
  271. LOGON_WITH_PROFILE, NULL, const_cast<WCHAR*>(cmdLine.c_str()), CREATE_NEW_CONSOLE, NULL,
  272. NULL, &startupInfo, &processInfo);
  273. if (retval)
  274. {
  275. CloseHandle(processInfo.hProcess);
  276. CloseHandle(processInfo.hThread);
  277. return TRUE;
  278. }
  279. return FALSE;
  280. }
  281. }