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