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