Remove pre-Linux 2.6 support
[openafs.git] / src / afs / LINUX / osi_groups.c
index bf21172..f78562a 100644 (file)
 #include <linux/seq_file.h>
 #endif
 
-RCSID
-    ("$Header$");
 
 #include "afs/sysincludes.h"
 #include "afsincludes.h"
 #include "afs/afs_stats.h"     /* statistics */
-#ifdef AFS_LINUX22_ENV
+#include "afs/nfsclient.h"
 #include "h/smp_lock.h"
+
+#ifdef AFS_LINUX26_ONEGROUP_ENV
+#define NUMPAGGROUPS 1
+#else
+#define NUMPAGGROUPS 2
 #endif
 
-#if defined(AFS_LINUX26_ENV)
 static int
 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
 {
@@ -44,43 +46,20 @@ afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
 
     crset(*cr);
 
+#if defined(STRUCT_TASK_STRUCT_HAS_PARENT) && !defined(STRUCT_TASK_HAS_CRED)
     if (change_parent) {
        old_info = current->parent->group_info;
        get_group_info(group_info);
        current->parent->group_info = group_info;
        put_group_info(old_info);
     }
+#endif
 
     return (0);
 }
-#else
-static int
-afs_setgroups(cred_t **cr, int ngroups, gid_t * gidset, int change_parent)
-{
-    int ngrps;
-    int i;
-    gid_t *gp;
-
-    AFS_STATCNT(afs_setgroups);
-
-    if (ngroups > NGROUPS)
-       return EINVAL;
-
-    gp = (*cr)->cr_groups;
-    if (ngroups < NGROUPS)
-       gp[ngroups] = (gid_t) NOGROUP;
-
-    for (i = ngroups; i > 0; i--) {
-       *gp++ = *gidset++;
-    }
-
-    (*cr)->cr_ngroups = ngroups;
-    crset(*cr);
-    return (0);
-}
-#endif
-
-#if defined(AFS_LINUX26_ENV)
+/* 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)
 {
@@ -89,99 +68,64 @@ afs_getgroups(cred_t * cr)
     get_group_info(cr->cr_group_info);
     return cr->cr_group_info;
 }
-#else
-/* Returns number of groups. And we trust groups to be large enough to
- * hold all the groups.
- */
-static int
-afs_getgroups(cred_t *cr, gid_t *groups)
-{
-    int i;
-    int n;
-    gid_t *gp;
-
-    AFS_STATCNT(afs_getgroups);
-
-    gp = cr->cr_groups;
-    n = cr->cr_ngroups;
-
-    for (i = 0; (i < n) && (*gp != (gid_t) NOGROUP); i++)
-       *groups++ = *gp++;
-    return i;
-}
-#endif
-
-#if !defined(AFS_LINUX26_ENV)
-/* Only propogate the PAG to the parent process. Unix's propogate to 
- * all processes sharing the cred.
- */
-int
-set_pag_in_parent(int pag, int g0, int g1)
-{
-    int i;
-#ifdef STRUCT_TASK_STRUCT_HAS_PARENT
-    gid_t *gp = current->parent->groups;
-    int ngroups = current->parent->ngroups;
-#else
-    gid_t *gp = current->p_pptr->groups;
-    int ngroups = current->p_pptr->ngroups;
-#endif
-
-    if ((ngroups < 2) || (afs_get_pag_from_groups(gp[0], gp[1]) == NOPAG)) {
-       /* We will have to shift grouplist to make room for pag */
-       if (ngroups + 2 > NGROUPS) {
-           return EINVAL;
-       }
-       for (i = ngroups - 1; i >= 0; i--) {
-           gp[i + 2] = gp[i];
-       }
-       ngroups += 2;
-    }
-    gp[0] = g0;
-    gp[1] = g1;
-    if (ngroups < NGROUPS)
-       gp[ngroups] = NOGROUP;
-
-#ifdef STRUCT_TASK_STRUCT_HAS_PARENT
-    current->parent->ngroups = ngroups;
-#else
-    current->p_pptr->ngroups = ngroups;
-#endif
-    return 0;
-}
-#endif
 
-#if defined(AFS_LINUX26_ENV)
 int
-__setpag(cred_t ** cr, afs_uint32 pagvalue, afs_uint32 * newpag,
+__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;
 
-    AFS_STATCNT(setpag);
-
     group_info = afs_getgroups(*cr);
-    if (group_info->ngroups < 2
-       ||  afs_get_pag_from_groups(GROUP_AT(group_info, 0),
-                                   GROUP_AT(group_info, 1)) == NOPAG) 
+    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 = 2;
+       need_space = NUMPAGGROUPS;
 
     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
     put_group_info(group_info);
     group_info = tmp;
 
-    *newpag = (pagvalue == -1 ? genpag() : pagvalue);
+#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);
 
@@ -191,48 +135,101 @@ __setpag(cred_t ** cr, afs_uint32 pagvalue, afs_uint32 * newpag,
 }
 
 #ifdef LINUX_KEYRING_SUPPORT
-#include <asm/unistd.h>
-#include <linux/keyctl.h>
+extern struct key_type key_type_keyring __attribute__((weak));
+static struct key_type *__key_type_keyring = &key_type_keyring;
 
-static int errno;
-static inline _syscall2(long, keyctl, int, option, void*, arg2);
-
-static long
-__join_session_keyring(char *name)
+static int
+install_session_keyring(struct key *keyring)
 {
-       return keyctl(KEYCTL_JOIN_SESSION_KEYRING, name);
+    struct key *old;
+    char desc[20];
+    unsigned long not_in_quota;
+    int code = -EINVAL;
+
+    if (!__key_type_keyring)
+       return code;
+
+    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,
+                           current_uid(), current_gid(),
+                           (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
+                           not_in_quota);
+#endif
+       if (IS_ERR(keyring)) {
+           code = PTR_ERR(keyring);
+           goto out;
+       }
+    }
+
+    code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
+    if (code < 0) {
+       key_put(keyring);
+       goto out;
+    }
+
+    /* install the keyring */
+    spin_lock_irq(&current->sighand->siglock);
+    old = task_session_keyring(current);
+    smp_wmb();
+    task_session_keyring(current) = keyring;
+    spin_unlock_irq(&current->sighand->siglock);
+
+    if (old)
+           key_put(old);
+
+out:
+    return code;
 }
 #endif /* LINUX_KEYRING_SUPPORT */
 
 int
-setpag(cred_t ** cr, afs_uint32 pagvalue, afs_uint32 * newpag,
-         int change_parent)
+setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
+       int change_parent)
 {
     int code;
 
+    AFS_STATCNT(setpag);
+
     code = __setpag(cr, pagvalue, newpag, change_parent);
 
 #ifdef LINUX_KEYRING_SUPPORT
-    if (code == 0) {
-
-       (void) __join_session_keyring(NULL);
+    if (code == 0 && (*cr)->cr_rgid != NFSXLATOR_CRED) {
+       (void) install_session_keyring(NULL);
 
-       if (current->signal->session_keyring) {
+       if (current_session_keyring()) {
            struct key *key;
            key_perm_t perm;
 
            perm = KEY_POS_VIEW | KEY_POS_SEARCH;
            perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
 
-#ifdef KEY_ALLOC_NEEDS_STRUCT_TASK
+#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
 
            if (!IS_ERR(key)) {
                key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
-                                        current->signal->session_keyring, NULL);
+                                        current_session_keyring(), NULL);
                key_put(key);
            }
        }
@@ -241,46 +238,6 @@ setpag(cred_t ** cr, afs_uint32 pagvalue, afs_uint32 * newpag,
 
     return code;
 }
-#else
-int
-setpag(cred_t ** cr, afs_uint32 pagvalue, afs_uint32 * newpag,
-       int change_parent)
-{
-    gid_t *gidset;
-    afs_int32 ngroups, code = 0;
-    int j;
-
-    AFS_STATCNT(setpag);
-
-    gidset = (gid_t *) osi_Alloc(NGROUPS * sizeof(gidset[0]));
-    ngroups = afs_getgroups(*cr, gidset);
-
-    if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
-       /* We will have to shift grouplist to make room for pag */
-       if (ngroups + 2 > NGROUPS) {
-           osi_Free((char *)gidset, NGROUPS * sizeof(int));
-           return EINVAL;
-       }
-       for (j = ngroups - 1; j >= 0; j--) {
-           gidset[j + 2] = gidset[j];
-       }
-       ngroups += 2;
-    }
-    *newpag = (pagvalue == -1 ? genpag() : pagvalue);
-    afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
-    code = afs_setgroups(cr, ngroups, gidset, change_parent);
-
-    /* If change_parent is set, then we should set the pag in the parent as
-     * well.
-     */
-    if (change_parent && !code) {
-       code = set_pag_in_parent(*newpag, gidset[0], gidset[1]);
-    }
-
-    osi_Free((char *)gidset, NGROUPS * sizeof(int));
-    return code;
-}
-#endif
 
 
 /* Intercept the standard system call. */
@@ -307,7 +264,7 @@ afs_xsetgroups(int gidsetsize, gid_t * grouplist)
     cr = crref();
     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
        /* re-install old pag if there's room. */
-       code = setpag(&cr, old_pag, &junk, 0);
+       code = __setpag(&cr, old_pag, &junk, 0);
     }
     crfree(cr);
     unlock_kernel();
@@ -316,7 +273,6 @@ afs_xsetgroups(int gidsetsize, gid_t * grouplist)
     return (-code);
 }
 
-#if defined(AFS_LINUX24_ENV)
 /* Intercept the standard uid32 system call. */
 extern asmlinkage long (*sys_setgroups32p) (int gidsetsize, gid_t * grouplist);
 asmlinkage long
@@ -342,7 +298,7 @@ afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
     cr = crref();
     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
        /* re-install old pag if there's room. */
-       code = setpag(&cr, old_pag, &junk, 0);
+       code = __setpag(&cr, old_pag, &junk, 0);
     }
     crfree(cr);
     unlock_kernel();
@@ -350,7 +306,6 @@ afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
     /* Linux syscall ABI returns errno as negative */
     return (-code);
 }
-#endif
 
 #if defined(AFS_PPC64_LINUX20_ENV)
 /* Intercept the uid16 system call as used by 32bit programs. */
@@ -376,7 +331,7 @@ asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
     cr = crref();
     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
        /* re-install old pag if there's room. */
-       code = setpag(&cr, old_pag, &junk, 0);
+       code = __setpag(&cr, old_pag, &junk, 0);
     }
     crfree(cr);
     unlock_kernel();
@@ -411,7 +366,7 @@ afs32_xsetgroups(int gidsetsize, u16 * grouplist)
     cr = crref();
     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
        /* re-install old pag if there's room. */
-       code = setpag(&cr, old_pag, &junk, 0);
+       code = __setpag(&cr, old_pag, &junk, 0);
     }
     crfree(cr);
     unlock_kernel();
@@ -420,7 +375,6 @@ afs32_xsetgroups(int gidsetsize, u16 * grouplist)
     return (-code);
 }
 
-#ifdef AFS_LINUX24_ENV
 /* Intercept the uid32 system call as used by 32bit programs. */
 extern long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
 asmlinkage long
@@ -445,7 +399,7 @@ afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
     cr = crref();
     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
        /* re-install old pag if there's room. */
-       code = setpag(&cr, old_pag, &junk, 0);
+       code = __setpag(&cr, old_pag, &junk, 0);
     }
     crfree(cr);
     unlock_kernel();
@@ -454,7 +408,6 @@ afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
     return (-code);
 }
 #endif
-#endif
 
 
 #ifdef LINUX_KEYRING_SUPPORT
@@ -469,26 +422,31 @@ 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;
 
     code = -EINVAL;
-    get_group_info(current->group_info);
+    get_group_info(current_group_info());
 
     if (datalen != sizeof(afs_uint32) || !data)
        goto error;
 
-    if (current->group_info->ngroups < 2)
+    if (current_group_info()->ngroups < NUMPAGGROUPS)
        goto error;
 
     /* ensure key being set matches current pag */
-
-    g0 = GROUP_AT(current->group_info, 0);
-    g1 = GROUP_AT(current->group_info, 1);
+#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_get_pag_from_groups(g0, g1);
+#endif
     if (pag == NOPAG)
        goto error;
 
@@ -501,7 +459,7 @@ static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen
     code = 0;
 
 error:
-    put_group_info(current->group_info);
+    put_group_info(current_group_info());
     return code;
 }
 
@@ -510,16 +468,81 @@ static int afs_pag_match(const struct key *key, const void *description)
        return strcmp(key->description, description) == 0;
 }
 
+static void afs_pag_destroy(struct key *key)
+{
+    afs_uint32 pag = key->payload.value;
+    struct unixuser *pu;
+    int locked = ISAFS_GLOCK();
+
+    if (!locked)
+       AFS_GLOCK();
+    pu = afs_FindUser(pag, -1, READ_LOCK);
+    if (pu) {
+       pu->ct.EndTimestamp = 0;
+       pu->tokenTime = 0;
+       afs_PutUser(pu, READ_LOCK);
+    }
+    if (!locked)
+       AFS_GUNLOCK();
+}
+
 struct key_type key_type_afs_pag =
 {
     .name        = "afs_pag",
     .describe    = afs_pag_describe,
     .instantiate = afs_pag_instantiate,
     .match       = afs_pag_match,
+    .destroy     = afs_pag_destroy,
 };
 
+#ifdef EXPORTED_TASKLIST_LOCK
+extern rwlock_t tasklist_lock __attribute__((weak));
+#endif
+
 void osi_keyring_init(void)
 {
+#if !defined(EXPORTED_KEY_TYPE_KEYRING)
+    struct task_struct *p;
+
+    /* If we can't lock the tasklist, either with its explicit lock,
+     * or by using the RCU lock, then we can't safely work out the 
+     * type of a keyring. So, we have to rely on the weak reference. 
+     * If that's not available, then keyring based PAGs won't work.
+     */
+    
+#if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
+    if (__key_type_keyring == NULL) {
+# ifdef EXPORTED_TASKLIST_LOCK
+       if (&tasklist_lock)
+           read_lock(&tasklist_lock);
+# endif
+# if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
+#  ifdef EXPORTED_TASKLIST_LOCK
+       else
+#  endif
+           rcu_read_lock();
+# endif
+#if defined(EXPORTED_FIND_TASK_BY_PID)
+       p = find_task_by_pid(1);
+#else
+       p = find_task_by_vpid(1);
+#endif
+       if (p && task_user(p)->session_keyring)
+           __key_type_keyring = task_user(p)->session_keyring->type;
+# ifdef EXPORTED_TASKLIST_LOCK
+       if (&tasklist_lock)
+           read_unlock(&tasklist_lock);
+# endif
+# if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
+#  ifdef EXPORTED_TASKLIST_LOCK
+       else
+#  endif
+           rcu_read_unlock();
+# endif
+    }
+#endif
+#endif
+
     register_key_type(&key_type_afs_pag);
 }