Line data Source code
1 : /*
2 : * Unix SMB/CIFS implementation.
3 : * Test pthreadpool_tevent
4 : * Copyright (C) Volker Lendecke 2018
5 : *
6 : * This program is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation; either version 3 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include "includes.h"
21 : #include "torture/proto.h"
22 : #include "libsmb/libsmb.h"
23 : #include "libcli/security/security.h"
24 :
25 0 : static NTSTATUS servertime(
26 : struct cli_state *cli, const char *fname, struct timeval *tv)
27 : {
28 : struct smb_create_returns cr;
29 : NTSTATUS status;
30 : uint16_t fnum;
31 :
32 0 : status = cli_ntcreate(
33 : cli,
34 : fname,
35 : 0,
36 : FILE_GENERIC_WRITE|DELETE_ACCESS,
37 : FILE_ATTRIBUTE_NORMAL,
38 : 0,
39 : FILE_CREATE,
40 : FILE_DELETE_ON_CLOSE,
41 : 0,
42 : &fnum,
43 : &cr);
44 0 : if (!NT_STATUS_IS_OK(status)) {
45 0 : d_printf("cli_ntcreate failed: %s\n", nt_errstr(status));
46 0 : return status;
47 : }
48 :
49 0 : status = cli_close(cli, fnum);
50 0 : if (!NT_STATUS_IS_OK(status)) {
51 0 : d_printf("cli_close failed: %s\n", nt_errstr(status));
52 0 : return status;
53 : }
54 :
55 0 : nttime_to_timeval(tv, cr.creation_time);
56 :
57 0 : return NT_STATUS_OK;
58 : }
59 :
60 : struct have_file_state {
61 : bool found;
62 : const char *fname;
63 : };
64 :
65 0 : static NTSTATUS have_file_fn(struct file_info *f,
66 : const char *mask,
67 : void *private_data)
68 : {
69 0 : struct have_file_state *state = private_data;
70 0 : state->found |= strequal(f->name, state->fname);
71 0 : return NT_STATUS_OK;
72 : }
73 :
74 0 : static bool have_file(struct cli_state *cli, const char *fname)
75 : {
76 0 : struct have_file_state state = { .fname = fname };
77 : NTSTATUS status;
78 :
79 0 : status = cli_list(
80 : cli,
81 : "*.*",
82 : FILE_ATTRIBUTE_DIRECTORY|
83 : FILE_ATTRIBUTE_SYSTEM|
84 : FILE_ATTRIBUTE_HIDDEN,
85 : have_file_fn,
86 : &state);
87 0 : if (!NT_STATUS_IS_OK(status)) {
88 0 : d_printf("cli_list failed: %s\n", nt_errstr(status));
89 0 : return false;
90 : }
91 :
92 0 : return state.found;
93 : }
94 :
95 0 : bool run_hidenewfiles(int dummy)
96 : {
97 0 : const char *tsname = "timestamp.txt";
98 0 : const char *fname = "new_hidden.txt";
99 : struct cli_state *cli;
100 : struct smb_create_returns cr;
101 : struct timeval create_time;
102 : uint16_t fnum;
103 : NTSTATUS status;
104 0 : bool ret = false;
105 0 : bool gotit = false;
106 : bool ok;
107 :
108 : /* what is configure in smb.conf */
109 0 : unsigned hideunreadable_seconds = 5;
110 :
111 0 : ok = torture_open_connection(&cli, 0);
112 0 : if (!ok) {
113 0 : return false;
114 : }
115 :
116 0 : cli_unlink(cli, tsname, FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN);
117 0 : cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN);
118 :
119 0 : status = cli_ntcreate(
120 : cli,
121 : fname,
122 : 0,
123 : FILE_GENERIC_WRITE|DELETE_ACCESS,
124 : FILE_ATTRIBUTE_NORMAL,
125 : 0,
126 : FILE_CREATE,
127 : 0,
128 : 0,
129 : &fnum,
130 : &cr);
131 0 : if (!NT_STATUS_IS_OK(status)) {
132 0 : d_printf("cli_ntcreate failed: %s\n", nt_errstr(status));
133 0 : return false;
134 : }
135 0 : nttime_to_timeval(&create_time, cr.last_write_time);
136 :
137 0 : while (!gotit) {
138 : struct timeval now;
139 : double age;
140 :
141 0 : gotit = have_file(cli, fname);
142 :
143 0 : status = servertime(cli, tsname, &now);
144 0 : if (!NT_STATUS_IS_OK(status)) {
145 0 : d_printf("servertime failed: %s\n",
146 : nt_errstr(status));
147 0 : goto fail;
148 : }
149 0 : age = timeval_elapsed2(&create_time, &now);
150 :
151 0 : if ((age < hideunreadable_seconds) && gotit) {
152 0 : d_printf("Found file at age of %f\n", age);
153 0 : goto fail;
154 : }
155 0 : if ((age > (hideunreadable_seconds*10)) && !gotit) {
156 0 : d_printf("Did not find file after %f seconds\n", age);
157 0 : goto fail;
158 : }
159 0 : if (gotit) {
160 0 : break;
161 : }
162 :
163 0 : smb_msleep(1000);
164 : }
165 :
166 0 : ret = true;
167 0 : fail:
168 0 : cli_nt_delete_on_close(cli, fnum, true);
169 0 : cli_close(cli, fnum);
170 :
171 0 : return ret;
172 : }
|