5be18366b9d5464921ed207d64ef2740f3211cd4
[ira/wip.git] / source3 / libsmb / clifile.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client file operations
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Jeremy Allison 2001-2002
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 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 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 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 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 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 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 dev_major = IVAL(rdata,60);
315                 uint32 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 mode, uint32 uid, uint32 gid)
352 {
353         unsigned int data_len = 0;
354         unsigned int param_len = 0;
355         uint16 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 bool cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
433 {
434         char *p;
435
436         memset(cli->outbuf,'\0',smb_size);
437         memset(cli->inbuf,'\0',smb_size);
438
439         cli_set_message(cli->outbuf,1, 0, true);
440
441         SCVAL(cli->outbuf,smb_com,SMBmv);
442         SSVAL(cli->outbuf,smb_tid,cli->cnum);
443         cli_setup_packet(cli);
444
445         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
446
447         p = smb_buf(cli->outbuf);
448         *p++ = 4;
449         p += clistr_push(cli, p, fname_src,
450                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
451         *p++ = 4;
452         p += clistr_push(cli, p, fname_dst,
453                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
454
455         cli_setup_bcc(cli, p);
456
457         cli_send_smb(cli);
458         if (!cli_receive_smb(cli)) {
459                 return false;
460         }
461
462         if (cli_is_error(cli)) {
463                 return false;
464         }
465
466         return true;
467 }
468
469 /****************************************************************************
470  NT Rename a file.
471 ****************************************************************************/
472
473 bool cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
474 {
475         char *p;
476
477         memset(cli->outbuf,'\0',smb_size);
478         memset(cli->inbuf,'\0',smb_size);
479
480         cli_set_message(cli->outbuf, 4, 0, true);
481
482         SCVAL(cli->outbuf,smb_com,SMBntrename);
483         SSVAL(cli->outbuf,smb_tid,cli->cnum);
484         cli_setup_packet(cli);
485
486         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
487         SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_RENAME);
488
489         p = smb_buf(cli->outbuf);
490         *p++ = 4;
491         p += clistr_push(cli, p, fname_src,
492                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
493         *p++ = 4;
494         p += clistr_push(cli, p, fname_dst,
495                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
496
497         cli_setup_bcc(cli, p);
498
499         cli_send_smb(cli);
500         if (!cli_receive_smb(cli)) {
501                 return false;
502         }
503
504         if (cli_is_error(cli)) {
505                 return false;
506         }
507
508         return true;
509 }
510
511 /****************************************************************************
512  NT hardlink a file.
513 ****************************************************************************/
514
515 bool cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
516 {
517         char *p;
518
519         memset(cli->outbuf,'\0',smb_size);
520         memset(cli->inbuf,'\0',smb_size);
521
522         cli_set_message(cli->outbuf, 4, 0, true);
523
524         SCVAL(cli->outbuf,smb_com,SMBntrename);
525         SSVAL(cli->outbuf,smb_tid,cli->cnum);
526         cli_setup_packet(cli);
527
528         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
529         SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_HARD_LINK);
530
531         p = smb_buf(cli->outbuf);
532         *p++ = 4;
533         p += clistr_push(cli, p, fname_src,
534                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
535         *p++ = 4;
536         p += clistr_push(cli, p, fname_dst,
537                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
538
539         cli_setup_bcc(cli, p);
540
541         cli_send_smb(cli);
542         if (!cli_receive_smb(cli)) {
543                 return false;
544         }
545
546         if (cli_is_error(cli)) {
547                 return false;
548         }
549
550         return true;
551 }
552
553 /****************************************************************************
554  Delete a file.
555 ****************************************************************************/
556
557 bool cli_unlink_full(struct cli_state *cli, const char *fname, uint16 attrs)
558 {
559         char *p;
560
561         memset(cli->outbuf,'\0',smb_size);
562         memset(cli->inbuf,'\0',smb_size);
563
564         cli_set_message(cli->outbuf,1, 0, true);
565
566         SCVAL(cli->outbuf,smb_com,SMBunlink);
567         SSVAL(cli->outbuf,smb_tid,cli->cnum);
568         cli_setup_packet(cli);
569
570         SSVAL(cli->outbuf,smb_vwv0, attrs);
571
572         p = smb_buf(cli->outbuf);
573         *p++ = 4;
574         p += clistr_push(cli, p, fname,
575                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
576
577         cli_setup_bcc(cli, p);
578         cli_send_smb(cli);
579         if (!cli_receive_smb(cli)) {
580                 return false;
581         }
582
583         if (cli_is_error(cli)) {
584                 return false;
585         }
586
587         return true;
588 }
589
590 /****************************************************************************
591  Delete a file.
592 ****************************************************************************/
593
594 bool cli_unlink(struct cli_state *cli, const char *fname)
595 {
596         return cli_unlink_full(cli, fname, aSYSTEM | aHIDDEN);
597 }
598
599 #if 0
600 /****************************************************************************
601  Create a directory.
602 ****************************************************************************/
603
604 bool cli_mkdir(struct cli_state *cli, const char *dname)
605 {
606         char *p;
607
608         memset(cli->outbuf,'\0',smb_size);
609         memset(cli->inbuf,'\0',smb_size);
610
611         cli_set_message(cli->outbuf,0, 0, true);
612
613         SCVAL(cli->outbuf,smb_com,SMBmkdir);
614         SSVAL(cli->outbuf,smb_tid,cli->cnum);
615         cli_setup_packet(cli);
616
617         p = smb_buf(cli->outbuf);
618         *p++ = 4;
619         p += clistr_push(cli, p, dname,
620                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
621
622         cli_setup_bcc(cli, p);
623
624         cli_send_smb(cli);
625         if (!cli_receive_smb(cli)) {
626                 return False;
627         }
628
629         if (cli_is_error(cli)) {
630                 return False;
631         }
632
633         return True;
634 }
635 #endif
636
637 /****************************************************************************
638  Create a directory.
639 ****************************************************************************/
640
641 static void cli_mkdir_done(struct tevent_req *subreq);
642
643 struct cli_mkdir_state {
644         int dummy;
645 };
646
647 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
648                                   struct event_context *ev,
649                                   struct cli_state *cli,
650                                   const char *dname)
651 {
652         struct tevent_req *req = NULL, *subreq = NULL;
653         struct cli_mkdir_state *state = NULL;
654         uint8_t additional_flags = 0;
655         uint8_t *bytes = NULL;
656
657         req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
658         if (req == NULL) {
659                 return NULL;
660         }
661
662         bytes = talloc_array(state, uint8_t, 1);
663         if (!bytes) {
664                 return NULL;
665         }
666         bytes[0] = 4;
667         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
668                                    strlen(dname)+1, NULL);
669
670         if (tevent_req_nomem(bytes, req)) {
671                 return tevent_req_post(req, ev);
672         }
673
674         subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
675                                     0, NULL, talloc_get_size(bytes), bytes);
676         if (tevent_req_nomem(subreq, req)) {
677                 return tevent_req_post(req, ev);
678         }
679         tevent_req_set_callback(subreq, cli_mkdir_done, req);
680         return req;
681 }
682
683 static void cli_mkdir_done(struct tevent_req *subreq)
684 {
685         struct tevent_req *req = tevent_req_callback_data(
686                 subreq, struct tevent_req);
687         NTSTATUS status;
688
689         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
690         TALLOC_FREE(subreq);
691         if (!NT_STATUS_IS_OK(status)) {
692                 tevent_req_nterror(req, status);
693                 return;
694         }
695         tevent_req_done(req);
696 }
697
698 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
699 {
700         return tevent_req_simple_recv_ntstatus(req);
701 }
702
703 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
704 {
705         TALLOC_CTX *frame = talloc_stackframe();
706         struct event_context *ev;
707         struct tevent_req *req;
708         NTSTATUS status = NT_STATUS_OK;
709
710         if (cli_has_async_calls(cli)) {
711                 /*
712                  * Can't use sync call while an async call is in flight
713                  */
714                 status = NT_STATUS_INVALID_PARAMETER;
715                 goto fail;
716         }
717
718         ev = event_context_init(frame);
719         if (ev == NULL) {
720                 status = NT_STATUS_NO_MEMORY;
721                 goto fail;
722         }
723
724         req = cli_mkdir_send(frame, ev, cli, dname);
725         if (req == NULL) {
726                 status = NT_STATUS_NO_MEMORY;
727                 goto fail;
728         }
729
730         if (!tevent_req_poll(req, ev)) {
731                 status = map_nt_error_from_unix(errno);
732                 goto fail;
733         }
734
735         status = cli_mkdir_recv(req);
736
737  fail:
738         TALLOC_FREE(frame);
739         if (!NT_STATUS_IS_OK(status)) {
740                 cli_set_error(cli, status);
741         }
742         return status;
743 }
744
745 /****************************************************************************
746  Remove a directory.
747 ****************************************************************************/
748
749 bool cli_rmdir(struct cli_state *cli, const char *dname)
750 {
751         char *p;
752
753         memset(cli->outbuf,'\0',smb_size);
754         memset(cli->inbuf,'\0',smb_size);
755
756         cli_set_message(cli->outbuf,0, 0, true);
757
758         SCVAL(cli->outbuf,smb_com,SMBrmdir);
759         SSVAL(cli->outbuf,smb_tid,cli->cnum);
760         cli_setup_packet(cli);
761
762         p = smb_buf(cli->outbuf);
763         *p++ = 4;
764         p += clistr_push(cli, p, dname,
765                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
766
767         cli_setup_bcc(cli, p);
768
769         cli_send_smb(cli);
770         if (!cli_receive_smb(cli)) {
771                 return false;
772         }
773
774         if (cli_is_error(cli)) {
775                 return false;
776         }
777
778         return true;
779 }
780
781 /****************************************************************************
782  Set or clear the delete on close flag.
783 ****************************************************************************/
784
785 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, bool flag)
786 {
787         unsigned int data_len = 1;
788         unsigned int param_len = 6;
789         uint16 setup = TRANSACT2_SETFILEINFO;
790         char param[6];
791         unsigned char data;
792         char *rparam=NULL, *rdata=NULL;
793
794         memset(param, 0, param_len);
795         SSVAL(param,0,fnum);
796         SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
797
798         data = flag ? 1 : 0;
799
800         if (!cli_send_trans(cli, SMBtrans2,
801                         NULL,                        /* name */
802                         -1, 0,                          /* fid, flags */
803                         &setup, 1, 0,                   /* setup, length, max */
804                         param, param_len, 2,            /* param, length, max */
805                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
806                         )) {
807                 return false;
808         }
809
810         if (!cli_receive_trans(cli, SMBtrans2,
811                         &rparam, &param_len,
812                         &rdata, &data_len)) {
813                 return false;
814         }
815
816         SAFE_FREE(rdata);
817         SAFE_FREE(rparam);
818
819         return true;
820 }
821
822 /****************************************************************************
823  Open a file - exposing the full horror of the NT API :-).
824  Used in smbtorture.
825 ****************************************************************************/
826
827 int cli_nt_create_full(struct cli_state *cli, const char *fname,
828                        uint32 CreatFlags, uint32 DesiredAccess,
829                        uint32 FileAttributes, uint32 ShareAccess,
830                        uint32 CreateDisposition, uint32 CreateOptions,
831                        uint8 SecurityFlags)
832 {
833         char *p;
834         int len;
835
836         memset(cli->outbuf,'\0',smb_size);
837         memset(cli->inbuf,'\0',smb_size);
838
839         cli_set_message(cli->outbuf,24,0, true);
840
841         SCVAL(cli->outbuf,smb_com,SMBntcreateX);
842         SSVAL(cli->outbuf,smb_tid,cli->cnum);
843         cli_setup_packet(cli);
844
845         SSVAL(cli->outbuf,smb_vwv0,0xFF);
846         if (cli->use_oplocks)
847                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
848
849         SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
850         SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
851         SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
852         SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
853         SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
854         SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
855         SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
856         SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
857         SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecurityFlags);
858
859         p = smb_buf(cli->outbuf);
860         /* this alignment and termination is critical for netapp filers. Don't change */
861         p += clistr_align_out(cli, p, 0);
862         len = clistr_push(cli, p, fname,
863                         cli->bufsize - PTR_DIFF(p,cli->outbuf), 0);
864         p += len;
865         SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
866         /* sigh. this copes with broken netapp filer behaviour */
867         p += clistr_push(cli, p, "",
868                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
869
870         cli_setup_bcc(cli, p);
871
872         cli_send_smb(cli);
873         if (!cli_receive_smb(cli)) {
874                 return -1;
875         }
876
877         if (cli_is_error(cli)) {
878                 return -1;
879         }
880
881         return SVAL(cli->inbuf,smb_vwv2 + 1);
882 }
883
884 struct cli_ntcreate_state {
885         uint16_t vwv[24];
886         uint16_t fnum;
887 };
888
889 static void cli_ntcreate_done(struct tevent_req *subreq);
890
891 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
892                                      struct event_context *ev,
893                                      struct cli_state *cli,
894                                      const char *fname,
895                                      uint32_t CreatFlags,
896                                      uint32_t DesiredAccess,
897                                      uint32_t FileAttributes,
898                                      uint32_t ShareAccess,
899                                      uint32_t CreateDisposition,
900                                      uint32_t CreateOptions,
901                                      uint8_t SecurityFlags)
902 {
903         struct tevent_req *req, *subreq;
904         struct cli_ntcreate_state *state;
905         uint16_t *vwv;
906         uint8_t *bytes;
907         size_t converted_len;
908
909         req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
910         if (req == NULL) {
911                 return NULL;
912         }
913         vwv = state->vwv;
914
915         SCVAL(vwv+0, 0, 0xFF);
916         SCVAL(vwv+0, 1, 0);
917         SSVAL(vwv+1, 0, 0);
918         SCVAL(vwv+2, 0, 0);
919
920         if (cli->use_oplocks) {
921                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
922         }
923         SIVAL(vwv+3, 1, CreatFlags);
924         SIVAL(vwv+5, 1, 0x0);   /* RootDirectoryFid */
925         SIVAL(vwv+7, 1, DesiredAccess);
926         SIVAL(vwv+9, 1, 0x0);   /* AllocationSize */
927         SIVAL(vwv+11, 1, 0x0);  /* AllocationSize */
928         SIVAL(vwv+13, 1, FileAttributes);
929         SIVAL(vwv+15, 1, ShareAccess);
930         SIVAL(vwv+17, 1, CreateDisposition);
931         SIVAL(vwv+19, 1, CreateOptions);
932         SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
933         SCVAL(vwv+23, 1, SecurityFlags);
934
935         bytes = talloc_array(state, uint8_t, 0);
936         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
937                                    fname, strlen(fname)+1,
938                                    &converted_len);
939
940         /* sigh. this copes with broken netapp filer behaviour */
941         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
942
943         if (tevent_req_nomem(bytes, req)) {
944                 return tevent_req_post(req, ev);
945         }
946
947         SIVAL(vwv+2, 1, converted_len);
948
949         subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
950                               talloc_get_size(bytes), bytes);
951         if (tevent_req_nomem(subreq, req)) {
952                 return tevent_req_post(req, ev);
953         }
954         tevent_req_set_callback(subreq, cli_ntcreate_done, req);
955         return req;
956 }
957
958 static void cli_ntcreate_done(struct tevent_req *subreq)
959 {
960         struct tevent_req *req = tevent_req_callback_data(
961                 subreq, struct tevent_req);
962         struct cli_ntcreate_state *state = tevent_req_data(
963                 req, struct cli_ntcreate_state);
964         uint8_t wct;
965         uint16_t *vwv;
966         uint32_t num_bytes;
967         uint8_t *bytes;
968         NTSTATUS status;
969
970         status = cli_smb_recv(subreq, 3, &wct, &vwv, &num_bytes, &bytes);
971         if (!NT_STATUS_IS_OK(status)) {
972                 TALLOC_FREE(subreq);
973                 tevent_req_nterror(req, status);
974                 return;
975         }
976         state->fnum = SVAL(vwv+2, 1);
977         tevent_req_done(req);
978 }
979
980 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
981 {
982         struct cli_ntcreate_state *state = tevent_req_data(
983                 req, struct cli_ntcreate_state);
984         NTSTATUS status;
985
986         if (tevent_req_is_nterror(req, &status)) {
987                 return status;
988         }
989         *pfnum = state->fnum;
990         return NT_STATUS_OK;
991 }
992
993 NTSTATUS cli_ntcreate(struct cli_state *cli,
994                       const char *fname,
995                       uint32_t CreatFlags,
996                       uint32_t DesiredAccess,
997                       uint32_t FileAttributes,
998                       uint32_t ShareAccess,
999                       uint32_t CreateDisposition,
1000                       uint32_t CreateOptions,
1001                       uint8_t SecurityFlags,
1002                       uint16_t *pfid)
1003 {
1004         TALLOC_CTX *frame = talloc_stackframe();
1005         struct event_context *ev;
1006         struct tevent_req *req;
1007         NTSTATUS status = NT_STATUS_OK;
1008
1009         if (cli_has_async_calls(cli)) {
1010                 /*
1011                  * Can't use sync call while an async call is in flight
1012                  */
1013                 status = NT_STATUS_INVALID_PARAMETER;
1014                 goto fail;
1015         }
1016
1017         ev = event_context_init(frame);
1018         if (ev == NULL) {
1019                 status = NT_STATUS_NO_MEMORY;
1020                 goto fail;
1021         }
1022
1023         req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1024                                 DesiredAccess, FileAttributes, ShareAccess,
1025                                 CreateDisposition, CreateOptions,
1026                                 SecurityFlags);
1027         if (req == NULL) {
1028                 status = NT_STATUS_NO_MEMORY;
1029                 goto fail;
1030         }
1031
1032         if (!tevent_req_poll(req, ev)) {
1033                 status = map_nt_error_from_unix(errno);
1034                 goto fail;
1035         }
1036
1037         status = cli_ntcreate_recv(req, pfid);
1038  fail:
1039         TALLOC_FREE(frame);
1040         if (!NT_STATUS_IS_OK(status)) {
1041                 cli_set_error(cli, status);
1042         }
1043         return status;
1044 }
1045
1046 /****************************************************************************
1047  Open a file.
1048 ****************************************************************************/
1049
1050 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
1051 {
1052         return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
1053                                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
1054 }
1055
1056 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
1057                             const char *str, size_t str_len,
1058                             size_t *pconverted_size)
1059 {
1060         size_t buflen;
1061         char *converted;
1062         size_t converted_size;
1063
1064         if (buf == NULL) {
1065                 return NULL;
1066         }
1067
1068         buflen = talloc_get_size(buf);
1069         /*
1070          * We're pushing into an SMB buffer, align odd
1071          */
1072         if (ucs2 && (buflen % 2 == 0)) {
1073                 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
1074                 if (buf == NULL) {
1075                         return NULL;
1076                 }
1077                 buf[buflen] = '\0';
1078                 buflen += 1;
1079         }
1080
1081         if (!convert_string_talloc(talloc_tos(), CH_UNIX,
1082                                    ucs2 ? CH_UTF16LE : CH_DOS,
1083                                    str, str_len, &converted,
1084                                    &converted_size, true)) {
1085                 return NULL;
1086         }
1087
1088         buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
1089                                    buflen + converted_size);
1090         if (buf == NULL) {
1091                 TALLOC_FREE(converted);
1092                 return NULL;
1093         }
1094
1095         memcpy(buf + buflen, converted, converted_size);
1096
1097         TALLOC_FREE(converted);
1098
1099         if (pconverted_size) {
1100                 *pconverted_size = converted_size;
1101         }
1102
1103         return buf;
1104 }
1105
1106 /****************************************************************************
1107  Open a file
1108  WARNING: if you open with O_WRONLY then getattrE won't work!
1109 ****************************************************************************/
1110
1111 struct cli_open_state {
1112         uint16_t vwv[15];
1113         int fnum;
1114         struct iovec bytes;
1115 };
1116
1117 static void cli_open_done(struct tevent_req *subreq);
1118
1119 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
1120                                    struct event_context *ev,
1121                                    struct cli_state *cli, const char *fname,
1122                                    int flags, int share_mode,
1123                                    struct tevent_req **psmbreq)
1124 {
1125         struct tevent_req *req, *subreq;
1126         struct cli_open_state *state;
1127         unsigned openfn;
1128         unsigned accessmode;
1129         uint8_t additional_flags;
1130         uint8_t *bytes;
1131
1132         req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
1133         if (req == NULL) {
1134                 return NULL;
1135         }
1136
1137         openfn = 0;
1138         if (flags & O_CREAT) {
1139                 openfn |= (1<<4);
1140         }
1141         if (!(flags & O_EXCL)) {
1142                 if (flags & O_TRUNC)
1143                         openfn |= (1<<1);
1144                 else
1145                         openfn |= (1<<0);
1146         }
1147
1148         accessmode = (share_mode<<4);
1149
1150         if ((flags & O_ACCMODE) == O_RDWR) {
1151                 accessmode |= 2;
1152         } else if ((flags & O_ACCMODE) == O_WRONLY) {
1153                 accessmode |= 1;
1154         }
1155
1156 #if defined(O_SYNC)
1157         if ((flags & O_SYNC) == O_SYNC) {
1158                 accessmode |= (1<<14);
1159         }
1160 #endif /* O_SYNC */
1161
1162         if (share_mode == DENY_FCB) {
1163                 accessmode = 0xFF;
1164         }
1165
1166         SCVAL(state->vwv + 0, 0, 0xFF);
1167         SCVAL(state->vwv + 0, 1, 0);
1168         SSVAL(state->vwv + 1, 0, 0);
1169         SSVAL(state->vwv + 2, 0, 0);  /* no additional info */
1170         SSVAL(state->vwv + 3, 0, accessmode);
1171         SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
1172         SSVAL(state->vwv + 5, 0, 0);
1173         SIVAL(state->vwv + 6, 0, 0);
1174         SSVAL(state->vwv + 8, 0, openfn);
1175         SIVAL(state->vwv + 9, 0, 0);
1176         SIVAL(state->vwv + 11, 0, 0);
1177         SIVAL(state->vwv + 13, 0, 0);
1178
1179         additional_flags = 0;
1180
1181         if (cli->use_oplocks) {
1182                 /* if using oplocks then ask for a batch oplock via
1183                    core and extended methods */
1184                 additional_flags =
1185                         FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1186                 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
1187         }
1188
1189         bytes = talloc_array(state, uint8_t, 0);
1190         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1191                                    strlen(fname)+1, NULL);
1192
1193         if (tevent_req_nomem(bytes, req)) {
1194                 return tevent_req_post(req, ev);
1195         }
1196
1197         state->bytes.iov_base = bytes;
1198         state->bytes.iov_len = talloc_get_size(bytes);
1199
1200         subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
1201                                     15, state->vwv, 1, &state->bytes);
1202         if (subreq == NULL) {
1203                 TALLOC_FREE(req);
1204                 return NULL;
1205         }
1206         tevent_req_set_callback(subreq, cli_open_done, req);
1207         *psmbreq = subreq;
1208         return req;
1209 }
1210
1211 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1212                                  struct cli_state *cli, const char *fname,
1213                                  int flags, int share_mode)
1214 {
1215         struct tevent_req *req, *subreq;
1216
1217         req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
1218                               &subreq);
1219         if ((req == NULL) || !cli_smb_req_send(subreq)) {
1220                 TALLOC_FREE(req);
1221                 return NULL;
1222         }
1223         return req;
1224 }
1225
1226 static void cli_open_done(struct tevent_req *subreq)
1227 {
1228         struct tevent_req *req = tevent_req_callback_data(
1229                 subreq, struct tevent_req);
1230         struct cli_open_state *state = tevent_req_data(
1231                 req, struct cli_open_state);
1232         uint8_t wct;
1233         uint16_t *vwv;
1234         NTSTATUS status;
1235
1236         status = cli_smb_recv(subreq, 3, &wct, &vwv, NULL, NULL);
1237         if (!NT_STATUS_IS_OK(status)) {
1238                 TALLOC_FREE(subreq);
1239                 tevent_req_nterror(req, status);
1240                 return;
1241         }
1242         state->fnum = SVAL(vwv+2, 0);
1243         tevent_req_done(req);
1244 }
1245
1246 NTSTATUS cli_open_recv(struct tevent_req *req, int *fnum)
1247 {
1248         struct cli_open_state *state = tevent_req_data(
1249                 req, struct cli_open_state);
1250         NTSTATUS status;
1251
1252         if (tevent_req_is_nterror(req, &status)) {
1253                 return status;
1254         }
1255         *fnum = state->fnum;
1256         return NT_STATUS_OK;
1257 }
1258
1259 int cli_open(struct cli_state *cli, const char *fname, int flags,
1260              int share_mode)
1261 {
1262         TALLOC_CTX *frame = talloc_stackframe();
1263         struct event_context *ev;
1264         struct tevent_req *req;
1265         NTSTATUS status = NT_STATUS_OK;
1266         int result = -1;
1267
1268         if (cli_has_async_calls(cli)) {
1269                 /*
1270                  * Can't use sync call while an async call is in flight
1271                  */
1272                 status = NT_STATUS_INVALID_PARAMETER;
1273                 goto fail;
1274         }
1275
1276         ev = event_context_init(frame);
1277         if (ev == NULL) {
1278                 status = NT_STATUS_NO_MEMORY;
1279                 goto fail;
1280         }
1281
1282         req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
1283         if (req == NULL) {
1284                 status = NT_STATUS_NO_MEMORY;
1285                 goto fail;
1286         }
1287
1288         if (!tevent_req_poll(req, ev)) {
1289                 status = map_nt_error_from_unix(errno);
1290                 goto fail;
1291         }
1292
1293         cli_open_recv(req, &result);
1294  fail:
1295         TALLOC_FREE(frame);
1296         if (!NT_STATUS_IS_OK(status)) {
1297                 cli_set_error(cli, status);
1298         }
1299         return result;
1300 }
1301
1302 /****************************************************************************
1303  Close a file.
1304 ****************************************************************************/
1305
1306 struct cli_close_state {
1307         uint16_t vwv[3];
1308 };
1309
1310 static void cli_close_done(struct tevent_req *subreq);
1311
1312 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
1313                                     struct event_context *ev,
1314                                     struct cli_state *cli, int fnum,
1315                                     struct tevent_req **psubreq)
1316 {
1317         struct tevent_req *req, *subreq;
1318         struct cli_close_state *state;
1319
1320         req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
1321         if (req == NULL) {
1322                 return NULL;
1323         }
1324         SSVAL(state->vwv+0, 0, fnum);
1325         SIVALS(state->vwv+1, 0, -1);
1326
1327         subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
1328                                     0, NULL);
1329         if (subreq == NULL) {
1330                 TALLOC_FREE(req);
1331                 return NULL;
1332         }
1333         tevent_req_set_callback(subreq, cli_close_done, req);
1334         *psubreq = subreq;
1335         return req;
1336 }
1337
1338 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
1339                                   struct event_context *ev,
1340                                   struct cli_state *cli, int fnum)
1341 {
1342         struct tevent_req *req, *subreq;
1343
1344         req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
1345         if ((req == NULL) || !cli_smb_req_send(subreq)) {
1346                 TALLOC_FREE(req);
1347                 return NULL;
1348         }
1349         return req;
1350 }
1351
1352 static void cli_close_done(struct tevent_req *subreq)
1353 {
1354         struct tevent_req *req = tevent_req_callback_data(
1355                 subreq, struct tevent_req);
1356         NTSTATUS status;
1357
1358         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1359         TALLOC_FREE(subreq);
1360         if (!NT_STATUS_IS_OK(status)) {
1361                 tevent_req_nterror(req, status);
1362                 return;
1363         }
1364         tevent_req_done(req);
1365 }
1366
1367 NTSTATUS cli_close_recv(struct tevent_req *req)
1368 {
1369         return tevent_req_simple_recv_ntstatus(req);
1370 }
1371
1372 bool cli_close(struct cli_state *cli, int fnum)
1373 {
1374         TALLOC_CTX *frame = talloc_stackframe();
1375         struct event_context *ev;
1376         struct tevent_req *req;
1377         NTSTATUS status = NT_STATUS_OK;
1378         bool result = false;
1379
1380         if (cli_has_async_calls(cli)) {
1381                 /*
1382                  * Can't use sync call while an async call is in flight
1383                  */
1384                 status = NT_STATUS_INVALID_PARAMETER;
1385                 goto fail;
1386         }
1387
1388         ev = event_context_init(frame);
1389         if (ev == NULL) {
1390                 status = NT_STATUS_NO_MEMORY;
1391                 goto fail;
1392         }
1393
1394         req = cli_close_send(frame, ev, cli, fnum);
1395         if (req == NULL) {
1396                 status = NT_STATUS_NO_MEMORY;
1397                 goto fail;
1398         }
1399
1400         if (!tevent_req_poll(req, ev)) {
1401                 status = map_nt_error_from_unix(errno);
1402                 goto fail;
1403         }
1404
1405         result = NT_STATUS_IS_OK(cli_close_recv(req));
1406  fail:
1407         TALLOC_FREE(frame);
1408         if (!NT_STATUS_IS_OK(status)) {
1409                 cli_set_error(cli, status);
1410         }
1411         return result;
1412 }
1413
1414 /****************************************************************************
1415  Truncate a file to a specified size
1416 ****************************************************************************/
1417
1418 bool cli_ftruncate(struct cli_state *cli, int fnum, uint64_t size)
1419 {
1420         unsigned int param_len = 6;
1421         unsigned int data_len = 8;
1422         uint16 setup = TRANSACT2_SETFILEINFO;
1423         char param[6];
1424         unsigned char data[8];
1425         char *rparam=NULL, *rdata=NULL;
1426         int saved_timeout = cli->timeout;
1427
1428         SSVAL(param,0,fnum);
1429         SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
1430         SSVAL(param,4,0);
1431
1432         SBVAL(data, 0, size);
1433
1434         if (!cli_send_trans(cli, SMBtrans2,
1435                             NULL,                    /* name */
1436                             -1, 0,                   /* fid, flags */
1437                             &setup, 1, 0,            /* setup, length, max */
1438                             param, param_len, 2,     /* param, length, max */
1439                             (char *)&data,  data_len,/* data, length, ... */
1440                             cli->max_xmit)) {        /* ... max */
1441                 cli->timeout = saved_timeout;
1442                 return False;
1443         }
1444
1445         if (!cli_receive_trans(cli, SMBtrans2,
1446                                 &rparam, &param_len,
1447                                 &rdata, &data_len)) {
1448                 cli->timeout = saved_timeout;
1449                 SAFE_FREE(rdata);
1450                 SAFE_FREE(rparam);
1451                 return False;
1452         }
1453
1454         cli->timeout = saved_timeout;
1455
1456         SAFE_FREE(rdata);
1457         SAFE_FREE(rparam);
1458
1459         return True;
1460 }
1461
1462
1463 /****************************************************************************
1464  send a lock with a specified locktype
1465  this is used for testing LOCKING_ANDX_CANCEL_LOCK
1466 ****************************************************************************/
1467
1468 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
1469                       uint32 offset, uint32 len,
1470                       int timeout, unsigned char locktype)
1471 {
1472         char *p;
1473         int saved_timeout = cli->timeout;
1474
1475         memset(cli->outbuf,'\0',smb_size);
1476         memset(cli->inbuf,'\0', smb_size);
1477
1478         cli_set_message(cli->outbuf,8,0,True);
1479
1480         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1481         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1482         cli_setup_packet(cli);
1483
1484         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1485         SSVAL(cli->outbuf,smb_vwv2,fnum);
1486         SCVAL(cli->outbuf,smb_vwv3,locktype);
1487         SIVALS(cli->outbuf, smb_vwv4, timeout);
1488         SSVAL(cli->outbuf,smb_vwv6,0);
1489         SSVAL(cli->outbuf,smb_vwv7,1);
1490
1491         p = smb_buf(cli->outbuf);
1492         SSVAL(p, 0, cli->pid);
1493         SIVAL(p, 2, offset);
1494         SIVAL(p, 6, len);
1495
1496         p += 10;
1497
1498         cli_setup_bcc(cli, p);
1499
1500         cli_send_smb(cli);
1501
1502         if (timeout != 0) {
1503                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1504         }
1505
1506         if (!cli_receive_smb(cli)) {
1507                 cli->timeout = saved_timeout;
1508                 return NT_STATUS_UNSUCCESSFUL;
1509         }
1510
1511         cli->timeout = saved_timeout;
1512
1513         return cli_nt_error(cli);
1514 }
1515
1516 /****************************************************************************
1517  Lock a file.
1518  note that timeout is in units of 2 milliseconds
1519 ****************************************************************************/
1520
1521 bool cli_lock(struct cli_state *cli, int fnum,
1522               uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
1523 {
1524         char *p;
1525         int saved_timeout = cli->timeout;
1526
1527         memset(cli->outbuf,'\0',smb_size);
1528         memset(cli->inbuf,'\0', smb_size);
1529
1530         cli_set_message(cli->outbuf,8,0,True);
1531
1532         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1533         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1534         cli_setup_packet(cli);
1535
1536         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1537         SSVAL(cli->outbuf,smb_vwv2,fnum);
1538         SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1539         SIVALS(cli->outbuf, smb_vwv4, timeout);
1540         SSVAL(cli->outbuf,smb_vwv6,0);
1541         SSVAL(cli->outbuf,smb_vwv7,1);
1542
1543         p = smb_buf(cli->outbuf);
1544         SSVAL(p, 0, cli->pid);
1545         SIVAL(p, 2, offset);
1546         SIVAL(p, 6, len);
1547
1548         p += 10;
1549
1550         cli_setup_bcc(cli, p);
1551
1552         cli_send_smb(cli);
1553
1554         if (timeout != 0) {
1555                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1556         }
1557
1558         if (!cli_receive_smb(cli)) {
1559                 cli->timeout = saved_timeout;
1560                 return False;
1561         }
1562
1563         cli->timeout = saved_timeout;
1564
1565         if (cli_is_error(cli)) {
1566                 return False;
1567         }
1568
1569         return True;
1570 }
1571
1572 /****************************************************************************
1573  Unlock a file.
1574 ****************************************************************************/
1575
1576 bool cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
1577 {
1578         char *p;
1579
1580         memset(cli->outbuf,'\0',smb_size);
1581         memset(cli->inbuf,'\0',smb_size);
1582
1583         cli_set_message(cli->outbuf,8,0,True);
1584
1585         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1586         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1587         cli_setup_packet(cli);
1588
1589         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1590         SSVAL(cli->outbuf,smb_vwv2,fnum);
1591         SCVAL(cli->outbuf,smb_vwv3,0);
1592         SIVALS(cli->outbuf, smb_vwv4, 0);
1593         SSVAL(cli->outbuf,smb_vwv6,1);
1594         SSVAL(cli->outbuf,smb_vwv7,0);
1595
1596         p = smb_buf(cli->outbuf);
1597         SSVAL(p, 0, cli->pid);
1598         SIVAL(p, 2, offset);
1599         SIVAL(p, 6, len);
1600         p += 10;
1601         cli_setup_bcc(cli, p);
1602         cli_send_smb(cli);
1603         if (!cli_receive_smb(cli)) {
1604                 return False;
1605         }
1606
1607         if (cli_is_error(cli)) {
1608                 return False;
1609         }
1610
1611         return True;
1612 }
1613
1614 /****************************************************************************
1615  Lock a file with 64 bit offsets.
1616 ****************************************************************************/
1617
1618 bool cli_lock64(struct cli_state *cli, int fnum,
1619                 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
1620 {
1621         char *p;
1622         int saved_timeout = cli->timeout;
1623         int ltype;
1624
1625         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1626                 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1627         }
1628
1629         ltype = (lock_type == READ_LOCK? 1 : 0);
1630         ltype |= LOCKING_ANDX_LARGE_FILES;
1631
1632         memset(cli->outbuf,'\0',smb_size);
1633         memset(cli->inbuf,'\0', smb_size);
1634
1635         cli_set_message(cli->outbuf,8,0,True);
1636
1637         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1638         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1639         cli_setup_packet(cli);
1640
1641         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1642         SSVAL(cli->outbuf,smb_vwv2,fnum);
1643         SCVAL(cli->outbuf,smb_vwv3,ltype);
1644         SIVALS(cli->outbuf, smb_vwv4, timeout);
1645         SSVAL(cli->outbuf,smb_vwv6,0);
1646         SSVAL(cli->outbuf,smb_vwv7,1);
1647
1648         p = smb_buf(cli->outbuf);
1649         SIVAL(p, 0, cli->pid);
1650         SOFF_T_R(p, 4, offset);
1651         SOFF_T_R(p, 12, len);
1652         p += 20;
1653
1654         cli_setup_bcc(cli, p);
1655         cli_send_smb(cli);
1656
1657         if (timeout != 0) {
1658                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1659         }
1660
1661         if (!cli_receive_smb(cli)) {
1662                 cli->timeout = saved_timeout;
1663                 return False;
1664         }
1665
1666         cli->timeout = saved_timeout;
1667
1668         if (cli_is_error(cli)) {
1669                 return False;
1670         }
1671
1672         return True;
1673 }
1674
1675 /****************************************************************************
1676  Unlock a file with 64 bit offsets.
1677 ****************************************************************************/
1678
1679 bool cli_unlock64(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1680 {
1681         char *p;
1682
1683         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1684                 return cli_unlock(cli, fnum, offset, len);
1685         }
1686
1687         memset(cli->outbuf,'\0',smb_size);
1688         memset(cli->inbuf,'\0',smb_size);
1689
1690         cli_set_message(cli->outbuf,8,0,True);
1691
1692         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1693         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1694         cli_setup_packet(cli);
1695
1696         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1697         SSVAL(cli->outbuf,smb_vwv2,fnum);
1698         SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1699         SIVALS(cli->outbuf, smb_vwv4, 0);
1700         SSVAL(cli->outbuf,smb_vwv6,1);
1701         SSVAL(cli->outbuf,smb_vwv7,0);
1702
1703         p = smb_buf(cli->outbuf);
1704         SIVAL(p, 0, cli->pid);
1705         SOFF_T_R(p, 4, offset);
1706         SOFF_T_R(p, 12, len);
1707         p += 20;
1708         cli_setup_bcc(cli, p);
1709         cli_send_smb(cli);
1710         if (!cli_receive_smb(cli)) {
1711                 return False;
1712         }
1713
1714         if (cli_is_error(cli)) {
1715                 return False;
1716         }
1717
1718         return True;
1719 }
1720
1721 /****************************************************************************
1722  Get/unlock a POSIX lock on a file - internal function.
1723 ****************************************************************************/
1724
1725 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
1726                 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
1727 {
1728         unsigned int param_len = 4;
1729         unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1730         uint16 setup = TRANSACT2_SETFILEINFO;
1731         char param[4];
1732         unsigned char data[POSIX_LOCK_DATA_SIZE];
1733         char *rparam=NULL, *rdata=NULL;
1734         int saved_timeout = cli->timeout;
1735
1736         SSVAL(param,0,fnum);
1737         SSVAL(param,2,SMB_SET_POSIX_LOCK);
1738
1739         switch (lock_type) {
1740                 case READ_LOCK:
1741                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1742                         break;
1743                 case WRITE_LOCK:
1744                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1745                         break;
1746                 case UNLOCK_LOCK:
1747                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1748                         break;
1749                 default:
1750                         return False;
1751         }
1752
1753         if (wait_lock) {
1754                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1755                 cli->timeout = 0x7FFFFFFF;
1756         } else {
1757                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1758         }
1759
1760         SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1761         SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1762         SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
1763
1764         if (!cli_send_trans(cli, SMBtrans2,
1765                         NULL,                        /* name */
1766                         -1, 0,                          /* fid, flags */
1767                         &setup, 1, 0,                   /* setup, length, max */
1768                         param, param_len, 2,            /* param, length, max */
1769                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
1770                         )) {
1771                 cli->timeout = saved_timeout;
1772                 return False;
1773         }
1774
1775         if (!cli_receive_trans(cli, SMBtrans2,
1776                                 &rparam, &param_len,
1777                                 &rdata, &data_len)) {
1778                 cli->timeout = saved_timeout;
1779                 SAFE_FREE(rdata);
1780                 SAFE_FREE(rparam);
1781                 return False;
1782         }
1783
1784         cli->timeout = saved_timeout;
1785
1786         SAFE_FREE(rdata);
1787         SAFE_FREE(rparam);
1788
1789         return True;
1790 }
1791
1792 /****************************************************************************
1793  POSIX Lock a file.
1794 ****************************************************************************/
1795
1796 bool cli_posix_lock(struct cli_state *cli, int fnum,
1797                         uint64_t offset, uint64_t len,
1798                         bool wait_lock, enum brl_type lock_type)
1799 {
1800         if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
1801                 return False;
1802         }
1803         return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
1804 }
1805
1806 /****************************************************************************
1807  POSIX Unlock a file.
1808 ****************************************************************************/
1809
1810 bool cli_posix_unlock(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1811 {
1812         return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
1813 }
1814
1815 /****************************************************************************
1816  POSIX Get any lock covering a file.
1817 ****************************************************************************/
1818
1819 bool cli_posix_getlock(struct cli_state *cli, int fnum, uint64_t *poffset, uint64_t *plen)
1820 {
1821         return True;
1822 }
1823
1824 /****************************************************************************
1825  Do a SMBgetattrE call.
1826 ****************************************************************************/
1827
1828 bool cli_getattrE(struct cli_state *cli, int fd,
1829                   uint16 *attr, SMB_OFF_T *size,
1830                   time_t *change_time,
1831                   time_t *access_time,
1832                   time_t *write_time)
1833 {
1834         memset(cli->outbuf,'\0',smb_size);
1835         memset(cli->inbuf,'\0',smb_size);
1836
1837         cli_set_message(cli->outbuf,1,0,True);
1838
1839         SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1840         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1841         cli_setup_packet(cli);
1842
1843         SSVAL(cli->outbuf,smb_vwv0,fd);
1844
1845         cli_send_smb(cli);
1846         if (!cli_receive_smb(cli)) {
1847                 return False;
1848         }
1849
1850         if (cli_is_error(cli)) {
1851                 return False;
1852         }
1853
1854         if (size) {
1855                 *size = IVAL(cli->inbuf, smb_vwv6);
1856         }
1857
1858         if (attr) {
1859                 *attr = SVAL(cli->inbuf,smb_vwv10);
1860         }
1861
1862         if (change_time) {
1863                 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
1864         }
1865
1866         if (access_time) {
1867                 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
1868         }
1869
1870         if (write_time) {
1871                 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
1872         }
1873
1874         return True;
1875 }
1876
1877 /****************************************************************************
1878  Do a SMBgetatr call
1879 ****************************************************************************/
1880
1881 bool cli_getatr(struct cli_state *cli, const char *fname,
1882                 uint16 *attr, SMB_OFF_T *size, time_t *write_time)
1883 {
1884         char *p;
1885
1886         memset(cli->outbuf,'\0',smb_size);
1887         memset(cli->inbuf,'\0',smb_size);
1888
1889         cli_set_message(cli->outbuf,0,0,True);
1890
1891         SCVAL(cli->outbuf,smb_com,SMBgetatr);
1892         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1893         cli_setup_packet(cli);
1894
1895         p = smb_buf(cli->outbuf);
1896         *p++ = 4;
1897         p += clistr_push(cli, p, fname,
1898                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1899
1900         cli_setup_bcc(cli, p);
1901
1902         cli_send_smb(cli);
1903         if (!cli_receive_smb(cli)) {
1904                 return False;
1905         }
1906
1907         if (cli_is_error(cli)) {
1908                 return False;
1909         }
1910
1911         if (size) {
1912                 *size = IVAL(cli->inbuf, smb_vwv3);
1913         }
1914
1915         if (write_time) {
1916                 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
1917         }
1918
1919         if (attr) {
1920                 *attr = SVAL(cli->inbuf,smb_vwv0);
1921         }
1922
1923         return True;
1924 }
1925
1926 /****************************************************************************
1927  Do a SMBsetattrE call.
1928 ****************************************************************************/
1929
1930 bool cli_setattrE(struct cli_state *cli, int fd,
1931                   time_t change_time,
1932                   time_t access_time,
1933                   time_t write_time)
1934
1935 {
1936         char *p;
1937
1938         memset(cli->outbuf,'\0',smb_size);
1939         memset(cli->inbuf,'\0',smb_size);
1940
1941         cli_set_message(cli->outbuf,7,0,True);
1942
1943         SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1944         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1945         cli_setup_packet(cli);
1946
1947         SSVAL(cli->outbuf,smb_vwv0, fd);
1948         cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
1949         cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
1950         cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
1951
1952         p = smb_buf(cli->outbuf);
1953         *p++ = 4;
1954
1955         cli_setup_bcc(cli, p);
1956
1957         cli_send_smb(cli);
1958         if (!cli_receive_smb(cli)) {
1959                 return False;
1960         }
1961
1962         if (cli_is_error(cli)) {
1963                 return False;
1964         }
1965
1966         return True;
1967 }
1968
1969 /****************************************************************************
1970  Do a SMBsetatr call.
1971 ****************************************************************************/
1972
1973 bool cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1974 {
1975         char *p;
1976
1977         memset(cli->outbuf,'\0',smb_size);
1978         memset(cli->inbuf,'\0',smb_size);
1979
1980         cli_set_message(cli->outbuf,8,0,True);
1981
1982         SCVAL(cli->outbuf,smb_com,SMBsetatr);
1983         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1984         cli_setup_packet(cli);
1985
1986         SSVAL(cli->outbuf,smb_vwv0, attr);
1987         cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
1988
1989         p = smb_buf(cli->outbuf);
1990         *p++ = 4;
1991         p += clistr_push(cli, p, fname,
1992                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1993         *p++ = 4;
1994
1995         cli_setup_bcc(cli, p);
1996
1997         cli_send_smb(cli);
1998         if (!cli_receive_smb(cli)) {
1999                 return False;
2000         }
2001
2002         if (cli_is_error(cli)) {
2003                 return False;
2004         }
2005
2006         return True;
2007 }
2008
2009 /****************************************************************************
2010  Check for existance of a dir.
2011 ****************************************************************************/
2012
2013 bool cli_chkpath(struct cli_state *cli, const char *path)
2014 {
2015         char *path2 = NULL;
2016         char *p;
2017         TALLOC_CTX *frame = talloc_stackframe();
2018
2019         path2 = talloc_strdup(frame, path);
2020         if (!path2) {
2021                 TALLOC_FREE(frame);
2022                 return false;
2023         }
2024         trim_char(path2,'\0','\\');
2025         if (!*path2) {
2026                 path2 = talloc_strdup(frame, "\\");
2027                 if (!path2) {
2028                         TALLOC_FREE(frame);
2029                         return false;
2030                 }
2031         }
2032
2033         memset(cli->outbuf,'\0',smb_size);
2034         cli_set_message(cli->outbuf,0,0,True);
2035         SCVAL(cli->outbuf,smb_com,SMBcheckpath);
2036         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2037         cli_setup_packet(cli);
2038         p = smb_buf(cli->outbuf);
2039         *p++ = 4;
2040         p += clistr_push(cli, p, path2,
2041                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2042
2043         cli_setup_bcc(cli, p);
2044
2045         cli_send_smb(cli);
2046         if (!cli_receive_smb(cli)) {
2047                 TALLOC_FREE(frame);
2048                 return False;
2049         }
2050
2051         TALLOC_FREE(frame);
2052
2053         if (cli_is_error(cli)) return False;
2054
2055         return True;
2056 }
2057
2058 /****************************************************************************
2059  Query disk space.
2060 ****************************************************************************/
2061
2062 bool cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
2063 {
2064         memset(cli->outbuf,'\0',smb_size);
2065         cli_set_message(cli->outbuf,0,0,True);
2066         SCVAL(cli->outbuf,smb_com,SMBdskattr);
2067         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2068         cli_setup_packet(cli);
2069
2070         cli_send_smb(cli);
2071         if (!cli_receive_smb(cli)) {
2072                 return False;
2073         }
2074
2075         *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
2076         *total = SVAL(cli->inbuf,smb_vwv0);
2077         *avail = SVAL(cli->inbuf,smb_vwv3);
2078
2079         return True;
2080 }
2081
2082 /****************************************************************************
2083  Create and open a temporary file.
2084 ****************************************************************************/
2085
2086 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
2087 {
2088         int len;
2089         char *p;
2090
2091         memset(cli->outbuf,'\0',smb_size);
2092         memset(cli->inbuf,'\0',smb_size);
2093
2094         cli_set_message(cli->outbuf,3,0,True);
2095
2096         SCVAL(cli->outbuf,smb_com,SMBctemp);
2097         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2098         cli_setup_packet(cli);
2099
2100         SSVAL(cli->outbuf,smb_vwv0,0);
2101         SIVALS(cli->outbuf,smb_vwv1,-1);
2102
2103         p = smb_buf(cli->outbuf);
2104         *p++ = 4;
2105         p += clistr_push(cli, p, path,
2106                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2107
2108         cli_setup_bcc(cli, p);
2109
2110         cli_send_smb(cli);
2111         if (!cli_receive_smb(cli)) {
2112                 return -1;
2113         }
2114
2115         if (cli_is_error(cli)) {
2116                 return -1;
2117         }
2118
2119         /* despite the spec, the result has a -1, followed by
2120            length, followed by name */
2121         p = smb_buf(cli->inbuf);
2122         p += 4;
2123         len = smb_buflen(cli->inbuf) - 4;
2124         if (len <= 0 || len > PATH_MAX) return -1;
2125
2126         if (tmp_path) {
2127                 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
2128                 if (!path2) {
2129                         return -1;
2130                 }
2131                 clistr_pull(cli->inbuf, path2, p,
2132                             len+1, len, STR_ASCII);
2133                 *tmp_path = path2;
2134         }
2135
2136         return SVAL(cli->inbuf,smb_vwv0);
2137 }
2138
2139 /*
2140    send a raw ioctl - used by the torture code
2141 */
2142 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
2143 {
2144         memset(cli->outbuf,'\0',smb_size);
2145         memset(cli->inbuf,'\0',smb_size);
2146
2147         cli_set_message(cli->outbuf, 3, 0, True);
2148         SCVAL(cli->outbuf,smb_com,SMBioctl);
2149         cli_setup_packet(cli);
2150
2151         SSVAL(cli->outbuf, smb_vwv0, fnum);
2152         SSVAL(cli->outbuf, smb_vwv1, code>>16);
2153         SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
2154
2155         cli_send_smb(cli);
2156         if (!cli_receive_smb(cli)) {
2157                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2158         }
2159
2160         if (cli_is_error(cli)) {
2161                 return cli_nt_error(cli);
2162         }
2163
2164         *blob = data_blob_null;
2165
2166         return NT_STATUS_OK;
2167 }
2168
2169 /*********************************************************
2170  Set an extended attribute utility fn.
2171 *********************************************************/
2172
2173 static bool cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
2174                         const char *ea_name, const char *ea_val, size_t ea_len)
2175 {
2176         unsigned int data_len = 0;
2177         char *data = NULL;
2178         char *rparam=NULL, *rdata=NULL;
2179         char *p;
2180         size_t ea_namelen = strlen(ea_name);
2181
2182         if (ea_namelen == 0 && ea_len == 0) {
2183                 data_len = 4;
2184                 data = (char *)SMB_MALLOC(data_len);
2185                 if (!data) {
2186                         return False;
2187                 }
2188                 p = data;
2189                 SIVAL(p,0,data_len);
2190         } else {
2191                 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
2192                 data = (char *)SMB_MALLOC(data_len);
2193                 if (!data) {
2194                         return False;
2195                 }
2196                 p = data;
2197                 SIVAL(p,0,data_len);
2198                 p += 4;
2199                 SCVAL(p, 0, 0); /* EA flags. */
2200                 SCVAL(p, 1, ea_namelen);
2201                 SSVAL(p, 2, ea_len);
2202                 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
2203                 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
2204         }
2205
2206         if (!cli_send_trans(cli, SMBtrans2,
2207                         NULL,                        /* name */
2208                         -1, 0,                          /* fid, flags */
2209                         &setup, 1, 0,                   /* setup, length, max */
2210                         param, param_len, 2,            /* param, length, max */
2211                         data,  data_len, cli->max_xmit /* data, length, max */
2212                         )) {
2213                 SAFE_FREE(data);
2214                 return False;
2215         }
2216
2217         if (!cli_receive_trans(cli, SMBtrans2,
2218                         &rparam, &param_len,
2219                         &rdata, &data_len)) {
2220                         SAFE_FREE(data);
2221                 return false;
2222         }
2223
2224         SAFE_FREE(data);
2225         SAFE_FREE(rdata);
2226         SAFE_FREE(rparam);
2227
2228         return True;
2229 }
2230
2231 /*********************************************************
2232  Set an extended attribute on a pathname.
2233 *********************************************************/
2234
2235 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
2236 {
2237         uint16 setup = TRANSACT2_SETPATHINFO;
2238         unsigned int param_len = 0;
2239         char *param;
2240         size_t srclen = 2*(strlen(path)+1);
2241         char *p;
2242         bool ret;
2243
2244         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2245         if (!param) {
2246                 return false;
2247         }
2248         memset(param, '\0', 6);
2249         SSVAL(param,0,SMB_INFO_SET_EA);
2250         p = &param[6];
2251
2252         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2253         param_len = PTR_DIFF(p, param);
2254
2255         ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
2256         SAFE_FREE(param);
2257         return ret;
2258 }
2259
2260 /*********************************************************
2261  Set an extended attribute on an fnum.
2262 *********************************************************/
2263
2264 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
2265 {
2266         char param[6];
2267         uint16 setup = TRANSACT2_SETFILEINFO;
2268
2269         memset(param, 0, 6);
2270         SSVAL(param,0,fnum);
2271         SSVAL(param,2,SMB_INFO_SET_EA);
2272
2273         return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
2274 }
2275
2276 /*********************************************************
2277  Get an extended attribute list utility fn.
2278 *********************************************************/
2279
2280 static bool cli_get_ea_list(struct cli_state *cli,
2281                 uint16 setup, char *param, unsigned int param_len,
2282                 TALLOC_CTX *ctx,
2283                 size_t *pnum_eas,
2284                 struct ea_struct **pea_list)
2285 {
2286         unsigned int data_len = 0;
2287         unsigned int rparam_len, rdata_len;
2288         char *rparam=NULL, *rdata=NULL;
2289         char *p;
2290         size_t ea_size;
2291         size_t num_eas;
2292         bool ret = False;
2293         struct ea_struct *ea_list;
2294
2295         *pnum_eas = 0;
2296         if (pea_list) {
2297                 *pea_list = NULL;
2298         }
2299
2300         if (!cli_send_trans(cli, SMBtrans2,
2301                         NULL,           /* Name */
2302                         -1, 0,          /* fid, flags */
2303                         &setup, 1, 0,   /* setup, length, max */
2304                         param, param_len, 10, /* param, length, max */
2305                         NULL, data_len, cli->max_xmit /* data, length, max */
2306                                 )) {
2307                 return False;
2308         }
2309
2310         if (!cli_receive_trans(cli, SMBtrans2,
2311                         &rparam, &rparam_len,
2312                         &rdata, &rdata_len)) {
2313                 return False;
2314         }
2315
2316         if (!rdata || rdata_len < 4) {
2317                 goto out;
2318         }
2319
2320         ea_size = (size_t)IVAL(rdata,0);
2321         if (ea_size > rdata_len) {
2322                 goto out;
2323         }
2324
2325         if (ea_size == 0) {
2326                 /* No EA's present. */
2327                 ret = True;
2328                 goto out;
2329         }
2330
2331         p = rdata + 4;
2332         ea_size -= 4;
2333
2334         /* Validate the EA list and count it. */
2335         for (num_eas = 0; ea_size >= 4; num_eas++) {
2336                 unsigned int ea_namelen = CVAL(p,1);
2337                 unsigned int ea_valuelen = SVAL(p,2);
2338                 if (ea_namelen == 0) {
2339                         goto out;
2340                 }
2341                 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
2342                         goto out;
2343                 }
2344                 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
2345                 p += 4 + ea_namelen + 1 + ea_valuelen;
2346         }
2347
2348         if (num_eas == 0) {
2349                 ret = True;
2350                 goto out;
2351         }
2352
2353         *pnum_eas = num_eas;
2354         if (!pea_list) {
2355                 /* Caller only wants number of EA's. */
2356                 ret = True;
2357                 goto out;
2358         }
2359
2360         ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
2361         if (!ea_list) {
2362                 goto out;
2363         }
2364
2365         ea_size = (size_t)IVAL(rdata,0);
2366         p = rdata + 4;
2367
2368         for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
2369                 struct ea_struct *ea = &ea_list[num_eas];
2370                 fstring unix_ea_name;
2371                 unsigned int ea_namelen = CVAL(p,1);
2372                 unsigned int ea_valuelen = SVAL(p,2);
2373
2374                 ea->flags = CVAL(p,0);
2375                 unix_ea_name[0] = '\0';
2376                 pull_ascii_fstring(unix_ea_name, p + 4);
2377                 ea->name = talloc_strdup(ctx, unix_ea_name);
2378                 /* Ensure the value is null terminated (in case it's a string). */
2379                 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
2380                 if (!ea->value.data) {
2381                         goto out;
2382                 }
2383                 if (ea_valuelen) {
2384                         memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
2385                 }
2386                 ea->value.data[ea_valuelen] = 0;
2387                 ea->value.length--;
2388                 p += 4 + ea_namelen + 1 + ea_valuelen;
2389         }
2390
2391         *pea_list = ea_list;
2392         ret = True;
2393
2394  out :
2395
2396         SAFE_FREE(rdata);
2397         SAFE_FREE(rparam);
2398         return ret;
2399 }
2400
2401 /*********************************************************
2402  Get an extended attribute list from a pathname.
2403 *********************************************************/
2404
2405 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
2406                 TALLOC_CTX *ctx,
2407                 size_t *pnum_eas,
2408                 struct ea_struct **pea_list)
2409 {
2410         uint16 setup = TRANSACT2_QPATHINFO;
2411         unsigned int param_len = 0;
2412         char *param;
2413         char *p;
2414         size_t srclen = 2*(strlen(path)+1);
2415         bool ret;
2416
2417         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2418         if (!param) {
2419                 return false;
2420         }
2421         p = param;
2422         memset(p, 0, 6);
2423         SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
2424         p += 6;
2425         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2426         param_len = PTR_DIFF(p, param);
2427
2428         ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
2429         SAFE_FREE(param);
2430         return ret;
2431 }
2432
2433 /*********************************************************
2434  Get an extended attribute list from an fnum.
2435 *********************************************************/
2436
2437 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
2438                 TALLOC_CTX *ctx,
2439                 size_t *pnum_eas,
2440                 struct ea_struct **pea_list)
2441 {
2442         uint16 setup = TRANSACT2_QFILEINFO;
2443         char param[6];
2444
2445         memset(param, 0, 6);
2446         SSVAL(param,0,fnum);
2447         SSVAL(param,2,SMB_INFO_SET_EA);
2448
2449         return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
2450 }
2451
2452 /****************************************************************************
2453  Convert open "flags" arg to uint32 on wire.
2454 ****************************************************************************/
2455
2456 static uint32 open_flags_to_wire(int flags)
2457 {
2458         int open_mode = flags & O_ACCMODE;
2459         uint32 ret = 0;
2460
2461         switch (open_mode) {
2462                 case O_WRONLY:
2463                         ret |= SMB_O_WRONLY;
2464                         break;
2465                 case O_RDWR:
2466                         ret |= SMB_O_RDWR;
2467                         break;
2468                 default:
2469                 case O_RDONLY:
2470                         ret |= SMB_O_RDONLY;
2471                         break;
2472         }
2473
2474         if (flags & O_CREAT) {
2475                 ret |= SMB_O_CREAT;
2476         }
2477         if (flags & O_EXCL) {
2478                 ret |= SMB_O_EXCL;
2479         }
2480         if (flags & O_TRUNC) {
2481                 ret |= SMB_O_TRUNC;
2482         }
2483 #if defined(O_SYNC)
2484         if (flags & O_SYNC) {
2485                 ret |= SMB_O_SYNC;
2486         }
2487 #endif /* O_SYNC */
2488         if (flags & O_APPEND) {
2489                 ret |= SMB_O_APPEND;
2490         }
2491 #if defined(O_DIRECT)
2492         if (flags & O_DIRECT) {
2493                 ret |= SMB_O_DIRECT;
2494         }
2495 #endif
2496 #if defined(O_DIRECTORY)
2497         if (flags & O_DIRECTORY) {
2498                 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2499                 ret |= SMB_O_DIRECTORY;
2500         }
2501 #endif
2502         return ret;
2503 }
2504
2505 /****************************************************************************
2506  Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
2507 ****************************************************************************/
2508
2509 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
2510 {
2511         unsigned int data_len = 0;
2512         unsigned int param_len = 0;
2513         uint16 setup = TRANSACT2_SETPATHINFO;
2514         char *param;
2515         char data[18];
2516         char *rparam=NULL, *rdata=NULL;
2517         char *p;
2518         int fnum = -1;
2519         uint32 wire_flags = open_flags_to_wire(flags);
2520         size_t srclen = 2*(strlen(fname)+1);
2521
2522         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2523         if (!param) {
2524                 return false;
2525         }
2526         memset(param, '\0', 6);
2527         SSVAL(param,0, SMB_POSIX_PATH_OPEN);
2528         p = &param[6];
2529
2530         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2531         param_len = PTR_DIFF(p, param);
2532
2533         if (is_dir) {
2534                 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2535                 wire_flags |= SMB_O_DIRECTORY;
2536         }
2537
2538         p = data;
2539         SIVAL(p,0,0); /* No oplock. */
2540         SIVAL(p,4,wire_flags);
2541         SIVAL(p,8,unix_perms_to_wire(mode));
2542         SIVAL(p,12,0); /* Top bits of perms currently undefined. */
2543         SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
2544
2545         data_len = 18;
2546
2547         if (!cli_send_trans(cli, SMBtrans2,
2548                         NULL,                        /* name */
2549                         -1, 0,                          /* fid, flags */
2550                         &setup, 1, 0,                   /* setup, length, max */
2551                         param, param_len, 2,            /* param, length, max */
2552                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2553                         )) {
2554                 SAFE_FREE(param);
2555                 return -1;
2556         }
2557
2558         SAFE_FREE(param);
2559
2560         if (!cli_receive_trans(cli, SMBtrans2,
2561                 &rparam, &param_len,
2562                 &rdata, &data_len)) {
2563                         return -1;
2564         }
2565
2566         fnum = SVAL(rdata,2);
2567
2568         SAFE_FREE(rdata);
2569         SAFE_FREE(rparam);
2570
2571         return fnum;
2572 }
2573
2574 /****************************************************************************
2575  open - POSIX semantics.
2576 ****************************************************************************/
2577
2578 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
2579 {
2580         return cli_posix_open_internal(cli, fname, flags, mode, False);
2581 }
2582
2583 /****************************************************************************
2584  mkdir - POSIX semantics.
2585 ****************************************************************************/
2586
2587 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
2588 {
2589         return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
2590 }
2591
2592 /****************************************************************************
2593  unlink or rmdir - POSIX semantics.
2594 ****************************************************************************/
2595
2596 static bool cli_posix_unlink_internal(struct cli_state *cli, const char *fname, bool is_dir)
2597 {
2598         unsigned int data_len = 0;
2599         unsigned int param_len = 0;
2600         uint16 setup = TRANSACT2_SETPATHINFO;
2601         char *param;
2602         char data[2];
2603         char *rparam=NULL, *rdata=NULL;
2604         char *p;
2605         size_t srclen = 2*(strlen(fname)+1);
2606
2607         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2608         if (!param) {
2609                 return false;
2610         }
2611         memset(param, '\0', 6);
2612         SSVAL(param,0, SMB_POSIX_PATH_UNLINK);
2613         p = &param[6];
2614
2615         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2616         param_len = PTR_DIFF(p, param);
2617
2618         SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
2619                         SMB_POSIX_UNLINK_FILE_TARGET);
2620         data_len = 2;
2621
2622         if (!cli_send_trans(cli, SMBtrans2,
2623                         NULL,                        /* name */
2624                         -1, 0,                          /* fid, flags */
2625                         &setup, 1, 0,                   /* setup, length, max */
2626                         param, param_len, 2,            /* param, length, max */
2627                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2628                         )) {
2629                 SAFE_FREE(param);
2630                 return False;
2631         }
2632
2633         SAFE_FREE(param);
2634
2635         if (!cli_receive_trans(cli, SMBtrans2,
2636                 &rparam, &param_len,
2637                 &rdata, &data_len)) {
2638                         return False;
2639         }
2640
2641         SAFE_FREE(rdata);
2642         SAFE_FREE(rparam);
2643
2644         return True;
2645 }
2646
2647 /****************************************************************************
2648  unlink - POSIX semantics.
2649 ****************************************************************************/
2650
2651 bool cli_posix_unlink(struct cli_state *cli, const char *fname)
2652 {
2653         return cli_posix_unlink_internal(cli, fname, False);
2654 }
2655
2656 /****************************************************************************
2657  rmdir - POSIX semantics.
2658 ****************************************************************************/
2659
2660 int cli_posix_rmdir(struct cli_state *cli, const char *fname)
2661 {
2662         return cli_posix_unlink_internal(cli, fname, True);
2663 }