source: trunk/server/source3/libsmb/clifile.c@ 745

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

Samba Server: updated trunk to 3.6.0

File size: 133.4 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// @todo in earlier port the above cli->max_xmit was changed to CLI_BUFFER_SIZE
4255// @todo is that still needed? if no regressions are found please remove it
4256 if (tevent_req_nomem(subreq, req)) {
4257 return tevent_req_post(req, ev);
4258 }
4259 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4260 return req;
4261}
4262
4263static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4264{
4265 struct tevent_req *req = tevent_req_callback_data(
4266 subreq, struct tevent_req);
4267 struct cli_get_ea_list_path_state *state = tevent_req_data(
4268 req, struct cli_get_ea_list_path_state);
4269 NTSTATUS status;
4270
4271 status = cli_qpathinfo_recv(subreq, state, &state->data,
4272 &state->num_data);
4273 TALLOC_FREE(subreq);
4274 if (tevent_req_nterror(req, status)) {
4275 return;
4276 }
4277 tevent_req_done(req);
4278}
4279
4280NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4281 size_t *pnum_eas, struct ea_struct **peas)
4282{
4283 struct cli_get_ea_list_path_state *state = tevent_req_data(
4284 req, struct cli_get_ea_list_path_state);
4285 NTSTATUS status;
4286
4287 if (tevent_req_is_nterror(req, &status)) {
4288 return status;
4289 }
4290 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4291 pnum_eas, peas)) {
4292 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4293 }
4294 return NT_STATUS_OK;
4295}
4296
4297NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4298 TALLOC_CTX *ctx,
4299 size_t *pnum_eas,
4300 struct ea_struct **pea_list)
4301{
4302 TALLOC_CTX *frame = talloc_stackframe();
4303 struct event_context *ev = NULL;
4304 struct tevent_req *req = NULL;
4305 NTSTATUS status = NT_STATUS_NO_MEMORY;
4306
4307 if (cli_has_async_calls(cli)) {
4308 /*
4309 * Can't use sync call while an async call is in flight
4310 */
4311 status = NT_STATUS_INVALID_PARAMETER;
4312 goto fail;
4313 }
4314 ev = event_context_init(frame);
4315 if (ev == NULL) {
4316 goto fail;
4317 }
4318 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4319 if (req == NULL) {
4320 goto fail;
4321 }
4322 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4323 goto fail;
4324 }
4325 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4326 fail:
4327 TALLOC_FREE(frame);
4328 if (!NT_STATUS_IS_OK(status)) {
4329 cli_set_error(cli, status);
4330 }
4331 return status;
4332}
4333
4334/****************************************************************************
4335 Convert open "flags" arg to uint32_t on wire.
4336****************************************************************************/
4337
4338static uint32_t open_flags_to_wire(int flags)
4339{
4340 int open_mode = flags & O_ACCMODE;
4341 uint32_t ret = 0;
4342
4343 switch (open_mode) {
4344 case O_WRONLY:
4345 ret |= SMB_O_WRONLY;
4346 break;
4347 case O_RDWR:
4348 ret |= SMB_O_RDWR;
4349 break;
4350 default:
4351 case O_RDONLY:
4352 ret |= SMB_O_RDONLY;
4353 break;
4354 }
4355
4356 if (flags & O_CREAT) {
4357 ret |= SMB_O_CREAT;
4358 }
4359 if (flags & O_EXCL) {
4360 ret |= SMB_O_EXCL;
4361 }
4362 if (flags & O_TRUNC) {
4363 ret |= SMB_O_TRUNC;
4364 }
4365#if defined(O_SYNC)
4366 if (flags & O_SYNC) {
4367 ret |= SMB_O_SYNC;
4368 }
4369#endif /* O_SYNC */
4370 if (flags & O_APPEND) {
4371 ret |= SMB_O_APPEND;
4372 }
4373#if defined(O_DIRECT)
4374 if (flags & O_DIRECT) {
4375 ret |= SMB_O_DIRECT;
4376 }
4377#endif
4378#if defined(O_DIRECTORY)
4379 if (flags & O_DIRECTORY) {
4380 ret |= SMB_O_DIRECTORY;
4381 }
4382#endif
4383 return ret;
4384}
4385
4386/****************************************************************************
4387 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4388****************************************************************************/
4389
4390struct posix_open_state {
4391 uint16_t setup;
4392 uint8_t *param;
4393 uint8_t data[18];
4394 uint16_t fnum; /* Out */
4395};
4396
4397static void cli_posix_open_internal_done(struct tevent_req *subreq)
4398{
4399 struct tevent_req *req = tevent_req_callback_data(
4400 subreq, struct tevent_req);
4401 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4402 NTSTATUS status;
4403 uint8_t *data;
4404 uint32_t num_data;
4405
4406 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4407 NULL, 0, NULL, &data, 12, &num_data);
4408 TALLOC_FREE(subreq);
4409 if (tevent_req_nterror(req, status)) {
4410 return;
4411 }
4412 state->fnum = SVAL(data,2);
4413 tevent_req_done(req);
4414}
4415
4416static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4417 struct event_context *ev,
4418 struct cli_state *cli,
4419 const char *fname,
4420 int flags,
4421 mode_t mode,
4422 bool is_dir)
4423{
4424 struct tevent_req *req = NULL, *subreq = NULL;
4425 struct posix_open_state *state = NULL;
4426 uint32_t wire_flags = open_flags_to_wire(flags);
4427
4428 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4429 if (req == NULL) {
4430 return NULL;
4431 }
4432
4433 /* Setup setup word. */
4434 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4435
4436 /* Setup param array. */
4437 state->param = talloc_array(state, uint8_t, 6);
4438 if (tevent_req_nomem(state->param, req)) {
4439 return tevent_req_post(req, ev);
4440 }
4441 memset(state->param, '\0', 6);
4442 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4443
4444 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4445 strlen(fname)+1, NULL);
4446
4447 if (tevent_req_nomem(state->param, req)) {
4448 return tevent_req_post(req, ev);
4449 }
4450
4451 /* Setup data words. */
4452 if (is_dir) {
4453 wire_flags |= SMB_O_DIRECTORY;
4454 }
4455
4456 SIVAL(state->data,0,0); /* No oplock. */
4457 SIVAL(state->data,4,wire_flags);
4458 SIVAL(state->data,8,unix_perms_to_wire(mode));
4459 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4460 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4461
4462 subreq = cli_trans_send(state, /* mem ctx. */
4463 ev, /* event ctx. */
4464 cli, /* cli_state. */
4465 SMBtrans2, /* cmd. */
4466 NULL, /* pipe name. */
4467 -1, /* fid. */
4468 0, /* function. */
4469 0, /* flags. */
4470 &state->setup, /* setup. */
4471 1, /* num setup uint16_t words. */
4472 0, /* max returned setup. */
4473 state->param, /* param. */
4474 talloc_get_size(state->param),/* num param. */
4475 2, /* max returned param. */
4476 state->data, /* data. */
4477 18, /* num data. */
4478 12); /* max returned data. */
4479
4480 if (tevent_req_nomem(subreq, req)) {
4481 return tevent_req_post(req, ev);
4482 }
4483 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4484 return req;
4485}
4486
4487struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4488 struct event_context *ev,
4489 struct cli_state *cli,
4490 const char *fname,
4491 int flags,
4492 mode_t mode)
4493{
4494 return cli_posix_open_internal_send(mem_ctx, ev,
4495 cli, fname, flags, mode, false);
4496}
4497
4498NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4499{
4500 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4501 NTSTATUS status;
4502
4503 if (tevent_req_is_nterror(req, &status)) {
4504 return status;
4505 }
4506 *pfnum = state->fnum;
4507 return NT_STATUS_OK;
4508}
4509
4510/****************************************************************************
4511 Open - POSIX semantics. Doesn't request oplock.
4512****************************************************************************/
4513
4514NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4515 int flags, mode_t mode, uint16_t *pfnum)
4516{
4517
4518 TALLOC_CTX *frame = talloc_stackframe();
4519 struct event_context *ev = NULL;
4520 struct tevent_req *req = NULL;
4521 NTSTATUS status = NT_STATUS_OK;
4522
4523 if (cli_has_async_calls(cli)) {
4524 /*
4525 * Can't use sync call while an async call is in flight
4526 */
4527 status = NT_STATUS_INVALID_PARAMETER;
4528 goto fail;
4529 }
4530
4531 ev = event_context_init(frame);
4532 if (ev == NULL) {
4533 status = NT_STATUS_NO_MEMORY;
4534 goto fail;
4535 }
4536
4537 req = cli_posix_open_send(frame,
4538 ev,
4539 cli,
4540 fname,
4541 flags,
4542 mode);
4543 if (req == NULL) {
4544 status = NT_STATUS_NO_MEMORY;
4545 goto fail;
4546 }
4547
4548 if (!tevent_req_poll(req, ev)) {
4549 status = map_nt_error_from_unix(errno);
4550 goto fail;
4551 }
4552
4553 status = cli_posix_open_recv(req, pfnum);
4554
4555 fail:
4556 TALLOC_FREE(frame);
4557 if (!NT_STATUS_IS_OK(status)) {
4558 cli_set_error(cli, status);
4559 }
4560 return status;
4561}
4562
4563struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4564 struct event_context *ev,
4565 struct cli_state *cli,
4566 const char *fname,
4567 mode_t mode)
4568{
4569 return cli_posix_open_internal_send(mem_ctx, ev,
4570 cli, fname, O_CREAT, mode, true);
4571}
4572
4573NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4574{
4575 return tevent_req_simple_recv_ntstatus(req);
4576}
4577
4578NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4579{
4580 TALLOC_CTX *frame = talloc_stackframe();
4581 struct event_context *ev = NULL;
4582 struct tevent_req *req = NULL;
4583 NTSTATUS status = NT_STATUS_OK;
4584
4585 if (cli_has_async_calls(cli)) {
4586 /*
4587 * Can't use sync call while an async call is in flight
4588 */
4589 status = NT_STATUS_INVALID_PARAMETER;
4590 goto fail;
4591 }
4592
4593 ev = event_context_init(frame);
4594 if (ev == NULL) {
4595 status = NT_STATUS_NO_MEMORY;
4596 goto fail;
4597 }
4598
4599 req = cli_posix_mkdir_send(frame,
4600 ev,
4601 cli,
4602 fname,
4603 mode);
4604 if (req == NULL) {
4605 status = NT_STATUS_NO_MEMORY;
4606 goto fail;
4607 }
4608
4609 if (!tevent_req_poll(req, ev)) {
4610 status = map_nt_error_from_unix(errno);
4611 goto fail;
4612 }
4613
4614 status = cli_posix_mkdir_recv(req);
4615
4616 fail:
4617 TALLOC_FREE(frame);
4618 if (!NT_STATUS_IS_OK(status)) {
4619 cli_set_error(cli, status);
4620 }
4621 return status;
4622}
4623
4624/****************************************************************************
4625 unlink or rmdir - POSIX semantics.
4626****************************************************************************/
4627
4628struct cli_posix_unlink_internal_state {
4629 uint8_t data[2];
4630};
4631
4632static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4633
4634static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4635 struct event_context *ev,
4636 struct cli_state *cli,
4637 const char *fname,
4638 uint16_t level)
4639{
4640 struct tevent_req *req = NULL, *subreq = NULL;
4641 struct cli_posix_unlink_internal_state *state = NULL;
4642
4643 req = tevent_req_create(mem_ctx, &state,
4644 struct cli_posix_unlink_internal_state);
4645 if (req == NULL) {
4646 return NULL;
4647 }
4648
4649 /* Setup data word. */
4650 SSVAL(state->data, 0, level);
4651
4652 subreq = cli_setpathinfo_send(state, ev, cli,
4653 SMB_POSIX_PATH_UNLINK,
4654 fname,
4655 state->data, sizeof(state->data));
4656 if (tevent_req_nomem(subreq, req)) {
4657 return tevent_req_post(req, ev);
4658 }
4659 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4660 return req;
4661}
4662
4663static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4664{
4665 NTSTATUS status = cli_setpathinfo_recv(subreq);
4666 tevent_req_simple_finish_ntstatus(subreq, status);
4667}
4668
4669struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4670 struct event_context *ev,
4671 struct cli_state *cli,
4672 const char *fname)
4673{
4674 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4675 SMB_POSIX_UNLINK_FILE_TARGET);
4676}
4677
4678NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4679{
4680 return tevent_req_simple_recv_ntstatus(req);
4681}
4682
4683/****************************************************************************
4684 unlink - POSIX semantics.
4685****************************************************************************/
4686
4687NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4688{
4689 TALLOC_CTX *frame = talloc_stackframe();
4690 struct event_context *ev = NULL;
4691 struct tevent_req *req = NULL;
4692 NTSTATUS status = NT_STATUS_OK;
4693
4694 if (cli_has_async_calls(cli)) {
4695 /*
4696 * Can't use sync call while an async call is in flight
4697 */
4698 status = NT_STATUS_INVALID_PARAMETER;
4699 goto fail;
4700 }
4701
4702 ev = event_context_init(frame);
4703 if (ev == NULL) {
4704 status = NT_STATUS_NO_MEMORY;
4705 goto fail;
4706 }
4707
4708 req = cli_posix_unlink_send(frame,
4709 ev,
4710 cli,
4711 fname);
4712 if (req == NULL) {
4713 status = NT_STATUS_NO_MEMORY;
4714 goto fail;
4715 }
4716
4717 if (!tevent_req_poll(req, ev)) {
4718 status = map_nt_error_from_unix(errno);
4719 goto fail;
4720 }
4721
4722 status = cli_posix_unlink_recv(req);
4723
4724 fail:
4725 TALLOC_FREE(frame);
4726 if (!NT_STATUS_IS_OK(status)) {
4727 cli_set_error(cli, status);
4728 }
4729 return status;
4730}
4731
4732/****************************************************************************
4733 rmdir - POSIX semantics.
4734****************************************************************************/
4735
4736struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4737 struct event_context *ev,
4738 struct cli_state *cli,
4739 const char *fname)
4740{
4741 return cli_posix_unlink_internal_send(
4742 mem_ctx, ev, cli, fname,
4743 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4744}
4745
4746NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4747{
4748 return tevent_req_simple_recv_ntstatus(req);
4749}
4750
4751NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4752{
4753 TALLOC_CTX *frame = talloc_stackframe();
4754 struct event_context *ev = NULL;
4755 struct tevent_req *req = NULL;
4756 NTSTATUS status = NT_STATUS_OK;
4757
4758 if (cli_has_async_calls(cli)) {
4759 /*
4760 * Can't use sync call while an async call is in flight
4761 */
4762 status = NT_STATUS_INVALID_PARAMETER;
4763 goto fail;
4764 }
4765
4766 ev = event_context_init(frame);
4767 if (ev == NULL) {
4768 status = NT_STATUS_NO_MEMORY;
4769 goto fail;
4770 }
4771
4772 req = cli_posix_rmdir_send(frame,
4773 ev,
4774 cli,
4775 fname);
4776 if (req == NULL) {
4777 status = NT_STATUS_NO_MEMORY;
4778 goto fail;
4779 }
4780
4781 if (!tevent_req_poll(req, ev)) {
4782 status = map_nt_error_from_unix(errno);
4783 goto fail;
4784 }
4785
4786 status = cli_posix_rmdir_recv(req, frame);
4787
4788 fail:
4789 TALLOC_FREE(frame);
4790 if (!NT_STATUS_IS_OK(status)) {
4791 cli_set_error(cli, status);
4792 }
4793 return status;
4794}
4795
4796/****************************************************************************
4797 filechangenotify
4798****************************************************************************/
4799
4800struct cli_notify_state {
4801 uint8_t setup[8];
4802 uint32_t num_changes;
4803 struct notify_change *changes;
4804};
4805
4806static void cli_notify_done(struct tevent_req *subreq);
4807
4808struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4809 struct tevent_context *ev,
4810 struct cli_state *cli, uint16_t fnum,
4811 uint32_t buffer_size,
4812 uint32_t completion_filter, bool recursive)
4813{
4814 struct tevent_req *req, *subreq;
4815 struct cli_notify_state *state;
4816
4817 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4818 if (req == NULL) {
4819 return NULL;
4820 }
4821
4822 SIVAL(state->setup, 0, completion_filter);
4823 SSVAL(state->setup, 4, fnum);
4824 SSVAL(state->setup, 6, recursive);
4825
4826 subreq = cli_trans_send(
4827 state, /* mem ctx. */
4828 ev, /* event ctx. */
4829 cli, /* cli_state. */
4830 SMBnttrans, /* cmd. */
4831 NULL, /* pipe name. */
4832 -1, /* fid. */
4833 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4834 0, /* flags. */
4835 (uint16_t *)state->setup, /* setup. */
4836 4, /* num setup uint16_t words. */
4837 0, /* max returned setup. */
4838 NULL, /* param. */
4839 0, /* num param. */
4840 buffer_size, /* max returned param. */
4841 NULL, /* data. */
4842 0, /* num data. */
4843 0); /* max returned data. */
4844
4845 if (tevent_req_nomem(subreq, req)) {
4846 return tevent_req_post(req, ev);
4847 }
4848 tevent_req_set_callback(subreq, cli_notify_done, req);
4849 return req;
4850}
4851
4852static void cli_notify_done(struct tevent_req *subreq)
4853{
4854 struct tevent_req *req = tevent_req_callback_data(
4855 subreq, struct tevent_req);
4856 struct cli_notify_state *state = tevent_req_data(
4857 req, struct cli_notify_state);
4858 NTSTATUS status;
4859 uint8_t *params;
4860 uint32_t i, ofs, num_params;
4861 uint16_t flags2;
4862
4863 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4864 &params, 0, &num_params, NULL, 0, NULL);
4865 TALLOC_FREE(subreq);
4866 if (tevent_req_nterror(req, status)) {
4867 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4868 return;
4869 }
4870
4871 state->num_changes = 0;
4872 ofs = 0;
4873
4874 while (num_params - ofs > 12) {
4875 uint32_t len = IVAL(params, ofs);
4876 state->num_changes += 1;
4877
4878 if ((len == 0) || (ofs+len >= num_params)) {
4879 break;
4880 }
4881 ofs += len;
4882 }
4883
4884 state->changes = talloc_array(state, struct notify_change,
4885 state->num_changes);
4886 if (tevent_req_nomem(state->changes, req)) {
4887 TALLOC_FREE(params);
4888 return;
4889 }
4890
4891 ofs = 0;
4892
4893 for (i=0; i<state->num_changes; i++) {
4894 uint32_t next = IVAL(params, ofs);
4895 uint32_t len = IVAL(params, ofs+8);
4896 ssize_t ret;
4897 char *name;
4898
4899 if ((next != 0) && (len+12 != next)) {
4900 TALLOC_FREE(params);
4901 tevent_req_nterror(
4902 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4903 return;
4904 }
4905
4906 state->changes[i].action = IVAL(params, ofs+4);
4907 ret = clistr_pull_talloc(params, (char *)params, flags2,
4908 &name, params+ofs+12, len,
4909 STR_TERMINATE|STR_UNICODE);
4910 if (ret == -1) {
4911 TALLOC_FREE(params);
4912 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4913 return;
4914 }
4915 state->changes[i].name = name;
4916 ofs += next;
4917 }
4918
4919 TALLOC_FREE(params);
4920 tevent_req_done(req);
4921}
4922
4923NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4924 uint32_t *pnum_changes,
4925 struct notify_change **pchanges)
4926{
4927 struct cli_notify_state *state = tevent_req_data(
4928 req, struct cli_notify_state);
4929 NTSTATUS status;
4930
4931 if (tevent_req_is_nterror(req, &status)) {
4932 return status;
4933 }
4934
4935 *pnum_changes = state->num_changes;
4936 *pchanges = talloc_move(mem_ctx, &state->changes);
4937 return NT_STATUS_OK;
4938}
4939
4940struct cli_qpathinfo_state {
4941 uint8_t *param;
4942 uint8_t *data;
4943 uint16_t setup[1];
4944 uint32_t min_rdata;
4945 uint8_t *rdata;
4946 uint32_t num_rdata;
4947};
4948
4949static void cli_qpathinfo_done(struct tevent_req *subreq);
4950
4951struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4952 struct tevent_context *ev,
4953 struct cli_state *cli, const char *fname,
4954 uint16_t level, uint32_t min_rdata,
4955 uint32_t max_rdata)
4956{
4957 struct tevent_req *req, *subreq;
4958 struct cli_qpathinfo_state *state;
4959
4960 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
4961 if (req == NULL) {
4962 return NULL;
4963 }
4964 state->min_rdata = min_rdata;
4965 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
4966
4967 state->param = talloc_zero_array(state, uint8_t, 6);
4968 if (tevent_req_nomem(state->param, req)) {
4969 return tevent_req_post(req, ev);
4970 }
4971 SSVAL(state->param, 0, level);
4972 state->param = trans2_bytes_push_str(
4973 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
4974 if (tevent_req_nomem(state->param, req)) {
4975 return tevent_req_post(req, ev);
4976 }
4977
4978 subreq = cli_trans_send(
4979 state, /* mem ctx. */
4980 ev, /* event ctx. */
4981 cli, /* cli_state. */
4982 SMBtrans2, /* cmd. */
4983 NULL, /* pipe name. */
4984 -1, /* fid. */
4985 0, /* function. */
4986 0, /* flags. */
4987 state->setup, /* setup. */
4988 1, /* num setup uint16_t words. */
4989 0, /* max returned setup. */
4990 state->param, /* param. */
4991 talloc_get_size(state->param), /* num param. */
4992 2, /* max returned param. */
4993 NULL, /* data. */
4994 0, /* num data. */
4995 max_rdata); /* max returned data. */
4996
4997 if (tevent_req_nomem(subreq, req)) {
4998 return tevent_req_post(req, ev);
4999 }
5000 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5001 return req;
5002}
5003
5004static void cli_qpathinfo_done(struct tevent_req *subreq)
5005{
5006 struct tevent_req *req = tevent_req_callback_data(
5007 subreq, struct tevent_req);
5008 struct cli_qpathinfo_state *state = tevent_req_data(
5009 req, struct cli_qpathinfo_state);
5010 NTSTATUS status;
5011
5012 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5013 NULL, 0, NULL,
5014 &state->rdata, state->min_rdata,
5015 &state->num_rdata);
5016 if (tevent_req_nterror(req, status)) {
5017 return;
5018 }
5019 tevent_req_done(req);
5020}
5021
5022NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5023 uint8_t **rdata, uint32_t *num_rdata)
5024{
5025 struct cli_qpathinfo_state *state = tevent_req_data(
5026 req, struct cli_qpathinfo_state);
5027 NTSTATUS status;
5028
5029 if (tevent_req_is_nterror(req, &status)) {
5030 return status;
5031 }
5032 if (rdata != NULL) {
5033 *rdata = talloc_move(mem_ctx, &state->rdata);
5034 } else {
5035 TALLOC_FREE(state->rdata);
5036 }
5037 if (num_rdata != NULL) {
5038 *num_rdata = state->num_rdata;
5039 }
5040 return NT_STATUS_OK;
5041}
5042
5043NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5044 const char *fname, uint16_t level, uint32_t min_rdata,
5045 uint32_t max_rdata,
5046 uint8_t **rdata, uint32_t *num_rdata)
5047{
5048 TALLOC_CTX *frame = talloc_stackframe();
5049 struct event_context *ev;
5050 struct tevent_req *req;
5051 NTSTATUS status = NT_STATUS_NO_MEMORY;
5052
5053 if (cli_has_async_calls(cli)) {
5054 /*
5055 * Can't use sync call while an async call is in flight
5056 */
5057 status = NT_STATUS_INVALID_PARAMETER;
5058 goto fail;
5059 }
5060 ev = event_context_init(frame);
5061 if (ev == NULL) {
5062 goto fail;
5063 }
5064 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5065 max_rdata);
5066 if (req == NULL) {
5067 goto fail;
5068 }
5069 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5070 goto fail;
5071 }
5072 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5073 fail:
5074 TALLOC_FREE(frame);
5075 if (!NT_STATUS_IS_OK(status)) {
5076 cli_set_error(cli, status);
5077 }
5078 return status;
5079}
5080
5081struct cli_qfileinfo_state {
5082 uint16_t setup[1];
5083 uint8_t param[4];
5084 uint8_t *data;
5085 uint32_t min_rdata;
5086 uint8_t *rdata;
5087 uint32_t num_rdata;
5088};
5089
5090static void cli_qfileinfo_done(struct tevent_req *subreq);
5091
5092struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5093 struct tevent_context *ev,
5094 struct cli_state *cli, uint16_t fnum,
5095 uint16_t level, uint32_t min_rdata,
5096 uint32_t max_rdata)
5097{
5098 struct tevent_req *req, *subreq;
5099 struct cli_qfileinfo_state *state;
5100
5101 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5102 if (req == NULL) {
5103 return NULL;
5104 }
5105 state->min_rdata = min_rdata;
5106 SSVAL(state->param, 0, fnum);
5107 SSVAL(state->param, 2, level);
5108 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5109
5110 subreq = cli_trans_send(
5111 state, /* mem ctx. */
5112 ev, /* event ctx. */
5113 cli, /* cli_state. */
5114 SMBtrans2, /* cmd. */
5115 NULL, /* pipe name. */
5116 -1, /* fid. */
5117 0, /* function. */
5118 0, /* flags. */
5119 state->setup, /* setup. */
5120 1, /* num setup uint16_t words. */
5121 0, /* max returned setup. */
5122 state->param, /* param. */
5123 sizeof(state->param), /* num param. */
5124 2, /* max returned param. */
5125 NULL, /* data. */
5126 0, /* num data. */
5127 max_rdata); /* max returned data. */
5128
5129 if (tevent_req_nomem(subreq, req)) {
5130 return tevent_req_post(req, ev);
5131 }
5132 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5133 return req;
5134}
5135
5136static void cli_qfileinfo_done(struct tevent_req *subreq)
5137{
5138 struct tevent_req *req = tevent_req_callback_data(
5139 subreq, struct tevent_req);
5140 struct cli_qfileinfo_state *state = tevent_req_data(
5141 req, struct cli_qfileinfo_state);
5142 NTSTATUS status;
5143
5144 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5145 NULL, 0, NULL,
5146 &state->rdata, state->min_rdata,
5147 &state->num_rdata);
5148 if (tevent_req_nterror(req, status)) {
5149 return;
5150 }
5151 tevent_req_done(req);
5152}
5153
5154NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5155 uint8_t **rdata, uint32_t *num_rdata)
5156{
5157 struct cli_qfileinfo_state *state = tevent_req_data(
5158 req, struct cli_qfileinfo_state);
5159 NTSTATUS status;
5160
5161 if (tevent_req_is_nterror(req, &status)) {
5162 return status;
5163 }
5164 if (rdata != NULL) {
5165 *rdata = talloc_move(mem_ctx, &state->rdata);
5166 } else {
5167 TALLOC_FREE(state->rdata);
5168 }
5169 if (num_rdata != NULL) {
5170 *num_rdata = state->num_rdata;
5171 }
5172 return NT_STATUS_OK;
5173}
5174
5175NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5176 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5177 uint32_t max_rdata,
5178 uint8_t **rdata, uint32_t *num_rdata)
5179{
5180 TALLOC_CTX *frame = talloc_stackframe();
5181 struct event_context *ev;
5182 struct tevent_req *req;
5183 NTSTATUS status = NT_STATUS_NO_MEMORY;
5184
5185 if (cli_has_async_calls(cli)) {
5186 /*
5187 * Can't use sync call while an async call is in flight
5188 */
5189 status = NT_STATUS_INVALID_PARAMETER;
5190 goto fail;
5191 }
5192 ev = event_context_init(frame);
5193 if (ev == NULL) {
5194 goto fail;
5195 }
5196 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5197 max_rdata);
5198 if (req == NULL) {
5199 goto fail;
5200 }
5201 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5202 goto fail;
5203 }
5204 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5205 fail:
5206 TALLOC_FREE(frame);
5207 if (!NT_STATUS_IS_OK(status)) {
5208 cli_set_error(cli, status);
5209 }
5210 return status;
5211}
5212
5213struct cli_flush_state {
5214 uint16_t vwv[1];
5215};
5216
5217static void cli_flush_done(struct tevent_req *subreq);
5218
5219struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5220 struct event_context *ev,
5221 struct cli_state *cli,
5222 uint16_t fnum)
5223{
5224 struct tevent_req *req, *subreq;
5225 struct cli_flush_state *state;
5226
5227 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5228 if (req == NULL) {
5229 return NULL;
5230 }
5231 SSVAL(state->vwv + 0, 0, fnum);
5232
5233 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5234 0, NULL);
5235 if (tevent_req_nomem(subreq, req)) {
5236 return tevent_req_post(req, ev);
5237 }
5238 tevent_req_set_callback(subreq, cli_flush_done, req);
5239 return req;
5240}
5241
5242static void cli_flush_done(struct tevent_req *subreq)
5243{
5244 struct tevent_req *req = tevent_req_callback_data(
5245 subreq, struct tevent_req);
5246 NTSTATUS status;
5247
5248 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5249 TALLOC_FREE(subreq);
5250 if (tevent_req_nterror(req, status)) {
5251 return;
5252 }
5253 tevent_req_done(req);
5254}
5255
5256NTSTATUS cli_flush_recv(struct tevent_req *req)
5257{
5258 return tevent_req_simple_recv_ntstatus(req);
5259}
5260
5261NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5262{
5263 TALLOC_CTX *frame = talloc_stackframe();
5264 struct event_context *ev;
5265 struct tevent_req *req;
5266 NTSTATUS status = NT_STATUS_NO_MEMORY;
5267
5268 if (cli_has_async_calls(cli)) {
5269 /*
5270 * Can't use sync call while an async call is in flight
5271 */
5272 status = NT_STATUS_INVALID_PARAMETER;
5273 goto fail;
5274 }
5275 ev = event_context_init(frame);
5276 if (ev == NULL) {
5277 goto fail;
5278 }
5279 req = cli_flush_send(frame, ev, cli, fnum);
5280 if (req == NULL) {
5281 goto fail;
5282 }
5283 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5284 goto fail;
5285 }
5286 status = cli_flush_recv(req);
5287 fail:
5288 TALLOC_FREE(frame);
5289 if (!NT_STATUS_IS_OK(status)) {
5290 cli_set_error(cli, status);
5291 }
5292 return status;
5293}
5294
5295struct cli_shadow_copy_data_state {
5296 uint16_t setup[4];
5297 uint8_t *data;
5298 uint32_t num_data;
5299 bool get_names;
5300};
5301
5302static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5303
5304struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5305 struct tevent_context *ev,
5306 struct cli_state *cli,
5307 uint16_t fnum,
5308 bool get_names)
5309{
5310 struct tevent_req *req, *subreq;
5311 struct cli_shadow_copy_data_state *state;
5312 uint32_t ret_size;
5313
5314 req = tevent_req_create(mem_ctx, &state,
5315 struct cli_shadow_copy_data_state);
5316 if (req == NULL) {
5317 return NULL;
5318 }
5319 state->get_names = get_names;
5320 ret_size = get_names ? cli->max_xmit : 16;
5321
5322 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5323 SSVAL(state->setup + 2, 0, fnum);
5324 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5325 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5326
5327 subreq = cli_trans_send(
5328 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5329 state->setup, ARRAY_SIZE(state->setup), 0,
5330 NULL, 0, 0,
5331 NULL, 0, ret_size);
5332 if (tevent_req_nomem(subreq, req)) {
5333 return tevent_req_post(req, ev);
5334 }
5335 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5336 return req;
5337}
5338
5339static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5340{
5341 struct tevent_req *req = tevent_req_callback_data(
5342 subreq, struct tevent_req);
5343 struct cli_shadow_copy_data_state *state = tevent_req_data(
5344 req, struct cli_shadow_copy_data_state);
5345 NTSTATUS status;
5346
5347 status = cli_trans_recv(subreq, state, NULL,
5348 NULL, 0, NULL, /* setup */
5349 NULL, 0, NULL, /* param */
5350 &state->data, 12, &state->num_data);
5351 TALLOC_FREE(subreq);
5352 if (tevent_req_nterror(req, status)) {
5353 return;
5354 }
5355 tevent_req_done(req);
5356}
5357
5358NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5359 char ***pnames, int *pnum_names)
5360{
5361 struct cli_shadow_copy_data_state *state = tevent_req_data(
5362 req, struct cli_shadow_copy_data_state);
5363 char **names;
5364 int i, num_names;
5365 uint32_t dlength;
5366 NTSTATUS status;
5367
5368 if (tevent_req_is_nterror(req, &status)) {
5369 return status;
5370 }
5371 num_names = IVAL(state->data, 4);
5372 dlength = IVAL(state->data, 8);
5373
5374 if (!state->get_names) {
5375 *pnum_names = num_names;
5376 return NT_STATUS_OK;
5377 }
5378
5379 if (dlength+12 > state->num_data) {
5380 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5381 }
5382 names = talloc_array(mem_ctx, char *, num_names);
5383 if (names == NULL) {
5384 return NT_STATUS_NO_MEMORY;
5385 }
5386
5387 for (i=0; i<num_names; i++) {
5388 bool ret;
5389 uint8_t *src;
5390 size_t converted_size;
5391
5392 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5393 ret = convert_string_talloc(
5394 names, CH_UTF16LE, CH_UNIX,
5395 src, 2 * sizeof(SHADOW_COPY_LABEL),
5396 &names[i], &converted_size, True);
5397 if (!ret) {
5398 TALLOC_FREE(names);
5399 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5400 }
5401 }
5402 *pnum_names = num_names;
5403 *pnames = names;
5404 return NT_STATUS_OK;
5405}
5406
5407NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5408 uint16_t fnum, bool get_names,
5409 char ***pnames, int *pnum_names)
5410{
5411 TALLOC_CTX *frame = talloc_stackframe();
5412 struct event_context *ev;
5413 struct tevent_req *req;
5414 NTSTATUS status = NT_STATUS_NO_MEMORY;
5415
5416 if (cli_has_async_calls(cli)) {
5417 /*
5418 * Can't use sync call while an async call is in flight
5419 */
5420 status = NT_STATUS_INVALID_PARAMETER;
5421 goto fail;
5422 }
5423 ev = event_context_init(frame);
5424 if (ev == NULL) {
5425 goto fail;
5426 }
5427 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5428 if (req == NULL) {
5429 goto fail;
5430 }
5431 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5432 goto fail;
5433 }
5434 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5435 fail:
5436 TALLOC_FREE(frame);
5437 if (!NT_STATUS_IS_OK(status)) {
5438 cli_set_error(cli, status);
5439 }
5440 return status;
5441}
Note: See TracBrowser for help on using the repository browser.