48268c421ef03a709037aebdf74e81af7751d121
[samba.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 #include "../lib/util/data_blob.h"
27 #include "libcli/util/ntstatus.h"
28
29 #define GENSEC_SASL_NAME_NTLMSSP "NTLM"
30
31 #define GENSEC_OID_NTLMSSP "1.3.6.1.4.1.311.2.2.10"
32 #define GENSEC_OID_SPNEGO "1.3.6.1.5.5.2"
33 #define GENSEC_OID_KERBEROS5 "1.2.840.113554.1.2.2"
34 #define GENSEC_OID_KERBEROS5_OLD "1.2.840.48018.1.2.2"
35 #define GENSEC_OID_KERBEROS5_USER2USER "1.2.840.113554.1.2.2.3"
36
37 enum gensec_priority {
38         GENSEC_SPNEGO = 90,
39         GENSEC_GSSAPI = 80,
40         GENSEC_KRB5 = 70,
41         GENSEC_SCHANNEL = 60,
42         GENSEC_NTLMSSP = 50,
43         GENSEC_SASL = 20,
44         GENSEC_OTHER = 0
45 };
46
47 struct gensec_security;
48 struct gensec_target {
49         const char *principal;
50         const char *hostname;
51         const char *service;
52 };
53
54 #define GENSEC_FEATURE_SESSION_KEY      0x00000001
55 #define GENSEC_FEATURE_SIGN             0x00000002
56 #define GENSEC_FEATURE_SEAL             0x00000004
57 #define GENSEC_FEATURE_DCE_STYLE        0x00000008
58 #define GENSEC_FEATURE_ASYNC_REPLIES    0x00000010
59 #define GENSEC_FEATURE_DATAGRAM_MODE    0x00000020
60 #define GENSEC_FEATURE_SIGN_PKT_HEADER  0x00000040
61 #define GENSEC_FEATURE_NEW_SPNEGO       0x00000080
62
63 /* GENSEC mode */
64 enum gensec_role
65 {
66         GENSEC_SERVER,
67         GENSEC_CLIENT
68 };
69
70 struct auth_session_info;
71 struct cli_credentials;
72 struct gensec_settings;
73 struct tevent_context;
74 struct tevent_req;
75
76 struct gensec_settings {
77         struct loadparm_context *lp_ctx;
78         const char *target_hostname;
79 };
80
81 struct gensec_security_ops {
82         const char *name;
83         const char *sasl_name;
84         uint8_t auth_type;  /* 0 if not offered on DCE-RPC */
85         const char **oid;  /* NULL if not offered by SPNEGO */
86         NTSTATUS (*client_start)(struct gensec_security *gensec_security);
87         NTSTATUS (*server_start)(struct gensec_security *gensec_security);
88         /**
89            Determine if a packet has the right 'magic' for this mechanism
90         */
91         NTSTATUS (*magic)(struct gensec_security *gensec_security, 
92                           const DATA_BLOB *first_packet);
93         NTSTATUS (*update)(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
94                            const DATA_BLOB in, DATA_BLOB *out);
95         NTSTATUS (*seal_packet)(struct gensec_security *gensec_security, TALLOC_CTX *sig_mem_ctx,
96                                 uint8_t *data, size_t length, 
97                                 const uint8_t *whole_pdu, size_t pdu_length, 
98                                 DATA_BLOB *sig);
99         NTSTATUS (*sign_packet)(struct gensec_security *gensec_security, TALLOC_CTX *sig_mem_ctx,
100                                 const uint8_t *data, size_t length, 
101                                 const uint8_t *whole_pdu, size_t pdu_length, 
102                                 DATA_BLOB *sig);
103         size_t   (*sig_size)(struct gensec_security *gensec_security, size_t data_size);
104         size_t   (*max_input_size)(struct gensec_security *gensec_security);
105         size_t   (*max_wrapped_size)(struct gensec_security *gensec_security);
106         NTSTATUS (*check_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                                  const DATA_BLOB *sig);
110         NTSTATUS (*unseal_packet)(struct gensec_security *gensec_security, TALLOC_CTX *sig_mem_ctx,
111                                   uint8_t *data, size_t length, 
112                                   const uint8_t *whole_pdu, size_t pdu_length, 
113                                   const DATA_BLOB *sig);
114         NTSTATUS (*wrap)(struct gensec_security *gensec_security, 
115                                   TALLOC_CTX *mem_ctx, 
116                                   const DATA_BLOB *in, 
117                                   DATA_BLOB *out); 
118         NTSTATUS (*unwrap)(struct gensec_security *gensec_security, 
119                            TALLOC_CTX *mem_ctx, 
120                            const DATA_BLOB *in, 
121                            DATA_BLOB *out); 
122         NTSTATUS (*wrap_packets)(struct gensec_security *gensec_security, 
123                                  TALLOC_CTX *mem_ctx, 
124                                  const DATA_BLOB *in, 
125                                  DATA_BLOB *out,
126                                  size_t *len_processed); 
127         NTSTATUS (*unwrap_packets)(struct gensec_security *gensec_security, 
128                                    TALLOC_CTX *mem_ctx, 
129                                    const DATA_BLOB *in, 
130                                    DATA_BLOB *out,
131                                    size_t *len_processed); 
132         NTSTATUS (*packet_full_request)(struct gensec_security *gensec_security,
133                                         DATA_BLOB blob, size_t *size);
134         NTSTATUS (*session_key)(struct gensec_security *gensec_security, DATA_BLOB *session_key);
135         NTSTATUS (*session_info)(struct gensec_security *gensec_security, 
136                                  struct auth_session_info **session_info); 
137         void (*want_feature)(struct gensec_security *gensec_security,
138                                     uint32_t feature);
139         bool (*have_feature)(struct gensec_security *gensec_security,
140                                     uint32_t feature); 
141         bool enabled;
142         bool kerberos;
143         enum gensec_priority priority;
144 };
145         
146 struct gensec_security_ops_wrapper {
147         const struct gensec_security_ops *op;
148         const char *oid;
149 };
150
151 #define GENSEC_INTERFACE_VERSION 0
152
153 struct gensec_security {
154         const struct gensec_security_ops *ops;
155         void *private_data;
156         struct cli_credentials *credentials;
157         struct gensec_target target;
158         enum gensec_role gensec_role;
159         bool subcontext;
160         uint32_t want_features;
161         struct tevent_context *event_ctx;
162         struct tsocket_address *local_addr, *remote_addr;
163         struct gensec_settings *settings;
164         
165         /* When we are a server, this may be filled in to provide an
166          * NTLM authentication backend, and user lookup (such as if no
167          * PAC is found) */
168         struct auth_context *auth_context;
169 };
170
171 /* this structure is used by backends to determine the size of some critical types */
172 struct gensec_critical_sizes {
173         int interface_version;
174         int sizeof_gensec_security_ops;
175         int sizeof_gensec_security;
176 };
177
178 /* Socket wrapper */
179
180 struct gensec_security;
181 struct socket_context;
182 struct auth_context;
183 struct auth_user_info_dc;
184
185 NTSTATUS gensec_socket_init(struct gensec_security *gensec_security,
186                             TALLOC_CTX *mem_ctx, 
187                             struct socket_context *current_socket,
188                             struct tevent_context *ev,
189                             void (*recv_handler)(void *, uint16_t),
190                             void *recv_private,
191                             struct socket_context **new_socket);
192 /* These functions are for use here only (public because SPNEGO must
193  * use them for recursion) */
194 NTSTATUS gensec_wrap_packets(struct gensec_security *gensec_security, 
195                              TALLOC_CTX *mem_ctx, 
196                              const DATA_BLOB *in, 
197                              DATA_BLOB *out,
198                              size_t *len_processed);
199 /* These functions are for use here only (public because SPNEGO must
200  * use them for recursion) */
201 NTSTATUS gensec_unwrap_packets(struct gensec_security *gensec_security, 
202                                TALLOC_CTX *mem_ctx, 
203                                const DATA_BLOB *in, 
204                                DATA_BLOB *out,
205                                size_t *len_processed);
206
207 /* These functions are for use here only (public because SPNEGO must
208  * use them for recursion) */
209 NTSTATUS gensec_packet_full_request(struct gensec_security *gensec_security,
210                                     DATA_BLOB blob, size_t *size);
211
212 struct loadparm_context;
213
214 NTSTATUS gensec_subcontext_start(TALLOC_CTX *mem_ctx, 
215                                  struct gensec_security *parent, 
216                                  struct gensec_security **gensec_security);
217 NTSTATUS gensec_client_start(TALLOC_CTX *mem_ctx, 
218                              struct gensec_security **gensec_security,
219                              struct tevent_context *ev,
220                              struct gensec_settings *settings);
221 NTSTATUS gensec_start_mech_by_sasl_list(struct gensec_security *gensec_security, 
222                                                  const char **sasl_names);
223 NTSTATUS gensec_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx, 
224                        const DATA_BLOB in, DATA_BLOB *out);
225 struct tevent_req *gensec_update_send(TALLOC_CTX *mem_ctx,
226                                       struct tevent_context *ev,
227                                       struct gensec_security *gensec_security,
228                                       const DATA_BLOB in);
229 NTSTATUS gensec_update_recv(struct tevent_req *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 NTSTATUS gensec_init(struct loadparm_context *lp_ctx);
246 NTSTATUS gensec_unseal_packet(struct gensec_security *gensec_security, 
247                               TALLOC_CTX *mem_ctx, 
248                               uint8_t *data, size_t length, 
249                               const uint8_t *whole_pdu, size_t pdu_length, 
250                               const DATA_BLOB *sig);
251 NTSTATUS gensec_check_packet(struct gensec_security *gensec_security, 
252                              TALLOC_CTX *mem_ctx, 
253                              const uint8_t *data, size_t length, 
254                              const uint8_t *whole_pdu, size_t pdu_length, 
255                              const DATA_BLOB *sig);
256 size_t gensec_sig_size(struct gensec_security *gensec_security, size_t data_size);
257 NTSTATUS gensec_seal_packet(struct gensec_security *gensec_security, 
258                             TALLOC_CTX *mem_ctx, 
259                             uint8_t *data, size_t length, 
260                             const uint8_t *whole_pdu, size_t pdu_length, 
261                             DATA_BLOB *sig);
262 NTSTATUS gensec_sign_packet(struct gensec_security *gensec_security, 
263                             TALLOC_CTX *mem_ctx, 
264                             const uint8_t *data, size_t length, 
265                             const uint8_t *whole_pdu, size_t pdu_length, 
266                             DATA_BLOB *sig);
267 NTSTATUS gensec_start_mech_by_authtype(struct gensec_security *gensec_security, 
268                                        uint8_t auth_type, uint8_t auth_level);
269 const char *gensec_get_name_by_authtype(struct gensec_security *gensec_security, uint8_t authtype);
270 NTSTATUS gensec_server_start(TALLOC_CTX *mem_ctx, 
271                              struct tevent_context *ev,
272                              struct gensec_settings *settings,
273                              struct auth_context *auth_context,
274                              struct gensec_security **gensec_security);
275 NTSTATUS gensec_session_info(struct gensec_security *gensec_security, 
276                              struct auth_session_info **session_info);
277 struct netlogon_creds_CredentialState;
278 NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
279                                TALLOC_CTX *mem_ctx,
280                                struct netlogon_creds_CredentialState **creds);
281
282
283 NTSTATUS gensec_set_local_address(struct gensec_security *gensec_security,
284                 const struct tsocket_address *local);
285 NTSTATUS gensec_set_remote_address(struct gensec_security *gensec_security,
286                 const struct tsocket_address *remote);
287 const struct tsocket_address *gensec_get_local_address(struct gensec_security *gensec_security);
288 const struct tsocket_address *gensec_get_remote_address(struct gensec_security *gensec_security);
289
290 NTSTATUS gensec_start_mech_by_name(struct gensec_security *gensec_security, 
291                                         const char *name);
292
293 NTSTATUS gensec_unwrap(struct gensec_security *gensec_security, 
294                        TALLOC_CTX *mem_ctx, 
295                        const DATA_BLOB *in, 
296                        DATA_BLOB *out);
297 NTSTATUS gensec_wrap(struct gensec_security *gensec_security, 
298                      TALLOC_CTX *mem_ctx, 
299                      const DATA_BLOB *in, 
300                      DATA_BLOB *out);
301
302 struct gensec_security_ops **gensec_security_all(void);
303 bool gensec_security_ops_enabled(struct gensec_security_ops *ops, struct gensec_security *security);
304 struct gensec_security_ops **gensec_use_kerberos_mechs(TALLOC_CTX *mem_ctx, 
305                                                        struct gensec_security_ops **old_gensec_list, 
306                                                        struct cli_credentials *creds);
307
308 NTSTATUS gensec_start_mech_by_sasl_name(struct gensec_security *gensec_security, 
309                                         const char *sasl_name);
310
311 int gensec_setting_int(struct gensec_settings *settings, const char *mechanism, const char *name, int default_value);
312 bool gensec_setting_bool(struct gensec_settings *settings, const char *mechanism, const char *name, bool default_value);
313
314 NTSTATUS gensec_set_target_principal(struct gensec_security *gensec_security, const char *principal);
315
316 #endif /* __GENSEC_H__ */