Remove pre-Linux 2.6 support
[openafs.git] / src / afs / LINUX / osi_cred.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * osi_cred.c - Linux cred handling routines.
12  *
13  */
14 #include <afsconfig.h>
15 #include "afs/param.h"
16
17
18 #include "afs/sysincludes.h"
19 #include "afsincludes.h"
20
21 cred_t *
22 crget(void)
23 {
24     cred_t *tmp;
25     
26 #if !defined(GFP_NOFS)
27 #define GFP_NOFS GFP_KERNEL
28 #endif
29     tmp = kmalloc(sizeof(cred_t), GFP_NOFS);
30     if (!tmp)
31         osi_Panic("crget: No more memory for creds!\n");
32     
33     tmp->cr_ref = 1;
34     return tmp;
35 }
36
37 void
38 crfree(cred_t * cr)
39 {
40     if (cr->cr_ref > 1) {
41         cr->cr_ref--;
42         return;
43     }
44
45     put_group_info(cr->cr_group_info);
46
47     kfree(cr);
48 }
49
50
51 /* Return a duplicate of the cred. */
52 cred_t *
53 crdup(cred_t * cr)
54 {
55     cred_t *tmp = crget();
56
57     tmp->cr_uid = cr->cr_uid;
58     tmp->cr_ruid = cr->cr_ruid;
59     tmp->cr_gid = cr->cr_gid;
60     tmp->cr_rgid = cr->cr_rgid;
61
62     get_group_info(cr->cr_group_info);
63     tmp->cr_group_info = cr->cr_group_info;
64
65     return tmp;
66 }
67
68 cred_t *
69 crref(void)
70 {
71     cred_t *cr = crget();
72
73     cr->cr_uid = current_fsuid();
74     cr->cr_ruid = current_uid();
75     cr->cr_gid = current_fsgid();
76     cr->cr_rgid = current_gid();
77
78     task_lock(current);
79     get_group_info(current_group_info());
80     cr->cr_group_info = current_group_info();
81     task_unlock(current);
82
83     return cr;
84 }
85
86 /* Set the cred info into the current task */
87 void
88 crset(cred_t * cr)
89 {
90     struct group_info *old_info;
91 #if defined(STRUCT_TASK_HAS_CRED)
92     struct cred *new_creds;
93
94     /* If our current task doesn't have identical real and effective
95      * credentials, commit_cred won't let us change them, so we just
96      * bail here.
97      */
98     if (current->cred != current->real_cred)
99         return;
100     new_creds = prepare_creds();
101     new_creds->fsuid = cr->cr_uid;
102     new_creds->uid = cr->cr_ruid;
103     new_creds->fsgid = cr->cr_gid;
104     new_creds->gid = cr->cr_rgid;
105 #else
106     current->fsuid = cr->cr_uid;
107     current->uid = cr->cr_ruid;
108     current->fsgid = cr->cr_gid;
109     current->gid = cr->cr_rgid;
110 #endif
111
112     /* using set_current_groups() will sort the groups */
113     get_group_info(cr->cr_group_info);
114
115     task_lock(current);
116 #if defined(STRUCT_TASK_HAS_CRED)
117     old_info = current->cred->group_info;
118     new_creds->group_info = cr->cr_group_info;
119     commit_creds(new_creds);
120 #else
121     old_info = current->group_info;
122     current->group_info = cr->cr_group_info;
123 #endif
124     task_unlock(current);
125
126     put_group_info(old_info);
127 }