235d2008fb253128877e89ccf09a04a56525982d
[ira/wip.git] / source4 / auth / gensec / gensec.h
1 /* 
2    Unix SMB/CIFS implementation.
3  
4    Generic Authentication Interface
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #ifndef __GENSEC_H__
24 #define __GENSEC_H__
25
26 #define GENSEC_OID_NTLMSSP "1.3.6.1.4.1.311.2.2.10"
27 #define GENSEC_OID_SPNEGO "1.3.6.1.5.5.2"
28 #define GENSEC_OID_KERBEROS5 "1.2.840.113554.1.2.2"
29 #define GENSEC_OID_KERBEROS5_OLD "1.2.840.48018.1.2.2"
30 #define GENSEC_OID_KERBEROS5_USER2USER "1.2.840.113554.1.2.2.3"
31
32 enum gensec_priority {
33         GENSEC_SPNEGO = 90,
34         GENSEC_GSSAPI = 80,
35         GENSEC_KRB5 = 70,
36         GENSEC_SCHANNEL = 60,
37         GENSEC_NTLMSSP = 50,
38         GENSEC_SASL = 20,
39         GENSEC_OTHER = 0
40 };
41
42 struct gensec_security;
43 struct gensec_target {
44         const char *principal;
45         const char *hostname;
46         const char *service;
47 };
48
49 #define GENSEC_FEATURE_SESSION_KEY      0x00000001
50 #define GENSEC_FEATURE_SIGN             0x00000002
51 #define GENSEC_FEATURE_SEAL             0x00000004
52 #define GENSEC_FEATURE_DCE_STYLE        0x00000008
53 #define GENSEC_FEATURE_ASYNC_REPLIES    0x00000010
54 #define GENSEC_FEATURE_DATAGRAM_MODE    0x00000020
55 #define GENSEC_FEATURE_SIGN_PKT_HEADER  0x00000040
56 #define GENSEC_FEATURE_NEW_SPNEGO       0x00000080
57
58 /* GENSEC mode */
59 enum gensec_role
60 {
61         GENSEC_SERVER,
62         GENSEC_CLIENT
63 };
64
65 struct auth_session_info;
66 struct cli_credentials;
67 struct gensec_settings;
68 struct event_context;
69
70 struct gensec_update_request {
71         struct gensec_security *gensec_security;
72         void *private_data;
73         DATA_BLOB in;
74         DATA_BLOB out;
75         NTSTATUS status;
76         struct {
77                 void (*fn)(struct gensec_update_request *req, void *private_data);
78                 void *private_data;
79         } callback;
80 };
81
82 struct gensec_settings {
83         struct loadparm_context *lp_ctx;
84         struct smb_iconv_convenience *iconv_convenience;
85         const char *target_hostname;
86 };
87
88 struct gensec_security_ops {
89         const char *name;
90         const char *sasl_name;
91         uint8_t auth_type;  /* 0 if not offered on DCE-RPC */
92         const char **oid;  /* NULL if not offered by SPNEGO */
93         NTSTATUS (*client_start)(struct gensec_security *gensec_security);
94         NTSTATUS (*server_start)(struct gensec_security *gensec_security);
95         /**
96            Determine if a packet has the right 'magic' for this mechanism
97         */
98         NTSTATUS (*magic)(struct gensec_security *gensec_security, 
99                           const DATA_BLOB *first_packet);
100         NTSTATUS (*update)(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
101                            const DATA_BLOB in, DATA_BLOB *out);
102         NTSTATUS (*seal_packet)(struct gensec_security *gensec_security, TALLOC_CTX *sig_mem_ctx,
103                                 uint8_t *data, size_t length, 
104                                 const uint8_t *whole_pdu, size_t pdu_length, 
105                                 DATA_BLOB *sig);
106         NTSTATUS (*sign_packet)(struct gensec_security *gensec_security, TALLOC_CTX *sig_mem_ctx,
107                                 const uint8_t *data, size_t length, 
108                                 const uint8_t *whole_pdu, size_t pdu_length, 
109                                 DATA_BLOB *sig);
110         size_t   (*sig_size)(struct gensec_security *gensec_security, size_t data_size);
111         size_t   (*max_input_size)(struct gensec_security *gensec_security);
112         size_t   (*max_wrapped_size)(struct gensec_security *gensec_security);
113         NTSTATUS (*check_packet)(struct gensec_security *gensec_security, TALLOC_CTX *sig_mem_ctx, 
114                                  const uint8_t *data, size_t length, 
115                                  const uint8_t *whole_pdu, size_t pdu_length, 
116                                  const DATA_BLOB *sig);
117         NTSTATUS (*unseal_packet)(struct gensec_security *gensec_security, TALLOC_CTX *sig_mem_ctx,
118                                   uint8_t *data, size_t length, 
119                                   const uint8_t *whole_pdu, size_t pdu_length, 
120                                   const DATA_BLOB *sig);
121         NTSTATUS (*wrap)(struct gensec_security *gensec_security, 
122                                   TALLOC_CTX *mem_ctx, 
123                                   const DATA_BLOB *in, 
124                                   DATA_BLOB *out); 
125         NTSTATUS (*unwrap)(struct gensec_security *gensec_security, 
126                            TALLOC_CTX *mem_ctx, 
127                            const DATA_BLOB *in, 
128                            DATA_BLOB *out); 
129         NTSTATUS (*wrap_packets)(struct gensec_security *gensec_security, 
130                                  TALLOC_CTX *mem_ctx, 
131                                  const DATA_BLOB *in, 
132                                  DATA_BLOB *out,
133                                  size_t *len_processed); 
134         NTSTATUS (*unwrap_packets)(struct gensec_security *gensec_security, 
135                                    TALLOC_CTX *mem_ctx, 
136                                    const DATA_BLOB *in, 
137                                    DATA_BLOB *out,
138                                    size_t *len_processed); 
139         NTSTATUS (*packet_full_request)(struct gensec_security *gensec_security,
140                                         DATA_BLOB blob, size_t *size);
141         NTSTATUS (*session_key)(struct gensec_security *gensec_security, DATA_BLOB *session_key);
142         NTSTATUS (*session_info)(struct gensec_security *gensec_security, 
143                                  struct auth_session_info **session_info); 
144         void (*want_feature)(struct gensec_security *gensec_security,
145                                     uint32_t feature);
146         bool (*have_feature)(struct gensec_security *gensec_security,
147                                     uint32_t feature); 
148         bool enabled;
149         bool kerberos;
150         enum gensec_priority priority;
151 };
152         
153 struct gensec_security_ops_wrapper {
154         const struct gensec_security_ops *op;
155         const char *oid;
156 };
157
158 #define GENSEC_INTERFACE_VERSION 0
159
160 struct gensec_security {
161         const struct gensec_security_ops *ops;
162         void *private_data;
163         struct cli_credentials *credentials;
164         struct gensec_target target;
165         enum gensec_role gensec_role;
166         bool subcontext;
167         uint32_t want_features;
168         struct event_context *event_ctx;
169         struct messaging_context *msg_ctx; /* only valid as server */
170         struct socket_address *my_addr, *peer_addr;
171         struct gensec_settings *settings;
172 };
173
174 /* this structure is used by backends to determine the size of some critical types */
175 struct gensec_critical_sizes {
176         int interface_version;
177         int sizeof_gensec_security_ops;
178         int sizeof_gensec_security;
179 };
180
181 /* Socket wrapper */
182
183 struct gensec_security;
184 struct socket_context;
185
186 NTSTATUS gensec_socket_init(struct gensec_security *gensec_security,
187                             TALLOC_CTX *mem_ctx, 
188                             struct socket_context *current_socket,
189                             struct event_context *ev,
190                             void (*recv_handler)(void *, uint16_t),
191                             void *recv_private,
192                             struct socket_context **new_socket);
193 /* These functions are for use here only (public because SPNEGO must
194  * use them for recursion) */
195 NTSTATUS gensec_wrap_packets(struct gensec_security *gensec_security, 
196                              TALLOC_CTX *mem_ctx, 
197                              const DATA_BLOB *in, 
198                              DATA_BLOB *out,
199                              size_t *len_processed);
200 /* These functions are for use here only (public because SPNEGO must
201  * use them for recursion) */
202 NTSTATUS gensec_unwrap_packets(struct gensec_security *gensec_security, 
203                                TALLOC_CTX *mem_ctx, 
204                                const DATA_BLOB *in, 
205                                DATA_BLOB *out,
206                                size_t *len_processed);
207
208 /* These functions are for use here only (public because SPNEGO must
209  * use them for recursion) */
210 NTSTATUS gensec_packet_full_request(struct gensec_security *gensec_security,
211                                     DATA_BLOB blob, size_t *size);
212
213 struct loadparm_context;
214
215 NTSTATUS gensec_subcontext_start(TALLOC_CTX *mem_ctx, 
216                                  struct gensec_security *parent, 
217                                  struct gensec_security **gensec_security);
218 NTSTATUS gensec_client_start(TALLOC_CTX *mem_ctx, 
219                              struct gensec_security **gensec_security,
220                              struct event_context *ev,
221                              struct gensec_settings *settings);
222 NTSTATUS gensec_start_mech_by_sasl_list(struct gensec_security *gensec_security, 
223                                                  const char **sasl_names);
224 NTSTATUS gensec_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx, 
225                        const DATA_BLOB in, DATA_BLOB *out);
226 void gensec_update_send(struct gensec_security *gensec_security, const DATA_BLOB in,
227                                  void (*callback)(struct gensec_update_request *req, void *private_data),
228                                  void *private_data);
229 NTSTATUS gensec_update_recv(struct gensec_update_request *req, TALLOC_CTX *out_mem_ctx, DATA_BLOB *out);
230 void gensec_want_feature(struct gensec_security *gensec_security,
231                          uint32_t feature);
232 bool gensec_have_feature(struct gensec_security *gensec_security,
233                          uint32_t feature);
234 NTSTATUS gensec_set_credentials(struct gensec_security *gensec_security, struct cli_credentials *credentials);
235 NTSTATUS gensec_set_target_service(struct gensec_security *gensec_security, const char *service);
236 const char *gensec_get_target_service(struct gensec_security *gensec_security);
237 NTSTATUS gensec_set_target_hostname(struct gensec_security *gensec_security, const char *hostname);
238 const char *gensec_get_target_hostname(struct gensec_security *gensec_security);
239 NTSTATUS gensec_session_key(struct gensec_security *gensec_security, 
240                             DATA_BLOB *session_key);
241 NTSTATUS gensec_start_mech_by_oid(struct gensec_security *gensec_security, 
242                                   const char *mech_oid);
243 const char *gensec_get_name_by_oid(struct gensec_security *gensec_security, const char *oid_string);
244 struct cli_credentials *gensec_get_credentials(struct gensec_security *gensec_security);
245 struct socket_address *gensec_get_peer_addr(struct gensec_security *gensec_security);
246 NTSTATUS gensec_init(struct loadparm_context *lp_ctx);
247 NTSTATUS gensec_unseal_packet(struct gensec_security *gensec_security, 
248                               TALLOC_CTX *mem_ctx, 
249                               uint8_t *data, size_t length, 
250                               const uint8_t *whole_pdu, size_t pdu_length, 
251                               const DATA_BLOB *sig);
252 NTSTATUS gensec_check_packet(struct gensec_security *gensec_security, 
253                              TALLOC_CTX *mem_ctx, 
254                              const uint8_t *data, size_t length, 
255                              const uint8_t *whole_pdu, size_t pdu_length, 
256                              const DATA_BLOB *sig);
257 size_t gensec_sig_size(struct gensec_security *gensec_security, size_t data_size);
258 NTSTATUS gensec_seal_packet(struct gensec_security *gensec_security, 
259                             TALLOC_CTX *mem_ctx, 
260                             uint8_t *data, size_t length, 
261                             const uint8_t *whole_pdu, size_t pdu_length, 
262                             DATA_BLOB *sig);
263 NTSTATUS gensec_sign_packet(struct gensec_security *gensec_security, 
264                             TALLOC_CTX *mem_ctx, 
265                             const uint8_t *data, size_t length, 
266                             const uint8_t *whole_pdu, size_t pdu_length, 
267                             DATA_BLOB *sig);
268 NTSTATUS gensec_start_mech_by_authtype(struct gensec_security *gensec_security, 
269                                        uint8_t auth_type, uint8_t auth_level);
270 const char *gensec_get_name_by_authtype(struct gensec_security *gensec_security, uint8_t authtype);
271 NTSTATUS gensec_server_start(TALLOC_CTX *mem_ctx, 
272                              struct event_context *ev,
273                              struct gensec_settings *settings,
274                              struct messaging_context *msg,
275                              struct gensec_security **gensec_security);
276 NTSTATUS gensec_session_info(struct gensec_security *gensec_security, 
277                              struct auth_session_info **session_info);
278 NTSTATUS auth_nt_status_squash(NTSTATUS nt_status);
279 struct creds_CredentialState;
280 NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
281                                TALLOC_CTX *mem_ctx,
282                                struct creds_CredentialState **creds);
283 NTSTATUS gensec_set_peer_addr(struct gensec_security *gensec_security, struct socket_address *peer_addr);
284 NTSTATUS gensec_set_my_addr(struct gensec_security *gensec_security, struct socket_address *my_addr);
285
286 NTSTATUS gensec_start_mech_by_name(struct gensec_security *gensec_security, 
287                                         const char *name);
288
289 NTSTATUS gensec_unwrap(struct gensec_security *gensec_security, 
290                        TALLOC_CTX *mem_ctx, 
291                        const DATA_BLOB *in, 
292                        DATA_BLOB *out);
293 NTSTATUS gensec_wrap(struct gensec_security *gensec_security, 
294                      TALLOC_CTX *mem_ctx, 
295                      const DATA_BLOB *in, 
296                      DATA_BLOB *out);
297
298 struct gensec_security_ops **gensec_security_all(void);
299 bool gensec_security_ops_enabled(struct gensec_security_ops *ops, struct loadparm_context *lp_ctx);
300 struct gensec_security_ops **gensec_use_kerberos_mechs(TALLOC_CTX *mem_ctx, 
301                                                        struct gensec_security_ops **old_gensec_list, 
302                                                        struct cli_credentials *creds);
303
304 NTSTATUS gensec_start_mech_by_sasl_name(struct gensec_security *gensec_security, 
305                                         const char *sasl_name);
306
307 int gensec_setting_int(struct gensec_settings *settings, const char *mechanism, const char *name, int default_value);
308 bool gensec_setting_bool(struct gensec_settings *settings, const char *mechanism, const char *name, bool default_value);
309
310 #endif /* __GENSEC_H__ */