a79e5adb1028686b961390fd3e1bfbbe04af1e00
[samba.git] / source4 / librpc / rpc / dcerpc_smb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc over SMB transport
5
6    Copyright (C) Tim Potter 2003
7    Copyright (C) Andrew Tridgell 2003
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 /* transport private information used by SMB pipe transport */
27 struct smb_private {
28         uint16 fnum;
29         struct cli_tree *tree;
30 };
31
32 static struct cli_request *dcerpc_raw_send(struct dcerpc_pipe *p, DATA_BLOB *blob)
33 {
34         struct smb_private *smb = p->transport.private;
35         struct smb_trans2 trans;
36         uint16 setup[2];
37         struct cli_request *req;
38         TALLOC_CTX *mem_ctx;
39
40         mem_ctx = talloc_init("dcerpc_raw_send");
41         if (!mem_ctx) return NULL;
42
43         trans.in.data = *blob;
44         trans.in.params = data_blob(NULL, 0);
45         
46         setup[0] = TRANSACT_DCERPCCMD;
47         setup[1] = smb->fnum;
48
49         trans.in.max_param = 0;
50         trans.in.max_data = 0x8000;
51         trans.in.max_setup = 0;
52         trans.in.setup_count = 2;
53         trans.in.flags = 0;
54         trans.in.timeout = 0;
55         trans.in.setup = setup;
56         trans.in.trans_name = "\\PIPE\\";
57
58         req = smb_raw_trans_send(smb->tree, &trans);
59
60         talloc_destroy(mem_ctx);
61
62         return req;
63 }
64
65
66 static NTSTATUS dcerpc_raw_recv(struct dcerpc_pipe *p, 
67                                 struct cli_request *req,
68                                 TALLOC_CTX *mem_ctx,
69                                 DATA_BLOB *blob)
70 {
71         struct smb_private *smb = p->transport.private;
72         struct smb_trans2 trans;
73         NTSTATUS status;
74         uint16 frag_length;
75         DATA_BLOB payload;
76
77         status = smb_raw_trans_recv(req, mem_ctx, &trans);
78         /* STATUS_BUFFER_OVERFLOW means that there is more data
79            available via SMBreadX */
80         if (!NT_STATUS_IS_OK(status) && 
81             !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
82                 return status;
83         }
84
85         payload = trans.out.data;
86
87         if (trans.out.data.length < 16 || 
88             !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
89                 goto done;
90         }
91
92         /* we might have recieved a partial fragment, in which case we
93            need to pull the rest of it */
94         frag_length = dcerpc_get_frag_length(&payload);
95         if (frag_length <= payload.length) {
96                 goto done;
97         }
98
99         /* make sure the payload can hold the whole fragment */
100         payload.data = talloc_realloc(mem_ctx, payload.data, frag_length);
101         if (!payload.data) {
102                 return NT_STATUS_NO_MEMORY;
103         }
104
105         /* the rest of the data is available via SMBreadX */
106         while (frag_length > payload.length) {
107                 uint32 n;
108                 union smb_read io;
109
110                 n = frag_length - payload.length;
111                 if (n > 0xFF00) {
112                         n = 0xFF00;
113                 }
114
115                 io.generic.level = RAW_READ_READX;
116                 io.readx.in.fnum = smb->fnum;
117                 io.readx.in.mincnt = n;
118                 io.readx.in.maxcnt = n;
119                 io.readx.in.offset = 0;
120                 io.readx.in.remaining = 0;
121                 io.readx.out.data = payload.data + payload.length;
122                 status = smb_raw_read(smb->tree, &io);
123                 if (!NT_STATUS_IS_OK(status) &&
124                     !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
125                         break;
126                 }
127                 
128                 n = io.readx.out.nread;
129                 if (n == 0) {
130                         status = NT_STATUS_UNSUCCESSFUL;
131                         break;
132                 }
133                 
134                 payload.length += n;
135
136                 /* if the SMBreadX returns NT_STATUS_OK then there
137                    isn't any more data to be read */
138                 if (NT_STATUS_IS_OK(status)) {
139                         break;
140                 }
141         }
142
143 done:
144         if (blob) {
145                 *blob = payload;
146         }
147
148         return status;
149 }
150
151 static NTSTATUS smb_full_request(struct dcerpc_pipe *p, 
152                                  TALLOC_CTX *mem_ctx,
153                                  DATA_BLOB *request_blob,
154                                  DATA_BLOB *reply_blob)
155 {
156         struct cli_request *req;
157         req = dcerpc_raw_send(p, request_blob);
158         return dcerpc_raw_recv(p, req, mem_ctx, reply_blob);
159 }
160               
161
162 /* 
163    retrieve a secondary pdu from a pipe 
164 */
165 static NTSTATUS smb_secondary_request(struct dcerpc_pipe *p, 
166                                TALLOC_CTX *mem_ctx,
167                                DATA_BLOB *blob)
168 {
169         struct smb_private *smb = p->transport.private;
170         union smb_read io;
171         uint32 n = 0x2000;
172         uint32 frag_length;
173         NTSTATUS status;
174
175         *blob = data_blob_talloc(mem_ctx, NULL, n);
176         if (!blob->data) {
177                 return NT_STATUS_NO_MEMORY;
178         }
179
180         io.generic.level = RAW_READ_READX;
181         io.readx.in.fnum = smb->fnum;
182         io.readx.in.mincnt = n;
183         io.readx.in.maxcnt = n;
184         io.readx.in.offset = 0;
185         io.readx.in.remaining = 0;
186         io.readx.out.data = blob->data;
187
188         status = smb_raw_read(smb->tree, &io);
189         if (!NT_STATUS_IS_OK(status) &&
190             !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
191                 return status;
192         }
193
194         blob->length = io.readx.out.nread;
195
196         if (blob->length < 16) {
197                 return status;
198         }
199
200         frag_length = dcerpc_get_frag_length(blob);
201         if (frag_length <= blob->length) {
202                 return status;
203         }
204
205         blob->data = talloc_realloc(mem_ctx, blob->data, frag_length);
206         if (!blob->data) {
207                 return NT_STATUS_NO_MEMORY;
208         }
209
210         while (frag_length > blob->length &&
211                NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
212
213                 n = frag_length - blob->length;
214                 if (n > 0xFF00) {
215                         n = 0xFF00;
216                 }
217
218                 io.readx.in.mincnt = n;
219                 io.readx.in.maxcnt = n;
220                 io.readx.out.data = blob->data + blob->length;
221                 status = smb_raw_read(smb->tree, &io);
222
223                 if (!NT_STATUS_IS_OK(status) &&
224                     !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
225                         return status;
226                 }
227                 
228                 n = io.readx.out.nread;
229                 blob->length += n;
230         }
231
232         return status;
233 }
234
235
236 /* 
237    send an initial pdu in a multi-pdu sequence
238 */
239 static NTSTATUS smb_initial_request(struct dcerpc_pipe *p, 
240                                     TALLOC_CTX *mem_ctx,
241                                     DATA_BLOB *blob)
242 {
243         struct smb_private *smb = p->transport.private;
244         union smb_write io;
245         NTSTATUS status;
246
247         io.generic.level = RAW_WRITE_WRITEX;
248         io.writex.in.fnum = smb->fnum;
249         io.writex.in.offset = 0;
250         io.writex.in.wmode = PIPE_START_MESSAGE;
251         io.writex.in.remaining = blob->length;
252         io.writex.in.count = blob->length;
253         io.writex.in.data = blob->data;
254
255         status = smb_raw_write(smb->tree, &io);
256         if (NT_STATUS_IS_OK(status)) {
257                 return status;
258         }
259
260         /* make sure it accepted it all */
261         if (io.writex.out.nwritten != blob->length) {
262                 return NT_STATUS_UNSUCCESSFUL;
263         }
264
265         return status;
266 }
267
268
269 /* 
270    shutdown SMB pipe connection
271 */
272 static NTSTATUS smb_shutdown_pipe(struct dcerpc_pipe *p)
273 {
274         struct smb_private *smb = p->transport.private;
275         union smb_close c;
276
277         /* maybe we're still starting up */
278         if (!smb) return NT_STATUS_OK;
279
280         c.close.level = RAW_CLOSE_CLOSE;
281         c.close.in.fnum = smb->fnum;
282         c.close.in.write_time = 0;
283         smb_raw_close(smb->tree, &c);
284         cli_tree_close(smb->tree);
285
286         return NT_STATUS_OK;
287 }
288
289 /*
290   return SMB server name
291 */
292 static const char *smb_peer_name(struct dcerpc_pipe *p)
293 {
294         struct smb_private *smb = p->transport.private;
295         return smb->tree->session->transport->called.name;
296 }
297
298
299 /* 
300    open a rpc connection to a named pipe 
301 */
302 NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe **p, 
303                               struct cli_tree *tree,
304                               const char *pipe_name)
305 {
306         struct smb_private *smb;
307         NTSTATUS status;
308         char *name = NULL;
309         union smb_open io;
310         TALLOC_CTX *mem_ctx;
311
312         asprintf(&name, "\\%s", pipe_name);
313         if (!name) {
314                 return NT_STATUS_NO_MEMORY;
315         }
316
317         io.ntcreatex.level = RAW_OPEN_NTCREATEX;
318         io.ntcreatex.in.flags = 0;
319         io.ntcreatex.in.root_fid = 0;
320         io.ntcreatex.in.access_mask = 
321                 STD_RIGHT_READ_CONTROL_ACCESS | 
322                 SA_RIGHT_FILE_WRITE_ATTRIBUTES | 
323                 SA_RIGHT_FILE_WRITE_EA | 
324                 GENERIC_RIGHTS_FILE_READ |
325                 GENERIC_RIGHTS_FILE_WRITE;
326         io.ntcreatex.in.file_attr = 0;
327         io.ntcreatex.in.alloc_size = 0;
328         io.ntcreatex.in.share_access = 
329                 NTCREATEX_SHARE_ACCESS_READ |
330                 NTCREATEX_SHARE_ACCESS_WRITE;
331         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
332         io.ntcreatex.in.create_options = 0;
333         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
334         io.ntcreatex.in.security_flags = 0;
335         io.ntcreatex.in.fname = name;
336
337         mem_ctx = talloc_init("torture_rpc_connection");
338         if (!mem_ctx) {
339                 free(name);
340                 return NT_STATUS_NO_MEMORY;
341         }
342         status = smb_raw_open(tree, mem_ctx, &io);
343         free(name);
344         talloc_destroy(mem_ctx);
345
346         if (!NT_STATUS_IS_OK(status)) {
347                 return status;
348         }
349
350         if (!(*p = dcerpc_pipe_init())) {
351                 return NT_STATUS_NO_MEMORY;
352         }
353  
354         /*
355           fill in the transport methods
356         */
357         (*p)->transport.transport = NCACN_NP;
358         (*p)->transport.private = NULL;
359         (*p)->transport.full_request = smb_full_request;
360         (*p)->transport.secondary_request = smb_secondary_request;
361         (*p)->transport.initial_request = smb_initial_request;
362         (*p)->transport.shutdown_pipe = smb_shutdown_pipe;
363         (*p)->transport.peer_name = smb_peer_name;
364         
365         smb = talloc((*p)->mem_ctx, sizeof(*smb));
366         if (!smb) {
367                 dcerpc_pipe_close(*p);
368                 return NT_STATUS_NO_MEMORY;
369         }
370
371         smb->fnum = io.ntcreatex.out.fnum;
372         smb->tree = tree;
373
374         (*p)->transport.private = smb;
375         tree->reference_count++;
376
377         return NT_STATUS_OK;
378 }
379
380 /*
381   return the SMB tree used for a dcerpc over SMB pipe
382 */
383 struct cli_tree *dcerpc_smb_tree(struct dcerpc_pipe *p)
384 {
385         struct smb_private *smb = p->transport.private;
386
387         if (p->transport.transport != NCACN_NP) {
388                 return NULL;
389         }
390
391         return smb->tree;
392 }