Use a common function to parse all pathnames from the wire. This allows
[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 #define NO_SYSLOG
23
24 #include "includes.h"
25
26 /****************************************************************************
27  Hard/Symlink a file (UNIX extensions).
28 ****************************************************************************/
29
30 static BOOL cli_link_internal(struct cli_state *cli, const char *fname_src, const char *fname_dst, BOOL hard_link)
31 {
32         unsigned int data_len = 0;
33         unsigned int param_len = 0;
34         uint16 setup = TRANSACT2_SETPATHINFO;
35         char param[sizeof(pstring)+6];
36         pstring data;
37         char *rparam=NULL, *rdata=NULL;
38         char *p;
39
40         memset(param, 0, sizeof(param));
41         SSVAL(param,0,hard_link ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
42         p = &param[6];
43
44         p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
45         param_len = PTR_DIFF(p, param);
46
47         p = data;
48         p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
49         data_len = PTR_DIFF(p, data);
50
51         if (!cli_send_trans(cli, SMBtrans2,
52                 NULL,                        /* name */
53                 -1, 0,                          /* fid, flags */
54                 &setup, 1, 0,                   /* setup, length, max */
55                 param, param_len, 2,            /* param, length, max */
56                 (char *)&data,  data_len, cli->max_xmit /* data, length, max */
57                 )) {
58                         return False;
59         }
60
61         if (!cli_receive_trans(cli, SMBtrans2,
62                 &rparam, &param_len,
63                 &rdata, &data_len)) {
64                         return False;
65         }
66
67         SAFE_FREE(rdata);
68         SAFE_FREE(rparam);
69
70         return True;
71 }
72
73 /****************************************************************************
74  Map standard UNIX permissions onto wire representations.
75 ****************************************************************************/
76
77 uint32  unix_perms_to_wire(mode_t perms)
78 {
79         unsigned int ret = 0;
80
81         ret |= ((perms & S_IXOTH) ?  UNIX_X_OTH : 0);
82         ret |= ((perms & S_IWOTH) ?  UNIX_W_OTH : 0);
83         ret |= ((perms & S_IROTH) ?  UNIX_R_OTH : 0);
84         ret |= ((perms & S_IXGRP) ?  UNIX_X_GRP : 0);
85         ret |= ((perms & S_IWGRP) ?  UNIX_W_GRP : 0);
86         ret |= ((perms & S_IRGRP) ?  UNIX_R_GRP : 0);
87         ret |= ((perms & S_IXUSR) ?  UNIX_X_USR : 0);
88         ret |= ((perms & S_IWUSR) ?  UNIX_W_USR : 0);
89         ret |= ((perms & S_IRUSR) ?  UNIX_R_USR : 0);
90 #ifdef S_ISVTX
91         ret |= ((perms & S_ISVTX) ?  UNIX_STICKY : 0);
92 #endif
93 #ifdef S_ISGID
94         ret |= ((perms & S_ISGID) ?  UNIX_SET_GID : 0);
95 #endif
96 #ifdef S_ISUID
97         ret |= ((perms & S_ISUID) ?  UNIX_SET_UID : 0);
98 #endif
99         return ret;
100 }
101
102 /****************************************************************************
103  Symlink a file (UNIX extensions).
104 ****************************************************************************/
105
106 BOOL cli_unix_symlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
107 {
108         return cli_link_internal(cli, fname_src, fname_dst, False);
109 }
110
111 /****************************************************************************
112  Hard a file (UNIX extensions).
113 ****************************************************************************/
114
115 BOOL cli_unix_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
116 {
117         return cli_link_internal(cli, fname_src, fname_dst, True);
118 }
119
120 /****************************************************************************
121  Chmod or chown a file internal (UNIX extensions).
122 ****************************************************************************/
123
124 static BOOL cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32 mode, uint32 uid, uint32 gid)
125 {
126         unsigned int data_len = 0;
127         unsigned int param_len = 0;
128         uint16 setup = TRANSACT2_SETPATHINFO;
129         char param[sizeof(pstring)+6];
130         char data[100];
131         char *rparam=NULL, *rdata=NULL;
132         char *p;
133
134         memset(param, 0, sizeof(param));
135         memset(data, 0, sizeof(data));
136         SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
137         p = &param[6];
138
139         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
140         param_len = PTR_DIFF(p, param);
141
142         SIVAL(data,40,uid);
143         SIVAL(data,48,gid);
144         SIVAL(data,84,mode);
145
146         data_len = 100;
147
148         if (!cli_send_trans(cli, SMBtrans2,
149                 NULL,                        /* name */
150                 -1, 0,                          /* fid, flags */
151                 &setup, 1, 0,                   /* setup, length, max */
152                 param, param_len, 2,            /* param, length, max */
153                 (char *)&data,  data_len, cli->max_xmit /* data, length, max */
154                 )) {
155                         return False;
156         }
157
158         if (!cli_receive_trans(cli, SMBtrans2,
159                 &rparam, &param_len,
160                 &rdata, &data_len)) {
161                         return False;
162         }
163
164         SAFE_FREE(rdata);
165         SAFE_FREE(rparam);
166
167         return True;
168 }
169
170 /****************************************************************************
171  chmod a file (UNIX extensions).
172 ****************************************************************************/
173
174 BOOL cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
175 {
176         return cli_unix_chmod_chown_internal(cli, fname, 
177                 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
178 }
179
180 /****************************************************************************
181  chown a file (UNIX extensions).
182 ****************************************************************************/
183
184 BOOL cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
185 {
186         return cli_unix_chmod_chown_internal(cli, fname, SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
187 }
188
189 /****************************************************************************
190  Rename a file.
191 ****************************************************************************/
192
193 BOOL cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
194 {
195         char *p;
196
197         memset(cli->outbuf,'\0',smb_size);
198         memset(cli->inbuf,'\0',smb_size);
199
200         set_message(cli->outbuf,1, 0, True);
201
202         SCVAL(cli->outbuf,smb_com,SMBmv);
203         SSVAL(cli->outbuf,smb_tid,cli->cnum);
204         cli_setup_packet(cli);
205
206         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
207
208         p = smb_buf(cli->outbuf);
209         *p++ = 4;
210         p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
211         *p++ = 4;
212         p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
213
214         cli_setup_bcc(cli, p);
215
216         cli_send_smb(cli);
217         if (!cli_receive_smb(cli))
218                 return False;
219
220         if (cli_is_error(cli))
221                 return False;
222
223         return True;
224 }
225
226 /****************************************************************************
227  NT Rename a file.
228 ****************************************************************************/
229
230 BOOL cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
231 {
232         char *p;
233
234         memset(cli->outbuf,'\0',smb_size);
235         memset(cli->inbuf,'\0',smb_size);
236
237         set_message(cli->outbuf, 4, 0, True);
238
239         SCVAL(cli->outbuf,smb_com,SMBntrename);
240         SSVAL(cli->outbuf,smb_tid,cli->cnum);
241         cli_setup_packet(cli);
242
243         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
244         SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_RENAME);
245
246         p = smb_buf(cli->outbuf);
247         *p++ = 4;
248         p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
249         *p++ = 4;
250         p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
251
252         cli_setup_bcc(cli, p);
253
254         cli_send_smb(cli);
255         if (!cli_receive_smb(cli))
256                 return False;
257
258         if (cli_is_error(cli))
259                 return False;
260
261         return True;
262 }
263
264 /****************************************************************************
265  Delete a file.
266 ****************************************************************************/
267
268 BOOL cli_unlink(struct cli_state *cli, const char *fname)
269 {
270         char *p;
271
272         memset(cli->outbuf,'\0',smb_size);
273         memset(cli->inbuf,'\0',smb_size);
274
275         set_message(cli->outbuf,1, 0,True);
276
277         SCVAL(cli->outbuf,smb_com,SMBunlink);
278         SSVAL(cli->outbuf,smb_tid,cli->cnum);
279         cli_setup_packet(cli);
280
281         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN);
282   
283         p = smb_buf(cli->outbuf);
284         *p++ = 4;      
285         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
286
287         cli_setup_bcc(cli, p);
288         cli_send_smb(cli);
289         if (!cli_receive_smb(cli)) {
290                 return False;
291         }
292
293         if (cli_is_error(cli)) {
294                 return False;
295         }
296
297         return True;
298 }
299
300 /****************************************************************************
301  Create a directory.
302 ****************************************************************************/
303
304 BOOL cli_mkdir(struct cli_state *cli, const char *dname)
305 {
306         char *p;
307
308         memset(cli->outbuf,'\0',smb_size);
309         memset(cli->inbuf,'\0',smb_size);
310
311         set_message(cli->outbuf,0, 0,True);
312
313         SCVAL(cli->outbuf,smb_com,SMBmkdir);
314         SSVAL(cli->outbuf,smb_tid,cli->cnum);
315         cli_setup_packet(cli);
316
317         p = smb_buf(cli->outbuf);
318         *p++ = 4;      
319         p += clistr_push(cli, p, dname, -1, STR_TERMINATE);
320
321         cli_setup_bcc(cli, p);
322
323         cli_send_smb(cli);
324         if (!cli_receive_smb(cli)) {
325                 return False;
326         }
327
328         if (cli_is_error(cli)) {
329                 return False;
330         }
331
332         return True;
333 }
334
335 /****************************************************************************
336  Remove a directory.
337 ****************************************************************************/
338
339 BOOL cli_rmdir(struct cli_state *cli, const char *dname)
340 {
341         char *p;
342
343         memset(cli->outbuf,'\0',smb_size);
344         memset(cli->inbuf,'\0',smb_size);
345
346         set_message(cli->outbuf,0, 0, True);
347
348         SCVAL(cli->outbuf,smb_com,SMBrmdir);
349         SSVAL(cli->outbuf,smb_tid,cli->cnum);
350         cli_setup_packet(cli);
351
352         p = smb_buf(cli->outbuf);
353         *p++ = 4;      
354         p += clistr_push(cli, p, dname, -1, STR_TERMINATE);
355
356         cli_setup_bcc(cli, p);
357
358         cli_send_smb(cli);
359         if (!cli_receive_smb(cli)) {
360                 return False;
361         }
362
363         if (cli_is_error(cli)) {
364                 return False;
365         }
366
367         return True;
368 }
369
370 /****************************************************************************
371  Set or clear the delete on close flag.
372 ****************************************************************************/
373
374 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, BOOL flag)
375 {
376         unsigned int data_len = 1;
377         unsigned int param_len = 6;
378         uint16 setup = TRANSACT2_SETFILEINFO;
379         pstring param;
380         unsigned char data;
381         char *rparam=NULL, *rdata=NULL;
382
383         memset(param, 0, param_len);
384         SSVAL(param,0,fnum);
385         SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
386
387         data = flag ? 1 : 0;
388
389         if (!cli_send_trans(cli, SMBtrans2,
390                                                 NULL,                        /* name */
391                                                 -1, 0,                          /* fid, flags */
392                                                 &setup, 1, 0,                   /* setup, length, max */
393                                                 param, param_len, 2,            /* param, length, max */
394                                                 (char *)&data,  data_len, cli->max_xmit /* data, length, max */
395                                                 )) {
396                 return False;
397         }
398
399         if (!cli_receive_trans(cli, SMBtrans2,
400                                                 &rparam, &param_len,
401                                                 &rdata, &data_len)) {
402                 return False;
403         }
404
405         SAFE_FREE(rdata);
406         SAFE_FREE(rparam);
407
408         return True;
409 }
410
411 /****************************************************************************
412  Open a file - exposing the full horror of the NT API :-).
413  Used in smbtorture.
414 ****************************************************************************/
415
416 int cli_nt_create_full(struct cli_state *cli, const char *fname, 
417                  uint32 CreatFlags, uint32 DesiredAccess,
418                  uint32 FileAttributes, uint32 ShareAccess,
419                  uint32 CreateDisposition, uint32 CreateOptions,
420                  uint8 SecuityFlags)
421 {
422         char *p;
423         int len;
424
425         memset(cli->outbuf,'\0',smb_size);
426         memset(cli->inbuf,'\0',smb_size);
427
428         set_message(cli->outbuf,24,0,True);
429
430         SCVAL(cli->outbuf,smb_com,SMBntcreateX);
431         SSVAL(cli->outbuf,smb_tid,cli->cnum);
432         cli_setup_packet(cli);
433
434         SSVAL(cli->outbuf,smb_vwv0,0xFF);
435         if (cli->use_oplocks)
436                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
437         
438         SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
439         SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
440         SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
441         SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
442         SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
443         SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
444         SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
445         SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
446         SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecuityFlags);
447
448         p = smb_buf(cli->outbuf);
449         /* this alignment and termination is critical for netapp filers. Don't change */
450         p += clistr_align_out(cli, p, 0);
451         len = clistr_push(cli, p, fname, -1, 0);
452         p += len;
453         SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
454         /* sigh. this copes with broken netapp filer behaviour */
455         p += clistr_push(cli, p, "", -1, STR_TERMINATE);
456
457         cli_setup_bcc(cli, p);
458
459         cli_send_smb(cli);
460         if (!cli_receive_smb(cli)) {
461                 return -1;
462         }
463
464         if (cli_is_error(cli)) {
465                 return -1;
466         }
467
468         return SVAL(cli->inbuf,smb_vwv2 + 1);
469 }
470
471 /****************************************************************************
472  Open a file.
473 ****************************************************************************/
474
475 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
476 {
477         return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
478                                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_EXISTS_OPEN, 0x0, 0x0);
479 }
480
481 /****************************************************************************
482  Open a file
483  WARNING: if you open with O_WRONLY then getattrE won't work!
484 ****************************************************************************/
485
486 int cli_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
487 {
488         char *p;
489         unsigned openfn=0;
490         unsigned accessmode=0;
491
492         if (flags & O_CREAT)
493                 openfn |= (1<<4);
494         if (!(flags & O_EXCL)) {
495                 if (flags & O_TRUNC)
496                         openfn |= (1<<1);
497                 else
498                         openfn |= (1<<0);
499         }
500
501         accessmode = (share_mode<<4);
502
503         if ((flags & O_ACCMODE) == O_RDWR) {
504                 accessmode |= 2;
505         } else if ((flags & O_ACCMODE) == O_WRONLY) {
506                 accessmode |= 1;
507         } 
508
509 #if defined(O_SYNC)
510         if ((flags & O_SYNC) == O_SYNC) {
511                 accessmode |= (1<<14);
512         }
513 #endif /* O_SYNC */
514
515         if (share_mode == DENY_FCB) {
516                 accessmode = 0xFF;
517         }
518
519         memset(cli->outbuf,'\0',smb_size);
520         memset(cli->inbuf,'\0',smb_size);
521
522         set_message(cli->outbuf,15,0,True);
523
524         SCVAL(cli->outbuf,smb_com,SMBopenX);
525         SSVAL(cli->outbuf,smb_tid,cli->cnum);
526         cli_setup_packet(cli);
527
528         SSVAL(cli->outbuf,smb_vwv0,0xFF);
529         SSVAL(cli->outbuf,smb_vwv2,0);  /* no additional info */
530         SSVAL(cli->outbuf,smb_vwv3,accessmode);
531         SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
532         SSVAL(cli->outbuf,smb_vwv5,0);
533         SSVAL(cli->outbuf,smb_vwv8,openfn);
534
535         if (cli->use_oplocks) {
536                 /* if using oplocks then ask for a batch oplock via
537                    core and extended methods */
538                 SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
539                         FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
540                 SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
541         }
542   
543         p = smb_buf(cli->outbuf);
544         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
545
546         cli_setup_bcc(cli, p);
547
548         cli_send_smb(cli);
549         if (!cli_receive_smb(cli)) {
550                 return -1;
551         }
552
553         if (cli_is_error(cli)) {
554                 return -1;
555         }
556
557         return SVAL(cli->inbuf,smb_vwv2);
558 }
559
560 /****************************************************************************
561  Close a file.
562 ****************************************************************************/
563
564 BOOL cli_close(struct cli_state *cli, int fnum)
565 {
566         memset(cli->outbuf,'\0',smb_size);
567         memset(cli->inbuf,'\0',smb_size);
568
569         set_message(cli->outbuf,3,0,True);
570
571         SCVAL(cli->outbuf,smb_com,SMBclose);
572         SSVAL(cli->outbuf,smb_tid,cli->cnum);
573         cli_setup_packet(cli);
574
575         SSVAL(cli->outbuf,smb_vwv0,fnum);
576         SIVALS(cli->outbuf,smb_vwv1,-1);
577
578         cli_send_smb(cli);
579         if (!cli_receive_smb(cli)) {
580                 return False;
581         }
582
583         return !cli_is_error(cli);
584 }
585
586
587 /****************************************************************************
588  send a lock with a specified locktype 
589  this is used for testing LOCKING_ANDX_CANCEL_LOCK
590 ****************************************************************************/
591 NTSTATUS cli_locktype(struct cli_state *cli, int fnum, 
592                       uint32 offset, uint32 len, int timeout, unsigned char locktype)
593 {
594         char *p;
595         int saved_timeout = cli->timeout;
596
597         memset(cli->outbuf,'\0',smb_size);
598         memset(cli->inbuf,'\0', smb_size);
599
600         set_message(cli->outbuf,8,0,True);
601
602         SCVAL(cli->outbuf,smb_com,SMBlockingX);
603         SSVAL(cli->outbuf,smb_tid,cli->cnum);
604         cli_setup_packet(cli);
605
606         SCVAL(cli->outbuf,smb_vwv0,0xFF);
607         SSVAL(cli->outbuf,smb_vwv2,fnum);
608         SCVAL(cli->outbuf,smb_vwv3,locktype);
609         SIVALS(cli->outbuf, smb_vwv4, timeout);
610         SSVAL(cli->outbuf,smb_vwv6,0);
611         SSVAL(cli->outbuf,smb_vwv7,1);
612
613         p = smb_buf(cli->outbuf);
614         SSVAL(p, 0, cli->pid);
615         SIVAL(p, 2, offset);
616         SIVAL(p, 6, len);
617
618         p += 10;
619
620         cli_setup_bcc(cli, p);
621
622         cli_send_smb(cli);
623
624         if (timeout != 0) {
625                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
626         }
627
628         if (!cli_receive_smb(cli)) {
629                 cli->timeout = saved_timeout;
630                 return NT_STATUS_UNSUCCESSFUL;
631         }
632
633         cli->timeout = saved_timeout;
634
635         return cli_nt_error(cli);
636 }
637
638
639 /****************************************************************************
640  Lock a file.
641  note that timeout is in units of 2 milliseconds
642 ****************************************************************************/
643 BOOL cli_lock(struct cli_state *cli, int fnum, 
644               uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
645 {
646         char *p;
647         int saved_timeout = cli->timeout;
648
649         memset(cli->outbuf,'\0',smb_size);
650         memset(cli->inbuf,'\0', smb_size);
651
652         set_message(cli->outbuf,8,0,True);
653
654         SCVAL(cli->outbuf,smb_com,SMBlockingX);
655         SSVAL(cli->outbuf,smb_tid,cli->cnum);
656         cli_setup_packet(cli);
657
658         SCVAL(cli->outbuf,smb_vwv0,0xFF);
659         SSVAL(cli->outbuf,smb_vwv2,fnum);
660         SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
661         SIVALS(cli->outbuf, smb_vwv4, timeout);
662         SSVAL(cli->outbuf,smb_vwv6,0);
663         SSVAL(cli->outbuf,smb_vwv7,1);
664
665         p = smb_buf(cli->outbuf);
666         SSVAL(p, 0, cli->pid);
667         SIVAL(p, 2, offset);
668         SIVAL(p, 6, len);
669
670         p += 10;
671
672         cli_setup_bcc(cli, p);
673
674         cli_send_smb(cli);
675
676         if (timeout != 0) {
677                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
678         }
679
680         if (!cli_receive_smb(cli)) {
681                 cli->timeout = saved_timeout;
682                 return False;
683         }
684
685         cli->timeout = saved_timeout;
686
687         if (cli_is_error(cli)) {
688                 return False;
689         }
690
691         return True;
692 }
693
694 /****************************************************************************
695  Unlock a file.
696 ****************************************************************************/
697
698 BOOL cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
699 {
700         char *p;
701
702         memset(cli->outbuf,'\0',smb_size);
703         memset(cli->inbuf,'\0',smb_size);
704
705         set_message(cli->outbuf,8,0,True);
706
707         SCVAL(cli->outbuf,smb_com,SMBlockingX);
708         SSVAL(cli->outbuf,smb_tid,cli->cnum);
709         cli_setup_packet(cli);
710
711         SCVAL(cli->outbuf,smb_vwv0,0xFF);
712         SSVAL(cli->outbuf,smb_vwv2,fnum);
713         SCVAL(cli->outbuf,smb_vwv3,0);
714         SIVALS(cli->outbuf, smb_vwv4, 0);
715         SSVAL(cli->outbuf,smb_vwv6,1);
716         SSVAL(cli->outbuf,smb_vwv7,0);
717
718         p = smb_buf(cli->outbuf);
719         SSVAL(p, 0, cli->pid);
720         SIVAL(p, 2, offset);
721         SIVAL(p, 6, len);
722         p += 10;
723         cli_setup_bcc(cli, p);
724         cli_send_smb(cli);
725         if (!cli_receive_smb(cli)) {
726                 return False;
727         }
728
729         if (cli_is_error(cli)) {
730                 return False;
731         }
732
733         return True;
734 }
735
736 /****************************************************************************
737  Lock a file with 64 bit offsets.
738 ****************************************************************************/
739
740 BOOL cli_lock64(struct cli_state *cli, int fnum, 
741                 SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
742 {
743         char *p;
744         int saved_timeout = cli->timeout;
745         int ltype;
746
747         if (! (cli->capabilities & CAP_LARGE_FILES)) {
748                 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
749         }
750
751         ltype = (lock_type == READ_LOCK? 1 : 0);
752         ltype |= LOCKING_ANDX_LARGE_FILES;
753
754         memset(cli->outbuf,'\0',smb_size);
755         memset(cli->inbuf,'\0', smb_size);
756
757         set_message(cli->outbuf,8,0,True);
758
759         SCVAL(cli->outbuf,smb_com,SMBlockingX);
760         SSVAL(cli->outbuf,smb_tid,cli->cnum);
761         cli_setup_packet(cli);
762
763         SCVAL(cli->outbuf,smb_vwv0,0xFF);
764         SSVAL(cli->outbuf,smb_vwv2,fnum);
765         SCVAL(cli->outbuf,smb_vwv3,ltype);
766         SIVALS(cli->outbuf, smb_vwv4, timeout);
767         SSVAL(cli->outbuf,smb_vwv6,0);
768         SSVAL(cli->outbuf,smb_vwv7,1);
769
770         p = smb_buf(cli->outbuf);
771         SIVAL(p, 0, cli->pid);
772         SOFF_T_R(p, 4, offset);
773         SOFF_T_R(p, 12, len);
774         p += 20;
775
776         cli_setup_bcc(cli, p);
777         cli_send_smb(cli);
778
779         if (timeout != 0) {
780                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
781         }
782
783         if (!cli_receive_smb(cli)) {
784                 cli->timeout = saved_timeout;
785                 return False;
786         }
787
788         cli->timeout = saved_timeout;
789
790         if (cli_is_error(cli)) {
791                 return False;
792         }
793
794         return True;
795 }
796
797 /****************************************************************************
798  Unlock a file with 64 bit offsets.
799 ****************************************************************************/
800
801 BOOL cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
802 {
803         char *p;
804
805         if (! (cli->capabilities & CAP_LARGE_FILES)) {
806                 return cli_unlock(cli, fnum, offset, len);
807         }
808
809         memset(cli->outbuf,'\0',smb_size);
810         memset(cli->inbuf,'\0',smb_size);
811
812         set_message(cli->outbuf,8,0,True);
813
814         SCVAL(cli->outbuf,smb_com,SMBlockingX);
815         SSVAL(cli->outbuf,smb_tid,cli->cnum);
816         cli_setup_packet(cli);
817
818         SCVAL(cli->outbuf,smb_vwv0,0xFF);
819         SSVAL(cli->outbuf,smb_vwv2,fnum);
820         SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
821         SIVALS(cli->outbuf, smb_vwv4, 0);
822         SSVAL(cli->outbuf,smb_vwv6,1);
823         SSVAL(cli->outbuf,smb_vwv7,0);
824
825         p = smb_buf(cli->outbuf);
826         SIVAL(p, 0, cli->pid);
827         SOFF_T_R(p, 4, offset);
828         SOFF_T_R(p, 12, len);
829         p += 20;
830         cli_setup_bcc(cli, p);
831         cli_send_smb(cli);
832         if (!cli_receive_smb(cli)) {
833                 return False;
834         }
835
836         if (cli_is_error(cli)) {
837                 return False;
838         }
839
840         return True;
841 }
842
843
844 /****************************************************************************
845  Do a SMBgetattrE call.
846 ****************************************************************************/
847
848 BOOL cli_getattrE(struct cli_state *cli, int fd, 
849                   uint16 *attr, SMB_BIG_UINT *size, 
850                   time_t *c_time, time_t *a_time, time_t *m_time)
851 {
852         memset(cli->outbuf,'\0',smb_size);
853         memset(cli->inbuf,'\0',smb_size);
854
855         set_message(cli->outbuf,1,0,True);
856
857         SCVAL(cli->outbuf,smb_com,SMBgetattrE);
858         SSVAL(cli->outbuf,smb_tid,cli->cnum);
859         cli_setup_packet(cli);
860
861         SSVAL(cli->outbuf,smb_vwv0,fd);
862
863         cli_send_smb(cli);
864         if (!cli_receive_smb(cli)) {
865                 return False;
866         }
867         
868         if (cli_is_error(cli)) {
869                 return False;
870         }
871
872         if (size) {
873                 *size = IVAL(cli->inbuf, smb_vwv6);
874         }
875
876         if (attr) {
877                 *attr = SVAL(cli->inbuf,smb_vwv10);
878         }
879
880         if (c_time) {
881                 *c_time = make_unix_date3(cli->inbuf+smb_vwv0);
882         }
883
884         if (a_time) {
885                 *a_time = make_unix_date3(cli->inbuf+smb_vwv2);
886         }
887
888         if (m_time) {
889                 *m_time = make_unix_date3(cli->inbuf+smb_vwv4);
890         }
891
892         return True;
893 }
894
895 /****************************************************************************
896  Do a SMBgetatr call
897 ****************************************************************************/
898
899 BOOL cli_getatr(struct cli_state *cli, const char *fname, 
900                 uint16 *attr, size_t *size, time_t *t)
901 {
902         char *p;
903
904         memset(cli->outbuf,'\0',smb_size);
905         memset(cli->inbuf,'\0',smb_size);
906
907         set_message(cli->outbuf,0,0,True);
908
909         SCVAL(cli->outbuf,smb_com,SMBgetatr);
910         SSVAL(cli->outbuf,smb_tid,cli->cnum);
911         cli_setup_packet(cli);
912
913         p = smb_buf(cli->outbuf);
914         *p++ = 4;
915         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
916
917         cli_setup_bcc(cli, p);
918
919         cli_send_smb(cli);
920         if (!cli_receive_smb(cli)) {
921                 return False;
922         }
923         
924         if (cli_is_error(cli)) {
925                 return False;
926         }
927
928         if (size) {
929                 *size = IVAL(cli->inbuf, smb_vwv3);
930         }
931
932         if (t) {
933                 *t = make_unix_date3(cli->inbuf+smb_vwv1);
934         }
935
936         if (attr) {
937                 *attr = SVAL(cli->inbuf,smb_vwv0);
938         }
939
940
941         return True;
942 }
943
944 /****************************************************************************
945  Do a SMBsetatr call.
946 ****************************************************************************/
947
948 BOOL cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
949 {
950         char *p;
951
952         memset(cli->outbuf,'\0',smb_size);
953         memset(cli->inbuf,'\0',smb_size);
954
955         set_message(cli->outbuf,8,0,True);
956
957         SCVAL(cli->outbuf,smb_com,SMBsetatr);
958         SSVAL(cli->outbuf,smb_tid,cli->cnum);
959         cli_setup_packet(cli);
960
961         SSVAL(cli->outbuf,smb_vwv0, attr);
962         put_dos_date3(cli->outbuf,smb_vwv1, t);
963
964         p = smb_buf(cli->outbuf);
965         *p++ = 4;
966         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
967         *p++ = 4;
968
969         cli_setup_bcc(cli, p);
970
971         cli_send_smb(cli);
972         if (!cli_receive_smb(cli)) {
973                 return False;
974         }
975         
976         if (cli_is_error(cli)) {
977                 return False;
978         }
979
980         return True;
981 }
982
983 /****************************************************************************
984  Check for existance of a dir.
985 ****************************************************************************/
986 BOOL cli_chkpath(struct cli_state *cli, const char *path)
987 {
988         pstring path2;
989         char *p;
990         
991         pstrcpy(path2,path);
992         trim_char(path2,'\0','\\');
993         if (!*path2)
994                 *path2 = '\\';
995         
996         memset(cli->outbuf,'\0',smb_size);
997         set_message(cli->outbuf,0,0,True);
998         SCVAL(cli->outbuf,smb_com,SMBchkpth);
999         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1000         cli_setup_packet(cli);
1001         p = smb_buf(cli->outbuf);
1002         *p++ = 4;
1003         p += clistr_push(cli, p, path2, -1, STR_TERMINATE);
1004
1005         cli_setup_bcc(cli, p);
1006
1007         cli_send_smb(cli);
1008         if (!cli_receive_smb(cli)) {
1009                 return False;
1010         }
1011
1012         if (cli_is_error(cli)) return False;
1013
1014         return True;
1015 }
1016
1017 /****************************************************************************
1018  Query disk space.
1019 ****************************************************************************/
1020
1021 BOOL cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1022 {
1023         memset(cli->outbuf,'\0',smb_size);
1024         set_message(cli->outbuf,0,0,True);
1025         SCVAL(cli->outbuf,smb_com,SMBdskattr);
1026         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1027         cli_setup_packet(cli);
1028
1029         cli_send_smb(cli);
1030         if (!cli_receive_smb(cli)) {
1031                 return False;
1032         }
1033
1034         *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1035         *total = SVAL(cli->inbuf,smb_vwv0);
1036         *avail = SVAL(cli->inbuf,smb_vwv3);
1037         
1038         return True;
1039 }
1040
1041 /****************************************************************************
1042  Create and open a temporary file.
1043 ****************************************************************************/
1044
1045 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1046 {
1047         int len;
1048         char *p;
1049
1050         memset(cli->outbuf,'\0',smb_size);
1051         memset(cli->inbuf,'\0',smb_size);
1052
1053         set_message(cli->outbuf,3,0,True);
1054
1055         SCVAL(cli->outbuf,smb_com,SMBctemp);
1056         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1057         cli_setup_packet(cli);
1058
1059         SSVAL(cli->outbuf,smb_vwv0,0);
1060         SIVALS(cli->outbuf,smb_vwv1,-1);
1061
1062         p = smb_buf(cli->outbuf);
1063         *p++ = 4;
1064         p += clistr_push(cli, p, path, -1, STR_TERMINATE);
1065
1066         cli_setup_bcc(cli, p);
1067
1068         cli_send_smb(cli);
1069         if (!cli_receive_smb(cli)) {
1070                 return -1;
1071         }
1072
1073         if (cli_is_error(cli)) {
1074                 return -1;
1075         }
1076
1077         /* despite the spec, the result has a -1, followed by
1078            length, followed by name */
1079         p = smb_buf(cli->inbuf);
1080         p += 4;
1081         len = smb_buflen(cli->inbuf) - 4;
1082         if (len <= 0) return -1;
1083
1084         if (tmp_path) {
1085                 pstring path2;
1086                 clistr_pull(cli, path2, p, 
1087                             sizeof(path2), len, STR_ASCII);
1088                 *tmp_path = strdup(path2);
1089         }
1090
1091         return SVAL(cli->inbuf,smb_vwv0);
1092 }
1093
1094
1095 /* 
1096    send a raw ioctl - used by the torture code
1097 */
1098 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1099 {
1100         memset(cli->outbuf,'\0',smb_size);
1101         memset(cli->inbuf,'\0',smb_size);
1102
1103         set_message(cli->outbuf, 3, 0, True);
1104         SCVAL(cli->outbuf,smb_com,SMBioctl);
1105         cli_setup_packet(cli);
1106
1107         SSVAL(cli->outbuf, smb_vwv0, fnum);
1108         SSVAL(cli->outbuf, smb_vwv1, code>>16);
1109         SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1110
1111         cli_send_smb(cli);
1112         if (!cli_receive_smb(cli)) {
1113                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1114         }
1115
1116         if (cli_is_error(cli)) {
1117                 return cli_nt_error(cli);
1118         }
1119
1120         *blob = data_blob(NULL, 0);
1121
1122         return NT_STATUS_OK;
1123 }