patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / 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
12 #define RPCDBG_FACILITY RPCDBG_AUTH
13
14
15 /*
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.
20  */
21
22
23 static char *strdup(char *s)
24 {
25         char *rv = kmalloc(strlen(s)+1, GFP_KERNEL);
26         if (rv)
27                 strcpy(rv, s);
28         return rv;
29 }
30
31 struct unix_domain {
32         struct auth_domain      h;
33         int     addr_changes;
34         /* other stuff later */
35 };
36
37 struct auth_domain *unix_domain_find(char *name)
38 {
39         struct auth_domain *rv, ud;
40         struct unix_domain *new;
41
42         ud.name = name;
43         
44         rv = auth_domain_lookup(&ud, 0);
45
46  foundit:
47         if (rv && rv->flavour != RPC_AUTH_UNIX) {
48                 auth_domain_put(rv);
49                 return NULL;
50         }
51         if (rv)
52                 return rv;
53
54         new = kmalloc(sizeof(*new), GFP_KERNEL);
55         if (new == NULL)
56                 return NULL;
57         cache_init(&new->h.h);
58         atomic_inc(&new->h.h.refcnt);
59         new->h.name = strdup(name);
60         new->h.flavour = RPC_AUTH_UNIX;
61         new->addr_changes = 0;
62         new->h.h.expiry_time = NEVER;
63         new->h.h.flags = 0;
64
65         rv = auth_domain_lookup(&new->h, 2);
66         if (rv == &new->h) {
67                 if (atomic_dec_and_test(&new->h.h.refcnt)) BUG();
68         } else {
69                 auth_domain_put(&new->h);
70                 goto foundit;
71         }
72
73         return rv;
74 }
75
76 static void svcauth_unix_domain_release(struct auth_domain *dom)
77 {
78         struct unix_domain *ud = container_of(dom, struct unix_domain, h);
79
80         kfree(dom->name);
81         kfree(ud);
82 }
83
84
85 /**************************************************
86  * cache for IP address to unix_domain
87  * as needed by AUTH_UNIX
88  */
89 #define IP_HASHBITS     8
90 #define IP_HASHMAX      (1<<IP_HASHBITS)
91 #define IP_HASHMASK     (IP_HASHMAX-1)
92
93 struct ip_map {
94         struct cache_head       h;
95         char                    *m_class; /* e.g. "nfsd" */
96         struct in_addr          m_addr;
97         struct unix_domain      *m_client;
98         int                     m_add_change;
99 };
100 static struct cache_head        *ip_table[IP_HASHMAX];
101
102 void ip_map_put(struct cache_head *item, struct cache_detail *cd)
103 {
104         struct ip_map *im = container_of(item, struct ip_map,h);
105         if (cache_put(item, cd)) {
106                 if (test_bit(CACHE_VALID, &item->flags) &&
107                     !test_bit(CACHE_NEGATIVE, &item->flags))
108                         auth_domain_put(&im->m_client->h);
109                 kfree(im);
110         }
111 }
112
113 static inline int ip_map_hash(struct ip_map *item)
114 {
115         return hash_str(item->m_class, IP_HASHBITS) ^ 
116                 hash_long((unsigned long)item->m_addr.s_addr, IP_HASHBITS);
117 }
118 static inline int ip_map_match(struct ip_map *item, struct ip_map *tmp)
119 {
120         return strcmp(tmp->m_class, item->m_class) == 0
121                 && tmp->m_addr.s_addr == item->m_addr.s_addr;
122 }
123 static inline void ip_map_init(struct ip_map *new, struct ip_map *item)
124 {
125         new->m_class = item->m_class;
126         item->m_class = NULL;
127         new->m_addr.s_addr = item->m_addr.s_addr;
128 }
129 static inline void ip_map_update(struct ip_map *new, struct ip_map *item)
130 {
131         cache_get(&item->m_client->h.h);
132         new->m_client = item->m_client;
133         new->m_add_change = item->m_add_change;
134 }
135
136 static void ip_map_request(struct cache_detail *cd,
137                                   struct cache_head *h,
138                                   char **bpp, int *blen)
139 {
140         char text_addr[20];
141         struct ip_map *im = container_of(h, struct ip_map, h);
142         __u32 addr = im->m_addr.s_addr;
143         
144         snprintf(text_addr, 20, "%u.%u.%u.%u",
145                  ntohl(addr) >> 24 & 0xff,
146                  ntohl(addr) >> 16 & 0xff,
147                  ntohl(addr) >>  8 & 0xff,
148                  ntohl(addr) >>  0 & 0xff);
149
150         qword_add(bpp, blen, im->m_class);
151         qword_add(bpp, blen, text_addr);
152         (*bpp)[-1] = '\n';
153 }
154
155 static struct ip_map *ip_map_lookup(struct ip_map *, int);
156 static int ip_map_parse(struct cache_detail *cd,
157                           char *mesg, int mlen)
158 {
159         /* class ipaddress [domainname] */
160         char class[50], buf[50];
161         int len;
162         int b1,b2,b3,b4;
163         char c;
164         struct ip_map ipm, *ipmp;
165         struct auth_domain *dom;
166         time_t expiry;
167
168         if (mesg[mlen-1] != '\n')
169                 return -EINVAL;
170         mesg[mlen-1] = 0;
171
172         /* class */
173         len = qword_get(&mesg, class, 50);
174         if (len <= 0) return -EINVAL;
175
176         /* ip address */
177         len = qword_get(&mesg, buf, 50);
178         if (len <= 0) return -EINVAL;
179
180         if (sscanf(buf, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
181                 return -EINVAL;
182         
183         expiry = get_expiry(&mesg);
184         if (expiry ==0)
185                 return -EINVAL;
186
187         /* domainname, or empty for NEGATIVE */
188         len = qword_get(&mesg, buf, 50);
189         if (len < 0) return -EINVAL;
190
191         if (len) {
192                 dom = unix_domain_find(buf);
193                 if (dom == NULL)
194                         return -ENOENT;
195         } else
196                 dom = NULL;
197
198         ipm.m_class = strdup(class);
199         if (ipm.m_class == NULL)
200                 return -ENOMEM;
201         ipm.m_addr.s_addr =
202                 htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
203         ipm.h.flags = 0;
204         if (dom) {
205                 ipm.m_client = container_of(dom, struct unix_domain, h);
206                 ipm.m_add_change = ipm.m_client->addr_changes;
207         } else
208                 set_bit(CACHE_NEGATIVE, &ipm.h.flags);
209         ipm.h.expiry_time = expiry;
210
211         ipmp = ip_map_lookup(&ipm, 1);
212         if (ipmp)
213                 ip_map_put(&ipmp->h, &ip_map_cache);
214         if (dom)
215                 auth_domain_put(dom);
216         if (ipm.m_class) kfree(ipm.m_class);
217         if (!ipmp)
218                 return -ENOMEM;
219         cache_flush();
220         return 0;
221 }
222
223 static int ip_map_show(struct seq_file *m,
224                        struct cache_detail *cd,
225                        struct cache_head *h)
226 {
227         struct ip_map *im;
228         struct in_addr addr;
229         char *dom = "-no-domain-";
230
231         if (h == NULL) {
232                 seq_puts(m, "#class IP domain\n");
233                 return 0;
234         }
235         im = container_of(h, struct ip_map, h);
236         /* class addr domain */
237         addr = im->m_addr;
238
239         if (test_bit(CACHE_VALID, &h->flags) && 
240             !test_bit(CACHE_NEGATIVE, &h->flags))
241                 dom = im->m_client->h.name;
242
243         seq_printf(m, "%s %d.%d.%d.%d %s\n",
244                    im->m_class,
245                    htonl(addr.s_addr) >> 24 & 0xff,
246                    htonl(addr.s_addr) >> 16 & 0xff,
247                    htonl(addr.s_addr) >>  8 & 0xff,
248                    htonl(addr.s_addr) >>  0 & 0xff,
249                    dom
250                    );
251         return 0;
252 }
253         
254
255 struct cache_detail ip_map_cache = {
256         .hash_size      = IP_HASHMAX,
257         .hash_table     = ip_table,
258         .name           = "auth.unix.ip",
259         .cache_put      = ip_map_put,
260         .cache_request  = ip_map_request,
261         .cache_parse    = ip_map_parse,
262         .cache_show     = ip_map_show,
263 };
264
265 static DefineSimpleCacheLookup(ip_map, 0)
266
267
268 int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
269 {
270         struct unix_domain *udom;
271         struct ip_map ip, *ipmp;
272
273         if (dom->flavour != RPC_AUTH_UNIX)
274                 return -EINVAL;
275         udom = container_of(dom, struct unix_domain, h);
276         ip.m_class = strdup("nfsd");
277         if (!ip.m_class)
278                 return -ENOMEM;
279         ip.m_addr = addr;
280         ip.m_client = udom;
281         ip.m_add_change = udom->addr_changes+1;
282         ip.h.flags = 0;
283         ip.h.expiry_time = NEVER;
284         
285         ipmp = ip_map_lookup(&ip, 1);
286         if (ip.m_class) kfree(ip.m_class);
287         if (ipmp) {
288                 ip_map_put(&ipmp->h, &ip_map_cache);
289                 return 0;
290         } else
291                 return -ENOMEM;
292 }
293
294 int auth_unix_forget_old(struct auth_domain *dom)
295 {
296         struct unix_domain *udom;
297         
298         if (dom->flavour != RPC_AUTH_UNIX)
299                 return -EINVAL;
300         udom = container_of(dom, struct unix_domain, h);
301         udom->addr_changes++;
302         return 0;
303 }
304
305 struct auth_domain *auth_unix_lookup(struct in_addr addr)
306 {
307         struct ip_map key, *ipm;
308         struct auth_domain *rv;
309
310         key.m_class = "nfsd";
311         key.m_addr = addr;
312
313         ipm = ip_map_lookup(&key, 0);
314
315         if (!ipm)
316                 return NULL;
317         if (cache_check(&ip_map_cache, &ipm->h, NULL))
318                 return NULL;
319
320         if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
321                 set_bit(CACHE_NEGATIVE, &ipm->h.flags);
322                 rv = NULL;
323         } else {
324                 rv = &ipm->m_client->h;
325                 cache_get(&rv->h);
326         }
327         ip_map_put(&ipm->h, &ip_map_cache);
328         return rv;
329 }
330
331 void svcauth_unix_purge(void)
332 {
333         cache_purge(&ip_map_cache);
334         cache_purge(&auth_domain_cache);
335 }
336
337
338 static int
339 svcauth_null_accept(struct svc_rqst *rqstp, u32 *authp)
340 {
341         struct iovec    *argv = &rqstp->rq_arg.head[0];
342         struct iovec    *resv = &rqstp->rq_res.head[0];
343         int             rv=0;
344         struct ip_map key, *ipm;
345
346         if (argv->iov_len < 3*4)
347                 return SVC_GARBAGE;
348
349         if (svc_getu32(argv) != 0) { 
350                 dprintk("svc: bad null cred\n");
351                 *authp = rpc_autherr_badcred;
352                 return SVC_DENIED;
353         }
354         if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
355                 dprintk("svc: bad null verf\n");
356                 *authp = rpc_autherr_badverf;
357                 return SVC_DENIED;
358         }
359
360         /* Signal that mapping to nobody uid/gid is required */
361         rqstp->rq_cred.cr_uid = (uid_t) -1;
362         rqstp->rq_cred.cr_gid = (gid_t) -1;
363         rqstp->rq_cred.cr_group_info = groups_alloc(0);
364         if (rqstp->rq_cred.cr_group_info == NULL)
365                 return SVC_DROP; /* kmalloc failure - client must retry */
366
367         /* Put NULL verifier */
368         svc_putu32(resv, RPC_AUTH_NULL);
369         svc_putu32(resv, 0);
370
371         key.m_class = rqstp->rq_server->sv_program->pg_class;
372         key.m_addr = rqstp->rq_addr.sin_addr;
373
374         ipm = ip_map_lookup(&key, 0);
375
376         rqstp->rq_client = NULL;
377
378         if (ipm)
379                 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
380                 case -EAGAIN:
381                         rv = SVC_DROP;
382                         break;
383                 case -ENOENT:
384                         rv = SVC_OK; /* rq_client is NULL */
385                         break;
386                 case 0:
387                         rqstp->rq_client = &ipm->m_client->h;
388                         cache_get(&rqstp->rq_client->h);
389                         ip_map_put(&ipm->h, &ip_map_cache);
390                         rv = SVC_OK;
391                         break;
392                 default: BUG();
393                 }
394         else rv = SVC_DROP;
395
396         if (rqstp->rq_client == NULL && rqstp->rq_proc != 0)
397                 *authp = rpc_autherr_badcred;
398
399         return rv;
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
409         return 0; /* don't drop */
410 }
411
412
413 struct auth_ops svcauth_null = {
414         .name           = "null",
415         .owner          = THIS_MODULE,
416         .flavour        = RPC_AUTH_NULL,
417         .accept         = svcauth_null_accept,
418         .release        = svcauth_null_release,
419 };
420
421
422 int
423 svcauth_unix_accept(struct svc_rqst *rqstp, u32 *authp)
424 {
425         struct iovec    *argv = &rqstp->rq_arg.head[0];
426         struct iovec    *resv = &rqstp->rq_res.head[0];
427         struct svc_cred *cred = &rqstp->rq_cred;
428         u32             slen, i;
429         int             len   = argv->iov_len;
430         int             rv=0;
431         struct ip_map key, *ipm;
432
433         cred->cr_group_info = NULL;
434         rqstp->rq_client = NULL;
435
436         if ((len -= 3*4) < 0)
437                 return SVC_GARBAGE;
438
439         svc_getu32(argv);                       /* length */
440         svc_getu32(argv);                       /* time stamp */
441         slen = XDR_QUADLEN(ntohl(svc_getu32(argv)));    /* machname length */
442         if (slen > 64 || (len -= (slen + 3)*4) < 0)
443                 goto badcred;
444         argv->iov_base = (void*)((u32*)argv->iov_base + slen);  /* skip machname */
445         argv->iov_len -= slen*4;
446
447         cred->cr_uid = ntohl(svc_getu32(argv));         /* uid */
448         cred->cr_gid = ntohl(svc_getu32(argv));         /* gid */
449         slen = ntohl(svc_getu32(argv));                 /* gids length */
450         if (slen > 16 || (len -= (slen + 2)*4) < 0)
451                 goto badcred;
452         cred->cr_group_info = groups_alloc(slen);
453         if (cred->cr_group_info == NULL)
454                 return SVC_DROP;
455         for (i = 0; i < slen; i++)
456                 GROUP_AT(cred->cr_group_info, i) = ntohl(svc_getu32(argv));
457
458         if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
459                 *authp = rpc_autherr_badverf;
460                 return SVC_DENIED;
461         }
462
463
464         key.m_class = rqstp->rq_server->sv_program->pg_class;
465         key.m_addr = rqstp->rq_addr.sin_addr;
466
467
468         ipm = ip_map_lookup(&key, 0);
469
470         if (ipm)
471                 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
472                 case -EAGAIN:
473                         rv = SVC_DROP;
474                         break;
475                 case -ENOENT:
476                         rv = SVC_OK; /* rq_client is NULL */
477                         break;
478                 case 0:
479                         rqstp->rq_client = &ipm->m_client->h;
480                         cache_get(&rqstp->rq_client->h);
481                         ip_map_put(&ipm->h, &ip_map_cache);
482                         rv = SVC_OK;
483                         break;
484                 default: BUG();
485                 }
486         else rv = SVC_DROP;
487
488         if (rv  == SVC_OK && rqstp->rq_client == NULL && rqstp->rq_proc != 0)
489                 goto badcred;
490
491         /* Put NULL verifier */
492         svc_putu32(resv, RPC_AUTH_NULL);
493         svc_putu32(resv, 0);
494
495         return rv;
496
497 badcred:
498         *authp = rpc_autherr_badcred;
499         return SVC_DENIED;
500 }
501
502 int
503 svcauth_unix_release(struct svc_rqst *rqstp)
504 {
505         /* Verifier (such as it is) is already in place.
506          */
507         if (rqstp->rq_client)
508                 auth_domain_put(rqstp->rq_client);
509         rqstp->rq_client = NULL;
510         if (rqstp->rq_cred.cr_group_info)
511                 put_group_info(rqstp->rq_cred.cr_group_info);
512         rqstp->rq_cred.cr_group_info = NULL;
513
514         return 0;
515 }
516
517
518 struct auth_ops svcauth_unix = {
519         .name           = "unix",
520         .owner          = THIS_MODULE,
521         .flavour        = RPC_AUTH_UNIX,
522         .accept         = svcauth_unix_accept,
523         .release        = svcauth_unix_release,
524         .domain_release = svcauth_unix_domain_release,
525 };
526