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/socket.h>
14 #include <linux/sunrpc/clnt.h>
15 #include <linux/sunrpc/auth.h>
16 #include <linux/vserver/xid.h>
18 #define NFS_NGROUPS 16
21 struct rpc_cred uc_base;
24 gid_t uc_gids[NFS_NGROUPS];
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
31 #define UNX_CRED_EXPIRE (60 * HZ)
33 #define UNX_WRITESLACK (21 + (UNX_MAXNODENAME >> 2))
36 # define RPCDBG_FACILITY RPCDBG_AUTH
39 static struct rpc_auth unix_auth;
40 static struct rpc_cred_cache unix_cred_cache;
41 static struct rpc_credops unix_credops;
43 static struct rpc_auth *
44 unx_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
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);
53 unx_destroy(struct rpc_auth *auth)
55 dprintk("RPC: destroying UNIX authenticator %p\n", auth);
56 rpcauth_free_credcache(auth);
60 * Lookup AUTH_UNIX creds for current process
62 static struct rpc_cred *
63 unx_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
65 return rpcauth_lookup_credcache(auth, acred, flags);
68 static struct rpc_cred *
69 unx_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
71 struct unx_cred *cred;
74 dprintk("RPC: allocating UNIX cred for uid %d gid %d\n",
75 acred->uid, acred->gid);
77 if (!(cred = (struct unx_cred *) kmalloc(sizeof(*cred), GFP_KERNEL)))
78 return ERR_PTR(-ENOMEM);
80 atomic_set(&cred->uc_count, 1);
81 cred->uc_flags = RPCAUTH_CRED_UPTODATE;
82 if (flags & RPC_TASK_ROOTCREDS) {
85 cred->uc_xid = vx_current_xid();
86 cred->uc_gids[0] = NOGROUP;
88 int groups = acred->group_info->ngroups;
89 if (groups > NFS_NGROUPS)
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);
98 cred->uc_gids[i] = NOGROUP;
100 cred->uc_base.cr_ops = &unix_credops;
102 return (struct rpc_cred *) cred;
106 unx_destroy_cred(struct rpc_cred *cred)
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).
117 unx_match(struct auth_cred *acred, struct rpc_cred *rcred, int taskflags)
119 struct unx_cred *cred = (struct unx_cred *) rcred;
122 if (!(taskflags & RPC_TASK_ROOTCREDS)) {
125 if (cred->uc_uid != acred->uid
126 || cred->uc_gid != acred->gid
127 || cred->uc_xid != acred->xid)
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))
138 return (cred->uc_uid == 0
140 && cred->uc_gids[0] == (gid_t) NOGROUP);
144 * Marshal credentials.
145 * Maybe we should keep a cached credential for performance reasons.
148 unx_marshal(struct rpc_task *task, u32 *p)
150 struct rpc_clnt *clnt = task->tk_client;
151 struct unx_cred *cred = (struct unx_cred *) task->tk_msg.rpc_cred;
155 *p++ = htonl(RPC_AUTH_UNIX);
157 *p++ = htonl(jiffies/HZ);
160 * Copy the UTS nodename captured when the client was created.
162 p = xdr_encode_array(p, clnt->cl_nodename, clnt->cl_nodelen);
163 tagxid = task->tk_client->cl_tagxid;
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));
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 */
175 *p++ = htonl(RPC_AUTH_NULL);
182 * Refresh credentials. This is a no-op for AUTH_UNIX
185 unx_refresh(struct rpc_task *task)
187 task->tk_msg.rpc_cred->cr_flags |= RPCAUTH_CRED_UPTODATE;
192 unx_validate(struct rpc_task *task, u32 *p)
194 rpc_authflavor_t flavor;
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);
206 if (size > RPC_MAX_AUTH_SIZE) {
207 printk("RPC: giant verf size: %u\n", size);
210 task->tk_auth->au_rslack = (size >> 2) + 2;
216 struct rpc_authops authunix_ops = {
217 .owner = THIS_MODULE,
218 .au_flavor = RPC_AUTH_UNIX,
222 .create = unx_create,
223 .destroy = unx_destroy,
224 .lookup_cred = unx_lookup_cred,
225 .crcreate = unx_create_cred,
229 struct rpc_cred_cache unix_cred_cache = {
230 .expire = UNX_CRED_EXPIRE,
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,
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,