2 Unix SMB/CIFS implementation.
4 SMB2 client utility functions
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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/>.
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/raw/raw_proto.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "libcli/smb_composite/smb_composite.h"
30 simple close wrapper with SMB2
32 NTSTATUS smb2_util_close(struct smb2_tree *tree, struct smb2_handle h)
39 return smb2_close(tree, &c);
43 unlink a file with SMB2
45 NTSTATUS smb2_util_unlink(struct smb2_tree *tree, const char *fname)
50 io.unlink.in.pattern = fname;
52 return smb2_composite_unlink(tree, &io);
59 NTSTATUS smb2_util_rmdir(struct smb2_tree *tree, const char *dname)
66 return smb2_composite_rmdir(tree, &io);
73 NTSTATUS smb2_util_mkdir(struct smb2_tree *tree, const char *dname)
78 io.mkdir.level = RAW_MKDIR_MKDIR;
79 io.mkdir.in.path = dname;
81 return smb2_composite_mkdir(tree, &io);
86 set file attribute with SMB2
88 NTSTATUS smb2_util_setatr(struct smb2_tree *tree, const char *name, uint32_t attrib)
90 union smb_setfileinfo io;
93 io.basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION;
94 io.basic_info.in.file.path = name;
95 io.basic_info.in.attrib = attrib;
97 return smb2_composite_setpathinfo(tree, &io);
104 recursively descend a tree deleting all files
105 returns the number of files deleted, or -1 on error
107 int smb2_deltree(struct smb2_tree *tree, const char *dname)
110 uint32_t total_deleted = 0;
112 union smb_search_data *list;
113 TALLOC_CTX *tmp_ctx = talloc_new(tree);
115 struct smb2_create create_parm;
117 /* it might be a file */
118 status = smb2_util_unlink(tree, dname);
119 if (NT_STATUS_IS_OK(status)) {
120 talloc_free(tmp_ctx);
123 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) ||
124 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_PATH_NOT_FOUND) ||
125 NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_FILE)) {
126 talloc_free(tmp_ctx);
130 if (NT_STATUS_EQUAL(status, NT_STATUS_CANNOT_DELETE)) {
131 /* it could be read-only */
132 status = smb2_util_setatr(tree, dname, FILE_ATTRIBUTE_NORMAL);
133 status = smb2_util_unlink(tree, dname);
135 if (NT_STATUS_IS_OK(status)) {
136 talloc_free(tmp_ctx);
140 ZERO_STRUCT(create_parm);
141 create_parm.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
142 create_parm.in.share_access =
143 NTCREATEX_SHARE_ACCESS_READ|
144 NTCREATEX_SHARE_ACCESS_WRITE;
145 create_parm.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
146 create_parm.in.create_disposition = NTCREATEX_DISP_OPEN;
147 create_parm.in.fname = dname;
149 status = smb2_create(tree, tmp_ctx, &create_parm);
150 if (NT_STATUS_IS_ERR(status)) {
151 DEBUG(2,("Failed to open %s - %s\n", dname, nt_errstr(status)));
152 talloc_free(tmp_ctx);
158 f.in.file.handle = create_parm.out.file.handle;
159 f.in.max_response_size = 0x10000;
160 f.in.level = SMB2_FIND_NAME_INFO;
163 status = smb2_find_level(tree, tmp_ctx, &f, &count, &list);
164 if (NT_STATUS_IS_ERR(status)) {
165 DEBUG(2,("Failed to list %s - %s\n",
166 dname, nt_errstr(status)));
167 smb2_util_close(tree, create_parm.out.file.handle);
168 talloc_free(tmp_ctx);
172 for (i=0;i<count;i++) {
174 if (strcmp(".", list[i].name_info.name.s) == 0 ||
175 strcmp("..", list[i].name_info.name.s) == 0) {
178 name = talloc_asprintf(tmp_ctx, "%s\\%s", dname, list[i].name_info.name.s);
179 status = smb2_util_unlink(tree, name);
180 if (NT_STATUS_EQUAL(status, NT_STATUS_CANNOT_DELETE)) {
181 /* it could be read-only */
182 status = smb2_util_setatr(tree, name, FILE_ATTRIBUTE_NORMAL);
183 status = smb2_util_unlink(tree, name);
186 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
188 ret = smb2_deltree(tree, name);
189 if (ret > 0) total_deleted += ret;
192 if (NT_STATUS_IS_OK(status)) {
197 smb2_util_close(tree, create_parm.out.file.handle);
199 status = smb2_util_rmdir(tree, dname);
200 if (NT_STATUS_IS_ERR(status)) {
201 DEBUG(2,("Failed to delete %s - %s\n",
202 dname, nt_errstr(status)));
203 talloc_free(tmp_ctx);
207 talloc_free(tmp_ctx);
209 return total_deleted;