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