blob: 6815157bd65cfa97dd216fdf42b4f86ea473665e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/types.h>
2#include <linux/sched.h>
3#include <linux/module.h>
4#include <linux/sunrpc/types.h>
5#include <linux/sunrpc/xdr.h>
6#include <linux/sunrpc/svcsock.h>
7#include <linux/sunrpc/svcauth.h>
Andy Adamsonc4170582007-07-17 04:04:42 -07008#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/err.h>
10#include <linux/seq_file.h>
11#include <linux/hash.h>
Paulo Marques543537b2005-06-23 00:09:02 -070012#include <linux/string.h>
Greg Banks7b2b1fe2006-10-04 02:15:50 -070013#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#define RPCDBG_FACILITY RPCDBG_AUTH
16
17
18/*
19 * AUTHUNIX and AUTHNULL credentials are both handled here.
20 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
21 * are always nobody (-2). i.e. we do the same IP address checks for
22 * AUTHNULL as for AUTHUNIX, and that is done here.
23 */
24
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026struct unix_domain {
27 struct auth_domain h;
28 int addr_changes;
29 /* other stuff later */
30};
31
NeilBrownefc36aa2006-03-27 01:14:59 -080032extern struct auth_ops svcauth_unix;
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034struct auth_domain *unix_domain_find(char *name)
35{
NeilBrownefc36aa2006-03-27 01:14:59 -080036 struct auth_domain *rv;
37 struct unix_domain *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
NeilBrownefc36aa2006-03-27 01:14:59 -080039 rv = auth_domain_lookup(name, NULL);
40 while(1) {
NeilBrownad1b5222006-03-27 01:15:11 -080041 if (rv) {
42 if (new && rv != &new->h)
43 auth_domain_put(&new->h);
44
45 if (rv->flavour != &svcauth_unix) {
46 auth_domain_put(rv);
47 return NULL;
48 }
NeilBrownefc36aa2006-03-27 01:14:59 -080049 return rv;
50 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
NeilBrownefc36aa2006-03-27 01:14:59 -080052 new = kmalloc(sizeof(*new), GFP_KERNEL);
53 if (new == NULL)
54 return NULL;
55 kref_init(&new->h.ref);
56 new->h.name = kstrdup(name, GFP_KERNEL);
NeilBrowndd08d6e2006-12-13 00:35:44 -080057 if (new->h.name == NULL) {
58 kfree(new);
59 return NULL;
60 }
NeilBrownefc36aa2006-03-27 01:14:59 -080061 new->h.flavour = &svcauth_unix;
62 new->addr_changes = 0;
63 rv = auth_domain_lookup(name, &new->h);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67static void svcauth_unix_domain_release(struct auth_domain *dom)
68{
69 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
70
71 kfree(dom->name);
72 kfree(ud);
73}
74
75
76/**************************************************
77 * cache for IP address to unix_domain
78 * as needed by AUTH_UNIX
79 */
80#define IP_HASHBITS 8
81#define IP_HASHMAX (1<<IP_HASHBITS)
82#define IP_HASHMASK (IP_HASHMAX-1)
83
84struct ip_map {
85 struct cache_head h;
86 char m_class[8]; /* e.g. "nfsd" */
87 struct in_addr m_addr;
88 struct unix_domain *m_client;
89 int m_add_change;
90};
91static struct cache_head *ip_table[IP_HASHMAX];
92
NeilBrownbaab9352006-03-27 01:15:09 -080093static void ip_map_put(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
NeilBrownbaab9352006-03-27 01:15:09 -080095 struct cache_head *item = container_of(kref, struct cache_head, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 struct ip_map *im = container_of(item, struct ip_map,h);
NeilBrownbaab9352006-03-27 01:15:09 -080097
98 if (test_bit(CACHE_VALID, &item->flags) &&
99 !test_bit(CACHE_NEGATIVE, &item->flags))
100 auth_domain_put(&im->m_client->h);
101 kfree(im);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
NeilBrown1f1e0302006-01-06 00:09:49 -0800104#if IP_HASHBITS == 8
105/* hash_long on a 64 bit machine is currently REALLY BAD for
106 * IP addresses in reverse-endian (i.e. on a little-endian machine).
107 * So use a trivial but reliable hash instead
108 */
Al Viro48061262006-11-08 00:22:34 -0800109static inline int hash_ip(__be32 ip)
NeilBrown1f1e0302006-01-06 00:09:49 -0800110{
Al Viro48061262006-11-08 00:22:34 -0800111 int hash = (__force u32)ip ^ ((__force u32)ip>>16);
NeilBrown1f1e0302006-01-06 00:09:49 -0800112 return (hash ^ (hash>>8)) & 0xff;
113}
114#endif
NeilBrown1a9917c2006-03-27 01:15:02 -0800115static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
116{
117 struct ip_map *orig = container_of(corig, struct ip_map, h);
118 struct ip_map *new = container_of(cnew, struct ip_map, h);
119 return strcmp(orig->m_class, new->m_class) == 0
120 && orig->m_addr.s_addr == new->m_addr.s_addr;
121}
122static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
123{
124 struct ip_map *new = container_of(cnew, struct ip_map, h);
125 struct ip_map *item = container_of(citem, struct ip_map, h);
NeilBrown1f1e0302006-01-06 00:09:49 -0800126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 strcpy(new->m_class, item->m_class);
128 new->m_addr.s_addr = item->m_addr.s_addr;
129}
NeilBrown1a9917c2006-03-27 01:15:02 -0800130static void update(struct cache_head *cnew, struct cache_head *citem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
NeilBrown1a9917c2006-03-27 01:15:02 -0800132 struct ip_map *new = container_of(cnew, struct ip_map, h);
133 struct ip_map *item = container_of(citem, struct ip_map, h);
134
NeilBrownefc36aa2006-03-27 01:14:59 -0800135 kref_get(&item->m_client->h.ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 new->m_client = item->m_client;
137 new->m_add_change = item->m_add_change;
138}
NeilBrown1a9917c2006-03-27 01:15:02 -0800139static struct cache_head *ip_map_alloc(void)
140{
141 struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
142 if (i)
143 return &i->h;
144 else
145 return NULL;
146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148static void ip_map_request(struct cache_detail *cd,
149 struct cache_head *h,
150 char **bpp, int *blen)
151{
152 char text_addr[20];
153 struct ip_map *im = container_of(h, struct ip_map, h);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700154 __be32 addr = im->m_addr.s_addr;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 snprintf(text_addr, 20, "%u.%u.%u.%u",
157 ntohl(addr) >> 24 & 0xff,
158 ntohl(addr) >> 16 & 0xff,
159 ntohl(addr) >> 8 & 0xff,
160 ntohl(addr) >> 0 & 0xff);
161
162 qword_add(bpp, blen, im->m_class);
163 qword_add(bpp, blen, text_addr);
164 (*bpp)[-1] = '\n';
165}
166
NeilBrown1a9917c2006-03-27 01:15:02 -0800167static struct ip_map *ip_map_lookup(char *class, struct in_addr addr);
168static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170static int ip_map_parse(struct cache_detail *cd,
171 char *mesg, int mlen)
172{
173 /* class ipaddress [domainname] */
174 /* should be safe just to use the start of the input buffer
175 * for scratch: */
176 char *buf = mesg;
177 int len;
178 int b1,b2,b3,b4;
179 char c;
NeilBrown1a9917c2006-03-27 01:15:02 -0800180 char class[8];
181 struct in_addr addr;
182 int err;
183
184 struct ip_map *ipmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct auth_domain *dom;
186 time_t expiry;
187
188 if (mesg[mlen-1] != '\n')
189 return -EINVAL;
190 mesg[mlen-1] = 0;
191
192 /* class */
NeilBrown1a9917c2006-03-27 01:15:02 -0800193 len = qword_get(&mesg, class, sizeof(class));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (len <= 0) return -EINVAL;
195
196 /* ip address */
197 len = qword_get(&mesg, buf, mlen);
198 if (len <= 0) return -EINVAL;
199
200 if (sscanf(buf, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
201 return -EINVAL;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 expiry = get_expiry(&mesg);
204 if (expiry ==0)
205 return -EINVAL;
206
207 /* domainname, or empty for NEGATIVE */
208 len = qword_get(&mesg, buf, mlen);
209 if (len < 0) return -EINVAL;
210
211 if (len) {
212 dom = unix_domain_find(buf);
213 if (dom == NULL)
214 return -ENOENT;
215 } else
216 dom = NULL;
217
NeilBrown1a9917c2006-03-27 01:15:02 -0800218 addr.s_addr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
NeilBrown1a9917c2006-03-27 01:15:02 -0800221 ipmp = ip_map_lookup(class,addr);
222 if (ipmp) {
223 err = ip_map_update(ipmp,
224 container_of(dom, struct unix_domain, h),
225 expiry);
226 } else
227 err = -ENOMEM;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (dom)
230 auth_domain_put(dom);
NeilBrown1a9917c2006-03-27 01:15:02 -0800231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 cache_flush();
NeilBrown1a9917c2006-03-27 01:15:02 -0800233 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236static int ip_map_show(struct seq_file *m,
237 struct cache_detail *cd,
238 struct cache_head *h)
239{
240 struct ip_map *im;
241 struct in_addr addr;
242 char *dom = "-no-domain-";
243
244 if (h == NULL) {
245 seq_puts(m, "#class IP domain\n");
246 return 0;
247 }
248 im = container_of(h, struct ip_map, h);
249 /* class addr domain */
250 addr = im->m_addr;
251
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800252 if (test_bit(CACHE_VALID, &h->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 !test_bit(CACHE_NEGATIVE, &h->flags))
254 dom = im->m_client->h.name;
255
256 seq_printf(m, "%s %d.%d.%d.%d %s\n",
257 im->m_class,
Al Viro753ed902006-09-26 22:30:23 -0700258 ntohl(addr.s_addr) >> 24 & 0xff,
259 ntohl(addr.s_addr) >> 16 & 0xff,
260 ntohl(addr.s_addr) >> 8 & 0xff,
261 ntohl(addr.s_addr) >> 0 & 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 dom
263 );
264 return 0;
265}
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268struct cache_detail ip_map_cache = {
Bruce Allanf35279d2005-09-06 15:17:08 -0700269 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .hash_size = IP_HASHMAX,
271 .hash_table = ip_table,
272 .name = "auth.unix.ip",
273 .cache_put = ip_map_put,
274 .cache_request = ip_map_request,
275 .cache_parse = ip_map_parse,
276 .cache_show = ip_map_show,
NeilBrown1a9917c2006-03-27 01:15:02 -0800277 .match = ip_map_match,
278 .init = ip_map_init,
279 .update = update,
280 .alloc = ip_map_alloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281};
282
NeilBrown1a9917c2006-03-27 01:15:02 -0800283static struct ip_map *ip_map_lookup(char *class, struct in_addr addr)
284{
285 struct ip_map ip;
286 struct cache_head *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
NeilBrown1a9917c2006-03-27 01:15:02 -0800288 strcpy(ip.m_class, class);
289 ip.m_addr = addr;
290 ch = sunrpc_cache_lookup(&ip_map_cache, &ip.h,
291 hash_str(class, IP_HASHBITS) ^
Al Viro48061262006-11-08 00:22:34 -0800292 hash_ip(addr.s_addr));
NeilBrown1a9917c2006-03-27 01:15:02 -0800293
294 if (ch)
295 return container_of(ch, struct ip_map, h);
296 else
297 return NULL;
298}
299
300static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry)
301{
302 struct ip_map ip;
303 struct cache_head *ch;
304
305 ip.m_client = udom;
306 ip.h.flags = 0;
307 if (!udom)
308 set_bit(CACHE_NEGATIVE, &ip.h.flags);
309 else {
310 ip.m_add_change = udom->addr_changes;
311 /* if this is from the legacy set_client system call,
312 * we need m_add_change to be one higher
313 */
314 if (expiry == NEVER)
315 ip.m_add_change++;
316 }
317 ip.h.expiry_time = expiry;
318 ch = sunrpc_cache_update(&ip_map_cache,
319 &ip.h, &ipm->h,
320 hash_str(ipm->m_class, IP_HASHBITS) ^
Al Viro48061262006-11-08 00:22:34 -0800321 hash_ip(ipm->m_addr.s_addr));
NeilBrown1a9917c2006-03-27 01:15:02 -0800322 if (!ch)
323 return -ENOMEM;
NeilBrownbaab9352006-03-27 01:15:09 -0800324 cache_put(ch, &ip_map_cache);
NeilBrown1a9917c2006-03-27 01:15:02 -0800325 return 0;
326}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
329{
330 struct unix_domain *udom;
NeilBrown1a9917c2006-03-27 01:15:02 -0800331 struct ip_map *ipmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
NeilBrownefc36aa2006-03-27 01:14:59 -0800333 if (dom->flavour != &svcauth_unix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return -EINVAL;
335 udom = container_of(dom, struct unix_domain, h);
NeilBrown1a9917c2006-03-27 01:15:02 -0800336 ipmp = ip_map_lookup("nfsd", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
NeilBrown1a9917c2006-03-27 01:15:02 -0800338 if (ipmp)
339 return ip_map_update(ipmp, udom, NEVER);
340 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -ENOMEM;
342}
343
344int auth_unix_forget_old(struct auth_domain *dom)
345{
346 struct unix_domain *udom;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800347
NeilBrownefc36aa2006-03-27 01:14:59 -0800348 if (dom->flavour != &svcauth_unix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return -EINVAL;
350 udom = container_of(dom, struct unix_domain, h);
351 udom->addr_changes++;
352 return 0;
353}
354
355struct auth_domain *auth_unix_lookup(struct in_addr addr)
356{
Greg Banks40f10522006-10-02 02:17:43 -0700357 struct ip_map *ipm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct auth_domain *rv;
359
NeilBrown1a9917c2006-03-27 01:15:02 -0800360 ipm = ip_map_lookup("nfsd", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 if (!ipm)
363 return NULL;
364 if (cache_check(&ip_map_cache, &ipm->h, NULL))
365 return NULL;
366
367 if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
368 if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
369 auth_domain_put(&ipm->m_client->h);
370 rv = NULL;
371 } else {
372 rv = &ipm->m_client->h;
NeilBrownefc36aa2006-03-27 01:14:59 -0800373 kref_get(&rv->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
NeilBrownbaab9352006-03-27 01:15:09 -0800375 cache_put(&ipm->h, &ip_map_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return rv;
377}
378
379void svcauth_unix_purge(void)
380{
381 cache_purge(&ip_map_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700384static inline struct ip_map *
385ip_map_cached_get(struct svc_rqst *rqstp)
386{
Tom Tuckerdef13d72007-12-30 21:08:08 -0600387 struct ip_map *ipm = NULL;
388 struct svc_xprt *xprt = rqstp->rq_xprt;
389
390 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
391 spin_lock(&xprt->xpt_lock);
392 ipm = xprt->xpt_auth_cache;
393 if (ipm != NULL) {
394 if (!cache_valid(&ipm->h)) {
395 /*
396 * The entry has been invalidated since it was
397 * remembered, e.g. by a second mount from the
398 * same IP address.
399 */
400 xprt->xpt_auth_cache = NULL;
401 spin_unlock(&xprt->xpt_lock);
402 cache_put(&ipm->h, &ip_map_cache);
403 return NULL;
404 }
405 cache_get(&ipm->h);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700406 }
Tom Tuckerdef13d72007-12-30 21:08:08 -0600407 spin_unlock(&xprt->xpt_lock);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700408 }
409 return ipm;
410}
411
412static inline void
413ip_map_cached_put(struct svc_rqst *rqstp, struct ip_map *ipm)
414{
Tom Tuckerdef13d72007-12-30 21:08:08 -0600415 struct svc_xprt *xprt = rqstp->rq_xprt;
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700416
Tom Tuckerdef13d72007-12-30 21:08:08 -0600417 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
418 spin_lock(&xprt->xpt_lock);
419 if (xprt->xpt_auth_cache == NULL) {
420 /* newly cached, keep the reference */
421 xprt->xpt_auth_cache = ipm;
422 ipm = NULL;
423 }
424 spin_unlock(&xprt->xpt_lock);
NeilBrown30f3dee2007-04-16 22:53:25 -0700425 }
NeilBrown30f3dee2007-04-16 22:53:25 -0700426 if (ipm)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700427 cache_put(&ipm->h, &ip_map_cache);
428}
429
430void
431svcauth_unix_info_release(void *info)
432{
433 struct ip_map *ipm = info;
434 cache_put(&ipm->h, &ip_map_cache);
435}
436
NeilBrown3fc605a2007-02-14 00:33:13 -0800437/****************************************************************************
438 * auth.unix.gid cache
439 * simple cache to map a UID to a list of GIDs
440 * because AUTH_UNIX aka AUTH_SYS has a max of 16
441 */
442#define GID_HASHBITS 8
443#define GID_HASHMAX (1<<GID_HASHBITS)
444#define GID_HASHMASK (GID_HASHMAX - 1)
445
446struct unix_gid {
447 struct cache_head h;
448 uid_t uid;
449 struct group_info *gi;
450};
451static struct cache_head *gid_table[GID_HASHMAX];
452
453static void unix_gid_put(struct kref *kref)
454{
455 struct cache_head *item = container_of(kref, struct cache_head, ref);
456 struct unix_gid *ug = container_of(item, struct unix_gid, h);
457 if (test_bit(CACHE_VALID, &item->flags) &&
458 !test_bit(CACHE_NEGATIVE, &item->flags))
459 put_group_info(ug->gi);
460 kfree(ug);
461}
462
463static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
464{
465 struct unix_gid *orig = container_of(corig, struct unix_gid, h);
466 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
467 return orig->uid == new->uid;
468}
469static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
470{
471 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
472 struct unix_gid *item = container_of(citem, struct unix_gid, h);
473 new->uid = item->uid;
474}
475static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
476{
477 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
478 struct unix_gid *item = container_of(citem, struct unix_gid, h);
479
480 get_group_info(item->gi);
481 new->gi = item->gi;
482}
483static struct cache_head *unix_gid_alloc(void)
484{
485 struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
486 if (g)
487 return &g->h;
488 else
489 return NULL;
490}
491
492static void unix_gid_request(struct cache_detail *cd,
493 struct cache_head *h,
494 char **bpp, int *blen)
495{
496 char tuid[20];
497 struct unix_gid *ug = container_of(h, struct unix_gid, h);
498
499 snprintf(tuid, 20, "%u", ug->uid);
500 qword_add(bpp, blen, tuid);
501 (*bpp)[-1] = '\n';
502}
503
504static struct unix_gid *unix_gid_lookup(uid_t uid);
505extern struct cache_detail unix_gid_cache;
506
507static int unix_gid_parse(struct cache_detail *cd,
508 char *mesg, int mlen)
509{
510 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
511 int uid;
512 int gids;
513 int rv;
514 int i;
515 int err;
516 time_t expiry;
517 struct unix_gid ug, *ugp;
518
519 if (mlen <= 0 || mesg[mlen-1] != '\n')
520 return -EINVAL;
521 mesg[mlen-1] = 0;
522
523 rv = get_int(&mesg, &uid);
524 if (rv)
525 return -EINVAL;
526 ug.uid = uid;
527
528 expiry = get_expiry(&mesg);
529 if (expiry == 0)
530 return -EINVAL;
531
532 rv = get_int(&mesg, &gids);
533 if (rv || gids < 0 || gids > 8192)
534 return -EINVAL;
535
536 ug.gi = groups_alloc(gids);
537 if (!ug.gi)
538 return -ENOMEM;
539
540 for (i = 0 ; i < gids ; i++) {
541 int gid;
542 rv = get_int(&mesg, &gid);
543 err = -EINVAL;
544 if (rv)
545 goto out;
546 GROUP_AT(ug.gi, i) = gid;
547 }
548
549 ugp = unix_gid_lookup(uid);
550 if (ugp) {
551 struct cache_head *ch;
552 ug.h.flags = 0;
553 ug.h.expiry_time = expiry;
554 ch = sunrpc_cache_update(&unix_gid_cache,
555 &ug.h, &ugp->h,
556 hash_long(uid, GID_HASHBITS));
557 if (!ch)
558 err = -ENOMEM;
559 else {
560 err = 0;
561 cache_put(ch, &unix_gid_cache);
562 }
563 } else
564 err = -ENOMEM;
565 out:
566 if (ug.gi)
567 put_group_info(ug.gi);
568 return err;
569}
570
571static int unix_gid_show(struct seq_file *m,
572 struct cache_detail *cd,
573 struct cache_head *h)
574{
575 struct unix_gid *ug;
576 int i;
577 int glen;
578
579 if (h == NULL) {
580 seq_puts(m, "#uid cnt: gids...\n");
581 return 0;
582 }
583 ug = container_of(h, struct unix_gid, h);
584 if (test_bit(CACHE_VALID, &h->flags) &&
585 !test_bit(CACHE_NEGATIVE, &h->flags))
586 glen = ug->gi->ngroups;
587 else
588 glen = 0;
589
590 seq_printf(m, "%d %d:", ug->uid, glen);
591 for (i = 0; i < glen; i++)
592 seq_printf(m, " %d", GROUP_AT(ug->gi, i));
593 seq_printf(m, "\n");
594 return 0;
595}
596
597struct cache_detail unix_gid_cache = {
598 .owner = THIS_MODULE,
599 .hash_size = GID_HASHMAX,
600 .hash_table = gid_table,
601 .name = "auth.unix.gid",
602 .cache_put = unix_gid_put,
603 .cache_request = unix_gid_request,
604 .cache_parse = unix_gid_parse,
605 .cache_show = unix_gid_show,
606 .match = unix_gid_match,
607 .init = unix_gid_init,
608 .update = unix_gid_update,
609 .alloc = unix_gid_alloc,
610};
611
612static struct unix_gid *unix_gid_lookup(uid_t uid)
613{
614 struct unix_gid ug;
615 struct cache_head *ch;
616
617 ug.uid = uid;
618 ch = sunrpc_cache_lookup(&unix_gid_cache, &ug.h,
619 hash_long(uid, GID_HASHBITS));
620 if (ch)
621 return container_of(ch, struct unix_gid, h);
622 else
623 return NULL;
624}
625
626static int unix_gid_find(uid_t uid, struct group_info **gip,
627 struct svc_rqst *rqstp)
628{
629 struct unix_gid *ug = unix_gid_lookup(uid);
630 if (!ug)
631 return -EAGAIN;
632 switch (cache_check(&unix_gid_cache, &ug->h, &rqstp->rq_chandle)) {
633 case -ENOENT:
634 *gip = NULL;
635 return 0;
636 case 0:
637 *gip = ug->gi;
638 get_group_info(*gip);
639 return 0;
640 default:
641 return -EAGAIN;
642 }
643}
644
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700645int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646svcauth_unix_set_client(struct svc_rqst *rqstp)
647{
Chuck Lever27459f02007-02-12 00:53:34 -0800648 struct sockaddr_in *sin = svc_addr_in(rqstp);
NeilBrown1a9917c2006-03-27 01:15:02 -0800649 struct ip_map *ipm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 rqstp->rq_client = NULL;
652 if (rqstp->rq_proc == 0)
653 return SVC_OK;
654
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700655 ipm = ip_map_cached_get(rqstp);
656 if (ipm == NULL)
657 ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
Chuck Lever27459f02007-02-12 00:53:34 -0800658 sin->sin_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (ipm == NULL)
661 return SVC_DENIED;
662
663 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
664 default:
665 BUG();
666 case -EAGAIN:
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800667 case -ETIMEDOUT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return SVC_DROP;
669 case -ENOENT:
670 return SVC_DENIED;
671 case 0:
672 rqstp->rq_client = &ipm->m_client->h;
NeilBrownefc36aa2006-03-27 01:14:59 -0800673 kref_get(&rqstp->rq_client->ref);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700674 ip_map_cached_put(rqstp, ipm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 break;
676 }
677 return SVC_OK;
678}
679
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700680EXPORT_SYMBOL(svcauth_unix_set_client);
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682static int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700683svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 struct kvec *argv = &rqstp->rq_arg.head[0];
686 struct kvec *resv = &rqstp->rq_res.head[0];
687 struct svc_cred *cred = &rqstp->rq_cred;
688
689 cred->cr_group_info = NULL;
690 rqstp->rq_client = NULL;
691
692 if (argv->iov_len < 3*4)
693 return SVC_GARBAGE;
694
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800695 if (svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 dprintk("svc: bad null cred\n");
697 *authp = rpc_autherr_badcred;
698 return SVC_DENIED;
699 }
Alexey Dobriyan76994312006-09-26 22:28:46 -0700700 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 dprintk("svc: bad null verf\n");
702 *authp = rpc_autherr_badverf;
703 return SVC_DENIED;
704 }
705
706 /* Signal that mapping to nobody uid/gid is required */
707 cred->cr_uid = (uid_t) -1;
708 cred->cr_gid = (gid_t) -1;
709 cred->cr_group_info = groups_alloc(0);
710 if (cred->cr_group_info == NULL)
711 return SVC_DROP; /* kmalloc failure - client must retry */
712
713 /* Put NULL verifier */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700714 svc_putnl(resv, RPC_AUTH_NULL);
715 svc_putnl(resv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Andy Adamsonc4170582007-07-17 04:04:42 -0700717 rqstp->rq_flavor = RPC_AUTH_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return SVC_OK;
719}
720
721static int
722svcauth_null_release(struct svc_rqst *rqstp)
723{
724 if (rqstp->rq_client)
725 auth_domain_put(rqstp->rq_client);
726 rqstp->rq_client = NULL;
727 if (rqstp->rq_cred.cr_group_info)
728 put_group_info(rqstp->rq_cred.cr_group_info);
729 rqstp->rq_cred.cr_group_info = NULL;
730
731 return 0; /* don't drop */
732}
733
734
735struct auth_ops svcauth_null = {
736 .name = "null",
737 .owner = THIS_MODULE,
738 .flavour = RPC_AUTH_NULL,
739 .accept = svcauth_null_accept,
740 .release = svcauth_null_release,
741 .set_client = svcauth_unix_set_client,
742};
743
744
745static int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700746svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 struct kvec *argv = &rqstp->rq_arg.head[0];
749 struct kvec *resv = &rqstp->rq_res.head[0];
750 struct svc_cred *cred = &rqstp->rq_cred;
751 u32 slen, i;
752 int len = argv->iov_len;
753
754 cred->cr_group_info = NULL;
755 rqstp->rq_client = NULL;
756
757 if ((len -= 3*4) < 0)
758 return SVC_GARBAGE;
759
760 svc_getu32(argv); /* length */
761 svc_getu32(argv); /* time stamp */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700762 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (slen > 64 || (len -= (slen + 3)*4) < 0)
764 goto badcred;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700765 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 argv->iov_len -= slen*4;
767
Alexey Dobriyan76994312006-09-26 22:28:46 -0700768 cred->cr_uid = svc_getnl(argv); /* uid */
769 cred->cr_gid = svc_getnl(argv); /* gid */
770 slen = svc_getnl(argv); /* gids length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (slen > 16 || (len -= (slen + 2)*4) < 0)
772 goto badcred;
NeilBrown3fc605a2007-02-14 00:33:13 -0800773 if (unix_gid_find(cred->cr_uid, &cred->cr_group_info, rqstp)
774 == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return SVC_DROP;
NeilBrown3fc605a2007-02-14 00:33:13 -0800776 if (cred->cr_group_info == NULL) {
777 cred->cr_group_info = groups_alloc(slen);
778 if (cred->cr_group_info == NULL)
779 return SVC_DROP;
780 for (i = 0; i < slen; i++)
781 GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
782 } else {
783 for (i = 0; i < slen ; i++)
784 svc_getnl(argv);
785 }
Alexey Dobriyan76994312006-09-26 22:28:46 -0700786 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 *authp = rpc_autherr_badverf;
788 return SVC_DENIED;
789 }
790
791 /* Put NULL verifier */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700792 svc_putnl(resv, RPC_AUTH_NULL);
793 svc_putnl(resv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Andy Adamsonc4170582007-07-17 04:04:42 -0700795 rqstp->rq_flavor = RPC_AUTH_UNIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return SVC_OK;
797
798badcred:
799 *authp = rpc_autherr_badcred;
800 return SVC_DENIED;
801}
802
803static int
804svcauth_unix_release(struct svc_rqst *rqstp)
805{
806 /* Verifier (such as it is) is already in place.
807 */
808 if (rqstp->rq_client)
809 auth_domain_put(rqstp->rq_client);
810 rqstp->rq_client = NULL;
811 if (rqstp->rq_cred.cr_group_info)
812 put_group_info(rqstp->rq_cred.cr_group_info);
813 rqstp->rq_cred.cr_group_info = NULL;
814
815 return 0;
816}
817
818
819struct auth_ops svcauth_unix = {
820 .name = "unix",
821 .owner = THIS_MODULE,
822 .flavour = RPC_AUTH_UNIX,
823 .accept = svcauth_unix_accept,
824 .release = svcauth_unix_release,
825 .domain_release = svcauth_unix_domain_release,
826 .set_client = svcauth_unix_set_client,
827};
828