summaryrefslogtreecommitdiff
path: root/source3/torture/test_hidenewfiles.c
blob: 2f1638a6ae7faba7f4b440c0abe801d8b470a377 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Unix SMB/CIFS implementation.
 * Test pthreadpool_tevent
 * Copyright (C) Volker Lendecke 2018
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include "torture/proto.h"
#include "libsmb/libsmb.h"
#include "libcli/security/security.h"

static NTSTATUS servertime(
	struct cli_state *cli, const char *fname, struct timeval *tv)
{
	struct smb_create_returns cr;
	NTSTATUS status;
	uint16_t fnum;

	status = cli_ntcreate(
		cli,
		fname,
		0,
		FILE_GENERIC_WRITE|DELETE_ACCESS,
		FILE_ATTRIBUTE_NORMAL,
		0,
		FILE_CREATE,
		FILE_DELETE_ON_CLOSE,
		0,
		&fnum,
		&cr);
	if (!NT_STATUS_IS_OK(status)) {
		d_printf("cli_ntcreate failed: %s\n", nt_errstr(status));
		return status;
	}

	status = cli_close(cli, fnum);
	if (!NT_STATUS_IS_OK(status)) {
		d_printf("cli_close failed: %s\n", nt_errstr(status));
		return status;
	}

	nttime_to_timeval(tv, cr.creation_time);

	return NT_STATUS_OK;
}

struct have_file_state {
	bool found;
	const char *fname;
};

static NTSTATUS have_file_fn(const char *mntpoint,
			     struct file_info *f,
			     const char *mask,
			     void *private_data)
{
	struct have_file_state *state = private_data;
	state->found |= strequal(f->name, state->fname);
	return NT_STATUS_OK;
}

static bool have_file(struct cli_state *cli, const char *fname)
{
	struct have_file_state state = { .fname = fname };
	NTSTATUS status;

	status = cli_list(
		cli,
		"*.*",
		FILE_ATTRIBUTE_DIRECTORY|
		FILE_ATTRIBUTE_SYSTEM|
		FILE_ATTRIBUTE_HIDDEN,
		have_file_fn,
		&state);
	if (!NT_STATUS_IS_OK(status)) {
		d_printf("cli_list failed: %s\n", nt_errstr(status));
		return false;
	}

	return state.found;
}

bool run_hidenewfiles(int dummy)
{
	const char *tsname = "timestamp.txt";
	const char *fname = "new_hidden.txt";
	struct cli_state *cli;
	struct smb_create_returns cr;
	struct timeval create_time;
	uint16_t fnum;
	NTSTATUS status;
	bool ret = false;
	bool gotit = false;
	bool ok;

	/* what is configure in smb.conf */
	unsigned hideunreadable_seconds = 5;

	ok = torture_open_connection(&cli, 0);
	if (!ok) {
		return false;
	}

	cli_unlink(cli, tsname, FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN);
	cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN);

	status = cli_ntcreate(
		cli,
		fname,
		0,
		FILE_GENERIC_WRITE|DELETE_ACCESS,
		FILE_ATTRIBUTE_NORMAL,
		0,
		FILE_CREATE,
		0,
		0,
		&fnum,
		&cr);
	if (!NT_STATUS_IS_OK(status)) {
		d_printf("cli_ntcreate failed: %s\n", nt_errstr(status));
		return false;
	}
	nttime_to_timeval(&create_time, cr.last_write_time);

	while (!gotit) {
		struct timeval now;
		double age;

		gotit = have_file(cli, fname);

		status = servertime(cli, tsname, &now);
		if (!NT_STATUS_IS_OK(status)) {
			d_printf("servertime failed: %s\n",
				 nt_errstr(status));
			goto fail;
		}
		age = timeval_elapsed2(&create_time, &now);

		if ((age < hideunreadable_seconds) && gotit) {
			d_printf("Found file at age of %f\n", age);
			goto fail;
		}
		if ((age > (hideunreadable_seconds*10)) && !gotit) {
			d_printf("Did not find file after %f seconds\n", age);
			goto fail;
		}
		if (gotit) {
			break;
		}

		smb_msleep(1000);
	}

	ret = true;
fail:
	cli_nt_delete_on_close(cli, fnum, true);
	cli_close(cli, fnum);

	return ret;
}