5e680fbcecb776017b283165212c74c056165879
[kai/samba-autobuild/.git] / source4 / torture / smb2 / util.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    helper functions for SMB2 test suite
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "lib/events/events.h"
27 #include "system/time.h"
28 #include "librpc/gen_ndr/ndr_security.h"
29
30
31 /*
32   close a handle with SMB2
33 */
34 NTSTATUS smb2_util_close(struct smb2_tree *tree, struct smb2_handle h)
35 {
36         struct smb2_close c;
37
38         ZERO_STRUCT(c);
39         c.in.file.handle = h;
40
41         return smb2_close(tree, &c);
42 }
43
44 /*
45   unlink a file with SMB2
46 */
47 NTSTATUS smb2_util_unlink(struct smb2_tree *tree, const char *fname)
48 {
49         struct smb2_create io;
50         NTSTATUS status;
51
52         ZERO_STRUCT(io);
53         io.in.access_mask = SEC_RIGHTS_FILE_ALL;
54         io.in.file_attr   = FILE_ATTRIBUTE_NORMAL;
55         io.in.open_disposition = NTCREATEX_DISP_OPEN;
56         io.in.share_access = 
57                 NTCREATEX_SHARE_ACCESS_DELETE|
58                 NTCREATEX_SHARE_ACCESS_READ|
59                 NTCREATEX_SHARE_ACCESS_WRITE;
60         io.in.create_options = NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
61         io.in.fname = fname;
62
63         status = smb2_create(tree, tree, &io);
64         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
65                 return NT_STATUS_OK;
66         }
67         NT_STATUS_NOT_OK_RETURN(status);
68
69         return smb2_util_close(tree, io.out.file.handle);
70 }
71
72 /*
73   write to a file on SMB2
74 */
75 NTSTATUS smb2_util_write(struct smb2_tree *tree,
76                          struct smb2_handle handle, 
77                          const void *buf, off_t offset, size_t size)
78 {
79         struct smb2_write w;
80
81         ZERO_STRUCT(w);
82         w.in.file.handle = handle;
83         w.in.offset      = offset;
84         w.in.data        = data_blob_const(buf, size);
85
86         return smb2_write(tree, &w);
87 }
88
89 /*
90   create a complex file/dir using the SMB2 protocol
91 */
92 static NTSTATUS smb2_create_complex(struct smb2_tree *tree, const char *fname, 
93                                          struct smb2_handle *handle, BOOL dir)
94 {
95         TALLOC_CTX *tmp_ctx = talloc_new(tree);
96         char buf[7] = "abc";
97         struct smb2_create io;
98         union smb_setfileinfo setfile;
99         union smb_fileinfo fileinfo;
100         time_t t = (time(NULL) & ~1);
101         NTSTATUS status;
102
103         smb2_util_unlink(tree, fname);
104         ZERO_STRUCT(io);
105         io.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
106         io.in.file_attr   = FILE_ATTRIBUTE_NORMAL;
107         io.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
108         io.in.share_access = 
109                 NTCREATEX_SHARE_ACCESS_DELETE|
110                 NTCREATEX_SHARE_ACCESS_READ|
111                 NTCREATEX_SHARE_ACCESS_WRITE;
112         io.in.create_options = 0;
113         io.in.fname = fname;
114         if (dir) {
115                 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
116                 io.in.share_access &= ~NTCREATEX_SHARE_ACCESS_DELETE;
117                 io.in.file_attr   = FILE_ATTRIBUTE_DIRECTORY;
118                 io.in.open_disposition = NTCREATEX_DISP_CREATE;
119         }
120
121         if (strchr(fname, ':') == NULL) {
122                 /* setup some EAs */
123                 io.in.eas.num_eas = 2;
124                 io.in.eas.eas = talloc_array(tmp_ctx, struct ea_struct, 2);
125                 io.in.eas.eas[0].flags = 0;
126                 io.in.eas.eas[0].name.s = "EAONE";
127                 io.in.eas.eas[0].value = data_blob_talloc(tmp_ctx, "VALUE1", 6);
128                 io.in.eas.eas[1].flags = 0;
129                 io.in.eas.eas[1].name.s = "SECONDEA";
130                 io.in.eas.eas[1].value = data_blob_talloc(tmp_ctx, "ValueTwo", 8);
131         }
132
133         status = smb2_create(tree, tmp_ctx, &io);
134         talloc_free(tmp_ctx);
135         NT_STATUS_NOT_OK_RETURN(status);
136
137         *handle = io.out.file.handle;
138
139         if (!dir) {
140                 status = smb2_util_write(tree, *handle, buf, 0, sizeof(buf));
141                 NT_STATUS_NOT_OK_RETURN(status);
142         }
143
144         /* make sure all the timestamps aren't the same, and are also 
145            in different DST zones*/
146         setfile.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
147         setfile.generic.in.file.handle = *handle;
148
149         unix_to_nt_time(&setfile.basic_info.in.create_time, t + 9*30*24*60*60);
150         unix_to_nt_time(&setfile.basic_info.in.access_time, t + 6*30*24*60*60);
151         unix_to_nt_time(&setfile.basic_info.in.write_time,  t + 3*30*24*60*60);
152         unix_to_nt_time(&setfile.basic_info.in.change_time, t + 1*30*24*60*60);
153         setfile.basic_info.in.attrib      = FILE_ATTRIBUTE_NORMAL;
154
155         status = smb2_setinfo_file(tree, &setfile);
156         if (!NT_STATUS_IS_OK(status)) {
157                 printf("Failed to setup file times - %s\n", nt_errstr(status));
158                 return status;
159         }
160
161         /* make sure all the timestamps aren't the same */
162         fileinfo.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
163         fileinfo.generic.in.file.handle = *handle;
164
165         status = smb2_getinfo_file(tree, tree, &fileinfo);
166         if (!NT_STATUS_IS_OK(status)) {
167                 printf("Failed to query file times - %s\n", nt_errstr(status));
168                 return status;
169                 
170         }
171
172 #define CHECK_TIME(field) do {\
173         if (setfile.basic_info.in.field != fileinfo.all_info2.out.field) { \
174                 printf("(%s) " #field " not setup correctly: %s(%llu) => %s(%llu)\n", \
175                         __location__, \
176                         nt_time_string(tree, setfile.basic_info.in.field), \
177                         (unsigned long long)setfile.basic_info.in.field, \
178                         nt_time_string(tree, fileinfo.basic_info.out.field), \
179                         (unsigned long long)fileinfo.basic_info.out.field); \
180                 status = NT_STATUS_INVALID_PARAMETER; \
181         } \
182 } while (0)
183
184         CHECK_TIME(create_time);
185         CHECK_TIME(access_time);
186         CHECK_TIME(write_time);
187         CHECK_TIME(change_time);
188
189         return status;
190 }
191
192 /*
193   create a complex file using the SMB2 protocol
194 */
195 NTSTATUS smb2_create_complex_file(struct smb2_tree *tree, const char *fname, 
196                                          struct smb2_handle *handle)
197 {
198         return smb2_create_complex(tree, fname, handle, False);
199 }
200
201 /*
202   create a complex dir using the SMB2 protocol
203 */
204 NTSTATUS smb2_create_complex_dir(struct smb2_tree *tree, const char *fname, 
205                                  struct smb2_handle *handle)
206 {
207         return smb2_create_complex(tree, fname, handle, True);
208 }
209
210 /*
211   show lots of information about a file
212 */
213 void torture_smb2_all_info(struct smb2_tree *tree, struct smb2_handle handle)
214 {
215         NTSTATUS status;
216         TALLOC_CTX *tmp_ctx = talloc_new(tree);
217         union smb_fileinfo io;
218
219         io.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
220         io.generic.in.file.handle = handle;
221
222         status = smb2_getinfo_file(tree, tmp_ctx, &io);
223         if (!NT_STATUS_IS_OK(status)) {
224                 DEBUG(0,("getinfo failed - %s\n", nt_errstr(status)));
225                 talloc_free(tmp_ctx);
226                 return;
227         }
228
229         d_printf("all_info for '%s'\n", io.all_info2.out.fname.s);
230         d_printf("\tcreate_time:    %s\n", nt_time_string(tmp_ctx, io.all_info2.out.create_time));
231         d_printf("\taccess_time:    %s\n", nt_time_string(tmp_ctx, io.all_info2.out.access_time));
232         d_printf("\twrite_time:     %s\n", nt_time_string(tmp_ctx, io.all_info2.out.write_time));
233         d_printf("\tchange_time:    %s\n", nt_time_string(tmp_ctx, io.all_info2.out.change_time));
234         d_printf("\tattrib:         0x%x\n", io.all_info2.out.attrib);
235         d_printf("\tunknown1:       0x%x\n", io.all_info2.out.unknown1);
236         d_printf("\talloc_size:     %llu\n", (long long)io.all_info2.out.alloc_size);
237         d_printf("\tsize:           %llu\n", (long long)io.all_info2.out.size);
238         d_printf("\tnlink:          %u\n", io.all_info2.out.nlink);
239         d_printf("\tdelete_pending: %u\n", io.all_info2.out.delete_pending);
240         d_printf("\tdirectory:      %u\n", io.all_info2.out.directory);
241         d_printf("\tfile_id:        %llu\n", (long long)io.all_info2.out.file_id);
242         d_printf("\tea_size:        %u\n", io.all_info2.out.ea_size);
243         d_printf("\taccess_mask:    0x%08x\n", io.all_info2.out.access_mask);
244         d_printf("\tposition:       0x%llx\n", (long long)io.all_info2.out.position);
245         d_printf("\tmode:           0x%llx\n", (long long)io.all_info2.out.mode);
246
247         /* short name, if any */
248         io.generic.level = RAW_FILEINFO_ALT_NAME_INFORMATION;
249         status = smb2_getinfo_file(tree, tmp_ctx, &io);
250         if (NT_STATUS_IS_OK(status)) {
251                 d_printf("\tshort name:     '%s'\n", io.alt_name_info.out.fname.s);
252         }
253
254         /* the EAs, if any */
255         io.generic.level = RAW_FILEINFO_SMB2_ALL_EAS;
256         status = smb2_getinfo_file(tree, tmp_ctx, &io);
257         if (NT_STATUS_IS_OK(status)) {
258                 int i;
259                 for (i=0;i<io.all_eas.out.num_eas;i++) {
260                         d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
261                                  io.all_eas.out.eas[i].flags,
262                                  (int)io.all_eas.out.eas[i].value.length,
263                                  io.all_eas.out.eas[i].name.s);
264                 }
265         }
266
267         /* streams, if available */
268         io.generic.level = RAW_FILEINFO_STREAM_INFORMATION;
269         status = smb2_getinfo_file(tree, tmp_ctx, &io);
270         if (NT_STATUS_IS_OK(status)) {
271                 int i;
272                 for (i=0;i<io.stream_info.out.num_streams;i++) {
273                         d_printf("\tstream %d:\n", i);
274                         d_printf("\t\tsize       %ld\n", 
275                                  (long)io.stream_info.out.streams[i].size);
276                         d_printf("\t\talloc size %ld\n", 
277                                  (long)io.stream_info.out.streams[i].alloc_size);
278                         d_printf("\t\tname       %s\n", io.stream_info.out.streams[i].stream_name.s);
279                 }
280         }       
281
282         if (DEBUGLVL(1)) {
283                 /* the security descriptor */
284                 io.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
285                 io.query_secdesc.in.secinfo_flags = 
286                         SECINFO_OWNER|SECINFO_GROUP|
287                         SECINFO_DACL;
288                 status = smb2_getinfo_file(tree, tmp_ctx, &io);
289                 if (NT_STATUS_IS_OK(status)) {
290                         NDR_PRINT_DEBUG(security_descriptor, io.query_secdesc.out.sd);
291                 }
292         }
293
294         talloc_free(tmp_ctx);   
295 }
296
297
298 /*
299   open a smb2 connection
300 */
301 BOOL torture_smb2_connection(TALLOC_CTX *mem_ctx, struct smb2_tree **tree)
302 {
303         NTSTATUS status;
304         const char *host = lp_parm_string(-1, "torture", "host");
305         const char *share = lp_parm_string(-1, "torture", "share");
306         struct cli_credentials *credentials = cmdline_credentials;
307
308         status = smb2_connect(mem_ctx, host, share, credentials, tree, 
309                               event_context_find(mem_ctx));
310         if (!NT_STATUS_IS_OK(status)) {
311                 printf("Failed to connect to SMB2 share \\\\%s\\%s - %s\n",
312                        host, share, nt_errstr(status));
313                 return False;
314         }
315         return True;
316 }
317
318
319 /*
320   create and return a handle to a test file
321 */
322 NTSTATUS torture_smb2_testfile(struct smb2_tree *tree, const char *fname, 
323                                struct smb2_handle *handle)
324 {
325         struct smb2_create io;
326         struct smb2_read r;
327         NTSTATUS status;
328
329         ZERO_STRUCT(io);
330         io.in.oplock_flags = 0;
331         io.in.access_mask = SEC_RIGHTS_FILE_ALL;
332         io.in.file_attr   = FILE_ATTRIBUTE_NORMAL;
333         io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
334         io.in.share_access = 
335                 NTCREATEX_SHARE_ACCESS_DELETE|
336                 NTCREATEX_SHARE_ACCESS_READ|
337                 NTCREATEX_SHARE_ACCESS_WRITE;
338         io.in.create_options = 0;
339         io.in.fname = fname;
340
341         status = smb2_create(tree, tree, &io);
342         NT_STATUS_NOT_OK_RETURN(status);
343
344         *handle = io.out.file.handle;
345
346         ZERO_STRUCT(r);
347         r.in.file.handle = *handle;
348         r.in.length      = 5;
349         r.in.offset      = 0;
350
351         smb2_read(tree, tree, &r);
352
353         return NT_STATUS_OK;
354 }
355
356 /*
357   create and return a handle to a test directory
358 */
359 NTSTATUS torture_smb2_testdir(struct smb2_tree *tree, const char *fname, 
360                               struct smb2_handle *handle)
361 {
362         struct smb2_create io;
363         NTSTATUS status;
364
365         ZERO_STRUCT(io);
366         io.in.oplock_flags = 0;
367         io.in.access_mask = SEC_RIGHTS_DIR_ALL;
368         io.in.file_attr   = FILE_ATTRIBUTE_DIRECTORY;
369         io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
370         io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE;
371         io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
372         io.in.fname = fname;
373
374         status = smb2_create(tree, tree, &io);
375         NT_STATUS_NOT_OK_RETURN(status);
376
377         *handle = io.out.file.handle;
378
379         return NT_STATUS_OK;
380 }
381
382
383 /*
384   create a complex file using the old SMB protocol, to make it easier to 
385   find fields in SMB2 getinfo levels
386 */
387 NTSTATUS torture_setup_complex_file(struct smb2_tree *tree, const char *fname)
388 {
389         struct smb2_handle handle;
390         NTSTATUS status = smb2_create_complex_file(tree, fname, &handle);
391         NT_STATUS_NOT_OK_RETURN(status);
392         return smb2_util_close(tree, handle);
393 }
394
395
396 /*
397   create a complex dir using the old SMB protocol, to make it easier to 
398   find fields in SMB2 getinfo levels
399 */
400 NTSTATUS torture_setup_complex_dir(struct smb2_tree *tree, const char *fname)
401 {
402         struct smb2_handle handle;
403         NTSTATUS status = smb2_create_complex_dir(tree, fname, &handle);
404         NT_STATUS_NOT_OK_RETURN(status);
405         return smb2_util_close(tree, handle);
406 }
407
408
409 /*
410   return a handle to the root of the share
411 */
412 NTSTATUS smb2_util_roothandle(struct smb2_tree *tree, struct smb2_handle *handle)
413 {
414         struct smb2_create io;
415         NTSTATUS status;
416
417         ZERO_STRUCT(io);
418         io.in.oplock_flags = 0;
419         io.in.access_mask = SEC_STD_SYNCHRONIZE | SEC_DIR_READ_ATTRIBUTE | SEC_DIR_LIST;
420         io.in.file_attr   = 0;
421         io.in.open_disposition = NTCREATEX_DISP_OPEN;
422         io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE;
423         io.in.create_options = NTCREATEX_OPTIONS_ASYNC_ALERT;
424         io.in.fname = "";
425
426         status = smb2_create(tree, tree, &io);
427         NT_STATUS_NOT_OK_RETURN(status);
428
429         *handle = io.out.file.handle;
430
431         return NT_STATUS_OK;
432 }