From 56c29a3c875454c334b5d9fe0e30ed7f73ae0527 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Sun, 4 Nov 2001 17:24:24 +0000 Subject: [PATCH] * 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. --- winsup/utils/ChangeLog | 14 ++++ winsup/utils/getfacl.c | 113 ++++++++++++++++++++++---- winsup/utils/setfacl.c | 179 ++++++++++++++++++++++++++++++++--------- 3 files changed, 255 insertions(+), 51 deletions(-) diff --git a/winsup/utils/ChangeLog b/winsup/utils/ChangeLog index 9df1877a9..162292172 100644 --- a/winsup/utils/ChangeLog +++ b/winsup/utils/ChangeLog @@ -1,3 +1,17 @@ +2001-11-04 Corinna Vinschen + + * 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 * strace.cc (main): New option '-w'. Start traced process in separate diff --git a/winsup/utils/getfacl.c b/winsup/utils/getfacl.c index a0618076a..356d53971 100644 --- a/winsup/utils/getfacl.c +++ b/winsup/utils/getfacl.c @@ -31,31 +31,93 @@ permstr (mode_t perm) return pbuf; } -#if 0 -char * +const char * username (uid_t uid) { static char ubuf[256]; struct passwd *pw; - if (pw = getpwuid (uid)) + if ((pw = getpwuid (uid))) strcpy (ubuf, pw->pw_name); else - strcpy (ubuf, ""); + sprintf (ubuf, "%d ", uid); + return ubuf; } -char * +const char * groupname (gid_t gid) { static char gbuf[256]; struct group *gr; - if (gr = getgruid (gid)) + if ((gr = getgrgid (gid))) strcpy (gbuf, gr->gr_name); else - strcpy (gbuf, ""); + sprintf (gbuf, "%d ", 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 main (int argc, char **argv) @@ -64,11 +126,12 @@ main (int argc, char **argv) int c, i; int aopt = 0; int dopt = 0; + int nopt = 0; int first = 1; struct stat st; 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) { case 'a': @@ -77,10 +140,16 @@ main (int argc, char **argv) case 'd': dopt = 1; break; + case 'n': + nopt = 1; + break; + case 'h': + return usage (argv[0], 1); default: - fprintf (stderr, "usage: %s [-ad] file...\n", argv[0]); - return 1; + return usage (argv[0], 0); } + if (optind > argc - 1) + return usage (argv[0], 0); while ((c = optind++) < argc) { if (stat (argv[c], &st)) @@ -92,8 +161,16 @@ main (int argc, char **argv) putchar ('\n'); first = 0; printf ("# file: %s\n", argv[c]); - printf ("# owner: %d\n", st.st_uid); - printf ("# group: %d\n", st.st_gid); + 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)); + } if ((c = acl (argv[c], GETACL, MAX_ACL_ENTRIES, acls)) < 0) { perror (argv[0]); @@ -115,13 +192,19 @@ main (int argc, char **argv) printf ("user::"); break; 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; case GROUP_OBJ: printf ("group::"); break; 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; case CLASS_OBJ: printf ("mask::"); diff --git a/winsup/utils/setfacl.c b/winsup/utils/setfacl.c index 259874896..04daba7e8 100644 --- a/winsup/utils/setfacl.c +++ b/winsup/utils/setfacl.c @@ -48,14 +48,6 @@ typedef enum { 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) { if (isdigit (*in) && !in[1]) @@ -65,6 +57,8 @@ mode_t getperm (char *in) return ILLEGAL_MODE; return i << 6 | i << 3 | i; } + if (strlen (in) > 3 && strchr (" \t\n\r#", in[3])) + in[3] = '\0'; if (strlen (in) != 3) return ILLEGAL_MODE; if (!strchr ("r-", in[0]) @@ -106,16 +100,12 @@ getaclentry (action_t action, char *c, aclent_t *ace) else if (!strncmp (c, "mask:", 5) || !strncmp (c, "m:", 2)) { - if (!(ace->a_type & ACL_DEFAULT)) - return FALSE; ace->a_type |= CLASS_OBJ; c = strchr (c, ':') + 1; } else if (!strncmp (c, "other:", 6) || !strncmp (c, "o:", 2)) { - if (!(ace->a_type & ACL_DEFAULT)) - return FALSE; ace->a_type |= OTHER_OBJ; c = strchr (c, ':') + 1; } @@ -158,19 +148,24 @@ getaclentry (action_t action, char *c, aclent_t *ace) return FALSE; ace->a_id = gr->gr_gid; } - if (ace->a_type & USER_OBJ) + if (c2 != c) { - ace->a_type &= ~USER_OBJ; - ace->a_type |= USER; - } - else - { - ace->a_type &= ~GROUP_OBJ; - ace->a_type |= GROUP; - } + if (ace->a_type & USER_OBJ) + { + ace->a_type &= ~USER_OBJ; + ace->a_type |= USER; + } + else + { + ace->a_type &= ~GROUP_OBJ; + ace->a_type |= GROUP; + } + } if (c2) c = c2 + 1; } + else if (*c++ != ':') + return FALSE; if (action == Delete) { 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) { 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; - 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); return FALSE; } } - fclose (fp); + if (fp != stdin) + fclose (fp); } else 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); } +#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 main (int argc, char **argv) { @@ -287,7 +392,7 @@ main (int argc, char **argv) myname = argv[0]; 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) { case 'd': @@ -296,7 +401,7 @@ main (int argc, char **argv) else if (action == Modify) action = ModNDel; else - return usage (); + return usage (0); if (! getaclentries (Delete, optarg, acls, &aclidx)) { fprintf (stderr, "%s: illegal acl entries\n", myname); @@ -307,7 +412,7 @@ main (int argc, char **argv) if (action == NoAction) action = Set; else - return usage (); + return usage (0); if (! getaclentries (SetFromFile, optarg, acls, &aclidx)) { fprintf (stderr, "%s: illegal acl entries\n", myname); @@ -320,7 +425,7 @@ main (int argc, char **argv) else if (action == Delete) action = ModNDel; else - return usage (); + return usage (0); if (! getaclentries (Modify, optarg, acls, &aclidx)) { fprintf (stderr, "%s: illegal acl entries\n", myname); @@ -331,26 +436,29 @@ main (int argc, char **argv) if (!ropt) ropt = 1; else - return usage (); + return usage (0); break; case 's': if (action == NoAction) action = Set; else - return usage (); + return usage (0); break; if (! getaclentries (Set, optarg, acls, &aclidx)) { fprintf (stderr, "%s: illegal acl entries\n", myname); return 2; } + break; + case 'h': + return usage (1); default: - return usage (); + return usage (0); } if (action == NoAction) - return usage (); + return usage (0); if (optind > argc - 1) - return usage (); + return usage (0); if (action == Set) switch (aclcheck (acls, aclidx, NULL)) { @@ -385,4 +493,3 @@ main (int argc, char **argv) setfacl (action, argv[c], acls, aclidx); return 0; } -