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