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