ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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
17 #define NFS_NGROUPS     16
18
19 struct unx_cred {
20         struct rpc_cred         uc_base;
21         gid_t                   uc_gid;
22         uid_t                   uc_puid;                /* process uid */
23         gid_t                   uc_pgid;                /* process gid */
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_credops       unix_credops;
40
41 static struct rpc_auth *
42 unx_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
43 {
44         struct rpc_auth *auth;
45
46         dprintk("RPC: creating UNIX authenticator for client %p\n", clnt);
47         if (!(auth = (struct rpc_auth *) kmalloc(sizeof(*auth), GFP_KERNEL)))
48                 return NULL;
49         auth->au_cslack = UNX_WRITESLACK;
50         auth->au_rslack = 2;    /* assume AUTH_NULL verf */
51         auth->au_expire = UNX_CRED_EXPIRE;
52         auth->au_ops = &authunix_ops;
53
54         rpcauth_init_credcache(auth);
55
56         return auth;
57 }
58
59 static void
60 unx_destroy(struct rpc_auth *auth)
61 {
62         dprintk("RPC: destroying UNIX authenticator %p\n", auth);
63         rpcauth_free_credcache(auth);
64 }
65
66 static struct rpc_cred *
67 unx_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
68 {
69         struct unx_cred *cred;
70         int             i;
71
72         dprintk("RPC:      allocating UNIX cred for uid %d gid %d\n",
73                                 acred->uid, acred->gid);
74
75         if (!(cred = (struct unx_cred *) kmalloc(sizeof(*cred), GFP_KERNEL)))
76                 return NULL;
77
78         atomic_set(&cred->uc_count, 0);
79         cred->uc_flags = RPCAUTH_CRED_UPTODATE;
80         if (flags & RPC_TASK_ROOTCREDS) {
81                 cred->uc_uid = cred->uc_puid = 0;
82                 cred->uc_gid = cred->uc_pgid = 0;
83                 cred->uc_gids[0] = NOGROUP;
84         } else {
85                 int groups = acred->group_info->ngroups;
86                 if (groups > NFS_NGROUPS)
87                         groups = NFS_NGROUPS;
88
89                 cred->uc_uid = acred->uid;
90                 cred->uc_gid = acred->gid;
91                 cred->uc_puid = current->uid;
92                 cred->uc_pgid = current->gid;
93                 for (i = 0; i < groups; i++)
94                         cred->uc_gids[i] = GROUP_AT(acred->group_info, i);
95                 if (i < NFS_NGROUPS)
96                   cred->uc_gids[i] = NOGROUP;
97         }
98         cred->uc_base.cr_ops = &unix_credops;
99
100         return (struct rpc_cred *) cred;
101 }
102
103 static void
104 unx_destroy_cred(struct rpc_cred *cred)
105 {
106         kfree(cred);
107 }
108
109 /*
110  * Match credentials against current process creds.
111  * The root_override argument takes care of cases where the caller may
112  * request root creds (e.g. for NFS swapping).
113  */
114 static int
115 unx_match(struct auth_cred *acred, struct rpc_cred *rcred, int taskflags)
116 {
117         struct unx_cred *cred = (struct unx_cred *) rcred;
118         int             i;
119
120         if (!(taskflags & RPC_TASK_ROOTCREDS)) {
121                 int groups;
122
123                 if (cred->uc_uid != acred->uid
124                  || cred->uc_gid != acred->gid
125                  || cred->uc_puid != current->uid
126                  || cred->uc_pgid != current->gid)
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 && cred->uc_puid == 0
138              && cred->uc_gid == 0 && cred->uc_pgid == 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, int ruid)
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;
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
163         /* Note: we don't use real uid if it involves raising privilege */
164         if (ruid && cred->uc_puid != 0 && cred->uc_pgid != 0) {
165                 *p++ = htonl((u32) cred->uc_puid);
166                 *p++ = htonl((u32) cred->uc_pgid);
167         } else {
168                 *p++ = htonl((u32) cred->uc_uid);
169                 *p++ = htonl((u32) cred->uc_gid);
170         }
171         hold = p++;
172         for (i = 0; i < 16 && cred->uc_gids[i] != (gid_t) NOGROUP; i++)
173                 *p++ = htonl((u32) cred->uc_gids[i]);
174         *hold = htonl(p - hold - 1);            /* gid array length */
175         *base = htonl((p - base - 1) << 2);     /* cred length */
176
177         *p++ = htonl(RPC_AUTH_NULL);
178         *p++ = htonl(0);
179
180         return p;
181 }
182
183 /*
184  * Refresh credentials. This is a no-op for AUTH_UNIX
185  */
186 static int
187 unx_refresh(struct rpc_task *task)
188 {
189         task->tk_msg.rpc_cred->cr_flags |= RPCAUTH_CRED_UPTODATE;
190         return task->tk_status = -EACCES;
191 }
192
193 static u32 *
194 unx_validate(struct rpc_task *task, u32 *p)
195 {
196         rpc_authflavor_t        flavor;
197         u32                     size;
198
199         flavor = ntohl(*p++);
200         if (flavor != RPC_AUTH_NULL &&
201             flavor != RPC_AUTH_UNIX &&
202             flavor != RPC_AUTH_SHORT) {
203                 printk("RPC: bad verf flavor: %u\n", flavor);
204                 return NULL;
205         }
206
207         size = ntohl(*p++);
208         if (size > RPC_MAX_AUTH_SIZE) {
209                 printk("RPC: giant verf size: %u\n", size);
210                 return NULL;
211         }
212         task->tk_auth->au_rslack = (size >> 2) + 2;
213         p += (size >> 2);
214
215         return p;
216 }
217
218 struct rpc_authops      authunix_ops = {
219         .owner          = THIS_MODULE,
220         .au_flavor      = RPC_AUTH_UNIX,
221 #ifdef RPC_DEBUG
222         .au_name        = "UNIX",
223 #endif
224         .create         = unx_create,
225         .destroy        = unx_destroy,
226         .crcreate       = unx_create_cred,
227 };
228
229 static
230 struct rpc_credops      unix_credops = {
231         .crdestroy      = unx_destroy_cred,
232         .crmatch        = unx_match,
233         .crmarshal      = unx_marshal,
234         .crrefresh      = unx_refresh,
235         .crvalidate     = unx_validate,
236 };