gensec: Add a way to request a unix token from GENSEC
[amitay/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 #define GENSEC_FEATURE_UNIX_TOKEN       0x00000100
63
64 /* GENSEC mode */
65 enum gensec_role
66 {
67         GENSEC_SERVER,
68         GENSEC_CLIENT
69 };
70
71 struct auth_session_info;
72 struct cli_credentials;
73 struct gensec_settings;
74 struct tevent_context;
75 struct tevent_req;
76
77 struct gensec_settings {
78         struct loadparm_context *lp_ctx;
79         const char *target_hostname;
80 };
81
82 struct gensec_security_ops {
83         const char *name;
84         const char *sasl_name;
85         uint8_t auth_type;  /* 0 if not offered on DCE-RPC */
86         const char **oid;  /* NULL if not offered by SPNEGO */
87         NTSTATUS (*client_start)(struct gensec_security *gensec_security);
88         NTSTATUS (*server_start)(struct gensec_security *gensec_security);
89         /**
90            Determine if a packet has the right 'magic' for this mechanism
91         */
92         NTSTATUS (*magic)(struct gensec_security *gensec_security, 
93                           const DATA_BLOB *first_packet);
94         NTSTATUS (*update)(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
95                            const DATA_BLOB in, DATA_BLOB *out);
96         NTSTATUS (*seal_packet)(struct gensec_security *gensec_security, TALLOC_CTX *sig_mem_ctx,
97                                 uint8_t *data, size_t length, 
98                                 const uint8_t *whole_pdu, size_t pdu_length, 
99                                 DATA_BLOB *sig);
100         NTSTATUS (*sign_packet)(struct gensec_security *gensec_security, TALLOC_CTX *sig_mem_ctx,
101                                 const uint8_t *data, size_t length, 
102                                 const uint8_t *whole_pdu, size_t pdu_length, 
103                                 DATA_BLOB *sig);
104         size_t   (*sig_size)(struct gensec_security *gensec_security, size_t data_size);
105         size_t   (*max_input_size)(struct gensec_security *gensec_security);
106         size_t   (*max_wrapped_size)(struct gensec_security *gensec_security);
107         NTSTATUS (*check_packet)(struct gensec_security *gensec_security, TALLOC_CTX *sig_mem_ctx, 
108                                  const uint8_t *data, size_t length, 
109                                  const uint8_t *whole_pdu, size_t pdu_length, 
110                                  const DATA_BLOB *sig);
111         NTSTATUS (*unseal_packet)(struct gensec_security *gensec_security, TALLOC_CTX *sig_mem_ctx,
112                                   uint8_t *data, size_t length, 
113                                   const uint8_t *whole_pdu, size_t pdu_length, 
114                                   const DATA_BLOB *sig);
115         NTSTATUS (*wrap)(struct gensec_security *gensec_security, 
116                                   TALLOC_CTX *mem_ctx, 
117                                   const DATA_BLOB *in, 
118                                   DATA_BLOB *out); 
119         NTSTATUS (*unwrap)(struct gensec_security *gensec_security, 
120                            TALLOC_CTX *mem_ctx, 
121                            const DATA_BLOB *in, 
122                            DATA_BLOB *out); 
123         NTSTATUS (*wrap_packets)(struct gensec_security *gensec_security, 
124                                  TALLOC_CTX *mem_ctx, 
125                                  const DATA_BLOB *in, 
126                                  DATA_BLOB *out,
127                                  size_t *len_processed); 
128         NTSTATUS (*unwrap_packets)(struct gensec_security *gensec_security, 
129                                    TALLOC_CTX *mem_ctx, 
130                                    const DATA_BLOB *in, 
131                                    DATA_BLOB *out,
132                                    size_t *len_processed); 
133         NTSTATUS (*packet_full_request)(struct gensec_security *gensec_security,
134                                         DATA_BLOB blob, size_t *size);
135         NTSTATUS (*session_key)(struct gensec_security *gensec_security, DATA_BLOB *session_key);
136         NTSTATUS (*session_info)(struct gensec_security *gensec_security, 
137                                  struct auth_session_info **session_info); 
138         void (*want_feature)(struct gensec_security *gensec_security,
139                                     uint32_t feature);
140         bool (*have_feature)(struct gensec_security *gensec_security,
141                                     uint32_t feature); 
142         bool enabled;
143         bool kerberos;
144         enum gensec_priority priority;
145 };
146         
147 struct gensec_security_ops_wrapper {
148         const struct gensec_security_ops *op;
149         const char *oid;
150 };
151
152 #define GENSEC_INTERFACE_VERSION 0
153
154 struct gensec_security {
155         const struct gensec_security_ops *ops;
156         void *private_data;
157         struct cli_credentials *credentials;
158         struct gensec_target target;
159         enum gensec_role gensec_role;
160         bool subcontext;
161         uint32_t want_features;
162         struct tevent_context *event_ctx;
163         struct tsocket_address *local_addr, *remote_addr;
164         struct gensec_settings *settings;
165         
166         /* When we are a server, this may be filled in to provide an
167          * NTLM authentication backend, and user lookup (such as if no
168          * PAC is found) */
169         struct auth4_context *auth_context;
170 };
171
172 /* this structure is used by backends to determine the size of some critical types */
173 struct gensec_critical_sizes {
174         int interface_version;
175         int sizeof_gensec_security_ops;
176         int sizeof_gensec_security;
177 };
178
179 /* Socket wrapper */
180
181 struct gensec_security;
182 struct socket_context;
183 struct auth4_context;
184 struct auth_user_info_dc;
185
186 NTSTATUS gensec_socket_init(struct gensec_security *gensec_security,
187                             TALLOC_CTX *mem_ctx, 
188                             struct socket_context *current_socket,
189                             struct tevent_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 tevent_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 struct tevent_req *gensec_update_send(TALLOC_CTX *mem_ctx,
227                                       struct tevent_context *ev,
228                                       struct gensec_security *gensec_security,
229                                       const DATA_BLOB in);
230 NTSTATUS gensec_update_recv(struct tevent_req *req, TALLOC_CTX *out_mem_ctx, DATA_BLOB *out);
231 void gensec_want_feature(struct gensec_security *gensec_security,
232                          uint32_t feature);
233 bool gensec_have_feature(struct gensec_security *gensec_security,
234                          uint32_t feature);
235 NTSTATUS gensec_set_credentials(struct gensec_security *gensec_security, struct cli_credentials *credentials);
236 NTSTATUS gensec_set_target_service(struct gensec_security *gensec_security, const char *service);
237 const char *gensec_get_target_service(struct gensec_security *gensec_security);
238 NTSTATUS gensec_set_target_hostname(struct gensec_security *gensec_security, const char *hostname);
239 const char *gensec_get_target_hostname(struct gensec_security *gensec_security);
240 NTSTATUS gensec_session_key(struct gensec_security *gensec_security, 
241                             DATA_BLOB *session_key);
242 NTSTATUS gensec_start_mech_by_oid(struct gensec_security *gensec_security, 
243                                   const char *mech_oid);
244 const char *gensec_get_name_by_oid(struct gensec_security *gensec_security, const char *oid_string);
245 struct cli_credentials *gensec_get_credentials(struct gensec_security *gensec_security);
246 NTSTATUS gensec_init(void);
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 tevent_context *ev,
273                              struct gensec_settings *settings,
274                              struct auth4_context *auth_context,
275                              struct gensec_security **gensec_security);
276 NTSTATUS gensec_session_info(struct gensec_security *gensec_security, 
277                              struct auth_session_info **session_info);
278 struct netlogon_creds_CredentialState;
279 NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
280                                TALLOC_CTX *mem_ctx,
281                                struct netlogon_creds_CredentialState **creds);
282
283
284 NTSTATUS gensec_set_local_address(struct gensec_security *gensec_security,
285                 const struct tsocket_address *local);
286 NTSTATUS gensec_set_remote_address(struct gensec_security *gensec_security,
287                 const struct tsocket_address *remote);
288 const struct tsocket_address *gensec_get_local_address(struct gensec_security *gensec_security);
289 const struct tsocket_address *gensec_get_remote_address(struct gensec_security *gensec_security);
290
291 NTSTATUS gensec_start_mech_by_name(struct gensec_security *gensec_security, 
292                                         const char *name);
293
294 NTSTATUS gensec_unwrap(struct gensec_security *gensec_security, 
295                        TALLOC_CTX *mem_ctx, 
296                        const DATA_BLOB *in, 
297                        DATA_BLOB *out);
298 NTSTATUS gensec_wrap(struct gensec_security *gensec_security, 
299                      TALLOC_CTX *mem_ctx, 
300                      const DATA_BLOB *in, 
301                      DATA_BLOB *out);
302
303 struct gensec_security_ops **gensec_security_all(void);
304 bool gensec_security_ops_enabled(struct gensec_security_ops *ops, struct gensec_security *security);
305 struct gensec_security_ops **gensec_use_kerberos_mechs(TALLOC_CTX *mem_ctx, 
306                                                        struct gensec_security_ops **old_gensec_list, 
307                                                        struct cli_credentials *creds);
308
309 NTSTATUS gensec_start_mech_by_sasl_name(struct gensec_security *gensec_security, 
310                                         const char *sasl_name);
311
312 int gensec_setting_int(struct gensec_settings *settings, const char *mechanism, const char *name, int default_value);
313 bool gensec_setting_bool(struct gensec_settings *settings, const char *mechanism, const char *name, bool default_value);
314
315 NTSTATUS gensec_set_target_principal(struct gensec_security *gensec_security, const char *principal);
316
317 #endif /* __GENSEC_H__ */