Remove arbitrary 1k limit on pathnames. Malloc them.
[nivanova/samba-autobuild/.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(6+newlen+2);
41         data = SMB_MALLOC(oldlen+2);
42         if (!param || !data) {
43                 return false;
44         }
45
46         SSVAL(param,0,hard_link ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
47         SIVAL(param,2,0);
48         p = &param[6];
49
50         p += clistr_push(cli, p, newname, newlen, STR_TERMINATE);
51         param_len = PTR_DIFF(p, param);
52
53         p = data;
54         p += clistr_push(cli, p, oldname, oldlen, STR_TERMINATE);
55         data_len = PTR_DIFF(p, data);
56
57         if (!cli_send_trans(cli, SMBtrans2,
58                         NULL,                        /* name */
59                         -1, 0,                          /* fid, flags */
60                         &setup, 1, 0,                   /* setup, length, max */
61                         param, param_len, 2,            /* param, length, max */
62                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
63                         )) {
64                 SAFE_FREE(data);
65                 SAFE_FREE(param);
66                 return false;
67         }
68
69         SAFE_FREE(data);
70         SAFE_FREE(param);
71
72         if (!cli_receive_trans(cli, SMBtrans2,
73                         &rparam, &param_len,
74                         &rdata, &data_len)) {
75                         return false;
76         }
77
78         SAFE_FREE(data);
79         SAFE_FREE(param);
80         SAFE_FREE(rdata);
81         SAFE_FREE(rparam);
82
83         return true;
84 }
85
86 /****************************************************************************
87  Map standard UNIX permissions onto wire representations.
88 ****************************************************************************/
89
90 uint32 unix_perms_to_wire(mode_t perms)
91 {
92         unsigned int ret = 0;
93
94         ret |= ((perms & S_IXOTH) ?  UNIX_X_OTH : 0);
95         ret |= ((perms & S_IWOTH) ?  UNIX_W_OTH : 0);
96         ret |= ((perms & S_IROTH) ?  UNIX_R_OTH : 0);
97         ret |= ((perms & S_IXGRP) ?  UNIX_X_GRP : 0);
98         ret |= ((perms & S_IWGRP) ?  UNIX_W_GRP : 0);
99         ret |= ((perms & S_IRGRP) ?  UNIX_R_GRP : 0);
100         ret |= ((perms & S_IXUSR) ?  UNIX_X_USR : 0);
101         ret |= ((perms & S_IWUSR) ?  UNIX_W_USR : 0);
102         ret |= ((perms & S_IRUSR) ?  UNIX_R_USR : 0);
103 #ifdef S_ISVTX
104         ret |= ((perms & S_ISVTX) ?  UNIX_STICKY : 0);
105 #endif
106 #ifdef S_ISGID
107         ret |= ((perms & S_ISGID) ?  UNIX_SET_GID : 0);
108 #endif
109 #ifdef S_ISUID
110         ret |= ((perms & S_ISUID) ?  UNIX_SET_UID : 0);
111 #endif
112         return ret;
113 }
114
115 /****************************************************************************
116  Map wire permissions to standard UNIX.
117 ****************************************************************************/
118
119 mode_t wire_perms_to_unix(uint32 perms)
120 {
121         mode_t ret = (mode_t)0;
122
123         ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
124         ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
125         ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
126         ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
127         ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
128         ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
129         ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
130         ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
131         ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
132 #ifdef S_ISVTX
133         ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
134 #endif
135 #ifdef S_ISGID
136         ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
137 #endif
138 #ifdef S_ISUID
139         ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
140 #endif
141         return ret;
142 }
143
144 /****************************************************************************
145  Return the file type from the wire filetype for UNIX extensions.
146 ****************************************************************************/
147
148 static mode_t unix_filetype_from_wire(uint32 wire_type)
149 {
150         switch (wire_type) {
151                 case UNIX_TYPE_FILE:
152                         return S_IFREG;
153                 case UNIX_TYPE_DIR:
154                         return S_IFDIR;
155 #ifdef S_IFLNK
156                 case UNIX_TYPE_SYMLINK:
157                         return S_IFLNK;
158 #endif
159 #ifdef S_IFCHR
160                 case UNIX_TYPE_CHARDEV:
161                         return S_IFCHR;
162 #endif
163 #ifdef S_IFBLK
164                 case UNIX_TYPE_BLKDEV:
165                         return S_IFBLK;
166 #endif
167 #ifdef S_IFIFO
168                 case UNIX_TYPE_FIFO:
169                         return S_IFIFO;
170 #endif
171 #ifdef S_IFSOCK
172                 case UNIX_TYPE_SOCKET:
173                         return S_IFSOCK;
174 #endif
175                 default:
176                         return (mode_t)0;
177         }
178 }
179
180 /****************************************************************************
181  Do a POSIX getfacl (UNIX extensions).
182 ****************************************************************************/
183
184 bool cli_unix_getfacl(struct cli_state *cli, const char *name, size_t *prb_size, char **retbuf)
185 {
186         unsigned int param_len = 0;
187         unsigned int data_len = 0;
188         uint16 setup = TRANSACT2_QPATHINFO;
189         char *param;
190         size_t nlen = 2*(strlen(name)+1);
191         char *rparam=NULL, *rdata=NULL;
192         char *p;
193
194         param = SMB_MALLOC(6+nlen+2);
195         if (!param) {
196                 return false;
197         }
198
199         p = param;
200         memset(p, '\0', 6);
201         SSVAL(p, 0, SMB_QUERY_POSIX_ACL);
202         p += 6;
203         p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
204         param_len = PTR_DIFF(p, param);
205
206         if (!cli_send_trans(cli, SMBtrans2,
207                 NULL,                        /* name */
208                 -1, 0,                       /* fid, flags */
209                 &setup, 1, 0,                /* setup, length, max */
210                 param, param_len, 2,         /* param, length, max */
211                 NULL,  0, cli->max_xmit      /* data, length, max */
212                 )) {
213                 SAFE_FREE(param);
214                 return false;
215         }
216
217         SAFE_FREE(param);
218
219         if (!cli_receive_trans(cli, SMBtrans2,
220                         &rparam, &param_len,
221                         &rdata, &data_len)) {
222                 return false;
223         }
224
225         if (data_len < 6) {
226                 SAFE_FREE(rdata);
227                 SAFE_FREE(rparam);
228                 return false;
229         }
230
231         SAFE_FREE(rparam);
232         *retbuf = rdata;
233         *prb_size = (size_t)data_len;
234
235         return true;
236 }
237
238 /****************************************************************************
239  Stat a file (UNIX extensions).
240 ****************************************************************************/
241
242 bool cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbuf)
243 {
244         unsigned int param_len = 0;
245         unsigned int data_len = 0;
246         uint16 setup = TRANSACT2_QPATHINFO;
247         char *param;
248         size_t nlen = 2*(strlen(name)+1);
249         char *rparam=NULL, *rdata=NULL;
250         char *p;
251
252         ZERO_STRUCTP(sbuf);
253
254         param = SMB_MALLOC(6+nlen+2);
255         if (!param) {
256                 return false;
257         }
258         p = param;
259         memset(p, '\0', 6);
260         SSVAL(p, 0, SMB_QUERY_FILE_UNIX_BASIC);
261         p += 6;
262         p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
263         param_len = PTR_DIFF(p, param);
264
265         if (!cli_send_trans(cli, SMBtrans2,
266                         NULL,                        /* name */
267                         -1, 0,                       /* fid, flags */
268                         &setup, 1, 0,                /* setup, length, max */
269                         param, param_len, 2,         /* param, length, max */
270                         NULL,  0, cli->max_xmit      /* data, length, max */
271                         )) {
272                 SAFE_FREE(param);
273                 return false;
274         }
275
276         SAFE_FREE(param);
277
278         if (!cli_receive_trans(cli, SMBtrans2,
279                         &rparam, &param_len,
280                         &rdata, &data_len)) {
281                 return false;
282         }
283
284         if (data_len < 96) {
285                 SAFE_FREE(rdata);
286                 SAFE_FREE(rparam);
287                 return false;
288         }
289
290         sbuf->st_size = IVAL2_TO_SMB_BIG_UINT(rdata,0);     /* total size, in bytes */
291         sbuf->st_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8);   /* number of blocks allocated */
292 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
293         sbuf->st_blocks /= STAT_ST_BLOCKSIZE;
294 #else
295         /* assume 512 byte blocks */
296         sbuf->st_blocks /= 512;
297 #endif
298         set_ctimespec(sbuf, interpret_long_date(rdata + 16));    /* time of last change */
299         set_atimespec(sbuf, interpret_long_date(rdata + 24));    /* time of last access */
300         set_mtimespec(sbuf, interpret_long_date(rdata + 32));    /* time of last modification */
301
302         sbuf->st_uid = (uid_t) IVAL(rdata,40);      /* user ID of owner */
303         sbuf->st_gid = (gid_t) IVAL(rdata,48);      /* group ID of owner */
304         sbuf->st_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
305 #if defined(HAVE_MAKEDEV)
306         {
307                 uint32 dev_major = IVAL(rdata,60);
308                 uint32 dev_minor = IVAL(rdata,68);
309                 sbuf->st_rdev = makedev(dev_major, dev_minor);
310         }
311 #endif
312         sbuf->st_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76);      /* inode */
313         sbuf->st_mode |= wire_perms_to_unix(IVAL(rdata,84));     /* protection */
314         sbuf->st_nlink = IVAL(rdata,92);    /* number of hard links */
315
316         SAFE_FREE(rdata);
317         SAFE_FREE(rparam);
318
319         return true;
320 }
321
322 /****************************************************************************
323  Symlink a file (UNIX extensions).
324 ****************************************************************************/
325
326 bool cli_unix_symlink(struct cli_state *cli, const char *oldname, const char *newname)
327 {
328         return cli_link_internal(cli, oldname, newname, False);
329 }
330
331 /****************************************************************************
332  Hard a file (UNIX extensions).
333 ****************************************************************************/
334
335 bool cli_unix_hardlink(struct cli_state *cli, const char *oldname, const char *newname)
336 {
337         return cli_link_internal(cli, oldname, newname, True);
338 }
339
340 /****************************************************************************
341  Chmod or chown a file internal (UNIX extensions).
342 ****************************************************************************/
343
344 static bool cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32 mode, uint32 uid, uint32 gid)
345 {
346         unsigned int data_len = 0;
347         unsigned int param_len = 0;
348         uint16 setup = TRANSACT2_SETPATHINFO;
349         size_t nlen = 2*(strlen(fname)+1);
350         char *param;
351         char data[100];
352         char *rparam=NULL, *rdata=NULL;
353         char *p;
354
355         param = SMB_MALLOC(6+nlen+2);
356         if (!param) {
357                 return false;
358         }
359         memset(param, '\0', 6);
360         memset(data, 0, sizeof(data));
361
362         SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
363         p = &param[6];
364
365         p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
366         param_len = PTR_DIFF(p, param);
367
368         memset(data, 0xff, 40); /* Set all sizes/times to no change. */
369
370         SIVAL(data,40,uid);
371         SIVAL(data,48,gid);
372         SIVAL(data,84,mode);
373
374         data_len = 100;
375
376         if (!cli_send_trans(cli, SMBtrans2,
377                         NULL,                        /* name */
378                         -1, 0,                          /* fid, flags */
379                         &setup, 1, 0,                   /* setup, length, max */
380                         param, param_len, 2,            /* param, length, max */
381                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
382                         )) {
383                 SAFE_FREE(param);
384                 return False;
385         }
386
387         SAFE_FREE(param);
388
389         if (!cli_receive_trans(cli, SMBtrans2,
390                         &rparam, &param_len,
391                         &rdata, &data_len)) {
392                 return false;
393         }
394
395         SAFE_FREE(rdata);
396         SAFE_FREE(rparam);
397
398         return true;
399 }
400
401 /****************************************************************************
402  chmod a file (UNIX extensions).
403 ****************************************************************************/
404
405 bool cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
406 {
407         return cli_unix_chmod_chown_internal(cli, fname,
408                 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
409 }
410
411 /****************************************************************************
412  chown a file (UNIX extensions).
413 ****************************************************************************/
414
415 bool cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
416 {
417         return cli_unix_chmod_chown_internal(cli, fname,
418                         SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
419 }
420
421 /****************************************************************************
422  Rename a file.
423 ****************************************************************************/
424
425 bool cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
426 {
427         char *p;
428
429         memset(cli->outbuf,'\0',smb_size);
430         memset(cli->inbuf,'\0',smb_size);
431
432         set_message(cli->outbuf,1, 0, true);
433
434         SCVAL(cli->outbuf,smb_com,SMBmv);
435         SSVAL(cli->outbuf,smb_tid,cli->cnum);
436         cli_setup_packet(cli);
437
438         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
439
440         p = smb_buf(cli->outbuf);
441         *p++ = 4;
442         p += clistr_push(cli, p, fname_src,
443                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
444         *p++ = 4;
445         p += clistr_push(cli, p, fname_dst,
446                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
447
448         cli_setup_bcc(cli, p);
449
450         cli_send_smb(cli);
451         if (!cli_receive_smb(cli)) {
452                 return false;
453         }
454
455         if (cli_is_error(cli)) {
456                 return false;
457         }
458
459         return true;
460 }
461
462 /****************************************************************************
463  NT Rename a file.
464 ****************************************************************************/
465
466 bool cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
467 {
468         char *p;
469
470         memset(cli->outbuf,'\0',smb_size);
471         memset(cli->inbuf,'\0',smb_size);
472
473         set_message(cli->outbuf, 4, 0, true);
474
475         SCVAL(cli->outbuf,smb_com,SMBntrename);
476         SSVAL(cli->outbuf,smb_tid,cli->cnum);
477         cli_setup_packet(cli);
478
479         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
480         SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_RENAME);
481
482         p = smb_buf(cli->outbuf);
483         *p++ = 4;
484         p += clistr_push(cli, p, fname_src,
485                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
486         *p++ = 4;
487         p += clistr_push(cli, p, fname_dst,
488                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
489
490         cli_setup_bcc(cli, p);
491
492         cli_send_smb(cli);
493         if (!cli_receive_smb(cli)) {
494                 return false;
495         }
496
497         if (cli_is_error(cli)) {
498                 return false;
499         }
500
501         return true;
502 }
503
504 /****************************************************************************
505  NT hardlink a file.
506 ****************************************************************************/
507
508 bool cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
509 {
510         char *p;
511
512         memset(cli->outbuf,'\0',smb_size);
513         memset(cli->inbuf,'\0',smb_size);
514
515         set_message(cli->outbuf, 4, 0, true);
516
517         SCVAL(cli->outbuf,smb_com,SMBntrename);
518         SSVAL(cli->outbuf,smb_tid,cli->cnum);
519         cli_setup_packet(cli);
520
521         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
522         SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_HARD_LINK);
523
524         p = smb_buf(cli->outbuf);
525         *p++ = 4;
526         p += clistr_push(cli, p, fname_src,
527                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
528         *p++ = 4;
529         p += clistr_push(cli, p, fname_dst,
530                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
531
532         cli_setup_bcc(cli, p);
533
534         cli_send_smb(cli);
535         if (!cli_receive_smb(cli)) {
536                 return false;
537         }
538
539         if (cli_is_error(cli)) {
540                 return false;
541         }
542
543         return true;
544 }
545
546 /****************************************************************************
547  Delete a file.
548 ****************************************************************************/
549
550 bool cli_unlink_full(struct cli_state *cli, const char *fname, uint16 attrs)
551 {
552         char *p;
553
554         memset(cli->outbuf,'\0',smb_size);
555         memset(cli->inbuf,'\0',smb_size);
556
557         set_message(cli->outbuf,1, 0, true);
558
559         SCVAL(cli->outbuf,smb_com,SMBunlink);
560         SSVAL(cli->outbuf,smb_tid,cli->cnum);
561         cli_setup_packet(cli);
562
563         SSVAL(cli->outbuf,smb_vwv0, attrs);
564
565         p = smb_buf(cli->outbuf);
566         *p++ = 4;
567         p += clistr_push(cli, p, fname,
568                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
569
570         cli_setup_bcc(cli, p);
571         cli_send_smb(cli);
572         if (!cli_receive_smb(cli)) {
573                 return false;
574         }
575
576         if (cli_is_error(cli)) {
577                 return false;
578         }
579
580         return true;
581 }
582
583 /****************************************************************************
584  Delete a file.
585 ****************************************************************************/
586
587 bool cli_unlink(struct cli_state *cli, const char *fname)
588 {
589         return cli_unlink_full(cli, fname, aSYSTEM | aHIDDEN);
590 }
591
592 /****************************************************************************
593  Create a directory.
594 ****************************************************************************/
595
596 bool cli_mkdir(struct cli_state *cli, const char *dname)
597 {
598         char *p;
599
600         memset(cli->outbuf,'\0',smb_size);
601         memset(cli->inbuf,'\0',smb_size);
602
603         set_message(cli->outbuf,0, 0, true);
604
605         SCVAL(cli->outbuf,smb_com,SMBmkdir);
606         SSVAL(cli->outbuf,smb_tid,cli->cnum);
607         cli_setup_packet(cli);
608
609         p = smb_buf(cli->outbuf);
610         *p++ = 4;
611         p += clistr_push(cli, p, dname,
612                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
613
614         cli_setup_bcc(cli, p);
615
616         cli_send_smb(cli);
617         if (!cli_receive_smb(cli)) {
618                 return False;
619         }
620
621         if (cli_is_error(cli)) {
622                 return False;
623         }
624
625         return True;
626 }
627
628 /****************************************************************************
629  Remove a directory.
630 ****************************************************************************/
631
632 bool cli_rmdir(struct cli_state *cli, const char *dname)
633 {
634         char *p;
635
636         memset(cli->outbuf,'\0',smb_size);
637         memset(cli->inbuf,'\0',smb_size);
638
639         set_message(cli->outbuf,0, 0, true);
640
641         SCVAL(cli->outbuf,smb_com,SMBrmdir);
642         SSVAL(cli->outbuf,smb_tid,cli->cnum);
643         cli_setup_packet(cli);
644
645         p = smb_buf(cli->outbuf);
646         *p++ = 4;
647         p += clistr_push(cli, p, dname,
648                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
649
650         cli_setup_bcc(cli, p);
651
652         cli_send_smb(cli);
653         if (!cli_receive_smb(cli)) {
654                 return false;
655         }
656
657         if (cli_is_error(cli)) {
658                 return false;
659         }
660
661         return true;
662 }
663
664 /****************************************************************************
665  Set or clear the delete on close flag.
666 ****************************************************************************/
667
668 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, bool flag)
669 {
670         unsigned int data_len = 1;
671         unsigned int param_len = 6;
672         uint16 setup = TRANSACT2_SETFILEINFO;
673         char param[6];
674         unsigned char data;
675         char *rparam=NULL, *rdata=NULL;
676
677         memset(param, 0, param_len);
678         SSVAL(param,0,fnum);
679         SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
680
681         data = flag ? 1 : 0;
682
683         if (!cli_send_trans(cli, SMBtrans2,
684                         NULL,                        /* name */
685                         -1, 0,                          /* fid, flags */
686                         &setup, 1, 0,                   /* setup, length, max */
687                         param, param_len, 2,            /* param, length, max */
688                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
689                         )) {
690                 return false;
691         }
692
693         if (!cli_receive_trans(cli, SMBtrans2,
694                         &rparam, &param_len,
695                         &rdata, &data_len)) {
696                 return false;
697         }
698
699         SAFE_FREE(rdata);
700         SAFE_FREE(rparam);
701
702         return true;
703 }
704
705 /****************************************************************************
706  Open a file - exposing the full horror of the NT API :-).
707  Used in smbtorture.
708 ****************************************************************************/
709
710 int cli_nt_create_full(struct cli_state *cli, const char *fname,
711                  uint32 CreatFlags, uint32 DesiredAccess,
712                  uint32 FileAttributes, uint32 ShareAccess,
713                  uint32 CreateDisposition, uint32 CreateOptions,
714                  uint8 SecuityFlags)
715 {
716         char *p;
717         int len;
718
719         memset(cli->outbuf,'\0',smb_size);
720         memset(cli->inbuf,'\0',smb_size);
721
722         set_message(cli->outbuf,24,0, true);
723
724         SCVAL(cli->outbuf,smb_com,SMBntcreateX);
725         SSVAL(cli->outbuf,smb_tid,cli->cnum);
726         cli_setup_packet(cli);
727
728         SSVAL(cli->outbuf,smb_vwv0,0xFF);
729         if (cli->use_oplocks)
730                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
731
732         SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
733         SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
734         SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
735         SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
736         SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
737         SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
738         SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
739         SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
740         SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecuityFlags);
741
742         p = smb_buf(cli->outbuf);
743         /* this alignment and termination is critical for netapp filers. Don't change */
744         p += clistr_align_out(cli, p, 0);
745         len = clistr_push(cli, p, fname,
746                         cli->bufsize - PTR_DIFF(p,cli->outbuf), 0);
747         p += len;
748         SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
749         /* sigh. this copes with broken netapp filer behaviour */
750         p += clistr_push(cli, p, "",
751                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
752
753         cli_setup_bcc(cli, p);
754
755         cli_send_smb(cli);
756         if (!cli_receive_smb(cli)) {
757                 return -1;
758         }
759
760         if (cli_is_error(cli)) {
761                 return -1;
762         }
763
764         return SVAL(cli->inbuf,smb_vwv2 + 1);
765 }
766
767 /****************************************************************************
768  Open a file.
769 ****************************************************************************/
770
771 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
772 {
773         return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
774                                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
775 }
776
777 /****************************************************************************
778  Open a file
779  WARNING: if you open with O_WRONLY then getattrE won't work!
780 ****************************************************************************/
781
782 int cli_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
783 {
784         char *p;
785         unsigned openfn=0;
786         unsigned accessmode=0;
787
788         if (flags & O_CREAT)
789                 openfn |= (1<<4);
790         if (!(flags & O_EXCL)) {
791                 if (flags & O_TRUNC)
792                         openfn |= (1<<1);
793                 else
794                         openfn |= (1<<0);
795         }
796
797         accessmode = (share_mode<<4);
798
799         if ((flags & O_ACCMODE) == O_RDWR) {
800                 accessmode |= 2;
801         } else if ((flags & O_ACCMODE) == O_WRONLY) {
802                 accessmode |= 1;
803         }
804
805 #if defined(O_SYNC)
806         if ((flags & O_SYNC) == O_SYNC) {
807                 accessmode |= (1<<14);
808         }
809 #endif /* O_SYNC */
810
811         if (share_mode == DENY_FCB) {
812                 accessmode = 0xFF;
813         }
814
815         memset(cli->outbuf,'\0',smb_size);
816         memset(cli->inbuf,'\0',smb_size);
817
818         set_message(cli->outbuf,15,0, true);
819
820         SCVAL(cli->outbuf,smb_com,SMBopenX);
821         SSVAL(cli->outbuf,smb_tid,cli->cnum);
822         cli_setup_packet(cli);
823
824         SSVAL(cli->outbuf,smb_vwv0,0xFF);
825         SSVAL(cli->outbuf,smb_vwv2,0);  /* no additional info */
826         SSVAL(cli->outbuf,smb_vwv3,accessmode);
827         SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
828         SSVAL(cli->outbuf,smb_vwv5,0);
829         SSVAL(cli->outbuf,smb_vwv8,openfn);
830
831         if (cli->use_oplocks) {
832                 /* if using oplocks then ask for a batch oplock via
833                    core and extended methods */
834                 SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
835                         FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
836                 SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
837         }
838
839         p = smb_buf(cli->outbuf);
840         p += clistr_push(cli, p, fname,
841                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
842
843         cli_setup_bcc(cli, p);
844
845         cli_send_smb(cli);
846         if (!cli_receive_smb(cli)) {
847                 return -1;
848         }
849
850         if (cli_is_error(cli)) {
851                 return -1;
852         }
853
854         return SVAL(cli->inbuf,smb_vwv2);
855 }
856
857 /****************************************************************************
858  Close a file.
859 ****************************************************************************/
860
861 bool cli_close(struct cli_state *cli, int fnum)
862 {
863         memset(cli->outbuf,'\0',smb_size);
864         memset(cli->inbuf,'\0',smb_size);
865
866         set_message(cli->outbuf,3,0,True);
867
868         SCVAL(cli->outbuf,smb_com,SMBclose);
869         SSVAL(cli->outbuf,smb_tid,cli->cnum);
870         cli_setup_packet(cli);
871
872         SSVAL(cli->outbuf,smb_vwv0,fnum);
873         SIVALS(cli->outbuf,smb_vwv1,-1);
874
875         cli_send_smb(cli);
876         if (!cli_receive_smb(cli)) {
877                 return False;
878         }
879
880         return !cli_is_error(cli);
881 }
882
883
884 /****************************************************************************
885  send a lock with a specified locktype
886  this is used for testing LOCKING_ANDX_CANCEL_LOCK
887 ****************************************************************************/
888
889 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
890                       uint32 offset, uint32 len,
891                       int timeout, unsigned char locktype)
892 {
893         char *p;
894         int saved_timeout = cli->timeout;
895
896         memset(cli->outbuf,'\0',smb_size);
897         memset(cli->inbuf,'\0', smb_size);
898
899         set_message(cli->outbuf,8,0,True);
900
901         SCVAL(cli->outbuf,smb_com,SMBlockingX);
902         SSVAL(cli->outbuf,smb_tid,cli->cnum);
903         cli_setup_packet(cli);
904
905         SCVAL(cli->outbuf,smb_vwv0,0xFF);
906         SSVAL(cli->outbuf,smb_vwv2,fnum);
907         SCVAL(cli->outbuf,smb_vwv3,locktype);
908         SIVALS(cli->outbuf, smb_vwv4, timeout);
909         SSVAL(cli->outbuf,smb_vwv6,0);
910         SSVAL(cli->outbuf,smb_vwv7,1);
911
912         p = smb_buf(cli->outbuf);
913         SSVAL(p, 0, cli->pid);
914         SIVAL(p, 2, offset);
915         SIVAL(p, 6, len);
916
917         p += 10;
918
919         cli_setup_bcc(cli, p);
920
921         cli_send_smb(cli);
922
923         if (timeout != 0) {
924                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
925         }
926
927         if (!cli_receive_smb(cli)) {
928                 cli->timeout = saved_timeout;
929                 return NT_STATUS_UNSUCCESSFUL;
930         }
931
932         cli->timeout = saved_timeout;
933
934         return cli_nt_error(cli);
935 }
936
937 /****************************************************************************
938  Lock a file.
939  note that timeout is in units of 2 milliseconds
940 ****************************************************************************/
941
942 bool cli_lock(struct cli_state *cli, int fnum,
943               uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
944 {
945         char *p;
946         int saved_timeout = cli->timeout;
947
948         memset(cli->outbuf,'\0',smb_size);
949         memset(cli->inbuf,'\0', smb_size);
950
951         set_message(cli->outbuf,8,0,True);
952
953         SCVAL(cli->outbuf,smb_com,SMBlockingX);
954         SSVAL(cli->outbuf,smb_tid,cli->cnum);
955         cli_setup_packet(cli);
956
957         SCVAL(cli->outbuf,smb_vwv0,0xFF);
958         SSVAL(cli->outbuf,smb_vwv2,fnum);
959         SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
960         SIVALS(cli->outbuf, smb_vwv4, timeout);
961         SSVAL(cli->outbuf,smb_vwv6,0);
962         SSVAL(cli->outbuf,smb_vwv7,1);
963
964         p = smb_buf(cli->outbuf);
965         SSVAL(p, 0, cli->pid);
966         SIVAL(p, 2, offset);
967         SIVAL(p, 6, len);
968
969         p += 10;
970
971         cli_setup_bcc(cli, p);
972
973         cli_send_smb(cli);
974
975         if (timeout != 0) {
976                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
977         }
978
979         if (!cli_receive_smb(cli)) {
980                 cli->timeout = saved_timeout;
981                 return False;
982         }
983
984         cli->timeout = saved_timeout;
985
986         if (cli_is_error(cli)) {
987                 return False;
988         }
989
990         return True;
991 }
992
993 /****************************************************************************
994  Unlock a file.
995 ****************************************************************************/
996
997 bool cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
998 {
999         char *p;
1000
1001         memset(cli->outbuf,'\0',smb_size);
1002         memset(cli->inbuf,'\0',smb_size);
1003
1004         set_message(cli->outbuf,8,0,True);
1005
1006         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1007         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1008         cli_setup_packet(cli);
1009
1010         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1011         SSVAL(cli->outbuf,smb_vwv2,fnum);
1012         SCVAL(cli->outbuf,smb_vwv3,0);
1013         SIVALS(cli->outbuf, smb_vwv4, 0);
1014         SSVAL(cli->outbuf,smb_vwv6,1);
1015         SSVAL(cli->outbuf,smb_vwv7,0);
1016
1017         p = smb_buf(cli->outbuf);
1018         SSVAL(p, 0, cli->pid);
1019         SIVAL(p, 2, offset);
1020         SIVAL(p, 6, len);
1021         p += 10;
1022         cli_setup_bcc(cli, p);
1023         cli_send_smb(cli);
1024         if (!cli_receive_smb(cli)) {
1025                 return False;
1026         }
1027
1028         if (cli_is_error(cli)) {
1029                 return False;
1030         }
1031
1032         return True;
1033 }
1034
1035 /****************************************************************************
1036  Lock a file with 64 bit offsets.
1037 ****************************************************************************/
1038
1039 bool cli_lock64(struct cli_state *cli, int fnum,
1040                 SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
1041 {
1042         char *p;
1043         int saved_timeout = cli->timeout;
1044         int ltype;
1045
1046         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1047                 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1048         }
1049
1050         ltype = (lock_type == READ_LOCK? 1 : 0);
1051         ltype |= LOCKING_ANDX_LARGE_FILES;
1052
1053         memset(cli->outbuf,'\0',smb_size);
1054         memset(cli->inbuf,'\0', smb_size);
1055
1056         set_message(cli->outbuf,8,0,True);
1057
1058         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1059         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1060         cli_setup_packet(cli);
1061
1062         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1063         SSVAL(cli->outbuf,smb_vwv2,fnum);
1064         SCVAL(cli->outbuf,smb_vwv3,ltype);
1065         SIVALS(cli->outbuf, smb_vwv4, timeout);
1066         SSVAL(cli->outbuf,smb_vwv6,0);
1067         SSVAL(cli->outbuf,smb_vwv7,1);
1068
1069         p = smb_buf(cli->outbuf);
1070         SIVAL(p, 0, cli->pid);
1071         SOFF_T_R(p, 4, offset);
1072         SOFF_T_R(p, 12, len);
1073         p += 20;
1074
1075         cli_setup_bcc(cli, p);
1076         cli_send_smb(cli);
1077
1078         if (timeout != 0) {
1079                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1080         }
1081
1082         if (!cli_receive_smb(cli)) {
1083                 cli->timeout = saved_timeout;
1084                 return False;
1085         }
1086
1087         cli->timeout = saved_timeout;
1088
1089         if (cli_is_error(cli)) {
1090                 return False;
1091         }
1092
1093         return True;
1094 }
1095
1096 /****************************************************************************
1097  Unlock a file with 64 bit offsets.
1098 ****************************************************************************/
1099
1100 bool cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1101 {
1102         char *p;
1103
1104         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1105                 return cli_unlock(cli, fnum, offset, len);
1106         }
1107
1108         memset(cli->outbuf,'\0',smb_size);
1109         memset(cli->inbuf,'\0',smb_size);
1110
1111         set_message(cli->outbuf,8,0,True);
1112
1113         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1114         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1115         cli_setup_packet(cli);
1116
1117         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1118         SSVAL(cli->outbuf,smb_vwv2,fnum);
1119         SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1120         SIVALS(cli->outbuf, smb_vwv4, 0);
1121         SSVAL(cli->outbuf,smb_vwv6,1);
1122         SSVAL(cli->outbuf,smb_vwv7,0);
1123
1124         p = smb_buf(cli->outbuf);
1125         SIVAL(p, 0, cli->pid);
1126         SOFF_T_R(p, 4, offset);
1127         SOFF_T_R(p, 12, len);
1128         p += 20;
1129         cli_setup_bcc(cli, p);
1130         cli_send_smb(cli);
1131         if (!cli_receive_smb(cli)) {
1132                 return False;
1133         }
1134
1135         if (cli_is_error(cli)) {
1136                 return False;
1137         }
1138
1139         return True;
1140 }
1141
1142 /****************************************************************************
1143  Get/unlock a POSIX lock on a file - internal function.
1144 ****************************************************************************/
1145
1146 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
1147                 SMB_BIG_UINT offset, SMB_BIG_UINT len, bool wait_lock, enum brl_type lock_type)
1148 {
1149         unsigned int param_len = 4;
1150         unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1151         uint16 setup = TRANSACT2_SETFILEINFO;
1152         char param[4];
1153         unsigned char data[POSIX_LOCK_DATA_SIZE];
1154         char *rparam=NULL, *rdata=NULL;
1155         int saved_timeout = cli->timeout;
1156
1157         SSVAL(param,0,fnum);
1158         SSVAL(param,2,SMB_SET_POSIX_LOCK);
1159
1160         switch (lock_type) {
1161                 case READ_LOCK:
1162                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1163                         break;
1164                 case WRITE_LOCK:
1165                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1166                         break;
1167                 case UNLOCK_LOCK:
1168                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1169                         break;
1170                 default:
1171                         return False;
1172         }
1173
1174         if (wait_lock) {
1175                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1176                 cli->timeout = 0x7FFFFFFF;
1177         } else {
1178                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1179         }
1180
1181         SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1182         SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1183         SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
1184
1185         if (!cli_send_trans(cli, SMBtrans2,
1186                         NULL,                        /* name */
1187                         -1, 0,                          /* fid, flags */
1188                         &setup, 1, 0,                   /* setup, length, max */
1189                         param, param_len, 2,            /* param, length, max */
1190                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
1191                         )) {
1192                 cli->timeout = saved_timeout;
1193                 return False;
1194         }
1195
1196         if (!cli_receive_trans(cli, SMBtrans2,
1197                                 &rparam, &param_len,
1198                                 &rdata, &data_len)) {
1199                 cli->timeout = saved_timeout;
1200                 SAFE_FREE(rdata);
1201                 SAFE_FREE(rparam);
1202                 return False;
1203         }
1204
1205         cli->timeout = saved_timeout;
1206
1207         SAFE_FREE(rdata);
1208         SAFE_FREE(rparam);
1209
1210         return True;
1211 }
1212
1213 /****************************************************************************
1214  POSIX Lock a file.
1215 ****************************************************************************/
1216
1217 bool cli_posix_lock(struct cli_state *cli, int fnum,
1218                         SMB_BIG_UINT offset, SMB_BIG_UINT len,
1219                         bool wait_lock, enum brl_type lock_type)
1220 {
1221         if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
1222                 return False;
1223         }
1224         return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
1225 }
1226
1227 /****************************************************************************
1228  POSIX Unlock a file.
1229 ****************************************************************************/
1230
1231 bool cli_posix_unlock(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1232 {
1233         return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
1234 }
1235
1236 /****************************************************************************
1237  POSIX Get any lock covering a file.
1238 ****************************************************************************/
1239
1240 bool cli_posix_getlock(struct cli_state *cli, int fnum, SMB_BIG_UINT *poffset, SMB_BIG_UINT *plen)
1241 {
1242         return True;
1243 }
1244
1245 /****************************************************************************
1246  Do a SMBgetattrE call.
1247 ****************************************************************************/
1248
1249 bool cli_getattrE(struct cli_state *cli, int fd,
1250                   uint16 *attr, SMB_OFF_T *size,
1251                   time_t *change_time,
1252                   time_t *access_time,
1253                   time_t *write_time)
1254 {
1255         memset(cli->outbuf,'\0',smb_size);
1256         memset(cli->inbuf,'\0',smb_size);
1257
1258         set_message(cli->outbuf,1,0,True);
1259
1260         SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1261         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1262         cli_setup_packet(cli);
1263
1264         SSVAL(cli->outbuf,smb_vwv0,fd);
1265
1266         cli_send_smb(cli);
1267         if (!cli_receive_smb(cli)) {
1268                 return False;
1269         }
1270
1271         if (cli_is_error(cli)) {
1272                 return False;
1273         }
1274
1275         if (size) {
1276                 *size = IVAL(cli->inbuf, smb_vwv6);
1277         }
1278
1279         if (attr) {
1280                 *attr = SVAL(cli->inbuf,smb_vwv10);
1281         }
1282
1283         if (change_time) {
1284                 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
1285         }
1286
1287         if (access_time) {
1288                 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
1289         }
1290
1291         if (write_time) {
1292                 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
1293         }
1294
1295         return True;
1296 }
1297
1298 /****************************************************************************
1299  Do a SMBgetatr call
1300 ****************************************************************************/
1301
1302 bool cli_getatr(struct cli_state *cli, const char *fname,
1303                 uint16 *attr, SMB_OFF_T *size, time_t *write_time)
1304 {
1305         char *p;
1306
1307         memset(cli->outbuf,'\0',smb_size);
1308         memset(cli->inbuf,'\0',smb_size);
1309
1310         set_message(cli->outbuf,0,0,True);
1311
1312         SCVAL(cli->outbuf,smb_com,SMBgetatr);
1313         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1314         cli_setup_packet(cli);
1315
1316         p = smb_buf(cli->outbuf);
1317         *p++ = 4;
1318         p += clistr_push(cli, p, fname,
1319                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1320
1321         cli_setup_bcc(cli, p);
1322
1323         cli_send_smb(cli);
1324         if (!cli_receive_smb(cli)) {
1325                 return False;
1326         }
1327
1328         if (cli_is_error(cli)) {
1329                 return False;
1330         }
1331
1332         if (size) {
1333                 *size = IVAL(cli->inbuf, smb_vwv3);
1334         }
1335
1336         if (write_time) {
1337                 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
1338         }
1339
1340         if (attr) {
1341                 *attr = SVAL(cli->inbuf,smb_vwv0);
1342         }
1343
1344         return True;
1345 }
1346
1347 /****************************************************************************
1348  Do a SMBsetattrE call.
1349 ****************************************************************************/
1350
1351 bool cli_setattrE(struct cli_state *cli, int fd,
1352                   time_t change_time,
1353                   time_t access_time,
1354                   time_t write_time)
1355
1356 {
1357         char *p;
1358
1359         memset(cli->outbuf,'\0',smb_size);
1360         memset(cli->inbuf,'\0',smb_size);
1361
1362         set_message(cli->outbuf,7,0,True);
1363
1364         SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1365         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1366         cli_setup_packet(cli);
1367
1368         SSVAL(cli->outbuf,smb_vwv0, fd);
1369         cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
1370         cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
1371         cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
1372
1373         p = smb_buf(cli->outbuf);
1374         *p++ = 4;
1375
1376         cli_setup_bcc(cli, p);
1377
1378         cli_send_smb(cli);
1379         if (!cli_receive_smb(cli)) {
1380                 return False;
1381         }
1382
1383         if (cli_is_error(cli)) {
1384                 return False;
1385         }
1386
1387         return True;
1388 }
1389
1390 /****************************************************************************
1391  Do a SMBsetatr call.
1392 ****************************************************************************/
1393
1394 bool cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1395 {
1396         char *p;
1397
1398         memset(cli->outbuf,'\0',smb_size);
1399         memset(cli->inbuf,'\0',smb_size);
1400
1401         set_message(cli->outbuf,8,0,True);
1402
1403         SCVAL(cli->outbuf,smb_com,SMBsetatr);
1404         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1405         cli_setup_packet(cli);
1406
1407         SSVAL(cli->outbuf,smb_vwv0, attr);
1408         cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
1409
1410         p = smb_buf(cli->outbuf);
1411         *p++ = 4;
1412         p += clistr_push(cli, p, fname,
1413                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1414         *p++ = 4;
1415
1416         cli_setup_bcc(cli, p);
1417
1418         cli_send_smb(cli);
1419         if (!cli_receive_smb(cli)) {
1420                 return False;
1421         }
1422
1423         if (cli_is_error(cli)) {
1424                 return False;
1425         }
1426
1427         return True;
1428 }
1429
1430 /****************************************************************************
1431  Check for existance of a dir.
1432 ****************************************************************************/
1433
1434 bool cli_chkpath(struct cli_state *cli, const char *path)
1435 {
1436         char *path2 = NULL;
1437         char *p;
1438         TALLOC_CTX *frame = talloc_stackframe();
1439
1440         path2 = talloc_strdup(frame, path);
1441         if (!path2) {
1442                 TALLOC_FREE(frame);
1443                 return false;
1444         }
1445         trim_char(path2,'\0','\\');
1446         if (!*path2) {
1447                 path2 = talloc_strdup(frame, "\\");
1448                 if (!path2) {
1449                         TALLOC_FREE(frame);
1450                         return false;
1451                 }
1452         }
1453
1454         memset(cli->outbuf,'\0',smb_size);
1455         set_message(cli->outbuf,0,0,True);
1456         SCVAL(cli->outbuf,smb_com,SMBcheckpath);
1457         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1458         cli_setup_packet(cli);
1459         p = smb_buf(cli->outbuf);
1460         *p++ = 4;
1461         p += clistr_push(cli, p, path2,
1462                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1463
1464         cli_setup_bcc(cli, p);
1465
1466         cli_send_smb(cli);
1467         if (!cli_receive_smb(cli)) {
1468                 TALLOC_FREE(frame);
1469                 return False;
1470         }
1471
1472         TALLOC_FREE(frame);
1473
1474         if (cli_is_error(cli)) return False;
1475
1476         return True;
1477 }
1478
1479 /****************************************************************************
1480  Query disk space.
1481 ****************************************************************************/
1482
1483 bool cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1484 {
1485         memset(cli->outbuf,'\0',smb_size);
1486         set_message(cli->outbuf,0,0,True);
1487         SCVAL(cli->outbuf,smb_com,SMBdskattr);
1488         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1489         cli_setup_packet(cli);
1490
1491         cli_send_smb(cli);
1492         if (!cli_receive_smb(cli)) {
1493                 return False;
1494         }
1495
1496         *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1497         *total = SVAL(cli->inbuf,smb_vwv0);
1498         *avail = SVAL(cli->inbuf,smb_vwv3);
1499
1500         return True;
1501 }
1502
1503 /****************************************************************************
1504  Create and open a temporary file.
1505 ****************************************************************************/
1506
1507 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1508 {
1509         int len;
1510         char *p;
1511
1512         memset(cli->outbuf,'\0',smb_size);
1513         memset(cli->inbuf,'\0',smb_size);
1514
1515         set_message(cli->outbuf,3,0,True);
1516
1517         SCVAL(cli->outbuf,smb_com,SMBctemp);
1518         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1519         cli_setup_packet(cli);
1520
1521         SSVAL(cli->outbuf,smb_vwv0,0);
1522         SIVALS(cli->outbuf,smb_vwv1,-1);
1523
1524         p = smb_buf(cli->outbuf);
1525         *p++ = 4;
1526         p += clistr_push(cli, p, path,
1527                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1528
1529         cli_setup_bcc(cli, p);
1530
1531         cli_send_smb(cli);
1532         if (!cli_receive_smb(cli)) {
1533                 return -1;
1534         }
1535
1536         if (cli_is_error(cli)) {
1537                 return -1;
1538         }
1539
1540         /* despite the spec, the result has a -1, followed by
1541            length, followed by name */
1542         p = smb_buf(cli->inbuf);
1543         p += 4;
1544         len = smb_buflen(cli->inbuf) - 4;
1545         if (len <= 0 || len > PATH_MAX) return -1;
1546
1547         if (tmp_path) {
1548                 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
1549                 if (!path2) {
1550                         return -1;
1551                 }
1552                 clistr_pull(cli, path2, p,
1553                             len+1, len, STR_ASCII);
1554                 *tmp_path = path2;
1555         }
1556
1557         return SVAL(cli->inbuf,smb_vwv0);
1558 }
1559
1560 /*
1561    send a raw ioctl - used by the torture code
1562 */
1563 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1564 {
1565         memset(cli->outbuf,'\0',smb_size);
1566         memset(cli->inbuf,'\0',smb_size);
1567
1568         set_message(cli->outbuf, 3, 0, True);
1569         SCVAL(cli->outbuf,smb_com,SMBioctl);
1570         cli_setup_packet(cli);
1571
1572         SSVAL(cli->outbuf, smb_vwv0, fnum);
1573         SSVAL(cli->outbuf, smb_vwv1, code>>16);
1574         SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1575
1576         cli_send_smb(cli);
1577         if (!cli_receive_smb(cli)) {
1578                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1579         }
1580
1581         if (cli_is_error(cli)) {
1582                 return cli_nt_error(cli);
1583         }
1584
1585         *blob = data_blob_null;
1586
1587         return NT_STATUS_OK;
1588 }
1589
1590 /*********************************************************
1591  Set an extended attribute utility fn.
1592 *********************************************************/
1593
1594 static bool cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1595                         const char *ea_name, const char *ea_val, size_t ea_len)
1596 {
1597         unsigned int data_len = 0;
1598         char *data = NULL;
1599         char *rparam=NULL, *rdata=NULL;
1600         char *p;
1601         size_t ea_namelen = strlen(ea_name);
1602
1603         if (ea_namelen == 0 && ea_len == 0) {
1604                 data_len = 4;
1605                 data = (char *)SMB_MALLOC(data_len);
1606                 if (!data) {
1607                         return False;
1608                 }
1609                 p = data;
1610                 SIVAL(p,0,data_len);
1611         } else {
1612                 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1613                 data = (char *)SMB_MALLOC(data_len);
1614                 if (!data) {
1615                         return False;
1616                 }
1617                 p = data;
1618                 SIVAL(p,0,data_len);
1619                 p += 4;
1620                 SCVAL(p, 0, 0); /* EA flags. */
1621                 SCVAL(p, 1, ea_namelen);
1622                 SSVAL(p, 2, ea_len);
1623                 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1624                 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1625         }
1626
1627         if (!cli_send_trans(cli, SMBtrans2,
1628                         NULL,                        /* name */
1629                         -1, 0,                          /* fid, flags */
1630                         &setup, 1, 0,                   /* setup, length, max */
1631                         param, param_len, 2,            /* param, length, max */
1632                         data,  data_len, cli->max_xmit /* data, length, max */
1633                         )) {
1634                 SAFE_FREE(data);
1635                 return False;
1636         }
1637
1638         if (!cli_receive_trans(cli, SMBtrans2,
1639                         &rparam, &param_len,
1640                         &rdata, &data_len)) {
1641                         SAFE_FREE(data);
1642                 return false;
1643         }
1644
1645         SAFE_FREE(data);
1646         SAFE_FREE(rdata);
1647         SAFE_FREE(rparam);
1648
1649         return True;
1650 }
1651
1652 /*********************************************************
1653  Set an extended attribute on a pathname.
1654 *********************************************************/
1655
1656 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1657 {
1658         uint16 setup = TRANSACT2_SETPATHINFO;
1659         unsigned int param_len = 0;
1660         char *param;
1661         size_t srclen = 2*(strlen(path)+1);
1662         char *p;
1663         bool ret;
1664
1665         param = SMB_MALLOC(6+srclen+2);
1666         if (!param) {
1667                 return false;
1668         }
1669         memset(param, '\0', 6);
1670         SSVAL(param,0,SMB_INFO_SET_EA);
1671         p = &param[6];
1672
1673         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
1674         param_len = PTR_DIFF(p, param);
1675
1676         ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
1677         SAFE_FREE(param);
1678         return ret;
1679 }
1680
1681 /*********************************************************
1682  Set an extended attribute on an fnum.
1683 *********************************************************/
1684
1685 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
1686 {
1687         char param[6];
1688         uint16 setup = TRANSACT2_SETFILEINFO;
1689
1690         memset(param, 0, 6);
1691         SSVAL(param,0,fnum);
1692         SSVAL(param,2,SMB_INFO_SET_EA);
1693
1694         return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
1695 }
1696
1697 /*********************************************************
1698  Get an extended attribute list tility fn.
1699 *********************************************************/
1700
1701 static bool cli_get_ea_list(struct cli_state *cli,
1702                 uint16 setup, char *param, unsigned int param_len,
1703                 TALLOC_CTX *ctx,
1704                 size_t *pnum_eas,
1705                 struct ea_struct **pea_list)
1706 {
1707         unsigned int data_len = 0;
1708         unsigned int rparam_len, rdata_len;
1709         char *rparam=NULL, *rdata=NULL;
1710         char *p;
1711         size_t ea_size;
1712         size_t num_eas;
1713         bool ret = False;
1714         struct ea_struct *ea_list;
1715
1716         *pnum_eas = 0;
1717         if (pea_list) {
1718                 *pea_list = NULL;
1719         }
1720
1721         if (!cli_send_trans(cli, SMBtrans2,
1722                         NULL,           /* Name */
1723                         -1, 0,          /* fid, flags */
1724                         &setup, 1, 0,   /* setup, length, max */
1725                         param, param_len, 10, /* param, length, max */
1726                         NULL, data_len, cli->max_xmit /* data, length, max */
1727                                 )) {
1728                 return False;
1729         }
1730
1731         if (!cli_receive_trans(cli, SMBtrans2,
1732                         &rparam, &rparam_len,
1733                         &rdata, &rdata_len)) {
1734                 return False;
1735         }
1736
1737         if (!rdata || rdata_len < 4) {
1738                 goto out;
1739         }
1740
1741         ea_size = (size_t)IVAL(rdata,0);
1742         if (ea_size > rdata_len) {
1743                 goto out;
1744         }
1745
1746         if (ea_size == 0) {
1747                 /* No EA's present. */
1748                 ret = True;
1749                 goto out;
1750         }
1751
1752         p = rdata + 4;
1753         ea_size -= 4;
1754
1755         /* Validate the EA list and count it. */
1756         for (num_eas = 0; ea_size >= 4; num_eas++) {
1757                 unsigned int ea_namelen = CVAL(p,1);
1758                 unsigned int ea_valuelen = SVAL(p,2);
1759                 if (ea_namelen == 0) {
1760                         goto out;
1761                 }
1762                 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
1763                         goto out;
1764                 }
1765                 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
1766                 p += 4 + ea_namelen + 1 + ea_valuelen;
1767         }
1768
1769         if (num_eas == 0) {
1770                 ret = True;
1771                 goto out;
1772         }
1773
1774         *pnum_eas = num_eas;
1775         if (!pea_list) {
1776                 /* Caller only wants number of EA's. */
1777                 ret = True;
1778                 goto out;
1779         }
1780
1781         ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
1782         if (!ea_list) {
1783                 goto out;
1784         }
1785
1786         ea_size = (size_t)IVAL(rdata,0);
1787         p = rdata + 4;
1788
1789         for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
1790                 struct ea_struct *ea = &ea_list[num_eas];
1791                 fstring unix_ea_name;
1792                 unsigned int ea_namelen = CVAL(p,1);
1793                 unsigned int ea_valuelen = SVAL(p,2);
1794
1795                 ea->flags = CVAL(p,0);
1796                 unix_ea_name[0] = '\0';
1797                 pull_ascii_fstring(unix_ea_name, p + 4);
1798                 ea->name = talloc_strdup(ctx, unix_ea_name);
1799                 /* Ensure the value is null terminated (in case it's a string). */
1800                 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
1801                 if (!ea->value.data) {
1802                         goto out;
1803                 }
1804                 if (ea_valuelen) {
1805                         memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
1806                 }
1807                 ea->value.data[ea_valuelen] = 0;
1808                 ea->value.length--;
1809                 p += 4 + ea_namelen + 1 + ea_valuelen;
1810         }
1811
1812         *pea_list = ea_list;
1813         ret = True;
1814
1815  out :
1816
1817         SAFE_FREE(rdata);
1818         SAFE_FREE(rparam);
1819         return ret;
1820 }
1821
1822 /*********************************************************
1823  Get an extended attribute list from a pathname.
1824 *********************************************************/
1825
1826 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
1827                 TALLOC_CTX *ctx,
1828                 size_t *pnum_eas,
1829                 struct ea_struct **pea_list)
1830 {
1831         uint16 setup = TRANSACT2_QPATHINFO;
1832         unsigned int param_len = 0;
1833         char *param;
1834         char *p;
1835         size_t srclen = 2*(strlen(path)+1);
1836         bool ret;
1837
1838         param = SMB_MALLOC(6+srclen+2);
1839         if (!param) {
1840                 return false;
1841         }
1842         p = param;
1843         memset(p, 0, 6);
1844         SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
1845         p += 6;
1846         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
1847         param_len = PTR_DIFF(p, param);
1848
1849         ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
1850         SAFE_FREE(param);
1851         return ret;
1852 }
1853
1854 /*********************************************************
1855  Get an extended attribute list from an fnum.
1856 *********************************************************/
1857
1858 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
1859                 TALLOC_CTX *ctx,
1860                 size_t *pnum_eas,
1861                 struct ea_struct **pea_list)
1862 {
1863         uint16 setup = TRANSACT2_QFILEINFO;
1864         char param[6];
1865
1866         memset(param, 0, 6);
1867         SSVAL(param,0,fnum);
1868         SSVAL(param,2,SMB_INFO_SET_EA);
1869
1870         return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
1871 }
1872
1873 /****************************************************************************
1874  Convert open "flags" arg to uint32 on wire.
1875 ****************************************************************************/
1876
1877 static uint32 open_flags_to_wire(int flags)
1878 {
1879         int open_mode = flags & O_ACCMODE;
1880         uint32 ret = 0;
1881
1882         switch (open_mode) {
1883                 case O_WRONLY:
1884                         ret |= SMB_O_WRONLY;
1885                         break;
1886                 case O_RDWR:
1887                         ret |= SMB_O_RDWR;
1888                         break;
1889                 default:
1890                 case O_RDONLY:
1891                         ret |= SMB_O_RDONLY;
1892                         break;
1893         }
1894
1895         if (flags & O_CREAT) {
1896                 ret |= SMB_O_CREAT;
1897         }
1898         if (flags & O_EXCL) {
1899                 ret |= SMB_O_EXCL;
1900         }
1901         if (flags & O_TRUNC) {
1902                 ret |= SMB_O_TRUNC;
1903         }
1904 #if defined(O_SYNC)
1905         if (flags & O_SYNC) {
1906                 ret |= SMB_O_SYNC;
1907         }
1908 #endif /* O_SYNC */
1909         if (flags & O_APPEND) {
1910                 ret |= SMB_O_APPEND;
1911         }
1912 #if defined(O_DIRECT)
1913         if (flags & O_DIRECT) {
1914                 ret |= SMB_O_DIRECT;
1915         }
1916 #endif
1917 #if defined(O_DIRECTORY)
1918         if (flags & O_DIRECTORY) {
1919                 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
1920                 ret |= SMB_O_DIRECTORY;
1921         }
1922 #endif
1923         return ret;
1924 }
1925
1926 /****************************************************************************
1927  Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
1928 ****************************************************************************/
1929
1930 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
1931 {
1932         unsigned int data_len = 0;
1933         unsigned int param_len = 0;
1934         uint16 setup = TRANSACT2_SETPATHINFO;
1935         char *param;
1936         char data[18];
1937         char *rparam=NULL, *rdata=NULL;
1938         char *p;
1939         int fnum = -1;
1940         uint32 wire_flags = open_flags_to_wire(flags);
1941         size_t srclen = 2*(strlen(fname)+1);
1942
1943         param = SMB_MALLOC(6+srclen+2);
1944         if (!param) {
1945                 return false;
1946         }
1947         memset(param, '\0', 6);
1948         SSVAL(param,0, SMB_POSIX_PATH_OPEN);
1949         p = &param[6];
1950
1951         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
1952         param_len = PTR_DIFF(p, param);
1953
1954         if (is_dir) {
1955                 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
1956                 wire_flags |= SMB_O_DIRECTORY;
1957         }
1958
1959         p = data;
1960         SIVAL(p,0,0); /* No oplock. */
1961         SIVAL(p,4,wire_flags);
1962         SIVAL(p,8,unix_perms_to_wire(mode));
1963         SIVAL(p,12,0); /* Top bits of perms currently undefined. */
1964         SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
1965
1966         data_len = 18;
1967
1968         if (!cli_send_trans(cli, SMBtrans2,
1969                         NULL,                        /* name */
1970                         -1, 0,                          /* fid, flags */
1971                         &setup, 1, 0,                   /* setup, length, max */
1972                         param, param_len, 2,            /* param, length, max */
1973                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
1974                         )) {
1975                 SAFE_FREE(param);
1976                 return -1;
1977         }
1978
1979         SAFE_FREE(param);
1980
1981         if (!cli_receive_trans(cli, SMBtrans2,
1982                 &rparam, &param_len,
1983                 &rdata, &data_len)) {
1984                         return -1;
1985         }
1986
1987         fnum = SVAL(rdata,2);
1988
1989         SAFE_FREE(rdata);
1990         SAFE_FREE(rparam);
1991
1992         return fnum;
1993 }
1994
1995 /****************************************************************************
1996  open - POSIX semantics.
1997 ****************************************************************************/
1998
1999 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
2000 {
2001         return cli_posix_open_internal(cli, fname, flags, mode, False);
2002 }
2003
2004 /****************************************************************************
2005  mkdir - POSIX semantics.
2006 ****************************************************************************/
2007
2008 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
2009 {
2010         return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
2011 }
2012
2013 /****************************************************************************
2014  unlink or rmdir - POSIX semantics.
2015 ****************************************************************************/
2016
2017 static bool cli_posix_unlink_internal(struct cli_state *cli, const char *fname, bool is_dir)
2018 {
2019         unsigned int data_len = 0;
2020         unsigned int param_len = 0;
2021         uint16 setup = TRANSACT2_SETPATHINFO;
2022         char *param;
2023         char data[2];
2024         char *rparam=NULL, *rdata=NULL;
2025         char *p;
2026         size_t srclen = 2*(strlen(fname)+1);
2027
2028         param = SMB_MALLOC(6+srclen+2);
2029         if (!param) {
2030                 return false;
2031         }
2032         memset(param, '\0', 6);
2033         SSVAL(param,0, SMB_POSIX_PATH_UNLINK);
2034         p = &param[6];
2035
2036         p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2037         param_len = PTR_DIFF(p, param);
2038
2039         SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
2040                         SMB_POSIX_UNLINK_FILE_TARGET);
2041         data_len = 2;
2042
2043         if (!cli_send_trans(cli, SMBtrans2,
2044                         NULL,                        /* name */
2045                         -1, 0,                          /* fid, flags */
2046                         &setup, 1, 0,                   /* setup, length, max */
2047                         param, param_len, 2,            /* param, length, max */
2048                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2049                         )) {
2050                 SAFE_FREE(param);
2051                 return False;
2052         }
2053
2054         SAFE_FREE(param);
2055
2056         if (!cli_receive_trans(cli, SMBtrans2,
2057                 &rparam, &param_len,
2058                 &rdata, &data_len)) {
2059                         return False;
2060         }
2061
2062         SAFE_FREE(rdata);
2063         SAFE_FREE(rparam);
2064
2065         return True;
2066 }
2067
2068 /****************************************************************************
2069  unlink - POSIX semantics.
2070 ****************************************************************************/
2071
2072 bool cli_posix_unlink(struct cli_state *cli, const char *fname)
2073 {
2074         return cli_posix_unlink_internal(cli, fname, False);
2075 }
2076
2077 /****************************************************************************
2078  rmdir - POSIX semantics.
2079 ****************************************************************************/
2080
2081 int cli_posix_rmdir(struct cli_state *cli, const char *fname)
2082 {
2083         return cli_posix_unlink_internal(cli, fname, True);
2084 }