blob: ff374ab3083902a7052a07b2ca8bb40cf3136338 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/include/linux/sunrpc/svcauth.h
3 *
4 * RPC server-side authentication stuff.
5 *
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 */
8
9#ifndef _LINUX_SUNRPC_SVCAUTH_H_
10#define _LINUX_SUNRPC_SVCAUTH_H_
11
12#ifdef __KERNEL__
13
14#include <linux/string.h>
15#include <linux/sunrpc/msg_prot.h>
16#include <linux/sunrpc/cache.h>
17#include <linux/hash.h>
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -040018#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020struct svc_cred {
Eric W. Biederman7eaf0402013-02-01 16:31:17 -080021 kuid_t cr_uid;
22 kgid_t cr_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 struct group_info *cr_group_info;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -040024 u32 cr_flavor; /* pseudoflavor */
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -040025 char *cr_principal; /* for gss */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026};
27
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -040028static inline void free_svc_cred(struct svc_cred *cred)
29{
30 if (cred->cr_group_info)
31 put_group_info(cred->cr_group_info);
32 kfree(cred->cr_principal);
33}
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035struct svc_rqst; /* forward decl */
Aurélien Charbonf15364b2008-01-18 15:50:56 +010036struct in6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* Authentication is done in the context of a domain.
39 *
40 * Currently, the nfs server uses the auth_domain to stand
41 * for the "client" listed in /etc/exports.
42 *
43 * More generally, a domain might represent a group of clients using
44 * a common mechanism for authentication and having a common mapping
45 * between local identity (uid) and network identity. All clients
46 * in a domain have similar general access rights. Each domain can
47 * contain multiple principals which will have different specific right
48 * based on normal Discretionary Access Control.
49 *
50 * A domain is created by an authentication flavour module based on name
51 * only. Userspace then fills in detail on demand.
52 *
53 * In the case of auth_unix and auth_null, the auth_domain is also
54 * associated with entries in another cache representing the mapping
55 * of ip addresses to the given client.
56 */
57struct auth_domain {
NeilBrownefc36aa2006-03-27 01:14:59 -080058 struct kref ref;
59 struct hlist_node hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 char *name;
NeilBrownefc36aa2006-03-27 01:14:59 -080061 struct auth_ops *flavour;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64/*
65 * Each authentication flavour registers an auth_ops
66 * structure.
67 * name is simply the name.
68 * flavour gives the auth flavour. It determines where the flavour is registered
69 * accept() is given a request and should verify it.
70 * It should inspect the authenticator and verifier, and possibly the data.
71 * If there is a problem with the authentication *authp should be set.
72 * The return value of accept() can indicate:
73 * OK - authorised. client and credential are set in rqstp.
74 * reqbuf points to arguments
75 * resbuf points to good place for results. verfier
76 * is (probably) already in place. Certainly space is
77 * reserved for it.
78 * DROP - simply drop the request. It may have been deferred
79 * GARBAGE - rpc garbage_args error
80 * SYSERR - rpc system_err error
81 * DENIED - authp holds reason for denial.
82 * COMPLETE - the reply is encoded already and ready to be sent; no
83 * further processing is necessary. (This is used for processing
84 * null procedure calls which are used to set up encryption
85 * contexts.)
86 *
87 * accept is passed the proc number so that it can accept NULL rpc requests
88 * even if it cannot authenticate the client (as is sometimes appropriate).
89 *
90 * release() is given a request after the procedure has been run.
91 * It should sign/encrypt the results if needed
92 * It should return:
93 * OK - the resbuf is ready to be sent
94 * DROP - the reply should be quitely dropped
95 * DENIED - authp holds a reason for MSG_DENIED
96 * SYSERR - rpc system_err
97 *
98 * domain_release()
99 * This call releases a domain.
NeilBrownefc36aa2006-03-27 01:14:59 -0800100 * set_client()
101 * Givens a pending request (struct svc_rqst), finds and assigns
102 * an appropriate 'auth_domain' as the client.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
104struct auth_ops {
105 char * name;
106 struct module *owner;
107 int flavour;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700108 int (*accept)(struct svc_rqst *rq, __be32 *authp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int (*release)(struct svc_rqst *rq);
110 void (*domain_release)(struct auth_domain *);
111 int (*set_client)(struct svc_rqst *rq);
112};
113
114#define SVC_GARBAGE 1
115#define SVC_SYSERR 2
116#define SVC_VALID 3
117#define SVC_NEGATIVE 4
118#define SVC_OK 5
119#define SVC_DROP 6
NeilBrown1ebede82010-08-12 17:04:07 +1000120#define SVC_CLOSE 7 /* Like SVC_DROP, but request is definitely
121 * lost so if there is a tcp connection, it
122 * should be closed
123 */
124#define SVC_DENIED 8
125#define SVC_PENDING 9
126#define SVC_COMPLETE 10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Pavel Emelyanove3bfca02010-09-27 13:58:42 +0400128struct svc_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700130extern int svc_authenticate(struct svc_rqst *rqstp, __be32 *authp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131extern int svc_authorise(struct svc_rqst *rqstp);
132extern int svc_set_client(struct svc_rqst *rqstp);
133extern int svc_auth_register(rpc_authflavor_t flavor, struct auth_ops *aops);
134extern void svc_auth_unregister(rpc_authflavor_t flavor);
135
136extern struct auth_domain *unix_domain_find(char *name);
137extern void auth_domain_put(struct auth_domain *item);
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400138extern int auth_unix_add_addr(struct net *net, struct in6_addr *addr, struct auth_domain *dom);
NeilBrownefc36aa2006-03-27 01:14:59 -0800139extern struct auth_domain *auth_domain_lookup(char *name, struct auth_domain *new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140extern struct auth_domain *auth_domain_find(char *name);
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400141extern struct auth_domain *auth_unix_lookup(struct net *net, struct in6_addr *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142extern int auth_unix_forget_old(struct auth_domain *dom);
Stanislav Kinsburskye5f06f72012-04-11 15:13:28 +0400143extern void svcauth_unix_purge(struct net *net);
Pavel Emelyanove3bfca02010-09-27 13:58:42 +0400144extern void svcauth_unix_info_release(struct svc_xprt *xpt);
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700145extern int svcauth_unix_set_client(struct svc_rqst *rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Trond Myklebust09acfea2012-03-11 15:22:54 -0400147extern int unix_gid_cache_create(struct net *net);
148extern void unix_gid_cache_destroy(struct net *net);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static inline unsigned long hash_str(char *name, int bits)
151{
152 unsigned long hash = 0;
153 unsigned long l = 0;
154 int len = 0;
155 unsigned char c;
156 do {
157 if (unlikely(!(c = *name++))) {
158 c = (char)len; len = -1;
159 }
160 l = (l << 8) | c;
161 len++;
162 if ((len & (BITS_PER_LONG/8-1))==0)
163 hash = hash_long(hash^l, BITS_PER_LONG);
164 } while (len);
165 return hash >> (BITS_PER_LONG - bits);
166}
167
168static inline unsigned long hash_mem(char *buf, int length, int bits)
169{
170 unsigned long hash = 0;
171 unsigned long l = 0;
172 int len = 0;
173 unsigned char c;
174 do {
175 if (len == length) {
176 c = (char)len; len = -1;
177 } else
178 c = *buf++;
179 l = (l << 8) | c;
180 len++;
181 if ((len & (BITS_PER_LONG/8-1))==0)
182 hash = hash_long(hash^l, BITS_PER_LONG);
183 } while (len);
184 return hash >> (BITS_PER_LONG - bits);
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#endif /* __KERNEL__ */
188
189#endif /* _LINUX_SUNRPC_SVCAUTH_H_ */