Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

665 lines
14 KiB

  1. #include "winnt.hxx"
  2. #pragma hdrstop
  3. HRESULT
  4. WinNTCreateComputer(
  5. LPWSTR szServerName,
  6. LPWSTR szComputerName
  7. );
  8. HRESULT
  9. WinNTDeleteComputer(
  10. LPWSTR szServerName,
  11. LPWSTR szComputerName
  12. );
  13. HRESULT
  14. WinNTCreateLocalGroup(
  15. LPWSTR szServerName,
  16. LPWSTR szGroupName
  17. );
  18. HRESULT
  19. WinNTCreateGlobalGroup(
  20. LPWSTR szServerName,
  21. LPWSTR szGroupName
  22. );
  23. HRESULT
  24. WinNTDeleteLocalGroup(
  25. LPWSTR szServerName,
  26. LPWSTR szGroupName
  27. );
  28. HRESULT
  29. WinNTDeleteGlobalGroup(
  30. LPWSTR szServerName,
  31. LPWSTR szGroupName
  32. );
  33. HRESULT
  34. WinNTCreateUser(
  35. LPWSTR szServerName,
  36. LPWSTR szUserName
  37. );
  38. HRESULT
  39. WinNTDeleteUser(
  40. LPWSTR szServerName,
  41. LPWSTR szUserName
  42. );
  43. HRESULT
  44. WinNTCreateComputer(
  45. LPWSTR szServerName,
  46. LPWSTR szComputerName
  47. )
  48. {
  49. HRESULT hr = S_OK;
  50. WCHAR szTargBuffer[MAX_PATH];
  51. WCHAR szComputerBuffer[MAX_PATH];
  52. USER_INFO_1 UserInfo1;
  53. PUSER_INFO_1 pUserInfo1 = &UserInfo1;
  54. NET_API_STATUS nasStatus;
  55. DWORD dwParmErr;
  56. if (!szServerName || !szComputerName ) {
  57. RRETURN(E_FAIL);
  58. }
  59. wcscpy(szComputerBuffer, szComputerName);
  60. wcscat(szComputerBuffer, L"$");
  61. memset(pUserInfo1, 0, sizeof(USER_INFO_1));
  62. pUserInfo1->usri1_name = szComputerBuffer;
  63. pUserInfo1->usri1_password = NULL;
  64. pUserInfo1->usri1_password_age = 0;
  65. pUserInfo1->usri1_priv = USER_PRIV_USER;
  66. pUserInfo1->usri1_home_dir = NULL;
  67. pUserInfo1->usri1_comment = NULL;
  68. pUserInfo1->usri1_flags = UF_SCRIPT | UF_WORKSTATION_TRUST_ACCOUNT ;
  69. pUserInfo1->usri1_script_path = NULL;
  70. hr = MakeUncName(
  71. szServerName,
  72. szTargBuffer
  73. );
  74. BAIL_ON_FAILURE(hr);
  75. nasStatus = NetUserAdd(
  76. szTargBuffer,
  77. 1,
  78. (LPBYTE)pUserInfo1,
  79. &dwParmErr
  80. );
  81. hr = HRESULT_FROM_WIN32(nasStatus);
  82. BAIL_ON_FAILURE(hr);
  83. error:
  84. RRETURN(hr);
  85. }
  86. HRESULT
  87. WinNTDeleteComputer(
  88. LPWSTR szServerName,
  89. LPWSTR szComputerName
  90. )
  91. {
  92. HRESULT hr = S_OK;
  93. NET_API_STATUS nasStatus;
  94. WCHAR szComputerBuffer[MAX_PATH];
  95. WCHAR szTargBuffer[MAX_PATH];
  96. if (!szServerName || !szComputerName ) {
  97. RRETURN(E_FAIL);
  98. }
  99. wcscpy(szComputerBuffer, szComputerName);
  100. wcscat(szComputerBuffer, L"$");
  101. hr = MakeUncName(
  102. szServerName,
  103. szTargBuffer
  104. );
  105. BAIL_ON_FAILURE(hr);
  106. nasStatus = NetUserDel(
  107. szTargBuffer,
  108. szComputerBuffer
  109. );
  110. hr = HRESULT_FROM_WIN32(nasStatus);
  111. BAIL_ON_FAILURE(hr);
  112. error:
  113. RRETURN(hr);
  114. }
  115. HRESULT
  116. WinNTCreateGlobalGroup(
  117. LPWSTR szServerName,
  118. LPWSTR szGroupName
  119. )
  120. {
  121. HRESULT hr = S_OK;
  122. WCHAR szTargBuffer[MAX_PATH];
  123. GROUP_INFO_1 GroupInfo1;
  124. PGROUP_INFO_1 pGroupInfo1 = &GroupInfo1;
  125. NET_API_STATUS nasStatus;
  126. DWORD dwParmErr;
  127. memset(pGroupInfo1, 0, sizeof(GROUP_INFO_1));
  128. pGroupInfo1->grpi1_name = szGroupName;
  129. if (!szServerName || !szGroupName ) {
  130. RRETURN(E_FAIL);
  131. }
  132. hr = MakeUncName(
  133. szServerName,
  134. szTargBuffer
  135. );
  136. BAIL_ON_FAILURE(hr);
  137. nasStatus = NetGroupAdd(
  138. szTargBuffer,
  139. 1,
  140. (LPBYTE)pGroupInfo1,
  141. &dwParmErr
  142. );
  143. hr = HRESULT_FROM_WIN32(nasStatus);
  144. BAIL_ON_FAILURE(hr);
  145. error:
  146. RRETURN(hr);
  147. }
  148. HRESULT
  149. WinNTCreateLocalGroup(
  150. LPWSTR szServerName,
  151. LPWSTR szGroupName
  152. )
  153. {
  154. HRESULT hr = S_OK;
  155. WCHAR szTargBuffer[MAX_PATH];
  156. LOCALGROUP_INFO_1 LocalGroupInfo1;
  157. PLOCALGROUP_INFO_1 pLocalGroupInfo1 = &LocalGroupInfo1;
  158. NET_API_STATUS nasStatus;
  159. DWORD dwParmErr;
  160. memset(pLocalGroupInfo1, 0, sizeof(LOCALGROUP_INFO_1));
  161. pLocalGroupInfo1->lgrpi1_name = szGroupName;
  162. if (!szServerName || !szGroupName ) {
  163. RRETURN(E_FAIL);
  164. }
  165. hr = MakeUncName(
  166. szServerName,
  167. szTargBuffer
  168. );
  169. BAIL_ON_FAILURE(hr);
  170. nasStatus = NetLocalGroupAdd(
  171. szTargBuffer,
  172. 1,
  173. (LPBYTE)pLocalGroupInfo1,
  174. &dwParmErr
  175. );
  176. hr = HRESULT_FROM_WIN32(nasStatus);
  177. BAIL_ON_FAILURE(hr);
  178. error:
  179. RRETURN(hr);
  180. }
  181. HRESULT
  182. WinNTDeleteLocalGroup(
  183. LPWSTR szServerName,
  184. LPWSTR szGroupName
  185. )
  186. {
  187. WCHAR szTargBuffer[MAX_PATH];
  188. HRESULT hr;
  189. NET_API_STATUS nasStatus;
  190. if (!szServerName || !szGroupName ) {
  191. RRETURN(E_FAIL);
  192. }
  193. hr = MakeUncName(
  194. szServerName,
  195. szTargBuffer
  196. );
  197. BAIL_ON_FAILURE(hr);
  198. nasStatus = NetLocalGroupDel(
  199. szTargBuffer,
  200. szGroupName
  201. );
  202. hr = HRESULT_FROM_WIN32(nasStatus);
  203. BAIL_ON_FAILURE(hr);
  204. error:
  205. RRETURN(hr);
  206. }
  207. HRESULT
  208. WinNTDeleteGlobalGroup(
  209. LPWSTR szServerName,
  210. LPWSTR szGroupName
  211. )
  212. {
  213. HRESULT hr;
  214. NET_API_STATUS nasStatus;
  215. WCHAR szTargBuffer[MAX_PATH];
  216. if (!szServerName || !szGroupName ) {
  217. RRETURN(E_FAIL);
  218. }
  219. hr = MakeUncName(
  220. szServerName,
  221. szTargBuffer
  222. );
  223. BAIL_ON_FAILURE(hr);
  224. nasStatus = NetGroupDel(
  225. szTargBuffer,
  226. szGroupName
  227. );
  228. hr = HRESULT_FROM_WIN32(nasStatus);
  229. BAIL_ON_FAILURE(hr);
  230. error:
  231. RRETURN(hr);
  232. }
  233. HRESULT
  234. WinNTCreateUser(
  235. LPWSTR szServerName,
  236. LPWSTR szUserName,
  237. LPWSTR szUserPassword
  238. )
  239. {
  240. HRESULT hr = S_OK;
  241. WCHAR szTargBuffer[MAX_PATH];
  242. USER_INFO_1 UserInfo1;
  243. PUSER_INFO_1 pUserInfo1 = &UserInfo1;
  244. NET_API_STATUS nasStatus;
  245. DWORD dwParmErr;
  246. WCHAR szCompName[MAX_PATH];
  247. DWORD dwSize = MAX_PATH;
  248. if (!szServerName || !szUserName ) {
  249. RRETURN(E_FAIL);
  250. }
  251. memset(pUserInfo1, 0, sizeof(USER_INFO_1));
  252. pUserInfo1->usri1_name = szUserName;
  253. pUserInfo1->usri1_password = szUserPassword;
  254. pUserInfo1->usri1_password_age = DEF_MAX_PWAGE;
  255. pUserInfo1->usri1_priv = 1;
  256. pUserInfo1->usri1_home_dir = NULL;
  257. pUserInfo1->usri1_comment = NULL;
  258. pUserInfo1->usri1_script_path = NULL;
  259. pUserInfo1->usri1_flags = UF_NORMAL_ACCOUNT | UF_SCRIPT;
  260. hr = MakeUncName(
  261. szServerName,
  262. szTargBuffer
  263. );
  264. BAIL_ON_FAILURE(hr);
  265. nasStatus = NetUserAdd(
  266. szTargBuffer,
  267. USER_PRIV_USER,
  268. (LPBYTE)pUserInfo1,
  269. &dwParmErr
  270. );
  271. hr = HRESULT_FROM_WIN32(nasStatus);
  272. //
  273. // If we fail without workstation services, check if the
  274. // machine name matches and if so add with NULL as name
  275. //
  276. if (hr == HRESULT_FROM_WIN32(NERR_WkstaNotStarted)) {
  277. if (!GetComputerName(szCompName, &dwSize)) {
  278. // We cannot get the computer name so bail
  279. BAIL_ON_FAILURE(hr);
  280. }
  281. // Compare the names before we continue
  282. #ifdef WIN95
  283. if (_wcsicmp(szServerName, szCompName)) {
  284. #else
  285. if (CompareStringW(
  286. LOCALE_SYSTEM_DEFAULT,
  287. NORM_IGNORECASE,
  288. szServerName,
  289. -1,
  290. szCompName,
  291. -1
  292. ) != CSTR_EQUAL ) {
  293. #endif
  294. // names do not match
  295. BAIL_ON_FAILURE(hr);
  296. }
  297. nasStatus = NetUserAdd(
  298. NULL,
  299. USER_PRIV_USER,
  300. (LPBYTE)pUserInfo1,
  301. &dwParmErr
  302. );
  303. hr = HRESULT_FROM_WIN32(nasStatus);
  304. }
  305. BAIL_ON_FAILURE(hr);
  306. error:
  307. RRETURN(hr);
  308. }
  309. HRESULT
  310. WinNTDeleteUser(
  311. LPWSTR szServerName,
  312. LPWSTR szUserName
  313. )
  314. {
  315. HRESULT hr;
  316. NET_API_STATUS nasStatus;
  317. WCHAR szTargBuffer[MAX_PATH];
  318. WCHAR szCompName[MAX_PATH];
  319. DWORD dwSize = MAX_PATH;
  320. if (!szServerName || !szUserName ) {
  321. RRETURN(E_FAIL);
  322. }
  323. hr = MakeUncName(
  324. szServerName,
  325. szTargBuffer
  326. );
  327. BAIL_ON_FAILURE(hr);
  328. nasStatus = NetUserDel(
  329. szTargBuffer,
  330. szUserName
  331. );
  332. hr = HRESULT_FROM_WIN32(nasStatus);
  333. //
  334. // If we fail without workstation services, check if the
  335. // machine name matches and if so add with NULL as name
  336. //
  337. if (hr == HRESULT_FROM_WIN32(NERR_WkstaNotStarted)) {
  338. if (!GetComputerName(szCompName, &dwSize)) {
  339. // We cannot get the computer name so bail
  340. BAIL_ON_FAILURE(hr);
  341. }
  342. // Compare the names before we continue
  343. #ifdef WIN95
  344. if (_wcsicmp(szServerName, szCompName)) {
  345. #else
  346. if (CompareStringW(
  347. LOCALE_SYSTEM_DEFAULT,
  348. NORM_IGNORECASE,
  349. szServerName,
  350. -1,
  351. szCompName,
  352. -1
  353. ) != CSTR_EQUAL ) {
  354. #endif
  355. // names do not match
  356. BAIL_ON_FAILURE(hr);
  357. }
  358. nasStatus = NetUserDel(
  359. NULL,
  360. szUserName
  361. );
  362. hr = HRESULT_FROM_WIN32(nasStatus);
  363. }
  364. BAIL_ON_FAILURE(hr);
  365. error:
  366. RRETURN(hr);
  367. }
  368. HRESULT
  369. WinNTDeleteGroup(
  370. POBJECTINFO pObjectInfo,
  371. DWORD dwGroupType,
  372. const CWinNTCredentials& Credentials
  373. )
  374. {
  375. WCHAR szHostServerName[MAX_PATH];
  376. HRESULT hr;
  377. NET_API_STATUS nasStatus;
  378. if (!pObjectInfo) {
  379. RRETURN(E_FAIL);
  380. }
  381. switch (pObjectInfo->NumComponents) {
  382. case 2:
  383. hr = WinNTGetCachedDCName(
  384. pObjectInfo->ComponentArray[0],
  385. szHostServerName,
  386. Credentials.GetFlags() // we need writeable dc
  387. );
  388. BAIL_ON_FAILURE(hr);
  389. if (dwGroupType == WINNT_GROUP_EITHER) {
  390. //
  391. // - try both local and global groups if "group" for backward
  392. // compatability
  393. // - confirmed with cliffv (no global and local group under same
  394. // container in nt4 or nt5. So ok.
  395. hr = WinNTDeleteGlobalGroup(
  396. (szHostServerName +2),
  397. pObjectInfo->ComponentArray[1]
  398. );
  399. if (FAILED(hr)) {
  400. hr = WinNTDeleteLocalGroup(
  401. (szHostServerName + 2),
  402. pObjectInfo->ComponentArray[1]
  403. );
  404. }
  405. } else if (dwGroupType == WINNT_GROUP_LOCAL) {
  406. hr = WinNTDeleteLocalGroup(
  407. (szHostServerName + 2),
  408. pObjectInfo->ComponentArray[1]
  409. );
  410. } else if (dwGroupType == WINNT_GROUP_GLOBAL) {
  411. hr = WinNTDeleteGlobalGroup(
  412. (szHostServerName + 2),
  413. pObjectInfo->ComponentArray[1]
  414. );
  415. } else {
  416. //
  417. // private funct'n -> must be ADSI own coding bug
  418. //
  419. ADsAssert(FALSE);
  420. }
  421. BAIL_ON_FAILURE(hr);
  422. break;
  423. case 3:
  424. if (dwGroupType == WINNT_GROUP_EITHER) {
  425. //
  426. // - try both local and global groups if "group" for backward
  427. // compatability
  428. // - confirmed with cliffv (no global and local group under same
  429. // container in nt4 or nt5. So ok.
  430. hr = WinNTDeleteGlobalGroup(
  431. pObjectInfo->ComponentArray[1],
  432. pObjectInfo->ComponentArray[2]
  433. );
  434. if (FAILED(hr)) {
  435. hr = WinNTDeleteLocalGroup(
  436. pObjectInfo->ComponentArray[1],
  437. pObjectInfo->ComponentArray[2]
  438. );
  439. }
  440. } else if (dwGroupType == WINNT_GROUP_LOCAL) {
  441. hr = WinNTDeleteLocalGroup(
  442. pObjectInfo->ComponentArray[1],
  443. pObjectInfo->ComponentArray[2]
  444. );
  445. } else if (dwGroupType == WINNT_GROUP_GLOBAL) {
  446. hr = WinNTDeleteGlobalGroup(
  447. pObjectInfo->ComponentArray[1],
  448. pObjectInfo->ComponentArray[2]
  449. );
  450. } else {
  451. //
  452. // private funct'n -> must be ADSI own coding bug
  453. //
  454. ADsAssert(FALSE);
  455. hr = E_FAIL;
  456. }
  457. BAIL_ON_FAILURE(hr);
  458. break;
  459. default:
  460. RRETURN(E_FAIL);
  461. }
  462. error:
  463. RRETURN(hr);
  464. }
  465. HRESULT
  466. WinNTDeleteUser(
  467. POBJECTINFO pObjectInfo,
  468. const CWinNTCredentials& Credentials
  469. )
  470. {
  471. WCHAR szHostServerName[MAX_PATH];
  472. HRESULT hr;
  473. NET_API_STATUS nasStatus;
  474. if (!pObjectInfo) {
  475. RRETURN(E_FAIL);
  476. }
  477. switch (pObjectInfo->NumComponents) {
  478. case 2:
  479. hr = WinNTGetCachedDCName(
  480. pObjectInfo->ComponentArray[0],
  481. szHostServerName,
  482. Credentials.GetFlags() // we need a writeable dc
  483. );
  484. BAIL_ON_FAILURE(hr);
  485. hr = WinNTDeleteUser(
  486. (szHostServerName +2),
  487. pObjectInfo->ComponentArray[1]
  488. );
  489. BAIL_ON_FAILURE(hr);
  490. break;
  491. case 3:
  492. hr = WinNTDeleteUser(
  493. pObjectInfo->ComponentArray[1],
  494. pObjectInfo->ComponentArray[2]
  495. );
  496. BAIL_ON_FAILURE(hr);
  497. break;
  498. default:
  499. RRETURN(E_FAIL);
  500. }
  501. error:
  502. RRETURN(hr);
  503. }
  504. HRESULT
  505. WinNTDeleteComputer(
  506. POBJECTINFO pObjectInfo,
  507. const CWinNTCredentials& Credentials
  508. )
  509. {
  510. WCHAR szHostServerName[MAX_PATH];
  511. HRESULT hr;
  512. NET_API_STATUS nasStatus;
  513. if (!pObjectInfo) {
  514. RRETURN(E_FAIL);
  515. }
  516. switch (pObjectInfo->NumComponents) {
  517. case 2:
  518. hr = WinNTGetCachedDCName(
  519. pObjectInfo->ComponentArray[0],
  520. szHostServerName,
  521. Credentials.GetFlags() // we need a writeable DC
  522. );
  523. BAIL_ON_FAILURE(hr);
  524. hr = WinNTDeleteComputer(
  525. (szHostServerName +2),
  526. pObjectInfo->ComponentArray[1]
  527. );
  528. BAIL_ON_FAILURE(hr);
  529. break;
  530. default:
  531. RRETURN(E_FAIL);
  532. }
  533. error:
  534. RRETURN(hr);
  535. }