backported vs2.1.x fix to irq handling, which caused incorrect scheduler behavior
[linux-2.6.git] / net / sunrpc / auth_unix.c
1 /*
2  * linux/net/sunrpc/auth_unix.c
3  *
4  * UNIX-style authentication; no AUTH_SHORT support
5  *
6  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #include <linux/types.h>
10 #include <linux/sched.h>
11 #include <linux/module.h>
12 #include <linux/sunrpc/clnt.h>
13 #include <linux/sunrpc/auth.h>
14 #include <linux/vs_base.h>
15 #include <linux/vserver/xid.h>
16
17 #define NFS_NGROUPS     16
18
19 struct unx_cred {
20         struct rpc_cred         uc_base;
21         gid_t                   uc_gid;
22         xid_t                   uc_xid;
23         gid_t                   uc_gids[NFS_NGROUPS];
24 };
25 #define uc_uid                  uc_base.cr_uid
26 #define uc_count                uc_base.cr_count
27 #define uc_flags                uc_base.cr_flags
28 #define uc_expire               uc_base.cr_expire
29
30 #define UNX_CRED_EXPIRE         (60 * HZ)
31
32 #define UNX_WRITESLACK          (21 + (UNX_MAXNODENAME >> 2))
33
34 #ifdef RPC_DEBUG
35 # define RPCDBG_FACILITY        RPCDBG_AUTH
36 #endif
37
38 static struct rpc_auth          unix_auth;
39 static struct rpc_cred_cache    unix_cred_cache;
40 static struct rpc_credops       unix_credops;
41
42 static struct rpc_auth *
43 unx_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
44 {
45         dprintk("RPC: creating UNIX authenticator for client %p\n", clnt);
46         if (atomic_inc_return(&unix_auth.au_count) == 0)
47                 unix_cred_cache.nextgc = jiffies + (unix_cred_cache.expire >> 1);
48         return &unix_auth;
49 }
50
51 static void
52 unx_destroy(struct rpc_auth *auth)
53 {
54         dprintk("RPC: destroying UNIX authenticator %p\n", auth);
55         rpcauth_free_credcache(auth);
56 }
57
58 /*
59  * Lookup AUTH_UNIX creds for current process
60  */
61 static struct rpc_cred *
62 unx_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
63 {
64         return rpcauth_lookup_credcache(auth, acred, flags);
65 }
66
67 static struct rpc_cred *
68 unx_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
69 {
70         struct unx_cred *cred;
71         int             i;
72
73         dprintk("RPC:      allocating UNIX cred for uid %d gid %d\n",
74                                 acred->uid, acred->gid);
75
76         if (!(cred = kmalloc(sizeof(*cred), GFP_KERNEL)))
77                 return ERR_PTR(-ENOMEM);
78
79         atomic_set(&cred->uc_count, 1);
80         cred->uc_flags = RPCAUTH_CRED_UPTODATE;
81         if (flags & RPCAUTH_LOOKUP_ROOTCREDS) {
82                 cred->uc_uid = 0;
83                 cred->uc_gid = 0;
84                 cred->uc_xid = vx_current_xid();
85                 cred->uc_gids[0] = NOGROUP;
86         } else {
87                 int groups = acred->group_info->ngroups;
88                 if (groups > NFS_NGROUPS)
89                         groups = NFS_NGROUPS;
90
91                 cred->uc_uid = acred->uid;
92                 cred->uc_gid = acred->gid;
93                 cred->uc_xid = acred->xid;
94                 for (i = 0; i < groups; i++)
95                         cred->uc_gids[i] = GROUP_AT(acred->group_info, i);
96                 if (i < NFS_NGROUPS)
97                   cred->uc_gids[i] = NOGROUP;
98         }
99         cred->uc_base.cr_ops = &unix_credops;
100
101         return (struct rpc_cred *) cred;
102 }
103
104 static void
105 unx_destroy_cred(struct rpc_cred *cred)
106 {
107         kfree(cred);
108 }
109
110 /*
111  * Match credentials against current process creds.
112  * The root_override argument takes care of cases where the caller may
113  * request root creds (e.g. for NFS swapping).
114  */
115 static int
116 unx_match(struct auth_cred *acred, struct rpc_cred *rcred, int flags)
117 {
118         struct unx_cred *cred = (struct unx_cred *) rcred;
119         int             i;
120
121         if (!(flags & RPCAUTH_LOOKUP_ROOTCREDS)) {
122                 int groups;
123
124                 if (cred->uc_uid != acred->uid
125                  || cred->uc_gid != acred->gid
126                  || cred->uc_xid != acred->xid)
127                         return 0;
128
129                 groups = acred->group_info->ngroups;
130                 if (groups > NFS_NGROUPS)
131                         groups = NFS_NGROUPS;
132                 for (i = 0; i < groups ; i++)
133                         if (cred->uc_gids[i] != GROUP_AT(acred->group_info, i))
134                                 return 0;
135                 return 1;
136         }
137         return (cred->uc_uid == 0
138              && cred->uc_gid == 0
139              && cred->uc_gids[0] == (gid_t) NOGROUP);
140 }
141
142 /*
143  * Marshal credentials.
144  * Maybe we should keep a cached credential for performance reasons.
145  */
146 static u32 *
147 unx_marshal(struct rpc_task *task, u32 *p)
148 {
149         struct rpc_clnt *clnt = task->tk_client;
150         struct unx_cred *cred = (struct unx_cred *) task->tk_msg.rpc_cred;
151         u32             *base, *hold;
152         int             i, tagxid;
153
154         *p++ = htonl(RPC_AUTH_UNIX);
155         base = p++;
156         *p++ = htonl(jiffies/HZ);
157
158         /*
159          * Copy the UTS nodename captured when the client was created.
160          */
161         p = xdr_encode_array(p, clnt->cl_nodename, clnt->cl_nodelen);
162         tagxid = task->tk_client->cl_tagxid;
163
164         *p++ = htonl((u32) XIDINO_UID(tagxid,
165                 cred->uc_uid, cred->uc_xid));
166         *p++ = htonl((u32) XIDINO_GID(tagxid,
167                 cred->uc_gid, cred->uc_xid));
168         hold = p++;
169         for (i = 0; i < 16 && cred->uc_gids[i] != (gid_t) NOGROUP; i++)
170                 *p++ = htonl((u32) cred->uc_gids[i]);
171         *hold = htonl(p - hold - 1);            /* gid array length */
172         *base = htonl((p - base - 1) << 2);     /* cred length */
173
174         *p++ = htonl(RPC_AUTH_NULL);
175         *p++ = htonl(0);
176
177         return p;
178 }
179
180 /*
181  * Refresh credentials. This is a no-op for AUTH_UNIX
182  */
183 static int
184 unx_refresh(struct rpc_task *task)
185 {
186         task->tk_msg.rpc_cred->cr_flags |= RPCAUTH_CRED_UPTODATE;
187         return 0;
188 }
189
190 static u32 *
191 unx_validate(struct rpc_task *task, u32 *p)
192 {
193         rpc_authflavor_t        flavor;
194         u32                     size;
195
196         flavor = ntohl(*p++);
197         if (flavor != RPC_AUTH_NULL &&
198             flavor != RPC_AUTH_UNIX &&
199             flavor != RPC_AUTH_SHORT) {
200                 printk("RPC: bad verf flavor: %u\n", flavor);
201                 return NULL;
202         }
203
204         size = ntohl(*p++);
205         if (size > RPC_MAX_AUTH_SIZE) {
206                 printk("RPC: giant verf size: %u\n", size);
207                 return NULL;
208         }
209         task->tk_auth->au_rslack = (size >> 2) + 2;
210         p += (size >> 2);
211
212         return p;
213 }
214
215 struct rpc_authops      authunix_ops = {
216         .owner          = THIS_MODULE,
217         .au_flavor      = RPC_AUTH_UNIX,
218 #ifdef RPC_DEBUG
219         .au_name        = "UNIX",
220 #endif
221         .create         = unx_create,
222         .destroy        = unx_destroy,
223         .lookup_cred    = unx_lookup_cred,
224         .crcreate       = unx_create_cred,
225 };
226
227 static
228 struct rpc_cred_cache   unix_cred_cache = {
229         .expire         = UNX_CRED_EXPIRE,
230 };
231
232 static
233 struct rpc_auth         unix_auth = {
234         .au_cslack      = UNX_WRITESLACK,
235         .au_rslack      = 2,                    /* assume AUTH_NULL verf */
236         .au_ops         = &authunix_ops,
237         .au_flavor      = RPC_AUTH_UNIX,
238         .au_count       = ATOMIC_INIT(0),
239         .au_credcache   = &unix_cred_cache,
240 };
241
242 static
243 struct rpc_credops      unix_credops = {
244         .cr_name        = "AUTH_UNIX",
245         .crdestroy      = unx_destroy_cred,
246         .crmatch        = unx_match,
247         .crmarshal      = unx_marshal,
248         .crrefresh      = unx_refresh,
249         .crvalidate     = unx_validate,
250 };