r25553: Convert to standard bool type.
[samba.git] / source4 / rpc_server / dcesrv_auth.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    server side dcerpc authentication code
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Stefan (metze) Metzmacher 2004
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 "rpc_server/dcerpc_server.h"
25 #include "librpc/gen_ndr/ndr_dcerpc.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/gensec/gensec.h"
28 #include "param/param.h"
29
30 /*
31   parse any auth information from a dcerpc bind request
32   return false if we can't handle the auth request for some 
33   reason (in which case we send a bind_nak)
34 */
35 bool dcesrv_auth_bind(struct dcesrv_call_state *call)
36 {
37         struct cli_credentials *server_credentials;
38         struct ncacn_packet *pkt = &call->pkt;
39         struct dcesrv_connection *dce_conn = call->conn;
40         struct dcesrv_auth *auth = &dce_conn->auth_state;
41         NTSTATUS status;
42
43         if (pkt->u.bind.auth_info.length == 0) {
44                 dce_conn->auth_state.auth_info = NULL;
45                 return true;
46         }
47
48         dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
49         if (!dce_conn->auth_state.auth_info) {
50                 return false;
51         }
52
53         status = ndr_pull_struct_blob(&pkt->u.bind.auth_info,
54                                       call,
55                                       dce_conn->auth_state.auth_info,
56                                       (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
57         if (!NT_STATUS_IS_OK(status)) {
58                 return false;
59         }
60
61         status = gensec_server_start(dce_conn, call->event_ctx, call->msg_ctx, &auth->gensec_security);
62         if (!NT_STATUS_IS_OK(status)) {
63                 DEBUG(1, ("Failed to start GENSEC for DCERPC server: %s\n", nt_errstr(status)));
64                 return false;
65         }
66
67         server_credentials 
68                 = cli_credentials_init(call);
69         if (!server_credentials) {
70                 DEBUG(1, ("Failed to init server credentials\n"));
71                 return false;
72         }
73         
74         cli_credentials_set_conf(server_credentials, global_loadparm);
75         status = cli_credentials_set_machine_account(server_credentials);
76         if (!NT_STATUS_IS_OK(status)) {
77                 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
78                 talloc_free(server_credentials);
79                 server_credentials = NULL;
80         }
81
82         gensec_set_credentials(auth->gensec_security, server_credentials);
83
84         status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_info->auth_type, 
85                                                auth->auth_info->auth_level);
86
87         if (!NT_STATUS_IS_OK(status)) {
88                 DEBUG(1, ("Failed to start GENSEC mechanism for DCERPC server: auth_type=%d, auth_level=%d: %s\n", 
89                           (int)auth->auth_info->auth_type,
90                           (int)auth->auth_info->auth_level,
91                           nt_errstr(status)));
92                 return false;
93         }
94
95         return true;
96 }
97
98 /*
99   add any auth information needed in a bind ack, and process the authentication
100   information found in the bind.
101 */
102 NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
103 {
104         struct dcesrv_connection *dce_conn = call->conn;
105         NTSTATUS status;
106
107         if (!call->conn->auth_state.gensec_security) {
108                 return NT_STATUS_OK;
109         }
110
111         status = gensec_update(dce_conn->auth_state.gensec_security,
112                                call,
113                                dce_conn->auth_state.auth_info->credentials, 
114                                &dce_conn->auth_state.auth_info->credentials);
115         
116         if (NT_STATUS_IS_OK(status)) {
117                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
118                                              &dce_conn->auth_state.session_info);
119                 if (!NT_STATUS_IS_OK(status)) {
120                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
121                         return status;
122                 }
123
124                 /* Now that we are authenticated, go back to the generic session key... */
125                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
126                 return NT_STATUS_OK;
127         } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
128                 dce_conn->auth_state.auth_info->auth_pad_length = 0;
129                 dce_conn->auth_state.auth_info->auth_reserved = 0;
130                 return NT_STATUS_OK;
131         } else {
132                 DEBUG(2, ("Failed to start dcesrv auth negotiate: %s\n", nt_errstr(status)));
133                 return status;
134         }
135 }
136
137
138 /*
139   process the final stage of a auth request
140 */
141 bool dcesrv_auth_auth3(struct dcesrv_call_state *call)
142 {
143         struct ncacn_packet *pkt = &call->pkt;
144         struct dcesrv_connection *dce_conn = call->conn;
145         NTSTATUS status;
146
147         /* We can't work without an existing gensec state, and an new blob to feed it */
148         if (!dce_conn->auth_state.auth_info ||
149             !dce_conn->auth_state.gensec_security ||
150             pkt->u.auth3.auth_info.length == 0) {
151                 return false;
152         }
153
154         status = ndr_pull_struct_blob(&pkt->u.auth3.auth_info,
155                                       call,
156                                       dce_conn->auth_state.auth_info,
157                                       (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
158         if (!NT_STATUS_IS_OK(status)) {
159                 return false;
160         }
161
162         /* Pass the extra data we got from the client down to gensec for processing */
163         status = gensec_update(dce_conn->auth_state.gensec_security,
164                                call,
165                                dce_conn->auth_state.auth_info->credentials, 
166                                &dce_conn->auth_state.auth_info->credentials);
167         if (NT_STATUS_IS_OK(status)) {
168                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
169                                              &dce_conn->auth_state.session_info);
170                 if (!NT_STATUS_IS_OK(status)) {
171                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
172                         return false;
173                 }
174                 /* Now that we are authenticated, go back to the generic session key... */
175                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
176                 return true;
177         } else {
178                 DEBUG(4, ("dcesrv_auth_auth3: failed to authenticate: %s\n", 
179                           nt_errstr(status)));
180                 return false;
181         }
182
183         return true;
184 }
185
186 /*
187   parse any auth information from a dcerpc alter request
188   return false if we can't handle the auth request for some 
189   reason (in which case we send a bind_nak (is this true for here?))
190 */
191 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
192 {
193         struct ncacn_packet *pkt = &call->pkt;
194         struct dcesrv_connection *dce_conn = call->conn;
195         NTSTATUS status;
196
197         /* on a pure interface change there is no auth blob */
198         if (pkt->u.alter.auth_info.length == 0) {
199                 return true;
200         }
201
202         /* We can't work without an existing gensec state */
203         if (!dce_conn->auth_state.gensec_security) {
204                 return false;
205         }
206
207         dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
208         if (!dce_conn->auth_state.auth_info) {
209                 return false;
210         }
211
212         status = ndr_pull_struct_blob(&pkt->u.alter.auth_info,
213                                       call,
214                                       dce_conn->auth_state.auth_info,
215                                       (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
216         if (!NT_STATUS_IS_OK(status)) {
217                 return false;
218         }
219
220         return true;
221 }
222
223 /*
224   add any auth information needed in a alter ack, and process the authentication
225   information found in the alter.
226 */
227 NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
228 {
229         struct dcesrv_connection *dce_conn = call->conn;
230         NTSTATUS status;
231
232         /* on a pure interface change there is no auth_info structure
233            setup */
234         if (!call->conn->auth_state.auth_info ||
235             dce_conn->auth_state.auth_info->credentials.length == 0) {
236                 return NT_STATUS_OK;
237         }
238
239         if (!call->conn->auth_state.gensec_security) {
240                 return NT_STATUS_INVALID_PARAMETER;
241         }
242
243         status = gensec_update(dce_conn->auth_state.gensec_security,
244                                call,
245                                dce_conn->auth_state.auth_info->credentials, 
246                                &dce_conn->auth_state.auth_info->credentials);
247
248         if (NT_STATUS_IS_OK(status)) {
249                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
250                                              &dce_conn->auth_state.session_info);
251                 if (!NT_STATUS_IS_OK(status)) {
252                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
253                         return status;
254                 }
255
256                 /* Now that we are authenticated, got back to the generic session key... */
257                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
258                 return NT_STATUS_OK;
259         } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
260                 dce_conn->auth_state.auth_info->auth_pad_length = 0;
261                 dce_conn->auth_state.auth_info->auth_reserved = 0;
262                 return NT_STATUS_OK;
263         }
264
265         DEBUG(2, ("Failed to finish dcesrv auth alter_ack: %s\n", nt_errstr(status)));
266         return status;
267 }
268
269 /*
270   generate a CONNECT level verifier
271 */
272 static NTSTATUS dcesrv_connect_verifier(TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
273 {
274         *blob = data_blob_talloc(mem_ctx, NULL, 16);
275         if (blob->data == NULL) {
276                 return NT_STATUS_NO_MEMORY;
277         }
278         SIVAL(blob->data, 0, 1);
279         memset(blob->data+4, 0, 12);
280         return NT_STATUS_OK;
281 }
282
283 /*
284   generate a CONNECT level verifier
285 */
286 static NTSTATUS dcesrv_check_connect_verifier(DATA_BLOB *blob)
287 {
288         if (blob->length != 16 ||
289             IVAL(blob->data, 0) != 1) {
290                 return NT_STATUS_ACCESS_DENIED;
291         }
292         return NT_STATUS_OK;
293 }
294
295
296 /*
297   check credentials on a request
298 */
299 bool dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
300 {
301         struct ncacn_packet *pkt = &call->pkt;
302         struct dcesrv_connection *dce_conn = call->conn;
303         DATA_BLOB auth_blob;
304         struct dcerpc_auth auth;
305         struct ndr_pull *ndr;
306         NTSTATUS status;
307
308         if (!dce_conn->auth_state.auth_info ||
309             !dce_conn->auth_state.gensec_security) {
310                 return true;
311         }
312
313         auth_blob.length = 8 + pkt->auth_length;
314
315         /* check for a valid length */
316         if (pkt->u.request.stub_and_verifier.length < auth_blob.length) {
317                 return false;
318         }
319
320         auth_blob.data = 
321                 pkt->u.request.stub_and_verifier.data + 
322                 pkt->u.request.stub_and_verifier.length - auth_blob.length;
323         pkt->u.request.stub_and_verifier.length -= auth_blob.length;
324
325         /* pull the auth structure */
326         ndr = ndr_pull_init_blob(&auth_blob, call);
327         if (!ndr) {
328                 return false;
329         }
330
331         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
332                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
333         }
334
335         status = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, &auth);
336         if (!NT_STATUS_IS_OK(status)) {
337                 talloc_free(ndr);
338                 return false;
339         }
340
341         /* check signature or unseal the packet */
342         switch (dce_conn->auth_state.auth_info->auth_level) {
343         case DCERPC_AUTH_LEVEL_PRIVACY:
344                 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
345                                               call,
346                                               full_packet->data + DCERPC_REQUEST_LENGTH,
347                                               pkt->u.request.stub_and_verifier.length, 
348                                               full_packet->data,
349                                               full_packet->length-auth.credentials.length,
350                                               &auth.credentials);
351                 memcpy(pkt->u.request.stub_and_verifier.data, 
352                        full_packet->data + DCERPC_REQUEST_LENGTH,
353                        pkt->u.request.stub_and_verifier.length);
354                 break;
355
356         case DCERPC_AUTH_LEVEL_INTEGRITY:
357                 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
358                                              call,
359                                              pkt->u.request.stub_and_verifier.data, 
360                                              pkt->u.request.stub_and_verifier.length,
361                                              full_packet->data,
362                                              full_packet->length-auth.credentials.length,
363                                              &auth.credentials);
364                 break;
365
366         case DCERPC_AUTH_LEVEL_CONNECT:
367                 status = dcesrv_check_connect_verifier(&auth.credentials);
368                 break;
369
370         default:
371                 status = NT_STATUS_INVALID_LEVEL;
372                 break;
373         }
374
375         /* remove the indicated amount of padding */
376         if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
377                 talloc_free(ndr);
378                 return false;
379         }
380         pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
381         talloc_free(ndr);
382
383         return NT_STATUS_IS_OK(status);
384 }
385
386
387 /* 
388    push a signed or sealed dcerpc request packet into a blob
389 */
390 bool dcesrv_auth_response(struct dcesrv_call_state *call,
391                           DATA_BLOB *blob, struct ncacn_packet *pkt)
392 {
393         struct dcesrv_connection *dce_conn = call->conn;
394         NTSTATUS status;
395         struct ndr_push *ndr;
396         uint32_t payload_length;
397         DATA_BLOB creds2;
398
399         /* non-signed packets are simple */
400         if (!dce_conn->auth_state.auth_info || !dce_conn->auth_state.gensec_security) {
401                 status = ncacn_push_auth(blob, call, pkt, NULL);
402                 return NT_STATUS_IS_OK(status);
403         }
404
405         ndr = ndr_push_init_ctx(call);
406         if (!ndr) {
407                 return false;
408         }
409
410         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
411                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
412         }
413
414         status = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
415         if (!NT_STATUS_IS_OK(status)) {
416                 return false;
417         }
418
419         /* pad to 16 byte multiple, match win2k3 */
420         dce_conn->auth_state.auth_info->auth_pad_length = NDR_ALIGN(ndr, 16);
421         ndr_push_zero(ndr, dce_conn->auth_state.auth_info->auth_pad_length);
422
423         payload_length = ndr->offset - DCERPC_REQUEST_LENGTH;
424
425         if (dce_conn->auth_state.auth_info->auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
426                 status = dcesrv_connect_verifier(call,
427                                                  &dce_conn->auth_state.auth_info->credentials);
428                 if (!NT_STATUS_IS_OK(status)) {
429                         return false;
430                 }
431         } else {
432
433                 /* We hope this length is accruate.  If must be if the
434                  * GENSEC mech does AEAD signing of the packet
435                  * headers */
436                 dce_conn->auth_state.auth_info->credentials
437                         = data_blob_talloc(call, NULL, 
438                                            gensec_sig_size(dce_conn->auth_state.gensec_security, 
439                                                            payload_length));
440                 data_blob_clear(&dce_conn->auth_state.auth_info->credentials);
441         }
442
443         /* add the auth verifier */
444         status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, 
445                                       dce_conn->auth_state.auth_info);
446         if (!NT_STATUS_IS_OK(status)) {
447                 return false;
448         }
449
450         /* extract the whole packet as a blob */
451         *blob = ndr_push_blob(ndr);
452
453         /* fill in the fragment length and auth_length, we can't fill
454            in these earlier as we don't know the signature length (it
455            could be variable length) */
456         dcerpc_set_frag_length(blob, blob->length);
457
458         /* We hope this value is accruate.  If must be if the GENSEC
459          * mech does AEAD signing of the packet headers */
460         dcerpc_set_auth_length(blob, dce_conn->auth_state.auth_info->credentials.length);
461
462         /* sign or seal the packet */
463         switch (dce_conn->auth_state.auth_info->auth_level) {
464         case DCERPC_AUTH_LEVEL_PRIVACY:
465                 status = gensec_seal_packet(dce_conn->auth_state.gensec_security, 
466                                             call,
467                                             ndr->data + DCERPC_REQUEST_LENGTH, 
468                                             payload_length,
469                                             blob->data,
470                                             blob->length - dce_conn->auth_state.auth_info->credentials.length,
471                                             &creds2);
472
473                 if (NT_STATUS_IS_OK(status)) {
474                         blob->length -= dce_conn->auth_state.auth_info->credentials.length;
475                         if (!data_blob_append(call, blob, creds2.data, creds2.length))
476                                 status = NT_STATUS_NO_MEMORY;
477                         else
478                                 status = NT_STATUS_OK;
479                 }
480
481                 /* If we did AEAD signing of the packet headers, then we hope
482                  * this value didn't change... */
483                 dcerpc_set_auth_length(blob, creds2.length);
484                 dcerpc_set_frag_length(blob, dcerpc_get_frag_length(blob)+creds2.length);
485                 data_blob_free(&creds2);
486                 break;
487
488         case DCERPC_AUTH_LEVEL_INTEGRITY:
489                 status = gensec_sign_packet(dce_conn->auth_state.gensec_security, 
490                                             call,
491                                             ndr->data + DCERPC_REQUEST_LENGTH, 
492                                             payload_length,
493                                             blob->data,
494                                             blob->length - dce_conn->auth_state.auth_info->credentials.length,
495                                             &creds2);
496                 if (NT_STATUS_IS_OK(status)) {
497                         blob->length -= dce_conn->auth_state.auth_info->credentials.length;
498                         if (!data_blob_append(call, blob, creds2.data, creds2.length))
499                                 status = NT_STATUS_NO_MEMORY;
500                         else
501                                 status = NT_STATUS_OK;
502                 }
503
504                 /* If we did AEAD signing of the packet headers, then we hope
505                  * this value didn't change... */
506                 dcerpc_set_auth_length(blob, creds2.length);
507                 dcerpc_set_frag_length(blob, dcerpc_get_frag_length(blob)+creds2.length);
508                 data_blob_free(&creds2);
509                 break;
510
511         case DCERPC_AUTH_LEVEL_CONNECT:
512                 break;
513
514         default:
515                 status = NT_STATUS_INVALID_LEVEL;
516                 break;
517         }
518
519         data_blob_free(&dce_conn->auth_state.auth_info->credentials);
520
521         if (!NT_STATUS_IS_OK(status)) {
522                 return false;
523         }       
524
525         return true;
526 }