1
0

netman.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041
  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. // BinToHexStr
  65. // ****************************************************************
  66. int BinToHexStr(char* bin, unsigned long binLen, char** hexStr)
  67. {
  68. int hexStrLen = binLen * 2 + 1;
  69. *hexStr = (char*)calloc(hexStrLen, sizeof(char));
  70. for(unsigned long i = 0; i < binLen; i++)
  71. {
  72. sprintf(&(*hexStr)[i * 2], "%02X", (unsigned char)bin[i]);
  73. }
  74. return 0;
  75. }
  76. // ****************************************************************
  77. // HexStrToBin
  78. // ****************************************************************
  79. int HexStrToBin(char* hexStr, char** bin, unsigned long* binLen)
  80. {
  81. int temp;
  82. *binLen = strlen(hexStr) / 2;
  83. *bin = (char*)malloc(*binLen);
  84. for(unsigned long i = 0; i < *binLen; i++)
  85. {
  86. sscanf(&hexStr[i * 2], "%02X", &temp);
  87. (*bin)[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. BinToHexStr(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. HexStrToBin(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. NetApiBufferAllocate(sizeof(USER_INFO_3),(LPVOID*)&userInfo);
  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. BinToHexStr((char*)userInfo->usri3_logon_hours, 21, &result);
  619. }
  620. return result;
  621. }
  622. // ****************************************************************
  623. // NTUser::SetLogonHours
  624. // ****************************************************************
  625. int NTUser::SetLogonHours(char* logonHours)
  626. {
  627. int result = 0;
  628. char* binValue;
  629. unsigned long binLen = 0;
  630. if(userInfo != NULL)
  631. {
  632. HexStrToBin(logonHours, &binValue, &binLen);
  633. userInfo->usri3_logon_hours = (unsigned char*)binValue;
  634. }
  635. else
  636. {
  637. result = -1;
  638. }
  639. return result;
  640. }
  641. // ****************************************************************
  642. // NTUser::GetMaxStorage
  643. // ****************************************************************
  644. unsigned long NTUser::GetMaxStorage()
  645. {
  646. unsigned long result = 0;
  647. if(userInfo != NULL)
  648. {
  649. result = userInfo->usri3_max_storage;
  650. }
  651. return result;
  652. }
  653. // ****************************************************************
  654. // NTUser::SetMaxStorage
  655. // ****************************************************************
  656. int NTUser::SetMaxStorage(unsigned long maxStorage)
  657. {
  658. int result = 0;
  659. if(userInfo != NULL)
  660. {
  661. userInfo->usri3_max_storage = maxStorage;
  662. }
  663. else
  664. {
  665. result = -1;
  666. }
  667. return result;
  668. }
  669. // ****************************************************************
  670. // NTUser::GetNumLogons
  671. // ****************************************************************
  672. unsigned long NTUser::GetNumLogons()
  673. {
  674. unsigned long result = 0;
  675. if(userInfo != NULL)
  676. {
  677. result = userInfo->usri3_num_logons;
  678. }
  679. return result;
  680. }
  681. // ****************************************************************
  682. // NTUser::GetProfile
  683. // ****************************************************************
  684. char* NTUser::GetProfile()
  685. {
  686. char* result = NULL;
  687. if(userInfo != NULL)
  688. {
  689. result = UTF16ToUTF8(userInfo->usri3_profile);
  690. }
  691. return result;
  692. }
  693. // ****************************************************************
  694. // NTUser::SetProfile
  695. // ****************************************************************
  696. int NTUser::SetProfile(char* path)
  697. {
  698. int result = 0;
  699. if(userInfo != NULL)
  700. {
  701. userInfo->usri3_profile = UTF8ToUTF16(path);
  702. }
  703. else
  704. {
  705. result = -1;
  706. }
  707. return result;
  708. }
  709. // ****************************************************************
  710. // NTUser::GetScriptPath
  711. // ****************************************************************
  712. char* NTUser::GetScriptPath()
  713. {
  714. char* result = NULL;
  715. if(userInfo != NULL)
  716. {
  717. result = UTF16ToUTF8(userInfo->usri3_script_path);
  718. }
  719. return result;
  720. }
  721. // ****************************************************************
  722. // NTUser::SetScriptPath
  723. // ****************************************************************
  724. int NTUser::SetScriptPath(char* path)
  725. {
  726. int result = 0;
  727. if(userInfo != NULL)
  728. {
  729. userInfo->usri3_script_path = UTF8ToUTF16(path);
  730. }
  731. else
  732. {
  733. result = -1;
  734. }
  735. return result;
  736. }
  737. // ****************************************************************
  738. // NTUser::GetWorkstations
  739. // ****************************************************************
  740. char* NTUser::GetWorkstations()
  741. {
  742. char* result = NULL;
  743. if(userInfo != NULL)
  744. {
  745. result = UTF16ToUTF8(userInfo->usri3_workstations);
  746. }
  747. return result;
  748. }
  749. // ****************************************************************
  750. // NTUser::SetWorkstations
  751. // ****************************************************************
  752. int NTUser::SetWorkstations(char* workstations)
  753. {
  754. int result = 0;
  755. if(userInfo != NULL)
  756. {
  757. userInfo->usri3_workstations = UTF8ToUTF16(workstations);
  758. }
  759. else
  760. {
  761. result = -1;
  762. }
  763. return result;
  764. }
  765. // ****************************************************************
  766. // NTUser::GetFullname
  767. // ****************************************************************
  768. char* NTUser::GetFullname()
  769. {
  770. char* result = NULL;
  771. if(userInfo != NULL)
  772. {
  773. result = UTF16ToUTF8(userInfo->usri3_full_name);
  774. }
  775. return result;
  776. }
  777. // ****************************************************************
  778. // NTUser::SetFullname
  779. // ****************************************************************
  780. int NTUser::SetFullname(char* fullname)
  781. {
  782. int result = 0;
  783. if(userInfo != NULL)
  784. {
  785. userInfo->usri3_full_name = UTF8ToUTF16(fullname);
  786. }
  787. else
  788. {
  789. result = -1;
  790. }
  791. return result;
  792. }
  793. // ****************************************************************
  794. // NTUser::SetPassword
  795. // ****************************************************************
  796. int NTUser::SetPassword(char* password)
  797. {
  798. int result = 0;
  799. if(userInfo != NULL)
  800. {
  801. userInfo->usri3_password = UTF8ToUTF16(password);
  802. }
  803. else
  804. {
  805. result = -1;
  806. }
  807. return result;
  808. }
  809. // ****************************************************************
  810. // NTUser::AddToGroup
  811. // ****************************************************************
  812. int NTUser::AddToGroup(char* groupName)
  813. {
  814. int result = 0;
  815. if(userInfo != NULL)
  816. {
  817. result = NetGroupAddUser(NULL, UTF8ToUTF16(groupName), userInfo->usri3_name);
  818. }
  819. else
  820. {
  821. result = -1;
  822. }
  823. return result;
  824. }
  825. // ****************************************************************
  826. // NTUser::RemoveFromGroup
  827. // ****************************************************************
  828. int NTUser::RemoveFromGroup(char* groupName)
  829. {
  830. int result = 0;
  831. if(userInfo != NULL)
  832. {
  833. result = NetGroupDelUser(NULL, UTF8ToUTF16(groupName), userInfo->usri3_name);
  834. }
  835. else
  836. {
  837. result = -1;
  838. }
  839. return result;
  840. }
  841. // ****************************************************************
  842. // NTUser::LoadGroups
  843. // ****************************************************************
  844. int NTUser::LoadGroups()
  845. {
  846. int result = 0;
  847. if(userInfo != NULL)
  848. {
  849. if(groupsInfo != NULL)
  850. {
  851. NetApiBufferFree(groupsInfo);
  852. groupsInfo = NULL;
  853. currentGroupEntry = 0;
  854. groupEntriesRead = 0;
  855. groupEntriesTotal = 0;
  856. }
  857. result = NetUserGetGroups(NULL, userInfo->usri3_name, USER_GROUPS_INFO_LEVEL, (unsigned char**)&groupsInfo, MAX_PREFERRED_LENGTH, &groupEntriesRead, &groupEntriesTotal);
  858. }
  859. else
  860. {
  861. result = -1;
  862. }
  863. return result;
  864. }
  865. // ****************************************************************
  866. // NTUser::HasMoreGroups
  867. // ****************************************************************
  868. bool NTUser::HasMoreGroups()
  869. {
  870. bool result;
  871. if(currentGroupEntry < groupEntriesRead)
  872. {
  873. result = true;
  874. }
  875. else
  876. {
  877. result = false;
  878. }
  879. return result;
  880. }
  881. // ****************************************************************
  882. // NTUser::NextGroupName
  883. // ****************************************************************
  884. char* NTUser::NextGroupName()
  885. {
  886. char* groupName = NULL;
  887. GROUP_USERS_INFO_0* thisEntry;
  888. if(currentGroupEntry < groupEntriesRead)
  889. {
  890. thisEntry = &(groupsInfo[currentGroupEntry]);
  891. groupName = UTF16ToUTF8(thisEntry->grui0_name);
  892. currentGroupEntry++;
  893. }
  894. return groupName;
  895. }
  896. // ****************************************************************
  897. // NTUser::AddToLocalGroup
  898. // ****************************************************************
  899. int NTUser::AddToLocalGroup(char* localGroupName)
  900. {
  901. int result = 0;
  902. char userSID[256];
  903. wchar_t domain[256];
  904. DWORD SIDLen = sizeof(userSID);
  905. DWORD domainLen = sizeof(domain);
  906. SID_NAME_USE SIDUseIndicator;
  907. LOCALGROUP_MEMBERS_INFO_0 membersbuf[1];
  908. memset(&domain, 0, sizeof(domain));
  909. memset(&userSID, 0, sizeof(userSID));
  910. if(userInfo != NULL)
  911. {
  912. result = LookupAccountName(NULL, UTF16ToUTF8(userInfo->usri3_name), &userSID, &SIDLen, (LPTSTR)&domain, &domainLen, &SIDUseIndicator);
  913. if(result != 0)
  914. {
  915. membersbuf[0].lgrmi0_sid = &userSID;
  916. result = NetLocalGroupAddMembers(NULL, UTF8ToUTF16(localGroupName), 0, (LPBYTE)&membersbuf, 1);
  917. }
  918. }
  919. else
  920. {
  921. result = -1;
  922. }
  923. return result;
  924. }
  925. // ****************************************************************
  926. // NTUser::RemoveFromLocalGroup
  927. // ****************************************************************
  928. int NTUser::RemoveFromLocalGroup(char* localGroupName)
  929. {
  930. int result = 0;
  931. char userSID[256];
  932. wchar_t domain[256];
  933. DWORD SIDLen = sizeof(userSID);
  934. DWORD domainLen = sizeof(domain);
  935. SID_NAME_USE SIDUseIndicator;
  936. LOCALGROUP_MEMBERS_INFO_0 membersbuf[1];
  937. memset(&domain, 0, sizeof(domain));
  938. memset(&userSID, 0, sizeof(userSID));
  939. if(userInfo != NULL)
  940. {
  941. result = LookupAccountName(NULL, UTF16ToUTF8(userInfo->usri3_name), &userSID, &SIDLen, (LPTSTR)&domain, &domainLen, &SIDUseIndicator);
  942. if(result != 0)
  943. {
  944. membersbuf[0].lgrmi0_sid = &userSID;
  945. result = NetLocalGroupDelMembers(NULL, UTF8ToUTF16(localGroupName), 0, (LPBYTE)&membersbuf, 1);
  946. }
  947. }
  948. else
  949. {
  950. result = -1;
  951. }
  952. return result;
  953. }
  954. // ****************************************************************
  955. // NTUser::LoadLocalGroups
  956. // ****************************************************************
  957. int NTUser::LoadLocalGroups()
  958. {
  959. int result = 0;
  960. if(userInfo != NULL)
  961. {
  962. if(localGroupsInfo != NULL)
  963. {
  964. NetApiBufferFree(localGroupsInfo);
  965. localGroupsInfo = NULL;
  966. currentLocalGroupEntry = 0;
  967. localGroupEntriesRead = 0;
  968. localGroupEntriesTotal = 0;
  969. }
  970. result = NetUserGetLocalGroups(NULL, userInfo->usri3_name, 0, USER_LOCALGROUPS_INFO_LEVEL, (unsigned char**)&localGroupsInfo, MAX_PREFERRED_LENGTH, &localGroupEntriesRead, &localGroupEntriesTotal);
  971. }
  972. else
  973. {
  974. result = -1;
  975. }
  976. return result;
  977. }
  978. // ****************************************************************
  979. // NTUser::HasMoreLocalGroups
  980. // ****************************************************************
  981. bool NTUser::HasMoreLocalGroups()
  982. {
  983. bool result;
  984. if(currentLocalGroupEntry < localGroupEntriesRead)
  985. {
  986. result = true;
  987. }
  988. else
  989. {
  990. result = false;
  991. }
  992. return result;
  993. }
  994. // ****************************************************************
  995. // NTUser::NextLocalGroupName
  996. // ****************************************************************
  997. char* NTUser::NextLocalGroupName()
  998. {
  999. char* localGroupName = NULL;
  1000. LOCALGROUP_USERS_INFO_0* thisEntry;
  1001. if(currentLocalGroupEntry < localGroupEntriesRead)
  1002. {
  1003. thisEntry = &(localGroupsInfo[currentLocalGroupEntry]);
  1004. localGroupName = UTF16ToUTF8(thisEntry->lgrui0_name);
  1005. currentLocalGroupEntry++;
  1006. }
  1007. return localGroupName;
  1008. }
  1009. // ****************************************************************
  1010. // NTUserList::NTUserList
  1011. // ****************************************************************
  1012. NTUserList::NTUserList()
  1013. {
  1014. entriesRead = 0;
  1015. totalEntries = 0;
  1016. bufptr = NULL;
  1017. currentEntry = 0;
  1018. resumeHandle = 0;
  1019. }
  1020. // ****************************************************************
  1021. // NTUserList::~NTUserList
  1022. // ****************************************************************
  1023. NTUserList::~NTUserList()
  1024. {
  1025. if(bufptr != NULL)
  1026. {
  1027. NetApiBufferFree(bufptr);
  1028. bufptr = NULL;
  1029. }
  1030. }
  1031. // ****************************************************************
  1032. // NTUserList::loadList
  1033. // ****************************************************************
  1034. int NTUserList::loadList()
  1035. {
  1036. int result;
  1037. result = NetUserEnum(NULL, USER_INFO_LEVEL, 0, (LPBYTE*)&bufptr, MAX_PREFERRED_LENGTH, &entriesRead, &totalEntries, &resumeHandle);
  1038. return result;
  1039. }
  1040. // ****************************************************************
  1041. // NTUserList::hasMore
  1042. // ****************************************************************
  1043. bool NTUserList::hasMore()
  1044. {
  1045. bool result;
  1046. if(currentEntry < entriesRead)
  1047. {
  1048. result = true;
  1049. }
  1050. else
  1051. {
  1052. result = false;
  1053. }
  1054. return result;
  1055. }
  1056. // ****************************************************************
  1057. // NTUserList::nextUsername
  1058. // ****************************************************************
  1059. char* NTUserList::nextUsername()
  1060. {
  1061. char* username = NULL;
  1062. USER_INFO_3* thisEntry;
  1063. if(currentEntry < entriesRead)
  1064. {
  1065. thisEntry = &(bufptr[currentEntry]);
  1066. username = UTF16ToUTF8(thisEntry->usri3_name);
  1067. currentEntry++;
  1068. }
  1069. return username;
  1070. }
  1071. // ****************************************************************
  1072. // NTGroup::NTGroup
  1073. // ****************************************************************
  1074. NTGroup::NTGroup()
  1075. {
  1076. groupInfo = NULL;
  1077. usersInfo = NULL;
  1078. currentUserEntry = 0;
  1079. userEntriesRead = 0;
  1080. userEntriesTotal = 0;
  1081. }
  1082. // ****************************************************************
  1083. // NTGroup::~NTGroup
  1084. // ****************************************************************
  1085. NTGroup::~NTGroup()
  1086. {
  1087. if(groupInfo != NULL)
  1088. {
  1089. NetApiBufferFree(groupInfo);
  1090. groupInfo = NULL;
  1091. }
  1092. if(usersInfo != NULL)
  1093. {
  1094. NetApiBufferFree(usersInfo);
  1095. usersInfo = NULL;
  1096. }
  1097. }
  1098. // ****************************************************************
  1099. // NTGroup::NewGroup
  1100. // ****************************************************************
  1101. void NTGroup::NewGroup(char* groupName)
  1102. {
  1103. if(groupInfo != NULL)
  1104. {
  1105. NetApiBufferFree(groupInfo);
  1106. groupInfo = NULL;
  1107. }
  1108. NetApiBufferAllocate(sizeof(GROUP_INFO_2),(LPVOID*)&groupInfo);
  1109. memset(groupInfo, 0, sizeof(GROUP_INFO_2));
  1110. groupInfo->grpi2_name = UTF8ToUTF16(groupName);
  1111. }
  1112. // ****************************************************************
  1113. // NTGroup::DeleteGroup
  1114. // ****************************************************************
  1115. int NTGroup::DeleteGroup(char* groupName)
  1116. {
  1117. int result;
  1118. result = NetGroupDel(NULL, UTF8ToUTF16(groupName));
  1119. return result;
  1120. }
  1121. // ****************************************************************
  1122. // NTGroup::RetriveGroup
  1123. // ****************************************************************
  1124. int NTGroup::RetriveGroupByAccountName(char* groupName)
  1125. {
  1126. int result;
  1127. if(groupInfo != NULL)
  1128. {
  1129. NetApiBufferFree(groupInfo);
  1130. groupInfo = NULL;
  1131. }
  1132. result = NetGroupGetInfo(NULL, UTF8ToUTF16(groupName), GROUP_INFO_LEVEL, (unsigned char**)&groupInfo);
  1133. return result;
  1134. }
  1135. // ****************************************************************
  1136. // NTGroup::RetriveGroupBySIDHexStr
  1137. // ****************************************************************
  1138. int NTGroup::RetriveGroupBySIDHexStr(char* sidHexStr)
  1139. {
  1140. int result = 0;
  1141. char* groupName;
  1142. if(groupInfo != NULL)
  1143. {
  1144. NetApiBufferFree(groupInfo);
  1145. groupInfo = NULL;
  1146. }
  1147. if(GetAccountNameBySIDHexStr(sidHexStr, &groupName) != 0)
  1148. {
  1149. result = -1;
  1150. goto exit;
  1151. }
  1152. if(RetriveGroupByAccountName(groupName) != 0)
  1153. {
  1154. result =-1;
  1155. goto exit;
  1156. }
  1157. exit:
  1158. return result;
  1159. }
  1160. // ****************************************************************
  1161. // NTGroup::AddGroup
  1162. // ****************************************************************
  1163. int NTGroup::AddGroup()
  1164. {
  1165. int result;
  1166. DWORD badParam = 0;
  1167. result = NetGroupAdd(NULL, GROUP_INFO_LEVEL, (unsigned char*)groupInfo, &badParam);
  1168. return result;
  1169. }
  1170. // ****************************************************************
  1171. // NTGroup::StoreGroup
  1172. // ****************************************************************
  1173. int NTGroup::StoreGroup()
  1174. {
  1175. int result = -1;
  1176. if(groupInfo != NULL)
  1177. {
  1178. result = NetGroupSetInfo(NULL, groupInfo->grpi2_name, GROUP_INFO_LEVEL, (unsigned char*)groupInfo, NULL);
  1179. }
  1180. return result;
  1181. }
  1182. // ****************************************************************
  1183. // NTGroup::GetAccountName
  1184. // ****************************************************************
  1185. char* NTGroup::GetAccountName()
  1186. {
  1187. char* result = NULL;
  1188. if(groupInfo != NULL)
  1189. {
  1190. result = UTF16ToUTF8(groupInfo->grpi2_name);
  1191. }
  1192. return result;
  1193. }
  1194. // ****************************************************************
  1195. // NTGroup::GetSIDHexStr
  1196. // ****************************************************************
  1197. char* NTGroup::GetSIDHexStr()
  1198. {
  1199. char* result = NULL;
  1200. if(groupInfo != NULL)
  1201. {
  1202. GetSIDHexStrByAccountName(UTF16ToUTF8(groupInfo->grpi2_name), &result);
  1203. }
  1204. return result;
  1205. }
  1206. // ****************************************************************
  1207. // NTGroup::GetComment
  1208. // ****************************************************************
  1209. char* NTGroup::GetComment()
  1210. {
  1211. char* result = NULL;
  1212. if(groupInfo != NULL)
  1213. {
  1214. result = UTF16ToUTF8(groupInfo->grpi2_comment);
  1215. }
  1216. return result;
  1217. }
  1218. // ****************************************************************
  1219. // NTGroup::SetComment
  1220. // ****************************************************************
  1221. int NTGroup::SetComment(char* comment)
  1222. {
  1223. int result = 0;
  1224. if(groupInfo != NULL)
  1225. {
  1226. groupInfo->grpi2_comment = UTF8ToUTF16(comment);
  1227. }
  1228. else
  1229. {
  1230. result = -1;
  1231. }
  1232. return result;
  1233. }
  1234. // ****************************************************************
  1235. // NTGroup::AddUser
  1236. // ****************************************************************
  1237. int NTGroup::AddUser(char* userName)
  1238. {
  1239. int result = 0;
  1240. if(groupInfo != NULL)
  1241. {
  1242. result = NetGroupAddUser(NULL, groupInfo->grpi2_name, UTF8ToUTF16(userName));
  1243. }
  1244. else
  1245. {
  1246. result = -1;
  1247. }
  1248. return result;
  1249. }
  1250. // ****************************************************************
  1251. // NTGroup::RemoveUser
  1252. // ****************************************************************
  1253. int NTGroup::RemoveUser(char* userName)
  1254. {
  1255. int result = 0;
  1256. if(groupInfo != NULL)
  1257. {
  1258. result = NetGroupDelUser(NULL, groupInfo->grpi2_name, UTF8ToUTF16(userName));
  1259. }
  1260. else
  1261. {
  1262. result = -1;
  1263. }
  1264. return result;
  1265. }
  1266. // ****************************************************************
  1267. // NTGroup::LoadUsers
  1268. // ****************************************************************
  1269. int NTGroup::LoadUsers()
  1270. {
  1271. int result = 0;
  1272. if(groupInfo != NULL)
  1273. {
  1274. if(usersInfo != NULL)
  1275. {
  1276. NetApiBufferFree(usersInfo);
  1277. usersInfo = NULL;
  1278. currentUserEntry = 0;
  1279. userEntriesRead = 0;
  1280. userEntriesTotal = 0;
  1281. }
  1282. result = NetGroupGetUsers(NULL, groupInfo->grpi2_name, GROUP_USERS_INFO_LEVEL, (unsigned char**)&usersInfo, MAX_PREFERRED_LENGTH, &userEntriesRead, &userEntriesTotal, NULL);
  1283. }
  1284. else
  1285. {
  1286. result = -1;
  1287. }
  1288. return result;
  1289. }
  1290. // ****************************************************************
  1291. // NTGroup::HasMoreUsers
  1292. // ****************************************************************
  1293. bool NTGroup::HasMoreUsers()
  1294. {
  1295. bool result;
  1296. if(currentUserEntry < userEntriesRead)
  1297. {
  1298. result = true;
  1299. }
  1300. else
  1301. {
  1302. result = false;
  1303. }
  1304. return result;
  1305. }
  1306. // ****************************************************************
  1307. // NTGroup::NextUserName
  1308. // ****************************************************************
  1309. char* NTGroup::NextUserName()
  1310. {
  1311. char* userName = NULL;
  1312. LOCALGROUP_USERS_INFO_0* thisEntry;
  1313. if(currentUserEntry < userEntriesRead)
  1314. {
  1315. thisEntry = &(usersInfo[currentUserEntry]);
  1316. userName = UTF16ToUTF8(thisEntry->lgrui0_name);
  1317. currentUserEntry++;
  1318. }
  1319. return userName;
  1320. }
  1321. // ****************************************************************
  1322. // NTGroupList::NTGroupList
  1323. // ****************************************************************
  1324. NTGroupList::NTGroupList()
  1325. {
  1326. entriesRead = 0;
  1327. totalEntries = 0;
  1328. bufptr = NULL;
  1329. currentEntry = 0;
  1330. resumeHandle = 0;
  1331. }
  1332. // ****************************************************************
  1333. // NTGroupList::~NTGroupList
  1334. // ****************************************************************
  1335. NTGroupList::~NTGroupList()
  1336. {
  1337. if(bufptr != NULL)
  1338. {
  1339. NetApiBufferFree(bufptr);
  1340. bufptr = NULL;
  1341. }
  1342. }
  1343. // ****************************************************************
  1344. // NTGroupList::loadList
  1345. // ****************************************************************
  1346. int NTGroupList::loadList()
  1347. {
  1348. int result;
  1349. result = NetGroupEnum(NULL, GROUP_INFO_LEVEL, (LPBYTE*)&bufptr, MAX_PREFERRED_LENGTH, &entriesRead, &totalEntries, &resumeHandle);
  1350. return result;
  1351. }
  1352. // ****************************************************************
  1353. // NTGroupList::hasMore
  1354. // ****************************************************************
  1355. bool NTGroupList::hasMore()
  1356. {
  1357. bool result;
  1358. if(currentEntry < entriesRead)
  1359. {
  1360. result = true;
  1361. }
  1362. else
  1363. {
  1364. result = false;
  1365. }
  1366. return result;
  1367. }
  1368. // ****************************************************************
  1369. // NTGroupList::nextGroupName
  1370. // ****************************************************************
  1371. char* NTGroupList::nextGroupName()
  1372. {
  1373. char* groupName = NULL;
  1374. GROUP_INFO_2* thisEntry;
  1375. if(currentEntry < entriesRead)
  1376. {
  1377. thisEntry = &(bufptr[currentEntry]);
  1378. groupName = UTF16ToUTF8(thisEntry->grpi2_name);
  1379. currentEntry++;
  1380. }
  1381. return groupName;
  1382. }
  1383. // ****************************************************************
  1384. // NTLocalGroup::NTLocalGroup
  1385. // ****************************************************************
  1386. NTLocalGroup::NTLocalGroup()
  1387. {
  1388. localGroupInfo = NULL;
  1389. usersInfo = NULL;
  1390. currentUserEntry = 0;
  1391. userEntriesRead = 0;
  1392. userEntriesTotal = 0;
  1393. }
  1394. // ****************************************************************
  1395. // NTLocalGroup::~NTLocalGroup
  1396. // ****************************************************************
  1397. NTLocalGroup::~NTLocalGroup()
  1398. {
  1399. if(localGroupInfo != NULL)
  1400. {
  1401. NetApiBufferFree(localGroupInfo);
  1402. localGroupInfo = NULL;
  1403. }
  1404. if(usersInfo != NULL)
  1405. {
  1406. NetApiBufferFree(usersInfo);
  1407. usersInfo = NULL;
  1408. }
  1409. }
  1410. // ****************************************************************
  1411. // NTLocalGroup::NewLocalGroup
  1412. // ****************************************************************
  1413. void NTLocalGroup::NewLocalGroup(char* localGroupName)
  1414. {
  1415. if(localGroupInfo != NULL)
  1416. {
  1417. NetApiBufferFree(localGroupInfo);
  1418. localGroupInfo = NULL;
  1419. }
  1420. NetApiBufferAllocate(sizeof(LOCALGROUP_INFO_1),(LPVOID*)&localGroupInfo);
  1421. memset(localGroupInfo, 0, sizeof(LOCALGROUP_INFO_1));
  1422. localGroupInfo->lgrpi1_name = UTF8ToUTF16(localGroupName);
  1423. }
  1424. // ****************************************************************
  1425. // NTLocalGroup::DeleteLocalGroup
  1426. // ****************************************************************
  1427. int NTLocalGroup::DeleteLocalGroup(char* localGroupName)
  1428. {
  1429. int result;
  1430. result = NetLocalGroupDel(NULL, UTF8ToUTF16(localGroupName));
  1431. return result;
  1432. }
  1433. // ****************************************************************
  1434. // NTLocalGroup::RetriveLocalGroup
  1435. // ****************************************************************
  1436. int NTLocalGroup::RetriveLocalGroupByAccountName(char* localGroupName)
  1437. {
  1438. int result;
  1439. if(localGroupInfo != NULL)
  1440. {
  1441. NetApiBufferFree(localGroupInfo);
  1442. localGroupInfo = NULL;
  1443. }
  1444. result = NetLocalGroupGetInfo(NULL, UTF8ToUTF16(localGroupName), LOCALGROUP_INFO_LEVEL, (unsigned char**)&localGroupInfo);
  1445. return result;
  1446. }
  1447. // ****************************************************************
  1448. // NTGroup::RetriveLocalGroupBySIDHexStr
  1449. // ****************************************************************
  1450. int NTLocalGroup::RetriveLocalGroupBySIDHexStr(char* sidHexStr)
  1451. {
  1452. int result = 0;
  1453. char* localGroupName;
  1454. if(localGroupInfo != NULL)
  1455. {
  1456. NetApiBufferFree(localGroupInfo);
  1457. localGroupInfo = NULL;
  1458. }
  1459. if(GetAccountNameBySIDHexStr(sidHexStr, &localGroupName) != 0)
  1460. {
  1461. result = -1;
  1462. goto exit;
  1463. }
  1464. if(RetriveLocalGroupByAccountName(localGroupName) != 0)
  1465. {
  1466. result =-1;
  1467. goto exit;
  1468. }
  1469. exit:
  1470. return result;
  1471. }
  1472. // ****************************************************************
  1473. // NTLocalGroup::AddLocalGroup
  1474. // ****************************************************************
  1475. int NTLocalGroup::AddLocalGroup()
  1476. {
  1477. int result;
  1478. DWORD badParam = 0;
  1479. result = NetLocalGroupAdd(NULL, LOCALGROUP_INFO_LEVEL, (unsigned char*)localGroupInfo, &badParam);
  1480. return result;
  1481. }
  1482. // ****************************************************************
  1483. // NTLocalGroup::StoreLocalGroup
  1484. // ****************************************************************
  1485. int NTLocalGroup::StoreLocalGroup()
  1486. {
  1487. int result = 0;
  1488. if(localGroupInfo != NULL)
  1489. {
  1490. result = NetLocalGroupSetInfo(NULL, localGroupInfo->lgrpi1_name, LOCALGROUP_INFO_LEVEL, (unsigned char*)localGroupInfo, NULL);
  1491. }
  1492. else
  1493. {
  1494. result = -1;
  1495. }
  1496. return result;
  1497. }
  1498. // ****************************************************************
  1499. // NTLocalGroup::GetAccountName
  1500. // ****************************************************************
  1501. char* NTLocalGroup::GetAccountName()
  1502. {
  1503. char* result = NULL;
  1504. if(localGroupInfo != NULL)
  1505. {
  1506. result = UTF16ToUTF8(localGroupInfo->lgrpi1_name);
  1507. }
  1508. return result;
  1509. }
  1510. // ****************************************************************
  1511. // NTLocalGroup::GetSIDHexStr
  1512. // ****************************************************************
  1513. char* NTLocalGroup::GetSIDHexStr()
  1514. {
  1515. char* result = NULL;
  1516. if(localGroupInfo != NULL)
  1517. {
  1518. GetSIDHexStrByAccountName(UTF16ToUTF8(localGroupInfo->lgrpi1_name), &result);
  1519. }
  1520. return result;
  1521. }
  1522. // ****************************************************************
  1523. // NTLocalGroup::GetComment
  1524. // ****************************************************************
  1525. char* NTLocalGroup::GetComment()
  1526. {
  1527. char* result = NULL;
  1528. if(localGroupInfo != NULL)
  1529. {
  1530. result = UTF16ToUTF8(localGroupInfo->lgrpi1_comment);
  1531. }
  1532. return result;
  1533. }
  1534. // ****************************************************************
  1535. // NTLocalGroup::SetComment
  1536. // ****************************************************************
  1537. int NTLocalGroup::SetComment(char* comment)
  1538. {
  1539. int result = 0;
  1540. if(localGroupInfo != NULL)
  1541. {
  1542. localGroupInfo->lgrpi1_comment = UTF8ToUTF16(comment);
  1543. }
  1544. else
  1545. {
  1546. result = -1;
  1547. }
  1548. return result;
  1549. }
  1550. // ****************************************************************
  1551. // NTLocalGroup::AddUser
  1552. // ****************************************************************
  1553. int NTLocalGroup::AddUser(char* username)
  1554. {
  1555. int result = 0;
  1556. LOCALGROUP_MEMBERS_INFO_0 members[1];
  1557. if(localGroupInfo != NULL)
  1558. {
  1559. GetSIDByAccountName(username, (char**)&members[0].lgrmi0_sid);
  1560. result = NetLocalGroupAddMembers(NULL, localGroupInfo->lgrpi1_name, 0, (unsigned char*)&members, 1);
  1561. free(members[0].lgrmi0_sid);
  1562. }
  1563. else
  1564. {
  1565. result = -1;
  1566. }
  1567. return result;
  1568. }
  1569. // ****************************************************************
  1570. // NTLocalGroup::RemoveUser
  1571. // ****************************************************************
  1572. int NTLocalGroup::RemoveUser(char* username)
  1573. {
  1574. int result = 0;
  1575. LOCALGROUP_MEMBERS_INFO_0 members[1];
  1576. if(localGroupInfo != NULL)
  1577. {
  1578. GetSIDByAccountName(username, (char**)&members[0].lgrmi0_sid);
  1579. result = NetLocalGroupDelMembers(NULL, localGroupInfo->lgrpi1_name, 0, (unsigned char*)&members, 1);
  1580. free(members[0].lgrmi0_sid);
  1581. }
  1582. else
  1583. {
  1584. result = -1;
  1585. }
  1586. return result;
  1587. }
  1588. // ****************************************************************
  1589. // NTLocalGroup::LoadUsers
  1590. // ****************************************************************
  1591. int NTLocalGroup::LoadUsers()
  1592. {
  1593. int result = 0;
  1594. if(localGroupInfo != NULL)
  1595. {
  1596. if(usersInfo != NULL)
  1597. {
  1598. NetApiBufferFree(usersInfo);
  1599. usersInfo = NULL;
  1600. currentUserEntry = 0;
  1601. userEntriesRead = 0;
  1602. userEntriesTotal = 0;
  1603. }
  1604. result = NetLocalGroupGetMembers(NULL, localGroupInfo->lgrpi1_name, LOCALGROUP_USERS_INFO_LEVEL, (unsigned char**)&usersInfo, MAX_PREFERRED_LENGTH, &userEntriesRead, &userEntriesTotal, NULL);
  1605. }
  1606. else
  1607. {
  1608. result = -1;
  1609. }
  1610. return result;
  1611. }
  1612. // ****************************************************************
  1613. // NTLocalGroup::HasMoreUsers
  1614. // ****************************************************************
  1615. bool NTLocalGroup::HasMoreUsers()
  1616. {
  1617. bool result;
  1618. if(currentUserEntry < userEntriesRead)
  1619. {
  1620. result = true;
  1621. }
  1622. else
  1623. {
  1624. result = false;
  1625. }
  1626. return result;
  1627. }
  1628. // ****************************************************************
  1629. // NTLocalGroup::NextUserName
  1630. // ****************************************************************
  1631. char* NTLocalGroup::NextUserName()
  1632. {
  1633. char* username = NULL;
  1634. LOCALGROUP_MEMBERS_INFO_0* thisEntry;
  1635. if(currentUserEntry < userEntriesRead)
  1636. {
  1637. thisEntry = &(usersInfo[currentUserEntry]);
  1638. GetAccountNameBySID((char*)thisEntry->lgrmi0_sid, &username);
  1639. currentUserEntry++;
  1640. }
  1641. return username;
  1642. }
  1643. // ****************************************************************
  1644. // NTLocalGroupList::NTLocalGroupList
  1645. // ****************************************************************
  1646. NTLocalGroupList::NTLocalGroupList()
  1647. {
  1648. entriesRead = 0;
  1649. totalEntries = 0;
  1650. bufptr = NULL;
  1651. currentEntry = 0;
  1652. resumeHandle = 0;
  1653. }
  1654. // ****************************************************************
  1655. // NTLocalGroupList::~NTLocalGroupList
  1656. // ****************************************************************
  1657. NTLocalGroupList::~NTLocalGroupList()
  1658. {
  1659. if(bufptr != NULL)
  1660. {
  1661. NetApiBufferFree(bufptr);
  1662. bufptr = NULL;
  1663. }
  1664. }
  1665. // ****************************************************************
  1666. // NTLocalGroupList::loadList
  1667. // ****************************************************************
  1668. int NTLocalGroupList::loadList()
  1669. {
  1670. int result;
  1671. result = NetLocalGroupEnum(NULL, LOCALGROUP_INFO_LEVEL, (LPBYTE*)&bufptr, MAX_PREFERRED_LENGTH, &entriesRead, &totalEntries, &resumeHandle);
  1672. return result;
  1673. }
  1674. // ****************************************************************
  1675. // NTLocalGroupList::hasMore
  1676. // ****************************************************************
  1677. bool NTLocalGroupList::hasMore()
  1678. {
  1679. bool result;
  1680. if(currentEntry < entriesRead)
  1681. {
  1682. result = true;
  1683. }
  1684. else
  1685. {
  1686. result = false;
  1687. }
  1688. return result;
  1689. }
  1690. // ****************************************************************
  1691. // NTLocalGroupList::nextLocalGroupName
  1692. // ****************************************************************
  1693. char* NTLocalGroupList::nextLocalGroupName()
  1694. {
  1695. char* localGroupName = NULL;
  1696. LOCALGROUP_INFO_1* thisEntry;
  1697. if(currentEntry < entriesRead)
  1698. {
  1699. thisEntry = &(bufptr[currentEntry]);
  1700. localGroupName = UTF16ToUTF8(thisEntry->lgrpi1_name);
  1701. currentEntry++;
  1702. }
  1703. return localGroupName;
  1704. }