2 * linux/net/sunrpc/svcauth.c
4 * The generic interface for RPC authentication on the server side.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
9 * 19-Apr-2000 Chris Evans - Security fix
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>
21 #define RPCDBG_FACILITY RPCDBG_AUTH
25 * Table of authenticators
27 extern struct auth_ops svcauth_null;
28 extern struct auth_ops svcauth_unix;
30 static struct auth_ops *authtab[RPC_AUTH_MAXFLAVOR] = {
36 svc_authenticate(struct svc_rqst *rqstp, u32 *authp)
38 rpc_authflavor_t flavor;
39 struct auth_ops *aops;
43 flavor = ntohl(svc_getu32(&rqstp->rq_arg.head[0]));
45 dprintk("svc: svc_authenticate (%d)\n", flavor);
46 if (flavor >= RPC_AUTH_MAXFLAVOR || !(aops = authtab[flavor])) {
47 *authp = rpc_autherr_badcred;
51 rqstp->rq_authop = aops;
52 return aops->accept(rqstp, authp);
55 /* A request, which was authenticated, has now executed.
56 * Time to finalise the the credentials and verifier
57 * and release and resources
59 int svc_authorise(struct svc_rqst *rqstp)
61 struct auth_ops *aops = rqstp->rq_authop;
64 rqstp->rq_authop = NULL;
67 rv = aops->release(rqstp);
69 /* FIXME should I count and release authops */
74 svc_auth_register(rpc_authflavor_t flavor, struct auth_ops *aops)
76 if (flavor >= RPC_AUTH_MAXFLAVOR || authtab[flavor])
78 authtab[flavor] = aops;
83 svc_auth_unregister(rpc_authflavor_t flavor)
85 if (flavor < RPC_AUTH_MAXFLAVOR)
86 authtab[flavor] = NULL;
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,
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
112 #define DN_HASHBITS 6
113 #define DN_HASHMAX (1<<DN_HASHBITS)
114 #define DN_HASHMASK (DN_HASHMAX-1)
116 static struct cache_head *auth_domain_table[DN_HASHMAX];
117 void auth_domain_drop(struct cache_head *item, struct cache_detail *cd)
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);
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,
132 void auth_domain_put(struct auth_domain *dom)
134 auth_domain_drop(&dom->h, &auth_domain_cache);
137 static inline int auth_domain_hash(struct auth_domain *item)
139 return hash_str(item->name, DN_HASHBITS);
141 static inline int auth_domain_match(struct auth_domain *tmp, struct auth_domain *item)
143 return strcmp(tmp->name, item->name) == 0;
145 DefineCacheLookup(struct auth_domain,
148 (struct auth_domain *item, int set),
151 auth_domain_hash(item),
152 auth_domain_match(tmp, item),
153 kfree(new); if(!set) {
155 write_unlock(&auth_domain_cache.hash_lock);
157 read_unlock(&auth_domain_cache.hash_lock);
160 new=item; atomic_inc(&new->h.refcnt),
162 0 /* no inplace updates */
165 struct auth_domain *auth_domain_find(char *name)
167 struct auth_domain *rv, ad;
170 rv = auth_domain_lookup(&ad, 0);