2001-03-06 01:55:23 +08:00
|
|
|
/* getfacl.c
|
|
|
|
|
|
|
|
Copyright 2000, 2001 Red Hat Inc.
|
|
|
|
|
|
|
|
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-18 03:38:33 +08: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>
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
char *
|
|
|
|
permstr (mode_t perm)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
static char pbuf[4];
|
|
|
|
|
|
|
|
pbuf[0] = (perm & S_IREAD) ? 'r' : '-';
|
|
|
|
pbuf[1] = (perm & S_IWRITE) ? 'w' : '-';
|
|
|
|
pbuf[2] = (perm & S_IEXEC) ? 'x' : '-';
|
|
|
|
pbuf[3] = '\0';
|
|
|
|
return pbuf;
|
|
|
|
}
|
|
|
|
|
2001-11-05 01:24:24 +08:00
|
|
|
const char *
|
2000-10-28 13:00:00 +08:00
|
|
|
username (uid_t uid)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
static char ubuf[256];
|
|
|
|
struct passwd *pw;
|
|
|
|
|
2001-11-05 01:24:24 +08:00
|
|
|
if ((pw = getpwuid (uid)))
|
2000-02-18 03:38:33 +08:00
|
|
|
strcpy (ubuf, pw->pw_name);
|
|
|
|
else
|
2001-11-05 01:24:24 +08:00
|
|
|
sprintf (ubuf, "%d <unknown>", uid);
|
|
|
|
return ubuf;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-11-05 01:24:24 +08:00
|
|
|
const char *
|
2000-10-28 13:00:00 +08:00
|
|
|
groupname (gid_t gid)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
static char gbuf[256];
|
|
|
|
struct group *gr;
|
|
|
|
|
2001-11-05 01:24:24 +08:00
|
|
|
if ((gr = getgrgid (gid)))
|
2000-02-18 03:38:33 +08:00
|
|
|
strcpy (gbuf, gr->gr_name);
|
|
|
|
else
|
2001-11-05 01:24:24 +08:00
|
|
|
sprintf (gbuf, "%d <unknown>", gid);
|
|
|
|
return gbuf;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2001-11-05 01:24:24 +08:00
|
|
|
|
|
|
|
#define pn(txt) fprintf (fp, txt "\n", name)
|
|
|
|
#define p(txt) fprintf (fp, txt "\n")
|
|
|
|
|
|
|
|
int
|
|
|
|
usage (const char *name, int help)
|
|
|
|
{
|
|
|
|
FILE *fp = help ? stdout : stderr;
|
|
|
|
|
|
|
|
pn ("usage: %s [-adn] file...");
|
|
|
|
if (!help)
|
|
|
|
pn ("Try `%s --help' for more information.");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p ("");
|
|
|
|
p ("Display file and directory access control lists (ACLs).");
|
|
|
|
p ("");
|
|
|
|
p ("For each argument that is a regular file, special file or");
|
|
|
|
p ("directory, getfacl displays the owner, the group, and the ACL.");
|
|
|
|
p ("For directories getfacl displays additionally the default ACL.");
|
|
|
|
p ("");
|
|
|
|
p ("With no options specified, getfacl displays the filename, the");
|
|
|
|
p ("owner, the group, and both the ACL and the default ACL, if it");
|
|
|
|
p ("exists.");
|
|
|
|
p ("");
|
|
|
|
p ("The following options are supported:");
|
|
|
|
p ("");
|
|
|
|
p ("-a Display the filename, the owner, the group, and the ACL");
|
|
|
|
p (" of the file.");
|
|
|
|
p ("");
|
|
|
|
p ("-d Display the filename, the owner, the group, and the default");
|
|
|
|
p (" ACL of the directory, if it exists.");
|
|
|
|
p ("");
|
|
|
|
p ("-n Display user and group IDs instead of names.");
|
|
|
|
p ("");
|
|
|
|
p ("The format for ACL output is as follows:");
|
|
|
|
p (" # file: filename");
|
|
|
|
p (" # owner: name or uid");
|
|
|
|
p (" # group: name or uid");
|
|
|
|
p (" user::perm");
|
|
|
|
p (" user:name or uid:perm");
|
|
|
|
p (" group::perm");
|
|
|
|
p (" group:name or gid:perm");
|
|
|
|
p (" mask:perm");
|
|
|
|
p (" other:perm");
|
|
|
|
p (" default:user::perm");
|
|
|
|
p (" default:user:name or uid:perm");
|
|
|
|
p (" default:group::perm");
|
|
|
|
p (" default:group:name or gid:perm");
|
|
|
|
p (" default:mask:perm");
|
|
|
|
p (" default:other:perm");
|
|
|
|
p ("");
|
|
|
|
p ("When multiple files are specified on the command line, a blank");
|
|
|
|
p ("line separates the ACLs for each file.");
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct option longopts[] = {
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{0, no_argument, NULL, 0}
|
|
|
|
};
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
extern int optind;
|
|
|
|
int c, i;
|
|
|
|
int aopt = 0;
|
|
|
|
int dopt = 0;
|
2001-11-05 01:24:24 +08:00
|
|
|
int nopt = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
int first = 1;
|
|
|
|
struct stat st;
|
|
|
|
aclent_t acls[MAX_ACL_ENTRIES];
|
|
|
|
|
2001-11-05 01:24:24 +08:00
|
|
|
while ((c = getopt_long (argc, argv, "adn", longopts, NULL)) != EOF)
|
2000-02-18 03:38:33 +08:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'a':
|
2000-10-28 13:00:00 +08:00
|
|
|
aopt = 1;
|
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
case 'd':
|
2000-10-28 13:00:00 +08:00
|
|
|
dopt = 1;
|
|
|
|
break;
|
2001-11-05 01:24:24 +08:00
|
|
|
case 'n':
|
|
|
|
nopt = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
return usage (argv[0], 1);
|
2000-02-18 03:38:33 +08:00
|
|
|
default:
|
2001-11-05 01:24:24 +08:00
|
|
|
return usage (argv[0], 0);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2001-11-05 01:24:24 +08:00
|
|
|
if (optind > argc - 1)
|
|
|
|
return usage (argv[0], 0);
|
2000-02-18 03:38:33 +08:00
|
|
|
while ((c = optind++) < argc)
|
|
|
|
{
|
|
|
|
if (stat (argv[c], &st))
|
2000-10-28 13:00:00 +08:00
|
|
|
{
|
|
|
|
perror (argv[0]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!first)
|
|
|
|
putchar ('\n');
|
2000-02-18 03:38:33 +08:00
|
|
|
first = 0;
|
|
|
|
printf ("# file: %s\n", argv[c]);
|
2001-11-05 01:24:24 +08:00
|
|
|
if (nopt)
|
|
|
|
{
|
|
|
|
printf ("# owner: %d\n", st.st_uid);
|
|
|
|
printf ("# group: %d\n", st.st_gid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf ("# owner: %s\n", username (st.st_uid));
|
|
|
|
printf ("# group: %s\n", groupname (st.st_gid));
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
if ((c = acl (argv[c], GETACL, MAX_ACL_ENTRIES, acls)) < 0)
|
2000-10-28 13:00:00 +08:00
|
|
|
{
|
|
|
|
perror (argv[0]);
|
|
|
|
continue;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
for (i = 0; i < c; ++i)
|
2000-10-28 13:00:00 +08: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-05 01:24:24 +08:00
|
|
|
if (nopt)
|
|
|
|
printf ("user:%d:", acls[i].a_id);
|
|
|
|
else
|
|
|
|
printf ("user:%s:", username (acls[i].a_id));
|
2000-10-28 13:00:00 +08:00
|
|
|
break;
|
|
|
|
case GROUP_OBJ:
|
|
|
|
printf ("group::");
|
|
|
|
break;
|
|
|
|
case GROUP:
|
2001-11-05 01:24:24 +08:00
|
|
|
if (nopt)
|
|
|
|
printf ("group:%d:", acls[i].a_id);
|
|
|
|
else
|
|
|
|
printf ("group:%s:", groupname (acls[i].a_id));
|
2000-10-28 13:00:00 +08:00
|
|
|
break;
|
|
|
|
case CLASS_OBJ:
|
|
|
|
printf ("mask::");
|
|
|
|
break;
|
|
|
|
case OTHER_OBJ:
|
|
|
|
printf ("other::");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf ("%s\n", permstr (acls[i].a_perm));
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|