2 * linux/net/sunrpc/rpcauth_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/slab.h>
11 #include <linux/socket.h>
13 #include <linux/sunrpc/clnt.h>
14 #include <linux/sunrpc/auth.h>
16 #define NFS_NGROUPS 16
18 struct rpc_cred uc_base;
20 gid_t uc_gid, uc_fsgid;
21 gid_t uc_gids[NFS_NGROUPS];
23 #define uc_uid uc_base.cr_uid
24 #define uc_count uc_base.cr_count
25 #define uc_flags uc_base.cr_flags
26 #define uc_expire uc_base.cr_expire
28 #define UNX_CRED_EXPIRE (60 * HZ)
30 #define UNX_WRITESLACK (21 + (UNX_MAXNODENAME >> 2))
33 # define RPCDBG_FACILITY RPCDBG_AUTH
36 static struct rpc_credops unix_credops;
38 static struct rpc_auth *
39 unx_create(struct rpc_clnt *clnt)
41 struct rpc_auth *auth;
43 dprintk("RPC: creating UNIX authenticator for client %p\n", clnt);
44 if (!(auth = (struct rpc_auth *) rpc_allocate(0, sizeof(*auth))))
46 auth->au_cslack = UNX_WRITESLACK;
47 auth->au_rslack = 2; /* assume AUTH_NULL verf */
48 auth->au_expire = UNX_CRED_EXPIRE;
49 auth->au_ops = &authunix_ops;
51 rpcauth_init_credcache(auth);
57 unx_destroy(struct rpc_auth *auth)
59 dprintk("RPC: destroying UNIX authenticator %p\n", auth);
60 rpcauth_free_credcache(auth);
64 static struct rpc_cred *
65 unx_create_cred(int flags)
67 struct unx_cred *cred;
70 dprintk("RPC: allocating UNIX cred for uid %d gid %d\n",
71 current->uid, current->gid);
73 if (!(cred = (struct unx_cred *) rpc_allocate(flags, sizeof(*cred))))
76 atomic_set(&cred->uc_count, 0);
77 cred->uc_flags = RPCAUTH_CRED_UPTODATE;
78 if (flags & RPC_TASK_ROOTCREDS) {
79 cred->uc_uid = cred->uc_fsuid = 0;
80 cred->uc_gid = cred->uc_fsgid = 0;
81 cred->uc_gids[0] = NOGROUP;
83 int groups = current->ngroups;
84 if (groups > NFS_NGROUPS)
87 cred->uc_uid = current->uid;
88 cred->uc_gid = current->gid;
89 cred->uc_fsuid = current->fsuid;
90 cred->uc_fsgid = current->fsgid;
91 for (i = 0; i < groups; i++)
92 cred->uc_gids[i] = (gid_t) current->groups[i];
94 cred->uc_gids[i] = NOGROUP;
96 cred->uc_base.cr_ops = &unix_credops;
98 return (struct rpc_cred *) cred;
102 authunix_fake_cred(struct rpc_task *task, uid_t uid, gid_t gid)
104 struct unx_cred *cred;
106 dprintk("RPC: allocating fake UNIX cred for uid %d gid %d\n",
109 if (!(cred = (struct unx_cred *) rpc_malloc(task, sizeof(*cred))))
112 atomic_set(&cred->uc_count, 1);
113 cred->uc_flags = RPCAUTH_CRED_DEAD|RPCAUTH_CRED_UPTODATE;
116 cred->uc_fsuid = uid;
117 cred->uc_fsgid = gid;
118 cred->uc_gids[0] = (gid_t) NOGROUP;
120 return task->tk_msg.rpc_cred = (struct rpc_cred *) cred;
124 unx_destroy_cred(struct rpc_cred *cred)
130 * Match credentials against current process creds.
131 * The root_override argument takes care of cases where the caller may
132 * request root creds (e.g. for NFS swapping).
135 unx_match(struct rpc_cred *rcred, int taskflags)
137 struct unx_cred *cred = (struct unx_cred *) rcred;
140 if (!(taskflags & RPC_TASK_ROOTCREDS)) {
143 if (cred->uc_uid != current->uid
144 || cred->uc_gid != current->gid
145 || cred->uc_fsuid != current->fsuid
146 || cred->uc_fsgid != current->fsgid)
149 groups = current->ngroups;
150 if (groups > NFS_NGROUPS)
151 groups = NFS_NGROUPS;
152 for (i = 0; i < groups ; i++)
153 if (cred->uc_gids[i] != (gid_t) current->groups[i])
157 return (cred->uc_uid == 0 && cred->uc_fsuid == 0
158 && cred->uc_gid == 0 && cred->uc_fsgid == 0
159 && cred->uc_gids[0] == (gid_t) NOGROUP);
163 * Marshal credentials.
164 * Maybe we should keep a cached credential for performance reasons.
167 unx_marshal(struct rpc_task *task, u32 *p, int ruid)
169 struct rpc_clnt *clnt = task->tk_client;
170 struct unx_cred *cred = (struct unx_cred *) task->tk_msg.rpc_cred;
174 *p++ = htonl(RPC_AUTH_UNIX);
176 *p++ = htonl(jiffies/HZ);
179 * Copy the UTS nodename captured when the client was created.
181 n = clnt->cl_nodelen;
183 memcpy(p, clnt->cl_nodename, n);
186 /* Note: we don't use real uid if it involves raising priviledge */
187 if (ruid && cred->uc_uid != 0 && cred->uc_gid != 0) {
188 *p++ = htonl((u32) cred->uc_uid);
189 *p++ = htonl((u32) cred->uc_gid);
191 *p++ = htonl((u32) cred->uc_fsuid);
192 *p++ = htonl((u32) cred->uc_fsgid);
195 for (i = 0; i < 16 && cred->uc_gids[i] != (gid_t) NOGROUP; i++)
196 *p++ = htonl((u32) cred->uc_gids[i]);
197 *hold = htonl(p - hold - 1); /* gid array length */
198 *base = htonl((p - base - 1) << 2); /* cred length */
200 *p++ = htonl(RPC_AUTH_NULL);
207 * Refresh credentials. This is a no-op for AUTH_UNIX
210 unx_refresh(struct rpc_task *task)
212 task->tk_msg.rpc_cred->cr_flags |= RPCAUTH_CRED_UPTODATE;
213 return task->tk_status = -EACCES;
217 unx_validate(struct rpc_task *task, u32 *p)
221 if (n != RPC_AUTH_NULL && n != RPC_AUTH_UNIX && n != RPC_AUTH_SHORT) {
222 printk("RPC: bad verf flavor: %ld\n", (unsigned long) n);
225 if ((n = ntohl(*p++)) > 400) {
226 printk("RPC: giant verf size: %ld\n", (unsigned long) n);
229 task->tk_auth->au_rslack = (n >> 2) + 2;
235 struct rpc_authops authunix_ops = {
246 struct rpc_credops unix_credops = {