gensec: clarify memory ownership for gensec_session_info() and gensec_session_key()
[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;
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         uint32_t auth_length;
48
49         if (pkt->u.bind.auth_info.length == 0) {
50                 dce_conn->auth_state.auth_info = NULL;
51                 return true;
52         }
53
54         dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
55         if (!dce_conn->auth_state.auth_info) {
56                 return false;
57         }
58
59         status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
60                                           dce_conn->auth_state.auth_info,
61                                           &auth_length, false);
62         server_credentials 
63                 = cli_credentials_init(call);
64         if (!server_credentials) {
65                 DEBUG(1, ("Failed to init server credentials\n"));
66                 return false;
67         }
68         
69         cli_credentials_set_conf(server_credentials, call->conn->dce_ctx->lp_ctx);
70         status = cli_credentials_set_machine_account(server_credentials, call->conn->dce_ctx->lp_ctx);
71         if (!NT_STATUS_IS_OK(status)) {
72                 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
73                 talloc_free(server_credentials);
74                 server_credentials = NULL;
75         }
76
77         status = samba_server_gensec_start(dce_conn, call->event_ctx, 
78                                            call->msg_ctx,
79                                            call->conn->dce_ctx->lp_ctx,
80                                            server_credentials,
81                                            NULL,
82                                            &auth->gensec_security);
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(3, ("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         if (call->conn->state_flags & DCESRV_CALL_STATE_FLAG_HEADER_SIGNING) {
96                 gensec_want_feature(auth->gensec_security, GENSEC_FEATURE_SIGN_PKT_HEADER);
97         }
98
99         return true;
100 }
101
102 /*
103   add any auth information needed in a bind ack, and process the authentication
104   information found in the bind.
105 */
106 NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
107 {
108         struct dcesrv_connection *dce_conn = call->conn;
109         NTSTATUS status;
110
111         if (!call->conn->auth_state.gensec_security) {
112                 return NT_STATUS_OK;
113         }
114
115         status = gensec_update(dce_conn->auth_state.gensec_security,
116                                call,
117                                dce_conn->auth_state.auth_info->credentials, 
118                                &dce_conn->auth_state.auth_info->credentials);
119         
120         if (NT_STATUS_IS_OK(status)) {
121                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
122                                              dce_conn,
123                                              &dce_conn->auth_state.session_info);
124                 if (!NT_STATUS_IS_OK(status)) {
125                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
126                         return status;
127                 }
128
129                 if (dce_conn->state_flags & DCESRV_CALL_STATE_FLAG_HEADER_SIGNING) {
130                         gensec_want_feature(dce_conn->auth_state.gensec_security,
131                                             GENSEC_FEATURE_SIGN_PKT_HEADER);
132                 }
133
134                 /* Now that we are authenticated, go back to the generic session key... */
135                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
136                 return NT_STATUS_OK;
137         } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
138                 dce_conn->auth_state.auth_info->auth_pad_length = 0;
139                 dce_conn->auth_state.auth_info->auth_reserved = 0;
140                 return NT_STATUS_OK;
141         } else {
142                 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_ack: %s\n",
143                           nt_errstr(status)));
144                 return status;
145         }
146 }
147
148
149 /*
150   process the final stage of a auth request
151 */
152 bool dcesrv_auth_auth3(struct dcesrv_call_state *call)
153 {
154         struct ncacn_packet *pkt = &call->pkt;
155         struct dcesrv_connection *dce_conn = call->conn;
156         NTSTATUS status;
157         uint32_t auth_length;
158
159         /* We can't work without an existing gensec state, and an new blob to feed it */
160         if (!dce_conn->auth_state.auth_info ||
161             !dce_conn->auth_state.gensec_security ||
162             pkt->u.auth3.auth_info.length == 0) {
163                 return false;
164         }
165
166         status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
167                                           dce_conn->auth_state.auth_info, &auth_length, true);
168         if (!NT_STATUS_IS_OK(status)) {
169                 return false;
170         }
171
172         /* Pass the extra data we got from the client down to gensec for processing */
173         status = gensec_update(dce_conn->auth_state.gensec_security,
174                                call,
175                                dce_conn->auth_state.auth_info->credentials, 
176                                &dce_conn->auth_state.auth_info->credentials);
177         if (NT_STATUS_IS_OK(status)) {
178                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
179                                              dce_conn,
180                                              &dce_conn->auth_state.session_info);
181                 if (!NT_STATUS_IS_OK(status)) {
182                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
183                         return false;
184                 }
185                 /* Now that we are authenticated, go back to the generic session key... */
186                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
187                 return true;
188         } else {
189                 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_auth3: %s\n",
190                           nt_errstr(status)));
191                 return false;
192         }
193 }
194
195 /*
196   parse any auth information from a dcerpc alter request
197   return false if we can't handle the auth request for some 
198   reason (in which case we send a bind_nak (is this true for here?))
199 */
200 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
201 {
202         struct ncacn_packet *pkt = &call->pkt;
203         struct dcesrv_connection *dce_conn = call->conn;
204         NTSTATUS status;
205         uint32_t auth_length;
206
207         /* on a pure interface change there is no auth blob */
208         if (pkt->u.alter.auth_info.length == 0) {
209                 return true;
210         }
211
212         /* We can't work without an existing gensec state */
213         if (!dce_conn->auth_state.gensec_security) {
214                 return false;
215         }
216
217         dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
218         if (!dce_conn->auth_state.auth_info) {
219                 return false;
220         }
221
222         status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
223                                           dce_conn->auth_state.auth_info,
224                                           &auth_length, true);
225         if (!NT_STATUS_IS_OK(status)) {
226                 return false;
227         }
228
229         return true;
230 }
231
232 /*
233   add any auth information needed in a alter ack, and process the authentication
234   information found in the alter.
235 */
236 NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
237 {
238         struct dcesrv_connection *dce_conn = call->conn;
239         NTSTATUS status;
240
241         /* on a pure interface change there is no auth_info structure
242            setup */
243         if (!call->conn->auth_state.auth_info ||
244             dce_conn->auth_state.auth_info->credentials.length == 0) {
245                 return NT_STATUS_OK;
246         }
247
248         if (!call->conn->auth_state.gensec_security) {
249                 return NT_STATUS_INVALID_PARAMETER;
250         }
251
252         status = gensec_update(dce_conn->auth_state.gensec_security,
253                                call,
254                                dce_conn->auth_state.auth_info->credentials, 
255                                &dce_conn->auth_state.auth_info->credentials);
256
257         if (NT_STATUS_IS_OK(status)) {
258                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
259                                              dce_conn,
260                                              &dce_conn->auth_state.session_info);
261                 if (!NT_STATUS_IS_OK(status)) {
262                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
263                         return status;
264                 }
265
266                 /* Now that we are authenticated, got back to the generic session key... */
267                 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
268                 return NT_STATUS_OK;
269         } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
270                 dce_conn->auth_state.auth_info->auth_pad_length = 0;
271                 dce_conn->auth_state.auth_info->auth_reserved = 0;
272                 return NT_STATUS_OK;
273         }
274
275         DEBUG(4, ("GENSEC mech rejected the incoming authentication at auth alter_ack: %s\n",
276                   nt_errstr(status)));
277         return status;
278 }
279
280 /*
281   check credentials on a request
282 */
283 bool dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
284 {
285         struct ncacn_packet *pkt = &call->pkt;
286         struct dcesrv_connection *dce_conn = call->conn;
287         struct dcerpc_auth auth;
288         NTSTATUS status;
289         uint32_t auth_length;
290         size_t hdr_size = DCERPC_REQUEST_LENGTH;
291
292         if (!dce_conn->auth_state.auth_info ||
293             !dce_conn->auth_state.gensec_security) {
294                 return true;
295         }
296
297         if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
298                 hdr_size += 16;
299         }
300
301         switch (dce_conn->auth_state.auth_info->auth_level) {
302         case DCERPC_AUTH_LEVEL_PRIVACY:
303         case DCERPC_AUTH_LEVEL_INTEGRITY:
304                 break;
305
306         case DCERPC_AUTH_LEVEL_CONNECT:
307                 if (pkt->auth_length != 0) {
308                         break;
309                 }
310                 return true;
311         case DCERPC_AUTH_LEVEL_NONE:
312                 if (pkt->auth_length != 0) {
313                         return false;
314                 }
315                 return true;
316
317         default:
318                 return false;
319         }
320
321         status = dcerpc_pull_auth_trailer(pkt, call,
322                                           &pkt->u.request.stub_and_verifier,
323                                           &auth, &auth_length, false);
324         if (!NT_STATUS_IS_OK(status)) {
325                 return false;
326         }
327
328         pkt->u.request.stub_and_verifier.length -= auth_length;
329
330         /* check signature or unseal the packet */
331         switch (dce_conn->auth_state.auth_info->auth_level) {
332         case DCERPC_AUTH_LEVEL_PRIVACY:
333                 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
334                                               full_packet->data + hdr_size,
335                                               pkt->u.request.stub_and_verifier.length, 
336                                               full_packet->data,
337                                               full_packet->length-auth.credentials.length,
338                                               &auth.credentials);
339                 memcpy(pkt->u.request.stub_and_verifier.data, 
340                        full_packet->data + hdr_size,
341                        pkt->u.request.stub_and_verifier.length);
342                 break;
343
344         case DCERPC_AUTH_LEVEL_INTEGRITY:
345                 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
346                                              pkt->u.request.stub_and_verifier.data, 
347                                              pkt->u.request.stub_and_verifier.length,
348                                              full_packet->data,
349                                              full_packet->length-auth.credentials.length,
350                                              &auth.credentials);
351                 break;
352
353         case DCERPC_AUTH_LEVEL_CONNECT:
354                 /* for now we ignore possible signatures here */
355                 status = NT_STATUS_OK;
356                 break;
357
358         default:
359                 status = NT_STATUS_INVALID_LEVEL;
360                 break;
361         }
362
363         /* remove the indicated amount of padding */
364         if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
365                 return false;
366         }
367         pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
368
369         return NT_STATUS_IS_OK(status);
370 }
371
372
373 /* 
374    push a signed or sealed dcerpc request packet into a blob
375 */
376 bool dcesrv_auth_response(struct dcesrv_call_state *call,
377                           DATA_BLOB *blob, size_t sig_size,
378                           struct ncacn_packet *pkt)
379 {
380         struct dcesrv_connection *dce_conn = call->conn;
381         NTSTATUS status;
382         enum ndr_err_code ndr_err;
383         struct ndr_push *ndr;
384         uint32_t payload_length;
385         DATA_BLOB creds2;
386
387         /* non-signed packets are simple */
388         if (sig_size == 0) {
389                 status = ncacn_push_auth(blob, call, pkt, NULL);
390                 return NT_STATUS_IS_OK(status);
391         }
392
393         switch (dce_conn->auth_state.auth_info->auth_level) {
394         case DCERPC_AUTH_LEVEL_PRIVACY:
395         case DCERPC_AUTH_LEVEL_INTEGRITY:
396                 break;
397
398         case DCERPC_AUTH_LEVEL_CONNECT:
399                 /*
400                  * TODO: let the gensec mech decide if it wants to generate a
401                  *       signature that might be needed for schannel...
402                  */
403                 status = ncacn_push_auth(blob, call, pkt, NULL);
404                 return NT_STATUS_IS_OK(status);
405
406         case DCERPC_AUTH_LEVEL_NONE:
407                 status = ncacn_push_auth(blob, call, pkt, NULL);
408                 return NT_STATUS_IS_OK(status);
409
410         default:
411                 return false;
412         }
413
414         ndr = ndr_push_init_ctx(call);
415         if (!ndr) {
416                 return false;
417         }
418
419         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
420                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
421         }
422
423         ndr_err = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
424         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
425                 return false;
426         }
427
428         /* pad to 16 byte multiple in the payload portion of the
429            packet. This matches what w2k3 does. Note that we can't use
430            ndr_push_align() as that is relative to the start of the
431            whole packet, whereas w2k8 wants it relative to the start
432            of the stub */
433         dce_conn->auth_state.auth_info->auth_pad_length =
434                 (16 - (pkt->u.response.stub_and_verifier.length & 15)) & 15;
435         ndr_err = ndr_push_zero(ndr,
436                                 dce_conn->auth_state.auth_info->auth_pad_length);
437         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
438                 return false;
439         }
440
441         payload_length = pkt->u.response.stub_and_verifier.length +
442                 dce_conn->auth_state.auth_info->auth_pad_length;
443
444         /* we start without signature, it will appended later */
445         dce_conn->auth_state.auth_info->credentials = data_blob(NULL, 0);
446
447         /* add the auth verifier */
448         ndr_err = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS,
449                                        dce_conn->auth_state.auth_info);
450         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
451                 return false;
452         }
453
454         /* extract the whole packet as a blob */
455         *blob = ndr_push_blob(ndr);
456
457         /*
458          * Setup the frag and auth length in the packet buffer.
459          * This is needed if the GENSEC mech does AEAD signing
460          * of the packet headers. The signature itself will be
461          * appended later.
462          */
463         dcerpc_set_frag_length(blob, blob->length + sig_size);
464         dcerpc_set_auth_length(blob, sig_size);
465
466         /* sign or seal the packet */
467         switch (dce_conn->auth_state.auth_info->auth_level) {
468         case DCERPC_AUTH_LEVEL_PRIVACY:
469                 status = gensec_seal_packet(dce_conn->auth_state.gensec_security, 
470                                             call,
471                                             ndr->data + DCERPC_REQUEST_LENGTH, 
472                                             payload_length,
473                                             blob->data,
474                                             blob->length,
475                                             &creds2);
476                 break;
477
478         case DCERPC_AUTH_LEVEL_INTEGRITY:
479                 status = gensec_sign_packet(dce_conn->auth_state.gensec_security, 
480                                             call,
481                                             ndr->data + DCERPC_REQUEST_LENGTH, 
482                                             payload_length,
483                                             blob->data,
484                                             blob->length,
485                                             &creds2);
486                 break;
487
488         default:
489                 status = NT_STATUS_INVALID_LEVEL;
490                 break;
491         }
492
493         if (!NT_STATUS_IS_OK(status)) {
494                 return false;
495         }       
496
497         if (creds2.length != sig_size) {
498                 DEBUG(3,("dcesrv_auth_response: creds2.length[%u] != sig_size[%u] pad[%u] stub[%u]\n",
499                          (unsigned)creds2.length, (uint32_t)sig_size,
500                          (unsigned)dce_conn->auth_state.auth_info->auth_pad_length,
501                          (unsigned)pkt->u.response.stub_and_verifier.length));
502                 dcerpc_set_frag_length(blob, blob->length + creds2.length);
503                 dcerpc_set_auth_length(blob, creds2.length);
504         }
505
506         if (!data_blob_append(call, blob, creds2.data, creds2.length)) {
507                 status = NT_STATUS_NO_MEMORY;
508                 return false;
509         }
510         data_blob_free(&creds2);
511
512         return true;
513 }