📄 vfs_afsacl.c
字号:
/* * Convert AFS acls to NT acls and vice versa. * * Copyright (C) Volker Lendecke, 2003 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include "includes.h"#undef DBGC_CLASS#define DBGC_CLASS DBGC_VFS#include <afs/stds.h>#include <afs/afs.h>#include <afs/auth.h>#include <afs/venus.h>#include <afs/prs_fs.h>#define MAXSIZE 2048extern const DOM_SID global_sid_World;extern const DOM_SID global_sid_Builtin_Administrators;extern const DOM_SID global_sid_Builtin_Backup_Operators;extern const DOM_SID global_sid_Authenticated_Users;extern const DOM_SID global_sid_NULL;static char space_replacement = '%';/* Do we expect SIDs as pts names? */static BOOL sidpts;extern int afs_syscall(int, char *, int, char *, int);struct afs_ace { BOOL positive; char *name; DOM_SID sid; enum SID_NAME_USE type; uint32 rights; struct afs_ace *next;};struct afs_acl { TALLOC_CTX *ctx; int type; int num_aces; struct afs_ace *acelist;};struct afs_iob { char *in, *out; uint16 in_size, out_size;};static BOOL init_afs_acl(struct afs_acl *acl){ ZERO_STRUCT(*acl); acl->ctx = talloc_init("afs_acl"); if (acl->ctx == NULL) { DEBUG(10, ("Could not init afs_acl")); return False; } return True;}static void free_afs_acl(struct afs_acl *acl){ if (acl->ctx != NULL) talloc_destroy(acl->ctx); acl->ctx = NULL; acl->num_aces = 0; acl->acelist = NULL;}static struct afs_ace *clone_afs_ace(TALLOC_CTX *mem_ctx, struct afs_ace *ace){ struct afs_ace *result = TALLOC_P(mem_ctx, struct afs_ace); if (result == NULL) return NULL; *result = *ace; result->next = NULL; result->name = talloc_strdup(mem_ctx, ace->name); if (result->name == NULL) { return NULL; } return result;} static struct afs_ace *new_afs_ace(TALLOC_CTX *mem_ctx, BOOL positive, const char *name, uint32 rights){ DOM_SID sid; enum SID_NAME_USE type; struct afs_ace *result; if (strcmp(name, "system:administrators") == 0) { sid_copy(&sid, &global_sid_Builtin_Administrators); type = SID_NAME_ALIAS; } else if (strcmp(name, "system:anyuser") == 0) { sid_copy(&sid, &global_sid_World); type = SID_NAME_ALIAS; } else if (strcmp(name, "system:authuser") == 0) { sid_copy(&sid, &global_sid_Authenticated_Users); type = SID_NAME_WKN_GRP; } else if (strcmp(name, "system:backup") == 0) { sid_copy(&sid, &global_sid_Builtin_Backup_Operators); type = SID_NAME_ALIAS; } else if (sidpts) { /* All PTS users/groups are expressed as SIDs */ sid_copy(&sid, &global_sid_NULL); type = SID_NAME_UNKNOWN; if (string_to_sid(&sid, name)) { fstring user, domain; /* We have to find the type, look up the SID */ lookup_sid(&sid, domain, user, &type); } } else { fstring domain, uname; char *p; p = strchr_m(name, lp_winbind_separator()); if (p != NULL) { *p = '\\'; } if (!lookup_name(name, LOOKUP_NAME_FULL, domain, uname, &sid, &type)) { DEBUG(10, ("Could not find AFS user %s\n", name)); sid_copy(&sid, &global_sid_NULL); type = SID_NAME_UNKNOWN; } } result = TALLOC_P(mem_ctx, struct afs_ace); if (result == NULL) { DEBUG(0, ("Could not talloc AFS ace\n")); return NULL; } result->name = talloc_strdup(mem_ctx, name); if (result->name == NULL) { DEBUG(0, ("Could not talloc AFS ace name\n")); return NULL; } result->sid = sid; result->type = type; result->positive = positive; result->rights = rights; return result;}static void add_afs_ace(struct afs_acl *acl, BOOL positive, const char *name, uint32 rights){ struct afs_ace *ace; for (ace = acl->acelist; ace != NULL; ace = ace->next) { if ((ace->positive == positive) && (strequal(ace->name, name))) { ace->rights |= rights; return; } } ace = new_afs_ace(acl->ctx, positive, name, rights); ace->next = acl->acelist; acl->acelist = ace; acl->num_aces += 1; DEBUG(10, ("add_afs_ace: Added %s entry for %s with rights %d\n", ace->positive?"positive":"negative", ace->name, ace->rights)); return;}/* AFS ACLs in string form are a long string of fields delimited with \n. * * First line: Number of positive entries * Second line: Number of negative entries * Third and following lines: The entries themselves * * An ACE is a line of two fields, delimited by \t. * * First field: Name * Second field: Rights */static BOOL parse_afs_acl(struct afs_acl *acl, const char *acl_str){ int nplus, nminus; int aces; char str[MAXSIZE+1]; char *p = str; strncpy(str, acl_str, MAXSIZE); if (sscanf(p, "%d", &nplus) != 1) return False; DEBUG(10, ("Found %d positive entries\n", nplus)); if ((p = strchr(p, '\n')) == NULL) return False; p += 1; if (sscanf(p, "%d", &nminus) != 1) return False; DEBUG(10, ("Found %d negative entries\n", nminus)); if ((p = strchr(p, '\n')) == NULL) return False; p += 1; for (aces = nplus+nminus; aces > 0; aces--) { const char *namep; fstring name; uint32 rights; char *space; namep = p; if ((p = strchr(p, '\t')) == NULL) return False; *p = '\0'; p += 1; if (sscanf(p, "%d", &rights) != 1) return False; if ((p = strchr(p, '\n')) == NULL) return False; p += 1; fstrcpy(name, namep); while ((space = strchr_m(name, space_replacement)) != NULL) *space = ' '; add_afs_ace(acl, nplus>0, name, rights); nplus -= 1; } return True;}static BOOL unparse_afs_acl(struct afs_acl *acl, char *acl_str){ /* TODO: String length checks!!!! */ int positives = 0; int negatives = 0; fstring line; *acl_str = 0; struct afs_ace *ace = acl->acelist; while (ace != NULL) { if (ace->positive) positives++; else negatives++; ace = ace->next; } fstr_sprintf(line, "%d\n", positives); safe_strcat(acl_str, line, MAXSIZE); fstr_sprintf(line, "%d\n", negatives); safe_strcat(acl_str, line, MAXSIZE); ace = acl->acelist; while (ace != NULL) { fstr_sprintf(line, "%s\t%d\n", ace->name, ace->rights); safe_strcat(acl_str, line, MAXSIZE); ace = ace->next; } return True;}static uint32 afs_to_nt_file_rights(uint32 rights){ uint32 result = 0; if (rights & PRSFS_READ) result |= FILE_READ_DATA | FILE_READ_EA | FILE_EXECUTE | FILE_READ_ATTRIBUTES | READ_CONTROL_ACCESS | SYNCHRONIZE_ACCESS; if (rights & PRSFS_WRITE) result |= FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA; if (rights & PRSFS_LOCK) result |= WRITE_OWNER_ACCESS; if (rights & PRSFS_DELETE) result |= DELETE_ACCESS; return result;}static void afs_to_nt_dir_rights(uint32 afs_rights, uint32 *nt_rights, uint8 *flag){ *nt_rights = 0; *flag = SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_CONTAINER_INHERIT; if (afs_rights & PRSFS_INSERT) *nt_rights |= FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY; if (afs_rights & PRSFS_LOOKUP) *nt_rights |= FILE_READ_DATA | FILE_READ_EA | FILE_EXECUTE | FILE_READ_ATTRIBUTES | READ_CONTROL_ACCESS | SYNCHRONIZE_ACCESS; if (afs_rights & PRSFS_WRITE) *nt_rights |= FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_EA; if ((afs_rights & (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE)) == (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE)) *nt_rights |= FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | GENERIC_WRITE_ACCESS; if (afs_rights & PRSFS_DELETE) *nt_rights |= DELETE_ACCESS; if (afs_rights & PRSFS_ADMINISTER) *nt_rights |= FILE_DELETE_CHILD | WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS; if ( (afs_rights & PRSFS_LOOKUP) == (afs_rights & (PRSFS_LOOKUP|PRSFS_READ)) ) { /* Only lookup right */ *flag = SEC_ACE_FLAG_CONTAINER_INHERIT; } return;}#define AFS_FILE_RIGHTS (PRSFS_READ|PRSFS_WRITE|PRSFS_LOCK)#define AFS_DIR_RIGHTS (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE|PRSFS_ADMINISTER)static void split_afs_acl(struct afs_acl *acl, struct afs_acl *dir_acl, struct afs_acl *file_acl){ struct afs_ace *ace; init_afs_acl(dir_acl); init_afs_acl(file_acl); for (ace = acl->acelist; ace != NULL; ace = ace->next) { if (ace->rights & AFS_FILE_RIGHTS) { add_afs_ace(file_acl, ace->positive, ace->name, ace->rights & AFS_FILE_RIGHTS); } if (ace->rights & AFS_DIR_RIGHTS) { add_afs_ace(dir_acl, ace->positive, ace->name, ace->rights & AFS_DIR_RIGHTS); } } return;}static BOOL same_principal(struct afs_ace *x, struct afs_ace *y){ return ( (x->positive == y->positive) && (sid_compare(&x->sid, &y->sid) == 0) );}static void merge_afs_acls(struct afs_acl *dir_acl, struct afs_acl *file_acl, struct afs_acl *target){ struct afs_ace *ace; init_afs_acl(target); for (ace = dir_acl->acelist; ace != NULL; ace = ace->next) { struct afs_ace *file_ace; BOOL found = False; for (file_ace = file_acl->acelist; file_ace != NULL; file_ace = file_ace->next) { if (!same_principal(ace, file_ace)) continue; add_afs_ace(target, ace->positive, ace->name, ace->rights | file_ace->rights); found = True; break; } if (!found) add_afs_ace(target, ace->positive, ace->name, ace->rights); } for (ace = file_acl->acelist; ace != NULL; ace = ace->next) { struct afs_ace *dir_ace; BOOL already_seen = False; for (dir_ace = dir_acl->acelist; dir_ace != NULL; dir_ace = dir_ace->next) { if (!same_principal(ace, dir_ace)) continue; already_seen = True; break; } if (!already_seen) add_afs_ace(target, ace->positive, ace->name, ace->rights); }}#define PERMS_READ 0x001200a9#define PERMS_CHANGE 0x001301bf#define PERMS_FULL 0x001f01ffstatic struct static_dir_ace_mapping { uint8 type; uint8 flags; uint32 mask; uint32 afs_rights;} ace_mappings[] = { /* Full control */ { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT, PERMS_FULL, 127 /* rlidwka */ }, /* Change (write) */ { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT, PERMS_CHANGE, 63 /* rlidwk */ }, /* Read (including list folder content) */ { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT, PERMS_READ, 9 /* rl */ }, /* Read without list folder content -- same as "l" */ { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT, 0x00120089, 8 /* l */ }, /* some stupid workaround for preventing fallbacks */ { 0, 0x3, 0x0012019F, 9 /* rl */ }, { 0, 0x13, PERMS_FULL, 127 /* full */ }, /* read, delete and execute access plus synchronize */ { 0, 0x3, 0x001300A9, 9 /* should be rdl, set to rl */}, /* classical read list */ { 0, 0x13, 0x001200A9, 9 /* rl */}, /* almost full control, no delete */ { 0, 0x13, PERMS_CHANGE, 63 /* rwidlk */}, /* List folder */ { 0, SEC_ACE_FLAG_CONTAINER_INHERIT, PERMS_READ, 8 /* l */ }, /* FULL without inheritance -- in all cases here we also get the corresponding INHERIT_ONLY ACE in the same ACL */ { 0, 0, PERMS_FULL, 127 /* rlidwka */ }, /* FULL inherit only -- counterpart to previous one */ { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_INHERIT_ONLY, PERMS_FULL | GENERIC_RIGHT_WRITE_ACCESS, 127 /* rlidwka */ }, /* CHANGE without inheritance -- in all cases here we also get the corresponding INHERIT_ONLY ACE in the same ACL */ { 0, 0, PERMS_CHANGE, 63 /* rlidwk */ },
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -