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
|
/*
Unix SMB/CIFS implementation.
dcerpc over standard sockets transport
Copyright (C) Andrew Tridgell 2003
Copyright (C) Jelmer Vernooij 2004
Copyright (C) Rafal Szczesniak 2006
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/>.
*/
#include "includes.h"
#include "system/filesys.h"
#include "lib/events/events.h"
#include "lib/socket/socket.h"
#include "lib/tsocket/tsocket.h"
#include "libcli/composite/composite.h"
#include "librpc/rpc/dcerpc.h"
#include "librpc/rpc/dcerpc_proto.h"
#include "libcli/resolve/resolve.h"
#include "librpc/rpc/rpc_common.h"
struct pipe_open_socket_state {
struct dcecli_connection *conn;
struct socket_context *socket_ctx;
struct socket_address *localaddr;
struct socket_address *server;
const char *target_hostname;
enum dcerpc_transport_t transport;
struct socket_address *client;
};
static void continue_socket_connect(struct composite_context *ctx)
{
struct dcecli_connection *conn;
struct composite_context *c = talloc_get_type_abort(
ctx->async.private_data, struct composite_context);
struct pipe_open_socket_state *s = talloc_get_type_abort(
c->private_data, struct pipe_open_socket_state);
int rc;
int sock_fd;
/* make it easier to write a function calls */
conn = s->conn;
c->status = socket_connect_recv(ctx);
if (!NT_STATUS_IS_OK(c->status)) {
DBG_NOTICE("Failed to connect host %s on port %d - %s\n",
s->server->addr, s->server->port,
nt_errstr(c->status));
composite_error(c, c->status);
return;
}
s->client = socket_get_my_addr(s->socket_ctx, s);
if (s->client == NULL) {
TALLOC_FREE(s->socket_ctx);
composite_error(c, NT_STATUS_NO_MEMORY);
return;
}
sock_fd = socket_get_fd(s->socket_ctx);
if (sock_fd == -1) {
TALLOC_FREE(s->socket_ctx);
composite_error(c, NT_STATUS_INVALID_HANDLE);
return;
}
socket_set_flags(s->socket_ctx, SOCKET_FLAG_NOCLOSE);
TALLOC_FREE(s->socket_ctx);
/*
fill in the transport methods
*/
conn->transport.transport = s->transport;
conn->transport.private_data = NULL;
/*
* Windows uses 5840 for ncacn_ip_tcp,
* so we also use it (for every transport which uses bsd sockets)
*/
conn->srv_max_xmit_frag = 5840;
conn->srv_max_recv_frag = 5840;
conn->transport.pending_reads = 0;
conn->server_name = strupper_talloc(conn, s->target_hostname);
rc = tstream_bsd_existing_socket(conn, sock_fd,
&conn->transport.stream);
if (rc < 0) {
close(sock_fd);
composite_error(c, NT_STATUS_NO_MEMORY);
return;
}
conn->transport.write_queue =
tevent_queue_create(conn, "dcerpc sock write queue");
if (conn->transport.write_queue == NULL) {
TALLOC_FREE(conn->transport.stream);
composite_error(c, NT_STATUS_NO_MEMORY);
return;
}
/* ensure we don't get SIGPIPE */
BlockSignals(true, SIGPIPE);
composite_done(c);
}
static struct composite_context *dcerpc_pipe_open_socket_send(TALLOC_CTX *mem_ctx,
struct dcecli_connection *cn,
struct socket_address *localaddr,
struct socket_address *server,
const char *target_hostname,
const char *full_path,
enum dcerpc_transport_t transport)
{
struct composite_context *c;
struct pipe_open_socket_state *s;
struct composite_context *conn_req;
c = composite_create(mem_ctx, cn->event_ctx);
if (c == NULL) return NULL;
s = talloc_zero(c, struct pipe_open_socket_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
s->conn = cn;
s->transport = transport;
if (localaddr) {
s->localaddr = socket_address_copy(s, localaddr);
if (composite_nomem(s->localaddr, c)) return c;
}
s->server = socket_address_copy(s, server);
if (composite_nomem(s->server, c)) return c;
if (target_hostname) {
s->target_hostname = talloc_strdup(s, target_hostname);
if (composite_nomem(s->target_hostname, c)) return c;
}
c->status = socket_create(s, server->family, SOCKET_TYPE_STREAM,
&s->socket_ctx, 0);
if (!composite_is_ok(c)) return c;
conn_req = socket_connect_send(s->socket_ctx, s->localaddr, s->server, 0,
c->event_ctx);
composite_continue(c, conn_req, continue_socket_connect, c);
return c;
}
static NTSTATUS dcerpc_pipe_open_socket_recv(struct composite_context *c,
TALLOC_CTX *mem_ctx,
struct socket_address **localaddr)
{
NTSTATUS status = composite_wait(c);
if (NT_STATUS_IS_OK(status)) {
struct pipe_open_socket_state *s =
talloc_get_type_abort(c->private_data,
struct pipe_open_socket_state);
if (localaddr != NULL) {
*localaddr = talloc_move(mem_ctx, &s->client);
}
}
talloc_free(c);
return status;
}
struct pipe_tcp_state {
const char *server;
const char *target_hostname;
const char **addresses;
uint32_t index;
uint32_t port;
struct socket_address *localaddr;
struct socket_address *srvaddr;
struct resolve_context *resolve_ctx;
struct dcecli_connection *conn;
struct nbt_name name;
char *local_address;
char *remote_address;
};
static void continue_ip_open_socket(struct composite_context *ctx);
static void continue_ip_resolve_name(struct composite_context *ctx);
static void continue_ip_resolve_name(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type_abort(
ctx->async.private_data, struct composite_context);
struct pipe_tcp_state *s = talloc_get_type_abort(
c->private_data, struct pipe_tcp_state);
struct composite_context *sock_ip_req;
c->status = resolve_name_multiple_recv(ctx, s, &s->addresses);
if (!composite_is_ok(c)) return;
/* prepare server address using host ip:port and transport name */
s->srvaddr = socket_address_from_strings(s->conn, "ip", s->addresses[s->index], s->port);
s->index++;
if (composite_nomem(s->srvaddr, c)) return;
sock_ip_req = dcerpc_pipe_open_socket_send(c, s->conn, s->localaddr,
s->srvaddr, s->target_hostname,
NULL,
NCACN_IP_TCP);
composite_continue(c, sock_ip_req, continue_ip_open_socket, c);
}
/*
Stage 2 of dcerpc_pipe_open_tcp_send: receive result of pipe open request
on IP transport.
*/
static void continue_ip_open_socket(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type_abort(
ctx->async.private_data, struct composite_context);
struct pipe_tcp_state *s = talloc_get_type_abort(
c->private_data, struct pipe_tcp_state);
struct socket_address *localaddr = NULL;
/* receive result socket open request */
c->status = dcerpc_pipe_open_socket_recv(ctx, s, &localaddr);
if (!NT_STATUS_IS_OK(c->status)) {
/* something went wrong... */
DBG_NOTICE("Failed to connect host %s (%s) on port %d - %s.\n",
s->addresses[s->index - 1], s->target_hostname,
s->port, nt_errstr(c->status));
if (s->addresses[s->index]) {
struct composite_context *sock_ip_req;
talloc_free(s->srvaddr);
/* prepare server address using host ip:port and transport name */
s->srvaddr = socket_address_from_strings(s->conn, "ip", s->addresses[s->index], s->port);
s->index++;
if (composite_nomem(s->srvaddr, c)) return;
sock_ip_req = dcerpc_pipe_open_socket_send(c, s->conn, s->localaddr,
s->srvaddr, s->target_hostname,
NULL,
NCACN_IP_TCP);
composite_continue(c, sock_ip_req, continue_ip_open_socket, c);
return;
} else {
composite_error(c, c->status);
return;
}
}
s->local_address = talloc_strdup(s, localaddr->addr);
if (composite_nomem(s->local_address, c)) return;
s->remote_address = talloc_strdup(s, s->addresses[s->index - 1]);
if (composite_nomem(s->remote_address, c)) return;
composite_done(c);
}
/*
Send rpc pipe open request to given host:port using
tcp/ip transport
*/
struct composite_context* dcerpc_pipe_open_tcp_send(struct dcecli_connection *conn,
const char *localaddr,
const char *server,
const char *target_hostname,
uint32_t port,
struct resolve_context *resolve_ctx)
{
struct composite_context *c;
struct pipe_tcp_state *s;
struct composite_context *resolve_req;
/* composite context allocation and setup */
c = composite_create(conn, conn->event_ctx);
if (c == NULL) return NULL;
s = talloc_zero(c, struct pipe_tcp_state);
if (composite_nomem(s, c)) return c;
c->private_data = s;
/* store input parameters in state structure */
s->server = talloc_strdup(c, server);
if (composite_nomem(s->server, c)) return c;
if (target_hostname) {
s->target_hostname = talloc_strdup(c, target_hostname);
if (composite_nomem(s->target_hostname, c)) return c;
}
s->port = port;
s->conn = conn;
s->resolve_ctx = resolve_ctx;
if (localaddr) {
s->localaddr = socket_address_from_strings(s, "ip", localaddr, 0);
/* if there is no localaddr, we pass NULL for
s->localaddr, which is handled by the socket libraries as
meaning no local binding address specified */
}
make_nbt_name_server(&s->name, s->server);
resolve_req = resolve_name_send(resolve_ctx, s, &s->name, c->event_ctx);
composite_continue(c, resolve_req, continue_ip_resolve_name, c);
return c;
}
/*
Receive result of pipe open request on tcp/ip
*/
NTSTATUS dcerpc_pipe_open_tcp_recv(struct composite_context *c,
TALLOC_CTX *mem_ctx,
char **localaddr,
char **remoteaddr)
{
NTSTATUS status;
status = composite_wait(c);
if (NT_STATUS_IS_OK(status)) {
struct pipe_tcp_state *s = talloc_get_type_abort(
c->private_data, struct pipe_tcp_state);
if (localaddr != NULL) {
*localaddr = talloc_move(mem_ctx, &s->local_address);
}
if (remoteaddr != NULL) {
*remoteaddr = talloc_move(mem_ctx, &s->remote_address);
}
}
talloc_free(c);
return status;
}
struct pipe_unix_state {
const char *path;
struct socket_address *srvaddr;
struct dcecli_connection *conn;
};
/*
Stage 2 of dcerpc_pipe_open_unix_stream_send: receive result of pipe open
request on unix socket.
*/
static void continue_unix_open_socket(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type_abort(
ctx->async.private_data, struct composite_context);
c->status = dcerpc_pipe_open_socket_recv(ctx, NULL, NULL);
if (NT_STATUS_IS_OK(c->status)) {
composite_done(c);
return;
}
composite_error(c, c->status);
}
/*
Send pipe open request on unix socket
*/
struct composite_context *dcerpc_pipe_open_unix_stream_send(struct dcecli_connection *conn,
const char *path)
{
struct composite_context *c;
struct composite_context *sock_unix_req;
struct pipe_unix_state *s;
/* composite context allocation and setup */
c = composite_create(conn, 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;
/* store parameters in state structure */
s->path = talloc_strdup(c, path);
if (composite_nomem(s->path, c)) return c;
s->conn = conn;
/* prepare server address using socket path and transport name */
s->srvaddr = socket_address_from_strings(conn, "unix", s->path, 0);
if (composite_nomem(s->srvaddr, c)) return c;
/* send socket open request */
sock_unix_req = dcerpc_pipe_open_socket_send(c, s->conn, NULL,
s->srvaddr, NULL,
s->path,
NCALRPC);
composite_continue(c, sock_unix_req, continue_unix_open_socket, c);
return c;
}
/*
Receive result of pipe open request on unix socket
*/
NTSTATUS dcerpc_pipe_open_unix_stream_recv(struct composite_context *c)
{
NTSTATUS status = composite_wait(c);
talloc_free(c);
return status;
}
/*
Stage 2 of dcerpc_pipe_open_pipe_send: receive socket open request
*/
static void continue_np_open_socket(struct composite_context *ctx)
{
struct composite_context *c = talloc_get_type_abort(
ctx->async.private_data, struct composite_context);
c->status = dcerpc_pipe_open_socket_recv(ctx, NULL, NULL);
if (!composite_is_ok(c)) return;
composite_done(c);
}
/*
Send pipe open request on ncalrpc
*/
struct composite_context* dcerpc_pipe_open_pipe_send(struct dcecli_connection *conn,
const char *ncalrpc_dir,
const char *identifier)
{
char *canon = NULL;
struct composite_context *c;
struct composite_context *sock_np_req;
struct pipe_unix_state *s;
/* composite context allocation and setup */
c = composite_create(conn, 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;
/* store parameters in state structure */
canon = talloc_strdup(s, identifier);
if (composite_nomem(canon, c)) return c;
s->conn = conn;
string_replace(canon, '/', '\\');
s->path = talloc_asprintf(canon, "%s/%s", ncalrpc_dir, canon);
if (composite_nomem(s->path, c)) return c;
/* prepare server address using path and transport name */
s->srvaddr = socket_address_from_strings(conn, "unix", s->path, 0);
if (composite_nomem(s->srvaddr, c)) return c;
/* send socket open request */
sock_np_req = dcerpc_pipe_open_socket_send(c, s->conn, NULL, s->srvaddr, NULL, s->path, NCALRPC);
composite_continue(c, sock_np_req, continue_np_open_socket, c);
return c;
}
/*
Receive result of pipe open request on ncalrpc
*/
NTSTATUS dcerpc_pipe_open_pipe_recv(struct composite_context *c)
{
NTSTATUS status = composite_wait(c);
talloc_free(c);
return status;
}
/*
Open a rpc pipe on a named pipe - sync version
*/
NTSTATUS dcerpc_pipe_open_pipe(struct dcecli_connection *conn, const char *ncalrpc_dir, const char *identifier)
{
struct composite_context *c = dcerpc_pipe_open_pipe_send(conn, ncalrpc_dir, identifier);
return dcerpc_pipe_open_pipe_recv(c);
}
|