1 /* Test whether two files have the same ACLs.
2 Copyright (C) 2008 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
27 #if HAVE_ACL_GET_FILE || HAVE_ACL || HAVE_ACLX_GET || HAVE_STATACL
28 # include <sys/types.h>
33 #include "read-file.h"
36 #define ASSERT(expr) \
41 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
49 main (int argc, char *argv[])
54 set_program_name (argv[0]);
61 /* Compare the contents of the two files. */
68 contents1 = read_file (file1, &size1);
69 if (contents1 == NULL)
71 fprintf (stderr, "error reading file %s: errno = %d\n", file1, errno);
75 contents2 = read_file (file2, &size2);
76 if (contents2 == NULL)
78 fprintf (stderr, "error reading file %s: errno = %d\n", file2, errno);
85 fprintf (stderr, "files %s and %s have different sizes\n",
90 if (memcmp (contents1, contents2, size1) != 0)
92 fprintf (stderr, "files %s and %s have different contents\n",
99 /* Compare the access permissions of the two files, including ACLs. */
101 struct stat statbuf1;
102 struct stat statbuf2;
104 if (stat (file1, &statbuf1) < 0)
106 fprintf (stderr, "error accessing file %s: errno = %d\n", file1, errno);
110 if (stat (file2, &statbuf2) < 0)
112 fprintf (stderr, "error accessing file %s: errno = %d\n", file2, errno);
116 if (statbuf1.st_mode != statbuf2.st_mode)
118 fprintf (stderr, "files %s and %s have different access modes: %03o and %03o\n",
120 (unsigned int) statbuf1.st_mode, (unsigned int) statbuf2.st_mode);
125 #if HAVE_ACL_GET_FILE /* Linux, FreeBSD, MacOS X, IRIX, Tru64 */
126 static const int types[] =
129 # if HAVE_ACL_TYPE_EXTENDED /* MacOS X */
135 for (t = 0; t < sizeof (types) / sizeof (types[0]); t++)
145 acl1 = acl_get_file (file1, type);
146 if (acl1 == (acl_t)NULL)
153 text1 = acl_to_text (acl1, NULL);
159 acl2 = acl_get_file (file2, type);
160 if (acl2 == (acl_t)NULL)
167 text2 = acl_to_text (acl2, NULL);
174 if (acl1 != (acl_t)NULL)
176 if (acl2 != (acl_t)NULL)
182 if (strcmp (text1, text2) != 0)
184 fprintf (stderr, "files %s and %s have different ACLs:\n%s\n%s\n",
185 file1, file2, text1, text2);
191 fprintf (stderr, "file %s has a valid ACL, but file %s has an invalid ACL\n",
200 fprintf (stderr, "file %s has an invalid ACL, but file %s has a valid ACL\n",
206 if (errno1 != errno2)
208 fprintf (stderr, "files %s and %s have differently invalid ACLs, errno = %d vs. %d\n",
209 file1, file2, errno1, errno2);
217 fprintf (stderr, "file %s has an ACL, but file %s has no ACL\n",
224 if (acl2 != (acl_t)NULL)
226 fprintf (stderr, "file %s has no ACL, but file %s has an ACL\n",
232 #elif HAVE_ACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
236 count1 = acl (file1, GETACLCNT, 0, NULL);
237 count2 = acl (file2, GETACLCNT, 0, NULL);
241 fprintf (stderr, "error accessing the ACLs of file %s\n", file1);
247 fprintf (stderr, "error accessing the ACLs of file %s\n", file2);
251 if (count1 != count2)
253 fprintf (stderr, "files %s and %s have different number of ACLs: %d and %d\n",
254 file1, file2, count1, count2);
259 aclent_t *entries1 = XNMALLOC (count1, aclent_t);
260 aclent_t *entries2 = XNMALLOC (count2, aclent_t);
263 if (acl (file1, GETACL, count1, entries1) < count1)
265 fprintf (stderr, "error retrieving the ACLs of file %s\n", file1);
269 if (acl (file2, GETACL, count2, entries2) < count1)
271 fprintf (stderr, "error retrieving the ACLs of file %s\n", file2);
275 for (i = 0; i < count1; i++)
277 if (entries1[i].a_type != entries2[i].a_type)
279 fprintf (stderr, "files %s and %s: different ACL entry #%d: different types %d and %d\n",
280 file1, file2, i, entries1[i].a_type, entries2[i].a_type);
283 if (entries1[i].a_id != entries2[i].a_id)
285 fprintf (stderr, "files %s and %s: different ACL entry #%d: different ids %d and %d\n",
286 file1, file2, i, (int)entries1[i].a_id, (int)entries2[i].a_id);
289 if (entries1[i].a_perm != entries2[i].a_perm)
291 fprintf (stderr, "files %s and %s: different ACL entry #%d: different permissions %03o and %03o\n",
292 file1, file2, i, (unsigned int) entries1[i].a_perm, (unsigned int) entries2[i].a_perm);
298 count1 = acl (file1, ACE_GETACLCNT, 0, NULL);
299 if (count1 < 0 && errno == EINVAL)
301 count2 = acl (file2, ACE_GETACLCNT, 0, NULL);
302 if (count2 < 0 && errno == EINVAL)
306 fprintf (stderr, "error accessing the ACE-ACLs of file %s\n", file1);
312 fprintf (stderr, "error accessing the ACE-ACLs of file %s\n", file2);
316 if (count1 != count2)
318 fprintf (stderr, "files %s and %s have different number of ACE-ACLs: %d and %d\n",
319 file1, file2, count1, count2);
324 ace_t *entries1 = XNMALLOC (count1, ace_t);
325 ace_t *entries2 = XNMALLOC (count2, ace_t);
328 if (acl (file1, ACE_GETACL, count1, entries1) < count1)
330 fprintf (stderr, "error retrieving the ACE-ACLs of file %s\n", file1);
334 if (acl (file2, ACE_GETACL, count2, entries2) < count1)
336 fprintf (stderr, "error retrieving the ACE-ACLs of file %s\n", file2);
340 for (i = 0; i < count1; i++)
342 if (entries1[i].a_type != entries2[i].a_type)
344 fprintf (stderr, "files %s and %s: different ACE-ACL entry #%d: different types %d and %d\n",
345 file1, file2, i, entries1[i].a_type, entries2[i].a_type);
348 if (entries1[i].a_who != entries2[i].a_who)
350 fprintf (stderr, "files %s and %s: different ACE-ACL entry #%d: different ids %d and %d\n",
351 file1, file2, i, (int)entries1[i].a_who, (int)entries2[i].a_who);
354 if (entries1[i].a_access_mask != entries2[i].a_access_mask)
356 fprintf (stderr, "files %s and %s: different ACE-ACL entry #%d: different access masks %03o and %03o\n",
357 file1, file2, i, (unsigned int) entries1[i].a_access_mask, (unsigned int) entries2[i].a_access_mask);
360 if (entries1[i].a_flags != entries2[i].a_flags)
362 fprintf (stderr, "files %s and %s: different ACE-ACL entry #%d: different flags 0x%x and 0x%x\n",
363 file1, file2, i, (unsigned int) entries1[i].a_flags, (unsigned int) entries2[i].a_flags);
369 #elif HAVE_GETACL /* HP-UX */
373 count1 = getacl (file1, 0, NULL);
374 if (count1 < 0 && (errno == ENOSYS || errno == EOPNOTSUPP))
376 count2 = getacl (file2, 0, NULL);
377 if (count2 < 0 && (errno == ENOSYS || errno == EOPNOTSUPP))
382 fprintf (stderr, "error accessing the ACLs of file %s\n", file1);
388 fprintf (stderr, "error accessing the ACLs of file %s\n", file2);
392 if (count1 != count2)
394 fprintf (stderr, "files %s and %s have different number of ACLs: %d and %d\n",
395 file1, file2, count1, count2);
400 struct acl_entry *entries1 = XNMALLOC (count1, struct acl_entry);
401 struct acl_entry *entries2 = XNMALLOC (count2, struct acl_entry);
404 if (getacl (file1, count1, entries1) < count1)
406 fprintf (stderr, "error retrieving the ACLs of file %s\n", file1);
410 if (getacl (file2, count2, entries2) < count1)
412 fprintf (stderr, "error retrieving the ACLs of file %s\n", file2);
416 for (i = 0; i < count1; i++)
418 if (entries1[i].uid != entries2[i].uid)
420 fprintf (stderr, "files %s and %s: different ACL entry #%d: different uids %d and %d\n",
421 file1, file2, i, (int)entries1[i].uid, (int)entries2[i].uid);
424 if (entries1[i].gid != entries2[i].gid)
426 fprintf (stderr, "files %s and %s: different ACL entry #%d: different gids %d and %d\n",
427 file1, file2, i, (int)entries1[i].gid, (int)entries2[i].gid);
430 if (entries1[i].mode != entries2[i].mode)
432 fprintf (stderr, "files %s and %s: different ACL entry #%d: different permissions %03o and %03o\n",
433 file1, file2, i, (unsigned int) entries1[i].mode, (unsigned int) entries2[i].mode);
438 #elif HAVE_ACLX_GET /* AIX */
441 size_t aclsize1 = sizeof (acl1);
446 size_t aclsize2 = sizeof (acl2);
450 /* The docs say that type1 being 0 is equivalent to ACL_ANY, but it is not
453 if (aclx_get (file1, 0, &type1, acl1, &aclsize1, &mode1) < 0)
455 fprintf (stderr, "error accessing the ACLs of file %s\n", file1);
459 if (aclx_printStr (text1, sizeof (text1), acl1, aclsize1, type1, file1, 0) < 0)
461 fprintf (stderr, "cannot convert the ACLs of file %s to text\n", file1);
466 /* The docs say that type2 being 0 is equivalent to ACL_ANY, but it is not
469 if (aclx_get (file2, 0, &type2, acl2, &aclsize2, &mode2) < 0)
471 fprintf (stderr, "error accessing the ACLs of file %s\n", file2);
475 if (aclx_printStr (text2, sizeof (text2), acl2, aclsize2, type2, file2, 0) < 0)
477 fprintf (stderr, "cannot convert the ACLs of file %s to text\n", file2);
482 if (strcmp (text1, text2) != 0)
484 fprintf (stderr, "files %s and %s have different ACLs:\n%s\n%s\n",
485 file1, file2, text1, text2);
488 #elif HAVE_STATACL /* older AIX */
489 union { struct acl a; char room[4096]; } acl1;
490 union { struct acl a; char room[4096]; } acl2;
493 if (statacl (file1, STX_NORMAL, &acl1.a, sizeof (acl1)) < 0)
495 fprintf (stderr, "error accessing the ACLs of file %s\n", file1);
499 if (statacl (file2, STX_NORMAL, &acl2.a, sizeof (acl2)) < 0)
501 fprintf (stderr, "error accessing the ACLs of file %s\n", file2);
506 if (acl1.a.acl_len != acl2.a.acl_len)
508 fprintf (stderr, "files %s and %s have different ACL lengths: %u and %u\n",
509 file1, file2, acl1.a.acl_len, acl2.a.acl_len);
512 if (acl1.a.acl_mode != acl2.a.acl_mode)
514 fprintf (stderr, "files %s and %s have different ACL modes: %03o and %03o\n",
515 file1, file2, acl1.a.acl_mode, acl2.a.acl_mode);
518 if (acl1.a.u_access != acl2.a.u_access
519 || acl1.a.g_access != acl2.a.g_access
520 || acl1.a.o_access != acl2.a.o_access)
522 fprintf (stderr, "files %s and %s have different ACL access masks: %03o %03o %03o and %03o %03o %03o\n",
524 acl1.a.u_access, acl1.a.g_access, acl1.a.o_access,
525 acl2.a.u_access, acl2.a.g_access, acl2.a.o_access);
528 if (memcmp (acl1.a.acl_ext, acl2.a.acl_ext, acl1.a.acl_len) != 0)
530 fprintf (stderr, "files %s and %s have different ACL entries\n",