📄 setfacl.c
字号:
/* setfacl.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 theCygwin license. Please consult the file "CYGWIN_LICENSE" fordetails. */#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <getopt.h>#include <pwd.h>#include <grp.h>#include <sys/types.h>#include <sys/acl.h>#ifndef BOOL#define BOOL int#endif#ifndef TRUE#define TRUE (1)#endif#ifndef FALSE#define FALSE (0)#endif#ifndef ILLEGAL_MODE#define ILLEGAL_MODE ((mode_t)0xffffffff)#endifstatic const char version[] = "$Revision: 1.13 $";static char *prog_name;typedef enum { NoAction, Set, Modify, Delete, ModNDel, SetFromFile} action_t;mode_t getperm (char *in){ if (isdigit (*in) && !in[1]) { int i = atoi (in); if (i < 0 || i > 7) 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]) || !strchr ("w-", in[1]) || !strchr ("x-", in[2])) return ILLEGAL_MODE; return (in[0] == 'r' ? S_IROTH : 0) | (in[1] == 'w' ? S_IWOTH : 0) | (in[2] == 'x' ? S_IXOTH : 0);}BOOLgetaclentry (action_t action, char *c, aclent_t *ace){ char *c2; ace->a_type = 0; ace->a_id = -1; ace->a_perm = 0; if (!strncmp (c, "default:", 8) || !strncmp (c, "d:", 2)) { ace->a_type = ACL_DEFAULT; c = strchr (c, ':') + 1; } if (!strncmp (c, "user:", 5) || !strncmp (c, "u:", 2)) { ace->a_type |= USER_OBJ; c = strchr (c, ':') + 1; } else if (!strncmp (c, "group:", 6) || !strncmp (c, "g:", 2)) { ace->a_type |= GROUP_OBJ; c = strchr (c, ':') + 1; } else if (!strncmp (c, "mask:", 5) || !strncmp (c, "m:", 2)) { ace->a_type |= CLASS_OBJ; c = strchr (c, ':') + 1; } else if (!strncmp (c, "other:", 6) || !strncmp (c, "o:", 2)) { ace->a_type |= OTHER_OBJ; c = strchr (c, ':') + 1; } else return FALSE; if (ace->a_type & (USER_OBJ | GROUP_OBJ)) { if ((c2 = strchr (c, ':'))) { if (action == Delete) return FALSE; *c2 = '\0'; } else if (action != Delete) return FALSE; if (c2 == c) { if (action == Delete) return FALSE; } else if (isdigit (*c)) { char *c3; ace->a_id = strtol (c, &c3, 10); if (*c3) return FALSE; } else if (ace->a_type & USER_OBJ) { struct passwd *pw = getpwnam (c); if (!pw) return FALSE; ace->a_id = pw->pw_uid; } else { struct group *gr = getgrnam (c); if (!gr) return FALSE; ace->a_id = gr->gr_gid; } if (c2 != c) { 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)) && *c) return FALSE; ace->a_perm = ILLEGAL_MODE; return TRUE; } if ((ace->a_perm = getperm (c)) == ILLEGAL_MODE) return FALSE; return TRUE;}BOOLgetaclentries (action_t action, char *buf, aclent_t *acls, int *idx){ char *c; if (action == SetFromFile) { FILE *fp; char fbuf[256], *fb; if (!strcmp (buf, "-")) fp = stdin; else if (! (fp = fopen (buf, "r"))) return FALSE; while ((fb = fgets (fbuf, 256, fp))) { while (strchr (" \t", *fb)) ++fb; if (strchr ("\n\r#", *fb)) continue; if (!getaclentry (action, fb, acls + (*idx)++)) { fclose (fp); return FALSE; } } if (fp != stdin) fclose (fp); } else for (c = strtok (buf, ","); c; c = strtok (NULL, ",")) if (!getaclentry (action, c, acls + (*idx)++)) return FALSE; return TRUE;}intsearchace (aclent_t *aclp, int nentries, int type, int id){ int i; for (i = 0; i < nentries; ++i) if ((aclp[i].a_type == type && (id < 0 || aclp[i].a_id == id)) || !aclp[i].a_type) return i; return -1;}intmodacl (aclent_t *tgt, int tcnt, aclent_t *src, int scnt){ int t, s, i; for (s = 0; s < scnt; ++s) { t = searchace (tgt, MAX_ACL_ENTRIES, src[s].a_type, (src[s].a_type & (USER | GROUP)) ? src[s].a_id : -1); if (t < 0) return -1; if (src[s].a_perm == ILLEGAL_MODE) { if (t < tcnt) { for (i = t + 1; i < tcnt; ++i) tgt[i - 1] = tgt[i]; --tcnt; } } else { tgt[t] = src[s]; if (t >= tcnt) ++tcnt; } } return tcnt;}voidsetfacl (action_t action, char *path, aclent_t *acls, int cnt){ aclent_t lacl[MAX_ACL_ENTRIES]; int lcnt; memset (lacl, 0, sizeof lacl); if (action == Set) { if (acl (path, SETACL, cnt, acls)) perror (prog_name); return; } if ((lcnt = acl (path, GETACL, MAX_ACL_ENTRIES, lacl)) < 0 || (lcnt = modacl (lacl, lcnt, acls, cnt)) < 0 || (lcnt = acl (path, SETACL, lcnt, lacl)) < 0) perror (prog_name);}static voidusage (FILE * stream){ fprintf (stream, "" "Usage: %s [-r] (-f ACL_FILE | -s acl_entries) FILE...\n" " %s [-r] ([-d acl_entries] [-m acl_entries]) FILE...\n"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -