Leaked source code of windows server 2003
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.

5468 lines
192 KiB

  1. //***************************************************************************
  2. // Copyright (c) Microsoft Corporation
  3. //
  4. // Module Name:
  5. // TAKEOWN.C
  6. //
  7. // Abstract:
  8. // Implements a recovery scheme to give an Administrator
  9. // access to a file that has been denied to all.
  10. //
  11. // Author:
  12. // Wipro Technologies
  13. //
  14. // Revision History:
  15. // Wipro Technologies 22-jun-01 : Created It.
  16. //***************************************************************************
  17. // Include files
  18. #include "pch.h"
  19. #include "takeown.h"
  20. #include "resource.h"
  21. #include "Lm.h"
  22. //#include <malloc.h>
  23. /*GLOBAL VARIABLES*/
  24. PStore_Path_Name g_pPathName = NULL ; // Holds path name from where started .
  25. PStore_Path_Name g_pFollowPathName = NULL ; // Holds information about a subdirectory .
  26. LPTSTR g_lpszFileToSearch = NULL; // Holds information about directories and subdirectories .
  27. DWORD
  28. _cdecl _tmain(
  29. IN DWORD argc,
  30. IN LPCWSTR argv[]
  31. )
  32. /*++
  33. Routine Description:
  34. Main function which calls all the other functions depending on the
  35. option specified by the user.
  36. Arguments:
  37. [ IN ] argc - Number of command line arguments.
  38. [ IN ] argv - Array containing command line arguments.
  39. Return Value:
  40. EXIT_FAILURE if takeown utility is not successful.
  41. EXIT_SUCCESS if takeown utility is successful.
  42. --*/
  43. {
  44. //local variables
  45. BOOL bUsage = FALSE;
  46. BOOL bFlag = FALSE;
  47. BOOL bNeedPassword = FALSE;
  48. BOOL bCloseConnection = FALSE;
  49. BOOL bLocalSystem = FALSE;
  50. BOOL bTakeOwnAllFiles = FALSE;
  51. BOOL bCurrDirTakeOwnAllFiles = FALSE;
  52. BOOL bDriveCurrDirTakeOwnAllFiles = FALSE;
  53. BOOL bRecursive = FALSE;
  54. BOOL bLogonDomainAdmin = FALSE;
  55. BOOL bAdminsOwner = FALSE;
  56. BOOL bFileInUNCFormat = FALSE;
  57. BOOL bNTFSFileSystem = FALSE;
  58. BOOL bMatchPattern = FALSE;
  59. LPWSTR szUserName = NULL;
  60. LPWSTR szPassword = NULL;
  61. LPWSTR szMachineName = NULL;
  62. LPWSTR wszPatternString = NULL;
  63. LPWSTR szTmpFileName = NULL;
  64. LPWSTR szDirTok = NULL;
  65. LPWSTR szFileName = NULL;
  66. LPWSTR szTemporaryFileName = NULL;
  67. LPWSTR szTempPath = NULL;
  68. LPTSTR szFilePart = NULL;
  69. LPWSTR szFullPath = NULL;
  70. LPWSTR szDispFileName = NULL;
  71. WCHAR dwUserName[2 * MAX_STRING_LENGTH] ;
  72. WCHAR szOwnerString[4 * MAX_STRING_LENGTH+5] ;
  73. WCHAR ch = L'\\';
  74. WCHAR szTempChar[20] ;
  75. WCHAR szConfirm [MAX_CONFIRM_VALUE] ;
  76. DWORD nSize1 = 4 * MAX_STRING_LENGTH + 5;
  77. DWORD dwi = 0;
  78. DWORD dwCount = 2;
  79. DWORD dwFileCount = 0;
  80. DWORD dwCnt = 0;
  81. HRESULT hr;
  82. SecureZeroMemory(dwUserName, (2 * MAX_STRING_LENGTH) * sizeof(WCHAR));
  83. SecureZeroMemory(szOwnerString, (4 * MAX_STRING_LENGTH+5) * sizeof(WCHAR));
  84. SecureZeroMemory(szTempChar, (20) * sizeof(WCHAR));
  85. SecureZeroMemory(szConfirm, (MAX_CONFIRM_VALUE) * sizeof(WCHAR));
  86. bFlag = ParseCmdLine( argc, argv, &szMachineName, &szUserName,
  87. &szPassword, &szFileName, &bUsage, &bNeedPassword, &bRecursive, &bAdminsOwner, szConfirm);
  88. //if syntax of command line arguments is false display the error
  89. //and exit
  90. if( FALSE == bFlag )
  91. {
  92. FREE_MEMORY(szMachineName);
  93. FREE_MEMORY(szUserName);
  94. FREE_MEMORY(szPassword);
  95. FREE_MEMORY(szFileName);
  96. ReleaseGlobals();
  97. return( EXIT_FAILURE );
  98. }
  99. //if usage is specified at command line, display usage
  100. if( TRUE == bUsage )
  101. {
  102. DisplayUsage();
  103. FREE_MEMORY(szMachineName);
  104. FREE_MEMORY(szUserName);
  105. FREE_MEMORY(szPassword);
  106. FREE_MEMORY(szFileName);
  107. ReleaseGlobals();
  108. return( EXIT_SUCCESS );
  109. }
  110. if(0 == GetUserNameEx(NameSamCompatible, szOwnerString,&nSize1))
  111. {
  112. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  113. FREE_MEMORY(szMachineName);
  114. FREE_MEMORY(szUserName);
  115. FREE_MEMORY(szPassword);
  116. FREE_MEMORY(szFileName);
  117. ReleaseGlobals();
  118. return EXIT_FAILURE;
  119. }
  120. /*Check whether the current logged on user is domain administrator or not*/
  121. if( EXIT_FAIL == IsLogonDomainAdmin(szOwnerString,&bLogonDomainAdmin) )
  122. {
  123. FREE_MEMORY(szMachineName);
  124. FREE_MEMORY(szUserName);
  125. FREE_MEMORY(szPassword);
  126. FREE_MEMORY(szFileName);
  127. ReleaseGlobals();
  128. return EXIT_FAILURE;
  129. }
  130. /*If /a option is specified, before giving ownership to the administrators group, check whether current logged on user has administrative privileges or NOT*/
  131. if(TRUE == bAdminsOwner)
  132. {
  133. if(FALSE == IsUserAdmin())
  134. {
  135. ShowMessage( stderr, GetResString(IDS_NOT_ADMINISTRATOR ));
  136. FREE_MEMORY(szMachineName);
  137. FREE_MEMORY(szUserName);
  138. FREE_MEMORY(szPassword);
  139. FREE_MEMORY(szFileName);
  140. ReleaseGlobals();
  141. return( EXIT_FAILURE );
  142. }
  143. }
  144. if( StringLengthW( szFileName, 0 ) > 3 )
  145. {
  146. if((szFileName[1] == L':') && ( SINGLE_QUOTE == szFileName[ 2 ] ) &&
  147. ( SINGLE_QUOTE == szFileName[ 3 ] ))
  148. {
  149. ShowMessage( stderr , ERROR_PATH_NAME ) ;
  150. FREE_MEMORY(szMachineName);
  151. FREE_MEMORY(szUserName);
  152. FREE_MEMORY(szPassword);
  153. FREE_MEMORY(szFileName);
  154. ReleaseGlobals();
  155. return EXIT_FAILURE;
  156. }
  157. }
  158. if(StringLengthW(szFileName, 0) != 0)
  159. {
  160. if(-1 != FindChar2((szFileName), L'?', TRUE, 0))
  161. {
  162. ShowMessage( stderr , ERROR_PATH_NAME ) ;
  163. FREE_MEMORY(szMachineName);
  164. FREE_MEMORY(szUserName);
  165. FREE_MEMORY(szPassword);
  166. FREE_MEMORY(szFileName);
  167. ReleaseGlobals();
  168. return EXIT_FAILURE;
  169. }
  170. }
  171. bLocalSystem = IsLocalSystem(IsUNCFormat(szMachineName) ? szMachineName+2:szMachineName);
  172. if(TRUE == IsUNCFormat(szFileName))
  173. {
  174. bFileInUNCFormat = TRUE;
  175. }
  176. szTemporaryFileName = (LPWSTR)AllocateMemory((StringLengthW(szFileName, 0) + MAX_STRING_LENGTH) * sizeof(WCHAR));
  177. if(NULL == szTemporaryFileName)
  178. {
  179. FREE_MEMORY(szMachineName);
  180. FREE_MEMORY(szUserName);
  181. FREE_MEMORY(szPassword);
  182. FREE_MEMORY(szFileName);
  183. ReleaseGlobals();
  184. return EXIT_FAILURE;
  185. }
  186. //if remote machine establish connection
  187. //if( FALSE == bLocalSystem && FALSE == bFileInUNCFormat)
  188. if( FALSE == bLocalSystem )
  189. {
  190. //if remote machine and wild card then display error and exit
  191. if( ( 1 == StringLengthW( szFileName, 0 ) ) && ( 0 == StringCompare( szFileName, WILDCARD, TRUE, 0 ) ) )
  192. {
  193. ShowMessage( stderr, ERROR_INVALID_WILDCARD );
  194. FREE_MEMORY(szMachineName);
  195. FREE_MEMORY(szUserName);
  196. FREE_MEMORY(szPassword);
  197. FREE_MEMORY(szFileName);
  198. FREE_MEMORY(szTemporaryFileName);
  199. ReleaseGlobals();
  200. return( EXIT_FAILURE );
  201. }
  202. //if remote machine establish connection
  203. bFlag = EstablishConnection( szMachineName, (LPTSTR)szUserName,
  204. GetBufferSize( szUserName ) / sizeof(WCHAR), (LPTSTR)szPassword,
  205. GetBufferSize( szPassword ) / sizeof(WCHAR), bNeedPassword );
  206. //failed to establish connection
  207. if ( FALSE == bFlag )
  208. {
  209. // failed in establishing n/w connection
  210. ShowMessage( stderr, ERROR_STRING );
  211. ShowMessage( stderr, SPACE_CHAR );
  212. ShowMessage( stderr, GetReason() );
  213. FREE_MEMORY(szMachineName);
  214. FREE_MEMORY(szUserName);
  215. FREE_MEMORY(szFileName);
  216. FREE_MEMORY(szPassword);
  217. FREE_MEMORY(szTemporaryFileName);
  218. ReleaseGlobals();
  219. return( EXIT_FAILURE );
  220. }
  221. switch( GetLastError() )
  222. {
  223. case I_NO_CLOSE_CONNECTION:
  224. bCloseConnection = FALSE;
  225. break;
  226. case E_LOCAL_CREDENTIALS:
  227. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  228. {
  229. //
  230. // some error occured ... but can be ignored
  231. // connection need not be disconnected
  232. bCloseConnection= FALSE;
  233. // show the warning message
  234. ShowLastErrorEx(stderr, SLE_TYPE_WARNING | SLE_SYSTEM);
  235. break;
  236. }
  237. default:
  238. bCloseConnection = TRUE;
  239. }
  240. FREE_MEMORY(szPassword);
  241. szTmpFileName = (LPWSTR)AllocateMemory((StringLengthW(szMachineName, 0) + StringLengthW(szFileName, 0) + MAX_STRING_LENGTH) * sizeof(WCHAR));
  242. if(NULL == szTmpFileName)
  243. {
  244. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  245. if( TRUE == bCloseConnection )
  246. {
  247. CloseConnection( szMachineName );
  248. }
  249. FREE_MEMORY(szMachineName);
  250. FREE_MEMORY(szUserName);
  251. FREE_MEMORY(szFileName);
  252. FREE_MEMORY(szTemporaryFileName);
  253. ReleaseGlobals();
  254. return( EXIT_FAILURE );
  255. }
  256. if(FALSE == IsUNCFormat(szMachineName))
  257. {
  258. StringCopyW( szTmpFileName, DOUBLE_QUOTE, GetBufferSize(szTmpFileName) / sizeof(WCHAR));
  259. StringConcat(szTmpFileName, szMachineName, GetBufferSize(szTmpFileName) / sizeof(WCHAR));
  260. }
  261. else
  262. {
  263. StringCopyW( szTmpFileName, szMachineName, GetBufferSize(szTmpFileName) / sizeof(WCHAR) );
  264. }
  265. StringConcat(szTmpFileName, L"\\", GetBufferSize(szTmpFileName) / sizeof(WCHAR));
  266. if(FALSE == IsUNCFormat(szFileName))
  267. {
  268. StringCopyW( szTemporaryFileName, szFileName, GetBufferSize(szTemporaryFileName) / sizeof(WCHAR) );
  269. if( szFileName[1] == COLON )
  270. {
  271. szFileName[1] = DOLLOR;
  272. }
  273. StringConcat(szTmpFileName, szFileName, GetBufferSize(szTmpFileName) / sizeof(WCHAR));
  274. if( szFileName[1] == DOLLOR )
  275. {
  276. szFileName[1] = COLON;
  277. }
  278. }
  279. else
  280. {
  281. szTempPath = wcsrchr(szFileName,COLON); //go from the reverse direction to check for ":" . Let \\server\c:\temp
  282. if(NULL != szTempPath)
  283. {
  284. szTempPath--; //Go back reverse by one step..If :\temp is obtained , by this step , we get c:\temp
  285. if(NULL != szTempPath)
  286. {
  287. //ShowMessage( stderr, GetResString(IDS_IGNORE_CREDENTIALS) );
  288. StringCopyW( szTemporaryFileName, szTempPath, GetBufferSize(szTemporaryFileName) / sizeof(WCHAR) );
  289. if( szTemporaryFileName[1] == COLON ) // change the " : " to " $ " , so , c$\temp
  290. {
  291. szTemporaryFileName[1] = DOLLOR;
  292. }
  293. szDirTok = wcstok(szFileName,L":"); // get the taken value , like \\server\c
  294. StringCopyW( szTmpFileName, szDirTok, GetBufferSize(szTmpFileName) / sizeof(WCHAR) );
  295. StringConcat(szTmpFileName, szTemporaryFileName+1, GetBufferSize(szTmpFileName) / sizeof(WCHAR));
  296. //attach the value, say "$\temp" to \\server\c , so that the value becomes "\\server\c$\temp"
  297. if( szTemporaryFileName[1] == DOLLOR )//convert back the original string from dollar to colon
  298. {
  299. szTemporaryFileName[1] = COLON;
  300. }
  301. }
  302. }
  303. else
  304. {
  305. //ShowMessage( stderr, GetResString(IDS_IGNORE_CREDENTIALS) );
  306. StringCopyW( szTmpFileName, szFileName, GetBufferSize(szTmpFileName) / sizeof(WCHAR) );
  307. }
  308. }
  309. }
  310. else
  311. {
  312. FREE_MEMORY(szPassword);
  313. szTmpFileName = (LPWSTR)AllocateMemory((StringLengthW(szFileName, 0) + MAX_STRING_LENGTH) * sizeof(WCHAR));
  314. if(NULL == szTmpFileName)
  315. {
  316. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  317. FREE_MEMORY(szMachineName);
  318. FREE_MEMORY(szUserName);
  319. FREE_MEMORY(szFileName);
  320. FREE_MEMORY(szTemporaryFileName);
  321. ReleaseGlobals();
  322. return( EXIT_FAILURE );
  323. }
  324. if( StringLengthW( szFileName, 0 ) == 2 && szFileName[0] == L'\\' && (szFileName[1] == L'\\' || szFileName[1] == L':') )
  325. {
  326. ShowMessage( stderr , ERROR_PATH_NAME ) ;
  327. ReleaseGlobals();
  328. FREE_MEMORY(szMachineName);
  329. FREE_MEMORY(szUserName);
  330. FREE_MEMORY(szFileName);
  331. FREE_MEMORY(szTmpFileName);
  332. FREE_MEMORY(szTemporaryFileName);
  333. return EXIT_FAILURE;
  334. }
  335. if( (StringLengthW( szFileName, 0 )) > 2 )
  336. {
  337. if(( SINGLE_QUOTE == szFileName[ 0 ] ) &&
  338. ( SINGLE_QUOTE == szFileName[ 1 ] ))
  339. {
  340. szTempChar[0] = szFileName[2];
  341. szTempChar[1] = L'\0';
  342. if(wcspbrk(szTempChar, CHAR_SET2) != NULL)
  343. {
  344. DISPLAY_MESSAGE( stderr , ERROR_PATH_NAME ) ;
  345. ReleaseGlobals();
  346. FREE_MEMORY(szMachineName);
  347. FREE_MEMORY(szUserName);
  348. FREE_MEMORY(szFileName);
  349. FREE_MEMORY(szTmpFileName);
  350. FREE_MEMORY(szTemporaryFileName);
  351. return EXIT_FAILURE;
  352. }
  353. if( NULL != FindString( ( szFileName + 2 ), DOUBLE_QUOTE, 0 ) )
  354. {
  355. DISPLAY_MESSAGE( stderr , ERROR_PATH_NAME ) ;
  356. FREE_MEMORY(szMachineName);
  357. FREE_MEMORY(szUserName);
  358. FREE_MEMORY(szFileName);
  359. FREE_MEMORY(szTmpFileName);
  360. FREE_MEMORY(szTemporaryFileName);
  361. ReleaseGlobals();
  362. return EXIT_FAILURE;
  363. }
  364. }
  365. }
  366. if( (StringLengthW( szFileName, 0 ) == 1) || (StringLengthW( szFileName, 0 ) == 2))
  367. {
  368. if(szFileName[0] == L':')
  369. {
  370. DISPLAY_MESSAGE( stderr , ERROR_PATH_NAME ) ;
  371. FREE_MEMORY(szMachineName);
  372. FREE_MEMORY(szUserName);
  373. FREE_MEMORY(szFileName);
  374. FREE_MEMORY(szTmpFileName);
  375. FREE_MEMORY(szTemporaryFileName);
  376. ReleaseGlobals();
  377. return EXIT_FAILURE;
  378. }
  379. }
  380. if(TRUE == bFileInUNCFormat)
  381. {
  382. szTempPath = wcsrchr(szFileName,COLON);
  383. if(NULL != szTempPath)
  384. {
  385. szTempPath--;
  386. if(NULL != szTempPath)
  387. {
  388. StringCopyW( szTemporaryFileName, szTempPath, GetBufferSize(szTemporaryFileName) / sizeof(WCHAR) );
  389. if( szTemporaryFileName[1] == COLON )
  390. {
  391. szTemporaryFileName[1] = DOLLOR;
  392. }
  393. szDirTok = wcstok(szFileName, L":");
  394. StringCopyW( szTmpFileName, szDirTok, GetBufferSize(szTmpFileName) / sizeof(WCHAR) );
  395. StringConcat(szTmpFileName, szTemporaryFileName+1, GetBufferSize(szTmpFileName) / sizeof(WCHAR));
  396. if( szTemporaryFileName[1] == DOLLOR )
  397. {
  398. szTemporaryFileName[1] = COLON;
  399. }
  400. }
  401. }
  402. else
  403. {
  404. StringCopyW( szTmpFileName, szFileName, GetBufferSize(szTmpFileName) / sizeof(WCHAR) );
  405. }
  406. }
  407. else
  408. {
  409. StringCopyW( szTmpFileName, szFileName, GetBufferSize(szTmpFileName) / sizeof(WCHAR) );
  410. }
  411. if((StringLengthW( szTmpFileName, 0 )) > 2)
  412. {
  413. if(wcspbrk(szTmpFileName+2,CHAR_SET3) != NULL)
  414. {
  415. DISPLAY_MESSAGE( stderr , ERROR_PATH_NAME ) ;
  416. FREE_MEMORY(szMachineName);
  417. FREE_MEMORY(szUserName);
  418. FREE_MEMORY(szFileName);
  419. FREE_MEMORY(szTmpFileName);
  420. FREE_MEMORY(szTemporaryFileName);
  421. ReleaseGlobals();
  422. return EXIT_FAILURE;
  423. }
  424. }
  425. }//end of else loop for (FALSE == BLocalSystem)
  426. /*Check whether * is given in order to give ownership to all the files in the directory specicied or current directory*/
  427. if((StringLengthW( szTmpFileName, 0 )) >= 2)
  428. {
  429. szTempPath = wcsrchr(szTmpFileName, ch);
  430. if(szTempPath != NULL && (*(szTempPath + 1) != L'\0'))
  431. {
  432. if(*(szTempPath+1) == L'*')
  433. {
  434. for(dwCount;;dwCount++)
  435. {
  436. if(*(szTempPath + dwCount) != L'\0')
  437. {
  438. if(*(szTempPath + dwCount) != L'*')
  439. {
  440. bMatchPattern = TRUE;
  441. wszPatternString = (LPWSTR)AllocateMemory((StringLengthW(szTempPath + 1, 0) + 10) * sizeof(WCHAR));
  442. if(NULL == wszPatternString)
  443. {
  444. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  445. if( TRUE == bCloseConnection )
  446. {
  447. CloseConnection( szMachineName );
  448. }
  449. FREE_MEMORY(szMachineName);
  450. FREE_MEMORY(szUserName);
  451. FREE_MEMORY(szFileName);
  452. FREE_MEMORY(szTemporaryFileName);
  453. ReleaseGlobals();
  454. return EXIT_FAILURE;
  455. }
  456. StringCopyW(wszPatternString, szTempPath + 1, GetBufferSize(wszPatternString) / sizeof(WCHAR));
  457. if(EXIT_FAILURE == RemoveStarFromPattern(wszPatternString))
  458. {
  459. if( TRUE == bCloseConnection )
  460. {
  461. CloseConnection( szMachineName );
  462. }
  463. FREE_MEMORY(szMachineName);
  464. FREE_MEMORY(szUserName);
  465. FREE_MEMORY(szFileName);
  466. FREE_MEMORY(szTemporaryFileName);
  467. FREE_MEMORY(wszPatternString);
  468. ReleaseGlobals();
  469. return EXIT_FAILURE;
  470. }
  471. break;
  472. }
  473. }
  474. else
  475. {
  476. break;
  477. }
  478. }
  479. szTempPath++; ////
  480. *(szTempPath) = '\0';
  481. bTakeOwnAllFiles = TRUE;
  482. }
  483. else
  484. {
  485. if(-1 != FindChar2((LPCWSTR)(szTempPath + 1), L'*', TRUE, 0) &&
  486. (-1 == FindChar2((szTempPath + 1), L'?', TRUE, 0)))
  487. {
  488. bMatchPattern = TRUE;
  489. wszPatternString = (LPWSTR)AllocateMemory((StringLengthW(szTempPath + 1, 0) + 10) * sizeof(WCHAR));
  490. if(NULL == wszPatternString)
  491. {
  492. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  493. if( TRUE == bCloseConnection )
  494. {
  495. CloseConnection( szMachineName );
  496. }
  497. FREE_MEMORY(szMachineName);
  498. FREE_MEMORY(szUserName);
  499. FREE_MEMORY(szFileName);
  500. FREE_MEMORY(szTemporaryFileName);
  501. ReleaseGlobals();
  502. return EXIT_FAILURE;
  503. }
  504. StringCopyW(wszPatternString, szTempPath + 1, GetBufferSize(wszPatternString) / sizeof(WCHAR));
  505. if(EXIT_FAILURE == RemoveStarFromPattern(wszPatternString))
  506. {
  507. if( TRUE == bCloseConnection )
  508. {
  509. CloseConnection( szMachineName );
  510. }
  511. FREE_MEMORY(szMachineName);
  512. FREE_MEMORY(szUserName);
  513. FREE_MEMORY(szFileName);
  514. FREE_MEMORY(szTemporaryFileName);
  515. FREE_MEMORY(wszPatternString);
  516. ReleaseGlobals();
  517. return EXIT_FAILURE;
  518. }
  519. szTempPath++;
  520. *(szTempPath) = '\0';
  521. bTakeOwnAllFiles = TRUE;
  522. }
  523. }
  524. }
  525. else
  526. {
  527. dwCount = 0;
  528. for(dwCount;;dwCount++)
  529. {
  530. if(*(szTmpFileName + dwCount) != L'\0')
  531. {
  532. if(*(szTmpFileName + dwCount) != L'*')
  533. {
  534. break;
  535. }
  536. }
  537. else
  538. {
  539. bCurrDirTakeOwnAllFiles = TRUE;
  540. break;
  541. }
  542. }
  543. if(bCurrDirTakeOwnAllFiles == FALSE)
  544. {
  545. dwCount = 2;
  546. szTempPath = wcsrchr(szTmpFileName, COLON);
  547. if(szTempPath != NULL)
  548. {
  549. if((*(szTempPath + 1) != L'\0') && (*(szTempPath + 1) == L'*'))
  550. {
  551. for(dwCount;;dwCount++)
  552. {
  553. if(*(szTempPath + dwCount) != L'\0')
  554. {
  555. if(*(szTempPath + dwCount) != L'*')
  556. {
  557. bMatchPattern = TRUE;
  558. wszPatternString = (LPWSTR)AllocateMemory((StringLengthW(szTempPath + 1, 0) + 10) * sizeof(WCHAR));
  559. if(NULL == wszPatternString)
  560. {
  561. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  562. if( TRUE == bCloseConnection )
  563. {
  564. CloseConnection( szMachineName );
  565. }
  566. FREE_MEMORY(szMachineName);
  567. FREE_MEMORY(szUserName);
  568. FREE_MEMORY(szFileName);
  569. FREE_MEMORY(szTemporaryFileName);
  570. ReleaseGlobals();
  571. return EXIT_FAILURE;
  572. }
  573. StringCopyW(wszPatternString, szTempPath + 1, GetBufferSize(wszPatternString) / sizeof(WCHAR));
  574. if(EXIT_FAILURE == RemoveStarFromPattern(wszPatternString))
  575. {
  576. if( TRUE == bCloseConnection )
  577. {
  578. CloseConnection( szMachineName );
  579. }
  580. FREE_MEMORY(szMachineName);
  581. FREE_MEMORY(szUserName);
  582. FREE_MEMORY(szFileName);
  583. FREE_MEMORY(szTemporaryFileName);
  584. FREE_MEMORY(wszPatternString);
  585. ReleaseGlobals();
  586. return EXIT_FAILURE;
  587. }
  588. bDriveCurrDirTakeOwnAllFiles = TRUE;
  589. break;
  590. }
  591. }
  592. else
  593. {
  594. break;
  595. }
  596. }
  597. szTempPath++; ////
  598. *(szTempPath) = L'\0';
  599. bDriveCurrDirTakeOwnAllFiles = TRUE;
  600. }
  601. else
  602. {
  603. if((-1 != FindChar2((szTempPath + 1), L'*', TRUE, 0)) &&
  604. (-1 == FindChar2((szTempPath + 1), L'?', TRUE, 0)))
  605. {
  606. bMatchPattern = TRUE;
  607. wszPatternString = (LPWSTR)AllocateMemory((StringLengthW(szTempPath + 1, 0) + 10) * sizeof(WCHAR));
  608. if(NULL == wszPatternString)
  609. {
  610. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  611. if( TRUE == bCloseConnection )
  612. {
  613. CloseConnection( szMachineName );
  614. }
  615. FREE_MEMORY(szMachineName);
  616. FREE_MEMORY(szUserName);
  617. FREE_MEMORY(szFileName);
  618. FREE_MEMORY(szTemporaryFileName);
  619. ReleaseGlobals();
  620. return EXIT_FAILURE;
  621. }
  622. StringCopyW(wszPatternString, szTempPath + 1, GetBufferSize(wszPatternString) / sizeof(WCHAR));
  623. if(EXIT_FAILURE == RemoveStarFromPattern(wszPatternString))
  624. {
  625. if( TRUE == bCloseConnection )
  626. {
  627. CloseConnection( szMachineName );
  628. }
  629. FREE_MEMORY(szMachineName);
  630. FREE_MEMORY(szUserName);
  631. FREE_MEMORY(szFileName);
  632. FREE_MEMORY(szTemporaryFileName);
  633. FREE_MEMORY(wszPatternString);
  634. ReleaseGlobals();
  635. return EXIT_FAILURE;
  636. }
  637. szTempPath++;
  638. *(szTempPath) = L'\0';
  639. bDriveCurrDirTakeOwnAllFiles = TRUE;
  640. }
  641. }
  642. }
  643. else
  644. {
  645. if(-1 != FindChar2((szTmpFileName), L'*', TRUE, 0) &&
  646. (-1 == FindChar2((szTmpFileName + 1), L'?', TRUE, 0)))
  647. {
  648. bMatchPattern = TRUE;
  649. wszPatternString = (LPWSTR)AllocateMemory((StringLengthW(szTmpFileName + 1, 0) + 10) * sizeof(WCHAR));
  650. if(NULL == wszPatternString)
  651. {
  652. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  653. if( TRUE == bCloseConnection )
  654. {
  655. CloseConnection( szMachineName );
  656. }
  657. FREE_MEMORY(szMachineName);
  658. FREE_MEMORY(szUserName);
  659. FREE_MEMORY(szFileName);
  660. FREE_MEMORY(szTemporaryFileName);
  661. ReleaseGlobals();
  662. return EXIT_FAILURE;
  663. }
  664. StringCopyW(wszPatternString, szTmpFileName, GetBufferSize(wszPatternString) / sizeof(WCHAR));
  665. if(EXIT_FAILURE == RemoveStarFromPattern(wszPatternString))
  666. {
  667. if( TRUE == bCloseConnection )
  668. {
  669. CloseConnection( szMachineName );
  670. }
  671. FREE_MEMORY(szMachineName);
  672. FREE_MEMORY(szUserName);
  673. FREE_MEMORY(szFileName);
  674. FREE_MEMORY(szTemporaryFileName);
  675. FREE_MEMORY(wszPatternString);
  676. ReleaseGlobals();
  677. return EXIT_FAILURE;
  678. }
  679. bCurrDirTakeOwnAllFiles = TRUE;
  680. }
  681. }
  682. }
  683. }
  684. }
  685. if (1 == StringLengthW( szTmpFileName, 0 ) && 0 == StringCompare( szTmpFileName, WILDCARD, TRUE, 0 ))
  686. {
  687. bCurrDirTakeOwnAllFiles = TRUE;
  688. }
  689. if((TRUE == bLocalSystem) && (FALSE == bCurrDirTakeOwnAllFiles))
  690. {
  691. if(wcspbrk(szTmpFileName,CHAR_SET) != NULL)
  692. {
  693. DISPLAY_MESSAGE( stderr, ERROR_PATH_NAME ) ;
  694. if( TRUE == bCloseConnection )
  695. {
  696. CloseConnection( szMachineName );
  697. }
  698. FREE_MEMORY(szMachineName);
  699. FREE_MEMORY(szUserName);
  700. FREE_MEMORY(szFileName);
  701. FREE_MEMORY(szTemporaryFileName);
  702. FREE_MEMORY(szTmpFileName);
  703. FREE_MEMORY(wszPatternString);
  704. ReleaseGlobals();
  705. return EXIT_FAILURE;
  706. }
  707. }
  708. /*Get the full path in order to get the ownership for the specified file*/
  709. if((FALSE == bCurrDirTakeOwnAllFiles) || (bDriveCurrDirTakeOwnAllFiles == TRUE ))
  710. {
  711. dwi = GetFullPathName( szTmpFileName, 0, szFullPath, &szFilePart );
  712. if( 0 == dwi )
  713. {
  714. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  715. if( TRUE == bCloseConnection )
  716. {
  717. CloseConnection( szMachineName );
  718. }
  719. FREE_MEMORY(szMachineName);
  720. FREE_MEMORY(szUserName);
  721. FREE_MEMORY(szFileName);
  722. FREE_MEMORY(szTemporaryFileName);
  723. FREE_MEMORY(szTmpFileName);
  724. FREE_MEMORY(wszPatternString);
  725. ReleaseGlobals();
  726. return EXIT_FAILURE;
  727. }
  728. szFullPath = (LPWSTR)AllocateMemory((dwi+10) * sizeof(WCHAR));// an additional ten bytes are added for safe side in order to avoid unexpected results
  729. if(NULL == szFullPath)
  730. {
  731. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  732. if( TRUE == bCloseConnection )
  733. {
  734. CloseConnection( szMachineName );
  735. }
  736. FREE_MEMORY(szMachineName);
  737. FREE_MEMORY(szUserName);
  738. FREE_MEMORY(szFileName);
  739. FREE_MEMORY(szTemporaryFileName);
  740. FREE_MEMORY(szTmpFileName);
  741. FREE_MEMORY(wszPatternString);
  742. ReleaseGlobals();
  743. return EXIT_FAILURE;
  744. }
  745. if( 0 == GetFullPathName( szTmpFileName,dwi+10, szFullPath, &szFilePart ))
  746. {
  747. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  748. if( TRUE == bCloseConnection )
  749. {
  750. CloseConnection( szMachineName );
  751. }
  752. FREE_MEMORY(szMachineName);
  753. FREE_MEMORY(szUserName);
  754. FREE_MEMORY(szFileName);
  755. FREE_MEMORY(szTemporaryFileName);
  756. FREE_MEMORY(szTmpFileName);
  757. FREE_MEMORY(szFullPath);
  758. FREE_MEMORY(wszPatternString);
  759. ReleaseGlobals();
  760. return EXIT_FAILURE;
  761. }
  762. szDispFileName = (LPWSTR)AllocateMemory((dwi + MAX_STRING_LENGTH) * sizeof(WCHAR));
  763. if(NULL == szDispFileName)
  764. {
  765. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  766. if( TRUE == bCloseConnection )
  767. {
  768. CloseConnection( szMachineName );
  769. }
  770. FREE_MEMORY(szMachineName);
  771. FREE_MEMORY(szUserName);
  772. FREE_MEMORY(szFileName);
  773. FREE_MEMORY(szTemporaryFileName);
  774. FREE_MEMORY(szTmpFileName);
  775. FREE_MEMORY(szFullPath);
  776. FREE_MEMORY(wszPatternString);
  777. ReleaseGlobals();
  778. return EXIT_FAILURE;
  779. }
  780. StringCopyW( szDispFileName, szFullPath, (GetBufferSize(szDispFileName) / sizeof(WCHAR)) );
  781. }
  782. /*Check whether the system is NTFS or not*/
  783. dwCnt = 0;
  784. //dwCnt = IsNTFSFileSystem(szDispFileName, bLocalSystem, bFileInUNCFormat, bCurrDirTakeOwnAllFiles, szUserName, &bNTFSFileSystem);
  785. dwCnt = IsNTFSFileSystem(szDispFileName, bLocalSystem, bCurrDirTakeOwnAllFiles, szUserName, &bNTFSFileSystem);
  786. if(EXIT_FAILURE == dwCnt )
  787. {
  788. if( TRUE == bCloseConnection )
  789. {
  790. CloseConnection( szMachineName );
  791. }
  792. FREE_MEMORY(szMachineName);
  793. FREE_MEMORY(szUserName);
  794. FREE_MEMORY(szFileName);
  795. FREE_MEMORY(szTemporaryFileName);
  796. FREE_MEMORY(szTmpFileName);
  797. FREE_MEMORY(szFullPath);
  798. FREE_MEMORY(szDispFileName);
  799. FREE_MEMORY(wszPatternString);
  800. ReleaseGlobals();
  801. return EXIT_FAILURE;
  802. }
  803. if(FALSE == bNTFSFileSystem)
  804. {
  805. ShowMessage(stderr, GetResString(IDS_FAT_VOLUME));
  806. if( TRUE == bCloseConnection )
  807. {
  808. CloseConnection( szMachineName );
  809. }
  810. FREE_MEMORY(szMachineName);
  811. FREE_MEMORY(szUserName);
  812. FREE_MEMORY(szFileName);
  813. FREE_MEMORY(szTemporaryFileName);
  814. FREE_MEMORY(szTmpFileName);
  815. FREE_MEMORY(szFullPath);
  816. FREE_MEMORY(szDispFileName);
  817. FREE_MEMORY(wszPatternString);
  818. ReleaseGlobals();
  819. return EXIT_FAILURE;
  820. }
  821. if(TRUE == bRecursive)
  822. {
  823. if( EXIT_FAILURE == TakeOwnerShipRecursive(szDispFileName, bCurrDirTakeOwnAllFiles, bAdminsOwner, szOwnerString,
  824. bTakeOwnAllFiles, bDriveCurrDirTakeOwnAllFiles,
  825. bMatchPattern, wszPatternString, szConfirm))
  826. {
  827. if( TRUE == bCloseConnection )
  828. {
  829. CloseConnection( szMachineName );
  830. }
  831. FREE_MEMORY(szFullPath);
  832. FREE_MEMORY(szDispFileName);
  833. FREE_MEMORY(szMachineName);
  834. FREE_MEMORY(szUserName);
  835. FREE_MEMORY(szFileName);
  836. FREE_MEMORY(szTemporaryFileName);
  837. FREE_MEMORY(szTmpFileName);
  838. FREE_MEMORY(wszPatternString);
  839. ReleaseGlobals();
  840. return EXIT_FAILURE;
  841. }
  842. }
  843. else
  844. {
  845. //if filename is wildcard then give owner ship to all the files in
  846. //the current directory
  847. if( (TRUE == bCurrDirTakeOwnAllFiles) || (TRUE == bTakeOwnAllFiles ) ||(bDriveCurrDirTakeOwnAllFiles == TRUE))
  848. {
  849. /*Get the ownership for all the files in the specified directory*/
  850. bFlag = TakeOwnerShipAll(szDispFileName,bCurrDirTakeOwnAllFiles,&dwFileCount,bDriveCurrDirTakeOwnAllFiles,bAdminsOwner, szOwnerString, bMatchPattern, wszPatternString);
  851. if( FALSE == bFlag )
  852. {
  853. switch ( GetLastError() )
  854. {
  855. case ERROR_ACCESS_DENIED :
  856. ShowMessage(stderr,GetResString(IDS_ACCESS_DENIED_ERROR));
  857. /*ShowMessage( stderr, L"( \"" );
  858. ShowMessage( stderr, _X(szDispFileName) );
  859. ShowMessage( stderr, L"\" )\n" ); */
  860. break;
  861. case ERROR_BAD_NET_NAME :
  862. case ERROR_BAD_NETPATH :
  863. case ERROR_INVALID_NAME :
  864. SetLastError( ERROR_FILE_NOT_FOUND );
  865. SaveLastError();
  866. default :
  867. if(FALSE == bMatchPattern)
  868. {
  869. ShowMessage( stderr, ERROR_STRING );
  870. ShowMessage( stderr, SPACE_CHAR );
  871. ShowMessage( stderr, GetReason() );
  872. }
  873. else
  874. {
  875. ShowMessage( stdout, GetResString(IDS_NO_PATTERN_FOUND));
  876. }
  877. }
  878. if( TRUE == bCloseConnection )
  879. {
  880. CloseConnection( szMachineName );
  881. }
  882. FREE_MEMORY(szFullPath);
  883. FREE_MEMORY(szDispFileName);
  884. FREE_MEMORY(szMachineName);
  885. FREE_MEMORY(szUserName);
  886. FREE_MEMORY(szFileName);
  887. FREE_MEMORY(szTemporaryFileName);
  888. FREE_MEMORY(szTmpFileName);
  889. FREE_MEMORY(wszPatternString);
  890. ReleaseGlobals();
  891. return( EXIT_FAILURE );
  892. }
  893. }
  894. else // give ownership to the specified file
  895. {
  896. /*take the owner ship of the file specified for the administrators group or the current logged on user*/
  897. if(TRUE == bAdminsOwner)
  898. {
  899. bFlag = TakeOwnerShip( szDispFileName);
  900. }
  901. else
  902. {
  903. bFlag = TakeOwnerShipIndividual(szDispFileName);
  904. }
  905. if( FALSE == bFlag )
  906. {
  907. if( ERROR_NOT_ALL_ASSIGNED == GetLastError() )
  908. {
  909. hr = StringCchPrintf(szDispFileName, (GetBufferSize(szDispFileName) / sizeof(WCHAR)), GetResString(IDS_NOT_OWNERSHIP_ERROR) , szFullPath);
  910. if(FAILED(hr))
  911. {
  912. SetLastError(HRESULT_CODE(hr));
  913. SaveLastError();
  914. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  915. if( TRUE == bCloseConnection )
  916. {
  917. CloseConnection( szMachineName );
  918. }
  919. FREE_MEMORY(szMachineName);
  920. FREE_MEMORY(szUserName);
  921. FREE_MEMORY(szFileName);
  922. FREE_MEMORY(szTemporaryFileName);
  923. FREE_MEMORY(szTmpFileName);
  924. FREE_MEMORY(szFullPath);
  925. FREE_MEMORY(szDispFileName);
  926. FREE_MEMORY(wszPatternString);
  927. ReleaseGlobals();
  928. return( EXIT_FAILURE );
  929. }
  930. ShowMessage(stderr, szDispFileName);
  931. }
  932. else if(ERROR_SHARING_VIOLATION == GetLastError())
  933. {
  934. hr = StringCchPrintf(szDispFileName, (GetBufferSize(szDispFileName) / sizeof(WCHAR)), GetResString(IDS_SHARING_VIOLATION_ERROR) , szFullPath);
  935. if(FAILED(hr))
  936. {
  937. SetLastError(HRESULT_CODE(hr));
  938. SaveLastError();
  939. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  940. if( TRUE == bCloseConnection )
  941. {
  942. CloseConnection( szMachineName );
  943. }
  944. FREE_MEMORY(szMachineName);
  945. FREE_MEMORY(szUserName);
  946. FREE_MEMORY(szFileName);
  947. FREE_MEMORY(szTemporaryFileName);
  948. FREE_MEMORY(szTmpFileName);
  949. FREE_MEMORY(szFullPath);
  950. FREE_MEMORY(szDispFileName);
  951. FREE_MEMORY(wszPatternString);
  952. ReleaseGlobals();
  953. return( EXIT_FAILURE );
  954. }
  955. ShowMessage(stderr, szDispFileName);
  956. }
  957. else
  958. {
  959. if( ( ERROR_BAD_NET_NAME == GetLastError() ) ||
  960. ( ERROR_BAD_NETPATH == GetLastError() ) ||
  961. ( ERROR_INVALID_NAME == GetLastError() ) )
  962. {
  963. SetLastError( ERROR_FILE_NOT_FOUND );
  964. SaveLastError();
  965. }
  966. ShowMessage( stderr, ERROR_STRING );
  967. ShowMessage( stderr, SPACE_CHAR );
  968. ShowMessage( stderr, GetReason() );
  969. }
  970. if( TRUE == bCloseConnection )
  971. {
  972. CloseConnection( szMachineName );
  973. }
  974. FREE_MEMORY(szMachineName);
  975. FREE_MEMORY(szUserName);
  976. FREE_MEMORY(szFileName);
  977. FREE_MEMORY(szTemporaryFileName);
  978. FREE_MEMORY(szTmpFileName);
  979. FREE_MEMORY(szFullPath);
  980. FREE_MEMORY(szDispFileName);
  981. FREE_MEMORY(wszPatternString);
  982. ReleaseGlobals();
  983. //if connection is established to a remote machine close it
  984. return( EXIT_FAILURE );
  985. }
  986. else
  987. {
  988. if(TRUE == bAdminsOwner)
  989. {
  990. hr = StringCchPrintf(szDispFileName, (GetBufferSize(szDispFileName) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL, szFullPath);
  991. if(FAILED(hr))
  992. {
  993. SetLastError(HRESULT_CODE(hr));
  994. SaveLastError();
  995. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  996. if( TRUE == bCloseConnection )
  997. {
  998. CloseConnection( szMachineName );
  999. }
  1000. FREE_MEMORY(szMachineName);
  1001. FREE_MEMORY(szUserName);
  1002. FREE_MEMORY(szFileName);
  1003. FREE_MEMORY(szTemporaryFileName);
  1004. FREE_MEMORY(szTmpFileName);
  1005. FREE_MEMORY(szFullPath);
  1006. FREE_MEMORY(szDispFileName);
  1007. FREE_MEMORY(wszPatternString);
  1008. ReleaseGlobals();
  1009. return( EXIT_FAILURE );
  1010. }
  1011. }
  1012. else
  1013. {
  1014. //hr = StringCchPrintf(szDispFileName, (GetBufferSize(szDispFileName) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL_USER, szOwnerString, szFullPath);
  1015. hr = StringCchPrintf(szDispFileName, (GetBufferSize(szDispFileName) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL_USER, szFullPath, szOwnerString);
  1016. if(FAILED(hr))
  1017. {
  1018. SetLastError(HRESULT_CODE(hr));
  1019. SaveLastError();
  1020. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  1021. if( TRUE == bCloseConnection )
  1022. {
  1023. CloseConnection( szMachineName );
  1024. }
  1025. FREE_MEMORY(szMachineName);
  1026. FREE_MEMORY(szUserName);
  1027. FREE_MEMORY(szFileName);
  1028. FREE_MEMORY(szTemporaryFileName);
  1029. FREE_MEMORY(szTmpFileName);
  1030. FREE_MEMORY(szFullPath);
  1031. FREE_MEMORY(szDispFileName);
  1032. FREE_MEMORY(wszPatternString);
  1033. ReleaseGlobals();
  1034. return( EXIT_FAILURE );
  1035. }
  1036. }
  1037. ShowMessage( stdout, _X(szDispFileName) );
  1038. if( TRUE == bCloseConnection )
  1039. {
  1040. CloseConnection( szMachineName );
  1041. }
  1042. FREE_MEMORY(szMachineName);
  1043. FREE_MEMORY(szUserName);
  1044. FREE_MEMORY(szFileName);
  1045. FREE_MEMORY(szTemporaryFileName);
  1046. FREE_MEMORY(szTmpFileName);
  1047. FREE_MEMORY(szFullPath);
  1048. FREE_MEMORY(szDispFileName);
  1049. FREE_MEMORY(wszPatternString);
  1050. ReleaseGlobals();
  1051. return( EXIT_SUCCESS );
  1052. }
  1053. }
  1054. }
  1055. //if connection is established to a remote machine close it
  1056. if( TRUE == bCloseConnection )
  1057. {
  1058. CloseConnection( szMachineName );
  1059. }
  1060. FREE_MEMORY(szMachineName);
  1061. FREE_MEMORY(szUserName);
  1062. FREE_MEMORY(szFileName);
  1063. FREE_MEMORY(szTemporaryFileName);
  1064. FREE_MEMORY(szTmpFileName);
  1065. FREE_MEMORY(szFullPath);
  1066. FREE_MEMORY(szDispFileName);
  1067. FREE_MEMORY(wszPatternString);
  1068. ReleaseGlobals();
  1069. return( EXIT_SUCCESS );
  1070. }
  1071. BOOL
  1072. ParseCmdLine(
  1073. IN DWORD argc,
  1074. IN LPCWSTR argv[],
  1075. OUT LPWSTR* szMachineName,
  1076. OUT LPWSTR* szUserName,
  1077. OUT LPWSTR* szPassword,
  1078. OUT LPWSTR* szFileName,
  1079. OUT BOOL *pbUsage,
  1080. OUT BOOL *pbNeedPassword,
  1081. OUT BOOL *pbRecursive,
  1082. OUT BOOL *pbAdminsOwner,
  1083. OUT LPWSTR szConfirm
  1084. )
  1085. /*++
  1086. Routine Description:
  1087. This function parses the command line arguments which are obtained as input
  1088. parameters and gets the values into the corresponding variables which are
  1089. pass by reference parameters to this function.
  1090. Arguments:
  1091. [ IN ] argc - Number of command line arguments.
  1092. [ IN ] argv - Array containing command line arguments.
  1093. [ OUT ] szMachineName - To hold machine name.
  1094. [ OUT ] szUserName - To hold the User Name.
  1095. [ OUT ] szPassword - To hold the Password.
  1096. [ OUT ] szFileName - The filename whose attributes will be set.
  1097. [ OUT ] pbUsage - usage is mentioned at command line.
  1098. [ OUT ] pbNeedPassword - To know whether the password is required or not.
  1099. [ OUT ] pbRecursive - To know whether it is recursive or not.
  1100. [ OUT ] pbAdminsOwner - to know whether ownership to be given for administrators group
  1101. Return Value:
  1102. TRUE if command parser succeeds.
  1103. FALSE if command parser fails .
  1104. --*/
  1105. {
  1106. //local varibles
  1107. BOOL bFlag = FALSE;
  1108. TCMDPARSER2 tcmdOptions[MAX_OPTIONS];
  1109. //command line options
  1110. const WCHAR* wszCmdOptionUsage = L"?" ; //CMDOPTION_USAGE
  1111. const WCHAR* wszCmdOptionServer = L"S" ; //CMDOPTION_SERVER
  1112. const WCHAR* wszCmdOptionUser = L"U" ; //CMDOPTION_USER
  1113. const WCHAR* wszCmdOptionPassword = L"P" ; //CMDOPTION_PASSWORD
  1114. const WCHAR* wszCmdOptionFilename = L"F" ; //CMDOPTION_FILENAME
  1115. const WCHAR* wszCmdOptionRecurse = L"R" ; //CMDOPTION_RECURSE
  1116. const WCHAR* wszCmdOptionAdmin = L"A" ; //CMDOPTION_ADMIN
  1117. const WCHAR* wszCmdOptionDefault = L"D" ;
  1118. WCHAR wszConfirmValues[MAX_CONFIRM_VALUE] ;
  1119. SecureZeroMemory(wszConfirmValues, MAX_CONFIRM_VALUE * sizeof(WCHAR));
  1120. StringCopy(wszConfirmValues,GetResString(IDS_YESNO),SIZE_OF_ARRAY(wszConfirmValues));
  1121. //validate input parameters
  1122. if( ( NULL == pbUsage ) || ( NULL == pbNeedPassword ) )
  1123. {
  1124. SetLastError( (DWORD)E_OUTOFMEMORY );
  1125. SaveLastError();
  1126. ShowMessage( stderr, ERROR_STRING );
  1127. ShowMessage( stderr, SPACE_CHAR );
  1128. ShowLastError( stderr );
  1129. return FALSE;
  1130. }
  1131. //Initialize the valid command line arguments
  1132. // /s option
  1133. StringCopyA( tcmdOptions[CMD_PARSE_SERVER].szSignature, "PARSER2\0", 8 );
  1134. tcmdOptions[ CMD_PARSE_SERVER ].dwType = CP_TYPE_TEXT;
  1135. tcmdOptions[ CMD_PARSE_SERVER ].pwszOptions = wszCmdOptionServer;
  1136. tcmdOptions[ CMD_PARSE_SERVER ].pwszFriendlyName = NULL;
  1137. tcmdOptions[ CMD_PARSE_SERVER ].pwszValues = NULL;
  1138. tcmdOptions[ CMD_PARSE_SERVER ].dwCount = 1;
  1139. tcmdOptions[ CMD_PARSE_SERVER ].dwActuals = 0;
  1140. tcmdOptions[ CMD_PARSE_SERVER ].dwFlags = CP2_VALUE_TRIMINPUT | CP2_ALLOCMEMORY | CP2_VALUE_NONULL;
  1141. tcmdOptions[ CMD_PARSE_SERVER ].pValue = NULL;
  1142. tcmdOptions[ CMD_PARSE_SERVER ].dwLength = 0;
  1143. tcmdOptions[ CMD_PARSE_SERVER ].pFunction = NULL;
  1144. tcmdOptions[ CMD_PARSE_SERVER ].pFunctionData = NULL;
  1145. tcmdOptions[ CMD_PARSE_SERVER ].dwReserved = 0;
  1146. tcmdOptions[ CMD_PARSE_SERVER ].pReserved1 = NULL;
  1147. tcmdOptions[ CMD_PARSE_SERVER ].pReserved2 = NULL;
  1148. tcmdOptions[ CMD_PARSE_SERVER ].pReserved3 = NULL;
  1149. // /u option
  1150. StringCopyA( tcmdOptions[CMD_PARSE_USER].szSignature, "PARSER2\0", 8 );
  1151. tcmdOptions[ CMD_PARSE_USER ].dwType = CP_TYPE_TEXT;
  1152. tcmdOptions[ CMD_PARSE_USER ].pwszOptions = wszCmdOptionUser;
  1153. tcmdOptions[ CMD_PARSE_USER ].pwszFriendlyName = NULL;
  1154. tcmdOptions[ CMD_PARSE_USER ].pwszValues = NULL;
  1155. tcmdOptions[ CMD_PARSE_USER ].dwCount = 1;
  1156. tcmdOptions[ CMD_PARSE_USER ].dwActuals = 0;
  1157. tcmdOptions[ CMD_PARSE_USER ].dwFlags = CP2_VALUE_TRIMINPUT | CP2_ALLOCMEMORY | CP2_VALUE_NONULL;
  1158. tcmdOptions[ CMD_PARSE_USER ].pValue = NULL;
  1159. tcmdOptions[ CMD_PARSE_USER ].dwLength = 0;
  1160. tcmdOptions[ CMD_PARSE_USER ].pFunction = NULL;
  1161. tcmdOptions[ CMD_PARSE_USER ].pFunctionData = NULL;
  1162. tcmdOptions[ CMD_PARSE_USER ].dwReserved = 0;
  1163. tcmdOptions[ CMD_PARSE_USER ].pReserved1 = NULL;
  1164. tcmdOptions[ CMD_PARSE_USER ].pReserved2 = NULL;
  1165. tcmdOptions[ CMD_PARSE_USER ].pReserved3 = NULL;
  1166. // /p option
  1167. StringCopyA( tcmdOptions[CMD_PARSE_PWD].szSignature, "PARSER2\0", 8 );
  1168. tcmdOptions[ CMD_PARSE_PWD ].dwType = CP_TYPE_TEXT;
  1169. tcmdOptions[ CMD_PARSE_PWD ].pwszOptions = wszCmdOptionPassword;
  1170. tcmdOptions[ CMD_PARSE_PWD ].pwszFriendlyName = NULL;
  1171. tcmdOptions[ CMD_PARSE_PWD ].pwszValues = NULL;
  1172. tcmdOptions[ CMD_PARSE_PWD ].dwCount = 1;
  1173. tcmdOptions[ CMD_PARSE_PWD ].dwActuals = 0;
  1174. tcmdOptions[ CMD_PARSE_PWD ].dwFlags = CP2_VALUE_OPTIONAL | CP2_ALLOCMEMORY;
  1175. tcmdOptions[ CMD_PARSE_PWD ].pValue = NULL;
  1176. tcmdOptions[ CMD_PARSE_PWD ].dwLength = 0;
  1177. tcmdOptions[ CMD_PARSE_PWD ].pFunction = NULL;
  1178. tcmdOptions[ CMD_PARSE_PWD ].pFunctionData = NULL;
  1179. tcmdOptions[ CMD_PARSE_PWD ].dwReserved = 0;
  1180. tcmdOptions[ CMD_PARSE_PWD ].pReserved1 = NULL;
  1181. tcmdOptions[ CMD_PARSE_PWD ].pReserved2 = NULL;
  1182. tcmdOptions[ CMD_PARSE_PWD ].pReserved3 = NULL;
  1183. // /? option
  1184. StringCopyA( tcmdOptions[CMD_PARSE_USG].szSignature, "PARSER2\0", 8 );
  1185. tcmdOptions[ CMD_PARSE_USG ].dwType = CP_TYPE_BOOLEAN;
  1186. tcmdOptions[ CMD_PARSE_USG ].pwszOptions = wszCmdOptionUsage;
  1187. tcmdOptions[ CMD_PARSE_USG ].pwszFriendlyName = NULL;
  1188. tcmdOptions[ CMD_PARSE_USG ].pwszValues = NULL;
  1189. tcmdOptions[ CMD_PARSE_USG ].dwCount = 1;
  1190. tcmdOptions[ CMD_PARSE_USG ].dwActuals = 0;
  1191. tcmdOptions[ CMD_PARSE_USG ].dwFlags = CP2_USAGE;
  1192. tcmdOptions[ CMD_PARSE_USG ].pValue = pbUsage;
  1193. tcmdOptions[ CMD_PARSE_USG ].dwLength = MAX_STRING_LENGTH;
  1194. tcmdOptions[ CMD_PARSE_USG ].pFunction = NULL;
  1195. tcmdOptions[ CMD_PARSE_USG ].pFunctionData = NULL;
  1196. tcmdOptions[ CMD_PARSE_USG ].dwReserved = 0;
  1197. tcmdOptions[ CMD_PARSE_USG ].pReserved1 = NULL;
  1198. tcmdOptions[ CMD_PARSE_USG ].pReserved2 = NULL;
  1199. tcmdOptions[ CMD_PARSE_USG ].pReserved3 = NULL;
  1200. // /f name
  1201. StringCopyA( tcmdOptions[CMD_PARSE_FN].szSignature, "PARSER2\0", 8 );
  1202. tcmdOptions[ CMD_PARSE_FN ].dwType = CP_TYPE_TEXT;
  1203. tcmdOptions[ CMD_PARSE_FN ].pwszOptions = wszCmdOptionFilename;
  1204. tcmdOptions[ CMD_PARSE_FN ].pwszFriendlyName = NULL;
  1205. tcmdOptions[ CMD_PARSE_FN ].pwszValues = NULL;
  1206. tcmdOptions[ CMD_PARSE_FN ].dwCount = 1;
  1207. tcmdOptions[ CMD_PARSE_FN ].dwActuals = 0;
  1208. tcmdOptions[ CMD_PARSE_FN ].dwFlags = CP2_VALUE_TRIMINPUT | CP2_ALLOCMEMORY | CP2_VALUE_NONULL;
  1209. tcmdOptions[ CMD_PARSE_FN ].pValue = NULL;
  1210. tcmdOptions[ CMD_PARSE_FN ].dwLength = 0;
  1211. tcmdOptions[ CMD_PARSE_FN ].pFunction = NULL;
  1212. tcmdOptions[ CMD_PARSE_FN ].pFunctionData = NULL;
  1213. tcmdOptions[ CMD_PARSE_FN ].dwReserved = 0;
  1214. tcmdOptions[ CMD_PARSE_FN ].pReserved1 = NULL;
  1215. tcmdOptions[ CMD_PARSE_FN ].pReserved2 = NULL;
  1216. tcmdOptions[ CMD_PARSE_FN ].pReserved3 = NULL;
  1217. // /r option
  1218. StringCopyA( tcmdOptions[CMD_PARSE_RECURSE].szSignature, "PARSER2\0", 8 );
  1219. tcmdOptions[ CMD_PARSE_RECURSE ].dwType = CP_TYPE_BOOLEAN;
  1220. tcmdOptions[ CMD_PARSE_RECURSE ].pwszOptions = wszCmdOptionRecurse;
  1221. tcmdOptions[ CMD_PARSE_RECURSE ].pwszFriendlyName = NULL;
  1222. tcmdOptions[ CMD_PARSE_RECURSE ].pwszValues = NULL;
  1223. tcmdOptions[ CMD_PARSE_RECURSE ].dwCount = 1;
  1224. tcmdOptions[ CMD_PARSE_RECURSE ].dwActuals = 0;
  1225. tcmdOptions[ CMD_PARSE_RECURSE ].dwFlags = 0;
  1226. tcmdOptions[ CMD_PARSE_RECURSE ].pValue = pbRecursive;
  1227. tcmdOptions[ CMD_PARSE_RECURSE ].dwLength = MAX_STRING_LENGTH;
  1228. tcmdOptions[ CMD_PARSE_RECURSE ].pFunction = NULL;
  1229. tcmdOptions[ CMD_PARSE_RECURSE ].pFunctionData = NULL;
  1230. tcmdOptions[ CMD_PARSE_RECURSE ].dwReserved = 0;
  1231. tcmdOptions[ CMD_PARSE_RECURSE ].pReserved1 = NULL;
  1232. tcmdOptions[ CMD_PARSE_RECURSE ].pReserved2 = NULL;
  1233. tcmdOptions[ CMD_PARSE_RECURSE ].pReserved3 = NULL;
  1234. // /a option
  1235. StringCopyA( tcmdOptions[CMD_PARSE_ADMIN].szSignature, "PARSER2\0", 8 );
  1236. tcmdOptions[ CMD_PARSE_ADMIN ].dwType = CP_TYPE_BOOLEAN;
  1237. tcmdOptions[ CMD_PARSE_ADMIN ].pwszOptions = wszCmdOptionAdmin;
  1238. tcmdOptions[ CMD_PARSE_ADMIN ].pwszFriendlyName = NULL;
  1239. tcmdOptions[ CMD_PARSE_ADMIN ].pwszValues = NULL;
  1240. tcmdOptions[ CMD_PARSE_ADMIN ].dwCount = 1;
  1241. tcmdOptions[ CMD_PARSE_ADMIN ].dwActuals = 0;
  1242. tcmdOptions[ CMD_PARSE_ADMIN ].dwFlags = 0;
  1243. tcmdOptions[ CMD_PARSE_ADMIN ].pValue = pbAdminsOwner;
  1244. tcmdOptions[ CMD_PARSE_ADMIN ].dwLength = MAX_STRING_LENGTH;
  1245. tcmdOptions[ CMD_PARSE_ADMIN ].pFunction = NULL;
  1246. tcmdOptions[ CMD_PARSE_ADMIN ].pFunctionData = NULL;
  1247. tcmdOptions[ CMD_PARSE_ADMIN ].dwReserved = 0;
  1248. tcmdOptions[ CMD_PARSE_ADMIN ].pReserved1 = NULL;
  1249. tcmdOptions[ CMD_PARSE_ADMIN ].pReserved2 = NULL;
  1250. tcmdOptions[ CMD_PARSE_ADMIN ].pReserved3 = NULL;
  1251. StringCopyA( tcmdOptions[CMD_PARSE_CONFIRM].szSignature, "PARSER2\0", 8 );
  1252. tcmdOptions[ CMD_PARSE_CONFIRM ].dwType = CP_TYPE_TEXT;
  1253. tcmdOptions[ CMD_PARSE_CONFIRM ].pwszOptions = wszCmdOptionDefault;
  1254. tcmdOptions[ CMD_PARSE_CONFIRM ].pwszFriendlyName = NULL;
  1255. tcmdOptions[ CMD_PARSE_CONFIRM ].pwszValues = wszConfirmValues;
  1256. tcmdOptions[ CMD_PARSE_CONFIRM ].dwCount = 1;
  1257. tcmdOptions[ CMD_PARSE_CONFIRM ].dwActuals = 0;
  1258. tcmdOptions[ CMD_PARSE_CONFIRM ].dwFlags = CP2_MODE_VALUES | CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  1259. tcmdOptions[ CMD_PARSE_CONFIRM ].pValue = szConfirm;
  1260. tcmdOptions[ CMD_PARSE_CONFIRM ].dwLength = MAX_CONFIRM_VALUE;
  1261. tcmdOptions[ CMD_PARSE_CONFIRM ].pFunction = NULL;
  1262. tcmdOptions[ CMD_PARSE_CONFIRM ].pFunctionData = NULL;
  1263. tcmdOptions[ CMD_PARSE_CONFIRM ].dwReserved = 0;
  1264. tcmdOptions[ CMD_PARSE_CONFIRM ].pReserved1 = NULL;
  1265. tcmdOptions[ CMD_PARSE_CONFIRM ].pReserved2 = NULL;
  1266. tcmdOptions[ CMD_PARSE_CONFIRM ].pReserved3 = NULL;
  1267. //parse the command line arguments
  1268. bFlag = DoParseParam2( argc, argv, -1, SIZE_OF_ARRAY( tcmdOptions ), tcmdOptions, 0 );
  1269. //if syntax of command line arguments is false display the error and exit
  1270. if( FALSE == bFlag )
  1271. {
  1272. ShowMessage( stderr, ERROR_STRING );
  1273. ShowMessage( stderr, SPACE_CHAR );
  1274. ShowMessage( stderr, GetReason() );
  1275. return( FALSE );
  1276. }
  1277. *szMachineName = (LPWSTR)tcmdOptions[CMD_PARSE_SERVER].pValue;
  1278. *szUserName = (LPWSTR)tcmdOptions[CMD_PARSE_USER].pValue;
  1279. *szPassword = (LPWSTR)tcmdOptions[CMD_PARSE_PWD].pValue;
  1280. *szFileName = (LPWSTR)tcmdOptions[CMD_PARSE_FN].pValue;
  1281. // remove trialing spaces
  1282. //if usage is specified at command line, then check whether any other
  1283. //arguments are entered at the command line and if so display syntax
  1284. //error
  1285. if( TRUE == *pbUsage )
  1286. {
  1287. if( argc > 2 )
  1288. {
  1289. ShowMessage( stderr, ERROR_SYNTAX_ERROR );
  1290. return( FALSE );
  1291. }
  1292. else
  1293. {
  1294. return (TRUE);
  1295. }
  1296. }
  1297. // check whether the password (-p) specified in the command line or not
  1298. // and also check whether '*' or empty is given for -p or not
  1299. // check the remote connectivity information
  1300. if ( *szMachineName != NULL )
  1301. {
  1302. //
  1303. // if -u is not specified, we need to allocate memory
  1304. // in order to be able to retrive the current user name
  1305. //
  1306. // case 1: -p is not at all specified
  1307. // as the value for this switch is optional, we have to rely
  1308. // on the dwActuals to determine whether the switch is specified or not
  1309. // in this case utility needs to try to connect first and if it fails
  1310. // then prompt for the password -- in fact, we need not check for this
  1311. // condition explicitly except for noting that we need to prompt for the
  1312. // password
  1313. //
  1314. // case 2: -p is specified
  1315. // but we need to check whether the value is specified or not
  1316. // in this case user wants the utility to prompt for the password
  1317. // before trying to connect
  1318. //
  1319. // case 3: -p * is specified
  1320. // user name
  1321. if ( *szUserName == NULL )
  1322. {
  1323. *szUserName = (LPWSTR) AllocateMemory( MAX_STRING_LENGTH * sizeof( WCHAR ) );
  1324. if ( *szUserName == NULL )
  1325. {
  1326. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  1327. return( FALSE );
  1328. }
  1329. }
  1330. // password
  1331. if ( *szPassword == NULL )
  1332. {
  1333. *pbNeedPassword = TRUE;
  1334. *szPassword = (LPWSTR)AllocateMemory( MAX_STRING_LENGTH * sizeof( WCHAR ) );
  1335. if ( *szPassword == NULL )
  1336. {
  1337. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  1338. return( FALSE );
  1339. }
  1340. }
  1341. // case 1
  1342. /*if ( cmdOptions[OPTION_PASSWORD].dwActuals == 0 )
  1343. {
  1344. // we need not do anything special here
  1345. }*/
  1346. if ( tcmdOptions[CMD_PARSE_PWD].pValue == NULL )
  1347. {
  1348. StringCopy( *szPassword, L"*", GetBufferSize((LPVOID)(*szPassword)));
  1349. }
  1350. else
  1351. if ( StringCompareEx( *szPassword, L"*", TRUE, 0 ) == 0 )
  1352. {
  1353. if ( ReallocateMemory( (LPVOID*)(szPassword),
  1354. MAX_STRING_LENGTH * sizeof( WCHAR ) ) == FALSE )
  1355. {
  1356. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  1357. return( FALSE );
  1358. }
  1359. else
  1360. if(NULL == szPassword)
  1361. {
  1362. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  1363. return( FALSE );
  1364. }
  1365. // ...
  1366. *pbNeedPassword = TRUE;
  1367. }
  1368. // case 3
  1369. }
  1370. if((0 != tcmdOptions[ CMD_PARSE_CONFIRM ].dwActuals ) && (0 == tcmdOptions[ CMD_PARSE_RECURSE ].dwActuals ) )
  1371. {
  1372. ShowMessage( stderr, GetResString(IDS_CONFIRM_ERROR ));
  1373. return( FALSE );
  1374. }
  1375. if( (StringLengthW(*szFileName, 0) == 0 ) && (0 != tcmdOptions[ CMD_PARSE_FN ].dwActuals ) )
  1376. {
  1377. ShowMessage( stderr, GetResString(IDS_NULL_FILENAME ));
  1378. return( FALSE );
  1379. }
  1380. //if default argument is not speciifed
  1381. if( ( 0 == tcmdOptions[ CMD_PARSE_FN ].dwActuals ) &&
  1382. ( FALSE == *pbUsage ) )
  1383. {
  1384. ShowMessage( stderr, ERROR_SYNTAX_ERROR );
  1385. return( FALSE );
  1386. }
  1387. // return false if username is entered without machine name
  1388. if ( ( 0 != tcmdOptions[ CMD_PARSE_USER ].dwActuals ) &&
  1389. ( 0 == tcmdOptions[ CMD_PARSE_SERVER ].dwActuals ) )
  1390. {
  1391. ShowMessage( stderr, ERROR_USER_WITH_NOSERVER );
  1392. return( FALSE );
  1393. }
  1394. //if password entered without username then return false
  1395. if( ( 0 == tcmdOptions[ CMD_PARSE_USER ].dwActuals ) &&
  1396. ( 0 != tcmdOptions[ CMD_PARSE_PWD ].dwActuals ) )
  1397. {
  1398. ShowMessage( stderr, ERROR_PASSWORD_WITH_NUSER );
  1399. return( FALSE );
  1400. }
  1401. //if /s is entered with empty string
  1402. if( ( 0 != tcmdOptions[ CMD_PARSE_SERVER ].dwActuals != 0 ) &&
  1403. ( 0 == StringLengthW( *szMachineName, 0 ) ) )
  1404. //( 0 == lstrlen( szMachineName ) ) )
  1405. {
  1406. ShowMessage( stderr, ERROR_NULL_SERVER );
  1407. return( FALSE );
  1408. }
  1409. //if /u is entered with empty string
  1410. if( ( 0 != tcmdOptions[ CMD_PARSE_USER ].dwActuals ) &&
  1411. ( 0 == StringLengthW( *szUserName, 0 ) ) )
  1412. {
  1413. ShowMessage( stderr, ERROR_NULL_USER );
  1414. return( FALSE );
  1415. }
  1416. //assign the data obtained from parsing to the call by address parameters
  1417. if ( ( 0 != tcmdOptions[ CMD_PARSE_PWD ].dwActuals ) &&
  1418. ( 0 == StringCompare( *szPassword, L"*", TRUE, 0 ) ) )
  1419. {
  1420. // user wants the utility to prompt for the password before trying to connect
  1421. *pbNeedPassword = TRUE;
  1422. }
  1423. else if ( 0 == tcmdOptions[ CMD_PARSE_PWD ].dwActuals &&
  1424. ( 0 != tcmdOptions[ CMD_PARSE_SERVER ].dwActuals || 0 != tcmdOptions[ CMD_PARSE_USER ].dwActuals ) )
  1425. {
  1426. // /s, /u is specified without password ...
  1427. // utility needs to try to connect first and if it fails then prompt for the password
  1428. *pbNeedPassword = TRUE;
  1429. StringCopyW( *szPassword, NULL_U_STRING, GetBufferSize(*szPassword) / sizeof(WCHAR));
  1430. }
  1431. return( TRUE );
  1432. }
  1433. VOID
  1434. DisplayUsage(
  1435. )
  1436. /*++
  1437. Routine Description:
  1438. This function displays the usage of takeown utility.
  1439. Arguments:
  1440. None.
  1441. Return Value:
  1442. VOID
  1443. --*/
  1444. {
  1445. DWORD dwIndex = 0;
  1446. //redirect the usage to the console
  1447. for( dwIndex = IDS_USAGE_BEGINING; dwIndex <= IDS_USAGE_ENDING; dwIndex++ )
  1448. {
  1449. ShowMessage( stdout, GetResString( dwIndex ) );
  1450. }
  1451. return;
  1452. }
  1453. BOOL
  1454. TakeOwnerShip(
  1455. IN LPCWSTR lpszFileName
  1456. )
  1457. /*++
  1458. Routine Description:
  1459. This routine takes the ownership of the specified file
  1460. Arguments:
  1461. [ IN ] lpszFileName - File name for whose ownership has to be taken.
  1462. Return Value:
  1463. TRUE if owner ship of the specified file has been taken
  1464. else FALSE
  1465. --*/
  1466. {
  1467. //local variables
  1468. SECURITY_DESCRIPTOR SecurityDescriptor;
  1469. PSECURITY_DESCRIPTOR pSd = NULL;
  1470. PACL pDacl;
  1471. HANDLE hFile;
  1472. PSID pAliasAdminsSid = NULL;
  1473. SID_IDENTIFIER_AUTHORITY SepNtAuthority = SECURITY_NT_AUTHORITY;
  1474. HANDLE hTokenHandle = NULL;
  1475. BOOL bResult = TRUE;
  1476. BOOL bInvalidFileHandle = FALSE;
  1477. //check for valid input parameters
  1478. if( lpszFileName == NULL )
  1479. {
  1480. SetLastError( (DWORD) E_OUTOFMEMORY );
  1481. SaveLastError();
  1482. return FALSE;
  1483. }
  1484. //allocate and initialise sid
  1485. bResult = AllocateAndInitializeSid(
  1486. &SepNtAuthority,
  1487. 2,
  1488. SECURITY_BUILTIN_DOMAIN_RID,
  1489. DOMAIN_ALIAS_RID_ADMINS,
  1490. 0,
  1491. 0,
  1492. 0,
  1493. 0,
  1494. 0,
  1495. 0,
  1496. &pAliasAdminsSid
  1497. );
  1498. if( FALSE == bResult )
  1499. {
  1500. SaveLastError();
  1501. return( FALSE );
  1502. }
  1503. //get the token of the current process
  1504. bResult = GetTokenHandle( &hTokenHandle );
  1505. if( FALSE == bResult )
  1506. {
  1507. SaveLastError();
  1508. if(NULL != pAliasAdminsSid)
  1509. {
  1510. FreeSid( pAliasAdminsSid );
  1511. }
  1512. return( FALSE );
  1513. }
  1514. // Attempt to put a NULL Dacl on the object
  1515. bResult = InitializeSecurityDescriptor( &SecurityDescriptor,
  1516. SECURITY_DESCRIPTOR_REVISION );
  1517. if( FALSE == bResult )
  1518. {
  1519. SaveLastError();
  1520. if(NULL != pAliasAdminsSid)
  1521. {
  1522. FreeSid( pAliasAdminsSid );
  1523. }
  1524. if( 0 == CloseHandle( hTokenHandle ))
  1525. {
  1526. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  1527. }
  1528. return( FALSE );
  1529. }
  1530. //Get the handle of the file or directory
  1531. hFile = CreateFile( lpszFileName, READ_CONTROL , FILE_SHARE_READ, NULL,
  1532. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL );
  1533. //try once again it may be a directory
  1534. if( INVALID_HANDLE_VALUE != hFile )
  1535. {
  1536. //get the DACL for the currently existing file or directory
  1537. if( 0 != GetSecurityInfo( hFile, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL,
  1538. NULL, &pDacl, NULL, &pSd ) )
  1539. {
  1540. SaveLastError();
  1541. if(NULL != pAliasAdminsSid)
  1542. {
  1543. FreeSid( pAliasAdminsSid );
  1544. }
  1545. CloseHandle( hFile );
  1546. if(0 == CloseHandle( hTokenHandle ))
  1547. {
  1548. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  1549. }
  1550. if(NULL != pSd)
  1551. {
  1552. LocalFree( pSd );
  1553. }
  1554. return( FALSE );
  1555. }
  1556. // set the security descriptor to acl
  1557. bResult = SetSecurityDescriptorDacl ( &SecurityDescriptor,
  1558. TRUE, pDacl, FALSE );
  1559. if( FALSE == bResult )
  1560. {
  1561. SaveLastError();
  1562. if(NULL != pAliasAdminsSid)
  1563. {
  1564. FreeSid( pAliasAdminsSid );
  1565. }
  1566. CloseHandle( hFile );
  1567. if(0 == CloseHandle( hTokenHandle ))
  1568. {
  1569. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  1570. }
  1571. if(NULL != pSd)
  1572. {
  1573. LocalFree( pSd );
  1574. }
  1575. return( FALSE );
  1576. }
  1577. }
  1578. else
  1579. {
  1580. bInvalidFileHandle = TRUE;
  1581. }
  1582. // Attempt to make Administrator the owner of the file.
  1583. bResult = SetSecurityDescriptorOwner ( &SecurityDescriptor,
  1584. pAliasAdminsSid, FALSE );
  1585. if( FALSE == bResult )
  1586. {
  1587. SaveLastError();
  1588. if(NULL != pAliasAdminsSid)
  1589. {
  1590. FreeSid( pAliasAdminsSid );
  1591. }
  1592. CloseHandle( hFile );
  1593. if(0 == CloseHandle( hTokenHandle ))
  1594. {
  1595. //DisplayErrorMsg(GetLastError());
  1596. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  1597. }
  1598. if(NULL != pSd)
  1599. {
  1600. LocalFree( pSd );
  1601. }
  1602. return( FALSE );
  1603. }
  1604. //set the file security to adminsitrator owner
  1605. bResult = SetFileSecurity( lpszFileName, OWNER_SECURITY_INFORMATION,
  1606. &SecurityDescriptor );
  1607. if( TRUE == bResult )
  1608. {
  1609. if(NULL != pAliasAdminsSid)
  1610. {
  1611. FreeSid( pAliasAdminsSid );
  1612. }
  1613. CloseHandle( hFile );
  1614. if(0 == CloseHandle( hTokenHandle ))
  1615. {
  1616. //DisplayErrorMsg(GetLastError());
  1617. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  1618. }
  1619. if(NULL != pSd)
  1620. {
  1621. LocalFree( pSd );
  1622. }
  1623. return( TRUE );
  1624. }
  1625. // Assert TakeOwnership privilege for current process, then try again
  1626. bResult = AssertTakeOwnership( hTokenHandle );
  1627. if( FALSE == bResult )
  1628. {
  1629. //SaveLastError();
  1630. if(TRUE == bInvalidFileHandle)
  1631. {
  1632. hFile = CreateFile( lpszFileName, READ_CONTROL , FILE_SHARE_READ, NULL,
  1633. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL );
  1634. if( INVALID_HANDLE_VALUE == hFile )
  1635. {
  1636. SaveLastError();
  1637. if(NULL != pAliasAdminsSid)
  1638. {
  1639. FreeSid( pAliasAdminsSid );
  1640. }
  1641. //CloseHandle( hFile );
  1642. if(0 == CloseHandle( hTokenHandle ))
  1643. {
  1644. //DisplayErrorMsg(GetLastError());
  1645. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  1646. }
  1647. return( FALSE );
  1648. }
  1649. }
  1650. if(NULL != pAliasAdminsSid)
  1651. {
  1652. FreeSid( pAliasAdminsSid );
  1653. }
  1654. CloseHandle( hFile );
  1655. if(0 == CloseHandle( hTokenHandle ))
  1656. {
  1657. //DisplayErrorMsg(GetLastError());
  1658. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  1659. }
  1660. if(NULL != pSd)
  1661. {
  1662. LocalFree( pSd );
  1663. }
  1664. return( FALSE );
  1665. }
  1666. //Now try to set ownership security privilege for the file
  1667. bResult = SetFileSecurity( lpszFileName, OWNER_SECURITY_INFORMATION,
  1668. &SecurityDescriptor );
  1669. if( FALSE == bResult )
  1670. {
  1671. SaveLastError();
  1672. if(TRUE == bInvalidFileHandle)
  1673. {
  1674. //Check out whether it is an invalid file or file does not exist
  1675. hFile = CreateFile( lpszFileName, READ_CONTROL , FILE_SHARE_READ, NULL,
  1676. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL );
  1677. if( INVALID_HANDLE_VALUE == hFile )
  1678. {
  1679. SaveLastError();
  1680. if(NULL != pAliasAdminsSid)
  1681. {
  1682. FreeSid( pAliasAdminsSid );
  1683. }
  1684. //CloseHandle( hFile );
  1685. if(0 == CloseHandle( hTokenHandle ))
  1686. {
  1687. //DisplayErrorMsg(GetLastError());
  1688. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  1689. }
  1690. return( FALSE );
  1691. }
  1692. }
  1693. if(NULL != pAliasAdminsSid)
  1694. {
  1695. FreeSid( pAliasAdminsSid );
  1696. }
  1697. CloseHandle( hFile );
  1698. if(0 == CloseHandle( hTokenHandle ))
  1699. {
  1700. //DisplayErrorMsg(GetLastError());
  1701. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  1702. }
  1703. if(NULL != pSd)
  1704. {
  1705. LocalFree( pSd );
  1706. }
  1707. return( FALSE );
  1708. }
  1709. if(NULL != pAliasAdminsSid)
  1710. {
  1711. FreeSid( pAliasAdminsSid );
  1712. }
  1713. CloseHandle( hFile );
  1714. if(0 == CloseHandle( hTokenHandle ))
  1715. {
  1716. //DisplayErrorMsg(GetLastError());
  1717. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  1718. }
  1719. if(NULL != pSd)
  1720. {
  1721. LocalFree( pSd );
  1722. }
  1723. return( TRUE );
  1724. }
  1725. BOOL
  1726. GetTokenHandle(
  1727. OUT PHANDLE hTokenHandle
  1728. )
  1729. /*++
  1730. Routine Description:
  1731. Get the token handle of the current process.
  1732. Arguments:
  1733. [ OUT ] hTokenHandle - handle to the current token.
  1734. Return Value:
  1735. TRUE if successful in getting the token
  1736. else FALSE
  1737. --*/
  1738. {
  1739. //local variables
  1740. BOOL bFlag = TRUE;
  1741. HANDLE hProcessHandle = NULL;
  1742. //check for valid input arguments
  1743. if( hTokenHandle == NULL )
  1744. {
  1745. SetLastError( (DWORD) E_OUTOFMEMORY );
  1746. SaveLastError();
  1747. return FALSE;
  1748. }
  1749. //open the current process
  1750. hProcessHandle = OpenProcess( PROCESS_QUERY_INFORMATION,
  1751. FALSE, GetCurrentProcessId() );
  1752. //if unable to open the current process
  1753. if ( NULL == hProcessHandle )
  1754. {
  1755. SaveLastError();
  1756. return( FALSE );
  1757. }
  1758. //open the token of the current process
  1759. bFlag = OpenProcessToken ( hProcessHandle,
  1760. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  1761. hTokenHandle );
  1762. if ( FALSE == bFlag )
  1763. {
  1764. SaveLastError();
  1765. CloseHandle( hProcessHandle );
  1766. return FALSE;
  1767. }
  1768. if( 0 == CloseHandle( hProcessHandle ))
  1769. {
  1770. //DisplayErrorMsg(GetLastError());
  1771. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  1772. }
  1773. return( TRUE );
  1774. }
  1775. BOOL
  1776. AssertTakeOwnership(
  1777. IN HANDLE hTokenHandle
  1778. )
  1779. /*++
  1780. Routine Description:
  1781. This routine asserts the takeownership privilege to the current process
  1782. Arguments:
  1783. [ IN ] hTokenHandle - Token handle of the current process.
  1784. Return Value:
  1785. TRUE if successful in asserting the takeownership privilege to current process
  1786. else FALSE
  1787. --*/
  1788. {
  1789. //local variables
  1790. LUID TakeOwnershipValue;
  1791. TOKEN_PRIVILEGES TokenPrivileges;
  1792. BOOL bResult = TRUE;
  1793. //check for valid input arguments
  1794. if( hTokenHandle == NULL )
  1795. {
  1796. SetLastError( (DWORD) E_OUTOFMEMORY );
  1797. SaveLastError();
  1798. return FALSE;
  1799. }
  1800. // First, assert TakeOwnership privilege
  1801. bResult = LookupPrivilegeValue( NULL, SE_TAKE_OWNERSHIP_NAME,
  1802. &TakeOwnershipValue );
  1803. if ( FALSE == bResult )
  1804. {
  1805. SaveLastError();
  1806. return( FALSE );
  1807. }
  1808. // Set up the privilege set we will need
  1809. TokenPrivileges.PrivilegeCount = 1;
  1810. TokenPrivileges.Privileges[0].Luid = TakeOwnershipValue;
  1811. TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1812. //adjust the privlige to this new privilege
  1813. (VOID) AdjustTokenPrivileges (
  1814. hTokenHandle,
  1815. FALSE,
  1816. &TokenPrivileges,
  1817. sizeof( TOKEN_PRIVILEGES ),
  1818. NULL,
  1819. NULL
  1820. );
  1821. if ( NO_ERROR != GetLastError() )
  1822. {
  1823. SaveLastError();
  1824. return( FALSE );
  1825. }
  1826. return( TRUE );
  1827. }
  1828. BOOL
  1829. TakeOwnerShipAll(IN LPWSTR lpszFileName,
  1830. IN BOOL bCurrDirTakeOwnAllFiles,
  1831. IN PDWORD dwFileCount,
  1832. IN BOOL bDriveCurrDirTakeOwnAllFiles,
  1833. IN BOOL bAdminsOwner,
  1834. IN LPWSTR szOwnerString,
  1835. BOOL bMatchPattern,
  1836. LPWSTR wszPatternString)
  1837. /*++
  1838. Routine Description:
  1839. This routine takes the owner ship of the all the files in the
  1840. current directory
  1841. Arguments:
  1842. [ IN ] lpszFileName : The path name to which ownership for the files in the path to be given
  1843. [ IN ] bCurrDirTakeOwnAllFiles : To determine whether ownership to be given for current directory files
  1844. [ IN ] dwFileCount : To determine whether there is not even a sigle file found in the specified path
  1845. [ IN ] bDriveCurrDirTakeOwnAllFiles : To determine whether ownership to be given for files specified in the path
  1846. [ IN ] bAdminsOwner : To ddetermine whether ownership to be given for administrators group
  1847. [ IN ] dwUserName : Logged on user name
  1848. [ IN ] szOwnerString : Logged on user name in Sam Compatible format
  1849. [ IN ] bLogonDomainAdmin : Indicates whether the logged on user is domain administrator or not
  1850. Return Value:
  1851. TRUE if owner ship of the files in the current directory is successful
  1852. else FALSE
  1853. --*/
  1854. {
  1855. //local variables
  1856. WIN32_FIND_DATA FindFileData;
  1857. BOOL bFlag = TRUE;
  1858. DWORD dwRet = 0;
  1859. HANDLE hHandle = NULL;
  1860. WCHAR szFileName[MAX_RES_STRING + 3*EXTRA_MEM] ;
  1861. WCHAR wszTempMessage[3*MAX_STRING_LENGTH] ;
  1862. LPWSTR szDir = NULL;
  1863. LPWSTR szTakeownFile = NULL;
  1864. LPWSTR szTmpFileName = NULL;
  1865. LPWSTR wszFormedMessage = NULL;
  1866. LPWSTR lpNextTok = NULL;
  1867. //LPWSTR szDirStart = NULL;
  1868. HRESULT hr;
  1869. SecureZeroMemory(szFileName, (MAX_RES_STRING + 3*EXTRA_MEM) * sizeof(WCHAR));
  1870. SecureZeroMemory(wszTempMessage, (3 * MAX_STRING_LENGTH) * sizeof(WCHAR));
  1871. if(FALSE == bCurrDirTakeOwnAllFiles)
  1872. {
  1873. //ASSIGN_MEMORY(szDir,WCHAR,(StringLengthW(lpszFileName, 0)) + 20);
  1874. szDir = (LPWSTR)AllocateMemory((StringLengthW(lpszFileName, 0) + 20) * sizeof(WCHAR));
  1875. if(NULL == szDir)
  1876. {
  1877. SaveLastError();
  1878. return FALSE;
  1879. }
  1880. szTakeownFile = (LPWSTR)AllocateMemory((StringLengthW(lpszFileName, 0) + MAX_STRING_LENGTH + 20) * sizeof(WCHAR));
  1881. if(NULL == szTakeownFile)
  1882. {
  1883. SaveLastError();
  1884. FREE_MEMORY(szDir);
  1885. return FALSE;
  1886. }
  1887. szTmpFileName = (LPWSTR)AllocateMemory((StringLengthW(lpszFileName, 0) + (2 * MAX_STRING_LENGTH) + 20) * sizeof(WCHAR));
  1888. if(NULL == szTmpFileName)
  1889. {
  1890. SaveLastError();
  1891. FREE_MEMORY(szDir);
  1892. FREE_MEMORY(szTakeownFile);
  1893. return FALSE;
  1894. }
  1895. }
  1896. if(TRUE == bCurrDirTakeOwnAllFiles)
  1897. {
  1898. dwRet = GetCurrentDirectory( 0, szDir );
  1899. if( 0 == dwRet )
  1900. {
  1901. SaveLastError();
  1902. return FALSE;
  1903. }
  1904. szDir = (LPWSTR)AllocateMemory((dwRet + 20) * sizeof(WCHAR));
  1905. if(NULL == szDir)
  1906. {
  1907. SaveLastError();
  1908. return FALSE;
  1909. }
  1910. dwRet = GetCurrentDirectory( dwRet + 20, szDir );
  1911. if( 0 == dwRet )
  1912. {
  1913. SaveLastError();
  1914. FREE_MEMORY(szDir);
  1915. return FALSE;
  1916. }
  1917. szTakeownFile = (LPWSTR)AllocateMemory((StringLengthW(szDir, 0) + MAX_STRING_LENGTH + 20) * sizeof(WCHAR));
  1918. if(NULL == szTakeownFile)
  1919. {
  1920. SaveLastError();
  1921. FREE_MEMORY(szDir);
  1922. return FALSE;
  1923. }
  1924. szTmpFileName = (LPWSTR)AllocateMemory((StringLengthW(szDir, 0) + (2 * MAX_STRING_LENGTH) + 20) * sizeof(WCHAR));
  1925. if(NULL == szTmpFileName)
  1926. {
  1927. SaveLastError();
  1928. FREE_MEMORY(szDir);
  1929. FREE_MEMORY(szTakeownFile);
  1930. return FALSE;
  1931. }
  1932. }
  1933. else
  1934. {
  1935. StringCopy( szDir, lpszFileName, (GetBufferSize(szDir) / sizeof(WCHAR)) );
  1936. }
  1937. /*Attach "*.*" at the end of the path to get all the files*/
  1938. if(StringLengthW(szDir, 0) != 0 && FALSE == bMatchPattern)
  1939. {
  1940. if( *(szDir + StringLengthW(szDir, 0) - 1) != L'\\' )
  1941. {
  1942. StringConcat(szDir, ALL_FILES, GetBufferSize(szDir)/sizeof(TCHAR));
  1943. }
  1944. else
  1945. {
  1946. StringConcat(szDir,L"*.*" , (GetBufferSize(szDir) / sizeof(WCHAR)));
  1947. }
  1948. }
  1949. else
  1950. {
  1951. StringConcat(szDir, L"\\", (GetBufferSize(szDir) / sizeof(WCHAR)));
  1952. StringConcat(szDir, wszPatternString, (GetBufferSize(szDir) / sizeof(WCHAR)));
  1953. }
  1954. if( INVALID_HANDLE_VALUE != ( hHandle = FindFirstFile( szDir, &FindFileData ) ) )
  1955. {
  1956. StringCopy( szFileName, FindFileData.cFileName, SIZE_OF_ARRAY(szFileName) );
  1957. if( ( 0 != StringCompare( szFileName, DOT, TRUE, 0 ) ) &&
  1958. ( 0 != StringCompare( szFileName, DOTS, TRUE, 0 ) ) )
  1959. {
  1960. (*dwFileCount)= (*dwFileCount) + 1;
  1961. if(FALSE == bCurrDirTakeOwnAllFiles && FALSE == bDriveCurrDirTakeOwnAllFiles)
  1962. {
  1963. hr = StringCchPrintf(szTakeownFile, (GetBufferSize(szTakeownFile) / sizeof(WCHAR)), L"%s%s", lpszFileName,szFileName);
  1964. if(FAILED(hr))
  1965. {
  1966. SetLastError(HRESULT_CODE(hr));
  1967. SaveLastError();
  1968. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  1969. FREE_MEMORY(szDir);
  1970. FREE_MEMORY(szTakeownFile);
  1971. FREE_MEMORY(szTmpFileName);
  1972. return FALSE;
  1973. }
  1974. }
  1975. else
  1976. if(TRUE == bCurrDirTakeOwnAllFiles)
  1977. {
  1978. StringCopy( szTakeownFile, szFileName, (GetBufferSize(szTakeownFile) / sizeof(WCHAR)) );
  1979. }
  1980. else
  1981. {
  1982. if( *(lpszFileName + StringLengthW(lpszFileName, 0) - 1) != L'\\' )
  1983. {
  1984. hr = StringCchPrintf(szTakeownFile, (GetBufferSize(szTakeownFile) / sizeof(WCHAR)), L"%s\\%s", lpszFileName,szFileName);
  1985. }
  1986. else
  1987. {
  1988. hr = StringCchPrintf(szTakeownFile, (GetBufferSize(szTakeownFile) / sizeof(WCHAR)), L"%s%s", lpszFileName,szFileName);
  1989. }
  1990. if(FAILED(hr))
  1991. {
  1992. SetLastError(HRESULT_CODE(hr));
  1993. SaveLastError();
  1994. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  1995. FREE_MEMORY(szDir);
  1996. FREE_MEMORY(szTakeownFile);
  1997. FREE_MEMORY(szTmpFileName);
  1998. return FALSE;
  1999. }
  2000. }
  2001. if(TRUE == bAdminsOwner)
  2002. {
  2003. bFlag = TakeOwnerShip( szTakeownFile);
  2004. }
  2005. else
  2006. {
  2007. bFlag = TakeOwnerShipIndividual(szTakeownFile);
  2008. }
  2009. if( FALSE == bFlag )
  2010. {
  2011. if( ERROR_NOT_ALL_ASSIGNED == GetLastError())
  2012. {
  2013. hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), GetResString(IDS_NOT_OWNERSHIP_INFO), szTakeownFile);
  2014. if(FAILED(hr))
  2015. {
  2016. SetLastError(HRESULT_CODE(hr));
  2017. SaveLastError();
  2018. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2019. FREE_MEMORY(szDir);
  2020. FREE_MEMORY(szTakeownFile);
  2021. FREE_MEMORY(szTmpFileName);
  2022. return( FALSE );
  2023. }
  2024. ShowMessage(stdout, szTmpFileName);
  2025. }
  2026. else if(ERROR_SHARING_VIOLATION == GetLastError())
  2027. {
  2028. hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), GetResString(IDS_SHARING_VIOLATION_INFO) , szTakeownFile);
  2029. if(FAILED(hr))
  2030. {
  2031. SetLastError(HRESULT_CODE(hr));
  2032. SaveLastError();
  2033. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2034. FREE_MEMORY(szDir);
  2035. FREE_MEMORY(szTakeownFile);
  2036. FREE_MEMORY(szTmpFileName);
  2037. return( FALSE );
  2038. }
  2039. ShowMessage(stdout, szTmpFileName);
  2040. }
  2041. else
  2042. {
  2043. wszFormedMessage = (LPWSTR)AllocateMemory((StringLengthW(szTakeownFile, 0) + MAX_STRING_LENGTH) * sizeof(WCHAR));
  2044. if ( wszFormedMessage == NULL )
  2045. {
  2046. SaveLastError();
  2047. FREE_MEMORY(szDir);
  2048. FREE_MEMORY(szTakeownFile);
  2049. FREE_MEMORY(szTmpFileName);
  2050. return( FALSE );
  2051. }
  2052. ShowMessage( stderr, ERROR_STRING );
  2053. ShowMessage( stderr, SPACE_CHAR );
  2054. //lstrcpy(wszTempMessage, GetReason());
  2055. StringCopy( wszTempMessage, GetReason(), SIZE_OF_ARRAY(wszTempMessage) );
  2056. lpNextTok = _tcstok(wszTempMessage, L".");
  2057. ShowMessage(stdout,wszTempMessage);
  2058. //hr = StringCchPrintf(wszFormedMessage, (GetBufferSize(wszFormedMessage) / sizeof(WCHAR)), GetResString(IDS_ON_FILE_FOLDER), szTakeownFile);
  2059. hr = StringCchPrintf(wszFormedMessage, (GetBufferSize(wszFormedMessage) / sizeof(WCHAR)), L". ( \"%s\" )\n", szTakeownFile);
  2060. if(FAILED(hr))
  2061. {
  2062. SetLastError(HRESULT_CODE(hr));
  2063. SaveLastError();
  2064. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2065. FREE_MEMORY(szDir);
  2066. FREE_MEMORY(szTakeownFile);
  2067. FREE_MEMORY(szTmpFileName);
  2068. return( FALSE );
  2069. }
  2070. ShowMessage(stderr, wszFormedMessage);
  2071. FREE_MEMORY(wszFormedMessage);
  2072. }
  2073. }
  2074. else
  2075. {
  2076. if(TRUE == bAdminsOwner)
  2077. {
  2078. hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL, szTakeownFile);
  2079. if(FAILED(hr))
  2080. {
  2081. SetLastError(HRESULT_CODE(hr));
  2082. SaveLastError();
  2083. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2084. FREE_MEMORY(szDir);
  2085. FREE_MEMORY(szTakeownFile);
  2086. FREE_MEMORY(szTmpFileName);
  2087. return( FALSE );
  2088. }
  2089. }
  2090. else
  2091. {
  2092. //hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL_USER, szOwnerString, szTakeownFile);
  2093. hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL_USER, szTakeownFile, szOwnerString);
  2094. if(FAILED(hr))
  2095. {
  2096. SetLastError(HRESULT_CODE(hr));
  2097. SaveLastError();
  2098. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2099. FREE_MEMORY(szDir);
  2100. FREE_MEMORY(szTakeownFile);
  2101. FREE_MEMORY(szTmpFileName);
  2102. return( FALSE );
  2103. }
  2104. }
  2105. ShowMessage( stdout, _X(szTmpFileName) );
  2106. }
  2107. }
  2108. bFlag = FindNextFile( hHandle, &FindFileData );
  2109. while( TRUE == bFlag )
  2110. {
  2111. //lstrcpy( szFileName, FindFileData.cFileName );
  2112. StringCopy( szFileName, FindFileData.cFileName, SIZE_OF_ARRAY(szFileName) );
  2113. //if ( ( 0 != lstrcmp( szFileName, DOT ) ) &&
  2114. // ( 0 != lstrcmp( szFileName, DOTS ) ) )
  2115. if ( ( 0 != StringCompare( szFileName, DOT, TRUE, 0 ) ) &&
  2116. ( 0 != StringCompare( szFileName, DOTS, TRUE, 0 ) ) )
  2117. {
  2118. (*dwFileCount)= (*dwFileCount) +1;
  2119. if(FALSE == bCurrDirTakeOwnAllFiles && FALSE == bDriveCurrDirTakeOwnAllFiles)
  2120. {
  2121. hr = StringCchPrintf(szTakeownFile, (GetBufferSize(szTakeownFile) / sizeof(WCHAR)), L"%s%s", lpszFileName,szFileName);
  2122. if(FAILED(hr))
  2123. {
  2124. SetLastError(HRESULT_CODE(hr));
  2125. SaveLastError();
  2126. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2127. FREE_MEMORY(szDir);
  2128. FREE_MEMORY(szTakeownFile);
  2129. FREE_MEMORY(szTmpFileName);
  2130. return FALSE;
  2131. }
  2132. }
  2133. else
  2134. if(TRUE == bCurrDirTakeOwnAllFiles)
  2135. {
  2136. StringCopy( szTakeownFile, szFileName, (GetBufferSize(szTakeownFile) / sizeof(WCHAR)) );
  2137. }
  2138. else
  2139. {
  2140. if( *(lpszFileName + StringLengthW(lpszFileName, 0) - 1) != L'\\' )
  2141. {
  2142. hr = StringCchPrintf(szTakeownFile, (GetBufferSize(szTakeownFile) / sizeof(WCHAR)), L"%s\\%s", lpszFileName,szFileName);
  2143. }
  2144. else
  2145. {
  2146. hr = StringCchPrintf(szTakeownFile, (GetBufferSize(szTakeownFile) / sizeof(WCHAR)), L"%s%s", lpszFileName,szFileName);
  2147. }
  2148. if(FAILED(hr))
  2149. {
  2150. SetLastError(HRESULT_CODE(hr));
  2151. SaveLastError();
  2152. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2153. FREE_MEMORY(szDir);
  2154. FREE_MEMORY(szTakeownFile);
  2155. FREE_MEMORY(szTmpFileName);
  2156. return FALSE;
  2157. }
  2158. }
  2159. if(TRUE == bAdminsOwner)
  2160. {
  2161. bFlag = TakeOwnerShip( szTakeownFile);
  2162. }
  2163. else
  2164. {
  2165. bFlag = TakeOwnerShipIndividual(szTakeownFile);
  2166. }
  2167. if( FALSE == bFlag )
  2168. {
  2169. if( ( GetLastError() == ERROR_NOT_ALL_ASSIGNED ))
  2170. {
  2171. hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), GetResString(IDS_NOT_OWNERSHIP_INFO), szTakeownFile);
  2172. if(FAILED(hr))
  2173. {
  2174. SetLastError(HRESULT_CODE(hr));
  2175. SaveLastError();
  2176. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2177. FREE_MEMORY(szDir);
  2178. FREE_MEMORY(szTakeownFile);
  2179. FREE_MEMORY(szTmpFileName);
  2180. return( FALSE );
  2181. }
  2182. ShowMessage(stdout, szTmpFileName);
  2183. }
  2184. else if(ERROR_SHARING_VIOLATION == GetLastError())
  2185. {
  2186. hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), GetResString(IDS_SHARING_VIOLATION_INFO), szTakeownFile);
  2187. if(FAILED(hr))
  2188. {
  2189. SetLastError(HRESULT_CODE(hr));
  2190. SaveLastError();
  2191. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2192. FREE_MEMORY(szDir);
  2193. FREE_MEMORY(szTakeownFile);
  2194. FREE_MEMORY(szTmpFileName);
  2195. return( FALSE );
  2196. }
  2197. ShowMessage(stdout, szTmpFileName);
  2198. }
  2199. else
  2200. {
  2201. if( ( ERROR_BAD_NET_NAME == GetLastError() ) ||
  2202. ( ERROR_BAD_NETPATH == GetLastError() ) ||
  2203. ( ERROR_INVALID_NAME == GetLastError() ) )
  2204. {
  2205. SetLastError( ERROR_FILE_NOT_FOUND );
  2206. SaveLastError();
  2207. }
  2208. wszFormedMessage = (LPWSTR)AllocateMemory((StringLengthW(szTakeownFile, 0) + MAX_STRING_LENGTH) * sizeof(WCHAR));
  2209. if ( wszFormedMessage == NULL )
  2210. {
  2211. SaveLastError();
  2212. FREE_MEMORY(szDir);
  2213. FREE_MEMORY(szTakeownFile);
  2214. FREE_MEMORY(szTmpFileName);
  2215. return( FALSE );
  2216. }
  2217. ShowMessage( stdout, L"\n" );
  2218. ShowMessage( stdout, TAG_INFORMATION );
  2219. ShowMessage( stdout, SPACE_CHAR );
  2220. StringCopy( wszTempMessage, GetReason(), SIZE_OF_ARRAY(wszTempMessage) );
  2221. lpNextTok = _tcstok(wszTempMessage, L".");
  2222. ShowMessage(stdout,wszTempMessage);
  2223. //hr = StringCchPrintf(wszFormedMessage, (GetBufferSize(wszFormedMessage) / sizeof(WCHAR)), GetResString(IDS_ON_FILE_FOLDER), szTakeownFile);
  2224. hr = StringCchPrintf(wszFormedMessage, (GetBufferSize(wszFormedMessage) / sizeof(WCHAR)), L". ( \"%s\" )\n", szTakeownFile);
  2225. if(FAILED(hr))
  2226. {
  2227. SetLastError(HRESULT_CODE(hr));
  2228. SaveLastError();
  2229. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2230. FREE_MEMORY(szDir);
  2231. FREE_MEMORY(szTakeownFile);
  2232. FREE_MEMORY(szTmpFileName);
  2233. return( FALSE );
  2234. }
  2235. ShowMessage(stdout, wszFormedMessage);
  2236. FREE_MEMORY(wszFormedMessage);
  2237. }
  2238. }
  2239. else
  2240. {
  2241. if(TRUE == bAdminsOwner)
  2242. {
  2243. hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL, szTakeownFile);
  2244. if(FAILED(hr))
  2245. {
  2246. SetLastError(HRESULT_CODE(hr));
  2247. SaveLastError();
  2248. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2249. FREE_MEMORY(szDir);
  2250. FREE_MEMORY(szTakeownFile);
  2251. FREE_MEMORY(szTmpFileName);
  2252. return( FALSE );
  2253. }
  2254. }
  2255. else
  2256. {
  2257. //hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL_USER, szOwnerString, szTakeownFile);
  2258. hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL_USER, szTakeownFile, szOwnerString);
  2259. if(FAILED(hr))
  2260. {
  2261. SetLastError(HRESULT_CODE(hr));
  2262. SaveLastError();
  2263. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2264. FREE_MEMORY(szDir);
  2265. FREE_MEMORY(szTakeownFile);
  2266. FREE_MEMORY(szTmpFileName);
  2267. return( FALSE );
  2268. }
  2269. }
  2270. ShowMessage( stdout, _X(szTmpFileName) );
  2271. }
  2272. }
  2273. bFlag = FindNextFile( hHandle, &FindFileData );
  2274. }
  2275. }
  2276. else
  2277. {
  2278. SaveLastError();
  2279. FREE_MEMORY(szDir);
  2280. FREE_MEMORY(szTakeownFile);
  2281. FREE_MEMORY(szTmpFileName);
  2282. return( FALSE );
  2283. }
  2284. if(0 == *dwFileCount)
  2285. {
  2286. if(FALSE == bMatchPattern)
  2287. {
  2288. ShowMessage( stdout, GetResString(IDS_NO_FILES_AVAILABLE));
  2289. }
  2290. else
  2291. {
  2292. ShowMessage( stdout, GetResString(IDS_NO_PATTERN_FOUND));
  2293. }
  2294. }
  2295. CLOSE_FILE_HANDLE( hHandle ) ;
  2296. FREE_MEMORY(szDir);
  2297. FREE_MEMORY(szTakeownFile);
  2298. FREE_MEMORY(szTmpFileName);
  2299. return( TRUE );
  2300. }
  2301. DWORD
  2302. IsLogonDomainAdmin(IN LPWSTR szOwnerString,
  2303. OUT PBOOL pbLogonDomainAdmin)
  2304. /*++
  2305. Routine Description:
  2306. This function Checks whether the logged on user is domain administrator or not
  2307. Arguments:
  2308. [ IN ] szOwnerString : The logged on user
  2309. [ OUT ] pbLogonDomainAdmin: Indicates whether Domain admin or not
  2310. Return Value:
  2311. EXIT_FAIL : On failure
  2312. EXIT_SUCC : On success
  2313. --*/
  2314. {
  2315. WCHAR szSystemName[MAX_SYSTEMNAME] = NULL_U_STRING;
  2316. WCHAR szOwnerStringTemp[(2 * MAX_STRING_LENGTH) + 5] = NULL_U_STRING;
  2317. DWORD dwMemory = MAX_SYSTEMNAME;
  2318. LPWSTR szToken = NULL;
  2319. SecureZeroMemory(szSystemName, MAX_SYSTEMNAME * sizeof(WCHAR));
  2320. SecureZeroMemory(szOwnerStringTemp, ((2 * MAX_STRING_LENGTH) + 5) * sizeof(WCHAR));
  2321. StringCopy( szOwnerStringTemp, szOwnerString, SIZE_OF_ARRAY(szOwnerStringTemp) );
  2322. if( 0 == GetComputerName(szSystemName,&dwMemory))
  2323. {
  2324. //DisplayErrorMsg(GetLastError());
  2325. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  2326. return EXIT_FAIL;
  2327. }
  2328. szToken = wcstok(szOwnerStringTemp,L"\\");
  2329. if(NULL == szToken )
  2330. {
  2331. //DisplayErrorMsg(GetLastError());
  2332. SetLastError(IDS_INVALID_USERNAME);
  2333. SaveLastError();
  2334. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2335. return EXIT_FAIL;
  2336. }
  2337. //if(lstrcmpi(szSystemName,szToken) == 0 )
  2338. if(StringCompare( szSystemName, szToken, TRUE, 0 ) == 0 )
  2339. {
  2340. *pbLogonDomainAdmin = FALSE;
  2341. }
  2342. else
  2343. {
  2344. *pbLogonDomainAdmin = TRUE;
  2345. }
  2346. return EXIT_SUCC;
  2347. }
  2348. BOOL
  2349. TakeOwnerShipRecursive(IN LPWSTR lpszFileName,
  2350. IN BOOL bCurrDirTakeOwnAllFiles,
  2351. IN BOOL bAdminsOwner,
  2352. IN LPWSTR szOwnerString,
  2353. IN BOOL bTakeOwnAllFiles,
  2354. IN BOOL bDriveCurrDirTakeOwnAllFiles,
  2355. IN BOOL bMatchPattern,
  2356. IN LPWSTR wszPatternString,
  2357. IN LPWSTR szConfirm)
  2358. /*++
  2359. Routine Description:
  2360. This function gives ownership recursively to all the files in the path specified
  2361. Arguments:
  2362. [ IN ] lpszFileName : The path to search the files recursively
  2363. [ IN ] bCurrDirTakeOwnAllFiles: Indicates current directory files or not
  2364. [ IN ] bAdminsOwner : Indicates whether to give ownership to the administrators group
  2365. [ IN ] dwUserName : Logged on user name
  2366. [ IN ] szOwnerString : Logged on user name in Sam Compatible format
  2367. [ IN ] bLogonDomainAdmin : Indicates whether the logged on user is domain administrator or not
  2368. Return Value:
  2369. EXIT_FAILURE : On failure
  2370. EXIT_SUCCESS : On success
  2371. --*/
  2372. {
  2373. DWORD dwRet = 0;
  2374. DWORD dwSize = MAX_RES_STRING;
  2375. BOOL bFlag = FALSE;
  2376. //WCHAR szDir[2*MAX_STRING_LENGTH] = NULL_U_STRING;
  2377. LPWSTR szDir = NULL;
  2378. LPWSTR szTempDirectory = NULL;
  2379. DWORD dwRetval = 1;
  2380. DWORD dwAttr = 1;
  2381. HRESULT hr;
  2382. BOOL bFilesNone = TRUE;
  2383. if(FALSE == bCurrDirTakeOwnAllFiles )
  2384. {
  2385. dwAttr = GetFileAttributes(lpszFileName);
  2386. if(0xffffffff == dwAttr)
  2387. {
  2388. if(ERROR_SHARING_VIOLATION == GetLastError())
  2389. {
  2390. ShowMessage(stderr,GetResString(IDS_INVALID_DIRECTORY));
  2391. }
  2392. else
  2393. {
  2394. //DisplayErrorMsg(GetLastError());
  2395. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  2396. }
  2397. return EXIT_FAILURE;
  2398. }
  2399. if( !(dwAttr&FILE_ATTRIBUTE_DIRECTORY) )
  2400. {
  2401. ShowMessage(stderr,GetResString(IDS_INVALID_DIRECTORY));
  2402. return EXIT_FAILURE;
  2403. }
  2404. szDir = (LPWSTR)AllocateMemory((StringLengthW(lpszFileName, 0) + BOUNDARYVALUE) * sizeof(WCHAR));
  2405. if(NULL == szDir)
  2406. {
  2407. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2408. return EXIT_FAILURE;
  2409. }
  2410. StringCopy( szDir, lpszFileName, GetBufferSize(szDir) / sizeof(WCHAR) );
  2411. }
  2412. else //else if(TRUE == bCurrDirTakeOwnAllFiles)
  2413. {
  2414. szDir = (LPWSTR)AllocateMemory((MAX_PATH + BOUNDARYVALUE) * sizeof(WCHAR));
  2415. if(NULL == szDir)
  2416. {
  2417. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2418. return EXIT_FAILURE;
  2419. }
  2420. dwRet = GetCurrentDirectory( dwSize, szDir );
  2421. if( 0 == dwRet )
  2422. {
  2423. //DisplayErrorMsg(GetLastError());
  2424. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  2425. FREE_MEMORY(szDir);
  2426. return EXIT_FAILURE;
  2427. }
  2428. }
  2429. if(FALSE == bCurrDirTakeOwnAllFiles && FALSE == bTakeOwnAllFiles &&
  2430. FALSE == bDriveCurrDirTakeOwnAllFiles && FALSE == bMatchPattern)
  2431. {
  2432. szTempDirectory = (LPWSTR)AllocateMemory((StringLengthW(szDir, 0) + (2 * MAX_STRING_LENGTH)) * sizeof(WCHAR));
  2433. if(NULL == szTempDirectory)
  2434. {
  2435. //DisplayErrorMsg(GetLastError());
  2436. FREE_MEMORY(szDir);
  2437. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2438. return EXIT_FAILURE;
  2439. }
  2440. if(TRUE == bAdminsOwner)
  2441. {
  2442. //bFlag = TakeOwnerShip( lpszFileName);
  2443. bFlag = TakeOwnerShip( szDir);
  2444. }
  2445. else
  2446. {
  2447. bFlag = TakeOwnerShipIndividual(szDir);
  2448. }
  2449. if( FALSE == bFlag )
  2450. {
  2451. if( ERROR_NOT_ALL_ASSIGNED == GetLastError()) // || (ERROR_INVALID_HANDLE == GetLastError()))
  2452. {
  2453. hr = StringCchPrintf(szTempDirectory, (GetBufferSize(szTempDirectory) / sizeof(WCHAR)), GetResString(IDS_NOT_OWNERSHIP_ERROR) , szDir);
  2454. if(FAILED(hr))
  2455. {
  2456. SetLastError(HRESULT_CODE(hr));
  2457. SaveLastError();
  2458. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2459. FREE_MEMORY(szTempDirectory);
  2460. FREE_MEMORY(szDir);
  2461. return( EXIT_FAILURE );
  2462. }
  2463. ShowMessage(stderr, szTempDirectory);
  2464. FREE_MEMORY( szTempDirectory ) ;
  2465. return EXIT_FAILURE;
  2466. }
  2467. else if(ERROR_SHARING_VIOLATION == GetLastError())
  2468. {
  2469. hr = StringCchPrintf(szTempDirectory, (GetBufferSize(szTempDirectory) / sizeof(WCHAR)), GetResString(IDS_SHARING_VIOLATION_ERROR) , szDir);
  2470. if(FAILED(hr))
  2471. {
  2472. SetLastError(HRESULT_CODE(hr));
  2473. SaveLastError();
  2474. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2475. FREE_MEMORY(szTempDirectory);
  2476. FREE_MEMORY(szDir);
  2477. return( EXIT_FAILURE );
  2478. }
  2479. ShowMessage(stderr, szTempDirectory);
  2480. FREE_MEMORY( szTempDirectory ) ;
  2481. FREE_MEMORY(szDir);
  2482. return EXIT_FAILURE;
  2483. }
  2484. else
  2485. {
  2486. ShowMessage( stderr, ERROR_STRING );
  2487. ShowMessage( stderr, SPACE_CHAR );
  2488. ShowMessage( stderr, GetReason() );
  2489. FREE_MEMORY( szTempDirectory ) ;
  2490. FREE_MEMORY(szDir);
  2491. return EXIT_FAILURE;
  2492. }
  2493. }
  2494. else
  2495. {
  2496. if(TRUE == bAdminsOwner)
  2497. {
  2498. hr = StringCchPrintf(szTempDirectory, (GetBufferSize(szTempDirectory) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL, szDir);
  2499. if(FAILED(hr))
  2500. {
  2501. SetLastError(HRESULT_CODE(hr));
  2502. SaveLastError();
  2503. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2504. FREE_MEMORY( szTempDirectory ) ;
  2505. FREE_MEMORY(szDir);
  2506. return EXIT_FAILURE;
  2507. }
  2508. }
  2509. else
  2510. {
  2511. //hr = StringCchPrintf(szTempDirectory, (GetBufferSize(szTempDirectory) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL_USER, szOwnerString, szDir );
  2512. hr = StringCchPrintf(szTempDirectory, (GetBufferSize(szTempDirectory) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL_USER, szDir, szOwnerString);
  2513. if(FAILED(hr))
  2514. {
  2515. SetLastError(HRESULT_CODE(hr));
  2516. SaveLastError();
  2517. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2518. FREE_MEMORY( szTempDirectory ) ;
  2519. FREE_MEMORY(szDir);
  2520. return EXIT_FAILURE;
  2521. }
  2522. }
  2523. ShowMessage( stdout, _X(szTempDirectory) );
  2524. FREE_MEMORY( szTempDirectory ) ;
  2525. }
  2526. }
  2527. if( TRUE == Push( szDir ) )
  2528. { // Push the current directory .
  2529. dwRetval = GetMatchedFiles( bAdminsOwner,szOwnerString, bMatchPattern, wszPatternString, &bFilesNone, szConfirm );
  2530. if((TRUE == dwRetval) || (EXIT_CANCELED == dwRetval) )
  2531. {
  2532. if(TRUE == bFilesNone)
  2533. {
  2534. if((TRUE == bCurrDirTakeOwnAllFiles || TRUE == bTakeOwnAllFiles ||
  2535. TRUE == bDriveCurrDirTakeOwnAllFiles) && FALSE == bMatchPattern)
  2536. {
  2537. ShowMessage(stdout, GetResString(IDS_NO_FILES_AVAILABLE));
  2538. }
  2539. else
  2540. if((TRUE == bCurrDirTakeOwnAllFiles || TRUE == bTakeOwnAllFiles ||
  2541. TRUE == bDriveCurrDirTakeOwnAllFiles) && TRUE == bMatchPattern)
  2542. {
  2543. ShowMessage(stdout, GetResString(IDS_NO_PATTERN_FOUND));
  2544. }
  2545. FREE_MEMORY(szDir);
  2546. return EXIT_SUCCESS ;
  2547. }
  2548. else
  2549. {
  2550. //FREE_MEMORY( szTempDirectory ) ;
  2551. FREE_MEMORY(szDir);
  2552. return EXIT_SUCCESS ;
  2553. }
  2554. }
  2555. else
  2556. {
  2557. //FREE_MEMORY( szTempDirectory ) ;
  2558. FREE_MEMORY(szDir);
  2559. return EXIT_FAILURE;
  2560. }
  2561. }
  2562. else
  2563. {
  2564. //FREE_MEMORY( szTempDirectory ) ;
  2565. FREE_MEMORY(szDir);
  2566. return EXIT_FAILURE;
  2567. }
  2568. }
  2569. DWORD GetMatchedFiles(IN BOOL bAdminsOwner,
  2570. IN LPWSTR szOwnerString,
  2571. IN BOOL bMatchPattern,
  2572. IN LPWSTR wszPatternString,
  2573. IN OUT PBOOL pbFilesNone,
  2574. IN LPWSTR szConfirm)
  2575. /*++
  2576. Routine Description:
  2577. This function takes care of getting the files and giving the ownership
  2578. Arguments:
  2579. [ IN ] bAdminsOwner : Indicates whether to give ownership to the administrators group
  2580. [ IN ] szOwnerString : Logged on user name in Sam Compatible format
  2581. [ IN ] bLogonDomainAdmin : Indicates whether the logged on user is domain administrator or not
  2582. [ IN ] bMatchPattern : Whether pattern matching exists
  2583. [ IN ] wszPatternString : The pattern string used for pattern match
  2584. [ IN ] pbFilesNone : Whether there are any files existing or not
  2585. Return Value:
  2586. EXIT_FAILURE : On failure
  2587. EXIT_SUCCESS : On success
  2588. --*/
  2589. {
  2590. //LPWSTR lpszSlashAvailLast = NULL;
  2591. BOOL bACLChgPermGranted = FALSE;
  2592. DWORD dwRetval = 1;
  2593. LPWSTR lpszTempPathName = NULL;
  2594. DWORD dwMem = 0;
  2595. //ASSIGN_MEMORY( g_lpszFileToSearch , TCHAR , MAX_STRING_LENGTH ) ;
  2596. g_lpszFileToSearch = (LPTSTR)AllocateMemory((MAX_STRING_LENGTH) * sizeof(WCHAR));
  2597. if( NULL == g_lpszFileToSearch )
  2598. { // Memory allocation failed .
  2599. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  2600. return FALSE ;
  2601. }
  2602. lpszTempPathName = (LPWSTR)AllocateMemory((MAX_STRING_LENGTH) * sizeof(WCHAR));
  2603. if( NULL == lpszTempPathName )
  2604. { // Memory allocation failed .
  2605. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  2606. FREE_MEMORY( g_lpszFileToSearch ) ;
  2607. return FALSE ;
  2608. }
  2609. // Loop until data strycture( stack) has no item left in it .
  2610. while( NULL != g_pPathName )
  2611. {
  2612. if( FALSE == Pop( ) )
  2613. { // Control should come here only when linkedlist have no node to POP .
  2614. FREE_MEMORY( g_lpszFileToSearch ) ; // Error message is already displayed .
  2615. FREE_MEMORY( g_lpszFileToSearch ) ;
  2616. return FALSE ;
  2617. }
  2618. dwMem = (StringLength(g_lpszFileToSearch, 0) + EXTRA_MEM ) * sizeof(WCHAR);
  2619. if((DWORD)GetBufferSize(lpszTempPathName) < (dwMem))
  2620. {
  2621. if(FALSE == ReallocateMemory((LPVOID*)&lpszTempPathName,( dwMem) ))
  2622. {
  2623. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2624. FREE_MEMORY(lpszTempPathName);
  2625. FREE_MEMORY( g_lpszFileToSearch ) ;
  2626. return FALSE ;
  2627. }
  2628. else
  2629. if(NULL == lpszTempPathName)
  2630. {
  2631. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2632. FREE_MEMORY(lpszTempPathName);
  2633. FREE_MEMORY( g_lpszFileToSearch ) ;
  2634. return FALSE ;
  2635. }
  2636. }
  2637. StringCopy( lpszTempPathName, g_lpszFileToSearch, GetBufferSize(lpszTempPathName) / sizeof(WCHAR) );
  2638. if( *(g_lpszFileToSearch+StringLengthW(g_lpszFileToSearch, 0) - 1) != L'\\' )
  2639. {
  2640. StringConcat(g_lpszFileToSearch, _T( "\\" ), GetBufferSize(g_lpszFileToSearch)/sizeof(TCHAR));
  2641. }
  2642. StringConcat(g_lpszFileToSearch, _T( "*.*" ), GetBufferSize(g_lpszFileToSearch)/sizeof(TCHAR));
  2643. /*Store all the subdirectories in the stack*/
  2644. dwRetval = StoreSubDirectory( lpszTempPathName, &bACLChgPermGranted, szOwnerString, bMatchPattern,
  2645. wszPatternString, szConfirm, bAdminsOwner ) ;
  2646. if(FAILURE == dwRetval )
  2647. {
  2648. FREE_MEMORY( g_lpszFileToSearch ) ;
  2649. FREE_MEMORY(lpszTempPathName);
  2650. return FALSE ;
  2651. }
  2652. else if( EXIT_CANCELED == dwRetval )
  2653. {
  2654. FREE_MEMORY( g_lpszFileToSearch ) ;
  2655. FREE_MEMORY(lpszTempPathName);
  2656. return EXIT_CANCELED ;
  2657. }
  2658. /*Get the ownership for the files or directories in the current folder only*/
  2659. if( FALSE == GetOwnershipForFiles(lpszTempPathName, bAdminsOwner, szOwnerString,
  2660. bMatchPattern, wszPatternString, pbFilesNone))
  2661. {
  2662. FREE_MEMORY( g_lpszFileToSearch ) ;
  2663. FREE_MEMORY(lpszTempPathName);
  2664. return FALSE ;
  2665. }
  2666. }
  2667. FREE_MEMORY( g_lpszFileToSearch ) ;
  2668. FREE_MEMORY(lpszTempPathName);
  2669. return TRUE;
  2670. }
  2671. DWORD
  2672. StoreSubDirectory(IN LPTSTR lpszPathName,
  2673. IN PBOOL pbACLChgPermGranted,
  2674. IN LPWSTR szOwnerString,
  2675. IN BOOL bMatchPattern,
  2676. IN LPWSTR wszPatternString,
  2677. IN LPWSTR szConfirm,
  2678. IN BOOL bAdminsOwner)
  2679. /*++
  2680. Routine Description:
  2681. Find and store subdirectories present in a directory matching criteria
  2682. file was created between specified date or not .
  2683. Arguments:
  2684. [ IN ] lpszPathName : Contains path of a directory from where files matching
  2685. a criteria are to be displayed .
  2686. [ IN ] pbACLChgPermGranted : To check whether ACLs has to be changed to give full permission.
  2687. [ IN ] szOwnerString : Logged on user name in Sam Compatible format.
  2688. [ IN ] bLogonDomainAdmin : Indicates whether the logged on user is domain administrator or not.
  2689. [ IN ] bMatchPattern : Whether pattern matching is supported or not
  2690. [ IN ] wszPatternString : The pattern string used for pattern matching
  2691. Return value:
  2692. SUCCESS : On Success
  2693. FAILURE : On Failure
  2694. EXIT_CANCELED : On Exiting Immediately
  2695. --*/
  2696. {
  2697. HANDLE hFindFile ;
  2698. WIN32_FIND_DATA wfdFindFile ;
  2699. HANDLE hInput = 0;// Stores the input handle device
  2700. DWORD dwBytesRead = 0;// Stores number of byes read from console
  2701. DWORD dwMode = 0;// Stores mode for input device
  2702. BOOL bSuccess = FALSE; // Stores return value
  2703. WCHAR chTmp[MAX_RES_STRING] ;
  2704. WCHAR ch = NULL_U_CHAR;
  2705. LPWSTR lpszDispMsg = NULL;
  2706. LPWSTR lpszDispMsg2 = NULL;
  2707. DWORD dwCount = 0;
  2708. DWORD dwMem = 0;
  2709. HRESULT hr;
  2710. BOOL bNTFSFileSystem = FALSE;
  2711. BOOL bIndirectionInputWithZeroLength = FALSE;
  2712. BOOL bIndirectionInput = FALSE;
  2713. //WCHAR szTemp[MAX_STRING_LENGTH] = NULL_U_STRING;
  2714. LPWSTR lpszTempStr = NULL;
  2715. SecureZeroMemory(chTmp, MAX_RES_STRING * sizeof( WCHAR ));
  2716. SecureZeroMemory(&wfdFindFile, sizeof( WIN32_FIND_DATA ));
  2717. if( INVALID_HANDLE_VALUE != ( hFindFile = FindFirstFile( g_lpszFileToSearch , &wfdFindFile ) ) )
  2718. {
  2719. do // Loop until files are present in the directory to display .
  2720. {
  2721. // Check again whether obtained handle points to a directory or file .
  2722. // If directory then check whether files in subdir are to be displayed .
  2723. if( 0 != ( wfdFindFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) )
  2724. {
  2725. // Is single dot "." present or Is double dot ".." present .
  2726. if( ( 0 != _tcsicmp( wfdFindFile.cFileName , DOT ) ) &&
  2727. ( 0 != _tcsicmp( wfdFindFile.cFileName , DOTS ) ) )
  2728. {
  2729. dwMem = ( StringLengthW( lpszPathName, 0 ) + StringLengthW( wfdFindFile.cFileName, 0 ) + EXTRA_MEM );
  2730. // Reallocate memory .
  2731. if(((DWORD)GetBufferSize(g_lpszFileToSearch)) < (dwMem * sizeof(WCHAR)))
  2732. {
  2733. if(FALSE == ReallocateMemory((LPVOID*)&g_lpszFileToSearch,( dwMem) * sizeof(WCHAR) ))
  2734. {
  2735. //ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC));
  2736. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2737. CLOSE_FILE_HANDLE( hFindFile ) ;
  2738. return FAILURE ;
  2739. }
  2740. else
  2741. if(NULL == g_lpszFileToSearch)
  2742. {
  2743. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2744. CLOSE_FILE_HANDLE( hFindFile ) ;
  2745. return FAILURE ;
  2746. }
  2747. }
  2748. StringCopy( g_lpszFileToSearch, lpszPathName, (GetBufferSize(g_lpszFileToSearch) / sizeof(TCHAR)) );
  2749. //if((NULL != g_lpszFileToSearch) &&(*(g_lpszFileToSearch+lstrlen(g_lpszFileToSearch)-1)) != L'\\' )
  2750. if((NULL != g_lpszFileToSearch) &&(*(g_lpszFileToSearch + StringLengthW(g_lpszFileToSearch, 0) - 1)) != L'\\' )
  2751. {
  2752. StringConcat(g_lpszFileToSearch, L"\\", (GetBufferSize(g_lpszFileToSearch) / sizeof(TCHAR)));
  2753. }
  2754. StringConcat(g_lpszFileToSearch, wfdFindFile.cFileName, (GetBufferSize(g_lpszFileToSearch) / sizeof(TCHAR)));
  2755. if(EXIT_SUCCESS == IsNTFSFileSystem2(g_lpszFileToSearch, &bNTFSFileSystem))
  2756. {
  2757. if(FALSE == bNTFSFileSystem)
  2758. {
  2759. continue;
  2760. }
  2761. }
  2762. lpszTempStr = (LPWSTR)AllocateMemory((StringLengthW(wfdFindFile.cFileName, 0) + 10) * sizeof(WCHAR));
  2763. if(NULL == lpszTempStr)
  2764. {
  2765. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2766. CLOSE_FILE_HANDLE( hFindFile ) ;
  2767. return FAILURE ;
  2768. }
  2769. if( FindString( wszPatternString, L".",0) != NULL && (FindString(wfdFindFile.cFileName, L".",0)== NULL) )
  2770. {
  2771. StringCopy( lpszTempStr, wfdFindFile.cFileName, GetBufferSize(lpszTempStr) / sizeof(WCHAR) );
  2772. StringConcat( lpszTempStr, L".", GetBufferSize(lpszTempStr) / sizeof(WCHAR) );
  2773. }
  2774. else
  2775. {
  2776. StringCopy( lpszTempStr, wfdFindFile.cFileName, GetBufferSize(lpszTempStr) / sizeof(WCHAR) );
  2777. }
  2778. if((FALSE == bMatchPattern) || ((TRUE == bMatchPattern) && (TRUE == MatchPattern(wszPatternString, lpszTempStr))))
  2779. {
  2780. if( (FALSE == TakeOwnerShipIndividual(g_lpszFileToSearch)))
  2781. {
  2782. FREE_MEMORY(lpszTempStr);
  2783. continue;
  2784. }
  2785. else
  2786. {
  2787. // Copy current path name and store it .
  2788. if( FALSE == Push( g_lpszFileToSearch ) )
  2789. { // Control comes here when memory allocation fails .
  2790. CLOSE_FILE_HANDLE( hFindFile ) ;
  2791. FREE_MEMORY(lpszTempStr);
  2792. return FAILURE ;
  2793. } // Push Is Over .
  2794. FREE_MEMORY(lpszTempStr);
  2795. }
  2796. }
  2797. else
  2798. {
  2799. if( FALSE == Push( g_lpszFileToSearch ) )
  2800. { // Control comes here when memory allocation fails .
  2801. CLOSE_FILE_HANDLE( hFindFile ) ;
  2802. FREE_MEMORY(lpszTempStr);
  2803. return FAILURE ;
  2804. } // Push I
  2805. FREE_MEMORY(lpszTempStr);
  2806. }
  2807. } // If
  2808. else
  2809. { // If obtained directory is A "." or ".." ,
  2810. continue ;
  2811. }
  2812. }
  2813. }while( 0 != FindNextFile( hFindFile , &wfdFindFile ) ) ; // Continue till no files are present to display.
  2814. }
  2815. else
  2816. {
  2817. if(0 == StringLengthW(szConfirm, 0))
  2818. {
  2819. if(FALSE == *pbACLChgPermGranted) // Check whether permission for change of ACL is already granted
  2820. {
  2821. lpszDispMsg = (LPWSTR)AllocateMemory((StringLengthW( lpszPathName, 0 ) + MAX_STRING_LENGTH) * sizeof(WCHAR));
  2822. if( NULL == lpszDispMsg ) // Check is memory allocation is successful or not .
  2823. { // Memory allocation is not successful .
  2824. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  2825. return FAILURE ;
  2826. }
  2827. lpszDispMsg2 = (LPWSTR)AllocateMemory((StringLengthW( lpszPathName, 0 ) + MAX_STRING_LENGTH) * sizeof(WCHAR));
  2828. if( NULL == lpszDispMsg2 ) // Check is memory allocation is successful or not .
  2829. { // Memory allocation is not successful .
  2830. FREE_MEMORY(lpszDispMsg);
  2831. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  2832. return FAILURE ;
  2833. }
  2834. hr = StringCchPrintf(lpszDispMsg, (GetBufferSize(lpszDispMsg) / sizeof(WCHAR)), GIVE_FULL_PERMISSIONS, lpszPathName );
  2835. if(FAILED(hr))
  2836. {
  2837. SetLastError(HRESULT_CODE(hr));
  2838. SaveLastError();
  2839. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2840. FREE_MEMORY(lpszDispMsg);
  2841. return FAILURE;
  2842. }
  2843. hr = StringCchPrintf(lpszDispMsg2, (GetBufferSize(lpszDispMsg2) / sizeof(WCHAR)), GIVE_FULL_PERMISSIONS2, lpszPathName );
  2844. if(FAILED(hr))
  2845. {
  2846. SetLastError(HRESULT_CODE(hr));
  2847. SaveLastError();
  2848. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2849. FREE_MEMORY(lpszDispMsg);
  2850. return FAILURE;
  2851. }
  2852. do
  2853. {
  2854. if(FALSE == bIndirectionInputWithZeroLength)
  2855. {
  2856. if(0 == dwCount)//Give a message asking the user for change of ACLs
  2857. {
  2858. ShowMessage(stdout,_X(lpszDispMsg));
  2859. ShowMessage(stdout,_X(lpszDispMsg2));
  2860. }
  2861. else
  2862. {
  2863. ShowMessage(stdout,L"\n\n");
  2864. ShowMessage(stdout,_X(lpszDispMsg2));
  2865. }
  2866. }
  2867. dwCount+= 1;
  2868. hInput = GetStdHandle( STD_INPUT_HANDLE );
  2869. if( INVALID_HANDLE_VALUE == hInput)
  2870. {
  2871. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  2872. // Set Reason for error in memory
  2873. return FAILURE;
  2874. }
  2875. // Get console mode, so we can change the input mode
  2876. bSuccess = GetConsoleMode( hInput, &dwMode );
  2877. if ( TRUE == bSuccess)
  2878. {
  2879. // turn off line input and echo
  2880. dwMode &= ~( ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT );
  2881. bSuccess = SetConsoleMode( hInput, dwMode );
  2882. if (FALSE == bSuccess)
  2883. {
  2884. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  2885. // Set Reason for error in memory
  2886. return FAILURE;
  2887. }
  2888. // Flush the buffer initially
  2889. if ( FlushConsoleInputBuffer( hInput ) == FALSE )
  2890. {
  2891. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  2892. // Set Reason for error in memory
  2893. return FAILURE;
  2894. }
  2895. }
  2896. if ( ReadFile(hInput, &ch, 1, &dwBytesRead, NULL) == FALSE )
  2897. {
  2898. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  2899. return FAILURE;
  2900. }
  2901. if( ( hInput != (HANDLE)0x0000000F )&&( hInput != (HANDLE)0x00000003 ) && ( hInput != INVALID_HANDLE_VALUE ) )
  2902. {
  2903. bIndirectionInput = TRUE;
  2904. }
  2905. hr = StringCchPrintf(chTmp, (SIZE_OF_ARRAY(chTmp)), L"%c" , ch );
  2906. if(FAILED(hr))
  2907. {
  2908. return FAILURE;
  2909. }
  2910. if( TRUE == bIndirectionInput)
  2911. {
  2912. if( chTmp[0] == L'\n' || chTmp[0] == L'\r'|| chTmp[0] == L'\t')
  2913. {
  2914. bIndirectionInputWithZeroLength = TRUE;
  2915. continue;
  2916. }
  2917. else
  2918. {
  2919. bIndirectionInputWithZeroLength = FALSE;
  2920. }
  2921. }
  2922. ShowMessage(stdout,_X(chTmp));
  2923. }while(0 != dwBytesRead && !(((StringCompare( chTmp, LOWER_YES, TRUE, 0 ) == 0) || (StringCompare( chTmp, LOWER_NO, TRUE, 0 ) == 0) || (StringCompare( chTmp, LOWER_CANCEL, TRUE, 0 ) == 0))));
  2924. FREE_MEMORY(lpszDispMsg);
  2925. }
  2926. else
  2927. {
  2928. StringCopy( chTmp, LOWER_YES, SIZE_OF_ARRAY(chTmp) );
  2929. dwBytesRead =2;
  2930. }
  2931. }
  2932. else
  2933. {
  2934. StringCopy( chTmp, szConfirm, SIZE_OF_ARRAY(chTmp) );
  2935. dwBytesRead =2;
  2936. }
  2937. if(0 != dwBytesRead && (StringCompare( chTmp, LOWER_YES, TRUE, 0 ) == 0) )
  2938. {
  2939. *pbACLChgPermGranted = TRUE;
  2940. /*if Permission for granting ACLS are obtained , then give the full permission*/
  2941. if(TRUE == AddAccessRights(lpszPathName, 0xF0FFFFFF, szOwnerString, bAdminsOwner))
  2942. {
  2943. if( INVALID_HANDLE_VALUE != ( hFindFile = FindFirstFile( g_lpszFileToSearch , &wfdFindFile ) ) )
  2944. {
  2945. do // Loop until files are present in the directory to display .
  2946. {
  2947. // Check again whether obtained handle points to a directory or file .
  2948. // If directory then check whether files in subdir are to be displayed .
  2949. if( 0 != ( wfdFindFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) )
  2950. {
  2951. // Is single dot "." present or Is double dot ".." present .
  2952. if( ( 0 != _tcsicmp( wfdFindFile.cFileName , DOT ) ) &&
  2953. ( 0 != _tcsicmp( wfdFindFile.cFileName , DOTS ) ) )
  2954. {
  2955. dwMem = ( StringLengthW( lpszPathName, 0 ) + StringLengthW( wfdFindFile.cFileName, 0 ) + EXTRA_MEM );
  2956. // Reallocate memory .
  2957. if( (DWORD)GetBufferSize(g_lpszFileToSearch) < (dwMem * sizeof(WCHAR)))
  2958. {
  2959. if( FALSE == ReallocateMemory((LPVOID*)&g_lpszFileToSearch,
  2960. ( StringLengthW( lpszPathName, 0 ) + StringLengthW( wfdFindFile.cFileName, 0 ) + EXTRA_MEM ) * sizeof(WCHAR) ))
  2961. {
  2962. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2963. CLOSE_FILE_HANDLE( hFindFile ) ;
  2964. return FAILURE ;
  2965. }
  2966. else
  2967. if(NULL == g_lpszFileToSearch)
  2968. {
  2969. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2970. CLOSE_FILE_HANDLE( hFindFile ) ;
  2971. return FAILURE ;
  2972. }
  2973. }
  2974. StringCopy( g_lpszFileToSearch, lpszPathName, (GetBufferSize(g_lpszFileToSearch) / sizeof(TCHAR)) );
  2975. if((NULL != g_lpszFileToSearch) && (*(g_lpszFileToSearch + StringLengthW(g_lpszFileToSearch, 0) - 1)) != L'\\' )
  2976. {
  2977. StringConcat(g_lpszFileToSearch, L"\\", (GetBufferSize(g_lpszFileToSearch) / sizeof(TCHAR)));
  2978. }
  2979. StringConcat(g_lpszFileToSearch, wfdFindFile.cFileName, GetBufferSize(g_lpszFileToSearch)/sizeof(TCHAR));
  2980. // Copy current path name and store it .
  2981. if( FALSE == Push( g_lpszFileToSearch ) )
  2982. { // Control comes here when memory allocation fails .
  2983. CLOSE_FILE_HANDLE( hFindFile ) ;
  2984. return FAILURE ;
  2985. } // Push Is Over .
  2986. } // If
  2987. else
  2988. { // If obtained directory is A "." or ".." ,
  2989. continue ;
  2990. }
  2991. }
  2992. }while( 0 != FindNextFile( hFindFile , &wfdFindFile ) ) ; // Continue till no files are present to display.
  2993. }
  2994. }
  2995. else
  2996. {
  2997. ShowMessage(stderr, L"\n");
  2998. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2999. return FAILURE;
  3000. }
  3001. }
  3002. else
  3003. if(0 == StringLengthW(szConfirm, 0))
  3004. {
  3005. if(0 != dwBytesRead && (StringCompare( chTmp, LOWER_CANCEL, TRUE, 0 ) == 0) )
  3006. {
  3007. ShowMessage(stdout,L"\n");
  3008. CLOSE_FILE_HANDLE( hFindFile ) ;
  3009. return EXIT_CANCELED;
  3010. }
  3011. else
  3012. {
  3013. ShowMessage(stdout,L"\n");
  3014. }
  3015. }
  3016. }
  3017. CLOSE_FILE_HANDLE( hFindFile ) ;
  3018. return SUCCESS ;
  3019. }
  3020. BOOL GetOwnershipForFiles( IN LPWSTR lpszPathName,
  3021. IN BOOL bAdminsOwner,
  3022. IN LPWSTR szOwnerString,
  3023. IN BOOL bMatchPattern,
  3024. IN LPWSTR wszPatternString,
  3025. IN OUT PBOOL pbFilesNone)
  3026. /*++
  3027. Routine Description:
  3028. This function gives the ownership for the files specified in the path
  3029. Arguments:
  3030. [ IN ] lpszPathName : The path to search the files for giving ownership
  3031. [ IN ] bAdminsOwner : Indicates whether to give ownership to the administrators group
  3032. [ IN ] dwUserName : Logged on user name
  3033. [ IN ] szOwnerString : Logged on user name in Sam Compatible format
  3034. [ IN ] bLogonDomainAdmin : Indicates whether the logged on user is domain administrator or not
  3035. Return Value:
  3036. FALSE : On failure
  3037. TRUE : On success
  3038. --*/
  3039. {
  3040. HANDLE hFindFile = NULL ; // Handle to a file .
  3041. WIN32_FIND_DATA wfdFindFile ; // Structure keeping information about the found file .
  3042. BOOL bTakeOwnerShipError = FALSE;
  3043. BOOL bFlag = TRUE;
  3044. BOOL bNTFSFileSystem = FALSE;
  3045. LPWSTR szTmpFileName = NULL;
  3046. WCHAR wszTempMessage[3*MAX_STRING_LENGTH] ;
  3047. LPWSTR szTemporaryFileName = NULL;
  3048. LPWSTR lpNextTok = NULL;
  3049. LPWSTR wszFormedMessage = NULL;
  3050. long dwMem = 0;
  3051. HRESULT hr;
  3052. LPWSTR lpszTempStr = NULL;
  3053. SecureZeroMemory(wszTempMessage, (3*MAX_STRING_LENGTH) * sizeof(WCHAR));
  3054. szTemporaryFileName = (LPWSTR)AllocateMemory((StringLengthW(lpszPathName, 0) + EXTRA_MEM)* sizeof(WCHAR));
  3055. if(NULL == szTemporaryFileName)
  3056. {
  3057. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  3058. return FALSE ;
  3059. }
  3060. StringCopy( szTemporaryFileName, lpszPathName, GetBufferSize(szTemporaryFileName) / sizeof(WCHAR));
  3061. StringConcat(lpszPathName, L"\\", GetBufferSize(lpszPathName) / sizeof(WCHAR));
  3062. StringConcat(lpszPathName, L"*.*", GetBufferSize(lpszPathName) / sizeof(WCHAR));
  3063. SecureZeroMemory(&wfdFindFile, sizeof( WIN32_FIND_DATA ));
  3064. // From here onwards directory and file information should be displayed .
  3065. if( INVALID_HANDLE_VALUE != ( hFindFile = FindFirstFile( lpszPathName , &wfdFindFile ) ) )
  3066. {
  3067. do // Loop until files are present in the directory to display .
  3068. {
  3069. // Check again whether obtained handle points to a directory or file .
  3070. // If directory then check whether files in subdir are to be displayed .
  3071. if( 0 != ( wfdFindFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) )
  3072. {
  3073. // Is single dot "." present or Is double dot ".." present .
  3074. if( ( 0 == _tcsicmp( wfdFindFile.cFileName , DOT ) ) ||
  3075. ( 0 == _tcsicmp( wfdFindFile.cFileName , DOTS ) ) )
  3076. {
  3077. continue ;
  3078. }
  3079. }
  3080. // Execute a command specified at command prompt .
  3081. // Reallocate memory .
  3082. dwMem = ( StringLengthW( g_lpszFileToSearch, 0) + StringLengthW( wfdFindFile.cFileName, 0 ) + EXTRA_MEM );
  3083. if(((DWORD)GetBufferSize(g_lpszFileToSearch)) < (dwMem * sizeof(WCHAR)))
  3084. {
  3085. if(FALSE == ReallocateMemory((LPVOID*)&g_lpszFileToSearch,
  3086. ( StringLengthW( g_lpszFileToSearch, 0 ) + StringLengthW( wfdFindFile.cFileName, 0 ) + EXTRA_MEM ) * sizeof(WCHAR) ))
  3087. {
  3088. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  3089. CLOSE_FILE_HANDLE( hFindFile ) ;
  3090. FREE_MEMORY(szTemporaryFileName);
  3091. return FALSE ;
  3092. }
  3093. else
  3094. if(NULL == g_lpszFileToSearch)
  3095. {
  3096. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  3097. CLOSE_FILE_HANDLE( hFindFile ) ;
  3098. FREE_MEMORY(szTemporaryFileName);
  3099. return FALSE ;
  3100. }
  3101. }
  3102. StringCopy( g_lpszFileToSearch, szTemporaryFileName, (GetBufferSize(g_lpszFileToSearch) / sizeof(TCHAR)) );
  3103. if((NULL != g_lpszFileToSearch) && (*(g_lpszFileToSearch + StringLengthW(g_lpszFileToSearch, 0) - 1)) != L'\\' )
  3104. {
  3105. StringConcat(g_lpszFileToSearch, L"\\", (GetBufferSize(g_lpszFileToSearch) / sizeof(TCHAR)) );
  3106. }
  3107. StringConcat(g_lpszFileToSearch, wfdFindFile.cFileName, (GetBufferSize(g_lpszFileToSearch) / sizeof(TCHAR)) );
  3108. lpszTempStr = (LPWSTR)AllocateMemory((StringLengthW(wfdFindFile.cFileName, 0) + 10) * sizeof(WCHAR));
  3109. if(NULL == lpszTempStr)
  3110. {
  3111. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  3112. CLOSE_FILE_HANDLE( hFindFile ) ;
  3113. FREE_MEMORY(szTemporaryFileName);
  3114. return FAILURE ;
  3115. }
  3116. if( FindString( wszPatternString, L".",0) != NULL && (FindString(wfdFindFile.cFileName, L".",0)== NULL) )
  3117. {
  3118. StringCopy( lpszTempStr, wfdFindFile.cFileName, GetBufferSize(lpszTempStr) / sizeof(WCHAR) );
  3119. StringConcat( lpszTempStr, L".", GetBufferSize(lpszTempStr) / sizeof(WCHAR) );
  3120. }
  3121. else
  3122. {
  3123. StringCopy( lpszTempStr, wfdFindFile.cFileName, GetBufferSize(lpszTempStr) / sizeof(WCHAR) );
  3124. }
  3125. if((FALSE == bMatchPattern) || ((TRUE == bMatchPattern) && (TRUE == MatchPattern(wszPatternString, lpszTempStr))))
  3126. {
  3127. FREE_MEMORY(lpszTempStr);
  3128. *pbFilesNone = FALSE;
  3129. if(EXIT_SUCCESS == IsNTFSFileSystem2(g_lpszFileToSearch, &bNTFSFileSystem))
  3130. {
  3131. if(FALSE == bNTFSFileSystem)
  3132. {
  3133. wszFormedMessage = (LPWSTR)AllocateMemory((StringLengthW( g_lpszFileToSearch, 0 ) + MAX_RES_STRING + EXTRA_MEM ) * sizeof(WCHAR));
  3134. if( NULL == wszFormedMessage )
  3135. { // Reallocation failed .
  3136. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3137. FREE_MEMORY(szTemporaryFileName);
  3138. CLOSE_FILE_HANDLE( hFindFile ) ;
  3139. return FALSE ;
  3140. }
  3141. hr = StringCchPrintf(wszFormedMessage, (GetBufferSize(wszFormedMessage) / sizeof(WCHAR)), GetResString(IDS_FAT_VOLUME_INFO), _X(g_lpszFileToSearch));
  3142. if(FAILED(hr))
  3143. {
  3144. SetLastError(HRESULT_CODE(hr));
  3145. SaveLastError();
  3146. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  3147. FREE_MEMORY( wszFormedMessage ) ;
  3148. FREE_MEMORY(szTemporaryFileName);
  3149. CLOSE_FILE_HANDLE( hFindFile ) ;
  3150. return FALSE;
  3151. }
  3152. ShowMessage(stdout, wszFormedMessage);
  3153. FREE_MEMORY(wszFormedMessage);
  3154. continue;
  3155. }
  3156. }
  3157. if(TRUE == bAdminsOwner)
  3158. {
  3159. bFlag = TakeOwnerShip( g_lpszFileToSearch);
  3160. }
  3161. else
  3162. {
  3163. bFlag = TakeOwnerShipIndividual(g_lpszFileToSearch);
  3164. }
  3165. if( FALSE == bFlag && bTakeOwnerShipError == FALSE )
  3166. {
  3167. if( ERROR_NOT_ALL_ASSIGNED == GetLastError()) // || (ERROR_INVALID_HANDLE == GetLastError()))
  3168. {
  3169. if(NULL == szTmpFileName)
  3170. {
  3171. szTmpFileName = (LPWSTR)AllocateMemory((StringLengthW( g_lpszFileToSearch, 0 ) + MAX_RES_STRING + EXTRA_MEM ) * sizeof(WCHAR));
  3172. if( NULL == szTmpFileName )
  3173. { // Reallocation failed .
  3174. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3175. FREE_MEMORY(szTemporaryFileName);
  3176. CLOSE_FILE_HANDLE( hFindFile ) ;
  3177. return FALSE ;
  3178. }
  3179. }
  3180. else
  3181. {
  3182. dwMem = ( StringLengthW( g_lpszFileToSearch, 0) + MAX_RES_STRING + EXTRA_MEM );
  3183. if((DWORD)GetBufferSize(szTmpFileName) < dwMem * sizeof(WCHAR))
  3184. {
  3185. if(FALSE == ReallocateMemory((LPVOID*)&szTmpFileName,( StringLengthW( g_lpszFileToSearch, 0 ) + MAX_RES_STRING + EXTRA_MEM ) * sizeof(WCHAR) ))
  3186. {
  3187. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3188. FREE_MEMORY(szTmpFileName);
  3189. FREE_MEMORY(szTemporaryFileName);
  3190. CLOSE_FILE_HANDLE( hFindFile ) ;
  3191. return FALSE ;
  3192. }
  3193. else
  3194. if(NULL == szTmpFileName)
  3195. {
  3196. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3197. FREE_MEMORY(szTmpFileName);
  3198. FREE_MEMORY(szTemporaryFileName);
  3199. CLOSE_FILE_HANDLE( hFindFile ) ;
  3200. return FALSE ;
  3201. }
  3202. }
  3203. }
  3204. hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), GetResString(IDS_NOT_OWNERSHIP_INFO), g_lpszFileToSearch);
  3205. if(FAILED(hr))
  3206. {
  3207. SetLastError(HRESULT_CODE(hr));
  3208. SaveLastError();
  3209. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  3210. FREE_MEMORY(szTmpFileName);
  3211. FREE_MEMORY(szTemporaryFileName);
  3212. CLOSE_FILE_HANDLE( hFindFile ) ;
  3213. return( FALSE );
  3214. }
  3215. ShowMessage(stdout, szTmpFileName);
  3216. }
  3217. else if(ERROR_SHARING_VIOLATION == GetLastError())
  3218. {
  3219. if(NULL == szTmpFileName)
  3220. {
  3221. szTmpFileName = (LPWSTR)AllocateMemory((StringLengthW( g_lpszFileToSearch, 0 ) + MAX_RES_STRING + EXTRA_MEM ) * sizeof(WCHAR));
  3222. if( NULL == szTmpFileName )
  3223. { // Reallocation failed .
  3224. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3225. FREE_MEMORY(szTemporaryFileName);
  3226. CLOSE_FILE_HANDLE( hFindFile ) ;
  3227. return FALSE ;
  3228. }
  3229. }
  3230. else
  3231. {
  3232. dwMem = ( StringLengthW( g_lpszFileToSearch, 0) + MAX_RES_STRING + EXTRA_MEM );
  3233. if((DWORD)GetBufferSize(szTmpFileName) < dwMem * sizeof(WCHAR))
  3234. {
  3235. if( FALSE == ReallocateMemory((LPVOID*)&szTmpFileName,
  3236. ( StringLengthW( g_lpszFileToSearch, 0 ) + MAX_RES_STRING + EXTRA_MEM ) * sizeof(WCHAR) ))
  3237. {
  3238. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3239. FREE_MEMORY(szTmpFileName);
  3240. FREE_MEMORY(szTemporaryFileName);
  3241. CLOSE_FILE_HANDLE( hFindFile ) ;
  3242. return FALSE ;
  3243. }
  3244. else
  3245. if(NULL == szTmpFileName)
  3246. {
  3247. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3248. FREE_MEMORY(szTmpFileName);
  3249. FREE_MEMORY(szTemporaryFileName);
  3250. CLOSE_FILE_HANDLE( hFindFile ) ;
  3251. return FALSE ;
  3252. }
  3253. }
  3254. }
  3255. hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), GetResString(IDS_SHARING_VIOLATION_INFO), g_lpszFileToSearch);
  3256. if(FAILED(hr))
  3257. {
  3258. SetLastError(HRESULT_CODE(hr));
  3259. SaveLastError();
  3260. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  3261. FREE_MEMORY(szTmpFileName);
  3262. FREE_MEMORY(szTemporaryFileName);
  3263. CLOSE_FILE_HANDLE( hFindFile ) ;
  3264. return( FALSE );
  3265. }
  3266. ShowMessage(stdout, szTmpFileName);
  3267. }
  3268. else
  3269. {
  3270. wszFormedMessage = (LPWSTR)AllocateMemory((StringLengthW( g_lpszFileToSearch, 0 ) + MAX_STRING_LENGTH) * sizeof(WCHAR));
  3271. if(NULL == wszFormedMessage)
  3272. {
  3273. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  3274. FREE_MEMORY(szTmpFileName);
  3275. FREE_MEMORY(szTemporaryFileName);
  3276. CLOSE_FILE_HANDLE( hFindFile ) ;
  3277. return FALSE;
  3278. }
  3279. ShowMessage( stdout, L"\n" );
  3280. ShowMessage( stdout, TAG_INFORMATION );
  3281. ShowMessage( stdout, SPACE_CHAR );
  3282. StringCopy( wszTempMessage, GetReason(), SIZE_OF_ARRAY(wszTempMessage) );
  3283. lpNextTok = _tcstok(wszTempMessage, L".");
  3284. ShowMessage(stdout,wszTempMessage);
  3285. //hr = StringCchPrintf(wszFormedMessage, (GetBufferSize(wszFormedMessage) / sizeof(WCHAR)), GetResString(IDS_ON_FILE_FOLDER), _X(g_lpszFileToSearch) );
  3286. hr = StringCchPrintf(wszFormedMessage, (GetBufferSize(wszFormedMessage) / sizeof(WCHAR)), L". ( \"%s\" )\n", _X(g_lpszFileToSearch));
  3287. if(FAILED(hr))
  3288. {
  3289. SetLastError(HRESULT_CODE(hr));
  3290. SaveLastError();
  3291. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  3292. FREE_MEMORY( wszFormedMessage ) ;
  3293. FREE_MEMORY(szTemporaryFileName);
  3294. CLOSE_FILE_HANDLE( hFindFile ) ;
  3295. return FALSE;
  3296. }
  3297. ShowMessage(stdout, wszFormedMessage);
  3298. FREE_MEMORY(wszFormedMessage);
  3299. }
  3300. }
  3301. else
  3302. {
  3303. if(NULL == szTmpFileName)
  3304. {
  3305. szTmpFileName = (LPWSTR)AllocateMemory((StringLengthW( g_lpszFileToSearch, 0 ) + MAX_RES_STRING + EXTRA_MEM ) * sizeof(WCHAR));
  3306. if( NULL == szTmpFileName )
  3307. { // Reallocation failed .
  3308. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3309. FREE_MEMORY(szTemporaryFileName);
  3310. CLOSE_FILE_HANDLE( hFindFile ) ;
  3311. return FALSE ;
  3312. }
  3313. }
  3314. else
  3315. {
  3316. dwMem = ( StringLengthW( g_lpszFileToSearch, 0) + MAX_RES_STRING + EXTRA_MEM );
  3317. if((DWORD)GetBufferSize(szTmpFileName) < dwMem * sizeof(WCHAR))
  3318. {
  3319. if(FALSE == ReallocateMemory((LPVOID*)&szTmpFileName,( StringLengthW( g_lpszFileToSearch, 0 ) + MAX_RES_STRING + EXTRA_MEM ) * sizeof(WCHAR) ))
  3320. {
  3321. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3322. FREE_MEMORY(szTemporaryFileName);
  3323. FREE_MEMORY(szTmpFileName);
  3324. CLOSE_FILE_HANDLE( hFindFile ) ;
  3325. return FALSE ;
  3326. }
  3327. else
  3328. if(NULL == szTmpFileName)
  3329. {
  3330. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3331. FREE_MEMORY(szTemporaryFileName);
  3332. FREE_MEMORY(szTmpFileName);
  3333. CLOSE_FILE_HANDLE( hFindFile ) ;
  3334. return FALSE ;
  3335. }
  3336. }
  3337. }
  3338. if(TRUE == bAdminsOwner)
  3339. {
  3340. hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL,g_lpszFileToSearch );
  3341. if(FAILED(hr))
  3342. {
  3343. SetLastError(HRESULT_CODE(hr));
  3344. SaveLastError();
  3345. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  3346. FREE_MEMORY(szTmpFileName);
  3347. FREE_MEMORY(szTemporaryFileName);
  3348. CLOSE_FILE_HANDLE( hFindFile ) ;
  3349. return FALSE;
  3350. }
  3351. }
  3352. else
  3353. {
  3354. //hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL_USER, szOwnerString, g_lpszFileToSearch );
  3355. hr = StringCchPrintf(szTmpFileName, (GetBufferSize(szTmpFileName) / sizeof(WCHAR)), TAKEOWN_SUCCESSFUL_USER, g_lpszFileToSearch, szOwnerString);
  3356. if(FAILED(hr))
  3357. {
  3358. SetLastError(HRESULT_CODE(hr));
  3359. SaveLastError();
  3360. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  3361. FREE_MEMORY(szTmpFileName);
  3362. FREE_MEMORY(szTemporaryFileName);
  3363. CLOSE_FILE_HANDLE( hFindFile ) ;
  3364. return FALSE;
  3365. }
  3366. }
  3367. ShowMessage( stdout, _X(szTmpFileName) );
  3368. }
  3369. }
  3370. FREE_MEMORY(lpszTempStr);
  3371. } while( 0 != FindNextFile( hFindFile , &wfdFindFile ) ) ; // Continue till no files are present to display.
  3372. if( GetLastError() != ERROR_NO_MORE_FILES ) // If error occurs , check is error other than NOMOREFILES .
  3373. { // If error is other than NOMOREFILES then display ERROR .
  3374. SaveLastError();
  3375. ShowMessage( stderr , GetReason() ) ;
  3376. CLOSE_FILE_HANDLE( hFindFile ) ;
  3377. FREE_MEMORY(szTmpFileName);
  3378. FREE_MEMORY(szTemporaryFileName);
  3379. return FALSE ;
  3380. }
  3381. }
  3382. CLOSE_FILE_HANDLE( hFindFile ) ; // Close open find file handle .
  3383. g_pFollowPathName = NULL ;
  3384. FREE_MEMORY(szTmpFileName);
  3385. FREE_MEMORY(szTemporaryFileName);
  3386. return TRUE ;
  3387. }
  3388. BOOL
  3389. Push(
  3390. IN LPTSTR szPathName )
  3391. /*++
  3392. Routine Description:
  3393. Store the path of obtained subdirectory .
  3394. Arguments:
  3395. [ IN ] szPathName : Contains path of a subdirectory .
  3396. Return value:
  3397. TRUE if succedded in storing a path else FALSE if failed to get memory.
  3398. --*/
  3399. {
  3400. // Get a temporary variable .
  3401. PStore_Path_Name pAddPathName ;
  3402. // Assign memory To Temporary Variable .
  3403. pAddPathName = (PStore_Path_Name)AllocateMemory((1) * sizeof(struct __STORE_PATH_NAME ));
  3404. if( NULL == pAddPathName ) // Check is memory allocation is successful or not .
  3405. { // Memory allocation is successful .
  3406. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3407. return FALSE ;
  3408. }
  3409. // Assign memory to string variable which is going to store full path name of a valid directory .
  3410. pAddPathName->pszDirName = (LPTSTR)AllocateMemory((StringLengthW( szPathName, 0 ) + EXTRA_MEM) * sizeof( WCHAR ));
  3411. if( NULL == pAddPathName->pszDirName )// Check is memory allocation was successful or not .
  3412. { // Memory allocation was unsuccessful .
  3413. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3414. FREE_MEMORY( pAddPathName ) ;
  3415. return FALSE ;
  3416. }
  3417. // Copy path name to memory allocated string variable .
  3418. StringCopy(( LPTSTR ) pAddPathName->pszDirName, szPathName, (GetBufferSize(pAddPathName->pszDirName) / sizeof(WCHAR)) );
  3419. pAddPathName->NextNode = NULL ; // Assign null , had only one subdirectory stored .
  3420. // Check global variable is NULL or not .
  3421. if( NULL == g_pPathName )
  3422. { // Add memory to store path of subdirectory .
  3423. g_pPathName = pAddPathName ;
  3424. g_pFollowPathName = g_pPathName ;
  3425. }
  3426. else
  3427. {
  3428. if( NULL == g_pFollowPathName )
  3429. { // Store first obtained subdirectory .
  3430. pAddPathName->NextNode = g_pPathName ;
  3431. g_pPathName = pAddPathName ;
  3432. g_pFollowPathName = g_pPathName ;
  3433. }
  3434. else
  3435. {
  3436. // Stroe subdirectory in the middle
  3437. pAddPathName->NextNode = g_pFollowPathName->NextNode ;
  3438. g_pFollowPathName->NextNode = pAddPathName ;
  3439. g_pFollowPathName = pAddPathName ;
  3440. }
  3441. }
  3442. return TRUE ;
  3443. }
  3444. BOOL
  3445. Pop(
  3446. void )
  3447. /*++
  3448. Routine Description:
  3449. Get a subdirectory which has to be searched for a file matching a user
  3450. specified criteria .
  3451. Arguments:
  3452. Return value:
  3453. TRUE if successful in getting a path else FALSE if failed to get memory or
  3454. if no path is stored .
  3455. --*/
  3456. {
  3457. // Linked list has more than 1 node .
  3458. PStore_Path_Name pDelPathName = g_pPathName ;
  3459. // Check whether linked list is having any nodes .
  3460. if( NULL == g_pPathName )
  3461. { // No nodes present , return False ,
  3462. // Should not happen ever . Control should not come here .
  3463. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3464. return FALSE ;
  3465. }
  3466. // Realloc memory and give buffer space in which path name can fix .
  3467. if((DWORD)GetBufferSize( g_lpszFileToSearch) < ((StringLengthW( g_pPathName->pszDirName, 0 ) + EXTRA_MEM) * sizeof(WCHAR)))
  3468. {
  3469. if(FALSE == ReallocateMemory((LPVOID*)g_lpszFileToSearch, ( StringLengthW( g_pPathName->pszDirName, 0 ) + EXTRA_MEM ) * sizeof(WCHAR) ))
  3470. {
  3471. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3472. return FALSE ;
  3473. }
  3474. else
  3475. if( NULL == g_lpszFileToSearch )
  3476. { // Memory reallocation failed .
  3477. ShowMessage( stderr , GetResString(IDS_ERROR_MEMORY_ALLOC)) ;
  3478. return FALSE ;
  3479. }
  3480. }
  3481. // Check linked list has only one node .
  3482. if( NULL == g_pPathName->NextNode )
  3483. { // List has only one node .
  3484. // Memory allocation successful . Copy pathname to the buffer .
  3485. StringCopy(g_lpszFileToSearch, g_pPathName->pszDirName, (GetBufferSize(g_lpszFileToSearch) / sizeof(WCHAR)) );
  3486. g_pFollowPathName = NULL ; // Formality , better practice to assign NULL .
  3487. // Free node . Linked list is now empty .
  3488. FREE_MEMORY( g_pPathName->pszDirName ) ;
  3489. FREE_MEMORY( g_pPathName ) ;
  3490. return TRUE;
  3491. }
  3492. g_pPathName = pDelPathName->NextNode ;
  3493. // Memory allocation successful . Copy pathname to the buffer .
  3494. StringCopy(g_lpszFileToSearch, pDelPathName->pszDirName, (GetBufferSize(g_lpszFileToSearch) / sizeof(WCHAR)) );
  3495. // Free node .
  3496. FREE_MEMORY( pDelPathName->pszDirName ) ;
  3497. FREE_MEMORY( pDelPathName ) ;
  3498. return TRUE ;
  3499. }
  3500. BOOL
  3501. TakeOwnerShipIndividual(
  3502. IN LPCTSTR lpszFileName
  3503. )
  3504. /*++
  3505. Routine Description:
  3506. This routine takes the ownership of the specified file
  3507. Arguments:
  3508. [ IN ] lpszFileName - File name for whose ownership has to be taken.
  3509. [ IN ] lpszUserName - User Name in the Sam compatible format.
  3510. [ IN ] dwUserName - Logged on user name.
  3511. [ IN ] bLogonDomainAdmin - To know whether the logged on user is Domain admin or not.
  3512. Return Value:
  3513. TRUE if owner ship of the specified file has been taken
  3514. else FALSE
  3515. --*/
  3516. {
  3517. //local variables
  3518. SECURITY_DESCRIPTOR SecurityDescriptor;
  3519. PSECURITY_DESCRIPTOR pSd = NULL;
  3520. //PSID pAliasAdminsSid = NULL;
  3521. //SID_IDENTIFIER_AUTHORITY SepNtAuthority = SECURITY_NT_AUTHORITY;
  3522. PACL pDacl;
  3523. HANDLE hFile;
  3524. HANDLE hTokenHandle = NULL;
  3525. BOOL bResult = TRUE;
  3526. BOOL bInvalidFileHandle = FALSE;
  3527. PSID pSid = NULL;
  3528. PTOKEN_USER pTokUser = NULL;
  3529. DWORD dwTokLen = 0;
  3530. //check for valid input parameters
  3531. if( lpszFileName == NULL )
  3532. {
  3533. SetLastError( (DWORD) E_OUTOFMEMORY );
  3534. SaveLastError();
  3535. FREE_MEMORY(pTokUser);
  3536. CloseHandle( hTokenHandle );
  3537. return FALSE;
  3538. }
  3539. //get the token of the current process
  3540. bResult = GetTokenHandle( &hTokenHandle );
  3541. if( FALSE == bResult )
  3542. {
  3543. SaveLastError();
  3544. return( FALSE );
  3545. }
  3546. GetTokenInformation(hTokenHandle,TokenUser,NULL,0,&dwTokLen);
  3547. if(0 == dwTokLen)
  3548. {
  3549. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  3550. CloseHandle( hTokenHandle );
  3551. return( FALSE );
  3552. }
  3553. pTokUser = (TOKEN_USER*)AllocateMemory(dwTokLen );
  3554. if( pTokUser == NULL )
  3555. {
  3556. SetLastError( (DWORD) E_OUTOFMEMORY );
  3557. SaveLastError();
  3558. CloseHandle( hTokenHandle );
  3559. return FALSE;
  3560. }
  3561. if(!GetTokenInformation(hTokenHandle,TokenUser,pTokUser,dwTokLen,&dwTokLen))
  3562. {
  3563. SaveLastError();
  3564. FREE_MEMORY(pTokUser);
  3565. CloseHandle( hTokenHandle );
  3566. return( FALSE );
  3567. }
  3568. // Attempt to put a NULL Dacl on the object
  3569. bResult = InitializeSecurityDescriptor( &SecurityDescriptor,
  3570. SECURITY_DESCRIPTOR_REVISION );
  3571. if( FALSE == bResult )
  3572. {
  3573. SaveLastError();
  3574. FREE_MEMORY(pTokUser);
  3575. CloseHandle( hTokenHandle );
  3576. FREE_MEMORY(pSid);
  3577. return( FALSE );
  3578. }
  3579. //Get the handle of the file or directory
  3580. hFile = CreateFile( lpszFileName, READ_CONTROL , FILE_SHARE_READ, NULL,
  3581. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL );
  3582. //try once again it may be a directory
  3583. if( INVALID_HANDLE_VALUE != hFile )
  3584. {
  3585. //get the DACL for the currently existing file or directory
  3586. if( 0 != GetSecurityInfo( hFile, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL,
  3587. NULL, &pDacl, NULL, &pSd ) )
  3588. {
  3589. SaveLastError();
  3590. FREE_MEMORY(pTokUser);
  3591. FREE_MEMORY(pSid);
  3592. CloseHandle( hFile );
  3593. CloseHandle( hTokenHandle );
  3594. if(NULL != pSd)
  3595. {
  3596. LocalFree( pSd );
  3597. }
  3598. return( FALSE );
  3599. }
  3600. // set the security descriptor to acl
  3601. bResult = SetSecurityDescriptorDacl ( &SecurityDescriptor,
  3602. TRUE, pDacl, FALSE );
  3603. if( FALSE == bResult )
  3604. {
  3605. SaveLastError();
  3606. FREE_MEMORY(pTokUser);
  3607. FREE_MEMORY(pSid);
  3608. CloseHandle( hFile );
  3609. CloseHandle( hTokenHandle );
  3610. if(NULL != pSd)
  3611. {
  3612. LocalFree( pSd );
  3613. }
  3614. return( FALSE );
  3615. }
  3616. }
  3617. else
  3618. {
  3619. bInvalidFileHandle = TRUE;
  3620. }
  3621. bResult = SetSecurityDescriptorOwner ( &SecurityDescriptor,
  3622. pTokUser->User.Sid, FALSE );
  3623. if( FALSE == bResult )
  3624. {
  3625. SaveLastError();
  3626. FREE_MEMORY(pTokUser);
  3627. FREE_MEMORY(pSid);
  3628. CloseHandle( hTokenHandle );
  3629. CloseHandle( hFile );
  3630. if(NULL != pSd)
  3631. {
  3632. LocalFree( pSd );
  3633. }
  3634. return( FALSE );
  3635. }
  3636. //set the file security to adminsitrator owner
  3637. bResult = SetFileSecurity( lpszFileName, OWNER_SECURITY_INFORMATION,
  3638. &SecurityDescriptor );
  3639. if( TRUE == bResult )
  3640. {
  3641. if(NULL != pSd)
  3642. {
  3643. LocalFree( pSd );
  3644. }
  3645. FREE_MEMORY(pTokUser);
  3646. FREE_MEMORY(pSid);
  3647. CloseHandle( hTokenHandle );
  3648. CloseHandle( hFile );
  3649. return( TRUE );
  3650. }
  3651. // Assert TakeOwnership privilege for current process, then try again
  3652. bResult = AssertTakeOwnership( hTokenHandle );
  3653. if( FALSE == bResult )
  3654. {
  3655. if(TRUE == bInvalidFileHandle)
  3656. {
  3657. hFile = CreateFile( lpszFileName, READ_CONTROL , FILE_SHARE_READ, NULL,
  3658. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL );
  3659. if( INVALID_HANDLE_VALUE == hFile )
  3660. {
  3661. if( ( ERROR_BAD_NET_NAME == GetLastError() ) ||
  3662. ( ERROR_BAD_NETPATH == GetLastError() ) ||
  3663. ( ERROR_INVALID_NAME == GetLastError() ) )
  3664. {
  3665. FREE_MEMORY(pTokUser);
  3666. SetLastError(ERROR_INVALID_NAME);
  3667. }
  3668. else if( ERROR_SHARING_VIOLATION == GetLastError() )
  3669. {
  3670. FREE_MEMORY(pTokUser);
  3671. SetLastError(ERROR_SHARING_VIOLATION);
  3672. }
  3673. else
  3674. {
  3675. SaveLastError();
  3676. FREE_MEMORY(pTokUser);
  3677. }
  3678. CloseHandle( hTokenHandle );
  3679. return( FALSE );
  3680. }
  3681. }
  3682. switch (GetLastError())
  3683. {
  3684. case ERROR_NOT_ALL_ASSIGNED:
  3685. FREE_MEMORY(pTokUser);
  3686. FREE_MEMORY(pSid);
  3687. SetLastError(ERROR_NOT_ALL_ASSIGNED);
  3688. break;
  3689. case ERROR_SHARING_VIOLATION:
  3690. FREE_MEMORY(pTokUser);
  3691. FREE_MEMORY(pSid);
  3692. SetLastError(ERROR_SHARING_VIOLATION);
  3693. break;
  3694. case ERROR_BAD_NET_NAME :
  3695. case ERROR_BAD_NETPATH :
  3696. case ERROR_INVALID_NAME : FREE_MEMORY(pTokUser);
  3697. FREE_MEMORY(pSid);
  3698. SetLastError(ERROR_BAD_NET_NAME);
  3699. break;
  3700. default : FREE_MEMORY(pTokUser);
  3701. FREE_MEMORY(pSid);
  3702. break;
  3703. }
  3704. CloseHandle( hTokenHandle );
  3705. CloseHandle( hFile );
  3706. if(NULL != pSd)
  3707. {
  3708. LocalFree( pSd );
  3709. }
  3710. return( FALSE );
  3711. }
  3712. //Now try to set ownership security privilege for the file
  3713. bResult = SetFileSecurity( lpszFileName, OWNER_SECURITY_INFORMATION,
  3714. &SecurityDescriptor );
  3715. if( FALSE == bResult )
  3716. {
  3717. SaveLastError();
  3718. if(TRUE == bInvalidFileHandle)
  3719. {
  3720. //Check out whether it is an invalid file or file does not exist
  3721. hFile = CreateFile( lpszFileName, READ_CONTROL , FILE_SHARE_READ, NULL,
  3722. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL );
  3723. if( INVALID_HANDLE_VALUE == hFile )
  3724. {
  3725. if( ( ERROR_BAD_NET_NAME == GetLastError() ) ||
  3726. ( ERROR_BAD_NETPATH == GetLastError() ) ||
  3727. ( ERROR_INVALID_NAME == GetLastError() ) )
  3728. {
  3729. FREE_MEMORY(pTokUser);
  3730. SetLastError(ERROR_INVALID_NAME);
  3731. }
  3732. else if( ERROR_SHARING_VIOLATION == GetLastError() )
  3733. {
  3734. FREE_MEMORY(pTokUser);
  3735. SetLastError(ERROR_SHARING_VIOLATION);
  3736. }
  3737. else
  3738. {
  3739. SaveLastError();
  3740. FREE_MEMORY(pTokUser);
  3741. }
  3742. CloseHandle( hTokenHandle );
  3743. return( FALSE );
  3744. }
  3745. }
  3746. switch (GetLastError())
  3747. {
  3748. case ERROR_NOT_ALL_ASSIGNED:
  3749. FREE_MEMORY(pTokUser);
  3750. FREE_MEMORY(pSid);
  3751. SetLastError(ERROR_NOT_ALL_ASSIGNED);
  3752. break;
  3753. case ERROR_SHARING_VIOLATION:
  3754. FREE_MEMORY(pTokUser);
  3755. FREE_MEMORY(pSid);
  3756. SetLastError(ERROR_SHARING_VIOLATION);
  3757. break;
  3758. case ERROR_BAD_NET_NAME :
  3759. case ERROR_BAD_NETPATH :
  3760. case ERROR_INVALID_NAME : FREE_MEMORY(pTokUser);
  3761. FREE_MEMORY(pSid);
  3762. SetLastError(ERROR_BAD_NET_NAME);
  3763. break;
  3764. default : FREE_MEMORY(pTokUser);
  3765. FREE_MEMORY(pSid);
  3766. break;
  3767. }
  3768. CloseHandle( hTokenHandle );
  3769. CloseHandle( hFile );
  3770. if(NULL != pSd)
  3771. {
  3772. LocalFree( pSd );
  3773. }
  3774. return( FALSE );
  3775. }
  3776. if(NULL != pSd)
  3777. {
  3778. LocalFree( pSd );
  3779. }
  3780. FREE_MEMORY(pTokUser);
  3781. FREE_MEMORY(pSid);
  3782. CloseHandle( hTokenHandle );
  3783. CloseHandle( hFile );
  3784. return( TRUE );
  3785. }
  3786. BOOL
  3787. AddAccessRights(IN WCHAR *lpszFileName,
  3788. IN DWORD dwAccessMask,
  3789. IN LPWSTR dwUserName,
  3790. IN BOOL bAdminsOwner)
  3791. /*++
  3792. Routine Description:
  3793. This routine takes the ownership of the specified file
  3794. Arguments:
  3795. [ IN ] lpszFileName - Directory name for whom access permissions has to be granted.
  3796. [ IN ] dwAccessMask - Access Mask for giving the permissions.
  3797. [ IN ] dwUserName - User Name in the Sam compatible format.
  3798. Return Value:
  3799. TRUE if owner ship of the specified file has been taken
  3800. else FALSE
  3801. --*/
  3802. {
  3803. // SID variables.
  3804. SID_NAME_USE snuType;
  3805. WCHAR * szDomain = NULL;
  3806. DWORD cbDomain = 0;
  3807. PSID pUserSID = NULL;
  3808. DWORD cbUserSID = 0;
  3809. // File SD variables.
  3810. PSECURITY_DESCRIPTOR pFileSD = NULL;
  3811. DWORD cbFileSD = 0;
  3812. // New SD variables.
  3813. PSECURITY_DESCRIPTOR pNewSD = NULL;
  3814. // ACL variables.
  3815. PACL pACL = NULL;
  3816. BOOL fDaclPresent;
  3817. BOOL fDaclDefaulted;
  3818. ACL_SIZE_INFORMATION AclInfo;
  3819. // New ACL variables.
  3820. PACL pNewACL = NULL;
  3821. DWORD cbNewACL = 0;
  3822. // Assume function will fail.
  3823. BOOL fResult = FALSE;
  3824. BOOL fAPISuccess ;
  3825. BOOL bResult = FALSE;
  3826. ACCESS_ALLOWED_ACE *pace = NULL;
  3827. WORD acesize = 0;
  3828. PSID pAliasAdminsSid = NULL;
  3829. SID_IDENTIFIER_AUTHORITY SepNtAuthority = SECURITY_NT_AUTHORITY;
  3830. dwAccessMask = 0;
  3831. if(TRUE == bAdminsOwner)
  3832. {
  3833. //allocate and initialise sid
  3834. bResult = AllocateAndInitializeSid(
  3835. &SepNtAuthority,
  3836. 2,
  3837. SECURITY_BUILTIN_DOMAIN_RID,
  3838. DOMAIN_ALIAS_RID_ADMINS,
  3839. 0,
  3840. 0,
  3841. 0,
  3842. 0,
  3843. 0,
  3844. 0,
  3845. &pAliasAdminsSid
  3846. );
  3847. }
  3848. //
  3849. // Get the actual size of SID for current user.
  3850. //
  3851. pUserSID = NULL;
  3852. cbUserSID = 0;
  3853. fAPISuccess = LookupAccountName( NULL, dwUserName,
  3854. pUserSID, &cbUserSID, szDomain, &cbDomain, &snuType);
  3855. // Since the buffer size is too small..API fails with insufficient buffer.
  3856. // If the error code is other than ERROR_INSUFFICIENT_BUFFER, then return failure.
  3857. if ( (FALSE == fAPISuccess) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER) )
  3858. {
  3859. SaveLastError();
  3860. return FALSE;
  3861. }
  3862. // allocate pUserSID with the actual size i.e.,cbUserSID
  3863. pUserSID = (PSID)AllocateMemory(cbUserSID);
  3864. if(NULL == pUserSID)
  3865. {
  3866. SaveLastError();
  3867. if(NULL != pAliasAdminsSid)
  3868. {
  3869. FreeSid( pAliasAdminsSid );
  3870. }
  3871. return FALSE;
  3872. }
  3873. szDomain = (WCHAR*)AllocateMemory(cbDomain*sizeof(WCHAR));
  3874. if(NULL == szDomain)
  3875. {
  3876. SaveLastError();
  3877. FreeMemory(&pUserSID);
  3878. if(NULL != pAliasAdminsSid)
  3879. {
  3880. FreeSid( pAliasAdminsSid );
  3881. }
  3882. return FALSE;
  3883. }
  3884. fAPISuccess = LookupAccountName( NULL, dwUserName,
  3885. pUserSID, &cbUserSID, szDomain, &cbDomain, &snuType);
  3886. if(0 == fAPISuccess)
  3887. {
  3888. SaveLastError();
  3889. FreeMemory(&pUserSID);
  3890. FreeMemory(&szDomain);
  3891. if(NULL != pAliasAdminsSid)
  3892. {
  3893. FreeSid( pAliasAdminsSid );
  3894. }
  3895. return FALSE;
  3896. }
  3897. //
  3898. // Get security descriptor (SD) for file.
  3899. //
  3900. fAPISuccess = GetFileSecurity(lpszFileName,
  3901. DACL_SECURITY_INFORMATION, pFileSD, 0, &cbFileSD);
  3902. // API should have failed with insufficient buffer.
  3903. if(0 != cbFileSD)
  3904. {
  3905. pFileSD = (PSECURITY_DESCRIPTOR)AllocateMemory(cbFileSD);
  3906. if(NULL == pFileSD)
  3907. {
  3908. SaveLastError();
  3909. FreeMemory(&pUserSID);
  3910. FreeMemory(&szDomain);
  3911. if(NULL != pAliasAdminsSid)
  3912. {
  3913. FreeSid( pAliasAdminsSid );
  3914. }
  3915. return FALSE;
  3916. }
  3917. }
  3918. fAPISuccess = GetFileSecurity(lpszFileName,
  3919. DACL_SECURITY_INFORMATION, pFileSD, cbFileSD, &cbFileSD);
  3920. if (!fAPISuccess)
  3921. {
  3922. SaveLastError();
  3923. FreeMemory(&pUserSID);
  3924. FreeMemory(&szDomain);
  3925. FreeMemory(pFileSD);
  3926. if(NULL != pAliasAdminsSid)
  3927. {
  3928. FreeSid( pAliasAdminsSid );
  3929. }
  3930. return FALSE;
  3931. }
  3932. //
  3933. // Initialize new SD.
  3934. //
  3935. pNewSD = (PSECURITY_DESCRIPTOR)AllocateMemory(cbFileSD); // Should be same size as FileSD.
  3936. if (!pNewSD)
  3937. {
  3938. SaveLastError();
  3939. FreeMemory(&pUserSID);
  3940. FreeMemory(&szDomain);
  3941. FreeMemory(pFileSD);
  3942. if(NULL != pAliasAdminsSid)
  3943. {
  3944. FreeSid( pAliasAdminsSid );
  3945. }
  3946. return FALSE;
  3947. }
  3948. if (!InitializeSecurityDescriptor(pNewSD,
  3949. SECURITY_DESCRIPTOR_REVISION))
  3950. {
  3951. SaveLastError();
  3952. FreeMemory(&pUserSID);
  3953. FreeMemory(&szDomain);
  3954. FreeMemory(pFileSD);
  3955. FreeMemory(pNewSD);
  3956. if(NULL != pAliasAdminsSid)
  3957. {
  3958. FreeSid( pAliasAdminsSid );
  3959. }
  3960. return FALSE;
  3961. }
  3962. //
  3963. // Get DACL from SD.
  3964. //
  3965. if (!GetSecurityDescriptorDacl(pFileSD, &fDaclPresent, &pACL,
  3966. &fDaclDefaulted))
  3967. {
  3968. SaveLastError();
  3969. FreeMemory(&pUserSID);
  3970. FreeMemory(&szDomain);
  3971. FreeMemory(pFileSD);
  3972. FreeMemory(pNewSD);
  3973. if(NULL != pAliasAdminsSid)
  3974. {
  3975. FreeSid( pAliasAdminsSid );
  3976. }
  3977. return FALSE;
  3978. }
  3979. //
  3980. // Get size information for DACL.
  3981. //
  3982. AclInfo.AceCount = 0; // Assume NULL DACL.
  3983. AclInfo.AclBytesFree = 0;
  3984. AclInfo.AclBytesInUse = sizeof(ACL);
  3985. // If not NULL DACL, gather size information from DACL.
  3986. if (fDaclPresent && pACL)
  3987. {
  3988. if(!GetAclInformation(pACL, &AclInfo,
  3989. sizeof(ACL_SIZE_INFORMATION), AclSizeInformation))
  3990. {
  3991. SaveLastError();
  3992. FreeMemory(&pUserSID);
  3993. FreeMemory(&szDomain);
  3994. FreeMemory(pFileSD);
  3995. FreeMemory(pNewSD);
  3996. if(NULL != pAliasAdminsSid)
  3997. {
  3998. FreeSid( pAliasAdminsSid );
  3999. }
  4000. return FALSE;
  4001. }
  4002. }
  4003. if(TRUE == bAdminsOwner)
  4004. {
  4005. pace = (ACCESS_ALLOWED_ACE*)AllocateMemory(sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(pAliasAdminsSid) - sizeof(DWORD));
  4006. if(NULL == pace)
  4007. {
  4008. SaveLastError();
  4009. FreeMemory(&pUserSID);
  4010. FreeMemory(&szDomain);
  4011. FreeMemory(pFileSD);
  4012. FreeMemory(pNewSD);
  4013. if(NULL != pAliasAdminsSid)
  4014. {
  4015. FreeSid( pAliasAdminsSid );
  4016. }
  4017. return FALSE;
  4018. }
  4019. memcpy(&pace->SidStart, pAliasAdminsSid, GetLengthSid(pAliasAdminsSid));
  4020. }
  4021. else
  4022. {
  4023. pace = (ACCESS_ALLOWED_ACE*)AllocateMemory(sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(pUserSID) - sizeof(DWORD));
  4024. if(NULL == pace)
  4025. {
  4026. SaveLastError();
  4027. FreeMemory(&pUserSID);
  4028. FreeMemory(&szDomain);
  4029. FreeMemory(pFileSD);
  4030. FreeMemory(pNewSD);
  4031. return FALSE;
  4032. }
  4033. memcpy(&pace->SidStart,pUserSID,GetLengthSid(pUserSID));
  4034. }
  4035. pace->Mask = 2032127;
  4036. pace->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  4037. if(TRUE == bAdminsOwner)
  4038. {
  4039. acesize = (WORD) (sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(pAliasAdminsSid) - sizeof(DWORD));
  4040. }
  4041. else
  4042. {
  4043. acesize = (WORD) (sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(pUserSID) - sizeof(DWORD) );
  4044. }
  4045. pace->Header.AceSize = acesize;
  4046. pace->Header.AceFlags = INHERIT_ONLY_ACE | OBJECT_INHERIT_ACE;
  4047. //pace->Header.AceFlags = OBJECT_INHERIT_ACE ;
  4048. //
  4049. // Compute size needed for the new ACL.
  4050. //
  4051. if(TRUE == bAdminsOwner)
  4052. {
  4053. cbNewACL = AclInfo.AclBytesInUse + sizeof(ACCESS_ALLOWED_ACE)
  4054. + GetLengthSid(pAliasAdminsSid) - sizeof(DWORD) + 100;
  4055. }
  4056. else
  4057. {
  4058. cbNewACL = AclInfo.AclBytesInUse + sizeof(ACCESS_ALLOWED_ACE)
  4059. + GetLengthSid(pUserSID) - sizeof(DWORD) + 100;
  4060. }
  4061. //
  4062. // Allocate memory for new ACL.
  4063. //
  4064. pNewACL = (PACL) AllocateMemory(cbNewACL);
  4065. if (!pNewACL)
  4066. {
  4067. SaveLastError();
  4068. FreeMemory(&pUserSID);
  4069. FreeMemory(&szDomain);
  4070. FreeMemory(pFileSD);
  4071. FreeMemory(pNewSD);
  4072. if(NULL != pAliasAdminsSid)
  4073. {
  4074. FreeSid( pAliasAdminsSid );
  4075. }
  4076. return FALSE;
  4077. }
  4078. //
  4079. // Initialize the new ACL.
  4080. //
  4081. if(!InitializeAcl(pNewACL, cbNewACL, ACL_REVISION))
  4082. {
  4083. SaveLastError();
  4084. //heapfree(pUserSID);
  4085. FreeMemory(&pUserSID);
  4086. //heapfree(szDomain);
  4087. FreeMemory(&szDomain);
  4088. //heapfree(pFileSD);
  4089. FreeMemory(pFileSD);
  4090. //heapfree(pNewSD);
  4091. FreeMemory(pNewSD);
  4092. //heapfree(pNewACL);
  4093. FreeMemory((LPVOID*)pNewACL);
  4094. if(NULL != pAliasAdminsSid)
  4095. {
  4096. FreeSid( pAliasAdminsSid );
  4097. }
  4098. return FALSE;
  4099. }
  4100. if(!AddAce(pNewACL,
  4101. ACL_REVISION,
  4102. 0xffffffff,
  4103. pace,
  4104. pace->Header.AceSize))
  4105. {
  4106. SaveLastError();
  4107. FreeMemory(&pUserSID);
  4108. FreeMemory(&szDomain);
  4109. FreeMemory(pFileSD);
  4110. FreeMemory(pNewSD);
  4111. FreeMemory((LPVOID*)pNewACL);
  4112. if(NULL != pAliasAdminsSid)
  4113. {
  4114. FreeSid( pAliasAdminsSid );
  4115. }
  4116. return FALSE;
  4117. }
  4118. pace->Header.AceFlags = CONTAINER_INHERIT_ACE ;
  4119. if(!AddAce(pNewACL,
  4120. pNewACL->AclRevision,
  4121. //ACL_REVISION,
  4122. 0xffffffff,
  4123. pace,
  4124. pace->Header.AceSize))
  4125. {
  4126. SaveLastError();
  4127. FreeMemory(&pUserSID);
  4128. FreeMemory(&szDomain);
  4129. FreeMemory(pFileSD);
  4130. FreeMemory(pNewSD);
  4131. FreeMemory((LPVOID*)pNewACL);
  4132. if(NULL != pAliasAdminsSid)
  4133. {
  4134. FreeSid( pAliasAdminsSid );
  4135. }
  4136. return FALSE;
  4137. }
  4138. //
  4139. // Set the new DACL to the file SD.
  4140. //
  4141. if (!SetSecurityDescriptorDacl(pNewSD, TRUE, pNewACL,
  4142. FALSE))
  4143. {
  4144. SaveLastError();
  4145. FreeMemory(&pUserSID);
  4146. FreeMemory(&szDomain);
  4147. FreeMemory(pFileSD);
  4148. FreeMemory(pNewSD);
  4149. FreeMemory((LPVOID*)pNewACL);
  4150. if(NULL != pAliasAdminsSid)
  4151. {
  4152. FreeSid( pAliasAdminsSid );
  4153. }
  4154. return FALSE;
  4155. }
  4156. //
  4157. // Set the SD to the File.
  4158. //
  4159. if (!SetFileSecurity(lpszFileName, DACL_SECURITY_INFORMATION,
  4160. pNewSD))
  4161. {
  4162. SaveLastError();
  4163. FreeMemory(&pUserSID);
  4164. FreeMemory(&szDomain);
  4165. FreeMemory(pFileSD);
  4166. FreeMemory(pNewSD);
  4167. FreeMemory((LPVOID*)pNewACL);
  4168. if(NULL != pAliasAdminsSid)
  4169. {
  4170. FreeSid( pAliasAdminsSid );
  4171. }
  4172. return FALSE;
  4173. }
  4174. fResult = TRUE;
  4175. //
  4176. // Free allocated memory
  4177. //
  4178. if (pUserSID)
  4179. {
  4180. FreeMemory(&pUserSID);
  4181. }
  4182. if (szDomain)
  4183. {
  4184. FreeMemory(&szDomain);
  4185. }
  4186. if (pFileSD)
  4187. {
  4188. FreeMemory(pFileSD);
  4189. }
  4190. if (pNewSD)
  4191. {
  4192. FreeMemory(pNewSD);
  4193. }
  4194. if (pNewACL)
  4195. {
  4196. FreeMemory((LPVOID*)pNewACL);
  4197. }
  4198. if(NULL != pAliasAdminsSid)
  4199. {
  4200. FreeSid( pAliasAdminsSid );
  4201. }
  4202. return fResult;
  4203. }
  4204. DWORD
  4205. IsNTFSFileSystem(IN LPWSTR lpszPath,
  4206. BOOL bLocalSystem,
  4207. //BOOL bFileInUNCFormat,
  4208. BOOL bCurrDirTakeOwnAllFiles,
  4209. LPWSTR szUserName,
  4210. OUT PBOOL pbNTFSFileSystem)
  4211. /*++
  4212. Routine Description:
  4213. This routine finds whether Persistant ACLs are available are not
  4214. Arguments:
  4215. [ IN ] lpszPath - Path for which to find for Persistant ACLs.
  4216. [ IN ] bLocalSystem - Information whether local system or not
  4217. [ IN ] bFileInUNCFormat - whether file in UNC format or not
  4218. [ IN ] bCurrDirTakeOwnAllFiles - Whether takeown applied for curr directory
  4219. [ IN ] szUserName - user name
  4220. [ IN ] pbNTFSFileSystem - To know whether Persistant ACLs are available
  4221. Return Value:
  4222. EXIT_SUCCESS if the Function is passed
  4223. EXIT_FAILURE if the function fails
  4224. --*/
  4225. {
  4226. DWORD dwi = 0;
  4227. LPWSTR lpszTempDrive = NULL;
  4228. if(TRUE == bCurrDirTakeOwnAllFiles)
  4229. {
  4230. dwi = GetCurrentDirectory( 0, lpszTempDrive );
  4231. if( 0 == dwi )
  4232. {
  4233. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  4234. return EXIT_FAILURE;
  4235. }
  4236. lpszTempDrive = (LPWSTR)AllocateMemory((dwi + 20) * sizeof(WCHAR));
  4237. if(NULL == lpszTempDrive)
  4238. {
  4239. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  4240. return EXIT_FAILURE;
  4241. }
  4242. dwi = GetCurrentDirectory( dwi + 10, lpszTempDrive );
  4243. if( 0 == dwi )
  4244. {
  4245. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  4246. FREE_MEMORY(lpszTempDrive);
  4247. return EXIT_FAILURE;
  4248. }
  4249. }
  4250. else
  4251. {
  4252. dwi = StringLengthW(lpszPath, 0);
  4253. lpszTempDrive = (LPWSTR)AllocateMemory((dwi + 20) * sizeof(WCHAR));
  4254. if(NULL == lpszTempDrive)
  4255. {
  4256. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  4257. return EXIT_FAILURE;
  4258. }
  4259. StringCopy( lpszTempDrive, lpszPath, (GetBufferSize(lpszTempDrive) / sizeof(WCHAR)) );
  4260. }
  4261. if((TRUE == bLocalSystem) && (StringLengthW( szUserName, 0 ) != 0))
  4262. {
  4263. ShowMessage( stderr, IGNORE_LOCALCREDENTIALS );
  4264. /*if(FALSE == bFileInUNCFormat)
  4265. {
  4266. ShowMessage( stderr, IGNORE_LOCALCREDENTIALS );
  4267. }
  4268. else
  4269. {
  4270. ShowMessage( stderr, GetResString(IDS_IGNORE_CREDENTIALS) );
  4271. }*/
  4272. }
  4273. /*if((FALSE == bLocalSystem) && TRUE == bFileInUNCFormat)
  4274. {
  4275. ShowMessage( stderr, GetResString(IDS_IGNORE_CREDENTIALS) );
  4276. }*/
  4277. if(EXIT_FAILURE == IsNTFSFileSystem2(lpszTempDrive, pbNTFSFileSystem))
  4278. {
  4279. FREE_MEMORY(lpszTempDrive);
  4280. return EXIT_FAILURE;
  4281. }
  4282. FREE_MEMORY(lpszTempDrive);
  4283. return EXIT_SUCCESS;
  4284. }
  4285. DWORD
  4286. IsNTFSFileSystem2(IN LPWSTR lpszTempDrive,
  4287. OUT PBOOL pbNTFSFileSystem)
  4288. /*++
  4289. Routine Description:
  4290. This routine finds whether Persistant ACLs are available are not
  4291. Arguments:
  4292. [ IN ] lpszPath - Path for which to find for Persistant ACLs.
  4293. [ IN ] bLocalSystem - Information whether local system or not
  4294. [ IN ] bFileInUNCFormat - whether file in UNC format or not
  4295. [ IN ] bCurrDirTakeOwnAllFiles - Whether takeown applied for curr directory
  4296. [ IN ] szUserName - user name
  4297. [ IN ] pbNTFSFileSystem - To know whether Persistant ACLs are available
  4298. Return Value:
  4299. EXIT_SUCCESS if the function is passed
  4300. EXIT_FAILURE if the function fails
  4301. --*/
  4302. {
  4303. DWORD dwSysFlags = 0;
  4304. LPWSTR lpszMountPath = NULL;
  4305. WCHAR wszFileSysNameBuf[FILESYSNAMEBUFSIZE] ;
  4306. DWORD dwi = 0;
  4307. LPWSTR lpszTempPath = NULL;
  4308. SecureZeroMemory(wszFileSysNameBuf, FILESYSNAMEBUFSIZE * sizeof(WCHAR));
  4309. lpszTempPath = (LPWSTR)AllocateMemory((StringLengthW(lpszTempDrive, 0) + 10) * sizeof(WCHAR));
  4310. if(NULL == lpszTempPath)
  4311. {
  4312. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  4313. return EXIT_FAILURE;
  4314. }
  4315. StringCopy(lpszTempPath, lpszTempDrive, (GetBufferSize(lpszTempPath) / sizeof(WCHAR)));
  4316. StringConcat(lpszTempPath, L"\\\\", (GetBufferSize(lpszTempPath) / sizeof(WCHAR)) );
  4317. lpszMountPath = (LPWSTR)AllocateMemory((StringLengthW(lpszTempPath, 0) + 10) * sizeof(WCHAR));
  4318. if(NULL == lpszMountPath)
  4319. {
  4320. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  4321. FREE_MEMORY(lpszTempPath);
  4322. return EXIT_FAILURE;
  4323. }
  4324. if(0 == GetVolumePathName( lpszTempPath, lpszMountPath, StringLengthW(lpszTempPath, 0)))
  4325. {
  4326. if( ( ERROR_BAD_NET_NAME == GetLastError() ) ||
  4327. ( ERROR_BAD_NETPATH == GetLastError() ) ||
  4328. ( ERROR_INVALID_NAME == GetLastError() ) )
  4329. {
  4330. SetLastError( ERROR_FILE_NOT_FOUND );
  4331. SaveLastError();
  4332. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  4333. }
  4334. else
  4335. {
  4336. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  4337. }
  4338. FREE_MEMORY(lpszMountPath);
  4339. FREE_MEMORY(lpszTempPath);
  4340. return EXIT_FAILURE;
  4341. };
  4342. dwi = GetVolumeInformation(lpszMountPath,NULL,0,NULL,NULL,&dwSysFlags,wszFileSysNameBuf,FILESYSNAMEBUFSIZE);
  4343. if(dwi == 0)
  4344. {
  4345. if( GetLastError() == ERROR_DIR_NOT_ROOT )
  4346. {
  4347. FREE_MEMORY(lpszMountPath);
  4348. FREE_MEMORY(lpszTempPath);
  4349. return EXIT_SUCCESS;
  4350. }
  4351. else
  4352. {
  4353. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_SYSTEM);
  4354. FREE_MEMORY(lpszMountPath);
  4355. FREE_MEMORY(lpszTempPath);
  4356. return EXIT_FAILURE;
  4357. }
  4358. }
  4359. else
  4360. {
  4361. if((dwSysFlags & FS_PERSISTENT_ACLS) == FS_PERSISTENT_ACLS)
  4362. {
  4363. *pbNTFSFileSystem = TRUE;
  4364. }
  4365. FREE_MEMORY(lpszMountPath);
  4366. FREE_MEMORY(lpszTempPath);
  4367. return EXIT_SUCCESS;
  4368. }
  4369. }
  4370. DWORD RemoveStarFromPattern( IN OUT LPWSTR szPattern )
  4371. /*++
  4372. Routine Description:
  4373. This routine helps to remove the stars if there are more than one star available in pattern
  4374. Arguments:
  4375. [ IN OUT ] szPattern - Pattern to remove stars
  4376. Return Value:
  4377. EXIT_SUCCESS if the stars are removed
  4378. EXIT_FAILURE if the function fails
  4379. --*/
  4380. {
  4381. LPWSTR szTempPattern = NULL;
  4382. DWORD i = 0;
  4383. DWORD j = 0;
  4384. szTempPattern = (LPWSTR) AllocateMemory((StringLengthW(szPattern,0)+1)*sizeof(WCHAR) );
  4385. if( NULL == szTempPattern )
  4386. {
  4387. ShowLastErrorEx(stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  4388. return (EXIT_FAILURE);
  4389. }
  4390. while( szPattern[i] )
  4391. {
  4392. if( szPattern[i] == L'*' )
  4393. {
  4394. for(;szPattern[i]==L'*' && szPattern[i];i++);
  4395. szTempPattern[j] = L'*';
  4396. }
  4397. else
  4398. {
  4399. szTempPattern[j] = szPattern[i++];
  4400. }
  4401. j++;
  4402. }
  4403. szTempPattern[j]=0;
  4404. StringCopy( szPattern, szTempPattern, StringLengthW(szPattern,0)+1 );
  4405. FreeMemory(&szTempPattern);
  4406. return (EXIT_SUCCESS);
  4407. }