s4-gensec: Added remote and local setter/getter using tsocket.
[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 #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 socket_address *my_addr, *peer_addr;
173         struct tsocket_address *local_addr, *remote_addr;
174         struct gensec_settings *settings;
175         
176         /* When we are a server, this may be filled in to provide an
177          * NTLM authentication backend, and user lookup (such as if no
178          * PAC is found) */
179         struct auth_context *auth_context;
180 };
181
182 /* this structure is used by backends to determine the size of some critical types */
183 struct gensec_critical_sizes {
184         int interface_version;
185         int sizeof_gensec_security_ops;
186         int sizeof_gensec_security;
187 };
188
189 /* Socket wrapper */
190
191 struct gensec_security;
192 struct socket_context;
193 struct auth_context;
194
195 NTSTATUS gensec_socket_init(struct gensec_security *gensec_security,
196                             TALLOC_CTX *mem_ctx, 
197                             struct socket_context *current_socket,
198                             struct tevent_context *ev,
199                             void (*recv_handler)(void *, uint16_t),
200                             void *recv_private,
201                             struct socket_context **new_socket);
202 /* These functions are for use here only (public because SPNEGO must
203  * use them for recursion) */
204 NTSTATUS gensec_wrap_packets(struct gensec_security *gensec_security, 
205                              TALLOC_CTX *mem_ctx, 
206                              const DATA_BLOB *in, 
207                              DATA_BLOB *out,
208                              size_t *len_processed);
209 /* These functions are for use here only (public because SPNEGO must
210  * use them for recursion) */
211 NTSTATUS gensec_unwrap_packets(struct gensec_security *gensec_security, 
212                                TALLOC_CTX *mem_ctx, 
213                                const DATA_BLOB *in, 
214                                DATA_BLOB *out,
215                                size_t *len_processed);
216
217 /* These functions are for use here only (public because SPNEGO must
218  * use them for recursion) */
219 NTSTATUS gensec_packet_full_request(struct gensec_security *gensec_security,
220                                     DATA_BLOB blob, size_t *size);
221
222 struct loadparm_context;
223
224 NTSTATUS gensec_subcontext_start(TALLOC_CTX *mem_ctx, 
225                                  struct gensec_security *parent, 
226                                  struct gensec_security **gensec_security);
227 NTSTATUS gensec_client_start(TALLOC_CTX *mem_ctx, 
228                              struct gensec_security **gensec_security,
229                              struct tevent_context *ev,
230                              struct gensec_settings *settings);
231 NTSTATUS gensec_start_mech_by_sasl_list(struct gensec_security *gensec_security, 
232                                                  const char **sasl_names);
233 NTSTATUS gensec_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx, 
234                        const DATA_BLOB in, DATA_BLOB *out);
235 void gensec_update_send(struct gensec_security *gensec_security, const DATA_BLOB in,
236                                  void (*callback)(struct gensec_update_request *req, void *private_data),
237                                  void *private_data);
238 NTSTATUS gensec_update_recv(struct gensec_update_request *req, TALLOC_CTX *out_mem_ctx, DATA_BLOB *out);
239 void gensec_want_feature(struct gensec_security *gensec_security,
240                          uint32_t feature);
241 bool gensec_have_feature(struct gensec_security *gensec_security,
242                          uint32_t feature);
243 NTSTATUS gensec_set_credentials(struct gensec_security *gensec_security, struct cli_credentials *credentials);
244 NTSTATUS gensec_set_target_service(struct gensec_security *gensec_security, const char *service);
245 const char *gensec_get_target_service(struct gensec_security *gensec_security);
246 NTSTATUS gensec_set_target_hostname(struct gensec_security *gensec_security, const char *hostname);
247 const char *gensec_get_target_hostname(struct gensec_security *gensec_security);
248 NTSTATUS gensec_session_key(struct gensec_security *gensec_security, 
249                             DATA_BLOB *session_key);
250 NTSTATUS gensec_start_mech_by_oid(struct gensec_security *gensec_security, 
251                                   const char *mech_oid);
252 const char *gensec_get_name_by_oid(struct gensec_security *gensec_security, const char *oid_string);
253 struct cli_credentials *gensec_get_credentials(struct gensec_security *gensec_security);
254 struct socket_address *gensec_get_peer_addr(struct gensec_security *gensec_security);
255 NTSTATUS gensec_init(struct loadparm_context *lp_ctx);
256 NTSTATUS gensec_unseal_packet(struct gensec_security *gensec_security, 
257                               TALLOC_CTX *mem_ctx, 
258                               uint8_t *data, size_t length, 
259                               const uint8_t *whole_pdu, size_t pdu_length, 
260                               const DATA_BLOB *sig);
261 NTSTATUS gensec_check_packet(struct gensec_security *gensec_security, 
262                              TALLOC_CTX *mem_ctx, 
263                              const uint8_t *data, size_t length, 
264                              const uint8_t *whole_pdu, size_t pdu_length, 
265                              const DATA_BLOB *sig);
266 size_t gensec_sig_size(struct gensec_security *gensec_security, size_t data_size);
267 NTSTATUS gensec_seal_packet(struct gensec_security *gensec_security, 
268                             TALLOC_CTX *mem_ctx, 
269                             uint8_t *data, size_t length, 
270                             const uint8_t *whole_pdu, size_t pdu_length, 
271                             DATA_BLOB *sig);
272 NTSTATUS gensec_sign_packet(struct gensec_security *gensec_security, 
273                             TALLOC_CTX *mem_ctx, 
274                             const uint8_t *data, size_t length, 
275                             const uint8_t *whole_pdu, size_t pdu_length, 
276                             DATA_BLOB *sig);
277 NTSTATUS gensec_start_mech_by_authtype(struct gensec_security *gensec_security, 
278                                        uint8_t auth_type, uint8_t auth_level);
279 const char *gensec_get_name_by_authtype(struct gensec_security *gensec_security, uint8_t authtype);
280 NTSTATUS gensec_server_start(TALLOC_CTX *mem_ctx, 
281                              struct tevent_context *ev,
282                              struct gensec_settings *settings,
283                              struct auth_context *auth_context,
284                              struct gensec_security **gensec_security);
285 NTSTATUS gensec_session_info(struct gensec_security *gensec_security, 
286                              struct auth_session_info **session_info);
287 NTSTATUS auth_nt_status_squash(NTSTATUS nt_status);
288 struct netlogon_creds_CredentialState;
289 NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
290                                TALLOC_CTX *mem_ctx,
291                                struct netlogon_creds_CredentialState **creds);
292
293
294 NTSTATUS gensec_set_local_address(struct gensec_security *gensec_security,
295                 const struct tsocket_address *local);
296 NTSTATUS gensec_set_remote_address(struct gensec_security *gensec_security,
297                 const struct tsocket_address *remote);
298 const struct tsocket_address *gensec_get_local_address(struct gensec_security *gensec_security);
299 const struct tsocket_address *gensec_get_remote_address(struct gensec_security *gensec_security);
300
301 NTSTATUS gensec_set_peer_addr(struct gensec_security *gensec_security, struct socket_address *peer_addr);
302 NTSTATUS gensec_set_my_addr(struct gensec_security *gensec_security, struct socket_address *my_addr);
303
304 NTSTATUS gensec_start_mech_by_name(struct gensec_security *gensec_security, 
305                                         const char *name);
306
307 NTSTATUS gensec_unwrap(struct gensec_security *gensec_security, 
308                        TALLOC_CTX *mem_ctx, 
309                        const DATA_BLOB *in, 
310                        DATA_BLOB *out);
311 NTSTATUS gensec_wrap(struct gensec_security *gensec_security, 
312                      TALLOC_CTX *mem_ctx, 
313                      const DATA_BLOB *in, 
314                      DATA_BLOB *out);
315
316 struct gensec_security_ops **gensec_security_all(void);
317 bool gensec_security_ops_enabled(struct gensec_security_ops *ops, struct gensec_security *security);
318 struct gensec_security_ops **gensec_use_kerberos_mechs(TALLOC_CTX *mem_ctx, 
319                                                        struct gensec_security_ops **old_gensec_list, 
320                                                        struct cli_credentials *creds);
321
322 NTSTATUS gensec_start_mech_by_sasl_name(struct gensec_security *gensec_security, 
323                                         const char *sasl_name);
324
325 int gensec_setting_int(struct gensec_settings *settings, const char *mechanism, const char *name, int default_value);
326 bool gensec_setting_bool(struct gensec_settings *settings, const char *mechanism, const char *name, bool default_value);
327
328 #endif /* __GENSEC_H__ */