[PATCH] knfsd: Get rid of 'inplace' sunrpc caches
[safe/jmp/linux-2.6] / net / sunrpc / svcauth_unix.c
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>
8 #include <linux/err.h>
9 #include <linux/seq_file.h>
10 #include <linux/hash.h>
11 #include <linux/string.h>
12
13 #define RPCDBG_FACILITY RPCDBG_AUTH
14
15
16 /*
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.
21  */
22
23
24 struct unix_domain {
25         struct auth_domain      h;
26         int     addr_changes;
27         /* other stuff later */
28 };
29
30 extern struct auth_ops svcauth_unix;
31
32 struct auth_domain *unix_domain_find(char *name)
33 {
34         struct auth_domain *rv;
35         struct unix_domain *new = NULL;
36
37         rv = auth_domain_lookup(name, NULL);
38         while(1) {
39                 if (rv != &new->h) {
40                         if (new) auth_domain_put(&new->h);
41                         return rv;
42                 }
43                 if (rv && rv->flavour != &svcauth_unix) {
44                         auth_domain_put(rv);
45                         return NULL;
46                 }
47                 if (rv)
48                         return rv;
49
50                 new = kmalloc(sizeof(*new), GFP_KERNEL);
51                 if (new == NULL)
52                         return NULL;
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);
58         }
59 }
60
61 static void svcauth_unix_domain_release(struct auth_domain *dom)
62 {
63         struct unix_domain *ud = container_of(dom, struct unix_domain, h);
64
65         kfree(dom->name);
66         kfree(ud);
67 }
68
69
70 /**************************************************
71  * cache for IP address to unix_domain
72  * as needed by AUTH_UNIX
73  */
74 #define IP_HASHBITS     8
75 #define IP_HASHMAX      (1<<IP_HASHBITS)
76 #define IP_HASHMASK     (IP_HASHMAX-1)
77
78 struct ip_map {
79         struct cache_head       h;
80         char                    m_class[8]; /* e.g. "nfsd" */
81         struct in_addr          m_addr;
82         struct unix_domain      *m_client;
83         int                     m_add_change;
84 };
85 static struct cache_head        *ip_table[IP_HASHMAX];
86
87 static void ip_map_put(struct cache_head *item, struct cache_detail *cd)
88 {
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);
94                 kfree(im);
95         }
96 }
97
98 #if IP_HASHBITS == 8
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
102  */
103 static inline int hash_ip(unsigned long ip)
104 {
105         int hash = ip ^ (ip>>16);
106         return (hash ^ (hash>>8)) & 0xff;
107 }
108 #endif
109
110 static inline int ip_map_hash(struct ip_map *item)
111 {
112         return hash_str(item->m_class, IP_HASHBITS) ^ 
113                 hash_ip((unsigned long)item->m_addr.s_addr);
114 }
115 static inline int ip_map_match(struct ip_map *item, struct ip_map *tmp)
116 {
117         return strcmp(tmp->m_class, item->m_class) == 0
118                 && tmp->m_addr.s_addr == item->m_addr.s_addr;
119 }
120 static inline void ip_map_init(struct ip_map *new, struct ip_map *item)
121 {
122         strcpy(new->m_class, item->m_class);
123         new->m_addr.s_addr = item->m_addr.s_addr;
124 }
125 static inline void ip_map_update(struct ip_map *new, struct ip_map *item)
126 {
127         kref_get(&item->m_client->h.ref);
128         new->m_client = item->m_client;
129         new->m_add_change = item->m_add_change;
130 }
131
132 static void ip_map_request(struct cache_detail *cd,
133                                   struct cache_head *h,
134                                   char **bpp, int *blen)
135 {
136         char text_addr[20];
137         struct ip_map *im = container_of(h, struct ip_map, h);
138         __u32 addr = im->m_addr.s_addr;
139         
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);
145
146         qword_add(bpp, blen, im->m_class);
147         qword_add(bpp, blen, text_addr);
148         (*bpp)[-1] = '\n';
149 }
150
151 static struct ip_map *ip_map_lookup(struct ip_map *, int);
152
153 static int ip_map_parse(struct cache_detail *cd,
154                           char *mesg, int mlen)
155 {
156         /* class ipaddress [domainname] */
157         /* should be safe just to use the start of the input buffer
158          * for scratch: */
159         char *buf = mesg;
160         int len;
161         int b1,b2,b3,b4;
162         char c;
163         struct ip_map ipm, *ipmp;
164         struct auth_domain *dom;
165         time_t expiry;
166
167         if (mesg[mlen-1] != '\n')
168                 return -EINVAL;
169         mesg[mlen-1] = 0;
170
171         /* class */
172         len = qword_get(&mesg, ipm.m_class, sizeof(ipm.m_class));
173         if (len <= 0) return -EINVAL;
174
175         /* ip address */
176         len = qword_get(&mesg, buf, mlen);
177         if (len <= 0) return -EINVAL;
178
179         if (sscanf(buf, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
180                 return -EINVAL;
181         
182         expiry = get_expiry(&mesg);
183         if (expiry ==0)
184                 return -EINVAL;
185
186         /* domainname, or empty for NEGATIVE */
187         len = qword_get(&mesg, buf, mlen);
188         if (len < 0) return -EINVAL;
189
190         if (len) {
191                 dom = unix_domain_find(buf);
192                 if (dom == NULL)
193                         return -ENOENT;
194         } else
195                 dom = NULL;
196
197         ipm.m_addr.s_addr =
198                 htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
199         ipm.h.flags = 0;
200         if (dom) {
201                 ipm.m_client = container_of(dom, struct unix_domain, h);
202                 ipm.m_add_change = ipm.m_client->addr_changes;
203         } else
204                 set_bit(CACHE_NEGATIVE, &ipm.h.flags);
205         ipm.h.expiry_time = expiry;
206
207         ipmp = ip_map_lookup(&ipm, 1);
208         if (ipmp)
209                 ip_map_put(&ipmp->h, &ip_map_cache);
210         if (dom)
211                 auth_domain_put(dom);
212         if (!ipmp)
213                 return -ENOMEM;
214         cache_flush();
215         return 0;
216 }
217
218 static int ip_map_show(struct seq_file *m,
219                        struct cache_detail *cd,
220                        struct cache_head *h)
221 {
222         struct ip_map *im;
223         struct in_addr addr;
224         char *dom = "-no-domain-";
225
226         if (h == NULL) {
227                 seq_puts(m, "#class IP domain\n");
228                 return 0;
229         }
230         im = container_of(h, struct ip_map, h);
231         /* class addr domain */
232         addr = im->m_addr;
233
234         if (test_bit(CACHE_VALID, &h->flags) && 
235             !test_bit(CACHE_NEGATIVE, &h->flags))
236                 dom = im->m_client->h.name;
237
238         seq_printf(m, "%s %d.%d.%d.%d %s\n",
239                    im->m_class,
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,
244                    dom
245                    );
246         return 0;
247 }
248         
249
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,
259 };
260
261 static DefineSimpleCacheLookup(ip_map, ip_map)
262
263
264 int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
265 {
266         struct unix_domain *udom;
267         struct ip_map ip, *ipmp;
268
269         if (dom->flavour != &svcauth_unix)
270                 return -EINVAL;
271         udom = container_of(dom, struct unix_domain, h);
272         strcpy(ip.m_class, "nfsd");
273         ip.m_addr = addr;
274         ip.m_client = udom;
275         ip.m_add_change = udom->addr_changes+1;
276         ip.h.flags = 0;
277         ip.h.expiry_time = NEVER;
278         
279         ipmp = ip_map_lookup(&ip, 1);
280
281         if (ipmp) {
282                 ip_map_put(&ipmp->h, &ip_map_cache);
283                 return 0;
284         } else
285                 return -ENOMEM;
286 }
287
288 int auth_unix_forget_old(struct auth_domain *dom)
289 {
290         struct unix_domain *udom;
291         
292         if (dom->flavour != &svcauth_unix)
293                 return -EINVAL;
294         udom = container_of(dom, struct unix_domain, h);
295         udom->addr_changes++;
296         return 0;
297 }
298
299 struct auth_domain *auth_unix_lookup(struct in_addr addr)
300 {
301         struct ip_map key, *ipm;
302         struct auth_domain *rv;
303
304         strcpy(key.m_class, "nfsd");
305         key.m_addr = addr;
306
307         ipm = ip_map_lookup(&key, 0);
308
309         if (!ipm)
310                 return NULL;
311         if (cache_check(&ip_map_cache, &ipm->h, NULL))
312                 return NULL;
313
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);
317                 rv = NULL;
318         } else {
319                 rv = &ipm->m_client->h;
320                 kref_get(&rv->ref);
321         }
322         ip_map_put(&ipm->h, &ip_map_cache);
323         return rv;
324 }
325
326 void svcauth_unix_purge(void)
327 {
328         cache_purge(&ip_map_cache);
329 }
330
331 static int
332 svcauth_unix_set_client(struct svc_rqst *rqstp)
333 {
334         struct ip_map key, *ipm;
335
336         rqstp->rq_client = NULL;
337         if (rqstp->rq_proc == 0)
338                 return SVC_OK;
339
340         strcpy(key.m_class, rqstp->rq_server->sv_program->pg_class);
341         key.m_addr = rqstp->rq_addr.sin_addr;
342
343         ipm = ip_map_lookup(&key, 0);
344
345         if (ipm == NULL)
346                 return SVC_DENIED;
347
348         switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
349                 default:
350                         BUG();
351                 case -EAGAIN:
352                         return SVC_DROP;
353                 case -ENOENT:
354                         return SVC_DENIED;
355                 case 0:
356                         rqstp->rq_client = &ipm->m_client->h;
357                         kref_get(&rqstp->rq_client->ref);
358                         ip_map_put(&ipm->h, &ip_map_cache);
359                         break;
360         }
361         return SVC_OK;
362 }
363
364 static int
365 svcauth_null_accept(struct svc_rqst *rqstp, u32 *authp)
366 {
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;
370
371         cred->cr_group_info = NULL;
372         rqstp->rq_client = NULL;
373
374         if (argv->iov_len < 3*4)
375                 return SVC_GARBAGE;
376
377         if (svc_getu32(argv) != 0) { 
378                 dprintk("svc: bad null cred\n");
379                 *authp = rpc_autherr_badcred;
380                 return SVC_DENIED;
381         }
382         if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
383                 dprintk("svc: bad null verf\n");
384                 *authp = rpc_autherr_badverf;
385                 return SVC_DENIED;
386         }
387
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 */
394
395         /* Put NULL verifier */
396         svc_putu32(resv, RPC_AUTH_NULL);
397         svc_putu32(resv, 0);
398
399         return SVC_OK;
400 }
401
402 static int
403 svcauth_null_release(struct svc_rqst *rqstp)
404 {
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;
411
412         return 0; /* don't drop */
413 }
414
415
416 struct auth_ops svcauth_null = {
417         .name           = "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,
423 };
424
425
426 static int
427 svcauth_unix_accept(struct svc_rqst *rqstp, u32 *authp)
428 {
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;
432         u32             slen, i;
433         int             len   = argv->iov_len;
434
435         cred->cr_group_info = NULL;
436         rqstp->rq_client = NULL;
437
438         if ((len -= 3*4) < 0)
439                 return SVC_GARBAGE;
440
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)
445                 goto badcred;
446         argv->iov_base = (void*)((u32*)argv->iov_base + slen);  /* skip machname */
447         argv->iov_len -= slen*4;
448
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)
453                 goto badcred;
454         cred->cr_group_info = groups_alloc(slen);
455         if (cred->cr_group_info == NULL)
456                 return SVC_DROP;
457         for (i = 0; i < slen; i++)
458                 GROUP_AT(cred->cr_group_info, i) = ntohl(svc_getu32(argv));
459
460         if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
461                 *authp = rpc_autherr_badverf;
462                 return SVC_DENIED;
463         }
464
465         /* Put NULL verifier */
466         svc_putu32(resv, RPC_AUTH_NULL);
467         svc_putu32(resv, 0);
468
469         return SVC_OK;
470
471 badcred:
472         *authp = rpc_autherr_badcred;
473         return SVC_DENIED;
474 }
475
476 static int
477 svcauth_unix_release(struct svc_rqst *rqstp)
478 {
479         /* Verifier (such as it is) is already in place.
480          */
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;
487
488         return 0;
489 }
490
491
492 struct auth_ops svcauth_unix = {
493         .name           = "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,
500 };
501