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
,
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
);
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
);
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
, state
->flags
, state
,
101 &data
.data
, &data
.length
)) {
106 if (smbXcli_conn_protocol(state
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
107 subreq
= cli_smb2_set_reparse_point_fnum_send(state
,
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,
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
)) {
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
);
145 state
->set_reparse_status
= cli_trans_recv(
147 NULL
, 0, NULL
, /* rsetup */
148 NULL
, 0, NULL
, /* rparam */
149 NULL
, 0, NULL
); /* rdata */
153 if (NT_STATUS_IS_OK(state
->set_reparse_status
)) {
154 subreq
= cli_close_send(state
, state
->ev
, state
->cli
,
156 if (tevent_req_nomem(subreq
, req
)) {
159 tevent_req_set_callback(subreq
, cli_symlink_close_done
, req
);
162 subreq
= cli_nt_delete_on_close_send(
163 state
, state
->ev
, state
->cli
, state
->fnum
, true);
164 if (tevent_req_nomem(subreq
, req
)) {
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
);
184 subreq
= cli_close_send(state
, state
->ev
, state
->cli
, state
->fnum
);
185 if (tevent_req_nomem(subreq
, req
)) {
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
);
199 status
= cli_close_recv(subreq
);
202 if (tevent_req_nterror(req
, status
)) {
205 if (tevent_req_nterror(req
, state
->set_reparse_status
)) {
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
;
228 ev
= samba_tevent_context_init(frame
);
232 req
= cli_symlink_send(frame
, ev
, cli
, link_target
, newname
, flags
);
236 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
239 status
= cli_symlink_recv(req
);
245 struct cli_readlink_state
{
246 struct tevent_context
*ev
;
247 struct cli_state
*cli
;
251 NTSTATUS get_reparse_status
;
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
,
265 struct tevent_req
*req
, *subreq
;
266 struct cli_readlink_state
*state
;
268 req
= tevent_req_create(mem_ctx
, &state
, struct cli_readlink_state
);
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
);
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
);
295 status
= cli_ntcreate_recv(subreq
, &state
->fnum
, NULL
);
297 if (tevent_req_nterror(req
, status
)) {
301 if (smbXcli_conn_protocol(state
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
302 subreq
= cli_smb2_get_reparse_point_fnum_send(state
,
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
,
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
)) {
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
) {
336 state
->get_reparse_status
=
337 cli_smb2_get_reparse_point_fnum_recv(subreq
,
340 if (NT_STATUS_IS_OK(state
->get_reparse_status
)) {
341 state
->data
= recv_data
.data
;
342 state
->num_data
= recv_data
.length
;
345 state
->get_reparse_status
= cli_trans_recv(
347 NULL
, 0, NULL
, /* rsetup */
348 NULL
, 0, NULL
, /* rparam */
349 &state
->data
, 20, &state
->num_data
); /* rdata */
353 subreq
= cli_close_send(state
, state
->ev
, state
->cli
, state
->fnum
);
354 if (tevent_req_nomem(subreq
, req
)) {
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
);
366 status
= cli_close_recv(subreq
);
368 if (tevent_req_nterror(req
, status
)) {
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
,
378 struct cli_readlink_state
*state
= tevent_req_data(
379 req
, struct cli_readlink_state
);
381 char *substitute_name
;
385 if (tevent_req_is_nterror(req
, &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
) {
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
;
424 ev
= samba_tevent_context_init(frame
);
428 req
= cli_readlink_send(frame
, ev
, cli
, fname
);
432 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
435 status
= cli_readlink_recv(req
, mem_ctx
, psubstitute_name
,
436 pprint_name
, pflags
);