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