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