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