2 Unix SMB/Netbios implementation.
5 Copyright (C) Ricky Poulten 1995
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.
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.
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.
26 extern void setup_pkt(char *outbuf);
27 extern BOOL reopen_connection(char *inbuf,char *outbuf);
28 extern void do_dir(char *inbuf,char *outbuf,char *Mask,int attribute,void (*fn)(),BOOL recurse_dir);
30 int tar_parseargs(int argc, char *argv[], char *Optarg, int Optind);
34 #define SEPARATORS " \t\n\r"
35 extern int DEBUGLEVEL;
38 /* These defines are for the do_setrattr routine, to indicate
39 * setting and reseting of file attributes in the function call */
43 static int attribute = aDIR | aSYSTEM | aHIDDEN;
45 #ifndef CLIENT_TIMEOUT
46 #define CLIENT_TIMEOUT (30*1000)
50 static int tp, ntarf, tbufsiz;
51 /* Incremental mode */
53 /* Reset archive bit */
55 /* Include / exclude mode (true=include, false=exclude) */
58 static char **cliplist=NULL;
61 extern file_info def_finfo;
62 extern BOOL lowercase;
64 extern BOOL readbraw_supported;
66 extern pstring cur_dir;
67 extern int get_total_time_ms;
68 extern int get_total_size;
74 static void writetarheader();
75 static void do_atar();
78 static void fixtarname();
79 static int dotarbuf();
80 static void dozerobuf();
81 static void dotareof();
82 static void initarbuf();
83 static int do_setrattr();
86 char **toktocliplist();
88 /* restore functions */
89 static long readtarheader();
91 static void do_tarput();
92 static void unfixtarname();
95 * tar specific utitlities
98 /****************************************************************************
99 Write a tar header to buffer
100 ****************************************************************************/
101 static void writetarheader(int f, char *aname, int size, time_t mtime,
108 memset(hb.dummy, 0, sizeof(hb.dummy));
113 DEBUG(0, ("tar file %s name length exceeds NAMSIZ\n", aname));
116 /* use l + 1 to do the null too */
117 fixtarname(hb.dbuf.name, aname, (l >= NAMSIZ) ? NAMSIZ : l + 1);
120 strlower(hb.dbuf.name);
122 /* write out a "standard" tar format header */
124 hb.dbuf.name[NAMSIZ-1]='\0';
125 strcpy(hb.dbuf.mode, amode);
126 oct_it(0L, 8, hb.dbuf.uid);
127 oct_it(0L, 8, hb.dbuf.gid);
128 oct_it((long) size, 13, hb.dbuf.size);
129 oct_it((long) mtime, 13, hb.dbuf.mtime);
130 memcpy(hb.dbuf.chksum, " ", sizeof(hb.dbuf.chksum));
131 hb.dbuf.linkflag='0';
132 memset(hb.dbuf.linkname, 0, NAMSIZ);
134 for (chk=0, i=sizeof(hb.dummy), jp=hb.dummy; --i>=0;) chk+=(0xFF & *jp++);
136 oct_it((long) chk, 8, hb.dbuf.chksum);
137 hb.dbuf.chksum[6] = '\0';
139 (void) dotarbuf(f, hb.dummy, sizeof(hb.dummy));
142 /****************************************************************************
143 Read a tar header into a hblock structure, and validate
144 ***************************************************************************/
145 static long readtarheader(union hblock *hb, file_info *finfo, char *prefix)
152 * read in a "standard" tar format header - we're not that interested
153 * in that many fields, though
156 /* check the checksum */
157 for (chk=0, i=sizeof(hb->dummy), jp=hb->dummy; --i>=0;) chk+=(0xFF & *jp++);
162 /* compensate for blanks in chksum header */
163 for (i=sizeof(hb->dbuf.chksum), jp=hb->dbuf.chksum; --i>=0;)
166 chk += ' ' * sizeof(hb->dbuf.chksum);
168 fchk=unoct(hb->dbuf.chksum, sizeof(hb->dbuf.chksum));
170 DEBUG(5, ("checksum totals chk=%d fchk=%d chksum=%s\n",
171 chk, fchk, hb->dbuf.chksum));
175 DEBUG(0, ("checksums don't match %d %d\n", fchk, chk));
179 strcpy(finfo->name, prefix);
181 /* use l + 1 to do the null too; do prefix - prefcnt to zap leading slash */
182 unfixtarname(finfo->name + strlen(prefix), hb->dbuf.name,
183 strlen(hb->dbuf.name) + 1);
185 /* can't handle links at present */
186 if (hb->dbuf.linkflag != '0') {
187 if (hb->dbuf.linkflag == 0) {
188 DEBUG(6, ("Warning: NULL link flag (gnu tar archive ?) %s\n",
191 DEBUG(0, ("this tar file appears to contain some kind of link - ignoring\n"));
196 if ((unoct(hb->dbuf.mode, sizeof(hb->dbuf.mode)) & S_IFDIR)
197 || (*(finfo->name+strlen(finfo->name)-1) == '\\'))
202 finfo->mode=0; /* we don't care about mode at the moment, we'll
203 * just make it a regular file */
205 * Bug fix by richard@sj.co.uk
207 * REC: restore times correctly (as does tar)
208 * We only get the modification time of the file; set the creation time
209 * from the mod. time, and the access time to current time
211 finfo->mtime = finfo->ctime = strtol(hb->dbuf.mtime, NULL, 8);
212 finfo->atime = time(NULL);
213 finfo->size = unoct(hb->dbuf.size, sizeof(hb->dbuf.size));
218 /****************************************************************************
219 Write out the tar buffer to tape or wherever
220 ****************************************************************************/
221 static int dotarbuf(int f, char *b, int n)
225 /* This routine and the next one should be the only ones that do write()s */
226 if (tp + n >= tbufsiz)
231 memcpy(tarbuf + tp, b, diff);
232 fail=fail && (1+write(f, tarbuf, tbufsiz));
239 fail=fail && (1 + write(f, b, tbufsiz));
245 memcpy(tarbuf+tp, b, n);
249 return(fail ? writ : 0);
252 /****************************************************************************
253 Write a zeros to buffer / tape
254 ****************************************************************************/
255 static void dozerobuf(int f, int n)
257 /* short routine just to write out n zeros to buffer -
258 * used to round files to nearest block
259 * and to do tar EOFs */
263 memset(tarbuf+tp, 0, tbufsiz-tp);
264 write(f, tarbuf, tbufsiz);
265 memset(tarbuf, 0, (tp+=n-tbufsiz));
269 memset(tarbuf+tp, 0, n);
274 /****************************************************************************
276 ****************************************************************************/
277 static void initarbuf()
279 /* initialize tar buffer */
280 tbufsiz=blocksize*TBLOCK;
281 tarbuf=malloc(tbufsiz);
283 /* reset tar buffer pointer and tar file counter */
287 /****************************************************************************
288 Write two zero blocks at end of file
289 ****************************************************************************/
290 static void dotareof(int f)
293 /* Two zero blocks at end of file, write out full buffer */
295 (void) dozerobuf(f, TBLOCK);
296 (void) dozerobuf(f, TBLOCK);
298 if (fstat(f, &stbuf) == -1)
300 DEBUG(0, ("Couldn't stat file handle\n"));
304 /* Could be a pipe, in which case S_ISREG should fail,
305 * and we should write out at full size */
306 if (tp > 0) write(f, tarbuf, S_ISREG(stbuf.st_mode) ? tp : tbufsiz);
309 /****************************************************************************
310 (Un)mangle DOS pathname, make nonabsolute
311 ****************************************************************************/
312 static void fixtarname(char *tptr, char *fp, int l)
314 /* add a '.' to start of file name, convert from ugly dos \'s in path
315 * to lovely unix /'s :-} */
320 if (is_shift_jis (*fp)) {
324 } else if (is_kana (*fp)) {
327 } else if (*fp == '\\') {
337 while (l--) { *tptr=(*fp == '\\') ? '/' : *fp; tptr++; fp++; }
341 /****************************************************************************
342 Convert from decimal to octal string
343 ****************************************************************************/
344 static void oct_it (register long value, register int ndgs, register char *p)
346 /* Converts long to octal string, pads with leading zeros */
348 /* skip final null, but do final space */
352 /* Loop does at least one digit */
354 p[--ndgs] = '0' + (char) (value & 7);
357 while (ndgs > 0 && value != 0);
359 /* Do leading zeros */
364 /****************************************************************************
365 Convert from octal string to long
366 ***************************************************************************/
367 static long unoct(char *p, int ndgs)
370 /* Converts octal string to long, ignoring any non-digit */
375 value = (value << 3) | (long) (*p - '0');
383 /****************************************************************************
384 Compare two strings in a slash insensitive way
385 ***************************************************************************/
386 int strslashcmp(const char *s1, const char *s2)
390 || tolower(*s1) == tolower(*s2)
391 || (*s1 == '\\' && *s2=='/')
392 || (*s1 == '/' && *s2=='\\'))) {
400 * general smb utility functions
402 /****************************************************************************
403 Set DOS file attributes
404 ***************************************************************************/
405 static int do_setrattr(char *fname, int attr, int setit)
408 * First get the existing attribs from existing file
419 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
420 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
422 if (!inbuf || !outbuf)
424 DEBUG(0,("out of memory\n"));
428 /* send an smb getatr message */
430 memset(outbuf,0,smb_size);
431 set_message(outbuf,0,2 + strlen(fname),True);
432 CVAL(outbuf,smb_com) = SMBgetatr;
433 SSVAL(outbuf,smb_tid,cnum);
439 p += (strlen(fname)+1);
444 send_smb(Client,outbuf);
445 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
447 if (CVAL(inbuf,smb_rcls) != 0)
448 DEBUG(5,("getatr: %s\n",smb_errstr(inbuf)));
451 DEBUG(5,("\nattr 0x%X time %d size %d\n",
452 (int)CVAL(inbuf,smb_vwv0),
453 SVAL(inbuf,smb_vwv1),
454 SVAL(inbuf,smb_vwv3)));
457 fattr=CVAL(inbuf,smb_vwv0);
459 /* combine found attributes with bits to be set or reset */
461 attr=setit ? (fattr | attr) : (fattr & ~attr);
463 /* now try and set attributes by sending smb reset message */
465 /* clear out buffer and start again */
466 memset(outbuf,0,smb_size);
467 set_message(outbuf,8,4 + strlen(fname),True);
468 CVAL(outbuf,smb_com) = SMBsetatr;
469 SSVAL(outbuf,smb_tid,cnum);
472 SSVAL(outbuf,smb_vwv0,attr);
477 p += (strlen(fname)+1);
482 send_smb(Client,outbuf);
483 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
485 if (CVAL(inbuf,smb_rcls) != 0)
487 DEBUG(0,("%s setting attributes on file %s\n",
488 smb_errstr(inbuf), fname));
489 free(inbuf);free(outbuf);
493 free(inbuf);free(outbuf);
497 /****************************************************************************
498 Create a file on a share
499 ***************************************************************************/
500 static BOOL smbcreat(file_info finfo, int *fnum, char *inbuf, char *outbuf)
503 /* *must* be called with buffer ready malloc'ed */
504 /* open remote file */
506 memset(outbuf,0,smb_size);
507 set_message(outbuf,3,2 + strlen(finfo.name),True);
508 CVAL(outbuf,smb_com) = SMBcreate;
509 SSVAL(outbuf,smb_tid,cnum);
512 SSVAL(outbuf,smb_vwv0,finfo.mode);
513 put_dos_date3(outbuf,smb_vwv1,finfo.mtime);
517 strcpy(p,finfo.name);
519 send_smb(Client,outbuf);
520 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
522 if (CVAL(inbuf,smb_rcls) != 0)
524 DEBUG(0,("%s opening remote file %s\n",smb_errstr(inbuf),
529 *fnum = SVAL(inbuf,smb_vwv0);
533 /****************************************************************************
534 Write a file to a share
535 ***************************************************************************/
536 static BOOL smbwrite(int fnum, int n, int low, int high, int left,
537 char *bufferp, char *inbuf, char *outbuf)
539 /* *must* be called with buffer ready malloc'ed */
541 memset(outbuf,0,smb_size);
542 set_message(outbuf,5,n + 3,True);
544 memcpy(smb_buf(outbuf)+3, bufferp, n);
546 set_message(outbuf,5,n + 3, False);
547 CVAL(outbuf,smb_com) = SMBwrite;
548 SSVAL(outbuf,smb_tid,cnum);
551 SSVAL(outbuf,smb_vwv0,fnum);
552 SSVAL(outbuf,smb_vwv1,n);
553 SIVAL(outbuf,smb_vwv2,low);
554 SSVAL(outbuf,smb_vwv4,left);
555 CVAL(smb_buf(outbuf),0) = 1;
556 SSVAL(smb_buf(outbuf),1,n);
558 send_smb(Client,outbuf);
559 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
561 if (CVAL(inbuf,smb_rcls) != 0)
563 DEBUG(0,("%s writing remote file\n",smb_errstr(inbuf)));
567 if (n != SVAL(inbuf,smb_vwv0))
569 DEBUG(0,("Error: only wrote %d bytes out of %d\n",
570 SVAL(inbuf,smb_vwv0), n));
577 /****************************************************************************
578 Close a file on a share
579 ***************************************************************************/
580 static BOOL smbshut(file_info finfo, int fnum, char *inbuf, char *outbuf)
582 /* *must* be called with buffer ready malloc'ed */
584 memset(outbuf,0,smb_size);
585 set_message(outbuf,3,0,True);
586 CVAL(outbuf,smb_com) = SMBclose;
587 SSVAL(outbuf,smb_tid,cnum);
590 SSVAL(outbuf,smb_vwv0,fnum);
591 put_dos_date3(outbuf,smb_vwv1,finfo.mtime);
593 DEBUG(3,("Setting date to %s (0x%X)",
594 asctime(LocalTime(&finfo.mtime,GMT_TO_LOCAL)),
597 send_smb(Client,outbuf);
598 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
600 if (CVAL(inbuf,smb_rcls) != 0)
602 DEBUG(0,("%s closing remote file %s\n",smb_errstr(inbuf),
610 /****************************************************************************
611 Verify existence of path on share
612 ***************************************************************************/
613 static BOOL smbchkpath(char *fname, char *inbuf, char *outbuf)
617 memset(outbuf,0,smb_size);
618 set_message(outbuf,0,4 + strlen(fname),True);
619 CVAL(outbuf,smb_com) = SMBchkpth;
620 SSVAL(outbuf,smb_tid,cnum);
627 send_smb(Client,outbuf);
628 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
630 DEBUG(5,("smbchkpath: %s\n",smb_errstr(inbuf)));
632 return(CVAL(inbuf,smb_rcls) == 0);
635 /****************************************************************************
636 Make a directory on share
637 ***************************************************************************/
638 static BOOL smbmkdir(char *fname, char *inbuf, char *outbuf)
640 /* *must* be called with buffer ready malloc'ed */
643 memset(outbuf,0,smb_size);
644 set_message(outbuf,0,2 + strlen(fname),True);
646 CVAL(outbuf,smb_com) = SMBmkdir;
647 SSVAL(outbuf,smb_tid,cnum);
654 send_smb(Client,outbuf);
655 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
657 if (CVAL(inbuf,smb_rcls) != 0)
659 DEBUG(0,("%s making remote directory %s\n",
660 smb_errstr(inbuf),fname));
667 /****************************************************************************
668 Ensure a remote path exists (make if necessary)
669 ***************************************************************************/
670 static BOOL ensurepath(char *fname, char *inbuf, char *outbuf)
672 /* *must* be called with buffer ready malloc'ed */
673 /* ensures path exists */
675 pstring partpath, ffname;
676 char *p=fname, *basehack;
680 /* fname copied to ffname so can strtok */
682 strcpy(ffname, fname);
684 /* do a `basename' on ffname, so don't try and make file name directory */
685 if ((basehack=strrchr(ffname, '\\')) == NULL)
690 p=strtok(ffname, "\\");
696 if (!smbchkpath(partpath, inbuf, outbuf)) {
697 if (!smbmkdir(partpath, inbuf, outbuf))
699 DEBUG(0, ("Error mkdirhiering\n"));
703 DEBUG(3, ("mkdirhiering %s\n", partpath));
707 strcat(partpath, "\\");
708 p = strtok(NULL,"/\\");
715 * smbclient functions
717 /****************************************************************************
718 append one remote file to the tar file
719 ***************************************************************************/
720 static void do_atar(char *rname,char *lname,file_info *finfo1)
727 BOOL close_done = False;
728 BOOL shallitime=True;
729 BOOL ignore_close_error = False;
733 struct timeval tp_start;
734 GetTimeOfDay(&tp_start);
741 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
742 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
744 if (!inbuf || !outbuf)
746 DEBUG(0,("out of memory\n"));
750 memset(outbuf,0,smb_size);
751 set_message(outbuf,15,1 + strlen(rname),True);
753 CVAL(outbuf,smb_com) = SMBopenX;
754 SSVAL(outbuf,smb_tid,cnum);
757 SSVAL(outbuf,smb_vwv0,0xFF);
758 SSVAL(outbuf,smb_vwv2,1);
759 SSVAL(outbuf,smb_vwv3,(DENY_NONE<<4));
760 SSVAL(outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
761 SSVAL(outbuf,smb_vwv5,aSYSTEM | aHIDDEN);
762 SSVAL(outbuf,smb_vwv8,1);
766 p = skip_string(p,1);
768 dos_clean_name(rname);
770 /* do a chained openX with a readX? */
773 SSVAL(outbuf,smb_vwv0,SMBreadX);
774 SSVAL(outbuf,smb_vwv1,PTR_DIFF(p,outbuf) - 4);
778 SSVAL(p,smb_vwv0,0xFF);
779 SSVAL(p,smb_vwv5,MIN(max_xmit-500,finfo.size));
780 SSVAL(p,smb_vwv9,MIN(0xFFFF,finfo.size));
781 smb_setlen(outbuf,smb_len(outbuf)+11*2+1);
784 send_smb(Client,outbuf);
785 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
787 if (CVAL(inbuf,smb_rcls) != 0)
789 if (CVAL(inbuf,smb_rcls) == ERRSRV &&
790 SVAL(inbuf,smb_err) == ERRnoresource &&
791 reopen_connection(inbuf,outbuf))
793 do_atar(rname,lname,finfo1);
794 free(inbuf);free(outbuf);
798 DEBUG(0,("%s opening remote file %s\n",smb_errstr(inbuf),rname));
799 free(inbuf);free(outbuf);
803 strcpy(finfo.name,rname);
806 finfo.mode = SVAL(inbuf,smb_vwv3);
807 finfo.size = IVAL(inbuf,smb_vwv4);
808 finfo.mtime = make_unix_date3(inbuf+smb_vwv6);
809 finfo.atime = finfo.ctime = finfo.mtime;
812 DEBUG(3,("file %s attrib 0x%X\n",finfo.name,finfo.mode));
814 fnum = SVAL(inbuf,smb_vwv2);
816 if (tar_inc && !(finfo.mode & aARCH))
818 DEBUG(4, ("skipping %s - archive bit not set\n", finfo.name));
823 if (SVAL(inbuf,smb_vwv0) == SMBreadX)
825 p = (inbuf+4+SVAL(inbuf,smb_vwv1)) - smb_wct;
826 datalen = SVAL(p,smb_vwv5);
827 dataptr = inbuf + 4 + SVAL(p,smb_vwv6);
835 DEBUG(2,("getting file %s of size %d bytes as a tar file %s",
840 /* write a tar header, don't bother with mode - just set to 100644 */
841 writetarheader(tarhandle, rname, finfo.size, finfo.mtime, "100644 \0");
843 while (nread < finfo.size && !close_done)
846 static BOOL can_chain_close=True;
850 DEBUG(3,("nread=%d\n",nread));
852 /* 3 possible read types. readbraw if a large block is required.
853 readX + close if not much left and read if neither is supported */
855 /* we might have already read some data from a chained readX */
856 if (dataptr && datalen>0)
859 /* if we can finish now then readX+close */
860 if (method<0 && can_chain_close && (Protocol >= PROTOCOL_LANMAN1) &&
861 ((finfo.size - nread) <
862 (max_xmit - (2*smb_size + 13*SIZEOFWORD + 300))))
865 /* if we support readraw then use that */
866 if (method<0 && readbraw_supported)
869 /* if we can then use readX */
870 if (method<0 && (Protocol >= PROTOCOL_LANMAN1))
882 /* use readX + close */
883 memset(outbuf,0,smb_size);
884 set_message(outbuf,10,0,True);
885 CVAL(outbuf,smb_com) = SMBreadX;
886 SSVAL(outbuf,smb_tid,cnum);
891 CVAL(outbuf,smb_vwv0) = SMBclose;
892 SSVAL(outbuf,smb_vwv1,PTR_DIFF(smb_buf(outbuf),outbuf) - 4);
895 CVAL(outbuf,smb_vwv0) = 0xFF;
898 SSVAL(outbuf,smb_vwv2,fnum);
899 SIVAL(outbuf,smb_vwv3,nread);
900 SSVAL(outbuf,smb_vwv5,MIN(max_xmit-200,finfo.size - nread));
901 SSVAL(outbuf,smb_vwv6,0);
902 SIVAL(outbuf,smb_vwv7,0);
903 SSVAL(outbuf,smb_vwv9,MIN(0xFFFF,finfo.size-nread));
914 /* now set the total packet length */
915 smb_setlen(outbuf,smb_len(outbuf)+9);
918 send_smb(Client,outbuf);
919 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
921 if (CVAL(inbuf,smb_rcls) != 0)
923 DEBUG(0,("Error %s reading remote file\n",smb_errstr(inbuf)));
928 SVAL(inbuf,smb_vwv0) != SMBclose)
930 /* NOTE: WfWg sometimes just ignores the chained
931 command! This seems to break the spec? */
932 DEBUG(3,("Rejected chained close?\n"));
934 can_chain_close = False;
935 ignore_close_error = True;
938 datalen = SVAL(inbuf,smb_vwv5);
939 dataptr = inbuf + 4 + SVAL(inbuf,smb_vwv6);
946 static int readbraw_size = 0xFFFF;
949 memset(outbuf,0,smb_size);
950 set_message(outbuf,8,0,True);
951 CVAL(outbuf,smb_com) = SMBreadbraw;
952 SSVAL(outbuf,smb_tid,cnum);
954 SSVAL(outbuf,smb_vwv0,fnum);
955 SIVAL(outbuf,smb_vwv1,nread);
956 SSVAL(outbuf,smb_vwv3,MIN(finfo.size-nread,readbraw_size));
957 SSVAL(outbuf,smb_vwv4,0);
958 SIVALS(outbuf,smb_vwv5,-1);
959 send_smb(Client,outbuf);
961 /* Now read the raw data into the buffer and write it */
962 if(read_smb_length(Client,inbuf,0) == -1) {
963 DEBUG(0,("Failed to read length in readbraw\n"));
967 /* Even though this is not an smb message, smb_len
968 returns the generic length of an smb message */
969 datalen = smb_len(inbuf);
973 /* we got a readbraw error */
974 DEBUG(4,("readbraw error - reducing size\n"));
975 readbraw_size = (readbraw_size * 9) / 10;
977 if (readbraw_size < max_xmit)
979 DEBUG(0,("disabling readbraw\n"));
980 readbraw_supported = False;
987 if(read_data(Client,inbuf,datalen) != datalen) {
988 DEBUG(0,("Failed to read data in readbraw\n"));
996 /* we've already read some data with a chained readX */
1000 /* use plain read */
1001 memset(outbuf,0,smb_size);
1002 set_message(outbuf,5,0,True);
1003 CVAL(outbuf,smb_com) = SMBread;
1004 SSVAL(outbuf,smb_tid,cnum);
1007 SSVAL(outbuf,smb_vwv0,fnum);
1008 SSVAL(outbuf,smb_vwv1,MIN(max_xmit-200,finfo.size - nread));
1009 SIVAL(outbuf,smb_vwv2,nread);
1010 SSVAL(outbuf,smb_vwv4,finfo.size - nread);
1012 send_smb(Client,outbuf);
1013 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
1015 if (CVAL(inbuf,smb_rcls) != 0)
1017 DEBUG(0,("Error %s reading remote file\n",smb_errstr(inbuf)));
1021 datalen = SVAL(inbuf,smb_vwv0);
1022 dataptr = smb_buf(inbuf) + 3;
1027 /* add received bits of file to buffer - dotarbuf will
1028 * write out in 512 byte intervals */
1029 if (dotarbuf(tarhandle,dataptr,datalen) != datalen)
1031 DEBUG(0,("Error writing local file\n"));
1038 DEBUG(0,("Error reading file %s. Got 0 bytes\n", rname));
1046 /* round tar file to nearest block */
1047 if (finfo.size % TBLOCK)
1048 dozerobuf(tarhandle, TBLOCK - (finfo.size % TBLOCK));
1055 memset(outbuf,0,smb_size);
1056 set_message(outbuf,3,0,True);
1057 CVAL(outbuf,smb_com) = SMBclose;
1058 SSVAL(outbuf,smb_tid,cnum);
1061 SSVAL(outbuf,smb_vwv0,fnum);
1062 SIVALS(outbuf,smb_vwv1,-1);
1064 send_smb(Client,outbuf);
1065 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
1067 if (!ignore_close_error && CVAL(inbuf,smb_rcls) != 0)
1069 DEBUG(0,("Error %s closing remote file\n",smb_errstr(inbuf)));
1070 free(inbuf);free(outbuf);
1077 struct timeval tp_end;
1080 /* if shallitime is true then we didn't skip */
1081 if (tar_reset) (void) do_setrattr(finfo.name, aARCH, ATTRRESET);
1083 GetTimeOfDay(&tp_end);
1085 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1086 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1087 get_total_time_ms += this_time;
1088 get_total_size += finfo.size;
1090 /* Thanks to Carel-Jan Engel (ease@mail.wirehub.nl) for this one */
1091 DEBUG(2,("(%g kb/s) (average %g kb/s)\n",
1092 finfo.size / MAX(0.001, (1.024*this_time)),
1093 get_total_size / MAX(0.001, (1.024*get_total_time_ms))));
1096 free(inbuf);free(outbuf);
1099 /****************************************************************************
1100 Append single file to tar file (or not)
1101 ***************************************************************************/
1102 static void do_tar(file_info *finfo)
1106 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1109 /* Is it on the exclude list ? */
1110 if (!tar_excl && clipn) {
1113 strcpy(exclaim, cur_dir);
1114 *(exclaim+strlen(exclaim)-1)='\0';
1116 if (clipfind(cliplist, clipn, exclaim)) {
1117 DEBUG(3,("Skipping directory %s\n", exclaim));
1121 strcat(exclaim, "\\");
1122 strcat(exclaim, finfo->name);
1124 if (clipfind(cliplist, clipn, exclaim)) {
1125 DEBUG(3,("Skipping file %s\n", exclaim));
1130 if (finfo->mode & aDIR)
1132 pstring saved_curdir;
1134 char *inbuf,*outbuf;
1136 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
1137 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
1139 if (!inbuf || !outbuf)
1141 DEBUG(0,("out of memory\n"));
1145 strcpy(saved_curdir,cur_dir);
1147 strcat(cur_dir,finfo->name);
1148 strcat(cur_dir,"\\");
1150 /* write a tar directory, don't bother with mode - just set it to
1152 writetarheader(tarhandle, cur_dir, 0, finfo->mtime, "040755 \0");
1153 strcpy(mtar_mask,cur_dir);
1154 strcat(mtar_mask,"*");
1156 do_dir((char *)inbuf,(char *)outbuf,mtar_mask,attribute,do_tar,recurse);
1157 strcpy(cur_dir,saved_curdir);
1158 free(inbuf);free(outbuf);
1162 strcpy(rname,cur_dir);
1163 strcat(rname,finfo->name);
1164 do_atar(rname,finfo->name,finfo);
1168 /****************************************************************************
1169 Convert from UNIX to DOS file names
1170 ***************************************************************************/
1171 static void unfixtarname(char *tptr, char *fp, int l)
1173 /* remove '.' from start of file name, convert from unix /'s to
1174 * dos \'s in path. Kill any absolute path names.
1177 if (*fp == '.') fp++;
1178 if (*fp == '\\' || *fp == '/') fp++;
1182 if (is_shift_jis (*fp)) {
1186 } else if (is_kana (*fp)) {
1189 } else if (*fp == '/') {
1199 while (l--) { *tptr=(*fp == '/') ? '\\' : *fp; tptr++; fp++; }
1203 static void do_tarput()
1206 int nread=0, bufread;
1207 char *inbuf,*outbuf;
1210 struct timeval tp_start;
1211 BOOL tskip=False; /* We'll take each file as it comes */
1213 GetTimeOfDay(&tp_start);
1215 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
1216 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
1218 if (!inbuf || !outbuf)
1220 DEBUG(0,("out of memory\n"));
1225 * Must read in tbufsiz dollops
1228 /* These should be the only reads in clitar.c */
1229 while ((bufread=read(tarhandle, tarbuf, tbufsiz))>0) {
1230 char *bufferp, *endofbuffer;
1233 /* Code to handle a short read.
1234 * We always need a TBLOCK full of stuff
1236 if (bufread % TBLOCK) {
1237 int lchunk=TBLOCK-(bufread % TBLOCK);
1240 /* It's a shorty - a short read that is */
1241 DEBUG(3, ("Short read, read %d so far (need %d)\n", bufread, lchunk));
1243 while ((lread=read(tarhandle, tarbuf+bufread, lchunk))>0) {
1245 if (!(lchunk-=lread)) break;
1248 /* If we've reached EOF then that must be a short file */
1249 if (lread<=0) break;
1253 endofbuffer=tarbuf+bufread;
1256 if (fsize<bufread) {
1261 if (fsize==bufread) tskip=False;
1270 switch (readtarheader((union hblock *) bufferp, &finfo, cur_dir))
1272 case -2: /* something dodgy but not fatal about this */
1273 DEBUG(0, ("skipping %s...\n", finfo.name));
1274 bufferp+=TBLOCK; /* header - like a link */
1277 DEBUG(0, ("abandoning restore\n"));
1278 free(inbuf); free(outbuf);
1280 case 0: /* chksum is zero - we assume that one all zero
1281 *header block will do for eof */
1283 ("total of %d tar files restored to share\n", ntarf));
1284 free(inbuf); free(outbuf);
1291 && (clipfind(cliplist, clipn, finfo.name) ^ tar_excl);
1294 if (finfo.mode & aDIR)
1296 else if ((fsize=finfo.size) % TBLOCK) {
1297 fsize+=TBLOCK-(fsize%TBLOCK);
1299 if (fsize<endofbuffer-bufferp) {
1304 fsize-=endofbuffer-bufferp;
1309 if (finfo.mode & aDIR)
1311 if (!smbchkpath(finfo.name, inbuf, outbuf)
1312 && !smbmkdir(finfo.name, inbuf, outbuf))
1314 DEBUG(0, ("abandoning restore\n"));
1315 free(inbuf); free(outbuf);
1327 if (ensurepath(finfo.name, inbuf, outbuf)
1328 && !smbcreat(finfo, &fnum, inbuf, outbuf))
1330 DEBUG(0, ("abandoning restore\n"));
1331 free(inbuf);free(outbuf);
1335 DEBUG(0,("restore tar file %s of size %d bytes\n",
1336 finfo.name,finfo.size));
1339 if ((bufferp+=TBLOCK) >= endofbuffer) break;
1342 /* write out the file in chunk sized chunks - don't
1343 * go past end of buffer though */
1344 chunk=(fsize-nread < endofbuffer - bufferp)
1345 ? fsize - nread : endofbuffer - bufferp;
1348 int minichunk=MIN(chunk, max_xmit-200);
1350 if (!smbwrite(fnum, /* file descriptor */
1352 nread, /* offset low */
1353 0, /* offset high - not implemented */
1354 fsize-nread, /* left - only hint to server */
1359 DEBUG(0, ("Error writing remote file\n"));
1360 free(inbuf); free(outbuf);
1363 DEBUG(5, ("chunk writing fname=%s fnum=%d nread=%d minichunk=%d chunk=%d size=%d\n", finfo.name, fnum, nread, minichunk, chunk, fsize));
1365 bufferp+=minichunk; nread+=minichunk;
1371 if (!smbshut(finfo, fnum, inbuf, outbuf))
1373 DEBUG(0, ("Error closing remote file\n"));
1374 free(inbuf);free(outbuf);
1377 if (fsize % TBLOCK) bufferp+=TBLOCK - (fsize % TBLOCK);
1378 DEBUG(5, ("bufferp is now %d (psn=%d)\n",
1379 (long) bufferp, (long)(bufferp - tarbuf)));
1383 } while (bufferp < endofbuffer);
1386 DEBUG(0, ("premature eof on tar file ?\n"));
1387 DEBUG(0,("total of %d tar files restored to share\n", ntarf));
1389 free(inbuf); free(outbuf);
1393 * samba interactive commands
1396 /****************************************************************************
1398 ***************************************************************************/
1399 void cmd_block(void)
1404 if (!next_token(NULL,buf,NULL))
1406 DEBUG(0, ("blocksize <n>\n"));
1411 if (block < 0 || block > 65535)
1413 DEBUG(0, ("blocksize out of range"));
1418 DEBUG(2,("blocksize is now %d\n", blocksize));
1421 /****************************************************************************
1422 command to set incremental / reset mode
1423 ***************************************************************************/
1424 void cmd_tarmode(void)
1428 while (next_token(NULL,buf,NULL)) {
1429 if (strequal(buf, "full"))
1431 else if (strequal(buf, "inc"))
1433 else if (strequal(buf, "reset"))
1435 else if (strequal(buf, "noreset"))
1437 else DEBUG(0, ("tarmode: unrecognised option %s\n", buf));
1440 DEBUG(0, ("tarmode is now %s, %s\n",
1441 tar_inc ? "incremental" : "full",
1442 tar_reset ? "reset" : "noreset"));
1445 /****************************************************************************
1446 Feeble attrib command
1447 ***************************************************************************/
1448 void cmd_setmode(void)
1456 attra[0] = attra[1] = 0;
1458 if (!next_token(NULL,buf,NULL))
1460 DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
1464 strcpy(fname, cur_dir);
1467 while (next_token(NULL,buf,NULL)) {
1476 case 'r': attra[direct]|=aRONLY;
1478 case 'h': attra[direct]|=aHIDDEN;
1480 case 's': attra[direct]|=aSYSTEM;
1482 case 'a': attra[direct]|=aARCH;
1484 default: DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
1489 if (attra[ATTRSET]==0 && attra[ATTRRESET]==0)
1491 DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
1495 DEBUG(2, ("\nperm set %d %d\n", attra[ATTRSET], attra[ATTRRESET]));
1496 (void) do_setrattr(fname, attra[ATTRSET], ATTRSET);
1497 (void) do_setrattr(fname, attra[ATTRRESET], ATTRRESET);
1500 /****************************************************************************
1501 Principal command for creating / extracting
1502 ***************************************************************************/
1503 void cmd_tar(char *inbuf, char *outbuf)
1509 if (!next_token(NULL,buf,NULL))
1511 DEBUG(0,("tar <c|x>[IXbga] <filename>\n"));
1515 argl=toktocliplist(&argcl, NULL);
1516 if (!tar_parseargs(argcl, argl, buf, 0))
1519 process_tar(inbuf, outbuf);
1524 /****************************************************************************
1525 Command line (option) version
1526 ***************************************************************************/
1527 int process_tar(char *inbuf, char *outbuf)
1538 if (clipn && tar_excl) {
1542 for (i=0; i<clipn; i++) {
1543 DEBUG(0,("arg %d = %s\n", i, cliplist[i]));
1545 if (*(cliplist[i]+strlen(cliplist[i])-1)=='\\') {
1546 *(cliplist[i]+strlen(cliplist[i])-1)='\0';
1549 if (strrchr(cliplist[i], '\\')) {
1552 strcpy(saved_dir, cur_dir);
1554 if (*cliplist[i]=='\\') {
1555 strcpy(tarmac, cliplist[i]);
1557 strcpy(tarmac, cur_dir);
1558 strcat(tarmac, cliplist[i]);
1560 strcpy(cur_dir, tarmac);
1561 *(strrchr(cur_dir, '\\')+1)='\0';
1563 do_dir((char *)inbuf,(char *)outbuf,tarmac,attribute,do_tar,recurse);
1564 strcpy(cur_dir,saved_dir);
1566 strcpy(tarmac, cur_dir);
1567 strcat(tarmac, cliplist[i]);
1568 do_dir((char *)inbuf,(char *)outbuf,tarmac,attribute,do_tar,recurse);
1573 strcpy(mask,cur_dir);
1575 do_dir((char *)inbuf,(char *)outbuf,mask,attribute,do_tar,recurse);
1578 if (ntarf) dotareof(tarhandle);
1582 DEBUG(0, ("tar: dumped %d tar files\n", ntarf));
1589 /****************************************************************************
1590 Find a token (filename) in a clip list
1591 ***************************************************************************/
1592 int clipfind(char **aret, int ret, char *tok)
1594 if (aret==NULL) return 0;
1596 /* ignore leading slashes or dots in token */
1597 while(strchr("/\\.", *tok)) tok++;
1602 /* ignore leading slashes or dots in list */
1603 while(strchr("/\\.", *pkey)) pkey++;
1605 if (!strslashcmp(pkey, tok)) return 1;
1611 /****************************************************************************
1612 Parse tar arguments. Sets tar_type, tar_excl, etc.
1613 ***************************************************************************/
1614 int tar_parseargs(int argc, char *argv[], char *Optarg, int Optind)
1616 char tar_clipfl='\0';
1618 /* Reset back to defaults - could be from interactive version
1619 * reset mode and archive mode left as they are though
1630 if (tar_type=='c') {
1631 printf("Tar must be followed by only one of c or x.\n");
1637 if (Optind>=argc || !(blocksize=atoi(argv[Optind]))) {
1638 DEBUG(0,("Option b must be followed by valid blocksize\n"));
1649 DEBUG(0,("Option N must be followed by valid file name\n"));
1653 extern time_t newer_than;
1655 if (sys_stat(argv[Optind], &stbuf) == 0) {
1656 newer_than = stbuf.st_mtime;
1657 DEBUG(1,("Getting files newer than %s",
1658 asctime(LocalTime(&newer_than,GMT_TO_LOCAL))));
1661 DEBUG(0,("Error setting newer-than time\n"));
1671 DEBUG(0,("Only one of I,X must be specified\n"));
1678 DEBUG(0,("Only one of I,X must be specified\n"));
1684 DEBUG(0,("Unknown tar option\n"));
1689 printf("Option T must be followed by one of c or x.\n");
1693 if (Optind>=argc || !strcmp(argv[Optind], "-")) {
1694 /* Sets tar handle to either 0 or 1, as appropriate */
1695 tarhandle=(tar_type=='c');
1697 tar_excl=tar_clipfl!='X';
1699 if (Optind+1<argc) {
1700 cliplist=argv+Optind+1;
1701 clipn=argc-Optind-1;
1704 if ((tar_type=='x' && (tarhandle = open(argv[Optind], O_RDONLY)) == -1)
1705 || (tar_type=='c' && (tarhandle=creat(argv[Optind], 0644)) < 0))
1707 DEBUG(0,("Error opening local file %s\n",argv[Optind]));