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