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