PageRenderTime 2686ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/test-copy-acl.sh

https://gitlab.com/changlimin/gnulib
Shell | 664 lines | 395 code | 155 blank | 114 comment | 32 complexity | 09f8ea6aef2d717dd737bd5d890afccc MD5 | raw file
  1. #!/bin/sh
  2. # Show all commands when run with environment variable VERBOSE=yes.
  3. test -z "$VERBOSE" || set -x
  4. test "$USE_ACL" = 0 &&
  5. {
  6. echo "Skipping test: insufficient ACL support"
  7. exit 77
  8. }
  9. # func_tmpdir
  10. # creates a temporary directory.
  11. # Sets variable
  12. # - tmp pathname of freshly created temporary directory
  13. func_tmpdir ()
  14. {
  15. # Use the environment variable TMPDIR, falling back to /tmp. This allows
  16. # users to specify a different temporary directory, for example, if their
  17. # /tmp is filled up or too small.
  18. : ${TMPDIR=/tmp}
  19. {
  20. # Use the mktemp program if available. If not available, hide the error
  21. # message.
  22. tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
  23. test -n "$tmp" && test -d "$tmp"
  24. } ||
  25. {
  26. # Use a simple mkdir command. It is guaranteed to fail if the directory
  27. # already exists. $RANDOM is bash specific and expands to empty in shells
  28. # other than bash, ksh and zsh. Its use does not increase security;
  29. # rather, it minimizes the probability of failure in a very cluttered /tmp
  30. # directory.
  31. tmp=$TMPDIR/gl$$-$RANDOM
  32. (umask 077 && mkdir "$tmp")
  33. } ||
  34. {
  35. echo "$0: cannot create a temporary directory in $TMPDIR" >&2
  36. exit 1
  37. }
  38. }
  39. func_tmpdir
  40. # builddir may already be set by the script that invokes this one.
  41. case "$builddir" in
  42. '') builddir=`pwd` ;;
  43. /* | ?:*) ;;
  44. *) builddir=`pwd`/$builddir ;;
  45. esac
  46. cd "$builddir" ||
  47. {
  48. echo "$0: cannot determine build directory (unreadable parent dir?)" >&2
  49. exit 1
  50. }
  51. # Switch to a temporary directory, to increase the likelihood that ACLs are
  52. # supported on the current file system. (/tmp is usually locally mounted,
  53. # whereas the build dir is sometimes NFS-mounted.)
  54. ( cd "$tmp"
  55. # Prepare tmpfile0.
  56. rm -f tmpfile[0-9] tmpaclout[0-2]
  57. echo "Simple contents" > tmpfile0
  58. chmod 600 tmpfile0
  59. # Classification of the platform according to the programs available for
  60. # manipulating ACLs.
  61. # Possible values are:
  62. # linux, cygwin, freebsd, solaris, hpux, hpuxjfs, osf1, aix, macosx, irix, none.
  63. # TODO: Support also native Windows platforms (mingw).
  64. acl_flavor=none
  65. if (getfacl tmpfile0 >/dev/null) 2>/dev/null; then
  66. # Platforms with the getfacl and setfacl programs.
  67. # Linux, FreeBSD, Solaris, Cygwin.
  68. if (setfacl --help >/dev/null) 2>/dev/null; then
  69. # Linux, Cygwin.
  70. if (LC_ALL=C setfacl --help | grep ' --set-file' >/dev/null) 2>/dev/null; then
  71. # Linux.
  72. acl_flavor=linux
  73. else
  74. acl_flavor=cygwin
  75. fi
  76. else
  77. # FreeBSD, Solaris.
  78. if (LC_ALL=C setfacl 2>&1 | grep '\-x entries' >/dev/null) 2>/dev/null; then
  79. # FreeBSD.
  80. acl_flavor=freebsd
  81. else
  82. # Solaris.
  83. acl_flavor=solaris
  84. fi
  85. fi
  86. else
  87. if (lsacl / >/dev/null) 2>/dev/null; then
  88. # Platforms with the lsacl and chacl programs.
  89. # HP-UX, sometimes also IRIX.
  90. if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
  91. # HP-UX 11.11 or newer.
  92. acl_flavor=hpuxjfs
  93. else
  94. # HP-UX 11.00.
  95. acl_flavor=hpux
  96. fi
  97. else
  98. if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
  99. # Tru64, NonStop Kernel.
  100. if (getacl -m tmpfile0 >/dev/null) 2>/dev/null; then
  101. # Tru64.
  102. acl_flavor=osf1
  103. else
  104. # NonStop Kernel.
  105. acl_flavor=nsk
  106. fi
  107. else
  108. if (aclget tmpfile0 >/dev/null) 2>/dev/null; then
  109. # AIX.
  110. acl_flavor=aix
  111. else
  112. if (fsaclctl -v >/dev/null) 2>/dev/null; then
  113. # Mac OS X.
  114. acl_flavor=macosx
  115. else
  116. if test -f /sbin/chacl; then
  117. # IRIX.
  118. acl_flavor=irix
  119. fi
  120. fi
  121. fi
  122. fi
  123. fi
  124. fi
  125. # Define a function to test for the same ACLs, from the point of view of
  126. # the programs.
  127. # func_test_same_acls file1 file2
  128. case $acl_flavor in
  129. linux | cygwin | freebsd | solaris)
  130. func_test_same_acls ()
  131. {
  132. getfacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
  133. getfacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
  134. cmp tmpaclout1 tmpaclout2 > /dev/null
  135. }
  136. ;;
  137. hpux)
  138. func_test_same_acls ()
  139. {
  140. lsacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
  141. lsacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
  142. cmp tmpaclout1 tmpaclout2 > /dev/null
  143. }
  144. ;;
  145. hpuxjfs)
  146. func_test_same_acls ()
  147. {
  148. { lsacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
  149. lsacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
  150. cmp tmpaclout1 tmpaclout2 > /dev/null
  151. } &&
  152. { getacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
  153. getacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
  154. cmp tmpaclout1 tmpaclout2 > /dev/null
  155. }
  156. }
  157. ;;
  158. osf1 | nsk)
  159. func_test_same_acls ()
  160. {
  161. getacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
  162. getacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
  163. cmp tmpaclout1 tmpaclout2 > /dev/null
  164. }
  165. ;;
  166. aix)
  167. func_test_same_acls ()
  168. {
  169. aclget "$1" > tmpaclout1
  170. aclget "$2" > tmpaclout2
  171. cmp tmpaclout1 tmpaclout2 > /dev/null
  172. }
  173. ;;
  174. macosx)
  175. func_test_same_acls ()
  176. {
  177. /bin/ls -le "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
  178. /bin/ls -le "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
  179. cmp tmpaclout1 tmpaclout2 > /dev/null
  180. }
  181. ;;
  182. irix)
  183. func_test_same_acls ()
  184. {
  185. /bin/ls -lD "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
  186. /bin/ls -lD "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
  187. cmp tmpaclout1 tmpaclout2 > /dev/null
  188. }
  189. ;;
  190. none)
  191. func_test_same_acls ()
  192. {
  193. :
  194. }
  195. ;;
  196. esac
  197. # func_test_copy file1 file2
  198. # copies file1 to file2 and verifies the permissions and ACLs are the same
  199. # on both.
  200. func_test_copy ()
  201. {
  202. echo "Simple contents" > "$2"
  203. chmod 600 "$2"
  204. ${CHECKER} "$builddir"/test-copy-acl${EXEEXT} "$1" "$2" || exit 1
  205. ${CHECKER} "$builddir"/test-sameacls${EXEEXT} "$1" "$2" || exit 1
  206. func_test_same_acls "$1" "$2" || exit 1
  207. }
  208. func_test_copy tmpfile0 tmpfile1
  209. if test $acl_flavor != none; then
  210. # A POSIX compliant 'id' program.
  211. if test -f /usr/xpg4/bin/id; then
  212. ID=/usr/xpg4/bin/id
  213. else
  214. ID=id
  215. fi
  216. # Use a user and group id different from the current one, to avoid
  217. # redundant/ambiguous ACLs.
  218. myuid=`$ID -u`
  219. mygid=`$ID -g`
  220. auid=1
  221. if test "$auid" = "$myuid"; then auid=2; fi
  222. agid=1
  223. if test "$agid" = "$mygid"; then agid=2; fi
  224. case $acl_flavor in
  225. linux | freebsd | solaris)
  226. # Set an ACL for a user.
  227. setfacl -m user:$auid:1 tmpfile0
  228. func_test_copy tmpfile0 tmpfile2
  229. # Set an ACL for a group.
  230. setfacl -m group:$agid:4 tmpfile0
  231. func_test_copy tmpfile0 tmpfile3
  232. # Set an ACL for other.
  233. case $acl_flavor in
  234. freebsd) setfacl -m other::4 tmpfile0 ;;
  235. solaris) chmod o+r tmpfile0 ;;
  236. *) setfacl -m other:4 tmpfile0 ;;
  237. esac
  238. func_test_copy tmpfile0 tmpfile4
  239. # Remove the ACL for the user.
  240. case $acl_flavor in
  241. linux) setfacl -x user:$auid tmpfile0 ;;
  242. freebsd) setfacl -x user:$auid:1 tmpfile0 ;;
  243. *) setfacl -d user:$auid:1 tmpfile0 ;;
  244. esac
  245. func_test_copy tmpfile0 tmpfile5
  246. # Remove the ACL for other.
  247. case $acl_flavor in
  248. linux | solaris) ;; # impossible
  249. freebsd) setfacl -x other::4 tmpfile0 ;;
  250. *) setfacl -d other:4 tmpfile0 ;;
  251. esac
  252. func_test_copy tmpfile0 tmpfile6
  253. # Remove the ACL for the group.
  254. case $acl_flavor in
  255. linux) setfacl -x group:$agid tmpfile0 ;;
  256. freebsd) setfacl -x group:$agid:4 tmpfile0 ;;
  257. *) setfacl -d group:$agid:4 tmpfile0 ;;
  258. esac
  259. func_test_copy tmpfile0 tmpfile7
  260. # Delete all optional ACLs.
  261. case $acl_flavor in
  262. linux | freebsd)
  263. setfacl -m user:$auid:1 tmpfile0
  264. setfacl -b tmpfile0
  265. ;;
  266. *)
  267. setfacl -s user::6,group::0,other:0 tmpfile0 ;;
  268. esac
  269. func_test_copy tmpfile0 tmpfile8
  270. # Copy ACLs from a file that has no ACLs.
  271. echo > tmpfile9
  272. chmod a+x tmpfile9
  273. case $acl_flavor in
  274. linux) getfacl tmpfile9 | setfacl --set-file=- tmpfile0 ;;
  275. freebsd) ;;
  276. *) getfacl tmpfile9 | setfacl -f - tmpfile0 ;;
  277. esac
  278. rm -f tmpfile9
  279. func_test_copy tmpfile0 tmpfile9
  280. ;;
  281. cygwin)
  282. # Set an ACL for a group.
  283. setfacl -m group:0:1 tmpfile0
  284. func_test_copy tmpfile0 tmpfile2
  285. # Set an ACL for other.
  286. setfacl -m other:4 tmpfile0
  287. func_test_copy tmpfile0 tmpfile4
  288. # Remove the ACL for the group.
  289. setfacl -d group:0 tmpfile0
  290. func_test_copy tmpfile0 tmpfile5
  291. # Remove the ACL for other.
  292. setfacl -d other:4 tmpfile0
  293. func_test_copy tmpfile0 tmpfile6
  294. # Delete all optional ACLs.
  295. setfacl -s user::6,group::0,other:0 tmpfile0
  296. func_test_copy tmpfile0 tmpfile8
  297. # Copy ACLs from a file that has no ACLs.
  298. echo > tmpfile9
  299. chmod a+x tmpfile9
  300. getfacl tmpfile9 | setfacl -f - tmpfile0
  301. rm -f tmpfile9
  302. func_test_copy tmpfile0 tmpfile9
  303. ;;
  304. hpux)
  305. # Set an ACL for a user.
  306. orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
  307. chacl -r "${orig}($auid.%,--x)" tmpfile0
  308. func_test_copy tmpfile0 tmpfile2
  309. # Set an ACL for a group.
  310. orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
  311. chacl -r "${orig}(%.$agid,r--)" tmpfile0
  312. func_test_copy tmpfile0 tmpfile3
  313. # Set an ACL for other.
  314. orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
  315. chacl -r "${orig}(%.%,r--)" tmpfile0
  316. func_test_copy tmpfile0 tmpfile4
  317. # Remove the ACL for the user.
  318. chacl -d "($auid.%,--x)" tmpfile0
  319. func_test_copy tmpfile0 tmpfile5
  320. # Remove the ACL for the group.
  321. chacl -d "(%.$agid,r--)" tmpfile0
  322. func_test_copy tmpfile0 tmpfile6
  323. # Delete all optional ACLs.
  324. chacl -z tmpfile0
  325. func_test_copy tmpfile0 tmpfile8
  326. # Copy ACLs from a file that has no ACLs.
  327. echo > tmpfile9
  328. chmod a+x tmpfile9
  329. orig=`lsacl tmpfile9 | sed -e 's/ tmpfile9$//'`
  330. rm -f tmpfile9
  331. chacl -r "${orig}" tmpfile0
  332. func_test_copy tmpfile0 tmpfile9
  333. ;;
  334. hpuxjfs)
  335. # Set an ACL for a user.
  336. orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
  337. chacl -r "${orig}($auid.%,--x)" tmpfile0 \
  338. || setacl -m user:$auid:1 tmpfile0
  339. func_test_copy tmpfile0 tmpfile2
  340. # Set an ACL for a group.
  341. orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
  342. chacl -r "${orig}(%.$agid,r--)" tmpfile0 \
  343. || setacl -m group:$agid:4 tmpfile0
  344. func_test_copy tmpfile0 tmpfile3
  345. # Set an ACL for other.
  346. orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
  347. chacl -r "${orig}(%.%,r--)" tmpfile0 \
  348. || setacl -m other:4 tmpfile0
  349. func_test_copy tmpfile0 tmpfile4
  350. # Remove the ACL for the user.
  351. chacl -d "($auid.%,--x)" tmpfile0 \
  352. || setacl -d user:$auid tmpfile0
  353. func_test_copy tmpfile0 tmpfile5
  354. # Remove the ACL for the group.
  355. chacl -d "(%.$agid,r--)" tmpfile0 \
  356. || setacl -d group:$agid tmpfile0
  357. func_test_copy tmpfile0 tmpfile6
  358. # Delete all optional ACLs.
  359. chacl -z tmpfile0 \
  360. || { setacl -m user:$auid:1 tmpfile0
  361. setacl -s user::6,group::0,class:7,other:0 tmpfile0
  362. }
  363. func_test_copy tmpfile0 tmpfile8
  364. # Copy ACLs from a file that has no ACLs.
  365. echo > tmpfile9
  366. chmod a+x tmpfile9
  367. orig=`lsacl tmpfile9 | sed -e 's/ tmpfile9$//'`
  368. getacl tmpfile9 > tmpaclout0
  369. rm -f tmpfile9
  370. chacl -r "${orig}" tmpfile0 \
  371. || setacl -f tmpaclout0 tmpfile0
  372. func_test_copy tmpfile0 tmpfile9
  373. ;;
  374. osf1)
  375. # Set an ACL for a user.
  376. setacl -u user:$auid:1 tmpfile0
  377. func_test_copy tmpfile0 tmpfile2
  378. # Set an ACL for a group.
  379. setacl -u group:$agid:4 tmpfile0
  380. func_test_copy tmpfile0 tmpfile3
  381. # Set an ACL for other.
  382. setacl -u other::4 tmpfile0
  383. func_test_copy tmpfile0 tmpfile4
  384. # Remove the ACL for the user.
  385. setacl -x user:$auid:1 tmpfile0
  386. func_test_copy tmpfile0 tmpfile5
  387. if false; then # would give an error "can't set ACL: Invalid argument"
  388. # Remove the ACL for other.
  389. setacl -x other::4 tmpfile0
  390. func_test_copy tmpfile0 tmpfile6
  391. fi
  392. # Remove the ACL for the group.
  393. setacl -x group:$agid:4 tmpfile0
  394. func_test_copy tmpfile0 tmpfile7
  395. # Delete all optional ACLs.
  396. setacl -u user:$auid:1 tmpfile0
  397. setacl -b tmpfile0
  398. func_test_copy tmpfile0 tmpfile8
  399. # Copy ACLs from a file that has no ACLs.
  400. echo > tmpfile9
  401. chmod a+x tmpfile9
  402. getacl tmpfile9 > tmpaclout0
  403. setacl -b -U tmpaclout0 tmpfile0
  404. rm -f tmpfile9
  405. func_test_copy tmpfile0 tmpfile9
  406. ;;
  407. nsk)
  408. # Set an ACL for a user.
  409. setacl -m user:$auid:1 tmpfile0
  410. func_test_copy tmpfile0 tmpfile2
  411. # Set an ACL for a group.
  412. setacl -m group:$agid:4 tmpfile0
  413. func_test_copy tmpfile0 tmpfile3
  414. # Set an ACL for other.
  415. setacl -m other:4 tmpfile0
  416. func_test_copy tmpfile0 tmpfile4
  417. # Remove the ACL for the user.
  418. setacl -d user:$auid tmpfile0
  419. func_test_copy tmpfile0 tmpfile5
  420. # Remove the ACL for the group.
  421. setacl -d group:$agid tmpfile0
  422. func_test_copy tmpfile0 tmpfile6
  423. # Delete all optional ACLs.
  424. setacl -m user:$auid:1 tmpfile0
  425. setacl -s user::6,group::0,class:7,other:0 tmpfile0
  426. func_test_copy tmpfile0 tmpfile8
  427. # Copy ACLs from a file that has no ACLs.
  428. echo > tmpfile9
  429. chmod a+x tmpfile9
  430. getacl tmpfile9 > tmpaclout0
  431. setacl -f tmpaclout0 tmpfile0
  432. rm -f tmpfile9
  433. func_test_copy tmpfile0 tmpfile9
  434. ;;
  435. aix)
  436. # Set an ACL for a user.
  437. { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo " permit --x u:$auid"; } | aclput tmpfile0
  438. func_test_copy tmpfile0 tmpfile2
  439. # Set an ACL for a group.
  440. { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo " permit r-- g:$agid"; } | aclput tmpfile0
  441. func_test_copy tmpfile0 tmpfile3
  442. # Set an ACL for other.
  443. chmod o+r tmpfile0
  444. func_test_copy tmpfile0 tmpfile4
  445. # Remove the ACL for the user.
  446. aclget tmpfile0 | grep -v ' u:[^ ]*$' | aclput tmpfile0
  447. func_test_copy tmpfile0 tmpfile5
  448. # Remove the ACL for the group.
  449. aclget tmpfile0 | grep -v ' g:[^ ]*$' | aclput tmpfile0
  450. func_test_copy tmpfile0 tmpfile7
  451. # Delete all optional ACLs.
  452. aclget tmpfile0 | sed -e 's/enabled$/disabled/' | sed -e '/disabled$/q' | aclput tmpfile0
  453. func_test_copy tmpfile0 tmpfile8
  454. # Copy ACLs from a file that has no ACLs.
  455. echo > tmpfile9
  456. chmod a+x tmpfile9
  457. aclget tmpfile9 | aclput tmpfile0
  458. rm -f tmpfile9
  459. func_test_copy tmpfile0 tmpfile9
  460. ;;
  461. macosx)
  462. # Set an ACL for a user.
  463. /bin/chmod +a "user:daemon allow execute" tmpfile0
  464. func_test_copy tmpfile0 tmpfile2
  465. # Set an ACL for a group.
  466. /bin/chmod +a "group:daemon allow read" tmpfile0
  467. func_test_copy tmpfile0 tmpfile3
  468. # Set an ACL for other.
  469. chmod o+r tmpfile0
  470. func_test_copy tmpfile0 tmpfile4
  471. # Remove the ACL for the user.
  472. /bin/chmod -a "user:daemon allow execute" tmpfile0
  473. func_test_copy tmpfile0 tmpfile5
  474. # Remove the ACL for the group.
  475. /bin/chmod -a "group:daemon allow read" tmpfile0
  476. func_test_copy tmpfile0 tmpfile7
  477. # Delete all optional ACLs.
  478. /bin/chmod -N tmpfile0
  479. func_test_copy tmpfile0 tmpfile8
  480. # Copy ACLs from a file that has no ACLs.
  481. echo > tmpfile9
  482. chmod a+x tmpfile9
  483. { /bin/ls -le tmpfile9 | sed -n -e 's/^ [0-9][0-9]*: //p'; echo; } | /bin/chmod -E tmpfile0
  484. rm -f tmpfile9
  485. func_test_copy tmpfile0 tmpfile9
  486. ;;
  487. irix)
  488. # Set an ACL for a user.
  489. /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x tmpfile0
  490. func_test_copy tmpfile0 tmpfile2
  491. # Set an ACL for a group.
  492. /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x,group:$agid:r-- tmpfile0
  493. func_test_copy tmpfile0 tmpfile3
  494. # Set an ACL for other.
  495. /sbin/chacl user::rw-,group::---,user:$auid:--x,group:$agid:r--,other::r-- tmpfile0
  496. func_test_copy tmpfile0 tmpfile4
  497. # Remove the ACL for the user.
  498. /sbin/chacl user::rw-,group::---,group:$agid:r--,other::r-- tmpfile0
  499. func_test_copy tmpfile0 tmpfile5
  500. # Remove the ACL for the group.
  501. /sbin/chacl user::rw-,group::---,other::r-- tmpfile0
  502. func_test_copy tmpfile0 tmpfile7
  503. ;;
  504. esac
  505. fi
  506. rm -f tmpfile[0-9] tmpaclout[0-2]
  507. ) || exit 1
  508. rm -rf "$tmp"
  509. exit 0