nfsd4: move principal name into svc_cred
[linux-2.6-block.git] / include / linux / sunrpc / svcauth.h
1 /*
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>
18 #include <linux/cred.h>
19
20 struct svc_cred {
21         uid_t                   cr_uid;
22         gid_t                   cr_gid;
23         struct group_info       *cr_group_info;
24         char                    *cr_principal; /* for gss */
25 };
26
27 static inline void free_svc_cred(struct svc_cred *cred)
28 {
29         if (cred->cr_group_info)
30                 put_group_info(cred->cr_group_info);
31         kfree(cred->cr_principal);
32 }
33
34 struct svc_rqst;                /* forward decl */
35 struct in6_addr;
36
37 /* Authentication is done in the context of a domain.
38  *
39  * Currently, the nfs server uses the auth_domain to stand
40  * for the "client" listed in /etc/exports.
41  *
42  * More generally, a domain might represent a group of clients using
43  * a common mechanism for authentication and having a common mapping
44  * between local identity (uid) and network identity.  All clients
45  * in a domain have similar general access rights.  Each domain can
46  * contain multiple principals which will have different specific right
47  * based on normal Discretionary Access Control.
48  *
49  * A domain is created by an authentication flavour module based on name
50  * only.  Userspace then fills in detail on demand.
51  *
52  * In the case of auth_unix and auth_null, the auth_domain is also
53  * associated with entries in another cache representing the mapping
54  * of ip addresses to the given client.
55  */
56 struct auth_domain {
57         struct kref             ref;
58         struct hlist_node       hash;
59         char                    *name;
60         struct auth_ops         *flavour;
61 };
62
63 /*
64  * Each authentication flavour registers an auth_ops
65  * structure.
66  * name is simply the name.
67  * flavour gives the auth flavour. It determines where the flavour is registered
68  * accept() is given a request and should verify it.
69  *   It should inspect the authenticator and verifier, and possibly the data.
70  *    If there is a problem with the authentication *authp should be set.
71  *    The return value of accept() can indicate:
72  *      OK - authorised. client and credential are set in rqstp.
73  *           reqbuf points to arguments
74  *           resbuf points to good place for results.  verfier
75  *             is (probably) already in place.  Certainly space is
76  *             reserved for it.
77  *      DROP - simply drop the request. It may have been deferred
78  *      GARBAGE - rpc garbage_args error
79  *      SYSERR - rpc system_err error
80  *      DENIED - authp holds reason for denial.
81  *      COMPLETE - the reply is encoded already and ready to be sent; no
82  *              further processing is necessary.  (This is used for processing
83  *              null procedure calls which are used to set up encryption
84  *              contexts.)
85  *
86  *   accept is passed the proc number so that it can accept NULL rpc requests
87  *   even if it cannot authenticate the client (as is sometimes appropriate).
88  *
89  * release() is given a request after the procedure has been run.
90  *  It should sign/encrypt the results if needed
91  * It should return:
92  *    OK - the resbuf is ready to be sent
93  *    DROP - the reply should be quitely dropped
94  *    DENIED - authp holds a reason for MSG_DENIED
95  *    SYSERR - rpc system_err
96  *
97  * domain_release()
98  *   This call releases a domain.
99  * set_client()
100  *   Givens a pending request (struct svc_rqst), finds and assigns
101  *   an appropriate 'auth_domain' as the client.
102  */
103 struct auth_ops {
104         char *  name;
105         struct module *owner;
106         int     flavour;
107         int     (*accept)(struct svc_rqst *rq, __be32 *authp);
108         int     (*release)(struct svc_rqst *rq);
109         void    (*domain_release)(struct auth_domain *);
110         int     (*set_client)(struct svc_rqst *rq);
111 };
112
113 #define SVC_GARBAGE     1
114 #define SVC_SYSERR      2
115 #define SVC_VALID       3
116 #define SVC_NEGATIVE    4
117 #define SVC_OK          5
118 #define SVC_DROP        6
119 #define SVC_CLOSE       7       /* Like SVC_DROP, but request is definitely
120                                  * lost so if there is a tcp connection, it
121                                  * should be closed
122                                  */
123 #define SVC_DENIED      8
124 #define SVC_PENDING     9
125 #define SVC_COMPLETE    10
126
127 struct svc_xprt;
128
129 extern int      svc_authenticate(struct svc_rqst *rqstp, __be32 *authp);
130 extern int      svc_authorise(struct svc_rqst *rqstp);
131 extern int      svc_set_client(struct svc_rqst *rqstp);
132 extern int      svc_auth_register(rpc_authflavor_t flavor, struct auth_ops *aops);
133 extern void     svc_auth_unregister(rpc_authflavor_t flavor);
134
135 extern struct auth_domain *unix_domain_find(char *name);
136 extern void auth_domain_put(struct auth_domain *item);
137 extern int auth_unix_add_addr(struct net *net, struct in6_addr *addr, struct auth_domain *dom);
138 extern struct auth_domain *auth_domain_lookup(char *name, struct auth_domain *new);
139 extern struct auth_domain *auth_domain_find(char *name);
140 extern struct auth_domain *auth_unix_lookup(struct net *net, struct in6_addr *addr);
141 extern int auth_unix_forget_old(struct auth_domain *dom);
142 extern void svcauth_unix_purge(struct net *net);
143 extern void svcauth_unix_info_release(struct svc_xprt *xpt);
144 extern int svcauth_unix_set_client(struct svc_rqst *rqstp);
145
146 extern int unix_gid_cache_create(struct net *net);
147 extern void unix_gid_cache_destroy(struct net *net);
148
149 static inline unsigned long hash_str(char *name, int bits)
150 {
151         unsigned long hash = 0;
152         unsigned long l = 0;
153         int len = 0;
154         unsigned char c;
155         do {
156                 if (unlikely(!(c = *name++))) {
157                         c = (char)len; len = -1;
158                 }
159                 l = (l << 8) | c;
160                 len++;
161                 if ((len & (BITS_PER_LONG/8-1))==0)
162                         hash = hash_long(hash^l, BITS_PER_LONG);
163         } while (len);
164         return hash >> (BITS_PER_LONG - bits);
165 }
166
167 static inline unsigned long hash_mem(char *buf, int length, int bits)
168 {
169         unsigned long hash = 0;
170         unsigned long l = 0;
171         int len = 0;
172         unsigned char c;
173         do {
174                 if (len == length) {
175                         c = (char)len; len = -1;
176                 } else
177                         c = *buf++;
178                 l = (l << 8) | c;
179                 len++;
180                 if ((len & (BITS_PER_LONG/8-1))==0)
181                         hash = hash_long(hash^l, BITS_PER_LONG);
182         } while (len);
183         return hash >> (BITS_PER_LONG - bits);
184 }
185
186 #endif /* __KERNEL__ */
187
188 #endif /* _LINUX_SUNRPC_SVCAUTH_H_ */