r11694: fixed 2 valgrind errors
[kai/samba.git] / source4 / 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         SSVAL(req->out.body, 0x00, io->in.buffer_code);
47         SSVAL(req->out.body, 0x02, io->in.oplock_flags);
48         SIVAL(req->out.body, 0x04, io->in.unknown2);
49         SIVAL(req->out.body, 0x08, io->in.unknown3[0]);
50         SIVAL(req->out.body, 0x0C, io->in.unknown3[1]);
51         SIVAL(req->out.body, 0x10, io->in.unknown3[2]);
52         SIVAL(req->out.body, 0x14, io->in.unknown3[3]);
53         SIVAL(req->out.body, 0x18, io->in.access_mask);
54         SIVAL(req->out.body, 0x1C, io->in.file_attr);
55         SIVAL(req->out.body, 0x20, io->in.share_access);
56         SIVAL(req->out.body, 0x24, io->in.open_disposition);
57         SIVAL(req->out.body, 0x28, io->in.create_options);
58
59         SSVAL(req->out.body, 0x2C, 0x40+0x38); /* offset to fname */
60         SSVAL(req->out.body, 0x2E, path.length);
61         SIVAL(req->out.body, 0x30, 0x40+0x38+path.length); /* offset to 2nd buffer? */
62
63         SIVAL(req->out.body, 0x34, io->in.unknown6);
64
65         memcpy(req->out.body+0x38, path.data, path.length);
66
67         ptr = req->out.body+0x38+path.length;
68
69         SIVAL(ptr, 0x00, io->in.unknown7);
70         SIVAL(ptr, 0x04, io->in.unknown8);
71         SIVAL(ptr, 0x08, io->in.unknown9);
72         SIVAL(ptr, 0x0C, io->in.unknown10);
73         SBVAL(ptr, 0x10, io->in.unknown11);
74
75         data_blob_free(&path);
76
77         smb2_transport_send(req);
78
79         return req;
80 }
81
82
83 /*
84   recv a create reply
85 */
86 NTSTATUS smb2_create_recv(struct smb2_request *req, struct smb2_create *io)
87 {
88         smb2_request_receive(req);
89         dump_data(0, req->in.body, req->in.body_size);
90
91         if (!smb2_request_receive(req) || 
92             smb2_request_is_error(req)) {
93                 return smb2_request_destroy(req);
94         }
95
96         if (req->in.body_size < 0x54) {
97                 return NT_STATUS_BUFFER_TOO_SMALL;
98         }
99
100         SMB2_CHECK_BUFFER_CODE(req, 0x59);
101
102         io->out.oplock_flags   = SVAL(req->in.body, 0x02);
103         io->out.create_action  = IVAL(req->in.body, 0x04);
104         io->out.create_time    = smbcli_pull_nttime(req->in.body, 0x08);
105         io->out.access_time    = smbcli_pull_nttime(req->in.body, 0x10);
106         io->out.write_time     = smbcli_pull_nttime(req->in.body, 0x18);
107         io->out.change_time    = smbcli_pull_nttime(req->in.body, 0x20);
108         io->out.alloc_size     = BVAL(req->in.body, 0x28);
109         io->out.size           = BVAL(req->in.body, 0x30);
110         io->out.file_attr      = IVAL(req->in.body, 0x38);
111         io->out._pad           = IVAL(req->in.body, 0x3C);
112         io->out.handle.data[0] = BVAL(req->in.body, 0x40);
113         io->out.handle.data[1] = BVAL(req->in.body, 0x48);
114         io->out.unknown4 = IVAL(req->in.body, 0x50);
115
116         return smb2_request_destroy(req);
117 }
118
119 /*
120   sync create request
121 */
122 NTSTATUS smb2_create(struct smb2_tree *tree, struct smb2_create *io)
123 {
124         struct smb2_request *req = smb2_create_send(tree, io);
125         return smb2_create_recv(req, io);
126 }