blob: 2a76c7cf603ec4223f78144385676ff855a4ff0f [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Greg Banks7b2b1fe2006-10-04 02:15:50 -070014#include <net/sock.h>
Aurélien Charbonf15364b2008-01-18 15:50:56 +010015#include <net/ipv6.h>
16#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#define RPCDBG_FACILITY RPCDBG_AUTH
18
Chuck Lever07396052010-01-26 14:03:47 -050019#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21/*
22 * AUTHUNIX and AUTHNULL credentials are both handled here.
23 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
24 * are always nobody (-2). i.e. we do the same IP address checks for
25 * AUTHNULL as for AUTHUNIX, and that is done here.
26 */
27
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029struct unix_domain {
30 struct auth_domain h;
31 int addr_changes;
32 /* other stuff later */
33};
34
NeilBrownefc36aa2006-03-27 01:14:59 -080035extern struct auth_ops svcauth_unix;
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037struct auth_domain *unix_domain_find(char *name)
38{
NeilBrownefc36aa2006-03-27 01:14:59 -080039 struct auth_domain *rv;
40 struct unix_domain *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
NeilBrownefc36aa2006-03-27 01:14:59 -080042 rv = auth_domain_lookup(name, NULL);
43 while(1) {
NeilBrownad1b5222006-03-27 01:15:11 -080044 if (rv) {
45 if (new && rv != &new->h)
46 auth_domain_put(&new->h);
47
48 if (rv->flavour != &svcauth_unix) {
49 auth_domain_put(rv);
50 return NULL;
51 }
NeilBrownefc36aa2006-03-27 01:14:59 -080052 return rv;
53 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
NeilBrownefc36aa2006-03-27 01:14:59 -080055 new = kmalloc(sizeof(*new), GFP_KERNEL);
56 if (new == NULL)
57 return NULL;
58 kref_init(&new->h.ref);
59 new->h.name = kstrdup(name, GFP_KERNEL);
NeilBrowndd08d6e2006-12-13 00:35:44 -080060 if (new->h.name == NULL) {
61 kfree(new);
62 return NULL;
63 }
NeilBrownefc36aa2006-03-27 01:14:59 -080064 new->h.flavour = &svcauth_unix;
65 new->addr_changes = 0;
66 rv = auth_domain_lookup(name, &new->h);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
Trond Myklebust24c37672008-12-23 16:30:12 -050069EXPORT_SYMBOL_GPL(unix_domain_find);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71static void svcauth_unix_domain_release(struct auth_domain *dom)
72{
73 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
74
75 kfree(dom->name);
76 kfree(ud);
77}
78
79
80/**************************************************
81 * cache for IP address to unix_domain
82 * as needed by AUTH_UNIX
83 */
84#define IP_HASHBITS 8
85#define IP_HASHMAX (1<<IP_HASHBITS)
86#define IP_HASHMASK (IP_HASHMAX-1)
87
88struct ip_map {
89 struct cache_head h;
90 char m_class[8]; /* e.g. "nfsd" */
Aurélien Charbonf15364b2008-01-18 15:50:56 +010091 struct in6_addr m_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct unix_domain *m_client;
93 int m_add_change;
94};
95static struct cache_head *ip_table[IP_HASHMAX];
96
NeilBrownbaab9352006-03-27 01:15:09 -080097static void ip_map_put(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
NeilBrownbaab9352006-03-27 01:15:09 -080099 struct cache_head *item = container_of(kref, struct cache_head, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 struct ip_map *im = container_of(item, struct ip_map,h);
NeilBrownbaab9352006-03-27 01:15:09 -0800101
102 if (test_bit(CACHE_VALID, &item->flags) &&
103 !test_bit(CACHE_NEGATIVE, &item->flags))
104 auth_domain_put(&im->m_client->h);
105 kfree(im);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
NeilBrown1f1e0302006-01-06 00:09:49 -0800108#if IP_HASHBITS == 8
109/* hash_long on a 64 bit machine is currently REALLY BAD for
110 * IP addresses in reverse-endian (i.e. on a little-endian machine).
111 * So use a trivial but reliable hash instead
112 */
Al Viro48061262006-11-08 00:22:34 -0800113static inline int hash_ip(__be32 ip)
NeilBrown1f1e0302006-01-06 00:09:49 -0800114{
Al Viro48061262006-11-08 00:22:34 -0800115 int hash = (__force u32)ip ^ ((__force u32)ip>>16);
NeilBrown1f1e0302006-01-06 00:09:49 -0800116 return (hash ^ (hash>>8)) & 0xff;
117}
118#endif
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100119static inline int hash_ip6(struct in6_addr ip)
120{
121 return (hash_ip(ip.s6_addr32[0]) ^
122 hash_ip(ip.s6_addr32[1]) ^
123 hash_ip(ip.s6_addr32[2]) ^
124 hash_ip(ip.s6_addr32[3]));
125}
NeilBrown1a9917c2006-03-27 01:15:02 -0800126static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
127{
128 struct ip_map *orig = container_of(corig, struct ip_map, h);
129 struct ip_map *new = container_of(cnew, struct ip_map, h);
Joe Perchesf64f9e72009-11-29 16:55:45 -0800130 return strcmp(orig->m_class, new->m_class) == 0 &&
131 ipv6_addr_equal(&orig->m_addr, &new->m_addr);
NeilBrown1a9917c2006-03-27 01:15:02 -0800132}
133static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
134{
135 struct ip_map *new = container_of(cnew, struct ip_map, h);
136 struct ip_map *item = container_of(citem, struct ip_map, h);
NeilBrown1f1e0302006-01-06 00:09:49 -0800137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 strcpy(new->m_class, item->m_class);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100139 ipv6_addr_copy(&new->m_addr, &item->m_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
NeilBrown1a9917c2006-03-27 01:15:02 -0800141static void update(struct cache_head *cnew, struct cache_head *citem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
NeilBrown1a9917c2006-03-27 01:15:02 -0800143 struct ip_map *new = container_of(cnew, struct ip_map, h);
144 struct ip_map *item = container_of(citem, struct ip_map, h);
145
NeilBrownefc36aa2006-03-27 01:14:59 -0800146 kref_get(&item->m_client->h.ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 new->m_client = item->m_client;
148 new->m_add_change = item->m_add_change;
149}
NeilBrown1a9917c2006-03-27 01:15:02 -0800150static struct cache_head *ip_map_alloc(void)
151{
152 struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
153 if (i)
154 return &i->h;
155 else
156 return NULL;
157}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159static void ip_map_request(struct cache_detail *cd,
160 struct cache_head *h,
161 char **bpp, int *blen)
162{
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100163 char text_addr[40];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct ip_map *im = container_of(h, struct ip_map, h);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800165
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100166 if (ipv6_addr_v4mapped(&(im->m_addr))) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700167 snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100168 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700169 snprintf(text_addr, 40, "%pI6", &im->m_addr);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 qword_add(bpp, blen, im->m_class);
172 qword_add(bpp, blen, text_addr);
173 (*bpp)[-1] = '\n';
174}
175
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400176static int ip_map_upcall(struct cache_detail *cd, struct cache_head *h)
177{
178 return sunrpc_cache_pipe_upcall(cd, h, ip_map_request);
179}
180
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400181static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, struct in6_addr *addr);
182static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184static int ip_map_parse(struct cache_detail *cd,
185 char *mesg, int mlen)
186{
187 /* class ipaddress [domainname] */
188 /* should be safe just to use the start of the input buffer
189 * for scratch: */
190 char *buf = mesg;
191 int len;
NeilBrown1a9917c2006-03-27 01:15:02 -0800192 char class[8];
Chuck Lever07396052010-01-26 14:03:47 -0500193 union {
194 struct sockaddr sa;
195 struct sockaddr_in s4;
196 struct sockaddr_in6 s6;
197 } address;
198 struct sockaddr_in6 sin6;
NeilBrown1a9917c2006-03-27 01:15:02 -0800199 int err;
200
201 struct ip_map *ipmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct auth_domain *dom;
203 time_t expiry;
204
205 if (mesg[mlen-1] != '\n')
206 return -EINVAL;
207 mesg[mlen-1] = 0;
208
209 /* class */
NeilBrown1a9917c2006-03-27 01:15:02 -0800210 len = qword_get(&mesg, class, sizeof(class));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (len <= 0) return -EINVAL;
212
213 /* ip address */
214 len = qword_get(&mesg, buf, mlen);
215 if (len <= 0) return -EINVAL;
216
Chuck Lever07396052010-01-26 14:03:47 -0500217 if (rpc_pton(buf, len, &address.sa, sizeof(address)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return -EINVAL;
Chuck Lever07396052010-01-26 14:03:47 -0500219 switch (address.sa.sa_family) {
220 case AF_INET:
221 /* Form a mapped IPv4 address in sin6 */
222 memset(&sin6, 0, sizeof(sin6));
223 sin6.sin6_family = AF_INET6;
224 sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
225 sin6.sin6_addr.s6_addr32[3] = address.s4.sin_addr.s_addr;
226 break;
227#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
228 case AF_INET6:
229 memcpy(&sin6, &address.s6, sizeof(sin6));
230 break;
231#endif
232 default:
233 return -EINVAL;
234 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 expiry = get_expiry(&mesg);
237 if (expiry ==0)
238 return -EINVAL;
239
240 /* domainname, or empty for NEGATIVE */
241 len = qword_get(&mesg, buf, mlen);
242 if (len < 0) return -EINVAL;
243
244 if (len) {
245 dom = unix_domain_find(buf);
246 if (dom == NULL)
247 return -ENOENT;
248 } else
249 dom = NULL;
250
Chuck Lever07396052010-01-26 14:03:47 -0500251 /* IPv6 scope IDs are ignored for now */
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400252 ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr);
NeilBrown1a9917c2006-03-27 01:15:02 -0800253 if (ipmp) {
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400254 err = __ip_map_update(cd, ipmp,
NeilBrown1a9917c2006-03-27 01:15:02 -0800255 container_of(dom, struct unix_domain, h),
256 expiry);
257 } else
258 err = -ENOMEM;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (dom)
261 auth_domain_put(dom);
NeilBrown1a9917c2006-03-27 01:15:02 -0800262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 cache_flush();
NeilBrown1a9917c2006-03-27 01:15:02 -0800264 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267static int ip_map_show(struct seq_file *m,
268 struct cache_detail *cd,
269 struct cache_head *h)
270{
271 struct ip_map *im;
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100272 struct in6_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 char *dom = "-no-domain-";
274
275 if (h == NULL) {
276 seq_puts(m, "#class IP domain\n");
277 return 0;
278 }
279 im = container_of(h, struct ip_map, h);
280 /* class addr domain */
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100281 ipv6_addr_copy(&addr, &im->m_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800283 if (test_bit(CACHE_VALID, &h->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 !test_bit(CACHE_NEGATIVE, &h->flags))
285 dom = im->m_client->h.name;
286
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100287 if (ipv6_addr_v4mapped(&addr)) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700288 seq_printf(m, "%s %pI4 %s\n",
289 im->m_class, &addr.s6_addr32[3], dom);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100290 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700291 seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return 0;
294}
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297struct cache_detail ip_map_cache = {
Bruce Allanf35279d2005-09-06 15:17:08 -0700298 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 .hash_size = IP_HASHMAX,
300 .hash_table = ip_table,
301 .name = "auth.unix.ip",
302 .cache_put = ip_map_put,
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400303 .cache_upcall = ip_map_upcall,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 .cache_parse = ip_map_parse,
305 .cache_show = ip_map_show,
NeilBrown1a9917c2006-03-27 01:15:02 -0800306 .match = ip_map_match,
307 .init = ip_map_init,
308 .update = update,
309 .alloc = ip_map_alloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400312static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class,
313 struct in6_addr *addr)
NeilBrown1a9917c2006-03-27 01:15:02 -0800314{
315 struct ip_map ip;
316 struct cache_head *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
NeilBrown1a9917c2006-03-27 01:15:02 -0800318 strcpy(ip.m_class, class);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100319 ipv6_addr_copy(&ip.m_addr, addr);
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400320 ch = sunrpc_cache_lookup(cd, &ip.h,
NeilBrown1a9917c2006-03-27 01:15:02 -0800321 hash_str(class, IP_HASHBITS) ^
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100322 hash_ip6(*addr));
NeilBrown1a9917c2006-03-27 01:15:02 -0800323
324 if (ch)
325 return container_of(ch, struct ip_map, h);
326 else
327 return NULL;
328}
329
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400330static inline struct ip_map *ip_map_lookup(struct net *net, char *class,
331 struct in6_addr *addr)
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400332{
333 return __ip_map_lookup(&ip_map_cache, class, addr);
334}
335
336static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm,
337 struct unix_domain *udom, time_t expiry)
NeilBrown1a9917c2006-03-27 01:15:02 -0800338{
339 struct ip_map ip;
340 struct cache_head *ch;
341
342 ip.m_client = udom;
343 ip.h.flags = 0;
344 if (!udom)
345 set_bit(CACHE_NEGATIVE, &ip.h.flags);
346 else {
347 ip.m_add_change = udom->addr_changes;
348 /* if this is from the legacy set_client system call,
349 * we need m_add_change to be one higher
350 */
351 if (expiry == NEVER)
352 ip.m_add_change++;
353 }
354 ip.h.expiry_time = expiry;
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400355 ch = sunrpc_cache_update(cd, &ip.h, &ipm->h,
NeilBrown1a9917c2006-03-27 01:15:02 -0800356 hash_str(ipm->m_class, IP_HASHBITS) ^
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100357 hash_ip6(ipm->m_addr));
NeilBrown1a9917c2006-03-27 01:15:02 -0800358 if (!ch)
359 return -ENOMEM;
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400360 cache_put(ch, cd);
NeilBrown1a9917c2006-03-27 01:15:02 -0800361 return 0;
362}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400364static inline int ip_map_update(struct net *net, struct ip_map *ipm,
365 struct unix_domain *udom, time_t expiry)
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400366{
367 return __ip_map_update(&ip_map_cache, ipm, udom, expiry);
368}
369
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400370int auth_unix_add_addr(struct net *net, struct in6_addr *addr, struct auth_domain *dom)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct unix_domain *udom;
NeilBrown1a9917c2006-03-27 01:15:02 -0800373 struct ip_map *ipmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
NeilBrownefc36aa2006-03-27 01:14:59 -0800375 if (dom->flavour != &svcauth_unix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return -EINVAL;
377 udom = container_of(dom, struct unix_domain, h);
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400378 ipmp = ip_map_lookup(net, "nfsd", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
NeilBrown1a9917c2006-03-27 01:15:02 -0800380 if (ipmp)
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400381 return ip_map_update(net, ipmp, udom, NEVER);
NeilBrown1a9917c2006-03-27 01:15:02 -0800382 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return -ENOMEM;
384}
Trond Myklebust24c37672008-12-23 16:30:12 -0500385EXPORT_SYMBOL_GPL(auth_unix_add_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387int auth_unix_forget_old(struct auth_domain *dom)
388{
389 struct unix_domain *udom;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800390
NeilBrownefc36aa2006-03-27 01:14:59 -0800391 if (dom->flavour != &svcauth_unix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -EINVAL;
393 udom = container_of(dom, struct unix_domain, h);
394 udom->addr_changes++;
395 return 0;
396}
Trond Myklebust24c37672008-12-23 16:30:12 -0500397EXPORT_SYMBOL_GPL(auth_unix_forget_old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400399struct auth_domain *auth_unix_lookup(struct net *net, struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Greg Banks40f10522006-10-02 02:17:43 -0700401 struct ip_map *ipm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct auth_domain *rv;
403
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400404 ipm = ip_map_lookup(net, "nfsd", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 if (!ipm)
407 return NULL;
408 if (cache_check(&ip_map_cache, &ipm->h, NULL))
409 return NULL;
410
411 if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
412 if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
413 auth_domain_put(&ipm->m_client->h);
414 rv = NULL;
415 } else {
416 rv = &ipm->m_client->h;
NeilBrownefc36aa2006-03-27 01:14:59 -0800417 kref_get(&rv->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
NeilBrownbaab9352006-03-27 01:15:09 -0800419 cache_put(&ipm->h, &ip_map_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return rv;
421}
Trond Myklebust24c37672008-12-23 16:30:12 -0500422EXPORT_SYMBOL_GPL(auth_unix_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424void svcauth_unix_purge(void)
425{
426 cache_purge(&ip_map_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
Trond Myklebust24c37672008-12-23 16:30:12 -0500428EXPORT_SYMBOL_GPL(svcauth_unix_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700430static inline struct ip_map *
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400431ip_map_cached_get(struct svc_xprt *xprt)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700432{
Tom Tuckerdef13d72007-12-30 21:08:08 -0600433 struct ip_map *ipm = NULL;
Tom Tuckerdef13d72007-12-30 21:08:08 -0600434
435 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
436 spin_lock(&xprt->xpt_lock);
437 ipm = xprt->xpt_auth_cache;
438 if (ipm != NULL) {
439 if (!cache_valid(&ipm->h)) {
440 /*
441 * The entry has been invalidated since it was
442 * remembered, e.g. by a second mount from the
443 * same IP address.
444 */
445 xprt->xpt_auth_cache = NULL;
446 spin_unlock(&xprt->xpt_lock);
447 cache_put(&ipm->h, &ip_map_cache);
448 return NULL;
449 }
450 cache_get(&ipm->h);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700451 }
Tom Tuckerdef13d72007-12-30 21:08:08 -0600452 spin_unlock(&xprt->xpt_lock);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700453 }
454 return ipm;
455}
456
457static inline void
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400458ip_map_cached_put(struct svc_xprt *xprt, struct ip_map *ipm)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700459{
Tom Tuckerdef13d72007-12-30 21:08:08 -0600460 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
461 spin_lock(&xprt->xpt_lock);
462 if (xprt->xpt_auth_cache == NULL) {
463 /* newly cached, keep the reference */
464 xprt->xpt_auth_cache = ipm;
465 ipm = NULL;
466 }
467 spin_unlock(&xprt->xpt_lock);
NeilBrown30f3dee2007-04-16 22:53:25 -0700468 }
NeilBrown30f3dee2007-04-16 22:53:25 -0700469 if (ipm)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700470 cache_put(&ipm->h, &ip_map_cache);
471}
472
473void
Pavel Emelyanove3bfca02010-09-27 13:58:42 +0400474svcauth_unix_info_release(struct svc_xprt *xpt)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700475{
Pavel Emelyanove3bfca02010-09-27 13:58:42 +0400476 struct ip_map *ipm;
477
478 ipm = xpt->xpt_auth_cache;
479 if (ipm != NULL)
480 cache_put(&ipm->h, &ip_map_cache);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700481}
482
NeilBrown3fc605a2007-02-14 00:33:13 -0800483/****************************************************************************
484 * auth.unix.gid cache
485 * simple cache to map a UID to a list of GIDs
486 * because AUTH_UNIX aka AUTH_SYS has a max of 16
487 */
488#define GID_HASHBITS 8
489#define GID_HASHMAX (1<<GID_HASHBITS)
490#define GID_HASHMASK (GID_HASHMAX - 1)
491
492struct unix_gid {
493 struct cache_head h;
494 uid_t uid;
495 struct group_info *gi;
496};
497static struct cache_head *gid_table[GID_HASHMAX];
498
499static void unix_gid_put(struct kref *kref)
500{
501 struct cache_head *item = container_of(kref, struct cache_head, ref);
502 struct unix_gid *ug = container_of(item, struct unix_gid, h);
503 if (test_bit(CACHE_VALID, &item->flags) &&
504 !test_bit(CACHE_NEGATIVE, &item->flags))
505 put_group_info(ug->gi);
506 kfree(ug);
507}
508
509static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
510{
511 struct unix_gid *orig = container_of(corig, struct unix_gid, h);
512 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
513 return orig->uid == new->uid;
514}
515static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
516{
517 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
518 struct unix_gid *item = container_of(citem, struct unix_gid, h);
519 new->uid = item->uid;
520}
521static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
522{
523 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
524 struct unix_gid *item = container_of(citem, struct unix_gid, h);
525
526 get_group_info(item->gi);
527 new->gi = item->gi;
528}
529static struct cache_head *unix_gid_alloc(void)
530{
531 struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
532 if (g)
533 return &g->h;
534 else
535 return NULL;
536}
537
538static void unix_gid_request(struct cache_detail *cd,
539 struct cache_head *h,
540 char **bpp, int *blen)
541{
542 char tuid[20];
543 struct unix_gid *ug = container_of(h, struct unix_gid, h);
544
545 snprintf(tuid, 20, "%u", ug->uid);
546 qword_add(bpp, blen, tuid);
547 (*bpp)[-1] = '\n';
548}
549
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400550static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
551{
552 return sunrpc_cache_pipe_upcall(cd, h, unix_gid_request);
553}
554
NeilBrown3fc605a2007-02-14 00:33:13 -0800555static struct unix_gid *unix_gid_lookup(uid_t uid);
556extern struct cache_detail unix_gid_cache;
557
558static int unix_gid_parse(struct cache_detail *cd,
559 char *mesg, int mlen)
560{
561 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
562 int uid;
563 int gids;
564 int rv;
565 int i;
566 int err;
567 time_t expiry;
568 struct unix_gid ug, *ugp;
569
570 if (mlen <= 0 || mesg[mlen-1] != '\n')
571 return -EINVAL;
572 mesg[mlen-1] = 0;
573
574 rv = get_int(&mesg, &uid);
575 if (rv)
576 return -EINVAL;
577 ug.uid = uid;
578
579 expiry = get_expiry(&mesg);
580 if (expiry == 0)
581 return -EINVAL;
582
583 rv = get_int(&mesg, &gids);
584 if (rv || gids < 0 || gids > 8192)
585 return -EINVAL;
586
587 ug.gi = groups_alloc(gids);
588 if (!ug.gi)
589 return -ENOMEM;
590
591 for (i = 0 ; i < gids ; i++) {
592 int gid;
593 rv = get_int(&mesg, &gid);
594 err = -EINVAL;
595 if (rv)
596 goto out;
597 GROUP_AT(ug.gi, i) = gid;
598 }
599
600 ugp = unix_gid_lookup(uid);
601 if (ugp) {
602 struct cache_head *ch;
603 ug.h.flags = 0;
604 ug.h.expiry_time = expiry;
605 ch = sunrpc_cache_update(&unix_gid_cache,
606 &ug.h, &ugp->h,
607 hash_long(uid, GID_HASHBITS));
608 if (!ch)
609 err = -ENOMEM;
610 else {
611 err = 0;
612 cache_put(ch, &unix_gid_cache);
613 }
614 } else
615 err = -ENOMEM;
616 out:
617 if (ug.gi)
618 put_group_info(ug.gi);
619 return err;
620}
621
622static int unix_gid_show(struct seq_file *m,
623 struct cache_detail *cd,
624 struct cache_head *h)
625{
626 struct unix_gid *ug;
627 int i;
628 int glen;
629
630 if (h == NULL) {
631 seq_puts(m, "#uid cnt: gids...\n");
632 return 0;
633 }
634 ug = container_of(h, struct unix_gid, h);
635 if (test_bit(CACHE_VALID, &h->flags) &&
636 !test_bit(CACHE_NEGATIVE, &h->flags))
637 glen = ug->gi->ngroups;
638 else
639 glen = 0;
640
J. Bruce Fieldsccdb3572010-03-02 15:49:21 -0500641 seq_printf(m, "%u %d:", ug->uid, glen);
NeilBrown3fc605a2007-02-14 00:33:13 -0800642 for (i = 0; i < glen; i++)
643 seq_printf(m, " %d", GROUP_AT(ug->gi, i));
644 seq_printf(m, "\n");
645 return 0;
646}
647
648struct cache_detail unix_gid_cache = {
649 .owner = THIS_MODULE,
650 .hash_size = GID_HASHMAX,
651 .hash_table = gid_table,
652 .name = "auth.unix.gid",
653 .cache_put = unix_gid_put,
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400654 .cache_upcall = unix_gid_upcall,
NeilBrown3fc605a2007-02-14 00:33:13 -0800655 .cache_parse = unix_gid_parse,
656 .cache_show = unix_gid_show,
657 .match = unix_gid_match,
658 .init = unix_gid_init,
659 .update = unix_gid_update,
660 .alloc = unix_gid_alloc,
661};
662
663static struct unix_gid *unix_gid_lookup(uid_t uid)
664{
665 struct unix_gid ug;
666 struct cache_head *ch;
667
668 ug.uid = uid;
669 ch = sunrpc_cache_lookup(&unix_gid_cache, &ug.h,
670 hash_long(uid, GID_HASHBITS));
671 if (ch)
672 return container_of(ch, struct unix_gid, h);
673 else
674 return NULL;
675}
676
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400677static struct group_info *unix_gid_find(uid_t uid, struct svc_rqst *rqstp)
NeilBrown3fc605a2007-02-14 00:33:13 -0800678{
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400679 struct unix_gid *ug;
680 struct group_info *gi;
681 int ret;
682
683 ug = unix_gid_lookup(uid);
NeilBrown3fc605a2007-02-14 00:33:13 -0800684 if (!ug)
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400685 return ERR_PTR(-EAGAIN);
686 ret = cache_check(&unix_gid_cache, &ug->h, &rqstp->rq_chandle);
687 switch (ret) {
NeilBrown3fc605a2007-02-14 00:33:13 -0800688 case -ENOENT:
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400689 return ERR_PTR(-ENOENT);
NeilBrown1ebede82010-08-12 17:04:07 +1000690 case -ETIMEDOUT:
691 return ERR_PTR(-ESHUTDOWN);
NeilBrown3fc605a2007-02-14 00:33:13 -0800692 case 0:
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400693 gi = get_group_info(ug->gi);
NeilBrown560ab422009-08-04 15:22:39 +1000694 cache_put(&ug->h, &unix_gid_cache);
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400695 return gi;
NeilBrown3fc605a2007-02-14 00:33:13 -0800696 default:
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400697 return ERR_PTR(-EAGAIN);
NeilBrown3fc605a2007-02-14 00:33:13 -0800698 }
699}
700
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700701int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702svcauth_unix_set_client(struct svc_rqst *rqstp)
703{
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100704 struct sockaddr_in *sin;
705 struct sockaddr_in6 *sin6, sin6_storage;
NeilBrown1a9917c2006-03-27 01:15:02 -0800706 struct ip_map *ipm;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400707 struct group_info *gi;
708 struct svc_cred *cred = &rqstp->rq_cred;
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400709 struct svc_xprt *xprt = rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100711 switch (rqstp->rq_addr.ss_family) {
712 case AF_INET:
713 sin = svc_addr_in(rqstp);
714 sin6 = &sin6_storage;
Brian Haleyb301e822009-10-07 13:58:25 -0700715 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100716 break;
717 case AF_INET6:
718 sin6 = svc_addr_in6(rqstp);
719 break;
720 default:
721 BUG();
722 }
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 rqstp->rq_client = NULL;
725 if (rqstp->rq_proc == 0)
726 return SVC_OK;
727
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400728 ipm = ip_map_cached_get(xprt);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700729 if (ipm == NULL)
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400730 ipm = ip_map_lookup(&init_net, rqstp->rq_server->sv_program->pg_class,
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100731 &sin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 if (ipm == NULL)
734 return SVC_DENIED;
735
736 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
737 default:
738 BUG();
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800739 case -ETIMEDOUT:
NeilBrown1ebede82010-08-12 17:04:07 +1000740 return SVC_CLOSE;
741 case -EAGAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return SVC_DROP;
743 case -ENOENT:
744 return SVC_DENIED;
745 case 0:
746 rqstp->rq_client = &ipm->m_client->h;
NeilBrownefc36aa2006-03-27 01:14:59 -0800747 kref_get(&rqstp->rq_client->ref);
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400748 ip_map_cached_put(xprt, ipm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 break;
750 }
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400751
752 gi = unix_gid_find(cred->cr_uid, rqstp);
753 switch (PTR_ERR(gi)) {
754 case -EAGAIN:
755 return SVC_DROP;
NeilBrown1ebede82010-08-12 17:04:07 +1000756 case -ESHUTDOWN:
757 return SVC_CLOSE;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400758 case -ENOENT:
759 break;
760 default:
761 put_group_info(cred->cr_group_info);
762 cred->cr_group_info = gi;
763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return SVC_OK;
765}
766
Trond Myklebust24c37672008-12-23 16:30:12 -0500767EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769static int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700770svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct kvec *argv = &rqstp->rq_arg.head[0];
773 struct kvec *resv = &rqstp->rq_res.head[0];
774 struct svc_cred *cred = &rqstp->rq_cred;
775
776 cred->cr_group_info = NULL;
777 rqstp->rq_client = NULL;
778
779 if (argv->iov_len < 3*4)
780 return SVC_GARBAGE;
781
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800782 if (svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 dprintk("svc: bad null cred\n");
784 *authp = rpc_autherr_badcred;
785 return SVC_DENIED;
786 }
Alexey Dobriyan76994312006-09-26 22:28:46 -0700787 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 dprintk("svc: bad null verf\n");
789 *authp = rpc_autherr_badverf;
790 return SVC_DENIED;
791 }
792
793 /* Signal that mapping to nobody uid/gid is required */
794 cred->cr_uid = (uid_t) -1;
795 cred->cr_gid = (gid_t) -1;
796 cred->cr_group_info = groups_alloc(0);
797 if (cred->cr_group_info == NULL)
NeilBrown1ebede82010-08-12 17:04:07 +1000798 return SVC_CLOSE; /* kmalloc failure - client must retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 /* Put NULL verifier */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700801 svc_putnl(resv, RPC_AUTH_NULL);
802 svc_putnl(resv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Andy Adamsonc4170582007-07-17 04:04:42 -0700804 rqstp->rq_flavor = RPC_AUTH_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return SVC_OK;
806}
807
808static int
809svcauth_null_release(struct svc_rqst *rqstp)
810{
811 if (rqstp->rq_client)
812 auth_domain_put(rqstp->rq_client);
813 rqstp->rq_client = NULL;
814 if (rqstp->rq_cred.cr_group_info)
815 put_group_info(rqstp->rq_cred.cr_group_info);
816 rqstp->rq_cred.cr_group_info = NULL;
817
818 return 0; /* don't drop */
819}
820
821
822struct auth_ops svcauth_null = {
823 .name = "null",
824 .owner = THIS_MODULE,
825 .flavour = RPC_AUTH_NULL,
826 .accept = svcauth_null_accept,
827 .release = svcauth_null_release,
828 .set_client = svcauth_unix_set_client,
829};
830
831
832static int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700833svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 struct kvec *argv = &rqstp->rq_arg.head[0];
836 struct kvec *resv = &rqstp->rq_res.head[0];
837 struct svc_cred *cred = &rqstp->rq_cred;
838 u32 slen, i;
839 int len = argv->iov_len;
840
841 cred->cr_group_info = NULL;
842 rqstp->rq_client = NULL;
843
844 if ((len -= 3*4) < 0)
845 return SVC_GARBAGE;
846
847 svc_getu32(argv); /* length */
848 svc_getu32(argv); /* time stamp */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700849 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (slen > 64 || (len -= (slen + 3)*4) < 0)
851 goto badcred;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700852 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 argv->iov_len -= slen*4;
854
Alexey Dobriyan76994312006-09-26 22:28:46 -0700855 cred->cr_uid = svc_getnl(argv); /* uid */
856 cred->cr_gid = svc_getnl(argv); /* gid */
857 slen = svc_getnl(argv); /* gids length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (slen > 16 || (len -= (slen + 2)*4) < 0)
859 goto badcred;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400860 cred->cr_group_info = groups_alloc(slen);
861 if (cred->cr_group_info == NULL)
NeilBrown1ebede82010-08-12 17:04:07 +1000862 return SVC_CLOSE;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400863 for (i = 0; i < slen; i++)
864 GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
Alexey Dobriyan76994312006-09-26 22:28:46 -0700865 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 *authp = rpc_autherr_badverf;
867 return SVC_DENIED;
868 }
869
870 /* Put NULL verifier */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700871 svc_putnl(resv, RPC_AUTH_NULL);
872 svc_putnl(resv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Andy Adamsonc4170582007-07-17 04:04:42 -0700874 rqstp->rq_flavor = RPC_AUTH_UNIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return SVC_OK;
876
877badcred:
878 *authp = rpc_autherr_badcred;
879 return SVC_DENIED;
880}
881
882static int
883svcauth_unix_release(struct svc_rqst *rqstp)
884{
885 /* Verifier (such as it is) is already in place.
886 */
887 if (rqstp->rq_client)
888 auth_domain_put(rqstp->rq_client);
889 rqstp->rq_client = NULL;
890 if (rqstp->rq_cred.cr_group_info)
891 put_group_info(rqstp->rq_cred.cr_group_info);
892 rqstp->rq_cred.cr_group_info = NULL;
893
894 return 0;
895}
896
897
898struct auth_ops svcauth_unix = {
899 .name = "unix",
900 .owner = THIS_MODULE,
901 .flavour = RPC_AUTH_UNIX,
902 .accept = svcauth_unix_accept,
903 .release = svcauth_unix_release,
904 .domain_release = svcauth_unix_domain_release,
905 .set_client = svcauth_unix_set_client,
906};
907