netman.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * --- END COPYRIGHT BLOCK --- */
  37. // ****************************************************************
  38. // netman.cpp
  39. // ****************************************************************
  40. #include "netman.h"
  41. // ****************************************************************
  42. // UTF16ToUTF8
  43. // ****************************************************************
  44. char* UTF16ToUTF8(unsigned short* inString)
  45. {
  46. int length = WideCharToMultiByte(CP_ACP, 0, inString, -1, 0, 0, 0, 0);
  47. char* outString = NULL;
  48. outString = (char*)malloc(length);
  49. WideCharToMultiByte(CP_ACP, 0, inString, -1, outString, length, 0, 0);
  50. return outString;
  51. }
  52. // ****************************************************************
  53. // UTF8ToUTF16
  54. // ****************************************************************
  55. unsigned short* UTF8ToUTF16(char* inString)
  56. {
  57. unsigned short* outString = NULL;
  58. int length = MultiByteToWideChar(CP_ACP, 0, inString, -1, 0, 0);
  59. outString = (unsigned short*)malloc(length * 2);
  60. MultiByteToWideChar(CP_ACP, 0, inString, -1, outString, length);
  61. return outString;
  62. }
  63. // ****************************************************************
  64. // SIDToHexStr
  65. // ****************************************************************
  66. int SIDToHexStr(char* sid, unsigned long sidLen, char** hexStr)
  67. {
  68. int hexStrLen = sidLen * 2 + 1;
  69. *hexStr = (char*)calloc(hexStrLen, sizeof(char));
  70. for(unsigned long i = 0; i < sidLen; i++)
  71. {
  72. sprintf(&(*hexStr)[i * 2], "%02X", (unsigned char)sid[i]);
  73. }
  74. return 0;
  75. }
  76. // ****************************************************************
  77. // HexStrToSID
  78. // ****************************************************************
  79. int HexStrToSID(char* hexStr, char** sid, unsigned long* sidLen)
  80. {
  81. int temp;
  82. *sidLen = strlen(hexStr) / 2;
  83. *sid = (char*)malloc(*sidLen);
  84. for(unsigned long i = 0; i < *sidLen; i++)
  85. {
  86. sscanf(&hexStr[i * 2], "%02X", &temp);
  87. (*sid)[i] = (unsigned char)temp;
  88. }
  89. return 0;
  90. }
  91. // ****************************************************************
  92. // GetSIDByAccountName
  93. // ****************************************************************
  94. int GetSIDByAccountName(char* accountName, char** sid)
  95. {
  96. int result = 0;
  97. unsigned long sidLen = 0;
  98. char* domain;
  99. unsigned long domainLen = 0;
  100. SID_NAME_USE testType;
  101. if(LookupAccountName(NULL, accountName, NULL, &sidLen, NULL, &domainLen, &testType) == 0)
  102. {
  103. result = GetLastError();
  104. }
  105. *sid = (char*)malloc(sidLen);
  106. domain = (char*)malloc(domainLen);
  107. if(LookupAccountName(NULL, accountName, *sid, &sidLen, domain, &domainLen, &testType) == 0)
  108. {
  109. result = GetLastError();
  110. }
  111. else
  112. {
  113. result = 0;
  114. }
  115. return result;
  116. }
  117. // ****************************************************************
  118. // GetAccountNameBySID
  119. // ****************************************************************
  120. int GetAccountNameBySID(char* sid, char** accountName)
  121. {
  122. int result = 0;
  123. unsigned long sidLen = 0;
  124. char* domain;
  125. unsigned long domainLen = 0;
  126. SID_NAME_USE testType;
  127. unsigned long accountNameLen = 0;
  128. if(LookupAccountSid(NULL, sid, NULL, &accountNameLen, NULL, &domainLen, &testType) == 0)
  129. {
  130. result = GetLastError();
  131. }
  132. domain = (char*)malloc(domainLen);
  133. *accountName = (char*)calloc(accountNameLen, sizeof(char));
  134. if(LookupAccountSid(NULL, sid, *accountName, &accountNameLen, domain, &domainLen, &testType) == 0)
  135. {
  136. result = GetLastError();
  137. }
  138. else
  139. {
  140. result = 0;
  141. }
  142. return result;
  143. }
  144. // ****************************************************************
  145. // GetSIDHexStrByAccountName
  146. // ****************************************************************
  147. int GetSIDHexStrByAccountName(char* accountName, char** sidHexStr)
  148. {
  149. int result = 0;
  150. char* sid;
  151. unsigned long sidLen = 0;
  152. char* domain;
  153. unsigned long domainLen = 0;
  154. SID_NAME_USE testType;
  155. if(LookupAccountName(NULL, accountName, NULL, &sidLen, NULL, &domainLen, &testType) == 0)
  156. {
  157. result = GetLastError();
  158. }
  159. sid = (char*)malloc(sidLen);
  160. domain = (char*)malloc(domainLen);
  161. if(LookupAccountName(NULL, accountName, sid, &sidLen, domain, &domainLen, &testType) == 0)
  162. {
  163. result = GetLastError();
  164. }
  165. else
  166. {
  167. result = 0;
  168. }
  169. SIDToHexStr(sid, sidLen, sidHexStr);
  170. return result;
  171. }
  172. // ****************************************************************
  173. // GetAccountNameBySIDHexStr
  174. // ****************************************************************
  175. int GetAccountNameBySIDHexStr(char* sidHexStr, char** accountName)
  176. {
  177. int result = 0;
  178. char* sid;
  179. unsigned long sidLen = 0;
  180. char* domain;
  181. unsigned long domainLen = 0;
  182. SID_NAME_USE testType;
  183. unsigned long accountNameLen = 0;
  184. HexStrToSID(sidHexStr, &sid, &sidLen);
  185. if(LookupAccountSid(NULL, sid, NULL, &accountNameLen, NULL, &domainLen, &testType) == 0)
  186. {
  187. result = GetLastError();
  188. }
  189. domain = (char*)malloc(domainLen);
  190. *accountName = (char*)calloc(accountNameLen, sizeof(char));
  191. if(LookupAccountSid(NULL, sid, *accountName, &accountNameLen, domain, &domainLen, &testType) == 0)
  192. {
  193. result = GetLastError();
  194. }
  195. else
  196. {
  197. result = 0;
  198. }
  199. return result;
  200. }
  201. // ****************************************************************
  202. // NTUser::NTUser
  203. // ****************************************************************
  204. NTUser::NTUser()
  205. {
  206. userInfo = NULL;
  207. groupsInfo = NULL;
  208. currentGroupEntry = 0;
  209. groupEntriesRead = 0;
  210. groupEntriesTotal = 0;
  211. localGroupsInfo = NULL;
  212. currentLocalGroupEntry = 0;
  213. localGroupEntriesRead = 0;
  214. localGroupEntriesTotal = 0;
  215. }
  216. // ****************************************************************
  217. // NTUser::~NTUser
  218. // ****************************************************************
  219. NTUser::~NTUser()
  220. {
  221. if(userInfo != NULL)
  222. {
  223. NetApiBufferFree(userInfo);
  224. userInfo = NULL;
  225. }
  226. if(groupsInfo != NULL)
  227. {
  228. NetApiBufferFree(groupsInfo);
  229. groupsInfo = NULL;
  230. }
  231. if(localGroupsInfo != NULL)
  232. {
  233. NetApiBufferFree(localGroupsInfo);
  234. localGroupsInfo = NULL;
  235. }
  236. }
  237. // ****************************************************************
  238. // NTUser::NewUser
  239. // ****************************************************************
  240. void NTUser::NewUser(char* username)
  241. {
  242. if(userInfo != NULL)
  243. {
  244. NetApiBufferFree(userInfo);
  245. userInfo = NULL;
  246. }
  247. userInfo = (USER_INFO_3*)malloc(sizeof(USER_INFO_3));
  248. memset(userInfo, 0, sizeof(USER_INFO_3));
  249. userInfo->usri3_name = UTF8ToUTF16(username);
  250. // Possible required inits for AddUser
  251. //userInfo->usri3_priv = USER_PRIV_USER;
  252. //userInfo->usri3_home_dir = NULL;
  253. //userInfo->usri3_comment = NULL;
  254. //userInfo->usri3_script_path = NULL;
  255. // NT4 required inits for AddUser
  256. userInfo->usri3_flags = UF_SCRIPT;
  257. userInfo->usri3_primary_group_id = DOMAIN_GROUP_RID_USERS;
  258. // Other inits
  259. userInfo->usri3_acct_expires = (unsigned long)-1;
  260. }
  261. // ****************************************************************
  262. // NTUser::RetriveUserByUsername
  263. // ****************************************************************
  264. int NTUser::RetriveUserByAccountName(char* username)
  265. {
  266. int result;
  267. if(userInfo != NULL)
  268. {
  269. NetApiBufferFree(userInfo);
  270. userInfo = NULL;
  271. }
  272. result = NetUserGetInfo(NULL, UTF8ToUTF16(username), USER_INFO_LEVEL, (unsigned char**)&userInfo);
  273. return result;
  274. }
  275. // ****************************************************************
  276. // NTUser::RetriveUserBySIDHexStr
  277. // ****************************************************************
  278. int NTUser::RetriveUserBySIDHexStr(char* sidHexStr)
  279. {
  280. int result = 0;
  281. char* username;
  282. if(userInfo != NULL)
  283. {
  284. NetApiBufferFree(userInfo);
  285. userInfo = NULL;
  286. }
  287. if(GetAccountNameBySIDHexStr(sidHexStr, &username) != 0)
  288. {
  289. result = -1;
  290. goto exit;
  291. }
  292. if(RetriveUserByAccountName(username) != 0)
  293. {
  294. result =-1;
  295. goto exit;
  296. }
  297. exit:
  298. return result;
  299. }
  300. // ****************************************************************
  301. // NTUser::StoreUser
  302. // ****************************************************************
  303. int NTUser::StoreUser()
  304. {
  305. int result = 0;
  306. if(userInfo != NULL)
  307. {
  308. result = NetUserSetInfo(NULL, userInfo->usri3_name, USER_INFO_LEVEL, (unsigned char*)userInfo, NULL);
  309. }
  310. else
  311. {
  312. result = -1;
  313. }
  314. return result;
  315. }
  316. // ****************************************************************
  317. // NTUser::AddUser
  318. // ****************************************************************
  319. int NTUser::AddUser()
  320. {
  321. int result;
  322. DWORD badParam = 0;
  323. result = NetUserAdd(NULL, USER_INFO_LEVEL, (unsigned char*)userInfo, &badParam);
  324. return result;
  325. }
  326. // ****************************************************************
  327. // NTUser::DeleteUser
  328. // ****************************************************************
  329. int NTUser::DeleteUser(char* username)
  330. {
  331. int result;
  332. result = NetUserDel(NULL, UTF8ToUTF16(username));
  333. return result;
  334. }
  335. // ****************************************************************
  336. // NTUser::ChangeUsername
  337. // ****************************************************************
  338. int NTUser::ChangeUsername(char* oldUsername, char* newUsername)
  339. {
  340. int result;
  341. if((result = RetriveUserByAccountName(oldUsername)) == 0)
  342. {
  343. userInfo->usri3_name = UTF8ToUTF16(newUsername);
  344. if((result = AddUser()) == 0)
  345. {
  346. if((result = DeleteUser(oldUsername)) != 0)
  347. {
  348. DeleteUser(newUsername);
  349. }
  350. }
  351. }
  352. return result;
  353. }
  354. // ****************************************************************
  355. // NTUser::GetAccountName
  356. // ****************************************************************
  357. char* NTUser::GetAccountName()
  358. {
  359. char* result = NULL;
  360. if(userInfo != NULL)
  361. {
  362. result = UTF16ToUTF8(userInfo->usri3_name);
  363. }
  364. return result;
  365. }
  366. // ****************************************************************
  367. // NTUser::GetSIDHexStr
  368. // ****************************************************************
  369. char* NTUser::GetSIDHexStr()
  370. {
  371. char* result = NULL;
  372. if(userInfo != NULL)
  373. {
  374. GetSIDHexStrByAccountName(UTF16ToUTF8(userInfo->usri3_name), &result);
  375. }
  376. return result;
  377. }
  378. // ****************************************************************
  379. // NTUser::GetAccountExpires
  380. // ****************************************************************
  381. unsigned long NTUser::GetAccountExpires()
  382. {
  383. unsigned long result = 0;
  384. if(userInfo != NULL)
  385. {
  386. result = userInfo->usri3_acct_expires;
  387. }
  388. return result;
  389. }
  390. // ****************************************************************
  391. // NTUser::SetAccountExpires
  392. // ****************************************************************
  393. int NTUser::SetAccountExpires(unsigned long accountExpires)
  394. {
  395. int result = 0;
  396. if(userInfo != NULL)
  397. {
  398. userInfo->usri3_acct_expires = accountExpires;
  399. }
  400. else
  401. {
  402. result = -1;
  403. }
  404. return result;
  405. }
  406. // ****************************************************************
  407. // NTUser::GetBadPasswordCount
  408. // ****************************************************************
  409. unsigned long NTUser::GetBadPasswordCount()
  410. {
  411. unsigned long result = 0;
  412. if(userInfo != NULL)
  413. {
  414. result = userInfo->usri3_bad_pw_count;
  415. }
  416. return result;
  417. }
  418. // ****************************************************************
  419. // NTUser::GetCodePage
  420. // ****************************************************************
  421. unsigned long NTUser::GetCodePage()
  422. {
  423. unsigned long result = 0;
  424. if(userInfo != NULL)
  425. {
  426. result = userInfo->usri3_code_page;
  427. }
  428. return result;
  429. }
  430. // ****************************************************************
  431. // NTUser::SetCodePage
  432. // ****************************************************************
  433. int NTUser::SetCodePage(unsigned long codePage)
  434. {
  435. int result = 0;
  436. if(userInfo != NULL)
  437. {
  438. userInfo->usri3_code_page = codePage;
  439. }
  440. else
  441. {
  442. result = -1;
  443. }
  444. return result;
  445. }
  446. // ****************************************************************
  447. // NTUser::GetComment
  448. // ****************************************************************
  449. char* NTUser::GetComment()
  450. {
  451. char* result = NULL;
  452. if(userInfo != NULL)
  453. {
  454. result = UTF16ToUTF8(userInfo->usri3_comment);
  455. }
  456. return result;
  457. }
  458. // ****************************************************************
  459. // NTUser::SetComment
  460. // ****************************************************************
  461. int NTUser::SetComment(char* comment)
  462. {
  463. int result = 0;
  464. if(userInfo != NULL)
  465. {
  466. userInfo->usri3_comment = UTF8ToUTF16(comment);
  467. }
  468. else
  469. {
  470. result = -1;
  471. }
  472. return result;
  473. }
  474. // ****************************************************************
  475. // NTUser::GetCountryCode
  476. // ****************************************************************
  477. unsigned long NTUser::GetCountryCode()
  478. {
  479. unsigned long result = 0;
  480. if(userInfo != NULL)
  481. {
  482. result = userInfo->usri3_country_code;
  483. }
  484. return result;
  485. }
  486. // ****************************************************************
  487. // NTUser::SetCountryCode
  488. // ****************************************************************
  489. int NTUser::SetCountryCode(unsigned long countryCode)
  490. {
  491. int result = 0;
  492. if(userInfo != NULL)
  493. {
  494. userInfo->usri3_country_code = countryCode;
  495. }
  496. else
  497. {
  498. result = -1;
  499. }
  500. return result;
  501. }
  502. // ****************************************************************
  503. // NTUser::GetFlags
  504. // ****************************************************************
  505. unsigned long NTUser::GetFlags()
  506. {
  507. unsigned long result = 0;
  508. if(userInfo != NULL)
  509. {
  510. result = userInfo->usri3_flags;
  511. }
  512. return result;
  513. }
  514. // ****************************************************************
  515. // NTUser::SetFlags
  516. // ****************************************************************
  517. int NTUser::SetFlags(unsigned long flags)
  518. {
  519. int result = 0;
  520. if(userInfo != NULL)
  521. {
  522. userInfo->usri3_flags = flags;
  523. }
  524. else
  525. {
  526. result = -1;
  527. }
  528. return result;
  529. }
  530. // ****************************************************************
  531. // NTUser::GetHomeDir
  532. // ****************************************************************
  533. char* NTUser::GetHomeDir()
  534. {
  535. char* result = NULL;
  536. if(userInfo != NULL)
  537. {
  538. result = UTF16ToUTF8(userInfo->usri3_home_dir);
  539. }
  540. return result;
  541. }
  542. // ****************************************************************
  543. // NTUser::SetHomeDir
  544. // ****************************************************************
  545. int NTUser::SetHomeDir(char* path)
  546. {
  547. int result = 0;
  548. if(userInfo != NULL)
  549. {
  550. userInfo->usri3_home_dir = UTF8ToUTF16(path);
  551. }
  552. else
  553. {
  554. result = -1;
  555. }
  556. return result;
  557. }
  558. // ****************************************************************
  559. // NTUser::GetHomeDirDrive
  560. // ****************************************************************
  561. char* NTUser::GetHomeDirDrive()
  562. {
  563. char* result = NULL;
  564. if(userInfo != NULL)
  565. {
  566. result = UTF16ToUTF8(userInfo->usri3_home_dir_drive);
  567. }
  568. return result;
  569. }
  570. // ****************************************************************
  571. // NTUser::SetHomeDirDrive
  572. // ****************************************************************
  573. int NTUser::SetHomeDirDrive(char* path)
  574. {
  575. int result = 0;
  576. if(userInfo != NULL)
  577. {
  578. userInfo->usri3_home_dir_drive = UTF8ToUTF16(path);
  579. }
  580. else
  581. {
  582. result = -1;
  583. }
  584. return result;
  585. }
  586. // ****************************************************************
  587. // NTUser::GetLastLogoff
  588. // ****************************************************************
  589. unsigned long NTUser::GetLastLogoff()
  590. {
  591. unsigned long result = 0;
  592. if(userInfo != NULL)
  593. {
  594. result = userInfo->usri3_last_logoff;
  595. }
  596. return result;
  597. }
  598. // ****************************************************************
  599. // NTUser::GetLastLogon
  600. // ****************************************************************
  601. unsigned long NTUser::GetLastLogon()
  602. {
  603. unsigned long result = 0;
  604. if(userInfo != NULL)
  605. {
  606. result = userInfo->usri3_last_logon;
  607. }
  608. return result;
  609. }
  610. // ****************************************************************
  611. // NTUser::GetLogonHours
  612. // ****************************************************************
  613. char* NTUser::GetLogonHours()
  614. {
  615. char* result = NULL;
  616. if(userInfo != NULL)
  617. {
  618. result = (char*)userInfo->usri3_logon_hours;
  619. }
  620. return result;
  621. }
  622. // ****************************************************************
  623. // NTUser::SetLogonHours
  624. // ****************************************************************
  625. int NTUser::SetLogonHours(char* logonHours)
  626. {
  627. int result = 0;
  628. if(userInfo != NULL)
  629. {
  630. userInfo->usri3_logon_hours = (unsigned char*)logonHours;
  631. }
  632. else
  633. {
  634. result = -1;
  635. }
  636. return result;
  637. }
  638. // ****************************************************************
  639. // NTUser::GetMaxStorage
  640. // ****************************************************************
  641. unsigned long NTUser::GetMaxStorage()
  642. {
  643. unsigned long result = 0;
  644. if(userInfo != NULL)
  645. {
  646. result = userInfo->usri3_max_storage;
  647. }
  648. return result;
  649. }
  650. // ****************************************************************
  651. // NTUser::SetMaxStorage
  652. // ****************************************************************
  653. int NTUser::SetMaxStorage(unsigned long maxStorage)
  654. {
  655. int result = 0;
  656. if(userInfo != NULL)
  657. {
  658. userInfo->usri3_max_storage = maxStorage;
  659. }
  660. else
  661. {
  662. result = -1;
  663. }
  664. return result;
  665. }
  666. // ****************************************************************
  667. // NTUser::GetNumLogons
  668. // ****************************************************************
  669. unsigned long NTUser::GetNumLogons()
  670. {
  671. unsigned long result = 0;
  672. if(userInfo != NULL)
  673. {
  674. result = userInfo->usri3_num_logons;
  675. }
  676. return result;
  677. }
  678. // ****************************************************************
  679. // NTUser::GetProfile
  680. // ****************************************************************
  681. char* NTUser::GetProfile()
  682. {
  683. char* result = NULL;
  684. if(userInfo != NULL)
  685. {
  686. result = UTF16ToUTF8(userInfo->usri3_profile);
  687. }
  688. return result;
  689. }
  690. // ****************************************************************
  691. // NTUser::SetProfile
  692. // ****************************************************************
  693. int NTUser::SetProfile(char* path)
  694. {
  695. int result = 0;
  696. if(userInfo != NULL)
  697. {
  698. userInfo->usri3_profile = UTF8ToUTF16(path);
  699. }
  700. else
  701. {
  702. result = -1;
  703. }
  704. return result;
  705. }
  706. // ****************************************************************
  707. // NTUser::GetScriptPath
  708. // ****************************************************************
  709. char* NTUser::GetScriptPath()
  710. {
  711. char* result = NULL;
  712. if(userInfo != NULL)
  713. {
  714. result = UTF16ToUTF8(userInfo->usri3_script_path);
  715. }
  716. return result;
  717. }
  718. // ****************************************************************
  719. // NTUser::SetScriptPath
  720. // ****************************************************************
  721. int NTUser::SetScriptPath(char* path)
  722. {
  723. int result = 0;
  724. if(userInfo != NULL)
  725. {
  726. userInfo->usri3_script_path = UTF8ToUTF16(path);
  727. }
  728. else
  729. {
  730. result = -1;
  731. }
  732. return result;
  733. }
  734. // ****************************************************************
  735. // NTUser::GetWorkstations
  736. // ****************************************************************
  737. char* NTUser::GetWorkstations()
  738. {
  739. char* result = NULL;
  740. if(userInfo != NULL)
  741. {
  742. result = UTF16ToUTF8(userInfo->usri3_workstations);
  743. }
  744. return result;
  745. }
  746. // ****************************************************************
  747. // NTUser::SetWorkstations
  748. // ****************************************************************
  749. int NTUser::SetWorkstations(char* workstations)
  750. {
  751. int result = 0;
  752. if(userInfo != NULL)
  753. {
  754. userInfo->usri3_workstations = UTF8ToUTF16(workstations);
  755. }
  756. else
  757. {
  758. result = -1;
  759. }
  760. return result;
  761. }
  762. // ****************************************************************
  763. // NTUser::GetFullname
  764. // ****************************************************************
  765. char* NTUser::GetFullname()
  766. {
  767. char* result = NULL;
  768. if(userInfo != NULL)
  769. {
  770. result = UTF16ToUTF8(userInfo->usri3_full_name);
  771. }
  772. return result;
  773. }
  774. // ****************************************************************
  775. // NTUser::SetFullname
  776. // ****************************************************************
  777. int NTUser::SetFullname(char* fullname)
  778. {
  779. int result = 0;
  780. if(userInfo != NULL)
  781. {
  782. userInfo->usri3_full_name = UTF8ToUTF16(fullname);
  783. }
  784. else
  785. {
  786. result = -1;
  787. }
  788. return result;
  789. }
  790. // ****************************************************************
  791. // NTUser::SetPassword
  792. // ****************************************************************
  793. int NTUser::SetPassword(char* password)
  794. {
  795. int result = 0;
  796. if(userInfo != NULL)
  797. {
  798. userInfo->usri3_password = UTF8ToUTF16(password);
  799. }
  800. else
  801. {
  802. result = -1;
  803. }
  804. return result;
  805. }
  806. // ****************************************************************
  807. // NTUser::AddToGroup
  808. // ****************************************************************
  809. int NTUser::AddToGroup(char* groupName)
  810. {
  811. int result = 0;
  812. if(userInfo != NULL)
  813. {
  814. result = NetGroupAddUser(NULL, UTF8ToUTF16(groupName), userInfo->usri3_name);
  815. }
  816. else
  817. {
  818. result = -1;
  819. }
  820. return result;
  821. }
  822. // ****************************************************************
  823. // NTUser::RemoveFromGroup
  824. // ****************************************************************
  825. int NTUser::RemoveFromGroup(char* groupName)
  826. {
  827. int result = 0;
  828. if(userInfo != NULL)
  829. {
  830. result = NetGroupDelUser(NULL, UTF8ToUTF16(groupName), userInfo->usri3_name);
  831. }
  832. else
  833. {
  834. result = -1;
  835. }
  836. return result;
  837. }
  838. // ****************************************************************
  839. // NTUser::LoadGroups
  840. // ****************************************************************
  841. int NTUser::LoadGroups()
  842. {
  843. int result = 0;
  844. if(userInfo != NULL)
  845. {
  846. if(groupsInfo != NULL)
  847. {
  848. NetApiBufferFree(groupsInfo);
  849. groupsInfo = NULL;
  850. currentGroupEntry = 0;
  851. groupEntriesRead = 0;
  852. groupEntriesTotal = 0;
  853. }
  854. result = NetUserGetGroups(NULL, userInfo->usri3_name, USER_GROUPS_INFO_LEVEL, (unsigned char**)&groupsInfo, MAX_PREFERRED_LENGTH, &groupEntriesRead, &groupEntriesTotal);
  855. }
  856. else
  857. {
  858. result = -1;
  859. }
  860. return result;
  861. }
  862. // ****************************************************************
  863. // NTUser::HasMoreGroups
  864. // ****************************************************************
  865. bool NTUser::HasMoreGroups()
  866. {
  867. bool result;
  868. if(currentGroupEntry < groupEntriesRead)
  869. {
  870. result = true;
  871. }
  872. else
  873. {
  874. result = false;
  875. }
  876. return result;
  877. }
  878. // ****************************************************************
  879. // NTUser::NextGroupName
  880. // ****************************************************************
  881. char* NTUser::NextGroupName()
  882. {
  883. char* groupName = NULL;
  884. GROUP_USERS_INFO_0* thisEntry;
  885. if(currentGroupEntry < groupEntriesRead)
  886. {
  887. thisEntry = &(groupsInfo[currentGroupEntry]);
  888. groupName = UTF16ToUTF8(thisEntry->grui0_name);
  889. currentGroupEntry++;
  890. }
  891. return groupName;
  892. }
  893. // ****************************************************************
  894. // NTUser::AddToLocalGroup
  895. // ****************************************************************
  896. int NTUser::AddToLocalGroup(char* localGroupName)
  897. {
  898. int result = 0;
  899. char userSID[256];
  900. wchar_t domain[256];
  901. DWORD SIDLen = sizeof(userSID);
  902. DWORD domainLen = sizeof(domain);
  903. SID_NAME_USE SIDUseIndicator;
  904. LOCALGROUP_MEMBERS_INFO_0 membersbuf[1];
  905. memset(&domain, 0, sizeof(domain));
  906. memset(&userSID, 0, sizeof(userSID));
  907. if(userInfo != NULL)
  908. {
  909. result = LookupAccountName(NULL, UTF16ToUTF8(userInfo->usri3_name), &userSID, &SIDLen, (LPTSTR)&domain, &domainLen, &SIDUseIndicator);
  910. if(result != 0)
  911. {
  912. membersbuf[0].lgrmi0_sid = &userSID;
  913. result = NetLocalGroupAddMembers(NULL, UTF8ToUTF16(localGroupName), 0, (LPBYTE)&membersbuf, 1);
  914. }
  915. }
  916. else
  917. {
  918. result = -1;
  919. }
  920. return result;
  921. }
  922. // ****************************************************************
  923. // NTUser::RemoveFromLocalGroup
  924. // ****************************************************************
  925. int NTUser::RemoveFromLocalGroup(char* localGroupName)
  926. {
  927. int result = 0;
  928. char userSID[256];
  929. wchar_t domain[256];
  930. DWORD SIDLen = sizeof(userSID);
  931. DWORD domainLen = sizeof(domain);
  932. SID_NAME_USE SIDUseIndicator;
  933. LOCALGROUP_MEMBERS_INFO_0 membersbuf[1];
  934. memset(&domain, 0, sizeof(domain));
  935. memset(&userSID, 0, sizeof(userSID));
  936. if(userInfo != NULL)
  937. {
  938. result = LookupAccountName(NULL, UTF16ToUTF8(userInfo->usri3_name), &userSID, &SIDLen, (LPTSTR)&domain, &domainLen, &SIDUseIndicator);
  939. if(result != 0)
  940. {
  941. membersbuf[0].lgrmi0_sid = &userSID;
  942. result = NetLocalGroupDelMembers(NULL, UTF8ToUTF16(localGroupName), 0, (LPBYTE)&membersbuf, 1);
  943. }
  944. }
  945. else
  946. {
  947. result = -1;
  948. }
  949. return result;
  950. }
  951. // ****************************************************************
  952. // NTUser::LoadLocalGroups
  953. // ****************************************************************
  954. int NTUser::LoadLocalGroups()
  955. {
  956. int result = 0;
  957. if(userInfo != NULL)
  958. {
  959. if(localGroupsInfo != NULL)
  960. {
  961. NetApiBufferFree(localGroupsInfo);
  962. localGroupsInfo = NULL;
  963. currentLocalGroupEntry = 0;
  964. localGroupEntriesRead = 0;
  965. localGroupEntriesTotal = 0;
  966. }
  967. result = NetUserGetLocalGroups(NULL, userInfo->usri3_name, 0, USER_LOCALGROUPS_INFO_LEVEL, (unsigned char**)&localGroupsInfo, MAX_PREFERRED_LENGTH, &localGroupEntriesRead, &localGroupEntriesTotal);
  968. }
  969. else
  970. {
  971. result = -1;
  972. }
  973. return result;
  974. }
  975. // ****************************************************************
  976. // NTUser::HasMoreLocalGroups
  977. // ****************************************************************
  978. bool NTUser::HasMoreLocalGroups()
  979. {
  980. bool result;
  981. if(currentLocalGroupEntry < localGroupEntriesRead)
  982. {
  983. result = true;
  984. }
  985. else
  986. {
  987. result = false;
  988. }
  989. return result;
  990. }
  991. // ****************************************************************
  992. // NTUser::NextLocalGroupName
  993. // ****************************************************************
  994. char* NTUser::NextLocalGroupName()
  995. {
  996. char* localGroupName = NULL;
  997. LOCALGROUP_USERS_INFO_0* thisEntry;
  998. if(currentLocalGroupEntry < localGroupEntriesRead)
  999. {
  1000. thisEntry = &(localGroupsInfo[currentLocalGroupEntry]);
  1001. localGroupName = UTF16ToUTF8(thisEntry->lgrui0_name);
  1002. currentLocalGroupEntry++;
  1003. }
  1004. return localGroupName;
  1005. }
  1006. // ****************************************************************
  1007. // NTUserList::NTUserList
  1008. // ****************************************************************
  1009. NTUserList::NTUserList()
  1010. {
  1011. bufptr = NULL;
  1012. currentEntry = 0;
  1013. resumeHandle = 0;
  1014. }
  1015. // ****************************************************************
  1016. // NTUserList::~NTUserList
  1017. // ****************************************************************
  1018. NTUserList::~NTUserList()
  1019. {
  1020. if(bufptr != NULL)
  1021. {
  1022. NetApiBufferFree(bufptr);
  1023. bufptr = NULL;
  1024. }
  1025. }
  1026. // ****************************************************************
  1027. // NTUserList::loadList
  1028. // ****************************************************************
  1029. int NTUserList::loadList()
  1030. {
  1031. int result;
  1032. result = NetUserEnum(NULL, USER_INFO_LEVEL, 0, (LPBYTE*)&bufptr, MAX_PREFERRED_LENGTH, &entriesRead, &totalEntries, &resumeHandle);
  1033. return result;
  1034. }
  1035. // ****************************************************************
  1036. // NTUserList::hasMore
  1037. // ****************************************************************
  1038. bool NTUserList::hasMore()
  1039. {
  1040. bool result;
  1041. if(currentEntry < entriesRead)
  1042. {
  1043. result = true;
  1044. }
  1045. else
  1046. {
  1047. result = false;
  1048. }
  1049. return result;
  1050. }
  1051. // ****************************************************************
  1052. // NTUserList::nextUsername
  1053. // ****************************************************************
  1054. char* NTUserList::nextUsername()
  1055. {
  1056. char* username = NULL;
  1057. USER_INFO_3* thisEntry;
  1058. if(currentEntry < entriesRead)
  1059. {
  1060. thisEntry = &(bufptr[currentEntry]);
  1061. username = UTF16ToUTF8(thisEntry->usri3_name);
  1062. currentEntry++;
  1063. }
  1064. return username;
  1065. }
  1066. // ****************************************************************
  1067. // NTGroup::NTGroup
  1068. // ****************************************************************
  1069. NTGroup::NTGroup()
  1070. {
  1071. groupInfo = NULL;
  1072. }
  1073. // ****************************************************************
  1074. // NTGroup::~NTGroup
  1075. // ****************************************************************
  1076. NTGroup::~NTGroup()
  1077. {
  1078. if(groupInfo != NULL)
  1079. {
  1080. NetApiBufferFree(groupInfo);
  1081. groupInfo = NULL;
  1082. }
  1083. }
  1084. // ****************************************************************
  1085. // NTGroup::NewGroup
  1086. // ****************************************************************
  1087. void NTGroup::NewGroup(char* groupName)
  1088. {
  1089. if(groupInfo != NULL)
  1090. {
  1091. NetApiBufferFree(groupInfo);
  1092. groupInfo = NULL;
  1093. }
  1094. groupInfo = (GROUP_INFO_2*)malloc(sizeof(GROUP_INFO_2));
  1095. memset(groupInfo, 0, sizeof(GROUP_INFO_2));
  1096. groupInfo->grpi2_name = UTF8ToUTF16(groupName);
  1097. }
  1098. // ****************************************************************
  1099. // NTGroup::DeleteGroup
  1100. // ****************************************************************
  1101. int NTGroup::DeleteGroup(char* groupName)
  1102. {
  1103. int result;
  1104. result = NetGroupDel(NULL, UTF8ToUTF16(groupName));
  1105. return result;
  1106. }
  1107. // ****************************************************************
  1108. // NTGroup::RetriveGroup
  1109. // ****************************************************************
  1110. int NTGroup::RetriveGroupByAccountName(char* groupName)
  1111. {
  1112. int result;
  1113. if(groupInfo != NULL)
  1114. {
  1115. NetApiBufferFree(groupInfo);
  1116. groupInfo = NULL;
  1117. }
  1118. result = NetGroupGetInfo(NULL, UTF8ToUTF16(groupName), GROUP_INFO_LEVEL, (unsigned char**)&groupInfo);
  1119. return result;
  1120. }
  1121. // ****************************************************************
  1122. // NTGroup::RetriveGroupBySIDHexStr
  1123. // ****************************************************************
  1124. int NTGroup::RetriveGroupBySIDHexStr(char* sidHexStr)
  1125. {
  1126. int result = 0;
  1127. char* groupName;
  1128. if(groupInfo != NULL)
  1129. {
  1130. NetApiBufferFree(groupInfo);
  1131. groupInfo = NULL;
  1132. }
  1133. if(GetAccountNameBySIDHexStr(sidHexStr, &groupName) != 0)
  1134. {
  1135. result = -1;
  1136. goto exit;
  1137. }
  1138. if(RetriveGroupByAccountName(groupName) != 0)
  1139. {
  1140. result =-1;
  1141. goto exit;
  1142. }
  1143. exit:
  1144. return result;
  1145. }
  1146. // ****************************************************************
  1147. // NTGroup::AddGroup
  1148. // ****************************************************************
  1149. int NTGroup::AddGroup()
  1150. {
  1151. int result;
  1152. DWORD badParam = 0;
  1153. result = NetGroupAdd(NULL, GROUP_INFO_LEVEL, (unsigned char*)groupInfo, &badParam);
  1154. return result;
  1155. }
  1156. // ****************************************************************
  1157. // NTGroup::StoreGroup
  1158. // ****************************************************************
  1159. int NTGroup::StoreGroup()
  1160. {
  1161. int result = -1;
  1162. if(groupInfo != NULL)
  1163. {
  1164. result = NetGroupSetInfo(NULL, groupInfo->grpi2_name, GROUP_INFO_LEVEL, (unsigned char*)groupInfo, NULL);
  1165. }
  1166. return result;
  1167. }
  1168. // ****************************************************************
  1169. // NTGroup::GetAccountName
  1170. // ****************************************************************
  1171. char* NTGroup::GetAccountName()
  1172. {
  1173. char* result = NULL;
  1174. if(groupInfo != NULL)
  1175. {
  1176. result = UTF16ToUTF8(groupInfo->grpi2_name);
  1177. }
  1178. return result;
  1179. }
  1180. // ****************************************************************
  1181. // NTGroup::GetSIDHexStr
  1182. // ****************************************************************
  1183. char* NTGroup::GetSIDHexStr()
  1184. {
  1185. char* result = NULL;
  1186. if(groupInfo != NULL)
  1187. {
  1188. GetSIDHexStrByAccountName(UTF16ToUTF8(groupInfo->grpi2_name), &result);
  1189. }
  1190. return result;
  1191. }
  1192. // ****************************************************************
  1193. // NTGroup::AddUser
  1194. // ****************************************************************
  1195. int NTGroup::AddUser(char* userName)
  1196. {
  1197. int result = 0;
  1198. if(groupInfo != NULL)
  1199. {
  1200. result = NetGroupAddUser(NULL, groupInfo->grpi2_name, UTF8ToUTF16(userName));
  1201. }
  1202. else
  1203. {
  1204. result = -1;
  1205. }
  1206. return result;
  1207. }
  1208. // ****************************************************************
  1209. // NTGroup::RemoveUser
  1210. // ****************************************************************
  1211. int NTGroup::RemoveUser(char* userName)
  1212. {
  1213. int result = 0;
  1214. if(groupInfo != NULL)
  1215. {
  1216. result = NetGroupDelUser(NULL, groupInfo->grpi2_name, UTF8ToUTF16(userName));
  1217. }
  1218. else
  1219. {
  1220. result = -1;
  1221. }
  1222. return result;
  1223. }
  1224. // ****************************************************************
  1225. // NTGroup::LoadUsers
  1226. // ****************************************************************
  1227. int NTGroup::LoadUsers()
  1228. {
  1229. int result = 0;
  1230. if(groupInfo != NULL)
  1231. {
  1232. if(usersInfo != NULL)
  1233. {
  1234. NetApiBufferFree(usersInfo);
  1235. usersInfo = NULL;
  1236. currentUserEntry = 0;
  1237. userEntriesRead = 0;
  1238. userEntriesTotal = 0;
  1239. }
  1240. result = NetGroupGetUsers(NULL, groupInfo->grpi2_name, GROUP_USERS_INFO_LEVEL, (unsigned char**)&usersInfo, MAX_PREFERRED_LENGTH, &userEntriesRead, &userEntriesTotal, NULL);
  1241. }
  1242. else
  1243. {
  1244. result = -1;
  1245. }
  1246. return result;
  1247. }
  1248. // ****************************************************************
  1249. // NTGroup::HasMoreUsers
  1250. // ****************************************************************
  1251. bool NTGroup::HasMoreUsers()
  1252. {
  1253. bool result;
  1254. if(currentUserEntry < userEntriesRead)
  1255. {
  1256. result = true;
  1257. }
  1258. else
  1259. {
  1260. result = false;
  1261. }
  1262. return result;
  1263. }
  1264. // ****************************************************************
  1265. // NTGroup::NextUserName
  1266. // ****************************************************************
  1267. char* NTGroup::NextUserName()
  1268. {
  1269. char* userName = NULL;
  1270. LOCALGROUP_USERS_INFO_0* thisEntry;
  1271. if(currentUserEntry < userEntriesRead)
  1272. {
  1273. thisEntry = &(usersInfo[currentUserEntry]);
  1274. userName = UTF16ToUTF8(thisEntry->lgrui0_name);
  1275. currentUserEntry++;
  1276. }
  1277. return userName;
  1278. }
  1279. // ****************************************************************
  1280. // NTGroupList::NTGroupList
  1281. // ****************************************************************
  1282. NTGroupList::NTGroupList()
  1283. {
  1284. bufptr = NULL;
  1285. currentEntry = 0;
  1286. resumeHandle = 0;
  1287. }
  1288. // ****************************************************************
  1289. // NTGroupList::~NTGroupList
  1290. // ****************************************************************
  1291. NTGroupList::~NTGroupList()
  1292. {
  1293. if(bufptr != NULL)
  1294. {
  1295. NetApiBufferFree(bufptr);
  1296. bufptr = NULL;
  1297. }
  1298. }
  1299. // ****************************************************************
  1300. // NTGroupList::loadList
  1301. // ****************************************************************
  1302. int NTGroupList::loadList()
  1303. {
  1304. int result;
  1305. result = NetGroupEnum(NULL, GROUP_INFO_LEVEL, (LPBYTE*)&bufptr, MAX_PREFERRED_LENGTH, &entriesRead, &totalEntries, &resumeHandle);
  1306. return result;
  1307. }
  1308. // ****************************************************************
  1309. // NTGroupList::hasMore
  1310. // ****************************************************************
  1311. bool NTGroupList::hasMore()
  1312. {
  1313. bool result;
  1314. if(currentEntry < entriesRead)
  1315. {
  1316. result = true;
  1317. }
  1318. else
  1319. {
  1320. result = false;
  1321. }
  1322. return result;
  1323. }
  1324. // ****************************************************************
  1325. // NTGroupList::nextGroupName
  1326. // ****************************************************************
  1327. char* NTGroupList::nextGroupName()
  1328. {
  1329. char* groupName = NULL;
  1330. GROUP_INFO_2* thisEntry;
  1331. if(currentEntry < entriesRead)
  1332. {
  1333. thisEntry = &(bufptr[currentEntry]);
  1334. groupName = UTF16ToUTF8(thisEntry->grpi2_name);
  1335. currentEntry++;
  1336. }
  1337. return groupName;
  1338. }
  1339. // ****************************************************************
  1340. // NTLocalGroup::NTLocalGroup
  1341. // ****************************************************************
  1342. NTLocalGroup::NTLocalGroup()
  1343. {
  1344. localGroupInfo = NULL;
  1345. }
  1346. // ****************************************************************
  1347. // NTLocalGroup::~NTLocalGroup
  1348. // ****************************************************************
  1349. NTLocalGroup::~NTLocalGroup()
  1350. {
  1351. if(localGroupInfo != NULL)
  1352. {
  1353. NetApiBufferFree(localGroupInfo);
  1354. localGroupInfo = NULL;
  1355. }
  1356. }
  1357. // ****************************************************************
  1358. // NTLocalGroup::NewLocalGroup
  1359. // ****************************************************************
  1360. void NTLocalGroup::NewLocalGroup(char* localGroupName)
  1361. {
  1362. if(localGroupInfo != NULL)
  1363. {
  1364. NetApiBufferFree(localGroupInfo);
  1365. localGroupInfo = NULL;
  1366. }
  1367. localGroupInfo = (LOCALGROUP_INFO_1*)malloc(sizeof(LOCALGROUP_INFO_1));
  1368. memset(localGroupInfo, 0, sizeof(LOCALGROUP_INFO_1));
  1369. localGroupInfo->lgrpi1_name = UTF8ToUTF16(localGroupName);
  1370. }
  1371. // ****************************************************************
  1372. // NTLocalGroup::DeleteLocalGroup
  1373. // ****************************************************************
  1374. int NTLocalGroup::DeleteLocalGroup(char* localGroupName)
  1375. {
  1376. int result;
  1377. result = NetLocalGroupDel(NULL, UTF8ToUTF16(localGroupName));
  1378. return result;
  1379. }
  1380. // ****************************************************************
  1381. // NTLocalGroup::RetriveLocalGroup
  1382. // ****************************************************************
  1383. int NTLocalGroup::RetriveLocalGroupByAccountName(char* localGroupName)
  1384. {
  1385. int result;
  1386. if(localGroupInfo != NULL)
  1387. {
  1388. NetApiBufferFree(localGroupInfo);
  1389. localGroupInfo = NULL;
  1390. }
  1391. result = NetLocalGroupGetInfo(NULL, UTF8ToUTF16(localGroupName), LOCALGROUP_INFO_LEVEL, (unsigned char**)&localGroupInfo);
  1392. return result;
  1393. }
  1394. // ****************************************************************
  1395. // NTGroup::RetriveLocalGroupBySIDHexStr
  1396. // ****************************************************************
  1397. int NTLocalGroup::RetriveLocalGroupBySIDHexStr(char* sidHexStr)
  1398. {
  1399. int result = 0;
  1400. char* localGroupName;
  1401. if(localGroupInfo != NULL)
  1402. {
  1403. NetApiBufferFree(localGroupInfo);
  1404. localGroupInfo = NULL;
  1405. }
  1406. if(GetAccountNameBySIDHexStr(sidHexStr, &localGroupName) != 0)
  1407. {
  1408. result = -1;
  1409. goto exit;
  1410. }
  1411. if(RetriveLocalGroupByAccountName(localGroupName) != 0)
  1412. {
  1413. result =-1;
  1414. goto exit;
  1415. }
  1416. exit:
  1417. return result;
  1418. }
  1419. // ****************************************************************
  1420. // NTLocalGroup::AddLocalGroup
  1421. // ****************************************************************
  1422. int NTLocalGroup::AddLocalGroup()
  1423. {
  1424. int result;
  1425. DWORD badParam = 0;
  1426. result = NetLocalGroupAdd(NULL, LOCALGROUP_INFO_LEVEL, (unsigned char*)localGroupInfo, &badParam);
  1427. return result;
  1428. }
  1429. // ****************************************************************
  1430. // NTLocalGroup::StoreLocalGroup
  1431. // ****************************************************************
  1432. int NTLocalGroup::StoreLocalGroup()
  1433. {
  1434. int result = 0;
  1435. if(localGroupInfo != NULL)
  1436. {
  1437. result = NetLocalGroupSetInfo(NULL, localGroupInfo->lgrpi1_name, LOCALGROUP_INFO_LEVEL, (unsigned char*)localGroupInfo, NULL);
  1438. }
  1439. else
  1440. {
  1441. result = -1;
  1442. }
  1443. return result;
  1444. }
  1445. // ****************************************************************
  1446. // NTLocalGroup::GetAccountName
  1447. // ****************************************************************
  1448. char* NTLocalGroup::GetAccountName()
  1449. {
  1450. char* result = NULL;
  1451. if(localGroupInfo != NULL)
  1452. {
  1453. result = UTF16ToUTF8(localGroupInfo->lgrpi1_name);
  1454. }
  1455. return result;
  1456. }
  1457. // ****************************************************************
  1458. // NTLocalGroup::GetSIDHexStr
  1459. // ****************************************************************
  1460. char* NTLocalGroup::GetSIDHexStr()
  1461. {
  1462. char* result = NULL;
  1463. if(localGroupInfo != NULL)
  1464. {
  1465. GetSIDHexStrByAccountName(UTF16ToUTF8(localGroupInfo->lgrpi1_name), &result);
  1466. }
  1467. return result;
  1468. }
  1469. // ****************************************************************
  1470. // NTLocalGroup::AddUser
  1471. // ****************************************************************
  1472. int NTLocalGroup::AddUser(char* username)
  1473. {
  1474. int result = 0;
  1475. LOCALGROUP_MEMBERS_INFO_0 members[1];
  1476. if(localGroupInfo != NULL)
  1477. {
  1478. GetSIDByAccountName(username, (char**)&members[0].lgrmi0_sid);
  1479. result = NetLocalGroupAddMembers(NULL, localGroupInfo->lgrpi1_name, 0, (unsigned char*)&members, 1);
  1480. free(members[0].lgrmi0_sid);
  1481. }
  1482. else
  1483. {
  1484. result = -1;
  1485. }
  1486. return result;
  1487. }
  1488. // ****************************************************************
  1489. // NTLocalGroup::RemoveUser
  1490. // ****************************************************************
  1491. int NTLocalGroup::RemoveUser(char* username)
  1492. {
  1493. int result = 0;
  1494. LOCALGROUP_MEMBERS_INFO_0 members[1];
  1495. if(localGroupInfo != NULL)
  1496. {
  1497. GetSIDByAccountName(username, (char**)&members[0].lgrmi0_sid);
  1498. result = NetLocalGroupDelMembers(NULL, localGroupInfo->lgrpi1_name, 0, (unsigned char*)&members, 1);
  1499. free(members[0].lgrmi0_sid);
  1500. }
  1501. else
  1502. {
  1503. result = -1;
  1504. }
  1505. return result;
  1506. }
  1507. // ****************************************************************
  1508. // NTLocalGroup::LoadUsers
  1509. // ****************************************************************
  1510. int NTLocalGroup::LoadUsers()
  1511. {
  1512. int result = 0;
  1513. if(localGroupInfo != NULL)
  1514. {
  1515. if(usersInfo != NULL)
  1516. {
  1517. NetApiBufferFree(usersInfo);
  1518. usersInfo = NULL;
  1519. currentUserEntry = 0;
  1520. userEntriesRead = 0;
  1521. userEntriesTotal = 0;
  1522. }
  1523. result = NetLocalGroupGetMembers(NULL, localGroupInfo->lgrpi1_name, LOCALGROUP_USERS_INFO_LEVEL, (unsigned char**)&usersInfo, MAX_PREFERRED_LENGTH, &userEntriesRead, &userEntriesTotal, NULL);
  1524. }
  1525. else
  1526. {
  1527. result = -1;
  1528. }
  1529. return result;
  1530. }
  1531. // ****************************************************************
  1532. // NTLocalGroup::HasMoreUsers
  1533. // ****************************************************************
  1534. bool NTLocalGroup::HasMoreUsers()
  1535. {
  1536. bool result;
  1537. if(currentUserEntry < userEntriesRead)
  1538. {
  1539. result = true;
  1540. }
  1541. else
  1542. {
  1543. result = false;
  1544. }
  1545. return result;
  1546. }
  1547. // ****************************************************************
  1548. // NTLocalGroup::NextUserName
  1549. // ****************************************************************
  1550. char* NTLocalGroup::NextUserName()
  1551. {
  1552. char* username = NULL;
  1553. LOCALGROUP_MEMBERS_INFO_0* thisEntry;
  1554. if(currentUserEntry < userEntriesRead)
  1555. {
  1556. thisEntry = &(usersInfo[currentUserEntry]);
  1557. GetAccountNameBySID((char*)thisEntry->lgrmi0_sid, &username);
  1558. currentUserEntry++;
  1559. }
  1560. return username;
  1561. }
  1562. // ****************************************************************
  1563. // NTLocalGroupList::NTLocalGroupList
  1564. // ****************************************************************
  1565. NTLocalGroupList::NTLocalGroupList()
  1566. {
  1567. bufptr = NULL;
  1568. currentEntry = 0;
  1569. resumeHandle = 0;
  1570. }
  1571. // ****************************************************************
  1572. // NTLocalGroupList::~NTLocalGroupList
  1573. // ****************************************************************
  1574. NTLocalGroupList::~NTLocalGroupList()
  1575. {
  1576. if(bufptr != NULL)
  1577. {
  1578. NetApiBufferFree(bufptr);
  1579. bufptr = NULL;
  1580. }
  1581. }
  1582. // ****************************************************************
  1583. // NTLocalGroupList::loadList
  1584. // ****************************************************************
  1585. int NTLocalGroupList::loadList()
  1586. {
  1587. int result;
  1588. result = NetLocalGroupEnum(NULL, LOCALGROUP_INFO_LEVEL, (LPBYTE*)&bufptr, MAX_PREFERRED_LENGTH, &entriesRead, &totalEntries, &resumeHandle);
  1589. return result;
  1590. }
  1591. // ****************************************************************
  1592. // NTLocalGroupList::hasMore
  1593. // ****************************************************************
  1594. bool NTLocalGroupList::hasMore()
  1595. {
  1596. bool result;
  1597. if(currentEntry < entriesRead)
  1598. {
  1599. result = true;
  1600. }
  1601. else
  1602. {
  1603. result = false;
  1604. }
  1605. return result;
  1606. }
  1607. // ****************************************************************
  1608. // NTLocalGroupList::nextLocalGroupName
  1609. // ****************************************************************
  1610. char* NTLocalGroupList::nextLocalGroupName()
  1611. {
  1612. char* localGroupName = NULL;
  1613. LOCALGROUP_INFO_1* thisEntry;
  1614. if(currentEntry < entriesRead)
  1615. {
  1616. thisEntry = &(bufptr[currentEntry]);
  1617. localGroupName = UTF16ToUTF8(thisEntry->lgrpi1_name);
  1618. currentEntry++;
  1619. }
  1620. return localGroupName;
  1621. }