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"
28 #include "libcli/security/secdesc.h"
29 #include "libcli/security/security.h"
31 struct cli_symlink_state
{
32 struct tevent_context
*ev
;
33 struct cli_state
*cli
;
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
,
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
->oldpath
= oldpath
;
66 state
->newpath
= newpath
;
69 subreq
= cli_ntcreate_send(
70 state
, ev
, cli
, state
->oldpath
, 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
);
93 status
= cli_ntcreate_recv(subreq
, &state
->fnum
);
95 if (tevent_req_nterror(req
, status
)) {
99 SIVAL(state
->setup
, 0, FSCTL_SET_REPARSE_POINT
);
100 SSVAL(state
->setup
, 4, state
->fnum
);
101 SCVAL(state
->setup
, 6, 1); /* IsFcntl */
102 SCVAL(state
->setup
, 7, 0); /* IsFlags */
104 if (!symlink_reparse_buffer_marshall(
105 state
->newpath
, NULL
, state
->flags
, state
,
111 subreq
= cli_trans_send(state
, state
->ev
, state
->cli
, SMBnttrans
,
112 NULL
, -1, /* name, fid */
113 NT_TRANSACT_IOCTL
, 0,
114 state
->setup
, 4, 0, /* setup */
115 NULL
, 0, 0, /* param */
116 data
, data_len
, 0); /* data */
117 if (tevent_req_nomem(subreq
, req
)) {
120 tevent_req_set_callback(subreq
, cli_symlink_set_reparse_done
, req
);
123 static void cli_symlink_set_reparse_done(struct tevent_req
*subreq
)
125 struct tevent_req
*req
= tevent_req_callback_data(
126 subreq
, struct tevent_req
);
127 struct cli_symlink_state
*state
= tevent_req_data(
128 req
, struct cli_symlink_state
);
130 state
->set_reparse_status
= cli_trans_recv(
132 NULL
, 0, NULL
, /* rsetup */
133 NULL
, 0, NULL
, /* rparam */
134 NULL
, 0, NULL
); /* rdata */
137 if (NT_STATUS_IS_OK(state
->set_reparse_status
)) {
138 subreq
= cli_close_send(state
, state
->ev
, state
->cli
,
140 if (tevent_req_nomem(subreq
, req
)) {
143 tevent_req_set_callback(subreq
, cli_symlink_close_done
, req
);
146 subreq
= cli_nt_delete_on_close_send(
147 state
, state
->ev
, state
->cli
, state
->fnum
, true);
148 if (tevent_req_nomem(subreq
, req
)) {
151 tevent_req_set_callback(subreq
, cli_symlink_delete_on_close_done
, req
);
154 static void cli_symlink_delete_on_close_done(struct tevent_req
*subreq
)
156 struct tevent_req
*req
= tevent_req_callback_data(
157 subreq
, struct tevent_req
);
158 struct cli_symlink_state
*state
= tevent_req_data(
159 req
, struct cli_symlink_state
);
162 status
= cli_nt_delete_on_close_recv(subreq
);
166 * Ignore status, we can't do much anyway in case of failure
169 subreq
= cli_close_send(state
, state
->ev
, state
->cli
, state
->fnum
);
170 if (tevent_req_nomem(subreq
, req
)) {
173 tevent_req_set_callback(subreq
, cli_symlink_close_done
, req
);
176 static void cli_symlink_close_done(struct tevent_req
*subreq
)
178 struct tevent_req
*req
= tevent_req_callback_data(
179 subreq
, struct tevent_req
);
180 struct cli_symlink_state
*state
= tevent_req_data(
181 req
, struct cli_symlink_state
);
184 status
= cli_close_recv(subreq
);
187 if (tevent_req_nterror(req
, status
)) {
190 if (tevent_req_nterror(req
, state
->set_reparse_status
)) {
193 tevent_req_done(req
);
196 NTSTATUS
cli_symlink_recv(struct tevent_req
*req
)
198 return tevent_req_simple_recv_ntstatus(req
);
201 NTSTATUS
cli_symlink(struct cli_state
*cli
, const char *oldname
,
202 const char *newname
, uint32_t flags
)
204 TALLOC_CTX
*frame
= talloc_stackframe();
205 struct event_context
*ev
;
206 struct tevent_req
*req
;
207 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
209 if (cli_has_async_calls(cli
)) {
210 status
= NT_STATUS_INVALID_PARAMETER
;
213 ev
= event_context_init(frame
);
217 req
= cli_symlink_send(frame
, ev
, cli
, oldname
, newname
, flags
);
221 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
224 status
= cli_symlink_recv(req
);
230 struct cli_readlink_state
{
231 struct tevent_context
*ev
;
232 struct cli_state
*cli
;
236 NTSTATUS get_reparse_status
;
241 static void cli_readlink_opened(struct tevent_req
*subreq
);
242 static void cli_readlink_got_reparse_data(struct tevent_req
*subreq
);
243 static void cli_readlink_closed(struct tevent_req
*subreq
);
245 struct tevent_req
*cli_readlink_send(TALLOC_CTX
*mem_ctx
,
246 struct tevent_context
*ev
,
247 struct cli_state
*cli
,
250 struct tevent_req
*req
, *subreq
;
251 struct cli_readlink_state
*state
;
253 req
= tevent_req_create(mem_ctx
, &state
, struct cli_readlink_state
);
260 subreq
= cli_ntcreate_send(
261 state
, ev
, cli
, fname
, 0, FILE_READ_ATTRIBUTES
| FILE_READ_EA
,
262 0, FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
263 FILE_OPEN
, FILE_OPEN_REPARSE_POINT
, 0);
264 if (tevent_req_nomem(subreq
, req
)) {
265 return tevent_req_post(req
, ev
);
267 tevent_req_set_callback(subreq
, cli_readlink_opened
, req
);
271 static void cli_readlink_opened(struct tevent_req
*subreq
)
273 struct tevent_req
*req
= tevent_req_callback_data(
274 subreq
, struct tevent_req
);
275 struct cli_readlink_state
*state
= tevent_req_data(
276 req
, struct cli_readlink_state
);
279 status
= cli_ntcreate_recv(subreq
, &state
->fnum
);
281 if (tevent_req_nterror(req
, status
)) {
285 SIVAL(state
->setup
, 0, FSCTL_GET_REPARSE_POINT
);
286 SSVAL(state
->setup
, 4, state
->fnum
);
287 SCVAL(state
->setup
, 6, 1); /* IsFcntl */
288 SCVAL(state
->setup
, 7, 0); /* IsFlags */
290 subreq
= cli_trans_send(state
, state
->ev
, state
->cli
, SMBnttrans
,
291 NULL
, -1, /* name, fid */
292 NT_TRANSACT_IOCTL
, 0,
293 state
->setup
, 4, 0, /* setup */
294 NULL
, 0, 0, /* param */
295 NULL
, 0, 16384); /* data */
296 if (tevent_req_nomem(subreq
, req
)) {
299 tevent_req_set_callback(subreq
, cli_readlink_got_reparse_data
, req
);
302 static void cli_readlink_got_reparse_data(struct tevent_req
*subreq
)
304 struct tevent_req
*req
= tevent_req_callback_data(
305 subreq
, struct tevent_req
);
306 struct cli_readlink_state
*state
= tevent_req_data(
307 req
, struct cli_readlink_state
);
309 state
->get_reparse_status
= cli_trans_recv(
311 NULL
, 0, NULL
, /* rsetup */
312 NULL
, 0, NULL
, /* rparam */
313 &state
->data
, 20, &state
->num_data
); /* rdata */
316 subreq
= cli_close_send(state
, state
->ev
, state
->cli
, state
->fnum
);
317 if (tevent_req_nomem(subreq
, req
)) {
320 tevent_req_set_callback(subreq
, cli_readlink_closed
, req
);
323 static void cli_readlink_closed(struct tevent_req
*subreq
)
325 struct tevent_req
*req
= tevent_req_callback_data(
326 subreq
, struct tevent_req
);
329 status
= cli_close_recv(subreq
);
331 if (tevent_req_nterror(req
, status
)) {
334 tevent_req_done(req
);
337 NTSTATUS
cli_readlink_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
338 char **psubstitute_name
, char **pprint_name
,
341 struct cli_readlink_state
*state
= tevent_req_data(
342 req
, struct cli_readlink_state
);
344 char *substitute_name
;
348 if (tevent_req_is_nterror(req
, &status
)) {
352 if (!symlink_reparse_buffer_parse(state
->data
, state
->num_data
,
353 talloc_tos(), &substitute_name
,
354 &print_name
, &flags
)) {
355 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
358 if (psubstitute_name
!= NULL
) {
359 *psubstitute_name
= talloc_move(mem_ctx
, &substitute_name
);
361 TALLOC_FREE(substitute_name
);
363 if (pprint_name
!= NULL
) {
364 *pprint_name
= talloc_move(mem_ctx
, &print_name
);
366 TALLOC_FREE(print_name
);
368 if (pflags
!= NULL
) {
374 NTSTATUS
cli_readlink(struct cli_state
*cli
, const char *fname
,
375 TALLOC_CTX
*mem_ctx
, char **psubstitute_name
,
376 char **pprint_name
, uint32_t *pflags
)
378 TALLOC_CTX
*frame
= talloc_stackframe();
379 struct event_context
*ev
;
380 struct tevent_req
*req
;
381 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
383 if (cli_has_async_calls(cli
)) {
384 status
= NT_STATUS_INVALID_PARAMETER
;
387 ev
= event_context_init(frame
);
391 req
= cli_readlink_send(frame
, ev
, cli
, fname
);
395 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
398 status
= cli_readlink_recv(req
, mem_ctx
, psubstitute_name
,
399 pprint_name
, pflags
);