[PATCH] ppc64: Fix oprofile when compiled as a module
[linux-2.6/kvm.git] / net / sunrpc / svcauth_unix.c
blob3e6c694bbad17fb336dfa87c78ce8317dda18bb9
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>
13 #define RPCDBG_FACILITY RPCDBG_AUTH
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.
24 struct unix_domain {
25 struct auth_domain h;
26 int addr_changes;
27 /* other stuff later */
30 struct auth_domain *unix_domain_find(char *name)
32 struct auth_domain *rv, ud;
33 struct unix_domain *new;
35 ud.name = name;
37 rv = auth_domain_lookup(&ud, 0);
39 foundit:
40 if (rv && rv->flavour != RPC_AUTH_UNIX) {
41 auth_domain_put(rv);
42 return NULL;
44 if (rv)
45 return rv;
47 new = kmalloc(sizeof(*new), GFP_KERNEL);
48 if (new == NULL)
49 return NULL;
50 cache_init(&new->h.h);
51 new->h.name = kstrdup(name, GFP_KERNEL);
52 new->h.flavour = RPC_AUTH_UNIX;
53 new->addr_changes = 0;
54 new->h.h.expiry_time = NEVER;
56 rv = auth_domain_lookup(&new->h, 2);
57 if (rv == &new->h) {
58 if (atomic_dec_and_test(&new->h.h.refcnt)) BUG();
59 } else {
60 auth_domain_put(&new->h);
61 goto foundit;
64 return rv;
67 static void svcauth_unix_domain_release(struct auth_domain *dom)
69 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
71 kfree(dom->name);
72 kfree(ud);
76 /**************************************************
77 * cache for IP address to unix_domain
78 * as needed by AUTH_UNIX
80 #define IP_HASHBITS 8
81 #define IP_HASHMAX (1<<IP_HASHBITS)
82 #define IP_HASHMASK (IP_HASHMAX-1)
84 struct ip_map {
85 struct cache_head h;
86 char m_class[8]; /* e.g. "nfsd" */
87 struct in_addr m_addr;
88 struct unix_domain *m_client;
89 int m_add_change;
91 static struct cache_head *ip_table[IP_HASHMAX];
93 static void ip_map_put(struct cache_head *item, struct cache_detail *cd)
95 struct ip_map *im = container_of(item, struct ip_map,h);
96 if (cache_put(item, cd)) {
97 if (test_bit(CACHE_VALID, &item->flags) &&
98 !test_bit(CACHE_NEGATIVE, &item->flags))
99 auth_domain_put(&im->m_client->h);
100 kfree(im);
104 #if IP_HASHBITS == 8
105 /* hash_long on a 64 bit machine is currently REALLY BAD for
106 * IP addresses in reverse-endian (i.e. on a little-endian machine).
107 * So use a trivial but reliable hash instead
109 static inline int hash_ip(unsigned long ip)
111 int hash = ip ^ (ip>>16);
112 return (hash ^ (hash>>8)) & 0xff;
114 #endif
116 static inline int ip_map_hash(struct ip_map *item)
118 return hash_str(item->m_class, IP_HASHBITS) ^
119 hash_ip((unsigned long)item->m_addr.s_addr);
121 static inline int ip_map_match(struct ip_map *item, struct ip_map *tmp)
123 return strcmp(tmp->m_class, item->m_class) == 0
124 && tmp->m_addr.s_addr == item->m_addr.s_addr;
126 static inline void ip_map_init(struct ip_map *new, struct ip_map *item)
128 strcpy(new->m_class, item->m_class);
129 new->m_addr.s_addr = item->m_addr.s_addr;
131 static inline void ip_map_update(struct ip_map *new, struct ip_map *item)
133 cache_get(&item->m_client->h.h);
134 new->m_client = item->m_client;
135 new->m_add_change = item->m_add_change;
138 static void ip_map_request(struct cache_detail *cd,
139 struct cache_head *h,
140 char **bpp, int *blen)
142 char text_addr[20];
143 struct ip_map *im = container_of(h, struct ip_map, h);
144 __u32 addr = im->m_addr.s_addr;
146 snprintf(text_addr, 20, "%u.%u.%u.%u",
147 ntohl(addr) >> 24 & 0xff,
148 ntohl(addr) >> 16 & 0xff,
149 ntohl(addr) >> 8 & 0xff,
150 ntohl(addr) >> 0 & 0xff);
152 qword_add(bpp, blen, im->m_class);
153 qword_add(bpp, blen, text_addr);
154 (*bpp)[-1] = '\n';
157 static struct ip_map *ip_map_lookup(struct ip_map *, int);
159 static int ip_map_parse(struct cache_detail *cd,
160 char *mesg, int mlen)
162 /* class ipaddress [domainname] */
163 /* should be safe just to use the start of the input buffer
164 * for scratch: */
165 char *buf = mesg;
166 int len;
167 int b1,b2,b3,b4;
168 char c;
169 struct ip_map ipm, *ipmp;
170 struct auth_domain *dom;
171 time_t expiry;
173 if (mesg[mlen-1] != '\n')
174 return -EINVAL;
175 mesg[mlen-1] = 0;
177 /* class */
178 len = qword_get(&mesg, ipm.m_class, sizeof(ipm.m_class));
179 if (len <= 0) return -EINVAL;
181 /* ip address */
182 len = qword_get(&mesg, buf, mlen);
183 if (len <= 0) return -EINVAL;
185 if (sscanf(buf, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
186 return -EINVAL;
188 expiry = get_expiry(&mesg);
189 if (expiry ==0)
190 return -EINVAL;
192 /* domainname, or empty for NEGATIVE */
193 len = qword_get(&mesg, buf, mlen);
194 if (len < 0) return -EINVAL;
196 if (len) {
197 dom = unix_domain_find(buf);
198 if (dom == NULL)
199 return -ENOENT;
200 } else
201 dom = NULL;
203 ipm.m_addr.s_addr =
204 htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
205 ipm.h.flags = 0;
206 if (dom) {
207 ipm.m_client = container_of(dom, struct unix_domain, h);
208 ipm.m_add_change = ipm.m_client->addr_changes;
209 } else
210 set_bit(CACHE_NEGATIVE, &ipm.h.flags);
211 ipm.h.expiry_time = expiry;
213 ipmp = ip_map_lookup(&ipm, 1);
214 if (ipmp)
215 ip_map_put(&ipmp->h, &ip_map_cache);
216 if (dom)
217 auth_domain_put(dom);
218 if (!ipmp)
219 return -ENOMEM;
220 cache_flush();
221 return 0;
224 static int ip_map_show(struct seq_file *m,
225 struct cache_detail *cd,
226 struct cache_head *h)
228 struct ip_map *im;
229 struct in_addr addr;
230 char *dom = "-no-domain-";
232 if (h == NULL) {
233 seq_puts(m, "#class IP domain\n");
234 return 0;
236 im = container_of(h, struct ip_map, h);
237 /* class addr domain */
238 addr = im->m_addr;
240 if (test_bit(CACHE_VALID, &h->flags) &&
241 !test_bit(CACHE_NEGATIVE, &h->flags))
242 dom = im->m_client->h.name;
244 seq_printf(m, "%s %d.%d.%d.%d %s\n",
245 im->m_class,
246 htonl(addr.s_addr) >> 24 & 0xff,
247 htonl(addr.s_addr) >> 16 & 0xff,
248 htonl(addr.s_addr) >> 8 & 0xff,
249 htonl(addr.s_addr) >> 0 & 0xff,
252 return 0;
256 struct cache_detail ip_map_cache = {
257 .owner = THIS_MODULE,
258 .hash_size = IP_HASHMAX,
259 .hash_table = ip_table,
260 .name = "auth.unix.ip",
261 .cache_put = ip_map_put,
262 .cache_request = ip_map_request,
263 .cache_parse = ip_map_parse,
264 .cache_show = ip_map_show,
267 static DefineSimpleCacheLookup(ip_map, 0)
270 int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
272 struct unix_domain *udom;
273 struct ip_map ip, *ipmp;
275 if (dom->flavour != RPC_AUTH_UNIX)
276 return -EINVAL;
277 udom = container_of(dom, struct unix_domain, h);
278 strcpy(ip.m_class, "nfsd");
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;
285 ipmp = ip_map_lookup(&ip, 1);
287 if (ipmp) {
288 ip_map_put(&ipmp->h, &ip_map_cache);
289 return 0;
290 } else
291 return -ENOMEM;
294 int auth_unix_forget_old(struct auth_domain *dom)
296 struct unix_domain *udom;
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;
305 struct auth_domain *auth_unix_lookup(struct in_addr addr)
307 struct ip_map key, *ipm;
308 struct auth_domain *rv;
310 strcpy(key.m_class, "nfsd");
311 key.m_addr = addr;
313 ipm = ip_map_lookup(&key, 0);
315 if (!ipm)
316 return NULL;
317 if (cache_check(&ip_map_cache, &ipm->h, NULL))
318 return NULL;
320 if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
321 if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
322 auth_domain_put(&ipm->m_client->h);
323 rv = NULL;
324 } else {
325 rv = &ipm->m_client->h;
326 cache_get(&rv->h);
328 ip_map_put(&ipm->h, &ip_map_cache);
329 return rv;
332 void svcauth_unix_purge(void)
334 cache_purge(&ip_map_cache);
335 cache_purge(&auth_domain_cache);
338 static int
339 svcauth_unix_set_client(struct svc_rqst *rqstp)
341 struct ip_map key, *ipm;
343 rqstp->rq_client = NULL;
344 if (rqstp->rq_proc == 0)
345 return SVC_OK;
347 strcpy(key.m_class, rqstp->rq_server->sv_program->pg_class);
348 key.m_addr = rqstp->rq_addr.sin_addr;
350 ipm = ip_map_lookup(&key, 0);
352 if (ipm == NULL)
353 return SVC_DENIED;
355 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
356 default:
357 BUG();
358 case -EAGAIN:
359 return SVC_DROP;
360 case -ENOENT:
361 return SVC_DENIED;
362 case 0:
363 rqstp->rq_client = &ipm->m_client->h;
364 cache_get(&rqstp->rq_client->h);
365 ip_map_put(&ipm->h, &ip_map_cache);
366 break;
368 return SVC_OK;
371 static int
372 svcauth_null_accept(struct svc_rqst *rqstp, u32 *authp)
374 struct kvec *argv = &rqstp->rq_arg.head[0];
375 struct kvec *resv = &rqstp->rq_res.head[0];
376 struct svc_cred *cred = &rqstp->rq_cred;
378 cred->cr_group_info = NULL;
379 rqstp->rq_client = NULL;
381 if (argv->iov_len < 3*4)
382 return SVC_GARBAGE;
384 if (svc_getu32(argv) != 0) {
385 dprintk("svc: bad null cred\n");
386 *authp = rpc_autherr_badcred;
387 return SVC_DENIED;
389 if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
390 dprintk("svc: bad null verf\n");
391 *authp = rpc_autherr_badverf;
392 return SVC_DENIED;
395 /* Signal that mapping to nobody uid/gid is required */
396 cred->cr_uid = (uid_t) -1;
397 cred->cr_gid = (gid_t) -1;
398 cred->cr_group_info = groups_alloc(0);
399 if (cred->cr_group_info == NULL)
400 return SVC_DROP; /* kmalloc failure - client must retry */
402 /* Put NULL verifier */
403 svc_putu32(resv, RPC_AUTH_NULL);
404 svc_putu32(resv, 0);
406 return SVC_OK;
409 static int
410 svcauth_null_release(struct svc_rqst *rqstp)
412 if (rqstp->rq_client)
413 auth_domain_put(rqstp->rq_client);
414 rqstp->rq_client = NULL;
415 if (rqstp->rq_cred.cr_group_info)
416 put_group_info(rqstp->rq_cred.cr_group_info);
417 rqstp->rq_cred.cr_group_info = NULL;
419 return 0; /* don't drop */
423 struct auth_ops svcauth_null = {
424 .name = "null",
425 .owner = THIS_MODULE,
426 .flavour = RPC_AUTH_NULL,
427 .accept = svcauth_null_accept,
428 .release = svcauth_null_release,
429 .set_client = svcauth_unix_set_client,
433 static int
434 svcauth_unix_accept(struct svc_rqst *rqstp, u32 *authp)
436 struct kvec *argv = &rqstp->rq_arg.head[0];
437 struct kvec *resv = &rqstp->rq_res.head[0];
438 struct svc_cred *cred = &rqstp->rq_cred;
439 u32 slen, i;
440 int len = argv->iov_len;
442 cred->cr_group_info = NULL;
443 rqstp->rq_client = NULL;
445 if ((len -= 3*4) < 0)
446 return SVC_GARBAGE;
448 svc_getu32(argv); /* length */
449 svc_getu32(argv); /* time stamp */
450 slen = XDR_QUADLEN(ntohl(svc_getu32(argv))); /* machname length */
451 if (slen > 64 || (len -= (slen + 3)*4) < 0)
452 goto badcred;
453 argv->iov_base = (void*)((u32*)argv->iov_base + slen); /* skip machname */
454 argv->iov_len -= slen*4;
456 cred->cr_uid = ntohl(svc_getu32(argv)); /* uid */
457 cred->cr_gid = ntohl(svc_getu32(argv)); /* gid */
458 slen = ntohl(svc_getu32(argv)); /* gids length */
459 if (slen > 16 || (len -= (slen + 2)*4) < 0)
460 goto badcred;
461 cred->cr_group_info = groups_alloc(slen);
462 if (cred->cr_group_info == NULL)
463 return SVC_DROP;
464 for (i = 0; i < slen; i++)
465 GROUP_AT(cred->cr_group_info, i) = ntohl(svc_getu32(argv));
467 if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
468 *authp = rpc_autherr_badverf;
469 return SVC_DENIED;
472 /* Put NULL verifier */
473 svc_putu32(resv, RPC_AUTH_NULL);
474 svc_putu32(resv, 0);
476 return SVC_OK;
478 badcred:
479 *authp = rpc_autherr_badcred;
480 return SVC_DENIED;
483 static int
484 svcauth_unix_release(struct svc_rqst *rqstp)
486 /* Verifier (such as it is) is already in place.
488 if (rqstp->rq_client)
489 auth_domain_put(rqstp->rq_client);
490 rqstp->rq_client = NULL;
491 if (rqstp->rq_cred.cr_group_info)
492 put_group_info(rqstp->rq_cred.cr_group_info);
493 rqstp->rq_cred.cr_group_info = NULL;
495 return 0;
499 struct auth_ops svcauth_unix = {
500 .name = "unix",
501 .owner = THIS_MODULE,
502 .flavour = RPC_AUTH_UNIX,
503 .accept = svcauth_unix_accept,
504 .release = svcauth_unix_release,
505 .domain_release = svcauth_unix_domain_release,
506 .set_client = svcauth_unix_set_client,