2 * linux/net/sunrpc/auth_unix.c
4 * UNIX-style authentication; no AUTH_SHORT support
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
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>
17 #define NFS_NGROUPS 16
20 struct rpc_cred uc_base;
23 gid_t uc_gids[NFS_NGROUPS];
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
30 #define UNX_CRED_EXPIRE (60 * HZ)
32 #define UNX_WRITESLACK (21 + (UNX_MAXNODENAME >> 2))
35 # define RPCDBG_FACILITY RPCDBG_AUTH
38 static struct rpc_auth unix_auth;
39 static struct rpc_cred_cache unix_cred_cache;
40 static struct rpc_credops unix_credops;
42 static struct rpc_auth *
43 unx_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
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);
52 unx_destroy(struct rpc_auth *auth)
54 dprintk("RPC: destroying UNIX authenticator %p\n", auth);
55 rpcauth_free_credcache(auth);
59 * Lookup AUTH_UNIX creds for current process
61 static struct rpc_cred *
62 unx_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
64 return rpcauth_lookup_credcache(auth, acred, flags);
67 static struct rpc_cred *
68 unx_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
70 struct unx_cred *cred;
73 dprintk("RPC: allocating UNIX cred for uid %d gid %d\n",
74 acred->uid, acred->gid);
76 if (!(cred = kmalloc(sizeof(*cred), GFP_KERNEL)))
77 return ERR_PTR(-ENOMEM);
79 atomic_set(&cred->uc_count, 1);
80 cred->uc_flags = RPCAUTH_CRED_UPTODATE;
81 if (flags & RPCAUTH_LOOKUP_ROOTCREDS) {
84 cred->uc_xid = vx_current_xid();
85 cred->uc_gids[0] = NOGROUP;
87 int groups = acred->group_info->ngroups;
88 if (groups > NFS_NGROUPS)
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);
97 cred->uc_gids[i] = NOGROUP;
99 cred->uc_base.cr_ops = &unix_credops;
101 return (struct rpc_cred *) cred;
105 unx_destroy_cred(struct rpc_cred *cred)
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).
116 unx_match(struct auth_cred *acred, struct rpc_cred *rcred, int flags)
118 struct unx_cred *cred = (struct unx_cred *) rcred;
121 if (!(flags & RPCAUTH_LOOKUP_ROOTCREDS)) {
124 if (cred->uc_uid != acred->uid
125 || cred->uc_gid != acred->gid
126 || cred->uc_xid != acred->xid)
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))
137 return (cred->uc_uid == 0
139 && cred->uc_gids[0] == (gid_t) NOGROUP);
143 * Marshal credentials.
144 * Maybe we should keep a cached credential for performance reasons.
147 unx_marshal(struct rpc_task *task, u32 *p)
149 struct rpc_clnt *clnt = task->tk_client;
150 struct unx_cred *cred = (struct unx_cred *) task->tk_msg.rpc_cred;
154 *p++ = htonl(RPC_AUTH_UNIX);
156 *p++ = htonl(jiffies/HZ);
159 * Copy the UTS nodename captured when the client was created.
161 p = xdr_encode_array(p, clnt->cl_nodename, clnt->cl_nodelen);
162 tagxid = task->tk_client->cl_tagxid;
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));
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 */
174 *p++ = htonl(RPC_AUTH_NULL);
181 * Refresh credentials. This is a no-op for AUTH_UNIX
184 unx_refresh(struct rpc_task *task)
186 task->tk_msg.rpc_cred->cr_flags |= RPCAUTH_CRED_UPTODATE;
191 unx_validate(struct rpc_task *task, u32 *p)
193 rpc_authflavor_t flavor;
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);
205 if (size > RPC_MAX_AUTH_SIZE) {
206 printk("RPC: giant verf size: %u\n", size);
209 task->tk_auth->au_rslack = (size >> 2) + 2;
215 struct rpc_authops authunix_ops = {
216 .owner = THIS_MODULE,
217 .au_flavor = RPC_AUTH_UNIX,
221 .create = unx_create,
222 .destroy = unx_destroy,
223 .lookup_cred = unx_lookup_cred,
224 .crcreate = unx_create_cred,
228 struct rpc_cred_cache unix_cred_cache = {
229 .expire = UNX_CRED_EXPIRE,
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_count = ATOMIC_INIT(0),
238 .au_credcache = &unix_cred_cache,
242 struct rpc_credops unix_credops = {
243 .cr_name = "AUTH_UNIX",
244 .crdestroy = unx_destroy_cred,
245 .crmatch = unx_match,
246 .crmarshal = unx_marshal,
247 .crrefresh = unx_refresh,
248 .crvalidate = unx_validate,