linux/fs/nfsd/nfs4acl.c
<<
>>
Prefs
   1/*
   2 *  Common NFSv4 ACL handling code.
   3 *
   4 *  Copyright (c) 2002, 2003 The Regents of the University of Michigan.
   5 *  All rights reserved.
   6 *
   7 *  Marius Aamodt Eriksen <marius@umich.edu>
   8 *  Jeff Sedlak <jsedlak@umich.edu>
   9 *  J. Bruce Fields <bfields@umich.edu>
  10 *
  11 *  Redistribution and use in source and binary forms, with or without
  12 *  modification, are permitted provided that the following conditions
  13 *  are met:
  14 *
  15 *  1. Redistributions of source code must retain the above copyright
  16 *     notice, this list of conditions and the following disclaimer.
  17 *  2. Redistributions in binary form must reproduce the above copyright
  18 *     notice, this list of conditions and the following disclaimer in the
  19 *     documentation and/or other materials provided with the distribution.
  20 *  3. Neither the name of the University nor the names of its
  21 *     contributors may be used to endorse or promote products derived
  22 *     from this software without specific prior written permission.
  23 *
  24 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35 */
  36
  37#include <linux/slab.h>
  38#include <linux/nfs_fs.h>
  39#include "nfsfh.h"
  40#include "nfsd.h"
  41#include "acl.h"
  42#include "vfs.h"
  43
  44#define NFS4_ACL_TYPE_DEFAULT   0x01
  45#define NFS4_ACL_DIR            0x02
  46#define NFS4_ACL_OWNER          0x04
  47
  48/* mode bit translations: */
  49#define NFS4_READ_MODE (NFS4_ACE_READ_DATA)
  50#define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_APPEND_DATA)
  51#define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE
  52#define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE)
  53#define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)
  54
  55/* flags used to simulate posix default ACLs */
  56#define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
  57                | NFS4_ACE_DIRECTORY_INHERIT_ACE)
  58
  59#define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
  60                | NFS4_ACE_INHERIT_ONLY_ACE \
  61                | NFS4_ACE_IDENTIFIER_GROUP)
  62
  63static u32
  64mask_from_posix(unsigned short perm, unsigned int flags)
  65{
  66        int mask = NFS4_ANYONE_MODE;
  67
  68        if (flags & NFS4_ACL_OWNER)
  69                mask |= NFS4_OWNER_MODE;
  70        if (perm & ACL_READ)
  71                mask |= NFS4_READ_MODE;
  72        if (perm & ACL_WRITE)
  73                mask |= NFS4_WRITE_MODE;
  74        if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
  75                mask |= NFS4_ACE_DELETE_CHILD;
  76        if (perm & ACL_EXECUTE)
  77                mask |= NFS4_EXECUTE_MODE;
  78        return mask;
  79}
  80
  81static u32
  82deny_mask_from_posix(unsigned short perm, u32 flags)
  83{
  84        u32 mask = 0;
  85
  86        if (perm & ACL_READ)
  87                mask |= NFS4_READ_MODE;
  88        if (perm & ACL_WRITE)
  89                mask |= NFS4_WRITE_MODE;
  90        if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
  91                mask |= NFS4_ACE_DELETE_CHILD;
  92        if (perm & ACL_EXECUTE)
  93                mask |= NFS4_EXECUTE_MODE;
  94        return mask;
  95}
  96
  97/* XXX: modify functions to return NFS errors; they're only ever
  98 * used by nfs code, after all.... */
  99
 100/* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the
 101 * side of being more restrictive, so the mode bit mapping below is
 102 * pessimistic.  An optimistic version would be needed to handle DENY's,
 103 * but we espect to coalesce all ALLOWs and DENYs before mapping to mode
 104 * bits. */
 105
 106static void
 107low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
 108{
 109        u32 write_mode = NFS4_WRITE_MODE;
 110
 111        if (flags & NFS4_ACL_DIR)
 112                write_mode |= NFS4_ACE_DELETE_CHILD;
 113        *mode = 0;
 114        if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
 115                *mode |= ACL_READ;
 116        if ((perm & write_mode) == write_mode)
 117                *mode |= ACL_WRITE;
 118        if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
 119                *mode |= ACL_EXECUTE;
 120}
 121
 122static short ace2type(struct nfs4_ace *);
 123static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *,
 124                                unsigned int);
 125
 126int
 127nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry,
 128                struct nfs4_acl **acl)
 129{
 130        struct inode *inode = d_inode(dentry);
 131        int error = 0;
 132        struct posix_acl *pacl = NULL, *dpacl = NULL;
 133        unsigned int flags = 0;
 134        int size = 0;
 135
 136        pacl = get_acl(inode, ACL_TYPE_ACCESS);
 137        if (!pacl)
 138                pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
 139
 140        if (IS_ERR(pacl))
 141                return PTR_ERR(pacl);
 142
 143        /* allocate for worst case: one (deny, allow) pair each: */
 144        size += 2 * pacl->a_count;
 145
 146        if (S_ISDIR(inode->i_mode)) {
 147                flags = NFS4_ACL_DIR;
 148                dpacl = get_acl(inode, ACL_TYPE_DEFAULT);
 149                if (IS_ERR(dpacl)) {
 150                        error = PTR_ERR(dpacl);
 151                        goto rel_pacl;
 152                }
 153
 154                if (dpacl)
 155                        size += 2 * dpacl->a_count;
 156        }
 157
 158        *acl = kmalloc(nfs4_acl_bytes(size), GFP_KERNEL);
 159        if (*acl == NULL) {
 160                error = -ENOMEM;
 161                goto out;
 162        }
 163        (*acl)->naces = 0;
 164
 165        _posix_to_nfsv4_one(pacl, *acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
 166
 167        if (dpacl)
 168                _posix_to_nfsv4_one(dpacl, *acl, flags | NFS4_ACL_TYPE_DEFAULT);
 169
 170out:
 171        posix_acl_release(dpacl);
 172rel_pacl:
 173        posix_acl_release(pacl);
 174        return error;
 175}
 176
 177struct posix_acl_summary {
 178        unsigned short owner;
 179        unsigned short users;
 180        unsigned short group;
 181        unsigned short groups;
 182        unsigned short other;
 183        unsigned short mask;
 184};
 185
 186static void
 187summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
 188{
 189        struct posix_acl_entry *pa, *pe;
 190
 191        /*
 192         * Only pas.users and pas.groups need initialization; previous
 193         * posix_acl_valid() calls ensure that the other fields will be
 194         * initialized in the following loop.  But, just to placate gcc:
 195         */
 196        memset(pas, 0, sizeof(*pas));
 197        pas->mask = 07;
 198
 199        pe = acl->a_entries + acl->a_count;
 200
 201        FOREACH_ACL_ENTRY(pa, acl, pe) {
 202                switch (pa->e_tag) {
 203                        case ACL_USER_OBJ:
 204                                pas->owner = pa->e_perm;
 205                                break;
 206                        case ACL_GROUP_OBJ:
 207                                pas->group = pa->e_perm;
 208                                break;
 209                        case ACL_USER:
 210                                pas->users |= pa->e_perm;
 211                                break;
 212                        case ACL_GROUP:
 213                                pas->groups |= pa->e_perm;
 214                                break;
 215                        case ACL_OTHER:
 216                                pas->other = pa->e_perm;
 217                                break;
 218                        case ACL_MASK:
 219                                pas->mask = pa->e_perm;
 220                                break;
 221                }
 222        }
 223        /* We'll only care about effective permissions: */
 224        pas->users &= pas->mask;
 225        pas->group &= pas->mask;
 226        pas->groups &= pas->mask;
 227}
 228
 229/* We assume the acl has been verified with posix_acl_valid. */
 230static void
 231_posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
 232                                                unsigned int flags)
 233{
 234        struct posix_acl_entry *pa, *group_owner_entry;
 235        struct nfs4_ace *ace;
 236        struct posix_acl_summary pas;
 237        unsigned short deny;
 238        int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
 239                NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0);
 240
 241        BUG_ON(pacl->a_count < 3);
 242        summarize_posix_acl(pacl, &pas);
 243
 244        pa = pacl->a_entries;
 245        ace = acl->aces + acl->naces;
 246
 247        /* We could deny everything not granted by the owner: */
 248        deny = ~pas.owner;
 249        /*
 250         * but it is equivalent (and simpler) to deny only what is not
 251         * granted by later entries:
 252         */
 253        deny &= pas.users | pas.group | pas.groups | pas.other;
 254        if (deny) {
 255                ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
 256                ace->flag = eflag;
 257                ace->access_mask = deny_mask_from_posix(deny, flags);
 258                ace->whotype = NFS4_ACL_WHO_OWNER;
 259                ace++;
 260                acl->naces++;
 261        }
 262
 263        ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
 264        ace->flag = eflag;
 265        ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
 266        ace->whotype = NFS4_ACL_WHO_OWNER;
 267        ace++;
 268        acl->naces++;
 269        pa++;
 270
 271        while (pa->e_tag == ACL_USER) {
 272                deny = ~(pa->e_perm & pas.mask);
 273                deny &= pas.groups | pas.group | pas.other;
 274                if (deny) {
 275                        ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
 276                        ace->flag = eflag;
 277                        ace->access_mask = deny_mask_from_posix(deny, flags);
 278                        ace->whotype = NFS4_ACL_WHO_NAMED;
 279                        ace->who_uid = pa->e_uid;
 280                        ace++;
 281                        acl->naces++;
 282                }
 283                ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
 284                ace->flag = eflag;
 285                ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
 286                                                   flags);
 287                ace->whotype = NFS4_ACL_WHO_NAMED;
 288                ace->who_uid = pa->e_uid;
 289                ace++;
 290                acl->naces++;
 291                pa++;
 292        }
 293
 294        /* In the case of groups, we apply allow ACEs first, then deny ACEs,
 295         * since a user can be in more than one group.  */
 296
 297        /* allow ACEs */
 298
 299        group_owner_entry = pa;
 300
 301        ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
 302        ace->flag = eflag;
 303        ace->access_mask = mask_from_posix(pas.group, flags);
 304        ace->whotype = NFS4_ACL_WHO_GROUP;
 305        ace++;
 306        acl->naces++;
 307        pa++;
 308
 309        while (pa->e_tag == ACL_GROUP) {
 310                ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
 311                ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
 312                ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
 313                                                   flags);
 314                ace->whotype = NFS4_ACL_WHO_NAMED;
 315                ace->who_gid = pa->e_gid;
 316                ace++;
 317                acl->naces++;
 318                pa++;
 319        }
 320
 321        /* deny ACEs */
 322
 323        pa = group_owner_entry;
 324
 325        deny = ~pas.group & pas.other;
 326        if (deny) {
 327                ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
 328                ace->flag = eflag;
 329                ace->access_mask = deny_mask_from_posix(deny, flags);
 330                ace->whotype = NFS4_ACL_WHO_GROUP;
 331                ace++;
 332                acl->naces++;
 333        }
 334        pa++;
 335
 336        while (pa->e_tag == ACL_GROUP) {
 337                deny = ~(pa->e_perm & pas.mask);
 338                deny &= pas.other;
 339                if (deny) {
 340                        ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
 341                        ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
 342                        ace->access_mask = deny_mask_from_posix(deny, flags);
 343                        ace->whotype = NFS4_ACL_WHO_NAMED;
 344                        ace->who_gid = pa->e_gid;
 345                        ace++;
 346                        acl->naces++;
 347                }
 348                pa++;
 349        }
 350
 351        if (pa->e_tag == ACL_MASK)
 352                pa++;
 353        ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
 354        ace->flag = eflag;
 355        ace->access_mask = mask_from_posix(pa->e_perm, flags);
 356        ace->whotype = NFS4_ACL_WHO_EVERYONE;
 357        acl->naces++;
 358}
 359
 360static bool
 361pace_gt(struct posix_acl_entry *pace1, struct posix_acl_entry *pace2)
 362{
 363        if (pace1->e_tag != pace2->e_tag)
 364                return pace1->e_tag > pace2->e_tag;
 365        if (pace1->e_tag == ACL_USER)
 366                return uid_gt(pace1->e_uid, pace2->e_uid);
 367        if (pace1->e_tag == ACL_GROUP)
 368                return gid_gt(pace1->e_gid, pace2->e_gid);
 369        return false;
 370}
 371
 372static void
 373sort_pacl_range(struct posix_acl *pacl, int start, int end) {
 374        int sorted = 0, i;
 375
 376        /* We just do a bubble sort; easy to do in place, and we're not
 377         * expecting acl's to be long enough to justify anything more. */
 378        while (!sorted) {
 379                sorted = 1;
 380                for (i = start; i < end; i++) {
 381                        if (pace_gt(&pacl->a_entries[i],
 382                                    &pacl->a_entries[i+1])) {
 383                                sorted = 0;
 384                                swap(pacl->a_entries[i],
 385                                     pacl->a_entries[i + 1]);
 386                        }
 387                }
 388        }
 389}
 390
 391static void
 392sort_pacl(struct posix_acl *pacl)
 393{
 394        /* posix_acl_valid requires that users and groups be in order
 395         * by uid/gid. */
 396        int i, j;
 397
 398        /* no users or groups */
 399        if (!pacl || pacl->a_count <= 4)
 400                return;
 401
 402        i = 1;
 403        while (pacl->a_entries[i].e_tag == ACL_USER)
 404                i++;
 405        sort_pacl_range(pacl, 1, i-1);
 406
 407        BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
 408        j = ++i;
 409        while (pacl->a_entries[j].e_tag == ACL_GROUP)
 410                j++;
 411        sort_pacl_range(pacl, i, j-1);
 412        return;
 413}
 414
 415/*
 416 * While processing the NFSv4 ACE, this maintains bitmasks representing
 417 * which permission bits have been allowed and which denied to a given
 418 * entity: */
 419struct posix_ace_state {
 420        u32 allow;
 421        u32 deny;
 422};
 423
 424struct posix_user_ace_state {
 425        union {
 426                kuid_t uid;
 427                kgid_t gid;
 428        };
 429        struct posix_ace_state perms;
 430};
 431
 432struct posix_ace_state_array {
 433        int n;
 434        struct posix_user_ace_state aces[];
 435};
 436
 437/*
 438 * While processing the NFSv4 ACE, this maintains the partial permissions
 439 * calculated so far: */
 440
 441struct posix_acl_state {
 442        int empty;
 443        struct posix_ace_state owner;
 444        struct posix_ace_state group;
 445        struct posix_ace_state other;
 446        struct posix_ace_state everyone;
 447        struct posix_ace_state mask; /* Deny unused in this case */
 448        struct posix_ace_state_array *users;
 449        struct posix_ace_state_array *groups;
 450};
 451
 452static int
 453init_state(struct posix_acl_state *state, int cnt)
 454{
 455        int alloc;
 456
 457        memset(state, 0, sizeof(struct posix_acl_state));
 458        state->empty = 1;
 459        /*
 460         * In the worst case, each individual acl could be for a distinct
 461         * named user or group, but we don't no which, so we allocate
 462         * enough space for either:
 463         */
 464        alloc = sizeof(struct posix_ace_state_array)
 465                + cnt*sizeof(struct posix_user_ace_state);
 466        state->users = kzalloc(alloc, GFP_KERNEL);
 467        if (!state->users)
 468                return -ENOMEM;
 469        state->groups = kzalloc(alloc, GFP_KERNEL);
 470        if (!state->groups) {
 471                kfree(state->users);
 472                return -ENOMEM;
 473        }
 474        return 0;
 475}
 476
 477static void
 478free_state(struct posix_acl_state *state) {
 479        kfree(state->users);
 480        kfree(state->groups);
 481}
 482
 483static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
 484{
 485        state->mask.allow |= astate->allow;
 486}
 487
 488static struct posix_acl *
 489posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
 490{
 491        struct posix_acl_entry *pace;
 492        struct posix_acl *pacl;
 493        int nace;
 494        int i;
 495
 496        /*
 497         * ACLs with no ACEs are treated differently in the inheritable
 498         * and effective cases: when there are no inheritable ACEs,
 499         * calls ->set_acl with a NULL ACL structure.
 500         */
 501        if (state->empty && (flags & NFS4_ACL_TYPE_DEFAULT))
 502                return NULL;
 503
 504        /*
 505         * When there are no effective ACEs, the following will end
 506         * up setting a 3-element effective posix ACL with all
 507         * permissions zero.
 508         */
 509        if (!state->users->n && !state->groups->n)
 510                nace = 3;
 511        else /* Note we also include a MASK ACE in this case: */
 512                nace = 4 + state->users->n + state->groups->n;
 513        pacl = posix_acl_alloc(nace, GFP_KERNEL);
 514        if (!pacl)
 515                return ERR_PTR(-ENOMEM);
 516
 517        pace = pacl->a_entries;
 518        pace->e_tag = ACL_USER_OBJ;
 519        low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
 520
 521        for (i=0; i < state->users->n; i++) {
 522                pace++;
 523                pace->e_tag = ACL_USER;
 524                low_mode_from_nfs4(state->users->aces[i].perms.allow,
 525                                        &pace->e_perm, flags);
 526                pace->e_uid = state->users->aces[i].uid;
 527                add_to_mask(state, &state->users->aces[i].perms);
 528        }
 529
 530        pace++;
 531        pace->e_tag = ACL_GROUP_OBJ;
 532        low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
 533        add_to_mask(state, &state->group);
 534
 535        for (i=0; i < state->groups->n; i++) {
 536                pace++;
 537                pace->e_tag = ACL_GROUP;
 538                low_mode_from_nfs4(state->groups->aces[i].perms.allow,
 539                                        &pace->e_perm, flags);
 540                pace->e_gid = state->groups->aces[i].gid;
 541                add_to_mask(state, &state->groups->aces[i].perms);
 542        }
 543
 544        if (state->users->n || state->groups->n) {
 545                pace++;
 546                pace->e_tag = ACL_MASK;
 547                low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
 548        }
 549
 550        pace++;
 551        pace->e_tag = ACL_OTHER;
 552        low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
 553
 554        return pacl;
 555}
 556
 557static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
 558{
 559        /* Allow all bits in the mask not already denied: */
 560        astate->allow |= mask & ~astate->deny;
 561}
 562
 563static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
 564{
 565        /* Deny all bits in the mask not already allowed: */
 566        astate->deny |= mask & ~astate->allow;
 567}
 568
 569static int find_uid(struct posix_acl_state *state, kuid_t uid)
 570{
 571        struct posix_ace_state_array *a = state->users;
 572        int i;
 573
 574        for (i = 0; i < a->n; i++)
 575                if (uid_eq(a->aces[i].uid, uid))
 576                        return i;
 577        /* Not found: */
 578        a->n++;
 579        a->aces[i].uid = uid;
 580        a->aces[i].perms.allow = state->everyone.allow;
 581        a->aces[i].perms.deny  = state->everyone.deny;
 582
 583        return i;
 584}
 585
 586static int find_gid(struct posix_acl_state *state, kgid_t gid)
 587{
 588        struct posix_ace_state_array *a = state->groups;
 589        int i;
 590
 591        for (i = 0; i < a->n; i++)
 592                if (gid_eq(a->aces[i].gid, gid))
 593                        return i;
 594        /* Not found: */
 595        a->n++;
 596        a->aces[i].gid = gid;
 597        a->aces[i].perms.allow = state->everyone.allow;
 598        a->aces[i].perms.deny  = state->everyone.deny;
 599
 600        return i;
 601}
 602
 603static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
 604{
 605        int i;
 606
 607        for (i=0; i < a->n; i++)
 608                deny_bits(&a->aces[i].perms, mask);
 609}
 610
 611static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
 612{
 613        int i;
 614
 615        for (i=0; i < a->n; i++)
 616                allow_bits(&a->aces[i].perms, mask);
 617}
 618
 619static void process_one_v4_ace(struct posix_acl_state *state,
 620                                struct nfs4_ace *ace)
 621{
 622        u32 mask = ace->access_mask;
 623        int i;
 624
 625        state->empty = 0;
 626
 627        switch (ace2type(ace)) {
 628        case ACL_USER_OBJ:
 629                if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
 630                        allow_bits(&state->owner, mask);
 631                } else {
 632                        deny_bits(&state->owner, mask);
 633                }
 634                break;
 635        case ACL_USER:
 636                i = find_uid(state, ace->who_uid);
 637                if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
 638                        allow_bits(&state->users->aces[i].perms, mask);
 639                } else {
 640                        deny_bits(&state->users->aces[i].perms, mask);
 641                        mask = state->users->aces[i].perms.deny;
 642                        deny_bits(&state->owner, mask);
 643                }
 644                break;
 645        case ACL_GROUP_OBJ:
 646                if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
 647                        allow_bits(&state->group, mask);
 648                } else {
 649                        deny_bits(&state->group, mask);
 650                        mask = state->group.deny;
 651                        deny_bits(&state->owner, mask);
 652                        deny_bits(&state->everyone, mask);
 653                        deny_bits_array(state->users, mask);
 654                        deny_bits_array(state->groups, mask);
 655                }
 656                break;
 657        case ACL_GROUP:
 658                i = find_gid(state, ace->who_gid);
 659                if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
 660                        allow_bits(&state->groups->aces[i].perms, mask);
 661                } else {
 662                        deny_bits(&state->groups->aces[i].perms, mask);
 663                        mask = state->groups->aces[i].perms.deny;
 664                        deny_bits(&state->owner, mask);
 665                        deny_bits(&state->group, mask);
 666                        deny_bits(&state->everyone, mask);
 667                        deny_bits_array(state->users, mask);
 668                        deny_bits_array(state->groups, mask);
 669                }
 670                break;
 671        case ACL_OTHER:
 672                if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
 673                        allow_bits(&state->owner, mask);
 674                        allow_bits(&state->group, mask);
 675                        allow_bits(&state->other, mask);
 676                        allow_bits(&state->everyone, mask);
 677                        allow_bits_array(state->users, mask);
 678                        allow_bits_array(state->groups, mask);
 679                } else {
 680                        deny_bits(&state->owner, mask);
 681                        deny_bits(&state->group, mask);
 682                        deny_bits(&state->other, mask);
 683                        deny_bits(&state->everyone, mask);
 684                        deny_bits_array(state->users, mask);
 685                        deny_bits_array(state->groups, mask);
 686                }
 687        }
 688}
 689
 690static int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl,
 691                struct posix_acl **pacl, struct posix_acl **dpacl,
 692                unsigned int flags)
 693{
 694        struct posix_acl_state effective_acl_state, default_acl_state;
 695        struct nfs4_ace *ace;
 696        int ret;
 697
 698        ret = init_state(&effective_acl_state, acl->naces);
 699        if (ret)
 700                return ret;
 701        ret = init_state(&default_acl_state, acl->naces);
 702        if (ret)
 703                goto out_estate;
 704        ret = -EINVAL;
 705        for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
 706                if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
 707                    ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
 708                        goto out_dstate;
 709                if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
 710                        goto out_dstate;
 711                if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
 712                        process_one_v4_ace(&effective_acl_state, ace);
 713                        continue;
 714                }
 715                if (!(flags & NFS4_ACL_DIR))
 716                        goto out_dstate;
 717                /*
 718                 * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
 719                 * is set, we're effectively turning on the other.  That's OK,
 720                 * according to rfc 3530.
 721                 */
 722                process_one_v4_ace(&default_acl_state, ace);
 723
 724                if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
 725                        process_one_v4_ace(&effective_acl_state, ace);
 726        }
 727        *pacl = posix_state_to_acl(&effective_acl_state, flags);
 728        if (IS_ERR(*pacl)) {
 729                ret = PTR_ERR(*pacl);
 730                *pacl = NULL;
 731                goto out_dstate;
 732        }
 733        *dpacl = posix_state_to_acl(&default_acl_state,
 734                                                flags | NFS4_ACL_TYPE_DEFAULT);
 735        if (IS_ERR(*dpacl)) {
 736                ret = PTR_ERR(*dpacl);
 737                *dpacl = NULL;
 738                posix_acl_release(*pacl);
 739                *pacl = NULL;
 740                goto out_dstate;
 741        }
 742        sort_pacl(*pacl);
 743        sort_pacl(*dpacl);
 744        ret = 0;
 745out_dstate:
 746        free_state(&default_acl_state);
 747out_estate:
 748        free_state(&effective_acl_state);
 749        return ret;
 750}
 751
 752__be32
 753nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
 754                struct nfs4_acl *acl)
 755{
 756        __be32 error;
 757        int host_error;
 758        struct dentry *dentry;
 759        struct inode *inode;
 760        struct posix_acl *pacl = NULL, *dpacl = NULL;
 761        unsigned int flags = 0;
 762
 763        /* Get inode */
 764        error = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
 765        if (error)
 766                return error;
 767
 768        dentry = fhp->fh_dentry;
 769        inode = d_inode(dentry);
 770
 771        if (!inode->i_op->set_acl || !IS_POSIXACL(inode))
 772                return nfserr_attrnotsupp;
 773
 774        if (S_ISDIR(inode->i_mode))
 775                flags = NFS4_ACL_DIR;
 776
 777        host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
 778        if (host_error == -EINVAL)
 779                return nfserr_attrnotsupp;
 780        if (host_error < 0)
 781                goto out_nfserr;
 782
 783        host_error = inode->i_op->set_acl(inode, pacl, ACL_TYPE_ACCESS);
 784        if (host_error < 0)
 785                goto out_release;
 786
 787        if (S_ISDIR(inode->i_mode)) {
 788                host_error = inode->i_op->set_acl(inode, dpacl,
 789                                                  ACL_TYPE_DEFAULT);
 790        }
 791
 792out_release:
 793        posix_acl_release(pacl);
 794        posix_acl_release(dpacl);
 795out_nfserr:
 796        if (host_error == -EOPNOTSUPP)
 797                return nfserr_attrnotsupp;
 798        else
 799                return nfserrno(host_error);
 800}
 801
 802
 803static short
 804ace2type(struct nfs4_ace *ace)
 805{
 806        switch (ace->whotype) {
 807                case NFS4_ACL_WHO_NAMED:
 808                        return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
 809                                        ACL_GROUP : ACL_USER);
 810                case NFS4_ACL_WHO_OWNER:
 811                        return ACL_USER_OBJ;
 812                case NFS4_ACL_WHO_GROUP:
 813                        return ACL_GROUP_OBJ;
 814                case NFS4_ACL_WHO_EVERYONE:
 815                        return ACL_OTHER;
 816        }
 817        BUG();
 818        return -1;
 819}
 820
 821/*
 822 * return the size of the struct nfs4_acl required to represent an acl
 823 * with @entries entries.
 824 */
 825int nfs4_acl_bytes(int entries)
 826{
 827        return sizeof(struct nfs4_acl) + entries * sizeof(struct nfs4_ace);
 828}
 829
 830static struct {
 831        char *string;
 832        int   stringlen;
 833        int type;
 834} s2t_map[] = {
 835        {
 836                .string    = "OWNER@",
 837                .stringlen = sizeof("OWNER@") - 1,
 838                .type      = NFS4_ACL_WHO_OWNER,
 839        },
 840        {
 841                .string    = "GROUP@",
 842                .stringlen = sizeof("GROUP@") - 1,
 843                .type      = NFS4_ACL_WHO_GROUP,
 844        },
 845        {
 846                .string    = "EVERYONE@",
 847                .stringlen = sizeof("EVERYONE@") - 1,
 848                .type      = NFS4_ACL_WHO_EVERYONE,
 849        },
 850};
 851
 852int
 853nfs4_acl_get_whotype(char *p, u32 len)
 854{
 855        int i;
 856
 857        for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
 858                if (s2t_map[i].stringlen == len &&
 859                                0 == memcmp(s2t_map[i].string, p, len))
 860                        return s2t_map[i].type;
 861        }
 862        return NFS4_ACL_WHO_NAMED;
 863}
 864
 865__be32 nfs4_acl_write_who(struct xdr_stream *xdr, int who)
 866{
 867        __be32 *p;
 868        int i;
 869
 870        for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
 871                if (s2t_map[i].type != who)
 872                        continue;
 873                p = xdr_reserve_space(xdr, s2t_map[i].stringlen + 4);
 874                if (!p)
 875                        return nfserr_resource;
 876                p = xdr_encode_opaque(p, s2t_map[i].string,
 877                                        s2t_map[i].stringlen);
 878                return 0;
 879        }
 880        WARN_ON_ONCE(1);
 881        return nfserr_serverfault;
 882}
 883