2001-03-05 18:55:23 +01:00
|
|
|
/* getfacl.c
|
|
|
|
|
2014-08-31 15:46:34 +02:00
|
|
|
Copyright 2000, 2001, 2002, 2003, 2004, 2009, 2011, 2014 Red Hat Inc.
|
2001-03-05 18:55:23 +01:00
|
|
|
|
|
|
|
Written by Corinna Vinschen <vinschen@redhat.com>
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/acl.h>
|
|
|
|
#include <sys/stat.h>
|
2011-10-10 16:57:48 +02:00
|
|
|
#include <cygwin/version.h>
|
2002-01-29 04:08:42 +01:00
|
|
|
#include <string.h>
|
2009-11-11 11:12:35 +01:00
|
|
|
#include <errno.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-05-23 12:56:57 +02:00
|
|
|
static char *prog_name;
|
|
|
|
|
2000-10-28 07:00:00 +02:00
|
|
|
char *
|
|
|
|
permstr (mode_t perm)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
static char pbuf[4];
|
|
|
|
|
2002-11-24 17:15:33 +01:00
|
|
|
pbuf[0] = (perm & S_IROTH) ? 'r' : '-';
|
|
|
|
pbuf[1] = (perm & S_IWOTH) ? 'w' : '-';
|
|
|
|
pbuf[2] = (perm & S_IXOTH) ? 'x' : '-';
|
2000-02-17 20:38:33 +01:00
|
|
|
pbuf[3] = '\0';
|
|
|
|
return pbuf;
|
|
|
|
}
|
|
|
|
|
2001-11-04 18:24:24 +01:00
|
|
|
const char *
|
2000-10-28 07:00:00 +02:00
|
|
|
username (uid_t uid)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
static char ubuf[256];
|
|
|
|
struct passwd *pw;
|
|
|
|
|
2001-11-04 18:24:24 +01:00
|
|
|
if ((pw = getpwuid (uid)))
|
2000-02-17 20:38:33 +01:00
|
|
|
strcpy (ubuf, pw->pw_name);
|
|
|
|
else
|
2003-03-09 20:04:23 +01:00
|
|
|
sprintf (ubuf, "%lu <unknown>", (unsigned long)uid);
|
2001-11-04 18:24:24 +01:00
|
|
|
return ubuf;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2001-11-04 18:24:24 +01:00
|
|
|
const char *
|
2000-10-28 07:00:00 +02:00
|
|
|
groupname (gid_t gid)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
static char gbuf[256];
|
|
|
|
struct group *gr;
|
|
|
|
|
2001-11-04 18:24:24 +01:00
|
|
|
if ((gr = getgrgid (gid)))
|
2000-02-17 20:38:33 +01:00
|
|
|
strcpy (gbuf, gr->gr_name);
|
|
|
|
else
|
2003-03-09 20:04:23 +01:00
|
|
|
sprintf (gbuf, "%lu <unknown>", (unsigned long)gid);
|
2001-11-04 18:24:24 +01:00
|
|
|
return gbuf;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2001-11-04 18:24:24 +01:00
|
|
|
|
2002-05-23 12:56:57 +02:00
|
|
|
static void
|
|
|
|
usage (FILE * stream)
|
2001-11-04 18:24:24 +01:00
|
|
|
{
|
2002-05-23 12:56:57 +02:00
|
|
|
fprintf (stream, "Usage: %s [-adn] FILE [FILE2...]\n"
|
2011-12-18 00:39:47 +01:00
|
|
|
"\n"
|
|
|
|
"Display file and directory access control lists (ACLs).\n"
|
|
|
|
"\n"
|
|
|
|
" -a, --all display the filename, the owner, the group, and\n"
|
|
|
|
" the ACL of the file\n"
|
|
|
|
" -d, --dir display the filename, the owner, the group, and\n"
|
|
|
|
" the default ACL of the directory, if it exists\n"
|
|
|
|
" -h, --help output usage information and exit\n"
|
|
|
|
" -n, --noname display user and group IDs instead of names\n"
|
|
|
|
" -V, --version output version information and exit\n"
|
|
|
|
"\n"
|
|
|
|
"When multiple files are specified on the command line, a blank\n"
|
|
|
|
"line separates the ACLs for each file.\n", prog_name);
|
|
|
|
if (stream == stdout)
|
2001-11-04 18:24:24 +01:00
|
|
|
{
|
2002-05-23 12:56:57 +02:00
|
|
|
fprintf (stream, ""
|
2011-12-18 00:39:47 +01:00
|
|
|
"For each argument that is a regular file, special file or\n"
|
|
|
|
"directory, getfacl displays the owner, the group, and the ACL.\n"
|
|
|
|
"For directories getfacl displays additionally the default ACL.\n"
|
|
|
|
"\n"
|
|
|
|
"With no options specified, getfacl displays the filename, the\n"
|
2014-08-31 15:46:34 +02:00
|
|
|
"owner, the group, the setuid (s), setgid (s), and sticky (t)\n"
|
|
|
|
"bits if available, and both the ACL and the default ACL, if it\n"
|
2011-12-18 00:39:47 +01:00
|
|
|
"exists.\n"
|
|
|
|
"\n"
|
|
|
|
"The format for ACL output is as follows:\n"
|
|
|
|
" # file: filename\n"
|
|
|
|
" # owner: name or uid\n"
|
|
|
|
" # group: name or uid\n"
|
2014-08-31 15:46:34 +02:00
|
|
|
" # flags: sst\n"
|
2011-12-18 00:39:47 +01:00
|
|
|
" user::perm\n"
|
|
|
|
" user:name or uid:perm\n"
|
|
|
|
" group::perm\n"
|
|
|
|
" group:name or gid:perm\n"
|
|
|
|
" mask:perm\n"
|
|
|
|
" other:perm\n"
|
|
|
|
" default:user::perm\n"
|
|
|
|
" default:user:name or uid:perm\n"
|
|
|
|
" default:group::perm\n"
|
|
|
|
" default:group:name or gid:perm\n"
|
|
|
|
" default:mask:perm\n"
|
|
|
|
" default:other:perm\n"
|
|
|
|
"\n");
|
2001-11-04 18:24:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct option longopts[] = {
|
2002-05-23 12:56:57 +02:00
|
|
|
{"all", no_argument, NULL, 'a'},
|
|
|
|
{"dir", no_argument, NULL, 'd'},
|
2001-11-04 18:24:24 +01:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
2002-05-23 12:56:57 +02:00
|
|
|
{"noname", no_argument, NULL, 'n'},
|
2011-10-10 16:57:48 +02:00
|
|
|
{"version", no_argument, NULL, 'V'},
|
2001-11-04 18:24:24 +01:00
|
|
|
{0, no_argument, NULL, 0}
|
|
|
|
};
|
2011-10-10 16:57:48 +02:00
|
|
|
const char *opts = "adhnV";
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-05-23 12:56:57 +02:00
|
|
|
static void
|
|
|
|
print_version ()
|
|
|
|
{
|
2011-10-10 16:57:48 +02:00
|
|
|
printf ("getfacl (cygwin) %d.%d.%d\n"
|
2011-12-18 00:39:47 +01:00
|
|
|
"Get POSIX ACL information\n"
|
|
|
|
"Copyright (C) 2000 - %s Red Hat, Inc.\n"
|
|
|
|
"This is free software; see the source for copying conditions. There is NO\n"
|
2011-10-10 16:57:48 +02:00
|
|
|
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
|
2011-12-18 00:39:47 +01:00
|
|
|
CYGWIN_VERSION_DLL_MAJOR / 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MAJOR % 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MINOR,
|
|
|
|
strrchr (__DATE__, ' ') + 1);
|
2002-05-23 12:56:57 +02:00
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2009-11-11 11:12:35 +01:00
|
|
|
int c;
|
|
|
|
int ret = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
int aopt = 0;
|
|
|
|
int dopt = 0;
|
2001-11-04 18:24:24 +01:00
|
|
|
int nopt = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
struct stat st;
|
|
|
|
aclent_t acls[MAX_ACL_ENTRIES];
|
|
|
|
|
2011-10-10 16:57:48 +02:00
|
|
|
prog_name = program_invocation_short_name;
|
2002-05-23 12:56:57 +02:00
|
|
|
|
2011-10-10 16:57:48 +02:00
|
|
|
while ((c = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
|
2000-02-17 20:38:33 +01:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'a':
|
2000-10-28 07:00:00 +02:00
|
|
|
aopt = 1;
|
|
|
|
break;
|
2000-02-17 20:38:33 +01:00
|
|
|
case 'd':
|
2000-10-28 07:00:00 +02:00
|
|
|
dopt = 1;
|
|
|
|
break;
|
2002-05-23 12:56:57 +02:00
|
|
|
case 'h':
|
|
|
|
usage (stdout);
|
|
|
|
return 0;
|
2001-11-04 18:24:24 +01:00
|
|
|
case 'n':
|
|
|
|
nopt = 1;
|
|
|
|
break;
|
2011-10-10 16:57:48 +02:00
|
|
|
case 'V':
|
2002-05-23 12:56:57 +02:00
|
|
|
print_version ();
|
|
|
|
return 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
default:
|
2011-10-10 16:57:48 +02:00
|
|
|
fprintf (stderr, "Try `%s --help' for more information.\n", prog_name);
|
2002-05-23 12:56:57 +02:00
|
|
|
return 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2001-11-04 18:24:24 +01:00
|
|
|
if (optind > argc - 1)
|
2002-05-23 12:56:57 +02:00
|
|
|
{
|
|
|
|
usage (stderr);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-11-11 11:12:35 +01:00
|
|
|
for (; optind < argc; ++optind)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2009-11-11 11:12:35 +01:00
|
|
|
int i, num_acls;
|
|
|
|
if (stat (argv[optind], &st)
|
|
|
|
|| (num_acls = acl (argv[optind], GETACL, MAX_ACL_ENTRIES, acls)) < 0)
|
2000-10-28 07:00:00 +02:00
|
|
|
{
|
2009-11-11 11:12:35 +01:00
|
|
|
fprintf (stderr, "%s: %s: %s\n",
|
|
|
|
prog_name, argv[optind], strerror (errno));
|
|
|
|
ret = 2;
|
2000-10-28 07:00:00 +02:00
|
|
|
continue;
|
|
|
|
}
|
2009-11-11 11:12:35 +01:00
|
|
|
printf ("# file: %s\n", argv[optind]);
|
2001-11-04 18:24:24 +01:00
|
|
|
if (nopt)
|
2011-12-18 00:39:47 +01:00
|
|
|
{
|
2003-03-09 20:04:23 +01:00
|
|
|
printf ("# owner: %lu\n", (unsigned long)st.st_uid);
|
|
|
|
printf ("# group: %lu\n", (unsigned long)st.st_gid);
|
2001-11-04 18:24:24 +01:00
|
|
|
}
|
|
|
|
else
|
2011-12-18 00:39:47 +01:00
|
|
|
{
|
2001-11-04 18:24:24 +01:00
|
|
|
printf ("# owner: %s\n", username (st.st_uid));
|
|
|
|
printf ("# group: %s\n", groupname (st.st_gid));
|
|
|
|
}
|
2014-08-31 15:46:34 +02:00
|
|
|
if (st.st_mode & (S_ISUID | S_ISGID | S_ISVTX))
|
|
|
|
printf ("# flags: %c%c%c\n", (st.st_mode & S_ISUID) ? 's' : '-',
|
|
|
|
(st.st_mode & S_ISGID) ? 's' : '-',
|
|
|
|
(st.st_mode & S_ISVTX) ? 't' : '-');
|
2009-11-11 11:12:35 +01:00
|
|
|
for (i = 0; i < num_acls; ++i)
|
2000-10-28 07:00:00 +02:00
|
|
|
{
|
|
|
|
if (acls[i].a_type & ACL_DEFAULT)
|
|
|
|
{
|
|
|
|
if (aopt)
|
|
|
|
continue;
|
|
|
|
printf ("default:");
|
|
|
|
}
|
|
|
|
else if (dopt)
|
|
|
|
continue;
|
|
|
|
switch (acls[i].a_type & ~ACL_DEFAULT)
|
|
|
|
{
|
|
|
|
case USER_OBJ:
|
|
|
|
printf ("user::");
|
|
|
|
break;
|
|
|
|
case USER:
|
2001-11-04 18:24:24 +01:00
|
|
|
if (nopt)
|
2004-09-15 09:51:06 +02:00
|
|
|
printf ("user:%lu:", (unsigned long)acls[i].a_id);
|
2001-11-04 18:24:24 +01:00
|
|
|
else
|
|
|
|
printf ("user:%s:", username (acls[i].a_id));
|
2000-10-28 07:00:00 +02:00
|
|
|
break;
|
|
|
|
case GROUP_OBJ:
|
|
|
|
printf ("group::");
|
|
|
|
break;
|
|
|
|
case GROUP:
|
2001-11-04 18:24:24 +01:00
|
|
|
if (nopt)
|
2004-09-15 09:51:06 +02:00
|
|
|
printf ("group:%lu:", (unsigned long)acls[i].a_id);
|
2001-11-04 18:24:24 +01:00
|
|
|
else
|
|
|
|
printf ("group:%s:", groupname (acls[i].a_id));
|
2000-10-28 07:00:00 +02:00
|
|
|
break;
|
|
|
|
case CLASS_OBJ:
|
2002-12-15 11:09:35 +01:00
|
|
|
printf ("mask:");
|
2000-10-28 07:00:00 +02:00
|
|
|
break;
|
|
|
|
case OTHER_OBJ:
|
2002-12-15 11:09:35 +01:00
|
|
|
printf ("other:");
|
2000-10-28 07:00:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf ("%s\n", permstr (acls[i].a_perm));
|
|
|
|
}
|
2009-11-11 11:12:35 +01:00
|
|
|
putchar ('\n');
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2009-11-11 11:12:35 +01:00
|
|
|
return ret;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|