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>
9 #include <linux/seq_file.h>
10 #include <linux/hash.h>
11 #include <linux/string.h>
13 #define RPCDBG_FACILITY RPCDBG_AUTH
17 * AUTHUNIX and AUTHNULL credentials are both handled here.
18 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
19 * are always nobody (-2). i.e. we do the same IP address checks for
20 * AUTHNULL as for AUTHUNIX, and that is done here.
27 /* other stuff later */
30 extern struct auth_ops svcauth_unix;
32 struct auth_domain *unix_domain_find(char *name)
34 struct auth_domain *rv;
35 struct unix_domain *new = NULL;
37 rv = auth_domain_lookup(name, NULL);
40 if (new) auth_domain_put(&new->h);
43 if (rv && rv->flavour != &svcauth_unix) {
50 new = kmalloc(sizeof(*new), GFP_KERNEL);
53 kref_init(&new->h.ref);
54 new->h.name = kstrdup(name, GFP_KERNEL);
55 new->h.flavour = &svcauth_unix;
56 new->addr_changes = 0;
57 rv = auth_domain_lookup(name, &new->h);
61 static void svcauth_unix_domain_release(struct auth_domain *dom)
63 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
70 /**************************************************
71 * cache for IP address to unix_domain
72 * as needed by AUTH_UNIX
75 #define IP_HASHMAX (1<<IP_HASHBITS)
76 #define IP_HASHMASK (IP_HASHMAX-1)
80 char m_class[8]; /* e.g. "nfsd" */
81 struct in_addr m_addr;
82 struct unix_domain *m_client;
85 static struct cache_head *ip_table[IP_HASHMAX];
87 static void ip_map_put(struct cache_head *item, struct cache_detail *cd)
89 struct ip_map *im = container_of(item, struct ip_map,h);
90 if (cache_put(item, cd)) {
91 if (test_bit(CACHE_VALID, &item->flags) &&
92 !test_bit(CACHE_NEGATIVE, &item->flags))
93 auth_domain_put(&im->m_client->h);
99 /* hash_long on a 64 bit machine is currently REALLY BAD for
100 * IP addresses in reverse-endian (i.e. on a little-endian machine).
101 * So use a trivial but reliable hash instead
103 static inline int hash_ip(unsigned long ip)
105 int hash = ip ^ (ip>>16);
106 return (hash ^ (hash>>8)) & 0xff;
110 static inline int ip_map_hash(struct ip_map *item)
112 return hash_str(item->m_class, IP_HASHBITS) ^
113 hash_ip((unsigned long)item->m_addr.s_addr);
115 static inline int ip_map_match(struct ip_map *item, struct ip_map *tmp)
117 return strcmp(tmp->m_class, item->m_class) == 0
118 && tmp->m_addr.s_addr == item->m_addr.s_addr;
120 static inline void ip_map_init(struct ip_map *new, struct ip_map *item)
122 strcpy(new->m_class, item->m_class);
123 new->m_addr.s_addr = item->m_addr.s_addr;
125 static inline void ip_map_update(struct ip_map *new, struct ip_map *item)
127 kref_get(&item->m_client->h.ref);
128 new->m_client = item->m_client;
129 new->m_add_change = item->m_add_change;
132 static void ip_map_request(struct cache_detail *cd,
133 struct cache_head *h,
134 char **bpp, int *blen)
137 struct ip_map *im = container_of(h, struct ip_map, h);
138 __u32 addr = im->m_addr.s_addr;
140 snprintf(text_addr, 20, "%u.%u.%u.%u",
141 ntohl(addr) >> 24 & 0xff,
142 ntohl(addr) >> 16 & 0xff,
143 ntohl(addr) >> 8 & 0xff,
144 ntohl(addr) >> 0 & 0xff);
146 qword_add(bpp, blen, im->m_class);
147 qword_add(bpp, blen, text_addr);
151 static struct ip_map *ip_map_lookup(struct ip_map *, int);
153 static int ip_map_parse(struct cache_detail *cd,
154 char *mesg, int mlen)
156 /* class ipaddress [domainname] */
157 /* should be safe just to use the start of the input buffer
163 struct ip_map ipm, *ipmp;
164 struct auth_domain *dom;
167 if (mesg[mlen-1] != '\n')
172 len = qword_get(&mesg, ipm.m_class, sizeof(ipm.m_class));
173 if (len <= 0) return -EINVAL;
176 len = qword_get(&mesg, buf, mlen);
177 if (len <= 0) return -EINVAL;
179 if (sscanf(buf, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
182 expiry = get_expiry(&mesg);
186 /* domainname, or empty for NEGATIVE */
187 len = qword_get(&mesg, buf, mlen);
188 if (len < 0) return -EINVAL;
191 dom = unix_domain_find(buf);
198 htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
201 ipm.m_client = container_of(dom, struct unix_domain, h);
202 ipm.m_add_change = ipm.m_client->addr_changes;
204 set_bit(CACHE_NEGATIVE, &ipm.h.flags);
205 ipm.h.expiry_time = expiry;
207 ipmp = ip_map_lookup(&ipm, 1);
209 ip_map_put(&ipmp->h, &ip_map_cache);
211 auth_domain_put(dom);
218 static int ip_map_show(struct seq_file *m,
219 struct cache_detail *cd,
220 struct cache_head *h)
224 char *dom = "-no-domain-";
227 seq_puts(m, "#class IP domain\n");
230 im = container_of(h, struct ip_map, h);
231 /* class addr domain */
234 if (test_bit(CACHE_VALID, &h->flags) &&
235 !test_bit(CACHE_NEGATIVE, &h->flags))
236 dom = im->m_client->h.name;
238 seq_printf(m, "%s %d.%d.%d.%d %s\n",
240 htonl(addr.s_addr) >> 24 & 0xff,
241 htonl(addr.s_addr) >> 16 & 0xff,
242 htonl(addr.s_addr) >> 8 & 0xff,
243 htonl(addr.s_addr) >> 0 & 0xff,
250 struct cache_detail ip_map_cache = {
251 .owner = THIS_MODULE,
252 .hash_size = IP_HASHMAX,
253 .hash_table = ip_table,
254 .name = "auth.unix.ip",
255 .cache_put = ip_map_put,
256 .cache_request = ip_map_request,
257 .cache_parse = ip_map_parse,
258 .cache_show = ip_map_show,
261 static DefineSimpleCacheLookup(ip_map, 0)
264 int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
266 struct unix_domain *udom;
267 struct ip_map ip, *ipmp;
269 if (dom->flavour != &svcauth_unix)
271 udom = container_of(dom, struct unix_domain, h);
272 strcpy(ip.m_class, "nfsd");
275 ip.m_add_change = udom->addr_changes+1;
277 ip.h.expiry_time = NEVER;
279 ipmp = ip_map_lookup(&ip, 1);
282 ip_map_put(&ipmp->h, &ip_map_cache);
288 int auth_unix_forget_old(struct auth_domain *dom)
290 struct unix_domain *udom;
292 if (dom->flavour != &svcauth_unix)
294 udom = container_of(dom, struct unix_domain, h);
295 udom->addr_changes++;
299 struct auth_domain *auth_unix_lookup(struct in_addr addr)
301 struct ip_map key, *ipm;
302 struct auth_domain *rv;
304 strcpy(key.m_class, "nfsd");
307 ipm = ip_map_lookup(&key, 0);
311 if (cache_check(&ip_map_cache, &ipm->h, NULL))
314 if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
315 if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
316 auth_domain_put(&ipm->m_client->h);
319 rv = &ipm->m_client->h;
322 ip_map_put(&ipm->h, &ip_map_cache);
326 void svcauth_unix_purge(void)
328 cache_purge(&ip_map_cache);
332 svcauth_unix_set_client(struct svc_rqst *rqstp)
334 struct ip_map key, *ipm;
336 rqstp->rq_client = NULL;
337 if (rqstp->rq_proc == 0)
340 strcpy(key.m_class, rqstp->rq_server->sv_program->pg_class);
341 key.m_addr = rqstp->rq_addr.sin_addr;
343 ipm = ip_map_lookup(&key, 0);
348 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
356 rqstp->rq_client = &ipm->m_client->h;
357 kref_get(&rqstp->rq_client->ref);
358 ip_map_put(&ipm->h, &ip_map_cache);
365 svcauth_null_accept(struct svc_rqst *rqstp, u32 *authp)
367 struct kvec *argv = &rqstp->rq_arg.head[0];
368 struct kvec *resv = &rqstp->rq_res.head[0];
369 struct svc_cred *cred = &rqstp->rq_cred;
371 cred->cr_group_info = NULL;
372 rqstp->rq_client = NULL;
374 if (argv->iov_len < 3*4)
377 if (svc_getu32(argv) != 0) {
378 dprintk("svc: bad null cred\n");
379 *authp = rpc_autherr_badcred;
382 if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
383 dprintk("svc: bad null verf\n");
384 *authp = rpc_autherr_badverf;
388 /* Signal that mapping to nobody uid/gid is required */
389 cred->cr_uid = (uid_t) -1;
390 cred->cr_gid = (gid_t) -1;
391 cred->cr_group_info = groups_alloc(0);
392 if (cred->cr_group_info == NULL)
393 return SVC_DROP; /* kmalloc failure - client must retry */
395 /* Put NULL verifier */
396 svc_putu32(resv, RPC_AUTH_NULL);
403 svcauth_null_release(struct svc_rqst *rqstp)
405 if (rqstp->rq_client)
406 auth_domain_put(rqstp->rq_client);
407 rqstp->rq_client = NULL;
408 if (rqstp->rq_cred.cr_group_info)
409 put_group_info(rqstp->rq_cred.cr_group_info);
410 rqstp->rq_cred.cr_group_info = NULL;
412 return 0; /* don't drop */
416 struct auth_ops svcauth_null = {
418 .owner = THIS_MODULE,
419 .flavour = RPC_AUTH_NULL,
420 .accept = svcauth_null_accept,
421 .release = svcauth_null_release,
422 .set_client = svcauth_unix_set_client,
427 svcauth_unix_accept(struct svc_rqst *rqstp, u32 *authp)
429 struct kvec *argv = &rqstp->rq_arg.head[0];
430 struct kvec *resv = &rqstp->rq_res.head[0];
431 struct svc_cred *cred = &rqstp->rq_cred;
433 int len = argv->iov_len;
435 cred->cr_group_info = NULL;
436 rqstp->rq_client = NULL;
438 if ((len -= 3*4) < 0)
441 svc_getu32(argv); /* length */
442 svc_getu32(argv); /* time stamp */
443 slen = XDR_QUADLEN(ntohl(svc_getu32(argv))); /* machname length */
444 if (slen > 64 || (len -= (slen + 3)*4) < 0)
446 argv->iov_base = (void*)((u32*)argv->iov_base + slen); /* skip machname */
447 argv->iov_len -= slen*4;
449 cred->cr_uid = ntohl(svc_getu32(argv)); /* uid */
450 cred->cr_gid = ntohl(svc_getu32(argv)); /* gid */
451 slen = ntohl(svc_getu32(argv)); /* gids length */
452 if (slen > 16 || (len -= (slen + 2)*4) < 0)
454 cred->cr_group_info = groups_alloc(slen);
455 if (cred->cr_group_info == NULL)
457 for (i = 0; i < slen; i++)
458 GROUP_AT(cred->cr_group_info, i) = ntohl(svc_getu32(argv));
460 if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
461 *authp = rpc_autherr_badverf;
465 /* Put NULL verifier */
466 svc_putu32(resv, RPC_AUTH_NULL);
472 *authp = rpc_autherr_badcred;
477 svcauth_unix_release(struct svc_rqst *rqstp)
479 /* Verifier (such as it is) is already in place.
481 if (rqstp->rq_client)
482 auth_domain_put(rqstp->rq_client);
483 rqstp->rq_client = NULL;
484 if (rqstp->rq_cred.cr_group_info)
485 put_group_info(rqstp->rq_cred.cr_group_info);
486 rqstp->rq_cred.cr_group_info = NULL;
492 struct auth_ops svcauth_unix = {
494 .owner = THIS_MODULE,
495 .flavour = RPC_AUTH_UNIX,
496 .accept = svcauth_unix_accept,
497 .release = svcauth_unix_release,
498 .domain_release = svcauth_unix_domain_release,
499 .set_client = svcauth_unix_set_client,