* getfacl.c (username): New function.

(groupname): Ditto.
	(usage): Ditto.  Add more user friendly help text.
	(main): Add -n and --help option.  Print names instead of IDs
	unless -n option is given.
	* setfacl.c (getperm): Tolerate whitespace and comment chars in input.
	(getaclentry): Fix several input string misdetections.
	(getaclentries): Allow - as input file name to read from stdin.
	Tolerate whitespace and comment chars in input.
	(usage): Add more user friendly help text.
	(main): Add --help option.
This commit is contained in:
Corinna Vinschen 2001-11-04 17:24:24 +00:00
parent 86fbc3d90b
commit 56c29a3c87
3 changed files with 255 additions and 51 deletions

View File

@ -1,3 +1,17 @@
2001-11-04 Corinna Vinschen <corinna@vinschen.de>
* getfacl.c (username): New function.
(groupname): Ditto.
(usage): Ditto. Add more user friendly help text.
(main): Add -n and --help option. Print names instead of IDs
unless -n option is given.
* setfacl.c (getperm): Tolerate whitespace and comment chars in input.
(getaclentry): Fix several input string misdetections.
(getaclentries): Allow - as input file name to read from stdin.
Tolerate whitespace and comment chars in input.
(usage): Add more user friendly help text.
(main): Add --help option.
2001-11-04 Egor Duda <deo@logos-m.ru> 2001-11-04 Egor Duda <deo@logos-m.ru>
* strace.cc (main): New option '-w'. Start traced process in separate * strace.cc (main): New option '-w'. Start traced process in separate

View File

@ -31,31 +31,93 @@ permstr (mode_t perm)
return pbuf; return pbuf;
} }
#if 0 const char *
char *
username (uid_t uid) username (uid_t uid)
{ {
static char ubuf[256]; static char ubuf[256];
struct passwd *pw; struct passwd *pw;
if (pw = getpwuid (uid)) if ((pw = getpwuid (uid)))
strcpy (ubuf, pw->pw_name); strcpy (ubuf, pw->pw_name);
else else
strcpy (ubuf, "<unknown>"); sprintf (ubuf, "%d <unknown>", uid);
return ubuf;
} }
char * const char *
groupname (gid_t gid) groupname (gid_t gid)
{ {
static char gbuf[256]; static char gbuf[256];
struct group *gr; struct group *gr;
if (gr = getgruid (gid)) if ((gr = getgrgid (gid)))
strcpy (gbuf, gr->gr_name); strcpy (gbuf, gr->gr_name);
else else
strcpy (gbuf, "<unknown>"); sprintf (gbuf, "%d <unknown>", gid);
return gbuf;
} }
#endif
#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}
};
int int
main (int argc, char **argv) main (int argc, char **argv)
@ -64,11 +126,12 @@ main (int argc, char **argv)
int c, i; int c, i;
int aopt = 0; int aopt = 0;
int dopt = 0; int dopt = 0;
int nopt = 0;
int first = 1; int first = 1;
struct stat st; struct stat st;
aclent_t acls[MAX_ACL_ENTRIES]; aclent_t acls[MAX_ACL_ENTRIES];
while ((c = getopt (argc, argv, "ad")) != EOF) while ((c = getopt_long (argc, argv, "adn", longopts, NULL)) != EOF)
switch (c) switch (c)
{ {
case 'a': case 'a':
@ -77,10 +140,16 @@ main (int argc, char **argv)
case 'd': case 'd':
dopt = 1; dopt = 1;
break; break;
case 'n':
nopt = 1;
break;
case 'h':
return usage (argv[0], 1);
default: default:
fprintf (stderr, "usage: %s [-ad] file...\n", argv[0]); return usage (argv[0], 0);
return 1;
} }
if (optind > argc - 1)
return usage (argv[0], 0);
while ((c = optind++) < argc) while ((c = optind++) < argc)
{ {
if (stat (argv[c], &st)) if (stat (argv[c], &st))
@ -92,8 +161,16 @@ main (int argc, char **argv)
putchar ('\n'); putchar ('\n');
first = 0; first = 0;
printf ("# file: %s\n", argv[c]); printf ("# file: %s\n", argv[c]);
printf ("# owner: %d\n", st.st_uid); if (nopt)
printf ("# group: %d\n", st.st_gid); {
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));
}
if ((c = acl (argv[c], GETACL, MAX_ACL_ENTRIES, acls)) < 0) if ((c = acl (argv[c], GETACL, MAX_ACL_ENTRIES, acls)) < 0)
{ {
perror (argv[0]); perror (argv[0]);
@ -115,13 +192,19 @@ main (int argc, char **argv)
printf ("user::"); printf ("user::");
break; break;
case USER: case USER:
printf ("user:%d:", acls[i].a_id); if (nopt)
printf ("user:%d:", acls[i].a_id);
else
printf ("user:%s:", username (acls[i].a_id));
break; break;
case GROUP_OBJ: case GROUP_OBJ:
printf ("group::"); printf ("group::");
break; break;
case GROUP: case GROUP:
printf ("group:%d:", acls[i].a_id); if (nopt)
printf ("group:%d:", acls[i].a_id);
else
printf ("group:%s:", groupname (acls[i].a_id));
break; break;
case CLASS_OBJ: case CLASS_OBJ:
printf ("mask::"); printf ("mask::");

View File

@ -48,14 +48,6 @@ typedef enum {
char *myname; char *myname;
int usage ()
{
fprintf(stderr, "usage: %s [-r] -s acl_entries file...\n", myname);
fprintf(stderr, " %s [-r] -md acl_entries file...\n", myname);
fprintf(stderr, " %s [-r] -f acl_file file...\n", myname);
return 1;
}
mode_t getperm (char *in) mode_t getperm (char *in)
{ {
if (isdigit (*in) && !in[1]) if (isdigit (*in) && !in[1])
@ -65,6 +57,8 @@ mode_t getperm (char *in)
return ILLEGAL_MODE; return ILLEGAL_MODE;
return i << 6 | i << 3 | i; return i << 6 | i << 3 | i;
} }
if (strlen (in) > 3 && strchr (" \t\n\r#", in[3]))
in[3] = '\0';
if (strlen (in) != 3) if (strlen (in) != 3)
return ILLEGAL_MODE; return ILLEGAL_MODE;
if (!strchr ("r-", in[0]) if (!strchr ("r-", in[0])
@ -106,16 +100,12 @@ getaclentry (action_t action, char *c, aclent_t *ace)
else if (!strncmp (c, "mask:", 5) else if (!strncmp (c, "mask:", 5)
|| !strncmp (c, "m:", 2)) || !strncmp (c, "m:", 2))
{ {
if (!(ace->a_type & ACL_DEFAULT))
return FALSE;
ace->a_type |= CLASS_OBJ; ace->a_type |= CLASS_OBJ;
c = strchr (c, ':') + 1; c = strchr (c, ':') + 1;
} }
else if (!strncmp (c, "other:", 6) else if (!strncmp (c, "other:", 6)
|| !strncmp (c, "o:", 2)) || !strncmp (c, "o:", 2))
{ {
if (!(ace->a_type & ACL_DEFAULT))
return FALSE;
ace->a_type |= OTHER_OBJ; ace->a_type |= OTHER_OBJ;
c = strchr (c, ':') + 1; c = strchr (c, ':') + 1;
} }
@ -158,19 +148,24 @@ getaclentry (action_t action, char *c, aclent_t *ace)
return FALSE; return FALSE;
ace->a_id = gr->gr_gid; ace->a_id = gr->gr_gid;
} }
if (ace->a_type & USER_OBJ) if (c2 != c)
{ {
ace->a_type &= ~USER_OBJ; if (ace->a_type & USER_OBJ)
ace->a_type |= USER; {
} ace->a_type &= ~USER_OBJ;
else ace->a_type |= USER;
{ }
ace->a_type &= ~GROUP_OBJ; else
ace->a_type |= GROUP; {
} ace->a_type &= ~GROUP_OBJ;
ace->a_type |= GROUP;
}
}
if (c2) if (c2)
c = c2 + 1; c = c2 + 1;
} }
else if (*c++ != ':')
return FALSE;
if (action == Delete) if (action == Delete)
{ {
if ((ace->a_type & (CLASS_OBJ | OTHER_OBJ)) if ((ace->a_type & (CLASS_OBJ | OTHER_OBJ))
@ -192,19 +187,26 @@ getaclentries (action_t action, char *buf, aclent_t *acls, int *idx)
if (action == SetFromFile) if (action == SetFromFile)
{ {
FILE *fp; FILE *fp;
char fbuf[256]; char fbuf[256], *fb;
if (! (fp = fopen (buf, "r"))) if (!strcmp (buf, "-"))
fp = stdin;
else if (! (fp = fopen (buf, "r")))
return FALSE; return FALSE;
while (fgets (fbuf, 256, fp)) while ((fb = fgets (fbuf, 256, fp)))
{ {
if (!getaclentry (action, fbuf, acls + (*idx)++)) while (strchr (" \t", *fb))
++fb;
if (strchr ("\n\r#", *fb))
continue;
if (!getaclentry (action, fb, acls + (*idx)++))
{ {
fclose (fp); fclose (fp);
return FALSE; return FALSE;
} }
} }
fclose (fp); if (fp != stdin)
fclose (fp);
} }
else else
for (c = strtok (buf, ","); c; c = strtok (NULL, ",")) for (c = strtok (buf, ","); c; c = strtok (NULL, ","))
@ -274,6 +276,109 @@ setfacl (action_t action, char *path, aclent_t *acls, int cnt)
perror (myname); perror (myname);
} }
#define pn(txt) fprintf (fp, txt "\n", myname)
#define p(txt) fprintf (fp, txt "\n")
int
usage (int help)
{
FILE *fp = help ? stdout : stderr;
pn ("usage: %s [-r] -s acl_entries file...");
pn (" %s [-r] -md acl_entries file...");
pn (" %s [-r] -f acl_file file...");
if (!help)
pn ("Try `%s --help' for more information.");
else
{
p ("");
p ("Modify file and directory access control lists (ACLs)");
p ("");
pn ("For each file given as parameter, %s will either replace its");
p ("complete ACL (-s, -f)), or it will add, modify, or delete ACL");
p ("entries.");
p ("");
p ("The following options are supported:");
p ("");
p ("-s Substitute the ACL of the file by the entries specified on");
p (" the command line. Required entries are");
p (" - One user entry for the owner of the file.");
p (" - One group entry for the group of the file.");
p (" - One other entry.");
p (" If additional user and group entries are given:");
p (" - A mask entry for the file group class of the file.");
p (" - No duplicate user or group entries with the same uid/gid.");
p (" If it is a directory:");
p (" - One default user entry for the owner of the file.");
p (" - One default group entry for the group of the file.");
p (" - One default mask entry for the file group class of the file.");
p (" - One default other entry.");
p ("");
p (" Acl_entries are one or more comma-separated ACL entries from");
p (" the following list:");
p ("");
p (" u[ser]::perm");
p (" u[ser]:uid:perm");
p (" g[roup]::perm");
p (" g[roup]:gid:perm");
p (" m[ask]::perm");
p (" o[ther]::perm");
p ("");
p (" Default entries are like the above with the trailing default");
p (" identifier. E.g.");
p ("");
p (" d[efault]:u[ser]:uid:perm");
p ("");
p (" `perm' is either a 3-char permissions string in the form");
p (" \"rwx\" with the character - for not setting a permission");
p (" or it is the octal representation of the permissions, a");
p (" value from 0 (equivalent to \"---\") to 7 (\"rwx\").");
p (" `uid' is a user name or a numerical uid.");
p (" `gid' is a group name or a numerical gid.");
p ("");
p ("-f Like -s but take the ACL entries from `acl_file'. Acl_entries");
p (" are given one per line. Whitespace characters are ignored,");
p (" the character \"#\" may be used to start a comment. The");
p (" special filename \"-\" indicates reading from stdin.");
p ("");
p ("-m Add or modify one or more specified ACL entries. Acl_entries");
p (" is a comma-separated list of entries from the same list as");
p (" above.");
p ("");
p ("-d Delete one or more specified entries from the file's ACL.");
p (" The owner, group and others entries must not be deleted");
p (" Acl_entries are one or more comma-separated ACL entries");
p (" without permissions, taken from the following list:");
p ("");
p (" u[ser]:uid");
p (" g[roup]:gid");
p (" d[efault]:u[ser]:uid");
p (" d[efault]:g[roup]:gid");
p (" d[efault]:m[ask]:");
p (" d[efault]:o[ther]:");
p ("");
p ("-r Causes the permissions specified in the mask entry to be");
p (" ignored and replaced by the maximum permissions needed for");
p (" the file group class.");
p ("");
p ("While the -m and -d options may be used in the same command, the");
p ("-s and -f options may be used only exclusively.");
p ("");
p ("Directories may contain default ACL entries. Files created");
p ("in a directory that contains default ACL entries will have");
p ("permissions according to the combination of the current umask,");
p ("the explicit permissions requested and the default ACL entries");
p ("Note: Under Cygwin, the default ACL entries are not taken into");
p ("account currently.");
}
return 1;
}
struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{0, no_argument, NULL, 0}
};
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
@ -287,7 +392,7 @@ main (int argc, char **argv)
myname = argv[0]; myname = argv[0];
memset (acls, 0, sizeof acls); memset (acls, 0, sizeof acls);
while ((c = getopt (argc, argv, "d:f:m:rs:")) != EOF) while ((c = getopt_long (argc, argv, "d:f:m:rs:", longopts, NULL)) != EOF)
switch (c) switch (c)
{ {
case 'd': case 'd':
@ -296,7 +401,7 @@ main (int argc, char **argv)
else if (action == Modify) else if (action == Modify)
action = ModNDel; action = ModNDel;
else else
return usage (); return usage (0);
if (! getaclentries (Delete, optarg, acls, &aclidx)) if (! getaclentries (Delete, optarg, acls, &aclidx))
{ {
fprintf (stderr, "%s: illegal acl entries\n", myname); fprintf (stderr, "%s: illegal acl entries\n", myname);
@ -307,7 +412,7 @@ main (int argc, char **argv)
if (action == NoAction) if (action == NoAction)
action = Set; action = Set;
else else
return usage (); return usage (0);
if (! getaclentries (SetFromFile, optarg, acls, &aclidx)) if (! getaclentries (SetFromFile, optarg, acls, &aclidx))
{ {
fprintf (stderr, "%s: illegal acl entries\n", myname); fprintf (stderr, "%s: illegal acl entries\n", myname);
@ -320,7 +425,7 @@ main (int argc, char **argv)
else if (action == Delete) else if (action == Delete)
action = ModNDel; action = ModNDel;
else else
return usage (); return usage (0);
if (! getaclentries (Modify, optarg, acls, &aclidx)) if (! getaclentries (Modify, optarg, acls, &aclidx))
{ {
fprintf (stderr, "%s: illegal acl entries\n", myname); fprintf (stderr, "%s: illegal acl entries\n", myname);
@ -331,26 +436,29 @@ main (int argc, char **argv)
if (!ropt) if (!ropt)
ropt = 1; ropt = 1;
else else
return usage (); return usage (0);
break; break;
case 's': case 's':
if (action == NoAction) if (action == NoAction)
action = Set; action = Set;
else else
return usage (); return usage (0);
break; break;
if (! getaclentries (Set, optarg, acls, &aclidx)) if (! getaclentries (Set, optarg, acls, &aclidx))
{ {
fprintf (stderr, "%s: illegal acl entries\n", myname); fprintf (stderr, "%s: illegal acl entries\n", myname);
return 2; return 2;
} }
break;
case 'h':
return usage (1);
default: default:
return usage (); return usage (0);
} }
if (action == NoAction) if (action == NoAction)
return usage (); return usage (0);
if (optind > argc - 1) if (optind > argc - 1)
return usage (); return usage (0);
if (action == Set) if (action == Set)
switch (aclcheck (acls, aclidx, NULL)) switch (aclcheck (acls, aclidx, NULL))
{ {
@ -385,4 +493,3 @@ main (int argc, char **argv)
setfacl (action, argv[c], acls, aclidx); setfacl (action, argv[c], acls, aclidx);
return 0; return 0;
} }