PageRenderTime 38ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/php-5.4.5/ext/standard/tests/file/windows_acls/common.inc

#
Pascal | 199 lines | 84 code | 23 blank | 92 comment | 14 complexity | 53a5feba6acacfd9f3e876acb6a83073 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. error_reporting(E_ALL);
  3. define('PHPT_ACL_READ', 1 << 1);
  4. define('PHPT_ACL_WRITE', 1 << 2);
  5. define('PHPT_ACL_EXEC', 1 << 3);
  6. define('PHPT_ACL_NONE', 1 << 4);
  7. define('PHPT_ACL_FULL', 1 << 5);
  8. define('PHPT_ACL_GRANT', 1);
  9. define('PHPT_ACL_DENY', 2);
  10. function skipif() {
  11. if(substr(PHP_OS, 0, 3) != 'WIN' ) {
  12. die('skip windows only test');
  13. }
  14. if(stripos(php_uname(), 'XP') !== FALSE) {
  15. die('skip windows 2003 or newer only test');
  16. }
  17. }
  18. function get_username(){
  19. $user = getenv('USERNAME');
  20. if (!$user) {
  21. $user = get_current_user();
  22. }
  23. if (!$user) {
  24. $user = exec('echo %USERNAME%');
  25. }
  26. return $user;
  27. }
  28. function get_domainname()
  29. {
  30. $domain = getenv('USERDOMAIN');
  31. return $domain;
  32. }
  33. function get_icacls()
  34. {
  35. $sysroot = exec('echo %SYSTEMROOT%');
  36. return "$sysroot\\System32\\icacls.exe";
  37. }
  38. function fix_acls() {
  39. $user = get_username();
  40. /* Current user needs to be owner of the test files. As well
  41. all the other users having acls on the files must loose them.
  42. The following fixes this just partially, as dynamically reading
  43. all the users having acls on a file could be sophisticated. */
  44. exec(get_icacls() . ' . /setowner $user /T /L /Q 2> nul');
  45. exec(get_icacls() . ' . /remove:g Administrators /T /L /Q 2> nul');
  46. }
  47. function icacls_set($path, $mode, $perm) {
  48. $icacls = get_icacls();
  49. $user = get_username();
  50. $path_escaped = '"' . $path . '"';
  51. $perm_entry = array();
  52. if ($perm & PHPT_ACL_READ) $perm_entry[] = 'R';
  53. if ($perm & PHPT_ACL_WRITE) $perm_entry[] = 'W';
  54. if ($perm & PHPT_ACL_EXEC) $perm_entry[] = 'RX';
  55. if ($perm & PHPT_ACL_FULL) $perm_entry[] = 'F';
  56. // Deny all
  57. $cmd = $icacls . ' ' . $path_escaped . ' /inheritance:r /deny ' . $user . ':(F,M,R,RX,W)';
  58. exec($cmd);
  59. if ($perm & PHPT_ACL_NONE) {
  60. /*
  61. This is required to remove all the previously denied
  62. permission for the USER. Just granting permission doesn't
  63. remove the previously denied permission.
  64. */
  65. $cmd = $icacls . ' ' . $path_escaped . ' /remove:d ' . $user;
  66. exec($cmd);
  67. $cmd = $icacls . ' ' . $path_escaped . ' /remove:g ' . $user;
  68. exec($cmd);
  69. return;
  70. }
  71. if ($mode == PHPT_ACL_GRANT) {
  72. $mode = 'grant';
  73. } else {
  74. $mode = 'deny';
  75. }
  76. // Deny all
  77. $cmd = $icacls . ' ' . $path_escaped . ' /deny ' . $user . ':(F,M,R,RX,W)';
  78. exec($cmd);
  79. /*
  80. This is required to remove all the previously denied
  81. permission for the USER. Just granting permission doesn't
  82. remove the previously denied permission.
  83. */
  84. $cmd = $icacls . ' ' . $path_escaped . ' /remove:d ' . $user;
  85. exec($cmd);
  86. $cmd = $icacls . ' ' . $path_escaped . ' /remove:g ' . $user;
  87. exec($cmd);
  88. /*
  89. Required to set no permission and check that is_readable()
  90. returns false. If the $perm_entry contains 'N' skip this step.
  91. This will make the file/dir with NO aceess.
  92. */
  93. if (!in_array('N', $perm_entry)) {
  94. /*
  95. This is required to remove all the previously denied
  96. permission for the USER. Just granting permission doesn't
  97. remove the previously denied permission.
  98. */
  99. $cmd = $icacls . ' ' . $path_escaped . ' /remove:d ' . $user;
  100. exec($cmd);
  101. $cmd = $icacls . ' ' . $path_escaped . ' /remove:g ' . $user;
  102. exec($cmd);
  103. $cmd = $icacls . ' ' . $path_escaped . ' /' . $mode . ' ' . $user;
  104. $cmd .= ':' . '(' . implode($perm_entry, ',') . ')';
  105. exec($cmd);
  106. }
  107. }
  108. function create_dir($name, $perms) {
  109. if (empty($name)) {
  110. echo "create_dir: Empty name is not allowed\n";
  111. return;
  112. }
  113. mkdir($name);
  114. $dst = realpath($name);
  115. icacls_set($name, PHPT_ACL_GRANT, $perms);
  116. }
  117. function create_file($name, $perms) {
  118. if (empty($name)) {
  119. echo "create_file: Empty name is not allowed\n";
  120. return;
  121. }
  122. touch($name);
  123. icacls_set($name, PHPT_ACL_GRANT, $perms);
  124. }
  125. function delete_file($path) {
  126. icacls_set($path, PHPT_ACL_GRANT, PHPT_ACL_FULL);
  127. if (is_file($path)) {
  128. unlink($path);
  129. } else {
  130. echo "delete_file: '$path' is not a file\n";
  131. return;
  132. }
  133. }
  134. function delete_dir($path) {
  135. if (is_dir($path)) {
  136. icacls_set($path, PHPT_ACL_GRANT, PHPT_ACL_FULL);
  137. rmdir($path);
  138. } else {
  139. echo "delete_dir: '$path' is not a directory\n";
  140. return;
  141. }
  142. }
  143. if (0) {
  144. $path = __DIR__ . '/a.txt';
  145. create_file($path, PHPT_ACL_NONE);
  146. if (!is_writable($path)) {
  147. echo "PHPT_ACL_NONE success!!\n";
  148. } else {
  149. echo "PHPT_ACL_NONE failed!!\n";
  150. }
  151. delete_file($path);
  152. $path = __DIR__ . '/a.txt';
  153. create_file($path, PHPT_ACL_READ);
  154. if (!is_writable($path)) {
  155. echo "PHPT_ACL_READ success!!\n";
  156. } else {
  157. echo "PHPT_ACL_READ failed!!\n";
  158. }
  159. delete_file($path);
  160. $path = __DIR__ . '/adir';
  161. create_dir($path, PHPT_ACL_READ);
  162. if (!is_writable($path)) {
  163. echo "PHPT_ACL_READ dir success!!\n";
  164. } else {
  165. echo "PHPT_ACL_READ dir failed!!\n";
  166. }
  167. delete_dir($path);
  168. }