Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #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 Adamson | c417058 | 2007-07-17 04:04:42 -0700 | [diff] [blame] | 8 | #include <linux/sunrpc/gss_api.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/err.h> |
| 10 | #include <linux/seq_file.h> |
| 11 | #include <linux/hash.h> |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 12 | #include <linux/string.h> |
Greg Banks | 7b2b1fe | 2006-10-04 02:15:50 -0700 | [diff] [blame] | 13 | #include <net/sock.h> |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 14 | #include <net/ipv6.h> |
| 15 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #define RPCDBG_FACILITY RPCDBG_AUTH |
| 17 | |
Chuck Lever | 0739605 | 2010-01-26 14:03:47 -0500 | [diff] [blame] | 18 | #include <linux/sunrpc/clnt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * AUTHUNIX and AUTHNULL credentials are both handled here. |
| 22 | * AUTHNULL is treated just like AUTHUNIX except that the uid/gid |
| 23 | * are always nobody (-2). i.e. we do the same IP address checks for |
| 24 | * AUTHNULL as for AUTHUNIX, and that is done here. |
| 25 | */ |
| 26 | |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | struct unix_domain { |
| 29 | struct auth_domain h; |
| 30 | int addr_changes; |
| 31 | /* other stuff later */ |
| 32 | }; |
| 33 | |
NeilBrown | efc36aa | 2006-03-27 01:14:59 -0800 | [diff] [blame] | 34 | extern struct auth_ops svcauth_unix; |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | struct auth_domain *unix_domain_find(char *name) |
| 37 | { |
NeilBrown | efc36aa | 2006-03-27 01:14:59 -0800 | [diff] [blame] | 38 | struct auth_domain *rv; |
| 39 | struct unix_domain *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
NeilBrown | efc36aa | 2006-03-27 01:14:59 -0800 | [diff] [blame] | 41 | rv = auth_domain_lookup(name, NULL); |
| 42 | while(1) { |
NeilBrown | ad1b522 | 2006-03-27 01:15:11 -0800 | [diff] [blame] | 43 | if (rv) { |
| 44 | if (new && rv != &new->h) |
| 45 | auth_domain_put(&new->h); |
| 46 | |
| 47 | if (rv->flavour != &svcauth_unix) { |
| 48 | auth_domain_put(rv); |
| 49 | return NULL; |
| 50 | } |
NeilBrown | efc36aa | 2006-03-27 01:14:59 -0800 | [diff] [blame] | 51 | return rv; |
| 52 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
NeilBrown | efc36aa | 2006-03-27 01:14:59 -0800 | [diff] [blame] | 54 | new = kmalloc(sizeof(*new), GFP_KERNEL); |
| 55 | if (new == NULL) |
| 56 | return NULL; |
| 57 | kref_init(&new->h.ref); |
| 58 | new->h.name = kstrdup(name, GFP_KERNEL); |
NeilBrown | dd08d6e | 2006-12-13 00:35:44 -0800 | [diff] [blame] | 59 | if (new->h.name == NULL) { |
| 60 | kfree(new); |
| 61 | return NULL; |
| 62 | } |
NeilBrown | efc36aa | 2006-03-27 01:14:59 -0800 | [diff] [blame] | 63 | new->h.flavour = &svcauth_unix; |
| 64 | new->addr_changes = 0; |
| 65 | rv = auth_domain_lookup(name, &new->h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 68 | EXPORT_SYMBOL_GPL(unix_domain_find); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | static void svcauth_unix_domain_release(struct auth_domain *dom) |
| 71 | { |
| 72 | struct unix_domain *ud = container_of(dom, struct unix_domain, h); |
| 73 | |
| 74 | kfree(dom->name); |
| 75 | kfree(ud); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /************************************************** |
| 80 | * cache for IP address to unix_domain |
| 81 | * as needed by AUTH_UNIX |
| 82 | */ |
| 83 | #define IP_HASHBITS 8 |
| 84 | #define IP_HASHMAX (1<<IP_HASHBITS) |
| 85 | #define IP_HASHMASK (IP_HASHMAX-1) |
| 86 | |
| 87 | struct ip_map { |
| 88 | struct cache_head h; |
| 89 | char m_class[8]; /* e.g. "nfsd" */ |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 90 | struct in6_addr m_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | struct unix_domain *m_client; |
| 92 | int m_add_change; |
| 93 | }; |
| 94 | static struct cache_head *ip_table[IP_HASHMAX]; |
| 95 | |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 96 | static void ip_map_put(struct kref *kref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 98 | struct cache_head *item = container_of(kref, struct cache_head, ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | struct ip_map *im = container_of(item, struct ip_map,h); |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 100 | |
| 101 | if (test_bit(CACHE_VALID, &item->flags) && |
| 102 | !test_bit(CACHE_NEGATIVE, &item->flags)) |
| 103 | auth_domain_put(&im->m_client->h); |
| 104 | kfree(im); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
NeilBrown | 1f1e030 | 2006-01-06 00:09:49 -0800 | [diff] [blame] | 107 | #if IP_HASHBITS == 8 |
| 108 | /* hash_long on a 64 bit machine is currently REALLY BAD for |
| 109 | * IP addresses in reverse-endian (i.e. on a little-endian machine). |
| 110 | * So use a trivial but reliable hash instead |
| 111 | */ |
Al Viro | 4806126 | 2006-11-08 00:22:34 -0800 | [diff] [blame] | 112 | static inline int hash_ip(__be32 ip) |
NeilBrown | 1f1e030 | 2006-01-06 00:09:49 -0800 | [diff] [blame] | 113 | { |
Al Viro | 4806126 | 2006-11-08 00:22:34 -0800 | [diff] [blame] | 114 | int hash = (__force u32)ip ^ ((__force u32)ip>>16); |
NeilBrown | 1f1e030 | 2006-01-06 00:09:49 -0800 | [diff] [blame] | 115 | return (hash ^ (hash>>8)) & 0xff; |
| 116 | } |
| 117 | #endif |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 118 | static inline int hash_ip6(struct in6_addr ip) |
| 119 | { |
| 120 | return (hash_ip(ip.s6_addr32[0]) ^ |
| 121 | hash_ip(ip.s6_addr32[1]) ^ |
| 122 | hash_ip(ip.s6_addr32[2]) ^ |
| 123 | hash_ip(ip.s6_addr32[3])); |
| 124 | } |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 125 | static int ip_map_match(struct cache_head *corig, struct cache_head *cnew) |
| 126 | { |
| 127 | struct ip_map *orig = container_of(corig, struct ip_map, h); |
| 128 | struct ip_map *new = container_of(cnew, struct ip_map, h); |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 129 | return strcmp(orig->m_class, new->m_class) == 0 && |
| 130 | ipv6_addr_equal(&orig->m_addr, &new->m_addr); |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 131 | } |
| 132 | static void ip_map_init(struct cache_head *cnew, struct cache_head *citem) |
| 133 | { |
| 134 | struct ip_map *new = container_of(cnew, struct ip_map, h); |
| 135 | struct ip_map *item = container_of(citem, struct ip_map, h); |
NeilBrown | 1f1e030 | 2006-01-06 00:09:49 -0800 | [diff] [blame] | 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | strcpy(new->m_class, item->m_class); |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 138 | ipv6_addr_copy(&new->m_addr, &item->m_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 140 | static void update(struct cache_head *cnew, struct cache_head *citem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 142 | struct ip_map *new = container_of(cnew, struct ip_map, h); |
| 143 | struct ip_map *item = container_of(citem, struct ip_map, h); |
| 144 | |
NeilBrown | efc36aa | 2006-03-27 01:14:59 -0800 | [diff] [blame] | 145 | kref_get(&item->m_client->h.ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | new->m_client = item->m_client; |
| 147 | new->m_add_change = item->m_add_change; |
| 148 | } |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 149 | static struct cache_head *ip_map_alloc(void) |
| 150 | { |
| 151 | struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL); |
| 152 | if (i) |
| 153 | return &i->h; |
| 154 | else |
| 155 | return NULL; |
| 156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | static void ip_map_request(struct cache_detail *cd, |
| 159 | struct cache_head *h, |
| 160 | char **bpp, int *blen) |
| 161 | { |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 162 | char text_addr[40]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | struct ip_map *im = container_of(h, struct ip_map, h); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 164 | |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 165 | if (ipv6_addr_v4mapped(&(im->m_addr))) { |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 166 | snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]); |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 167 | } else { |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 168 | snprintf(text_addr, 40, "%pI6", &im->m_addr); |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | qword_add(bpp, blen, im->m_class); |
| 171 | qword_add(bpp, blen, text_addr); |
| 172 | (*bpp)[-1] = '\n'; |
| 173 | } |
| 174 | |
Trond Myklebust | bc74b4f | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 175 | static int ip_map_upcall(struct cache_detail *cd, struct cache_head *h) |
| 176 | { |
| 177 | return sunrpc_cache_pipe_upcall(cd, h, ip_map_request); |
| 178 | } |
| 179 | |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 180 | static struct ip_map *ip_map_lookup(char *class, struct in6_addr *addr); |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 181 | static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
| 183 | static int ip_map_parse(struct cache_detail *cd, |
| 184 | char *mesg, int mlen) |
| 185 | { |
| 186 | /* class ipaddress [domainname] */ |
| 187 | /* should be safe just to use the start of the input buffer |
| 188 | * for scratch: */ |
| 189 | char *buf = mesg; |
| 190 | int len; |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 191 | char class[8]; |
Chuck Lever | 0739605 | 2010-01-26 14:03:47 -0500 | [diff] [blame] | 192 | union { |
| 193 | struct sockaddr sa; |
| 194 | struct sockaddr_in s4; |
| 195 | struct sockaddr_in6 s6; |
| 196 | } address; |
| 197 | struct sockaddr_in6 sin6; |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 198 | int err; |
| 199 | |
| 200 | struct ip_map *ipmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | struct auth_domain *dom; |
| 202 | time_t expiry; |
| 203 | |
| 204 | if (mesg[mlen-1] != '\n') |
| 205 | return -EINVAL; |
| 206 | mesg[mlen-1] = 0; |
| 207 | |
| 208 | /* class */ |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 209 | len = qword_get(&mesg, class, sizeof(class)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | if (len <= 0) return -EINVAL; |
| 211 | |
| 212 | /* ip address */ |
| 213 | len = qword_get(&mesg, buf, mlen); |
| 214 | if (len <= 0) return -EINVAL; |
| 215 | |
Chuck Lever | 0739605 | 2010-01-26 14:03:47 -0500 | [diff] [blame] | 216 | if (rpc_pton(buf, len, &address.sa, sizeof(address)) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return -EINVAL; |
Chuck Lever | 0739605 | 2010-01-26 14:03:47 -0500 | [diff] [blame] | 218 | switch (address.sa.sa_family) { |
| 219 | case AF_INET: |
| 220 | /* Form a mapped IPv4 address in sin6 */ |
| 221 | memset(&sin6, 0, sizeof(sin6)); |
| 222 | sin6.sin6_family = AF_INET6; |
| 223 | sin6.sin6_addr.s6_addr32[2] = htonl(0xffff); |
| 224 | sin6.sin6_addr.s6_addr32[3] = address.s4.sin_addr.s_addr; |
| 225 | break; |
| 226 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 227 | case AF_INET6: |
| 228 | memcpy(&sin6, &address.s6, sizeof(sin6)); |
| 229 | break; |
| 230 | #endif |
| 231 | default: |
| 232 | return -EINVAL; |
| 233 | } |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | expiry = get_expiry(&mesg); |
| 236 | if (expiry ==0) |
| 237 | return -EINVAL; |
| 238 | |
| 239 | /* domainname, or empty for NEGATIVE */ |
| 240 | len = qword_get(&mesg, buf, mlen); |
| 241 | if (len < 0) return -EINVAL; |
| 242 | |
| 243 | if (len) { |
| 244 | dom = unix_domain_find(buf); |
| 245 | if (dom == NULL) |
| 246 | return -ENOENT; |
| 247 | } else |
| 248 | dom = NULL; |
| 249 | |
Chuck Lever | 0739605 | 2010-01-26 14:03:47 -0500 | [diff] [blame] | 250 | /* IPv6 scope IDs are ignored for now */ |
| 251 | ipmp = ip_map_lookup(class, &sin6.sin6_addr); |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 252 | if (ipmp) { |
| 253 | err = ip_map_update(ipmp, |
| 254 | container_of(dom, struct unix_domain, h), |
| 255 | expiry); |
| 256 | } else |
| 257 | err = -ENOMEM; |
| 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | if (dom) |
| 260 | auth_domain_put(dom); |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | cache_flush(); |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 263 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static int ip_map_show(struct seq_file *m, |
| 267 | struct cache_detail *cd, |
| 268 | struct cache_head *h) |
| 269 | { |
| 270 | struct ip_map *im; |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 271 | struct in6_addr addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | char *dom = "-no-domain-"; |
| 273 | |
| 274 | if (h == NULL) { |
| 275 | seq_puts(m, "#class IP domain\n"); |
| 276 | return 0; |
| 277 | } |
| 278 | im = container_of(h, struct ip_map, h); |
| 279 | /* class addr domain */ |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 280 | ipv6_addr_copy(&addr, &im->m_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 282 | if (test_bit(CACHE_VALID, &h->flags) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | !test_bit(CACHE_NEGATIVE, &h->flags)) |
| 284 | dom = im->m_client->h.name; |
| 285 | |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 286 | if (ipv6_addr_v4mapped(&addr)) { |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 287 | seq_printf(m, "%s %pI4 %s\n", |
| 288 | im->m_class, &addr.s6_addr32[3], dom); |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 289 | } else { |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 290 | seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom); |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 291 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | return 0; |
| 293 | } |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | struct cache_detail ip_map_cache = { |
Bruce Allan | f35279d | 2005-09-06 15:17:08 -0700 | [diff] [blame] | 297 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | .hash_size = IP_HASHMAX, |
| 299 | .hash_table = ip_table, |
| 300 | .name = "auth.unix.ip", |
| 301 | .cache_put = ip_map_put, |
Trond Myklebust | bc74b4f | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 302 | .cache_upcall = ip_map_upcall, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | .cache_parse = ip_map_parse, |
| 304 | .cache_show = ip_map_show, |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 305 | .match = ip_map_match, |
| 306 | .init = ip_map_init, |
| 307 | .update = update, |
| 308 | .alloc = ip_map_alloc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | }; |
| 310 | |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 311 | static struct ip_map *ip_map_lookup(char *class, struct in6_addr *addr) |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 312 | { |
| 313 | struct ip_map ip; |
| 314 | struct cache_head *ch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 316 | strcpy(ip.m_class, class); |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 317 | ipv6_addr_copy(&ip.m_addr, addr); |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 318 | ch = sunrpc_cache_lookup(&ip_map_cache, &ip.h, |
| 319 | hash_str(class, IP_HASHBITS) ^ |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 320 | hash_ip6(*addr)); |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 321 | |
| 322 | if (ch) |
| 323 | return container_of(ch, struct ip_map, h); |
| 324 | else |
| 325 | return NULL; |
| 326 | } |
| 327 | |
| 328 | static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry) |
| 329 | { |
| 330 | struct ip_map ip; |
| 331 | struct cache_head *ch; |
| 332 | |
| 333 | ip.m_client = udom; |
| 334 | ip.h.flags = 0; |
| 335 | if (!udom) |
| 336 | set_bit(CACHE_NEGATIVE, &ip.h.flags); |
| 337 | else { |
| 338 | ip.m_add_change = udom->addr_changes; |
| 339 | /* if this is from the legacy set_client system call, |
| 340 | * we need m_add_change to be one higher |
| 341 | */ |
| 342 | if (expiry == NEVER) |
| 343 | ip.m_add_change++; |
| 344 | } |
| 345 | ip.h.expiry_time = expiry; |
| 346 | ch = sunrpc_cache_update(&ip_map_cache, |
| 347 | &ip.h, &ipm->h, |
| 348 | hash_str(ipm->m_class, IP_HASHBITS) ^ |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 349 | hash_ip6(ipm->m_addr)); |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 350 | if (!ch) |
| 351 | return -ENOMEM; |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 352 | cache_put(ch, &ip_map_cache); |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 353 | return 0; |
| 354 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 356 | int auth_unix_add_addr(struct in6_addr *addr, struct auth_domain *dom) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | { |
| 358 | struct unix_domain *udom; |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 359 | struct ip_map *ipmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
NeilBrown | efc36aa | 2006-03-27 01:14:59 -0800 | [diff] [blame] | 361 | if (dom->flavour != &svcauth_unix) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | return -EINVAL; |
| 363 | udom = container_of(dom, struct unix_domain, h); |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 364 | ipmp = ip_map_lookup("nfsd", addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 366 | if (ipmp) |
| 367 | return ip_map_update(ipmp, udom, NEVER); |
| 368 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | return -ENOMEM; |
| 370 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 371 | EXPORT_SYMBOL_GPL(auth_unix_add_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | int auth_unix_forget_old(struct auth_domain *dom) |
| 374 | { |
| 375 | struct unix_domain *udom; |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 376 | |
NeilBrown | efc36aa | 2006-03-27 01:14:59 -0800 | [diff] [blame] | 377 | if (dom->flavour != &svcauth_unix) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | return -EINVAL; |
| 379 | udom = container_of(dom, struct unix_domain, h); |
| 380 | udom->addr_changes++; |
| 381 | return 0; |
| 382 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 383 | EXPORT_SYMBOL_GPL(auth_unix_forget_old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 385 | struct auth_domain *auth_unix_lookup(struct in6_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
Greg Banks | 40f1052 | 2006-10-02 02:17:43 -0700 | [diff] [blame] | 387 | struct ip_map *ipm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | struct auth_domain *rv; |
| 389 | |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 390 | ipm = ip_map_lookup("nfsd", addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | if (!ipm) |
| 393 | return NULL; |
| 394 | if (cache_check(&ip_map_cache, &ipm->h, NULL)) |
| 395 | return NULL; |
| 396 | |
| 397 | if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) { |
| 398 | if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0) |
| 399 | auth_domain_put(&ipm->m_client->h); |
| 400 | rv = NULL; |
| 401 | } else { |
| 402 | rv = &ipm->m_client->h; |
NeilBrown | efc36aa | 2006-03-27 01:14:59 -0800 | [diff] [blame] | 403 | kref_get(&rv->ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 405 | cache_put(&ipm->h, &ip_map_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | return rv; |
| 407 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 408 | EXPORT_SYMBOL_GPL(auth_unix_lookup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
| 410 | void svcauth_unix_purge(void) |
| 411 | { |
| 412 | cache_purge(&ip_map_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 414 | EXPORT_SYMBOL_GPL(svcauth_unix_purge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
Greg Banks | 7b2b1fe | 2006-10-04 02:15:50 -0700 | [diff] [blame] | 416 | static inline struct ip_map * |
| 417 | ip_map_cached_get(struct svc_rqst *rqstp) |
| 418 | { |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame] | 419 | struct ip_map *ipm = NULL; |
| 420 | struct svc_xprt *xprt = rqstp->rq_xprt; |
| 421 | |
| 422 | if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) { |
| 423 | spin_lock(&xprt->xpt_lock); |
| 424 | ipm = xprt->xpt_auth_cache; |
| 425 | if (ipm != NULL) { |
| 426 | if (!cache_valid(&ipm->h)) { |
| 427 | /* |
| 428 | * The entry has been invalidated since it was |
| 429 | * remembered, e.g. by a second mount from the |
| 430 | * same IP address. |
| 431 | */ |
| 432 | xprt->xpt_auth_cache = NULL; |
| 433 | spin_unlock(&xprt->xpt_lock); |
| 434 | cache_put(&ipm->h, &ip_map_cache); |
| 435 | return NULL; |
| 436 | } |
| 437 | cache_get(&ipm->h); |
Greg Banks | 7b2b1fe | 2006-10-04 02:15:50 -0700 | [diff] [blame] | 438 | } |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame] | 439 | spin_unlock(&xprt->xpt_lock); |
Greg Banks | 7b2b1fe | 2006-10-04 02:15:50 -0700 | [diff] [blame] | 440 | } |
| 441 | return ipm; |
| 442 | } |
| 443 | |
| 444 | static inline void |
| 445 | ip_map_cached_put(struct svc_rqst *rqstp, struct ip_map *ipm) |
| 446 | { |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame] | 447 | struct svc_xprt *xprt = rqstp->rq_xprt; |
Greg Banks | 7b2b1fe | 2006-10-04 02:15:50 -0700 | [diff] [blame] | 448 | |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame] | 449 | if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) { |
| 450 | spin_lock(&xprt->xpt_lock); |
| 451 | if (xprt->xpt_auth_cache == NULL) { |
| 452 | /* newly cached, keep the reference */ |
| 453 | xprt->xpt_auth_cache = ipm; |
| 454 | ipm = NULL; |
| 455 | } |
| 456 | spin_unlock(&xprt->xpt_lock); |
NeilBrown | 30f3dee | 2007-04-16 22:53:25 -0700 | [diff] [blame] | 457 | } |
NeilBrown | 30f3dee | 2007-04-16 22:53:25 -0700 | [diff] [blame] | 458 | if (ipm) |
Greg Banks | 7b2b1fe | 2006-10-04 02:15:50 -0700 | [diff] [blame] | 459 | cache_put(&ipm->h, &ip_map_cache); |
| 460 | } |
| 461 | |
| 462 | void |
| 463 | svcauth_unix_info_release(void *info) |
| 464 | { |
| 465 | struct ip_map *ipm = info; |
| 466 | cache_put(&ipm->h, &ip_map_cache); |
| 467 | } |
| 468 | |
NeilBrown | 3fc605a | 2007-02-14 00:33:13 -0800 | [diff] [blame] | 469 | /**************************************************************************** |
| 470 | * auth.unix.gid cache |
| 471 | * simple cache to map a UID to a list of GIDs |
| 472 | * because AUTH_UNIX aka AUTH_SYS has a max of 16 |
| 473 | */ |
| 474 | #define GID_HASHBITS 8 |
| 475 | #define GID_HASHMAX (1<<GID_HASHBITS) |
| 476 | #define GID_HASHMASK (GID_HASHMAX - 1) |
| 477 | |
| 478 | struct unix_gid { |
| 479 | struct cache_head h; |
| 480 | uid_t uid; |
| 481 | struct group_info *gi; |
| 482 | }; |
| 483 | static struct cache_head *gid_table[GID_HASHMAX]; |
| 484 | |
| 485 | static void unix_gid_put(struct kref *kref) |
| 486 | { |
| 487 | struct cache_head *item = container_of(kref, struct cache_head, ref); |
| 488 | struct unix_gid *ug = container_of(item, struct unix_gid, h); |
| 489 | if (test_bit(CACHE_VALID, &item->flags) && |
| 490 | !test_bit(CACHE_NEGATIVE, &item->flags)) |
| 491 | put_group_info(ug->gi); |
| 492 | kfree(ug); |
| 493 | } |
| 494 | |
| 495 | static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew) |
| 496 | { |
| 497 | struct unix_gid *orig = container_of(corig, struct unix_gid, h); |
| 498 | struct unix_gid *new = container_of(cnew, struct unix_gid, h); |
| 499 | return orig->uid == new->uid; |
| 500 | } |
| 501 | static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem) |
| 502 | { |
| 503 | struct unix_gid *new = container_of(cnew, struct unix_gid, h); |
| 504 | struct unix_gid *item = container_of(citem, struct unix_gid, h); |
| 505 | new->uid = item->uid; |
| 506 | } |
| 507 | static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem) |
| 508 | { |
| 509 | struct unix_gid *new = container_of(cnew, struct unix_gid, h); |
| 510 | struct unix_gid *item = container_of(citem, struct unix_gid, h); |
| 511 | |
| 512 | get_group_info(item->gi); |
| 513 | new->gi = item->gi; |
| 514 | } |
| 515 | static struct cache_head *unix_gid_alloc(void) |
| 516 | { |
| 517 | struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL); |
| 518 | if (g) |
| 519 | return &g->h; |
| 520 | else |
| 521 | return NULL; |
| 522 | } |
| 523 | |
| 524 | static void unix_gid_request(struct cache_detail *cd, |
| 525 | struct cache_head *h, |
| 526 | char **bpp, int *blen) |
| 527 | { |
| 528 | char tuid[20]; |
| 529 | struct unix_gid *ug = container_of(h, struct unix_gid, h); |
| 530 | |
| 531 | snprintf(tuid, 20, "%u", ug->uid); |
| 532 | qword_add(bpp, blen, tuid); |
| 533 | (*bpp)[-1] = '\n'; |
| 534 | } |
| 535 | |
Trond Myklebust | bc74b4f | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 536 | static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h) |
| 537 | { |
| 538 | return sunrpc_cache_pipe_upcall(cd, h, unix_gid_request); |
| 539 | } |
| 540 | |
NeilBrown | 3fc605a | 2007-02-14 00:33:13 -0800 | [diff] [blame] | 541 | static struct unix_gid *unix_gid_lookup(uid_t uid); |
| 542 | extern struct cache_detail unix_gid_cache; |
| 543 | |
| 544 | static int unix_gid_parse(struct cache_detail *cd, |
| 545 | char *mesg, int mlen) |
| 546 | { |
| 547 | /* uid expiry Ngid gid0 gid1 ... gidN-1 */ |
| 548 | int uid; |
| 549 | int gids; |
| 550 | int rv; |
| 551 | int i; |
| 552 | int err; |
| 553 | time_t expiry; |
| 554 | struct unix_gid ug, *ugp; |
| 555 | |
| 556 | if (mlen <= 0 || mesg[mlen-1] != '\n') |
| 557 | return -EINVAL; |
| 558 | mesg[mlen-1] = 0; |
| 559 | |
| 560 | rv = get_int(&mesg, &uid); |
| 561 | if (rv) |
| 562 | return -EINVAL; |
| 563 | ug.uid = uid; |
| 564 | |
| 565 | expiry = get_expiry(&mesg); |
| 566 | if (expiry == 0) |
| 567 | return -EINVAL; |
| 568 | |
| 569 | rv = get_int(&mesg, &gids); |
| 570 | if (rv || gids < 0 || gids > 8192) |
| 571 | return -EINVAL; |
| 572 | |
| 573 | ug.gi = groups_alloc(gids); |
| 574 | if (!ug.gi) |
| 575 | return -ENOMEM; |
| 576 | |
| 577 | for (i = 0 ; i < gids ; i++) { |
| 578 | int gid; |
| 579 | rv = get_int(&mesg, &gid); |
| 580 | err = -EINVAL; |
| 581 | if (rv) |
| 582 | goto out; |
| 583 | GROUP_AT(ug.gi, i) = gid; |
| 584 | } |
| 585 | |
| 586 | ugp = unix_gid_lookup(uid); |
| 587 | if (ugp) { |
| 588 | struct cache_head *ch; |
| 589 | ug.h.flags = 0; |
| 590 | ug.h.expiry_time = expiry; |
| 591 | ch = sunrpc_cache_update(&unix_gid_cache, |
| 592 | &ug.h, &ugp->h, |
| 593 | hash_long(uid, GID_HASHBITS)); |
| 594 | if (!ch) |
| 595 | err = -ENOMEM; |
| 596 | else { |
| 597 | err = 0; |
| 598 | cache_put(ch, &unix_gid_cache); |
| 599 | } |
| 600 | } else |
| 601 | err = -ENOMEM; |
| 602 | out: |
| 603 | if (ug.gi) |
| 604 | put_group_info(ug.gi); |
| 605 | return err; |
| 606 | } |
| 607 | |
| 608 | static int unix_gid_show(struct seq_file *m, |
| 609 | struct cache_detail *cd, |
| 610 | struct cache_head *h) |
| 611 | { |
| 612 | struct unix_gid *ug; |
| 613 | int i; |
| 614 | int glen; |
| 615 | |
| 616 | if (h == NULL) { |
| 617 | seq_puts(m, "#uid cnt: gids...\n"); |
| 618 | return 0; |
| 619 | } |
| 620 | ug = container_of(h, struct unix_gid, h); |
| 621 | if (test_bit(CACHE_VALID, &h->flags) && |
| 622 | !test_bit(CACHE_NEGATIVE, &h->flags)) |
| 623 | glen = ug->gi->ngroups; |
| 624 | else |
| 625 | glen = 0; |
| 626 | |
J. Bruce Fields | ccdb357 | 2010-03-02 15:49:21 -0500 | [diff] [blame] | 627 | seq_printf(m, "%u %d:", ug->uid, glen); |
NeilBrown | 3fc605a | 2007-02-14 00:33:13 -0800 | [diff] [blame] | 628 | for (i = 0; i < glen; i++) |
| 629 | seq_printf(m, " %d", GROUP_AT(ug->gi, i)); |
| 630 | seq_printf(m, "\n"); |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | struct cache_detail unix_gid_cache = { |
| 635 | .owner = THIS_MODULE, |
| 636 | .hash_size = GID_HASHMAX, |
| 637 | .hash_table = gid_table, |
| 638 | .name = "auth.unix.gid", |
| 639 | .cache_put = unix_gid_put, |
Trond Myklebust | bc74b4f | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 640 | .cache_upcall = unix_gid_upcall, |
NeilBrown | 3fc605a | 2007-02-14 00:33:13 -0800 | [diff] [blame] | 641 | .cache_parse = unix_gid_parse, |
| 642 | .cache_show = unix_gid_show, |
| 643 | .match = unix_gid_match, |
| 644 | .init = unix_gid_init, |
| 645 | .update = unix_gid_update, |
| 646 | .alloc = unix_gid_alloc, |
| 647 | }; |
| 648 | |
| 649 | static struct unix_gid *unix_gid_lookup(uid_t uid) |
| 650 | { |
| 651 | struct unix_gid ug; |
| 652 | struct cache_head *ch; |
| 653 | |
| 654 | ug.uid = uid; |
| 655 | ch = sunrpc_cache_lookup(&unix_gid_cache, &ug.h, |
| 656 | hash_long(uid, GID_HASHBITS)); |
| 657 | if (ch) |
| 658 | return container_of(ch, struct unix_gid, h); |
| 659 | else |
| 660 | return NULL; |
| 661 | } |
| 662 | |
J. Bruce Fields | dc83d6e | 2009-10-20 18:51:34 -0400 | [diff] [blame] | 663 | static struct group_info *unix_gid_find(uid_t uid, struct svc_rqst *rqstp) |
NeilBrown | 3fc605a | 2007-02-14 00:33:13 -0800 | [diff] [blame] | 664 | { |
J. Bruce Fields | dc83d6e | 2009-10-20 18:51:34 -0400 | [diff] [blame] | 665 | struct unix_gid *ug; |
| 666 | struct group_info *gi; |
| 667 | int ret; |
| 668 | |
| 669 | ug = unix_gid_lookup(uid); |
NeilBrown | 3fc605a | 2007-02-14 00:33:13 -0800 | [diff] [blame] | 670 | if (!ug) |
J. Bruce Fields | dc83d6e | 2009-10-20 18:51:34 -0400 | [diff] [blame] | 671 | return ERR_PTR(-EAGAIN); |
| 672 | ret = cache_check(&unix_gid_cache, &ug->h, &rqstp->rq_chandle); |
| 673 | switch (ret) { |
NeilBrown | 3fc605a | 2007-02-14 00:33:13 -0800 | [diff] [blame] | 674 | case -ENOENT: |
J. Bruce Fields | dc83d6e | 2009-10-20 18:51:34 -0400 | [diff] [blame] | 675 | return ERR_PTR(-ENOENT); |
NeilBrown | 3fc605a | 2007-02-14 00:33:13 -0800 | [diff] [blame] | 676 | case 0: |
J. Bruce Fields | dc83d6e | 2009-10-20 18:51:34 -0400 | [diff] [blame] | 677 | gi = get_group_info(ug->gi); |
NeilBrown | 560ab42 | 2009-08-04 15:22:39 +1000 | [diff] [blame] | 678 | cache_put(&ug->h, &unix_gid_cache); |
J. Bruce Fields | dc83d6e | 2009-10-20 18:51:34 -0400 | [diff] [blame] | 679 | return gi; |
NeilBrown | 3fc605a | 2007-02-14 00:33:13 -0800 | [diff] [blame] | 680 | default: |
J. Bruce Fields | dc83d6e | 2009-10-20 18:51:34 -0400 | [diff] [blame] | 681 | return ERR_PTR(-EAGAIN); |
NeilBrown | 3fc605a | 2007-02-14 00:33:13 -0800 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
J. Bruce Fields | 3ab4d8b | 2007-07-17 04:04:46 -0700 | [diff] [blame] | 685 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | svcauth_unix_set_client(struct svc_rqst *rqstp) |
| 687 | { |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 688 | struct sockaddr_in *sin; |
| 689 | struct sockaddr_in6 *sin6, sin6_storage; |
NeilBrown | 1a9917c | 2006-03-27 01:15:02 -0800 | [diff] [blame] | 690 | struct ip_map *ipm; |
J. Bruce Fields | dc83d6e | 2009-10-20 18:51:34 -0400 | [diff] [blame] | 691 | struct group_info *gi; |
| 692 | struct svc_cred *cred = &rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 694 | switch (rqstp->rq_addr.ss_family) { |
| 695 | case AF_INET: |
| 696 | sin = svc_addr_in(rqstp); |
| 697 | sin6 = &sin6_storage; |
Brian Haley | b301e82 | 2009-10-07 13:58:25 -0700 | [diff] [blame] | 698 | ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr); |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 699 | break; |
| 700 | case AF_INET6: |
| 701 | sin6 = svc_addr_in6(rqstp); |
| 702 | break; |
| 703 | default: |
| 704 | BUG(); |
| 705 | } |
| 706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | rqstp->rq_client = NULL; |
| 708 | if (rqstp->rq_proc == 0) |
| 709 | return SVC_OK; |
| 710 | |
Greg Banks | 7b2b1fe | 2006-10-04 02:15:50 -0700 | [diff] [blame] | 711 | ipm = ip_map_cached_get(rqstp); |
| 712 | if (ipm == NULL) |
| 713 | ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class, |
Aurélien Charbon | f15364b | 2008-01-18 15:50:56 +0100 | [diff] [blame] | 714 | &sin6->sin6_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | |
| 716 | if (ipm == NULL) |
| 717 | return SVC_DENIED; |
| 718 | |
| 719 | switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) { |
| 720 | default: |
| 721 | BUG(); |
| 722 | case -EAGAIN: |
J.Bruce Fields | e0bb89e | 2006-12-13 00:35:25 -0800 | [diff] [blame] | 723 | case -ETIMEDOUT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | return SVC_DROP; |
| 725 | case -ENOENT: |
| 726 | return SVC_DENIED; |
| 727 | case 0: |
| 728 | rqstp->rq_client = &ipm->m_client->h; |
NeilBrown | efc36aa | 2006-03-27 01:14:59 -0800 | [diff] [blame] | 729 | kref_get(&rqstp->rq_client->ref); |
Greg Banks | 7b2b1fe | 2006-10-04 02:15:50 -0700 | [diff] [blame] | 730 | ip_map_cached_put(rqstp, ipm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | break; |
| 732 | } |
J. Bruce Fields | dc83d6e | 2009-10-20 18:51:34 -0400 | [diff] [blame] | 733 | |
| 734 | gi = unix_gid_find(cred->cr_uid, rqstp); |
| 735 | switch (PTR_ERR(gi)) { |
| 736 | case -EAGAIN: |
| 737 | return SVC_DROP; |
| 738 | case -ENOENT: |
| 739 | break; |
| 740 | default: |
| 741 | put_group_info(cred->cr_group_info); |
| 742 | cred->cr_group_info = gi; |
| 743 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | return SVC_OK; |
| 745 | } |
| 746 | |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 747 | EXPORT_SYMBOL_GPL(svcauth_unix_set_client); |
J. Bruce Fields | 3ab4d8b | 2007-07-17 04:04:46 -0700 | [diff] [blame] | 748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | static int |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 750 | svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | { |
| 752 | struct kvec *argv = &rqstp->rq_arg.head[0]; |
| 753 | struct kvec *resv = &rqstp->rq_res.head[0]; |
| 754 | struct svc_cred *cred = &rqstp->rq_cred; |
| 755 | |
| 756 | cred->cr_group_info = NULL; |
| 757 | rqstp->rq_client = NULL; |
| 758 | |
| 759 | if (argv->iov_len < 3*4) |
| 760 | return SVC_GARBAGE; |
| 761 | |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 762 | if (svc_getu32(argv) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | dprintk("svc: bad null cred\n"); |
| 764 | *authp = rpc_autherr_badcred; |
| 765 | return SVC_DENIED; |
| 766 | } |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 767 | if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | dprintk("svc: bad null verf\n"); |
| 769 | *authp = rpc_autherr_badverf; |
| 770 | return SVC_DENIED; |
| 771 | } |
| 772 | |
| 773 | /* Signal that mapping to nobody uid/gid is required */ |
| 774 | cred->cr_uid = (uid_t) -1; |
| 775 | cred->cr_gid = (gid_t) -1; |
| 776 | cred->cr_group_info = groups_alloc(0); |
| 777 | if (cred->cr_group_info == NULL) |
| 778 | return SVC_DROP; /* kmalloc failure - client must retry */ |
| 779 | |
| 780 | /* Put NULL verifier */ |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 781 | svc_putnl(resv, RPC_AUTH_NULL); |
| 782 | svc_putnl(resv, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Andy Adamson | c417058 | 2007-07-17 04:04:42 -0700 | [diff] [blame] | 784 | rqstp->rq_flavor = RPC_AUTH_NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | return SVC_OK; |
| 786 | } |
| 787 | |
| 788 | static int |
| 789 | svcauth_null_release(struct svc_rqst *rqstp) |
| 790 | { |
| 791 | if (rqstp->rq_client) |
| 792 | auth_domain_put(rqstp->rq_client); |
| 793 | rqstp->rq_client = NULL; |
| 794 | if (rqstp->rq_cred.cr_group_info) |
| 795 | put_group_info(rqstp->rq_cred.cr_group_info); |
| 796 | rqstp->rq_cred.cr_group_info = NULL; |
| 797 | |
| 798 | return 0; /* don't drop */ |
| 799 | } |
| 800 | |
| 801 | |
| 802 | struct auth_ops svcauth_null = { |
| 803 | .name = "null", |
| 804 | .owner = THIS_MODULE, |
| 805 | .flavour = RPC_AUTH_NULL, |
| 806 | .accept = svcauth_null_accept, |
| 807 | .release = svcauth_null_release, |
| 808 | .set_client = svcauth_unix_set_client, |
| 809 | }; |
| 810 | |
| 811 | |
| 812 | static int |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 813 | svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | { |
| 815 | struct kvec *argv = &rqstp->rq_arg.head[0]; |
| 816 | struct kvec *resv = &rqstp->rq_res.head[0]; |
| 817 | struct svc_cred *cred = &rqstp->rq_cred; |
| 818 | u32 slen, i; |
| 819 | int len = argv->iov_len; |
| 820 | |
| 821 | cred->cr_group_info = NULL; |
| 822 | rqstp->rq_client = NULL; |
| 823 | |
| 824 | if ((len -= 3*4) < 0) |
| 825 | return SVC_GARBAGE; |
| 826 | |
| 827 | svc_getu32(argv); /* length */ |
| 828 | svc_getu32(argv); /* time stamp */ |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 829 | slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | if (slen > 64 || (len -= (slen + 3)*4) < 0) |
| 831 | goto badcred; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 832 | argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | argv->iov_len -= slen*4; |
| 834 | |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 835 | cred->cr_uid = svc_getnl(argv); /* uid */ |
| 836 | cred->cr_gid = svc_getnl(argv); /* gid */ |
| 837 | slen = svc_getnl(argv); /* gids length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | if (slen > 16 || (len -= (slen + 2)*4) < 0) |
| 839 | goto badcred; |
J. Bruce Fields | dc83d6e | 2009-10-20 18:51:34 -0400 | [diff] [blame] | 840 | cred->cr_group_info = groups_alloc(slen); |
| 841 | if (cred->cr_group_info == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | return SVC_DROP; |
J. Bruce Fields | dc83d6e | 2009-10-20 18:51:34 -0400 | [diff] [blame] | 843 | for (i = 0; i < slen; i++) |
| 844 | GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv); |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 845 | if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | *authp = rpc_autherr_badverf; |
| 847 | return SVC_DENIED; |
| 848 | } |
| 849 | |
| 850 | /* Put NULL verifier */ |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 851 | svc_putnl(resv, RPC_AUTH_NULL); |
| 852 | svc_putnl(resv, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | |
Andy Adamson | c417058 | 2007-07-17 04:04:42 -0700 | [diff] [blame] | 854 | rqstp->rq_flavor = RPC_AUTH_UNIX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | return SVC_OK; |
| 856 | |
| 857 | badcred: |
| 858 | *authp = rpc_autherr_badcred; |
| 859 | return SVC_DENIED; |
| 860 | } |
| 861 | |
| 862 | static int |
| 863 | svcauth_unix_release(struct svc_rqst *rqstp) |
| 864 | { |
| 865 | /* Verifier (such as it is) is already in place. |
| 866 | */ |
| 867 | if (rqstp->rq_client) |
| 868 | auth_domain_put(rqstp->rq_client); |
| 869 | rqstp->rq_client = NULL; |
| 870 | if (rqstp->rq_cred.cr_group_info) |
| 871 | put_group_info(rqstp->rq_cred.cr_group_info); |
| 872 | rqstp->rq_cred.cr_group_info = NULL; |
| 873 | |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | |
| 878 | struct auth_ops svcauth_unix = { |
| 879 | .name = "unix", |
| 880 | .owner = THIS_MODULE, |
| 881 | .flavour = RPC_AUTH_UNIX, |
| 882 | .accept = svcauth_unix_accept, |
| 883 | .release = svcauth_unix_release, |
| 884 | .domain_release = svcauth_unix_domain_release, |
| 885 | .set_client = svcauth_unix_set_client, |
| 886 | }; |
| 887 | |