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
);
94 status
= cli_ntcreate_recv(subreq
, &state
->fnum
, NULL
);
96 if (tevent_req_nterror(req
, status
)) {
100 if (!symlink_reparse_buffer_marshall(
101 state
->link_target
, NULL
, state
->flags
, state
,
102 &data
.data
, &data
.length
)) {
107 if (smbXcli_conn_protocol(state
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
108 subreq
= cli_smb2_set_reparse_point_fnum_send(state
,
114 SIVAL(state
->setup
, 0, FSCTL_SET_REPARSE_POINT
);
115 SSVAL(state
->setup
, 4, state
->fnum
);
116 SCVAL(state
->setup
, 6, 1); /* IsFcntl */
117 SCVAL(state
->setup
, 7, 0); /* IsFlags */
120 subreq
= cli_trans_send(state
, state
->ev
, state
->cli
, 0,
122 NULL
, -1, /* name, fid */
123 NT_TRANSACT_IOCTL
, 0,
124 state
->setup
, 4, 0, /* setup */
125 NULL
, 0, 0, /* param */
126 data
.data
, data
.length
, 0); /* data */
129 if (tevent_req_nomem(subreq
, req
)) {
132 tevent_req_set_callback(subreq
, cli_symlink_set_reparse_done
, req
);
135 static void cli_symlink_set_reparse_done(struct tevent_req
*subreq
)
137 struct tevent_req
*req
= tevent_req_callback_data(
138 subreq
, struct tevent_req
);
139 struct cli_symlink_state
*state
= tevent_req_data(
140 req
, struct cli_symlink_state
);
142 if (smbXcli_conn_protocol(state
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
143 state
->set_reparse_status
=
144 cli_smb2_set_reparse_point_fnum_recv(subreq
);
146 state
->set_reparse_status
= cli_trans_recv(
148 NULL
, 0, NULL
, /* rsetup */
149 NULL
, 0, NULL
, /* rparam */
150 NULL
, 0, NULL
); /* rdata */
154 if (NT_STATUS_IS_OK(state
->set_reparse_status
)) {
155 subreq
= cli_close_send(state
, state
->ev
, state
->cli
,
157 if (tevent_req_nomem(subreq
, req
)) {
160 tevent_req_set_callback(subreq
, cli_symlink_close_done
, req
);
163 subreq
= cli_nt_delete_on_close_send(
164 state
, state
->ev
, state
->cli
, state
->fnum
, true);
165 if (tevent_req_nomem(subreq
, req
)) {
168 tevent_req_set_callback(subreq
, cli_symlink_delete_on_close_done
, req
);
171 static void cli_symlink_delete_on_close_done(struct tevent_req
*subreq
)
173 struct tevent_req
*req
= tevent_req_callback_data(
174 subreq
, struct tevent_req
);
175 struct cli_symlink_state
*state
= tevent_req_data(
176 req
, struct cli_symlink_state
);
179 * Ignore status, we can't do much anyway in case of failure
182 (void)cli_nt_delete_on_close_recv(subreq
);
185 subreq
= cli_close_send(state
, state
->ev
, state
->cli
, state
->fnum
);
186 if (tevent_req_nomem(subreq
, req
)) {
189 tevent_req_set_callback(subreq
, cli_symlink_close_done
, req
);
192 static void cli_symlink_close_done(struct tevent_req
*subreq
)
194 struct tevent_req
*req
= tevent_req_callback_data(
195 subreq
, struct tevent_req
);
196 struct cli_symlink_state
*state
= tevent_req_data(
197 req
, struct cli_symlink_state
);
200 status
= cli_close_recv(subreq
);
203 if (tevent_req_nterror(req
, status
)) {
206 if (tevent_req_nterror(req
, state
->set_reparse_status
)) {
209 tevent_req_done(req
);
212 NTSTATUS
cli_symlink_recv(struct tevent_req
*req
)
214 return tevent_req_simple_recv_ntstatus(req
);
217 NTSTATUS
cli_symlink(struct cli_state
*cli
, const char *link_target
,
218 const char *newname
, uint32_t flags
)
220 TALLOC_CTX
*frame
= talloc_stackframe();
221 struct tevent_context
*ev
;
222 struct tevent_req
*req
;
223 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
225 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
226 status
= NT_STATUS_INVALID_PARAMETER
;
229 ev
= samba_tevent_context_init(frame
);
233 req
= cli_symlink_send(frame
, ev
, cli
, link_target
, newname
, flags
);
237 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
240 status
= cli_symlink_recv(req
);
246 struct cli_readlink_state
{
247 struct tevent_context
*ev
;
248 struct cli_state
*cli
;
252 NTSTATUS get_reparse_status
;
257 static void cli_readlink_opened(struct tevent_req
*subreq
);
258 static void cli_readlink_got_reparse_data(struct tevent_req
*subreq
);
259 static void cli_readlink_closed(struct tevent_req
*subreq
);
261 struct tevent_req
*cli_readlink_send(TALLOC_CTX
*mem_ctx
,
262 struct tevent_context
*ev
,
263 struct cli_state
*cli
,
266 struct tevent_req
*req
, *subreq
;
267 struct cli_readlink_state
*state
;
269 req
= tevent_req_create(mem_ctx
, &state
, struct cli_readlink_state
);
276 subreq
= cli_ntcreate_send(
277 state
, ev
, cli
, fname
, 0, FILE_READ_ATTRIBUTES
| FILE_READ_EA
,
278 0, FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
279 FILE_OPEN
, FILE_OPEN_REPARSE_POINT
,
280 SMB2_IMPERSONATION_IMPERSONATION
, 0);
281 if (tevent_req_nomem(subreq
, req
)) {
282 return tevent_req_post(req
, ev
);
284 tevent_req_set_callback(subreq
, cli_readlink_opened
, req
);
288 static void cli_readlink_opened(struct tevent_req
*subreq
)
290 struct tevent_req
*req
= tevent_req_callback_data(
291 subreq
, struct tevent_req
);
292 struct cli_readlink_state
*state
= tevent_req_data(
293 req
, struct cli_readlink_state
);
296 status
= cli_ntcreate_recv(subreq
, &state
->fnum
, NULL
);
298 if (tevent_req_nterror(req
, status
)) {
302 if (smbXcli_conn_protocol(state
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
303 subreq
= cli_smb2_get_reparse_point_fnum_send(state
,
308 SIVAL(state
->setup
, 0, FSCTL_GET_REPARSE_POINT
);
309 SSVAL(state
->setup
, 4, state
->fnum
);
310 SCVAL(state
->setup
, 6, 1); /* IsFcntl */
311 SCVAL(state
->setup
, 7, 0); /* IsFlags */
313 subreq
= cli_trans_send(state
, state
->ev
, state
->cli
,
315 NULL
, -1, /* name, fid */
316 NT_TRANSACT_IOCTL
, 0,
317 state
->setup
, 4, 0, /* setup */
318 NULL
, 0, 0, /* param */
319 NULL
, 0, 16384); /* data */
322 if (tevent_req_nomem(subreq
, req
)) {
325 tevent_req_set_callback(subreq
, cli_readlink_got_reparse_data
, req
);
328 static void cli_readlink_got_reparse_data(struct tevent_req
*subreq
)
330 struct tevent_req
*req
= tevent_req_callback_data(
331 subreq
, struct tevent_req
);
332 struct cli_readlink_state
*state
= tevent_req_data(
333 req
, struct cli_readlink_state
);
335 if (smbXcli_conn_protocol(state
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
337 state
->get_reparse_status
=
338 cli_smb2_get_reparse_point_fnum_recv(subreq
,
341 if (NT_STATUS_IS_OK(state
->get_reparse_status
)) {
342 state
->data
= recv_data
.data
;
343 state
->num_data
= recv_data
.length
;
346 state
->get_reparse_status
= cli_trans_recv(
348 NULL
, 0, NULL
, /* rsetup */
349 NULL
, 0, NULL
, /* rparam */
350 &state
->data
, 20, &state
->num_data
); /* rdata */
354 subreq
= cli_close_send(state
, state
->ev
, state
->cli
, state
->fnum
);
355 if (tevent_req_nomem(subreq
, req
)) {
358 tevent_req_set_callback(subreq
, cli_readlink_closed
, req
);
361 static void cli_readlink_closed(struct tevent_req
*subreq
)
363 struct tevent_req
*req
= tevent_req_callback_data(
364 subreq
, struct tevent_req
);
365 struct cli_readlink_state
*state
= tevent_req_data(
366 req
, struct cli_readlink_state
);
369 status
= cli_close_recv(subreq
);
371 if (tevent_req_nterror(req
, status
)) {
374 if (tevent_req_nterror(req
, state
->get_reparse_status
)) {
377 tevent_req_done(req
);
380 NTSTATUS
cli_readlink_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
381 char **psubstitute_name
, char **pprint_name
,
384 struct cli_readlink_state
*state
= tevent_req_data(
385 req
, struct cli_readlink_state
);
386 struct symlink_reparse_struct
*symlink
= NULL
;
389 if (tevent_req_is_nterror(req
, &status
)) {
393 symlink
= symlink_reparse_buffer_parse(
394 talloc_tos(), state
->data
, state
->num_data
);
395 if (symlink
== NULL
) {
396 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
399 if (psubstitute_name
!= NULL
) {
400 *psubstitute_name
= talloc_move(
401 mem_ctx
, &symlink
->substitute_name
);
404 if (pprint_name
!= NULL
) {
405 *pprint_name
= talloc_move(mem_ctx
, &symlink
->print_name
);
408 if (pflags
!= NULL
) {
409 *pflags
= symlink
->flags
;
412 TALLOC_FREE(symlink
);
417 NTSTATUS
cli_readlink(struct cli_state
*cli
, const char *fname
,
418 TALLOC_CTX
*mem_ctx
, char **psubstitute_name
,
419 char **pprint_name
, uint32_t *pflags
)
421 TALLOC_CTX
*frame
= talloc_stackframe();
422 struct tevent_context
*ev
;
423 struct tevent_req
*req
;
424 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
426 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
427 status
= NT_STATUS_INVALID_PARAMETER
;
430 ev
= samba_tevent_context_init(frame
);
434 req
= cli_readlink_send(frame
, ev
, cli
, fname
);
438 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
441 status
= cli_readlink_recv(req
, mem_ctx
, psubstitute_name
,
442 pprint_name
, pflags
);