r884: convert samba4 to use [u]int32_t instead of [u]int32
[nivanova/samba-autobuild/.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
79         /* STATUS_BUFFER_OVERFLOW means that there is more data
80            available via SMBreadX */
81         if (!NT_STATUS_IS_OK(status) && 
82             !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
83                 return status;
84         }
85
86         payload = trans.out.data;
87
88         if (trans.out.data.length < 16 || 
89             !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
90                 goto done;
91         }
92
93         /* we might have recieved a partial fragment, in which case we
94            need to pull the rest of it */
95         frag_length = dcerpc_get_frag_length(&payload);
96         if (frag_length <= payload.length) {
97                 goto done;
98         }
99
100         /* make sure the payload can hold the whole fragment */
101         payload.data = talloc_realloc(mem_ctx, payload.data, frag_length);
102         if (!payload.data) {
103                 return NT_STATUS_NO_MEMORY;
104         }
105
106         /* the rest of the data is available via SMBreadX */
107         while (frag_length > payload.length) {
108                 uint32_t n;
109                 union smb_read io;
110
111                 n = frag_length - payload.length;
112                 if (n > 0xFF00) {
113                         n = 0xFF00;
114                 }
115
116                 io.generic.level = RAW_READ_READX;
117                 io.readx.in.fnum = smb->fnum;
118                 io.readx.in.mincnt = n;
119                 io.readx.in.maxcnt = n;
120                 io.readx.in.offset = 0;
121                 io.readx.in.remaining = 0;
122                 io.readx.out.data = payload.data + payload.length;
123                 status = smb_raw_read(smb->tree, &io);
124                 if (!NT_STATUS_IS_OK(status) &&
125                     !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
126                         break;
127                 }
128                 
129                 n = io.readx.out.nread;
130                 if (n == 0) {
131                         status = NT_STATUS_UNSUCCESSFUL;
132                         break;
133                 }
134                 
135                 payload.length += n;
136
137                 /* if the SMBreadX returns NT_STATUS_OK then there
138                    isn't any more data to be read */
139                 if (NT_STATUS_IS_OK(status)) {
140                         break;
141                 }
142         }
143
144 done:
145         if (blob) {
146                 *blob = payload;
147         }
148
149         return status;
150 }
151
152 static NTSTATUS smb_full_request(struct dcerpc_pipe *p, 
153                                  TALLOC_CTX *mem_ctx,
154                                  DATA_BLOB *request_blob,
155                                  DATA_BLOB *reply_blob)
156 {
157         struct cli_request *req;
158         req = dcerpc_raw_send(p, request_blob);
159         return dcerpc_raw_recv(p, req, mem_ctx, reply_blob);
160 }
161               
162
163 /* 
164    retrieve a secondary pdu from a pipe 
165 */
166 static NTSTATUS smb_secondary_request(struct dcerpc_pipe *p, 
167                                TALLOC_CTX *mem_ctx,
168                                DATA_BLOB *blob)
169 {
170         struct smb_private *smb = p->transport.private;
171         union smb_read io;
172         uint32_t n = 0x2000;
173         uint32_t frag_length;
174         NTSTATUS status;
175
176         *blob = data_blob_talloc(mem_ctx, NULL, n);
177         if (!blob->data) {
178                 return NT_STATUS_NO_MEMORY;
179         }
180
181         io.generic.level = RAW_READ_READX;
182         io.readx.in.fnum = smb->fnum;
183         io.readx.in.mincnt = n;
184         io.readx.in.maxcnt = n;
185         io.readx.in.offset = 0;
186         io.readx.in.remaining = 0;
187         io.readx.out.data = blob->data;
188
189         status = smb_raw_read(smb->tree, &io);
190         if (!NT_STATUS_IS_OK(status) &&
191             !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
192                 return status;
193         }
194
195         blob->length = io.readx.out.nread;
196
197         if (blob->length < 16) {
198                 return status;
199         }
200
201         frag_length = dcerpc_get_frag_length(blob);
202         if (frag_length <= blob->length) {
203                 return status;
204         }
205
206         blob->data = talloc_realloc(mem_ctx, blob->data, frag_length);
207         if (!blob->data) {
208                 return NT_STATUS_NO_MEMORY;
209         }
210
211         while (frag_length > blob->length &&
212                NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
213
214                 n = frag_length - blob->length;
215                 if (n > 0xFF00) {
216                         n = 0xFF00;
217                 }
218
219                 io.readx.in.mincnt = n;
220                 io.readx.in.maxcnt = n;
221                 io.readx.out.data = blob->data + blob->length;
222                 status = smb_raw_read(smb->tree, &io);
223
224                 if (!NT_STATUS_IS_OK(status) &&
225                     !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
226                         return status;
227                 }
228                 
229                 n = io.readx.out.nread;
230                 blob->length += n;
231         }
232
233         return status;
234 }
235
236
237 /* 
238    send an initial pdu in a multi-pdu sequence
239 */
240 static NTSTATUS smb_initial_request(struct dcerpc_pipe *p, 
241                                     TALLOC_CTX *mem_ctx,
242                                     DATA_BLOB *blob)
243 {
244         struct smb_private *smb = p->transport.private;
245         union smb_write io;
246         NTSTATUS status;
247
248         io.generic.level = RAW_WRITE_WRITEX;
249         io.writex.in.fnum = smb->fnum;
250         io.writex.in.offset = 0;
251         io.writex.in.wmode = PIPE_START_MESSAGE;
252         io.writex.in.remaining = blob->length;
253         io.writex.in.count = blob->length;
254         io.writex.in.data = blob->data;
255
256         status = smb_raw_write(smb->tree, &io);
257         if (NT_STATUS_IS_OK(status)) {
258                 return status;
259         }
260
261         /* make sure it accepted it all */
262         if (io.writex.out.nwritten != blob->length) {
263                 return NT_STATUS_UNSUCCESSFUL;
264         }
265
266         return status;
267 }
268
269
270 /* 
271    shutdown SMB pipe connection
272 */
273 static NTSTATUS smb_shutdown_pipe(struct dcerpc_pipe *p)
274 {
275         struct smb_private *smb = p->transport.private;
276         union smb_close c;
277
278         /* maybe we're still starting up */
279         if (!smb) return NT_STATUS_OK;
280
281         c.close.level = RAW_CLOSE_CLOSE;
282         c.close.in.fnum = smb->fnum;
283         c.close.in.write_time = 0;
284         smb_raw_close(smb->tree, &c);
285         cli_tree_close(smb->tree);
286
287         return NT_STATUS_OK;
288 }
289
290 /*
291   return SMB server name
292 */
293 static const char *smb_peer_name(struct dcerpc_pipe *p)
294 {
295         struct smb_private *smb = p->transport.private;
296         return smb->tree->session->transport->called.name;
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 }