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"
31 struct cli_symlink_state {
32 struct tevent_context *ev;
33 struct cli_state *cli;
34 const char *link_target;
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,
56 struct tevent_req *req, *subreq;
57 struct cli_symlink_state *state;
59 req = tevent_req_create(mem_ctx, &state, struct cli_symlink_state);
65 state->link_target = link_target;
66 state->newpath = newpath;
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, 0);
76 if (tevent_req_nomem(subreq, req)) {
77 return tevent_req_post(req, ev);
79 tevent_req_set_callback(subreq, cli_symlink_create_done, req);
83 static void cli_symlink_create_done(struct tevent_req *subreq)
85 struct tevent_req *req = tevent_req_callback_data(
86 subreq, struct tevent_req);
87 struct cli_symlink_state *state = tevent_req_data(
88 req, struct cli_symlink_state);
92 status = cli_ntcreate_recv(subreq, &state->fnum, NULL);
94 if (tevent_req_nterror(req, status)) {
98 if (!symlink_reparse_buffer_marshall(
99 state->link_target, NULL, state->flags, state,
100 &data.data, &data.length)) {
105 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
106 subreq = cli_smb2_set_reparse_point_fnum_send(state,
112 SIVAL(state->setup, 0, FSCTL_SET_REPARSE_POINT);
113 SSVAL(state->setup, 4, state->fnum);
114 SCVAL(state->setup, 6, 1); /* IsFcntl */
115 SCVAL(state->setup, 7, 0); /* IsFlags */
118 subreq = cli_trans_send(state, state->ev, state->cli, 0,
120 NULL, -1, /* name, fid */
121 NT_TRANSACT_IOCTL, 0,
122 state->setup, 4, 0, /* setup */
123 NULL, 0, 0, /* param */
124 data.data, data.length, 0); /* data */
127 if (tevent_req_nomem(subreq, req)) {
130 tevent_req_set_callback(subreq, cli_symlink_set_reparse_done, req);
133 static void cli_symlink_set_reparse_done(struct tevent_req *subreq)
135 struct tevent_req *req = tevent_req_callback_data(
136 subreq, struct tevent_req);
137 struct cli_symlink_state *state = tevent_req_data(
138 req, struct cli_symlink_state);
140 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
141 state->set_reparse_status =
142 cli_smb2_set_reparse_point_fnum_recv(subreq);
144 state->set_reparse_status = cli_trans_recv(
146 NULL, 0, NULL, /* rsetup */
147 NULL, 0, NULL, /* rparam */
148 NULL, 0, NULL); /* rdata */
152 if (NT_STATUS_IS_OK(state->set_reparse_status)) {
153 subreq = cli_close_send(state, state->ev, state->cli,
155 if (tevent_req_nomem(subreq, req)) {
158 tevent_req_set_callback(subreq, cli_symlink_close_done, req);
161 subreq = cli_nt_delete_on_close_send(
162 state, state->ev, state->cli, state->fnum, true);
163 if (tevent_req_nomem(subreq, req)) {
166 tevent_req_set_callback(subreq, cli_symlink_delete_on_close_done, req);
169 static void cli_symlink_delete_on_close_done(struct tevent_req *subreq)
171 struct tevent_req *req = tevent_req_callback_data(
172 subreq, struct tevent_req);
173 struct cli_symlink_state *state = tevent_req_data(
174 req, struct cli_symlink_state);
177 * Ignore status, we can't do much anyway in case of failure
180 (void)cli_nt_delete_on_close_recv(subreq);
183 subreq = cli_close_send(state, state->ev, state->cli, state->fnum);
184 if (tevent_req_nomem(subreq, req)) {
187 tevent_req_set_callback(subreq, cli_symlink_close_done, req);
190 static void cli_symlink_close_done(struct tevent_req *subreq)
192 struct tevent_req *req = tevent_req_callback_data(
193 subreq, struct tevent_req);
194 struct cli_symlink_state *state = tevent_req_data(
195 req, struct cli_symlink_state);
198 status = cli_close_recv(subreq);
201 if (tevent_req_nterror(req, status)) {
204 if (tevent_req_nterror(req, state->set_reparse_status)) {
207 tevent_req_done(req);
210 NTSTATUS cli_symlink_recv(struct tevent_req *req)
212 return tevent_req_simple_recv_ntstatus(req);
215 NTSTATUS cli_symlink(struct cli_state *cli, const char *link_target,
216 const char *newname, uint32_t flags)
218 TALLOC_CTX *frame = talloc_stackframe();
219 struct tevent_context *ev;
220 struct tevent_req *req;
221 NTSTATUS status = NT_STATUS_NO_MEMORY;
223 if (smbXcli_conn_has_async_calls(cli->conn)) {
224 status = NT_STATUS_INVALID_PARAMETER;
227 ev = samba_tevent_context_init(frame);
231 req = cli_symlink_send(frame, ev, cli, link_target, newname, flags);
235 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
238 status = cli_symlink_recv(req);
244 struct cli_readlink_state {
245 struct tevent_context *ev;
246 struct cli_state *cli;
250 NTSTATUS get_reparse_status;
255 static void cli_readlink_opened(struct tevent_req *subreq);
256 static void cli_readlink_got_reparse_data(struct tevent_req *subreq);
257 static void cli_readlink_closed(struct tevent_req *subreq);
259 struct tevent_req *cli_readlink_send(TALLOC_CTX *mem_ctx,
260 struct tevent_context *ev,
261 struct cli_state *cli,
264 struct tevent_req *req, *subreq;
265 struct cli_readlink_state *state;
267 req = tevent_req_create(mem_ctx, &state, struct cli_readlink_state);
274 subreq = cli_ntcreate_send(
275 state, ev, cli, fname, 0, FILE_READ_ATTRIBUTES | FILE_READ_EA,
276 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
277 FILE_OPEN, FILE_OPEN_REPARSE_POINT, 0);
278 if (tevent_req_nomem(subreq, req)) {
279 return tevent_req_post(req, ev);
281 tevent_req_set_callback(subreq, cli_readlink_opened, req);
285 static void cli_readlink_opened(struct tevent_req *subreq)
287 struct tevent_req *req = tevent_req_callback_data(
288 subreq, struct tevent_req);
289 struct cli_readlink_state *state = tevent_req_data(
290 req, struct cli_readlink_state);
293 status = cli_ntcreate_recv(subreq, &state->fnum, NULL);
295 if (tevent_req_nterror(req, status)) {
299 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
300 subreq = cli_smb2_get_reparse_point_fnum_send(state,
305 SIVAL(state->setup, 0, FSCTL_GET_REPARSE_POINT);
306 SSVAL(state->setup, 4, state->fnum);
307 SCVAL(state->setup, 6, 1); /* IsFcntl */
308 SCVAL(state->setup, 7, 0); /* IsFlags */
310 subreq = cli_trans_send(state, state->ev, state->cli,
312 NULL, -1, /* name, fid */
313 NT_TRANSACT_IOCTL, 0,
314 state->setup, 4, 0, /* setup */
315 NULL, 0, 0, /* param */
316 NULL, 0, 16384); /* data */
319 if (tevent_req_nomem(subreq, req)) {
322 tevent_req_set_callback(subreq, cli_readlink_got_reparse_data, req);
325 static void cli_readlink_got_reparse_data(struct tevent_req *subreq)
327 struct tevent_req *req = tevent_req_callback_data(
328 subreq, struct tevent_req);
329 struct cli_readlink_state *state = tevent_req_data(
330 req, struct cli_readlink_state);
332 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
334 state->get_reparse_status =
335 cli_smb2_get_reparse_point_fnum_recv(subreq,
338 if (NT_STATUS_IS_OK(state->get_reparse_status)) {
339 state->data = recv_data.data;
340 state->num_data = recv_data.length;
343 state->get_reparse_status = cli_trans_recv(
345 NULL, 0, NULL, /* rsetup */
346 NULL, 0, NULL, /* rparam */
347 &state->data, 20, &state->num_data); /* rdata */
351 subreq = cli_close_send(state, state->ev, state->cli, state->fnum);
352 if (tevent_req_nomem(subreq, req)) {
355 tevent_req_set_callback(subreq, cli_readlink_closed, req);
358 static void cli_readlink_closed(struct tevent_req *subreq)
360 struct tevent_req *req = tevent_req_callback_data(
361 subreq, struct tevent_req);
364 status = cli_close_recv(subreq);
366 if (tevent_req_nterror(req, status)) {
369 tevent_req_done(req);
372 NTSTATUS cli_readlink_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
373 char **psubstitute_name, char **pprint_name,
376 struct cli_readlink_state *state = tevent_req_data(
377 req, struct cli_readlink_state);
379 char *substitute_name;
383 if (tevent_req_is_nterror(req, &status)) {
387 if (!symlink_reparse_buffer_parse(state->data, state->num_data,
388 talloc_tos(), &substitute_name,
389 &print_name, &flags)) {
390 return NT_STATUS_INVALID_NETWORK_RESPONSE;
393 if (psubstitute_name != NULL) {
394 *psubstitute_name = talloc_move(mem_ctx, &substitute_name);
396 TALLOC_FREE(substitute_name);
398 if (pprint_name != NULL) {
399 *pprint_name = talloc_move(mem_ctx, &print_name);
401 TALLOC_FREE(print_name);
403 if (pflags != NULL) {
409 NTSTATUS cli_readlink(struct cli_state *cli, const char *fname,
410 TALLOC_CTX *mem_ctx, char **psubstitute_name,
411 char **pprint_name, uint32_t *pflags)
413 TALLOC_CTX *frame = talloc_stackframe();
414 struct tevent_context *ev;
415 struct tevent_req *req;
416 NTSTATUS status = NT_STATUS_NO_MEMORY;
418 if (smbXcli_conn_has_async_calls(cli->conn)) {
419 status = NT_STATUS_INVALID_PARAMETER;
422 ev = samba_tevent_context_init(frame);
426 req = cli_readlink_send(frame, ev, cli, fname);
430 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
433 status = cli_readlink_recv(req, mem_ctx, psubstitute_name,
434 pprint_name, pflags);