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