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