ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / sunrpc / svcauth.c
1 /*
2  * linux/net/sunrpc/svcauth.c
3  *
4  * The generic interface for RPC authentication on the server side.
5  * 
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  *
8  * CHANGES
9  * 19-Apr-2000 Chris Evans      - Security fix
10  */
11
12 #include <linux/types.h>
13 #include <linux/sched.h>
14 #include <linux/sunrpc/types.h>
15 #include <linux/sunrpc/xdr.h>
16 #include <linux/sunrpc/svcsock.h>
17 #include <linux/sunrpc/svcauth.h>
18 #include <linux/err.h>
19 #include <linux/hash.h>
20
21 #define RPCDBG_FACILITY RPCDBG_AUTH
22
23
24 /*
25  * Table of authenticators
26  */
27 extern struct auth_ops svcauth_null;
28 extern struct auth_ops svcauth_unix;
29
30 static struct auth_ops  *authtab[RPC_AUTH_MAXFLAVOR] = {
31         [0] = &svcauth_null,
32         [1] = &svcauth_unix,
33 };
34
35 int
36 svc_authenticate(struct svc_rqst *rqstp, u32 *authp)
37 {
38         rpc_authflavor_t        flavor;
39         struct auth_ops         *aops;
40
41         *authp = rpc_auth_ok;
42
43         flavor = ntohl(svc_getu32(&rqstp->rq_arg.head[0]));
44
45         dprintk("svc: svc_authenticate (%d)\n", flavor);
46         if (flavor >= RPC_AUTH_MAXFLAVOR || !(aops = authtab[flavor])) {
47                 *authp = rpc_autherr_badcred;
48                 return SVC_DENIED;
49         }
50
51         rqstp->rq_authop = aops;
52         return aops->accept(rqstp, authp);
53 }
54
55 /* A request, which was authenticated, has now executed.
56  * Time to finalise the the credentials and verifier
57  * and release and resources
58  */
59 int svc_authorise(struct svc_rqst *rqstp)
60 {
61         struct auth_ops *aops = rqstp->rq_authop;
62         int rv = 0;
63
64         rqstp->rq_authop = NULL;
65         
66         if (aops) 
67                 rv = aops->release(rqstp);
68
69         /* FIXME should I count and release authops */
70         return rv;
71 }
72
73 int
74 svc_auth_register(rpc_authflavor_t flavor, struct auth_ops *aops)
75 {
76         if (flavor >= RPC_AUTH_MAXFLAVOR || authtab[flavor])
77                 return -EINVAL;
78         authtab[flavor] = aops;
79         return 0;
80 }
81
82 void
83 svc_auth_unregister(rpc_authflavor_t flavor)
84 {
85         if (flavor < RPC_AUTH_MAXFLAVOR)
86                 authtab[flavor] = NULL;
87 }
88
89 /**************************************************
90  * cache for domain name to auth_domain
91  * Entries are only added by flavours which will normally
92  * have a structure that 'inherits' from auth_domain.
93  * e.g. when an IP -> domainname is given to  auth_unix,
94  * and the domain name doesn't exist, it will create a
95  * auth_unix_domain and add it to this hash table.
96  * If it finds the name does exist, but isn't AUTH_UNIX,
97  * it will complain.
98  */
99
100 /*
101  * Auth auth_domain cache is somewhat different to other caches,
102  * largely because the entries are possibly of different types:
103  * each auth flavour has it's own type.
104  * One consequence of this that DefineCacheLookup cannot
105  * allocate a new structure as it cannot know the size.
106  * Notice that the "INIT" code fragment is quite different
107  * from other caches.  When auth_domain_lookup might be
108  * creating a new domain, the new domain is passed in
109  * complete and it is used as-is rather than being copied into
110  * another structure.
111  */
112 #define DN_HASHBITS     6
113 #define DN_HASHMAX      (1<<DN_HASHBITS)
114 #define DN_HASHMASK     (DN_HASHMAX-1)
115
116 static struct cache_head        *auth_domain_table[DN_HASHMAX];
117 void auth_domain_drop(struct cache_head *item, struct cache_detail *cd)
118 {
119         struct auth_domain *dom = container_of(item, struct auth_domain, h);
120         if (cache_put(item,cd))
121                 authtab[dom->flavour]->domain_release(dom);
122 }
123
124
125 struct cache_detail auth_domain_cache = {
126         .hash_size      = DN_HASHMAX,
127         .hash_table     = auth_domain_table,
128         .name           = "auth.domain",
129         .cache_put      = auth_domain_drop,
130 };
131
132 void auth_domain_put(struct auth_domain *dom)
133 {
134         auth_domain_drop(&dom->h, &auth_domain_cache);
135 }
136
137 static inline int auth_domain_hash(struct auth_domain *item)
138 {
139         return hash_str(item->name, DN_HASHBITS);
140 }
141 static inline int auth_domain_match(struct auth_domain *tmp, struct auth_domain *item)
142 {
143         return strcmp(tmp->name, item->name) == 0;
144 }
145 DefineCacheLookup(struct auth_domain,
146                   h,
147                   auth_domain_lookup,
148                   (struct auth_domain *item, int set),
149                   /* no setup */,
150                   &auth_domain_cache,
151                   auth_domain_hash(item),
152                   auth_domain_match(tmp, item),
153                   kfree(new); if(!set) {
154                         if (new)
155                                 write_unlock(&auth_domain_cache.hash_lock);
156                         else
157                                 read_unlock(&auth_domain_cache.hash_lock);
158                         return NULL;
159                   }
160                   new=item; atomic_inc(&new->h.refcnt),
161                   /* no update */,
162                   0 /* no inplace updates */
163                   )
164
165 struct auth_domain *auth_domain_find(char *name)
166 {
167         struct auth_domain *rv, ad;
168
169         ad.name = name;
170         rv = auth_domain_lookup(&ad, 0);
171         return rv;
172 }