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