Ignoring generated files:
[Samba/aatanasov.git] / source4 / libcli / smb2 / util.c
blob8602c91a9f670f28f7474a030f7589ce0239a17f
1 /*
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/>.
22 #include "includes.h"
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)
34 struct smb2_close c;
36 ZERO_STRUCT(c);
37 c.in.file.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)
47 union smb_unlink io;
49 ZERO_STRUCT(io);
50 io.unlink.in.pattern = fname;
52 return smb2_composite_unlink(tree, &io);
57 rmdir with SMB2
59 NTSTATUS smb2_util_rmdir(struct smb2_tree *tree, const char *dname)
61 struct smb_rmdir io;
63 ZERO_STRUCT(io);
64 io.in.path = dname;
66 return smb2_composite_rmdir(tree, &io);
71 mkdir with SMB2
73 NTSTATUS smb2_util_mkdir(struct smb2_tree *tree, const char *dname)
75 union smb_mkdir io;
77 ZERO_STRUCT(io);
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;
92 ZERO_STRUCT(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)
109 NTSTATUS status;
110 uint32_t total_deleted = 0;
111 uint_t count, i;
112 union smb_search_data *list;
113 TALLOC_CTX *tmp_ctx = talloc_new(tree);
114 struct smb2_find f;
115 struct smb2_create create_parm;
116 bool did_delete;
118 /* it might be a file */
119 status = smb2_util_unlink(tree, dname);
120 if (NT_STATUS_IS_OK(status)) {
121 talloc_free(tmp_ctx);
122 return 1;
124 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) ||
125 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_PATH_NOT_FOUND) ||
126 NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_FILE)) {
127 talloc_free(tmp_ctx);
128 return 0;
131 if (NT_STATUS_EQUAL(status, NT_STATUS_CANNOT_DELETE)) {
132 /* it could be read-only */
133 status = smb2_util_setatr(tree, dname, FILE_ATTRIBUTE_NORMAL);
134 status = smb2_util_unlink(tree, dname);
136 if (NT_STATUS_IS_OK(status)) {
137 talloc_free(tmp_ctx);
138 return 1;
141 ZERO_STRUCT(create_parm);
142 create_parm.in.desired_access = SEC_FILE_READ_DATA;
143 create_parm.in.share_access =
144 NTCREATEX_SHARE_ACCESS_READ|
145 NTCREATEX_SHARE_ACCESS_WRITE;
146 create_parm.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
147 create_parm.in.create_disposition = NTCREATEX_DISP_OPEN;
148 create_parm.in.fname = dname;
150 status = smb2_create(tree, tmp_ctx, &create_parm);
151 if (NT_STATUS_IS_ERR(status)) {
152 DEBUG(2,("Failed to open %s - %s\n", dname, nt_errstr(status)));
153 talloc_free(tmp_ctx);
154 return -1;
158 do {
159 did_delete = false;
161 ZERO_STRUCT(f);
162 f.in.file.handle = create_parm.out.file.handle;
163 f.in.max_response_size = 0x10000;
164 f.in.level = SMB2_FIND_NAME_INFO;
165 f.in.pattern = "*";
167 status = smb2_find_level(tree, tmp_ctx, &f, &count, &list);
168 if (NT_STATUS_IS_ERR(status)) {
169 DEBUG(2,("Failed to list %s - %s\n",
170 dname, nt_errstr(status)));
171 smb2_util_close(tree, create_parm.out.file.handle);
172 talloc_free(tmp_ctx);
173 return -1;
176 for (i=0;i<count;i++) {
177 char *name;
178 if (strcmp(".", list[i].name_info.name.s) == 0 ||
179 strcmp("..", list[i].name_info.name.s) == 0) {
180 continue;
182 name = talloc_asprintf(tmp_ctx, "%s\\%s", dname, list[i].name_info.name.s);
183 status = smb2_util_unlink(tree, name);
184 if (NT_STATUS_EQUAL(status, NT_STATUS_CANNOT_DELETE)) {
185 /* it could be read-only */
186 status = smb2_util_setatr(tree, name, FILE_ATTRIBUTE_NORMAL);
187 status = smb2_util_unlink(tree, name);
190 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
191 int ret;
192 ret = smb2_deltree(tree, name);
193 if (ret > 0) total_deleted += ret;
195 talloc_free(name);
196 if (NT_STATUS_IS_OK(status)) {
197 total_deleted++;
198 did_delete = true;
201 } while (did_delete);
203 smb2_util_close(tree, create_parm.out.file.handle);
205 status = smb2_util_rmdir(tree, dname);
206 if (NT_STATUS_EQUAL(status, NT_STATUS_CANNOT_DELETE)) {
207 /* it could be read-only */
208 status = smb2_util_setatr(tree, dname, FILE_ATTRIBUTE_NORMAL);
209 status = smb2_util_rmdir(tree, dname);
212 if (NT_STATUS_IS_ERR(status)) {
213 DEBUG(2,("Failed to delete %s - %s\n",
214 dname, nt_errstr(status)));
215 talloc_free(tmp_ctx);
216 return -1;
219 talloc_free(tmp_ctx);
221 return total_deleted;