Linux: Rework PAG to group conversions
[openafs.git] / src / afs / LINUX / osi_groups.c
index a034ce2..11bd17a 100644 (file)
 #include "afsincludes.h"
 #include "afs/afs_stats.h"     /* statistics */
 #include "afs/nfsclient.h"
+#include "osi_compat.h"
+
 #include <linux/smp_lock.h>
 
 #ifdef AFS_LINUX26_ONEGROUP_ENV
-#define NUMPAGGROUPS 1
+# define NUMPAGGROUPS 1
+
+static afs_uint32
+afs_linux_pag_from_groups(struct group_info *group_info) {
+    afs_uint32 g0 = 0;
+    afs_uint32 i;
+
+    if (group_info->ngroups < NUMPAGGROUPS)
+       return NOPAG;
+
+    for (i = 0; (i < group_info->ngroups &&
+                (g0 = GROUP_AT(group_info, i)) != (gid_t) NOGROUP); i++) {
+       if (((g0 >> 24) & 0xff) == 'A')
+           return g0;
+    }
+    return NOPAG;
+}
+
+static inline void
+afs_linux_pag_to_groups(afs_uint32 newpag,
+                       struct group_info *old, struct group_info **new) {
+    int need_space = 0;
+    int i;
+    int j;
+
+    if (afs_linux_pag_from_groups(old) == NOPAG)
+       need_space = NUMPAGGROUPS;
+
+    *new = groups_alloc(old->ngroups + need_space);
+
+    for (i = 0, j = 0; i < old->ngroups; ++i) {
+       int ths = GROUP_AT(old, i);
+       int last = i > 0 ? GROUP_AT(old, i-1) : 0;
+       if ((ths >> 24) == 'A')
+           continue;
+       if (last <= newpag && ths > newpag) {
+          GROUP_AT(*new, j) = newpag;
+          j++;
+       }
+       GROUP_AT(*new, j) = ths;
+       j++;
+    }
+    if (j != i + need_space)
+        GROUP_AT(*new, j) = newpag;
+}
+
 #else
-#define NUMPAGGROUPS 2
+# define NUMPAGGROUPS 2
+
+static inline afs_uint32
+afs_linux_pag_from_groups(struct group_info *group_info) {
+
+    if (group_info->ngroups < NUMPAGGROUPS)
+       return NOPAG;
+
+    return afs_get_pag_from_groups(GROUP_AT(group_info, 0), GROUP_AT(group_info, 1));
+}
+
+static inline void
+afs_linux_pag_to_groups(afs_uint32 newpag,
+                       struct group_info *old, struct group_info *new) {
+    int need_space = 0;
+    int i;
+    gid_t g0;
+    gid_t g1;
+
+    if (afs_linux_pag_from_groups(old) == NOPAG)
+       need_space = NUMPAGGGROUPS;
+
+    *new = groups_alloc(old->ngroups + need_space);
+
+    for (i = 0; i < old->ngroups; ++i)
+          GROUP_AT(new, i + need_space) = GROUP_AT(old, i);
+
+    afs_get_groups_from_pag(newpag, &g0, g1);
+    GROUP_AT(new, 0) = g0;
+    GROUP_AT(new, 1) = g1;
+}
 #endif
 
+afs_int32
+osi_get_group_pag(afs_ucred_t *cred) {
+    return afs_linux_pag_from_groups(afs_cr_group_info(cred));
+}
+
+
 static int
 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
 {
@@ -39,9 +122,9 @@ afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
 
     AFS_STATCNT(afs_setgroups);
 
-    old_info = cr_group_info(*cr);
+    old_info = afs_cr_group_info(*cr);
     get_group_info(group_info);
-    set_cr_group_info(*cr, group_info);
+    afs_set_cr_group_info(*cr, group_info);
     put_group_info(old_info);
 
     crset(*cr);
@@ -57,76 +140,23 @@ afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
 
     return (0);
 }
-/* Returns number of groups. And we trust groups to be large enough to
- * hold all the groups.
- */
-static struct group_info *
-afs_getgroups(cred_t * cr)
-{
-    AFS_STATCNT(afs_getgroups);
-
-    get_group_info(cr_group_info(cr));
-    return cr_group_info(cr);
-}
 
 int
 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
          int change_parent)
 {
     struct group_info *group_info;
-#ifndef AFS_LINUX26_ONEGROUP_ENV
-    gid_t g0, g1;
-#endif
     struct group_info *tmp;
-    int i;
-#ifdef AFS_LINUX26_ONEGROUP_ENV
-    int j;
-#endif
-    int need_space = 0;
 
-    group_info = afs_getgroups(*cr);
-    if (group_info->ngroups < NUMPAGGROUPS
-       ||  afs_get_pag_from_groups(
-#ifdef AFS_LINUX26_ONEGROUP_ENV
-           group_info
-#else
-           GROUP_AT(group_info, 0) ,GROUP_AT(group_info, 1)
-#endif
-                                   ) == NOPAG) 
-       /* We will have to make sure group_info is big enough for pag */
-       need_space = NUMPAGGROUPS;
+    get_group_info(afs_cr_group_info(*cr));
+    group_info = afs_cr_group_info(*cr);
 
-    tmp = groups_alloc(group_info->ngroups + need_space);
-    
     *newpag = (pagvalue == -1 ? genpag() : pagvalue);
-#ifdef AFS_LINUX26_ONEGROUP_ENV
-    for (i = 0, j = 0; i < group_info->ngroups; ++i) {
-       int ths = GROUP_AT(group_info, i);
-       int last = i > 0 ? GROUP_AT(group_info, i-1) : 0;
-       if ((ths >> 24) == 'A')
-           continue;
-       if (last <= *newpag && ths > *newpag) {
-          GROUP_AT(tmp, j) = *newpag;
-          j++;
-       }
-       GROUP_AT(tmp, j) = ths;
-       j++;
-    }
-    if (j != i + need_space)
-        GROUP_AT(tmp, j) = *newpag;
-#else
-    for (i = 0; i < group_info->ngroups; ++i)
-      GROUP_AT(tmp, i + need_space) = GROUP_AT(group_info, i);
-#endif
+    afs_linux_pag_to_groups(*newpag, group_info, &tmp);
+
     put_group_info(group_info);
     group_info = tmp;
 
-#ifndef AFS_LINUX26_ONEGROUP_ENV
-    afs_get_groups_from_pag(*newpag, &g0, &g1);
-    GROUP_AT(group_info, 0) = g0;
-    GROUP_AT(group_info, 1) = g1;
-#endif
-
     afs_setgroups(cr, group_info, change_parent);
 
     put_group_info(group_info);
@@ -143,7 +173,6 @@ install_session_keyring(struct key *keyring)
 {
     struct key *old;
     char desc[20];
-    unsigned long not_in_quota;
     int code = -EINVAL;
 
     if (!__key_type_keyring)
@@ -152,25 +181,14 @@ install_session_keyring(struct key *keyring)
     if (!keyring) {
 
        /* create an empty session keyring */
-       not_in_quota = KEY_ALLOC_IN_QUOTA;
        sprintf(desc, "_ses.%u", current->tgid);
 
-#if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
-       keyring = key_alloc(__key_type_keyring, desc,
-                           current_uid(), current_gid(), current,
-                           (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
-                           not_in_quota);
-#elif defined(KEY_ALLOC_NEEDS_CRED)
-       keyring = key_alloc(__key_type_keyring, desc,
-                           current_uid(), current_gid(), current_cred(),
-                           (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
-                           not_in_quota);
-#else
-       keyring = key_alloc(__key_type_keyring, desc,
+       keyring = afs_linux_key_alloc(
+                           __key_type_keyring, desc,
                            current_uid(), current_gid(),
                            (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
-                           not_in_quota);
-#endif
+                           KEY_ALLOC_IN_QUOTA);
+
        if (IS_ERR(keyring)) {
            code = PTR_ERR(keyring);
            goto out;
@@ -209,7 +227,7 @@ setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
     code = __setpag(cr, pagvalue, newpag, change_parent);
 
 #ifdef LINUX_KEYRING_SUPPORT
-    if (code == 0 && cr_rgid(*cr) != NFSXLATOR_CRED) {
+    if (code == 0 && afs_cr_rgid(*cr) != NFSXLATOR_CRED) {
        (void) install_session_keyring(NULL);
 
        if (current_session_keyring()) {
@@ -219,13 +237,7 @@ setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
            perm = KEY_POS_VIEW | KEY_POS_SEARCH;
            perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
 
-#if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
-           key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current, perm, 1);
-#elif defined(KEY_ALLOC_NEEDS_CRED)
-           key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current_cred(), perm, 1);
-#else
-           key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, 1);
-#endif
+           key = afs_linux_key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, 1);
 
            if (!IS_ERR(key)) {
                key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
@@ -422,9 +434,6 @@ static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen
 {
     int code;
     afs_uint32 *userpag, pag = NOPAG;
-#ifndef AFS_LINUX26_ONEGROUP_ENV
-    int g0, g1;
-#endif
 
     if (key->uid != 0 || key->gid != 0)
        return -EPERM;
@@ -435,18 +444,9 @@ static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen
     if (datalen != sizeof(afs_uint32) || !data)
        goto error;
 
-    if (current_group_info()->ngroups < NUMPAGGROUPS)
-       goto error;
-
     /* ensure key being set matches current pag */
-#ifdef AFS_LINUX26_ONEGROUP_ENV
-    pag = afs_get_pag_from_groups(current_group_info());
-#else
-    g0 = GROUP_AT(current_group_info(), 0);
-    g1 = GROUP_AT(current_group_info(), 1);
+    pag = afs_linux_pag_from_groups(current_group_info());
 
-    pag = afs_get_pag_from_groups(g0, g1);
-#endif
     if (pag == NOPAG)
        goto error;
 
@@ -551,6 +551,33 @@ void osi_keyring_shutdown(void)
     unregister_key_type(&key_type_afs_pag);
 }
 
+afs_int32
+osi_get_keyring_pag(afs_ucred_t *cred)
+{
+    struct key *key;
+    afs_uint32 newpag;
+    afs_int32 keyring_pag = NOPAG;
+
+    if (afs_cr_rgid(cred) != NFSXLATOR_CRED) {
+       key = afs_linux_search_keyring(cred, &key_type_afs_pag);
+
+       if (!IS_ERR(key)) {
+           if (key_validate(key) == 0 && key->uid == 0) {      /* also verify in the session keyring? */
+               keyring_pag = key->payload.value;
+               /* Only set PAG in groups if needed,
+                * and the creds are from the current process */
+               if (afs_linux_cred_is_current(cred) &&
+                    ((keyring_pag >> 24) & 0xff) == 'A' &&
+                   keyring_pag != afs_linux_pag_from_groups(current_group_info())) {
+                       __setpag(&cred, keyring_pag, &newpag, 0);
+               }
+           }
+           key_put(key);
+       }
+    }
+    return keyring_pag;
+}
+
 #else
 void osi_keyring_init(void)
 {