ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / sunrpc / svcauth_unix.c
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>
7 #include <linux/err.h>
8 #include <linux/seq_file.h>
9 #include <linux/hash.h>
10
11 #define RPCDBG_FACILITY RPCDBG_AUTH
12
13
14 /*
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.
19  */
20
21
22 static char *strdup(char *s)
23 {
24         char *rv = kmalloc(strlen(s)+1, GFP_KERNEL);
25         if (rv)
26                 strcpy(rv, s);
27         return rv;
28 }
29
30 struct unix_domain {
31         struct auth_domain      h;
32         int     addr_changes;
33         /* other stuff later */
34 };
35
36 struct auth_domain *unix_domain_find(char *name)
37 {
38         struct auth_domain *rv, ud;
39         struct unix_domain *new;
40
41         ud.name = name;
42         
43         rv = auth_domain_lookup(&ud, 0);
44
45  foundit:
46         if (rv && rv->flavour != RPC_AUTH_UNIX) {
47                 auth_domain_put(rv);
48                 return NULL;
49         }
50         if (rv)
51                 return rv;
52
53         new = kmalloc(sizeof(*new), GFP_KERNEL);
54         if (new == NULL)
55                 return NULL;
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;
62         new->h.h.flags = 0;
63
64         rv = auth_domain_lookup(&new->h, 2);
65         if (rv == &new->h) {
66                 if (atomic_dec_and_test(&new->h.h.refcnt)) BUG();
67         } else {
68                 auth_domain_put(&new->h);
69                 goto foundit;
70         }
71
72         return rv;
73 }
74
75 static void svcauth_unix_domain_release(struct auth_domain *dom)
76 {
77         struct unix_domain *ud = container_of(dom, struct unix_domain, h);
78
79         kfree(dom->name);
80         kfree(ud);
81 }
82
83
84 /**************************************************
85  * cache for IP address to unix_domain
86  * as needed by AUTH_UNIX
87  */
88 #define IP_HASHBITS     8
89 #define IP_HASHMAX      (1<<IP_HASHBITS)
90 #define IP_HASHMASK     (IP_HASHMAX-1)
91
92 struct ip_map {
93         struct cache_head       h;
94         char                    *m_class; /* e.g. "nfsd" */
95         struct in_addr          m_addr;
96         struct unix_domain      *m_client;
97         int                     m_add_change;
98 };
99 static struct cache_head        *ip_table[IP_HASHMAX];
100
101 void ip_map_put(struct cache_head *item, struct cache_detail *cd)
102 {
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);
108                 kfree(im);
109         }
110 }
111
112 static inline int ip_map_hash(struct ip_map *item)
113 {
114         return hash_str(item->m_class, IP_HASHBITS) ^ 
115                 hash_long((unsigned long)item->m_addr.s_addr, IP_HASHBITS);
116 }
117 static inline int ip_map_match(struct ip_map *item, struct ip_map *tmp)
118 {
119         return strcmp(tmp->m_class, item->m_class) == 0
120                 && tmp->m_addr.s_addr == item->m_addr.s_addr;
121 }
122 static inline void ip_map_init(struct ip_map *new, struct ip_map *item)
123 {
124         new->m_class = item->m_class;
125         item->m_class = NULL;
126         new->m_addr.s_addr = item->m_addr.s_addr;
127 }
128 static inline void ip_map_update(struct ip_map *new, struct ip_map *item)
129 {
130         cache_get(&item->m_client->h.h);
131         new->m_client = item->m_client;
132         new->m_add_change = item->m_add_change;
133 }
134
135 static void ip_map_request(struct cache_detail *cd,
136                                   struct cache_head *h,
137                                   char **bpp, int *blen)
138 {
139         char text_addr[20];
140         struct ip_map *im = container_of(h, struct ip_map, h);
141         __u32 addr = im->m_addr.s_addr;
142         
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);
148
149         qword_add(bpp, blen, im->m_class);
150         qword_add(bpp, blen, text_addr);
151         (*bpp)[-1] = '\n';
152 }
153
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)
157 {
158         /* class ipaddress [domainname] */
159         char class[50], buf[50];
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, class, 50);
173         if (len <= 0) return -EINVAL;
174
175         /* ip address */
176         len = qword_get(&mesg, buf, 50);
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, 50);
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_class = strdup(class);
198         if (ipm.m_class == NULL)
199                 return -ENOMEM;
200         ipm.m_addr.s_addr =
201                 htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
202         ipm.h.flags = 0;
203         if (dom) {
204                 ipm.m_client = container_of(dom, struct unix_domain, h);
205                 ipm.m_add_change = ipm.m_client->addr_changes;
206         } else
207                 set_bit(CACHE_NEGATIVE, &ipm.h.flags);
208         ipm.h.expiry_time = expiry;
209
210         ipmp = ip_map_lookup(&ipm, 1);
211         if (ipmp)
212                 ip_map_put(&ipmp->h, &ip_map_cache);
213         if (dom)
214                 auth_domain_put(dom);
215         if (ipm.m_class) kfree(ipm.m_class);
216         if (!ipmp)
217                 return -ENOMEM;
218         cache_flush();
219         return 0;
220 }
221
222 static int ip_map_show(struct seq_file *m,
223                        struct cache_detail *cd,
224                        struct cache_head *h)
225 {
226         struct ip_map *im;
227         struct in_addr addr;
228         char *dom = "-no-domain-";
229
230         if (h == NULL) {
231                 seq_puts(m, "#class IP domain\n");
232                 return 0;
233         }
234         im = container_of(h, struct ip_map, h);
235         /* class addr domain */
236         addr = im->m_addr;
237
238         if (test_bit(CACHE_VALID, &h->flags) && 
239             !test_bit(CACHE_NEGATIVE, &h->flags))
240                 dom = im->m_client->h.name;
241
242         seq_printf(m, "%s %d.%d.%d.%d %s\n",
243                    im->m_class,
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,
248                    dom
249                    );
250         return 0;
251 }
252         
253
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,
262 };
263
264 static DefineSimpleCacheLookup(ip_map, 0)
265
266
267 int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
268 {
269         struct unix_domain *udom;
270         struct ip_map ip, *ipmp;
271
272         if (dom->flavour != RPC_AUTH_UNIX)
273                 return -EINVAL;
274         udom = container_of(dom, struct unix_domain, h);
275         ip.m_class = strdup("nfsd");
276         if (!ip.m_class)
277                 return -ENOMEM;
278         ip.m_addr = addr;
279         ip.m_client = udom;
280         ip.m_add_change = udom->addr_changes+1;
281         ip.h.flags = 0;
282         ip.h.expiry_time = NEVER;
283         
284         ipmp = ip_map_lookup(&ip, 1);
285         if (ip.m_class) kfree(ip.m_class);
286         if (ipmp) {
287                 ip_map_put(&ipmp->h, &ip_map_cache);
288                 return 0;
289         } else
290                 return -ENOMEM;
291 }
292
293 int auth_unix_forget_old(struct auth_domain *dom)
294 {
295         struct unix_domain *udom;
296         
297         if (dom->flavour != RPC_AUTH_UNIX)
298                 return -EINVAL;
299         udom = container_of(dom, struct unix_domain, h);
300         udom->addr_changes++;
301         return 0;
302 }
303
304 struct auth_domain *auth_unix_lookup(struct in_addr addr)
305 {
306         struct ip_map key, *ipm;
307         struct auth_domain *rv;
308
309         key.m_class = "nfsd";
310         key.m_addr = addr;
311
312         ipm = ip_map_lookup(&key, 0);
313
314         if (!ipm)
315                 return NULL;
316         if (cache_check(&ip_map_cache, &ipm->h, NULL))
317                 return NULL;
318
319         if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
320                 set_bit(CACHE_NEGATIVE, &ipm->h.flags);
321                 rv = NULL;
322         } else {
323                 rv = &ipm->m_client->h;
324                 cache_get(&rv->h);
325         }
326         ip_map_put(&ipm->h, &ip_map_cache);
327         return rv;
328 }
329
330 void svcauth_unix_purge(void)
331 {
332         cache_purge(&ip_map_cache);
333         cache_purge(&auth_domain_cache);
334 }
335
336
337 static int
338 svcauth_null_accept(struct svc_rqst *rqstp, u32 *authp)
339 {
340         struct iovec    *argv = &rqstp->rq_arg.head[0];
341         struct iovec    *resv = &rqstp->rq_res.head[0];
342         int             rv=0;
343         struct ip_map key, *ipm;
344
345         if (argv->iov_len < 3*4)
346                 return SVC_GARBAGE;
347
348         if (svc_getu32(argv) != 0) { 
349                 dprintk("svc: bad null cred\n");
350                 *authp = rpc_autherr_badcred;
351                 return SVC_DENIED;
352         }
353         if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
354                 dprintk("svc: bad null verf\n");
355                 *authp = rpc_autherr_badverf;
356                 return SVC_DENIED;
357         }
358
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 */
365
366         /* Put NULL verifier */
367         svc_putu32(resv, RPC_AUTH_NULL);
368         svc_putu32(resv, 0);
369
370         key.m_class = rqstp->rq_server->sv_program->pg_class;
371         key.m_addr = rqstp->rq_addr.sin_addr;
372
373         ipm = ip_map_lookup(&key, 0);
374
375         rqstp->rq_client = NULL;
376
377         if (ipm)
378                 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
379                 case -EAGAIN:
380                         rv = SVC_DROP;
381                         break;
382                 case -ENOENT:
383                         rv = SVC_OK; /* rq_client is NULL */
384                         break;
385                 case 0:
386                         rqstp->rq_client = &ipm->m_client->h;
387                         cache_get(&rqstp->rq_client->h);
388                         ip_map_put(&ipm->h, &ip_map_cache);
389                         rv = SVC_OK;
390                         break;
391                 default: BUG();
392                 }
393         else rv = SVC_DROP;
394
395         if (rqstp->rq_client == NULL && rqstp->rq_proc != 0)
396                 *authp = rpc_autherr_badcred;
397
398         return rv;
399 }
400
401 static int
402 svcauth_null_release(struct svc_rqst *rqstp)
403 {
404         if (rqstp->rq_client)
405                 auth_domain_put(rqstp->rq_client);
406         rqstp->rq_client = NULL;
407
408         return 0; /* don't drop */
409 }
410
411
412 struct auth_ops svcauth_null = {
413         .name           = "null",
414         .flavour        = RPC_AUTH_NULL,
415         .accept         = svcauth_null_accept,
416         .release        = svcauth_null_release,
417 };
418
419
420 int
421 svcauth_unix_accept(struct svc_rqst *rqstp, u32 *authp)
422 {
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;
426         u32             slen, i;
427         int             len   = argv->iov_len;
428         int             rv=0;
429         struct ip_map key, *ipm;
430
431         cred->cr_group_info = NULL;
432         rqstp->rq_client = NULL;
433
434         if ((len -= 3*4) < 0)
435                 return SVC_GARBAGE;
436
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)
441                 goto badcred;
442         argv->iov_base = (void*)((u32*)argv->iov_base + slen);  /* skip machname */
443         argv->iov_len -= slen*4;
444
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)
449                 goto badcred;
450         cred->cr_group_info = groups_alloc(slen);
451         if (cred->cr_group_info == NULL)
452                 return SVC_DROP;
453         for (i = 0; i < slen; i++)
454                 GROUP_AT(cred->cr_group_info, i) = ntohl(svc_getu32(argv));
455
456         if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
457                 *authp = rpc_autherr_badverf;
458                 return SVC_DENIED;
459         }
460
461
462         key.m_class = rqstp->rq_server->sv_program->pg_class;
463         key.m_addr = rqstp->rq_addr.sin_addr;
464
465
466         ipm = ip_map_lookup(&key, 0);
467
468         if (ipm)
469                 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
470                 case -EAGAIN:
471                         rv = SVC_DROP;
472                         break;
473                 case -ENOENT:
474                         rv = SVC_OK; /* rq_client is NULL */
475                         break;
476                 case 0:
477                         rqstp->rq_client = &ipm->m_client->h;
478                         cache_get(&rqstp->rq_client->h);
479                         ip_map_put(&ipm->h, &ip_map_cache);
480                         rv = SVC_OK;
481                         break;
482                 default: BUG();
483                 }
484         else rv = SVC_DROP;
485
486         if (rv  == SVC_OK && rqstp->rq_client == NULL && rqstp->rq_proc != 0)
487                 goto badcred;
488
489         /* Put NULL verifier */
490         svc_putu32(resv, RPC_AUTH_NULL);
491         svc_putu32(resv, 0);
492
493         return rv;
494
495 badcred:
496         *authp = rpc_autherr_badcred;
497         return SVC_DENIED;
498 }
499
500 int
501 svcauth_unix_release(struct svc_rqst *rqstp)
502 {
503         /* Verifier (such as it is) is already in place.
504          */
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;
511
512         return 0;
513 }
514
515
516 struct auth_ops svcauth_unix = {
517         .name           = "unix",
518         .flavour        = RPC_AUTH_UNIX,
519         .accept         = svcauth_unix_accept,
520         .release        = svcauth_unix_release,
521         .domain_release = svcauth_unix_domain_release,
522 };
523