1 /*
2 * linux/net/sunrpc/rpcauth_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/malloc.h>
11 #include <linux/socket.h>
12 #include <linux/in.h>
13 #include <linux/sunrpc/clnt.h>
14 #include <linux/sunrpc/auth.h>
15
16 #define NFS_NGROUPS 16
17 struct unx_cred {
18 struct rpc_cred uc_base;
19 uid_t uc_fsuid;
20 gid_t uc_gid, uc_fsgid;
21 gid_t uc_gids[NFS_NGROUPS];
22 };
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
27
28 #define UNX_CRED_EXPIRE (60 * HZ)
29
30 #define UNX_WRITESLACK (21 + (UNX_MAXNODENAME >> 2))
31
32 #ifdef RPC_DEBUG
33 # define RPCDBG_FACILITY RPCDBG_AUTH
34 #endif
35
36 static struct rpc_auth *
37 unx_create(struct rpc_clnt *clnt)
38 {
39 struct rpc_auth *auth;
40
41 dprintk("RPC: creating UNIX authenticator for client %p\n", clnt);
42 if (!(auth = (struct rpc_auth *) rpc_allocate(0, sizeof(*auth))))
43 return NULL;
44 auth->au_cslack = UNX_WRITESLACK;
45 auth->au_rslack = 2; /* assume AUTH_NULL verf */
46 auth->au_expire = UNX_CRED_EXPIRE;
47 auth->au_ops = &authunix_ops;
48
49 rpcauth_init_credcache(auth);
50
51 return auth;
52 }
53
54 static void
55 unx_destroy(struct rpc_auth *auth)
56 {
57 dprintk("RPC: destroying UNIX authenticator %p\n", auth);
58 rpcauth_free_credcache(auth);
59 rpc_free(auth);
60 }
61
62 static struct rpc_cred *
63 unx_create_cred(int flags)
64 {
65 struct unx_cred *cred;
66 int i;
67
68 dprintk("RPC: allocating UNIX cred for uid %d gid %d\n",
69 current->uid, current->gid);
70
71 if (!(cred = (struct unx_cred *) rpc_allocate(flags, sizeof(*cred))))
72 return NULL;
73
74 cred->uc_count = 0;
75 cred->uc_flags = RPCAUTH_CRED_UPTODATE;
76 if (flags & RPC_TASK_ROOTCREDS) {
77 cred->uc_uid = cred->uc_fsuid = 0;
78 cred->uc_gid = cred->uc_fsgid = 0;
79 cred->uc_gids[0] = NOGROUP;
80 } else {
81 int groups = current->ngroups;
82 if (groups > NFS_NGROUPS)
83 groups = NFS_NGROUPS;
84
85 cred->uc_uid = current->uid;
86 cred->uc_gid = current->gid;
87 cred->uc_fsuid = current->fsuid;
88 cred->uc_fsgid = current->fsgid;
89 for (i = 0; i < groups; i++)
90 cred->uc_gids[i] = (gid_t) current->groups[i];
91 if (i < NFS_NGROUPS)
92 cred->uc_gids[i] = NOGROUP;
93 }
94
95 return (struct rpc_cred *) cred;
96 }
97
98 struct rpc_cred *
99 authunix_fake_cred(struct rpc_task *task, uid_t uid, gid_t gid)
100 {
101 struct unx_cred *cred;
102
103 dprintk("RPC: allocating fake UNIX cred for uid %d gid %d\n",
104 uid, gid);
105
106 if (!(cred = (struct unx_cred *) rpc_malloc(task, sizeof(*cred))))
107 return NULL;
108
109 cred->uc_count = 1;
110 cred->uc_flags = RPCAUTH_CRED_DEAD|RPCAUTH_CRED_UPTODATE;
111 cred->uc_uid = uid;
112 cred->uc_gid = gid;
113 cred->uc_fsuid = uid;
114 cred->uc_fsgid = gid;
115 cred->uc_gids[0] = (gid_t) NOGROUP;
116
117 return task->tk_msg.rpc_cred = (struct rpc_cred *) cred;
118 }
119
120 static void
121 unx_destroy_cred(struct rpc_cred *cred)
122 {
123 rpc_free(cred);
124 }
125
126 /*
127 * Match credentials against current process creds.
128 * The root_override argument takes care of cases where the caller may
129 * request root creds (e.g. for NFS swapping).
130 */
131 static int
132 unx_match(struct rpc_cred *rcred, int taskflags)
133 {
134 struct unx_cred *cred = (struct unx_cred *) rcred;
135 int i;
136
137 if (!(taskflags & RPC_TASK_ROOTCREDS)) {
138 int groups;
139
140 if (cred->uc_uid != current->uid
141 || cred->uc_gid != current->gid
142 || cred->uc_fsuid != current->fsuid
143 || cred->uc_fsgid != current->fsgid)
144 return 0;
145
146 groups = current->ngroups;
147 if (groups > NFS_NGROUPS)
148 groups = NFS_NGROUPS;
149 for (i = 0; i < groups ; i++)
150 if (cred->uc_gids[i] != (gid_t) current->groups[i])
151 return 0;
152 return 1;
153 }
154 return (cred->uc_uid == 0 && cred->uc_fsuid == 0
155 && cred->uc_gid == 0 && cred->uc_fsgid == 0
156 && cred->uc_gids[0] == (gid_t) NOGROUP);
157 }
158
159 /*
160 * Marshal credentials.
161 * Maybe we should keep a cached credential for performance reasons.
162 */
163 static u32 *
164 unx_marshal(struct rpc_task *task, u32 *p, int ruid)
165 {
166 struct rpc_clnt *clnt = task->tk_client;
167 struct unx_cred *cred = (struct unx_cred *) task->tk_msg.rpc_cred;
168 u32 *base, *hold;
169 int i, n;
170
171 *p++ = htonl(RPC_AUTH_UNIX);
172 base = p++;
173 *p++ = htonl(jiffies/HZ);
174
175 /*
176 * Copy the UTS nodename captured when the client was created.
177 */
178 n = clnt->cl_nodelen;
179 *p++ = htonl(n);
180 memcpy(p, clnt->cl_nodename, n);
181 p += (n + 3) >> 2;
182
183 /* Note: we don't use real uid if it involves raising priviledge */
184 if (ruid && cred->uc_uid != 0 && cred->uc_gid != 0) {
185 *p++ = htonl((u32) cred->uc_uid);
186 *p++ = htonl((u32) cred->uc_gid);
187 } else {
188 *p++ = htonl((u32) cred->uc_fsuid);
189 *p++ = htonl((u32) cred->uc_fsgid);
190 }
191 hold = p++;
192 for (i = 0; i < 16 && cred->uc_gids[i] != (gid_t) NOGROUP; i++)
193 *p++ = htonl((u32) cred->uc_gids[i]);
194 *hold = htonl(p - hold - 1); /* gid array length */
195 *base = htonl((p - base - 1) << 2); /* cred length */
196
197 *p++ = htonl(RPC_AUTH_NULL);
198 *p++ = htonl(0);
199
200 return p;
201 }
202
203 /*
204 * Refresh credentials. This is a no-op for AUTH_UNIX
205 */
206 static int
207 unx_refresh(struct rpc_task *task)
208 {
209 task->tk_msg.rpc_cred->cr_flags |= RPCAUTH_CRED_UPTODATE;
210 return task->tk_status = -EACCES;
211 }
212
213 static u32 *
214 unx_validate(struct rpc_task *task, u32 *p)
215 {
216 u32 n = ntohl(*p++);
217
218 if (n != RPC_AUTH_NULL && n != RPC_AUTH_UNIX && n != RPC_AUTH_SHORT) {
219 printk("RPC: bad verf flavor: %ld\n", (unsigned long) n);
220 return NULL;
221 }
222 if ((n = ntohl(*p++)) > 400) {
223 printk("RPC: giant verf size: %ld\n", (unsigned long) n);
224 return NULL;
225 }
226 task->tk_auth->au_rslack = (n >> 2) + 2;
227 p += (n >> 2);
228
229 return p;
230 }
231
232 struct rpc_authops authunix_ops = {
233 RPC_AUTH_UNIX,
234 #ifdef RPC_DEBUG
235 "UNIX",
236 #endif
237 unx_create,
238 unx_destroy,
239 unx_create_cred,
240 unx_destroy_cred,
241 unx_match,
242 unx_marshal,
243 unx_refresh,
244 unx_validate
245 };
246
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.