source: vendor/3.6.0/source3/libsmb/clifile.c

Last change on this file was 740, checked in by Silvan Scherrer, 13 years ago

Samba Server: update vendor to 3.6.0

File size: 133.3 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 client file operations
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "includes.h"
22#include "system/filesys.h"
23#include "libsmb/libsmb.h"
24#include "../lib/util/tevent_ntstatus.h"
25#include "async_smb.h"
26#include "libsmb/clirap.h"
27#include "trans2.h"
28#include "ntioctl.h"
29
30/***********************************************************
31 Common function for pushing stings, used by smb_bytes_push_str()
32 and trans_bytes_push_str(). Only difference is the align_odd
33 parameter setting.
34***********************************************************/
35
36static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
37 const char *str, size_t str_len,
38 bool align_odd,
39 size_t *pconverted_size)
40{
41 size_t buflen;
42 char *converted;
43 size_t converted_size;
44
45 if (buf == NULL) {
46 return NULL;
47 }
48
49 buflen = talloc_get_size(buf);
50
51 if (align_odd && ucs2 && (buflen % 2 == 0)) {
52 /*
53 * We're pushing into an SMB buffer, align odd
54 */
55 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
56 if (buf == NULL) {
57 return NULL;
58 }
59 buf[buflen] = '\0';
60 buflen += 1;
61 }
62
63 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
64 ucs2 ? CH_UTF16LE : CH_DOS,
65 str, str_len, &converted,
66 &converted_size, true)) {
67 return NULL;
68 }
69
70 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
71 buflen + converted_size);
72 if (buf == NULL) {
73 TALLOC_FREE(converted);
74 return NULL;
75 }
76
77 memcpy(buf + buflen, converted, converted_size);
78
79 TALLOC_FREE(converted);
80
81 if (pconverted_size) {
82 *pconverted_size = converted_size;
83 }
84
85 return buf;
86}
87
88/***********************************************************
89 Push a string into an SMB buffer, with odd byte alignment
90 if it's a UCS2 string.
91***********************************************************/
92
93uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
94 const char *str, size_t str_len,
95 size_t *pconverted_size)
96{
97 return internal_bytes_push_str(buf, ucs2, str, str_len,
98 true, pconverted_size);
99}
100
101uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
102 const uint8_t *bytes, size_t num_bytes)
103{
104 size_t buflen;
105
106 if (buf == NULL) {
107 return NULL;
108 }
109 buflen = talloc_get_size(buf);
110
111 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
112 buflen + 1 + num_bytes);
113 if (buf == NULL) {
114 return NULL;
115 }
116 buf[buflen] = prefix;
117 memcpy(&buf[buflen+1], bytes, num_bytes);
118 return buf;
119}
120
121/***********************************************************
122 Same as smb_bytes_push_str(), but without the odd byte
123 align for ucs2 (we're pushing into a param or data block).
124 static for now, although this will probably change when
125 other modules use async trans calls.
126***********************************************************/
127
128static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
129 const char *str, size_t str_len,
130 size_t *pconverted_size)
131{
132 return internal_bytes_push_str(buf, ucs2, str, str_len,
133 false, pconverted_size);
134}
135
136struct cli_setpathinfo_state {
137 uint16_t setup;
138 uint8_t *param;
139};
140
141static void cli_setpathinfo_done(struct tevent_req *subreq);
142
143struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
144 struct tevent_context *ev,
145 struct cli_state *cli,
146 uint16_t level,
147 const char *path,
148 uint8_t *data,
149 size_t data_len)
150{
151 struct tevent_req *req, *subreq;
152 struct cli_setpathinfo_state *state;
153
154 req = tevent_req_create(mem_ctx, &state,
155 struct cli_setpathinfo_state);
156 if (req == NULL) {
157 return NULL;
158 }
159
160 /* Setup setup word. */
161 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
162
163 /* Setup param array. */
164 state->param = TALLOC_ZERO_ARRAY(state, uint8_t, 6);
165 if (tevent_req_nomem(state->param, req)) {
166 return tevent_req_post(req, ev);
167 }
168 SSVAL(state->param, 0, level);
169
170 state->param = trans2_bytes_push_str(
171 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
172 if (tevent_req_nomem(state->param, req)) {
173 return tevent_req_post(req, ev);
174 }
175
176 subreq = cli_trans_send(
177 state, /* mem ctx. */
178 ev, /* event ctx. */
179 cli, /* cli_state. */
180 SMBtrans2, /* cmd. */
181 NULL, /* pipe name. */
182 -1, /* fid. */
183 0, /* function. */
184 0, /* flags. */
185 &state->setup, /* setup. */
186 1, /* num setup uint16_t words. */
187 0, /* max returned setup. */
188 state->param, /* param. */
189 talloc_get_size(state->param), /* num param. */
190 2, /* max returned param. */
191 data, /* data. */
192 data_len, /* num data. */
193 0); /* max returned data. */
194
195 if (tevent_req_nomem(subreq, req)) {
196 return tevent_req_post(req, ev);
197 }
198 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
199 return req;
200}
201
202static void cli_setpathinfo_done(struct tevent_req *subreq)
203{
204 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
205 NULL, 0, NULL, NULL, 0, NULL);
206 tevent_req_simple_finish_ntstatus(subreq, status);
207}
208
209NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
210{
211 return tevent_req_simple_recv_ntstatus(req);
212}
213
214NTSTATUS cli_setpathinfo(struct cli_state *cli,
215 uint16_t level,
216 const char *path,
217 uint8_t *data,
218 size_t data_len)
219{
220 TALLOC_CTX *frame = talloc_stackframe();
221 struct tevent_context *ev;
222 struct tevent_req *req;
223 NTSTATUS status = NT_STATUS_NO_MEMORY;
224
225 if (cli_has_async_calls(cli)) {
226 /*
227 * Can't use sync call while an async call is in flight
228 */
229 status = NT_STATUS_INVALID_PARAMETER;
230 goto fail;
231 }
232 ev = tevent_context_init(frame);
233 if (ev == NULL) {
234 goto fail;
235 }
236 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
237 if (req == NULL) {
238 goto fail;
239 }
240 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
241 goto fail;
242 }
243 status = cli_setpathinfo_recv(req);
244 fail:
245 TALLOC_FREE(frame);
246 return status;
247}
248
249/****************************************************************************
250 Hard/Symlink a file (UNIX extensions).
251 Creates new name (sym)linked to oldname.
252****************************************************************************/
253
254struct cli_posix_link_internal_state {
255 uint8_t *data;
256};
257
258static void cli_posix_link_internal_done(struct tevent_req *subreq);
259
260static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
261 struct event_context *ev,
262 struct cli_state *cli,
263 uint16_t level,
264 const char *oldname,
265 const char *newname)
266{
267 struct tevent_req *req = NULL, *subreq = NULL;
268 struct cli_posix_link_internal_state *state = NULL;
269
270 req = tevent_req_create(mem_ctx, &state,
271 struct cli_posix_link_internal_state);
272 if (req == NULL) {
273 return NULL;
274 }
275
276 /* Setup data array. */
277 state->data = talloc_array(state, uint8_t, 0);
278 if (tevent_req_nomem(state->data, req)) {
279 return tevent_req_post(req, ev);
280 }
281 state->data = trans2_bytes_push_str(
282 state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
283
284 subreq = cli_setpathinfo_send(
285 state, ev, cli, level, newname,
286 state->data, talloc_get_size(state->data));
287 if (tevent_req_nomem(subreq, req)) {
288 return tevent_req_post(req, ev);
289 }
290 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
291 return req;
292}
293
294static void cli_posix_link_internal_done(struct tevent_req *subreq)
295{
296 NTSTATUS status = cli_setpathinfo_recv(subreq);
297 tevent_req_simple_finish_ntstatus(subreq, status);
298}
299
300/****************************************************************************
301 Symlink a file (UNIX extensions).
302****************************************************************************/
303
304struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
305 struct event_context *ev,
306 struct cli_state *cli,
307 const char *oldname,
308 const char *newname)
309{
310 return cli_posix_link_internal_send(
311 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
312}
313
314NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
315{
316 return tevent_req_simple_recv_ntstatus(req);
317}
318
319NTSTATUS cli_posix_symlink(struct cli_state *cli,
320 const char *oldname,
321 const char *newname)
322{
323 TALLOC_CTX *frame = talloc_stackframe();
324 struct event_context *ev = NULL;
325 struct tevent_req *req = NULL;
326 NTSTATUS status = NT_STATUS_OK;
327
328 if (cli_has_async_calls(cli)) {
329 /*
330 * Can't use sync call while an async call is in flight
331 */
332 status = NT_STATUS_INVALID_PARAMETER;
333 goto fail;
334 }
335
336 ev = event_context_init(frame);
337 if (ev == NULL) {
338 status = NT_STATUS_NO_MEMORY;
339 goto fail;
340 }
341
342 req = cli_posix_symlink_send(frame,
343 ev,
344 cli,
345 oldname,
346 newname);
347 if (req == NULL) {
348 status = NT_STATUS_NO_MEMORY;
349 goto fail;
350 }
351
352 if (!tevent_req_poll(req, ev)) {
353 status = map_nt_error_from_unix(errno);
354 goto fail;
355 }
356
357 status = cli_posix_symlink_recv(req);
358
359 fail:
360 TALLOC_FREE(frame);
361 if (!NT_STATUS_IS_OK(status)) {
362 cli_set_error(cli, status);
363 }
364 return status;
365}
366
367/****************************************************************************
368 Read a POSIX symlink.
369****************************************************************************/
370
371struct readlink_state {
372 uint8_t *data;
373 uint32_t num_data;
374};
375
376static void cli_posix_readlink_done(struct tevent_req *subreq);
377
378struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
379 struct event_context *ev,
380 struct cli_state *cli,
381 const char *fname,
382 size_t len)
383{
384 struct tevent_req *req = NULL, *subreq = NULL;
385 struct readlink_state *state = NULL;
386 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
387
388 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
389 if (req == NULL) {
390 return NULL;
391 }
392
393 /*
394 * Len is in bytes, we need it in UCS2 units.
395 */
396 if ((2*len < len) || (maxbytelen < len)) {
397 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
398 return tevent_req_post(req, ev);
399 }
400
401 subreq = cli_qpathinfo_send(state, ev, cli, fname,
402 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
403 if (tevent_req_nomem(subreq, req)) {
404 return tevent_req_post(req, ev);
405 }
406 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
407 return req;
408}
409
410static void cli_posix_readlink_done(struct tevent_req *subreq)
411{
412 struct tevent_req *req = tevent_req_callback_data(
413 subreq, struct tevent_req);
414 struct readlink_state *state = tevent_req_data(
415 req, struct readlink_state);
416 NTSTATUS status;
417
418 status = cli_qpathinfo_recv(subreq, state, &state->data,
419 &state->num_data);
420 TALLOC_FREE(subreq);
421 if (tevent_req_nterror(req, status)) {
422 return;
423 }
424 /*
425 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
426 */
427 if (state->data[state->num_data-1] != '\0') {
428 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
429 return;
430 }
431 tevent_req_done(req);
432}
433
434NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
435 char *retpath, size_t len)
436{
437 NTSTATUS status;
438 char *converted = NULL;
439 size_t converted_size = 0;
440 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
441
442 if (tevent_req_is_nterror(req, &status)) {
443 return status;
444 }
445 /* The returned data is a pushed string, not raw data. */
446 if (!convert_string_talloc(state,
447 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
448 CH_UNIX,
449 state->data,
450 state->num_data,
451 &converted,
452 &converted_size,
453 true)) {
454 return NT_STATUS_NO_MEMORY;
455 }
456
457 len = MIN(len,converted_size);
458 if (len == 0) {
459 return NT_STATUS_DATA_ERROR;
460 }
461 memcpy(retpath, converted, len);
462 return NT_STATUS_OK;
463}
464
465NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
466 char *linkpath, size_t len)
467{
468 TALLOC_CTX *frame = talloc_stackframe();
469 struct event_context *ev = NULL;
470 struct tevent_req *req = NULL;
471 NTSTATUS status = NT_STATUS_OK;
472
473 if (cli_has_async_calls(cli)) {
474 /*
475 * Can't use sync call while an async call is in flight
476 */
477 status = NT_STATUS_INVALID_PARAMETER;
478 goto fail;
479 }
480
481 ev = event_context_init(frame);
482 if (ev == NULL) {
483 status = NT_STATUS_NO_MEMORY;
484 goto fail;
485 }
486
487 req = cli_posix_readlink_send(frame,
488 ev,
489 cli,
490 fname,
491 len);
492 if (req == NULL) {
493 status = NT_STATUS_NO_MEMORY;
494 goto fail;
495 }
496
497 if (!tevent_req_poll(req, ev)) {
498 status = map_nt_error_from_unix(errno);
499 goto fail;
500 }
501
502 status = cli_posix_readlink_recv(req, cli, linkpath, len);
503
504 fail:
505 TALLOC_FREE(frame);
506 if (!NT_STATUS_IS_OK(status)) {
507 cli_set_error(cli, status);
508 }
509 return status;
510}
511
512/****************************************************************************
513 Hard link a file (UNIX extensions).
514****************************************************************************/
515
516struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
517 struct event_context *ev,
518 struct cli_state *cli,
519 const char *oldname,
520 const char *newname)
521{
522 return cli_posix_link_internal_send(
523 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
524}
525
526NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
527{
528 return tevent_req_simple_recv_ntstatus(req);
529}
530
531NTSTATUS cli_posix_hardlink(struct cli_state *cli,
532 const char *oldname,
533 const char *newname)
534{
535 TALLOC_CTX *frame = talloc_stackframe();
536 struct event_context *ev = NULL;
537 struct tevent_req *req = NULL;
538 NTSTATUS status = NT_STATUS_OK;
539
540 if (cli_has_async_calls(cli)) {
541 /*
542 * Can't use sync call while an async call is in flight
543 */
544 status = NT_STATUS_INVALID_PARAMETER;
545 goto fail;
546 }
547
548 ev = event_context_init(frame);
549 if (ev == NULL) {
550 status = NT_STATUS_NO_MEMORY;
551 goto fail;
552 }
553
554 req = cli_posix_hardlink_send(frame,
555 ev,
556 cli,
557 oldname,
558 newname);
559 if (req == NULL) {
560 status = NT_STATUS_NO_MEMORY;
561 goto fail;
562 }
563
564 if (!tevent_req_poll(req, ev)) {
565 status = map_nt_error_from_unix(errno);
566 goto fail;
567 }
568
569 status = cli_posix_hardlink_recv(req);
570
571 fail:
572 TALLOC_FREE(frame);
573 if (!NT_STATUS_IS_OK(status)) {
574 cli_set_error(cli, status);
575 }
576 return status;
577}
578
579/****************************************************************************
580 Map standard UNIX permissions onto wire representations.
581****************************************************************************/
582
583uint32_t unix_perms_to_wire(mode_t perms)
584{
585 unsigned int ret = 0;
586
587 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
588 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
589 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
590 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
591 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
592 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
593 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
594 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
595 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
596#ifdef S_ISVTX
597 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
598#endif
599#ifdef S_ISGID
600 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
601#endif
602#ifdef S_ISUID
603 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
604#endif
605 return ret;
606}
607
608/****************************************************************************
609 Map wire permissions to standard UNIX.
610****************************************************************************/
611
612mode_t wire_perms_to_unix(uint32_t perms)
613{
614 mode_t ret = (mode_t)0;
615
616 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
617 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
618 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
619 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
620 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
621 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
622 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
623 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
624 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
625#ifdef S_ISVTX
626 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
627#endif
628#ifdef S_ISGID
629 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
630#endif
631#ifdef S_ISUID
632 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
633#endif
634 return ret;
635}
636
637/****************************************************************************
638 Return the file type from the wire filetype for UNIX extensions.
639****************************************************************************/
640
641static mode_t unix_filetype_from_wire(uint32_t wire_type)
642{
643 switch (wire_type) {
644 case UNIX_TYPE_FILE:
645 return S_IFREG;
646 case UNIX_TYPE_DIR:
647 return S_IFDIR;
648#ifdef S_IFLNK
649 case UNIX_TYPE_SYMLINK:
650 return S_IFLNK;
651#endif
652#ifdef S_IFCHR
653 case UNIX_TYPE_CHARDEV:
654 return S_IFCHR;
655#endif
656#ifdef S_IFBLK
657 case UNIX_TYPE_BLKDEV:
658 return S_IFBLK;
659#endif
660#ifdef S_IFIFO
661 case UNIX_TYPE_FIFO:
662 return S_IFIFO;
663#endif
664#ifdef S_IFSOCK
665 case UNIX_TYPE_SOCKET:
666 return S_IFSOCK;
667#endif
668 default:
669 return (mode_t)0;
670 }
671}
672
673/****************************************************************************
674 Do a POSIX getfacl (UNIX extensions).
675****************************************************************************/
676
677struct getfacl_state {
678 uint32_t num_data;
679 uint8_t *data;
680};
681
682static void cli_posix_getfacl_done(struct tevent_req *subreq);
683
684struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
685 struct event_context *ev,
686 struct cli_state *cli,
687 const char *fname)
688{
689 struct tevent_req *req = NULL, *subreq = NULL;
690 struct getfacl_state *state = NULL;
691
692 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
693 if (req == NULL) {
694 return NULL;
695 }
696 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
697 0, cli->max_xmit);
698 if (tevent_req_nomem(subreq, req)) {
699 return tevent_req_post(req, ev);
700 }
701 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
702 return req;
703}
704
705static void cli_posix_getfacl_done(struct tevent_req *subreq)
706{
707 struct tevent_req *req = tevent_req_callback_data(
708 subreq, struct tevent_req);
709 struct getfacl_state *state = tevent_req_data(
710 req, struct getfacl_state);
711 NTSTATUS status;
712
713 status = cli_qpathinfo_recv(subreq, state, &state->data,
714 &state->num_data);
715 TALLOC_FREE(subreq);
716 if (tevent_req_nterror(req, status)) {
717 return;
718 }
719 tevent_req_done(req);
720}
721
722NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
723 TALLOC_CTX *mem_ctx,
724 size_t *prb_size,
725 char **retbuf)
726{
727 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
728 NTSTATUS status;
729
730 if (tevent_req_is_nterror(req, &status)) {
731 return status;
732 }
733 *prb_size = (size_t)state->num_data;
734 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
735 return NT_STATUS_OK;
736}
737
738NTSTATUS cli_posix_getfacl(struct cli_state *cli,
739 const char *fname,
740 TALLOC_CTX *mem_ctx,
741 size_t *prb_size,
742 char **retbuf)
743{
744 TALLOC_CTX *frame = talloc_stackframe();
745 struct event_context *ev = NULL;
746 struct tevent_req *req = NULL;
747 NTSTATUS status = NT_STATUS_OK;
748
749 if (cli_has_async_calls(cli)) {
750 /*
751 * Can't use sync call while an async call is in flight
752 */
753 status = NT_STATUS_INVALID_PARAMETER;
754 goto fail;
755 }
756
757 ev = event_context_init(frame);
758 if (ev == NULL) {
759 status = NT_STATUS_NO_MEMORY;
760 goto fail;
761 }
762
763 req = cli_posix_getfacl_send(frame,
764 ev,
765 cli,
766 fname);
767 if (req == NULL) {
768 status = NT_STATUS_NO_MEMORY;
769 goto fail;
770 }
771
772 if (!tevent_req_poll(req, ev)) {
773 status = map_nt_error_from_unix(errno);
774 goto fail;
775 }
776
777 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
778
779 fail:
780 TALLOC_FREE(frame);
781 if (!NT_STATUS_IS_OK(status)) {
782 cli_set_error(cli, status);
783 }
784 return status;
785}
786
787/****************************************************************************
788 Stat a file (UNIX extensions).
789****************************************************************************/
790
791struct stat_state {
792 uint32_t num_data;
793 uint8_t *data;
794};
795
796static void cli_posix_stat_done(struct tevent_req *subreq);
797
798struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
799 struct event_context *ev,
800 struct cli_state *cli,
801 const char *fname)
802{
803 struct tevent_req *req = NULL, *subreq = NULL;
804 struct stat_state *state = NULL;
805
806 req = tevent_req_create(mem_ctx, &state, struct stat_state);
807 if (req == NULL) {
808 return NULL;
809 }
810 subreq = cli_qpathinfo_send(state, ev, cli, fname,
811 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
812 if (tevent_req_nomem(subreq, req)) {
813 return tevent_req_post(req, ev);
814 }
815 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
816 return req;
817}
818
819static void cli_posix_stat_done(struct tevent_req *subreq)
820{
821 struct tevent_req *req = tevent_req_callback_data(
822 subreq, struct tevent_req);
823 struct stat_state *state = tevent_req_data(req, struct stat_state);
824 NTSTATUS status;
825
826 status = cli_qpathinfo_recv(subreq, state, &state->data,
827 &state->num_data);
828 TALLOC_FREE(subreq);
829 if (tevent_req_nterror(req, status)) {
830 return;
831 }
832 tevent_req_done(req);
833}
834
835NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
836 SMB_STRUCT_STAT *sbuf)
837{
838 struct stat_state *state = tevent_req_data(req, struct stat_state);
839 NTSTATUS status;
840
841 if (tevent_req_is_nterror(req, &status)) {
842 return status;
843 }
844
845 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
846 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
847#if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
848 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
849#else
850 /* assume 512 byte blocks */
851 sbuf->st_ex_blocks /= 512;
852#endif
853 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
854 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
855 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
856
857 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
858 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
859 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
860#if defined(HAVE_MAKEDEV)
861 {
862 uint32_t dev_major = IVAL(state->data,60);
863 uint32_t dev_minor = IVAL(state->data,68);
864 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
865 }
866#endif
867 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
868 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
869 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
870
871 return NT_STATUS_OK;
872}
873
874NTSTATUS cli_posix_stat(struct cli_state *cli,
875 const char *fname,
876 SMB_STRUCT_STAT *sbuf)
877{
878 TALLOC_CTX *frame = talloc_stackframe();
879 struct event_context *ev = NULL;
880 struct tevent_req *req = NULL;
881 NTSTATUS status = NT_STATUS_OK;
882
883 if (cli_has_async_calls(cli)) {
884 /*
885 * Can't use sync call while an async call is in flight
886 */
887 status = NT_STATUS_INVALID_PARAMETER;
888 goto fail;
889 }
890
891 ev = event_context_init(frame);
892 if (ev == NULL) {
893 status = NT_STATUS_NO_MEMORY;
894 goto fail;
895 }
896
897 req = cli_posix_stat_send(frame,
898 ev,
899 cli,
900 fname);
901 if (req == NULL) {
902 status = NT_STATUS_NO_MEMORY;
903 goto fail;
904 }
905
906 if (!tevent_req_poll(req, ev)) {
907 status = map_nt_error_from_unix(errno);
908 goto fail;
909 }
910
911 status = cli_posix_stat_recv(req, sbuf);
912
913 fail:
914 TALLOC_FREE(frame);
915 if (!NT_STATUS_IS_OK(status)) {
916 cli_set_error(cli, status);
917 }
918 return status;
919}
920
921/****************************************************************************
922 Chmod or chown a file internal (UNIX extensions).
923****************************************************************************/
924
925struct cli_posix_chown_chmod_internal_state {
926 uint8_t data[100];
927};
928
929static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
930
931static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
932 struct event_context *ev,
933 struct cli_state *cli,
934 const char *fname,
935 uint32_t mode,
936 uint32_t uid,
937 uint32_t gid)
938{
939 struct tevent_req *req = NULL, *subreq = NULL;
940 struct cli_posix_chown_chmod_internal_state *state = NULL;
941
942 req = tevent_req_create(mem_ctx, &state,
943 struct cli_posix_chown_chmod_internal_state);
944 if (req == NULL) {
945 return NULL;
946 }
947
948 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
949 memset(&state->data[40], '\0', 60);
950 SIVAL(state->data,40,uid);
951 SIVAL(state->data,48,gid);
952 SIVAL(state->data,84,mode);
953
954 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
955 fname, state->data, sizeof(state->data));
956 if (tevent_req_nomem(subreq, req)) {
957 return tevent_req_post(req, ev);
958 }
959 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
960 req);
961 return req;
962}
963
964static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
965{
966 NTSTATUS status = cli_setpathinfo_recv(subreq);
967 tevent_req_simple_finish_ntstatus(subreq, status);
968}
969
970/****************************************************************************
971 chmod a file (UNIX extensions).
972****************************************************************************/
973
974struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
975 struct event_context *ev,
976 struct cli_state *cli,
977 const char *fname,
978 mode_t mode)
979{
980 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
981 fname,
982 unix_perms_to_wire(mode),
983 SMB_UID_NO_CHANGE,
984 SMB_GID_NO_CHANGE);
985}
986
987NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
988{
989 return tevent_req_simple_recv_ntstatus(req);
990}
991
992NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
993{
994 TALLOC_CTX *frame = talloc_stackframe();
995 struct event_context *ev = NULL;
996 struct tevent_req *req = NULL;
997 NTSTATUS status = NT_STATUS_OK;
998
999 if (cli_has_async_calls(cli)) {
1000 /*
1001 * Can't use sync call while an async call is in flight
1002 */
1003 status = NT_STATUS_INVALID_PARAMETER;
1004 goto fail;
1005 }
1006
1007 ev = event_context_init(frame);
1008 if (ev == NULL) {
1009 status = NT_STATUS_NO_MEMORY;
1010 goto fail;
1011 }
1012
1013 req = cli_posix_chmod_send(frame,
1014 ev,
1015 cli,
1016 fname,
1017 mode);
1018 if (req == NULL) {
1019 status = NT_STATUS_NO_MEMORY;
1020 goto fail;
1021 }
1022
1023 if (!tevent_req_poll(req, ev)) {
1024 status = map_nt_error_from_unix(errno);
1025 goto fail;
1026 }
1027
1028 status = cli_posix_chmod_recv(req);
1029
1030 fail:
1031 TALLOC_FREE(frame);
1032 if (!NT_STATUS_IS_OK(status)) {
1033 cli_set_error(cli, status);
1034 }
1035 return status;
1036}
1037
1038/****************************************************************************
1039 chown a file (UNIX extensions).
1040****************************************************************************/
1041
1042struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1043 struct event_context *ev,
1044 struct cli_state *cli,
1045 const char *fname,
1046 uid_t uid,
1047 gid_t gid)
1048{
1049 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1050 fname,
1051 SMB_MODE_NO_CHANGE,
1052 (uint32_t)uid,
1053 (uint32_t)gid);
1054}
1055
1056NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1057{
1058 return tevent_req_simple_recv_ntstatus(req);
1059}
1060
1061NTSTATUS cli_posix_chown(struct cli_state *cli,
1062 const char *fname,
1063 uid_t uid,
1064 gid_t gid)
1065{
1066 TALLOC_CTX *frame = talloc_stackframe();
1067 struct event_context *ev = NULL;
1068 struct tevent_req *req = NULL;
1069 NTSTATUS status = NT_STATUS_OK;
1070
1071 if (cli_has_async_calls(cli)) {
1072 /*
1073 * Can't use sync call while an async call is in flight
1074 */
1075 status = NT_STATUS_INVALID_PARAMETER;
1076 goto fail;
1077 }
1078
1079 ev = event_context_init(frame);
1080 if (ev == NULL) {
1081 status = NT_STATUS_NO_MEMORY;
1082 goto fail;
1083 }
1084
1085 req = cli_posix_chown_send(frame,
1086 ev,
1087 cli,
1088 fname,
1089 uid,
1090 gid);
1091 if (req == NULL) {
1092 status = NT_STATUS_NO_MEMORY;
1093 goto fail;
1094 }
1095
1096 if (!tevent_req_poll(req, ev)) {
1097 status = map_nt_error_from_unix(errno);
1098 goto fail;
1099 }
1100
1101 status = cli_posix_chown_recv(req);
1102
1103 fail:
1104 TALLOC_FREE(frame);
1105 if (!NT_STATUS_IS_OK(status)) {
1106 cli_set_error(cli, status);
1107 }
1108 return status;
1109}
1110
1111/****************************************************************************
1112 Rename a file.
1113****************************************************************************/
1114
1115static void cli_rename_done(struct tevent_req *subreq);
1116
1117struct cli_rename_state {
1118 uint16_t vwv[1];
1119};
1120
1121struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1122 struct event_context *ev,
1123 struct cli_state *cli,
1124 const char *fname_src,
1125 const char *fname_dst)
1126{
1127 struct tevent_req *req = NULL, *subreq = NULL;
1128 struct cli_rename_state *state = NULL;
1129 uint8_t additional_flags = 0;
1130 uint8_t *bytes = NULL;
1131
1132 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1133 if (req == NULL) {
1134 return NULL;
1135 }
1136
1137 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1138
1139 bytes = talloc_array(state, uint8_t, 1);
1140 if (tevent_req_nomem(bytes, req)) {
1141 return tevent_req_post(req, ev);
1142 }
1143 bytes[0] = 4;
1144 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1145 strlen(fname_src)+1, NULL);
1146 if (tevent_req_nomem(bytes, req)) {
1147 return tevent_req_post(req, ev);
1148 }
1149
1150 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1151 talloc_get_size(bytes)+1);
1152 if (tevent_req_nomem(bytes, req)) {
1153 return tevent_req_post(req, ev);
1154 }
1155
1156 bytes[talloc_get_size(bytes)-1] = 4;
1157 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1158 strlen(fname_dst)+1, NULL);
1159 if (tevent_req_nomem(bytes, req)) {
1160 return tevent_req_post(req, ev);
1161 }
1162
1163 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1164 1, state->vwv, talloc_get_size(bytes), bytes);
1165 if (tevent_req_nomem(subreq, req)) {
1166 return tevent_req_post(req, ev);
1167 }
1168 tevent_req_set_callback(subreq, cli_rename_done, req);
1169 return req;
1170}
1171
1172static void cli_rename_done(struct tevent_req *subreq)
1173{
1174 struct tevent_req *req = tevent_req_callback_data(
1175 subreq, struct tevent_req);
1176 NTSTATUS status;
1177
1178 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1179 TALLOC_FREE(subreq);
1180 if (tevent_req_nterror(req, status)) {
1181 return;
1182 }
1183 tevent_req_done(req);
1184}
1185
1186NTSTATUS cli_rename_recv(struct tevent_req *req)
1187{
1188 return tevent_req_simple_recv_ntstatus(req);
1189}
1190
1191NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1192{
1193 TALLOC_CTX *frame = talloc_stackframe();
1194 struct event_context *ev;
1195 struct tevent_req *req;
1196 NTSTATUS status = NT_STATUS_OK;
1197
1198 if (cli_has_async_calls(cli)) {
1199 /*
1200 * Can't use sync call while an async call is in flight
1201 */
1202 status = NT_STATUS_INVALID_PARAMETER;
1203 goto fail;
1204 }
1205
1206 ev = event_context_init(frame);
1207 if (ev == NULL) {
1208 status = NT_STATUS_NO_MEMORY;
1209 goto fail;
1210 }
1211
1212 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1213 if (req == NULL) {
1214 status = NT_STATUS_NO_MEMORY;
1215 goto fail;
1216 }
1217
1218 if (!tevent_req_poll(req, ev)) {
1219 status = map_nt_error_from_unix(errno);
1220 goto fail;
1221 }
1222
1223 status = cli_rename_recv(req);
1224
1225 fail:
1226 TALLOC_FREE(frame);
1227 if (!NT_STATUS_IS_OK(status)) {
1228 cli_set_error(cli, status);
1229 }
1230 return status;
1231}
1232
1233/****************************************************************************
1234 NT Rename a file.
1235****************************************************************************/
1236
1237static void cli_ntrename_internal_done(struct tevent_req *subreq);
1238
1239struct cli_ntrename_internal_state {
1240 uint16_t vwv[4];
1241};
1242
1243static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1244 struct event_context *ev,
1245 struct cli_state *cli,
1246 const char *fname_src,
1247 const char *fname_dst,
1248 uint16_t rename_flag)
1249{
1250 struct tevent_req *req = NULL, *subreq = NULL;
1251 struct cli_ntrename_internal_state *state = NULL;
1252 uint8_t additional_flags = 0;
1253 uint8_t *bytes = NULL;
1254
1255 req = tevent_req_create(mem_ctx, &state,
1256 struct cli_ntrename_internal_state);
1257 if (req == NULL) {
1258 return NULL;
1259 }
1260
1261 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1262 SSVAL(state->vwv+1, 0, rename_flag);
1263
1264 bytes = talloc_array(state, uint8_t, 1);
1265 if (tevent_req_nomem(bytes, req)) {
1266 return tevent_req_post(req, ev);
1267 }
1268 bytes[0] = 4;
1269 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1270 strlen(fname_src)+1, NULL);
1271 if (tevent_req_nomem(bytes, req)) {
1272 return tevent_req_post(req, ev);
1273 }
1274
1275 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1276 talloc_get_size(bytes)+1);
1277 if (tevent_req_nomem(bytes, req)) {
1278 return tevent_req_post(req, ev);
1279 }
1280
1281 bytes[talloc_get_size(bytes)-1] = 4;
1282 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1283 strlen(fname_dst)+1, NULL);
1284 if (tevent_req_nomem(bytes, req)) {
1285 return tevent_req_post(req, ev);
1286 }
1287
1288 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1289 4, state->vwv, talloc_get_size(bytes), bytes);
1290 if (tevent_req_nomem(subreq, req)) {
1291 return tevent_req_post(req, ev);
1292 }
1293 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1294 return req;
1295}
1296
1297static void cli_ntrename_internal_done(struct tevent_req *subreq)
1298{
1299 struct tevent_req *req = tevent_req_callback_data(
1300 subreq, struct tevent_req);
1301 NTSTATUS status;
1302
1303 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1304 TALLOC_FREE(subreq);
1305 if (tevent_req_nterror(req, status)) {
1306 return;
1307 }
1308 tevent_req_done(req);
1309}
1310
1311static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1312{
1313 return tevent_req_simple_recv_ntstatus(req);
1314}
1315
1316struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1317 struct event_context *ev,
1318 struct cli_state *cli,
1319 const char *fname_src,
1320 const char *fname_dst)
1321{
1322 return cli_ntrename_internal_send(mem_ctx,
1323 ev,
1324 cli,
1325 fname_src,
1326 fname_dst,
1327 RENAME_FLAG_RENAME);
1328}
1329
1330NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1331{
1332 return cli_ntrename_internal_recv(req);
1333}
1334
1335NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1336{
1337 TALLOC_CTX *frame = talloc_stackframe();
1338 struct event_context *ev;
1339 struct tevent_req *req;
1340 NTSTATUS status = NT_STATUS_OK;
1341
1342 if (cli_has_async_calls(cli)) {
1343 /*
1344 * Can't use sync call while an async call is in flight
1345 */
1346 status = NT_STATUS_INVALID_PARAMETER;
1347 goto fail;
1348 }
1349
1350 ev = event_context_init(frame);
1351 if (ev == NULL) {
1352 status = NT_STATUS_NO_MEMORY;
1353 goto fail;
1354 }
1355
1356 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1357 if (req == NULL) {
1358 status = NT_STATUS_NO_MEMORY;
1359 goto fail;
1360 }
1361
1362 if (!tevent_req_poll(req, ev)) {
1363 status = map_nt_error_from_unix(errno);
1364 goto fail;
1365 }
1366
1367 status = cli_ntrename_recv(req);
1368
1369 fail:
1370 TALLOC_FREE(frame);
1371 if (!NT_STATUS_IS_OK(status)) {
1372 cli_set_error(cli, status);
1373 }
1374 return status;
1375}
1376
1377/****************************************************************************
1378 NT hardlink a file.
1379****************************************************************************/
1380
1381struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1382 struct event_context *ev,
1383 struct cli_state *cli,
1384 const char *fname_src,
1385 const char *fname_dst)
1386{
1387 return cli_ntrename_internal_send(mem_ctx,
1388 ev,
1389 cli,
1390 fname_src,
1391 fname_dst,
1392 RENAME_FLAG_HARD_LINK);
1393}
1394
1395NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1396{
1397 return cli_ntrename_internal_recv(req);
1398}
1399
1400NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1401{
1402 TALLOC_CTX *frame = talloc_stackframe();
1403 struct event_context *ev;
1404 struct tevent_req *req;
1405 NTSTATUS status = NT_STATUS_OK;
1406
1407 if (cli_has_async_calls(cli)) {
1408 /*
1409 * Can't use sync call while an async call is in flight
1410 */
1411 status = NT_STATUS_INVALID_PARAMETER;
1412 goto fail;
1413 }
1414
1415 ev = event_context_init(frame);
1416 if (ev == NULL) {
1417 status = NT_STATUS_NO_MEMORY;
1418 goto fail;
1419 }
1420
1421 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1422 if (req == NULL) {
1423 status = NT_STATUS_NO_MEMORY;
1424 goto fail;
1425 }
1426
1427 if (!tevent_req_poll(req, ev)) {
1428 status = map_nt_error_from_unix(errno);
1429 goto fail;
1430 }
1431
1432 status = cli_nt_hardlink_recv(req);
1433
1434 fail:
1435 TALLOC_FREE(frame);
1436 if (!NT_STATUS_IS_OK(status)) {
1437 cli_set_error(cli, status);
1438 }
1439 return status;
1440}
1441
1442/****************************************************************************
1443 Delete a file.
1444****************************************************************************/
1445
1446static void cli_unlink_done(struct tevent_req *subreq);
1447
1448struct cli_unlink_state {
1449 uint16_t vwv[1];
1450};
1451
1452struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1453 struct event_context *ev,
1454 struct cli_state *cli,
1455 const char *fname,
1456 uint16_t mayhave_attrs)
1457{
1458 struct tevent_req *req = NULL, *subreq = NULL;
1459 struct cli_unlink_state *state = NULL;
1460 uint8_t additional_flags = 0;
1461 uint8_t *bytes = NULL;
1462
1463 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1464 if (req == NULL) {
1465 return NULL;
1466 }
1467
1468 SSVAL(state->vwv+0, 0, mayhave_attrs);
1469
1470 bytes = talloc_array(state, uint8_t, 1);
1471 if (tevent_req_nomem(bytes, req)) {
1472 return tevent_req_post(req, ev);
1473 }
1474 bytes[0] = 4;
1475 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1476 strlen(fname)+1, NULL);
1477
1478 if (tevent_req_nomem(bytes, req)) {
1479 return tevent_req_post(req, ev);
1480 }
1481
1482 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1483 1, state->vwv, talloc_get_size(bytes), bytes);
1484 if (tevent_req_nomem(subreq, req)) {
1485 return tevent_req_post(req, ev);
1486 }
1487 tevent_req_set_callback(subreq, cli_unlink_done, req);
1488 return req;
1489}
1490
1491static void cli_unlink_done(struct tevent_req *subreq)
1492{
1493 struct tevent_req *req = tevent_req_callback_data(
1494 subreq, struct tevent_req);
1495 NTSTATUS status;
1496
1497 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1498 TALLOC_FREE(subreq);
1499 if (tevent_req_nterror(req, status)) {
1500 return;
1501 }
1502 tevent_req_done(req);
1503}
1504
1505NTSTATUS cli_unlink_recv(struct tevent_req *req)
1506{
1507 return tevent_req_simple_recv_ntstatus(req);
1508}
1509
1510NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1511{
1512 TALLOC_CTX *frame = talloc_stackframe();
1513 struct event_context *ev;
1514 struct tevent_req *req;
1515 NTSTATUS status = NT_STATUS_OK;
1516
1517 if (cli_has_async_calls(cli)) {
1518 /*
1519 * Can't use sync call while an async call is in flight
1520 */
1521 status = NT_STATUS_INVALID_PARAMETER;
1522 goto fail;
1523 }
1524
1525 ev = event_context_init(frame);
1526 if (ev == NULL) {
1527 status = NT_STATUS_NO_MEMORY;
1528 goto fail;
1529 }
1530
1531 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1532 if (req == NULL) {
1533 status = NT_STATUS_NO_MEMORY;
1534 goto fail;
1535 }
1536
1537 if (!tevent_req_poll(req, ev)) {
1538 status = map_nt_error_from_unix(errno);
1539 goto fail;
1540 }
1541
1542 status = cli_unlink_recv(req);
1543
1544 fail:
1545 TALLOC_FREE(frame);
1546 if (!NT_STATUS_IS_OK(status)) {
1547 cli_set_error(cli, status);
1548 }
1549 return status;
1550}
1551
1552/****************************************************************************
1553 Create a directory.
1554****************************************************************************/
1555
1556static void cli_mkdir_done(struct tevent_req *subreq);
1557
1558struct cli_mkdir_state {
1559 int dummy;
1560};
1561
1562struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1563 struct event_context *ev,
1564 struct cli_state *cli,
1565 const char *dname)
1566{
1567 struct tevent_req *req = NULL, *subreq = NULL;
1568 struct cli_mkdir_state *state = NULL;
1569 uint8_t additional_flags = 0;
1570 uint8_t *bytes = NULL;
1571
1572 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1573 if (req == NULL) {
1574 return NULL;
1575 }
1576
1577 bytes = talloc_array(state, uint8_t, 1);
1578 if (tevent_req_nomem(bytes, req)) {
1579 return tevent_req_post(req, ev);
1580 }
1581 bytes[0] = 4;
1582 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1583 strlen(dname)+1, NULL);
1584
1585 if (tevent_req_nomem(bytes, req)) {
1586 return tevent_req_post(req, ev);
1587 }
1588
1589 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1590 0, NULL, talloc_get_size(bytes), bytes);
1591 if (tevent_req_nomem(subreq, req)) {
1592 return tevent_req_post(req, ev);
1593 }
1594 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1595 return req;
1596}
1597
1598static void cli_mkdir_done(struct tevent_req *subreq)
1599{
1600 struct tevent_req *req = tevent_req_callback_data(
1601 subreq, struct tevent_req);
1602 NTSTATUS status;
1603
1604 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1605 TALLOC_FREE(subreq);
1606 if (tevent_req_nterror(req, status)) {
1607 return;
1608 }
1609 tevent_req_done(req);
1610}
1611
1612NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1613{
1614 return tevent_req_simple_recv_ntstatus(req);
1615}
1616
1617NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1618{
1619 TALLOC_CTX *frame = talloc_stackframe();
1620 struct event_context *ev;
1621 struct tevent_req *req;
1622 NTSTATUS status = NT_STATUS_OK;
1623
1624 if (cli_has_async_calls(cli)) {
1625 /*
1626 * Can't use sync call while an async call is in flight
1627 */
1628 status = NT_STATUS_INVALID_PARAMETER;
1629 goto fail;
1630 }
1631
1632 ev = event_context_init(frame);
1633 if (ev == NULL) {
1634 status = NT_STATUS_NO_MEMORY;
1635 goto fail;
1636 }
1637
1638 req = cli_mkdir_send(frame, ev, cli, dname);
1639 if (req == NULL) {
1640 status = NT_STATUS_NO_MEMORY;
1641 goto fail;
1642 }
1643
1644 if (!tevent_req_poll(req, ev)) {
1645 status = map_nt_error_from_unix(errno);
1646 goto fail;
1647 }
1648
1649 status = cli_mkdir_recv(req);
1650
1651 fail:
1652 TALLOC_FREE(frame);
1653 if (!NT_STATUS_IS_OK(status)) {
1654 cli_set_error(cli, status);
1655 }
1656 return status;
1657}
1658
1659/****************************************************************************
1660 Remove a directory.
1661****************************************************************************/
1662
1663static void cli_rmdir_done(struct tevent_req *subreq);
1664
1665struct cli_rmdir_state {
1666 int dummy;
1667};
1668
1669struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1670 struct event_context *ev,
1671 struct cli_state *cli,
1672 const char *dname)
1673{
1674 struct tevent_req *req = NULL, *subreq = NULL;
1675 struct cli_rmdir_state *state = NULL;
1676 uint8_t additional_flags = 0;
1677 uint8_t *bytes = NULL;
1678
1679 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1680 if (req == NULL) {
1681 return NULL;
1682 }
1683
1684 bytes = talloc_array(state, uint8_t, 1);
1685 if (tevent_req_nomem(bytes, req)) {
1686 return tevent_req_post(req, ev);
1687 }
1688 bytes[0] = 4;
1689 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1690 strlen(dname)+1, NULL);
1691
1692 if (tevent_req_nomem(bytes, req)) {
1693 return tevent_req_post(req, ev);
1694 }
1695
1696 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1697 0, NULL, talloc_get_size(bytes), bytes);
1698 if (tevent_req_nomem(subreq, req)) {
1699 return tevent_req_post(req, ev);
1700 }
1701 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1702 return req;
1703}
1704
1705static void cli_rmdir_done(struct tevent_req *subreq)
1706{
1707 struct tevent_req *req = tevent_req_callback_data(
1708 subreq, struct tevent_req);
1709 NTSTATUS status;
1710
1711 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1712 TALLOC_FREE(subreq);
1713 if (tevent_req_nterror(req, status)) {
1714 return;
1715 }
1716 tevent_req_done(req);
1717}
1718
1719NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1720{
1721 return tevent_req_simple_recv_ntstatus(req);
1722}
1723
1724NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1725{
1726 TALLOC_CTX *frame = talloc_stackframe();
1727 struct event_context *ev;
1728 struct tevent_req *req;
1729 NTSTATUS status = NT_STATUS_OK;
1730
1731 if (cli_has_async_calls(cli)) {
1732 /*
1733 * Can't use sync call while an async call is in flight
1734 */
1735 status = NT_STATUS_INVALID_PARAMETER;
1736 goto fail;
1737 }
1738
1739 ev = event_context_init(frame);
1740 if (ev == NULL) {
1741 status = NT_STATUS_NO_MEMORY;
1742 goto fail;
1743 }
1744
1745 req = cli_rmdir_send(frame, ev, cli, dname);
1746 if (req == NULL) {
1747 status = NT_STATUS_NO_MEMORY;
1748 goto fail;
1749 }
1750
1751 if (!tevent_req_poll(req, ev)) {
1752 status = map_nt_error_from_unix(errno);
1753 goto fail;
1754 }
1755
1756 status = cli_rmdir_recv(req);
1757
1758 fail:
1759 TALLOC_FREE(frame);
1760 if (!NT_STATUS_IS_OK(status)) {
1761 cli_set_error(cli, status);
1762 }
1763 return status;
1764}
1765
1766/****************************************************************************
1767 Set or clear the delete on close flag.
1768****************************************************************************/
1769
1770struct doc_state {
1771 uint16_t setup;
1772 uint8_t param[6];
1773 uint8_t data[1];
1774};
1775
1776static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1777{
1778 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1779 NULL, 0, NULL, NULL, 0, NULL);
1780 tevent_req_simple_finish_ntstatus(subreq, status);
1781}
1782
1783struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1784 struct event_context *ev,
1785 struct cli_state *cli,
1786 uint16_t fnum,
1787 bool flag)
1788{
1789 struct tevent_req *req = NULL, *subreq = NULL;
1790 struct doc_state *state = NULL;
1791
1792 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1793 if (req == NULL) {
1794 return NULL;
1795 }
1796
1797 /* Setup setup word. */
1798 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1799
1800 /* Setup param array. */
1801 SSVAL(state->param,0,fnum);
1802 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1803
1804 /* Setup data array. */
1805 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1806
1807 subreq = cli_trans_send(state, /* mem ctx. */
1808 ev, /* event ctx. */
1809 cli, /* cli_state. */
1810 SMBtrans2, /* cmd. */
1811 NULL, /* pipe name. */
1812 -1, /* fid. */
1813 0, /* function. */
1814 0, /* flags. */
1815 &state->setup, /* setup. */
1816 1, /* num setup uint16_t words. */
1817 0, /* max returned setup. */
1818 state->param, /* param. */
1819 6, /* num param. */
1820 2, /* max returned param. */
1821 state->data, /* data. */
1822 1, /* num data. */
1823 0); /* max returned data. */
1824
1825 if (tevent_req_nomem(subreq, req)) {
1826 return tevent_req_post(req, ev);
1827 }
1828 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1829 return req;
1830}
1831
1832NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1833{
1834 return tevent_req_simple_recv_ntstatus(req);
1835}
1836
1837NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1838{
1839 TALLOC_CTX *frame = talloc_stackframe();
1840 struct event_context *ev = NULL;
1841 struct tevent_req *req = NULL;
1842 NTSTATUS status = NT_STATUS_OK;
1843
1844 if (cli_has_async_calls(cli)) {
1845 /*
1846 * Can't use sync call while an async call is in flight
1847 */
1848 status = NT_STATUS_INVALID_PARAMETER;
1849 goto fail;
1850 }
1851
1852 ev = event_context_init(frame);
1853 if (ev == NULL) {
1854 status = NT_STATUS_NO_MEMORY;
1855 goto fail;
1856 }
1857
1858 req = cli_nt_delete_on_close_send(frame,
1859 ev,
1860 cli,
1861 fnum,
1862 flag);
1863 if (req == NULL) {
1864 status = NT_STATUS_NO_MEMORY;
1865 goto fail;
1866 }
1867
1868 if (!tevent_req_poll(req, ev)) {
1869 status = map_nt_error_from_unix(errno);
1870 goto fail;
1871 }
1872
1873 status = cli_nt_delete_on_close_recv(req);
1874
1875 fail:
1876 TALLOC_FREE(frame);
1877 if (!NT_STATUS_IS_OK(status)) {
1878 cli_set_error(cli, status);
1879 }
1880 return status;
1881}
1882
1883struct cli_ntcreate_state {
1884 uint16_t vwv[24];
1885 uint16_t fnum;
1886};
1887
1888static void cli_ntcreate_done(struct tevent_req *subreq);
1889
1890struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1891 struct event_context *ev,
1892 struct cli_state *cli,
1893 const char *fname,
1894 uint32_t CreatFlags,
1895 uint32_t DesiredAccess,
1896 uint32_t FileAttributes,
1897 uint32_t ShareAccess,
1898 uint32_t CreateDisposition,
1899 uint32_t CreateOptions,
1900 uint8_t SecurityFlags)
1901{
1902 struct tevent_req *req, *subreq;
1903 struct cli_ntcreate_state *state;
1904 uint16_t *vwv;
1905 uint8_t *bytes;
1906 size_t converted_len;
1907
1908 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1909 if (req == NULL) {
1910 return NULL;
1911 }
1912
1913 vwv = state->vwv;
1914
1915 SCVAL(vwv+0, 0, 0xFF);
1916 SCVAL(vwv+0, 1, 0);
1917 SSVAL(vwv+1, 0, 0);
1918 SCVAL(vwv+2, 0, 0);
1919
1920 if (cli->use_oplocks) {
1921 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1922 }
1923 SIVAL(vwv+3, 1, CreatFlags);
1924 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1925 SIVAL(vwv+7, 1, DesiredAccess);
1926 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1927 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1928 SIVAL(vwv+13, 1, FileAttributes);
1929 SIVAL(vwv+15, 1, ShareAccess);
1930 SIVAL(vwv+17, 1, CreateDisposition);
1931 SIVAL(vwv+19, 1, CreateOptions);
1932 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1933 SCVAL(vwv+23, 1, SecurityFlags);
1934
1935 bytes = talloc_array(state, uint8_t, 0);
1936 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1937 fname, strlen(fname)+1,
1938 &converted_len);
1939
1940 /* sigh. this copes with broken netapp filer behaviour */
1941 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1942
1943 if (tevent_req_nomem(bytes, req)) {
1944 return tevent_req_post(req, ev);
1945 }
1946
1947 SSVAL(vwv+2, 1, converted_len);
1948
1949 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1950 talloc_get_size(bytes), bytes);
1951 if (tevent_req_nomem(subreq, req)) {
1952 return tevent_req_post(req, ev);
1953 }
1954 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1955 return req;
1956}
1957
1958static void cli_ntcreate_done(struct tevent_req *subreq)
1959{
1960 struct tevent_req *req = tevent_req_callback_data(
1961 subreq, struct tevent_req);
1962 struct cli_ntcreate_state *state = tevent_req_data(
1963 req, struct cli_ntcreate_state);
1964 uint8_t wct;
1965 uint16_t *vwv;
1966 uint32_t num_bytes;
1967 uint8_t *bytes;
1968 uint8_t *inbuf;
1969 NTSTATUS status;
1970
1971 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1972 &num_bytes, &bytes);
1973 TALLOC_FREE(subreq);
1974 if (tevent_req_nterror(req, status)) {
1975 return;
1976 }
1977 state->fnum = SVAL(vwv+2, 1);
1978 tevent_req_done(req);
1979}
1980
1981NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1982{
1983 struct cli_ntcreate_state *state = tevent_req_data(
1984 req, struct cli_ntcreate_state);
1985 NTSTATUS status;
1986
1987 if (tevent_req_is_nterror(req, &status)) {
1988 return status;
1989 }
1990 *pfnum = state->fnum;
1991 return NT_STATUS_OK;
1992}
1993
1994NTSTATUS cli_ntcreate(struct cli_state *cli,
1995 const char *fname,
1996 uint32_t CreatFlags,
1997 uint32_t DesiredAccess,
1998 uint32_t FileAttributes,
1999 uint32_t ShareAccess,
2000 uint32_t CreateDisposition,
2001 uint32_t CreateOptions,
2002 uint8_t SecurityFlags,
2003 uint16_t *pfid)
2004{
2005 TALLOC_CTX *frame = talloc_stackframe();
2006 struct event_context *ev;
2007 struct tevent_req *req;
2008 NTSTATUS status = NT_STATUS_OK;
2009
2010 if (cli_has_async_calls(cli)) {
2011 /*
2012 * Can't use sync call while an async call is in flight
2013 */
2014 status = NT_STATUS_INVALID_PARAMETER;
2015 goto fail;
2016 }
2017
2018 ev = event_context_init(frame);
2019 if (ev == NULL) {
2020 status = NT_STATUS_NO_MEMORY;
2021 goto fail;
2022 }
2023
2024 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2025 DesiredAccess, FileAttributes, ShareAccess,
2026 CreateDisposition, CreateOptions,
2027 SecurityFlags);
2028 if (req == NULL) {
2029 status = NT_STATUS_NO_MEMORY;
2030 goto fail;
2031 }
2032
2033 if (!tevent_req_poll(req, ev)) {
2034 status = map_nt_error_from_unix(errno);
2035 goto fail;
2036 }
2037
2038 status = cli_ntcreate_recv(req, pfid);
2039 fail:
2040 TALLOC_FREE(frame);
2041 if (!NT_STATUS_IS_OK(status)) {
2042 cli_set_error(cli, status);
2043 }
2044 return status;
2045}
2046
2047/****************************************************************************
2048 Open a file
2049 WARNING: if you open with O_WRONLY then getattrE won't work!
2050****************************************************************************/
2051
2052struct cli_open_state {
2053 uint16_t vwv[15];
2054 uint16_t fnum;
2055 struct iovec bytes;
2056};
2057
2058static void cli_open_done(struct tevent_req *subreq);
2059
2060struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2061 struct event_context *ev,
2062 struct cli_state *cli, const char *fname,
2063 int flags, int share_mode,
2064 struct tevent_req **psmbreq)
2065{
2066 struct tevent_req *req, *subreq;
2067 struct cli_open_state *state;
2068 unsigned openfn;
2069 unsigned accessmode;
2070 uint8_t additional_flags;
2071 uint8_t *bytes;
2072
2073 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2074 if (req == NULL) {
2075 return NULL;
2076 }
2077
2078 openfn = 0;
2079 if (flags & O_CREAT) {
2080 openfn |= (1<<4);
2081 }
2082 if (!(flags & O_EXCL)) {
2083 if (flags & O_TRUNC)
2084 openfn |= (1<<1);
2085 else
2086 openfn |= (1<<0);
2087 }
2088
2089 accessmode = (share_mode<<4);
2090
2091 if ((flags & O_ACCMODE) == O_RDWR) {
2092 accessmode |= 2;
2093 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2094 accessmode |= 1;
2095 }
2096
2097#if defined(O_SYNC)
2098 if ((flags & O_SYNC) == O_SYNC) {
2099 accessmode |= (1<<14);
2100 }
2101#endif /* O_SYNC */
2102
2103 if (share_mode == DENY_FCB) {
2104 accessmode = 0xFF;
2105 }
2106
2107 SCVAL(state->vwv + 0, 0, 0xFF);
2108 SCVAL(state->vwv + 0, 1, 0);
2109 SSVAL(state->vwv + 1, 0, 0);
2110 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2111 SSVAL(state->vwv + 3, 0, accessmode);
2112 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2113 SSVAL(state->vwv + 5, 0, 0);
2114 SIVAL(state->vwv + 6, 0, 0);
2115 SSVAL(state->vwv + 8, 0, openfn);
2116 SIVAL(state->vwv + 9, 0, 0);
2117 SIVAL(state->vwv + 11, 0, 0);
2118 SIVAL(state->vwv + 13, 0, 0);
2119
2120 additional_flags = 0;
2121
2122 if (cli->use_oplocks) {
2123 /* if using oplocks then ask for a batch oplock via
2124 core and extended methods */
2125 additional_flags =
2126 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2127 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2128 }
2129
2130 bytes = talloc_array(state, uint8_t, 0);
2131 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2132 strlen(fname)+1, NULL);
2133
2134 if (tevent_req_nomem(bytes, req)) {
2135 return tevent_req_post(req, ev);
2136 }
2137
2138 state->bytes.iov_base = (void *)bytes;
2139 state->bytes.iov_len = talloc_get_size(bytes);
2140
2141 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2142 15, state->vwv, 1, &state->bytes);
2143 if (subreq == NULL) {
2144 TALLOC_FREE(req);
2145 return NULL;
2146 }
2147 tevent_req_set_callback(subreq, cli_open_done, req);
2148 *psmbreq = subreq;
2149 return req;
2150}
2151
2152struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2153 struct cli_state *cli, const char *fname,
2154 int flags, int share_mode)
2155{
2156 struct tevent_req *req, *subreq;
2157 NTSTATUS status;
2158
2159 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2160 &subreq);
2161 if (req == NULL) {
2162 return NULL;
2163 }
2164
2165 status = cli_smb_req_send(subreq);
2166 if (tevent_req_nterror(req, status)) {
2167 return tevent_req_post(req, ev);
2168 }
2169 return req;
2170}
2171
2172static void cli_open_done(struct tevent_req *subreq)
2173{
2174 struct tevent_req *req = tevent_req_callback_data(
2175 subreq, struct tevent_req);
2176 struct cli_open_state *state = tevent_req_data(
2177 req, struct cli_open_state);
2178 uint8_t wct;
2179 uint16_t *vwv;
2180 uint8_t *inbuf;
2181 NTSTATUS status;
2182
2183 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2184 NULL);
2185 TALLOC_FREE(subreq);
2186 if (tevent_req_nterror(req, status)) {
2187 return;
2188 }
2189 state->fnum = SVAL(vwv+2, 0);
2190 tevent_req_done(req);
2191}
2192
2193NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2194{
2195 struct cli_open_state *state = tevent_req_data(
2196 req, struct cli_open_state);
2197 NTSTATUS status;
2198
2199 if (tevent_req_is_nterror(req, &status)) {
2200 return status;
2201 }
2202 *pfnum = state->fnum;
2203 return NT_STATUS_OK;
2204}
2205
2206NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2207 int share_mode, uint16_t *pfnum)
2208{
2209 TALLOC_CTX *frame = talloc_stackframe();
2210 struct event_context *ev;
2211 struct tevent_req *req;
2212 NTSTATUS status = NT_STATUS_OK;
2213
2214 if (cli_has_async_calls(cli)) {
2215 /*
2216 * Can't use sync call while an async call is in flight
2217 */
2218 status = NT_STATUS_INVALID_PARAMETER;
2219 goto fail;
2220 }
2221
2222 ev = event_context_init(frame);
2223 if (ev == NULL) {
2224 status = NT_STATUS_NO_MEMORY;
2225 goto fail;
2226 }
2227
2228 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2229 if (req == NULL) {
2230 status = NT_STATUS_NO_MEMORY;
2231 goto fail;
2232 }
2233
2234 if (!tevent_req_poll(req, ev)) {
2235 status = map_nt_error_from_unix(errno);
2236 goto fail;
2237 }
2238
2239 status = cli_open_recv(req, pfnum);
2240 fail:
2241 TALLOC_FREE(frame);
2242 if (!NT_STATUS_IS_OK(status)) {
2243 cli_set_error(cli, status);
2244 }
2245 return status;
2246}
2247
2248/****************************************************************************
2249 Close a file.
2250****************************************************************************/
2251
2252struct cli_close_state {
2253 uint16_t vwv[3];
2254};
2255
2256static void cli_close_done(struct tevent_req *subreq);
2257
2258struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2259 struct event_context *ev,
2260 struct cli_state *cli,
2261 uint16_t fnum,
2262 struct tevent_req **psubreq)
2263{
2264 struct tevent_req *req, *subreq;
2265 struct cli_close_state *state;
2266
2267 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2268 if (req == NULL) {
2269 return NULL;
2270 }
2271
2272 SSVAL(state->vwv+0, 0, fnum);
2273 SIVALS(state->vwv+1, 0, -1);
2274
2275 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2276 0, NULL);
2277 if (subreq == NULL) {
2278 TALLOC_FREE(req);
2279 return NULL;
2280 }
2281 tevent_req_set_callback(subreq, cli_close_done, req);
2282 *psubreq = subreq;
2283 return req;
2284}
2285
2286struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2287 struct event_context *ev,
2288 struct cli_state *cli,
2289 uint16_t fnum)
2290{
2291 struct tevent_req *req, *subreq;
2292 NTSTATUS status;
2293
2294 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2295 if (req == NULL) {
2296 return NULL;
2297 }
2298
2299 status = cli_smb_req_send(subreq);
2300 if (tevent_req_nterror(req, status)) {
2301 return tevent_req_post(req, ev);
2302 }
2303 return req;
2304}
2305
2306static void cli_close_done(struct tevent_req *subreq)
2307{
2308 struct tevent_req *req = tevent_req_callback_data(
2309 subreq, struct tevent_req);
2310 NTSTATUS status;
2311
2312 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2313 TALLOC_FREE(subreq);
2314 if (tevent_req_nterror(req, status)) {
2315 return;
2316 }
2317 tevent_req_done(req);
2318}
2319
2320NTSTATUS cli_close_recv(struct tevent_req *req)
2321{
2322 return tevent_req_simple_recv_ntstatus(req);
2323}
2324
2325NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2326{
2327 TALLOC_CTX *frame = talloc_stackframe();
2328 struct event_context *ev;
2329 struct tevent_req *req;
2330 NTSTATUS status = NT_STATUS_OK;
2331
2332 if (cli_has_async_calls(cli)) {
2333 /*
2334 * Can't use sync call while an async call is in flight
2335 */
2336 status = NT_STATUS_INVALID_PARAMETER;
2337 goto fail;
2338 }
2339
2340 ev = event_context_init(frame);
2341 if (ev == NULL) {
2342 status = NT_STATUS_NO_MEMORY;
2343 goto fail;
2344 }
2345
2346 req = cli_close_send(frame, ev, cli, fnum);
2347 if (req == NULL) {
2348 status = NT_STATUS_NO_MEMORY;
2349 goto fail;
2350 }
2351
2352 if (!tevent_req_poll(req, ev)) {
2353 status = map_nt_error_from_unix(errno);
2354 goto fail;
2355 }
2356
2357 status = cli_close_recv(req);
2358 fail:
2359 TALLOC_FREE(frame);
2360 if (!NT_STATUS_IS_OK(status)) {
2361 cli_set_error(cli, status);
2362 }
2363 return status;
2364}
2365
2366/****************************************************************************
2367 Truncate a file to a specified size
2368****************************************************************************/
2369
2370struct ftrunc_state {
2371 uint16_t setup;
2372 uint8_t param[6];
2373 uint8_t data[8];
2374};
2375
2376static void cli_ftruncate_done(struct tevent_req *subreq)
2377{
2378 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2379 NULL, 0, NULL, NULL, 0, NULL);
2380 tevent_req_simple_finish_ntstatus(subreq, status);
2381}
2382
2383struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2384 struct event_context *ev,
2385 struct cli_state *cli,
2386 uint16_t fnum,
2387 uint64_t size)
2388{
2389 struct tevent_req *req = NULL, *subreq = NULL;
2390 struct ftrunc_state *state = NULL;
2391
2392 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2393 if (req == NULL) {
2394 return NULL;
2395 }
2396
2397 /* Setup setup word. */
2398 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2399
2400 /* Setup param array. */
2401 SSVAL(state->param,0,fnum);
2402 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2403 SSVAL(state->param,4,0);
2404
2405 /* Setup data array. */
2406 SBVAL(state->data, 0, size);
2407
2408 subreq = cli_trans_send(state, /* mem ctx. */
2409 ev, /* event ctx. */
2410 cli, /* cli_state. */
2411 SMBtrans2, /* cmd. */
2412 NULL, /* pipe name. */
2413 -1, /* fid. */
2414 0, /* function. */
2415 0, /* flags. */
2416 &state->setup, /* setup. */
2417 1, /* num setup uint16_t words. */
2418 0, /* max returned setup. */
2419 state->param, /* param. */
2420 6, /* num param. */
2421 2, /* max returned param. */
2422 state->data, /* data. */
2423 8, /* num data. */
2424 0); /* max returned data. */
2425
2426 if (tevent_req_nomem(subreq, req)) {
2427 return tevent_req_post(req, ev);
2428 }
2429 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2430 return req;
2431}
2432
2433NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2434{
2435 return tevent_req_simple_recv_ntstatus(req);
2436}
2437
2438NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2439{
2440 TALLOC_CTX *frame = talloc_stackframe();
2441 struct event_context *ev = NULL;
2442 struct tevent_req *req = NULL;
2443 NTSTATUS status = NT_STATUS_OK;
2444
2445 if (cli_has_async_calls(cli)) {
2446 /*
2447 * Can't use sync call while an async call is in flight
2448 */
2449 status = NT_STATUS_INVALID_PARAMETER;
2450 goto fail;
2451 }
2452
2453 ev = event_context_init(frame);
2454 if (ev == NULL) {
2455 status = NT_STATUS_NO_MEMORY;
2456 goto fail;
2457 }
2458
2459 req = cli_ftruncate_send(frame,
2460 ev,
2461 cli,
2462 fnum,
2463 size);
2464 if (req == NULL) {
2465 status = NT_STATUS_NO_MEMORY;
2466 goto fail;
2467 }
2468
2469 if (!tevent_req_poll(req, ev)) {
2470 status = map_nt_error_from_unix(errno);
2471 goto fail;
2472 }
2473
2474 status = cli_ftruncate_recv(req);
2475
2476 fail:
2477 TALLOC_FREE(frame);
2478 if (!NT_STATUS_IS_OK(status)) {
2479 cli_set_error(cli, status);
2480 }
2481 return status;
2482}
2483
2484/****************************************************************************
2485 send a lock with a specified locktype
2486 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2487****************************************************************************/
2488
2489NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2490 uint32_t offset, uint32_t len,
2491 int timeout, unsigned char locktype)
2492{
2493 uint16_t vwv[8];
2494 uint8_t bytes[10];
2495 NTSTATUS status;
2496 int saved_timeout;
2497
2498 SCVAL(vwv + 0, 0, 0xff);
2499 SCVAL(vwv + 0, 1, 0);
2500 SSVAL(vwv + 1, 0, 0);
2501 SSVAL(vwv + 2, 0, fnum);
2502 SCVAL(vwv + 3, 0, locktype);
2503 SCVAL(vwv + 3, 1, 0);
2504 SIVALS(vwv + 4, 0, timeout);
2505 SSVAL(vwv + 6, 0, 0);
2506 SSVAL(vwv + 7, 0, 1);
2507
2508 SSVAL(bytes, 0, cli->pid);
2509 SIVAL(bytes, 2, offset);
2510 SIVAL(bytes, 6, len);
2511
2512 saved_timeout = cli->timeout;
2513
2514 if (timeout != 0) {
2515 cli->timeout = (timeout == -1)
2516 ? 0x7FFFFFFF : (timeout + 2*1000);
2517 }
2518
2519 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2520 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2521
2522 cli->timeout = saved_timeout;
2523
2524 return status;
2525}
2526
2527/****************************************************************************
2528 Lock a file.
2529 note that timeout is in units of 2 milliseconds
2530****************************************************************************/
2531
2532bool cli_lock(struct cli_state *cli, uint16_t fnum,
2533 uint32_t offset, uint32_t len, int timeout,
2534 enum brl_type lock_type)
2535{
2536 NTSTATUS status;
2537
2538 status = cli_locktype(cli, fnum, offset, len, timeout,
2539 (lock_type == READ_LOCK? 1 : 0));
2540 cli_set_error(cli, status);
2541 return NT_STATUS_IS_OK(status);
2542}
2543
2544/****************************************************************************
2545 Unlock a file.
2546****************************************************************************/
2547
2548struct cli_unlock_state {
2549 uint16_t vwv[8];
2550 uint8_t data[10];
2551};
2552
2553static void cli_unlock_done(struct tevent_req *subreq);
2554
2555struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2556 struct event_context *ev,
2557 struct cli_state *cli,
2558 uint16_t fnum,
2559 uint64_t offset,
2560 uint64_t len)
2561
2562{
2563 struct tevent_req *req = NULL, *subreq = NULL;
2564 struct cli_unlock_state *state = NULL;
2565 uint8_t additional_flags = 0;
2566
2567 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2568 if (req == NULL) {
2569 return NULL;
2570 }
2571
2572 SCVAL(state->vwv+0, 0, 0xFF);
2573 SSVAL(state->vwv+2, 0, fnum);
2574 SCVAL(state->vwv+3, 0, 0);
2575 SIVALS(state->vwv+4, 0, 0);
2576 SSVAL(state->vwv+6, 0, 1);
2577 SSVAL(state->vwv+7, 0, 0);
2578
2579 SSVAL(state->data, 0, cli->pid);
2580 SIVAL(state->data, 2, offset);
2581 SIVAL(state->data, 6, len);
2582
2583 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2584 8, state->vwv, 10, state->data);
2585 if (tevent_req_nomem(subreq, req)) {
2586 return tevent_req_post(req, ev);
2587 }
2588 tevent_req_set_callback(subreq, cli_unlock_done, req);
2589 return req;
2590}
2591
2592static void cli_unlock_done(struct tevent_req *subreq)
2593{
2594 struct tevent_req *req = tevent_req_callback_data(
2595 subreq, struct tevent_req);
2596 NTSTATUS status;
2597
2598 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2599 TALLOC_FREE(subreq);
2600 if (tevent_req_nterror(req, status)) {
2601 return;
2602 }
2603 tevent_req_done(req);
2604}
2605
2606NTSTATUS cli_unlock_recv(struct tevent_req *req)
2607{
2608 return tevent_req_simple_recv_ntstatus(req);
2609}
2610
2611NTSTATUS cli_unlock(struct cli_state *cli,
2612 uint16_t fnum,
2613 uint32_t offset,
2614 uint32_t len)
2615{
2616 TALLOC_CTX *frame = talloc_stackframe();
2617 struct event_context *ev;
2618 struct tevent_req *req;
2619 NTSTATUS status = NT_STATUS_OK;
2620
2621 if (cli_has_async_calls(cli)) {
2622 /*
2623 * Can't use sync call while an async call is in flight
2624 */
2625 status = NT_STATUS_INVALID_PARAMETER;
2626 goto fail;
2627 }
2628
2629 ev = event_context_init(frame);
2630 if (ev == NULL) {
2631 status = NT_STATUS_NO_MEMORY;
2632 goto fail;
2633 }
2634
2635 req = cli_unlock_send(frame, ev, cli,
2636 fnum, offset, len);
2637 if (req == NULL) {
2638 status = NT_STATUS_NO_MEMORY;
2639 goto fail;
2640 }
2641
2642 if (!tevent_req_poll(req, ev)) {
2643 status = map_nt_error_from_unix(errno);
2644 goto fail;
2645 }
2646
2647 status = cli_unlock_recv(req);
2648
2649 fail:
2650 TALLOC_FREE(frame);
2651 if (!NT_STATUS_IS_OK(status)) {
2652 cli_set_error(cli, status);
2653 }
2654 return status;
2655}
2656
2657/****************************************************************************
2658 Lock a file with 64 bit offsets.
2659****************************************************************************/
2660
2661bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2662 uint64_t offset, uint64_t len, int timeout,
2663 enum brl_type lock_type)
2664{
2665 uint16_t vwv[8];
2666 uint8_t bytes[20];
2667 int saved_timeout = cli->timeout;
2668 int ltype;
2669 NTSTATUS status;
2670
2671 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2672 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2673 }
2674
2675 ltype = (lock_type == READ_LOCK? 1 : 0);
2676 ltype |= LOCKING_ANDX_LARGE_FILES;
2677
2678 SCVAL(vwv + 0, 0, 0xff);
2679 SCVAL(vwv + 0, 1, 0);
2680 SSVAL(vwv + 1, 0, 0);
2681 SSVAL(vwv + 2, 0, fnum);
2682 SCVAL(vwv + 3, 0, ltype);
2683 SCVAL(vwv + 3, 1, 0);
2684 SIVALS(vwv + 4, 0, timeout);
2685 SSVAL(vwv + 6, 0, 0);
2686 SSVAL(vwv + 7, 0, 1);
2687
2688 SIVAL(bytes, 0, cli->pid);
2689 SOFF_T_R(bytes, 4, offset);
2690 SOFF_T_R(bytes, 12, len);
2691
2692 saved_timeout = cli->timeout;
2693
2694 if (timeout != 0) {
2695 cli->timeout = (timeout == -1)
2696 ? 0x7FFFFFFF : (timeout + 2*1000);
2697 }
2698
2699 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2700 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2701
2702 cli->timeout = saved_timeout;
2703
2704 cli_set_error(cli, status);
2705 return NT_STATUS_IS_OK(status);
2706}
2707
2708/****************************************************************************
2709 Unlock a file with 64 bit offsets.
2710****************************************************************************/
2711
2712struct cli_unlock64_state {
2713 uint16_t vwv[8];
2714 uint8_t data[20];
2715};
2716
2717static void cli_unlock64_done(struct tevent_req *subreq);
2718
2719struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2720 struct event_context *ev,
2721 struct cli_state *cli,
2722 uint16_t fnum,
2723 uint64_t offset,
2724 uint64_t len)
2725
2726{
2727 struct tevent_req *req = NULL, *subreq = NULL;
2728 struct cli_unlock64_state *state = NULL;
2729 uint8_t additional_flags = 0;
2730
2731 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2732 if (req == NULL) {
2733 return NULL;
2734 }
2735
2736 SCVAL(state->vwv+0, 0, 0xff);
2737 SSVAL(state->vwv+2, 0, fnum);
2738 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2739 SIVALS(state->vwv+4, 0, 0);
2740 SSVAL(state->vwv+6, 0, 1);
2741 SSVAL(state->vwv+7, 0, 0);
2742
2743 SIVAL(state->data, 0, cli->pid);
2744 SOFF_T_R(state->data, 4, offset);
2745 SOFF_T_R(state->data, 12, len);
2746
2747 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2748 8, state->vwv, 20, state->data);
2749 if (tevent_req_nomem(subreq, req)) {
2750 return tevent_req_post(req, ev);
2751 }
2752 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2753 return req;
2754}
2755
2756static void cli_unlock64_done(struct tevent_req *subreq)
2757{
2758 struct tevent_req *req = tevent_req_callback_data(
2759 subreq, struct tevent_req);
2760 NTSTATUS status;
2761
2762 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2763 TALLOC_FREE(subreq);
2764 if (tevent_req_nterror(req, status)) {
2765 return;
2766 }
2767 tevent_req_done(req);
2768}
2769
2770NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2771{
2772 return tevent_req_simple_recv_ntstatus(req);
2773}
2774
2775NTSTATUS cli_unlock64(struct cli_state *cli,
2776 uint16_t fnum,
2777 uint64_t offset,
2778 uint64_t len)
2779{
2780 TALLOC_CTX *frame = talloc_stackframe();
2781 struct event_context *ev;
2782 struct tevent_req *req;
2783 NTSTATUS status = NT_STATUS_OK;
2784
2785 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2786 return cli_unlock(cli, fnum, offset, len);
2787 }
2788
2789 if (cli_has_async_calls(cli)) {
2790 /*
2791 * Can't use sync call while an async call is in flight
2792 */
2793 status = NT_STATUS_INVALID_PARAMETER;
2794 goto fail;
2795 }
2796
2797 ev = event_context_init(frame);
2798 if (ev == NULL) {
2799 status = NT_STATUS_NO_MEMORY;
2800 goto fail;
2801 }
2802
2803 req = cli_unlock64_send(frame, ev, cli,
2804 fnum, offset, len);
2805 if (req == NULL) {
2806 status = NT_STATUS_NO_MEMORY;
2807 goto fail;
2808 }
2809
2810 if (!tevent_req_poll(req, ev)) {
2811 status = map_nt_error_from_unix(errno);
2812 goto fail;
2813 }
2814
2815 status = cli_unlock64_recv(req);
2816
2817 fail:
2818 TALLOC_FREE(frame);
2819 if (!NT_STATUS_IS_OK(status)) {
2820 cli_set_error(cli, status);
2821 }
2822 return status;
2823}
2824
2825/****************************************************************************
2826 Get/unlock a POSIX lock on a file - internal function.
2827****************************************************************************/
2828
2829struct posix_lock_state {
2830 uint16_t setup;
2831 uint8_t param[4];
2832 uint8_t data[POSIX_LOCK_DATA_SIZE];
2833};
2834
2835static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2836{
2837 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2838 NULL, 0, NULL, NULL, 0, NULL);
2839 tevent_req_simple_finish_ntstatus(subreq, status);
2840}
2841
2842static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2843 struct event_context *ev,
2844 struct cli_state *cli,
2845 uint16_t fnum,
2846 uint64_t offset,
2847 uint64_t len,
2848 bool wait_lock,
2849 enum brl_type lock_type)
2850{
2851 struct tevent_req *req = NULL, *subreq = NULL;
2852 struct posix_lock_state *state = NULL;
2853
2854 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2855 if (req == NULL) {
2856 return NULL;
2857 }
2858
2859 /* Setup setup word. */
2860 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2861
2862 /* Setup param array. */
2863 SSVAL(&state->param, 0, fnum);
2864 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2865
2866 /* Setup data array. */
2867 switch (lock_type) {
2868 case READ_LOCK:
2869 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2870 POSIX_LOCK_TYPE_READ);
2871 break;
2872 case WRITE_LOCK:
2873 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2874 POSIX_LOCK_TYPE_WRITE);
2875 break;
2876 case UNLOCK_LOCK:
2877 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2878 POSIX_LOCK_TYPE_UNLOCK);
2879 break;
2880 default:
2881 return NULL;
2882 }
2883
2884 if (wait_lock) {
2885 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2886 POSIX_LOCK_FLAG_WAIT);
2887 } else {
2888 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2889 POSIX_LOCK_FLAG_NOWAIT);
2890 }
2891
2892 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2893 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2894 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2895
2896 subreq = cli_trans_send(state, /* mem ctx. */
2897 ev, /* event ctx. */
2898 cli, /* cli_state. */
2899 SMBtrans2, /* cmd. */
2900 NULL, /* pipe name. */
2901 -1, /* fid. */
2902 0, /* function. */
2903 0, /* flags. */
2904 &state->setup, /* setup. */
2905 1, /* num setup uint16_t words. */
2906 0, /* max returned setup. */
2907 state->param, /* param. */
2908 4, /* num param. */
2909 2, /* max returned param. */
2910 state->data, /* data. */
2911 POSIX_LOCK_DATA_SIZE, /* num data. */
2912 0); /* max returned data. */
2913
2914 if (tevent_req_nomem(subreq, req)) {
2915 return tevent_req_post(req, ev);
2916 }
2917 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2918 return req;
2919}
2920
2921/****************************************************************************
2922 POSIX Lock a file.
2923****************************************************************************/
2924
2925struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2926 struct event_context *ev,
2927 struct cli_state *cli,
2928 uint16_t fnum,
2929 uint64_t offset,
2930 uint64_t len,
2931 bool wait_lock,
2932 enum brl_type lock_type)
2933{
2934 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2935 wait_lock, lock_type);
2936}
2937
2938NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2939{
2940 return tevent_req_simple_recv_ntstatus(req);
2941}
2942
2943NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2944 uint64_t offset, uint64_t len,
2945 bool wait_lock, enum brl_type lock_type)
2946{
2947 TALLOC_CTX *frame = talloc_stackframe();
2948 struct event_context *ev = NULL;
2949 struct tevent_req *req = NULL;
2950 NTSTATUS status = NT_STATUS_OK;
2951
2952 if (cli_has_async_calls(cli)) {
2953 /*
2954 * Can't use sync call while an async call is in flight
2955 */
2956 status = NT_STATUS_INVALID_PARAMETER;
2957 goto fail;
2958 }
2959
2960 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2961 status = NT_STATUS_INVALID_PARAMETER;
2962 goto fail;
2963 }
2964
2965 ev = event_context_init(frame);
2966 if (ev == NULL) {
2967 status = NT_STATUS_NO_MEMORY;
2968 goto fail;
2969 }
2970
2971 req = cli_posix_lock_send(frame,
2972 ev,
2973 cli,
2974 fnum,
2975 offset,
2976 len,
2977 wait_lock,
2978 lock_type);
2979 if (req == NULL) {
2980 status = NT_STATUS_NO_MEMORY;
2981 goto fail;
2982 }
2983
2984 if (!tevent_req_poll(req, ev)) {
2985 status = map_nt_error_from_unix(errno);
2986 goto fail;
2987 }
2988
2989 status = cli_posix_lock_recv(req);
2990
2991 fail:
2992 TALLOC_FREE(frame);
2993 if (!NT_STATUS_IS_OK(status)) {
2994 cli_set_error(cli, status);
2995 }
2996 return status;
2997}
2998
2999/****************************************************************************
3000 POSIX Unlock a file.
3001****************************************************************************/
3002
3003struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3004 struct event_context *ev,
3005 struct cli_state *cli,
3006 uint16_t fnum,
3007 uint64_t offset,
3008 uint64_t len)
3009{
3010 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3011 false, UNLOCK_LOCK);
3012}
3013
3014NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3015{
3016 return tevent_req_simple_recv_ntstatus(req);
3017}
3018
3019NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3020{
3021 TALLOC_CTX *frame = talloc_stackframe();
3022 struct event_context *ev = NULL;
3023 struct tevent_req *req = NULL;
3024 NTSTATUS status = NT_STATUS_OK;
3025
3026 if (cli_has_async_calls(cli)) {
3027 /*
3028 * Can't use sync call while an async call is in flight
3029 */
3030 status = NT_STATUS_INVALID_PARAMETER;
3031 goto fail;
3032 }
3033
3034 ev = event_context_init(frame);
3035 if (ev == NULL) {
3036 status = NT_STATUS_NO_MEMORY;
3037 goto fail;
3038 }
3039
3040 req = cli_posix_unlock_send(frame,
3041 ev,
3042 cli,
3043 fnum,
3044 offset,
3045 len);
3046 if (req == NULL) {
3047 status = NT_STATUS_NO_MEMORY;
3048 goto fail;
3049 }
3050
3051 if (!tevent_req_poll(req, ev)) {
3052 status = map_nt_error_from_unix(errno);
3053 goto fail;
3054 }
3055
3056 status = cli_posix_unlock_recv(req);
3057
3058 fail:
3059 TALLOC_FREE(frame);
3060 if (!NT_STATUS_IS_OK(status)) {
3061 cli_set_error(cli, status);
3062 }
3063 return status;
3064}
3065
3066/****************************************************************************
3067 Do a SMBgetattrE call.
3068****************************************************************************/
3069
3070static void cli_getattrE_done(struct tevent_req *subreq);
3071
3072struct cli_getattrE_state {
3073 uint16_t vwv[1];
3074 int zone_offset;
3075 uint16_t attr;
3076 SMB_OFF_T size;
3077 time_t change_time;
3078 time_t access_time;
3079 time_t write_time;
3080};
3081
3082struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3083 struct event_context *ev,
3084 struct cli_state *cli,
3085 uint16_t fnum)
3086{
3087 struct tevent_req *req = NULL, *subreq = NULL;
3088 struct cli_getattrE_state *state = NULL;
3089 uint8_t additional_flags = 0;
3090
3091 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3092 if (req == NULL) {
3093 return NULL;
3094 }
3095
3096 state->zone_offset = cli->serverzone;
3097 SSVAL(state->vwv+0,0,fnum);
3098
3099 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3100 1, state->vwv, 0, NULL);
3101 if (tevent_req_nomem(subreq, req)) {
3102 return tevent_req_post(req, ev);
3103 }
3104 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3105 return req;
3106}
3107
3108static void cli_getattrE_done(struct tevent_req *subreq)
3109{
3110 struct tevent_req *req = tevent_req_callback_data(
3111 subreq, struct tevent_req);
3112 struct cli_getattrE_state *state = tevent_req_data(
3113 req, struct cli_getattrE_state);
3114 uint8_t wct;
3115 uint16_t *vwv = NULL;
3116 uint8_t *inbuf;
3117 NTSTATUS status;
3118
3119 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3120 NULL, NULL);
3121 TALLOC_FREE(subreq);
3122 if (tevent_req_nterror(req, status)) {
3123 return;
3124 }
3125
3126 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3127 state->attr = SVAL(vwv+10,0);
3128 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3129 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3130 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3131
3132 tevent_req_done(req);
3133}
3134
3135NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3136 uint16_t *attr,
3137 SMB_OFF_T *size,
3138 time_t *change_time,
3139 time_t *access_time,
3140 time_t *write_time)
3141{
3142 struct cli_getattrE_state *state = tevent_req_data(
3143 req, struct cli_getattrE_state);
3144 NTSTATUS status;
3145
3146 if (tevent_req_is_nterror(req, &status)) {
3147 return status;
3148 }
3149 if (attr) {
3150 *attr = state->attr;
3151 }
3152 if (size) {
3153 *size = state->size;
3154 }
3155 if (change_time) {
3156 *change_time = state->change_time;
3157 }
3158 if (access_time) {
3159 *access_time = state->access_time;
3160 }
3161 if (write_time) {
3162 *write_time = state->write_time;
3163 }
3164 return NT_STATUS_OK;
3165}
3166
3167NTSTATUS cli_getattrE(struct cli_state *cli,
3168 uint16_t fnum,
3169 uint16_t *attr,
3170 SMB_OFF_T *size,
3171 time_t *change_time,
3172 time_t *access_time,
3173 time_t *write_time)
3174{
3175 TALLOC_CTX *frame = talloc_stackframe();
3176 struct event_context *ev = NULL;
3177 struct tevent_req *req = NULL;
3178 NTSTATUS status = NT_STATUS_OK;
3179
3180 if (cli_has_async_calls(cli)) {
3181 /*
3182 * Can't use sync call while an async call is in flight
3183 */
3184 status = NT_STATUS_INVALID_PARAMETER;
3185 goto fail;
3186 }
3187
3188 ev = event_context_init(frame);
3189 if (ev == NULL) {
3190 status = NT_STATUS_NO_MEMORY;
3191 goto fail;
3192 }
3193
3194 req = cli_getattrE_send(frame, ev, cli, fnum);
3195 if (req == NULL) {
3196 status = NT_STATUS_NO_MEMORY;
3197 goto fail;
3198 }
3199
3200 if (!tevent_req_poll(req, ev)) {
3201 status = map_nt_error_from_unix(errno);
3202 goto fail;
3203 }
3204
3205 status = cli_getattrE_recv(req,
3206 attr,
3207 size,
3208 change_time,
3209 access_time,
3210 write_time);
3211
3212 fail:
3213 TALLOC_FREE(frame);
3214 if (!NT_STATUS_IS_OK(status)) {
3215 cli_set_error(cli, status);
3216 }
3217 return status;
3218}
3219
3220/****************************************************************************
3221 Do a SMBgetatr call
3222****************************************************************************/
3223
3224static void cli_getatr_done(struct tevent_req *subreq);
3225
3226struct cli_getatr_state {
3227 int zone_offset;
3228 uint16_t attr;
3229 SMB_OFF_T size;
3230 time_t write_time;
3231};
3232
3233struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3234 struct event_context *ev,
3235 struct cli_state *cli,
3236 const char *fname)
3237{
3238 struct tevent_req *req = NULL, *subreq = NULL;
3239 struct cli_getatr_state *state = NULL;
3240 uint8_t additional_flags = 0;
3241 uint8_t *bytes = NULL;
3242
3243 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3244 if (req == NULL) {
3245 return NULL;
3246 }
3247
3248 state->zone_offset = cli->serverzone;
3249
3250 bytes = talloc_array(state, uint8_t, 1);
3251 if (tevent_req_nomem(bytes, req)) {
3252 return tevent_req_post(req, ev);
3253 }
3254 bytes[0] = 4;
3255 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3256 strlen(fname)+1, NULL);
3257
3258 if (tevent_req_nomem(bytes, req)) {
3259 return tevent_req_post(req, ev);
3260 }
3261
3262 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3263 0, NULL, talloc_get_size(bytes), bytes);
3264 if (tevent_req_nomem(subreq, req)) {
3265 return tevent_req_post(req, ev);
3266 }
3267 tevent_req_set_callback(subreq, cli_getatr_done, req);
3268 return req;
3269}
3270
3271static void cli_getatr_done(struct tevent_req *subreq)
3272{
3273 struct tevent_req *req = tevent_req_callback_data(
3274 subreq, struct tevent_req);
3275 struct cli_getatr_state *state = tevent_req_data(
3276 req, struct cli_getatr_state);
3277 uint8_t wct;
3278 uint16_t *vwv = NULL;
3279 uint8_t *inbuf;
3280 NTSTATUS status;
3281
3282 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3283 NULL);
3284 TALLOC_FREE(subreq);
3285 if (tevent_req_nterror(req, status)) {
3286 return;
3287 }
3288
3289 state->attr = SVAL(vwv+0,0);
3290 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3291 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3292
3293 tevent_req_done(req);
3294}
3295
3296NTSTATUS cli_getatr_recv(struct tevent_req *req,
3297 uint16_t *attr,
3298 SMB_OFF_T *size,
3299 time_t *write_time)
3300{
3301 struct cli_getatr_state *state = tevent_req_data(
3302 req, struct cli_getatr_state);
3303 NTSTATUS status;
3304
3305 if (tevent_req_is_nterror(req, &status)) {
3306 return status;
3307 }
3308 if (attr) {
3309 *attr = state->attr;
3310 }
3311 if (size) {
3312 *size = state->size;
3313 }
3314 if (write_time) {
3315 *write_time = state->write_time;
3316 }
3317 return NT_STATUS_OK;
3318}
3319
3320NTSTATUS cli_getatr(struct cli_state *cli,
3321 const char *fname,
3322 uint16_t *attr,
3323 SMB_OFF_T *size,
3324 time_t *write_time)
3325{
3326 TALLOC_CTX *frame = talloc_stackframe();
3327 struct event_context *ev = NULL;
3328 struct tevent_req *req = NULL;
3329 NTSTATUS status = NT_STATUS_OK;
3330
3331 if (cli_has_async_calls(cli)) {
3332 /*
3333 * Can't use sync call while an async call is in flight
3334 */
3335 status = NT_STATUS_INVALID_PARAMETER;
3336 goto fail;
3337 }
3338
3339 ev = event_context_init(frame);
3340 if (ev == NULL) {
3341 status = NT_STATUS_NO_MEMORY;
3342 goto fail;
3343 }
3344
3345 req = cli_getatr_send(frame, ev, cli, fname);
3346 if (req == NULL) {
3347 status = NT_STATUS_NO_MEMORY;
3348 goto fail;
3349 }
3350
3351 if (!tevent_req_poll(req, ev)) {
3352 status = map_nt_error_from_unix(errno);
3353 goto fail;
3354 }
3355
3356 status = cli_getatr_recv(req,
3357 attr,
3358 size,
3359 write_time);
3360
3361 fail:
3362 TALLOC_FREE(frame);
3363 if (!NT_STATUS_IS_OK(status)) {
3364 cli_set_error(cli, status);
3365 }
3366 return status;
3367}
3368
3369/****************************************************************************
3370 Do a SMBsetattrE call.
3371****************************************************************************/
3372
3373static void cli_setattrE_done(struct tevent_req *subreq);
3374
3375struct cli_setattrE_state {
3376 uint16_t vwv[7];
3377};
3378
3379struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3380 struct event_context *ev,
3381 struct cli_state *cli,
3382 uint16_t fnum,
3383 time_t change_time,
3384 time_t access_time,
3385 time_t write_time)
3386{
3387 struct tevent_req *req = NULL, *subreq = NULL;
3388 struct cli_setattrE_state *state = NULL;
3389 uint8_t additional_flags = 0;
3390
3391 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3392 if (req == NULL) {
3393 return NULL;
3394 }
3395
3396 SSVAL(state->vwv+0, 0, fnum);
3397 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3398 cli->serverzone);
3399 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3400 cli->serverzone);
3401 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3402 cli->serverzone);
3403
3404 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3405 7, state->vwv, 0, NULL);
3406 if (tevent_req_nomem(subreq, req)) {
3407 return tevent_req_post(req, ev);
3408 }
3409 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3410 return req;
3411}
3412
3413static void cli_setattrE_done(struct tevent_req *subreq)
3414{
3415 struct tevent_req *req = tevent_req_callback_data(
3416 subreq, struct tevent_req);
3417 NTSTATUS status;
3418
3419 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3420 TALLOC_FREE(subreq);
3421 if (tevent_req_nterror(req, status)) {
3422 return;
3423 }
3424 tevent_req_done(req);
3425}
3426
3427NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3428{
3429 return tevent_req_simple_recv_ntstatus(req);
3430}
3431
3432NTSTATUS cli_setattrE(struct cli_state *cli,
3433 uint16_t fnum,
3434 time_t change_time,
3435 time_t access_time,
3436 time_t write_time)
3437{
3438 TALLOC_CTX *frame = talloc_stackframe();
3439 struct event_context *ev = NULL;
3440 struct tevent_req *req = NULL;
3441 NTSTATUS status = NT_STATUS_OK;
3442
3443 if (cli_has_async_calls(cli)) {
3444 /*
3445 * Can't use sync call while an async call is in flight
3446 */
3447 status = NT_STATUS_INVALID_PARAMETER;
3448 goto fail;
3449 }
3450
3451 ev = event_context_init(frame);
3452 if (ev == NULL) {
3453 status = NT_STATUS_NO_MEMORY;
3454 goto fail;
3455 }
3456
3457 req = cli_setattrE_send(frame, ev,
3458 cli,
3459 fnum,
3460 change_time,
3461 access_time,
3462 write_time);
3463
3464 if (req == NULL) {
3465 status = NT_STATUS_NO_MEMORY;
3466 goto fail;
3467 }
3468
3469 if (!tevent_req_poll(req, ev)) {
3470 status = map_nt_error_from_unix(errno);
3471 goto fail;
3472 }
3473
3474 status = cli_setattrE_recv(req);
3475
3476 fail:
3477 TALLOC_FREE(frame);
3478 if (!NT_STATUS_IS_OK(status)) {
3479 cli_set_error(cli, status);
3480 }
3481 return status;
3482}
3483
3484/****************************************************************************
3485 Do a SMBsetatr call.
3486****************************************************************************/
3487
3488static void cli_setatr_done(struct tevent_req *subreq);
3489
3490struct cli_setatr_state {
3491 uint16_t vwv[8];
3492};
3493
3494struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3495 struct event_context *ev,
3496 struct cli_state *cli,
3497 const char *fname,
3498 uint16_t attr,
3499 time_t mtime)
3500{
3501 struct tevent_req *req = NULL, *subreq = NULL;
3502 struct cli_setatr_state *state = NULL;
3503 uint8_t additional_flags = 0;
3504 uint8_t *bytes = NULL;
3505
3506 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3507 if (req == NULL) {
3508 return NULL;
3509 }
3510
3511 SSVAL(state->vwv+0, 0, attr);
3512 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3513
3514 bytes = talloc_array(state, uint8_t, 1);
3515 if (tevent_req_nomem(bytes, req)) {
3516 return tevent_req_post(req, ev);
3517 }
3518 bytes[0] = 4;
3519 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3520 strlen(fname)+1, NULL);
3521 if (tevent_req_nomem(bytes, req)) {
3522 return tevent_req_post(req, ev);
3523 }
3524 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3525 talloc_get_size(bytes)+1);
3526 if (tevent_req_nomem(bytes, req)) {
3527 return tevent_req_post(req, ev);
3528 }
3529
3530 bytes[talloc_get_size(bytes)-1] = 4;
3531 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3532 1, NULL);
3533 if (tevent_req_nomem(bytes, req)) {
3534 return tevent_req_post(req, ev);
3535 }
3536
3537 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3538 8, state->vwv, talloc_get_size(bytes), bytes);
3539 if (tevent_req_nomem(subreq, req)) {
3540 return tevent_req_post(req, ev);
3541 }
3542 tevent_req_set_callback(subreq, cli_setatr_done, req);
3543 return req;
3544}
3545
3546static void cli_setatr_done(struct tevent_req *subreq)
3547{
3548 struct tevent_req *req = tevent_req_callback_data(
3549 subreq, struct tevent_req);
3550 NTSTATUS status;
3551
3552 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3553 TALLOC_FREE(subreq);
3554 if (tevent_req_nterror(req, status)) {
3555 return;
3556 }
3557 tevent_req_done(req);
3558}
3559
3560NTSTATUS cli_setatr_recv(struct tevent_req *req)
3561{
3562 return tevent_req_simple_recv_ntstatus(req);
3563}
3564
3565NTSTATUS cli_setatr(struct cli_state *cli,
3566 const char *fname,
3567 uint16_t attr,
3568 time_t mtime)
3569{
3570 TALLOC_CTX *frame = talloc_stackframe();
3571 struct event_context *ev = NULL;
3572 struct tevent_req *req = NULL;
3573 NTSTATUS status = NT_STATUS_OK;
3574
3575 if (cli_has_async_calls(cli)) {
3576 /*
3577 * Can't use sync call while an async call is in flight
3578 */
3579 status = NT_STATUS_INVALID_PARAMETER;
3580 goto fail;
3581 }
3582
3583 ev = event_context_init(frame);
3584 if (ev == NULL) {
3585 status = NT_STATUS_NO_MEMORY;
3586 goto fail;
3587 }
3588
3589 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3590 if (req == NULL) {
3591 status = NT_STATUS_NO_MEMORY;
3592 goto fail;
3593 }
3594
3595 if (!tevent_req_poll(req, ev)) {
3596 status = map_nt_error_from_unix(errno);
3597 goto fail;
3598 }
3599
3600 status = cli_setatr_recv(req);
3601
3602 fail:
3603 TALLOC_FREE(frame);
3604 if (!NT_STATUS_IS_OK(status)) {
3605 cli_set_error(cli, status);
3606 }
3607 return status;
3608}
3609
3610/****************************************************************************
3611 Check for existance of a dir.
3612****************************************************************************/
3613
3614static void cli_chkpath_done(struct tevent_req *subreq);
3615
3616struct cli_chkpath_state {
3617 int dummy;
3618};
3619
3620struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3621 struct event_context *ev,
3622 struct cli_state *cli,
3623 const char *fname)
3624{
3625 struct tevent_req *req = NULL, *subreq = NULL;
3626 struct cli_chkpath_state *state = NULL;
3627 uint8_t additional_flags = 0;
3628 uint8_t *bytes = NULL;
3629
3630 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3631 if (req == NULL) {
3632 return NULL;
3633 }
3634
3635 bytes = talloc_array(state, uint8_t, 1);
3636 if (tevent_req_nomem(bytes, req)) {
3637 return tevent_req_post(req, ev);
3638 }
3639 bytes[0] = 4;
3640 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3641 strlen(fname)+1, NULL);
3642
3643 if (tevent_req_nomem(bytes, req)) {
3644 return tevent_req_post(req, ev);
3645 }
3646
3647 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3648 0, NULL, talloc_get_size(bytes), bytes);
3649 if (tevent_req_nomem(subreq, req)) {
3650 return tevent_req_post(req, ev);
3651 }
3652 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3653 return req;
3654}
3655
3656static void cli_chkpath_done(struct tevent_req *subreq)
3657{
3658 struct tevent_req *req = tevent_req_callback_data(
3659 subreq, struct tevent_req);
3660 NTSTATUS status;
3661
3662 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3663 TALLOC_FREE(subreq);
3664 if (tevent_req_nterror(req, status)) {
3665 return;
3666 }
3667 tevent_req_done(req);
3668}
3669
3670NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3671{
3672 return tevent_req_simple_recv_ntstatus(req);
3673}
3674
3675NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3676{
3677 TALLOC_CTX *frame = talloc_stackframe();
3678 struct event_context *ev = NULL;
3679 struct tevent_req *req = NULL;
3680 char *path2 = NULL;
3681 NTSTATUS status = NT_STATUS_OK;
3682
3683 if (cli_has_async_calls(cli)) {
3684 /*
3685 * Can't use sync call while an async call is in flight
3686 */
3687 status = NT_STATUS_INVALID_PARAMETER;
3688 goto fail;
3689 }
3690
3691 path2 = talloc_strdup(frame, path);
3692 if (!path2) {
3693 status = NT_STATUS_NO_MEMORY;
3694 goto fail;
3695 }
3696 trim_char(path2,'\0','\\');
3697 if (!*path2) {
3698 path2 = talloc_strdup(frame, "\\");
3699 if (!path2) {
3700 status = NT_STATUS_NO_MEMORY;
3701 goto fail;
3702 }
3703 }
3704
3705 ev = event_context_init(frame);
3706 if (ev == NULL) {
3707 status = NT_STATUS_NO_MEMORY;
3708 goto fail;
3709 }
3710
3711 req = cli_chkpath_send(frame, ev, cli, path2);
3712 if (req == NULL) {
3713 status = NT_STATUS_NO_MEMORY;
3714 goto fail;
3715 }
3716
3717 if (!tevent_req_poll(req, ev)) {
3718 status = map_nt_error_from_unix(errno);
3719 goto fail;
3720 }
3721
3722 status = cli_chkpath_recv(req);
3723
3724 fail:
3725 TALLOC_FREE(frame);
3726 if (!NT_STATUS_IS_OK(status)) {
3727 cli_set_error(cli, status);
3728 }
3729 return status;
3730}
3731
3732/****************************************************************************
3733 Query disk space.
3734****************************************************************************/
3735
3736static void cli_dskattr_done(struct tevent_req *subreq);
3737
3738struct cli_dskattr_state {
3739 int bsize;
3740 int total;
3741 int avail;
3742};
3743
3744struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3745 struct event_context *ev,
3746 struct cli_state *cli)
3747{
3748 struct tevent_req *req = NULL, *subreq = NULL;
3749 struct cli_dskattr_state *state = NULL;
3750 uint8_t additional_flags = 0;
3751
3752 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3753 if (req == NULL) {
3754 return NULL;
3755 }
3756
3757 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3758 0, NULL, 0, NULL);
3759 if (tevent_req_nomem(subreq, req)) {
3760 return tevent_req_post(req, ev);
3761 }
3762 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3763 return req;
3764}
3765
3766static void cli_dskattr_done(struct tevent_req *subreq)
3767{
3768 struct tevent_req *req = tevent_req_callback_data(
3769 subreq, struct tevent_req);
3770 struct cli_dskattr_state *state = tevent_req_data(
3771 req, struct cli_dskattr_state);
3772 uint8_t wct;
3773 uint16_t *vwv = NULL;
3774 uint8_t *inbuf;
3775 NTSTATUS status;
3776
3777 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3778 NULL);
3779 TALLOC_FREE(subreq);
3780 if (tevent_req_nterror(req, status)) {
3781 return;
3782 }
3783 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3784 state->total = SVAL(vwv+0, 0);
3785 state->avail = SVAL(vwv+3, 0);
3786 tevent_req_done(req);
3787}
3788
3789NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3790{
3791 struct cli_dskattr_state *state = tevent_req_data(
3792 req, struct cli_dskattr_state);
3793 NTSTATUS status;
3794
3795 if (tevent_req_is_nterror(req, &status)) {
3796 return status;
3797 }
3798 *bsize = state->bsize;
3799 *total = state->total;
3800 *avail = state->avail;
3801 return NT_STATUS_OK;
3802}
3803
3804NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3805{
3806 TALLOC_CTX *frame = talloc_stackframe();
3807 struct event_context *ev = NULL;
3808 struct tevent_req *req = NULL;
3809 NTSTATUS status = NT_STATUS_OK;
3810
3811 if (cli_has_async_calls(cli)) {
3812 /*
3813 * Can't use sync call while an async call is in flight
3814 */
3815 status = NT_STATUS_INVALID_PARAMETER;
3816 goto fail;
3817 }
3818
3819 ev = event_context_init(frame);
3820 if (ev == NULL) {
3821 status = NT_STATUS_NO_MEMORY;
3822 goto fail;
3823 }
3824
3825 req = cli_dskattr_send(frame, ev, cli);
3826 if (req == NULL) {
3827 status = NT_STATUS_NO_MEMORY;
3828 goto fail;
3829 }
3830
3831 if (!tevent_req_poll(req, ev)) {
3832 status = map_nt_error_from_unix(errno);
3833 goto fail;
3834 }
3835
3836 status = cli_dskattr_recv(req, bsize, total, avail);
3837
3838 fail:
3839 TALLOC_FREE(frame);
3840 if (!NT_STATUS_IS_OK(status)) {
3841 cli_set_error(cli, status);
3842 }
3843 return status;
3844}
3845
3846/****************************************************************************
3847 Create and open a temporary file.
3848****************************************************************************/
3849
3850static void cli_ctemp_done(struct tevent_req *subreq);
3851
3852struct ctemp_state {
3853 uint16_t vwv[3];
3854 char *ret_path;
3855 uint16_t fnum;
3856};
3857
3858struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3859 struct event_context *ev,
3860 struct cli_state *cli,
3861 const char *path)
3862{
3863 struct tevent_req *req = NULL, *subreq = NULL;
3864 struct ctemp_state *state = NULL;
3865 uint8_t additional_flags = 0;
3866 uint8_t *bytes = NULL;
3867
3868 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3869 if (req == NULL) {
3870 return NULL;
3871 }
3872
3873 SSVAL(state->vwv,0,0);
3874 SIVALS(state->vwv+1,0,-1);
3875
3876 bytes = talloc_array(state, uint8_t, 1);
3877 if (tevent_req_nomem(bytes, req)) {
3878 return tevent_req_post(req, ev);
3879 }
3880 bytes[0] = 4;
3881 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3882 strlen(path)+1, NULL);
3883 if (tevent_req_nomem(bytes, req)) {
3884 return tevent_req_post(req, ev);
3885 }
3886
3887 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3888 3, state->vwv, talloc_get_size(bytes), bytes);
3889 if (tevent_req_nomem(subreq, req)) {
3890 return tevent_req_post(req, ev);
3891 }
3892 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3893 return req;
3894}
3895
3896static void cli_ctemp_done(struct tevent_req *subreq)
3897{
3898 struct tevent_req *req = tevent_req_callback_data(
3899 subreq, struct tevent_req);
3900 struct ctemp_state *state = tevent_req_data(
3901 req, struct ctemp_state);
3902 NTSTATUS status;
3903 uint8_t wcnt;
3904 uint16_t *vwv;
3905 uint32_t num_bytes = 0;
3906 uint8_t *bytes = NULL;
3907 uint8_t *inbuf;
3908
3909 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3910 &num_bytes, &bytes);
3911 TALLOC_FREE(subreq);
3912 if (tevent_req_nterror(req, status)) {
3913 return;
3914 }
3915
3916 state->fnum = SVAL(vwv+0, 0);
3917
3918 /* From W2K3, the result is just the ASCII name */
3919 if (num_bytes < 2) {
3920 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3921 return;
3922 }
3923
3924 if (pull_string_talloc(state,
3925 NULL,
3926 0,
3927 &state->ret_path,
3928 bytes,
3929 num_bytes,
3930 STR_ASCII) == 0) {
3931 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3932 return;
3933 }
3934 tevent_req_done(req);
3935}
3936
3937NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3938 TALLOC_CTX *ctx,
3939 uint16_t *pfnum,
3940 char **outfile)
3941{
3942 struct ctemp_state *state = tevent_req_data(req,
3943 struct ctemp_state);
3944 NTSTATUS status;
3945
3946 if (tevent_req_is_nterror(req, &status)) {
3947 return status;
3948 }
3949 *pfnum = state->fnum;
3950 *outfile = talloc_strdup(ctx, state->ret_path);
3951 if (!*outfile) {
3952 return NT_STATUS_NO_MEMORY;
3953 }
3954 return NT_STATUS_OK;
3955}
3956
3957NTSTATUS cli_ctemp(struct cli_state *cli,
3958 TALLOC_CTX *ctx,
3959 const char *path,
3960 uint16_t *pfnum,
3961 char **out_path)
3962{
3963 TALLOC_CTX *frame = talloc_stackframe();
3964 struct event_context *ev;
3965 struct tevent_req *req;
3966 NTSTATUS status = NT_STATUS_OK;
3967
3968 if (cli_has_async_calls(cli)) {
3969 /*
3970 * Can't use sync call while an async call is in flight
3971 */
3972 status = NT_STATUS_INVALID_PARAMETER;
3973 goto fail;
3974 }
3975
3976 ev = event_context_init(frame);
3977 if (ev == NULL) {
3978 status = NT_STATUS_NO_MEMORY;
3979 goto fail;
3980 }
3981
3982 req = cli_ctemp_send(frame, ev, cli, path);
3983 if (req == NULL) {
3984 status = NT_STATUS_NO_MEMORY;
3985 goto fail;
3986 }
3987
3988 if (!tevent_req_poll(req, ev)) {
3989 status = map_nt_error_from_unix(errno);
3990 goto fail;
3991 }
3992
3993 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
3994
3995 fail:
3996 TALLOC_FREE(frame);
3997 if (!NT_STATUS_IS_OK(status)) {
3998 cli_set_error(cli, status);
3999 }
4000 return status;
4001}
4002
4003/*
4004 send a raw ioctl - used by the torture code
4005*/
4006NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4007{
4008 uint16_t vwv[3];
4009 NTSTATUS status;
4010
4011 SSVAL(vwv+0, 0, fnum);
4012 SSVAL(vwv+1, 0, code>>16);
4013 SSVAL(vwv+2, 0, (code&0xFFFF));
4014
4015 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4016 NULL, 0, NULL, NULL, NULL, NULL);
4017 if (!NT_STATUS_IS_OK(status)) {
4018 return status;
4019 }
4020 *blob = data_blob_null;
4021 return NT_STATUS_OK;
4022}
4023
4024/*********************************************************
4025 Set an extended attribute utility fn.
4026*********************************************************/
4027
4028static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4029 uint8_t *param, unsigned int param_len,
4030 const char *ea_name,
4031 const char *ea_val, size_t ea_len)
4032{
4033 uint16_t setup[1];
4034 unsigned int data_len = 0;
4035 uint8_t *data = NULL;
4036 char *p;
4037 size_t ea_namelen = strlen(ea_name);
4038 NTSTATUS status;
4039
4040 SSVAL(setup, 0, setup_val);
4041
4042 if (ea_namelen == 0 && ea_len == 0) {
4043 data_len = 4;
4044 data = (uint8_t *)SMB_MALLOC(data_len);
4045 if (!data) {
4046 return NT_STATUS_NO_MEMORY;
4047 }
4048 p = (char *)data;
4049 SIVAL(p,0,data_len);
4050 } else {
4051 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4052 data = (uint8_t *)SMB_MALLOC(data_len);
4053 if (!data) {
4054 return NT_STATUS_NO_MEMORY;
4055 }
4056 p = (char *)data;
4057 SIVAL(p,0,data_len);
4058 p += 4;
4059 SCVAL(p, 0, 0); /* EA flags. */
4060 SCVAL(p, 1, ea_namelen);
4061 SSVAL(p, 2, ea_len);
4062 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4063 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4064 }
4065
4066 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4067 setup, 1, 0,
4068 param, param_len, 2,
4069 data, data_len, cli->max_xmit,
4070 NULL,
4071 NULL, 0, NULL, /* rsetup */
4072 NULL, 0, NULL, /* rparam */
4073 NULL, 0, NULL); /* rdata */
4074 SAFE_FREE(data);
4075 return status;
4076}
4077
4078/*********************************************************
4079 Set an extended attribute on a pathname.
4080*********************************************************/
4081
4082NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4083 const char *ea_name, const char *ea_val,
4084 size_t ea_len)
4085{
4086 unsigned int param_len = 0;
4087 uint8_t *param;
4088 size_t srclen = 2*(strlen(path)+1);
4089 char *p;
4090 NTSTATUS status;
4091
4092 param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
4093 if (!param) {
4094 return NT_STATUS_NO_MEMORY;
4095 }
4096 memset(param, '\0', 6);
4097 SSVAL(param,0,SMB_INFO_SET_EA);
4098 p = (char *)(&param[6]);
4099
4100 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4101 param_len = PTR_DIFF(p, param);
4102
4103 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4104 ea_name, ea_val, ea_len);
4105 SAFE_FREE(param);
4106 return status;
4107}
4108
4109/*********************************************************
4110 Set an extended attribute on an fnum.
4111*********************************************************/
4112
4113NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4114 const char *ea_name, const char *ea_val,
4115 size_t ea_len)
4116{
4117 uint8_t param[6];
4118
4119 memset(param, 0, 6);
4120 SSVAL(param,0,fnum);
4121 SSVAL(param,2,SMB_INFO_SET_EA);
4122
4123 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4124 ea_name, ea_val, ea_len);
4125}
4126
4127/*********************************************************
4128 Get an extended attribute list utility fn.
4129*********************************************************/
4130
4131static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4132 size_t rdata_len,
4133 size_t *pnum_eas, struct ea_struct **pea_list)
4134{
4135 struct ea_struct *ea_list = NULL;
4136 size_t num_eas;
4137 size_t ea_size;
4138 const uint8_t *p;
4139
4140 if (rdata_len < 4) {
4141 return false;
4142 }
4143
4144 ea_size = (size_t)IVAL(rdata,0);
4145 if (ea_size > rdata_len) {
4146 return false;
4147 }
4148
4149 if (ea_size == 0) {
4150 /* No EA's present. */
4151 *pnum_eas = 0;
4152 *pea_list = NULL;
4153 return true;
4154 }
4155
4156 p = rdata + 4;
4157 ea_size -= 4;
4158
4159 /* Validate the EA list and count it. */
4160 for (num_eas = 0; ea_size >= 4; num_eas++) {
4161 unsigned int ea_namelen = CVAL(p,1);
4162 unsigned int ea_valuelen = SVAL(p,2);
4163 if (ea_namelen == 0) {
4164 return false;
4165 }
4166 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4167 return false;
4168 }
4169 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4170 p += 4 + ea_namelen + 1 + ea_valuelen;
4171 }
4172
4173 if (num_eas == 0) {
4174 *pnum_eas = 0;
4175 *pea_list = NULL;
4176 return true;
4177 }
4178
4179 *pnum_eas = num_eas;
4180 if (!pea_list) {
4181 /* Caller only wants number of EA's. */
4182 return true;
4183 }
4184
4185 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4186 if (!ea_list) {
4187 return false;
4188 }
4189
4190 ea_size = (size_t)IVAL(rdata,0);
4191 p = rdata + 4;
4192
4193 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4194 struct ea_struct *ea = &ea_list[num_eas];
4195 fstring unix_ea_name;
4196 unsigned int ea_namelen = CVAL(p,1);
4197 unsigned int ea_valuelen = SVAL(p,2);
4198
4199 ea->flags = CVAL(p,0);
4200 unix_ea_name[0] = '\0';
4201 pull_ascii_fstring(unix_ea_name, p + 4);
4202 ea->name = talloc_strdup(ea_list, unix_ea_name);
4203 if (!ea->name) {
4204 goto fail;
4205 }
4206 /* Ensure the value is null terminated (in case it's a string). */
4207 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4208 if (!ea->value.data) {
4209 goto fail;
4210 }
4211 if (ea_valuelen) {
4212 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4213 }
4214 ea->value.data[ea_valuelen] = 0;
4215 ea->value.length--;
4216 p += 4 + ea_namelen + 1 + ea_valuelen;
4217 }
4218
4219 *pea_list = ea_list;
4220 return true;
4221
4222fail:
4223 TALLOC_FREE(ea_list);
4224 return false;
4225}
4226
4227/*********************************************************
4228 Get an extended attribute list from a pathname.
4229*********************************************************/
4230
4231struct cli_get_ea_list_path_state {
4232 uint32_t num_data;
4233 uint8_t *data;
4234};
4235
4236static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4237
4238struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4239 struct tevent_context *ev,
4240 struct cli_state *cli,
4241 const char *fname)
4242{
4243 struct tevent_req *req, *subreq;
4244 struct cli_get_ea_list_path_state *state;
4245
4246 req = tevent_req_create(mem_ctx, &state,
4247 struct cli_get_ea_list_path_state);
4248 if (req == NULL) {
4249 return NULL;
4250 }
4251 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4252 SMB_INFO_QUERY_ALL_EAS, 4,
4253 cli->max_xmit);
4254 if (tevent_req_nomem(subreq, req)) {
4255 return tevent_req_post(req, ev);
4256 }
4257 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4258 return req;
4259}
4260
4261static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4262{
4263 struct tevent_req *req = tevent_req_callback_data(
4264 subreq, struct tevent_req);
4265 struct cli_get_ea_list_path_state *state = tevent_req_data(
4266 req, struct cli_get_ea_list_path_state);
4267 NTSTATUS status;
4268
4269 status = cli_qpathinfo_recv(subreq, state, &state->data,
4270 &state->num_data);
4271 TALLOC_FREE(subreq);
4272 if (tevent_req_nterror(req, status)) {
4273 return;
4274 }
4275 tevent_req_done(req);
4276}
4277
4278NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4279 size_t *pnum_eas, struct ea_struct **peas)
4280{
4281 struct cli_get_ea_list_path_state *state = tevent_req_data(
4282 req, struct cli_get_ea_list_path_state);
4283 NTSTATUS status;
4284
4285 if (tevent_req_is_nterror(req, &status)) {
4286 return status;
4287 }
4288 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4289 pnum_eas, peas)) {
4290 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4291 }
4292 return NT_STATUS_OK;
4293}
4294
4295NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4296 TALLOC_CTX *ctx,
4297 size_t *pnum_eas,
4298 struct ea_struct **pea_list)
4299{
4300 TALLOC_CTX *frame = talloc_stackframe();
4301 struct event_context *ev = NULL;
4302 struct tevent_req *req = NULL;
4303 NTSTATUS status = NT_STATUS_NO_MEMORY;
4304
4305 if (cli_has_async_calls(cli)) {
4306 /*
4307 * Can't use sync call while an async call is in flight
4308 */
4309 status = NT_STATUS_INVALID_PARAMETER;
4310 goto fail;
4311 }
4312 ev = event_context_init(frame);
4313 if (ev == NULL) {
4314 goto fail;
4315 }
4316 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4317 if (req == NULL) {
4318 goto fail;
4319 }
4320 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4321 goto fail;
4322 }
4323 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4324 fail:
4325 TALLOC_FREE(frame);
4326 if (!NT_STATUS_IS_OK(status)) {
4327 cli_set_error(cli, status);
4328 }
4329 return status;
4330}
4331
4332/****************************************************************************
4333 Convert open "flags" arg to uint32_t on wire.
4334****************************************************************************/
4335
4336static uint32_t open_flags_to_wire(int flags)
4337{
4338 int open_mode = flags & O_ACCMODE;
4339 uint32_t ret = 0;
4340
4341 switch (open_mode) {
4342 case O_WRONLY:
4343 ret |= SMB_O_WRONLY;
4344 break;
4345 case O_RDWR:
4346 ret |= SMB_O_RDWR;
4347 break;
4348 default:
4349 case O_RDONLY:
4350 ret |= SMB_O_RDONLY;
4351 break;
4352 }
4353
4354 if (flags & O_CREAT) {
4355 ret |= SMB_O_CREAT;
4356 }
4357 if (flags & O_EXCL) {
4358 ret |= SMB_O_EXCL;
4359 }
4360 if (flags & O_TRUNC) {
4361 ret |= SMB_O_TRUNC;
4362 }
4363#if defined(O_SYNC)
4364 if (flags & O_SYNC) {
4365 ret |= SMB_O_SYNC;
4366 }
4367#endif /* O_SYNC */
4368 if (flags & O_APPEND) {
4369 ret |= SMB_O_APPEND;
4370 }
4371#if defined(O_DIRECT)
4372 if (flags & O_DIRECT) {
4373 ret |= SMB_O_DIRECT;
4374 }
4375#endif
4376#if defined(O_DIRECTORY)
4377 if (flags & O_DIRECTORY) {
4378 ret |= SMB_O_DIRECTORY;
4379 }
4380#endif
4381 return ret;
4382}
4383
4384/****************************************************************************
4385 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4386****************************************************************************/
4387
4388struct posix_open_state {
4389 uint16_t setup;
4390 uint8_t *param;
4391 uint8_t data[18];
4392 uint16_t fnum; /* Out */
4393};
4394
4395static void cli_posix_open_internal_done(struct tevent_req *subreq)
4396{
4397 struct tevent_req *req = tevent_req_callback_data(
4398 subreq, struct tevent_req);
4399 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4400 NTSTATUS status;
4401 uint8_t *data;
4402 uint32_t num_data;
4403
4404 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4405 NULL, 0, NULL, &data, 12, &num_data);
4406 TALLOC_FREE(subreq);
4407 if (tevent_req_nterror(req, status)) {
4408 return;
4409 }
4410 state->fnum = SVAL(data,2);
4411 tevent_req_done(req);
4412}
4413
4414static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4415 struct event_context *ev,
4416 struct cli_state *cli,
4417 const char *fname,
4418 int flags,
4419 mode_t mode,
4420 bool is_dir)
4421{
4422 struct tevent_req *req = NULL, *subreq = NULL;
4423 struct posix_open_state *state = NULL;
4424 uint32_t wire_flags = open_flags_to_wire(flags);
4425
4426 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4427 if (req == NULL) {
4428 return NULL;
4429 }
4430
4431 /* Setup setup word. */
4432 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4433
4434 /* Setup param array. */
4435 state->param = talloc_array(state, uint8_t, 6);
4436 if (tevent_req_nomem(state->param, req)) {
4437 return tevent_req_post(req, ev);
4438 }
4439 memset(state->param, '\0', 6);
4440 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4441
4442 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4443 strlen(fname)+1, NULL);
4444
4445 if (tevent_req_nomem(state->param, req)) {
4446 return tevent_req_post(req, ev);
4447 }
4448
4449 /* Setup data words. */
4450 if (is_dir) {
4451 wire_flags |= SMB_O_DIRECTORY;
4452 }
4453
4454 SIVAL(state->data,0,0); /* No oplock. */
4455 SIVAL(state->data,4,wire_flags);
4456 SIVAL(state->data,8,unix_perms_to_wire(mode));
4457 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4458 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4459
4460 subreq = cli_trans_send(state, /* mem ctx. */
4461 ev, /* event ctx. */
4462 cli, /* cli_state. */
4463 SMBtrans2, /* cmd. */
4464 NULL, /* pipe name. */
4465 -1, /* fid. */
4466 0, /* function. */
4467 0, /* flags. */
4468 &state->setup, /* setup. */
4469 1, /* num setup uint16_t words. */
4470 0, /* max returned setup. */
4471 state->param, /* param. */
4472 talloc_get_size(state->param),/* num param. */
4473 2, /* max returned param. */
4474 state->data, /* data. */
4475 18, /* num data. */
4476 12); /* max returned data. */
4477
4478 if (tevent_req_nomem(subreq, req)) {
4479 return tevent_req_post(req, ev);
4480 }
4481 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4482 return req;
4483}
4484
4485struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4486 struct event_context *ev,
4487 struct cli_state *cli,
4488 const char *fname,
4489 int flags,
4490 mode_t mode)
4491{
4492 return cli_posix_open_internal_send(mem_ctx, ev,
4493 cli, fname, flags, mode, false);
4494}
4495
4496NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4497{
4498 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4499 NTSTATUS status;
4500
4501 if (tevent_req_is_nterror(req, &status)) {
4502 return status;
4503 }
4504 *pfnum = state->fnum;
4505 return NT_STATUS_OK;
4506}
4507
4508/****************************************************************************
4509 Open - POSIX semantics. Doesn't request oplock.
4510****************************************************************************/
4511
4512NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4513 int flags, mode_t mode, uint16_t *pfnum)
4514{
4515
4516 TALLOC_CTX *frame = talloc_stackframe();
4517 struct event_context *ev = NULL;
4518 struct tevent_req *req = NULL;
4519 NTSTATUS status = NT_STATUS_OK;
4520
4521 if (cli_has_async_calls(cli)) {
4522 /*
4523 * Can't use sync call while an async call is in flight
4524 */
4525 status = NT_STATUS_INVALID_PARAMETER;
4526 goto fail;
4527 }
4528
4529 ev = event_context_init(frame);
4530 if (ev == NULL) {
4531 status = NT_STATUS_NO_MEMORY;
4532 goto fail;
4533 }
4534
4535 req = cli_posix_open_send(frame,
4536 ev,
4537 cli,
4538 fname,
4539 flags,
4540 mode);
4541 if (req == NULL) {
4542 status = NT_STATUS_NO_MEMORY;
4543 goto fail;
4544 }
4545
4546 if (!tevent_req_poll(req, ev)) {
4547 status = map_nt_error_from_unix(errno);
4548 goto fail;
4549 }
4550
4551 status = cli_posix_open_recv(req, pfnum);
4552
4553 fail:
4554 TALLOC_FREE(frame);
4555 if (!NT_STATUS_IS_OK(status)) {
4556 cli_set_error(cli, status);
4557 }
4558 return status;
4559}
4560
4561struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4562 struct event_context *ev,
4563 struct cli_state *cli,
4564 const char *fname,
4565 mode_t mode)
4566{
4567 return cli_posix_open_internal_send(mem_ctx, ev,
4568 cli, fname, O_CREAT, mode, true);
4569}
4570
4571NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4572{
4573 return tevent_req_simple_recv_ntstatus(req);
4574}
4575
4576NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4577{
4578 TALLOC_CTX *frame = talloc_stackframe();
4579 struct event_context *ev = NULL;
4580 struct tevent_req *req = NULL;
4581 NTSTATUS status = NT_STATUS_OK;
4582
4583 if (cli_has_async_calls(cli)) {
4584 /*
4585 * Can't use sync call while an async call is in flight
4586 */
4587 status = NT_STATUS_INVALID_PARAMETER;
4588 goto fail;
4589 }
4590
4591 ev = event_context_init(frame);
4592 if (ev == NULL) {
4593 status = NT_STATUS_NO_MEMORY;
4594 goto fail;
4595 }
4596
4597 req = cli_posix_mkdir_send(frame,
4598 ev,
4599 cli,
4600 fname,
4601 mode);
4602 if (req == NULL) {
4603 status = NT_STATUS_NO_MEMORY;
4604 goto fail;
4605 }
4606
4607 if (!tevent_req_poll(req, ev)) {
4608 status = map_nt_error_from_unix(errno);
4609 goto fail;
4610 }
4611
4612 status = cli_posix_mkdir_recv(req);
4613
4614 fail:
4615 TALLOC_FREE(frame);
4616 if (!NT_STATUS_IS_OK(status)) {
4617 cli_set_error(cli, status);
4618 }
4619 return status;
4620}
4621
4622/****************************************************************************
4623 unlink or rmdir - POSIX semantics.
4624****************************************************************************/
4625
4626struct cli_posix_unlink_internal_state {
4627 uint8_t data[2];
4628};
4629
4630static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4631
4632static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4633 struct event_context *ev,
4634 struct cli_state *cli,
4635 const char *fname,
4636 uint16_t level)
4637{
4638 struct tevent_req *req = NULL, *subreq = NULL;
4639 struct cli_posix_unlink_internal_state *state = NULL;
4640
4641 req = tevent_req_create(mem_ctx, &state,
4642 struct cli_posix_unlink_internal_state);
4643 if (req == NULL) {
4644 return NULL;
4645 }
4646
4647 /* Setup data word. */
4648 SSVAL(state->data, 0, level);
4649
4650 subreq = cli_setpathinfo_send(state, ev, cli,
4651 SMB_POSIX_PATH_UNLINK,
4652 fname,
4653 state->data, sizeof(state->data));
4654 if (tevent_req_nomem(subreq, req)) {
4655 return tevent_req_post(req, ev);
4656 }
4657 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4658 return req;
4659}
4660
4661static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4662{
4663 NTSTATUS status = cli_setpathinfo_recv(subreq);
4664 tevent_req_simple_finish_ntstatus(subreq, status);
4665}
4666
4667struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4668 struct event_context *ev,
4669 struct cli_state *cli,
4670 const char *fname)
4671{
4672 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4673 SMB_POSIX_UNLINK_FILE_TARGET);
4674}
4675
4676NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4677{
4678 return tevent_req_simple_recv_ntstatus(req);
4679}
4680
4681/****************************************************************************
4682 unlink - POSIX semantics.
4683****************************************************************************/
4684
4685NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4686{
4687 TALLOC_CTX *frame = talloc_stackframe();
4688 struct event_context *ev = NULL;
4689 struct tevent_req *req = NULL;
4690 NTSTATUS status = NT_STATUS_OK;
4691
4692 if (cli_has_async_calls(cli)) {
4693 /*
4694 * Can't use sync call while an async call is in flight
4695 */
4696 status = NT_STATUS_INVALID_PARAMETER;
4697 goto fail;
4698 }
4699
4700 ev = event_context_init(frame);
4701 if (ev == NULL) {
4702 status = NT_STATUS_NO_MEMORY;
4703 goto fail;
4704 }
4705
4706 req = cli_posix_unlink_send(frame,
4707 ev,
4708 cli,
4709 fname);
4710 if (req == NULL) {
4711 status = NT_STATUS_NO_MEMORY;
4712 goto fail;
4713 }
4714
4715 if (!tevent_req_poll(req, ev)) {
4716 status = map_nt_error_from_unix(errno);
4717 goto fail;
4718 }
4719
4720 status = cli_posix_unlink_recv(req);
4721
4722 fail:
4723 TALLOC_FREE(frame);
4724 if (!NT_STATUS_IS_OK(status)) {
4725 cli_set_error(cli, status);
4726 }
4727 return status;
4728}
4729
4730/****************************************************************************
4731 rmdir - POSIX semantics.
4732****************************************************************************/
4733
4734struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4735 struct event_context *ev,
4736 struct cli_state *cli,
4737 const char *fname)
4738{
4739 return cli_posix_unlink_internal_send(
4740 mem_ctx, ev, cli, fname,
4741 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4742}
4743
4744NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4745{
4746 return tevent_req_simple_recv_ntstatus(req);
4747}
4748
4749NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4750{
4751 TALLOC_CTX *frame = talloc_stackframe();
4752 struct event_context *ev = NULL;
4753 struct tevent_req *req = NULL;
4754 NTSTATUS status = NT_STATUS_OK;
4755
4756 if (cli_has_async_calls(cli)) {
4757 /*
4758 * Can't use sync call while an async call is in flight
4759 */
4760 status = NT_STATUS_INVALID_PARAMETER;
4761 goto fail;
4762 }
4763
4764 ev = event_context_init(frame);
4765 if (ev == NULL) {
4766 status = NT_STATUS_NO_MEMORY;
4767 goto fail;
4768 }
4769
4770 req = cli_posix_rmdir_send(frame,
4771 ev,
4772 cli,
4773 fname);
4774 if (req == NULL) {
4775 status = NT_STATUS_NO_MEMORY;
4776 goto fail;
4777 }
4778
4779 if (!tevent_req_poll(req, ev)) {
4780 status = map_nt_error_from_unix(errno);
4781 goto fail;
4782 }
4783
4784 status = cli_posix_rmdir_recv(req, frame);
4785
4786 fail:
4787 TALLOC_FREE(frame);
4788 if (!NT_STATUS_IS_OK(status)) {
4789 cli_set_error(cli, status);
4790 }
4791 return status;
4792}
4793
4794/****************************************************************************
4795 filechangenotify
4796****************************************************************************/
4797
4798struct cli_notify_state {
4799 uint8_t setup[8];
4800 uint32_t num_changes;
4801 struct notify_change *changes;
4802};
4803
4804static void cli_notify_done(struct tevent_req *subreq);
4805
4806struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4807 struct tevent_context *ev,
4808 struct cli_state *cli, uint16_t fnum,
4809 uint32_t buffer_size,
4810 uint32_t completion_filter, bool recursive)
4811{
4812 struct tevent_req *req, *subreq;
4813 struct cli_notify_state *state;
4814
4815 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4816 if (req == NULL) {
4817 return NULL;
4818 }
4819
4820 SIVAL(state->setup, 0, completion_filter);
4821 SSVAL(state->setup, 4, fnum);
4822 SSVAL(state->setup, 6, recursive);
4823
4824 subreq = cli_trans_send(
4825 state, /* mem ctx. */
4826 ev, /* event ctx. */
4827 cli, /* cli_state. */
4828 SMBnttrans, /* cmd. */
4829 NULL, /* pipe name. */
4830 -1, /* fid. */
4831 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4832 0, /* flags. */
4833 (uint16_t *)state->setup, /* setup. */
4834 4, /* num setup uint16_t words. */
4835 0, /* max returned setup. */
4836 NULL, /* param. */
4837 0, /* num param. */
4838 buffer_size, /* max returned param. */
4839 NULL, /* data. */
4840 0, /* num data. */
4841 0); /* max returned data. */
4842
4843 if (tevent_req_nomem(subreq, req)) {
4844 return tevent_req_post(req, ev);
4845 }
4846 tevent_req_set_callback(subreq, cli_notify_done, req);
4847 return req;
4848}
4849
4850static void cli_notify_done(struct tevent_req *subreq)
4851{
4852 struct tevent_req *req = tevent_req_callback_data(
4853 subreq, struct tevent_req);
4854 struct cli_notify_state *state = tevent_req_data(
4855 req, struct cli_notify_state);
4856 NTSTATUS status;
4857 uint8_t *params;
4858 uint32_t i, ofs, num_params;
4859 uint16_t flags2;
4860
4861 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4862 &params, 0, &num_params, NULL, 0, NULL);
4863 TALLOC_FREE(subreq);
4864 if (tevent_req_nterror(req, status)) {
4865 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4866 return;
4867 }
4868
4869 state->num_changes = 0;
4870 ofs = 0;
4871
4872 while (num_params - ofs > 12) {
4873 uint32_t len = IVAL(params, ofs);
4874 state->num_changes += 1;
4875
4876 if ((len == 0) || (ofs+len >= num_params)) {
4877 break;
4878 }
4879 ofs += len;
4880 }
4881
4882 state->changes = talloc_array(state, struct notify_change,
4883 state->num_changes);
4884 if (tevent_req_nomem(state->changes, req)) {
4885 TALLOC_FREE(params);
4886 return;
4887 }
4888
4889 ofs = 0;
4890
4891 for (i=0; i<state->num_changes; i++) {
4892 uint32_t next = IVAL(params, ofs);
4893 uint32_t len = IVAL(params, ofs+8);
4894 ssize_t ret;
4895 char *name;
4896
4897 if ((next != 0) && (len+12 != next)) {
4898 TALLOC_FREE(params);
4899 tevent_req_nterror(
4900 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4901 return;
4902 }
4903
4904 state->changes[i].action = IVAL(params, ofs+4);
4905 ret = clistr_pull_talloc(params, (char *)params, flags2,
4906 &name, params+ofs+12, len,
4907 STR_TERMINATE|STR_UNICODE);
4908 if (ret == -1) {
4909 TALLOC_FREE(params);
4910 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4911 return;
4912 }
4913 state->changes[i].name = name;
4914 ofs += next;
4915 }
4916
4917 TALLOC_FREE(params);
4918 tevent_req_done(req);
4919}
4920
4921NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4922 uint32_t *pnum_changes,
4923 struct notify_change **pchanges)
4924{
4925 struct cli_notify_state *state = tevent_req_data(
4926 req, struct cli_notify_state);
4927 NTSTATUS status;
4928
4929 if (tevent_req_is_nterror(req, &status)) {
4930 return status;
4931 }
4932
4933 *pnum_changes = state->num_changes;
4934 *pchanges = talloc_move(mem_ctx, &state->changes);
4935 return NT_STATUS_OK;
4936}
4937
4938struct cli_qpathinfo_state {
4939 uint8_t *param;
4940 uint8_t *data;
4941 uint16_t setup[1];
4942 uint32_t min_rdata;
4943 uint8_t *rdata;
4944 uint32_t num_rdata;
4945};
4946
4947static void cli_qpathinfo_done(struct tevent_req *subreq);
4948
4949struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4950 struct tevent_context *ev,
4951 struct cli_state *cli, const char *fname,
4952 uint16_t level, uint32_t min_rdata,
4953 uint32_t max_rdata)
4954{
4955 struct tevent_req *req, *subreq;
4956 struct cli_qpathinfo_state *state;
4957
4958 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
4959 if (req == NULL) {
4960 return NULL;
4961 }
4962 state->min_rdata = min_rdata;
4963 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
4964
4965 state->param = talloc_zero_array(state, uint8_t, 6);
4966 if (tevent_req_nomem(state->param, req)) {
4967 return tevent_req_post(req, ev);
4968 }
4969 SSVAL(state->param, 0, level);
4970 state->param = trans2_bytes_push_str(
4971 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
4972 if (tevent_req_nomem(state->param, req)) {
4973 return tevent_req_post(req, ev);
4974 }
4975
4976 subreq = cli_trans_send(
4977 state, /* mem ctx. */
4978 ev, /* event ctx. */
4979 cli, /* cli_state. */
4980 SMBtrans2, /* cmd. */
4981 NULL, /* pipe name. */
4982 -1, /* fid. */
4983 0, /* function. */
4984 0, /* flags. */
4985 state->setup, /* setup. */
4986 1, /* num setup uint16_t words. */
4987 0, /* max returned setup. */
4988 state->param, /* param. */
4989 talloc_get_size(state->param), /* num param. */
4990 2, /* max returned param. */
4991 NULL, /* data. */
4992 0, /* num data. */
4993 max_rdata); /* max returned data. */
4994
4995 if (tevent_req_nomem(subreq, req)) {
4996 return tevent_req_post(req, ev);
4997 }
4998 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
4999 return req;
5000}
5001
5002static void cli_qpathinfo_done(struct tevent_req *subreq)
5003{
5004 struct tevent_req *req = tevent_req_callback_data(
5005 subreq, struct tevent_req);
5006 struct cli_qpathinfo_state *state = tevent_req_data(
5007 req, struct cli_qpathinfo_state);
5008 NTSTATUS status;
5009
5010 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5011 NULL, 0, NULL,
5012 &state->rdata, state->min_rdata,
5013 &state->num_rdata);
5014 if (tevent_req_nterror(req, status)) {
5015 return;
5016 }
5017 tevent_req_done(req);
5018}
5019
5020NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5021 uint8_t **rdata, uint32_t *num_rdata)
5022{
5023 struct cli_qpathinfo_state *state = tevent_req_data(
5024 req, struct cli_qpathinfo_state);
5025 NTSTATUS status;
5026
5027 if (tevent_req_is_nterror(req, &status)) {
5028 return status;
5029 }
5030 if (rdata != NULL) {
5031 *rdata = talloc_move(mem_ctx, &state->rdata);
5032 } else {
5033 TALLOC_FREE(state->rdata);
5034 }
5035 if (num_rdata != NULL) {
5036 *num_rdata = state->num_rdata;
5037 }
5038 return NT_STATUS_OK;
5039}
5040
5041NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5042 const char *fname, uint16_t level, uint32_t min_rdata,
5043 uint32_t max_rdata,
5044 uint8_t **rdata, uint32_t *num_rdata)
5045{
5046 TALLOC_CTX *frame = talloc_stackframe();
5047 struct event_context *ev;
5048 struct tevent_req *req;
5049 NTSTATUS status = NT_STATUS_NO_MEMORY;
5050
5051 if (cli_has_async_calls(cli)) {
5052 /*
5053 * Can't use sync call while an async call is in flight
5054 */
5055 status = NT_STATUS_INVALID_PARAMETER;
5056 goto fail;
5057 }
5058 ev = event_context_init(frame);
5059 if (ev == NULL) {
5060 goto fail;
5061 }
5062 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5063 max_rdata);
5064 if (req == NULL) {
5065 goto fail;
5066 }
5067 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5068 goto fail;
5069 }
5070 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5071 fail:
5072 TALLOC_FREE(frame);
5073 if (!NT_STATUS_IS_OK(status)) {
5074 cli_set_error(cli, status);
5075 }
5076 return status;
5077}
5078
5079struct cli_qfileinfo_state {
5080 uint16_t setup[1];
5081 uint8_t param[4];
5082 uint8_t *data;
5083 uint32_t min_rdata;
5084 uint8_t *rdata;
5085 uint32_t num_rdata;
5086};
5087
5088static void cli_qfileinfo_done(struct tevent_req *subreq);
5089
5090struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5091 struct tevent_context *ev,
5092 struct cli_state *cli, uint16_t fnum,
5093 uint16_t level, uint32_t min_rdata,
5094 uint32_t max_rdata)
5095{
5096 struct tevent_req *req, *subreq;
5097 struct cli_qfileinfo_state *state;
5098
5099 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5100 if (req == NULL) {
5101 return NULL;
5102 }
5103 state->min_rdata = min_rdata;
5104 SSVAL(state->param, 0, fnum);
5105 SSVAL(state->param, 2, level);
5106 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5107
5108 subreq = cli_trans_send(
5109 state, /* mem ctx. */
5110 ev, /* event ctx. */
5111 cli, /* cli_state. */
5112 SMBtrans2, /* cmd. */
5113 NULL, /* pipe name. */
5114 -1, /* fid. */
5115 0, /* function. */
5116 0, /* flags. */
5117 state->setup, /* setup. */
5118 1, /* num setup uint16_t words. */
5119 0, /* max returned setup. */
5120 state->param, /* param. */
5121 sizeof(state->param), /* num param. */
5122 2, /* max returned param. */
5123 NULL, /* data. */
5124 0, /* num data. */
5125 max_rdata); /* max returned data. */
5126
5127 if (tevent_req_nomem(subreq, req)) {
5128 return tevent_req_post(req, ev);
5129 }
5130 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5131 return req;
5132}
5133
5134static void cli_qfileinfo_done(struct tevent_req *subreq)
5135{
5136 struct tevent_req *req = tevent_req_callback_data(
5137 subreq, struct tevent_req);
5138 struct cli_qfileinfo_state *state = tevent_req_data(
5139 req, struct cli_qfileinfo_state);
5140 NTSTATUS status;
5141
5142 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5143 NULL, 0, NULL,
5144 &state->rdata, state->min_rdata,
5145 &state->num_rdata);
5146 if (tevent_req_nterror(req, status)) {
5147 return;
5148 }
5149 tevent_req_done(req);
5150}
5151
5152NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5153 uint8_t **rdata, uint32_t *num_rdata)
5154{
5155 struct cli_qfileinfo_state *state = tevent_req_data(
5156 req, struct cli_qfileinfo_state);
5157 NTSTATUS status;
5158
5159 if (tevent_req_is_nterror(req, &status)) {
5160 return status;
5161 }
5162 if (rdata != NULL) {
5163 *rdata = talloc_move(mem_ctx, &state->rdata);
5164 } else {
5165 TALLOC_FREE(state->rdata);
5166 }
5167 if (num_rdata != NULL) {
5168 *num_rdata = state->num_rdata;
5169 }
5170 return NT_STATUS_OK;
5171}
5172
5173NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5174 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5175 uint32_t max_rdata,
5176 uint8_t **rdata, uint32_t *num_rdata)
5177{
5178 TALLOC_CTX *frame = talloc_stackframe();
5179 struct event_context *ev;
5180 struct tevent_req *req;
5181 NTSTATUS status = NT_STATUS_NO_MEMORY;
5182
5183 if (cli_has_async_calls(cli)) {
5184 /*
5185 * Can't use sync call while an async call is in flight
5186 */
5187 status = NT_STATUS_INVALID_PARAMETER;
5188 goto fail;
5189 }
5190 ev = event_context_init(frame);
5191 if (ev == NULL) {
5192 goto fail;
5193 }
5194 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5195 max_rdata);
5196 if (req == NULL) {
5197 goto fail;
5198 }
5199 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5200 goto fail;
5201 }
5202 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5203 fail:
5204 TALLOC_FREE(frame);
5205 if (!NT_STATUS_IS_OK(status)) {
5206 cli_set_error(cli, status);
5207 }
5208 return status;
5209}
5210
5211struct cli_flush_state {
5212 uint16_t vwv[1];
5213};
5214
5215static void cli_flush_done(struct tevent_req *subreq);
5216
5217struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5218 struct event_context *ev,
5219 struct cli_state *cli,
5220 uint16_t fnum)
5221{
5222 struct tevent_req *req, *subreq;
5223 struct cli_flush_state *state;
5224
5225 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5226 if (req == NULL) {
5227 return NULL;
5228 }
5229 SSVAL(state->vwv + 0, 0, fnum);
5230
5231 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5232 0, NULL);
5233 if (tevent_req_nomem(subreq, req)) {
5234 return tevent_req_post(req, ev);
5235 }
5236 tevent_req_set_callback(subreq, cli_flush_done, req);
5237 return req;
5238}
5239
5240static void cli_flush_done(struct tevent_req *subreq)
5241{
5242 struct tevent_req *req = tevent_req_callback_data(
5243 subreq, struct tevent_req);
5244 NTSTATUS status;
5245
5246 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5247 TALLOC_FREE(subreq);
5248 if (tevent_req_nterror(req, status)) {
5249 return;
5250 }
5251 tevent_req_done(req);
5252}
5253
5254NTSTATUS cli_flush_recv(struct tevent_req *req)
5255{
5256 return tevent_req_simple_recv_ntstatus(req);
5257}
5258
5259NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5260{
5261 TALLOC_CTX *frame = talloc_stackframe();
5262 struct event_context *ev;
5263 struct tevent_req *req;
5264 NTSTATUS status = NT_STATUS_NO_MEMORY;
5265
5266 if (cli_has_async_calls(cli)) {
5267 /*
5268 * Can't use sync call while an async call is in flight
5269 */
5270 status = NT_STATUS_INVALID_PARAMETER;
5271 goto fail;
5272 }
5273 ev = event_context_init(frame);
5274 if (ev == NULL) {
5275 goto fail;
5276 }
5277 req = cli_flush_send(frame, ev, cli, fnum);
5278 if (req == NULL) {
5279 goto fail;
5280 }
5281 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5282 goto fail;
5283 }
5284 status = cli_flush_recv(req);
5285 fail:
5286 TALLOC_FREE(frame);
5287 if (!NT_STATUS_IS_OK(status)) {
5288 cli_set_error(cli, status);
5289 }
5290 return status;
5291}
5292
5293struct cli_shadow_copy_data_state {
5294 uint16_t setup[4];
5295 uint8_t *data;
5296 uint32_t num_data;
5297 bool get_names;
5298};
5299
5300static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5301
5302struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5303 struct tevent_context *ev,
5304 struct cli_state *cli,
5305 uint16_t fnum,
5306 bool get_names)
5307{
5308 struct tevent_req *req, *subreq;
5309 struct cli_shadow_copy_data_state *state;
5310 uint32_t ret_size;
5311
5312 req = tevent_req_create(mem_ctx, &state,
5313 struct cli_shadow_copy_data_state);
5314 if (req == NULL) {
5315 return NULL;
5316 }
5317 state->get_names = get_names;
5318 ret_size = get_names ? cli->max_xmit : 16;
5319
5320 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5321 SSVAL(state->setup + 2, 0, fnum);
5322 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5323 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5324
5325 subreq = cli_trans_send(
5326 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5327 state->setup, ARRAY_SIZE(state->setup), 0,
5328 NULL, 0, 0,
5329 NULL, 0, ret_size);
5330 if (tevent_req_nomem(subreq, req)) {
5331 return tevent_req_post(req, ev);
5332 }
5333 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5334 return req;
5335}
5336
5337static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5338{
5339 struct tevent_req *req = tevent_req_callback_data(
5340 subreq, struct tevent_req);
5341 struct cli_shadow_copy_data_state *state = tevent_req_data(
5342 req, struct cli_shadow_copy_data_state);
5343 NTSTATUS status;
5344
5345 status = cli_trans_recv(subreq, state, NULL,
5346 NULL, 0, NULL, /* setup */
5347 NULL, 0, NULL, /* param */
5348 &state->data, 12, &state->num_data);
5349 TALLOC_FREE(subreq);
5350 if (tevent_req_nterror(req, status)) {
5351 return;
5352 }
5353 tevent_req_done(req);
5354}
5355
5356NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5357 char ***pnames, int *pnum_names)
5358{
5359 struct cli_shadow_copy_data_state *state = tevent_req_data(
5360 req, struct cli_shadow_copy_data_state);
5361 char **names;
5362 int i, num_names;
5363 uint32_t dlength;
5364 NTSTATUS status;
5365
5366 if (tevent_req_is_nterror(req, &status)) {
5367 return status;
5368 }
5369 num_names = IVAL(state->data, 4);
5370 dlength = IVAL(state->data, 8);
5371
5372 if (!state->get_names) {
5373 *pnum_names = num_names;
5374 return NT_STATUS_OK;
5375 }
5376
5377 if (dlength+12 > state->num_data) {
5378 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5379 }
5380 names = talloc_array(mem_ctx, char *, num_names);
5381 if (names == NULL) {
5382 return NT_STATUS_NO_MEMORY;
5383 }
5384
5385 for (i=0; i<num_names; i++) {
5386 bool ret;
5387 uint8_t *src;
5388 size_t converted_size;
5389
5390 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5391 ret = convert_string_talloc(
5392 names, CH_UTF16LE, CH_UNIX,
5393 src, 2 * sizeof(SHADOW_COPY_LABEL),
5394 &names[i], &converted_size, True);
5395 if (!ret) {
5396 TALLOC_FREE(names);
5397 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5398 }
5399 }
5400 *pnum_names = num_names;
5401 *pnames = names;
5402 return NT_STATUS_OK;
5403}
5404
5405NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5406 uint16_t fnum, bool get_names,
5407 char ***pnames, int *pnum_names)
5408{
5409 TALLOC_CTX *frame = talloc_stackframe();
5410 struct event_context *ev;
5411 struct tevent_req *req;
5412 NTSTATUS status = NT_STATUS_NO_MEMORY;
5413
5414 if (cli_has_async_calls(cli)) {
5415 /*
5416 * Can't use sync call while an async call is in flight
5417 */
5418 status = NT_STATUS_INVALID_PARAMETER;
5419 goto fail;
5420 }
5421 ev = event_context_init(frame);
5422 if (ev == NULL) {
5423 goto fail;
5424 }
5425 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5426 if (req == NULL) {
5427 goto fail;
5428 }
5429 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5430 goto fail;
5431 }
5432 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5433 fail:
5434 TALLOC_FREE(frame);
5435 if (!NT_STATUS_IS_OK(status)) {
5436 cli_set_error(cli, status);
5437 }
5438 return status;
5439}
Note: See TracBrowser for help on using the repository browser.