summaryrefslogtreecommitdiff
path: root/source3/rpc_client/rpc_transport_smbd.c
blob: bde8d04208f2a590b5467218dcba2851f7c52f6a (plain)
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
/*
 *  Unix SMB/CIFS implementation.
 *  RPC client transport over named pipes to a child smbd
 *  Copyright (C) Volker Lendecke 2009
 *
 *  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"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_RPC_CLI

/**
 * struct rpc_cli_smbd_conn represents a forked smbd. This structure should
 * exist only once per process which does the rpc calls.
 *
 * RPC pipe handles can be attached to this smbd connection with
 * rpc_pipe_open_local().
 *
 * For this to work right, we can not use rpc_transport_np directly, because
 * the child smbd wants to write its DEBUG output somewhere. We redirect the
 * child's output to rpc_cli_smbd_conn->stdout_fd. While the RPC calls are
 * active, we have an event context available and attach a fd event to the
 * stdout_df.
 */

struct rpc_cli_smbd_conn {
	/**
	 * The smb connection to handle the named pipe traffic over
	 */
	struct cli_state *cli;

	/**
	 * Attached to stdout in the forked smbd, this is where smbd will
	 * print its DEBUG.
	 */
	int stdout_fd;

	/**
	 * Custom callback provided by the owner of the
	 * rpc_cli_smbd_conn. Here we send the smbd DEBUG output. Can be NULL.
	 */
	struct {
		void (*fn)(char *buf, size_t len, void *priv);
		void *priv;
	} stdout_callback ;
};

/**
 * Event handler to be called whenever the forked smbd prints debugging
 * output.
 */

static void rpc_cli_smbd_stdout_reader(struct event_context *ev,
				       struct fd_event *fde,
				       uint16_t flags, void *priv)
{
	struct rpc_cli_smbd_conn *conn = talloc_get_type_abort(
		priv, struct rpc_cli_smbd_conn);
	char buf[1024];
	ssize_t nread;

	if ((flags & EVENT_FD_READ) == 0) {
		return;
	}

	nread = read(conn->stdout_fd, buf, sizeof(buf)-1);
	if (nread < 0) {
		DEBUG(0, ("Could not read from smbd stdout: %s\n",
			  strerror(errno)));
		TALLOC_FREE(fde);
		return;
	}
	if (nread == 0) {
		DEBUG(0, ("EOF from smbd stdout\n"));
		TALLOC_FREE(fde);
		return;
	}

	if (conn->stdout_callback.fn != NULL) {
		conn->stdout_callback.fn(buf, nread,
					 conn->stdout_callback.priv);
	}
}

/**
 * struct rpc_transport_smbd_state is the link from a struct rpc_pipe_client
 * to the rpc_cli_smbd_conn. We use a named pipe transport as a subtransport.
 */

struct rpc_transport_smbd_state {
	struct rpc_cli_smbd_conn *conn;
	struct rpc_cli_transport *sub_transp;
};

static int rpc_cli_smbd_conn_destructor(struct rpc_cli_smbd_conn *conn)
{
	if (conn->cli != NULL) {
		cli_shutdown(conn->cli);
		conn->cli = NULL;
	}
	if (conn->stdout_fd != -1) {
		close(conn->stdout_fd);
		conn->stdout_fd = -1;
	}
	return 0;
}

/*
 * Do the negprot/sesssetup/tcon to an anonymous ipc$ connection
 */

struct get_anon_ipc_state {
	struct event_context *ev;
	struct cli_state *cli;
};

static void get_anon_ipc_negprot_done(struct async_req *subreq);
static void get_anon_ipc_sesssetup_done(struct async_req *subreq);
static void get_anon_ipc_tcon_done(struct async_req *subreq);

static struct async_req *get_anon_ipc_send(TALLOC_CTX *mem_ctx,
					   struct event_context *ev,
					   struct cli_state *cli)
{
	struct async_req *result, *subreq;
	struct get_anon_ipc_state *state;

	if (!async_req_setup(mem_ctx, &result, &state,
			     struct get_anon_ipc_state)) {
		return NULL;
	}

	state->ev = ev;
	state->cli = cli;

	subreq = cli_negprot_send(state, ev, cli);
	if (subreq == NULL) {
		goto fail;
	}
	subreq->async.fn = get_anon_ipc_negprot_done;
	subreq->async.priv = result;
	return result;
 fail:
	TALLOC_FREE(result);
	return NULL;
}

static void get_anon_ipc_negprot_done(struct async_req *subreq)
{
	struct async_req *req = talloc_get_type_abort(
		subreq->async.priv, struct async_req);
	struct get_anon_ipc_state *state = talloc_get_type_abort(
		req->private_data, struct get_anon_ipc_state);
	NTSTATUS status;

	status = cli_negprot_recv(subreq);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		async_req_nterror(req, status);
		return;
	}

	subreq = cli_session_setup_guest_send(state, state->ev, state->cli);
	if (async_req_nomem(subreq, req)) {
		return;
	}
	subreq->async.fn = get_anon_ipc_sesssetup_done;
	subreq->async.priv = req;
}

static void get_anon_ipc_sesssetup_done(struct async_req *subreq)
{
	struct async_req *req = talloc_get_type_abort(
		subreq->async.priv, struct async_req);
	struct get_anon_ipc_state *state = talloc_get_type_abort(
		req->private_data, struct get_anon_ipc_state);
	NTSTATUS status;

	status = cli_session_setup_guest_recv(subreq);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		async_req_nterror(req, status);
		return;
	}

	subreq = cli_tcon_andx_send(state, state->ev, state->cli,
				    "IPC$", "IPC", NULL, 0);
	if (async_req_nomem(subreq, req)) {
		return;
	}
	subreq->async.fn = get_anon_ipc_tcon_done;
	subreq->async.priv = req;
}

static void get_anon_ipc_tcon_done(struct async_req *subreq)
{
	struct async_req *req = talloc_get_type_abort(
		subreq->async.priv, struct async_req);
	NTSTATUS status;

	status = cli_tcon_andx_recv(subreq);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		async_req_nterror(req, status);
		return;
	}
	async_req_done(req);
}

static NTSTATUS get_anon_ipc_recv(struct async_req *req)
{
	return async_req_simple_recv_ntstatus(req);
}

struct rpc_cli_smbd_conn_init_state {
	struct event_context *ev;
	struct rpc_cli_smbd_conn *conn;
};

static void rpc_cli_smbd_conn_init_done(struct async_req *subreq);

struct async_req *rpc_cli_smbd_conn_init_send(TALLOC_CTX *mem_ctx,
					      struct event_context *ev,
					      void (*stdout_callback)(char *buf,
								      size_t len,
								      void *priv),
					      void *priv)
{
	struct async_req *result, *subreq;
	struct rpc_cli_smbd_conn_init_state *state;
	int smb_sock[2];
	int stdout_pipe[2];
	NTSTATUS status;
	pid_t pid;
	int ret;

	smb_sock[0] = smb_sock[1] = stdout_pipe[0] = stdout_pipe[1] = -1;

	if (!async_req_setup(mem_ctx, &result, &state,
			     struct rpc_cli_smbd_conn_init_state)) {
		return NULL;
	}
	state->ev = ev;

	state->conn = talloc(state, struct rpc_cli_smbd_conn);
	if (state->conn == NULL) {
		goto nomem;
	}

	state->conn->cli = cli_initialise();
	if (state->conn->cli == NULL) {
		goto nomem;
	}
	state->conn->stdout_fd = -1;
	state->conn->stdout_callback.fn = stdout_callback;
	state->conn->stdout_callback.priv = priv;
	talloc_set_destructor(state->conn, rpc_cli_smbd_conn_destructor);

	ret = socketpair(AF_UNIX, SOCK_STREAM, 0, smb_sock);
	if (ret == -1) {
		status = map_nt_error_from_unix(errno);
		goto post_status;
	}
	ret = pipe(stdout_pipe);
	if (ret == -1) {
		status = map_nt_error_from_unix(errno);
		goto post_status;
	}

	pid = sys_fork();
	if (pid == -1) {
		status = map_nt_error_from_unix(errno);
		goto post_status;
	}
	if (pid == 0) {
		char *smbd_cmd;

		close(smb_sock[0]);
		close(stdout_pipe[0]);
		close(0);
		if (dup(smb_sock[1]) == -1) {
			exit(1);
		}
		close(smb_sock[1]);
		close(1);
		if (dup(stdout_pipe[1]) == -1) {
			exit(1);
		}
		close(stdout_pipe[1]);

		smbd_cmd = getenv("SMB_PATH");

		if ((smbd_cmd == NULL)
		    && (asprintf(&smbd_cmd, "%s/smbd", get_dyn_SBINDIR())
			== -1)) {
			printf("no memory");
			exit(1);
		}
		if (asprintf(&smbd_cmd, "%s -F -S", smbd_cmd) == -1) {
			printf("no memory");
			exit(1);
		}

		exit(system(smbd_cmd));
	}

	state->conn->cli->fd = smb_sock[0];
	smb_sock[0] = -1;
	close(smb_sock[1]);
	smb_sock[1] = -1;

	state->conn->stdout_fd = stdout_pipe[0];
	stdout_pipe[0] = -1;
	close(stdout_pipe[1]);
	stdout_pipe[1] = -1;

	subreq = get_anon_ipc_send(state, ev, state->conn->cli);
	if (subreq == NULL) {
		goto nomem;
	}

	if (event_add_fd(ev, subreq, state->conn->stdout_fd, EVENT_FD_READ,
			 rpc_cli_smbd_stdout_reader, state->conn) == NULL) {
		goto nomem;
	}

	subreq->async.fn = rpc_cli_smbd_conn_init_done;
	subreq->async.priv = result;
	return result;

 nomem:
	status = NT_STATUS_NO_MEMORY;
 post_status:
	if (smb_sock[0] != -1) {
		close(smb_sock[0]);
	}
	if (smb_sock[1] != -1) {
		close(smb_sock[1]);
	}
	if (stdout_pipe[0] != -1) {
		close(stdout_pipe[0]);
	}
	if (stdout_pipe[1] != -1) {
		close(stdout_pipe[1]);
	}
	if (async_post_ntstatus(result, ev, status)) {
		return result;
	}
	TALLOC_FREE(result);
	return NULL;
}

static void rpc_cli_smbd_conn_init_done(struct async_req *subreq)
{
	struct async_req *req = talloc_get_type_abort(
		subreq->async.priv, struct async_req);
	NTSTATUS status;

	status = get_anon_ipc_recv(subreq);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		async_req_nterror(req, status);
		return;
	}
	async_req_done(req);
}

NTSTATUS rpc_cli_smbd_conn_init_recv(struct async_req *req,
				     TALLOC_CTX *mem_ctx,
				     struct rpc_cli_smbd_conn **pconn)
{
	struct rpc_cli_smbd_conn_init_state *state = talloc_get_type_abort(
		req->private_data, struct rpc_cli_smbd_conn_init_state);
	NTSTATUS status;

	if (async_req_is_nterror(req, &status)) {
		return status;
	}
	*pconn = talloc_move(mem_ctx, &state->conn);
	return NT_STATUS_OK;
}

NTSTATUS rpc_cli_smbd_conn_init(TALLOC_CTX *mem_ctx,
				struct rpc_cli_smbd_conn **pconn,
				void (*stdout_callback)(char *buf,
							size_t len,
							void *priv),
				void *priv)
{
	TALLOC_CTX *frame = talloc_stackframe();
	struct event_context *ev;
	struct async_req *req;
	NTSTATUS status;

	ev = event_context_init(frame);
	if (ev == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto fail;
	}

	req = rpc_cli_smbd_conn_init_send(frame, ev, stdout_callback, priv);
	if (req == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto fail;
	}

	while (req->state < ASYNC_REQ_DONE) {
		event_loop_once(ev);
	}

	status = rpc_cli_smbd_conn_init_recv(req, mem_ctx, pconn);
 fail:
	TALLOC_FREE(frame);
	return status;
}

struct rpc_smbd_write_state {
	struct rpc_cli_transport *sub_transp;
	ssize_t written;
};

static void rpc_smbd_write_done(struct tevent_req *subreq);

static struct tevent_req *rpc_smbd_write_send(TALLOC_CTX *mem_ctx,
					      struct event_context *ev,
					      const uint8_t *data, size_t size,
					      void *priv)
{
	struct rpc_transport_smbd_state *transp = talloc_get_type_abort(
		priv, struct rpc_transport_smbd_state);
	struct tevent_req *req, *subreq;
	struct rpc_smbd_write_state *state;

	req = tevent_req_create(mem_ctx, &state, struct rpc_smbd_write_state);
	if (req == NULL) {
		return NULL;
	}
	state->sub_transp = transp->sub_transp;

	subreq = transp->sub_transp->write_send(state, ev, data, size,
						transp->sub_transp->priv);
	if (subreq == NULL) {
		goto fail;
	}

	if (event_add_fd(ev, subreq, transp->conn->stdout_fd, EVENT_FD_READ,
			 rpc_cli_smbd_stdout_reader, transp->conn) == NULL) {
		goto fail;
	}
	tevent_req_set_callback(subreq, rpc_smbd_write_done, req);
	return req;

 fail:
	TALLOC_FREE(req);
	return NULL;
}

static void rpc_smbd_write_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct rpc_smbd_write_state *state = tevent_req_data(
		req, struct rpc_smbd_write_state);
	NTSTATUS status;

	status = state->sub_transp->write_recv(subreq, &state->written);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		tevent_req_nterror(req, status);
		return;
	}
	tevent_req_done(req);
}

static NTSTATUS rpc_smbd_write_recv(struct tevent_req *req, ssize_t *pwritten)
{
	struct rpc_smbd_write_state *state = tevent_req_data(
		req, struct rpc_smbd_write_state);
	NTSTATUS status;

	if (tevent_req_is_nterror(req, &status)) {
		return status;
	}
	*pwritten = state->written;
	return NT_STATUS_OK;
}

struct rpc_smbd_read_state {
	struct rpc_cli_transport *sub_transp;
	ssize_t received;
};

static void rpc_smbd_read_done(struct tevent_req *subreq);

static struct tevent_req *rpc_smbd_read_send(TALLOC_CTX *mem_ctx,
					     struct event_context *ev,
					     uint8_t *data, size_t size,
					     void *priv)
{
	struct rpc_transport_smbd_state *transp = talloc_get_type_abort(
		priv, struct rpc_transport_smbd_state);
	struct tevent_req *req, *subreq;
	struct rpc_smbd_read_state *state;

	req = tevent_req_create(mem_ctx, &state, struct rpc_smbd_read_state);
	if (req == NULL) {
		return NULL;
	}
	state->sub_transp = transp->sub_transp;

	subreq = transp->sub_transp->read_send(state, ev, data, size,
						transp->sub_transp->priv);
	if (subreq == NULL) {
		goto fail;
	}

	if (event_add_fd(ev, subreq, transp->conn->stdout_fd, EVENT_FD_READ,
			 rpc_cli_smbd_stdout_reader, transp->conn) == NULL) {
		goto fail;
	}
	tevent_req_set_callback(subreq, rpc_smbd_read_done, req);
	return req;
 fail:
	TALLOC_FREE(req);
	return NULL;
}

static void rpc_smbd_read_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct rpc_smbd_read_state *state = tevent_req_data(
		req, struct rpc_smbd_read_state);
	NTSTATUS status;

	status = state->sub_transp->read_recv(subreq, &state->received);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		tevent_req_nterror(req, status);
		return;
	}
	tevent_req_done(req);
}

static NTSTATUS rpc_smbd_read_recv(struct tevent_req *req, ssize_t *preceived)
{
	struct rpc_smbd_read_state *state = tevent_req_data(
		req, struct rpc_smbd_read_state);
	NTSTATUS status;

	if (tevent_req_is_nterror(req, &status)) {
		return status;
	}
	*preceived = state->received;
	return NT_STATUS_OK;
}

struct rpc_transport_smbd_init_state {
	struct rpc_cli_transport *transport;
	struct rpc_transport_smbd_state *transport_smbd;
};

static void rpc_transport_smbd_init_done(struct async_req *subreq);

struct async_req *rpc_transport_smbd_init_send(TALLOC_CTX *mem_ctx,
					       struct event_context *ev,
					       struct rpc_cli_smbd_conn *conn,
					       const struct ndr_syntax_id *abstract_syntax)
{
	struct async_req *result, *subreq;
	struct rpc_transport_smbd_init_state *state;

	if (!async_req_setup(mem_ctx, &result, &state,
			     struct rpc_transport_smbd_init_state)) {
		return NULL;
	}

	state->transport = talloc(state, struct rpc_cli_transport);
	if (state->transport == NULL) {
		goto fail;
	}
	state->transport_smbd = talloc(state->transport,
				       struct rpc_transport_smbd_state);
	if (state->transport_smbd == NULL) {
		goto fail;
	}
	state->transport_smbd->conn = conn;
	state->transport->priv = state->transport_smbd;

	subreq = rpc_transport_np_init_send(state, ev, conn->cli,
					    abstract_syntax);
	if (subreq == NULL) {
		goto fail;
	}
	subreq->async.fn = rpc_transport_smbd_init_done;
	subreq->async.priv = result;
	return result;

 fail:
	TALLOC_FREE(result);
	return NULL;
}

static void rpc_transport_smbd_init_done(struct async_req *subreq)
{
	struct async_req *req = talloc_get_type_abort(
		subreq->async.priv, struct async_req);
	struct rpc_transport_smbd_init_state *state = talloc_get_type_abort(
		req->private_data, struct rpc_transport_smbd_init_state);
	NTSTATUS status;

	status = rpc_transport_np_init_recv(
		subreq, state->transport_smbd,
		&state->transport_smbd->sub_transp);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		async_req_nterror(req, status);
		return;
	}
	async_req_done(req);
}

NTSTATUS rpc_transport_smbd_init_recv(struct async_req *req,
				      TALLOC_CTX *mem_ctx,
				      struct rpc_cli_transport **presult)
{
	struct rpc_transport_smbd_init_state *state = talloc_get_type_abort(
		req->private_data, struct rpc_transport_smbd_init_state);
	NTSTATUS status;

	if (async_req_is_nterror(req, &status)) {
		return status;
	}

	state->transport->write_send = rpc_smbd_write_send;
	state->transport->write_recv = rpc_smbd_write_recv;
	state->transport->read_send = rpc_smbd_read_send;
	state->transport->read_recv = rpc_smbd_read_recv;
	state->transport->trans_send = NULL;
	state->transport->trans_recv = NULL;

	*presult = talloc_move(mem_ctx, &state->transport);
	return NT_STATUS_OK;
}

NTSTATUS rpc_transport_smbd_init(TALLOC_CTX *mem_ctx,
				 struct rpc_cli_smbd_conn *conn,
				 const struct ndr_syntax_id *abstract_syntax,
				 struct rpc_cli_transport **presult)
{
	TALLOC_CTX *frame = talloc_stackframe();
	struct event_context *ev;
	struct async_req *req;
	NTSTATUS status;

	ev = event_context_init(frame);
	if (ev == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto fail;
	}

	req = rpc_transport_smbd_init_send(frame, ev, conn, abstract_syntax);
	if (req == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto fail;
	}

	while (req->state < ASYNC_REQ_DONE) {
		event_loop_once(ev);
	}

	status = rpc_transport_smbd_init_recv(req, mem_ctx, presult);
 fail:
	TALLOC_FREE(frame);
	return status;
}