1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
6 *
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
10 */
11
12 /*
13 * Copyright 2024 RackTop Systems, Inc.
14 */
15
16 /*
17 * Test program for libsec:acl_fromtext
18 */
19
20 #include <sys/types.h>
21 #include <sys/acl.h>
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <unistd.h>
29
30 extern char *acl_strerror(int);
31 extern acl_t acl_canned;
32
33 int
acl_compare(acl_t * lib,acl_t * ref)34 acl_compare(acl_t *lib, acl_t *ref)
35 {
36 ace_t *lib_acep;
37 ace_t *ref_acep;
38 int errs;
39 int i;
40
41 if (lib->acl_type != ref->acl_type) {
42 fprintf(stderr, "acl_type = %d (expected %d)\n",
43 lib->acl_type, ref->acl_type);
44 return (-1);
45 }
46 if (lib->acl_cnt != ref->acl_cnt) {
47 fprintf(stderr, "acl_cnt = %d (expected %d)\n",
48 lib->acl_cnt, ref->acl_cnt);
49 return (-1);
50 }
51 if (lib->acl_entry_size != ref->acl_entry_size) {
52 fprintf(stderr, "acl_entry_size = %d (expected %d)\n",
53 lib->acl_entry_size, ref->acl_entry_size);
54 return (-1);
55 }
56
57 lib_acep = lib->acl_aclp;
58 ref_acep = ref->acl_aclp;
59 errs = 0;
60 for (i = 0; i < lib->acl_cnt; i++) {
61 if (lib_acep->a_who != ref_acep->a_who) {
62 fprintf(stderr, "ace[%d].a_who = %u"
63 " (expected %u)\n", i,
64 lib_acep->a_who,
65 ref_acep->a_who);
66 errs++;
67 }
68 if (lib_acep->a_access_mask != ref_acep->a_access_mask) {
69 fprintf(stderr, "ace[%d].a_access_mask = 0x%x"
70 " (expected 0x%x)\n", i,
71 lib_acep->a_access_mask,
72 ref_acep->a_access_mask);
73 errs++;
74 }
75 if (lib_acep->a_flags != ref_acep->a_flags) {
76 fprintf(stderr, "ace[%d].a_flags = %u"
77 " (expected %u)\n", i,
78 lib_acep->a_flags,
79 ref_acep->a_flags);
80 errs++;
81 }
82 if (lib_acep->a_type != ref_acep->a_type) {
83 fprintf(stderr, "ace[%d].a_type = %u"
84 " (expected %u)\n", i,
85 lib_acep->a_type,
86 ref_acep->a_type);
87 errs++;
88 }
89 lib_acep++;
90 ref_acep++;
91 }
92 return (errs);
93 }
94
95 int
main(int argc,char ** argv)96 main(int argc, char **argv)
97 {
98 acl_t *aclp = NULL; /* acl info */
99 char *str;
100 char *p;
101 int i, err;
102
103 for (i = 1; i < argc; i++) {
104 /*
105 * Allow input with newlines instead of commas.
106 */
107 str = strdup(argv[i]);
108 if (str == NULL) {
109 perror("strdup");
110 return (1);
111 }
112 for (p = str; *p != '\0'; p++) {
113 if (*p == '\n')
114 *p = ',';
115 }
116
117 err = acl_fromtext(str, &aclp);
118 if (err != 0) {
119 fprintf(stderr, "acl_fromtext(%s): %s\n",
120 str, acl_strerror(err));
121 return (1);
122 }
123
124 if (acl_compare(aclp, &acl_canned) != 0) {
125 fprintf(stderr, "compare failed on: %s\n", str);
126 return (1);
127 }
128
129 acl_free(aclp);
130 aclp = NULL;
131 }
132
133 return (0);
134 }
135