summaryrefslogtreecommitdiff
path: root/source4/librpc/rpc/dcerpc_smb.c
blob: 95e157b700d370940cdaf5cacb6f023e66206bfe (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
/* 
   Unix SMB/CIFS implementation.

   dcerpc over SMB transport

   Copyright (C) Tim Potter 2003
   Copyright (C) Andrew Tridgell 2003
   
   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"

/* transport private information used by SMB pipe transport */
struct smb_private {
	uint16_t fnum;
	struct cli_tree *tree;
};

static struct cli_request *dcerpc_raw_send(struct dcerpc_pipe *p, DATA_BLOB *blob)
{
	struct smb_private *smb = p->transport.private;
	struct smb_trans2 trans;
	uint16_t setup[2];
	struct cli_request *req;
	TALLOC_CTX *mem_ctx;

	mem_ctx = talloc_init("dcerpc_raw_send");
	if (!mem_ctx) return NULL;

	trans.in.data = *blob;
	trans.in.params = data_blob(NULL, 0);
	
	setup[0] = TRANSACT_DCERPCCMD;
	setup[1] = smb->fnum;

	trans.in.max_param = 0;
	trans.in.max_data = 0x8000;
	trans.in.max_setup = 0;
	trans.in.setup_count = 2;
	trans.in.flags = 0;
	trans.in.timeout = 0;
	trans.in.setup = setup;
	trans.in.trans_name = "\\PIPE\\";

	req = smb_raw_trans_send(smb->tree, &trans);

	talloc_destroy(mem_ctx);

	return req;
}


static NTSTATUS dcerpc_raw_recv(struct dcerpc_pipe *p, 
				struct cli_request *req,
				TALLOC_CTX *mem_ctx,
				DATA_BLOB *blob)
{
	struct smb_private *smb = p->transport.private;
	struct smb_trans2 trans;
	NTSTATUS status;
	uint16_t frag_length;
	DATA_BLOB payload;

	status = smb_raw_trans_recv(req, mem_ctx, &trans);

	/* STATUS_BUFFER_OVERFLOW means that there is more data
	   available via SMBreadX */
	if (!NT_STATUS_IS_OK(status) && 
	    !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
		return status;
	}

	payload = trans.out.data;

	if (trans.out.data.length < 16 || 
	    !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
		goto done;
	}

	/* we might have recieved a partial fragment, in which case we
	   need to pull the rest of it */
	frag_length = dcerpc_get_frag_length(&payload);
	if (frag_length <= payload.length) {
		goto done;
	}

	/* make sure the payload can hold the whole fragment */
	payload.data = talloc_realloc(mem_ctx, payload.data, frag_length);
	if (!payload.data) {
		return NT_STATUS_NO_MEMORY;
	}

	/* the rest of the data is available via SMBreadX */
	while (frag_length > payload.length) {
		uint32_t n;
		union smb_read io;

		n = frag_length - payload.length;
		if (n > 0xFF00) {
			n = 0xFF00;
		}

		io.generic.level = RAW_READ_READX;
		io.readx.in.fnum = smb->fnum;
		io.readx.in.mincnt = n;
		io.readx.in.maxcnt = n;
		io.readx.in.offset = 0;
		io.readx.in.remaining = 0;
		io.readx.out.data = payload.data + payload.length;
		status = smb_raw_read(smb->tree, &io);
		if (!NT_STATUS_IS_OK(status) &&
		    !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
			break;
		}
		
		n = io.readx.out.nread;
		if (n == 0) {
			status = NT_STATUS_UNSUCCESSFUL;
			break;
		}
		
		payload.length += n;

		/* if the SMBreadX returns NT_STATUS_OK then there
		   isn't any more data to be read */
		if (NT_STATUS_IS_OK(status)) {
			break;
		}
	}

done:
	if (blob) {
		*blob = payload;
	}

	return status;
}

static NTSTATUS smb_full_request(struct dcerpc_pipe *p, 
				 TALLOC_CTX *mem_ctx,
				 DATA_BLOB *request_blob,
				 DATA_BLOB *reply_blob)
{
	struct cli_request *req;
	req = dcerpc_raw_send(p, request_blob);
	return dcerpc_raw_recv(p, req, mem_ctx, reply_blob);
}
	      

/* 
   retrieve a secondary pdu from a pipe 
*/
static NTSTATUS smb_secondary_request(struct dcerpc_pipe *p, 
			       TALLOC_CTX *mem_ctx,
			       DATA_BLOB *blob)
{
	struct smb_private *smb = p->transport.private;
	union smb_read io;
	uint32_t n = 0x2000;
	uint32_t frag_length;
	NTSTATUS status;

	*blob = data_blob_talloc(mem_ctx, NULL, n);
	if (!blob->data) {
		return NT_STATUS_NO_MEMORY;
	}

	io.generic.level = RAW_READ_READX;
	io.readx.in.fnum = smb->fnum;
	io.readx.in.mincnt = n;
	io.readx.in.maxcnt = n;
	io.readx.in.offset = 0;
	io.readx.in.remaining = 0;
	io.readx.out.data = blob->data;

	status = smb_raw_read(smb->tree, &io);
	if (!NT_STATUS_IS_OK(status) &&
	    !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
		return status;
	}

	blob->length = io.readx.out.nread;

	if (blob->length < 16) {
		return status;
	}

	frag_length = dcerpc_get_frag_length(blob);
	if (frag_length <= blob->length) {
		return status;
	}

	blob->data = talloc_realloc(mem_ctx, blob->data, frag_length);
	if (!blob->data) {
		return NT_STATUS_NO_MEMORY;
	}

	while (frag_length > blob->length &&
	       NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {

		n = frag_length - blob->length;
		if (n > 0xFF00) {
			n = 0xFF00;
		}

		io.readx.in.mincnt = n;
		io.readx.in.maxcnt = n;
		io.readx.out.data = blob->data + blob->length;
		status = smb_raw_read(smb->tree, &io);

		if (!NT_STATUS_IS_OK(status) &&
		    !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
			return status;
		}
		
		n = io.readx.out.nread;
		blob->length += n;
	}

	return status;
}


/* 
   send an initial pdu in a multi-pdu sequence
*/
static NTSTATUS smb_initial_request(struct dcerpc_pipe *p, 
				    TALLOC_CTX *mem_ctx,
				    DATA_BLOB *blob)
{
	struct smb_private *smb = p->transport.private;
	union smb_write io;
	NTSTATUS status;

	io.generic.level = RAW_WRITE_WRITEX;
	io.writex.in.fnum = smb->fnum;
	io.writex.in.offset = 0;
	io.writex.in.wmode = PIPE_START_MESSAGE;
	io.writex.in.remaining = blob->length;
	io.writex.in.count = blob->length;
	io.writex.in.data = blob->data;

	status = smb_raw_write(smb->tree, &io);
	if (NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* make sure it accepted it all */
	if (io.writex.out.nwritten != blob->length) {
		return NT_STATUS_UNSUCCESSFUL;
	}

	return status;
}


/* 
   shutdown SMB pipe connection
*/
static NTSTATUS smb_shutdown_pipe(struct dcerpc_pipe *p)
{
	struct smb_private *smb = p->transport.private;
	union smb_close c;

	/* maybe we're still starting up */
	if (!smb) return NT_STATUS_OK;

	c.close.level = RAW_CLOSE_CLOSE;
	c.close.in.fnum = smb->fnum;
	c.close.in.write_time = 0;
	smb_raw_close(smb->tree, &c);
	cli_tree_close(smb->tree);

	return NT_STATUS_OK;
}

/*
  return SMB server name
*/
static const char *smb_peer_name(struct dcerpc_pipe *p)
{
	struct smb_private *smb = p->transport.private;
	return smb->tree->session->transport->called.name;
}

/* 
   open a rpc connection to a named pipe 
*/
NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe **p, 
			      struct cli_tree *tree,
			      const char *pipe_name)
{
	struct smb_private *smb;
        NTSTATUS status;
	char *name = NULL;
	union smb_open io;
	TALLOC_CTX *mem_ctx;

	asprintf(&name, "\\%s", pipe_name);
	if (!name) {
		return NT_STATUS_NO_MEMORY;
	}

	io.ntcreatex.level = RAW_OPEN_NTCREATEX;
	io.ntcreatex.in.flags = 0;
	io.ntcreatex.in.root_fid = 0;
	io.ntcreatex.in.access_mask = 
		STD_RIGHT_READ_CONTROL_ACCESS | 
		SA_RIGHT_FILE_WRITE_ATTRIBUTES | 
		SA_RIGHT_FILE_WRITE_EA | 
		GENERIC_RIGHTS_FILE_READ |
		GENERIC_RIGHTS_FILE_WRITE;
	io.ntcreatex.in.file_attr = 0;
	io.ntcreatex.in.alloc_size = 0;
	io.ntcreatex.in.share_access = 
		NTCREATEX_SHARE_ACCESS_READ |
		NTCREATEX_SHARE_ACCESS_WRITE;
	io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
	io.ntcreatex.in.create_options = 0;
	io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
	io.ntcreatex.in.security_flags = 0;
	io.ntcreatex.in.fname = name;

	mem_ctx = talloc_init("torture_rpc_connection");
	if (!mem_ctx) {
		free(name);
		return NT_STATUS_NO_MEMORY;
	}
	status = smb_raw_open(tree, mem_ctx, &io);
	free(name);
	talloc_destroy(mem_ctx);

	if (!NT_STATUS_IS_OK(status)) {
                return status;
        }

        if (!(*p = dcerpc_pipe_init())) {
                return NT_STATUS_NO_MEMORY;
	}
 
	/*
	  fill in the transport methods
	*/
	(*p)->transport.transport = NCACN_NP;
	(*p)->transport.private = NULL;
	(*p)->transport.full_request = smb_full_request;
	(*p)->transport.secondary_request = smb_secondary_request;
	(*p)->transport.initial_request = smb_initial_request;
	(*p)->transport.shutdown_pipe = smb_shutdown_pipe;
	(*p)->transport.peer_name = smb_peer_name;
	
	smb = talloc((*p)->mem_ctx, sizeof(*smb));
	if (!smb) {
		dcerpc_pipe_close(*p);
		return NT_STATUS_NO_MEMORY;
	}

	smb->fnum = io.ntcreatex.out.fnum;
	smb->tree = tree;

	(*p)->transport.private = smb;
	tree->reference_count++;

        return NT_STATUS_OK;
}

/*
  return the SMB tree used for a dcerpc over SMB pipe
*/
struct cli_tree *dcerpc_smb_tree(struct dcerpc_pipe *p)
{
	struct smb_private *smb = p->transport.private;

	if (p->transport.transport != NCACN_NP) {
		return NULL;
	}

	return smb->tree;
}