vfs_io_uring: move error handling out of vfs_io_uring_pread_recv()
[Samba.git] / source3 / libsmb / clisymlink.c
blob1330752358d0af08c8e038b5568b6afba7001585
1 /*
2 * Unix SMB/CIFS implementation.
3 * Client implementation of setting symlinks using reparse points
4 * Copyright (C) Volker Lendecke 2011
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.
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.
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/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "libsmb/libsmb.h"
23 #include "../lib/util/tevent_ntstatus.h"
24 #include "async_smb.h"
25 #include "libsmb/clirap.h"
26 #include "trans2.h"
27 #include "libcli/security/secdesc.h"
28 #include "libcli/security/security.h"
29 #include "../libcli/smb/smbXcli_base.h"
31 struct cli_symlink_state {
32 struct tevent_context *ev;
33 struct cli_state *cli;
34 const char *link_target;
35 const char *newpath;
36 uint32_t flags;
38 uint16_t fnum;
40 uint16_t setup[4];
41 NTSTATUS set_reparse_status;
44 static void cli_symlink_create_done(struct tevent_req *subreq);
45 static void cli_symlink_set_reparse_done(struct tevent_req *subreq);
46 static void cli_symlink_delete_on_close_done(struct tevent_req *subreq);
47 static void cli_symlink_close_done(struct tevent_req *subreq);
49 struct tevent_req *cli_symlink_send(TALLOC_CTX *mem_ctx,
50 struct tevent_context *ev,
51 struct cli_state *cli,
52 const char *link_target,
53 const char *newpath,
54 uint32_t flags)
56 struct tevent_req *req, *subreq;
57 struct cli_symlink_state *state;
59 req = tevent_req_create(mem_ctx, &state, struct cli_symlink_state);
60 if (req == NULL) {
61 return NULL;
63 state->ev = ev;
64 state->cli = cli;
65 state->link_target = link_target;
66 state->newpath = newpath;
67 state->flags = flags;
69 subreq = cli_ntcreate_send(
70 state, ev, cli, state->newpath, 0,
71 SYNCHRONIZE_ACCESS|DELETE_ACCESS|
72 FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES,
73 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_CREATE,
74 FILE_OPEN_REPARSE_POINT|FILE_SYNCHRONOUS_IO_NONALERT|
75 FILE_NON_DIRECTORY_FILE,
76 SMB2_IMPERSONATION_IMPERSONATION, 0);
77 if (tevent_req_nomem(subreq, req)) {
78 return tevent_req_post(req, ev);
80 tevent_req_set_callback(subreq, cli_symlink_create_done, req);
81 return req;
84 static void cli_symlink_create_done(struct tevent_req *subreq)
86 struct tevent_req *req = tevent_req_callback_data(
87 subreq, struct tevent_req);
88 struct cli_symlink_state *state = tevent_req_data(
89 req, struct cli_symlink_state);
90 DATA_BLOB data;
91 NTSTATUS status;
93 status = cli_ntcreate_recv(subreq, &state->fnum, NULL);
94 TALLOC_FREE(subreq);
95 if (tevent_req_nterror(req, status)) {
96 return;
99 if (!symlink_reparse_buffer_marshall(
100 state->link_target, NULL, state->flags, state,
101 &data.data, &data.length)) {
102 tevent_req_oom(req);
103 return;
106 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
107 subreq = cli_smb2_set_reparse_point_fnum_send(state,
108 state->ev,
109 state->cli,
110 state->fnum,
111 data);
112 } else {
113 SIVAL(state->setup, 0, FSCTL_SET_REPARSE_POINT);
114 SSVAL(state->setup, 4, state->fnum);
115 SCVAL(state->setup, 6, 1); /* IsFcntl */
116 SCVAL(state->setup, 7, 0); /* IsFlags */
119 subreq = cli_trans_send(state, state->ev, state->cli, 0,
120 SMBnttrans,
121 NULL, -1, /* name, fid */
122 NT_TRANSACT_IOCTL, 0,
123 state->setup, 4, 0, /* setup */
124 NULL, 0, 0, /* param */
125 data.data, data.length, 0); /* data */
128 if (tevent_req_nomem(subreq, req)) {
129 return;
131 tevent_req_set_callback(subreq, cli_symlink_set_reparse_done, req);
134 static void cli_symlink_set_reparse_done(struct tevent_req *subreq)
136 struct tevent_req *req = tevent_req_callback_data(
137 subreq, struct tevent_req);
138 struct cli_symlink_state *state = tevent_req_data(
139 req, struct cli_symlink_state);
141 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
142 state->set_reparse_status =
143 cli_smb2_set_reparse_point_fnum_recv(subreq);
144 } else {
145 state->set_reparse_status = cli_trans_recv(
146 subreq, NULL, NULL,
147 NULL, 0, NULL, /* rsetup */
148 NULL, 0, NULL, /* rparam */
149 NULL, 0, NULL); /* rdata */
151 TALLOC_FREE(subreq);
153 if (NT_STATUS_IS_OK(state->set_reparse_status)) {
154 subreq = cli_close_send(state, state->ev, state->cli,
155 state->fnum);
156 if (tevent_req_nomem(subreq, req)) {
157 return;
159 tevent_req_set_callback(subreq, cli_symlink_close_done, req);
160 return;
162 subreq = cli_nt_delete_on_close_send(
163 state, state->ev, state->cli, state->fnum, true);
164 if (tevent_req_nomem(subreq, req)) {
165 return;
167 tevent_req_set_callback(subreq, cli_symlink_delete_on_close_done, req);
170 static void cli_symlink_delete_on_close_done(struct tevent_req *subreq)
172 struct tevent_req *req = tevent_req_callback_data(
173 subreq, struct tevent_req);
174 struct cli_symlink_state *state = tevent_req_data(
175 req, struct cli_symlink_state);
178 * Ignore status, we can't do much anyway in case of failure
181 (void)cli_nt_delete_on_close_recv(subreq);
182 TALLOC_FREE(subreq);
184 subreq = cli_close_send(state, state->ev, state->cli, state->fnum);
185 if (tevent_req_nomem(subreq, req)) {
186 return;
188 tevent_req_set_callback(subreq, cli_symlink_close_done, req);
191 static void cli_symlink_close_done(struct tevent_req *subreq)
193 struct tevent_req *req = tevent_req_callback_data(
194 subreq, struct tevent_req);
195 struct cli_symlink_state *state = tevent_req_data(
196 req, struct cli_symlink_state);
197 NTSTATUS status;
199 status = cli_close_recv(subreq);
200 TALLOC_FREE(subreq);
202 if (tevent_req_nterror(req, status)) {
203 return;
205 if (tevent_req_nterror(req, state->set_reparse_status)) {
206 return;
208 tevent_req_done(req);
211 NTSTATUS cli_symlink_recv(struct tevent_req *req)
213 return tevent_req_simple_recv_ntstatus(req);
216 NTSTATUS cli_symlink(struct cli_state *cli, const char *link_target,
217 const char *newname, uint32_t flags)
219 TALLOC_CTX *frame = talloc_stackframe();
220 struct tevent_context *ev;
221 struct tevent_req *req;
222 NTSTATUS status = NT_STATUS_NO_MEMORY;
224 if (smbXcli_conn_has_async_calls(cli->conn)) {
225 status = NT_STATUS_INVALID_PARAMETER;
226 goto fail;
228 ev = samba_tevent_context_init(frame);
229 if (ev == NULL) {
230 goto fail;
232 req = cli_symlink_send(frame, ev, cli, link_target, newname, flags);
233 if (req == NULL) {
234 goto fail;
236 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
237 goto fail;
239 status = cli_symlink_recv(req);
240 fail:
241 TALLOC_FREE(frame);
242 return status;
245 struct cli_readlink_state {
246 struct tevent_context *ev;
247 struct cli_state *cli;
248 uint16_t fnum;
250 uint16_t setup[4];
251 NTSTATUS get_reparse_status;
252 uint8_t *data;
253 uint32_t num_data;
256 static void cli_readlink_opened(struct tevent_req *subreq);
257 static void cli_readlink_got_reparse_data(struct tevent_req *subreq);
258 static void cli_readlink_closed(struct tevent_req *subreq);
260 struct tevent_req *cli_readlink_send(TALLOC_CTX *mem_ctx,
261 struct tevent_context *ev,
262 struct cli_state *cli,
263 const char *fname)
265 struct tevent_req *req, *subreq;
266 struct cli_readlink_state *state;
268 req = tevent_req_create(mem_ctx, &state, struct cli_readlink_state);
269 if (req == NULL) {
270 return NULL;
272 state->ev = ev;
273 state->cli = cli;
275 subreq = cli_ntcreate_send(
276 state, ev, cli, fname, 0, FILE_READ_ATTRIBUTES | FILE_READ_EA,
277 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
278 FILE_OPEN, FILE_OPEN_REPARSE_POINT,
279 SMB2_IMPERSONATION_IMPERSONATION, 0);
280 if (tevent_req_nomem(subreq, req)) {
281 return tevent_req_post(req, ev);
283 tevent_req_set_callback(subreq, cli_readlink_opened, req);
284 return req;
287 static void cli_readlink_opened(struct tevent_req *subreq)
289 struct tevent_req *req = tevent_req_callback_data(
290 subreq, struct tevent_req);
291 struct cli_readlink_state *state = tevent_req_data(
292 req, struct cli_readlink_state);
293 NTSTATUS status;
295 status = cli_ntcreate_recv(subreq, &state->fnum, NULL);
296 TALLOC_FREE(subreq);
297 if (tevent_req_nterror(req, status)) {
298 return;
301 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
302 subreq = cli_smb2_get_reparse_point_fnum_send(state,
303 state->ev,
304 state->cli,
305 state->fnum);
306 } else {
307 SIVAL(state->setup, 0, FSCTL_GET_REPARSE_POINT);
308 SSVAL(state->setup, 4, state->fnum);
309 SCVAL(state->setup, 6, 1); /* IsFcntl */
310 SCVAL(state->setup, 7, 0); /* IsFlags */
312 subreq = cli_trans_send(state, state->ev, state->cli,
313 0, SMBnttrans,
314 NULL, -1, /* name, fid */
315 NT_TRANSACT_IOCTL, 0,
316 state->setup, 4, 0, /* setup */
317 NULL, 0, 0, /* param */
318 NULL, 0, 16384); /* data */
321 if (tevent_req_nomem(subreq, req)) {
322 return;
324 tevent_req_set_callback(subreq, cli_readlink_got_reparse_data, req);
327 static void cli_readlink_got_reparse_data(struct tevent_req *subreq)
329 struct tevent_req *req = tevent_req_callback_data(
330 subreq, struct tevent_req);
331 struct cli_readlink_state *state = tevent_req_data(
332 req, struct cli_readlink_state);
334 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
335 DATA_BLOB recv_data;
336 state->get_reparse_status =
337 cli_smb2_get_reparse_point_fnum_recv(subreq,
338 state,
339 &recv_data);
340 if (NT_STATUS_IS_OK(state->get_reparse_status)) {
341 state->data = recv_data.data;
342 state->num_data = recv_data.length;
344 } else {
345 state->get_reparse_status = cli_trans_recv(
346 subreq, state, NULL,
347 NULL, 0, NULL, /* rsetup */
348 NULL, 0, NULL, /* rparam */
349 &state->data, 20, &state->num_data); /* rdata */
351 TALLOC_FREE(subreq);
353 subreq = cli_close_send(state, state->ev, state->cli, state->fnum);
354 if (tevent_req_nomem(subreq, req)) {
355 return;
357 tevent_req_set_callback(subreq, cli_readlink_closed, req);
360 static void cli_readlink_closed(struct tevent_req *subreq)
362 struct tevent_req *req = tevent_req_callback_data(
363 subreq, struct tevent_req);
364 NTSTATUS status;
366 status = cli_close_recv(subreq);
367 TALLOC_FREE(subreq);
368 if (tevent_req_nterror(req, status)) {
369 return;
371 tevent_req_done(req);
374 NTSTATUS cli_readlink_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
375 char **psubstitute_name, char **pprint_name,
376 uint32_t *pflags)
378 struct cli_readlink_state *state = tevent_req_data(
379 req, struct cli_readlink_state);
380 NTSTATUS status;
381 char *substitute_name;
382 char *print_name;
383 uint32_t flags;
385 if (tevent_req_is_nterror(req, &status)) {
386 return status;
389 if (!symlink_reparse_buffer_parse(state->data, state->num_data,
390 talloc_tos(), &substitute_name,
391 &print_name, &flags)) {
392 return NT_STATUS_INVALID_NETWORK_RESPONSE;
395 if (psubstitute_name != NULL) {
396 *psubstitute_name = talloc_move(mem_ctx, &substitute_name);
398 TALLOC_FREE(substitute_name);
400 if (pprint_name != NULL) {
401 *pprint_name = talloc_move(mem_ctx, &print_name);
403 TALLOC_FREE(print_name);
405 if (pflags != NULL) {
406 *pflags = flags;
408 return NT_STATUS_OK;
411 NTSTATUS cli_readlink(struct cli_state *cli, const char *fname,
412 TALLOC_CTX *mem_ctx, char **psubstitute_name,
413 char **pprint_name, uint32_t *pflags)
415 TALLOC_CTX *frame = talloc_stackframe();
416 struct tevent_context *ev;
417 struct tevent_req *req;
418 NTSTATUS status = NT_STATUS_NO_MEMORY;
420 if (smbXcli_conn_has_async_calls(cli->conn)) {
421 status = NT_STATUS_INVALID_PARAMETER;
422 goto fail;
424 ev = samba_tevent_context_init(frame);
425 if (ev == NULL) {
426 goto fail;
428 req = cli_readlink_send(frame, ev, cli, fname);
429 if (req == NULL) {
430 goto fail;
432 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
433 goto fail;
435 status = cli_readlink_recv(req, mem_ctx, psubstitute_name,
436 pprint_name, pflags);
437 fail:
438 TALLOC_FREE(frame);
439 return status;