r11679: opening/creating files in SMB2 now works. Lots of unknown parameters
[kai/samba.git] / source / libcli / smb2 / create.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 client tree handling
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27
28 /*
29   send a create request
30 */
31 struct smb2_request *smb2_create_send(struct smb2_tree *tree, struct smb2_create *io)
32 {
33         struct smb2_request *req;
34         NTSTATUS status;
35         DATA_BLOB path;
36         uint8_t *ptr;
37
38         status = smb2_string_blob(tree, io->in.fname, &path);
39         if (!NT_STATUS_IS_OK(status)) {
40                 return NULL;
41         }
42
43         req = smb2_request_init_tree(tree, SMB2_OP_CREATE, 0x50 + path.length);
44         if (req == NULL) return NULL;
45
46         SIVAL(req->out.body, 0x00, io->in.unknown1);
47         SIVAL(req->out.body, 0x04, io->in.unknown2);
48         SIVAL(req->out.body, 0x08, io->in.unknown3[0]);
49         SIVAL(req->out.body, 0x0C, io->in.unknown3[1]);
50         SIVAL(req->out.body, 0x10, io->in.unknown3[2]);
51         SIVAL(req->out.body, 0x14, io->in.unknown3[3]);
52         SIVAL(req->out.body, 0x18, io->in.access_mask);
53         SIVAL(req->out.body, 0x1C, io->in.file_attr);
54         SIVAL(req->out.body, 0x20, io->in.unknown4);
55         SIVAL(req->out.body, 0x24, io->in.open_disposition);
56         SIVAL(req->out.body, 0x28, io->in.unknown5);
57
58         SSVAL(req->out.body, 0x2C, 0x40+0x38); /* offset to fname */
59         SSVAL(req->out.body, 0x2E, path.length);
60         SIVAL(req->out.body, 0x30, 0x40+0x38+path.length); /* offset to 2nd buffer? */
61
62         SIVAL(req->out.body, 0x34, io->in.unknown6);
63
64         memcpy(req->out.body+0x38, path.data, path.length);
65
66         ptr = req->out.body+0x38+path.length;
67
68         SIVAL(ptr, 0x00, io->in.unknown7);
69         SIVAL(ptr, 0x04, io->in.unknown8);
70         SIVAL(ptr, 0x08, io->in.unknown9);
71         SIVAL(ptr, 0x0C, io->in.unknown10);
72         SIVAL(ptr, 0x10, io->in.unknown11);
73
74         data_blob_free(&path);
75
76         smb2_transport_send(req);
77
78         return req;
79 }
80
81
82 /*
83   recv a create reply
84 */
85 NTSTATUS smb2_create_recv(struct smb2_request *req, struct smb2_create *io)
86 {
87         int i;
88         if (!smb2_request_receive(req) || 
89             smb2_request_is_error(req)) {
90                 return smb2_request_destroy(req);
91         }
92
93         if (req->in.body_size < 0x54) {
94                 printf("body size %d\n", req->in.body_size);
95                 return NT_STATUS_BUFFER_TOO_SMALL;
96         }
97
98         io->out.unknown1 = IVAL(req->in.body, 0x00);
99         io->out.unknown2 = IVAL(req->in.body, 0x04);
100         io->out.create_time = smbcli_pull_nttime(req->in.body, 0x08);
101         io->out.access_time = smbcli_pull_nttime(req->in.body, 0x10);
102         io->out.write_time  = smbcli_pull_nttime(req->in.body, 0x18);
103         io->out.change_time = smbcli_pull_nttime(req->in.body, 0x20);
104         io->out.unknown3 = IVAL(req->in.body, 0x24);
105         io->out.unknown4 = IVAL(req->in.body, 0x28);
106         io->out.unknown5 = IVAL(req->in.body, 0x2C);
107         io->out.unknown6 = IVAL(req->in.body, 0x30);
108         io->out.unknown7 = IVAL(req->in.body, 0x34);
109         memcpy(io->out.handle.data, req->in.body+0x38, 20);
110         for (i=0;i<2;i++) {
111                 io->out.unknown8[i] = IVAL(req->in.body, 0x4C + i*4);
112         }
113
114         return smb2_request_destroy(req);
115 }
116
117 /*
118   sync create request
119 */
120 NTSTATUS smb2_create(struct smb2_tree *tree, struct smb2_create *io)
121 {
122         struct smb2_request *req = smb2_create_send(tree, io);
123         return smb2_create_recv(req, io);
124 }