vserver 1.9.5.x5
[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/module.h>
15 #include <linux/sunrpc/types.h>
16 #include <linux/sunrpc/xdr.h>
17 #include <linux/sunrpc/svcsock.h>
18 #include <linux/sunrpc/svcauth.h>
19 #include <linux/err.h>
20 #include <linux/hash.h>
21
22 #define RPCDBG_FACILITY RPCDBG_AUTH
23
24
25 /*
26  * Table of authenticators
27  */
28 extern struct auth_ops svcauth_null;
29 extern struct auth_ops svcauth_unix;
30
31 static DEFINE_SPINLOCK(authtab_lock);
32 static struct auth_ops  *authtab[RPC_AUTH_MAXFLAVOR] = {
33         [0] = &svcauth_null,
34         [1] = &svcauth_unix,
35 };
36
37 int
38 svc_authenticate(struct svc_rqst *rqstp, u32 *authp)
39 {
40         rpc_authflavor_t        flavor;
41         struct auth_ops         *aops;
42
43         *authp = rpc_auth_ok;
44
45         flavor = ntohl(svc_getu32(&rqstp->rq_arg.head[0]));
46
47         dprintk("svc: svc_authenticate (%d)\n", flavor);
48
49         spin_lock(&authtab_lock);
50         if (flavor >= RPC_AUTH_MAXFLAVOR || !(aops = authtab[flavor])
51                         || !try_module_get(aops->owner)) {
52                 spin_unlock(&authtab_lock);
53                 *authp = rpc_autherr_badcred;
54                 return SVC_DENIED;
55         }
56         spin_unlock(&authtab_lock);
57
58         rqstp->rq_authop = aops;
59         return aops->accept(rqstp, authp);
60 }
61
62 /* A request, which was authenticated, has now executed.
63  * Time to finalise the the credentials and verifier
64  * and release and resources
65  */
66 int svc_authorise(struct svc_rqst *rqstp)
67 {
68         struct auth_ops *aops = rqstp->rq_authop;
69         int rv = 0;
70
71         rqstp->rq_authop = NULL;
72         
73         if (aops) {
74                 rv = aops->release(rqstp);
75                 module_put(aops->owner);
76         }
77         return rv;
78 }
79
80 int
81 svc_auth_register(rpc_authflavor_t flavor, struct auth_ops *aops)
82 {
83         int rv = -EINVAL;
84         spin_lock(&authtab_lock);
85         if (flavor < RPC_AUTH_MAXFLAVOR && authtab[flavor] == NULL) {
86                 authtab[flavor] = aops;
87                 rv = 0;
88         }
89         spin_unlock(&authtab_lock);
90         return rv;
91 }
92
93 void
94 svc_auth_unregister(rpc_authflavor_t flavor)
95 {
96         spin_lock(&authtab_lock);
97         if (flavor < RPC_AUTH_MAXFLAVOR)
98                 authtab[flavor] = NULL;
99         spin_unlock(&authtab_lock);
100 }
101 EXPORT_SYMBOL(svc_auth_unregister);
102
103 /**************************************************
104  * cache for domain name to auth_domain
105  * Entries are only added by flavours which will normally
106  * have a structure that 'inherits' from auth_domain.
107  * e.g. when an IP -> domainname is given to  auth_unix,
108  * and the domain name doesn't exist, it will create a
109  * auth_unix_domain and add it to this hash table.
110  * If it finds the name does exist, but isn't AUTH_UNIX,
111  * it will complain.
112  */
113
114 /*
115  * Auth auth_domain cache is somewhat different to other caches,
116  * largely because the entries are possibly of different types:
117  * each auth flavour has it's own type.
118  * One consequence of this that DefineCacheLookup cannot
119  * allocate a new structure as it cannot know the size.
120  * Notice that the "INIT" code fragment is quite different
121  * from other caches.  When auth_domain_lookup might be
122  * creating a new domain, the new domain is passed in
123  * complete and it is used as-is rather than being copied into
124  * another structure.
125  */
126 #define DN_HASHBITS     6
127 #define DN_HASHMAX      (1<<DN_HASHBITS)
128 #define DN_HASHMASK     (DN_HASHMAX-1)
129
130 static struct cache_head        *auth_domain_table[DN_HASHMAX];
131
132 static void auth_domain_drop(struct cache_head *item, struct cache_detail *cd)
133 {
134         struct auth_domain *dom = container_of(item, struct auth_domain, h);
135         if (cache_put(item,cd))
136                 authtab[dom->flavour]->domain_release(dom);
137 }
138
139
140 struct cache_detail auth_domain_cache = {
141         .hash_size      = DN_HASHMAX,
142         .hash_table     = auth_domain_table,
143         .name           = "auth.domain",
144         .cache_put      = auth_domain_drop,
145 };
146
147 void auth_domain_put(struct auth_domain *dom)
148 {
149         auth_domain_drop(&dom->h, &auth_domain_cache);
150 }
151
152 static inline int auth_domain_hash(struct auth_domain *item)
153 {
154         return hash_str(item->name, DN_HASHBITS);
155 }
156 static inline int auth_domain_match(struct auth_domain *tmp, struct auth_domain *item)
157 {
158         return strcmp(tmp->name, item->name) == 0;
159 }
160
161 struct auth_domain *
162 auth_domain_lookup(struct auth_domain *item, int set)
163 {
164         struct auth_domain *tmp = NULL;
165         struct cache_head **hp, **head;
166         head = &auth_domain_cache.hash_table[auth_domain_hash(item)];
167
168         if (set)
169                 write_lock(&auth_domain_cache.hash_lock);
170         else
171                 read_lock(&auth_domain_cache.hash_lock);
172         for (hp=head; *hp != NULL; hp = &tmp->h.next) {
173                 tmp = container_of(*hp, struct auth_domain, h);
174                 if (!auth_domain_match(tmp, item))
175                         continue;
176                 cache_get(&tmp->h);
177                 if (!set)
178                         goto out_noset;
179                 *hp = tmp->h.next;
180                 tmp->h.next = NULL;
181                 clear_bit(CACHE_HASHED, &tmp->h.flags);
182                 auth_domain_drop(&tmp->h, &auth_domain_cache);
183                 goto out_set;
184         }
185         /* Didn't find anything */
186         if (!set)
187                 goto out_nada;
188         auth_domain_cache.entries++;
189 out_set:
190         set_bit(CACHE_HASHED, &item->h.flags);
191         item->h.next = *head;
192         *head = &item->h;
193         write_unlock(&auth_domain_cache.hash_lock);
194         cache_fresh(&auth_domain_cache, &item->h, item->h.expiry_time);
195         cache_get(&item->h);
196         return item;
197 out_nada:
198         tmp = NULL;
199 out_noset:
200         read_unlock(&auth_domain_cache.hash_lock);
201         return tmp;
202 }
203
204 struct auth_domain *auth_domain_find(char *name)
205 {
206         struct auth_domain *rv, ad;
207
208         ad.name = name;
209         rv = auth_domain_lookup(&ad, 0);
210         return rv;
211 }