1 #include <linux/types.h>
2 #include <linux/sched.h>
3 #include <linux/sunrpc/types.h>
4 #include <linux/sunrpc/xdr.h>
5 #include <linux/sunrpc/svcsock.h>
6 #include <linux/sunrpc/svcauth.h>
8 #include <linux/seq_file.h>
9 #include <linux/hash.h>
11 #define RPCDBG_FACILITY RPCDBG_AUTH
15 * AUTHUNIX and AUTHNULL credentials are both handled here.
16 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
17 * are always nobody (-2). i.e. we do the same IP address checks for
18 * AUTHNULL as for AUTHUNIX, and that is done here.
22 static char *strdup(char *s)
24 char *rv = kmalloc(strlen(s)+1, GFP_KERNEL);
33 /* other stuff later */
36 struct auth_domain *unix_domain_find(char *name)
38 struct auth_domain *rv, ud;
39 struct unix_domain *new;
43 rv = auth_domain_lookup(&ud, 0);
46 if (rv && rv->flavour != RPC_AUTH_UNIX) {
53 new = kmalloc(sizeof(*new), GFP_KERNEL);
56 cache_init(&new->h.h);
57 atomic_inc(&new->h.h.refcnt);
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;
64 rv = auth_domain_lookup(&new->h, 2);
66 if (atomic_dec_and_test(&new->h.h.refcnt)) BUG();
68 auth_domain_put(&new->h);
75 static void svcauth_unix_domain_release(struct auth_domain *dom)
77 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
84 /**************************************************
85 * cache for IP address to unix_domain
86 * as needed by AUTH_UNIX
89 #define IP_HASHMAX (1<<IP_HASHBITS)
90 #define IP_HASHMASK (IP_HASHMAX-1)
94 char *m_class; /* e.g. "nfsd" */
95 struct in_addr m_addr;
96 struct unix_domain *m_client;
99 static struct cache_head *ip_table[IP_HASHMAX];
101 void ip_map_put(struct cache_head *item, struct cache_detail *cd)
103 struct ip_map *im = container_of(item, struct ip_map,h);
104 if (cache_put(item, cd)) {
105 if (test_bit(CACHE_VALID, &item->flags) &&
106 !test_bit(CACHE_NEGATIVE, &item->flags))
107 auth_domain_put(&im->m_client->h);
112 static inline int ip_map_hash(struct ip_map *item)
114 return hash_str(item->m_class, IP_HASHBITS) ^
115 hash_long((unsigned long)item->m_addr.s_addr, IP_HASHBITS);
117 static inline int ip_map_match(struct ip_map *item, struct ip_map *tmp)
119 return strcmp(tmp->m_class, item->m_class) == 0
120 && tmp->m_addr.s_addr == item->m_addr.s_addr;
122 static inline void ip_map_init(struct ip_map *new, struct ip_map *item)
124 new->m_class = item->m_class;
125 item->m_class = NULL;
126 new->m_addr.s_addr = item->m_addr.s_addr;
128 static inline void ip_map_update(struct ip_map *new, struct ip_map *item)
130 cache_get(&item->m_client->h.h);
131 new->m_client = item->m_client;
132 new->m_add_change = item->m_add_change;
135 static void ip_map_request(struct cache_detail *cd,
136 struct cache_head *h,
137 char **bpp, int *blen)
140 struct ip_map *im = container_of(h, struct ip_map, h);
141 __u32 addr = im->m_addr.s_addr;
143 snprintf(text_addr, 20, "%u.%u.%u.%u",
144 ntohl(addr) >> 24 & 0xff,
145 ntohl(addr) >> 16 & 0xff,
146 ntohl(addr) >> 8 & 0xff,
147 ntohl(addr) >> 0 & 0xff);
149 qword_add(bpp, blen, im->m_class);
150 qword_add(bpp, blen, text_addr);
154 static struct ip_map *ip_map_lookup(struct ip_map *, int);
155 static int ip_map_parse(struct cache_detail *cd,
156 char *mesg, int mlen)
158 /* class ipaddress [domainname] */
159 char class[50], buf[50];
163 struct ip_map ipm, *ipmp;
164 struct auth_domain *dom;
167 if (mesg[mlen-1] != '\n')
172 len = qword_get(&mesg, class, 50);
173 if (len <= 0) return -EINVAL;
176 len = qword_get(&mesg, buf, 50);
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, 50);
188 if (len < 0) return -EINVAL;
191 dom = unix_domain_find(buf);
197 ipm.m_class = strdup(class);
198 if (ipm.m_class == NULL)
201 htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
204 ipm.m_client = container_of(dom, struct unix_domain, h);
205 ipm.m_add_change = ipm.m_client->addr_changes;
207 set_bit(CACHE_NEGATIVE, &ipm.h.flags);
208 ipm.h.expiry_time = expiry;
210 ipmp = ip_map_lookup(&ipm, 1);
212 ip_map_put(&ipmp->h, &ip_map_cache);
214 auth_domain_put(dom);
215 if (ipm.m_class) kfree(ipm.m_class);
222 static int ip_map_show(struct seq_file *m,
223 struct cache_detail *cd,
224 struct cache_head *h)
228 char *dom = "-no-domain-";
231 seq_puts(m, "#class IP domain\n");
234 im = container_of(h, struct ip_map, h);
235 /* class addr domain */
238 if (test_bit(CACHE_VALID, &h->flags) &&
239 !test_bit(CACHE_NEGATIVE, &h->flags))
240 dom = im->m_client->h.name;
242 seq_printf(m, "%s %d.%d.%d.%d %s\n",
244 htonl(addr.s_addr) >> 24 & 0xff,
245 htonl(addr.s_addr) >> 16 & 0xff,
246 htonl(addr.s_addr) >> 8 & 0xff,
247 htonl(addr.s_addr) >> 0 & 0xff,
254 struct cache_detail ip_map_cache = {
255 .hash_size = IP_HASHMAX,
256 .hash_table = ip_table,
257 .name = "auth.unix.ip",
258 .cache_put = ip_map_put,
259 .cache_request = ip_map_request,
260 .cache_parse = ip_map_parse,
261 .cache_show = ip_map_show,
264 static DefineSimpleCacheLookup(ip_map, 0)
267 int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
269 struct unix_domain *udom;
270 struct ip_map ip, *ipmp;
272 if (dom->flavour != RPC_AUTH_UNIX)
274 udom = container_of(dom, struct unix_domain, h);
275 ip.m_class = strdup("nfsd");
280 ip.m_add_change = udom->addr_changes+1;
282 ip.h.expiry_time = NEVER;
284 ipmp = ip_map_lookup(&ip, 1);
285 if (ip.m_class) kfree(ip.m_class);
287 ip_map_put(&ipmp->h, &ip_map_cache);
293 int auth_unix_forget_old(struct auth_domain *dom)
295 struct unix_domain *udom;
297 if (dom->flavour != RPC_AUTH_UNIX)
299 udom = container_of(dom, struct unix_domain, h);
300 udom->addr_changes++;
304 struct auth_domain *auth_unix_lookup(struct in_addr addr)
306 struct ip_map key, *ipm;
307 struct auth_domain *rv;
309 key.m_class = "nfsd";
312 ipm = ip_map_lookup(&key, 0);
316 if (cache_check(&ip_map_cache, &ipm->h, NULL))
319 if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
320 set_bit(CACHE_NEGATIVE, &ipm->h.flags);
323 rv = &ipm->m_client->h;
326 ip_map_put(&ipm->h, &ip_map_cache);
330 void svcauth_unix_purge(void)
332 cache_purge(&ip_map_cache);
333 cache_purge(&auth_domain_cache);
338 svcauth_null_accept(struct svc_rqst *rqstp, u32 *authp)
340 struct iovec *argv = &rqstp->rq_arg.head[0];
341 struct iovec *resv = &rqstp->rq_res.head[0];
343 struct ip_map key, *ipm;
345 if (argv->iov_len < 3*4)
348 if (svc_getu32(argv) != 0) {
349 dprintk("svc: bad null cred\n");
350 *authp = rpc_autherr_badcred;
353 if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
354 dprintk("svc: bad null verf\n");
355 *authp = rpc_autherr_badverf;
359 /* Signal that mapping to nobody uid/gid is required */
360 rqstp->rq_cred.cr_uid = (uid_t) -1;
361 rqstp->rq_cred.cr_gid = (gid_t) -1;
362 rqstp->rq_cred.cr_group_info = groups_alloc(0);
363 if (rqstp->rq_cred.cr_group_info == NULL)
364 return SVC_DROP; /* kmalloc failure - client must retry */
366 /* Put NULL verifier */
367 svc_putu32(resv, RPC_AUTH_NULL);
370 key.m_class = rqstp->rq_server->sv_program->pg_class;
371 key.m_addr = rqstp->rq_addr.sin_addr;
373 ipm = ip_map_lookup(&key, 0);
375 rqstp->rq_client = NULL;
378 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
383 rv = SVC_OK; /* rq_client is NULL */
386 rqstp->rq_client = &ipm->m_client->h;
387 cache_get(&rqstp->rq_client->h);
388 ip_map_put(&ipm->h, &ip_map_cache);
395 if (rqstp->rq_client == NULL && rqstp->rq_proc != 0)
396 *authp = rpc_autherr_badcred;
402 svcauth_null_release(struct svc_rqst *rqstp)
404 if (rqstp->rq_client)
405 auth_domain_put(rqstp->rq_client);
406 rqstp->rq_client = NULL;
408 return 0; /* don't drop */
412 struct auth_ops svcauth_null = {
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 iovec *argv = &rqstp->rq_arg.head[0];
424 struct iovec *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 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 .flavour = RPC_AUTH_UNIX,
519 .accept = svcauth_unix_accept,
520 .release = svcauth_unix_release,
521 .domain_release = svcauth_unix_domain_release,