blob: 16f714a247bc5e52609b023b618546a83c9dc89f [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>
Aurélien Charbonf15364b2008-01-18 15:50:56 +010014#include <net/ipv6.h>
15#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#define RPCDBG_FACILITY RPCDBG_AUTH
17
18
19/*
20 * AUTHUNIX and AUTHNULL credentials are both handled here.
21 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
22 * are always nobody (-2). i.e. we do the same IP address checks for
23 * AUTHNULL as for AUTHUNIX, and that is done here.
24 */
25
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027struct unix_domain {
28 struct auth_domain h;
29 int addr_changes;
30 /* other stuff later */
31};
32
NeilBrownefc36aa2006-03-27 01:14:59 -080033extern struct auth_ops svcauth_unix;
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035struct auth_domain *unix_domain_find(char *name)
36{
NeilBrownefc36aa2006-03-27 01:14:59 -080037 struct auth_domain *rv;
38 struct unix_domain *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
NeilBrownefc36aa2006-03-27 01:14:59 -080040 rv = auth_domain_lookup(name, NULL);
41 while(1) {
NeilBrownad1b5222006-03-27 01:15:11 -080042 if (rv) {
43 if (new && rv != &new->h)
44 auth_domain_put(&new->h);
45
46 if (rv->flavour != &svcauth_unix) {
47 auth_domain_put(rv);
48 return NULL;
49 }
NeilBrownefc36aa2006-03-27 01:14:59 -080050 return rv;
51 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
NeilBrownefc36aa2006-03-27 01:14:59 -080053 new = kmalloc(sizeof(*new), GFP_KERNEL);
54 if (new == NULL)
55 return NULL;
56 kref_init(&new->h.ref);
57 new->h.name = kstrdup(name, GFP_KERNEL);
NeilBrowndd08d6e2006-12-13 00:35:44 -080058 if (new->h.name == NULL) {
59 kfree(new);
60 return NULL;
61 }
NeilBrownefc36aa2006-03-27 01:14:59 -080062 new->h.flavour = &svcauth_unix;
63 new->addr_changes = 0;
64 rv = auth_domain_lookup(name, &new->h);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
Trond Myklebustd2f7e792007-07-14 15:39:58 -040067EXPORT_SYMBOL(unix_domain_find);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69static void svcauth_unix_domain_release(struct auth_domain *dom)
70{
71 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
72
73 kfree(dom->name);
74 kfree(ud);
75}
76
77
78/**************************************************
79 * cache for IP address to unix_domain
80 * as needed by AUTH_UNIX
81 */
82#define IP_HASHBITS 8
83#define IP_HASHMAX (1<<IP_HASHBITS)
84#define IP_HASHMASK (IP_HASHMAX-1)
85
86struct ip_map {
87 struct cache_head h;
88 char m_class[8]; /* e.g. "nfsd" */
Aurélien Charbonf15364b2008-01-18 15:50:56 +010089 struct in6_addr m_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 struct unix_domain *m_client;
91 int m_add_change;
92};
93static struct cache_head *ip_table[IP_HASHMAX];
94
NeilBrownbaab9352006-03-27 01:15:09 -080095static void ip_map_put(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
NeilBrownbaab9352006-03-27 01:15:09 -080097 struct cache_head *item = container_of(kref, struct cache_head, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct ip_map *im = container_of(item, struct ip_map,h);
NeilBrownbaab9352006-03-27 01:15:09 -080099
100 if (test_bit(CACHE_VALID, &item->flags) &&
101 !test_bit(CACHE_NEGATIVE, &item->flags))
102 auth_domain_put(&im->m_client->h);
103 kfree(im);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
NeilBrown1f1e0302006-01-06 00:09:49 -0800106#if IP_HASHBITS == 8
107/* hash_long on a 64 bit machine is currently REALLY BAD for
108 * IP addresses in reverse-endian (i.e. on a little-endian machine).
109 * So use a trivial but reliable hash instead
110 */
Al Viro48061262006-11-08 00:22:34 -0800111static inline int hash_ip(__be32 ip)
NeilBrown1f1e0302006-01-06 00:09:49 -0800112{
Al Viro48061262006-11-08 00:22:34 -0800113 int hash = (__force u32)ip ^ ((__force u32)ip>>16);
NeilBrown1f1e0302006-01-06 00:09:49 -0800114 return (hash ^ (hash>>8)) & 0xff;
115}
116#endif
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100117static inline int hash_ip6(struct in6_addr ip)
118{
119 return (hash_ip(ip.s6_addr32[0]) ^
120 hash_ip(ip.s6_addr32[1]) ^
121 hash_ip(ip.s6_addr32[2]) ^
122 hash_ip(ip.s6_addr32[3]));
123}
NeilBrown1a9917c2006-03-27 01:15:02 -0800124static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
125{
126 struct ip_map *orig = container_of(corig, struct ip_map, h);
127 struct ip_map *new = container_of(cnew, struct ip_map, h);
128 return strcmp(orig->m_class, new->m_class) == 0
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100129 && ipv6_addr_equal(&orig->m_addr, &new->m_addr);
NeilBrown1a9917c2006-03-27 01:15:02 -0800130}
131static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
132{
133 struct ip_map *new = container_of(cnew, struct ip_map, h);
134 struct ip_map *item = container_of(citem, struct ip_map, h);
NeilBrown1f1e0302006-01-06 00:09:49 -0800135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 strcpy(new->m_class, item->m_class);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100137 ipv6_addr_copy(&new->m_addr, &item->m_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
NeilBrown1a9917c2006-03-27 01:15:02 -0800139static void update(struct cache_head *cnew, struct cache_head *citem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
NeilBrown1a9917c2006-03-27 01:15:02 -0800141 struct ip_map *new = container_of(cnew, struct ip_map, h);
142 struct ip_map *item = container_of(citem, struct ip_map, h);
143
NeilBrownefc36aa2006-03-27 01:14:59 -0800144 kref_get(&item->m_client->h.ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 new->m_client = item->m_client;
146 new->m_add_change = item->m_add_change;
147}
NeilBrown1a9917c2006-03-27 01:15:02 -0800148static struct cache_head *ip_map_alloc(void)
149{
150 struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
151 if (i)
152 return &i->h;
153 else
154 return NULL;
155}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157static void ip_map_request(struct cache_detail *cd,
158 struct cache_head *h,
159 char **bpp, int *blen)
160{
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100161 char text_addr[40];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct ip_map *im = container_of(h, struct ip_map, h);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800163
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100164 if (ipv6_addr_v4mapped(&(im->m_addr))) {
165 snprintf(text_addr, 20, NIPQUAD_FMT,
166 ntohl(im->m_addr.s6_addr32[3]) >> 24 & 0xff,
167 ntohl(im->m_addr.s6_addr32[3]) >> 16 & 0xff,
168 ntohl(im->m_addr.s6_addr32[3]) >> 8 & 0xff,
169 ntohl(im->m_addr.s6_addr32[3]) >> 0 & 0xff);
170 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700171 snprintf(text_addr, 40, "%pI6", &im->m_addr);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 qword_add(bpp, blen, im->m_class);
174 qword_add(bpp, blen, text_addr);
175 (*bpp)[-1] = '\n';
176}
177
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100178static struct ip_map *ip_map_lookup(char *class, struct in6_addr *addr);
NeilBrown1a9917c2006-03-27 01:15:02 -0800179static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181static int ip_map_parse(struct cache_detail *cd,
182 char *mesg, int mlen)
183{
184 /* class ipaddress [domainname] */
185 /* should be safe just to use the start of the input buffer
186 * for scratch: */
187 char *buf = mesg;
188 int len;
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100189 int b1, b2, b3, b4, b5, b6, b7, b8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 char c;
NeilBrown1a9917c2006-03-27 01:15:02 -0800191 char class[8];
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100192 struct in6_addr addr;
NeilBrown1a9917c2006-03-27 01:15:02 -0800193 int err;
194
195 struct ip_map *ipmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct auth_domain *dom;
197 time_t expiry;
198
199 if (mesg[mlen-1] != '\n')
200 return -EINVAL;
201 mesg[mlen-1] = 0;
202
203 /* class */
NeilBrown1a9917c2006-03-27 01:15:02 -0800204 len = qword_get(&mesg, class, sizeof(class));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (len <= 0) return -EINVAL;
206
207 /* ip address */
208 len = qword_get(&mesg, buf, mlen);
209 if (len <= 0) return -EINVAL;
210
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100211 if (sscanf(buf, NIPQUAD_FMT "%c", &b1, &b2, &b3, &b4, &c) == 4) {
212 addr.s6_addr32[0] = 0;
213 addr.s6_addr32[1] = 0;
214 addr.s6_addr32[2] = htonl(0xffff);
215 addr.s6_addr32[3] =
216 htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
Harvey Harrisonb189db52008-10-28 22:38:52 -0700217 } else if (sscanf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x%c",
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100218 &b1, &b2, &b3, &b4, &b5, &b6, &b7, &b8, &c) == 8) {
219 addr.s6_addr16[0] = htons(b1);
220 addr.s6_addr16[1] = htons(b2);
221 addr.s6_addr16[2] = htons(b3);
222 addr.s6_addr16[3] = htons(b4);
223 addr.s6_addr16[4] = htons(b5);
224 addr.s6_addr16[5] = htons(b6);
225 addr.s6_addr16[6] = htons(b7);
226 addr.s6_addr16[7] = htons(b8);
227 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -EINVAL;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 expiry = get_expiry(&mesg);
231 if (expiry ==0)
232 return -EINVAL;
233
234 /* domainname, or empty for NEGATIVE */
235 len = qword_get(&mesg, buf, mlen);
236 if (len < 0) return -EINVAL;
237
238 if (len) {
239 dom = unix_domain_find(buf);
240 if (dom == NULL)
241 return -ENOENT;
242 } else
243 dom = NULL;
244
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100245 ipmp = ip_map_lookup(class, &addr);
NeilBrown1a9917c2006-03-27 01:15:02 -0800246 if (ipmp) {
247 err = ip_map_update(ipmp,
248 container_of(dom, struct unix_domain, h),
249 expiry);
250 } else
251 err = -ENOMEM;
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (dom)
254 auth_domain_put(dom);
NeilBrown1a9917c2006-03-27 01:15:02 -0800255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 cache_flush();
NeilBrown1a9917c2006-03-27 01:15:02 -0800257 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260static int ip_map_show(struct seq_file *m,
261 struct cache_detail *cd,
262 struct cache_head *h)
263{
264 struct ip_map *im;
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100265 struct in6_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 char *dom = "-no-domain-";
267
268 if (h == NULL) {
269 seq_puts(m, "#class IP domain\n");
270 return 0;
271 }
272 im = container_of(h, struct ip_map, h);
273 /* class addr domain */
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100274 ipv6_addr_copy(&addr, &im->m_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800276 if (test_bit(CACHE_VALID, &h->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 !test_bit(CACHE_NEGATIVE, &h->flags))
278 dom = im->m_client->h.name;
279
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100280 if (ipv6_addr_v4mapped(&addr)) {
J. Bruce Fieldsd71a4dd2008-05-09 12:01:19 -0700281 seq_printf(m, "%s " NIPQUAD_FMT " %s\n",
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100282 im->m_class,
283 ntohl(addr.s6_addr32[3]) >> 24 & 0xff,
284 ntohl(addr.s6_addr32[3]) >> 16 & 0xff,
285 ntohl(addr.s6_addr32[3]) >> 8 & 0xff,
286 ntohl(addr.s6_addr32[3]) >> 0 & 0xff,
287 dom);
288 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700289 seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return 0;
292}
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295struct cache_detail ip_map_cache = {
Bruce Allanf35279d2005-09-06 15:17:08 -0700296 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 .hash_size = IP_HASHMAX,
298 .hash_table = ip_table,
299 .name = "auth.unix.ip",
300 .cache_put = ip_map_put,
301 .cache_request = ip_map_request,
302 .cache_parse = ip_map_parse,
303 .cache_show = ip_map_show,
NeilBrown1a9917c2006-03-27 01:15:02 -0800304 .match = ip_map_match,
305 .init = ip_map_init,
306 .update = update,
307 .alloc = ip_map_alloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308};
309
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100310static struct ip_map *ip_map_lookup(char *class, struct in6_addr *addr)
NeilBrown1a9917c2006-03-27 01:15:02 -0800311{
312 struct ip_map ip;
313 struct cache_head *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
NeilBrown1a9917c2006-03-27 01:15:02 -0800315 strcpy(ip.m_class, class);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100316 ipv6_addr_copy(&ip.m_addr, addr);
NeilBrown1a9917c2006-03-27 01:15:02 -0800317 ch = sunrpc_cache_lookup(&ip_map_cache, &ip.h,
318 hash_str(class, IP_HASHBITS) ^
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100319 hash_ip6(*addr));
NeilBrown1a9917c2006-03-27 01:15:02 -0800320
321 if (ch)
322 return container_of(ch, struct ip_map, h);
323 else
324 return NULL;
325}
326
327static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry)
328{
329 struct ip_map ip;
330 struct cache_head *ch;
331
332 ip.m_client = udom;
333 ip.h.flags = 0;
334 if (!udom)
335 set_bit(CACHE_NEGATIVE, &ip.h.flags);
336 else {
337 ip.m_add_change = udom->addr_changes;
338 /* if this is from the legacy set_client system call,
339 * we need m_add_change to be one higher
340 */
341 if (expiry == NEVER)
342 ip.m_add_change++;
343 }
344 ip.h.expiry_time = expiry;
345 ch = sunrpc_cache_update(&ip_map_cache,
346 &ip.h, &ipm->h,
347 hash_str(ipm->m_class, IP_HASHBITS) ^
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100348 hash_ip6(ipm->m_addr));
NeilBrown1a9917c2006-03-27 01:15:02 -0800349 if (!ch)
350 return -ENOMEM;
NeilBrownbaab9352006-03-27 01:15:09 -0800351 cache_put(ch, &ip_map_cache);
NeilBrown1a9917c2006-03-27 01:15:02 -0800352 return 0;
353}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100355int auth_unix_add_addr(struct in6_addr *addr, struct auth_domain *dom)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct unix_domain *udom;
NeilBrown1a9917c2006-03-27 01:15:02 -0800358 struct ip_map *ipmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
NeilBrownefc36aa2006-03-27 01:14:59 -0800360 if (dom->flavour != &svcauth_unix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return -EINVAL;
362 udom = container_of(dom, struct unix_domain, h);
NeilBrown1a9917c2006-03-27 01:15:02 -0800363 ipmp = ip_map_lookup("nfsd", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
NeilBrown1a9917c2006-03-27 01:15:02 -0800365 if (ipmp)
366 return ip_map_update(ipmp, udom, NEVER);
367 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return -ENOMEM;
369}
Trond Myklebustd2f7e792007-07-14 15:39:58 -0400370EXPORT_SYMBOL(auth_unix_add_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372int auth_unix_forget_old(struct auth_domain *dom)
373{
374 struct unix_domain *udom;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800375
NeilBrownefc36aa2006-03-27 01:14:59 -0800376 if (dom->flavour != &svcauth_unix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return -EINVAL;
378 udom = container_of(dom, struct unix_domain, h);
379 udom->addr_changes++;
380 return 0;
381}
Trond Myklebustd2f7e792007-07-14 15:39:58 -0400382EXPORT_SYMBOL(auth_unix_forget_old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100384struct auth_domain *auth_unix_lookup(struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Greg Banks40f10522006-10-02 02:17:43 -0700386 struct ip_map *ipm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct auth_domain *rv;
388
NeilBrown1a9917c2006-03-27 01:15:02 -0800389 ipm = ip_map_lookup("nfsd", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (!ipm)
392 return NULL;
393 if (cache_check(&ip_map_cache, &ipm->h, NULL))
394 return NULL;
395
396 if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
397 if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
398 auth_domain_put(&ipm->m_client->h);
399 rv = NULL;
400 } else {
401 rv = &ipm->m_client->h;
NeilBrownefc36aa2006-03-27 01:14:59 -0800402 kref_get(&rv->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
NeilBrownbaab9352006-03-27 01:15:09 -0800404 cache_put(&ipm->h, &ip_map_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return rv;
406}
Trond Myklebustd2f7e792007-07-14 15:39:58 -0400407EXPORT_SYMBOL(auth_unix_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409void svcauth_unix_purge(void)
410{
411 cache_purge(&ip_map_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
Trond Myklebustd2f7e792007-07-14 15:39:58 -0400413EXPORT_SYMBOL(svcauth_unix_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700415static inline struct ip_map *
416ip_map_cached_get(struct svc_rqst *rqstp)
417{
Tom Tuckerdef13d72007-12-30 21:08:08 -0600418 struct ip_map *ipm = NULL;
419 struct svc_xprt *xprt = rqstp->rq_xprt;
420
421 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
422 spin_lock(&xprt->xpt_lock);
423 ipm = xprt->xpt_auth_cache;
424 if (ipm != NULL) {
425 if (!cache_valid(&ipm->h)) {
426 /*
427 * The entry has been invalidated since it was
428 * remembered, e.g. by a second mount from the
429 * same IP address.
430 */
431 xprt->xpt_auth_cache = NULL;
432 spin_unlock(&xprt->xpt_lock);
433 cache_put(&ipm->h, &ip_map_cache);
434 return NULL;
435 }
436 cache_get(&ipm->h);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700437 }
Tom Tuckerdef13d72007-12-30 21:08:08 -0600438 spin_unlock(&xprt->xpt_lock);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700439 }
440 return ipm;
441}
442
443static inline void
444ip_map_cached_put(struct svc_rqst *rqstp, struct ip_map *ipm)
445{
Tom Tuckerdef13d72007-12-30 21:08:08 -0600446 struct svc_xprt *xprt = rqstp->rq_xprt;
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700447
Tom Tuckerdef13d72007-12-30 21:08:08 -0600448 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
449 spin_lock(&xprt->xpt_lock);
450 if (xprt->xpt_auth_cache == NULL) {
451 /* newly cached, keep the reference */
452 xprt->xpt_auth_cache = ipm;
453 ipm = NULL;
454 }
455 spin_unlock(&xprt->xpt_lock);
NeilBrown30f3dee2007-04-16 22:53:25 -0700456 }
NeilBrown30f3dee2007-04-16 22:53:25 -0700457 if (ipm)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700458 cache_put(&ipm->h, &ip_map_cache);
459}
460
461void
462svcauth_unix_info_release(void *info)
463{
464 struct ip_map *ipm = info;
465 cache_put(&ipm->h, &ip_map_cache);
466}
467
NeilBrown3fc605a2007-02-14 00:33:13 -0800468/****************************************************************************
469 * auth.unix.gid cache
470 * simple cache to map a UID to a list of GIDs
471 * because AUTH_UNIX aka AUTH_SYS has a max of 16
472 */
473#define GID_HASHBITS 8
474#define GID_HASHMAX (1<<GID_HASHBITS)
475#define GID_HASHMASK (GID_HASHMAX - 1)
476
477struct unix_gid {
478 struct cache_head h;
479 uid_t uid;
480 struct group_info *gi;
481};
482static struct cache_head *gid_table[GID_HASHMAX];
483
484static void unix_gid_put(struct kref *kref)
485{
486 struct cache_head *item = container_of(kref, struct cache_head, ref);
487 struct unix_gid *ug = container_of(item, struct unix_gid, h);
488 if (test_bit(CACHE_VALID, &item->flags) &&
489 !test_bit(CACHE_NEGATIVE, &item->flags))
490 put_group_info(ug->gi);
491 kfree(ug);
492}
493
494static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
495{
496 struct unix_gid *orig = container_of(corig, struct unix_gid, h);
497 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
498 return orig->uid == new->uid;
499}
500static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
501{
502 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
503 struct unix_gid *item = container_of(citem, struct unix_gid, h);
504 new->uid = item->uid;
505}
506static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
507{
508 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
509 struct unix_gid *item = container_of(citem, struct unix_gid, h);
510
511 get_group_info(item->gi);
512 new->gi = item->gi;
513}
514static struct cache_head *unix_gid_alloc(void)
515{
516 struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
517 if (g)
518 return &g->h;
519 else
520 return NULL;
521}
522
523static void unix_gid_request(struct cache_detail *cd,
524 struct cache_head *h,
525 char **bpp, int *blen)
526{
527 char tuid[20];
528 struct unix_gid *ug = container_of(h, struct unix_gid, h);
529
530 snprintf(tuid, 20, "%u", ug->uid);
531 qword_add(bpp, blen, tuid);
532 (*bpp)[-1] = '\n';
533}
534
535static struct unix_gid *unix_gid_lookup(uid_t uid);
536extern struct cache_detail unix_gid_cache;
537
538static int unix_gid_parse(struct cache_detail *cd,
539 char *mesg, int mlen)
540{
541 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
542 int uid;
543 int gids;
544 int rv;
545 int i;
546 int err;
547 time_t expiry;
548 struct unix_gid ug, *ugp;
549
550 if (mlen <= 0 || mesg[mlen-1] != '\n')
551 return -EINVAL;
552 mesg[mlen-1] = 0;
553
554 rv = get_int(&mesg, &uid);
555 if (rv)
556 return -EINVAL;
557 ug.uid = uid;
558
559 expiry = get_expiry(&mesg);
560 if (expiry == 0)
561 return -EINVAL;
562
563 rv = get_int(&mesg, &gids);
564 if (rv || gids < 0 || gids > 8192)
565 return -EINVAL;
566
567 ug.gi = groups_alloc(gids);
568 if (!ug.gi)
569 return -ENOMEM;
570
571 for (i = 0 ; i < gids ; i++) {
572 int gid;
573 rv = get_int(&mesg, &gid);
574 err = -EINVAL;
575 if (rv)
576 goto out;
577 GROUP_AT(ug.gi, i) = gid;
578 }
579
580 ugp = unix_gid_lookup(uid);
581 if (ugp) {
582 struct cache_head *ch;
583 ug.h.flags = 0;
584 ug.h.expiry_time = expiry;
585 ch = sunrpc_cache_update(&unix_gid_cache,
586 &ug.h, &ugp->h,
587 hash_long(uid, GID_HASHBITS));
588 if (!ch)
589 err = -ENOMEM;
590 else {
591 err = 0;
592 cache_put(ch, &unix_gid_cache);
593 }
594 } else
595 err = -ENOMEM;
596 out:
597 if (ug.gi)
598 put_group_info(ug.gi);
599 return err;
600}
601
602static int unix_gid_show(struct seq_file *m,
603 struct cache_detail *cd,
604 struct cache_head *h)
605{
606 struct unix_gid *ug;
607 int i;
608 int glen;
609
610 if (h == NULL) {
611 seq_puts(m, "#uid cnt: gids...\n");
612 return 0;
613 }
614 ug = container_of(h, struct unix_gid, h);
615 if (test_bit(CACHE_VALID, &h->flags) &&
616 !test_bit(CACHE_NEGATIVE, &h->flags))
617 glen = ug->gi->ngroups;
618 else
619 glen = 0;
620
621 seq_printf(m, "%d %d:", ug->uid, glen);
622 for (i = 0; i < glen; i++)
623 seq_printf(m, " %d", GROUP_AT(ug->gi, i));
624 seq_printf(m, "\n");
625 return 0;
626}
627
628struct cache_detail unix_gid_cache = {
629 .owner = THIS_MODULE,
630 .hash_size = GID_HASHMAX,
631 .hash_table = gid_table,
632 .name = "auth.unix.gid",
633 .cache_put = unix_gid_put,
634 .cache_request = unix_gid_request,
635 .cache_parse = unix_gid_parse,
636 .cache_show = unix_gid_show,
637 .match = unix_gid_match,
638 .init = unix_gid_init,
639 .update = unix_gid_update,
640 .alloc = unix_gid_alloc,
641};
642
643static struct unix_gid *unix_gid_lookup(uid_t uid)
644{
645 struct unix_gid ug;
646 struct cache_head *ch;
647
648 ug.uid = uid;
649 ch = sunrpc_cache_lookup(&unix_gid_cache, &ug.h,
650 hash_long(uid, GID_HASHBITS));
651 if (ch)
652 return container_of(ch, struct unix_gid, h);
653 else
654 return NULL;
655}
656
657static int unix_gid_find(uid_t uid, struct group_info **gip,
658 struct svc_rqst *rqstp)
659{
660 struct unix_gid *ug = unix_gid_lookup(uid);
661 if (!ug)
662 return -EAGAIN;
663 switch (cache_check(&unix_gid_cache, &ug->h, &rqstp->rq_chandle)) {
664 case -ENOENT:
665 *gip = NULL;
666 return 0;
667 case 0:
668 *gip = ug->gi;
669 get_group_info(*gip);
670 return 0;
671 default:
672 return -EAGAIN;
673 }
674}
675
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700676int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677svcauth_unix_set_client(struct svc_rqst *rqstp)
678{
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100679 struct sockaddr_in *sin;
680 struct sockaddr_in6 *sin6, sin6_storage;
NeilBrown1a9917c2006-03-27 01:15:02 -0800681 struct ip_map *ipm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100683 switch (rqstp->rq_addr.ss_family) {
684 case AF_INET:
685 sin = svc_addr_in(rqstp);
686 sin6 = &sin6_storage;
687 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
688 htonl(0x0000FFFF), sin->sin_addr.s_addr);
689 break;
690 case AF_INET6:
691 sin6 = svc_addr_in6(rqstp);
692 break;
693 default:
694 BUG();
695 }
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 rqstp->rq_client = NULL;
698 if (rqstp->rq_proc == 0)
699 return SVC_OK;
700
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700701 ipm = ip_map_cached_get(rqstp);
702 if (ipm == NULL)
703 ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100704 &sin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 if (ipm == NULL)
707 return SVC_DENIED;
708
709 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
710 default:
711 BUG();
712 case -EAGAIN:
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800713 case -ETIMEDOUT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return SVC_DROP;
715 case -ENOENT:
716 return SVC_DENIED;
717 case 0:
718 rqstp->rq_client = &ipm->m_client->h;
NeilBrownefc36aa2006-03-27 01:14:59 -0800719 kref_get(&rqstp->rq_client->ref);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700720 ip_map_cached_put(rqstp, ipm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 break;
722 }
723 return SVC_OK;
724}
725
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700726EXPORT_SYMBOL(svcauth_unix_set_client);
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728static int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700729svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 struct kvec *argv = &rqstp->rq_arg.head[0];
732 struct kvec *resv = &rqstp->rq_res.head[0];
733 struct svc_cred *cred = &rqstp->rq_cred;
734
735 cred->cr_group_info = NULL;
736 rqstp->rq_client = NULL;
737
738 if (argv->iov_len < 3*4)
739 return SVC_GARBAGE;
740
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800741 if (svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dprintk("svc: bad null cred\n");
743 *authp = rpc_autherr_badcred;
744 return SVC_DENIED;
745 }
Alexey Dobriyan76994312006-09-26 22:28:46 -0700746 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 dprintk("svc: bad null verf\n");
748 *authp = rpc_autherr_badverf;
749 return SVC_DENIED;
750 }
751
752 /* Signal that mapping to nobody uid/gid is required */
753 cred->cr_uid = (uid_t) -1;
754 cred->cr_gid = (gid_t) -1;
755 cred->cr_group_info = groups_alloc(0);
756 if (cred->cr_group_info == NULL)
757 return SVC_DROP; /* kmalloc failure - client must retry */
758
759 /* Put NULL verifier */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700760 svc_putnl(resv, RPC_AUTH_NULL);
761 svc_putnl(resv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Andy Adamsonc4170582007-07-17 04:04:42 -0700763 rqstp->rq_flavor = RPC_AUTH_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return SVC_OK;
765}
766
767static int
768svcauth_null_release(struct svc_rqst *rqstp)
769{
770 if (rqstp->rq_client)
771 auth_domain_put(rqstp->rq_client);
772 rqstp->rq_client = NULL;
773 if (rqstp->rq_cred.cr_group_info)
774 put_group_info(rqstp->rq_cred.cr_group_info);
775 rqstp->rq_cred.cr_group_info = NULL;
776
777 return 0; /* don't drop */
778}
779
780
781struct auth_ops svcauth_null = {
782 .name = "null",
783 .owner = THIS_MODULE,
784 .flavour = RPC_AUTH_NULL,
785 .accept = svcauth_null_accept,
786 .release = svcauth_null_release,
787 .set_client = svcauth_unix_set_client,
788};
789
790
791static int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700792svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 struct kvec *argv = &rqstp->rq_arg.head[0];
795 struct kvec *resv = &rqstp->rq_res.head[0];
796 struct svc_cred *cred = &rqstp->rq_cred;
797 u32 slen, i;
798 int len = argv->iov_len;
799
800 cred->cr_group_info = NULL;
801 rqstp->rq_client = NULL;
802
803 if ((len -= 3*4) < 0)
804 return SVC_GARBAGE;
805
806 svc_getu32(argv); /* length */
807 svc_getu32(argv); /* time stamp */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700808 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (slen > 64 || (len -= (slen + 3)*4) < 0)
810 goto badcred;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700811 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 argv->iov_len -= slen*4;
813
Alexey Dobriyan76994312006-09-26 22:28:46 -0700814 cred->cr_uid = svc_getnl(argv); /* uid */
815 cred->cr_gid = svc_getnl(argv); /* gid */
816 slen = svc_getnl(argv); /* gids length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (slen > 16 || (len -= (slen + 2)*4) < 0)
818 goto badcred;
NeilBrown3fc605a2007-02-14 00:33:13 -0800819 if (unix_gid_find(cred->cr_uid, &cred->cr_group_info, rqstp)
820 == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return SVC_DROP;
NeilBrown3fc605a2007-02-14 00:33:13 -0800822 if (cred->cr_group_info == NULL) {
823 cred->cr_group_info = groups_alloc(slen);
824 if (cred->cr_group_info == NULL)
825 return SVC_DROP;
826 for (i = 0; i < slen; i++)
827 GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
828 } else {
829 for (i = 0; i < slen ; i++)
830 svc_getnl(argv);
831 }
Alexey Dobriyan76994312006-09-26 22:28:46 -0700832 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 *authp = rpc_autherr_badverf;
834 return SVC_DENIED;
835 }
836
837 /* Put NULL verifier */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700838 svc_putnl(resv, RPC_AUTH_NULL);
839 svc_putnl(resv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Andy Adamsonc4170582007-07-17 04:04:42 -0700841 rqstp->rq_flavor = RPC_AUTH_UNIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return SVC_OK;
843
844badcred:
845 *authp = rpc_autherr_badcred;
846 return SVC_DENIED;
847}
848
849static int
850svcauth_unix_release(struct svc_rqst *rqstp)
851{
852 /* Verifier (such as it is) is already in place.
853 */
854 if (rqstp->rq_client)
855 auth_domain_put(rqstp->rq_client);
856 rqstp->rq_client = NULL;
857 if (rqstp->rq_cred.cr_group_info)
858 put_group_info(rqstp->rq_cred.cr_group_info);
859 rqstp->rq_cred.cr_group_info = NULL;
860
861 return 0;
862}
863
864
865struct auth_ops svcauth_unix = {
866 .name = "unix",
867 .owner = THIS_MODULE,
868 .flavour = RPC_AUTH_UNIX,
869 .accept = svcauth_unix_accept,
870 .release = svcauth_unix_release,
871 .domain_release = svcauth_unix_domain_release,
872 .set_client = svcauth_unix_set_client,
873};
874