Add async cli_close
[samba.git] / source3 / libsmb / clifile.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client file operations
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Jeremy Allison 2001-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                         (char *)&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 /****************************************************************************
600  Create a directory.
601 ****************************************************************************/
602
603 bool cli_mkdir(struct cli_state *cli, const char *dname)
604 {
605         char *p;
606
607         memset(cli->outbuf,'\0',smb_size);
608         memset(cli->inbuf,'\0',smb_size);
609
610         cli_set_message(cli->outbuf,0, 0, true);
611
612         SCVAL(cli->outbuf,smb_com,SMBmkdir);
613         SSVAL(cli->outbuf,smb_tid,cli->cnum);
614         cli_setup_packet(cli);
615
616         p = smb_buf(cli->outbuf);
617         *p++ = 4;
618         p += clistr_push(cli, p, dname,
619                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
620
621         cli_setup_bcc(cli, p);
622
623         cli_send_smb(cli);
624         if (!cli_receive_smb(cli)) {
625                 return False;
626         }
627
628         if (cli_is_error(cli)) {
629                 return False;
630         }
631
632         return True;
633 }
634
635 /****************************************************************************
636  Remove a directory.
637 ****************************************************************************/
638
639 bool cli_rmdir(struct cli_state *cli, const char *dname)
640 {
641         char *p;
642
643         memset(cli->outbuf,'\0',smb_size);
644         memset(cli->inbuf,'\0',smb_size);
645
646         cli_set_message(cli->outbuf,0, 0, true);
647
648         SCVAL(cli->outbuf,smb_com,SMBrmdir);
649         SSVAL(cli->outbuf,smb_tid,cli->cnum);
650         cli_setup_packet(cli);
651
652         p = smb_buf(cli->outbuf);
653         *p++ = 4;
654         p += clistr_push(cli, p, dname,
655                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
656
657         cli_setup_bcc(cli, p);
658
659         cli_send_smb(cli);
660         if (!cli_receive_smb(cli)) {
661                 return false;
662         }
663
664         if (cli_is_error(cli)) {
665                 return false;
666         }
667
668         return true;
669 }
670
671 /****************************************************************************
672  Set or clear the delete on close flag.
673 ****************************************************************************/
674
675 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, bool flag)
676 {
677         unsigned int data_len = 1;
678         unsigned int param_len = 6;
679         uint16 setup = TRANSACT2_SETFILEINFO;
680         char param[6];
681         unsigned char data;
682         char *rparam=NULL, *rdata=NULL;
683
684         memset(param, 0, param_len);
685         SSVAL(param,0,fnum);
686         SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
687
688         data = flag ? 1 : 0;
689
690         if (!cli_send_trans(cli, SMBtrans2,
691                         NULL,                        /* name */
692                         -1, 0,                          /* fid, flags */
693                         &setup, 1, 0,                   /* setup, length, max */
694                         param, param_len, 2,            /* param, length, max */
695                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
696                         )) {
697                 return false;
698         }
699
700         if (!cli_receive_trans(cli, SMBtrans2,
701                         &rparam, &param_len,
702                         &rdata, &data_len)) {
703                 return false;
704         }
705
706         SAFE_FREE(rdata);
707         SAFE_FREE(rparam);
708
709         return true;
710 }
711
712 /****************************************************************************
713  Open a file - exposing the full horror of the NT API :-).
714  Used in smbtorture.
715 ****************************************************************************/
716
717 int cli_nt_create_full(struct cli_state *cli, const char *fname,
718                        uint32 CreatFlags, uint32 DesiredAccess,
719                        uint32 FileAttributes, uint32 ShareAccess,
720                        uint32 CreateDisposition, uint32 CreateOptions,
721                        uint8 SecurityFlags)
722 {
723         char *p;
724         int len;
725
726         memset(cli->outbuf,'\0',smb_size);
727         memset(cli->inbuf,'\0',smb_size);
728
729         cli_set_message(cli->outbuf,24,0, true);
730
731         SCVAL(cli->outbuf,smb_com,SMBntcreateX);
732         SSVAL(cli->outbuf,smb_tid,cli->cnum);
733         cli_setup_packet(cli);
734
735         SSVAL(cli->outbuf,smb_vwv0,0xFF);
736         if (cli->use_oplocks)
737                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
738
739         SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
740         SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
741         SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
742         SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
743         SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
744         SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
745         SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
746         SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
747         SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecurityFlags);
748
749         p = smb_buf(cli->outbuf);
750         /* this alignment and termination is critical for netapp filers. Don't change */
751         p += clistr_align_out(cli, p, 0);
752         len = clistr_push(cli, p, fname,
753                         cli->bufsize - PTR_DIFF(p,cli->outbuf), 0);
754         p += len;
755         SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
756         /* sigh. this copes with broken netapp filer behaviour */
757         p += clistr_push(cli, p, "",
758                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
759
760         cli_setup_bcc(cli, p);
761
762         cli_send_smb(cli);
763         if (!cli_receive_smb(cli)) {
764                 return -1;
765         }
766
767         if (cli_is_error(cli)) {
768                 return -1;
769         }
770
771         return SVAL(cli->inbuf,smb_vwv2 + 1);
772 }
773
774 /****************************************************************************
775  Open a file.
776 ****************************************************************************/
777
778 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
779 {
780         return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
781                                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
782 }
783
784 /****************************************************************************
785  Open a file
786  WARNING: if you open with O_WRONLY then getattrE won't work!
787 ****************************************************************************/
788
789 int cli_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
790 {
791         char *p;
792         unsigned openfn=0;
793         unsigned accessmode=0;
794
795         if (flags & O_CREAT)
796                 openfn |= (1<<4);
797         if (!(flags & O_EXCL)) {
798                 if (flags & O_TRUNC)
799                         openfn |= (1<<1);
800                 else
801                         openfn |= (1<<0);
802         }
803
804         accessmode = (share_mode<<4);
805
806         if ((flags & O_ACCMODE) == O_RDWR) {
807                 accessmode |= 2;
808         } else if ((flags & O_ACCMODE) == O_WRONLY) {
809                 accessmode |= 1;
810         }
811
812 #if defined(O_SYNC)
813         if ((flags & O_SYNC) == O_SYNC) {
814                 accessmode |= (1<<14);
815         }
816 #endif /* O_SYNC */
817
818         if (share_mode == DENY_FCB) {
819                 accessmode = 0xFF;
820         }
821
822         memset(cli->outbuf,'\0',smb_size);
823         memset(cli->inbuf,'\0',smb_size);
824
825         cli_set_message(cli->outbuf,15,0, true);
826
827         SCVAL(cli->outbuf,smb_com,SMBopenX);
828         SSVAL(cli->outbuf,smb_tid,cli->cnum);
829         cli_setup_packet(cli);
830
831         SSVAL(cli->outbuf,smb_vwv0,0xFF);
832         SSVAL(cli->outbuf,smb_vwv2,0);  /* no additional info */
833         SSVAL(cli->outbuf,smb_vwv3,accessmode);
834         SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
835         SSVAL(cli->outbuf,smb_vwv5,0);
836         SSVAL(cli->outbuf,smb_vwv8,openfn);
837
838         if (cli->use_oplocks) {
839                 /* if using oplocks then ask for a batch oplock via
840                    core and extended methods */
841                 SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
842                         FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
843                 SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
844         }
845
846         p = smb_buf(cli->outbuf);
847         p += clistr_push(cli, p, fname,
848                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
849
850         cli_setup_bcc(cli, p);
851
852         cli_send_smb(cli);
853         if (!cli_receive_smb(cli)) {
854                 return -1;
855         }
856
857         if (cli_is_error(cli)) {
858                 return -1;
859         }
860
861         return SVAL(cli->inbuf,smb_vwv2);
862 }
863
864 /****************************************************************************
865  Close a file.
866 ****************************************************************************/
867
868 struct async_req *cli_close_send(TALLOC_CTX *mem_ctx, struct cli_state *cli,
869                                  int fnum)
870 {
871         uint16_t vwv[3];
872
873         SSVAL(vwv+0, 0, fnum);
874         SIVALS(vwv+1, 0, -1);
875
876         return cli_request_send(mem_ctx, cli, SMBclose, 0, 3, vwv, 0, NULL);
877 }
878
879 NTSTATUS cli_close_recv(struct async_req *req)
880 {
881         struct cli_request *cli_req = cli_request_get(req);
882
883         SMB_ASSERT(req->state >= ASYNC_REQ_DONE);
884         if (req->state == ASYNC_REQ_ERROR) {
885                 return req->status;
886         }
887
888         return cli_pull_error(cli_req->inbuf);
889 }
890
891 bool cli_close(struct cli_state *cli, int fnum)
892 {
893         TALLOC_CTX *frame = talloc_stackframe();
894         struct async_req *req;
895         bool result = false;
896
897         if (cli_tmp_event_ctx(frame, cli) == NULL) {
898                 goto fail;
899         }
900
901         req = cli_close_send(frame, cli, fnum);
902         if (req == NULL) {
903                 goto fail;
904         }
905
906         while (req->state < ASYNC_REQ_DONE) {
907                 event_loop_once(cli->event_ctx);
908         }
909
910         result = NT_STATUS_IS_OK(cli_close_recv(req));
911  fail:
912         TALLOC_FREE(frame);
913         return result;
914 }
915
916 /****************************************************************************
917  Truncate a file to a specified size
918 ****************************************************************************/
919
920 bool cli_ftruncate(struct cli_state *cli, int fnum, uint64_t size)
921 {
922         unsigned int param_len = 6;
923         unsigned int data_len = 8;
924         uint16 setup = TRANSACT2_SETFILEINFO;
925         char param[6];
926         unsigned char data[8];
927         char *rparam=NULL, *rdata=NULL;
928         int saved_timeout = cli->timeout;
929
930         SSVAL(param,0,fnum);
931         SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
932         SSVAL(param,4,0);
933
934         SBVAL(data, 0, size);
935
936         if (!cli_send_trans(cli, SMBtrans2,
937                             NULL,                    /* name */
938                             -1, 0,                   /* fid, flags */
939                             &setup, 1, 0,            /* setup, length, max */
940                             param, param_len, 2,     /* param, length, max */
941                             (char *)&data,  data_len,/* data, length, ... */
942                             cli->max_xmit)) {        /* ... max */
943                 cli->timeout = saved_timeout;
944                 return False;
945         }
946
947         if (!cli_receive_trans(cli, SMBtrans2,
948                                 &rparam, &param_len,
949                                 &rdata, &data_len)) {
950                 cli->timeout = saved_timeout;
951                 SAFE_FREE(rdata);
952                 SAFE_FREE(rparam);
953                 return False;
954         }
955
956         cli->timeout = saved_timeout;
957
958         SAFE_FREE(rdata);
959         SAFE_FREE(rparam);
960
961         return True;
962 }
963
964
965 /****************************************************************************
966  send a lock with a specified locktype
967  this is used for testing LOCKING_ANDX_CANCEL_LOCK
968 ****************************************************************************/
969
970 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
971                       uint32 offset, uint32 len,
972                       int timeout, unsigned char locktype)
973 {
974         char *p;
975         int saved_timeout = cli->timeout;
976
977         memset(cli->outbuf,'\0',smb_size);
978         memset(cli->inbuf,'\0', smb_size);
979
980         cli_set_message(cli->outbuf,8,0,True);
981
982         SCVAL(cli->outbuf,smb_com,SMBlockingX);
983         SSVAL(cli->outbuf,smb_tid,cli->cnum);
984         cli_setup_packet(cli);
985
986         SCVAL(cli->outbuf,smb_vwv0,0xFF);
987         SSVAL(cli->outbuf,smb_vwv2,fnum);
988         SCVAL(cli->outbuf,smb_vwv3,locktype);
989         SIVALS(cli->outbuf, smb_vwv4, timeout);
990         SSVAL(cli->outbuf,smb_vwv6,0);
991         SSVAL(cli->outbuf,smb_vwv7,1);
992
993         p = smb_buf(cli->outbuf);
994         SSVAL(p, 0, cli->pid);
995         SIVAL(p, 2, offset);
996         SIVAL(p, 6, len);
997
998         p += 10;
999
1000         cli_setup_bcc(cli, p);
1001
1002         cli_send_smb(cli);
1003
1004         if (timeout != 0) {
1005                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1006         }
1007
1008         if (!cli_receive_smb(cli)) {
1009                 cli->timeout = saved_timeout;
1010                 return NT_STATUS_UNSUCCESSFUL;
1011         }
1012
1013         cli->timeout = saved_timeout;
1014
1015         return cli_nt_error(cli);
1016 }
1017
1018 /****************************************************************************
1019  Lock a file.
1020  note that timeout is in units of 2 milliseconds
1021 ****************************************************************************/
1022
1023 bool cli_lock(struct cli_state *cli, int fnum,
1024               uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
1025 {
1026         char *p;
1027         int saved_timeout = cli->timeout;
1028
1029         memset(cli->outbuf,'\0',smb_size);
1030         memset(cli->inbuf,'\0', smb_size);
1031
1032         cli_set_message(cli->outbuf,8,0,True);
1033
1034         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1035         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1036         cli_setup_packet(cli);
1037
1038         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1039         SSVAL(cli->outbuf,smb_vwv2,fnum);
1040         SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1041         SIVALS(cli->outbuf, smb_vwv4, timeout);
1042         SSVAL(cli->outbuf,smb_vwv6,0);
1043         SSVAL(cli->outbuf,smb_vwv7,1);
1044
1045         p = smb_buf(cli->outbuf);
1046         SSVAL(p, 0, cli->pid);
1047         SIVAL(p, 2, offset);
1048         SIVAL(p, 6, len);
1049
1050         p += 10;
1051
1052         cli_setup_bcc(cli, p);
1053
1054         cli_send_smb(cli);
1055
1056         if (timeout != 0) {
1057                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1058         }
1059
1060         if (!cli_receive_smb(cli)) {
1061                 cli->timeout = saved_timeout;
1062                 return False;
1063         }
1064
1065         cli->timeout = saved_timeout;
1066
1067         if (cli_is_error(cli)) {
1068                 return False;
1069         }
1070
1071         return True;
1072 }
1073
1074 /****************************************************************************
1075  Unlock a file.
1076 ****************************************************************************/
1077
1078 bool cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
1079 {
1080         char *p;
1081
1082         memset(cli->outbuf,'\0',smb_size);
1083         memset(cli->inbuf,'\0',smb_size);
1084
1085         cli_set_message(cli->outbuf,8,0,True);
1086
1087         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1088         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1089         cli_setup_packet(cli);
1090
1091         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1092         SSVAL(cli->outbuf,smb_vwv2,fnum);
1093         SCVAL(cli->outbuf,smb_vwv3,0);
1094         SIVALS(cli->outbuf, smb_vwv4, 0);
1095         SSVAL(cli->outbuf,smb_vwv6,1);
1096         SSVAL(cli->outbuf,smb_vwv7,0);
1097
1098         p = smb_buf(cli->outbuf);
1099         SSVAL(p, 0, cli->pid);
1100         SIVAL(p, 2, offset);
1101         SIVAL(p, 6, len);
1102         p += 10;
1103         cli_setup_bcc(cli, p);
1104         cli_send_smb(cli);
1105         if (!cli_receive_smb(cli)) {
1106                 return False;
1107         }
1108
1109         if (cli_is_error(cli)) {
1110                 return False;
1111         }
1112
1113         return True;
1114 }
1115
1116 /****************************************************************************
1117  Lock a file with 64 bit offsets.
1118 ****************************************************************************/
1119
1120 bool cli_lock64(struct cli_state *cli, int fnum,
1121                 SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
1122 {
1123         char *p;
1124         int saved_timeout = cli->timeout;
1125         int ltype;
1126
1127         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1128                 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1129         }
1130
1131         ltype = (lock_type == READ_LOCK? 1 : 0);
1132         ltype |= LOCKING_ANDX_LARGE_FILES;
1133
1134         memset(cli->outbuf,'\0',smb_size);
1135         memset(cli->inbuf,'\0', smb_size);
1136
1137         cli_set_message(cli->outbuf,8,0,True);
1138
1139         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1140         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1141         cli_setup_packet(cli);
1142
1143         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1144         SSVAL(cli->outbuf,smb_vwv2,fnum);
1145         SCVAL(cli->outbuf,smb_vwv3,ltype);
1146         SIVALS(cli->outbuf, smb_vwv4, timeout);
1147         SSVAL(cli->outbuf,smb_vwv6,0);
1148         SSVAL(cli->outbuf,smb_vwv7,1);
1149
1150         p = smb_buf(cli->outbuf);
1151         SIVAL(p, 0, cli->pid);
1152         SOFF_T_R(p, 4, offset);
1153         SOFF_T_R(p, 12, len);
1154         p += 20;
1155
1156         cli_setup_bcc(cli, p);
1157         cli_send_smb(cli);
1158
1159         if (timeout != 0) {
1160                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1161         }
1162
1163         if (!cli_receive_smb(cli)) {
1164                 cli->timeout = saved_timeout;
1165                 return False;
1166         }
1167
1168         cli->timeout = saved_timeout;
1169
1170         if (cli_is_error(cli)) {
1171                 return False;
1172         }
1173
1174         return True;
1175 }
1176
1177 /****************************************************************************
1178  Unlock a file with 64 bit offsets.
1179 ****************************************************************************/
1180
1181 bool cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1182 {
1183         char *p;
1184
1185         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1186                 return cli_unlock(cli, fnum, offset, len);
1187         }
1188
1189         memset(cli->outbuf,'\0',smb_size);
1190         memset(cli->inbuf,'\0',smb_size);
1191
1192         cli_set_message(cli->outbuf,8,0,True);
1193
1194         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1195         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1196         cli_setup_packet(cli);
1197
1198         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1199         SSVAL(cli->outbuf,smb_vwv2,fnum);
1200         SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1201         SIVALS(cli->outbuf, smb_vwv4, 0);
1202         SSVAL(cli->outbuf,smb_vwv6,1);
1203         SSVAL(cli->outbuf,smb_vwv7,0);
1204
1205         p = smb_buf(cli->outbuf);
1206         SIVAL(p, 0, cli->pid);
1207         SOFF_T_R(p, 4, offset);
1208         SOFF_T_R(p, 12, len);
1209         p += 20;
1210         cli_setup_bcc(cli, p);
1211         cli_send_smb(cli);
1212         if (!cli_receive_smb(cli)) {
1213                 return False;
1214         }
1215
1216         if (cli_is_error(cli)) {
1217                 return False;
1218         }
1219
1220         return True;
1221 }
1222
1223 /****************************************************************************
1224  Get/unlock a POSIX lock on a file - internal function.
1225 ****************************************************************************/
1226
1227 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
1228                 SMB_BIG_UINT offset, SMB_BIG_UINT len, bool wait_lock, enum brl_type lock_type)
1229 {
1230         unsigned int param_len = 4;
1231         unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1232         uint16 setup = TRANSACT2_SETFILEINFO;
1233         char param[4];
1234         unsigned char data[POSIX_LOCK_DATA_SIZE];
1235         char *rparam=NULL, *rdata=NULL;
1236         int saved_timeout = cli->timeout;
1237
1238         SSVAL(param,0,fnum);
1239         SSVAL(param,2,SMB_SET_POSIX_LOCK);
1240
1241         switch (lock_type) {
1242                 case READ_LOCK:
1243                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1244                         break;
1245                 case WRITE_LOCK:
1246                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1247                         break;
1248                 case UNLOCK_LOCK:
1249                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1250                         break;
1251                 default:
1252                         return False;
1253         }
1254
1255         if (wait_lock) {
1256                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1257                 cli->timeout = 0x7FFFFFFF;
1258         } else {
1259                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1260         }
1261
1262         SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1263         SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1264         SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
1265
1266         if (!cli_send_trans(cli, SMBtrans2,
1267                         NULL,                        /* name */
1268                         -1, 0,                          /* fid, flags */
1269                         &setup, 1, 0,                   /* setup, length, max */
1270                         param, param_len, 2,            /* param, length, max */
1271                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
1272                         )) {
1273                 cli->timeout = saved_timeout;
1274                 return False;
1275         }
1276
1277         if (!cli_receive_trans(cli, SMBtrans2,
1278                                 &rparam, &param_len,
1279                                 &rdata, &data_len)) {
1280                 cli->timeout = saved_timeout;
1281                 SAFE_FREE(rdata);
1282                 SAFE_FREE(rparam);
1283                 return False;
1284         }
1285
1286         cli->timeout = saved_timeout;
1287
1288         SAFE_FREE(rdata);
1289         SAFE_FREE(rparam);
1290
1291         return True;
1292 }
1293
1294 /****************************************************************************
1295  POSIX Lock a file.
1296 ****************************************************************************/
1297
1298 bool cli_posix_lock(struct cli_state *cli, int fnum,
1299                         SMB_BIG_UINT offset, SMB_BIG_UINT len,
1300                         bool wait_lock, enum brl_type lock_type)
1301 {
1302         if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
1303                 return False;
1304         }
1305         return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
1306 }
1307
1308 /****************************************************************************
1309  POSIX Unlock a file.
1310 ****************************************************************************/
1311
1312 bool cli_posix_unlock(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1313 {
1314         return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
1315 }
1316
1317 /****************************************************************************
1318  POSIX Get any lock covering a file.
1319 ****************************************************************************/
1320
1321 bool cli_posix_getlock(struct cli_state *cli, int fnum, SMB_BIG_UINT *poffset, SMB_BIG_UINT *plen)
1322 {
1323         return True;
1324 }
1325
1326 /****************************************************************************
1327  Do a SMBgetattrE call.
1328 ****************************************************************************/
1329
1330 bool cli_getattrE(struct cli_state *cli, int fd,
1331                   uint16 *attr, SMB_OFF_T *size,
1332                   time_t *change_time,
1333                   time_t *access_time,
1334                   time_t *write_time)
1335 {
1336         memset(cli->outbuf,'\0',smb_size);
1337         memset(cli->inbuf,'\0',smb_size);
1338
1339         cli_set_message(cli->outbuf,1,0,True);
1340
1341         SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1342         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1343         cli_setup_packet(cli);
1344
1345         SSVAL(cli->outbuf,smb_vwv0,fd);
1346
1347         cli_send_smb(cli);
1348         if (!cli_receive_smb(cli)) {
1349                 return False;
1350         }
1351
1352         if (cli_is_error(cli)) {
1353                 return False;
1354         }
1355
1356         if (size) {
1357                 *size = IVAL(cli->inbuf, smb_vwv6);
1358         }
1359
1360         if (attr) {
1361                 *attr = SVAL(cli->inbuf,smb_vwv10);
1362         }
1363
1364         if (change_time) {
1365                 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
1366         }
1367
1368         if (access_time) {
1369                 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
1370         }
1371
1372         if (write_time) {
1373                 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
1374         }
1375
1376         return True;
1377 }
1378
1379 /****************************************************************************
1380  Do a SMBgetatr call
1381 ****************************************************************************/
1382
1383 bool cli_getatr(struct cli_state *cli, const char *fname,
1384                 uint16 *attr, SMB_OFF_T *size, time_t *write_time)
1385 {
1386         char *p;
1387
1388         memset(cli->outbuf,'\0',smb_size);
1389         memset(cli->inbuf,'\0',smb_size);
1390
1391         cli_set_message(cli->outbuf,0,0,True);
1392
1393         SCVAL(cli->outbuf,smb_com,SMBgetatr);
1394         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1395         cli_setup_packet(cli);
1396
1397         p = smb_buf(cli->outbuf);
1398         *p++ = 4;
1399         p += clistr_push(cli, p, fname,
1400                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1401
1402         cli_setup_bcc(cli, p);
1403
1404         cli_send_smb(cli);
1405         if (!cli_receive_smb(cli)) {
1406                 return False;
1407         }
1408
1409         if (cli_is_error(cli)) {
1410                 return False;
1411         }
1412
1413         if (size) {
1414                 *size = IVAL(cli->inbuf, smb_vwv3);
1415         }
1416
1417         if (write_time) {
1418                 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
1419         }
1420
1421         if (attr) {
1422                 *attr = SVAL(cli->inbuf,smb_vwv0);
1423         }
1424
1425         return True;
1426 }
1427
1428 /****************************************************************************
1429  Do a SMBsetattrE call.
1430 ****************************************************************************/
1431
1432 bool cli_setattrE(struct cli_state *cli, int fd,
1433                   time_t change_time,
1434                   time_t access_time,
1435                   time_t write_time)
1436
1437 {
1438         char *p;
1439
1440         memset(cli->outbuf,'\0',smb_size);
1441         memset(cli->inbuf,'\0',smb_size);
1442
1443         cli_set_message(cli->outbuf,7,0,True);
1444
1445         SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1446         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1447         cli_setup_packet(cli);
1448
1449         SSVAL(cli->outbuf,smb_vwv0, fd);
1450         cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
1451         cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
1452         cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
1453
1454         p = smb_buf(cli->outbuf);
1455         *p++ = 4;
1456
1457         cli_setup_bcc(cli, p);
1458
1459         cli_send_smb(cli);
1460         if (!cli_receive_smb(cli)) {
1461                 return False;
1462         }
1463
1464         if (cli_is_error(cli)) {
1465                 return False;
1466         }
1467
1468         return True;
1469 }
1470
1471 /****************************************************************************
1472  Do a SMBsetatr call.
1473 ****************************************************************************/
1474
1475 bool cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1476 {
1477         char *p;
1478
1479         memset(cli->outbuf,'\0',smb_size);
1480         memset(cli->inbuf,'\0',smb_size);
1481
1482         cli_set_message(cli->outbuf,8,0,True);
1483
1484         SCVAL(cli->outbuf,smb_com,SMBsetatr);
1485         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1486         cli_setup_packet(cli);
1487
1488         SSVAL(cli->outbuf,smb_vwv0, attr);
1489         cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
1490
1491         p = smb_buf(cli->outbuf);
1492         *p++ = 4;
1493         p += clistr_push(cli, p, fname,
1494                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1495         *p++ = 4;
1496
1497         cli_setup_bcc(cli, p);
1498
1499         cli_send_smb(cli);
1500         if (!cli_receive_smb(cli)) {
1501                 return False;
1502         }
1503
1504         if (cli_is_error(cli)) {
1505                 return False;
1506         }
1507
1508         return True;
1509 }
1510
1511 /****************************************************************************
1512  Check for existance of a dir.
1513 ****************************************************************************/
1514
1515 bool cli_chkpath(struct cli_state *cli, const char *path)
1516 {
1517         char *path2 = NULL;
1518         char *p;
1519         TALLOC_CTX *frame = talloc_stackframe();
1520
1521         path2 = talloc_strdup(frame, path);
1522         if (!path2) {
1523                 TALLOC_FREE(frame);
1524                 return false;
1525         }
1526         trim_char(path2,'\0','\\');
1527         if (!*path2) {
1528                 path2 = talloc_strdup(frame, "\\");
1529                 if (!path2) {
1530                         TALLOC_FREE(frame);
1531                         return false;
1532                 }
1533         }
1534
1535         memset(cli->outbuf,'\0',smb_size);
1536         cli_set_message(cli->outbuf,0,0,True);
1537         SCVAL(cli->outbuf,smb_com,SMBcheckpath);
1538         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1539         cli_setup_packet(cli);
1540         p = smb_buf(cli->outbuf);
1541         *p++ = 4;
1542         p += clistr_push(cli, p, path2,
1543                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1544
1545         cli_setup_bcc(cli, p);
1546
1547         cli_send_smb(cli);
1548         if (!cli_receive_smb(cli)) {
1549                 TALLOC_FREE(frame);
1550                 return False;
1551         }
1552
1553         TALLOC_FREE(frame);
1554
1555         if (cli_is_error(cli)) return False;
1556
1557         return True;
1558 }
1559
1560 /****************************************************************************
1561  Query disk space.
1562 ****************************************************************************/
1563
1564 bool cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1565 {
1566         memset(cli->outbuf,'\0',smb_size);
1567         cli_set_message(cli->outbuf,0,0,True);
1568         SCVAL(cli->outbuf,smb_com,SMBdskattr);
1569         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1570         cli_setup_packet(cli);
1571
1572         cli_send_smb(cli);
1573         if (!cli_receive_smb(cli)) {
1574                 return False;
1575         }
1576
1577         *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1578         *total = SVAL(cli->inbuf,smb_vwv0);
1579         *avail = SVAL(cli->inbuf,smb_vwv3);
1580
1581         return True;
1582 }
1583
1584 /****************************************************************************
1585  Create and open a temporary file.
1586 ****************************************************************************/
1587
1588 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1589 {
1590         int len;
1591         char *p;
1592
1593         memset(cli->outbuf,'\0',smb_size);
1594         memset(cli->inbuf,'\0',smb_size);
1595
1596         cli_set_message(cli->outbuf,3,0,True);
1597
1598         SCVAL(cli->outbuf,smb_com,SMBctemp);
1599         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1600         cli_setup_packet(cli);
1601
1602         SSVAL(cli->outbuf,smb_vwv0,0);
1603         SIVALS(cli->outbuf,smb_vwv1,-1);
1604
1605         p = smb_buf(cli->outbuf);
1606         *p++ = 4;
1607         p += clistr_push(cli, p, path,
1608                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1609
1610         cli_setup_bcc(cli, p);
1611
1612         cli_send_smb(cli);
1613         if (!cli_receive_smb(cli)) {
1614                 return -1;
1615         }
1616
1617         if (cli_is_error(cli)) {
1618                 return -1;
1619         }
1620
1621         /* despite the spec, the result has a -1, followed by
1622            length, followed by name */
1623         p = smb_buf(cli->inbuf);
1624         p += 4;
1625         len = smb_buflen(cli->inbuf) - 4;
1626         if (len <= 0 || len > PATH_MAX) return -1;
1627
1628         if (tmp_path) {
1629                 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
1630                 if (!path2) {
1631                         return -1;
1632                 }
1633                 clistr_pull(cli, path2, p,
1634                             len+1, len, STR_ASCII);
1635                 *tmp_path = path2;
1636         }
1637
1638         return SVAL(cli->inbuf,smb_vwv0);
1639 }
1640
1641 /*
1642    send a raw ioctl - used by the torture code
1643 */
1644 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1645 {
1646         memset(cli->outbuf,'\0',smb_size);
1647         memset(cli->inbuf,'\0',smb_size);
1648
1649         cli_set_message(cli->outbuf, 3, 0, True);
1650         SCVAL(cli->outbuf,smb_com,SMBioctl);
1651         cli_setup_packet(cli);
1652
1653         SSVAL(cli->outbuf, smb_vwv0, fnum);
1654         SSVAL(cli->outbuf, smb_vwv1, code>>16);
1655         SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1656
1657         cli_send_smb(cli);
1658         if (!cli_receive_smb(cli)) {
1659                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1660         }
1661
1662         if (cli_is_error(cli)) {
1663                 return cli_nt_error(cli);
1664         }
1665
1666         *blob = data_blob_null;
1667
1668         return NT_STATUS_OK;
1669 }
1670
1671 /*********************************************************
1672  Set an extended attribute utility fn.
1673 *********************************************************/
1674
1675 static bool cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1676                         const char *ea_name, const char *ea_val, size_t ea_len)
1677 {
1678         unsigned int data_len = 0;
1679         char *data = NULL;
1680         char *rparam=NULL, *rdata=NULL;
1681         char *p;
1682         size_t ea_namelen = strlen(ea_name);
1683
1684         if (ea_namelen == 0 && ea_len == 0) {
1685                 data_len = 4;
1686                 data = (char *)SMB_MALLOC(data_len);
1687                 if (!data) {
1688                         return False;
1689                 }
1690                 p = data;
1691                 SIVAL(p,0,data_len);
1692         } else {
1693                 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1694                 data = (char *)SMB_MALLOC(data_len);
1695                 if (!data) {
1696                         return False;
1697                 }
1698                 p = data;
1699                 SIVAL(p,0,data_len);
1700                 p += 4;
1701                 SCVAL(p, 0, 0); /* EA flags. */
1702                 SCVAL(p, 1, ea_namelen);
1703                 SSVAL(p, 2, ea_len);
1704                 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1705                 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1706         }
1707
1708         if (!cli_send_trans(cli, SMBtrans2,
1709                         NULL,                        /* name */
1710                         -1, 0,                          /* fid, flags */
1711                         &setup, 1, 0,                   /* setup, length, max */
1712                         param, param_len, 2,            /* param, length, max */
1713                         data,  data_len, cli->max_xmit /* data, length, max */
1714                         )) {
1715                 SAFE_FREE(data);
1716                 return False;
1717         }
1718
1719         if (!cli_receive_trans(cli, SMBtrans2,
1720                         &rparam, &param_len,
1721                         &rdata, &data_len)) {
1722                         SAFE_FREE(data);
1723                 return false;
1724         }
1725
1726         SAFE_FREE(data);
1727         SAFE_FREE(rdata);
1728         SAFE_FREE(rparam);
1729
1730         return True;
1731 }
1732
1733 /*********************************************************
1734  Set an extended attribute on a pathname.
1735 *********************************************************/
1736
1737 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1738 {
1739         uint16 setup = TRANSACT2_SETPATHINFO;
1740         unsigned int param_len = 0;
1741         char *param;
1742         size_t srclen = 2*(strlen(path)+1);
1743         char *p;
1744         bool ret;
1745
1746         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
1747         if (!param) {
1748                 return false;
1749         }
1750         memset(param, '\0', 6);
1751         SSVAL(param,0,SMB_INFO_SET_EA);
1752         p = &param[6];
1753
1754         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
1755         param_len = PTR_DIFF(p, param);
1756
1757         ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
1758         SAFE_FREE(param);
1759         return ret;
1760 }
1761
1762 /*********************************************************
1763  Set an extended attribute on an fnum.
1764 *********************************************************/
1765
1766 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
1767 {
1768         char param[6];
1769         uint16 setup = TRANSACT2_SETFILEINFO;
1770
1771         memset(param, 0, 6);
1772         SSVAL(param,0,fnum);
1773         SSVAL(param,2,SMB_INFO_SET_EA);
1774
1775         return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
1776 }
1777
1778 /*********************************************************
1779  Get an extended attribute list tility fn.
1780 *********************************************************/
1781
1782 static bool cli_get_ea_list(struct cli_state *cli,
1783                 uint16 setup, char *param, unsigned int param_len,
1784                 TALLOC_CTX *ctx,
1785                 size_t *pnum_eas,
1786                 struct ea_struct **pea_list)
1787 {
1788         unsigned int data_len = 0;
1789         unsigned int rparam_len, rdata_len;
1790         char *rparam=NULL, *rdata=NULL;
1791         char *p;
1792         size_t ea_size;
1793         size_t num_eas;
1794         bool ret = False;
1795         struct ea_struct *ea_list;
1796
1797         *pnum_eas = 0;
1798         if (pea_list) {
1799                 *pea_list = NULL;
1800         }
1801
1802         if (!cli_send_trans(cli, SMBtrans2,
1803                         NULL,           /* Name */
1804                         -1, 0,          /* fid, flags */
1805                         &setup, 1, 0,   /* setup, length, max */
1806                         param, param_len, 10, /* param, length, max */
1807                         NULL, data_len, cli->max_xmit /* data, length, max */
1808                                 )) {
1809                 return False;
1810         }
1811
1812         if (!cli_receive_trans(cli, SMBtrans2,
1813                         &rparam, &rparam_len,
1814                         &rdata, &rdata_len)) {
1815                 return False;
1816         }
1817
1818         if (!rdata || rdata_len < 4) {
1819                 goto out;
1820         }
1821
1822         ea_size = (size_t)IVAL(rdata,0);
1823         if (ea_size > rdata_len) {
1824                 goto out;
1825         }
1826
1827         if (ea_size == 0) {
1828                 /* No EA's present. */
1829                 ret = True;
1830                 goto out;
1831         }
1832
1833         p = rdata + 4;
1834         ea_size -= 4;
1835
1836         /* Validate the EA list and count it. */
1837         for (num_eas = 0; ea_size >= 4; num_eas++) {
1838                 unsigned int ea_namelen = CVAL(p,1);
1839                 unsigned int ea_valuelen = SVAL(p,2);
1840                 if (ea_namelen == 0) {
1841                         goto out;
1842                 }
1843                 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
1844                         goto out;
1845                 }
1846                 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
1847                 p += 4 + ea_namelen + 1 + ea_valuelen;
1848         }
1849
1850         if (num_eas == 0) {
1851                 ret = True;
1852                 goto out;
1853         }
1854
1855         *pnum_eas = num_eas;
1856         if (!pea_list) {
1857                 /* Caller only wants number of EA's. */
1858                 ret = True;
1859                 goto out;
1860         }
1861
1862         ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
1863         if (!ea_list) {
1864                 goto out;
1865         }
1866
1867         ea_size = (size_t)IVAL(rdata,0);
1868         p = rdata + 4;
1869
1870         for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
1871                 struct ea_struct *ea = &ea_list[num_eas];
1872                 fstring unix_ea_name;
1873                 unsigned int ea_namelen = CVAL(p,1);
1874                 unsigned int ea_valuelen = SVAL(p,2);
1875
1876                 ea->flags = CVAL(p,0);
1877                 unix_ea_name[0] = '\0';
1878                 pull_ascii_fstring(unix_ea_name, p + 4);
1879                 ea->name = talloc_strdup(ctx, unix_ea_name);
1880                 /* Ensure the value is null terminated (in case it's a string). */
1881                 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
1882                 if (!ea->value.data) {
1883                         goto out;
1884                 }
1885                 if (ea_valuelen) {
1886                         memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
1887                 }
1888                 ea->value.data[ea_valuelen] = 0;
1889                 ea->value.length--;
1890                 p += 4 + ea_namelen + 1 + ea_valuelen;
1891         }
1892
1893         *pea_list = ea_list;
1894         ret = True;
1895
1896  out :
1897
1898         SAFE_FREE(rdata);
1899         SAFE_FREE(rparam);
1900         return ret;
1901 }
1902
1903 /*********************************************************
1904  Get an extended attribute list from a pathname.
1905 *********************************************************/
1906
1907 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
1908                 TALLOC_CTX *ctx,
1909                 size_t *pnum_eas,
1910                 struct ea_struct **pea_list)
1911 {
1912         uint16 setup = TRANSACT2_QPATHINFO;
1913         unsigned int param_len = 0;
1914         char *param;
1915         char *p;
1916         size_t srclen = 2*(strlen(path)+1);
1917         bool ret;
1918
1919         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
1920         if (!param) {
1921                 return false;
1922         }
1923         p = param;
1924         memset(p, 0, 6);
1925         SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
1926         p += 6;
1927         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
1928         param_len = PTR_DIFF(p, param);
1929
1930         ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
1931         SAFE_FREE(param);
1932         return ret;
1933 }
1934
1935 /*********************************************************
1936  Get an extended attribute list from an fnum.
1937 *********************************************************/
1938
1939 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
1940                 TALLOC_CTX *ctx,
1941                 size_t *pnum_eas,
1942                 struct ea_struct **pea_list)
1943 {
1944         uint16 setup = TRANSACT2_QFILEINFO;
1945         char param[6];
1946
1947         memset(param, 0, 6);
1948         SSVAL(param,0,fnum);
1949         SSVAL(param,2,SMB_INFO_SET_EA);
1950
1951         return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
1952 }
1953
1954 /****************************************************************************
1955  Convert open "flags" arg to uint32 on wire.
1956 ****************************************************************************/
1957
1958 static uint32 open_flags_to_wire(int flags)
1959 {
1960         int open_mode = flags & O_ACCMODE;
1961         uint32 ret = 0;
1962
1963         switch (open_mode) {
1964                 case O_WRONLY:
1965                         ret |= SMB_O_WRONLY;
1966                         break;
1967                 case O_RDWR:
1968                         ret |= SMB_O_RDWR;
1969                         break;
1970                 default:
1971                 case O_RDONLY:
1972                         ret |= SMB_O_RDONLY;
1973                         break;
1974         }
1975
1976         if (flags & O_CREAT) {
1977                 ret |= SMB_O_CREAT;
1978         }
1979         if (flags & O_EXCL) {
1980                 ret |= SMB_O_EXCL;
1981         }
1982         if (flags & O_TRUNC) {
1983                 ret |= SMB_O_TRUNC;
1984         }
1985 #if defined(O_SYNC)
1986         if (flags & O_SYNC) {
1987                 ret |= SMB_O_SYNC;
1988         }
1989 #endif /* O_SYNC */
1990         if (flags & O_APPEND) {
1991                 ret |= SMB_O_APPEND;
1992         }
1993 #if defined(O_DIRECT)
1994         if (flags & O_DIRECT) {
1995                 ret |= SMB_O_DIRECT;
1996         }
1997 #endif
1998 #if defined(O_DIRECTORY)
1999         if (flags & O_DIRECTORY) {
2000                 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2001                 ret |= SMB_O_DIRECTORY;
2002         }
2003 #endif
2004         return ret;
2005 }
2006
2007 /****************************************************************************
2008  Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
2009 ****************************************************************************/
2010
2011 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
2012 {
2013         unsigned int data_len = 0;
2014         unsigned int param_len = 0;
2015         uint16 setup = TRANSACT2_SETPATHINFO;
2016         char *param;
2017         char data[18];
2018         char *rparam=NULL, *rdata=NULL;
2019         char *p;
2020         int fnum = -1;
2021         uint32 wire_flags = open_flags_to_wire(flags);
2022         size_t srclen = 2*(strlen(fname)+1);
2023
2024         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2025         if (!param) {
2026                 return false;
2027         }
2028         memset(param, '\0', 6);
2029         SSVAL(param,0, SMB_POSIX_PATH_OPEN);
2030         p = &param[6];
2031
2032         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2033         param_len = PTR_DIFF(p, param);
2034
2035         if (is_dir) {
2036                 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2037                 wire_flags |= SMB_O_DIRECTORY;
2038         }
2039
2040         p = data;
2041         SIVAL(p,0,0); /* No oplock. */
2042         SIVAL(p,4,wire_flags);
2043         SIVAL(p,8,unix_perms_to_wire(mode));
2044         SIVAL(p,12,0); /* Top bits of perms currently undefined. */
2045         SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
2046
2047         data_len = 18;
2048
2049         if (!cli_send_trans(cli, SMBtrans2,
2050                         NULL,                        /* name */
2051                         -1, 0,                          /* fid, flags */
2052                         &setup, 1, 0,                   /* setup, length, max */
2053                         param, param_len, 2,            /* param, length, max */
2054                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2055                         )) {
2056                 SAFE_FREE(param);
2057                 return -1;
2058         }
2059
2060         SAFE_FREE(param);
2061
2062         if (!cli_receive_trans(cli, SMBtrans2,
2063                 &rparam, &param_len,
2064                 &rdata, &data_len)) {
2065                         return -1;
2066         }
2067
2068         fnum = SVAL(rdata,2);
2069
2070         SAFE_FREE(rdata);
2071         SAFE_FREE(rparam);
2072
2073         return fnum;
2074 }
2075
2076 /****************************************************************************
2077  open - POSIX semantics.
2078 ****************************************************************************/
2079
2080 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
2081 {
2082         return cli_posix_open_internal(cli, fname, flags, mode, False);
2083 }
2084
2085 /****************************************************************************
2086  mkdir - POSIX semantics.
2087 ****************************************************************************/
2088
2089 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
2090 {
2091         return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
2092 }
2093
2094 /****************************************************************************
2095  unlink or rmdir - POSIX semantics.
2096 ****************************************************************************/
2097
2098 static bool cli_posix_unlink_internal(struct cli_state *cli, const char *fname, bool is_dir)
2099 {
2100         unsigned int data_len = 0;
2101         unsigned int param_len = 0;
2102         uint16 setup = TRANSACT2_SETPATHINFO;
2103         char *param;
2104         char data[2];
2105         char *rparam=NULL, *rdata=NULL;
2106         char *p;
2107         size_t srclen = 2*(strlen(fname)+1);
2108
2109         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2110         if (!param) {
2111                 return false;
2112         }
2113         memset(param, '\0', 6);
2114         SSVAL(param,0, SMB_POSIX_PATH_UNLINK);
2115         p = &param[6];
2116
2117         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2118         param_len = PTR_DIFF(p, param);
2119
2120         SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
2121                         SMB_POSIX_UNLINK_FILE_TARGET);
2122         data_len = 2;
2123
2124         if (!cli_send_trans(cli, SMBtrans2,
2125                         NULL,                        /* name */
2126                         -1, 0,                          /* fid, flags */
2127                         &setup, 1, 0,                   /* setup, length, max */
2128                         param, param_len, 2,            /* param, length, max */
2129                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2130                         )) {
2131                 SAFE_FREE(param);
2132                 return False;
2133         }
2134
2135         SAFE_FREE(param);
2136
2137         if (!cli_receive_trans(cli, SMBtrans2,
2138                 &rparam, &param_len,
2139                 &rdata, &data_len)) {
2140                         return False;
2141         }
2142
2143         SAFE_FREE(rdata);
2144         SAFE_FREE(rparam);
2145
2146         return True;
2147 }
2148
2149 /****************************************************************************
2150  unlink - POSIX semantics.
2151 ****************************************************************************/
2152
2153 bool cli_posix_unlink(struct cli_state *cli, const char *fname)
2154 {
2155         return cli_posix_unlink_internal(cli, fname, False);
2156 }
2157
2158 /****************************************************************************
2159  rmdir - POSIX semantics.
2160 ****************************************************************************/
2161
2162 int cli_posix_rmdir(struct cli_state *cli, const char *fname)
2163 {
2164         return cli_posix_unlink_internal(cli, fname, True);
2165 }