1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
|
/*
Unix SMB/CIFS implementation.
dcerpc connect functions
Copyright (C) Andrew Tridgell 2003
Copyright (C) Jelmer Vernooij 2004
Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
Copyright (C) Rafal Szczesniak 2005
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
#include "libcli/composite/composite.h"
#include "libcli/smb_composite/smb_composite.h"
#include "lib/events/events.h"
#include "libcli/smb2/smb2.h"
#include "libcli/smb2/smb2_calls.h"
#include "librpc/rpc/dcerpc.h"
#include "auth/credentials/credentials.h"
struct pipe_np_smb_state {
struct smb_composite_connect conn;
struct smbcli_tree *tree;
struct dcerpc_pipe_connect io;
};
/*
Stage 3 of ncacn_np_smb: Named pipe opened (or not)
*/
static void continue_pipe_open_smb(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
/* receive result of named pipe open request on smb */
c->status = dcerpc_pipe_open_smb_recv(ctx);
if (!composite_is_ok(c)) return;
composite_done(c);
}
/*
Stage 2 of ncacn_np_smb: Open a named pipe after successful smb connection
*/
static void continue_smb_connect(struct composite_context *ctx)
{
struct composite_context *open_ctx;
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
struct pipe_np_smb_state);
/* receive result of smb connect request */
c->status = smb_composite_connect_recv(ctx, c);
if (!composite_is_ok(c)) return;
/* prepare named pipe open parameters */
s->tree = s->conn.out.tree;
s->io.pipe_name = s->io.binding->endpoint;
/* send named pipe open request */
open_ctx = dcerpc_pipe_open_smb_send(s->io.pipe->conn, s->tree, s->io.pipe_name);
if (composite_nomem(open_ctx, c)) return;
composite_continue(c, open_ctx, continue_pipe_open_smb, c);
}
/*
Initiate async open of a rpc connection to a rpc pipe on SMB using
the binding structure to determine the endpoint and options
*/
struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *mem_ctx,
struct dcerpc_pipe_connect *io)
{
struct composite_context *c;
struct pipe_np_smb_state *s;
struct composite_context *conn_req;
struct smb_composite_connect *conn;
/* composite context allocation and setup */
c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
if (c == NULL) return NULL;
s = talloc_zero(c, struct pipe_np_smb_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
s->io = *io;
conn = &s->conn;
/* prepare smb connection parameters: we're connecting to IPC$ share on
remote rpc server */
conn->in.dest_host = s->io.binding->host;
conn->in.port = 0;
conn->in.called_name = strupper_talloc(mem_ctx, s->io.binding->host);
conn->in.service = "IPC$";
conn->in.service_type = NULL;
conn->in.workgroup = lp_workgroup();
/* verify if called_name has been allocated when uppercasing */
if (composite_nomem(conn->in.called_name, c)) return c;
/*
* provide proper credentials - user supplied, but allow a
* fallback to anonymous if this is an schannel connection
* (might be NT4 not allowing machine logins at session
* setup).
*/
s->conn.in.credentials = s->io.creds;
if (s->io.binding->flags & DCERPC_SCHANNEL) {
conn->in.fallback_to_anonymous = True;
} else {
conn->in.fallback_to_anonymous = False;
}
/* send smb connect request */
conn_req = smb_composite_connect_send(conn, s->io.pipe->conn, s->io.pipe->conn->event_ctx);
if (composite_nomem(conn_req, c)) return c;
composite_continue(c, conn_req, continue_smb_connect, c);
return c;
}
/*
Receive result of a rpc connection to a rpc pipe on SMB
*/
NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
{
NTSTATUS status = composite_wait(c);
talloc_free(c);
return status;
}
/*
Sync version of a rpc connection to a rpc pipe on SMB
*/
NTSTATUS dcerpc_pipe_connect_ncacn_np_smb(TALLOC_CTX *mem_ctx,
struct dcerpc_pipe_connect *io)
{
struct composite_context *c;
c = dcerpc_pipe_connect_ncacn_np_smb_send(mem_ctx, io);
return dcerpc_pipe_connect_ncacn_np_smb_recv(c);
}
struct pipe_np_smb2_state {
struct smb2_tree *tree;
struct dcerpc_pipe_connect io;
};
/*
Stage 3 of ncacn_np_smb: Named pipe opened (or not)
*/
static void continue_pipe_open_smb2(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
/* receive result of named pipe open request on smb2 */
c->status = dcerpc_pipe_open_smb2_recv(ctx);
if (!composite_is_ok(c)) return;
composite_done(c);
}
/*
Stage 2 of ncacn_np_smb2: Open a named pipe after successful smb2 connection
*/
static void continue_smb2_connect(struct composite_context *ctx)
{
struct composite_context *open_req;
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
struct pipe_np_smb2_state *s = talloc_get_type(c->private_data,
struct pipe_np_smb2_state);
/* receive result of smb2 connect request */
c->status = smb2_connect_recv(ctx, c, &s->tree);
if (!composite_is_ok(c)) return;
/* prepare named pipe open parameters */
s->io.pipe_name = s->io.binding->endpoint;
/* send named pipe open request */
open_req = dcerpc_pipe_open_smb2_send(s->io.pipe->conn, s->tree, s->io.pipe_name);
if (composite_nomem(open_req, c)) return;
composite_continue(c, open_req, continue_pipe_open_smb2, c);
}
/*
Initiate async open of a rpc connection request on SMB2 using
the binding structure to determine the endpoint and options
*/
struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(TALLOC_CTX *mem_ctx,
struct dcerpc_pipe_connect *io)
{
struct composite_context *c;
struct pipe_np_smb2_state *s;
struct composite_context *conn_req;
/* composite context allocation and setup */
c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
if (c == NULL) return NULL;
s = talloc_zero(c, struct pipe_np_smb2_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
s->io = *io;
/*
* provide proper credentials - user supplied or anonymous in case this is
* schannel connection
*/
if (s->io.binding->flags & DCERPC_SCHANNEL) {
s->io.creds = cli_credentials_init(mem_ctx);
if (composite_nomem(s->io.creds, c)) return c;
cli_credentials_set_anonymous(s->io.creds);
cli_credentials_guess(s->io.creds);
}
/* send smb2 connect request */
conn_req = smb2_connect_send(mem_ctx, s->io.binding->host, "IPC$", s->io.creds,
c->event_ctx);
composite_continue(c, conn_req, continue_smb2_connect, c);
return c;
}
/*
Receive result of a rpc connection to a rpc pipe on SMB2
*/
NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context *c)
{
NTSTATUS status = composite_wait(c);
talloc_free(c);
return status;
}
/*
Sync version of a rpc connection to a rpc pipe on SMB2
*/
NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2(TALLOC_CTX *mem_ctx,
struct dcerpc_pipe_connect *io)
{
struct composite_context *c;
c = dcerpc_pipe_connect_ncacn_np_smb2_send(mem_ctx, io);
return dcerpc_pipe_connect_ncacn_np_smb2_recv(c);
}
struct pipe_ip_tcp_state {
struct dcerpc_pipe_connect io;
const char *host;
uint32_t port;
};
/*
Stage 2 of ncacn_ip_tcp: rpc pipe opened (or not)
*/
static void continue_pipe_open_ncacn_ip_tcp(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
/* receive result of named pipe open request on tcp/ip */
c->status = dcerpc_pipe_open_tcp_recv(ctx);
if (!composite_is_ok(c)) return;
composite_done(c);
}
/*
Initiate async open of a rpc connection to a rpc pipe on TCP/IP using
the binding structure to determine the endpoint and options
*/
struct composite_context* dcerpc_pipe_connect_ncacn_ip_tcp_send(TALLOC_CTX *mem_ctx,
struct dcerpc_pipe_connect *io)
{
struct composite_context *c;
struct pipe_ip_tcp_state *s;
struct composite_context *pipe_req;
/* composite context allocation and setup */
c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
if (c == NULL) return NULL;
s = talloc_zero(c, struct pipe_ip_tcp_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
/* store input parameters in state structure */
s->io = *io;
s->host = talloc_strdup(c, io->binding->host);
s->port = atoi(io->binding->endpoint); /* port number is a binding endpoint here */
/* send pipe open request on tcp/ip */
pipe_req = dcerpc_pipe_open_tcp_send(s->io.pipe->conn, s->host, s->port);
composite_continue(c, pipe_req, continue_pipe_open_ncacn_ip_tcp, c);
return c;
}
/*
Receive result of a rpc connection to a rpc pipe on TCP/IP
*/
NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp_recv(struct composite_context *c)
{
NTSTATUS status = composite_wait(c);
talloc_free(c);
return status;
}
/*
Sync version of rpc connection to a rpc pipe on TCP/IP
*/
NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(TALLOC_CTX *mem_ctx,
struct dcerpc_pipe_connect *io)
{
struct composite_context *c;
c = dcerpc_pipe_connect_ncacn_ip_tcp_send(mem_ctx, io);
return dcerpc_pipe_connect_ncacn_ip_tcp_recv(c);
}
struct pipe_unix_state {
struct dcerpc_pipe_connect io;
const char *path;
};
/*
Stage 2 of ncacn_unix: rpc pipe opened (or not)
*/
static void continue_pipe_open_ncacn_unix_stream(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
/* receive result of pipe open request on unix socket */
c->status = dcerpc_pipe_open_unix_stream_recv(ctx);
if (!composite_is_ok(c)) return;
composite_done(c);
}
/*
Initiate async open of a rpc connection to a rpc pipe on unix socket using
the binding structure to determine the endpoint and options
*/
struct composite_context* dcerpc_pipe_connect_ncacn_unix_stream_send(TALLOC_CTX *mem_ctx,
struct dcerpc_pipe_connect *io)
{
struct composite_context *c;
struct pipe_unix_state *s;
struct composite_context *pipe_req;
/* composite context allocation and setup */
c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
if (c == NULL) return NULL;
s = talloc_zero(c, struct pipe_unix_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
/* prepare pipe open parameters and store them in state structure
also, verify whether biding endpoint is not null */
s->io = *io;
if (!io->binding->endpoint) {
DEBUG(0, ("Path to unix socket not specified\n"));
composite_error(c, NT_STATUS_INVALID_PARAMETER);
return c;
}
s->path = talloc_strdup(c, io->binding->endpoint); /* path is a binding endpoint here */
if (composite_nomem(s->path, c)) return c;
/* send pipe open request on unix socket */
pipe_req = dcerpc_pipe_open_unix_stream_send(s->io.pipe->conn, s->path);
composite_continue(c, pipe_req, continue_pipe_open_ncacn_unix_stream, c);
return c;
}
/*
Receive result of a rpc connection to a pipe on unix socket
*/
NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream_recv(struct composite_context *c)
{
NTSTATUS status = composite_wait(c);
talloc_free(c);
return status;
}
/*
Sync version of a rpc connection to a rpc pipe on unix socket
*/
NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream(TALLOC_CTX *mem_ctx,
struct dcerpc_pipe_connect *io)
{
struct composite_context *c;
c = dcerpc_pipe_connect_ncacn_unix_stream_send(mem_ctx, io);
return dcerpc_pipe_connect_ncacn_unix_stream_recv(c);
}
struct pipe_ncalrpc_state {
struct dcerpc_pipe_connect io;
};
/*
Stage 2 of ncalrpc: rpc pipe opened (or not)
*/
static void continue_pipe_open_ncalrpc(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
/* receive result of pipe open request on ncalrpc */
c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
if (!composite_is_ok(c)) return;
composite_done(c);
}
/*
Initiate async open of a rpc connection request on NCALRPC using
the binding structure to determine the endpoint and options
*/
struct composite_context* dcerpc_pipe_connect_ncalrpc_send(TALLOC_CTX *mem_ctx,
struct dcerpc_pipe_connect *io)
{
struct composite_context *c;
struct pipe_ncalrpc_state *s;
struct composite_context *pipe_req;
/* composite context allocation and setup */
c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
if (c == NULL) return NULL;
s = talloc_zero(c, struct pipe_ncalrpc_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
/* store input parameters in state structure */
s->io = *io;
/* send pipe open request */
pipe_req = dcerpc_pipe_open_pipe_send(s->io.pipe->conn, s->io.binding->endpoint);
composite_continue(c, pipe_req, continue_pipe_open_ncalrpc, c);
return c;
}
/*
Receive result of a rpc connection to a rpc pipe on NCALRPC
*/
NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c)
{
NTSTATUS status = composite_wait(c);
talloc_free(c);
return status;
}
/*
Sync version of a rpc connection to a rpc pipe on NCALRPC
*/
NTSTATUS dcerpc_pipe_connect_ncalrpc(TALLOC_CTX *mem_ctx,
struct dcerpc_pipe_connect *io)
{
struct composite_context *c = dcerpc_pipe_connect_ncalrpc_send(mem_ctx, io);
return dcerpc_pipe_connect_ncalrpc_recv(c);
}
struct pipe_connect_state {
struct dcerpc_pipe *pipe;
struct dcerpc_binding *binding;
const struct dcerpc_interface_table *table;
struct cli_credentials *credentials;
};
static void continue_map_binding(struct composite_context *ctx);
static void continue_connect(struct composite_context *c, struct pipe_connect_state *s);
static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx);
static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx);
static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx);
static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx);
static void continue_pipe_connect_ncalrpc(struct composite_context *ctx);
static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s);
static void continue_pipe_auth(struct composite_context *ctx);
/*
Stage 2 of pipe_connect_b: Receive result of endpoint mapping
*/
static void continue_map_binding(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
struct pipe_connect_state *s = talloc_get_type(c->private_data,
struct pipe_connect_state);
c->status = dcerpc_epm_map_binding_recv(ctx);
if (!composite_is_ok(c)) return;
DEBUG(2,("Mapped to DCERPC endpoint %s\n", s->binding->endpoint));
continue_connect(c, s);
}
/*
Stage 2 of pipe_connect_b: Continue connection after endpoint is known
*/
static void continue_connect(struct composite_context *c, struct pipe_connect_state *s)
{
struct dcerpc_pipe_connect pc;
/* potential exits to another stage by sending an async request */
struct composite_context *ncacn_np_smb2_req;
struct composite_context *ncacn_np_smb_req;
struct composite_context *ncacn_ip_tcp_req;
struct composite_context *ncacn_unix_req;
struct composite_context *ncalrpc_req;
/* dcerpc pipe connect input parameters */
pc.pipe = s->pipe;
pc.binding = s->binding;
pc.interface = s->table;
pc.creds = s->credentials;
/* connect dcerpc pipe depending on required transport */
switch (s->binding->transport) {
case NCACN_NP:
if (pc.binding->flags & DCERPC_SMB2) {
/* new varient of SMB a.k.a. SMB2 */
ncacn_np_smb2_req = dcerpc_pipe_connect_ncacn_np_smb2_send(c, &pc);
composite_continue(c, ncacn_np_smb2_req, continue_pipe_connect_ncacn_np_smb2, c);
return;
} else {
/* good old ordinary SMB */
ncacn_np_smb_req = dcerpc_pipe_connect_ncacn_np_smb_send(c, &pc);
composite_continue(c, ncacn_np_smb_req, continue_pipe_connect_ncacn_np_smb, c);
return;
}
break;
case NCACN_IP_TCP:
ncacn_ip_tcp_req = dcerpc_pipe_connect_ncacn_ip_tcp_send(c, &pc);
composite_continue(c, ncacn_ip_tcp_req, continue_pipe_connect_ncacn_ip_tcp, c);
return;
case NCACN_UNIX_STREAM:
ncacn_unix_req = dcerpc_pipe_connect_ncacn_unix_stream_send(c, &pc);
composite_continue(c, ncacn_unix_req, continue_pipe_connect_ncacn_unix, c);
return;
case NCALRPC:
ncalrpc_req = dcerpc_pipe_connect_ncalrpc_send(c, &pc);
composite_continue(c, ncalrpc_req, continue_pipe_connect_ncalrpc, c);
return;
default:
/* looks like a transport we don't support now */
composite_error(c, NT_STATUS_NOT_SUPPORTED);
}
}
/*
Stage 3 of pipe_connect_b: Receive result of pipe connect request on
named pipe on smb2
*/
static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
struct pipe_connect_state *s = talloc_get_type(c->private_data,
struct pipe_connect_state);
c->status = dcerpc_pipe_connect_ncacn_np_smb2_recv(ctx);
if (!composite_is_ok(c)) return;
continue_pipe_connect(c, s);
}
/*
Stage 3 of pipe_connect_b: Receive result of pipe connect request on
named pipe on smb
*/
static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
struct pipe_connect_state *s = talloc_get_type(c->private_data,
struct pipe_connect_state);
c->status = dcerpc_pipe_connect_ncacn_np_smb_recv(ctx);
if (!composite_is_ok(c)) return;
continue_pipe_connect(c, s);
}
/*
Stage 3 of pipe_connect_b: Receive result of pipe connect request on tcp/ip
*/
static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
struct pipe_connect_state *s = talloc_get_type(c->private_data,
struct pipe_connect_state);
c->status = dcerpc_pipe_connect_ncacn_ip_tcp_recv(ctx);
if (!composite_is_ok(c)) return;
continue_pipe_connect(c, s);
}
/*
Stage 3 of pipe_connect_b: Receive result of pipe connect request on unix socket
*/
static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
struct pipe_connect_state *s = talloc_get_type(c->private_data,
struct pipe_connect_state);
c->status = dcerpc_pipe_connect_ncacn_unix_stream_recv(ctx);
if (!composite_is_ok(c)) return;
continue_pipe_connect(c, s);
}
/*
Stage 3 of pipe_connect_b: Receive result of pipe connect request on local rpc
*/
static void continue_pipe_connect_ncalrpc(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
struct pipe_connect_state *s = talloc_get_type(c->private_data,
struct pipe_connect_state);
c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
if (!composite_is_ok(c)) return;
continue_pipe_connect(c, s);
}
/*
Stage 4 of pipe_connect_b: Start an authentication on connected dcerpc pipe
depending on credentials and binding flags passed.
*/
static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s)
{
struct composite_context *auth_bind_req;
s->pipe->binding = s->binding;
if (!talloc_reference(s->pipe, s->binding)) {
composite_error(c, NT_STATUS_NO_MEMORY);
return;
}
auth_bind_req = dcerpc_pipe_auth_send(s->pipe, s->binding, s->table,
s->credentials);
composite_continue(c, auth_bind_req, continue_pipe_auth, c);
}
/*
Stage 5 of pipe_connect_b: Receive result of pipe authentication request
and say if all went ok
*/
static void continue_pipe_auth(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
struct pipe_connect_state *s = talloc_get_type(c->private_data, struct pipe_connect_state);
c->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe);
if (!composite_is_ok(c)) return;
composite_done(c);
}
/*
start a request to open a rpc connection to a rpc pipe, using
specified binding structure to determine the endpoint and options
*/
struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
struct dcerpc_binding *binding,
const struct dcerpc_interface_table *table,
struct cli_credentials *credentials,
struct event_context *ev)
{
struct composite_context *c;
struct pipe_connect_state *s;
struct event_context *new_ev = NULL;
struct composite_context *binding_req;
if (ev == NULL) {
new_ev = event_context_init(parent_ctx);
if (new_ev == NULL) return NULL;
ev = new_ev;
}
/* composite context allocation and setup */
c = composite_create(parent_ctx, ev);
if (c == NULL) {
talloc_free(new_ev);
return NULL;
}
talloc_steal(c, new_ev);
s = talloc_zero(c, struct pipe_connect_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
/* initialise dcerpc pipe structure */
s->pipe = dcerpc_pipe_init(c, ev);
if (composite_nomem(s->pipe, c)) return c;
/* store parameters in state structure */
s->binding = binding;
s->table = table;
s->credentials = credentials;
switch (s->binding->transport) {
case NCACN_NP:
case NCACN_IP_TCP:
case NCALRPC:
if (!s->binding->endpoint) {
binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
s->pipe->conn->event_ctx);
composite_continue(c, binding_req, continue_map_binding, c);
return c;
}
default:
break;
}
continue_connect(c, s);
return c;
}
/*
receive result of a request to open a rpc connection to a rpc pipe
*/
NTSTATUS dcerpc_pipe_connect_b_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
struct dcerpc_pipe **p)
{
NTSTATUS status;
struct pipe_connect_state *s;
status = composite_wait(c);
s = talloc_get_type(c->private_data, struct pipe_connect_state);
talloc_steal(mem_ctx, s->pipe);
*p = s->pipe;
talloc_free(c);
return status;
}
/*
open a rpc connection to a rpc pipe, using the specified
binding structure to determine the endpoint and options - sync version
*/
NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
struct dcerpc_pipe **pp,
struct dcerpc_binding *binding,
const struct dcerpc_interface_table *table,
struct cli_credentials *credentials,
struct event_context *ev)
{
struct composite_context *c;
c = dcerpc_pipe_connect_b_send(parent_ctx, binding, table,
credentials, ev);
return dcerpc_pipe_connect_b_recv(c, parent_ctx, pp);
}
struct pipe_conn_state {
struct dcerpc_pipe *pipe;
};
static void continue_pipe_connect_b(struct composite_context *ctx);
/*
Initiate rpc connection to a rpc pipe, using the specified string
binding to determine the endpoint and options.
The string is to be parsed to a binding structure first.
*/
struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
struct dcerpc_pipe **pp,
const char *binding,
const struct dcerpc_interface_table *table,
struct cli_credentials *credentials,
struct event_context *ev)
{
struct composite_context *c;
struct pipe_conn_state *s;
struct dcerpc_binding *b;
struct composite_context *pipe_conn_req;
struct event_context *new_ev = NULL;
if (ev == NULL) {
new_ev = event_context_init(parent_ctx);
if (new_ev == NULL) return NULL;
ev = new_ev;
}
/* composite context allocation and setup */
c = composite_create(parent_ctx, ev);
if (c == NULL) {
talloc_free(new_ev);
return NULL;
}
talloc_steal(c, new_ev);
s = talloc_zero(c, struct pipe_conn_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
/* parse binding string to the structure */
c->status = dcerpc_parse_binding(c, binding, &b);
if (!NT_STATUS_IS_OK(c->status)) {
DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
composite_error(c, c->status);
return c;
}
DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(c, b)));
/*
start connecting to a rpc pipe after binding structure
is established
*/
pipe_conn_req = dcerpc_pipe_connect_b_send(c, b, table,
credentials, ev);
composite_continue(c, pipe_conn_req, continue_pipe_connect_b, c);
return c;
}
/*
Stage 2 of pipe_connect: Receive result of actual pipe connect request
and say if we're done ok
*/
static void continue_pipe_connect_b(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
struct pipe_conn_state *s = talloc_get_type(c->private_data,
struct pipe_conn_state);
c->status = dcerpc_pipe_connect_b_recv(ctx, c, &s->pipe);
talloc_steal(s, s->pipe);
if (!composite_is_ok(c)) return;
composite_done(c);
}
/*
Receive result of pipe connect (using binding string) request
and return connected pipe structure.
*/
NTSTATUS dcerpc_pipe_connect_recv(struct composite_context *c,
TALLOC_CTX *mem_ctx,
struct dcerpc_pipe **pp)
{
NTSTATUS status;
struct pipe_conn_state *s;
status = composite_wait(c);
s = talloc_get_type(c->private_data, struct pipe_conn_state);
*pp = talloc_steal(mem_ctx, s->pipe);
talloc_free(c);
return status;
}
/*
Open a rpc connection to a rpc pipe, using the specified string
binding to determine the endpoint and options - sync version
*/
NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx,
struct dcerpc_pipe **pp,
const char *binding,
const struct dcerpc_interface_table *table,
struct cli_credentials *credentials,
struct event_context *ev)
{
struct composite_context *c;
c = dcerpc_pipe_connect_send(parent_ctx, pp, binding, table,
credentials, ev);
return dcerpc_pipe_connect_recv(c, parent_ctx, pp);
}
struct sec_conn_state {
struct dcerpc_pipe *pipe;
struct dcerpc_pipe *pipe2;
struct dcerpc_binding *binding;
struct smbcli_tree *tree;
};
static void continue_open_smb(struct composite_context *ctx);
static void continue_open_tcp(struct composite_context *ctx);
static void continue_open_pipe(struct composite_context *ctx);
static void continue_pipe_open(struct composite_context *c);
/*
Send request to create a secondary dcerpc connection from a primary
connection
*/
struct composite_context* dcerpc_secondary_connection_send(struct dcerpc_pipe *p,
struct dcerpc_binding *b)
{
struct composite_context *c;
struct sec_conn_state *s;
struct composite_context *pipe_smb_req;
struct composite_context *pipe_tcp_req;
struct composite_context *pipe_ncalrpc_req;
/* composite context allocation and setup */
c = composite_create(p, p->conn->event_ctx);
if (c == NULL) return NULL;
s = talloc_zero(c, struct sec_conn_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
s->pipe = p;
s->binding = b;
/* initialise second dcerpc pipe based on primary pipe's event context */
s->pipe2 = dcerpc_pipe_init(c, s->pipe->conn->event_ctx);
if (composite_nomem(s->pipe2, c)) return c;
/* open second dcerpc pipe using the same transport as for primary pipe */
switch (s->pipe->conn->transport.transport) {
case NCACN_NP:
/* get smb tree of primary dcerpc pipe opened on smb */
s->tree = dcerpc_smb_tree(s->pipe->conn);
if (!s->tree) {
composite_error(c, NT_STATUS_INVALID_PARAMETER);
return c;
}
pipe_smb_req = dcerpc_pipe_open_smb_send(s->pipe2->conn, s->tree,
s->binding->endpoint);
composite_continue(c, pipe_smb_req, continue_open_smb, c);
return c;
case NCACN_IP_TCP:
pipe_tcp_req = dcerpc_pipe_open_tcp_send(s->pipe2->conn,
s->binding->host,
atoi(s->binding->endpoint));
composite_continue(c, pipe_tcp_req, continue_open_tcp, c);
return c;
case NCALRPC:
pipe_ncalrpc_req = dcerpc_pipe_open_pipe_send(s->pipe2->conn,
s->binding->endpoint);
composite_continue(c, pipe_ncalrpc_req, continue_open_pipe, c);
return c;
default:
/* looks like a transport we don't support */
composite_error(c, NT_STATUS_NOT_SUPPORTED);
}
return c;
}
/*
Stage 2 of secondary_connection: Receive result of pipe open request on smb
*/
static void continue_open_smb(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
c->status = dcerpc_pipe_open_smb_recv(ctx);
if (!composite_is_ok(c)) return;
continue_pipe_open(c);
}
/*
Stage 2 of secondary_connection: Receive result of pipe open request on tcp/ip
*/
static void continue_open_tcp(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
c->status = dcerpc_pipe_open_tcp_recv(ctx);
if (!composite_is_ok(c)) return;
continue_pipe_open(c);
}
/*
Stage 2 of secondary_connection: Receive result of pipe open request on ncalrpc
*/
static void continue_open_pipe(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type(ctx->async.private_data,
struct composite_context);
c->status = dcerpc_pipe_open_pipe_recv(ctx);
if (!composite_is_ok(c)) return;
continue_pipe_open(c);
}
/*
Stage 3 of secondary_connection: Get binding data and flags from primary pipe
and say if we're done ok.
*/
static void continue_pipe_open(struct composite_context *c)
{
struct sec_conn_state *s;
s = talloc_get_type(c->private_data, struct sec_conn_state);
s->pipe2->conn->flags = s->pipe->conn->flags;
s->pipe2->binding = s->binding;
if (!talloc_reference(s->pipe2, s->binding)) {
composite_error(c, NT_STATUS_NO_MEMORY);
return;
}
composite_done(c);
}
/*
Receive result of secondary rpc connection request and return
second dcerpc pipe.
*/
NTSTATUS dcerpc_secondary_connection_recv(struct composite_context *c,
struct dcerpc_pipe **p2)
{
NTSTATUS status = composite_wait(c);
struct sec_conn_state *s;
s = talloc_get_type(c->private_data, struct sec_conn_state);
if (NT_STATUS_IS_OK(status)) {
*p2 = talloc_steal(s->pipe, s->pipe2);
}
talloc_free(c);
return status;
}
/*
Create a secondary dcerpc connection from a primary connection
- sync version
If the primary is a SMB connection then the secondary connection
will be on the same SMB connection, but using a new fnum
*/
NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p,
struct dcerpc_pipe **p2,
struct dcerpc_binding *b)
{
struct composite_context *c;
c = dcerpc_secondary_connection_send(p, b);
return dcerpc_secondary_connection_recv(c, p2);
}
|