Add cli_posix_readlink() and a torture test for it.
[ira/wip.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
23 /***********************************************************
24  Common function for pushing stings, used by smb_bytes_push_str()
25  and trans_bytes_push_str(). Only difference is the align_odd
26  parameter setting.
27 ***********************************************************/
28
29 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
30                                 const char *str, size_t str_len,
31                                 bool align_odd,
32                                 size_t *pconverted_size)
33 {
34         size_t buflen;
35         char *converted;
36         size_t converted_size;
37
38         if (buf == NULL) {
39                 return NULL;
40         }
41
42         buflen = talloc_get_size(buf);
43
44         if (align_odd && ucs2 && (buflen % 2 == 0)) {
45                 /*
46                  * We're pushing into an SMB buffer, align odd
47                  */
48                 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
49                 if (buf == NULL) {
50                         return NULL;
51                 }
52                 buf[buflen] = '\0';
53                 buflen += 1;
54         }
55
56         if (!convert_string_talloc(talloc_tos(), CH_UNIX,
57                                    ucs2 ? CH_UTF16LE : CH_DOS,
58                                    str, str_len, &converted,
59                                    &converted_size, true)) {
60                 return NULL;
61         }
62
63         buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
64                                    buflen + converted_size);
65         if (buf == NULL) {
66                 TALLOC_FREE(converted);
67                 return NULL;
68         }
69
70         memcpy(buf + buflen, converted, converted_size);
71
72         TALLOC_FREE(converted);
73
74         if (pconverted_size) {
75                 *pconverted_size = converted_size;
76         }
77
78         return buf;
79 }
80
81 /***********************************************************
82  Push a string into an SMB buffer, with odd byte alignment
83  if it's a UCS2 string.
84 ***********************************************************/
85
86 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
87                             const char *str, size_t str_len,
88                             size_t *pconverted_size)
89 {
90         return internal_bytes_push_str(buf, ucs2, str, str_len,
91                         true, pconverted_size);
92 }
93
94 /***********************************************************
95  Same as smb_bytes_push_str(), but without the odd byte
96  align for ucs2 (we're pushing into a param or data block).
97  static for now, although this will probably change when
98  other modules use async trans calls.
99 ***********************************************************/
100
101 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
102                             const char *str, size_t str_len,
103                             size_t *pconverted_size)
104 {
105         return internal_bytes_push_str(buf, ucs2, str, str_len,
106                         false, pconverted_size);
107 }
108
109 /****************************************************************************
110  Hard/Symlink a file (UNIX extensions).
111  Creates new name (sym)linked to oldname.
112 ****************************************************************************/
113
114 struct link_state {
115         uint16_t setup;
116         uint8_t *param;
117         uint8_t *data;
118 };
119
120 static void cli_posix_link_internal_done(struct tevent_req *subreq)
121 {
122         struct tevent_req *req = tevent_req_callback_data(
123                                 subreq, struct tevent_req);
124         struct link_state *state = tevent_req_data(req, struct link_state);
125         NTSTATUS status;
126
127         status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
128         TALLOC_FREE(subreq);
129         if (!NT_STATUS_IS_OK(status)) {
130                 tevent_req_nterror(req, status);
131                 return;
132         }
133         tevent_req_done(req);
134 }
135
136 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
137                                         struct event_context *ev,
138                                         struct cli_state *cli,
139                                         const char *oldname,
140                                         const char *newname,
141                                         bool hardlink)
142 {
143         struct tevent_req *req = NULL, *subreq = NULL;
144         struct link_state *state = NULL;
145
146         req = tevent_req_create(mem_ctx, &state, struct link_state);
147         if (req == NULL) {
148                 return NULL;
149         }
150
151         /* Setup setup word. */
152         SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
153
154         /* Setup param array. */
155         state->param = talloc_array(state, uint8_t, 6);
156         if (tevent_req_nomem(state->param, req)) {
157                 return tevent_req_post(req, ev);
158         }
159         memset(state->param, '\0', 6);
160         SSVAL(state->param,0,hardlink ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
161
162         state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), newname,
163                                    strlen(newname)+1, NULL);
164
165         if (tevent_req_nomem(state->param, req)) {
166                 return tevent_req_post(req, ev);
167         }
168
169         /* Setup data array. */
170         state->data = talloc_array(state, uint8_t, 0);
171         if (tevent_req_nomem(state->data, req)) {
172                 return tevent_req_post(req, ev);
173         }
174         state->data = trans2_bytes_push_str(state->data, cli_ucs2(cli), oldname,
175                                    strlen(oldname)+1, NULL);
176
177         subreq = cli_trans_send(state,                  /* mem ctx. */
178                                 ev,                     /* event ctx. */
179                                 cli,                    /* cli_state. */
180                                 SMBtrans2,              /* cmd. */
181                                 NULL,                   /* pipe name. */
182                                 -1,                     /* fid. */
183                                 0,                      /* function. */
184                                 0,                      /* flags. */
185                                 &state->setup,          /* setup. */
186                                 1,                      /* num setup uint16_t words. */
187                                 0,                      /* max returned setup. */
188                                 state->param,           /* param. */
189                                 talloc_get_size(state->param),  /* num param. */
190                                 2,                      /* max returned param. */
191                                 state->data,            /* data. */
192                                 talloc_get_size(state->data),   /* num data. */
193                                 0);                     /* max returned data. */
194
195         if (tevent_req_nomem(subreq, req)) {
196                 return tevent_req_post(req, ev);
197         }
198         tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
199         return req;
200 }
201
202 /****************************************************************************
203  Symlink a file (UNIX extensions).
204 ****************************************************************************/
205
206 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
207                                         struct event_context *ev,
208                                         struct cli_state *cli,
209                                         const char *oldname,
210                                         const char *newname)
211 {
212         return cli_posix_link_internal_send(mem_ctx, ev, cli,
213                         oldname, newname, false);
214 }
215
216 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
217 {
218         NTSTATUS status;
219
220         if (tevent_req_is_nterror(req, &status)) {
221                 return status;
222         }
223         return NT_STATUS_OK;
224 }
225
226 NTSTATUS cli_posix_symlink(struct cli_state *cli,
227                         const char *oldname,
228                         const char *newname)
229 {
230         TALLOC_CTX *frame = talloc_stackframe();
231         struct event_context *ev = NULL;
232         struct tevent_req *req = NULL;
233         NTSTATUS status = NT_STATUS_OK;
234
235         if (cli_has_async_calls(cli)) {
236                 /*
237                  * Can't use sync call while an async call is in flight
238                  */
239                 status = NT_STATUS_INVALID_PARAMETER;
240                 goto fail;
241         }
242
243         ev = event_context_init(frame);
244         if (ev == NULL) {
245                 status = NT_STATUS_NO_MEMORY;
246                 goto fail;
247         }
248
249         req = cli_posix_symlink_send(frame,
250                                 ev,
251                                 cli,
252                                 oldname,
253                                 newname);
254         if (req == NULL) {
255                 status = NT_STATUS_NO_MEMORY;
256                 goto fail;
257         }
258
259         if (!tevent_req_poll(req, ev)) {
260                 status = map_nt_error_from_unix(errno);
261                 goto fail;
262         }
263
264         status = cli_posix_symlink_recv(req);
265
266  fail:
267         TALLOC_FREE(frame);
268         if (!NT_STATUS_IS_OK(status)) {
269                 cli_set_error(cli, status);
270         }
271         return status;
272 }
273
274 /****************************************************************************
275  Read a POSIX symlink.
276 ****************************************************************************/
277
278 struct readlink_state {
279         uint16_t setup;
280         uint8_t *param;
281         uint8_t *data;
282         uint32_t num_data;
283 };
284
285 static void cli_posix_readlink_done(struct tevent_req *subreq)
286 {
287         struct tevent_req *req = tevent_req_callback_data(
288                                 subreq, struct tevent_req);
289         struct readlink_state *state = tevent_req_data(req, struct readlink_state);
290         NTSTATUS status;
291
292         status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL,
293                         &state->data, &state->num_data);
294         TALLOC_FREE(subreq);
295         if (!NT_STATUS_IS_OK(status)) {
296                 tevent_req_nterror(req, status);
297                 return;
298         }
299         if (state->num_data == 0) {
300                 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
301                 return;
302         }
303         if (state->data[state->num_data-1] != '\0') {
304                 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
305                 return;
306         }
307         tevent_req_done(req);
308 }
309
310 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
311                                         struct event_context *ev,
312                                         struct cli_state *cli,
313                                         const char *fname,
314                                         size_t len)
315 {
316         struct tevent_req *req = NULL, *subreq = NULL;
317         struct readlink_state *state = NULL;
318         uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
319
320         if (maxbytelen < len) {
321                 return NULL;
322         }
323
324         req = tevent_req_create(mem_ctx, &state, struct readlink_state);
325         if (req == NULL) {
326                 return NULL;
327         }
328
329         /* Setup setup word. */
330         SSVAL(&state->setup, 0, TRANSACT2_QPATHINFO);
331
332         /* Setup param array. */
333         state->param = talloc_array(state, uint8_t, 6);
334         if (tevent_req_nomem(state->param, req)) {
335                 return tevent_req_post(req, ev);
336         }
337         memset(state->param, '\0', 6);
338         SSVAL(state->param,0,SMB_QUERY_FILE_UNIX_LINK);
339
340         state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
341                                    strlen(fname)+1, NULL);
342
343         if (tevent_req_nomem(state->param, req)) {
344                 return tevent_req_post(req, ev);
345         }
346
347         subreq = cli_trans_send(state,                  /* mem ctx. */
348                                 ev,                     /* event ctx. */
349                                 cli,                    /* cli_state. */
350                                 SMBtrans2,              /* cmd. */
351                                 NULL,                   /* pipe name. */
352                                 -1,                     /* fid. */
353                                 0,                      /* function. */
354                                 0,                      /* flags. */
355                                 &state->setup,          /* setup. */
356                                 1,                      /* num setup uint16_t words. */
357                                 0,                      /* max returned setup. */
358                                 state->param,           /* param. */
359                                 talloc_get_size(state->param),  /* num param. */
360                                 2,                      /* max returned param. */
361                                 NULL,                   /* data. */
362                                 0,                      /* num data. */
363                                 maxbytelen);            /* max returned data. */
364
365         if (tevent_req_nomem(subreq, req)) {
366                 return tevent_req_post(req, ev);
367         }
368         tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
369         return req;
370 }
371
372 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
373                                 char *retpath, size_t len)
374 {
375         NTSTATUS status;
376         char *converted = NULL;
377         size_t converted_size = 0;
378         struct readlink_state *state = tevent_req_data(req, struct readlink_state);
379
380         if (tevent_req_is_nterror(req, &status)) {
381                 return status;
382         }
383         /* The returned data is a pushed string, not raw data. */
384         if (!convert_string_talloc(state,
385                                 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS, 
386                                 CH_UNIX,
387                                 state->data,
388                                 state->num_data,
389                                 &converted,
390                                 &converted_size,
391                                 true)) {
392                 return NT_STATUS_NO_MEMORY;
393         }
394
395         len = MIN(len,converted_size);
396         if (len == 0) {
397                 return NT_STATUS_DATA_ERROR;
398         }
399         memcpy(retpath, converted, len);
400         return NT_STATUS_OK;
401 }
402
403 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
404                                 char *linkpath, size_t len)
405 {
406         TALLOC_CTX *frame = talloc_stackframe();
407         struct event_context *ev = NULL;
408         struct tevent_req *req = NULL;
409         NTSTATUS status = NT_STATUS_OK;
410
411         if (cli_has_async_calls(cli)) {
412                 /*
413                  * Can't use sync call while an async call is in flight
414                  */
415                 status = NT_STATUS_INVALID_PARAMETER;
416                 goto fail;
417         }
418
419         ev = event_context_init(frame);
420         if (ev == NULL) {
421                 status = NT_STATUS_NO_MEMORY;
422                 goto fail;
423         }
424
425         /* Len is in bytes, we need it in UCS2 units. */
426         if (2*len < len) {
427                 status = NT_STATUS_INVALID_PARAMETER;
428                 goto fail;
429         }
430
431         req = cli_posix_readlink_send(frame,
432                                 ev,
433                                 cli,
434                                 fname,
435                                 len);
436         if (req == NULL) {
437                 status = NT_STATUS_NO_MEMORY;
438                 goto fail;
439         }
440
441         if (!tevent_req_poll(req, ev)) {
442                 status = map_nt_error_from_unix(errno);
443                 goto fail;
444         }
445
446         status = cli_posix_readlink_recv(req, cli, linkpath, len);
447
448  fail:
449         TALLOC_FREE(frame);
450         if (!NT_STATUS_IS_OK(status)) {
451                 cli_set_error(cli, status);
452         }
453         return status;
454 }
455
456
457 /****************************************************************************
458  Hard link a file (UNIX extensions).
459 ****************************************************************************/
460
461 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
462                                         struct event_context *ev,
463                                         struct cli_state *cli,
464                                         const char *oldname,
465                                         const char *newname)
466 {
467         return cli_posix_link_internal_send(mem_ctx, ev, cli,
468                         oldname, newname, true);
469 }
470
471 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
472 {
473         NTSTATUS status;
474
475         if (tevent_req_is_nterror(req, &status)) {
476                 return status;
477         }
478         return NT_STATUS_OK;
479 }
480
481 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
482                         const char *oldname,
483                         const char *newname)
484 {
485         TALLOC_CTX *frame = talloc_stackframe();
486         struct event_context *ev = NULL;
487         struct tevent_req *req = NULL;
488         NTSTATUS status = NT_STATUS_OK;
489
490         if (cli_has_async_calls(cli)) {
491                 /*
492                  * Can't use sync call while an async call is in flight
493                  */
494                 status = NT_STATUS_INVALID_PARAMETER;
495                 goto fail;
496         }
497
498         ev = event_context_init(frame);
499         if (ev == NULL) {
500                 status = NT_STATUS_NO_MEMORY;
501                 goto fail;
502         }
503
504         req = cli_posix_hardlink_send(frame,
505                                 ev,
506                                 cli,
507                                 oldname,
508                                 newname);
509         if (req == NULL) {
510                 status = NT_STATUS_NO_MEMORY;
511                 goto fail;
512         }
513
514         if (!tevent_req_poll(req, ev)) {
515                 status = map_nt_error_from_unix(errno);
516                 goto fail;
517         }
518
519         status = cli_posix_hardlink_recv(req);
520
521  fail:
522         TALLOC_FREE(frame);
523         if (!NT_STATUS_IS_OK(status)) {
524                 cli_set_error(cli, status);
525         }
526         return status;
527 }
528
529 /****************************************************************************
530  Map standard UNIX permissions onto wire representations.
531 ****************************************************************************/
532
533 uint32_t unix_perms_to_wire(mode_t perms)
534 {
535         unsigned int ret = 0;
536
537         ret |= ((perms & S_IXOTH) ?  UNIX_X_OTH : 0);
538         ret |= ((perms & S_IWOTH) ?  UNIX_W_OTH : 0);
539         ret |= ((perms & S_IROTH) ?  UNIX_R_OTH : 0);
540         ret |= ((perms & S_IXGRP) ?  UNIX_X_GRP : 0);
541         ret |= ((perms & S_IWGRP) ?  UNIX_W_GRP : 0);
542         ret |= ((perms & S_IRGRP) ?  UNIX_R_GRP : 0);
543         ret |= ((perms & S_IXUSR) ?  UNIX_X_USR : 0);
544         ret |= ((perms & S_IWUSR) ?  UNIX_W_USR : 0);
545         ret |= ((perms & S_IRUSR) ?  UNIX_R_USR : 0);
546 #ifdef S_ISVTX
547         ret |= ((perms & S_ISVTX) ?  UNIX_STICKY : 0);
548 #endif
549 #ifdef S_ISGID
550         ret |= ((perms & S_ISGID) ?  UNIX_SET_GID : 0);
551 #endif
552 #ifdef S_ISUID
553         ret |= ((perms & S_ISUID) ?  UNIX_SET_UID : 0);
554 #endif
555         return ret;
556 }
557
558 /****************************************************************************
559  Map wire permissions to standard UNIX.
560 ****************************************************************************/
561
562 mode_t wire_perms_to_unix(uint32_t perms)
563 {
564         mode_t ret = (mode_t)0;
565
566         ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
567         ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
568         ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
569         ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
570         ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
571         ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
572         ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
573         ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
574         ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
575 #ifdef S_ISVTX
576         ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
577 #endif
578 #ifdef S_ISGID
579         ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
580 #endif
581 #ifdef S_ISUID
582         ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
583 #endif
584         return ret;
585 }
586
587 /****************************************************************************
588  Return the file type from the wire filetype for UNIX extensions.
589 ****************************************************************************/
590
591 static mode_t unix_filetype_from_wire(uint32_t wire_type)
592 {
593         switch (wire_type) {
594                 case UNIX_TYPE_FILE:
595                         return S_IFREG;
596                 case UNIX_TYPE_DIR:
597                         return S_IFDIR;
598 #ifdef S_IFLNK
599                 case UNIX_TYPE_SYMLINK:
600                         return S_IFLNK;
601 #endif
602 #ifdef S_IFCHR
603                 case UNIX_TYPE_CHARDEV:
604                         return S_IFCHR;
605 #endif
606 #ifdef S_IFBLK
607                 case UNIX_TYPE_BLKDEV:
608                         return S_IFBLK;
609 #endif
610 #ifdef S_IFIFO
611                 case UNIX_TYPE_FIFO:
612                         return S_IFIFO;
613 #endif
614 #ifdef S_IFSOCK
615                 case UNIX_TYPE_SOCKET:
616                         return S_IFSOCK;
617 #endif
618                 default:
619                         return (mode_t)0;
620         }
621 }
622
623 /****************************************************************************
624  Do a POSIX getfacl (UNIX extensions).
625 ****************************************************************************/
626
627 bool cli_unix_getfacl(struct cli_state *cli, const char *name, size_t *prb_size, char **retbuf)
628 {
629         unsigned int param_len = 0;
630         unsigned int data_len = 0;
631         uint16_t setup = TRANSACT2_QPATHINFO;
632         char *param;
633         size_t nlen = 2*(strlen(name)+1);
634         char *rparam=NULL, *rdata=NULL;
635         char *p;
636
637         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
638         if (!param) {
639                 return false;
640         }
641
642         p = param;
643         memset(p, '\0', 6);
644         SSVAL(p, 0, SMB_QUERY_POSIX_ACL);
645         p += 6;
646         p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
647         param_len = PTR_DIFF(p, param);
648
649         if (!cli_send_trans(cli, SMBtrans2,
650                 NULL,                        /* name */
651                 -1, 0,                       /* fid, flags */
652                 &setup, 1, 0,                /* setup, length, max */
653                 param, param_len, 2,         /* param, length, max */
654                 NULL,  0, cli->max_xmit      /* data, length, max */
655                 )) {
656                 SAFE_FREE(param);
657                 return false;
658         }
659
660         SAFE_FREE(param);
661
662         if (!cli_receive_trans(cli, SMBtrans2,
663                         &rparam, &param_len,
664                         &rdata, &data_len)) {
665                 return false;
666         }
667
668         if (data_len < 6) {
669                 SAFE_FREE(rdata);
670                 SAFE_FREE(rparam);
671                 return false;
672         }
673
674         SAFE_FREE(rparam);
675         *retbuf = rdata;
676         *prb_size = (size_t)data_len;
677
678         return true;
679 }
680
681 /****************************************************************************
682  Stat a file (UNIX extensions).
683 ****************************************************************************/
684
685 bool cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbuf)
686 {
687         unsigned int param_len = 0;
688         unsigned int data_len = 0;
689         uint16_t setup = TRANSACT2_QPATHINFO;
690         char *param;
691         size_t nlen = 2*(strlen(name)+1);
692         char *rparam=NULL, *rdata=NULL;
693         char *p;
694
695         ZERO_STRUCTP(sbuf);
696
697         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
698         if (!param) {
699                 return false;
700         }
701         p = param;
702         memset(p, '\0', 6);
703         SSVAL(p, 0, SMB_QUERY_FILE_UNIX_BASIC);
704         p += 6;
705         p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
706         param_len = PTR_DIFF(p, param);
707
708         if (!cli_send_trans(cli, SMBtrans2,
709                         NULL,                        /* name */
710                         -1, 0,                       /* fid, flags */
711                         &setup, 1, 0,                /* setup, length, max */
712                         param, param_len, 2,         /* param, length, max */
713                         NULL,  0, cli->max_xmit      /* data, length, max */
714                         )) {
715                 SAFE_FREE(param);
716                 return false;
717         }
718
719         SAFE_FREE(param);
720
721         if (!cli_receive_trans(cli, SMBtrans2,
722                         &rparam, &param_len,
723                         &rdata, &data_len)) {
724                 return false;
725         }
726
727         if (data_len < 96) {
728                 SAFE_FREE(rdata);
729                 SAFE_FREE(rparam);
730                 return false;
731         }
732
733         sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(rdata,0);     /* total size, in bytes */
734         sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8);   /* number of blocks allocated */
735 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
736         sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
737 #else
738         /* assume 512 byte blocks */
739         sbuf->st_ex_blocks /= 512;
740 #endif
741         sbuf->st_ex_ctime = interpret_long_date(rdata + 16);    /* time of last change */
742         sbuf->st_ex_atime = interpret_long_date(rdata + 24);    /* time of last access */
743         sbuf->st_ex_mtime = interpret_long_date(rdata + 32);    /* time of last modification */
744
745         sbuf->st_ex_uid = (uid_t) IVAL(rdata,40);      /* user ID of owner */
746         sbuf->st_ex_gid = (gid_t) IVAL(rdata,48);      /* group ID of owner */
747         sbuf->st_ex_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
748 #if defined(HAVE_MAKEDEV)
749         {
750                 uint32_t dev_major = IVAL(rdata,60);
751                 uint32_t dev_minor = IVAL(rdata,68);
752                 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
753         }
754 #endif
755         sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76);      /* inode */
756         sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(rdata,84));     /* protection */
757         sbuf->st_ex_nlink = IVAL(rdata,92);    /* number of hard links */
758
759         SAFE_FREE(rdata);
760         SAFE_FREE(rparam);
761
762         return true;
763 }
764 /****************************************************************************
765  Chmod or chown a file internal (UNIX extensions).
766 ****************************************************************************/
767
768 static bool cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32_t mode, uint32_t uid, uint32_t gid)
769 {
770         unsigned int data_len = 0;
771         unsigned int param_len = 0;
772         uint16_t setup = TRANSACT2_SETPATHINFO;
773         size_t nlen = 2*(strlen(fname)+1);
774         char *param;
775         char data[100];
776         char *rparam=NULL, *rdata=NULL;
777         char *p;
778
779         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
780         if (!param) {
781                 return false;
782         }
783         memset(param, '\0', 6);
784         memset(data, 0, sizeof(data));
785
786         SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
787         p = &param[6];
788
789         p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
790         param_len = PTR_DIFF(p, param);
791
792         memset(data, 0xff, 40); /* Set all sizes/times to no change. */
793
794         SIVAL(data,40,uid);
795         SIVAL(data,48,gid);
796         SIVAL(data,84,mode);
797
798         data_len = 100;
799
800         if (!cli_send_trans(cli, SMBtrans2,
801                         NULL,                        /* name */
802                         -1, 0,                          /* fid, flags */
803                         &setup, 1, 0,                   /* setup, length, max */
804                         param, param_len, 2,            /* param, length, max */
805                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
806                         )) {
807                 SAFE_FREE(param);
808                 return False;
809         }
810
811         SAFE_FREE(param);
812
813         if (!cli_receive_trans(cli, SMBtrans2,
814                         &rparam, &param_len,
815                         &rdata, &data_len)) {
816                 return false;
817         }
818
819         SAFE_FREE(rdata);
820         SAFE_FREE(rparam);
821
822         return true;
823 }
824
825 /****************************************************************************
826  chmod a file (UNIX extensions).
827 ****************************************************************************/
828
829 bool cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
830 {
831         return cli_unix_chmod_chown_internal(cli, fname,
832                 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
833 }
834
835 /****************************************************************************
836  chown a file (UNIX extensions).
837 ****************************************************************************/
838
839 bool cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
840 {
841         return cli_unix_chmod_chown_internal(cli, fname,
842                         SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
843 }
844
845 /****************************************************************************
846  Rename a file.
847 ****************************************************************************/
848
849 static void cli_rename_done(struct tevent_req *subreq);
850
851 struct cli_rename_state {
852         uint16_t vwv[1];
853 };
854
855 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
856                                 struct event_context *ev,
857                                 struct cli_state *cli,
858                                 const char *fname_src,
859                                 const char *fname_dst)
860 {
861         struct tevent_req *req = NULL, *subreq = NULL;
862         struct cli_rename_state *state = NULL;
863         uint8_t additional_flags = 0;
864         uint8_t *bytes = NULL;
865
866         req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
867         if (req == NULL) {
868                 return NULL;
869         }
870
871         SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
872
873         bytes = talloc_array(state, uint8_t, 1);
874         if (tevent_req_nomem(bytes, req)) {
875                 return tevent_req_post(req, ev);
876         }
877         bytes[0] = 4;
878         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
879                                    strlen(fname_src)+1, NULL);
880         if (tevent_req_nomem(bytes, req)) {
881                 return tevent_req_post(req, ev);
882         }
883
884         bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
885                         talloc_get_size(bytes)+1);
886         if (tevent_req_nomem(bytes, req)) {
887                 return tevent_req_post(req, ev);
888         }
889
890         bytes[talloc_get_size(bytes)-1] = 4;
891         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
892                                    strlen(fname_dst)+1, NULL);
893         if (tevent_req_nomem(bytes, req)) {
894                 return tevent_req_post(req, ev);
895         }
896
897         subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
898                               1, state->vwv, talloc_get_size(bytes), bytes);
899         if (tevent_req_nomem(subreq, req)) {
900                 return tevent_req_post(req, ev);
901         }
902         tevent_req_set_callback(subreq, cli_rename_done, req);
903         return req;
904 }
905
906 static void cli_rename_done(struct tevent_req *subreq)
907 {
908         struct tevent_req *req = tevent_req_callback_data(
909                                 subreq, struct tevent_req);
910         NTSTATUS status;
911
912         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
913         TALLOC_FREE(subreq);
914         if (!NT_STATUS_IS_OK(status)) {
915                 tevent_req_nterror(req, status);
916                 return;
917         }
918         tevent_req_done(req);
919 }
920
921 NTSTATUS cli_rename_recv(struct tevent_req *req)
922 {
923         return tevent_req_simple_recv_ntstatus(req);
924 }
925
926 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
927 {
928         TALLOC_CTX *frame = talloc_stackframe();
929         struct event_context *ev;
930         struct tevent_req *req;
931         NTSTATUS status = NT_STATUS_OK;
932
933         if (cli_has_async_calls(cli)) {
934                 /*
935                  * Can't use sync call while an async call is in flight
936                  */
937                 status = NT_STATUS_INVALID_PARAMETER;
938                 goto fail;
939         }
940
941         ev = event_context_init(frame);
942         if (ev == NULL) {
943                 status = NT_STATUS_NO_MEMORY;
944                 goto fail;
945         }
946
947         req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
948         if (req == NULL) {
949                 status = NT_STATUS_NO_MEMORY;
950                 goto fail;
951         }
952
953         if (!tevent_req_poll(req, ev)) {
954                 status = map_nt_error_from_unix(errno);
955                 goto fail;
956         }
957
958         status = cli_rename_recv(req);
959
960  fail:
961         TALLOC_FREE(frame);
962         if (!NT_STATUS_IS_OK(status)) {
963                 cli_set_error(cli, status);
964         }
965         return status;
966 }
967
968 /****************************************************************************
969  NT Rename a file.
970 ****************************************************************************/
971
972 static void cli_ntrename_internal_done(struct tevent_req *subreq);
973
974 struct cli_ntrename_internal_state {
975         uint16_t vwv[4];
976 };
977
978 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
979                                 struct event_context *ev,
980                                 struct cli_state *cli,
981                                 const char *fname_src,
982                                 const char *fname_dst,
983                                 uint16_t rename_flag)
984 {
985         struct tevent_req *req = NULL, *subreq = NULL;
986         struct cli_ntrename_internal_state *state = NULL;
987         uint8_t additional_flags = 0;
988         uint8_t *bytes = NULL;
989
990         req = tevent_req_create(mem_ctx, &state,
991                                 struct cli_ntrename_internal_state);
992         if (req == NULL) {
993                 return NULL;
994         }
995
996         SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
997         SSVAL(state->vwv+1, 0, rename_flag);
998
999         bytes = talloc_array(state, uint8_t, 1);
1000         if (tevent_req_nomem(bytes, req)) {
1001                 return tevent_req_post(req, ev);
1002         }
1003         bytes[0] = 4;
1004         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1005                                    strlen(fname_src)+1, NULL);
1006         if (tevent_req_nomem(bytes, req)) {
1007                 return tevent_req_post(req, ev);
1008         }
1009
1010         bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1011                         talloc_get_size(bytes)+1);
1012         if (tevent_req_nomem(bytes, req)) {
1013                 return tevent_req_post(req, ev);
1014         }
1015
1016         bytes[talloc_get_size(bytes)-1] = 4;
1017         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1018                                    strlen(fname_dst)+1, NULL);
1019         if (tevent_req_nomem(bytes, req)) {
1020                 return tevent_req_post(req, ev);
1021         }
1022
1023         subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1024                               4, state->vwv, talloc_get_size(bytes), bytes);
1025         if (tevent_req_nomem(subreq, req)) {
1026                 return tevent_req_post(req, ev);
1027         }
1028         tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1029         return req;
1030 }
1031
1032 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1033 {
1034         struct tevent_req *req = tevent_req_callback_data(
1035                                 subreq, struct tevent_req);
1036         NTSTATUS status;
1037
1038         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1039         TALLOC_FREE(subreq);
1040         if (!NT_STATUS_IS_OK(status)) {
1041                 tevent_req_nterror(req, status);
1042                 return;
1043         }
1044         tevent_req_done(req);
1045 }
1046
1047 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1048 {
1049         return tevent_req_simple_recv_ntstatus(req);
1050 }
1051
1052 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1053                                 struct event_context *ev,
1054                                 struct cli_state *cli,
1055                                 const char *fname_src,
1056                                 const char *fname_dst)
1057 {
1058         return cli_ntrename_internal_send(mem_ctx,
1059                                           ev,
1060                                           cli,
1061                                           fname_src,
1062                                           fname_dst,
1063                                           RENAME_FLAG_RENAME);
1064 }
1065
1066 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1067 {
1068         return cli_ntrename_internal_recv(req);
1069 }
1070
1071 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1072 {
1073         TALLOC_CTX *frame = talloc_stackframe();
1074         struct event_context *ev;
1075         struct tevent_req *req;
1076         NTSTATUS status = NT_STATUS_OK;
1077
1078         if (cli_has_async_calls(cli)) {
1079                 /*
1080                  * Can't use sync call while an async call is in flight
1081                  */
1082                 status = NT_STATUS_INVALID_PARAMETER;
1083                 goto fail;
1084         }
1085
1086         ev = event_context_init(frame);
1087         if (ev == NULL) {
1088                 status = NT_STATUS_NO_MEMORY;
1089                 goto fail;
1090         }
1091
1092         req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1093         if (req == NULL) {
1094                 status = NT_STATUS_NO_MEMORY;
1095                 goto fail;
1096         }
1097
1098         if (!tevent_req_poll(req, ev)) {
1099                 status = map_nt_error_from_unix(errno);
1100                 goto fail;
1101         }
1102
1103         status = cli_ntrename_recv(req);
1104
1105  fail:
1106         TALLOC_FREE(frame);
1107         if (!NT_STATUS_IS_OK(status)) {
1108                 cli_set_error(cli, status);
1109         }
1110         return status;
1111 }
1112
1113 /****************************************************************************
1114  NT hardlink a file.
1115 ****************************************************************************/
1116
1117 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1118                                 struct event_context *ev,
1119                                 struct cli_state *cli,
1120                                 const char *fname_src,
1121                                 const char *fname_dst)
1122 {
1123         return cli_ntrename_internal_send(mem_ctx,
1124                                           ev,
1125                                           cli,
1126                                           fname_src,
1127                                           fname_dst,
1128                                           RENAME_FLAG_HARD_LINK);
1129 }
1130
1131 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1132 {
1133         return cli_ntrename_internal_recv(req);
1134 }
1135
1136 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1137 {
1138         TALLOC_CTX *frame = talloc_stackframe();
1139         struct event_context *ev;
1140         struct tevent_req *req;
1141         NTSTATUS status = NT_STATUS_OK;
1142
1143         if (cli_has_async_calls(cli)) {
1144                 /*
1145                  * Can't use sync call while an async call is in flight
1146                  */
1147                 status = NT_STATUS_INVALID_PARAMETER;
1148                 goto fail;
1149         }
1150
1151         ev = event_context_init(frame);
1152         if (ev == NULL) {
1153                 status = NT_STATUS_NO_MEMORY;
1154                 goto fail;
1155         }
1156
1157         req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1158         if (req == NULL) {
1159                 status = NT_STATUS_NO_MEMORY;
1160                 goto fail;
1161         }
1162
1163         if (!tevent_req_poll(req, ev)) {
1164                 status = map_nt_error_from_unix(errno);
1165                 goto fail;
1166         }
1167
1168         status = cli_nt_hardlink_recv(req);
1169
1170  fail:
1171         TALLOC_FREE(frame);
1172         if (!NT_STATUS_IS_OK(status)) {
1173                 cli_set_error(cli, status);
1174         }
1175         return status;
1176 }
1177
1178 /****************************************************************************
1179  Delete a file.
1180 ****************************************************************************/
1181
1182 static void cli_unlink_done(struct tevent_req *subreq);
1183
1184 struct cli_unlink_state {
1185         uint16_t vwv[1];
1186 };
1187
1188 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1189                                 struct event_context *ev,
1190                                 struct cli_state *cli,
1191                                 const char *fname,
1192                                 uint16_t mayhave_attrs)
1193 {
1194         struct tevent_req *req = NULL, *subreq = NULL;
1195         struct cli_unlink_state *state = NULL;
1196         uint8_t additional_flags = 0;
1197         uint8_t *bytes = NULL;
1198
1199         req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1200         if (req == NULL) {
1201                 return NULL;
1202         }
1203
1204         SSVAL(state->vwv+0, 0, mayhave_attrs);
1205
1206         bytes = talloc_array(state, uint8_t, 1);
1207         if (tevent_req_nomem(bytes, req)) {
1208                 return tevent_req_post(req, ev);
1209         }
1210         bytes[0] = 4;
1211         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1212                                    strlen(fname)+1, NULL);
1213
1214         if (tevent_req_nomem(bytes, req)) {
1215                 return tevent_req_post(req, ev);
1216         }
1217
1218         subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1219                                 1, state->vwv, talloc_get_size(bytes), bytes);
1220         if (tevent_req_nomem(subreq, req)) {
1221                 return tevent_req_post(req, ev);
1222         }
1223         tevent_req_set_callback(subreq, cli_unlink_done, req);
1224         return req;
1225 }
1226
1227 static void cli_unlink_done(struct tevent_req *subreq)
1228 {
1229         struct tevent_req *req = tevent_req_callback_data(
1230                 subreq, struct tevent_req);
1231         NTSTATUS status;
1232
1233         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1234         TALLOC_FREE(subreq);
1235         if (!NT_STATUS_IS_OK(status)) {
1236                 tevent_req_nterror(req, status);
1237                 return;
1238         }
1239         tevent_req_done(req);
1240 }
1241
1242 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1243 {
1244         return tevent_req_simple_recv_ntstatus(req);
1245 }
1246
1247 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1248 {
1249         TALLOC_CTX *frame = talloc_stackframe();
1250         struct event_context *ev;
1251         struct tevent_req *req;
1252         NTSTATUS status = NT_STATUS_OK;
1253
1254         if (cli_has_async_calls(cli)) {
1255                 /*
1256                  * Can't use sync call while an async call is in flight
1257                  */
1258                 status = NT_STATUS_INVALID_PARAMETER;
1259                 goto fail;
1260         }
1261
1262         ev = event_context_init(frame);
1263         if (ev == NULL) {
1264                 status = NT_STATUS_NO_MEMORY;
1265                 goto fail;
1266         }
1267
1268         req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1269         if (req == NULL) {
1270                 status = NT_STATUS_NO_MEMORY;
1271                 goto fail;
1272         }
1273
1274         if (!tevent_req_poll(req, ev)) {
1275                 status = map_nt_error_from_unix(errno);
1276                 goto fail;
1277         }
1278
1279         status = cli_unlink_recv(req);
1280
1281  fail:
1282         TALLOC_FREE(frame);
1283         if (!NT_STATUS_IS_OK(status)) {
1284                 cli_set_error(cli, status);
1285         }
1286         return status;
1287 }
1288
1289 /****************************************************************************
1290  Create a directory.
1291 ****************************************************************************/
1292
1293 static void cli_mkdir_done(struct tevent_req *subreq);
1294
1295 struct cli_mkdir_state {
1296         int dummy;
1297 };
1298
1299 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1300                                   struct event_context *ev,
1301                                   struct cli_state *cli,
1302                                   const char *dname)
1303 {
1304         struct tevent_req *req = NULL, *subreq = NULL;
1305         struct cli_mkdir_state *state = NULL;
1306         uint8_t additional_flags = 0;
1307         uint8_t *bytes = NULL;
1308
1309         req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1310         if (req == NULL) {
1311                 return NULL;
1312         }
1313
1314         bytes = talloc_array(state, uint8_t, 1);
1315         if (tevent_req_nomem(bytes, req)) {
1316                 return tevent_req_post(req, ev);
1317         }
1318         bytes[0] = 4;
1319         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1320                                    strlen(dname)+1, NULL);
1321
1322         if (tevent_req_nomem(bytes, req)) {
1323                 return tevent_req_post(req, ev);
1324         }
1325
1326         subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1327                               0, NULL, talloc_get_size(bytes), bytes);
1328         if (tevent_req_nomem(subreq, req)) {
1329                 return tevent_req_post(req, ev);
1330         }
1331         tevent_req_set_callback(subreq, cli_mkdir_done, req);
1332         return req;
1333 }
1334
1335 static void cli_mkdir_done(struct tevent_req *subreq)
1336 {
1337         struct tevent_req *req = tevent_req_callback_data(
1338                 subreq, struct tevent_req);
1339         NTSTATUS status;
1340
1341         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1342         TALLOC_FREE(subreq);
1343         if (!NT_STATUS_IS_OK(status)) {
1344                 tevent_req_nterror(req, status);
1345                 return;
1346         }
1347         tevent_req_done(req);
1348 }
1349
1350 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1351 {
1352         return tevent_req_simple_recv_ntstatus(req);
1353 }
1354
1355 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1356 {
1357         TALLOC_CTX *frame = talloc_stackframe();
1358         struct event_context *ev;
1359         struct tevent_req *req;
1360         NTSTATUS status = NT_STATUS_OK;
1361
1362         if (cli_has_async_calls(cli)) {
1363                 /*
1364                  * Can't use sync call while an async call is in flight
1365                  */
1366                 status = NT_STATUS_INVALID_PARAMETER;
1367                 goto fail;
1368         }
1369
1370         ev = event_context_init(frame);
1371         if (ev == NULL) {
1372                 status = NT_STATUS_NO_MEMORY;
1373                 goto fail;
1374         }
1375
1376         req = cli_mkdir_send(frame, ev, cli, dname);
1377         if (req == NULL) {
1378                 status = NT_STATUS_NO_MEMORY;
1379                 goto fail;
1380         }
1381
1382         if (!tevent_req_poll(req, ev)) {
1383                 status = map_nt_error_from_unix(errno);
1384                 goto fail;
1385         }
1386
1387         status = cli_mkdir_recv(req);
1388
1389  fail:
1390         TALLOC_FREE(frame);
1391         if (!NT_STATUS_IS_OK(status)) {
1392                 cli_set_error(cli, status);
1393         }
1394         return status;
1395 }
1396
1397 /****************************************************************************
1398  Remove a directory.
1399 ****************************************************************************/
1400
1401 static void cli_rmdir_done(struct tevent_req *subreq);
1402
1403 struct cli_rmdir_state {
1404         int dummy;
1405 };
1406
1407 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1408                                   struct event_context *ev,
1409                                   struct cli_state *cli,
1410                                   const char *dname)
1411 {
1412         struct tevent_req *req = NULL, *subreq = NULL;
1413         struct cli_rmdir_state *state = NULL;
1414         uint8_t additional_flags = 0;
1415         uint8_t *bytes = NULL;
1416
1417         req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1418         if (req == NULL) {
1419                 return NULL;
1420         }
1421
1422         bytes = talloc_array(state, uint8_t, 1);
1423         if (tevent_req_nomem(bytes, req)) {
1424                 return tevent_req_post(req, ev);
1425         }
1426         bytes[0] = 4;
1427         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1428                                    strlen(dname)+1, NULL);
1429
1430         if (tevent_req_nomem(bytes, req)) {
1431                 return tevent_req_post(req, ev);
1432         }
1433
1434         subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1435                               0, NULL, talloc_get_size(bytes), bytes);
1436         if (tevent_req_nomem(subreq, req)) {
1437                 return tevent_req_post(req, ev);
1438         }
1439         tevent_req_set_callback(subreq, cli_rmdir_done, req);
1440         return req;
1441 }
1442
1443 static void cli_rmdir_done(struct tevent_req *subreq)
1444 {
1445         struct tevent_req *req = tevent_req_callback_data(
1446                 subreq, struct tevent_req);
1447         NTSTATUS status;
1448
1449         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1450         TALLOC_FREE(subreq);
1451         if (!NT_STATUS_IS_OK(status)) {
1452                 tevent_req_nterror(req, status);
1453                 return;
1454         }
1455         tevent_req_done(req);
1456 }
1457
1458 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1459 {
1460         return tevent_req_simple_recv_ntstatus(req);
1461 }
1462
1463 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1464 {
1465         TALLOC_CTX *frame = talloc_stackframe();
1466         struct event_context *ev;
1467         struct tevent_req *req;
1468         NTSTATUS status = NT_STATUS_OK;
1469
1470         if (cli_has_async_calls(cli)) {
1471                 /*
1472                  * Can't use sync call while an async call is in flight
1473                  */
1474                 status = NT_STATUS_INVALID_PARAMETER;
1475                 goto fail;
1476         }
1477
1478         ev = event_context_init(frame);
1479         if (ev == NULL) {
1480                 status = NT_STATUS_NO_MEMORY;
1481                 goto fail;
1482         }
1483
1484         req = cli_rmdir_send(frame, ev, cli, dname);
1485         if (req == NULL) {
1486                 status = NT_STATUS_NO_MEMORY;
1487                 goto fail;
1488         }
1489
1490         if (!tevent_req_poll(req, ev)) {
1491                 status = map_nt_error_from_unix(errno);
1492                 goto fail;
1493         }
1494
1495         status = cli_rmdir_recv(req);
1496
1497  fail:
1498         TALLOC_FREE(frame);
1499         if (!NT_STATUS_IS_OK(status)) {
1500                 cli_set_error(cli, status);
1501         }
1502         return status;
1503 }
1504
1505 /****************************************************************************
1506  Set or clear the delete on close flag.
1507 ****************************************************************************/
1508
1509 int cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1510 {
1511         unsigned int data_len = 1;
1512         unsigned int param_len = 6;
1513         uint16_t setup = TRANSACT2_SETFILEINFO;
1514         char param[6];
1515         unsigned char data;
1516         char *rparam=NULL, *rdata=NULL;
1517
1518         memset(param, 0, param_len);
1519         SSVAL(param,0,fnum);
1520         SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
1521
1522         data = flag ? 1 : 0;
1523
1524         if (!cli_send_trans(cli, SMBtrans2,
1525                         NULL,                        /* name */
1526                         -1, 0,                          /* fid, flags */
1527                         &setup, 1, 0,                   /* setup, length, max */
1528                         param, param_len, 2,            /* param, length, max */
1529                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
1530                         )) {
1531                 return false;
1532         }
1533
1534         if (!cli_receive_trans(cli, SMBtrans2,
1535                         &rparam, &param_len,
1536                         &rdata, &data_len)) {
1537                 return false;
1538         }
1539
1540         SAFE_FREE(rdata);
1541         SAFE_FREE(rparam);
1542
1543         return true;
1544 }
1545
1546 struct cli_ntcreate_state {
1547         uint16_t vwv[24];
1548         uint16_t fnum;
1549 };
1550
1551 static void cli_ntcreate_done(struct tevent_req *subreq);
1552
1553 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1554                                      struct event_context *ev,
1555                                      struct cli_state *cli,
1556                                      const char *fname,
1557                                      uint32_t CreatFlags,
1558                                      uint32_t DesiredAccess,
1559                                      uint32_t FileAttributes,
1560                                      uint32_t ShareAccess,
1561                                      uint32_t CreateDisposition,
1562                                      uint32_t CreateOptions,
1563                                      uint8_t SecurityFlags)
1564 {
1565         struct tevent_req *req, *subreq;
1566         struct cli_ntcreate_state *state;
1567         uint16_t *vwv;
1568         uint8_t *bytes;
1569         size_t converted_len;
1570
1571         req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1572         if (req == NULL) {
1573                 return NULL;
1574         }
1575         vwv = state->vwv;
1576
1577         SCVAL(vwv+0, 0, 0xFF);
1578         SCVAL(vwv+0, 1, 0);
1579         SSVAL(vwv+1, 0, 0);
1580         SCVAL(vwv+2, 0, 0);
1581
1582         if (cli->use_oplocks) {
1583                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1584         }
1585         SIVAL(vwv+3, 1, CreatFlags);
1586         SIVAL(vwv+5, 1, 0x0);   /* RootDirectoryFid */
1587         SIVAL(vwv+7, 1, DesiredAccess);
1588         SIVAL(vwv+9, 1, 0x0);   /* AllocationSize */
1589         SIVAL(vwv+11, 1, 0x0);  /* AllocationSize */
1590         SIVAL(vwv+13, 1, FileAttributes);
1591         SIVAL(vwv+15, 1, ShareAccess);
1592         SIVAL(vwv+17, 1, CreateDisposition);
1593         SIVAL(vwv+19, 1, CreateOptions);
1594         SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1595         SCVAL(vwv+23, 1, SecurityFlags);
1596
1597         bytes = talloc_array(state, uint8_t, 0);
1598         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1599                                    fname, strlen(fname)+1,
1600                                    &converted_len);
1601
1602         /* sigh. this copes with broken netapp filer behaviour */
1603         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1604
1605         if (tevent_req_nomem(bytes, req)) {
1606                 return tevent_req_post(req, ev);
1607         }
1608
1609         SIVAL(vwv+2, 1, converted_len);
1610
1611         subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1612                               talloc_get_size(bytes), bytes);
1613         if (tevent_req_nomem(subreq, req)) {
1614                 return tevent_req_post(req, ev);
1615         }
1616         tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1617         return req;
1618 }
1619
1620 static void cli_ntcreate_done(struct tevent_req *subreq)
1621 {
1622         struct tevent_req *req = tevent_req_callback_data(
1623                 subreq, struct tevent_req);
1624         struct cli_ntcreate_state *state = tevent_req_data(
1625                 req, struct cli_ntcreate_state);
1626         uint8_t wct;
1627         uint16_t *vwv;
1628         uint32_t num_bytes;
1629         uint8_t *bytes;
1630         NTSTATUS status;
1631
1632         status = cli_smb_recv(subreq, 3, &wct, &vwv, &num_bytes, &bytes);
1633         if (!NT_STATUS_IS_OK(status)) {
1634                 TALLOC_FREE(subreq);
1635                 tevent_req_nterror(req, status);
1636                 return;
1637         }
1638         state->fnum = SVAL(vwv+2, 1);
1639         tevent_req_done(req);
1640 }
1641
1642 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1643 {
1644         struct cli_ntcreate_state *state = tevent_req_data(
1645                 req, struct cli_ntcreate_state);
1646         NTSTATUS status;
1647
1648         if (tevent_req_is_nterror(req, &status)) {
1649                 return status;
1650         }
1651         *pfnum = state->fnum;
1652         return NT_STATUS_OK;
1653 }
1654
1655 NTSTATUS cli_ntcreate(struct cli_state *cli,
1656                       const char *fname,
1657                       uint32_t CreatFlags,
1658                       uint32_t DesiredAccess,
1659                       uint32_t FileAttributes,
1660                       uint32_t ShareAccess,
1661                       uint32_t CreateDisposition,
1662                       uint32_t CreateOptions,
1663                       uint8_t SecurityFlags,
1664                       uint16_t *pfid)
1665 {
1666         TALLOC_CTX *frame = talloc_stackframe();
1667         struct event_context *ev;
1668         struct tevent_req *req;
1669         NTSTATUS status = NT_STATUS_OK;
1670
1671         if (cli_has_async_calls(cli)) {
1672                 /*
1673                  * Can't use sync call while an async call is in flight
1674                  */
1675                 status = NT_STATUS_INVALID_PARAMETER;
1676                 goto fail;
1677         }
1678
1679         ev = event_context_init(frame);
1680         if (ev == NULL) {
1681                 status = NT_STATUS_NO_MEMORY;
1682                 goto fail;
1683         }
1684
1685         req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1686                                 DesiredAccess, FileAttributes, ShareAccess,
1687                                 CreateDisposition, CreateOptions,
1688                                 SecurityFlags);
1689         if (req == NULL) {
1690                 status = NT_STATUS_NO_MEMORY;
1691                 goto fail;
1692         }
1693
1694         if (!tevent_req_poll(req, ev)) {
1695                 status = map_nt_error_from_unix(errno);
1696                 goto fail;
1697         }
1698
1699         status = cli_ntcreate_recv(req, pfid);
1700  fail:
1701         TALLOC_FREE(frame);
1702         if (!NT_STATUS_IS_OK(status)) {
1703                 cli_set_error(cli, status);
1704         }
1705         return status;
1706 }
1707
1708 /****************************************************************************
1709  Open a file
1710  WARNING: if you open with O_WRONLY then getattrE won't work!
1711 ****************************************************************************/
1712
1713 struct cli_open_state {
1714         uint16_t vwv[15];
1715         uint16_t fnum;
1716         struct iovec bytes;
1717 };
1718
1719 static void cli_open_done(struct tevent_req *subreq);
1720
1721 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
1722                                    struct event_context *ev,
1723                                    struct cli_state *cli, const char *fname,
1724                                    int flags, int share_mode,
1725                                    struct tevent_req **psmbreq)
1726 {
1727         struct tevent_req *req, *subreq;
1728         struct cli_open_state *state;
1729         unsigned openfn;
1730         unsigned accessmode;
1731         uint8_t additional_flags;
1732         uint8_t *bytes;
1733
1734         req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
1735         if (req == NULL) {
1736                 return NULL;
1737         }
1738
1739         openfn = 0;
1740         if (flags & O_CREAT) {
1741                 openfn |= (1<<4);
1742         }
1743         if (!(flags & O_EXCL)) {
1744                 if (flags & O_TRUNC)
1745                         openfn |= (1<<1);
1746                 else
1747                         openfn |= (1<<0);
1748         }
1749
1750         accessmode = (share_mode<<4);
1751
1752         if ((flags & O_ACCMODE) == O_RDWR) {
1753                 accessmode |= 2;
1754         } else if ((flags & O_ACCMODE) == O_WRONLY) {
1755                 accessmode |= 1;
1756         }
1757
1758 #if defined(O_SYNC)
1759         if ((flags & O_SYNC) == O_SYNC) {
1760                 accessmode |= (1<<14);
1761         }
1762 #endif /* O_SYNC */
1763
1764         if (share_mode == DENY_FCB) {
1765                 accessmode = 0xFF;
1766         }
1767
1768         SCVAL(state->vwv + 0, 0, 0xFF);
1769         SCVAL(state->vwv + 0, 1, 0);
1770         SSVAL(state->vwv + 1, 0, 0);
1771         SSVAL(state->vwv + 2, 0, 0);  /* no additional info */
1772         SSVAL(state->vwv + 3, 0, accessmode);
1773         SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
1774         SSVAL(state->vwv + 5, 0, 0);
1775         SIVAL(state->vwv + 6, 0, 0);
1776         SSVAL(state->vwv + 8, 0, openfn);
1777         SIVAL(state->vwv + 9, 0, 0);
1778         SIVAL(state->vwv + 11, 0, 0);
1779         SIVAL(state->vwv + 13, 0, 0);
1780
1781         additional_flags = 0;
1782
1783         if (cli->use_oplocks) {
1784                 /* if using oplocks then ask for a batch oplock via
1785                    core and extended methods */
1786                 additional_flags =
1787                         FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1788                 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
1789         }
1790
1791         bytes = talloc_array(state, uint8_t, 0);
1792         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1793                                    strlen(fname)+1, NULL);
1794
1795         if (tevent_req_nomem(bytes, req)) {
1796                 return tevent_req_post(req, ev);
1797         }
1798
1799         state->bytes.iov_base = (void *)bytes;
1800         state->bytes.iov_len = talloc_get_size(bytes);
1801
1802         subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
1803                                     15, state->vwv, 1, &state->bytes);
1804         if (subreq == NULL) {
1805                 TALLOC_FREE(req);
1806                 return NULL;
1807         }
1808         tevent_req_set_callback(subreq, cli_open_done, req);
1809         *psmbreq = subreq;
1810         return req;
1811 }
1812
1813 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1814                                  struct cli_state *cli, const char *fname,
1815                                  int flags, int share_mode)
1816 {
1817         struct tevent_req *req, *subreq;
1818         NTSTATUS status;
1819
1820         req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
1821                               &subreq);
1822         if (req == NULL) {
1823                 return NULL;
1824         }
1825
1826         status = cli_smb_req_send(subreq);
1827         if (!NT_STATUS_IS_OK(status)) {
1828                 tevent_req_nterror(req, status);
1829                 return tevent_req_post(req, ev);
1830         }
1831         return req;
1832 }
1833
1834 static void cli_open_done(struct tevent_req *subreq)
1835 {
1836         struct tevent_req *req = tevent_req_callback_data(
1837                 subreq, struct tevent_req);
1838         struct cli_open_state *state = tevent_req_data(
1839                 req, struct cli_open_state);
1840         uint8_t wct;
1841         uint16_t *vwv;
1842         NTSTATUS status;
1843
1844         status = cli_smb_recv(subreq, 3, &wct, &vwv, NULL, NULL);
1845         if (!NT_STATUS_IS_OK(status)) {
1846                 TALLOC_FREE(subreq);
1847                 tevent_req_nterror(req, status);
1848                 return;
1849         }
1850         state->fnum = SVAL(vwv+2, 0);
1851         tevent_req_done(req);
1852 }
1853
1854 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
1855 {
1856         struct cli_open_state *state = tevent_req_data(
1857                 req, struct cli_open_state);
1858         NTSTATUS status;
1859
1860         if (tevent_req_is_nterror(req, &status)) {
1861                 return status;
1862         }
1863         *pfnum = state->fnum;
1864         return NT_STATUS_OK;
1865 }
1866
1867 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
1868              int share_mode, uint16_t *pfnum)
1869 {
1870         TALLOC_CTX *frame = talloc_stackframe();
1871         struct event_context *ev;
1872         struct tevent_req *req;
1873         NTSTATUS status = NT_STATUS_OK;
1874
1875         if (cli_has_async_calls(cli)) {
1876                 /*
1877                  * Can't use sync call while an async call is in flight
1878                  */
1879                 status = NT_STATUS_INVALID_PARAMETER;
1880                 goto fail;
1881         }
1882
1883         ev = event_context_init(frame);
1884         if (ev == NULL) {
1885                 status = NT_STATUS_NO_MEMORY;
1886                 goto fail;
1887         }
1888
1889         req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
1890         if (req == NULL) {
1891                 status = NT_STATUS_NO_MEMORY;
1892                 goto fail;
1893         }
1894
1895         if (!tevent_req_poll(req, ev)) {
1896                 status = map_nt_error_from_unix(errno);
1897                 goto fail;
1898         }
1899
1900         status = cli_open_recv(req, pfnum);
1901  fail:
1902         TALLOC_FREE(frame);
1903         if (!NT_STATUS_IS_OK(status)) {
1904                 cli_set_error(cli, status);
1905         }
1906         return status;
1907 }
1908
1909 /****************************************************************************
1910  Close a file.
1911 ****************************************************************************/
1912
1913 struct cli_close_state {
1914         uint16_t vwv[3];
1915 };
1916
1917 static void cli_close_done(struct tevent_req *subreq);
1918
1919 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
1920                                 struct event_context *ev,
1921                                 struct cli_state *cli,
1922                                 uint16_t fnum,
1923                                 struct tevent_req **psubreq)
1924 {
1925         struct tevent_req *req, *subreq;
1926         struct cli_close_state *state;
1927
1928         req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
1929         if (req == NULL) {
1930                 return NULL;
1931         }
1932         SSVAL(state->vwv+0, 0, fnum);
1933         SIVALS(state->vwv+1, 0, -1);
1934
1935         subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
1936                                     0, NULL);
1937         if (subreq == NULL) {
1938                 TALLOC_FREE(req);
1939                 return NULL;
1940         }
1941         tevent_req_set_callback(subreq, cli_close_done, req);
1942         *psubreq = subreq;
1943         return req;
1944 }
1945
1946 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
1947                                 struct event_context *ev,
1948                                 struct cli_state *cli,
1949                                 uint16_t fnum)
1950 {
1951         struct tevent_req *req, *subreq;
1952         NTSTATUS status;
1953
1954         req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
1955         if (req == NULL) {
1956                 return NULL;
1957         }
1958
1959         status = cli_smb_req_send(subreq);
1960         if (!NT_STATUS_IS_OK(status)) {
1961                 tevent_req_nterror(req, status);
1962                 return tevent_req_post(req, ev);
1963         }
1964         return req;
1965 }
1966
1967 static void cli_close_done(struct tevent_req *subreq)
1968 {
1969         struct tevent_req *req = tevent_req_callback_data(
1970                 subreq, struct tevent_req);
1971         NTSTATUS status;
1972
1973         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1974         TALLOC_FREE(subreq);
1975         if (!NT_STATUS_IS_OK(status)) {
1976                 tevent_req_nterror(req, status);
1977                 return;
1978         }
1979         tevent_req_done(req);
1980 }
1981
1982 NTSTATUS cli_close_recv(struct tevent_req *req)
1983 {
1984         return tevent_req_simple_recv_ntstatus(req);
1985 }
1986
1987 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
1988 {
1989         TALLOC_CTX *frame = talloc_stackframe();
1990         struct event_context *ev;
1991         struct tevent_req *req;
1992         NTSTATUS status = NT_STATUS_OK;
1993
1994         if (cli_has_async_calls(cli)) {
1995                 /*
1996                  * Can't use sync call while an async call is in flight
1997                  */
1998                 status = NT_STATUS_INVALID_PARAMETER;
1999                 goto fail;
2000         }
2001
2002         ev = event_context_init(frame);
2003         if (ev == NULL) {
2004                 status = NT_STATUS_NO_MEMORY;
2005                 goto fail;
2006         }
2007
2008         req = cli_close_send(frame, ev, cli, fnum);
2009         if (req == NULL) {
2010                 status = NT_STATUS_NO_MEMORY;
2011                 goto fail;
2012         }
2013
2014         if (!tevent_req_poll(req, ev)) {
2015                 status = map_nt_error_from_unix(errno);
2016                 goto fail;
2017         }
2018
2019         status = cli_close_recv(req);
2020  fail:
2021         TALLOC_FREE(frame);
2022         if (!NT_STATUS_IS_OK(status)) {
2023                 cli_set_error(cli, status);
2024         }
2025         return status;
2026 }
2027
2028 /****************************************************************************
2029  Truncate a file to a specified size
2030 ****************************************************************************/
2031
2032 bool cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2033 {
2034         unsigned int param_len = 6;
2035         unsigned int data_len = 8;
2036         uint16_t setup = TRANSACT2_SETFILEINFO;
2037         char param[6];
2038         unsigned char data[8];
2039         char *rparam=NULL, *rdata=NULL;
2040         int saved_timeout = cli->timeout;
2041
2042         SSVAL(param,0,fnum);
2043         SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2044         SSVAL(param,4,0);
2045
2046         SBVAL(data, 0, size);
2047
2048         if (!cli_send_trans(cli, SMBtrans2,
2049                             NULL,                    /* name */
2050                             -1, 0,                   /* fid, flags */
2051                             &setup, 1, 0,            /* setup, length, max */
2052                             param, param_len, 2,     /* param, length, max */
2053                             (char *)&data,  data_len,/* data, length, ... */
2054                             cli->max_xmit)) {        /* ... max */
2055                 cli->timeout = saved_timeout;
2056                 return False;
2057         }
2058
2059         if (!cli_receive_trans(cli, SMBtrans2,
2060                                 &rparam, &param_len,
2061                                 &rdata, &data_len)) {
2062                 cli->timeout = saved_timeout;
2063                 SAFE_FREE(rdata);
2064                 SAFE_FREE(rparam);
2065                 return False;
2066         }
2067
2068         cli->timeout = saved_timeout;
2069
2070         SAFE_FREE(rdata);
2071         SAFE_FREE(rparam);
2072
2073         return True;
2074 }
2075
2076
2077 /****************************************************************************
2078  send a lock with a specified locktype
2079  this is used for testing LOCKING_ANDX_CANCEL_LOCK
2080 ****************************************************************************/
2081
2082 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2083                       uint32_t offset, uint32_t len,
2084                       int timeout, unsigned char locktype)
2085 {
2086         char *p;
2087         int saved_timeout = cli->timeout;
2088
2089         memset(cli->outbuf,'\0',smb_size);
2090         memset(cli->inbuf,'\0', smb_size);
2091
2092         cli_set_message(cli->outbuf,8,0,True);
2093
2094         SCVAL(cli->outbuf,smb_com,SMBlockingX);
2095         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2096         cli_setup_packet(cli);
2097
2098         SCVAL(cli->outbuf,smb_vwv0,0xFF);
2099         SSVAL(cli->outbuf,smb_vwv2,fnum);
2100         SCVAL(cli->outbuf,smb_vwv3,locktype);
2101         SIVALS(cli->outbuf, smb_vwv4, timeout);
2102         SSVAL(cli->outbuf,smb_vwv6,0);
2103         SSVAL(cli->outbuf,smb_vwv7,1);
2104
2105         p = smb_buf(cli->outbuf);
2106         SSVAL(p, 0, cli->pid);
2107         SIVAL(p, 2, offset);
2108         SIVAL(p, 6, len);
2109
2110         p += 10;
2111
2112         cli_setup_bcc(cli, p);
2113
2114         cli_send_smb(cli);
2115
2116         if (timeout != 0) {
2117                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
2118         }
2119
2120         if (!cli_receive_smb(cli)) {
2121                 cli->timeout = saved_timeout;
2122                 return NT_STATUS_UNSUCCESSFUL;
2123         }
2124
2125         cli->timeout = saved_timeout;
2126
2127         return cli_nt_error(cli);
2128 }
2129
2130 /****************************************************************************
2131  Lock a file.
2132  note that timeout is in units of 2 milliseconds
2133 ****************************************************************************/
2134
2135 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2136               uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
2137 {
2138         char *p;
2139         int saved_timeout = cli->timeout;
2140
2141         memset(cli->outbuf,'\0',smb_size);
2142         memset(cli->inbuf,'\0', smb_size);
2143
2144         cli_set_message(cli->outbuf,8,0,True);
2145
2146         SCVAL(cli->outbuf,smb_com,SMBlockingX);
2147         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2148         cli_setup_packet(cli);
2149
2150         SCVAL(cli->outbuf,smb_vwv0,0xFF);
2151         SSVAL(cli->outbuf,smb_vwv2,fnum);
2152         SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
2153         SIVALS(cli->outbuf, smb_vwv4, timeout);
2154         SSVAL(cli->outbuf,smb_vwv6,0);
2155         SSVAL(cli->outbuf,smb_vwv7,1);
2156
2157         p = smb_buf(cli->outbuf);
2158         SSVAL(p, 0, cli->pid);
2159         SIVAL(p, 2, offset);
2160         SIVAL(p, 6, len);
2161
2162         p += 10;
2163
2164         cli_setup_bcc(cli, p);
2165
2166         cli_send_smb(cli);
2167
2168         if (timeout != 0) {
2169                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
2170         }
2171
2172         if (!cli_receive_smb(cli)) {
2173                 cli->timeout = saved_timeout;
2174                 return False;
2175         }
2176
2177         cli->timeout = saved_timeout;
2178
2179         if (cli_is_error(cli)) {
2180                 return False;
2181         }
2182
2183         return True;
2184 }
2185
2186 /****************************************************************************
2187  Unlock a file.
2188 ****************************************************************************/
2189
2190 bool cli_unlock(struct cli_state *cli, uint16_t fnum, uint32_t offset, uint32_t len)
2191 {
2192         char *p;
2193
2194         memset(cli->outbuf,'\0',smb_size);
2195         memset(cli->inbuf,'\0',smb_size);
2196
2197         cli_set_message(cli->outbuf,8,0,True);
2198
2199         SCVAL(cli->outbuf,smb_com,SMBlockingX);
2200         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2201         cli_setup_packet(cli);
2202
2203         SCVAL(cli->outbuf,smb_vwv0,0xFF);
2204         SSVAL(cli->outbuf,smb_vwv2,fnum);
2205         SCVAL(cli->outbuf,smb_vwv3,0);
2206         SIVALS(cli->outbuf, smb_vwv4, 0);
2207         SSVAL(cli->outbuf,smb_vwv6,1);
2208         SSVAL(cli->outbuf,smb_vwv7,0);
2209
2210         p = smb_buf(cli->outbuf);
2211         SSVAL(p, 0, cli->pid);
2212         SIVAL(p, 2, offset);
2213         SIVAL(p, 6, len);
2214         p += 10;
2215         cli_setup_bcc(cli, p);
2216         cli_send_smb(cli);
2217         if (!cli_receive_smb(cli)) {
2218                 return False;
2219         }
2220
2221         if (cli_is_error(cli)) {
2222                 return False;
2223         }
2224
2225         return True;
2226 }
2227
2228 /****************************************************************************
2229  Lock a file with 64 bit offsets.
2230 ****************************************************************************/
2231
2232 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2233                 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
2234 {
2235         char *p;
2236         int saved_timeout = cli->timeout;
2237         int ltype;
2238
2239         if (! (cli->capabilities & CAP_LARGE_FILES)) {
2240                 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2241         }
2242
2243         ltype = (lock_type == READ_LOCK? 1 : 0);
2244         ltype |= LOCKING_ANDX_LARGE_FILES;
2245
2246         memset(cli->outbuf,'\0',smb_size);
2247         memset(cli->inbuf,'\0', smb_size);
2248
2249         cli_set_message(cli->outbuf,8,0,True);
2250
2251         SCVAL(cli->outbuf,smb_com,SMBlockingX);
2252         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2253         cli_setup_packet(cli);
2254
2255         SCVAL(cli->outbuf,smb_vwv0,0xFF);
2256         SSVAL(cli->outbuf,smb_vwv2,fnum);
2257         SCVAL(cli->outbuf,smb_vwv3,ltype);
2258         SIVALS(cli->outbuf, smb_vwv4, timeout);
2259         SSVAL(cli->outbuf,smb_vwv6,0);
2260         SSVAL(cli->outbuf,smb_vwv7,1);
2261
2262         p = smb_buf(cli->outbuf);
2263         SIVAL(p, 0, cli->pid);
2264         SOFF_T_R(p, 4, offset);
2265         SOFF_T_R(p, 12, len);
2266         p += 20;
2267
2268         cli_setup_bcc(cli, p);
2269         cli_send_smb(cli);
2270
2271         if (timeout != 0) {
2272                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
2273         }
2274
2275         if (!cli_receive_smb(cli)) {
2276                 cli->timeout = saved_timeout;
2277                 return False;
2278         }
2279
2280         cli->timeout = saved_timeout;
2281
2282         if (cli_is_error(cli)) {
2283                 return False;
2284         }
2285
2286         return True;
2287 }
2288
2289 /****************************************************************************
2290  Unlock a file with 64 bit offsets.
2291 ****************************************************************************/
2292
2293 bool cli_unlock64(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2294 {
2295         char *p;
2296
2297         if (! (cli->capabilities & CAP_LARGE_FILES)) {
2298                 return cli_unlock(cli, fnum, offset, len);
2299         }
2300
2301         memset(cli->outbuf,'\0',smb_size);
2302         memset(cli->inbuf,'\0',smb_size);
2303
2304         cli_set_message(cli->outbuf,8,0,True);
2305
2306         SCVAL(cli->outbuf,smb_com,SMBlockingX);
2307         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2308         cli_setup_packet(cli);
2309
2310         SCVAL(cli->outbuf,smb_vwv0,0xFF);
2311         SSVAL(cli->outbuf,smb_vwv2,fnum);
2312         SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
2313         SIVALS(cli->outbuf, smb_vwv4, 0);
2314         SSVAL(cli->outbuf,smb_vwv6,1);
2315         SSVAL(cli->outbuf,smb_vwv7,0);
2316
2317         p = smb_buf(cli->outbuf);
2318         SIVAL(p, 0, cli->pid);
2319         SOFF_T_R(p, 4, offset);
2320         SOFF_T_R(p, 12, len);
2321         p += 20;
2322         cli_setup_bcc(cli, p);
2323         cli_send_smb(cli);
2324         if (!cli_receive_smb(cli)) {
2325                 return False;
2326         }
2327
2328         if (cli_is_error(cli)) {
2329                 return False;
2330         }
2331
2332         return True;
2333 }
2334
2335 /****************************************************************************
2336  Get/unlock a POSIX lock on a file - internal function.
2337 ****************************************************************************/
2338
2339 static bool cli_posix_lock_internal(struct cli_state *cli, uint16_t fnum,
2340                 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
2341 {
2342         unsigned int param_len = 4;
2343         unsigned int data_len = POSIX_LOCK_DATA_SIZE;
2344         uint16_t setup = TRANSACT2_SETFILEINFO;
2345         char param[4];
2346         unsigned char data[POSIX_LOCK_DATA_SIZE];
2347         char *rparam=NULL, *rdata=NULL;
2348         int saved_timeout = cli->timeout;
2349
2350         SSVAL(param,0,fnum);
2351         SSVAL(param,2,SMB_SET_POSIX_LOCK);
2352
2353         switch (lock_type) {
2354                 case READ_LOCK:
2355                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
2356                         break;
2357                 case WRITE_LOCK:
2358                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
2359                         break;
2360                 case UNLOCK_LOCK:
2361                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
2362                         break;
2363                 default:
2364                         return False;
2365         }
2366
2367         if (wait_lock) {
2368                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
2369                 cli->timeout = 0x7FFFFFFF;
2370         } else {
2371                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
2372         }
2373
2374         SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
2375         SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
2376         SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
2377
2378         if (!cli_send_trans(cli, SMBtrans2,
2379                         NULL,                        /* name */
2380                         -1, 0,                          /* fid, flags */
2381                         &setup, 1, 0,                   /* setup, length, max */
2382                         param, param_len, 2,            /* param, length, max */
2383                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2384                         )) {
2385                 cli->timeout = saved_timeout;
2386                 return False;
2387         }
2388
2389         if (!cli_receive_trans(cli, SMBtrans2,
2390                                 &rparam, &param_len,
2391                                 &rdata, &data_len)) {
2392                 cli->timeout = saved_timeout;
2393                 SAFE_FREE(rdata);
2394                 SAFE_FREE(rparam);
2395                 return False;
2396         }
2397
2398         cli->timeout = saved_timeout;
2399
2400         SAFE_FREE(rdata);
2401         SAFE_FREE(rparam);
2402
2403         return True;
2404 }
2405
2406 /****************************************************************************
2407  POSIX Lock a file.
2408 ****************************************************************************/
2409
2410 bool cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2411                         uint64_t offset, uint64_t len,
2412                         bool wait_lock, enum brl_type lock_type)
2413 {
2414         if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2415                 return False;
2416         }
2417         return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
2418 }
2419
2420 /****************************************************************************
2421  POSIX Unlock a file.
2422 ****************************************************************************/
2423
2424 bool cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2425 {
2426         return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
2427 }
2428
2429 /****************************************************************************
2430  POSIX Get any lock covering a file.
2431 ****************************************************************************/
2432
2433 bool cli_posix_getlock(struct cli_state *cli, uint16_t fnum, uint64_t *poffset, uint64_t *plen)
2434 {
2435         return True;
2436 }
2437
2438 /****************************************************************************
2439  Do a SMBgetattrE call.
2440 ****************************************************************************/
2441
2442 static void cli_getattrE_done(struct tevent_req *subreq);
2443
2444 struct cli_getattrE_state {
2445         uint16_t vwv[1];
2446         int zone_offset;
2447         uint16_t attr;
2448         SMB_OFF_T size;
2449         time_t change_time;
2450         time_t access_time;
2451         time_t write_time;
2452 };
2453
2454 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
2455                                 struct event_context *ev,
2456                                 struct cli_state *cli,
2457                                 uint16_t fnum)
2458 {
2459         struct tevent_req *req = NULL, *subreq = NULL;
2460         struct cli_getattrE_state *state = NULL;
2461         uint8_t additional_flags = 0;
2462
2463         req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
2464         if (req == NULL) {
2465                 return NULL;
2466         }
2467
2468         state->zone_offset = cli->serverzone;
2469         SSVAL(state->vwv+0,0,fnum);
2470
2471         subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
2472                               1, state->vwv, 0, NULL);
2473         if (tevent_req_nomem(subreq, req)) {
2474                 return tevent_req_post(req, ev);
2475         }
2476         tevent_req_set_callback(subreq, cli_getattrE_done, req);
2477         return req;
2478 }
2479
2480 static void cli_getattrE_done(struct tevent_req *subreq)
2481 {
2482         struct tevent_req *req = tevent_req_callback_data(
2483                 subreq, struct tevent_req);
2484         struct cli_getattrE_state *state = tevent_req_data(
2485                 req, struct cli_getattrE_state);
2486         uint8_t wct;
2487         uint16_t *vwv = NULL;
2488         NTSTATUS status;
2489
2490         status = cli_smb_recv(subreq, 11, &wct, &vwv, NULL, NULL);
2491         if (!NT_STATUS_IS_OK(status)) {
2492                 tevent_req_nterror(req, status);
2493                 return;
2494         }
2495
2496         state->size = (SMB_OFF_T)IVAL(vwv+6,0);
2497         state->attr = SVAL(vwv+10,0);
2498         state->change_time = make_unix_date2(vwv+0, state->zone_offset);
2499         state->access_time = make_unix_date2(vwv+2, state->zone_offset);
2500         state->write_time = make_unix_date2(vwv+4, state->zone_offset);
2501
2502         TALLOC_FREE(subreq);
2503         tevent_req_done(req);
2504 }
2505
2506 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
2507                         uint16_t *attr,
2508                         SMB_OFF_T *size,
2509                         time_t *change_time,
2510                         time_t *access_time,
2511                         time_t *write_time)
2512 {
2513         struct cli_getattrE_state *state = tevent_req_data(
2514                                 req, struct cli_getattrE_state);
2515         NTSTATUS status;
2516
2517         if (tevent_req_is_nterror(req, &status)) {
2518                 return status;
2519         }
2520         if (attr) {
2521                 *attr = state->attr;
2522         }
2523         if (size) {
2524                 *size = state->size;
2525         }
2526         if (change_time) {
2527                 *change_time = state->change_time;
2528         }
2529         if (access_time) {
2530                 *access_time = state->access_time;
2531         }
2532         if (write_time) {
2533                 *write_time = state->write_time;
2534         }
2535         return NT_STATUS_OK;
2536 }
2537
2538 NTSTATUS cli_getattrE(struct cli_state *cli,
2539                         uint16_t fnum,
2540                         uint16_t *attr,
2541                         SMB_OFF_T *size,
2542                         time_t *change_time,
2543                         time_t *access_time,
2544                         time_t *write_time)
2545 {
2546         TALLOC_CTX *frame = talloc_stackframe();
2547         struct event_context *ev = NULL;
2548         struct tevent_req *req = NULL;
2549         NTSTATUS status = NT_STATUS_OK;
2550
2551         if (cli_has_async_calls(cli)) {
2552                 /*
2553                  * Can't use sync call while an async call is in flight
2554                  */
2555                 status = NT_STATUS_INVALID_PARAMETER;
2556                 goto fail;
2557         }
2558
2559         ev = event_context_init(frame);
2560         if (ev == NULL) {
2561                 status = NT_STATUS_NO_MEMORY;
2562                 goto fail;
2563         }
2564
2565         req = cli_getattrE_send(frame, ev, cli, fnum);
2566         if (req == NULL) {
2567                 status = NT_STATUS_NO_MEMORY;
2568                 goto fail;
2569         }
2570
2571         if (!tevent_req_poll(req, ev)) {
2572                 status = map_nt_error_from_unix(errno);
2573                 goto fail;
2574         }
2575
2576         status = cli_getattrE_recv(req,
2577                                         attr,
2578                                         size,
2579                                         change_time,
2580                                         access_time,
2581                                         write_time);
2582
2583  fail:
2584         TALLOC_FREE(frame);
2585         if (!NT_STATUS_IS_OK(status)) {
2586                 cli_set_error(cli, status);
2587         }
2588         return status;
2589 }
2590
2591 /****************************************************************************
2592  Do a SMBgetatr call
2593 ****************************************************************************/
2594
2595 static void cli_getatr_done(struct tevent_req *subreq);
2596
2597 struct cli_getatr_state {
2598         int zone_offset;
2599         uint16_t attr;
2600         SMB_OFF_T size;
2601         time_t write_time;
2602 };
2603
2604 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
2605                                 struct event_context *ev,
2606                                 struct cli_state *cli,
2607                                 const char *fname)
2608 {
2609         struct tevent_req *req = NULL, *subreq = NULL;
2610         struct cli_getatr_state *state = NULL;
2611         uint8_t additional_flags = 0;
2612         uint8_t *bytes = NULL;
2613
2614         req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
2615         if (req == NULL) {
2616                 return NULL;
2617         }
2618
2619         state->zone_offset = cli->serverzone;
2620
2621         bytes = talloc_array(state, uint8_t, 1);
2622         if (tevent_req_nomem(bytes, req)) {
2623                 return tevent_req_post(req, ev);
2624         }
2625         bytes[0] = 4;
2626         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2627                                    strlen(fname)+1, NULL);
2628
2629         if (tevent_req_nomem(bytes, req)) {
2630                 return tevent_req_post(req, ev);
2631         }
2632
2633         subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
2634                               0, NULL, talloc_get_size(bytes), bytes);
2635         if (tevent_req_nomem(subreq, req)) {
2636                 return tevent_req_post(req, ev);
2637         }
2638         tevent_req_set_callback(subreq, cli_getatr_done, req);
2639         return req;
2640 }
2641
2642 static void cli_getatr_done(struct tevent_req *subreq)
2643 {
2644         struct tevent_req *req = tevent_req_callback_data(
2645                 subreq, struct tevent_req);
2646         struct cli_getatr_state *state = tevent_req_data(
2647                 req, struct cli_getatr_state);
2648         uint8_t wct;
2649         uint16_t *vwv = NULL;
2650         NTSTATUS status;
2651
2652         status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
2653         if (!NT_STATUS_IS_OK(status)) {
2654                 tevent_req_nterror(req, status);
2655                 return;
2656         }
2657
2658         state->attr = SVAL(vwv+0,0);
2659         state->size = (SMB_OFF_T)IVAL(vwv+3,0);
2660         state->write_time = make_unix_date3(vwv+1, state->zone_offset);
2661
2662         TALLOC_FREE(subreq);
2663         tevent_req_done(req);
2664 }
2665
2666 NTSTATUS cli_getatr_recv(struct tevent_req *req,
2667                         uint16_t *attr,
2668                         SMB_OFF_T *size,
2669                         time_t *write_time)
2670 {
2671         struct cli_getatr_state *state = tevent_req_data(
2672                                 req, struct cli_getatr_state);
2673         NTSTATUS status;
2674
2675         if (tevent_req_is_nterror(req, &status)) {
2676                 return status;
2677         }
2678         if (attr) {
2679                 *attr = state->attr;
2680         }
2681         if (size) {
2682                 *size = state->size;
2683         }
2684         if (write_time) {
2685                 *write_time = state->write_time;
2686         }
2687         return NT_STATUS_OK;
2688 }
2689
2690 NTSTATUS cli_getatr(struct cli_state *cli,
2691                         const char *fname,
2692                         uint16_t *attr,
2693                         SMB_OFF_T *size,
2694                         time_t *write_time)
2695 {
2696         TALLOC_CTX *frame = talloc_stackframe();
2697         struct event_context *ev = NULL;
2698         struct tevent_req *req = NULL;
2699         NTSTATUS status = NT_STATUS_OK;
2700
2701         if (cli_has_async_calls(cli)) {
2702                 /*
2703                  * Can't use sync call while an async call is in flight
2704                  */
2705                 status = NT_STATUS_INVALID_PARAMETER;
2706                 goto fail;
2707         }
2708
2709         ev = event_context_init(frame);
2710         if (ev == NULL) {
2711                 status = NT_STATUS_NO_MEMORY;
2712                 goto fail;
2713         }
2714
2715         req = cli_getatr_send(frame, ev, cli, fname);
2716         if (req == NULL) {
2717                 status = NT_STATUS_NO_MEMORY;
2718                 goto fail;
2719         }
2720
2721         if (!tevent_req_poll(req, ev)) {
2722                 status = map_nt_error_from_unix(errno);
2723                 goto fail;
2724         }
2725
2726         status = cli_getatr_recv(req,
2727                                 attr,
2728                                 size,
2729                                 write_time);
2730
2731  fail:
2732         TALLOC_FREE(frame);
2733         if (!NT_STATUS_IS_OK(status)) {
2734                 cli_set_error(cli, status);
2735         }
2736         return status;
2737 }
2738
2739 /****************************************************************************
2740  Do a SMBsetattrE call.
2741 ****************************************************************************/
2742
2743 static void cli_setattrE_done(struct tevent_req *subreq);
2744
2745 struct cli_setattrE_state {
2746         int dummy;
2747 };
2748
2749 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
2750                                 struct event_context *ev,
2751                                 struct cli_state *cli,
2752                                 uint16_t fnum,
2753                                 time_t change_time,
2754                                 time_t access_time,
2755                                 time_t write_time)
2756 {
2757         struct tevent_req *req = NULL, *subreq = NULL;
2758         struct cli_setattrE_state *state = NULL;
2759         uint8_t additional_flags = 0;
2760         uint16_t vwv[7];
2761
2762         req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
2763         if (req == NULL) {
2764                 return NULL;
2765         }
2766
2767         memset(vwv, '\0', sizeof(vwv));
2768         SSVAL(vwv+0, 0, fnum);
2769         cli_put_dos_date2(cli, (char *)&vwv[1], 0, change_time);
2770         cli_put_dos_date2(cli, (char *)&vwv[3], 0, access_time);
2771         cli_put_dos_date2(cli, (char *)&vwv[5], 0, write_time);
2772
2773         subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
2774                               7, vwv, 0, NULL);
2775         if (tevent_req_nomem(subreq, req)) {
2776                 return tevent_req_post(req, ev);
2777         }
2778         tevent_req_set_callback(subreq, cli_setattrE_done, req);
2779         return req;
2780 }
2781
2782 static void cli_setattrE_done(struct tevent_req *subreq)
2783 {
2784         struct tevent_req *req = tevent_req_callback_data(
2785                 subreq, struct tevent_req);
2786         NTSTATUS status;
2787
2788         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2789         TALLOC_FREE(subreq);
2790         if (!NT_STATUS_IS_OK(status)) {
2791                 tevent_req_nterror(req, status);
2792                 return;
2793         }
2794         tevent_req_done(req);
2795 }
2796
2797 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
2798 {
2799         return tevent_req_simple_recv_ntstatus(req);
2800 }
2801
2802 NTSTATUS cli_setattrE(struct cli_state *cli,
2803                         uint16_t fnum,
2804                         time_t change_time,
2805                         time_t access_time,
2806                         time_t write_time)
2807 {
2808         TALLOC_CTX *frame = talloc_stackframe();
2809         struct event_context *ev = NULL;
2810         struct tevent_req *req = NULL;
2811         NTSTATUS status = NT_STATUS_OK;
2812
2813         if (cli_has_async_calls(cli)) {
2814                 /*
2815                  * Can't use sync call while an async call is in flight
2816                  */
2817                 status = NT_STATUS_INVALID_PARAMETER;
2818                 goto fail;
2819         }
2820
2821         ev = event_context_init(frame);
2822         if (ev == NULL) {
2823                 status = NT_STATUS_NO_MEMORY;
2824                 goto fail;
2825         }
2826
2827         req = cli_setattrE_send(frame, ev,
2828                         cli,
2829                         fnum,
2830                         change_time,
2831                         access_time,
2832                         write_time);
2833
2834         if (req == NULL) {
2835                 status = NT_STATUS_NO_MEMORY;
2836                 goto fail;
2837         }
2838
2839         if (!tevent_req_poll(req, ev)) {
2840                 status = map_nt_error_from_unix(errno);
2841                 goto fail;
2842         }
2843
2844         status = cli_setattrE_recv(req);
2845
2846  fail:
2847         TALLOC_FREE(frame);
2848         if (!NT_STATUS_IS_OK(status)) {
2849                 cli_set_error(cli, status);
2850         }
2851         return status;
2852 }
2853
2854 /****************************************************************************
2855  Do a SMBsetatr call.
2856 ****************************************************************************/
2857
2858 static void cli_setatr_done(struct tevent_req *subreq);
2859
2860 struct cli_setatr_state {
2861         uint16_t vwv[8];
2862 };
2863
2864 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
2865                                 struct event_context *ev,
2866                                 struct cli_state *cli,
2867                                 const char *fname,
2868                                 uint16_t attr,
2869                                 time_t mtime)
2870 {
2871         struct tevent_req *req = NULL, *subreq = NULL;
2872         struct cli_setatr_state *state = NULL;
2873         uint8_t additional_flags = 0;
2874         uint8_t *bytes = NULL;
2875
2876         req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
2877         if (req == NULL) {
2878                 return NULL;
2879         }
2880
2881         memset(state->vwv, '\0', sizeof(state->vwv));
2882         SSVAL(state->vwv+0, 0, attr);
2883         cli_put_dos_date3(cli, (char *)&state->vwv[1], 0, mtime);
2884
2885         bytes = talloc_array(state, uint8_t, 1);
2886         if (tevent_req_nomem(bytes, req)) {
2887                 return tevent_req_post(req, ev);
2888         }
2889         bytes[0] = 4;
2890         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2891                                    strlen(fname)+1, NULL);
2892         if (tevent_req_nomem(bytes, req)) {
2893                 return tevent_req_post(req, ev);
2894         }
2895         bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
2896                         talloc_get_size(bytes)+1);
2897         if (tevent_req_nomem(bytes, req)) {
2898                 return tevent_req_post(req, ev);
2899         }
2900
2901         bytes[talloc_get_size(bytes)-1] = 4;
2902         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
2903                                    1, NULL);
2904         if (tevent_req_nomem(bytes, req)) {
2905                 return tevent_req_post(req, ev);
2906         }
2907
2908         subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
2909                               8, state->vwv, talloc_get_size(bytes), bytes);
2910         if (tevent_req_nomem(subreq, req)) {
2911                 return tevent_req_post(req, ev);
2912         }
2913         tevent_req_set_callback(subreq, cli_setatr_done, req);
2914         return req;
2915 }
2916
2917 static void cli_setatr_done(struct tevent_req *subreq)
2918 {
2919         struct tevent_req *req = tevent_req_callback_data(
2920                 subreq, struct tevent_req);
2921         NTSTATUS status;
2922
2923         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2924         TALLOC_FREE(subreq);
2925         if (!NT_STATUS_IS_OK(status)) {
2926                 tevent_req_nterror(req, status);
2927                 return;
2928         }
2929         tevent_req_done(req);
2930 }
2931
2932 NTSTATUS cli_setatr_recv(struct tevent_req *req)
2933 {
2934         return tevent_req_simple_recv_ntstatus(req);
2935 }
2936
2937 NTSTATUS cli_setatr(struct cli_state *cli,
2938                 const char *fname,
2939                 uint16_t attr,
2940                 time_t mtime)
2941 {
2942         TALLOC_CTX *frame = talloc_stackframe();
2943         struct event_context *ev = NULL;
2944         struct tevent_req *req = NULL;
2945         NTSTATUS status = NT_STATUS_OK;
2946
2947         if (cli_has_async_calls(cli)) {
2948                 /*
2949                  * Can't use sync call while an async call is in flight
2950                  */
2951                 status = NT_STATUS_INVALID_PARAMETER;
2952                 goto fail;
2953         }
2954
2955         ev = event_context_init(frame);
2956         if (ev == NULL) {
2957                 status = NT_STATUS_NO_MEMORY;
2958                 goto fail;
2959         }
2960
2961         req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
2962         if (req == NULL) {
2963                 status = NT_STATUS_NO_MEMORY;
2964                 goto fail;
2965         }
2966
2967         if (!tevent_req_poll(req, ev)) {
2968                 status = map_nt_error_from_unix(errno);
2969                 goto fail;
2970         }
2971
2972         status = cli_setatr_recv(req);
2973
2974  fail:
2975         TALLOC_FREE(frame);
2976         if (!NT_STATUS_IS_OK(status)) {
2977                 cli_set_error(cli, status);
2978         }
2979         return status;
2980 }
2981
2982 /****************************************************************************
2983  Check for existance of a dir.
2984 ****************************************************************************/
2985
2986 static void cli_chkpath_done(struct tevent_req *subreq);
2987
2988 struct cli_chkpath_state {
2989         int dummy;
2990 };
2991
2992 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
2993                                   struct event_context *ev,
2994                                   struct cli_state *cli,
2995                                   const char *fname)
2996 {
2997         struct tevent_req *req = NULL, *subreq = NULL;
2998         struct cli_chkpath_state *state = NULL;
2999         uint8_t additional_flags = 0;
3000         uint8_t *bytes = NULL;
3001
3002         req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3003         if (req == NULL) {
3004                 return NULL;
3005         }
3006
3007         bytes = talloc_array(state, uint8_t, 1);
3008         if (tevent_req_nomem(bytes, req)) {
3009                 return tevent_req_post(req, ev);
3010         }
3011         bytes[0] = 4;
3012         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3013                                    strlen(fname)+1, NULL);
3014
3015         if (tevent_req_nomem(bytes, req)) {
3016                 return tevent_req_post(req, ev);
3017         }
3018
3019         subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3020                               0, NULL, talloc_get_size(bytes), bytes);
3021         if (tevent_req_nomem(subreq, req)) {
3022                 return tevent_req_post(req, ev);
3023         }
3024         tevent_req_set_callback(subreq, cli_chkpath_done, req);
3025         return req;
3026 }
3027
3028 static void cli_chkpath_done(struct tevent_req *subreq)
3029 {
3030         struct tevent_req *req = tevent_req_callback_data(
3031                 subreq, struct tevent_req);
3032         NTSTATUS status;
3033
3034         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
3035         TALLOC_FREE(subreq);
3036         if (!NT_STATUS_IS_OK(status)) {
3037                 tevent_req_nterror(req, status);
3038                 return;
3039         }
3040         tevent_req_done(req);
3041 }
3042
3043 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3044 {
3045         return tevent_req_simple_recv_ntstatus(req);
3046 }
3047
3048 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3049 {
3050         TALLOC_CTX *frame = talloc_stackframe();
3051         struct event_context *ev = NULL;
3052         struct tevent_req *req = NULL;
3053         char *path2 = NULL;
3054         NTSTATUS status = NT_STATUS_OK;
3055
3056         if (cli_has_async_calls(cli)) {
3057                 /*
3058                  * Can't use sync call while an async call is in flight
3059                  */
3060                 status = NT_STATUS_INVALID_PARAMETER;
3061                 goto fail;
3062         }
3063
3064         path2 = talloc_strdup(frame, path);
3065         if (!path2) {
3066                 status = NT_STATUS_NO_MEMORY;
3067                 goto fail;
3068         }
3069         trim_char(path2,'\0','\\');
3070         if (!*path2) {
3071                 path2 = talloc_strdup(frame, "\\");
3072                 if (!path2) {
3073                         status = NT_STATUS_NO_MEMORY;
3074                         goto fail;
3075                 }
3076         }
3077
3078         ev = event_context_init(frame);
3079         if (ev == NULL) {
3080                 status = NT_STATUS_NO_MEMORY;
3081                 goto fail;
3082         }
3083
3084         req = cli_chkpath_send(frame, ev, cli, path2);
3085         if (req == NULL) {
3086                 status = NT_STATUS_NO_MEMORY;
3087                 goto fail;
3088         }
3089
3090         if (!tevent_req_poll(req, ev)) {
3091                 status = map_nt_error_from_unix(errno);
3092                 goto fail;
3093         }
3094
3095         status = cli_chkpath_recv(req);
3096
3097  fail:
3098         TALLOC_FREE(frame);
3099         if (!NT_STATUS_IS_OK(status)) {
3100                 cli_set_error(cli, status);
3101         }
3102         return status;
3103 }
3104
3105 /****************************************************************************
3106  Query disk space.
3107 ****************************************************************************/
3108
3109 static void cli_dskattr_done(struct tevent_req *subreq);
3110
3111 struct cli_dskattr_state {
3112         int bsize;
3113         int total;
3114         int avail;
3115 };
3116
3117 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3118                                   struct event_context *ev,
3119                                   struct cli_state *cli)
3120 {
3121         struct tevent_req *req = NULL, *subreq = NULL;
3122         struct cli_dskattr_state *state = NULL;
3123         uint8_t additional_flags = 0;
3124
3125         req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3126         if (req == NULL) {
3127                 return NULL;
3128         }
3129
3130         subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3131                               0, NULL, 0, NULL);
3132         if (tevent_req_nomem(subreq, req)) {
3133                 return tevent_req_post(req, ev);
3134         }
3135         tevent_req_set_callback(subreq, cli_dskattr_done, req);
3136         return req;
3137 }
3138
3139 static void cli_dskattr_done(struct tevent_req *subreq)
3140 {
3141         struct tevent_req *req = tevent_req_callback_data(
3142                 subreq, struct tevent_req);
3143         struct cli_dskattr_state *state = tevent_req_data(
3144                 req, struct cli_dskattr_state);
3145         uint8_t wct;
3146         uint16_t *vwv = NULL;
3147         NTSTATUS status;
3148
3149         status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
3150         if (!NT_STATUS_IS_OK(status)) {
3151                 tevent_req_nterror(req, status);
3152                 return;
3153         }
3154         state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3155         state->total = SVAL(vwv+0, 0);
3156         state->avail = SVAL(vwv+3, 0);
3157         TALLOC_FREE(subreq);
3158         tevent_req_done(req);
3159 }
3160
3161 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3162 {
3163         struct cli_dskattr_state *state = tevent_req_data(
3164                                 req, struct cli_dskattr_state);
3165         NTSTATUS status;
3166
3167         if (tevent_req_is_nterror(req, &status)) {
3168                 return status;
3169         }
3170         *bsize = state->bsize;
3171         *total = state->total;
3172         *avail = state->avail;
3173         return NT_STATUS_OK;
3174 }
3175
3176 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3177 {
3178         TALLOC_CTX *frame = talloc_stackframe();
3179         struct event_context *ev = NULL;
3180         struct tevent_req *req = NULL;
3181         NTSTATUS status = NT_STATUS_OK;
3182
3183         if (cli_has_async_calls(cli)) {
3184                 /*
3185                  * Can't use sync call while an async call is in flight
3186                  */
3187                 status = NT_STATUS_INVALID_PARAMETER;
3188                 goto fail;
3189         }
3190
3191         ev = event_context_init(frame);
3192         if (ev == NULL) {
3193                 status = NT_STATUS_NO_MEMORY;
3194                 goto fail;
3195         }
3196
3197         req = cli_dskattr_send(frame, ev, cli);
3198         if (req == NULL) {
3199                 status = NT_STATUS_NO_MEMORY;
3200                 goto fail;
3201         }
3202
3203         if (!tevent_req_poll(req, ev)) {
3204                 status = map_nt_error_from_unix(errno);
3205                 goto fail;
3206         }
3207
3208         status = cli_dskattr_recv(req, bsize, total, avail);
3209
3210  fail:
3211         TALLOC_FREE(frame);
3212         if (!NT_STATUS_IS_OK(status)) {
3213                 cli_set_error(cli, status);
3214         }
3215         return status;
3216 }
3217
3218 /****************************************************************************
3219  Create and open a temporary file.
3220 ****************************************************************************/
3221
3222 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
3223 {
3224         int len;
3225         char *p;
3226
3227         memset(cli->outbuf,'\0',smb_size);
3228         memset(cli->inbuf,'\0',smb_size);
3229
3230         cli_set_message(cli->outbuf,3,0,True);
3231
3232         SCVAL(cli->outbuf,smb_com,SMBctemp);
3233         SSVAL(cli->outbuf,smb_tid,cli->cnum);
3234         cli_setup_packet(cli);
3235
3236         SSVAL(cli->outbuf,smb_vwv0,0);
3237         SIVALS(cli->outbuf,smb_vwv1,-1);
3238
3239         p = smb_buf(cli->outbuf);
3240         *p++ = 4;
3241         p += clistr_push(cli, p, path,
3242                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
3243
3244         cli_setup_bcc(cli, p);
3245
3246         cli_send_smb(cli);
3247         if (!cli_receive_smb(cli)) {
3248                 return -1;
3249         }
3250
3251         if (cli_is_error(cli)) {
3252                 return -1;
3253         }
3254
3255         /* despite the spec, the result has a -1, followed by
3256            length, followed by name */
3257         p = smb_buf(cli->inbuf);
3258         p += 4;
3259         len = smb_buflen(cli->inbuf) - 4;
3260         if (len <= 0 || len > PATH_MAX) return -1;
3261
3262         if (tmp_path) {
3263                 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
3264                 if (!path2) {
3265                         return -1;
3266                 }
3267                 clistr_pull(cli->inbuf, path2, p,
3268                             len+1, len, STR_ASCII);
3269                 *tmp_path = path2;
3270         }
3271
3272         return SVAL(cli->inbuf,smb_vwv0);
3273 }
3274
3275 /*
3276    send a raw ioctl - used by the torture code
3277 */
3278 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
3279 {
3280         memset(cli->outbuf,'\0',smb_size);
3281         memset(cli->inbuf,'\0',smb_size);
3282
3283         cli_set_message(cli->outbuf, 3, 0, True);
3284         SCVAL(cli->outbuf,smb_com,SMBioctl);
3285         cli_setup_packet(cli);
3286
3287         SSVAL(cli->outbuf, smb_vwv0, fnum);
3288         SSVAL(cli->outbuf, smb_vwv1, code>>16);
3289         SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
3290
3291         cli_send_smb(cli);
3292         if (!cli_receive_smb(cli)) {
3293                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3294         }
3295
3296         if (cli_is_error(cli)) {
3297                 return cli_nt_error(cli);
3298         }
3299
3300         *blob = data_blob_null;
3301
3302         return NT_STATUS_OK;
3303 }
3304
3305 /*********************************************************
3306  Set an extended attribute utility fn.
3307 *********************************************************/
3308
3309 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
3310                         const char *ea_name, const char *ea_val, size_t ea_len)
3311 {
3312         unsigned int data_len = 0;
3313         char *data = NULL;
3314         char *rparam=NULL, *rdata=NULL;
3315         char *p;
3316         size_t ea_namelen = strlen(ea_name);
3317
3318         if (ea_namelen == 0 && ea_len == 0) {
3319                 data_len = 4;
3320                 data = (char *)SMB_MALLOC(data_len);
3321                 if (!data) {
3322                         return False;
3323                 }
3324                 p = data;
3325                 SIVAL(p,0,data_len);
3326         } else {
3327                 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
3328                 data = (char *)SMB_MALLOC(data_len);
3329                 if (!data) {
3330                         return False;
3331                 }
3332                 p = data;
3333                 SIVAL(p,0,data_len);
3334                 p += 4;
3335                 SCVAL(p, 0, 0); /* EA flags. */
3336                 SCVAL(p, 1, ea_namelen);
3337                 SSVAL(p, 2, ea_len);
3338                 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
3339                 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
3340         }
3341
3342         if (!cli_send_trans(cli, SMBtrans2,
3343                         NULL,                        /* name */
3344                         -1, 0,                          /* fid, flags */
3345                         &setup, 1, 0,                   /* setup, length, max */
3346                         param, param_len, 2,            /* param, length, max */
3347                         data,  data_len, cli->max_xmit /* data, length, max */
3348                         )) {
3349                 SAFE_FREE(data);
3350                 return False;
3351         }
3352
3353         if (!cli_receive_trans(cli, SMBtrans2,
3354                         &rparam, &param_len,
3355                         &rdata, &data_len)) {
3356                         SAFE_FREE(data);
3357                 return false;
3358         }
3359
3360         SAFE_FREE(data);
3361         SAFE_FREE(rdata);
3362         SAFE_FREE(rparam);
3363
3364         return True;
3365 }
3366
3367 /*********************************************************
3368  Set an extended attribute on a pathname.
3369 *********************************************************/
3370
3371 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
3372 {
3373         uint16_t setup = TRANSACT2_SETPATHINFO;
3374         unsigned int param_len = 0;
3375         char *param;
3376         size_t srclen = 2*(strlen(path)+1);
3377         char *p;
3378         bool ret;
3379
3380         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
3381         if (!param) {
3382                 return false;
3383         }
3384         memset(param, '\0', 6);
3385         SSVAL(param,0,SMB_INFO_SET_EA);
3386         p = &param[6];
3387
3388         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
3389         param_len = PTR_DIFF(p, param);
3390
3391         ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
3392         SAFE_FREE(param);