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
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
|
/*
Unix SMB/CIFS implementation.
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 3 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, see <http://www.gnu.org/licenses/>.
*/
/*
a composite function for domain handling on samr and lsa pipes
*/
#include "includes.h"
#include "libcli/composite/composite.h"
#include "libnet/libnet.h"
#include "librpc/gen_ndr/ndr_samr_c.h"
#include "librpc/gen_ndr/ndr_lsa_c.h"
struct domain_open_samr_state {
struct libnet_context *ctx;
struct dcerpc_pipe *pipe;
struct libnet_RpcConnect rpcconn;
struct samr_Connect connect;
struct samr_LookupDomain lookup;
struct samr_OpenDomain open;
struct samr_Close close;
struct lsa_String domain_name;
uint32_t access_mask;
struct policy_handle connect_handle;
struct policy_handle domain_handle;
/* information about the progress */
void (*monitor_fn)(struct monitor_msg*);
};
static void continue_domain_open_close(struct rpc_request *req);
static void continue_domain_open_connect(struct rpc_request *req);
static void continue_domain_open_lookup(struct rpc_request *req);
static void continue_domain_open_open(struct rpc_request *req);
/**
* Stage 0.5 (optional): Connect to samr rpc pipe
*/
static void continue_domain_open_rpc_connect(struct composite_context *ctx)
{
struct composite_context *c;
struct domain_open_samr_state *s;
struct rpc_request *conn_req;
c = talloc_get_type(ctx->async.private_data, struct composite_context);
s = talloc_get_type(c->private_data, struct domain_open_samr_state);
c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->rpcconn);
if (!composite_is_ok(c)) return;
s->pipe = s->rpcconn.out.dcerpc_pipe;
/* preparing parameters for samr_Connect rpc call */
s->connect.in.system_name = 0;
s->connect.in.access_mask = s->access_mask;
s->connect.out.connect_handle = &s->connect_handle;
/* send request */
conn_req = dcerpc_samr_Connect_send(s->pipe, c, &s->connect);
if (composite_nomem(conn_req, c)) return;
/* callback handler */
composite_continue_rpc(c, conn_req, continue_domain_open_connect, c);
}
/**
* Stage 0.5 (optional): Close existing (in libnet context) domain
* handle
*/
static void continue_domain_open_close(struct rpc_request *req)
{
struct composite_context *c;
struct domain_open_samr_state *s;
struct rpc_request *conn_req;
c = talloc_get_type(req->async.private_data, struct composite_context);
s = talloc_get_type(c->private_data, struct domain_open_samr_state);
/* receive samr_Close reply */
c->status = dcerpc_ndr_request_recv(req);
if (!composite_is_ok(c)) return;
if (s->monitor_fn) {
struct monitor_msg msg;
msg.type = mon_SamrClose;
msg.data = NULL;
msg.data_size = 0;
s->monitor_fn(&msg);
}
/* reset domain handle and associated data in libnet_context */
s->ctx->samr.name = NULL;
s->ctx->samr.access_mask = 0;
ZERO_STRUCT(s->ctx->samr.handle);
/* preparing parameters for samr_Connect rpc call */
s->connect.in.system_name = 0;
s->connect.in.access_mask = s->access_mask;
s->connect.out.connect_handle = &s->connect_handle;
/* send request */
conn_req = dcerpc_samr_Connect_send(s->pipe, c, &s->connect);
if (composite_nomem(conn_req, c)) return;
/* callback handler */
composite_continue_rpc(c, conn_req, continue_domain_open_connect, c);
}
/**
* Stage 1: Connect to SAM server.
*/
static void continue_domain_open_connect(struct rpc_request *req)
{
struct composite_context *c;
struct domain_open_samr_state *s;
struct rpc_request *lookup_req;
struct samr_LookupDomain *r;
c = talloc_get_type(req->async.private_data, struct composite_context);
s = talloc_get_type(c->private_data, struct domain_open_samr_state);
/* receive samr_Connect reply */
c->status = dcerpc_ndr_request_recv(req);
if (!composite_is_ok(c)) return;
if (s->monitor_fn) {
struct monitor_msg msg;
msg.type = mon_SamrConnect;
msg.data = NULL;
msg.data_size = 0;
s->monitor_fn(&msg);
}
r = &s->lookup;
/* prepare for samr_LookupDomain call */
r->in.connect_handle = &s->connect_handle;
r->in.domain_name = &s->domain_name;
lookup_req = dcerpc_samr_LookupDomain_send(s->pipe, c, r);
if (composite_nomem(lookup_req, c)) return;
composite_continue_rpc(c, lookup_req, continue_domain_open_lookup, c);
}
/**
* Stage 2: Lookup domain by name.
*/
static void continue_domain_open_lookup(struct rpc_request *req)
{
struct composite_context *c;
struct domain_open_samr_state *s;
struct rpc_request *opendom_req;
struct samr_OpenDomain *r;
c = talloc_get_type(req->async.private_data, struct composite_context);
s = talloc_get_type(c->private_data, struct domain_open_samr_state);
/* receive samr_LookupDomain reply */
c->status = dcerpc_ndr_request_recv(req);
if (s->monitor_fn) {
struct monitor_msg msg;
struct msg_rpc_lookup_domain data;
data.domain_name = s->domain_name.string;
msg.type = mon_SamrLookupDomain;
msg.data = (void*)&data;
msg.data_size = sizeof(data);
s->monitor_fn(&msg);
}
r = &s->open;
/* check the rpc layer status */
if (!composite_is_ok(c));
/* check the rpc call itself status */
if (!NT_STATUS_IS_OK(s->lookup.out.result)) {
composite_error(c, s->lookup.out.result);
return;
}
/* prepare for samr_OpenDomain call */
r->in.connect_handle = &s->connect_handle;
r->in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
r->in.sid = s->lookup.out.sid;
r->out.domain_handle = &s->domain_handle;
opendom_req = dcerpc_samr_OpenDomain_send(s->pipe, c, r);
if (composite_nomem(opendom_req, c)) return;
composite_continue_rpc(c, opendom_req, continue_domain_open_open, c);
}
/*
* Stage 3: Open domain.
*/
static void continue_domain_open_open(struct rpc_request *req)
{
struct composite_context *c;
struct domain_open_samr_state *s;
c = talloc_get_type(req->async.private_data, struct composite_context);
s = talloc_get_type(c->private_data, struct domain_open_samr_state);
/* receive samr_OpenDomain reply */
c->status = dcerpc_ndr_request_recv(req);
if (!composite_is_ok(c)) return;
if (s->monitor_fn) {
struct monitor_msg msg;
msg.type = mon_SamrOpenDomain;
msg.data = NULL;
msg.data_size = 0;
s->monitor_fn(&msg);
}
composite_done(c);
}
/**
* Sends asynchronous DomainOpenSamr request
*
* @param ctx initialised libnet context
* @param io arguments and results of the call
* @param monitor pointer to monitor function that is passed monitor message
*/
struct composite_context *libnet_DomainOpenSamr_send(struct libnet_context *ctx,
struct libnet_DomainOpen *io,
void (*monitor)(struct monitor_msg*))
{
struct composite_context *c;
struct domain_open_samr_state *s;
struct composite_context *rpcconn_req;
struct rpc_request *close_req, *conn_req;
c = composite_create(ctx, ctx->event_ctx);
if (c == NULL) return NULL;
s = talloc_zero(c, struct domain_open_samr_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
s->monitor_fn = monitor;
s->ctx = ctx;
s->pipe = ctx->samr.pipe;
s->access_mask = io->in.access_mask;
s->domain_name.string = talloc_strdup(c, io->in.domain_name);
/* check, if there's samr pipe opened already, before opening a domain */
if (ctx->samr.pipe == NULL) {
/* attempting to connect a domain controller */
s->rpcconn.level = LIBNET_RPC_CONNECT_DC;
s->rpcconn.in.name = io->in.domain_name;
s->rpcconn.in.dcerpc_iface = &ndr_table_samr;
/* send rpc pipe connect request */
rpcconn_req = libnet_RpcConnect_send(ctx, c, &s->rpcconn, s->monitor_fn);
if (composite_nomem(rpcconn_req, c)) return c;
composite_continue(c, rpcconn_req, continue_domain_open_rpc_connect, c);
return c;
}
/* libnet context's domain handle is not empty, so check out what
was opened first, before doing anything */
if (!policy_handle_empty(&ctx->samr.handle)) {
if (strequal(ctx->samr.name, io->in.domain_name) &&
ctx->samr.access_mask == io->in.access_mask) {
/* this domain is already opened */
composite_done(c);
return c;
} else {
/* another domain or access rights have been
requested - close the existing handle first */
s->close.in.handle = &ctx->samr.handle;
/* send request to close domain handle */
close_req = dcerpc_samr_Close_send(s->pipe, c, &s->close);
if (composite_nomem(close_req, c)) return c;
/* callback handler */
composite_continue_rpc(c, close_req, continue_domain_open_close, c);
return c;
}
}
/* preparing parameters for samr_Connect rpc call */
s->connect.in.system_name = 0;
s->connect.in.access_mask = s->access_mask;
s->connect.out.connect_handle = &s->connect_handle;
/* send request */
conn_req = dcerpc_samr_Connect_send(s->pipe, c, &s->connect);
if (composite_nomem(conn_req, c)) return c;
/* callback handler */
composite_continue_rpc(c, conn_req, continue_domain_open_connect, c);
return c;
}
/**
* Waits for and receives result of asynchronous DomainOpenSamr call
*
* @param c composite context returned by asynchronous DomainOpen call
* @param ctx initialised libnet context
* @param mem_ctx memory context of the call
* @param io pointer to results (and arguments) of the call
* @return nt status code of execution
*/
NTSTATUS libnet_DomainOpenSamr_recv(struct composite_context *c, struct libnet_context *ctx,
TALLOC_CTX *mem_ctx, struct libnet_DomainOpen *io)
{
NTSTATUS status;
struct domain_open_samr_state *s;
/* wait for results of sending request */
status = composite_wait(c);
if (NT_STATUS_IS_OK(status) && io) {
s = talloc_get_type(c->private_data, struct domain_open_samr_state);
io->out.domain_handle = s->domain_handle;
/* store the resulting handle and related data for use by other
libnet functions */
ctx->samr.connect_handle = s->connect_handle;
ctx->samr.handle = s->domain_handle;
ctx->samr.sid = talloc_steal(ctx, s->lookup.out.sid);
ctx->samr.name = talloc_steal(ctx, s->domain_name.string);
ctx->samr.access_mask = s->access_mask;
}
talloc_free(c);
return status;
}
struct domain_open_lsa_state {
const char *name;
uint32_t access_mask;
struct libnet_context *ctx;
struct libnet_RpcConnect rpcconn;
struct lsa_OpenPolicy2 openpol;
struct policy_handle handle;
struct dcerpc_pipe *pipe;
/* information about the progress */
void (*monitor_fn)(struct monitor_msg*);
};
static void continue_rpc_connect_lsa(struct composite_context *ctx);
static void continue_lsa_policy_open(struct rpc_request *req);
/**
* Sends asynchronous DomainOpenLsa request
*
* @param ctx initialised libnet context
* @param io arguments and results of the call
* @param monitor pointer to monitor function that is passed monitor message
*/
struct composite_context* libnet_DomainOpenLsa_send(struct libnet_context *ctx,
struct libnet_DomainOpen *io,
void (*monitor)(struct monitor_msg*))
{
struct composite_context *c;
struct domain_open_lsa_state *s;
struct composite_context *rpcconn_req;
struct rpc_request *openpol_req;
struct lsa_QosInfo *qos;
/* create composite context and state */
c = composite_create(ctx, ctx->event_ctx);
if (c == NULL) return c;
s = talloc_zero(c, struct domain_open_lsa_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
/* store arguments in the state structure */
s->name = talloc_strdup(c, io->in.domain_name);
s->access_mask = io->in.access_mask;
s->ctx = ctx;
/* check, if there's lsa pipe opened already, before opening a handle */
if (ctx->lsa.pipe == NULL) {
/* attempting to connect a domain controller */
s->rpcconn.level = LIBNET_RPC_CONNECT_DC;
s->rpcconn.in.name = talloc_strdup(c, io->in.domain_name);
s->rpcconn.in.dcerpc_iface = &ndr_table_lsarpc;
/* send rpc pipe connect request */
rpcconn_req = libnet_RpcConnect_send(ctx, c, &s->rpcconn, s->monitor_fn);
if (composite_nomem(rpcconn_req, c)) return c;
composite_continue(c, rpcconn_req, continue_rpc_connect_lsa, c);
return c;
}
s->pipe = ctx->lsa.pipe;
/* preparing parameters for lsa_OpenPolicy2 rpc call */
s->openpol.in.system_name = s->name;
s->openpol.in.access_mask = s->access_mask;
s->openpol.in.attr = talloc_zero(c, struct lsa_ObjectAttribute);
qos = talloc_zero(c, struct lsa_QosInfo);
qos->len = 0;
qos->impersonation_level = 2;
qos->context_mode = 1;
qos->effective_only = 0;
s->openpol.in.attr->sec_qos = qos;
s->openpol.out.handle = &s->handle;
/* send rpc request */
openpol_req = dcerpc_lsa_OpenPolicy2_send(s->pipe, c, &s->openpol);
if (composite_nomem(openpol_req, c)) return c;
composite_continue_rpc(c, openpol_req, continue_lsa_policy_open, c);
return c;
}
/*
Stage 0.5 (optional): Rpc pipe connected, send lsa open policy request
*/
static void continue_rpc_connect_lsa(struct composite_context *ctx)
{
struct composite_context *c;
struct domain_open_lsa_state *s;
struct lsa_QosInfo *qos;
struct rpc_request *openpol_req;
c = talloc_get_type(ctx->async.private_data, struct composite_context);
s = talloc_get_type(c->private_data, struct domain_open_lsa_state);
/* receive rpc connection */
c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->rpcconn);
if (!composite_is_ok(c)) return;
/* RpcConnect function leaves the pipe in libnet context,
so get it from there */
s->pipe = s->ctx->lsa.pipe;
/* prepare lsa_OpenPolicy2 call */
s->openpol.in.system_name = s->name;
s->openpol.in.access_mask = s->access_mask;
s->openpol.in.attr = talloc_zero(c, struct lsa_ObjectAttribute);
qos = talloc_zero(c, struct lsa_QosInfo);
qos->len = 0;
qos->impersonation_level = 2;
qos->context_mode = 1;
qos->effective_only = 0;
s->openpol.in.attr->sec_qos = qos;
s->openpol.out.handle = &s->handle;
/* send rpc request */
openpol_req = dcerpc_lsa_OpenPolicy2_send(s->pipe, c, &s->openpol);
if (composite_nomem(openpol_req, c)) return;
composite_continue_rpc(c, openpol_req, continue_lsa_policy_open, c);
}
/*
Stage 1: Lsa policy opened - we're done, if successfully
*/
static void continue_lsa_policy_open(struct rpc_request *req)
{
struct composite_context *c;
struct domain_open_lsa_state *s;
c = talloc_get_type(req->async.private_data, struct composite_context);
s = talloc_get_type(c->private_data, struct domain_open_lsa_state);
c->status = dcerpc_ndr_request_recv(req);
if (!composite_is_ok(c)) return;
if (s->monitor_fn) {
struct monitor_msg msg;
msg.type = mon_LsaOpenPolicy;
msg.data = NULL;
msg.data_size = 0;
s->monitor_fn(&msg);
}
composite_done(c);
}
/**
* Receives result of asynchronous DomainOpenLsa call
*
* @param c composite context returned by asynchronous DomainOpenLsa call
* @param ctx initialised libnet context
* @param mem_ctx memory context of the call
* @param io pointer to results (and arguments) of the call
* @return nt status code of execution
*/
NTSTATUS libnet_DomainOpenLsa_recv(struct composite_context *c, struct libnet_context *ctx,
TALLOC_CTX *mem_ctx, struct libnet_DomainOpen *io)
{
NTSTATUS status;
struct domain_open_lsa_state *s;
status = composite_wait(c);
if (NT_STATUS_IS_OK(status) && io) {
/* everything went fine - get the results and
return the error string */
s = talloc_get_type(c->private_data, struct domain_open_lsa_state);
io->out.domain_handle = s->handle;
ctx->lsa.handle = s->handle;
ctx->lsa.name = talloc_steal(ctx, s->name);
ctx->lsa.access_mask = s->access_mask;
io->out.error_string = talloc_strdup(mem_ctx, "Success");
} else if (!NT_STATUS_IS_OK(status)) {
/* there was an error, so provide nt status code description */
io->out.error_string = talloc_asprintf(mem_ctx,
"Failed to open domain: %s",
nt_errstr(status));
}
talloc_free(c);
return status;
}
/**
* Sends a request to open a domain in desired service
*
* @param ctx initalised libnet context
* @param io arguments and results of the call
* @param monitor pointer to monitor function that is passed monitor message
*/
struct composite_context* libnet_DomainOpen_send(struct libnet_context *ctx,
struct libnet_DomainOpen *io,
void (*monitor)(struct monitor_msg*))
{
struct composite_context *c;
switch (io->in.type) {
case DOMAIN_LSA:
/* reques to open a policy handle on \pipe\lsarpc */
c = libnet_DomainOpenLsa_send(ctx, io, monitor);
break;
case DOMAIN_SAMR:
default:
/* request to open a domain policy handle on \pipe\samr */
c = libnet_DomainOpenSamr_send(ctx, io, monitor);
break;
}
return c;
}
/**
* Receive result of domain open request
*
* @param c composite context returned by DomainOpen_send function
* @param ctx initialised libnet context
* @param mem_ctx memory context of the call
* @param io results and arguments of the call
*/
NTSTATUS libnet_DomainOpen_recv(struct composite_context *c, struct libnet_context *ctx,
TALLOC_CTX *mem_ctx, struct libnet_DomainOpen *io)
{
NTSTATUS status;
switch (io->in.type) {
case DOMAIN_LSA:
status = libnet_DomainOpenLsa_recv(c, ctx, mem_ctx, io);
break;
case DOMAIN_SAMR:
default:
status = libnet_DomainOpenSamr_recv(c, ctx, mem_ctx, io);
break;
}
return status;
}
/**
* Synchronous version of DomainOpen call
*
* @param ctx initialised libnet context
* @param mem_ctx memory context for the call
* @param io arguments and results of the call
* @return nt status code of execution
*/
NTSTATUS libnet_DomainOpen(struct libnet_context *ctx,
TALLOC_CTX *mem_ctx,
struct libnet_DomainOpen *io)
{
struct composite_context *c = libnet_DomainOpen_send(ctx, io, NULL);
return libnet_DomainOpen_recv(c, ctx, mem_ctx, io);
}
struct domain_close_lsa_state {
struct dcerpc_pipe *pipe;
struct lsa_Close close;
struct policy_handle handle;
void (*monitor_fn)(struct monitor_msg*);
};
static void continue_lsa_close(struct rpc_request *req);
struct composite_context* libnet_DomainCloseLsa_send(struct libnet_context *ctx,
struct libnet_DomainClose *io,
void (*monitor)(struct monitor_msg*))
{
struct composite_context *c;
struct domain_close_lsa_state *s;
struct rpc_request *close_req;
/* composite context and state structure allocation */
c = composite_create(ctx, ctx->event_ctx);
if (c == NULL) return c;
s = talloc_zero(c, struct domain_close_lsa_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
s->monitor_fn = monitor;
/* TODO: check if lsa pipe pointer is non-null */
if (!strequal(ctx->lsa.name, io->in.domain_name)) {
composite_error(c, NT_STATUS_INVALID_PARAMETER);
return c;
}
/* get opened lsarpc pipe pointer */
s->pipe = ctx->lsa.pipe;
/* prepare close handle call arguments */
s->close.in.handle = &ctx->lsa.handle;
s->close.out.handle = &s->handle;
/* send the request */
close_req = dcerpc_lsa_Close_send(s->pipe, c, &s->close);
if (composite_nomem(close_req, c)) return c;
composite_continue_rpc(c, close_req, continue_lsa_close, c);
return c;
}
/*
Stage 1: Receive result of lsa close call
*/
static void continue_lsa_close(struct rpc_request *req)
{
struct composite_context *c;
struct domain_close_lsa_state *s;
c = talloc_get_type(req->async.private_data, struct composite_context);
s = talloc_get_type(c->private_data, struct domain_close_lsa_state);
c->status = dcerpc_ndr_request_recv(req);
if (!composite_is_ok(c)) return;
if (s->monitor_fn) {
struct monitor_msg msg;
msg.type = mon_LsaClose;
msg.data = NULL;
msg.data_size = 0;
s->monitor_fn(&msg);
}
composite_done(c);
}
NTSTATUS libnet_DomainCloseLsa_recv(struct composite_context *c, struct libnet_context *ctx,
TALLOC_CTX *mem_ctx, struct libnet_DomainClose *io)
{
NTSTATUS status;
status = composite_wait(c);
if (NT_STATUS_IS_OK(status) && io) {
/* policy handle closed successfully */
ctx->lsa.name = NULL;
ZERO_STRUCT(ctx->lsa.handle);
io->out.error_string = talloc_asprintf(mem_ctx, "Success");
} else if (!NT_STATUS_IS_OK(status)) {
/* there was an error, so return description of the status code */
io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
}
talloc_free(c);
return status;
}
struct domain_close_samr_state {
struct samr_Close close;
struct policy_handle handle;
void (*monitor_fn)(struct monitor_msg*);
};
static void continue_samr_close(struct rpc_request *req);
struct composite_context* libnet_DomainCloseSamr_send(struct libnet_context *ctx,
struct libnet_DomainClose *io,
void (*monitor)(struct monitor_msg*))
{
struct composite_context *c;
struct domain_close_samr_state *s;
struct rpc_request *close_req;
/* composite context and state structure allocation */
c = composite_create(ctx, ctx->event_ctx);
if (c == NULL) return c;
s = talloc_zero(c, struct domain_close_samr_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
s->monitor_fn = monitor;
/* TODO: check if samr pipe pointer is non-null */
if (!strequal(ctx->samr.name, io->in.domain_name)) {
composite_error(c, NT_STATUS_INVALID_PARAMETER);
return c;
}
/* prepare close domain handle call arguments */
ZERO_STRUCT(s->close);
s->close.in.handle = &ctx->samr.handle;
s->close.out.handle = &s->handle;
/* send the request */
close_req = dcerpc_samr_Close_send(ctx->samr.pipe, ctx, &s->close);
if (composite_nomem(close_req, c)) return c;
composite_continue_rpc(c, close_req, continue_samr_close, c);
return c;
}
/*
Stage 1: Receive result of samr close call
*/
static void continue_samr_close(struct rpc_request *req)
{
struct composite_context *c;
struct domain_close_samr_state *s;
c = talloc_get_type(req->async.private_data, struct composite_context);
s = talloc_get_type(c->private_data, struct domain_close_samr_state);
c->status = dcerpc_ndr_request_recv(req);
if (!composite_is_ok(c)) return;
if (s->monitor_fn) {
struct monitor_msg msg;
msg.type = mon_SamrClose;
msg.data = NULL;
msg.data_size = 0;
s->monitor_fn(&msg);
}
composite_done(c);
}
NTSTATUS libnet_DomainCloseSamr_recv(struct composite_context *c, struct libnet_context *ctx,
TALLOC_CTX *mem_ctx, struct libnet_DomainClose *io)
{
NTSTATUS status;
status = composite_wait(c);
if (NT_STATUS_IS_OK(status) && io) {
/* domain policy handle closed successfully */
ZERO_STRUCT(ctx->samr.handle);
talloc_free(discard_const_p(char, ctx->samr.name));
talloc_free(ctx->samr.sid);
ctx->samr.name = NULL;
ctx->samr.sid = NULL;
io->out.error_string = talloc_asprintf(mem_ctx, "Success");
} else if (!NT_STATUS_IS_OK(status)) {
/* there was an error, so return description of the status code */
io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
}
talloc_free(c);
return status;
}
struct composite_context* libnet_DomainClose_send(struct libnet_context *ctx,
struct libnet_DomainClose *io,
void (*monitor)(struct monitor_msg*))
{
struct composite_context *c;
switch (io->in.type) {
case DOMAIN_LSA:
/* request to close policy handle on \pipe\lsarpc */
c = libnet_DomainCloseLsa_send(ctx, io, monitor);
break;
case DOMAIN_SAMR:
default:
/* request to close domain policy handle on \pipe\samr */
c = libnet_DomainCloseSamr_send(ctx, io, monitor);
break;
}
return c;
}
NTSTATUS libnet_DomainClose_recv(struct composite_context *c, struct libnet_context *ctx,
TALLOC_CTX *mem_ctx, struct libnet_DomainClose *io)
{
NTSTATUS status;
switch (io->in.type) {
case DOMAIN_LSA:
/* receive result of closing lsa policy handle */
status = libnet_DomainCloseLsa_recv(c, ctx, mem_ctx, io);
break;
case DOMAIN_SAMR:
default:
/* receive result of closing samr domain policy handle */
status = libnet_DomainCloseSamr_recv(c, ctx, mem_ctx, io);
break;
}
return status;
}
NTSTATUS libnet_DomainClose(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
struct libnet_DomainClose *io)
{
struct composite_context *c;
c = libnet_DomainClose_send(ctx, io, NULL);
return libnet_DomainClose_recv(c, ctx, mem_ctx, io);
}
struct domain_list_state {
struct libnet_context *ctx;
struct libnet_RpcConnect rpcconn;
struct samr_Connect samrconn;
struct samr_EnumDomains enumdom;
struct samr_Close samrclose;
const char *hostname;
struct policy_handle connect_handle;
int buf_size;
struct domainlist *domains;
uint32_t resume_handle;
uint32_t count;
void (*monitor_fn)(struct monitor_msg*);
};
static void continue_rpc_connect(struct composite_context *c);
static void continue_samr_connect(struct rpc_request *c);
static void continue_samr_enum_domains(struct rpc_request *req);
static void continue_samr_close_handle(struct rpc_request *req);
static struct domainlist* get_domain_list(TALLOC_CTX *mem_ctx, struct domain_list_state *s);
/*
Stage 1: Receive connected rpc pipe and send connection
request to SAMR service
*/
static void continue_rpc_connect(struct composite_context *ctx)
{
struct composite_context *c;
struct domain_list_state *s;
struct rpc_request *samrconn_req;
c = talloc_get_type(ctx->async.private_data, struct composite_context);
s = talloc_get_type(c->private_data, struct domain_list_state);
c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->rpcconn);
if (!composite_is_ok(c)) return;
s->samrconn.in.system_name = 0;
s->samrconn.in.access_mask = SEC_GENERIC_READ; /* should be enough */
s->samrconn.out.connect_handle = &s->connect_handle;
samrconn_req = dcerpc_samr_Connect_send(s->ctx->samr.pipe, c, &s->samrconn);
if (composite_nomem(samrconn_req, c)) return;
composite_continue_rpc(c, samrconn_req, continue_samr_connect, c);
}
/*
Stage 2: Receive policy handle to the connected SAMR service and issue
a request to enumerate domain databases available
*/
static void continue_samr_connect(struct rpc_request *req)
{
struct composite_context *c;
struct domain_list_state *s;
struct rpc_request *enumdom_req;
c = talloc_get_type(req->async.private_data, struct composite_context);
s = talloc_get_type(c->private_data, struct domain_list_state);
c->status = dcerpc_ndr_request_recv(req);
if (!composite_is_ok(c)) return;
if (s->monitor_fn) {
struct monitor_msg msg;
msg.type = mon_SamrConnect;
msg.data = NULL;
msg.data_size = 0;
s->monitor_fn(&msg);
}
s->enumdom.in.connect_handle = &s->connect_handle;
s->enumdom.in.resume_handle = &s->resume_handle;
s->enumdom.in.buf_size = s->buf_size;
s->enumdom.out.resume_handle = &s->resume_handle;
enumdom_req = dcerpc_samr_EnumDomains_send(s->ctx->samr.pipe, c, &s->enumdom);
if (composite_nomem(enumdom_req, c)) return;
composite_continue_rpc(c, enumdom_req, continue_samr_enum_domains, c);
}
/*
Stage 3: Receive domain names available and repeat the request
enumeration is not complete yet. Close samr connection handle
upon completion.
*/
static void continue_samr_enum_domains(struct rpc_request *req)
{
struct composite_context *c;
struct domain_list_state *s;
struct rpc_request *enumdom_req;
struct rpc_request *samrclose_req;
c = talloc_get_type(req->async.private_data, struct composite_context);
s = talloc_get_type(c->private_data, struct domain_list_state);
c->status = dcerpc_ndr_request_recv(req);
if (!composite_is_ok(c)) return;
if (s->monitor_fn) {
struct monitor_msg msg;
msg.type = mon_SamrEnumDomains;
msg.data = NULL;
msg.data_size = 0;
s->monitor_fn(&msg);
}
if (NT_STATUS_IS_OK(s->enumdom.out.result)) {
s->domains = get_domain_list(c, s);
} else if (NT_STATUS_EQUAL(s->enumdom.out.result, STATUS_MORE_ENTRIES)) {
s->domains = get_domain_list(c, s);
/* prepare next round of enumeration */
s->enumdom.in.connect_handle = &s->connect_handle;
s->enumdom.in.resume_handle = &s->resume_handle;
s->enumdom.in.buf_size = s->ctx->samr.buf_size;
s->enumdom.out.resume_handle = &s->resume_handle;
/* send the request */
enumdom_req = dcerpc_samr_EnumDomains_send(s->ctx->samr.pipe, c, &s->enumdom);
if (composite_nomem(enumdom_req, c)) return;
composite_continue_rpc(c, enumdom_req, continue_samr_enum_domains, c);
} else {
composite_error(c, s->enumdom.out.result);
return;
}
/* close samr connection handle */
s->samrclose.in.handle = &s->connect_handle;
s->samrclose.out.handle = &s->connect_handle;
/* send the request */
samrclose_req = dcerpc_samr_Close_send(s->ctx->samr.pipe, c, &s->samrclose);
if (composite_nomem(samrclose_req, c)) return;
composite_continue_rpc(c, samrclose_req, continue_samr_close_handle, c);
}
/*
Stage 4: Receive result of closing samr connection handle.
*/
static void continue_samr_close_handle(struct rpc_request *req)
{
struct composite_context *c;
struct domain_list_state *s;
c = talloc_get_type(req->async.private_data, struct composite_context);
s = talloc_get_type(c->private_data, struct domain_list_state);
c->status = dcerpc_ndr_request_recv(req);
if (!composite_is_ok(c)) return;
if (s->monitor_fn) {
struct monitor_msg msg;
msg.type = mon_SamrClose;
msg.data = NULL;
msg.data_size = 0;
s->monitor_fn(&msg);
}
/* did everything go fine ? */
if (!NT_STATUS_IS_OK(s->samrclose.out.result)) {
composite_error(c, s->samrclose.out.result);
}
composite_done(c);
}
/*
Utility function to copy domain names from result of samr_EnumDomains call
*/
static struct domainlist* get_domain_list(TALLOC_CTX *mem_ctx, struct domain_list_state *s)
{
int i;
if (mem_ctx == NULL || s == NULL) return NULL;
/* prepare domains array */
if (s->domains == NULL) {
s->domains = talloc_array(mem_ctx, struct domainlist,
s->enumdom.out.num_entries);
} else {
s->domains = talloc_realloc(mem_ctx, s->domains, struct domainlist,
s->count + s->enumdom.out.num_entries);
}
/* copy domain names returned from samr_EnumDomains call */
for (i = s->count; i < s->count + s->enumdom.out.num_entries; i++)
{
struct lsa_String *domain_name = &s->enumdom.out.sam->entries[i - s->count].name;
/* strdup name as a child of allocated array to make it follow the array
in case of talloc_steal or talloc_free */
s->domains[i].name = talloc_strdup(s->domains, domain_name->string);
s->domains[i].sid = NULL; /* this is to be filled out later */
}
/* number of entries returned (domains enumerated) */
s->count += s->enumdom.out.num_entries;
return s->domains;
}
/**
* Sends a request to list domains on given host
*
* @param ctx initalised libnet context
* @param mem_ctx memory context
* @param io arguments and results of the call
* @param monitor pointer to monitor function that is passed monitor messages
*/
struct composite_context* libnet_DomainList_send(struct libnet_context *ctx,
TALLOC_CTX *mem_ctx,
struct libnet_DomainList *io,
void (*monitor)(struct monitor_msg*))
{
struct composite_context *c;
struct domain_list_state *s;
struct composite_context *rpcconn_req;
struct rpc_request *samrconn_req;
/* composite context and state structure allocation */
c = composite_create(ctx, ctx->event_ctx);
if (c == NULL) return c;
s = talloc_zero(c, struct domain_list_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
s->monitor_fn = monitor;
s->ctx = ctx;
s->hostname = talloc_strdup(c, io->in.hostname);
if (composite_nomem(s->hostname, c)) return c;
/* check whether samr pipe has already been opened */
if (ctx->samr.pipe == NULL) {
/* prepare rpc connect call */
s->rpcconn.level = LIBNET_RPC_CONNECT_SERVER;
s->rpcconn.in.name = s->hostname;
s->rpcconn.in.dcerpc_iface = &ndr_table_samr;
rpcconn_req = libnet_RpcConnect_send(ctx, c, &s->rpcconn, s->monitor_fn);
if (composite_nomem(rpcconn_req, c)) return c;
composite_continue(c, rpcconn_req, continue_rpc_connect, c);
} else {
/* prepare samr_Connect call */
s->samrconn.in.system_name = 0;
s->samrconn.in.access_mask = SEC_GENERIC_READ;
s->samrconn.out.connect_handle = &s->connect_handle;
samrconn_req = dcerpc_samr_Connect_send(s->ctx->samr.pipe, c, &s->samrconn);
if (composite_nomem(samrconn_req, c)) return c;
composite_continue_rpc(c, samrconn_req, continue_samr_connect, c);
}
return c;
}
/**
* Receive result of domain list request
*
* @param c composite context returned by DomainList_send function
* @param ctx initialised libnet context
* @param mem_ctx memory context of the call
* @param io results and arguments of the call
*/
NTSTATUS libnet_DomainList_recv(struct composite_context *c, struct libnet_context *ctx,
TALLOC_CTX *mem_ctx, struct libnet_DomainList *io)
{
NTSTATUS status;
struct domain_list_state *s;
status = composite_wait(c);
s = talloc_get_type(c->private_data, struct domain_list_state);
if (NT_STATUS_IS_OK(status) && ctx && mem_ctx && io) {
/* fetch the results to be returned by io structure */
io->out.count = s->count;
io->out.domains = talloc_steal(mem_ctx, s->domains);
io->out.error_string = talloc_asprintf(mem_ctx, "Success");
} else if (!NT_STATUS_IS_OK(status)) {
/* there was an error, so return description of the status code */
io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
}
talloc_free(c);
return status;
}
/**
* Synchronous version of DomainList call
*
* @param ctx initialised libnet context
* @param mem_ctx memory context for the call
* @param io arguments and results of the call
* @return nt status code of execution
*/
NTSTATUS libnet_DomainList(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
struct libnet_DomainList *io)
{
struct composite_context *c;
c = libnet_DomainList_send(ctx, mem_ctx, io, NULL);
return libnet_DomainList_recv(c, ctx, mem_ctx, io);
}
|