s3-utils/net_rpc_printer.c: print more info on write error
[Samba/gebeck_regimport.git] / source3 / libsmb / clisymlink.c
blobe744a38dbe4df412b65c3e907b96633ac037a113
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 "ntioctl.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;
34 const char *oldpath;
35 const char *newpath;
36 uint32_t flags;
38 uint16_t fnum;
40 uint16_t setup[4];
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 *oldpath,
53 const char *newpath,
54 uint32_t flags)
56 struct tevent_req *req, *subreq;
57 struct cli_symlink_state *state;
59 req = tevent_req_create(mem_ctx, &state, struct cli_symlink_state);
60 if (req == NULL) {
61 return NULL;
63 state->ev = ev;
64 state->cli = cli;
65 state->oldpath = oldpath;
66 state->newpath = newpath;
67 state->flags = flags;
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);
80 return 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);
89 uint8_t *data;
90 size_t data_len;
91 NTSTATUS status;
93 status = cli_ntcreate_recv(subreq, &state->fnum);
94 TALLOC_FREE(subreq);
95 if (tevent_req_nterror(req, status)) {
96 return;
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,
106 &data, &data_len)) {
107 tevent_req_oom(req);
108 return;
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)) {
118 return;
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(
131 subreq, NULL, NULL,
132 NULL, 0, NULL, /* rsetup */
133 NULL, 0, NULL, /* rparam */
134 NULL, 0, NULL); /* rdata */
135 TALLOC_FREE(subreq);
137 if (NT_STATUS_IS_OK(state->set_reparse_status)) {
138 subreq = cli_close_send(state, state->ev, state->cli,
139 state->fnum);
140 if (tevent_req_nomem(subreq, req)) {
141 return;
143 tevent_req_set_callback(subreq, cli_symlink_close_done, req);
144 return;
146 subreq = cli_nt_delete_on_close_send(
147 state, state->ev, state->cli, state->fnum, true);
148 if (tevent_req_nomem(subreq, req)) {
149 return;
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);
160 NTSTATUS status;
162 status = cli_nt_delete_on_close_recv(subreq);
163 TALLOC_FREE(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)) {
171 return;
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);
182 NTSTATUS status;
184 status = cli_close_recv(subreq);
185 TALLOC_FREE(subreq);
187 if (tevent_req_nterror(req, status)) {
188 return;
190 if (tevent_req_nterror(req, state->set_reparse_status)) {
191 return;
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;
211 goto fail;
213 ev = event_context_init(frame);
214 if (ev == NULL) {
215 goto fail;
217 req = cli_symlink_send(frame, ev, cli, oldname, newname, flags);
218 if (req == NULL) {
219 goto fail;
221 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
222 goto fail;
224 status = cli_symlink_recv(req);
225 fail:
226 TALLOC_FREE(frame);
227 return status;
230 struct cli_readlink_state {
231 struct tevent_context *ev;
232 struct cli_state *cli;
233 uint16_t fnum;
235 uint16_t setup[4];
236 NTSTATUS get_reparse_status;
237 uint8_t *data;
238 uint32_t num_data;
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,
248 const char *fname)
250 struct tevent_req *req, *subreq;
251 struct cli_readlink_state *state;
253 req = tevent_req_create(mem_ctx, &state, struct cli_readlink_state);
254 if (req == NULL) {
255 return NULL;
257 state->ev = ev;
258 state->cli = cli;
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);
268 return 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);
277 NTSTATUS status;
279 status = cli_ntcreate_recv(subreq, &state->fnum);
280 TALLOC_FREE(subreq);
281 if (tevent_req_nterror(req, status)) {
282 return;
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)) {
297 return;
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(
310 subreq, state, NULL,
311 NULL, 0, NULL, /* rsetup */
312 NULL, 0, NULL, /* rparam */
313 &state->data, 20, &state->num_data); /* rdata */
314 TALLOC_FREE(subreq);
316 subreq = cli_close_send(state, state->ev, state->cli, state->fnum);
317 if (tevent_req_nomem(subreq, req)) {
318 return;
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);
327 NTSTATUS status;
329 status = cli_close_recv(subreq);
330 TALLOC_FREE(subreq);
331 if (tevent_req_nterror(req, status)) {
332 return;
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,
339 uint32_t *pflags)
341 struct cli_readlink_state *state = tevent_req_data(
342 req, struct cli_readlink_state);
343 NTSTATUS status;
344 char *substitute_name;
345 char *print_name;
346 uint32_t flags;
348 if (tevent_req_is_nterror(req, &status)) {
349 return 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) {
369 *pflags = flags;
371 return NT_STATUS_OK;
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;
385 goto fail;
387 ev = event_context_init(frame);
388 if (ev == NULL) {
389 goto fail;
391 req = cli_readlink_send(frame, ev, cli, fname);
392 if (req == NULL) {
393 goto fail;
395 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
396 goto fail;
398 status = cli_readlink_recv(req, mem_ctx, psubstitute_name,
399 pprint_name, pflags);
400 fail:
401 TALLOC_FREE(frame);
402 return status;