Linux: Don't panic when keys aren't found
[openafs.git] / src / afs / LINUX / osi_groups.c
index bf9f8a0..891cf07 100644 (file)
@@ -24,9 +24,7 @@
 #include "afsincludes.h"
 #include "afs/afs_stats.h"     /* statistics */
 #include "afs/nfsclient.h"
-#ifdef AFS_LINUX22_ENV
-#include "h/smp_lock.h"
-#endif
+#include <linux/smp_lock.h>
 
 #ifdef AFS_LINUX26_ONEGROUP_ENV
 #define NUMPAGGROUPS 1
@@ -34,7 +32,6 @@
 #define NUMPAGGROUPS 2
 #endif
 
-#if defined(AFS_LINUX26_ENV)
 static int
 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
 {
@@ -42,9 +39,9 @@ afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
 
     AFS_STATCNT(afs_setgroups);
 
-    old_info = (*cr)->cr_group_info;
+    old_info = afs_cr_group_info(*cr);
     get_group_info(group_info);
-    (*cr)->cr_group_info = group_info;
+    afs_set_cr_group_info(*cr, group_info);
     put_group_info(old_info);
 
     crset(*cr);
@@ -60,105 +57,18 @@ afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
 
     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)
-static struct group_info *
-afs_getgroups(cred_t * cr)
-{
-    AFS_STATCNT(afs_getgroups);
-
-    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)
+static struct group_info *
+afs_getgroups(cred_t * cr)
 {
-    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;
+    get_group_info(afs_cr_group_info(cr));
+    return afs_cr_group_info(cr);
 }
-#endif
 
-#if defined(AFS_LINUX26_ENV)
 int
 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
          int change_parent)
@@ -288,46 +198,6 @@ out:
 }
 #endif /* LINUX_KEYRING_SUPPORT */
 
-#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;
-
-    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
-
-
 int
 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
        int change_parent)
@@ -339,7 +209,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)->cr_rgid != NFSXLATOR_CRED) {
+    if (code == 0 && afs_cr_rgid(*cr) != NFSXLATOR_CRED) {
        (void) install_session_keyring(NULL);
 
        if (current_session_keyring()) {
@@ -403,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
@@ -437,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. */
@@ -507,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
@@ -541,7 +408,6 @@ afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
     return (-code);
 }
 #endif
-#endif
 
 
 #ifdef LINUX_KEYRING_SUPPORT
@@ -685,6 +551,53 @@ 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) {
+
+#if defined(STRUCT_TASK_HAS_CRED)
+       /* If we have a kernel cred, search the passed credentials */
+       if (cred->tgcred->session_keyring) {
+           key_ref_t key_ref;
+
+           key_ref = keyring_search(
+                         make_key_ref(cred->tgcred->session_keyring, 1),
+                         &key_type_afs_pag, "_pag");
+           if (IS_ERR(key_ref))
+               key = ERR_CAST(key_ref);
+           else
+               key = key_ref_to_ptr(key_ref);
+       } else {
+           key = ERR_PTR(-ENOKEY);
+       }
+#else
+       /* Search the keyrings of the current process */
+       key = request_key(&key_type_afs_pag, "_pag", NULL);
+#endif
+       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 defined(STRUCT_TASK_HAS_CRED)
+               if (cred == current_cred() && ((keyring_pag >> 24) & 0xff) == 'A') {
+#else
+               if (((keyring_pag >> 24) & 0xff) == 'A') {
+#endif
+                   if (keyring_pag != afs_get_pag_from_groups(current_group_info()))
+                       __setpag(&cred, keyring_pag, &newpag, 0);
+               }
+           }
+           key_put(key);
+       }
+    }
+    return keyring_pag;
+}
+
 #else
 void osi_keyring_init(void)
 {