This commit was manufactured by cvs2svn to create tag
[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         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
63         rv = auth_domain_lookup(&new->h, 2);
64         if (rv == &new->h) {
65                 if (atomic_dec_and_test(&new->h.h.refcnt)) BUG();
66         } else {
67                 auth_domain_put(&new->h);
68                 goto foundit;
69         }
70
71         return rv;
72 }
73
74 static void svcauth_unix_domain_release(struct auth_domain *dom)
75 {
76         struct unix_domain *ud = container_of(dom, struct unix_domain, h);
77
78         kfree(dom->name);
79         kfree(ud);
80 }
81
82
83 /**************************************************
84  * cache for IP address to unix_domain
85  * as needed by AUTH_UNIX
86  */
87 #define IP_HASHBITS     8
88 #define IP_HASHMAX      (1<<IP_HASHBITS)
89 #define IP_HASHMASK     (IP_HASHMAX-1)
90
91 struct ip_map {
92         struct cache_head       h;
93         char                    m_class[8]; /* e.g. "nfsd" */
94         struct in_addr          m_addr;
95         struct unix_domain      *m_client;
96         int                     m_add_change;
97 };
98 static struct cache_head        *ip_table[IP_HASHMAX];
99
100 void ip_map_put(struct cache_head *item, struct cache_detail *cd)
101 {
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);
107                 kfree(im);
108         }
109 }
110
111 static inline int ip_map_hash(struct ip_map *item)
112 {
113         return hash_str(item->m_class, IP_HASHBITS) ^ 
114                 hash_long((unsigned long)item->m_addr.s_addr, IP_HASHBITS);
115 }
116 static inline int ip_map_match(struct ip_map *item, struct ip_map *tmp)
117 {
118         return strcmp(tmp->m_class, item->m_class) == 0
119                 && tmp->m_addr.s_addr == item->m_addr.s_addr;
120 }
121 static inline void ip_map_init(struct ip_map *new, struct ip_map *item)
122 {
123         strcpy(new->m_class, item->m_class);
124         new->m_addr.s_addr = item->m_addr.s_addr;
125 }
126 static inline void ip_map_update(struct ip_map *new, struct ip_map *item)
127 {
128         cache_get(&item->m_client->h.h);
129         new->m_client = item->m_client;
130         new->m_add_change = item->m_add_change;
131 }
132
133 static void ip_map_request(struct cache_detail *cd,
134                                   struct cache_head *h,
135                                   char **bpp, int *blen)
136 {
137         char text_addr[20];
138         struct ip_map *im = container_of(h, struct ip_map, h);
139         __u32 addr = im->m_addr.s_addr;
140         
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);
146
147         qword_add(bpp, blen, im->m_class);
148         qword_add(bpp, blen, text_addr);
149         (*bpp)[-1] = '\n';
150 }
151
152 static struct ip_map *ip_map_lookup(struct ip_map *, int);
153
154 static int ip_map_parse(struct cache_detail *cd,
155                           char *mesg, int mlen)
156 {
157         /* class ipaddress [domainname] */
158         /* should be safe just to use the start of the input buffer
159          * for scratch: */
160         char *buf = mesg;
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, ipm.m_class, sizeof(ipm.m_class));
174         if (len <= 0) return -EINVAL;
175         if (len >= sizeof(ipm.m_class))
176                 return -EINVAL;
177
178         /* ip address */
179         len = qword_get(&mesg, buf, mlen);
180         if (len <= 0) return -EINVAL;
181
182         if (sscanf(buf, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
183                 return -EINVAL;
184         
185         expiry = get_expiry(&mesg);
186         if (expiry ==0)
187                 return -EINVAL;
188
189         /* domainname, or empty for NEGATIVE */
190         len = qword_get(&mesg, buf, mlen);
191         if (len < 0) return -EINVAL;
192
193         if (len) {
194                 dom = unix_domain_find(buf);
195                 if (dom == NULL)
196                         return -ENOENT;
197         } else
198                 dom = NULL;
199
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 (!ipmp)
216                 return -ENOMEM;
217         cache_flush();
218         return 0;
219 }
220
221 static int ip_map_show(struct seq_file *m,
222                        struct cache_detail *cd,
223                        struct cache_head *h)
224 {
225         struct ip_map *im;
226         struct in_addr addr;
227         char *dom = "-no-domain-";
228
229         if (h == NULL) {
230                 seq_puts(m, "#class IP domain\n");
231                 return 0;
232         }
233         im = container_of(h, struct ip_map, h);
234         /* class addr domain */
235         addr = im->m_addr;
236
237         if (test_bit(CACHE_VALID, &h->flags) && 
238             !test_bit(CACHE_NEGATIVE, &h->flags))
239                 dom = im->m_client->h.name;
240
241         seq_printf(m, "%s %d.%d.%d.%d %s\n",
242                    im->m_class,
243                    htonl(addr.s_addr) >> 24 & 0xff,
244                    htonl(addr.s_addr) >> 16 & 0xff,
245                    htonl(addr.s_addr) >>  8 & 0xff,
246                    htonl(addr.s_addr) >>  0 & 0xff,
247                    dom
248                    );
249         return 0;
250 }
251         
252
253 struct cache_detail ip_map_cache = {
254         .hash_size      = IP_HASHMAX,
255         .hash_table     = ip_table,
256         .name           = "auth.unix.ip",
257         .cache_put      = ip_map_put,
258         .cache_request  = ip_map_request,
259         .cache_parse    = ip_map_parse,
260         .cache_show     = ip_map_show,
261 };
262
263 static DefineSimpleCacheLookup(ip_map, 0)
264
265
266 int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
267 {
268         struct unix_domain *udom;
269         struct ip_map ip, *ipmp;
270
271         if (dom->flavour != RPC_AUTH_UNIX)
272                 return -EINVAL;
273         udom = container_of(dom, struct unix_domain, h);
274         strcpy(ip.m_class, "nfsd");
275         ip.m_addr = addr;
276         ip.m_client = udom;
277         ip.m_add_change = udom->addr_changes+1;
278         ip.h.flags = 0;
279         ip.h.expiry_time = NEVER;
280         
281         ipmp = ip_map_lookup(&ip, 1);
282
283         if (ipmp) {
284                 ip_map_put(&ipmp->h, &ip_map_cache);
285                 return 0;
286         } else
287                 return -ENOMEM;
288 }
289
290 int auth_unix_forget_old(struct auth_domain *dom)
291 {
292         struct unix_domain *udom;
293         
294         if (dom->flavour != RPC_AUTH_UNIX)
295                 return -EINVAL;
296         udom = container_of(dom, struct unix_domain, h);
297         udom->addr_changes++;
298         return 0;
299 }
300
301 struct auth_domain *auth_unix_lookup(struct in_addr addr)
302 {
303         struct ip_map key, *ipm;
304         struct auth_domain *rv;
305
306         strcpy(key.m_class, "nfsd");
307         key.m_addr = addr;
308
309         ipm = ip_map_lookup(&key, 0);
310
311         if (!ipm)
312                 return NULL;
313         if (cache_check(&ip_map_cache, &ipm->h, NULL))
314                 return NULL;
315
316         if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
317                 if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
318                         auth_domain_put(&ipm->m_client->h);
319                 rv = NULL;
320         } else {
321                 rv = &ipm->m_client->h;
322                 cache_get(&rv->h);
323         }
324         ip_map_put(&ipm->h, &ip_map_cache);
325         return rv;
326 }
327
328 void svcauth_unix_purge(void)
329 {
330         cache_purge(&ip_map_cache);
331         cache_purge(&auth_domain_cache);
332 }
333
334
335 static int
336 svcauth_null_accept(struct svc_rqst *rqstp, u32 *authp)
337 {
338         struct kvec     *argv = &rqstp->rq_arg.head[0];
339         struct kvec     *resv = &rqstp->rq_res.head[0];
340         int             rv=0;
341         struct ip_map key, *ipm;
342
343         if (argv->iov_len < 3*4)
344                 return SVC_GARBAGE;
345
346         if (svc_getu32(argv) != 0) { 
347                 dprintk("svc: bad null cred\n");
348                 *authp = rpc_autherr_badcred;
349                 return SVC_DENIED;
350         }
351         if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
352                 dprintk("svc: bad null verf\n");
353                 *authp = rpc_autherr_badverf;
354                 return SVC_DENIED;
355         }
356
357         /* Signal that mapping to nobody uid/gid is required */
358         rqstp->rq_cred.cr_uid = (uid_t) -1;
359         rqstp->rq_cred.cr_gid = (gid_t) -1;
360         rqstp->rq_cred.cr_group_info = groups_alloc(0);
361         if (rqstp->rq_cred.cr_group_info == NULL)
362                 return SVC_DROP; /* kmalloc failure - client must retry */
363
364         /* Put NULL verifier */
365         svc_putu32(resv, RPC_AUTH_NULL);
366         svc_putu32(resv, 0);
367
368         strcpy(key.m_class, rqstp->rq_server->sv_program->pg_class);
369         key.m_addr = rqstp->rq_addr.sin_addr;
370
371         ipm = ip_map_lookup(&key, 0);
372
373         rqstp->rq_client = NULL;
374
375         if (ipm)
376                 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
377                 case -EAGAIN:
378                         rv = SVC_DROP;
379                         break;
380                 case -ENOENT:
381                         rv = SVC_OK; /* rq_client is NULL */
382                         break;
383                 case 0:
384                         rqstp->rq_client = &ipm->m_client->h;
385                         cache_get(&rqstp->rq_client->h);
386                         ip_map_put(&ipm->h, &ip_map_cache);
387                         rv = SVC_OK;
388                         break;
389                 default: BUG();
390                 }
391         else rv = SVC_DROP;
392
393         if (rqstp->rq_client == NULL && rqstp->rq_proc != 0)
394                 *authp = rpc_autherr_badcred;
395
396         return rv;
397 }
398
399 static int
400 svcauth_null_release(struct svc_rqst *rqstp)
401 {
402         if (rqstp->rq_client)
403                 auth_domain_put(rqstp->rq_client);
404         rqstp->rq_client = NULL;
405         if (rqstp->rq_cred.cr_group_info)
406                 put_group_info(rqstp->rq_cred.cr_group_info);
407         rqstp->rq_cred.cr_group_info = 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 kvec     *argv = &rqstp->rq_arg.head[0];
426         struct kvec     *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         strcpy(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