linux cache file open fail print error
[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 /* Copy one credential structure to another, being careful about references */
22 static inline void
23 afs_copy_creds(cred_t *to_cred, const cred_t *from_cred) {
24     afs_set_cr_uid(to_cred, afs_cr_uid(from_cred));
25     afs_set_cr_gid(to_cred, afs_cr_gid(from_cred));
26     afs_set_cr_ruid(to_cred, afs_cr_ruid(from_cred));
27     afs_set_cr_rgid(to_cred, afs_cr_rgid(from_cred));
28     get_group_info(afs_cr_group_info(from_cred));
29     afs_set_cr_group_info(to_cred, afs_cr_group_info(from_cred));
30 }
31
32 cred_t *
33 crget(void)
34 {
35     cred_t *tmp;
36     
37     tmp = kmalloc(sizeof(cred_t), GFP_NOFS);
38     memset(tmp, 0, sizeof(cred_t));
39     if (!tmp)
40         osi_Panic("crget: No more memory for creds!\n");
41
42 #if defined(STRUCT_TASK_HAS_CRED)
43     get_cred(tmp);
44 #else
45     atomic_set(&tmp->cr_ref, 1);
46 #endif
47     return tmp;
48 }
49
50 void
51 crfree(cred_t * cr)
52 {
53 #if defined(STRUCT_TASK_HAS_CRED)
54     put_cred(cr);
55 #else
56     if (atomic_dec_and_test(&cr->cr_ref)) {
57         put_group_info(afs_cr_group_info(cr));
58         kfree(cr);
59     }
60 #endif
61 }
62
63
64 /* Return a duplicate of the cred. */
65 cred_t *
66 crdup(cred_t * cr)
67 {
68     cred_t *tmp = crget();
69 #if defined(STRUCT_TASK_HAS_CRED)
70     afs_copy_creds(tmp, cr);
71 #else
72     afs_set_cr_uid(tmp, afs_cr_uid(cr));
73     afs_set_cr_ruid(tmp, afs_cr_ruid(cr));
74     afs_set_cr_gid(tmp, afs_cr_gid(cr));
75     afs_set_cr_rgid(tmp, afs_cr_rgid(cr));
76
77     get_group_info(afs_cr_group_info(cr));
78     afs_set_cr_group_info(tmp, afs_cr_group_info(cr));
79 #endif
80     return tmp;
81 }
82
83 cred_t *
84 crref(void)
85 {
86 #if defined(STRUCT_TASK_HAS_CRED)
87     return (cred_t *)get_current_cred();
88 #else
89     cred_t *cr = crget();
90
91     afs_set_cr_uid(cr, current_fsuid());
92     afs_set_cr_ruid(cr, current_uid());
93     afs_set_cr_gid(cr, current_fsgid());
94     afs_set_cr_rgid(cr, current_gid());
95
96     task_lock(current);
97     get_group_info(current_group_info());
98     afs_set_cr_group_info(cr, current_group_info());
99     task_unlock(current);
100
101     return cr;
102 #endif
103 }
104
105 /* Set the cred info into the current task */
106 void
107 crset(cred_t * cr)
108 {
109 #if defined(STRUCT_TASK_HAS_CRED)
110     struct cred *new_creds;
111
112     /* If our current task doesn't have identical real and effective
113      * credentials, commit_cred won't let us change them, so we just
114      * bail here.
115      */
116     if (current->cred != current->real_cred)
117         return;
118     new_creds = prepare_creds();
119     /* Drop the reference to group_info - we'll overwrite it in afs_copy_creds */
120     put_group_info(new_creds->group_info);
121     afs_copy_creds(new_creds, current_cred());
122
123     commit_creds(new_creds);
124 #else
125     struct group_info *old_info;
126
127     current->fsuid = afs_cr_uid(cr);
128     current->uid = afs_cr_ruid(cr);
129     current->fsgid = afs_cr_gid(cr);
130     current->gid = afs_cr_rgid(cr);
131
132     get_group_info(afs_cr_group_info(cr));
133     task_lock(current);
134     old_info = current->group_info;
135     current->group_info = afs_cr_group_info(cr);
136     task_unlock(current);
137     put_group_info(old_info);
138 #endif
139 }