libsmb: move reparse_symlink to libcli/smb/
[Samba.git] / source3 / libsmb / clisymlink.c
blobd4b269cc2b58cdbc8ec7bb36311ea24c84e58063
1 /*
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/>.
20 #include "includes.h"
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"
26 #include "trans2.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;
36 const char *newpath;
37 uint32_t flags;
39 uint16_t fnum;
41 uint16_t setup[4];
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,
54 const char *newpath,
55 uint32_t flags)
57 struct tevent_req *req, *subreq;
58 struct cli_symlink_state *state;
60 req = tevent_req_create(mem_ctx, &state, struct cli_symlink_state);
61 if (req == NULL) {
62 return NULL;
64 state->ev = ev;
65 state->cli = cli;
66 state->link_target = link_target;
67 state->newpath = newpath;
68 state->flags = flags;
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);
82 return 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);
91 DATA_BLOB data;
92 NTSTATUS status;
94 status = cli_ntcreate_recv(subreq, &state->fnum, NULL);
95 TALLOC_FREE(subreq);
96 if (tevent_req_nterror(req, status)) {
97 return;
100 if (!symlink_reparse_buffer_marshall(
101 state->link_target, NULL, state->flags, state,
102 &data.data, &data.length)) {
103 tevent_req_oom(req);
104 return;
107 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
108 subreq = cli_smb2_set_reparse_point_fnum_send(state,
109 state->ev,
110 state->cli,
111 state->fnum,
112 data);
113 } else {
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,
121 SMBnttrans,
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)) {
130 return;
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);
145 } else {
146 state->set_reparse_status = cli_trans_recv(
147 subreq, NULL, NULL,
148 NULL, 0, NULL, /* rsetup */
149 NULL, 0, NULL, /* rparam */
150 NULL, 0, NULL); /* rdata */
152 TALLOC_FREE(subreq);
154 if (NT_STATUS_IS_OK(state->set_reparse_status)) {
155 subreq = cli_close_send(state, state->ev, state->cli,
156 state->fnum);
157 if (tevent_req_nomem(subreq, req)) {
158 return;
160 tevent_req_set_callback(subreq, cli_symlink_close_done, req);
161 return;
163 subreq = cli_nt_delete_on_close_send(
164 state, state->ev, state->cli, state->fnum, true);
165 if (tevent_req_nomem(subreq, req)) {
166 return;
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);
183 TALLOC_FREE(subreq);
185 subreq = cli_close_send(state, state->ev, state->cli, state->fnum);
186 if (tevent_req_nomem(subreq, req)) {
187 return;
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);
198 NTSTATUS status;
200 status = cli_close_recv(subreq);
201 TALLOC_FREE(subreq);
203 if (tevent_req_nterror(req, status)) {
204 return;
206 if (tevent_req_nterror(req, state->set_reparse_status)) {
207 return;
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;
227 goto fail;
229 ev = samba_tevent_context_init(frame);
230 if (ev == NULL) {
231 goto fail;
233 req = cli_symlink_send(frame, ev, cli, link_target, newname, flags);
234 if (req == NULL) {
235 goto fail;
237 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
238 goto fail;
240 status = cli_symlink_recv(req);
241 fail:
242 TALLOC_FREE(frame);
243 return status;
246 struct cli_readlink_state {
247 struct tevent_context *ev;
248 struct cli_state *cli;
249 uint16_t fnum;
251 uint16_t setup[4];
252 NTSTATUS get_reparse_status;
253 uint8_t *data;
254 uint32_t num_data;
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,
264 const char *fname)
266 struct tevent_req *req, *subreq;
267 struct cli_readlink_state *state;
269 req = tevent_req_create(mem_ctx, &state, struct cli_readlink_state);
270 if (req == NULL) {
271 return NULL;
273 state->ev = ev;
274 state->cli = cli;
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);
285 return 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);
294 NTSTATUS status;
296 status = cli_ntcreate_recv(subreq, &state->fnum, NULL);
297 TALLOC_FREE(subreq);
298 if (tevent_req_nterror(req, status)) {
299 return;
302 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
303 subreq = cli_smb2_get_reparse_point_fnum_send(state,
304 state->ev,
305 state->cli,
306 state->fnum);
307 } else {
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,
314 0, SMBnttrans,
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)) {
323 return;
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) {
336 DATA_BLOB recv_data;
337 state->get_reparse_status =
338 cli_smb2_get_reparse_point_fnum_recv(subreq,
339 state,
340 &recv_data);
341 if (NT_STATUS_IS_OK(state->get_reparse_status)) {
342 state->data = recv_data.data;
343 state->num_data = recv_data.length;
345 } else {
346 state->get_reparse_status = cli_trans_recv(
347 subreq, state, NULL,
348 NULL, 0, NULL, /* rsetup */
349 NULL, 0, NULL, /* rparam */
350 &state->data, 20, &state->num_data); /* rdata */
352 TALLOC_FREE(subreq);
354 subreq = cli_close_send(state, state->ev, state->cli, state->fnum);
355 if (tevent_req_nomem(subreq, req)) {
356 return;
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 NTSTATUS status;
367 status = cli_close_recv(subreq);
368 TALLOC_FREE(subreq);
369 if (tevent_req_nterror(req, status)) {
370 return;
372 tevent_req_done(req);
375 NTSTATUS cli_readlink_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
376 char **psubstitute_name, char **pprint_name,
377 uint32_t *pflags)
379 struct cli_readlink_state *state = tevent_req_data(
380 req, struct cli_readlink_state);
381 struct symlink_reparse_struct *symlink = NULL;
382 NTSTATUS status;
384 if (tevent_req_is_nterror(req, &status)) {
385 return status;
388 symlink = symlink_reparse_buffer_parse(
389 talloc_tos(), state->data, state->num_data);
390 if (symlink == NULL) {
391 return NT_STATUS_INVALID_NETWORK_RESPONSE;
394 if (psubstitute_name != NULL) {
395 *psubstitute_name = talloc_move(
396 mem_ctx, &symlink->substitute_name);
399 if (pprint_name != NULL) {
400 *pprint_name = talloc_move(mem_ctx, &symlink->print_name);
403 if (pflags != NULL) {
404 *pflags = symlink->flags;
407 TALLOC_FREE(symlink);
409 return NT_STATUS_OK;
412 NTSTATUS cli_readlink(struct cli_state *cli, const char *fname,
413 TALLOC_CTX *mem_ctx, char **psubstitute_name,
414 char **pprint_name, uint32_t *pflags)
416 TALLOC_CTX *frame = talloc_stackframe();
417 struct tevent_context *ev;
418 struct tevent_req *req;
419 NTSTATUS status = NT_STATUS_NO_MEMORY;
421 if (smbXcli_conn_has_async_calls(cli->conn)) {
422 status = NT_STATUS_INVALID_PARAMETER;
423 goto fail;
425 ev = samba_tevent_context_init(frame);
426 if (ev == NULL) {
427 goto fail;
429 req = cli_readlink_send(frame, ev, cli, fname);
430 if (req == NULL) {
431 goto fail;
433 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
434 goto fail;
436 status = cli_readlink_recv(req, mem_ctx, psubstitute_name,
437 pprint_name, pflags);
438 fail:
439 TALLOC_FREE(frame);
440 return status;