502173ab59d4f3a948da91d4a1fc5004a4cc510b
[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 "rpc_server/dcerpc_server_proto.h"
26 #include "rpc_server/common/proto.h"
27 #include "librpc/rpc/dcerpc_proto.h"
28 #include "librpc/gen_ndr/ndr_dcerpc.h"
29 #include "auth/credentials/credentials.h"
30 #include "auth/gensec/gensec.h"
31 #include "auth/auth.h"
32 #include "param/param.h"
33 #include "librpc/rpc/rpc_common.h"
34
35 /*
36   parse any auth information from a dcerpc bind request
37   return false if we can't handle the auth request for some 
38   reason (in which case we send a bind_nak)
39 */
40 bool dcesrv_auth_bind(struct dcesrv_call_state *call)
41 {
42         struct cli_credentials *server_credentials = NULL;
43         struct ncacn_packet *pkt = &call->pkt;
44         struct dcesrv_connection *dce_conn = call->conn;
45         struct dcesrv_auth *auth = &dce_conn->auth_state;
46         bool want_header_signing = false;
47         NTSTATUS status;
48
49         if (pkt->auth_length == 0) {
50                 enum dcerpc_transport_t transport =
51                         dcerpc_binding_get_transport(call->conn->endpoint->ep_description);
52                 const char *auth_type = derpc_transport_string_by_transport(transport);
53                 const char *transport_protection = AUTHZ_TRANSPORT_PROTECTION_NONE;
54                 if (transport == NCACN_NP) {
55                         transport_protection = AUTHZ_TRANSPORT_PROTECTION_SMB;
56                 }
57                 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
58                 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
59                 auth->auth_context_id = 0;
60
61                 /*
62                  * Log the authorization to this RPC interface.  This
63                  * covered ncacn_np pass-through auth, and anonymous
64                  * DCE/RPC (eg epmapper, netlogon etc)
65                  */
66                 log_successful_authz_event(call->conn->msg_ctx,
67                                            call->conn->dce_ctx->lp_ctx,
68                                            call->conn->remote_address,
69                                            call->conn->local_address,
70                                            "DCE/RPC",
71                                            auth_type,
72                                            transport_protection,
73                                            call->conn->auth_state.session_info);
74
75                 return true;
76         }
77
78         status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
79                                           &call->in_auth_info,
80                                           NULL, true);
81         if (!NT_STATUS_IS_OK(status)) {
82                 /*
83                  * Setting DCERPC_AUTH_LEVEL_NONE,
84                  * gives the caller the reject_reason
85                  * as auth_context_id.
86                  *
87                  * Note: DCERPC_AUTH_LEVEL_NONE == 1
88                  */
89                 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
90                 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
91                 auth->auth_context_id =
92                         DCERPC_BIND_NAK_REASON_PROTOCOL_VERSION_NOT_SUPPORTED;
93                 return false;
94         }
95
96         switch (call->in_auth_info.auth_level) {
97         case DCERPC_AUTH_LEVEL_CONNECT:
98         case DCERPC_AUTH_LEVEL_CALL:
99         case DCERPC_AUTH_LEVEL_PACKET:
100         case DCERPC_AUTH_LEVEL_INTEGRITY:
101         case DCERPC_AUTH_LEVEL_PRIVACY:
102                 /*
103                  * We evaluate auth_type only if auth_level was valid
104                  */
105                 break;
106         default:
107                 /*
108                  * Setting DCERPC_AUTH_LEVEL_NONE,
109                  * gives the caller the reject_reason
110                  * as auth_context_id.
111                  *
112                  * Note: DCERPC_AUTH_LEVEL_NONE == 1
113                  */
114                 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
115                 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
116                 auth->auth_context_id = DCERPC_BIND_NAK_REASON_NOT_SPECIFIED;
117                 return false;
118         }
119
120         auth->auth_type = call->in_auth_info.auth_type;
121         auth->auth_level = call->in_auth_info.auth_level;
122         auth->auth_context_id = call->in_auth_info.auth_context_id;
123
124         server_credentials 
125                 = cli_credentials_init(call);
126         if (!server_credentials) {
127                 DEBUG(1, ("Failed to init server credentials\n"));
128                 return false;
129         }
130         
131         cli_credentials_set_conf(server_credentials, call->conn->dce_ctx->lp_ctx);
132         status = cli_credentials_set_machine_account(server_credentials, call->conn->dce_ctx->lp_ctx);
133         if (!NT_STATUS_IS_OK(status)) {
134                 DEBUG(1, ("Failed to obtain server credentials: %s\n",
135                           nt_errstr(status)));
136                 return false;
137         }
138
139         status = samba_server_gensec_start(dce_conn, call->event_ctx, 
140                                            call->msg_ctx,
141                                            call->conn->dce_ctx->lp_ctx,
142                                            server_credentials,
143                                            NULL,
144                                            &auth->gensec_security);
145         if (!NT_STATUS_IS_OK(status)) {
146                 DEBUG(1, ("Failed to call samba_server_gensec_start %s\n",
147                           nt_errstr(status)));
148                 return false;
149         }
150
151         /*
152          * We have to call this because we set the target_service for
153          * Kerberos to NULL above, and in any case we wish to log a
154          * more specific service target.
155          *
156          */
157         status = gensec_set_target_service_description(auth->gensec_security,
158                                                        "DCE/RPC");
159         if (!NT_STATUS_IS_OK(status)) {
160                 DEBUG(1, ("Failed to call gensec_set_target_service_description %s\n",
161                           nt_errstr(status)));
162                 return false;
163         }
164
165         if (call->conn->remote_address != NULL) {
166                 status = gensec_set_remote_address(auth->gensec_security,
167                                                 call->conn->remote_address);
168                 if (!NT_STATUS_IS_OK(status)) {
169                         DEBUG(1, ("Failed to call gensec_set_remote_address() %s\n",
170                                   nt_errstr(status)));
171                         return false;
172                 }
173         }
174
175         if (call->conn->local_address != NULL) {
176                 status = gensec_set_local_address(auth->gensec_security,
177                                                   call->conn->local_address);
178                 if (!NT_STATUS_IS_OK(status)) {
179                         DEBUG(1, ("Failed to call gensec_set_local_address() %s\n",
180                                   nt_errstr(status)));
181                         return false;
182                 }
183         }
184
185         status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_type,
186                                                auth->auth_level);
187         if (!NT_STATUS_IS_OK(status)) {
188                 const char *backend_name =
189                         gensec_get_name_by_authtype(auth->gensec_security,
190                                                     auth->auth_type);
191
192                 DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: "
193                           "auth_type=%d (%s), auth_level=%d: %s\n",
194                           (int)auth->auth_type, backend_name,
195                           (int)auth->auth_level,
196                           nt_errstr(status)));
197
198                 /*
199                  * Setting DCERPC_AUTH_LEVEL_NONE,
200                  * gives the caller the reject_reason
201                  * as auth_context_id.
202                  *
203                  * Note: DCERPC_AUTH_LEVEL_NONE == 1
204                  */
205                 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
206                 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
207                 if (backend_name != NULL) {
208                         auth->auth_context_id =
209                                 DCERPC_BIND_NAK_REASON_INVALID_CHECKSUM;
210                 } else {
211                         auth->auth_context_id =
212                                 DCERPC_BIND_NAK_REASON_INVALID_AUTH_TYPE;
213                 }
214                 return false;
215         }
216
217         if (call->pkt.pfc_flags & DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN) {
218                 auth->client_hdr_signing = true;
219                 want_header_signing = true;
220         }
221
222         if (want_header_signing) {
223                 want_header_signing = gensec_have_feature(auth->gensec_security,
224                                                 GENSEC_FEATURE_SIGN_PKT_HEADER);
225         }
226
227         if (want_header_signing) {
228                 want_header_signing = lpcfg_parm_bool(dce_conn->dce_ctx->lp_ctx,
229                                                       NULL,
230                                                       "dcesrv",
231                                                       "header signing",
232                                                       true);
233         }
234
235         if (want_header_signing) {
236                 gensec_want_feature(auth->gensec_security,
237                                     GENSEC_FEATURE_SIGN_PKT_HEADER);
238                 auth->hdr_signing = true;
239         }
240
241         return true;
242 }
243
244 NTSTATUS dcesrv_auth_complete(struct dcesrv_call_state *call, NTSTATUS status)
245 {
246         struct dcesrv_connection *dce_conn = call->conn;
247         const char *pdu = "<unknown>";
248
249         switch (call->pkt.ptype) {
250         case DCERPC_PKT_BIND:
251                 pdu = "BIND";
252                 break;
253         case DCERPC_PKT_ALTER:
254                 pdu = "ALTER";
255                 break;
256         case DCERPC_PKT_AUTH3:
257                 pdu = "AUTH3";
258                 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
259                         DEBUG(4, ("GENSEC not finished at at %s\n", pdu));
260                         return NT_STATUS_RPC_SEC_PKG_ERROR;
261                 }
262                 break;
263         default:
264                 return NT_STATUS_INTERNAL_ERROR;
265         }
266
267         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
268                 return NT_STATUS_OK;
269         }
270
271         if (!NT_STATUS_IS_OK(status)) {
272                 DEBUG(4, ("GENSEC mech rejected the incoming authentication "
273                           "at %s: %s\n", pdu, nt_errstr(status)));
274                 return status;
275         }
276
277         status = gensec_session_info(dce_conn->auth_state.gensec_security,
278                                      dce_conn,
279                                      &dce_conn->auth_state.session_info);
280         if (!NT_STATUS_IS_OK(status)) {
281                 DEBUG(1, ("Failed to establish session_info: %s\n",
282                           nt_errstr(status)));
283                 return status;
284         }
285         dce_conn->auth_state.auth_finished = true;
286         dce_conn->allow_request = true;
287
288         /* Now that we are authenticated, go back to the generic session key... */
289         dce_conn->auth_state.session_key = dcesrv_generic_session_key;
290
291         if (call->pkt.ptype != DCERPC_PKT_AUTH3) {
292                 return NT_STATUS_OK;
293         }
294
295         if (call->out_auth_info->credentials.length != 0) {
296                 DEBUG(4, ("GENSEC produced output token (len=%zu) at %s\n",
297                           call->out_auth_info->credentials.length, pdu));
298                 return NT_STATUS_RPC_SEC_PKG_ERROR;
299         }
300
301         return NT_STATUS_OK;
302 }
303
304 /*
305   add any auth information needed in a bind ack, and process the authentication
306   information found in the bind.
307 */
308 NTSTATUS dcesrv_auth_prepare_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
309 {
310         struct dcesrv_connection *dce_conn = call->conn;
311
312         dce_conn->allow_alter = true;
313         dce_conn->allow_auth3 = true;
314
315         if (call->pkt.auth_length == 0) {
316                 dce_conn->auth_state.auth_finished = true;
317                 dce_conn->allow_request = true;
318                 return NT_STATUS_OK;
319         }
320
321         /* We can't work without an existing gensec state */
322         if (!call->conn->auth_state.gensec_security) {
323                 return NT_STATUS_INTERNAL_ERROR;
324         }
325
326         if (dce_conn->auth_state.hdr_signing) {
327                 pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
328         }
329
330         call->_out_auth_info = (struct dcerpc_auth) {
331                 .auth_type = dce_conn->auth_state.auth_type,
332                 .auth_level = dce_conn->auth_state.auth_level,
333                 .auth_context_id = dce_conn->auth_state.auth_context_id,
334         };
335         call->out_auth_info = &call->_out_auth_info;
336
337         return NT_STATUS_OK;
338 }
339
340 NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
341 {
342         struct dcesrv_connection *dce_conn = call->conn;
343         NTSTATUS status;
344
345         status = dcesrv_auth_prepare_bind_ack(call, pkt);
346         if (!NT_STATUS_IS_OK(status)) {
347                 return status;
348         }
349
350         if (dce_conn->auth_state.auth_finished) {
351                 return NT_STATUS_OK;
352         }
353
354         status = gensec_update_ev(dce_conn->auth_state.gensec_security,
355                                call, call->event_ctx,
356                                call->in_auth_info.credentials,
357                                &call->out_auth_info->credentials);
358
359         return dcesrv_auth_complete(call, status);
360 }
361
362
363 /*
364   process the final stage of a auth request
365 */
366 bool dcesrv_auth_prepare_auth3(struct dcesrv_call_state *call)
367 {
368         struct ncacn_packet *pkt = &call->pkt;
369         struct dcesrv_connection *dce_conn = call->conn;
370         NTSTATUS status;
371
372         if (pkt->auth_length == 0) {
373                 return false;
374         }
375
376         if (dce_conn->auth_state.auth_finished) {
377                 return false;
378         }
379
380         /* We can't work without an existing gensec state */
381         if (!dce_conn->auth_state.gensec_security) {
382                 return false;
383         }
384
385         status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
386                                           &call->in_auth_info, NULL, true);
387         if (!NT_STATUS_IS_OK(status)) {
388                 /*
389                  * Windows returns DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
390                  * instead of DCERPC_NCA_S_PROTO_ERROR.
391                  */
392                 call->fault_code = DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY;
393                 return false;
394         }
395
396         if (call->in_auth_info.auth_type != dce_conn->auth_state.auth_type) {
397                 return false;
398         }
399
400         if (call->in_auth_info.auth_level != dce_conn->auth_state.auth_level) {
401                 return false;
402         }
403
404         if (call->in_auth_info.auth_context_id != dce_conn->auth_state.auth_context_id) {
405                 return false;
406         }
407
408         call->_out_auth_info = (struct dcerpc_auth) {
409                 .auth_type = dce_conn->auth_state.auth_type,
410                 .auth_level = dce_conn->auth_state.auth_level,
411                 .auth_context_id = dce_conn->auth_state.auth_context_id,
412         };
413         call->out_auth_info = &call->_out_auth_info;
414
415         return true;
416 }
417
418 bool dcesrv_auth_auth3(struct dcesrv_call_state *call)
419 {
420         struct dcesrv_connection *dce_conn = call->conn;
421         NTSTATUS status;
422         bool ok;
423
424         ok = dcesrv_auth_prepare_auth3(call);
425         if (!ok) {
426                 return false;
427         }
428
429         /* Pass the extra data we got from the client down to gensec for processing */
430         status = gensec_update_ev(dce_conn->auth_state.gensec_security,
431                                call, call->event_ctx,
432                                call->in_auth_info.credentials,
433                                &call->out_auth_info->credentials);
434
435         status = dcesrv_auth_complete(call, status);
436         if (!NT_STATUS_IS_OK(status)) {
437                 return false;
438         }
439
440         return true;
441 }
442
443 /*
444   parse any auth information from a dcerpc alter request
445   return false if we can't handle the auth request for some 
446   reason (in which case we send a bind_nak (is this true for here?))
447 */
448 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
449 {
450         struct ncacn_packet *pkt = &call->pkt;
451         struct dcesrv_connection *dce_conn = call->conn;
452         NTSTATUS status;
453
454         /* on a pure interface change there is no auth blob */
455         if (pkt->auth_length == 0) {
456                 if (!dce_conn->auth_state.auth_finished) {
457                         return false;
458                 }
459                 return true;
460         }
461
462         if (dce_conn->auth_state.auth_finished) {
463                 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
464                 return false;
465         }
466
467         /* We can't work without an existing gensec state */
468         if (!dce_conn->auth_state.gensec_security) {
469                 return false;
470         }
471
472         status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
473                                           &call->in_auth_info, NULL, true);
474         if (!NT_STATUS_IS_OK(status)) {
475                 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
476                 return false;
477         }
478
479         if (call->in_auth_info.auth_type == DCERPC_AUTH_TYPE_NONE) {
480                 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
481                 return false;
482         }
483
484         if (call->in_auth_info.auth_type != dce_conn->auth_state.auth_type) {
485                 return false;
486         }
487
488         if (call->in_auth_info.auth_level != dce_conn->auth_state.auth_level) {
489                 return false;
490         }
491
492         if (call->in_auth_info.auth_context_id != dce_conn->auth_state.auth_context_id) {
493                 return false;
494         }
495
496         return true;
497 }
498
499 /*
500   add any auth information needed in a alter ack, and process the authentication
501   information found in the alter.
502 */
503 NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
504 {
505         struct dcesrv_connection *dce_conn = call->conn;
506         NTSTATUS status;
507
508         /* on a pure interface change there is no auth_info structure
509            setup */
510         if (call->pkt.auth_length == 0) {
511                 return NT_STATUS_OK;
512         }
513
514         if (!call->conn->auth_state.gensec_security) {
515                 return NT_STATUS_INTERNAL_ERROR;
516         }
517
518         call->_out_auth_info = (struct dcerpc_auth) {
519                 .auth_type = dce_conn->auth_state.auth_type,
520                 .auth_level = dce_conn->auth_state.auth_level,
521                 .auth_context_id = dce_conn->auth_state.auth_context_id,
522         };
523         call->out_auth_info = &call->_out_auth_info;
524
525         status = gensec_update_ev(dce_conn->auth_state.gensec_security,
526                                call, call->event_ctx,
527                                call->in_auth_info.credentials,
528                                &call->out_auth_info->credentials);
529
530         return dcesrv_auth_complete(call, status);
531 }
532
533 /*
534   check credentials on a packet
535 */
536 bool dcesrv_auth_pkt_pull(struct dcesrv_call_state *call,
537                           DATA_BLOB *full_packet,
538                           uint8_t required_flags,
539                           uint8_t optional_flags,
540                           uint8_t payload_offset,
541                           DATA_BLOB *payload_and_verifier)
542 {
543         struct ncacn_packet *pkt = &call->pkt;
544         struct dcesrv_connection *dce_conn = call->conn;
545         const struct dcerpc_auth tmp_auth = {
546                 .auth_type = dce_conn->auth_state.auth_type,
547                 .auth_level = dce_conn->auth_state.auth_level,
548                 .auth_context_id = dce_conn->auth_state.auth_context_id,
549         };
550         NTSTATUS status;
551
552         if (!dce_conn->allow_request) {
553                 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
554                 return false;
555         }
556
557         if (dce_conn->auth_state.auth_invalid) {
558                 return false;
559         }
560
561         status = dcerpc_ncacn_pull_pkt_auth(&tmp_auth,
562                                             dce_conn->auth_state.gensec_security,
563                                             call,
564                                             pkt->ptype,
565                                             required_flags,
566                                             optional_flags,
567                                             payload_offset,
568                                             payload_and_verifier,
569                                             full_packet,
570                                             pkt);
571         if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTOCOL_ERROR)) {
572                 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
573                 return false;
574         }
575         if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_UNSUPPORTED_AUTHN_LEVEL)) {
576                 call->fault_code = DCERPC_NCA_S_UNSUPPORTED_AUTHN_LEVEL;
577                 return false;
578         }
579         if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR)) {
580                 call->fault_code = DCERPC_FAULT_SEC_PKG_ERROR;
581                 return false;
582         }
583         if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
584                 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
585                 return false;
586         }
587         if (!NT_STATUS_IS_OK(status)) {
588                 return false;
589         }
590
591         return true;
592 }
593
594 /* 
595    push a signed or sealed dcerpc request packet into a blob
596 */
597 bool dcesrv_auth_pkt_push(struct dcesrv_call_state *call,
598                           DATA_BLOB *blob, size_t sig_size,
599                           uint8_t payload_offset,
600                           const DATA_BLOB *payload,
601                           const struct ncacn_packet *pkt)
602 {
603         struct dcesrv_connection *dce_conn = call->conn;
604         const struct dcerpc_auth tmp_auth = {
605                 .auth_type = dce_conn->auth_state.auth_type,
606                 .auth_level = dce_conn->auth_state.auth_level,
607                 .auth_context_id = dce_conn->auth_state.auth_context_id,
608         };
609         NTSTATUS status;
610
611         status = dcerpc_ncacn_push_pkt_auth(&tmp_auth,
612                                             dce_conn->auth_state.gensec_security,
613                                             call, blob, sig_size,
614                                             payload_offset,
615                                             payload,
616                                             pkt);
617         return NT_STATUS_IS_OK(status);
618 }