linux-2624-rc5-updates-20080110
[openafs.git] / src / afs / LINUX / osi_groups.c
index 1409624..184a3e3 100644 (file)
@@ -17,6 +17,9 @@
 #include "afs/param.h"
 #ifdef LINUX_KEYRING_SUPPORT
 #include <linux/seq_file.h>
+#if KEY_TYPE_H_EXISTS
+#include <linux/key-type.h>
+#endif
 #endif
 
 RCSID
@@ -25,10 +28,17 @@ RCSID
 #include "afs/sysincludes.h"
 #include "afsincludes.h"
 #include "afs/afs_stats.h"     /* statistics */
+#include "afs/nfsclient.h"
 #ifdef AFS_LINUX22_ENV
 #include "h/smp_lock.h"
 #endif
 
+#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)
@@ -160,26 +170,53 @@ __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
     gid_t g0, g1;
     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 < 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);
 
@@ -189,16 +226,60 @@ __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
 }
 
 #ifdef LINUX_KEYRING_SUPPORT
-#include <asm/unistd.h>
-#include <linux/keyctl.h>
-
-static int errno;
-static inline _syscall2(long, keyctl, int, option, void*, arg2);
+static struct key_type *__key_type_keyring;
 
-static long
-__join_session_keyring(char *name)
+static int
+install_session_keyring(struct task_struct *task, 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", task->tgid);
+
+#ifdef KEY_ALLOC_NEEDS_STRUCT_TASK
+       keyring = key_alloc(__key_type_keyring, desc,
+                           task->uid, task->gid, task,
+                           (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
+                           not_in_quota);
+#else
+       keyring = key_alloc(__key_type_keyring, desc,
+                           task->uid, task->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(&task->sighand->siglock);
+    old = task->signal->session_keyring;
+    smp_wmb();
+    task->signal->session_keyring = keyring;
+    spin_unlock_irq(&task->sighand->siglock);
+
+    if (old)
+           key_put(old);
+
+out:
+    return code;
 }
 #endif /* LINUX_KEYRING_SUPPORT */
 
@@ -253,9 +334,8 @@ setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
     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(current, NULL);
 
        if (current->signal->session_keyring) {
            struct key *key;
@@ -480,15 +560,18 @@ 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 < 2)
+    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_get_pag_from_groups(g0, g1);
+#endif
     if (pag == NOPAG)
        goto error;
 
@@ -510,16 +593,64 @@ 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)
 {
+    struct task_struct *p;
+#ifdef EXPORTED_TASKLIST_LOCK
+    if (&tasklist_lock)
+      read_lock(&tasklist_lock);
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
+#ifdef EXPORTED_TASKLIST_LOCK
+    else
+#endif
+      rcu_read_lock();
+#endif
+    p = find_task_by_pid(1);
+    if (p && p->user->session_keyring)
+       __key_type_keyring = p->user->session_keyring->type;
+#ifdef EXPORTED_TASKLIST_LOCK
+    if (&tasklist_lock)
+       read_unlock(&tasklist_lock);
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
+#ifdef EXPORTED_TASKLIST_LOCK
+    else
+#endif
+      rcu_read_unlock();
+#endif
+
     register_key_type(&key_type_afs_pag);
 }