s4-rpc: paranoid check for auth_length
[abartlet/samba.git/.git] / source4 / librpc / rpc / dcerpc_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc utility functions
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Jelmer Vernooij 2004
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
9    Copyright (C) Rafal Szczesniak 2006
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "lib/events/events.h"
27 #include "libcli/composite/composite.h"
28 #include "librpc/gen_ndr/ndr_epmapper_c.h"
29 #include "librpc/gen_ndr/ndr_dcerpc.h"
30 #include "librpc/gen_ndr/ndr_misc.h"
31 #include "librpc/rpc/dcerpc_proto.h"
32 #include "auth/credentials/credentials.h"
33 #include "param/param.h"
34
35 /*
36   find a dcerpc call on an interface by name
37 */
38 const struct ndr_interface_call *dcerpc_iface_find_call(const struct ndr_interface_table *iface,
39                                                         const char *name)
40 {
41         int i;
42         for (i=0;i<iface->num_calls;i++) {
43                 if (strcmp(iface->calls[i].name, name) == 0) {
44                         return &iface->calls[i];
45                 }
46         }
47         return NULL;
48 }
49
50 /* 
51    push a ncacn_packet into a blob, potentially with auth info
52 */
53 NTSTATUS ncacn_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
54                          struct smb_iconv_convenience *iconv_convenience,
55                          struct ncacn_packet *pkt,
56                          struct dcerpc_auth *auth_info)
57 {
58         struct ndr_push *ndr;
59         enum ndr_err_code ndr_err;
60
61         ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
62         if (!ndr) {
63                 return NT_STATUS_NO_MEMORY;
64         }
65
66         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
67                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
68         }
69
70         if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
71                 ndr->flags |= LIBNDR_FLAG_OBJECT_PRESENT;
72         }
73
74         if (auth_info) {
75                 pkt->auth_length = auth_info->credentials.length;
76         } else {
77                 pkt->auth_length = 0;
78         }
79
80         ndr_err = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
81         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
82                 return ndr_map_error2ntstatus(ndr_err);
83         }
84
85         if (auth_info) {
86 #if 0
87                 /* the s3 rpc server doesn't handle auth padding in
88                    bind requests. Use zero auth padding to keep us
89                    working with old servers */
90                 uint32_t offset = ndr->offset;
91                 ndr_err = ndr_push_align(ndr, 16);
92                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
93                         return ndr_map_error2ntstatus(ndr_err);
94                 }
95                 auth_info->auth_pad_length = ndr->offset - offset;
96 #else
97                 auth_info->auth_pad_length = 0;
98 #endif
99                 ndr_err = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth_info);
100                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
101                         return ndr_map_error2ntstatus(ndr_err);
102                 }
103         }
104
105         *blob = ndr_push_blob(ndr);
106
107         /* fill in the frag length */
108         dcerpc_set_frag_length(blob, blob->length);
109
110         return NT_STATUS_OK;
111 }
112
113
114 struct epm_map_binding_state {
115         struct dcerpc_binding *binding;
116         const struct ndr_interface_table *table;
117         struct dcerpc_pipe *pipe;
118         struct policy_handle handle;
119         struct GUID guid;
120         struct epm_twr_t twr;
121         struct epm_twr_t *twr_r;
122         struct epm_Map r;
123 };
124
125
126 static void continue_epm_recv_binding(struct composite_context *ctx);
127 static void continue_epm_map(struct rpc_request *req);
128
129
130 /*
131   Stage 2 of epm_map_binding: Receive connected rpc pipe and send endpoint
132   mapping rpc request
133 */
134 static void continue_epm_recv_binding(struct composite_context *ctx)
135 {
136         struct rpc_request *map_req;
137
138         struct composite_context *c = talloc_get_type(ctx->async.private_data,
139                                                       struct composite_context);
140         struct epm_map_binding_state *s = talloc_get_type(c->private_data,
141                                                           struct epm_map_binding_state);
142
143         /* receive result of rpc pipe connect request */
144         c->status = dcerpc_pipe_connect_b_recv(ctx, c, &s->pipe);
145         if (!composite_is_ok(c)) return;
146
147         s->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
148
149         /* prepare requested binding parameters */
150         s->binding->object         = s->table->syntax_id;
151
152         c->status = dcerpc_binding_build_tower(s->pipe, s->binding, &s->twr.tower);
153         if (!composite_is_ok(c)) return;
154         
155         /* with some nice pretty paper around it of course */
156         s->r.in.object        = &s->guid;
157         s->r.in.map_tower     = &s->twr;
158         s->r.in.entry_handle  = &s->handle;
159         s->r.in.max_towers    = 1;
160         s->r.out.entry_handle = &s->handle;
161
162         /* send request for an endpoint mapping - a rpc request on connected pipe */
163         map_req = dcerpc_epm_Map_send(s->pipe, c, &s->r);
164         if (composite_nomem(map_req, c)) return;
165         
166         composite_continue_rpc(c, map_req, continue_epm_map, c);
167 }
168
169
170 /*
171   Stage 3 of epm_map_binding: Receive endpoint mapping and provide binding details
172 */
173 static void continue_epm_map(struct rpc_request *req)
174 {
175         struct composite_context *c = talloc_get_type(req->async.private_data,
176                                                       struct composite_context);
177         struct epm_map_binding_state *s = talloc_get_type(c->private_data,
178                                                           struct epm_map_binding_state);
179
180         /* receive result of a rpc request */
181         c->status = dcerpc_ndr_request_recv(req);
182         if (!composite_is_ok(c)) return;
183
184         /* check the details */
185         if (s->r.out.result != 0 || *s->r.out.num_towers != 1) {
186                 composite_error(c, NT_STATUS_PORT_UNREACHABLE);
187                 return;
188         }
189         
190         s->twr_r = s->r.out.towers[0].twr;
191         if (s->twr_r == NULL) {
192                 composite_error(c, NT_STATUS_PORT_UNREACHABLE);
193                 return;
194         }
195
196         if (s->twr_r->tower.num_floors != s->twr.tower.num_floors ||
197             s->twr_r->tower.floors[3].lhs.protocol != s->twr.tower.floors[3].lhs.protocol) {
198                 composite_error(c, NT_STATUS_PORT_UNREACHABLE);
199                 return;
200         }
201
202         /* get received endpoint */
203         s->binding->endpoint = talloc_reference(s->binding,
204                                                 dcerpc_floor_get_rhs_data(c, &s->twr_r->tower.floors[3]));
205         if (composite_nomem(s->binding->endpoint, c)) return;
206
207         composite_done(c);
208 }
209
210
211 /*
212   Request for endpoint mapping of dcerpc binding - try to request for endpoint
213   unless there is default one.
214 */
215 struct composite_context *dcerpc_epm_map_binding_send(TALLOC_CTX *mem_ctx,
216                                                       struct dcerpc_binding *binding,
217                                                       const struct ndr_interface_table *table,
218                                                       struct tevent_context *ev,
219                                                       struct loadparm_context *lp_ctx)
220 {
221         struct composite_context *c;
222         struct epm_map_binding_state *s;
223         struct composite_context *pipe_connect_req;
224         struct cli_credentials *anon_creds;
225
226         NTSTATUS status;
227         struct dcerpc_binding *epmapper_binding;
228         int i;
229
230         if (ev == NULL) {
231                 return NULL;
232         }
233
234         /* composite context allocation and setup */
235         c = composite_create(mem_ctx, ev);
236         if (c == NULL) {
237                 return NULL;
238         }
239
240         s = talloc_zero(c, struct epm_map_binding_state);
241         if (composite_nomem(s, c)) return c;
242         c->private_data = s;
243
244         s->binding = binding;
245         s->table   = table;
246
247         /* anonymous credentials for rpc connection used to get endpoint mapping */
248         anon_creds = cli_credentials_init(mem_ctx);
249         cli_credentials_set_anonymous(anon_creds);
250
251         /*
252           First, check if there is a default endpoint specified in the IDL
253         */
254         if (table != NULL) {
255                 struct dcerpc_binding *default_binding;
256
257                 /* Find one of the default pipes for this interface */
258                 for (i = 0; i < table->endpoints->count; i++) {
259                         status = dcerpc_parse_binding(mem_ctx, table->endpoints->names[i], &default_binding);
260
261                         if (NT_STATUS_IS_OK(status)) {
262                                 if (binding->transport == NCA_UNKNOWN) 
263                                         binding->transport = default_binding->transport;
264                                 if (default_binding->transport == binding->transport && 
265                                         default_binding->endpoint) {
266                                         binding->endpoint = talloc_reference(binding, default_binding->endpoint);
267                                         talloc_free(default_binding);
268
269                                         composite_done(c);
270                                         return c;
271
272                                 } else {
273                                         talloc_free(default_binding);
274                                 }
275                         }
276                 }
277         }
278
279         epmapper_binding = talloc_zero(c, struct dcerpc_binding);
280         if (composite_nomem(epmapper_binding, c)) return c;
281
282         /* basic endpoint mapping data */
283         epmapper_binding->transport             = binding->transport;
284         epmapper_binding->host                  = talloc_reference(epmapper_binding, binding->host);
285         epmapper_binding->target_hostname       = epmapper_binding->host;
286         epmapper_binding->options               = NULL;
287         epmapper_binding->flags                 = 0;
288         epmapper_binding->assoc_group_id        = 0;
289         epmapper_binding->endpoint              = NULL;
290
291         /* initiate rpc pipe connection */
292         pipe_connect_req = dcerpc_pipe_connect_b_send(c, epmapper_binding, 
293                                                       &ndr_table_epmapper,
294                                                       anon_creds, c->event_ctx,
295                                                       lp_ctx);
296         if (composite_nomem(pipe_connect_req, c)) return c;
297         
298         composite_continue(c, pipe_connect_req, continue_epm_recv_binding, c);
299         return c;
300 }
301
302
303 /*
304   Receive result of endpoint mapping request
305  */
306 NTSTATUS dcerpc_epm_map_binding_recv(struct composite_context *c)
307 {
308         NTSTATUS status = composite_wait(c);
309         
310         talloc_free(c);
311         return status;
312 }
313
314
315 /*
316   Get endpoint mapping for rpc connection
317 */
318 _PUBLIC_ NTSTATUS dcerpc_epm_map_binding(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding,
319                                 const struct ndr_interface_table *table, struct tevent_context *ev,
320                                 struct loadparm_context *lp_ctx)
321 {
322         struct composite_context *c;
323
324         c = dcerpc_epm_map_binding_send(mem_ctx, binding, table, ev, lp_ctx);
325         return dcerpc_epm_map_binding_recv(c);
326 }
327
328
329 struct pipe_auth_state {
330         struct dcerpc_pipe *pipe;
331         struct dcerpc_binding *binding;
332         const struct ndr_interface_table *table;
333         struct loadparm_context *lp_ctx;
334         struct cli_credentials *credentials;
335 };
336
337
338 static void continue_auth_schannel(struct composite_context *ctx);
339 static void continue_auth(struct composite_context *ctx);
340 static void continue_auth_none(struct composite_context *ctx);
341 static void continue_ntlmssp_connection(struct composite_context *ctx);
342 static void continue_spnego_after_wrong_pass(struct composite_context *ctx);
343
344
345 /*
346   Stage 2 of pipe_auth: Receive result of schannel bind request
347 */
348 static void continue_auth_schannel(struct composite_context *ctx)
349 {
350         struct composite_context *c = talloc_get_type(ctx->async.private_data,
351                                                       struct composite_context);
352
353         c->status = dcerpc_bind_auth_schannel_recv(ctx);
354         if (!composite_is_ok(c)) return;
355
356         composite_done(c);
357 }
358
359
360 /*
361   Stage 2 of pipe_auth: Receive result of authenticated bind request
362 */
363 static void continue_auth(struct composite_context *ctx)
364 {
365         struct composite_context *c = talloc_get_type(ctx->async.private_data,
366                                                       struct composite_context);
367
368         c->status = dcerpc_bind_auth_recv(ctx);
369         if (!composite_is_ok(c)) return;
370         
371         composite_done(c);
372 }
373 /*
374   Stage 2 of pipe_auth: Receive result of authenticated bind request, but handle fallbacks:
375   SPNEGO -> NTLMSSP
376 */
377 static void continue_auth_auto(struct composite_context *ctx)
378 {
379         struct composite_context *c = talloc_get_type(ctx->async.private_data,
380                                                       struct composite_context);
381         struct pipe_auth_state *s = talloc_get_type(c->private_data, struct pipe_auth_state);
382         struct composite_context *sec_conn_req;
383
384         c->status = dcerpc_bind_auth_recv(ctx);
385         if (NT_STATUS_EQUAL(c->status, NT_STATUS_INVALID_PARAMETER)) {
386                 /*
387                  * Retry with NTLMSSP auth as fallback
388                  * send a request for secondary rpc connection
389                  */
390                 sec_conn_req = dcerpc_secondary_connection_send(s->pipe,
391                                                                 s->binding);
392                 composite_continue(c, sec_conn_req, continue_ntlmssp_connection, c);
393                 return;
394         } else if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
395                 if (cli_credentials_wrong_password(s->credentials)) {
396                         /*
397                          * Retry SPNEGO with a better password
398                          * send a request for secondary rpc connection
399                          */
400                         sec_conn_req = dcerpc_secondary_connection_send(s->pipe,
401                                                                         s->binding);
402                         composite_continue(c, sec_conn_req, continue_spnego_after_wrong_pass, c);
403                         return;
404                 }
405         }
406
407         if (!composite_is_ok(c)) return;
408
409         composite_done(c);
410 }
411
412 /*
413   Stage 3 of pipe_auth (fallback to NTLMSSP case): Receive secondary
414   rpc connection (the first one can't be used any more, due to the
415   bind nak) and perform authenticated bind request
416 */
417 static void continue_ntlmssp_connection(struct composite_context *ctx)
418 {
419         struct composite_context *c;
420         struct pipe_auth_state *s;
421         struct composite_context *auth_req;
422         struct dcerpc_pipe *p2;
423
424         c = talloc_get_type(ctx->async.private_data, struct composite_context);
425         s = talloc_get_type(c->private_data, struct pipe_auth_state);
426
427         /* receive secondary rpc connection */
428         c->status = dcerpc_secondary_connection_recv(ctx, &p2);
429         if (!composite_is_ok(c)) return;
430
431         talloc_steal(s, p2);
432         talloc_steal(p2, s->pipe);
433         s->pipe = p2;
434
435         /* initiate a authenticated bind */
436         auth_req = dcerpc_bind_auth_send(c, s->pipe, s->table,
437                                          s->credentials, 
438                                          lp_gensec_settings(c, s->lp_ctx),
439                                          DCERPC_AUTH_TYPE_NTLMSSP,
440                                          dcerpc_auth_level(s->pipe->conn),
441                                          s->table->authservices->names[0]);
442         composite_continue(c, auth_req, continue_auth, c);
443 }
444
445 /*
446   Stage 3 of pipe_auth (retry on wrong password): Receive secondary
447   rpc connection (the first one can't be used any more, due to the
448   bind nak) and perform authenticated bind request
449 */
450 static void continue_spnego_after_wrong_pass(struct composite_context *ctx)
451 {
452         struct composite_context *c;
453         struct pipe_auth_state *s;
454         struct composite_context *auth_req;
455         struct dcerpc_pipe *p2;
456
457         c = talloc_get_type(ctx->async.private_data, struct composite_context);
458         s = talloc_get_type(c->private_data, struct pipe_auth_state);
459
460         /* receive secondary rpc connection */
461         c->status = dcerpc_secondary_connection_recv(ctx, &p2);
462         if (!composite_is_ok(c)) return;
463
464         talloc_steal(s, p2);
465         talloc_steal(p2, s->pipe);
466         s->pipe = p2;
467
468         /* initiate a authenticated bind */
469         auth_req = dcerpc_bind_auth_send(c, s->pipe, s->table,
470                                          s->credentials, 
471                                          lp_gensec_settings(c, s->lp_ctx), 
472                                          DCERPC_AUTH_TYPE_SPNEGO,
473                                          dcerpc_auth_level(s->pipe->conn),
474                                          s->table->authservices->names[0]);
475         composite_continue(c, auth_req, continue_auth, c);
476 }
477
478
479 /*
480   Stage 2 of pipe_auth: Receive result of non-authenticated bind request
481 */
482 static void continue_auth_none(struct composite_context *ctx)
483 {
484         struct composite_context *c = talloc_get_type(ctx->async.private_data,
485                                                       struct composite_context);
486
487         c->status = dcerpc_bind_auth_none_recv(ctx);
488         if (!composite_is_ok(c)) return;
489         
490         composite_done(c);
491 }
492
493
494 /*
495   Request to perform an authenticated bind if required. Authentication
496   is determined using credentials passed and binding flags.
497 */
498 struct composite_context *dcerpc_pipe_auth_send(struct dcerpc_pipe *p, 
499                                                 struct dcerpc_binding *binding,
500                                                 const struct ndr_interface_table *table,
501                                                 struct cli_credentials *credentials,
502                                                 struct loadparm_context *lp_ctx)
503 {
504         struct composite_context *c;
505         struct pipe_auth_state *s;
506         struct composite_context *auth_schannel_req;
507         struct composite_context *auth_req;
508         struct composite_context *auth_none_req;
509         struct dcerpc_connection *conn;
510         uint8_t auth_type;
511
512         /* composite context allocation and setup */
513         c = composite_create(p, p->conn->event_ctx);
514         if (c == NULL) return NULL;
515
516         s = talloc_zero(c, struct pipe_auth_state);
517         if (composite_nomem(s, c)) return c;
518         c->private_data = s;
519
520         /* store parameters in state structure */
521         s->binding      = binding;
522         s->table        = table;
523         s->credentials  = credentials;
524         s->pipe         = p;
525         s->lp_ctx       = lp_ctx;
526
527         conn = s->pipe->conn;
528         conn->flags = binding->flags;
529
530         if (DEBUGLVL(100)) {
531                 conn->flags |= DCERPC_DEBUG_PRINT_BOTH;
532         }
533         
534         /* remember the binding string for possible secondary connections */
535         conn->binding_string = dcerpc_binding_string(p, binding);
536
537         if (cli_credentials_is_anonymous(s->credentials)) {
538                 auth_none_req = dcerpc_bind_auth_none_send(c, s->pipe, s->table);
539                 composite_continue(c, auth_none_req, continue_auth_none, c);
540                 return c;
541         }
542
543         if ((binding->flags & DCERPC_SCHANNEL) &&
544             !cli_credentials_get_netlogon_creds(s->credentials)) {
545                 /* If we don't already have netlogon credentials for
546                  * the schannel bind, then we have to get these
547                  * first */
548                 auth_schannel_req = dcerpc_bind_auth_schannel_send(c, s->pipe, s->table,
549                                                                    s->credentials, s->lp_ctx,
550                                                                    dcerpc_auth_level(conn));
551                 composite_continue(c, auth_schannel_req, continue_auth_schannel, c);
552                 return c;
553         }
554
555         /*
556          * we rely on the already authenticated CIFS connection
557          * if not doing sign or seal
558          */
559         if (conn->transport.transport == NCACN_NP &&
560             !(s->binding->flags & (DCERPC_SIGN|DCERPC_SEAL))) {
561                 auth_none_req = dcerpc_bind_auth_none_send(c, s->pipe, s->table);
562                 composite_continue(c, auth_none_req, continue_auth_none, c);
563                 return c;
564         }
565
566
567         /* Perform an authenticated DCE-RPC bind
568          */
569         if (!(conn->flags & (DCERPC_SIGN|DCERPC_SEAL))) {
570                 /*
571                   we are doing an authenticated connection,
572                   but not using sign or seal. We must force
573                   the CONNECT dcerpc auth type as a NONE auth
574                   type doesn't allow authentication
575                   information to be passed.
576                 */
577                 conn->flags |= DCERPC_CONNECT;
578         }
579
580         if (s->binding->flags & DCERPC_AUTH_SPNEGO) {
581                 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
582
583         } else if (s->binding->flags & DCERPC_AUTH_KRB5) {
584                 auth_type = DCERPC_AUTH_TYPE_KRB5;
585
586         } else if (s->binding->flags & DCERPC_SCHANNEL) {
587                 auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
588
589         } else if (s->binding->flags & DCERPC_AUTH_NTLM) {
590                 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
591
592         } else {
593                 /* try SPNEGO with fallback to NTLMSSP */
594                 auth_req = dcerpc_bind_auth_send(c, s->pipe, s->table,
595                                                  s->credentials, 
596                                                  lp_gensec_settings(c, s->lp_ctx), 
597                                                  DCERPC_AUTH_TYPE_SPNEGO,
598                                                  dcerpc_auth_level(conn),
599                                                  s->table->authservices->names[0]);
600                 composite_continue(c, auth_req, continue_auth_auto, c);
601                 return c;
602         }
603
604         auth_req = dcerpc_bind_auth_send(c, s->pipe, s->table,
605                                          s->credentials, 
606                                          lp_gensec_settings(c, s->lp_ctx), 
607                                          auth_type,
608                                          dcerpc_auth_level(conn),
609                                          s->table->authservices->names[0]);
610         composite_continue(c, auth_req, continue_auth, c);
611         return c;
612 }
613
614
615 /*
616   Receive result of authenticated bind request on dcerpc pipe
617
618   This returns *p, which may be different to the one originally
619   supllied, as it rebinds to a new pipe due to authentication fallback
620
621 */
622 NTSTATUS dcerpc_pipe_auth_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, 
623                                struct dcerpc_pipe **p)
624 {
625         NTSTATUS status;
626
627         struct pipe_auth_state *s = talloc_get_type(c->private_data,
628                                                     struct pipe_auth_state);
629         status = composite_wait(c);
630         if (!NT_STATUS_IS_OK(status)) {
631                 char *uuid_str = GUID_string(s->pipe, &s->table->syntax_id.uuid);
632                 DEBUG(0, ("Failed to bind to uuid %s - %s\n", uuid_str, nt_errstr(status)));
633                 talloc_free(uuid_str);
634         } else {
635                 talloc_steal(mem_ctx, s->pipe);
636                 *p = s->pipe;
637         }
638
639         talloc_free(c);
640         return status;
641 }
642
643
644 /* 
645    Perform an authenticated bind if needed - sync version
646
647    This may change *p, as it rebinds to a new pipe due to authentication fallback
648 */
649 _PUBLIC_ NTSTATUS dcerpc_pipe_auth(TALLOC_CTX *mem_ctx,
650                           struct dcerpc_pipe **p, 
651                           struct dcerpc_binding *binding,
652                           const struct ndr_interface_table *table,
653                           struct cli_credentials *credentials,
654                           struct loadparm_context *lp_ctx)
655 {
656         struct composite_context *c;
657
658         c = dcerpc_pipe_auth_send(*p, binding, table, credentials, lp_ctx);
659         return dcerpc_pipe_auth_recv(c, mem_ctx, p);
660 }
661
662
663 NTSTATUS dcerpc_generic_session_key(struct dcerpc_connection *c,
664                                     DATA_BLOB *session_key)
665 {
666         /* this took quite a few CPU cycles to find ... */
667         session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
668         session_key->length = 16;
669         return NT_STATUS_OK;
670 }
671
672 /*
673   fetch the user session key - may be default (above) or the SMB session key
674
675   The key is always truncated to 16 bytes 
676 */
677 _PUBLIC_ NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
678                                            DATA_BLOB *session_key)
679 {
680         NTSTATUS status;
681         status = p->conn->security_state.session_key(p->conn, session_key);
682         if (!NT_STATUS_IS_OK(status)) {
683                 return status;
684         }
685
686         session_key->length = MIN(session_key->length, 16);
687
688         return NT_STATUS_OK;
689 }
690
691
692 /*
693   log a rpc packet in a format suitable for ndrdump. This is especially useful
694   for sealed packets, where ethereal cannot easily see the contents
695
696   this triggers on a debug level of >= 10
697 */
698 _PUBLIC_ void dcerpc_log_packet(const char *lockdir,
699                                                                 const struct ndr_interface_table *ndr,
700                        uint32_t opnum, uint32_t flags, 
701                        DATA_BLOB *pkt)
702 {
703         const int num_examples = 20;
704         int i;
705
706         if (lockdir == NULL) return;
707
708         for (i=0;i<num_examples;i++) {
709                 char *name=NULL;
710                 asprintf(&name, "%s/rpclog/%s-%u.%d.%s", 
711                          lockdir, ndr->name, opnum, i,
712                          (flags&NDR_IN)?"in":"out");
713                 if (name == NULL) {
714                         return;
715                 }
716                 if (!file_exist(name)) {
717                         if (file_save(name, pkt->data, pkt->length)) {
718                                 DEBUG(10,("Logged rpc packet to %s\n", name));
719                         }
720                         free(name);
721                         break;
722                 }
723                 free(name);
724         }
725 }
726
727
728
729 /*
730   create a secondary context from a primary connection
731
732   this uses dcerpc_alter_context() to create a new dcerpc context_id
733 */
734 _PUBLIC_ NTSTATUS dcerpc_secondary_context(struct dcerpc_pipe *p, 
735                                   struct dcerpc_pipe **pp2,
736                                   const struct ndr_interface_table *table)
737 {
738         NTSTATUS status;
739         struct dcerpc_pipe *p2;
740         
741         p2 = talloc_zero(p, struct dcerpc_pipe);
742         if (p2 == NULL) {
743                 return NT_STATUS_NO_MEMORY;
744         }
745         p2->conn = talloc_reference(p2, p->conn);
746         p2->request_timeout = p->request_timeout;
747
748         p2->context_id = ++p->conn->next_context_id;
749
750         p2->syntax = table->syntax_id;
751
752         p2->transfer_syntax = p->transfer_syntax;
753
754         p2->binding = talloc_reference(p2, p->binding);
755
756         status = dcerpc_alter_context(p2, p2, &p2->syntax, &p2->transfer_syntax);
757         if (!NT_STATUS_IS_OK(status)) {
758                 talloc_free(p2);
759                 return status;
760         }
761
762         *pp2 = p2;
763
764         return NT_STATUS_OK;
765 }
766
767
768 /*
769   pull an dcerpc_auth structure, taking account of any auth padding in
770   the blob at the end of the structure
771  */
772 NTSTATUS dcerpc_pull_auth_trailer(struct ncacn_packet *pkt,
773                                   TALLOC_CTX *mem_ctx,
774                                   DATA_BLOB *pkt_auth_blob,
775                                   struct dcerpc_auth *auth,
776                                   uint32_t *auth_length,
777                                   bool check_pad)
778 {
779         struct ndr_pull *ndr;
780         enum ndr_err_code ndr_err;
781         uint32_t pad;
782
783         pad = pkt_auth_blob->length - (DCERPC_AUTH_TRAILER_LENGTH + pkt->auth_length);
784
785         /* paranoia check for pad size. This would be caught anyway by
786            the ndr_pull_advance() a few lines down, but it scared
787            Jeremy enough for him to call me, so we might as well check
788            it now, just to prevent someone posting a bogus YouTube
789            video in the future.
790         */
791         if (pad > pkt_auth_blob->length) {
792                 return NT_STATUS_INFO_LENGTH_MISMATCH;
793         }
794
795         *auth_length = pkt_auth_blob->length - pad;
796
797         ndr = ndr_pull_init_blob(pkt_auth_blob, mem_ctx, NULL);
798         if (!ndr) {
799                 return NT_STATUS_NO_MEMORY;
800         }
801
802         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
803                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
804         }
805
806         ndr_err = ndr_pull_advance(ndr, pad);
807         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
808                 talloc_free(ndr);
809                 return ndr_map_error2ntstatus(ndr_err);
810         }
811
812         ndr_err = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth);
813         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
814                 talloc_free(ndr);
815                 return ndr_map_error2ntstatus(ndr_err);
816         }
817
818         if (check_pad && pad != auth->auth_pad_length) {
819                 DEBUG(1,(__location__ ": WARNING: pad length mismatch. Calculated %u  got %u\n",
820                          (unsigned)pad, (unsigned)auth->auth_pad_length));
821         }
822
823         DEBUG(6,(__location__ ": auth_pad_length %u\n",
824                  (unsigned)auth->auth_pad_length));
825
826         talloc_steal(mem_ctx, auth->credentials.data);
827         talloc_free(ndr);
828
829         return NT_STATUS_OK;
830 }
831
832