fstests: Add an auxiliary program to create an AF_UNIX socket
[xfstests-dev.git] / src / acl_get.c
1 /*
2  * Copyright (c) 2001-2002 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would 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.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /*
20  * Get an access or default acl on a file
21  * using IRIX semantics or Linux semantics
22  */
23  
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <getopt.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/acl.h>
33 #include <acl/libacl.h>
34
35 char *prog;
36
37 void usage(void)
38 {
39     fprintf(stderr, "usage: %s [-a] [-d] [-f] [-i] path\n"
40            "flags:\n"
41            "    -a - get access ACL\n"
42            "    -d - get default ACL\n" 
43            "    -f - get access ACL using file descriptor\n" 
44            ,prog);
45            
46 }
47
48
49
50 int
51 main(int argc, char **argv)
52 {
53         int c;
54         char *file;
55         char *acl_text;
56         int getaccess = 0;
57         int getdefault = 0;
58         int usefd = 0;
59         int fd = -1;
60         acl_t acl;
61
62         prog = basename(argv[0]);
63
64         while ((c = getopt(argc, argv, "adf")) != -1) {
65                 switch (c) {
66                 case 'a':
67                         getaccess = 1;
68                         break;
69                 case 'd':
70                         getdefault = 1;
71                         break;
72                 case 'f':
73                         usefd = 1;
74                         break;
75                 case '?':
76                         usage();
77                         return 1;
78                 }
79         }
80
81         if (getdefault && usefd) {
82             fprintf(stderr, "%s: -f and -d are not compatible\n", prog);
83             return 1;
84         }
85
86         /* need path */
87         if (optind == argc) {
88             usage();
89             exit(1);
90         }
91         else {
92             file = argv[optind];
93         } 
94
95         if (usefd) {
96             fd = open(file, O_RDONLY);
97             if (fd < 0) {
98                 fprintf (stderr, "%s: error opening \"%s\": %s\n",
99                              prog, file, strerror(errno));
100                 usage(); 
101                 return 1;
102
103             }   
104         }
105
106         if (getaccess) {
107             if (usefd) { 
108                 acl = acl_get_fd(fd);
109             }
110             else {
111                 acl = acl_get_file(file, ACL_TYPE_ACCESS);
112             }
113             if (acl == NULL) {
114                 fprintf(stderr, "%s: error getting access ACL on \"%s\": %s\n",
115                              prog, file, strerror(errno));
116                 return 0;
117             }
118             acl_text = acl_to_any_text(acl, NULL, ',', TEXT_ABBREVIATE);
119             if (acl_text == NULL) {
120                 fprintf(stderr, "%s: cannot get access ACL text on '%s': %s\n",
121                         prog, file, strerror(errno));
122                 return 0;
123             }
124             printf("%s: access %s", file, acl_text);
125             acl_free(acl_text);
126             acl_free(acl);
127         }
128
129         if (getdefault) {
130             acl = acl_get_file(file, ACL_TYPE_DEFAULT);
131             if (acl == NULL) {
132                 fprintf(stderr, "%s: error getting default ACL on \"%s\": %s\n",
133                              prog, file, strerror(errno));
134                 return 0;
135             }
136             acl_text = acl_to_any_text(acl, NULL, ',', TEXT_ABBREVIATE);
137             if (acl_text == NULL) {
138                 fprintf(stderr, "%s: cannot get default ACL text on '%s': %s\n",
139                         prog, file, strerror(errno));
140                 return 0;
141             }
142             printf("%s: default %s", file, acl_text);
143             acl_free(acl_text);
144             acl_free(acl);
145         }
146
147         return 0;
148 }