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/>.
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"
27 #include "libcli/security/secdesc.h"
28 #include "libcli/security/security.h"
29 #include "../libcli/smb/smbXcli_base.h"
30 #include "libcli/smb/reparse_symlink.h"
32 struct cli_symlink_state {
33 struct tevent_context *ev;
34 struct cli_state *cli;
35 const char *link_target;
42 NTSTATUS set_reparse_status;
45 static void cli_symlink_create_done(struct tevent_req *subreq);
46 static void cli_symlink_set_reparse_done(struct tevent_req *subreq);
47 static void cli_symlink_delete_on_close_done(struct tevent_req *subreq);
48 static void cli_symlink_close_done(struct tevent_req *subreq);
50 struct tevent_req *cli_symlink_send(TALLOC_CTX *mem_ctx,
51 struct tevent_context *ev,
52 struct cli_state *cli,
53 const char *link_target,
57 struct tevent_req *req, *subreq;
58 struct cli_symlink_state *state;
60 req = tevent_req_create(mem_ctx, &state, struct cli_symlink_state);
66 state->link_target = link_target;
67 state->newpath = newpath;
70 subreq = cli_ntcreate_send(
71 state, ev, cli, state->newpath, 0,
72 SYNCHRONIZE_ACCESS|DELETE_ACCESS|
73 FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES,
74 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_CREATE,
75 FILE_OPEN_REPARSE_POINT|FILE_SYNCHRONOUS_IO_NONALERT|
76 FILE_NON_DIRECTORY_FILE,
77 SMB2_IMPERSONATION_IMPERSONATION, 0);
78 if (tevent_req_nomem(subreq, req)) {
79 return tevent_req_post(req, ev);
81 tevent_req_set_callback(subreq, cli_symlink_create_done, req);
85 static void cli_symlink_create_done(struct tevent_req *subreq)
87 struct tevent_req *req = tevent_req_callback_data(
88 subreq, struct tevent_req);
89 struct cli_symlink_state *state = tevent_req_data(
90 req, struct cli_symlink_state);
93 status = cli_ntcreate_recv(subreq, &state->fnum, NULL);
95 if (tevent_req_nterror(req, status)) {
99 if (!symlink_reparse_buffer_marshall(
100 state->link_target, NULL, 0, state->flags, state,
101 &state->in.data, &state->in.length)) {
106 subreq = cli_fsctl_send(
111 FSCTL_SET_REPARSE_POINT,
114 if (tevent_req_nomem(subreq, req)) {
117 tevent_req_set_callback(subreq, cli_symlink_set_reparse_done, req);
120 static void cli_symlink_set_reparse_done(struct tevent_req *subreq)
122 struct tevent_req *req = tevent_req_callback_data(
123 subreq, struct tevent_req);
124 struct cli_symlink_state *state = tevent_req_data(
125 req, struct cli_symlink_state);
127 state->set_reparse_status = cli_fsctl_recv(subreq, NULL, NULL);
130 if (NT_STATUS_IS_OK(state->set_reparse_status)) {
131 subreq = cli_close_send(state, state->ev, state->cli,
133 if (tevent_req_nomem(subreq, req)) {
136 tevent_req_set_callback(subreq, cli_symlink_close_done, req);
139 subreq = cli_nt_delete_on_close_send(
140 state, state->ev, state->cli, state->fnum, true);
141 if (tevent_req_nomem(subreq, req)) {
144 tevent_req_set_callback(subreq, cli_symlink_delete_on_close_done, req);
147 static void cli_symlink_delete_on_close_done(struct tevent_req *subreq)
149 struct tevent_req *req = tevent_req_callback_data(
150 subreq, struct tevent_req);
151 struct cli_symlink_state *state = tevent_req_data(
152 req, struct cli_symlink_state);
155 * Ignore status, we can't do much anyway in case of failure
158 (void)cli_nt_delete_on_close_recv(subreq);
161 subreq = cli_close_send(state, state->ev, state->cli, state->fnum);
162 if (tevent_req_nomem(subreq, req)) {
165 tevent_req_set_callback(subreq, cli_symlink_close_done, req);
168 static void cli_symlink_close_done(struct tevent_req *subreq)
170 struct tevent_req *req = tevent_req_callback_data(
171 subreq, struct tevent_req);
172 struct cli_symlink_state *state = tevent_req_data(
173 req, struct cli_symlink_state);
176 status = cli_close_recv(subreq);
179 if (tevent_req_nterror(req, status)) {
182 if (tevent_req_nterror(req, state->set_reparse_status)) {
185 tevent_req_done(req);
188 NTSTATUS cli_symlink_recv(struct tevent_req *req)
190 return tevent_req_simple_recv_ntstatus(req);
193 NTSTATUS cli_symlink(struct cli_state *cli, const char *link_target,
194 const char *newname, uint32_t flags)
196 TALLOC_CTX *frame = talloc_stackframe();
197 struct tevent_context *ev;
198 struct tevent_req *req;
199 NTSTATUS status = NT_STATUS_NO_MEMORY;
201 if (smbXcli_conn_has_async_calls(cli->conn)) {
202 status = NT_STATUS_INVALID_PARAMETER;
205 ev = samba_tevent_context_init(frame);
209 req = cli_symlink_send(frame, ev, cli, link_target, newname, flags);
213 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
216 status = cli_symlink_recv(req);
222 struct cli_readlink_state {
223 struct tevent_context *ev;
224 struct cli_state *cli;
228 NTSTATUS get_reparse_status;
234 static void cli_readlink_posix1_done(struct tevent_req *subreq);
235 static void cli_readlink_opened(struct tevent_req *subreq);
236 static void cli_readlink_got_reparse_data(struct tevent_req *subreq);
237 static void cli_readlink_closed(struct tevent_req *subreq);
239 struct tevent_req *cli_readlink_send(TALLOC_CTX *mem_ctx,
240 struct tevent_context *ev,
241 struct cli_state *cli,
244 struct tevent_req *req, *subreq;
245 struct cli_readlink_state *state;
247 req = tevent_req_create(mem_ctx, &state, struct cli_readlink_state);
254 if (cli->requested_posix_capabilities != 0) {
256 * Only happens for negotiated SMB1 posix caps
258 subreq = cli_posix_readlink_send(state, ev, cli, fname);
259 if (tevent_req_nomem(subreq, req)) {
260 return tevent_req_post(req, ev);
262 tevent_req_set_callback(subreq, cli_readlink_posix1_done, req);
266 subreq = cli_ntcreate_send(
267 state, ev, cli, fname, 0, FILE_READ_ATTRIBUTES | FILE_READ_EA,
268 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
269 FILE_OPEN, FILE_OPEN_REPARSE_POINT,
270 SMB2_IMPERSONATION_IMPERSONATION, 0);
271 if (tevent_req_nomem(subreq, req)) {
272 return tevent_req_post(req, ev);
274 tevent_req_set_callback(subreq, cli_readlink_opened, req);
278 static void cli_readlink_posix1_done(struct tevent_req *subreq)
280 struct tevent_req *req = tevent_req_callback_data(
281 subreq, struct tevent_req);
282 struct cli_readlink_state *state = tevent_req_data(
283 req, struct cli_readlink_state);
286 status = cli_posix_readlink_recv(subreq, state, &state->target);
288 if (tevent_req_nterror(req, status)) {
291 tevent_req_done(req);
294 static void cli_readlink_opened(struct tevent_req *subreq)
296 struct tevent_req *req = tevent_req_callback_data(
297 subreq, struct tevent_req);
298 struct cli_readlink_state *state = tevent_req_data(
299 req, struct cli_readlink_state);
302 status = cli_ntcreate_recv(subreq, &state->fnum, NULL);
304 if (tevent_req_nterror(req, status)) {
308 subreq = cli_fsctl_send(
313 FSCTL_GET_REPARSE_POINT,
317 if (tevent_req_nomem(subreq, req)) {
320 tevent_req_set_callback(subreq, cli_readlink_got_reparse_data, req);
323 static void cli_readlink_got_reparse_data(struct tevent_req *subreq)
325 struct tevent_req *req = tevent_req_callback_data(
326 subreq, struct tevent_req);
327 struct cli_readlink_state *state = tevent_req_data(
328 req, struct cli_readlink_state);
329 DATA_BLOB out = { .data = NULL, };
331 state->get_reparse_status = cli_fsctl_recv(subreq, state, &out);
334 if (NT_STATUS_IS_OK(state->get_reparse_status)) {
335 state->data = out.data;
336 state->num_data = out.length;
339 subreq = cli_close_send(state, state->ev, state->cli, state->fnum);
340 if (tevent_req_nomem(subreq, req)) {
343 tevent_req_set_callback(subreq, cli_readlink_closed, req);
346 static void cli_readlink_closed(struct tevent_req *subreq)
348 struct tevent_req *req = tevent_req_callback_data(
349 subreq, struct tevent_req);
350 struct cli_readlink_state *state = tevent_req_data(
351 req, struct cli_readlink_state);
354 status = cli_close_recv(subreq);
356 if (tevent_req_nterror(req, status)) {
359 if (tevent_req_nterror(req, state->get_reparse_status)) {
362 tevent_req_done(req);
365 NTSTATUS cli_readlink_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
366 char **psubstitute_name, char **pprint_name,
369 struct cli_readlink_state *state = tevent_req_data(
370 req, struct cli_readlink_state);
371 struct symlink_reparse_struct symlink = { .flags = 0, };
375 if (tevent_req_is_nterror(req, &status)) {
379 if (state->target != NULL) {
383 if (psubstitute_name != NULL) {
384 *psubstitute_name = talloc_move(
385 mem_ctx, &state->target);
387 if (pprint_name != NULL) {
390 if (pflags != NULL) {
396 ret = symlink_reparse_buffer_parse(
397 talloc_tos(), &symlink, state->data, state->num_data);
399 return NT_STATUS_INVALID_NETWORK_RESPONSE;
402 if (psubstitute_name != NULL) {
403 *psubstitute_name = talloc_move(
404 mem_ctx, &symlink.substitute_name);
407 if (pprint_name != NULL) {
408 *pprint_name = talloc_move(mem_ctx, &symlink.print_name);
411 if (pflags != NULL) {
412 *pflags = symlink.flags;
415 TALLOC_FREE(symlink.print_name);
416 TALLOC_FREE(symlink.substitute_name);
421 NTSTATUS cli_readlink(struct cli_state *cli, const char *fname,
422 TALLOC_CTX *mem_ctx, char **psubstitute_name,
423 char **pprint_name, uint32_t *pflags)
425 TALLOC_CTX *frame = talloc_stackframe();
426 struct tevent_context *ev;
427 struct tevent_req *req;
428 NTSTATUS status = NT_STATUS_NO_MEMORY;
430 if (smbXcli_conn_has_async_calls(cli->conn)) {
431 status = NT_STATUS_INVALID_PARAMETER;
434 ev = samba_tevent_context_init(frame);
438 req = cli_readlink_send(frame, ev, cli, fname);
442 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
445 status = cli_readlink_recv(req, mem_ctx, psubstitute_name,
446 pprint_name, pflags);