]> sourceware.org Git - newlib-cygwin.git/blob - winsup/utils/getfacl.c
Cygwin: add 3.2.1 release file and add fixes up to this point
[newlib-cygwin.git] / winsup / utils / getfacl.c
1 /* getfacl.c
2
3 Written by Corinna Vinschen <vinschen@redhat.com>
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include <pwd.h>
12 #include <grp.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <getopt.h>
17 #include <sys/acl.h>
18 #include <acl/libacl.h>
19 #include <sys/stat.h>
20 #include <cygwin/version.h>
21 #include <string.h>
22 #include <errno.h>
23
24 static char *prog_name;
25
26 const char *
27 username (uid_t uid)
28 {
29 static char ubuf[256];
30 struct passwd *pw;
31
32 if ((pw = getpwuid (uid)))
33 snprintf (ubuf, sizeof ubuf, "%s", pw->pw_name);
34 else
35 sprintf (ubuf, "%lu <unknown>", (unsigned long)uid);
36 return ubuf;
37 }
38
39 const char *
40 groupname (gid_t gid)
41 {
42 static char gbuf[256];
43 struct group *gr;
44
45 if ((gr = getgrgid (gid)))
46 snprintf (gbuf, sizeof gbuf, "%s", gr->gr_name);
47 else
48 sprintf (gbuf, "%lu <unknown>", (unsigned long)gid);
49 return gbuf;
50 }
51
52 static void __attribute__ ((__noreturn__))
53 usage (FILE * stream)
54 {
55 fprintf (stream, "Usage: %s [-adn] FILE [FILE2...]\n"
56 "\n"
57 "Display file and directory access control lists (ACLs).\n"
58 "\n"
59 " -a, --access display the file access control list only\n"
60 " -d, --default display the default access control list only\n"
61 " -c, --omit-header do not display the comment header\n"
62 " -e, --all-effective print all effective rights\n"
63 " -E, --no-effective print no effective rights\n"
64 " -n, --numeric print numeric user/group identifiers\n"
65 " -V, --version print version and exit\n"
66 " -h, --help this help text\n"
67 "\n"
68 "When multiple files are specified on the command line, a blank\n"
69 "line separates the ACLs for each file.\n", prog_name);
70 if (stream == stdout)
71 {
72 fprintf (stream, ""
73 "For each argument that is a regular file, special file or\n"
74 "directory, getfacl displays the owner, the group, and the ACL.\n"
75 "For directories getfacl displays additionally the default ACL.\n"
76 "\n"
77 "With no options specified, getfacl displays the filename, the\n"
78 "owner, the group, the setuid (s), setgid (s), and sticky (t)\n"
79 "bits if available, and both the ACL and the default ACL, if it\n"
80 "exists.\n"
81 "\n"
82 "The format for ACL output is as follows:\n"
83 " # file: filename\n"
84 " # owner: name or uid\n"
85 " # group: name or uid\n"
86 " # flags: sst\n"
87 " user::perm\n"
88 " user:name or uid:perm\n"
89 " group::perm\n"
90 " group:name or gid:perm\n"
91 " mask::perm\n"
92 " other::perm\n"
93 " default:user::perm\n"
94 " default:user:name or uid:perm\n"
95 " default:group::perm\n"
96 " default:group:name or gid:perm\n"
97 " default:mask::perm\n"
98 " default:other::perm\n"
99 "\n");
100 }
101 exit (stream == stdout ? 0 : 1);
102 }
103
104 struct option longopts[] = {
105 {"access", no_argument, NULL, 'a'},
106 {"all", no_argument, NULL, 'a'},
107 {"omit-header", no_argument, NULL, 'c'},
108 {"all-effective", no_argument, NULL, 'e'},
109 {"no-effective", no_argument, NULL, 'E'},
110 {"default", no_argument, NULL, 'd'},
111 {"dir", no_argument, NULL, 'd'},
112 {"help", no_argument, NULL, 'h'},
113 {"noname", no_argument, NULL, 'n'}, /* Backward compat */
114 {"numeric", no_argument, NULL, 'n'},
115 {"version", no_argument, NULL, 'V'},
116 {0, no_argument, NULL, 0}
117 };
118 const char *opts = "acdeEhnV";
119
120 static void
121 print_version ()
122 {
123 printf ("getfacl (cygwin) %d.%d.%d\n"
124 "Get POSIX ACL information\n"
125 "Copyright (C) 2000 - %s Cygwin Authors\n"
126 "This is free software; see the source for copying conditions. There is NO\n"
127 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
128 CYGWIN_VERSION_DLL_MAJOR / 1000,
129 CYGWIN_VERSION_DLL_MAJOR % 1000,
130 CYGWIN_VERSION_DLL_MINOR,
131 strrchr (__DATE__, ' ') + 1);
132 }
133
134 int
135 main (int argc, char **argv)
136 {
137 int c;
138 int ret = 0;
139 int aopt = 0;
140 int copt = 0;
141 int eopt = 0;
142 int dopt = 0;
143 int nopt = 0;
144 int options = 0;
145 int istty = isatty (fileno (stdout));
146 struct stat st;
147
148 prog_name = program_invocation_short_name;
149
150 while ((c = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
151 switch (c)
152 {
153 case 'a':
154 aopt = 1;
155 break;
156 case 'c':
157 copt = 1;
158 break;
159 case 'd':
160 dopt = 1;
161 break;
162 case 'e':
163 eopt = 1;
164 break;
165 case 'E':
166 eopt = -1;
167 break;
168 case 'h':
169 usage (stdout);
170 case 'n':
171 nopt = 1;
172 break;
173 case 'V':
174 print_version ();
175 return 0;
176 default:
177 fprintf (stderr, "Try `%s --help' for more information.\n", prog_name);
178 return 1;
179 }
180 if (optind > argc - 1)
181 usage (stderr);
182 if (nopt)
183 options |= TEXT_NUMERIC_IDS;
184 if (eopt > 0)
185 options |= TEXT_ALL_EFFECTIVE;
186 else if (!eopt)
187 options |= TEXT_SOME_EFFECTIVE;
188 if (istty)
189 options |= TEXT_SMART_INDENT;
190 for (; optind < argc; ++optind)
191 {
192 acl_t access_acl = NULL, default_acl = NULL;
193 char *access_txt, *default_txt;
194
195 if (stat (argv[optind], &st)
196 || (!dopt
197 && !(access_acl = acl_get_file (argv[optind], ACL_TYPE_ACCESS)))
198 || (!aopt && S_ISDIR (st.st_mode)
199 && !(default_acl = acl_get_file (argv[optind],
200 ACL_TYPE_DEFAULT))))
201 goto err;
202 if (!copt)
203 {
204 printf ("# file: %s\n", argv[optind]);
205 if (nopt)
206 {
207 printf ("# owner: %lu\n", (unsigned long)st.st_uid);
208 printf ("# group: %lu\n", (unsigned long)st.st_gid);
209 }
210 else
211 {
212 printf ("# owner: %s\n", username (st.st_uid));
213 printf ("# group: %s\n", groupname (st.st_gid));
214 }
215 if (st.st_mode & (S_ISUID | S_ISGID | S_ISVTX))
216 printf ("# flags: %c%c%c\n", (st.st_mode & S_ISUID) ? 's' : '-',
217 (st.st_mode & S_ISGID) ? 's' : '-',
218 (st.st_mode & S_ISVTX) ? 't' : '-');
219 }
220 if (access_acl)
221 {
222 if (!(access_txt = acl_to_any_text (access_acl, NULL, '\n', options)))
223 {
224 acl_free (access_acl);
225 goto err;
226 }
227 printf ("%s\n", access_txt);
228 acl_free (access_txt);
229 acl_free (access_acl);
230 }
231 if (default_acl)
232 {
233 if (!(default_txt = acl_to_any_text (default_acl, "default:",
234 '\n', options)))
235 {
236 acl_free (default_acl);
237 goto err;
238 }
239 printf ("%s\n", default_txt);
240 acl_free (default_txt);
241 acl_free (default_acl);
242 }
243 putchar ('\n');
244 continue;
245 err:
246 fprintf (stderr, "%s: %s: %s\n\n",
247 prog_name, argv[optind], strerror (errno));
248 ret = 2;
249 }
250 return ret;
251 }
This page took 0.046222 seconds and 5 git commands to generate.