| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 |
|
|---|
| 4 | SMB2 client utility functions
|
|---|
| 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/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"
|
|---|
| 28 |
|
|---|
| 29 | /*
|
|---|
| 30 | simple close wrapper with SMB2
|
|---|
| 31 | */
|
|---|
| 32 | NTSTATUS smb2_util_close(struct smb2_tree *tree, struct smb2_handle h)
|
|---|
| 33 | {
|
|---|
| 34 | struct smb2_close c;
|
|---|
| 35 |
|
|---|
| 36 | ZERO_STRUCT(c);
|
|---|
| 37 | c.in.file.handle = h;
|
|---|
| 38 |
|
|---|
| 39 | return smb2_close(tree, &c);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /*
|
|---|
| 43 | unlink a file with SMB2
|
|---|
| 44 | */
|
|---|
| 45 | NTSTATUS smb2_util_unlink(struct smb2_tree *tree, const char *fname)
|
|---|
| 46 | {
|
|---|
| 47 | union smb_unlink io;
|
|---|
| 48 |
|
|---|
| 49 | ZERO_STRUCT(io);
|
|---|
| 50 | io.unlink.in.pattern = fname;
|
|---|
| 51 |
|
|---|
| 52 | return smb2_composite_unlink(tree, &io);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 | /*
|
|---|
| 57 | rmdir with SMB2
|
|---|
| 58 | */
|
|---|
| 59 | NTSTATUS smb2_util_rmdir(struct smb2_tree *tree, const char *dname)
|
|---|
| 60 | {
|
|---|
| 61 | struct smb_rmdir io;
|
|---|
| 62 |
|
|---|
| 63 | ZERO_STRUCT(io);
|
|---|
| 64 | io.in.path = dname;
|
|---|
| 65 |
|
|---|
| 66 | return smb2_composite_rmdir(tree, &io);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 | /*
|
|---|
| 71 | mkdir with SMB2
|
|---|
| 72 | */
|
|---|
| 73 | NTSTATUS smb2_util_mkdir(struct smb2_tree *tree, const char *dname)
|
|---|
| 74 | {
|
|---|
| 75 | union smb_mkdir io;
|
|---|
| 76 |
|
|---|
| 77 | ZERO_STRUCT(io);
|
|---|
| 78 | io.mkdir.level = RAW_MKDIR_MKDIR;
|
|---|
| 79 | io.mkdir.in.path = dname;
|
|---|
| 80 |
|
|---|
| 81 | return smb2_composite_mkdir(tree, &io);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 | /*
|
|---|
| 86 | set file attribute with SMB2
|
|---|
| 87 | */
|
|---|
| 88 | NTSTATUS smb2_util_setatr(struct smb2_tree *tree, const char *name, uint32_t attrib)
|
|---|
| 89 | {
|
|---|
| 90 | union smb_setfileinfo io;
|
|---|
| 91 |
|
|---|
| 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;
|
|---|
| 96 |
|
|---|
| 97 | return smb2_composite_setpathinfo(tree, &io);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | /*
|
|---|
| 104 | recursively descend a tree deleting all files
|
|---|
| 105 | returns the number of files deleted, or -1 on error
|
|---|
| 106 | */
|
|---|
| 107 | int smb2_deltree(struct smb2_tree *tree, const char *dname)
|
|---|
| 108 | {
|
|---|
| 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;
|
|---|
| 117 |
|
|---|
| 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;
|
|---|
| 123 | }
|
|---|
| 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;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 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);
|
|---|
| 135 | }
|
|---|
| 136 | if (NT_STATUS_IS_OK(status)) {
|
|---|
| 137 | talloc_free(tmp_ctx);
|
|---|
| 138 | return 1;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 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;
|
|---|
| 149 |
|
|---|
| 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;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 | do {
|
|---|
| 159 | did_delete = false;
|
|---|
| 160 |
|
|---|
| 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 = "*";
|
|---|
| 166 |
|
|---|
| 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;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 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;
|
|---|
| 181 | }
|
|---|
| 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);
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 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;
|
|---|
| 194 | }
|
|---|
| 195 | talloc_free(name);
|
|---|
| 196 | if (NT_STATUS_IS_OK(status)) {
|
|---|
| 197 | total_deleted++;
|
|---|
| 198 | did_delete = true;
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 | } while (did_delete);
|
|---|
| 202 |
|
|---|
| 203 | smb2_util_close(tree, create_parm.out.file.handle);
|
|---|
| 204 |
|
|---|
| 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);
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 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;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | talloc_free(tmp_ctx);
|
|---|
| 220 |
|
|---|
| 221 | return total_deleted;
|
|---|
| 222 | }
|
|---|