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