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>
12 #define RPCDBG_FACILITY RPCDBG_AUTH
16 * AUTHUNIX and AUTHNULL credentials are both handled here.
17 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
18 * are always nobody (-2). i.e. we do the same IP address checks for
19 * AUTHNULL as for AUTHUNIX, and that is done here.
23 static char *strdup(char *s)
25 char *rv = kmalloc(strlen(s)+1, GFP_KERNEL);
34 /* other stuff later */
37 struct auth_domain *unix_domain_find(char *name)
39 struct auth_domain *rv, ud;
40 struct unix_domain *new;
44 rv = auth_domain_lookup(&ud, 0);
47 if (rv && rv->flavour != RPC_AUTH_UNIX) {
54 new = kmalloc(sizeof(*new), GFP_KERNEL);
57 cache_init(&new->h.h);
58 new->h.name = strdup(name);
59 new->h.flavour = RPC_AUTH_UNIX;
60 new->addr_changes = 0;
61 new->h.h.expiry_time = NEVER;
63 rv = auth_domain_lookup(&new->h, 2);
65 if (atomic_dec_and_test(&new->h.h.refcnt)) BUG();
67 auth_domain_put(&new->h);
74 static void svcauth_unix_domain_release(struct auth_domain *dom)
76 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
83 /**************************************************
84 * cache for IP address to unix_domain
85 * as needed by AUTH_UNIX
88 #define IP_HASHMAX (1<<IP_HASHBITS)
89 #define IP_HASHMASK (IP_HASHMAX-1)
93 char m_class[8]; /* e.g. "nfsd" */
94 struct in_addr m_addr;
95 struct unix_domain *m_client;
98 static struct cache_head *ip_table[IP_HASHMAX];
100 void ip_map_put(struct cache_head *item, struct cache_detail *cd)
102 struct ip_map *im = container_of(item, struct ip_map,h);
103 if (cache_put(item, cd)) {
104 if (test_bit(CACHE_VALID, &item->flags) &&
105 !test_bit(CACHE_NEGATIVE, &item->flags))
106 auth_domain_put(&im->m_client->h);
111 static inline int ip_map_hash(struct ip_map *item)
113 return hash_str(item->m_class, IP_HASHBITS) ^
114 hash_long((unsigned long)item->m_addr.s_addr, IP_HASHBITS);
116 static inline int ip_map_match(struct ip_map *item, struct ip_map *tmp)
118 return strcmp(tmp->m_class, item->m_class) == 0
119 && tmp->m_addr.s_addr == item->m_addr.s_addr;
121 static inline void ip_map_init(struct ip_map *new, struct ip_map *item)
123 strcpy(new->m_class, item->m_class);
124 new->m_addr.s_addr = item->m_addr.s_addr;
126 static inline void ip_map_update(struct ip_map *new, struct ip_map *item)
128 cache_get(&item->m_client->h.h);
129 new->m_client = item->m_client;
130 new->m_add_change = item->m_add_change;
133 static void ip_map_request(struct cache_detail *cd,
134 struct cache_head *h,
135 char **bpp, int *blen)
138 struct ip_map *im = container_of(h, struct ip_map, h);
139 __u32 addr = im->m_addr.s_addr;
141 snprintf(text_addr, 20, "%u.%u.%u.%u",
142 ntohl(addr) >> 24 & 0xff,
143 ntohl(addr) >> 16 & 0xff,
144 ntohl(addr) >> 8 & 0xff,
145 ntohl(addr) >> 0 & 0xff);
147 qword_add(bpp, blen, im->m_class);
148 qword_add(bpp, blen, text_addr);
152 static struct ip_map *ip_map_lookup(struct ip_map *, int);
154 static int ip_map_parse(struct cache_detail *cd,
155 char *mesg, int mlen)
157 /* class ipaddress [domainname] */
158 /* should be safe just to use the start of the input buffer
164 struct ip_map ipm, *ipmp;
165 struct auth_domain *dom;
168 if (mesg[mlen-1] != '\n')
173 len = qword_get(&mesg, ipm.m_class, sizeof(ipm.m_class));
174 if (len <= 0) return -EINVAL;
177 len = qword_get(&mesg, buf, mlen);
178 if (len <= 0) return -EINVAL;
180 if (sscanf(buf, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
183 expiry = get_expiry(&mesg);
187 /* domainname, or empty for NEGATIVE */
188 len = qword_get(&mesg, buf, mlen);
189 if (len < 0) return -EINVAL;
192 dom = unix_domain_find(buf);
199 htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
202 ipm.m_client = container_of(dom, struct unix_domain, h);
203 ipm.m_add_change = ipm.m_client->addr_changes;
205 set_bit(CACHE_NEGATIVE, &ipm.h.flags);
206 ipm.h.expiry_time = expiry;
208 ipmp = ip_map_lookup(&ipm, 1);
210 ip_map_put(&ipmp->h, &ip_map_cache);
212 auth_domain_put(dom);
219 static int ip_map_show(struct seq_file *m,
220 struct cache_detail *cd,
221 struct cache_head *h)
225 char *dom = "-no-domain-";
228 seq_puts(m, "#class IP domain\n");
231 im = container_of(h, struct ip_map, h);
232 /* class addr domain */
235 if (test_bit(CACHE_VALID, &h->flags) &&
236 !test_bit(CACHE_NEGATIVE, &h->flags))
237 dom = im->m_client->h.name;
239 seq_printf(m, "%s %d.%d.%d.%d %s\n",
241 htonl(addr.s_addr) >> 24 & 0xff,
242 htonl(addr.s_addr) >> 16 & 0xff,
243 htonl(addr.s_addr) >> 8 & 0xff,
244 htonl(addr.s_addr) >> 0 & 0xff,
251 struct cache_detail ip_map_cache = {
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 != RPC_AUTH_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 != RPC_AUTH_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);
329 cache_purge(&auth_domain_cache);
334 svcauth_null_accept(struct svc_rqst *rqstp, u32 *authp)
336 struct kvec *argv = &rqstp->rq_arg.head[0];
337 struct kvec *resv = &rqstp->rq_res.head[0];
339 struct ip_map key, *ipm;
341 if (argv->iov_len < 3*4)
344 if (svc_getu32(argv) != 0) {
345 dprintk("svc: bad null cred\n");
346 *authp = rpc_autherr_badcred;
349 if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
350 dprintk("svc: bad null verf\n");
351 *authp = rpc_autherr_badverf;
355 /* Signal that mapping to nobody uid/gid is required */
356 rqstp->rq_cred.cr_uid = (uid_t) -1;
357 rqstp->rq_cred.cr_gid = (gid_t) -1;
358 rqstp->rq_cred.cr_group_info = groups_alloc(0);
359 if (rqstp->rq_cred.cr_group_info == NULL)
360 return SVC_DROP; /* kmalloc failure - client must retry */
362 /* Put NULL verifier */
363 svc_putu32(resv, RPC_AUTH_NULL);
366 strcpy(key.m_class, rqstp->rq_server->sv_program->pg_class);
367 key.m_addr = rqstp->rq_addr.sin_addr;
369 ipm = ip_map_lookup(&key, 0);
371 rqstp->rq_client = NULL;
374 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
379 rv = SVC_OK; /* rq_client is NULL */
382 rqstp->rq_client = &ipm->m_client->h;
383 cache_get(&rqstp->rq_client->h);
384 ip_map_put(&ipm->h, &ip_map_cache);
391 if (rqstp->rq_client == NULL && rqstp->rq_proc != 0)
392 *authp = rpc_autherr_badcred;
398 svcauth_null_release(struct svc_rqst *rqstp)
400 if (rqstp->rq_client)
401 auth_domain_put(rqstp->rq_client);
402 rqstp->rq_client = NULL;
403 if (rqstp->rq_cred.cr_group_info)
404 put_group_info(rqstp->rq_cred.cr_group_info);
405 rqstp->rq_cred.cr_group_info = NULL;
407 return 0; /* don't drop */
411 struct auth_ops svcauth_null = {
413 .owner = THIS_MODULE,
414 .flavour = RPC_AUTH_NULL,
415 .accept = svcauth_null_accept,
416 .release = svcauth_null_release,
421 svcauth_unix_accept(struct svc_rqst *rqstp, u32 *authp)
423 struct kvec *argv = &rqstp->rq_arg.head[0];
424 struct kvec *resv = &rqstp->rq_res.head[0];
425 struct svc_cred *cred = &rqstp->rq_cred;
427 int len = argv->iov_len;
429 struct ip_map key, *ipm;
431 cred->cr_group_info = NULL;
432 rqstp->rq_client = NULL;
434 if ((len -= 3*4) < 0)
437 svc_getu32(argv); /* length */
438 svc_getu32(argv); /* time stamp */
439 slen = XDR_QUADLEN(ntohl(svc_getu32(argv))); /* machname length */
440 if (slen > 64 || (len -= (slen + 3)*4) < 0)
442 argv->iov_base = (void*)((u32*)argv->iov_base + slen); /* skip machname */
443 argv->iov_len -= slen*4;
445 cred->cr_uid = ntohl(svc_getu32(argv)); /* uid */
446 cred->cr_gid = ntohl(svc_getu32(argv)); /* gid */
447 slen = ntohl(svc_getu32(argv)); /* gids length */
448 if (slen > 16 || (len -= (slen + 2)*4) < 0)
450 cred->cr_group_info = groups_alloc(slen);
451 if (cred->cr_group_info == NULL)
453 for (i = 0; i < slen; i++)
454 GROUP_AT(cred->cr_group_info, i) = ntohl(svc_getu32(argv));
456 if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
457 *authp = rpc_autherr_badverf;
462 strcpy(key.m_class, rqstp->rq_server->sv_program->pg_class);
463 key.m_addr = rqstp->rq_addr.sin_addr;
466 ipm = ip_map_lookup(&key, 0);
469 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
474 rv = SVC_OK; /* rq_client is NULL */
477 rqstp->rq_client = &ipm->m_client->h;
478 cache_get(&rqstp->rq_client->h);
479 ip_map_put(&ipm->h, &ip_map_cache);
486 if (rv == SVC_OK && rqstp->rq_client == NULL && rqstp->rq_proc != 0)
489 /* Put NULL verifier */
490 svc_putu32(resv, RPC_AUTH_NULL);
496 *authp = rpc_autherr_badcred;
501 svcauth_unix_release(struct svc_rqst *rqstp)
503 /* Verifier (such as it is) is already in place.
505 if (rqstp->rq_client)
506 auth_domain_put(rqstp->rq_client);
507 rqstp->rq_client = NULL;
508 if (rqstp->rq_cred.cr_group_info)
509 put_group_info(rqstp->rq_cred.cr_group_info);
510 rqstp->rq_cred.cr_group_info = NULL;
516 struct auth_ops svcauth_unix = {
518 .owner = THIS_MODULE,
519 .flavour = RPC_AUTH_UNIX,
520 .accept = svcauth_unix_accept,
521 .release = svcauth_unix_release,
522 .domain_release = svcauth_unix_domain_release,