trying to get HEAD building again. If you want the code
[gd/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  Delete a file.
228 ****************************************************************************/
229
230 BOOL cli_unlink(struct cli_state *cli, const char *fname)
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,1, 0,True);
238
239         SCVAL(cli->outbuf,smb_com,SMBunlink);
240         SSVAL(cli->outbuf,smb_tid,cli->cnum);
241         cli_setup_packet(cli);
242
243         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN);
244   
245         p = smb_buf(cli->outbuf);
246         *p++ = 4;      
247         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
248
249         cli_setup_bcc(cli, p);
250         cli_send_smb(cli);
251         if (!cli_receive_smb(cli)) {
252                 return False;
253         }
254
255         if (cli_is_error(cli)) {
256                 return False;
257         }
258
259         return True;
260 }
261
262 /****************************************************************************
263  Create a directory.
264 ****************************************************************************/
265
266 BOOL cli_mkdir(struct cli_state *cli, const char *dname)
267 {
268         char *p;
269
270         memset(cli->outbuf,'\0',smb_size);
271         memset(cli->inbuf,'\0',smb_size);
272
273         set_message(cli->outbuf,0, 0,True);
274
275         SCVAL(cli->outbuf,smb_com,SMBmkdir);
276         SSVAL(cli->outbuf,smb_tid,cli->cnum);
277         cli_setup_packet(cli);
278
279         p = smb_buf(cli->outbuf);
280         *p++ = 4;      
281         p += clistr_push(cli, p, dname, -1, STR_TERMINATE);
282
283         cli_setup_bcc(cli, p);
284
285         cli_send_smb(cli);
286         if (!cli_receive_smb(cli)) {
287                 return False;
288         }
289
290         if (cli_is_error(cli)) {
291                 return False;
292         }
293
294         return True;
295 }
296
297 /****************************************************************************
298  Remove a directory.
299 ****************************************************************************/
300
301 BOOL cli_rmdir(struct cli_state *cli, const char *dname)
302 {
303         char *p;
304
305         memset(cli->outbuf,'\0',smb_size);
306         memset(cli->inbuf,'\0',smb_size);
307
308         set_message(cli->outbuf,0, 0, True);
309
310         SCVAL(cli->outbuf,smb_com,SMBrmdir);
311         SSVAL(cli->outbuf,smb_tid,cli->cnum);
312         cli_setup_packet(cli);
313
314         p = smb_buf(cli->outbuf);
315         *p++ = 4;      
316         p += clistr_push(cli, p, dname, -1, STR_TERMINATE);
317
318         cli_setup_bcc(cli, p);
319
320         cli_send_smb(cli);
321         if (!cli_receive_smb(cli)) {
322                 return False;
323         }
324
325         if (cli_is_error(cli)) {
326                 return False;
327         }
328
329         return True;
330 }
331
332 /****************************************************************************
333  Set or clear the delete on close flag.
334 ****************************************************************************/
335
336 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, BOOL flag)
337 {
338         unsigned int data_len = 1;
339         unsigned int param_len = 6;
340         uint16 setup = TRANSACT2_SETFILEINFO;
341         pstring param;
342         unsigned char data;
343         char *rparam=NULL, *rdata=NULL;
344
345         memset(param, 0, param_len);
346         SSVAL(param,0,fnum);
347         SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
348
349         data = flag ? 1 : 0;
350
351         if (!cli_send_trans(cli, SMBtrans2,
352                                                 NULL,                        /* name */
353                                                 -1, 0,                          /* fid, flags */
354                                                 &setup, 1, 0,                   /* setup, length, max */
355                                                 param, param_len, 2,            /* param, length, max */
356                                                 (char *)&data,  data_len, cli->max_xmit /* data, length, max */
357                                                 )) {
358                 return False;
359         }
360
361         if (!cli_receive_trans(cli, SMBtrans2,
362                                                 &rparam, &param_len,
363                                                 &rdata, &data_len)) {
364                 return False;
365         }
366
367         SAFE_FREE(rdata);
368         SAFE_FREE(rparam);
369
370         return True;
371 }
372
373 /****************************************************************************
374  Open a file - exposing the full horror of the NT API :-).
375  Used in smbtorture.
376 ****************************************************************************/
377
378 int cli_nt_create_full(struct cli_state *cli, const char *fname, 
379                  uint32 CreatFlags, uint32 DesiredAccess,
380                  uint32 FileAttributes, uint32 ShareAccess,
381                  uint32 CreateDisposition, uint32 CreateOptions,
382                  uint8 SecuityFlags)
383 {
384         char *p;
385         int len;
386
387         memset(cli->outbuf,'\0',smb_size);
388         memset(cli->inbuf,'\0',smb_size);
389
390         set_message(cli->outbuf,24,0,True);
391
392         SCVAL(cli->outbuf,smb_com,SMBntcreateX);
393         SSVAL(cli->outbuf,smb_tid,cli->cnum);
394         cli_setup_packet(cli);
395
396         SSVAL(cli->outbuf,smb_vwv0,0xFF);
397         if (cli->use_oplocks)
398                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
399         
400         SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
401         SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
402         SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
403         SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
404         SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
405         SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
406         SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
407         SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
408         SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecuityFlags);
409
410         p = smb_buf(cli->outbuf);
411         /* this alignment and termination is critical for netapp filers. Don't change */
412         p += clistr_align_out(cli, p, 0);
413         len = clistr_push(cli, p, fname, -1, 0);
414         p += len;
415         SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
416         /* sigh. this copes with broken netapp filer behaviour */
417         p += clistr_push(cli, p, "", -1, STR_TERMINATE);
418
419         cli_setup_bcc(cli, p);
420
421         cli_send_smb(cli);
422         if (!cli_receive_smb(cli)) {
423                 return -1;
424         }
425
426         if (cli_is_error(cli)) {
427                 return -1;
428         }
429
430         return SVAL(cli->inbuf,smb_vwv2 + 1);
431 }
432
433 /****************************************************************************
434  Open a file.
435 ****************************************************************************/
436
437 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
438 {
439         return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
440                                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_EXISTS_OPEN, 0x0, 0x0);
441 }
442
443 /****************************************************************************
444  Open a file
445  WARNING: if you open with O_WRONLY then getattrE won't work!
446 ****************************************************************************/
447
448 int cli_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
449 {
450         char *p;
451         unsigned openfn=0;
452         unsigned accessmode=0;
453
454         if (flags & O_CREAT)
455                 openfn |= (1<<4);
456         if (!(flags & O_EXCL)) {
457                 if (flags & O_TRUNC)
458                         openfn |= (1<<1);
459                 else
460                         openfn |= (1<<0);
461         }
462
463         accessmode = (share_mode<<4);
464
465         if ((flags & O_ACCMODE) == O_RDWR) {
466                 accessmode |= 2;
467         } else if ((flags & O_ACCMODE) == O_WRONLY) {
468                 accessmode |= 1;
469         } 
470
471 #if defined(O_SYNC)
472         if ((flags & O_SYNC) == O_SYNC) {
473                 accessmode |= (1<<14);
474         }
475 #endif /* O_SYNC */
476
477         if (share_mode == DENY_FCB) {
478                 accessmode = 0xFF;
479         }
480
481         memset(cli->outbuf,'\0',smb_size);
482         memset(cli->inbuf,'\0',smb_size);
483
484         set_message(cli->outbuf,15,0,True);
485
486         SCVAL(cli->outbuf,smb_com,SMBopenX);
487         SSVAL(cli->outbuf,smb_tid,cli->cnum);
488         cli_setup_packet(cli);
489
490         SSVAL(cli->outbuf,smb_vwv0,0xFF);
491         SSVAL(cli->outbuf,smb_vwv2,0);  /* no additional info */
492         SSVAL(cli->outbuf,smb_vwv3,accessmode);
493         SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
494         SSVAL(cli->outbuf,smb_vwv5,0);
495         SSVAL(cli->outbuf,smb_vwv8,openfn);
496
497         if (cli->use_oplocks) {
498                 /* if using oplocks then ask for a batch oplock via
499                    core and extended methods */
500                 SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
501                         FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
502                 SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
503         }
504   
505         p = smb_buf(cli->outbuf);
506         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
507
508         cli_setup_bcc(cli, p);
509
510         cli_send_smb(cli);
511         if (!cli_receive_smb(cli)) {
512                 return -1;
513         }
514
515         if (cli_is_error(cli)) {
516                 return -1;
517         }
518
519         return SVAL(cli->inbuf,smb_vwv2);
520 }
521
522 /****************************************************************************
523  Close a file.
524 ****************************************************************************/
525
526 BOOL cli_close(struct cli_state *cli, int fnum)
527 {
528         memset(cli->outbuf,'\0',smb_size);
529         memset(cli->inbuf,'\0',smb_size);
530
531         set_message(cli->outbuf,3,0,True);
532
533         SCVAL(cli->outbuf,smb_com,SMBclose);
534         SSVAL(cli->outbuf,smb_tid,cli->cnum);
535         cli_setup_packet(cli);
536
537         SSVAL(cli->outbuf,smb_vwv0,fnum);
538         SIVALS(cli->outbuf,smb_vwv1,-1);
539
540         cli_send_smb(cli);
541         if (!cli_receive_smb(cli)) {
542                 return False;
543         }
544
545         return !cli_is_error(cli);
546 }
547
548
549 /****************************************************************************
550  send a lock with a specified locktype 
551  this is used for testing LOCKING_ANDX_CANCEL_LOCK
552 ****************************************************************************/
553 NTSTATUS cli_locktype(struct cli_state *cli, int fnum, 
554                       uint32 offset, uint32 len, int timeout, unsigned char locktype)
555 {
556         char *p;
557         int saved_timeout = cli->timeout;
558
559         memset(cli->outbuf,'\0',smb_size);
560         memset(cli->inbuf,'\0', smb_size);
561
562         set_message(cli->outbuf,8,0,True);
563
564         SCVAL(cli->outbuf,smb_com,SMBlockingX);
565         SSVAL(cli->outbuf,smb_tid,cli->cnum);
566         cli_setup_packet(cli);
567
568         SCVAL(cli->outbuf,smb_vwv0,0xFF);
569         SSVAL(cli->outbuf,smb_vwv2,fnum);
570         SCVAL(cli->outbuf,smb_vwv3,locktype);
571         SIVALS(cli->outbuf, smb_vwv4, timeout);
572         SSVAL(cli->outbuf,smb_vwv6,0);
573         SSVAL(cli->outbuf,smb_vwv7,1);
574
575         p = smb_buf(cli->outbuf);
576         SSVAL(p, 0, cli->pid);
577         SIVAL(p, 2, offset);
578         SIVAL(p, 6, len);
579
580         p += 10;
581
582         cli_setup_bcc(cli, p);
583
584         cli_send_smb(cli);
585
586         if (timeout != 0) {
587                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
588         }
589
590         if (!cli_receive_smb(cli)) {
591                 cli->timeout = saved_timeout;
592                 return NT_STATUS_UNSUCCESSFUL;
593         }
594
595         cli->timeout = saved_timeout;
596
597         return cli_nt_error(cli);
598 }
599
600
601 /****************************************************************************
602  Lock a file.
603  note that timeout is in units of 2 milliseconds
604 ****************************************************************************/
605 BOOL cli_lock(struct cli_state *cli, int fnum, 
606               uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
607 {
608         char *p;
609         int saved_timeout = cli->timeout;
610
611         memset(cli->outbuf,'\0',smb_size);
612         memset(cli->inbuf,'\0', smb_size);
613
614         set_message(cli->outbuf,8,0,True);
615
616         SCVAL(cli->outbuf,smb_com,SMBlockingX);
617         SSVAL(cli->outbuf,smb_tid,cli->cnum);
618         cli_setup_packet(cli);
619
620         SCVAL(cli->outbuf,smb_vwv0,0xFF);
621         SSVAL(cli->outbuf,smb_vwv2,fnum);
622         SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
623         SIVALS(cli->outbuf, smb_vwv4, timeout);
624         SSVAL(cli->outbuf,smb_vwv6,0);
625         SSVAL(cli->outbuf,smb_vwv7,1);
626
627         p = smb_buf(cli->outbuf);
628         SSVAL(p, 0, cli->pid);
629         SIVAL(p, 2, offset);
630         SIVAL(p, 6, len);
631
632         p += 10;
633
634         cli_setup_bcc(cli, p);
635
636         cli_send_smb(cli);
637
638         if (timeout != 0) {
639                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
640         }
641
642         if (!cli_receive_smb(cli)) {
643                 cli->timeout = saved_timeout;
644                 return False;
645         }
646
647         cli->timeout = saved_timeout;
648
649         if (cli_is_error(cli)) {
650                 return False;
651         }
652
653         return True;
654 }
655
656 /****************************************************************************
657  Unlock a file.
658 ****************************************************************************/
659
660 BOOL cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
661 {
662         char *p;
663
664         memset(cli->outbuf,'\0',smb_size);
665         memset(cli->inbuf,'\0',smb_size);
666
667         set_message(cli->outbuf,8,0,True);
668
669         SCVAL(cli->outbuf,smb_com,SMBlockingX);
670         SSVAL(cli->outbuf,smb_tid,cli->cnum);
671         cli_setup_packet(cli);
672
673         SCVAL(cli->outbuf,smb_vwv0,0xFF);
674         SSVAL(cli->outbuf,smb_vwv2,fnum);
675         SCVAL(cli->outbuf,smb_vwv3,0);
676         SIVALS(cli->outbuf, smb_vwv4, 0);
677         SSVAL(cli->outbuf,smb_vwv6,1);
678         SSVAL(cli->outbuf,smb_vwv7,0);
679
680         p = smb_buf(cli->outbuf);
681         SSVAL(p, 0, cli->pid);
682         SIVAL(p, 2, offset);
683         SIVAL(p, 6, len);
684         p += 10;
685         cli_setup_bcc(cli, p);
686         cli_send_smb(cli);
687         if (!cli_receive_smb(cli)) {
688                 return False;
689         }
690
691         if (cli_is_error(cli)) {
692                 return False;
693         }
694
695         return True;
696 }
697
698 /****************************************************************************
699  Lock a file with 64 bit offsets.
700 ****************************************************************************/
701
702 BOOL cli_lock64(struct cli_state *cli, int fnum, 
703                 SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
704 {
705         char *p;
706         int saved_timeout = cli->timeout;
707         int ltype;
708
709         if (! (cli->capabilities & CAP_LARGE_FILES)) {
710                 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
711         }
712
713         ltype = (lock_type == READ_LOCK? 1 : 0);
714         ltype |= LOCKING_ANDX_LARGE_FILES;
715
716         memset(cli->outbuf,'\0',smb_size);
717         memset(cli->inbuf,'\0', smb_size);
718
719         set_message(cli->outbuf,8,0,True);
720
721         SCVAL(cli->outbuf,smb_com,SMBlockingX);
722         SSVAL(cli->outbuf,smb_tid,cli->cnum);
723         cli_setup_packet(cli);
724
725         SCVAL(cli->outbuf,smb_vwv0,0xFF);
726         SSVAL(cli->outbuf,smb_vwv2,fnum);
727         SCVAL(cli->outbuf,smb_vwv3,ltype);
728         SIVALS(cli->outbuf, smb_vwv4, timeout);
729         SSVAL(cli->outbuf,smb_vwv6,0);
730         SSVAL(cli->outbuf,smb_vwv7,1);
731
732         p = smb_buf(cli->outbuf);
733         SIVAL(p, 0, cli->pid);
734         SOFF_T_R(p, 4, offset);
735         SOFF_T_R(p, 12, len);
736         p += 20;
737
738         cli_setup_bcc(cli, p);
739         cli_send_smb(cli);
740
741         if (timeout != 0) {
742                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
743         }
744
745         if (!cli_receive_smb(cli)) {
746                 cli->timeout = saved_timeout;
747                 return False;
748         }
749
750         cli->timeout = saved_timeout;
751
752         if (cli_is_error(cli)) {
753                 return False;
754         }
755
756         return True;
757 }
758
759 /****************************************************************************
760  Unlock a file with 64 bit offsets.
761 ****************************************************************************/
762
763 BOOL cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
764 {
765         char *p;
766
767         if (! (cli->capabilities & CAP_LARGE_FILES)) {
768                 return cli_unlock(cli, fnum, offset, len);
769         }
770
771         memset(cli->outbuf,'\0',smb_size);
772         memset(cli->inbuf,'\0',smb_size);
773
774         set_message(cli->outbuf,8,0,True);
775
776         SCVAL(cli->outbuf,smb_com,SMBlockingX);
777         SSVAL(cli->outbuf,smb_tid,cli->cnum);
778         cli_setup_packet(cli);
779
780         SCVAL(cli->outbuf,smb_vwv0,0xFF);
781         SSVAL(cli->outbuf,smb_vwv2,fnum);
782         SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
783         SIVALS(cli->outbuf, smb_vwv4, 0);
784         SSVAL(cli->outbuf,smb_vwv6,1);
785         SSVAL(cli->outbuf,smb_vwv7,0);
786
787         p = smb_buf(cli->outbuf);
788         SIVAL(p, 0, cli->pid);
789         SOFF_T_R(p, 4, offset);
790         SOFF_T_R(p, 12, len);
791         p += 20;
792         cli_setup_bcc(cli, p);
793         cli_send_smb(cli);
794         if (!cli_receive_smb(cli)) {
795                 return False;
796         }
797
798         if (cli_is_error(cli)) {
799                 return False;
800         }
801
802         return True;
803 }
804
805
806 /****************************************************************************
807  Do a SMBgetattrE call.
808 ****************************************************************************/
809
810 BOOL cli_getattrE(struct cli_state *cli, int fd, 
811                   uint16 *attr, SMB_BIG_UINT *size, 
812                   time_t *c_time, time_t *a_time, time_t *m_time)
813 {
814         memset(cli->outbuf,'\0',smb_size);
815         memset(cli->inbuf,'\0',smb_size);
816
817         set_message(cli->outbuf,1,0,True);
818
819         SCVAL(cli->outbuf,smb_com,SMBgetattrE);
820         SSVAL(cli->outbuf,smb_tid,cli->cnum);
821         cli_setup_packet(cli);
822
823         SSVAL(cli->outbuf,smb_vwv0,fd);
824
825         cli_send_smb(cli);
826         if (!cli_receive_smb(cli)) {
827                 return False;
828         }
829         
830         if (cli_is_error(cli)) {
831                 return False;
832         }
833
834         if (size) {
835                 *size = IVAL(cli->inbuf, smb_vwv6);
836         }
837
838         if (attr) {
839                 *attr = SVAL(cli->inbuf,smb_vwv10);
840         }
841
842         if (c_time) {
843                 *c_time = make_unix_date3(cli->inbuf+smb_vwv0);
844         }
845
846         if (a_time) {
847                 *a_time = make_unix_date3(cli->inbuf+smb_vwv2);
848         }
849
850         if (m_time) {
851                 *m_time = make_unix_date3(cli->inbuf+smb_vwv4);
852         }
853
854         return True;
855 }
856
857 /****************************************************************************
858  Do a SMBgetatr call
859 ****************************************************************************/
860
861 BOOL cli_getatr(struct cli_state *cli, const char *fname, 
862                 uint16 *attr, size_t *size, time_t *t)
863 {
864         char *p;
865
866         memset(cli->outbuf,'\0',smb_size);
867         memset(cli->inbuf,'\0',smb_size);
868
869         set_message(cli->outbuf,0,0,True);
870
871         SCVAL(cli->outbuf,smb_com,SMBgetatr);
872         SSVAL(cli->outbuf,smb_tid,cli->cnum);
873         cli_setup_packet(cli);
874
875         p = smb_buf(cli->outbuf);
876         *p++ = 4;
877         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
878
879         cli_setup_bcc(cli, p);
880
881         cli_send_smb(cli);
882         if (!cli_receive_smb(cli)) {
883                 return False;
884         }
885         
886         if (cli_is_error(cli)) {
887                 return False;
888         }
889
890         if (size) {
891                 *size = IVAL(cli->inbuf, smb_vwv3);
892         }
893
894         if (t) {
895                 *t = make_unix_date3(cli->inbuf+smb_vwv1);
896         }
897
898         if (attr) {
899                 *attr = SVAL(cli->inbuf,smb_vwv0);
900         }
901
902
903         return True;
904 }
905
906 /****************************************************************************
907  Do a SMBsetatr call.
908 ****************************************************************************/
909
910 BOOL cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
911 {
912         char *p;
913
914         memset(cli->outbuf,'\0',smb_size);
915         memset(cli->inbuf,'\0',smb_size);
916
917         set_message(cli->outbuf,8,0,True);
918
919         SCVAL(cli->outbuf,smb_com,SMBsetatr);
920         SSVAL(cli->outbuf,smb_tid,cli->cnum);
921         cli_setup_packet(cli);
922
923         SSVAL(cli->outbuf,smb_vwv0, attr);
924         put_dos_date3(cli->outbuf,smb_vwv1, t);
925
926         p = smb_buf(cli->outbuf);
927         *p++ = 4;
928         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
929         *p++ = 4;
930
931         cli_setup_bcc(cli, p);
932
933         cli_send_smb(cli);
934         if (!cli_receive_smb(cli)) {
935                 return False;
936         }
937         
938         if (cli_is_error(cli)) {
939                 return False;
940         }
941
942         return True;
943 }
944
945 /****************************************************************************
946  Check for existance of a dir.
947 ****************************************************************************/
948 BOOL cli_chkpath(struct cli_state *cli, const char *path)
949 {
950         pstring path2;
951         char *p;
952         
953         pstrcpy(path2,path);
954         trim_string(path2,NULL,"\\");
955         if (!*path2) *path2 = '\\';
956         
957         memset(cli->outbuf,'\0',smb_size);
958         set_message(cli->outbuf,0,0,True);
959         SCVAL(cli->outbuf,smb_com,SMBchkpth);
960         SSVAL(cli->outbuf,smb_tid,cli->cnum);
961         cli_setup_packet(cli);
962         p = smb_buf(cli->outbuf);
963         *p++ = 4;
964         p += clistr_push(cli, p, path2, -1, STR_TERMINATE);
965
966         cli_setup_bcc(cli, p);
967
968         cli_send_smb(cli);
969         if (!cli_receive_smb(cli)) {
970                 return False;
971         }
972
973         if (cli_is_error(cli)) return False;
974
975         return True;
976 }
977
978 /****************************************************************************
979  Query disk space.
980 ****************************************************************************/
981
982 BOOL cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
983 {
984         memset(cli->outbuf,'\0',smb_size);
985         set_message(cli->outbuf,0,0,True);
986         SCVAL(cli->outbuf,smb_com,SMBdskattr);
987         SSVAL(cli->outbuf,smb_tid,cli->cnum);
988         cli_setup_packet(cli);
989
990         cli_send_smb(cli);
991         if (!cli_receive_smb(cli)) {
992                 return False;
993         }
994
995         *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
996         *total = SVAL(cli->inbuf,smb_vwv0);
997         *avail = SVAL(cli->inbuf,smb_vwv3);
998         
999         return True;
1000 }
1001
1002 /****************************************************************************
1003  Create and open a temporary file.
1004 ****************************************************************************/
1005
1006 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1007 {
1008         int len;
1009         char *p;
1010
1011         memset(cli->outbuf,'\0',smb_size);
1012         memset(cli->inbuf,'\0',smb_size);
1013
1014         set_message(cli->outbuf,3,0,True);
1015
1016         SCVAL(cli->outbuf,smb_com,SMBctemp);
1017         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1018         cli_setup_packet(cli);
1019
1020         SSVAL(cli->outbuf,smb_vwv0,0);
1021         SIVALS(cli->outbuf,smb_vwv1,-1);
1022
1023         p = smb_buf(cli->outbuf);
1024         *p++ = 4;
1025         p += clistr_push(cli, p, path, -1, STR_TERMINATE);
1026
1027         cli_setup_bcc(cli, p);
1028
1029         cli_send_smb(cli);
1030         if (!cli_receive_smb(cli)) {
1031                 return -1;
1032         }
1033
1034         if (cli_is_error(cli)) {
1035                 return -1;
1036         }
1037
1038         /* despite the spec, the result has a -1, followed by
1039            length, followed by name */
1040         p = smb_buf(cli->inbuf);
1041         p += 4;
1042         len = smb_buflen(cli->inbuf) - 4;
1043         if (len <= 0) return -1;
1044
1045         if (tmp_path) {
1046                 pstring path2;
1047                 clistr_pull(cli, path2, p, 
1048                             sizeof(path2), len, STR_ASCII);
1049                 *tmp_path = strdup(path2);
1050         }
1051
1052         return SVAL(cli->inbuf,smb_vwv0);
1053 }
1054
1055
1056 /* 
1057    send a raw ioctl - used by the torture code
1058 */
1059 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1060 {
1061         memset(cli->outbuf,'\0',smb_size);
1062         memset(cli->inbuf,'\0',smb_size);
1063
1064         set_message(cli->outbuf, 3, 0, True);
1065         SCVAL(cli->outbuf,smb_com,SMBioctl);
1066         cli_setup_packet(cli);
1067
1068         SSVAL(cli->outbuf, smb_vwv0, fnum);
1069         SSVAL(cli->outbuf, smb_vwv1, code>>16);
1070         SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1071
1072         cli_send_smb(cli);
1073         if (!cli_receive_smb(cli)) {
1074                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1075         }
1076
1077         if (cli_is_error(cli)) {
1078                 return cli_nt_error(cli);
1079         }
1080
1081         *blob = data_blob(NULL, 0);
1082
1083         return NT_STATUS_OK;
1084 }