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