Rework Samba4 to use the new common libcli/auth code
[ira/wip.git] / source4 / auth / gensec / schannel.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc schannel operations
5
6    Copyright (C) Andrew Tridgell 2004
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 #include "includes.h"
24 #include "librpc/gen_ndr/ndr_schannel.h"
25 #include "auth/auth.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/gensec/gensec.h"
28 #include "auth/gensec/gensec_proto.h"
29 #include "auth/gensec/schannel.h"
30 #include "librpc/rpc/dcerpc.h"
31 #include "param/param.h"
32 #include "auth/session_proto.h"
33
34 static size_t schannel_sig_size(struct gensec_security *gensec_security, size_t data_size)
35 {
36         return 32;
37 }
38
39 static NTSTATUS schannel_session_key(struct gensec_security *gensec_security, 
40                                             DATA_BLOB *session_key)
41 {
42         return NT_STATUS_NOT_IMPLEMENTED;
43 }
44
45 static NTSTATUS schannel_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx, 
46                                        const DATA_BLOB in, DATA_BLOB *out) 
47 {
48         struct schannel_state *state = (struct schannel_state *)gensec_security->private_data;
49         NTSTATUS status;
50         enum ndr_err_code ndr_err;
51         struct schannel_bind bind_schannel;
52         struct schannel_bind_ack bind_schannel_ack;
53         struct netlogon_creds_CredentialState *creds;
54         struct ldb_context *schannel_ldb;
55         const char *workstation;
56         const char *domain;
57         *out = data_blob(NULL, 0);
58
59         switch (gensec_security->gensec_role) {
60         case GENSEC_CLIENT:
61                 if (state->state != SCHANNEL_STATE_START) {
62                         /* we could parse the bind ack, but we don't know what it is yet */
63                         return NT_STATUS_OK;
64                 }
65
66                 state->creds = talloc_reference(state, cli_credentials_get_netlogon_creds(gensec_security->credentials));
67
68                 bind_schannel.unknown1 = 0;
69 #if 0
70                 /* to support this we'd need to have access to the full domain name */
71                 bind_schannel.bind_type = 23;
72                 bind_schannel.u.info23.domain = cli_credentials_get_domain(gensec_security->credentials);
73                 bind_schannel.u.info23.workstation = cli_credentials_get_workstation(gensec_security->credentials);
74                 bind_schannel.u.info23.dnsdomain = cli_credentials_get_realm(gensec_security->credentials);
75                 /* w2k3 refuses us if we use the full DNS workstation?
76                  why? perhaps because we don't fill in the dNSHostName
77                  attribute in the machine account? */
78                 bind_schannel.u.info23.dnsworkstation = cli_credentials_get_workstation(gensec_security->credentials);
79 #else
80                 bind_schannel.bind_type = 3;
81                 bind_schannel.u.info3.domain = cli_credentials_get_domain(gensec_security->credentials);
82                 bind_schannel.u.info3.workstation = cli_credentials_get_workstation(gensec_security->credentials);
83 #endif
84                 
85                 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, 
86                                                gensec_security->settings->iconv_convenience, &bind_schannel,
87                                                (ndr_push_flags_fn_t)ndr_push_schannel_bind);
88                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
89                         status = ndr_map_error2ntstatus(ndr_err);
90                         DEBUG(3, ("Could not create schannel bind: %s\n",
91                                   nt_errstr(status)));
92                         return status;
93                 }
94                 
95                 state->state = SCHANNEL_STATE_UPDATE_1;
96
97                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
98         case GENSEC_SERVER:
99                 
100                 if (state->state != SCHANNEL_STATE_START) {
101                         /* no third leg on this protocol */
102                         return NT_STATUS_INVALID_PARAMETER;
103                 }
104                 
105                 /* parse the schannel startup blob */
106                 ndr_err = ndr_pull_struct_blob(&in, out_mem_ctx,
107                         gensec_security->settings->iconv_convenience,
108                         &bind_schannel, 
109                         (ndr_pull_flags_fn_t)ndr_pull_schannel_bind);
110                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
111                         status = ndr_map_error2ntstatus(ndr_err);
112                         DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
113                                   nt_errstr(status)));
114                         return status;
115                 }
116                 
117                 if (bind_schannel.bind_type == 23) {
118                         workstation = bind_schannel.u.info23.workstation;
119                         domain = bind_schannel.u.info23.domain;
120                 } else {
121                         workstation = bind_schannel.u.info3.workstation;
122                         domain = bind_schannel.u.info3.domain;
123                 }
124                 
125                 if (strcasecmp_m(domain, lp_workgroup(gensec_security->settings->lp_ctx)) != 0) {
126                         DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
127                                   domain, lp_workgroup(gensec_security->settings->lp_ctx)));
128                         
129                         return NT_STATUS_LOGON_FAILURE;
130                 }
131
132                 schannel_ldb = schannel_db_connect(out_mem_ctx, gensec_security->event_ctx, 
133                                                    gensec_security->settings->lp_ctx);
134                 if (!schannel_ldb) {
135                         return NT_STATUS_ACCESS_DENIED;
136                 }
137                 /* pull the session key for this client */
138                 status = schannel_fetch_session_key(schannel_ldb, 
139                                                     out_mem_ctx, workstation, &creds);
140                 talloc_free(schannel_ldb);
141                 if (!NT_STATUS_IS_OK(status)) {
142                         DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
143                                   workstation, nt_errstr(status)));
144                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
145                                 return NT_STATUS_LOGON_FAILURE;
146                         }
147                         return status;
148                 }
149
150                 state->creds = talloc_reference(state, creds);
151
152                 bind_schannel_ack.unknown1 = 1;
153                 bind_schannel_ack.unknown2 = 0;
154                 bind_schannel_ack.unknown3 = 0x6c0000;
155                 
156                 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, 
157                                                gensec_security->settings->iconv_convenience, &bind_schannel_ack,
158                                                (ndr_push_flags_fn_t)ndr_push_schannel_bind_ack);
159                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
160                         status = ndr_map_error2ntstatus(ndr_err);
161                         DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
162                                   workstation, nt_errstr(status)));
163                         return status;
164                 }
165
166                 state->state = SCHANNEL_STATE_UPDATE_1;
167
168                 return NT_STATUS_OK;
169         }
170         return NT_STATUS_INVALID_PARAMETER;
171 }
172
173 /**
174  * Return the struct creds_CredentialState.
175  *
176  * Make sure not to call this unless gensec is using schannel...
177  */
178
179 /* TODO: make this non-public */
180
181 _PUBLIC_ NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
182                                         TALLOC_CTX *mem_ctx,
183                                         struct netlogon_creds_CredentialState **creds)
184
185         struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
186
187         *creds = talloc_reference(mem_ctx, state->creds);
188         if (!*creds) {
189                 return NT_STATUS_NO_MEMORY;
190         }
191         return NT_STATUS_OK;
192 }
193                 
194
195 /** 
196  * Returns anonymous credentials for schannel, matching Win2k3.
197  *
198  */
199
200 static NTSTATUS schannel_session_info(struct gensec_security *gensec_security,
201                                          struct auth_session_info **_session_info) 
202 {
203         struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
204         return auth_anonymous_session_info(state, gensec_security->event_ctx, gensec_security->settings->lp_ctx, _session_info);
205 }
206
207 static NTSTATUS schannel_start(struct gensec_security *gensec_security)
208 {
209         struct schannel_state *state;
210
211         state = talloc(gensec_security, struct schannel_state);
212         if (!state) {
213                 return NT_STATUS_NO_MEMORY;
214         }
215
216         state->state = SCHANNEL_STATE_START;
217         state->seq_num = 0;
218         gensec_security->private_data = state;
219
220         return NT_STATUS_OK;
221 }
222
223 static NTSTATUS schannel_server_start(struct gensec_security *gensec_security) 
224 {
225         NTSTATUS status;
226         struct schannel_state *state;
227
228         status = schannel_start(gensec_security);
229         if (!NT_STATUS_IS_OK(status)) {
230                 return status;
231         }
232
233         state = (struct schannel_state *)gensec_security->private_data;
234         state->initiator = false;
235                 
236         return NT_STATUS_OK;
237 }
238
239 static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
240 {
241         NTSTATUS status;
242         struct schannel_state *state;
243
244         status = schannel_start(gensec_security);
245         if (!NT_STATUS_IS_OK(status)) {
246                 return status;
247         }
248
249         state = (struct schannel_state *)gensec_security->private_data;
250         state->initiator = true;
251                 
252         return NT_STATUS_OK;
253 }
254
255
256 static bool schannel_have_feature(struct gensec_security *gensec_security,
257                                          uint32_t feature)
258 {
259         if (feature & (GENSEC_FEATURE_SIGN | 
260                        GENSEC_FEATURE_SEAL)) {
261                 return true;
262         }
263         if (feature & GENSEC_FEATURE_DCE_STYLE) {
264                 return true;
265         }
266         if (feature & GENSEC_FEATURE_ASYNC_REPLIES) {
267                 return true;
268         }
269         return false;
270 }
271
272
273 static const struct gensec_security_ops gensec_schannel_security_ops = {
274         .name           = "schannel",
275         .auth_type      = DCERPC_AUTH_TYPE_SCHANNEL,
276         .client_start   = schannel_client_start,
277         .server_start   = schannel_server_start,
278         .update         = schannel_update,
279         .seal_packet    = schannel_seal_packet,
280         .sign_packet    = schannel_sign_packet,
281         .check_packet   = schannel_check_packet,
282         .unseal_packet  = schannel_unseal_packet,
283         .session_key    = schannel_session_key,
284         .session_info   = schannel_session_info,
285         .sig_size       = schannel_sig_size,
286         .have_feature   = schannel_have_feature,
287         .enabled        = true,
288         .priority       = GENSEC_SCHANNEL
289 };
290
291 _PUBLIC_ NTSTATUS gensec_schannel_init(void)
292 {
293         NTSTATUS ret;
294         ret = gensec_register(&gensec_schannel_security_ops);
295         if (!NT_STATUS_IS_OK(ret)) {
296                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
297                         gensec_schannel_security_ops.name));
298                 return ret;
299         }
300
301         return ret;
302 }