Fix memory leaks on error path
[tprouty/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 SecuityFlags)
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, SecuityFlags);
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 bool cli_close(struct cli_state *cli, int fnum)
869 {
870         memset(cli->outbuf,'\0',smb_size);
871         memset(cli->inbuf,'\0',smb_size);
872
873         cli_set_message(cli->outbuf,3,0,True);
874
875         SCVAL(cli->outbuf,smb_com,SMBclose);
876         SSVAL(cli->outbuf,smb_tid,cli->cnum);
877         cli_setup_packet(cli);
878
879         SSVAL(cli->outbuf,smb_vwv0,fnum);
880         SIVALS(cli->outbuf,smb_vwv1,-1);
881
882         cli_send_smb(cli);
883         if (!cli_receive_smb(cli)) {
884                 return False;
885         }
886
887         return !cli_is_error(cli);
888 }
889
890
891 /****************************************************************************
892  send a lock with a specified locktype
893  this is used for testing LOCKING_ANDX_CANCEL_LOCK
894 ****************************************************************************/
895
896 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
897                       uint32 offset, uint32 len,
898                       int timeout, unsigned char locktype)
899 {
900         char *p;
901         int saved_timeout = cli->timeout;
902
903         memset(cli->outbuf,'\0',smb_size);
904         memset(cli->inbuf,'\0', smb_size);
905
906         cli_set_message(cli->outbuf,8,0,True);
907
908         SCVAL(cli->outbuf,smb_com,SMBlockingX);
909         SSVAL(cli->outbuf,smb_tid,cli->cnum);
910         cli_setup_packet(cli);
911
912         SCVAL(cli->outbuf,smb_vwv0,0xFF);
913         SSVAL(cli->outbuf,smb_vwv2,fnum);
914         SCVAL(cli->outbuf,smb_vwv3,locktype);
915         SIVALS(cli->outbuf, smb_vwv4, timeout);
916         SSVAL(cli->outbuf,smb_vwv6,0);
917         SSVAL(cli->outbuf,smb_vwv7,1);
918
919         p = smb_buf(cli->outbuf);
920         SSVAL(p, 0, cli->pid);
921         SIVAL(p, 2, offset);
922         SIVAL(p, 6, len);
923
924         p += 10;
925
926         cli_setup_bcc(cli, p);
927
928         cli_send_smb(cli);
929
930         if (timeout != 0) {
931                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
932         }
933
934         if (!cli_receive_smb(cli)) {
935                 cli->timeout = saved_timeout;
936                 return NT_STATUS_UNSUCCESSFUL;
937         }
938
939         cli->timeout = saved_timeout;
940
941         return cli_nt_error(cli);
942 }
943
944 /****************************************************************************
945  Lock a file.
946  note that timeout is in units of 2 milliseconds
947 ****************************************************************************/
948
949 bool cli_lock(struct cli_state *cli, int fnum,
950               uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
951 {
952         char *p;
953         int saved_timeout = cli->timeout;
954
955         memset(cli->outbuf,'\0',smb_size);
956         memset(cli->inbuf,'\0', smb_size);
957
958         cli_set_message(cli->outbuf,8,0,True);
959
960         SCVAL(cli->outbuf,smb_com,SMBlockingX);
961         SSVAL(cli->outbuf,smb_tid,cli->cnum);
962         cli_setup_packet(cli);
963
964         SCVAL(cli->outbuf,smb_vwv0,0xFF);
965         SSVAL(cli->outbuf,smb_vwv2,fnum);
966         SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
967         SIVALS(cli->outbuf, smb_vwv4, timeout);
968         SSVAL(cli->outbuf,smb_vwv6,0);
969         SSVAL(cli->outbuf,smb_vwv7,1);
970
971         p = smb_buf(cli->outbuf);
972         SSVAL(p, 0, cli->pid);
973         SIVAL(p, 2, offset);
974         SIVAL(p, 6, len);
975
976         p += 10;
977
978         cli_setup_bcc(cli, p);
979
980         cli_send_smb(cli);
981
982         if (timeout != 0) {
983                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
984         }
985
986         if (!cli_receive_smb(cli)) {
987                 cli->timeout = saved_timeout;
988                 return False;
989         }
990
991         cli->timeout = saved_timeout;
992
993         if (cli_is_error(cli)) {
994                 return False;
995         }
996
997         return True;
998 }
999
1000 /****************************************************************************
1001  Unlock a file.
1002 ****************************************************************************/
1003
1004 bool cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
1005 {
1006         char *p;
1007
1008         memset(cli->outbuf,'\0',smb_size);
1009         memset(cli->inbuf,'\0',smb_size);
1010
1011         cli_set_message(cli->outbuf,8,0,True);
1012
1013         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1014         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1015         cli_setup_packet(cli);
1016
1017         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1018         SSVAL(cli->outbuf,smb_vwv2,fnum);
1019         SCVAL(cli->outbuf,smb_vwv3,0);
1020         SIVALS(cli->outbuf, smb_vwv4, 0);
1021         SSVAL(cli->outbuf,smb_vwv6,1);
1022         SSVAL(cli->outbuf,smb_vwv7,0);
1023
1024         p = smb_buf(cli->outbuf);
1025         SSVAL(p, 0, cli->pid);
1026         SIVAL(p, 2, offset);
1027         SIVAL(p, 6, len);
1028         p += 10;
1029         cli_setup_bcc(cli, p);
1030         cli_send_smb(cli);
1031         if (!cli_receive_smb(cli)) {
1032                 return False;
1033         }
1034
1035         if (cli_is_error(cli)) {
1036                 return False;
1037         }
1038
1039         return True;
1040 }
1041
1042 /****************************************************************************
1043  Lock a file with 64 bit offsets.
1044 ****************************************************************************/
1045
1046 bool cli_lock64(struct cli_state *cli, int fnum,
1047                 SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
1048 {
1049         char *p;
1050         int saved_timeout = cli->timeout;
1051         int ltype;
1052
1053         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1054                 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1055         }
1056
1057         ltype = (lock_type == READ_LOCK? 1 : 0);
1058         ltype |= LOCKING_ANDX_LARGE_FILES;
1059
1060         memset(cli->outbuf,'\0',smb_size);
1061         memset(cli->inbuf,'\0', smb_size);
1062
1063         cli_set_message(cli->outbuf,8,0,True);
1064
1065         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1066         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1067         cli_setup_packet(cli);
1068
1069         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1070         SSVAL(cli->outbuf,smb_vwv2,fnum);
1071         SCVAL(cli->outbuf,smb_vwv3,ltype);
1072         SIVALS(cli->outbuf, smb_vwv4, timeout);
1073         SSVAL(cli->outbuf,smb_vwv6,0);
1074         SSVAL(cli->outbuf,smb_vwv7,1);
1075
1076         p = smb_buf(cli->outbuf);
1077         SIVAL(p, 0, cli->pid);
1078         SOFF_T_R(p, 4, offset);
1079         SOFF_T_R(p, 12, len);
1080         p += 20;
1081
1082         cli_setup_bcc(cli, p);
1083         cli_send_smb(cli);
1084
1085         if (timeout != 0) {
1086                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1087         }
1088
1089         if (!cli_receive_smb(cli)) {
1090                 cli->timeout = saved_timeout;
1091                 return False;
1092         }
1093
1094         cli->timeout = saved_timeout;
1095
1096         if (cli_is_error(cli)) {
1097                 return False;
1098         }
1099
1100         return True;
1101 }
1102
1103 /****************************************************************************
1104  Unlock a file with 64 bit offsets.
1105 ****************************************************************************/
1106
1107 bool cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1108 {
1109         char *p;
1110
1111         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1112                 return cli_unlock(cli, fnum, offset, len);
1113         }
1114
1115         memset(cli->outbuf,'\0',smb_size);
1116         memset(cli->inbuf,'\0',smb_size);
1117
1118         cli_set_message(cli->outbuf,8,0,True);
1119
1120         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1121         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1122         cli_setup_packet(cli);
1123
1124         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1125         SSVAL(cli->outbuf,smb_vwv2,fnum);
1126         SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1127         SIVALS(cli->outbuf, smb_vwv4, 0);
1128         SSVAL(cli->outbuf,smb_vwv6,1);
1129         SSVAL(cli->outbuf,smb_vwv7,0);
1130
1131         p = smb_buf(cli->outbuf);
1132         SIVAL(p, 0, cli->pid);
1133         SOFF_T_R(p, 4, offset);
1134         SOFF_T_R(p, 12, len);
1135         p += 20;
1136         cli_setup_bcc(cli, p);
1137         cli_send_smb(cli);
1138         if (!cli_receive_smb(cli)) {
1139                 return False;
1140         }
1141
1142         if (cli_is_error(cli)) {
1143                 return False;
1144         }
1145
1146         return True;
1147 }
1148
1149 /****************************************************************************
1150  Get/unlock a POSIX lock on a file - internal function.
1151 ****************************************************************************/
1152
1153 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
1154                 SMB_BIG_UINT offset, SMB_BIG_UINT len, bool wait_lock, enum brl_type lock_type)
1155 {
1156         unsigned int param_len = 4;
1157         unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1158         uint16 setup = TRANSACT2_SETFILEINFO;
1159         char param[4];
1160         unsigned char data[POSIX_LOCK_DATA_SIZE];
1161         char *rparam=NULL, *rdata=NULL;
1162         int saved_timeout = cli->timeout;
1163
1164         SSVAL(param,0,fnum);
1165         SSVAL(param,2,SMB_SET_POSIX_LOCK);
1166
1167         switch (lock_type) {
1168                 case READ_LOCK:
1169                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1170                         break;
1171                 case WRITE_LOCK:
1172                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1173                         break;
1174                 case UNLOCK_LOCK:
1175                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1176                         break;
1177                 default:
1178                         return False;
1179         }
1180
1181         if (wait_lock) {
1182                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1183                 cli->timeout = 0x7FFFFFFF;
1184         } else {
1185                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1186         }
1187
1188         SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1189         SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1190         SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
1191
1192         if (!cli_send_trans(cli, SMBtrans2,
1193                         NULL,                        /* name */
1194                         -1, 0,                          /* fid, flags */
1195                         &setup, 1, 0,                   /* setup, length, max */
1196                         param, param_len, 2,            /* param, length, max */
1197                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
1198                         )) {
1199                 cli->timeout = saved_timeout;
1200                 return False;
1201         }
1202
1203         if (!cli_receive_trans(cli, SMBtrans2,
1204                                 &rparam, &param_len,
1205                                 &rdata, &data_len)) {
1206                 cli->timeout = saved_timeout;
1207                 SAFE_FREE(rdata);
1208                 SAFE_FREE(rparam);
1209                 return False;
1210         }
1211
1212         cli->timeout = saved_timeout;
1213
1214         SAFE_FREE(rdata);
1215         SAFE_FREE(rparam);
1216
1217         return True;
1218 }
1219
1220 /****************************************************************************
1221  POSIX Lock a file.
1222 ****************************************************************************/
1223
1224 bool cli_posix_lock(struct cli_state *cli, int fnum,
1225                         SMB_BIG_UINT offset, SMB_BIG_UINT len,
1226                         bool wait_lock, enum brl_type lock_type)
1227 {
1228         if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
1229                 return False;
1230         }
1231         return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
1232 }
1233
1234 /****************************************************************************
1235  POSIX Unlock a file.
1236 ****************************************************************************/
1237
1238 bool cli_posix_unlock(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1239 {
1240         return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
1241 }
1242
1243 /****************************************************************************
1244  POSIX Get any lock covering a file.
1245 ****************************************************************************/
1246
1247 bool cli_posix_getlock(struct cli_state *cli, int fnum, SMB_BIG_UINT *poffset, SMB_BIG_UINT *plen)
1248 {
1249         return True;
1250 }
1251
1252 /****************************************************************************
1253  Do a SMBgetattrE call.
1254 ****************************************************************************/
1255
1256 bool cli_getattrE(struct cli_state *cli, int fd,
1257                   uint16 *attr, SMB_OFF_T *size,
1258                   time_t *change_time,
1259                   time_t *access_time,
1260                   time_t *write_time)
1261 {
1262         memset(cli->outbuf,'\0',smb_size);
1263         memset(cli->inbuf,'\0',smb_size);
1264
1265         cli_set_message(cli->outbuf,1,0,True);
1266
1267         SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1268         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1269         cli_setup_packet(cli);
1270
1271         SSVAL(cli->outbuf,smb_vwv0,fd);
1272
1273         cli_send_smb(cli);
1274         if (!cli_receive_smb(cli)) {
1275                 return False;
1276         }
1277
1278         if (cli_is_error(cli)) {
1279                 return False;
1280         }
1281
1282         if (size) {
1283                 *size = IVAL(cli->inbuf, smb_vwv6);
1284         }
1285
1286         if (attr) {
1287                 *attr = SVAL(cli->inbuf,smb_vwv10);
1288         }
1289
1290         if (change_time) {
1291                 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
1292         }
1293
1294         if (access_time) {
1295                 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
1296         }
1297
1298         if (write_time) {
1299                 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
1300         }
1301
1302         return True;
1303 }
1304
1305 /****************************************************************************
1306  Do a SMBgetatr call
1307 ****************************************************************************/
1308
1309 bool cli_getatr(struct cli_state *cli, const char *fname,
1310                 uint16 *attr, SMB_OFF_T *size, time_t *write_time)
1311 {
1312         char *p;
1313
1314         memset(cli->outbuf,'\0',smb_size);
1315         memset(cli->inbuf,'\0',smb_size);
1316
1317         cli_set_message(cli->outbuf,0,0,True);
1318
1319         SCVAL(cli->outbuf,smb_com,SMBgetatr);
1320         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1321         cli_setup_packet(cli);
1322
1323         p = smb_buf(cli->outbuf);
1324         *p++ = 4;
1325         p += clistr_push(cli, p, fname,
1326                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1327
1328         cli_setup_bcc(cli, p);
1329
1330         cli_send_smb(cli);
1331         if (!cli_receive_smb(cli)) {
1332                 return False;
1333         }
1334
1335         if (cli_is_error(cli)) {
1336                 return False;
1337         }
1338
1339         if (size) {
1340                 *size = IVAL(cli->inbuf, smb_vwv3);
1341         }
1342
1343         if (write_time) {
1344                 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
1345         }
1346
1347         if (attr) {
1348                 *attr = SVAL(cli->inbuf,smb_vwv0);
1349         }
1350
1351         return True;
1352 }
1353
1354 /****************************************************************************
1355  Do a SMBsetattrE call.
1356 ****************************************************************************/
1357
1358 bool cli_setattrE(struct cli_state *cli, int fd,
1359                   time_t change_time,
1360                   time_t access_time,
1361                   time_t write_time)
1362
1363 {
1364         char *p;
1365
1366         memset(cli->outbuf,'\0',smb_size);
1367         memset(cli->inbuf,'\0',smb_size);
1368
1369         cli_set_message(cli->outbuf,7,0,True);
1370
1371         SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1372         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1373         cli_setup_packet(cli);
1374
1375         SSVAL(cli->outbuf,smb_vwv0, fd);
1376         cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
1377         cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
1378         cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
1379
1380         p = smb_buf(cli->outbuf);
1381         *p++ = 4;
1382
1383         cli_setup_bcc(cli, p);
1384
1385         cli_send_smb(cli);
1386         if (!cli_receive_smb(cli)) {
1387                 return False;
1388         }
1389
1390         if (cli_is_error(cli)) {
1391                 return False;
1392         }
1393
1394         return True;
1395 }
1396
1397 /****************************************************************************
1398  Do a SMBsetatr call.
1399 ****************************************************************************/
1400
1401 bool cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1402 {
1403         char *p;
1404
1405         memset(cli->outbuf,'\0',smb_size);
1406         memset(cli->inbuf,'\0',smb_size);
1407
1408         cli_set_message(cli->outbuf,8,0,True);
1409
1410         SCVAL(cli->outbuf,smb_com,SMBsetatr);
1411         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1412         cli_setup_packet(cli);
1413
1414         SSVAL(cli->outbuf,smb_vwv0, attr);
1415         cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
1416
1417         p = smb_buf(cli->outbuf);
1418         *p++ = 4;
1419         p += clistr_push(cli, p, fname,
1420                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1421         *p++ = 4;
1422
1423         cli_setup_bcc(cli, p);
1424
1425         cli_send_smb(cli);
1426         if (!cli_receive_smb(cli)) {
1427                 return False;
1428         }
1429
1430         if (cli_is_error(cli)) {
1431                 return False;
1432         }
1433
1434         return True;
1435 }
1436
1437 /****************************************************************************
1438  Check for existance of a dir.
1439 ****************************************************************************/
1440
1441 bool cli_chkpath(struct cli_state *cli, const char *path)
1442 {
1443         char *path2 = NULL;
1444         char *p;
1445         TALLOC_CTX *frame = talloc_stackframe();
1446
1447         path2 = talloc_strdup(frame, path);
1448         if (!path2) {
1449                 TALLOC_FREE(frame);
1450                 return false;
1451         }
1452         trim_char(path2,'\0','\\');
1453         if (!*path2) {
1454                 path2 = talloc_strdup(frame, "\\");
1455                 if (!path2) {
1456                         TALLOC_FREE(frame);
1457                         return false;
1458                 }
1459         }
1460
1461         memset(cli->outbuf,'\0',smb_size);
1462         cli_set_message(cli->outbuf,0,0,True);
1463         SCVAL(cli->outbuf,smb_com,SMBcheckpath);
1464         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1465         cli_setup_packet(cli);
1466         p = smb_buf(cli->outbuf);
1467         *p++ = 4;
1468         p += clistr_push(cli, p, path2,
1469                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1470
1471         cli_setup_bcc(cli, p);
1472
1473         cli_send_smb(cli);
1474         if (!cli_receive_smb(cli)) {
1475                 TALLOC_FREE(frame);
1476                 return False;
1477         }
1478
1479         TALLOC_FREE(frame);
1480
1481         if (cli_is_error(cli)) return False;
1482
1483         return True;
1484 }
1485
1486 /****************************************************************************
1487  Query disk space.
1488 ****************************************************************************/
1489
1490 bool cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1491 {
1492         memset(cli->outbuf,'\0',smb_size);
1493         cli_set_message(cli->outbuf,0,0,True);
1494         SCVAL(cli->outbuf,smb_com,SMBdskattr);
1495         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1496         cli_setup_packet(cli);
1497
1498         cli_send_smb(cli);
1499         if (!cli_receive_smb(cli)) {
1500                 return False;
1501         }
1502
1503         *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1504         *total = SVAL(cli->inbuf,smb_vwv0);
1505         *avail = SVAL(cli->inbuf,smb_vwv3);
1506
1507         return True;
1508 }
1509
1510 /****************************************************************************
1511  Create and open a temporary file.
1512 ****************************************************************************/
1513
1514 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1515 {
1516         int len;
1517         char *p;
1518
1519         memset(cli->outbuf,'\0',smb_size);
1520         memset(cli->inbuf,'\0',smb_size);
1521
1522         cli_set_message(cli->outbuf,3,0,True);
1523
1524         SCVAL(cli->outbuf,smb_com,SMBctemp);
1525         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1526         cli_setup_packet(cli);
1527
1528         SSVAL(cli->outbuf,smb_vwv0,0);
1529         SIVALS(cli->outbuf,smb_vwv1,-1);
1530
1531         p = smb_buf(cli->outbuf);
1532         *p++ = 4;
1533         p += clistr_push(cli, p, path,
1534                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1535
1536         cli_setup_bcc(cli, p);
1537
1538         cli_send_smb(cli);
1539         if (!cli_receive_smb(cli)) {
1540                 return -1;
1541         }
1542
1543         if (cli_is_error(cli)) {
1544                 return -1;
1545         }
1546
1547         /* despite the spec, the result has a -1, followed by
1548            length, followed by name */
1549         p = smb_buf(cli->inbuf);
1550         p += 4;
1551         len = smb_buflen(cli->inbuf) - 4;
1552         if (len <= 0 || len > PATH_MAX) return -1;
1553
1554         if (tmp_path) {
1555                 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
1556                 if (!path2) {
1557                         return -1;
1558                 }
1559                 clistr_pull(cli, path2, p,
1560                             len+1, len, STR_ASCII);
1561                 *tmp_path = path2;
1562         }
1563
1564         return SVAL(cli->inbuf,smb_vwv0);
1565 }
1566
1567 /*
1568    send a raw ioctl - used by the torture code
1569 */
1570 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1571 {
1572         memset(cli->outbuf,'\0',smb_size);
1573         memset(cli->inbuf,'\0',smb_size);
1574
1575         cli_set_message(cli->outbuf, 3, 0, True);
1576         SCVAL(cli->outbuf,smb_com,SMBioctl);
1577         cli_setup_packet(cli);
1578
1579         SSVAL(cli->outbuf, smb_vwv0, fnum);
1580         SSVAL(cli->outbuf, smb_vwv1, code>>16);
1581         SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1582
1583         cli_send_smb(cli);
1584         if (!cli_receive_smb(cli)) {
1585                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1586         }
1587
1588         if (cli_is_error(cli)) {
1589                 return cli_nt_error(cli);
1590         }
1591
1592         *blob = data_blob_null;
1593
1594         return NT_STATUS_OK;
1595 }
1596
1597 /*********************************************************
1598  Set an extended attribute utility fn.
1599 *********************************************************/
1600
1601 static bool cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1602                         const char *ea_name, const char *ea_val, size_t ea_len)
1603 {
1604         unsigned int data_len = 0;
1605         char *data = NULL;
1606         char *rparam=NULL, *rdata=NULL;
1607         char *p;
1608         size_t ea_namelen = strlen(ea_name);
1609
1610         if (ea_namelen == 0 && ea_len == 0) {
1611                 data_len = 4;
1612                 data = (char *)SMB_MALLOC(data_len);
1613                 if (!data) {
1614                         return False;
1615                 }
1616                 p = data;
1617                 SIVAL(p,0,data_len);
1618         } else {
1619                 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1620                 data = (char *)SMB_MALLOC(data_len);
1621                 if (!data) {
1622                         return False;
1623                 }
1624                 p = data;
1625                 SIVAL(p,0,data_len);
1626                 p += 4;
1627                 SCVAL(p, 0, 0); /* EA flags. */
1628                 SCVAL(p, 1, ea_namelen);
1629                 SSVAL(p, 2, ea_len);
1630                 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1631                 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1632         }
1633
1634         if (!cli_send_trans(cli, SMBtrans2,
1635                         NULL,                        /* name */
1636                         -1, 0,                          /* fid, flags */
1637                         &setup, 1, 0,                   /* setup, length, max */
1638                         param, param_len, 2,            /* param, length, max */
1639                         data,  data_len, cli->max_xmit /* data, length, max */
1640                         )) {
1641                 SAFE_FREE(data);
1642                 return False;
1643         }
1644
1645         if (!cli_receive_trans(cli, SMBtrans2,
1646                         &rparam, &param_len,
1647                         &rdata, &data_len)) {
1648                         SAFE_FREE(data);
1649                 return false;
1650         }
1651
1652         SAFE_FREE(data);
1653         SAFE_FREE(rdata);
1654         SAFE_FREE(rparam);
1655
1656         return True;
1657 }
1658
1659 /*********************************************************
1660  Set an extended attribute on a pathname.
1661 *********************************************************/
1662
1663 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1664 {
1665         uint16 setup = TRANSACT2_SETPATHINFO;
1666         unsigned int param_len = 0;
1667         char *param;
1668         size_t srclen = 2*(strlen(path)+1);
1669         char *p;
1670         bool ret;
1671
1672         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
1673         if (!param) {
1674                 return false;
1675         }
1676         memset(param, '\0', 6);
1677         SSVAL(param,0,SMB_INFO_SET_EA);
1678         p = &param[6];
1679
1680         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
1681         param_len = PTR_DIFF(p, param);
1682
1683         ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
1684         SAFE_FREE(param);
1685         return ret;
1686 }
1687
1688 /*********************************************************
1689  Set an extended attribute on an fnum.
1690 *********************************************************/
1691
1692 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
1693 {
1694         char param[6];
1695         uint16 setup = TRANSACT2_SETFILEINFO;
1696
1697         memset(param, 0, 6);
1698         SSVAL(param,0,fnum);
1699         SSVAL(param,2,SMB_INFO_SET_EA);
1700
1701         return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
1702 }
1703
1704 /*********************************************************
1705  Get an extended attribute list tility fn.
1706 *********************************************************/
1707
1708 static bool cli_get_ea_list(struct cli_state *cli,
1709                 uint16 setup, char *param, unsigned int param_len,
1710                 TALLOC_CTX *ctx,
1711                 size_t *pnum_eas,
1712                 struct ea_struct **pea_list)
1713 {
1714         unsigned int data_len = 0;
1715         unsigned int rparam_len, rdata_len;
1716         char *rparam=NULL, *rdata=NULL;
1717         char *p;
1718         size_t ea_size;
1719         size_t num_eas;
1720         bool ret = False;
1721         struct ea_struct *ea_list;
1722
1723         *pnum_eas = 0;
1724         if (pea_list) {
1725                 *pea_list = NULL;
1726         }
1727
1728         if (!cli_send_trans(cli, SMBtrans2,
1729                         NULL,           /* Name */
1730                         -1, 0,          /* fid, flags */
1731                         &setup, 1, 0,   /* setup, length, max */
1732                         param, param_len, 10, /* param, length, max */
1733                         NULL, data_len, cli->max_xmit /* data, length, max */
1734                                 )) {
1735                 return False;
1736         }
1737
1738         if (!cli_receive_trans(cli, SMBtrans2,
1739                         &rparam, &rparam_len,
1740                         &rdata, &rdata_len)) {
1741                 return False;
1742         }
1743
1744         if (!rdata || rdata_len < 4) {
1745                 goto out;
1746         }
1747
1748         ea_size = (size_t)IVAL(rdata,0);
1749         if (ea_size > rdata_len) {
1750                 goto out;
1751         }
1752
1753         if (ea_size == 0) {
1754                 /* No EA's present. */
1755                 ret = True;
1756                 goto out;
1757         }
1758
1759         p = rdata + 4;
1760         ea_size -= 4;
1761
1762         /* Validate the EA list and count it. */
1763         for (num_eas = 0; ea_size >= 4; num_eas++) {
1764                 unsigned int ea_namelen = CVAL(p,1);
1765                 unsigned int ea_valuelen = SVAL(p,2);
1766                 if (ea_namelen == 0) {
1767                         goto out;
1768                 }
1769                 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
1770                         goto out;
1771                 }
1772                 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
1773                 p += 4 + ea_namelen + 1 + ea_valuelen;
1774         }
1775
1776         if (num_eas == 0) {
1777                 ret = True;
1778                 goto out;
1779         }
1780
1781         *pnum_eas = num_eas;
1782         if (!pea_list) {
1783                 /* Caller only wants number of EA's. */
1784                 ret = True;
1785                 goto out;
1786         }
1787
1788         ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
1789         if (!ea_list) {
1790                 goto out;
1791         }
1792
1793         ea_size = (size_t)IVAL(rdata,0);
1794         p = rdata + 4;
1795
1796         for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
1797                 struct ea_struct *ea = &ea_list[num_eas];
1798                 fstring unix_ea_name;
1799                 unsigned int ea_namelen = CVAL(p,1);
1800                 unsigned int ea_valuelen = SVAL(p,2);
1801
1802                 ea->flags = CVAL(p,0);
1803                 unix_ea_name[0] = '\0';
1804                 pull_ascii_fstring(unix_ea_name, p + 4);
1805                 ea->name = talloc_strdup(ctx, unix_ea_name);
1806                 /* Ensure the value is null terminated (in case it's a string). */
1807                 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
1808                 if (!ea->value.data) {
1809                         goto out;
1810                 }
1811                 if (ea_valuelen) {
1812                         memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
1813                 }
1814                 ea->value.data[ea_valuelen] = 0;
1815                 ea->value.length--;
1816                 p += 4 + ea_namelen + 1 + ea_valuelen;
1817         }
1818
1819         *pea_list = ea_list;
1820         ret = True;
1821
1822  out :
1823
1824         SAFE_FREE(rdata);
1825         SAFE_FREE(rparam);
1826         return ret;
1827 }
1828
1829 /*********************************************************
1830  Get an extended attribute list from a pathname.
1831 *********************************************************/
1832
1833 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
1834                 TALLOC_CTX *ctx,
1835                 size_t *pnum_eas,
1836                 struct ea_struct **pea_list)
1837 {
1838         uint16 setup = TRANSACT2_QPATHINFO;
1839         unsigned int param_len = 0;
1840         char *param;
1841         char *p;
1842         size_t srclen = 2*(strlen(path)+1);
1843         bool ret;
1844
1845         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
1846         if (!param) {
1847                 return false;
1848         }
1849         p = param;
1850         memset(p, 0, 6);
1851         SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
1852         p += 6;
1853         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
1854         param_len = PTR_DIFF(p, param);
1855
1856         ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
1857         SAFE_FREE(param);
1858         return ret;
1859 }
1860
1861 /*********************************************************
1862  Get an extended attribute list from an fnum.
1863 *********************************************************/
1864
1865 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
1866                 TALLOC_CTX *ctx,
1867                 size_t *pnum_eas,
1868                 struct ea_struct **pea_list)
1869 {
1870         uint16 setup = TRANSACT2_QFILEINFO;
1871         char param[6];
1872
1873         memset(param, 0, 6);
1874         SSVAL(param,0,fnum);
1875         SSVAL(param,2,SMB_INFO_SET_EA);
1876
1877         return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
1878 }
1879
1880 /****************************************************************************
1881  Convert open "flags" arg to uint32 on wire.
1882 ****************************************************************************/
1883
1884 static uint32 open_flags_to_wire(int flags)
1885 {
1886         int open_mode = flags & O_ACCMODE;
1887         uint32 ret = 0;
1888
1889         switch (open_mode) {
1890                 case O_WRONLY:
1891                         ret |= SMB_O_WRONLY;
1892                         break;
1893                 case O_RDWR:
1894                         ret |= SMB_O_RDWR;
1895                         break;
1896                 default:
1897                 case O_RDONLY:
1898                         ret |= SMB_O_RDONLY;
1899                         break;
1900         }
1901
1902         if (flags & O_CREAT) {
1903                 ret |= SMB_O_CREAT;
1904         }
1905         if (flags & O_EXCL) {
1906                 ret |= SMB_O_EXCL;
1907         }
1908         if (flags & O_TRUNC) {
1909                 ret |= SMB_O_TRUNC;
1910         }
1911 #if defined(O_SYNC)
1912         if (flags & O_SYNC) {
1913                 ret |= SMB_O_SYNC;
1914         }
1915 #endif /* O_SYNC */
1916         if (flags & O_APPEND) {
1917                 ret |= SMB_O_APPEND;
1918         }
1919 #if defined(O_DIRECT)
1920         if (flags & O_DIRECT) {
1921                 ret |= SMB_O_DIRECT;
1922         }
1923 #endif
1924 #if defined(O_DIRECTORY)
1925         if (flags & O_DIRECTORY) {
1926                 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
1927                 ret |= SMB_O_DIRECTORY;
1928         }
1929 #endif
1930         return ret;
1931 }
1932
1933 /****************************************************************************
1934  Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
1935 ****************************************************************************/
1936
1937 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
1938 {
1939         unsigned int data_len = 0;
1940         unsigned int param_len = 0;
1941         uint16 setup = TRANSACT2_SETPATHINFO;
1942         char *param;
1943         char data[18];
1944         char *rparam=NULL, *rdata=NULL;
1945         char *p;
1946         int fnum = -1;
1947         uint32 wire_flags = open_flags_to_wire(flags);
1948         size_t srclen = 2*(strlen(fname)+1);
1949
1950         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
1951         if (!param) {
1952                 return false;
1953         }
1954         memset(param, '\0', 6);
1955         SSVAL(param,0, SMB_POSIX_PATH_OPEN);
1956         p = &param[6];
1957
1958         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
1959         param_len = PTR_DIFF(p, param);
1960
1961         if (is_dir) {
1962                 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
1963                 wire_flags |= SMB_O_DIRECTORY;
1964         }
1965
1966         p = data;
1967         SIVAL(p,0,0); /* No oplock. */
1968         SIVAL(p,4,wire_flags);
1969         SIVAL(p,8,unix_perms_to_wire(mode));
1970         SIVAL(p,12,0); /* Top bits of perms currently undefined. */
1971         SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
1972
1973         data_len = 18;
1974
1975         if (!cli_send_trans(cli, SMBtrans2,
1976                         NULL,                        /* name */
1977                         -1, 0,                          /* fid, flags */
1978                         &setup, 1, 0,                   /* setup, length, max */
1979                         param, param_len, 2,            /* param, length, max */
1980                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
1981                         )) {
1982                 SAFE_FREE(param);
1983                 return -1;
1984         }
1985
1986         SAFE_FREE(param);
1987
1988         if (!cli_receive_trans(cli, SMBtrans2,
1989                 &rparam, &param_len,
1990                 &rdata, &data_len)) {
1991                         return -1;
1992         }
1993
1994         fnum = SVAL(rdata,2);
1995
1996         SAFE_FREE(rdata);
1997         SAFE_FREE(rparam);
1998
1999         return fnum;
2000 }
2001
2002 /****************************************************************************
2003  open - POSIX semantics.
2004 ****************************************************************************/
2005
2006 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
2007 {
2008         return cli_posix_open_internal(cli, fname, flags, mode, False);
2009 }
2010
2011 /****************************************************************************
2012  mkdir - POSIX semantics.
2013 ****************************************************************************/
2014
2015 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
2016 {
2017         return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
2018 }
2019
2020 /****************************************************************************
2021  unlink or rmdir - POSIX semantics.
2022 ****************************************************************************/
2023
2024 static bool cli_posix_unlink_internal(struct cli_state *cli, const char *fname, bool is_dir)
2025 {
2026         unsigned int data_len = 0;
2027         unsigned int param_len = 0;
2028         uint16 setup = TRANSACT2_SETPATHINFO;
2029         char *param;
2030         char data[2];
2031         char *rparam=NULL, *rdata=NULL;
2032         char *p;
2033         size_t srclen = 2*(strlen(fname)+1);
2034
2035         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2036         if (!param) {
2037                 return false;
2038         }
2039         memset(param, '\0', 6);
2040         SSVAL(param,0, SMB_POSIX_PATH_UNLINK);
2041         p = &param[6];
2042
2043         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2044         param_len = PTR_DIFF(p, param);
2045
2046         SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
2047                         SMB_POSIX_UNLINK_FILE_TARGET);
2048         data_len = 2;
2049
2050         if (!cli_send_trans(cli, SMBtrans2,
2051                         NULL,                        /* name */
2052                         -1, 0,                          /* fid, flags */
2053                         &setup, 1, 0,                   /* setup, length, max */
2054                         param, param_len, 2,            /* param, length, max */
2055                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2056                         )) {
2057                 SAFE_FREE(param);
2058                 return False;
2059         }
2060
2061         SAFE_FREE(param);
2062
2063         if (!cli_receive_trans(cli, SMBtrans2,
2064                 &rparam, &param_len,
2065                 &rdata, &data_len)) {
2066                         return False;
2067         }
2068
2069         SAFE_FREE(rdata);
2070         SAFE_FREE(rparam);
2071
2072         return True;
2073 }
2074
2075 /****************************************************************************
2076  unlink - POSIX semantics.
2077 ****************************************************************************/
2078
2079 bool cli_posix_unlink(struct cli_state *cli, const char *fname)
2080 {
2081         return cli_posix_unlink_internal(cli, fname, False);
2082 }
2083
2084 /****************************************************************************
2085  rmdir - POSIX semantics.
2086 ****************************************************************************/
2087
2088 int cli_posix_rmdir(struct cli_state *cli, const char *fname)
2089 {
2090         return cli_posix_unlink_internal(cli, fname, True);
2091 }