2 Unix SMB/Netbios implementation.
5 Copyright (C) Ricky Poulten 1995-1997
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.
28 #define SEPARATORS " \t\n\r"
29 extern int DEBUGLEVEL;
32 /* These defines are for the do_setrattr routine, to indicate
33 * setting and reseting of file attributes in the function call */
37 static int attribute = aDIR | aSYSTEM | aHIDDEN;
39 #ifndef CLIENT_TIMEOUT
40 #define CLIENT_TIMEOUT (30*1000)
44 static int tp, ntarf, tbufsiz, ttarf;
45 /* Incremental mode */
47 /* Reset archive bit */
49 /* Include / exclude mode (true=include, false=exclude) */
52 static char **cliplist=NULL;
55 extern file_info def_finfo;
56 extern BOOL lowercase;
58 extern BOOL readbraw_supported;
60 extern pstring cur_dir;
61 extern int get_total_time_ms;
62 extern int get_total_size;
68 static void writetarheader();
69 static void do_atar();
72 static void fixtarname();
73 static int dotarbuf();
74 static void dozerobuf();
75 static void dotareof();
76 static void initarbuf();
77 static int do_setrattr();
79 /* restore functions */
80 static long readtarheader();
82 static void do_tarput();
83 static void unfixtarname();
86 * tar specific utitlities
89 /****************************************************************************
90 Write a tar header to buffer
91 ****************************************************************************/
92 static void writetarheader(int f, char *aname, int size, time_t mtime,
99 memset(hb.dummy, 0, sizeof(hb.dummy));
103 /* write a GNU tar style long header */
105 b = (char *)malloc(l+TBLOCK+100);
107 DEBUG(0,("out of memory\n"));
110 writetarheader(f, "/./@LongLink", l+1, 0, " 0 \0");
111 memset(b, 0, l+TBLOCK+100);
112 fixtarname(b, aname, l+1);
114 dotarbuf(f, b, TBLOCK*((i+(TBLOCK-1)/TBLOCK)));
118 /* use l + 1 to do the null too */
119 fixtarname(hb.dbuf.name, aname, (l >= NAMSIZ) ? NAMSIZ : l + 1);
122 strlower(hb.dbuf.name);
124 /* write out a "standard" tar format header */
126 hb.dbuf.name[NAMSIZ-1]='\0';
127 strcpy(hb.dbuf.mode, amode);
128 oct_it(0L, 8, hb.dbuf.uid);
129 oct_it(0L, 8, hb.dbuf.gid);
130 oct_it((long) size, 13, hb.dbuf.size);
131 oct_it((long) mtime, 13, hb.dbuf.mtime);
132 memcpy(hb.dbuf.chksum, " ", sizeof(hb.dbuf.chksum));
133 memset(hb.dbuf.linkname, 0, NAMSIZ);
134 if (strcmp("/./@LongLink", aname) == 0) {
135 /* we're doing a GNU tar long filename */
136 hb.dbuf.linkflag='L';
138 hb.dbuf.linkflag='0';
141 for (chk=0, i=sizeof(hb.dummy), jp=hb.dummy; --i>=0;) chk+=(0xFF & *jp++);
143 oct_it((long) chk, 8, hb.dbuf.chksum);
144 hb.dbuf.chksum[6] = '\0';
146 (void) dotarbuf(f, hb.dummy, sizeof(hb.dummy));
149 /****************************************************************************
150 Read a tar header into a hblock structure, and validate
151 ***************************************************************************/
152 static long readtarheader(union hblock *hb, file_info *finfo, char *prefix)
159 * read in a "standard" tar format header - we're not that interested
160 * in that many fields, though
163 /* check the checksum */
164 for (chk=0, i=sizeof(hb->dummy), jp=hb->dummy; --i>=0;) chk+=(0xFF & *jp++);
169 /* compensate for blanks in chksum header */
170 for (i=sizeof(hb->dbuf.chksum), jp=hb->dbuf.chksum; --i>=0;)
173 chk += ' ' * sizeof(hb->dbuf.chksum);
175 fchk=unoct(hb->dbuf.chksum, sizeof(hb->dbuf.chksum));
177 DEBUG(5, ("checksum totals chk=%d fchk=%d chksum=%s\n",
178 chk, fchk, hb->dbuf.chksum));
182 DEBUG(0, ("checksums don't match %d %d\n", fchk, chk));
186 strcpy(finfo->name, prefix);
188 /* use l + 1 to do the null too; do prefix - prefcnt to zap leading slash */
189 unfixtarname(finfo->name + strlen(prefix), hb->dbuf.name,
190 strlen(hb->dbuf.name) + 1);
192 /* can't handle links at present */
193 if (hb->dbuf.linkflag != '0') {
194 if (hb->dbuf.linkflag == 0) {
195 DEBUG(6, ("Warning: NULL link flag (gnu tar archive ?) %s\n",
198 DEBUG(0, ("this tar file appears to contain some kind of link - ignoring\n"));
203 if ((unoct(hb->dbuf.mode, sizeof(hb->dbuf.mode)) & S_IFDIR)
204 || (*(finfo->name+strlen(finfo->name)-1) == '\\'))
209 finfo->mode=0; /* we don't care about mode at the moment, we'll
210 * just make it a regular file */
212 * Bug fix by richard@sj.co.uk
214 * REC: restore times correctly (as does tar)
215 * We only get the modification time of the file; set the creation time
216 * from the mod. time, and the access time to current time
218 finfo->mtime = finfo->ctime = strtol(hb->dbuf.mtime, NULL, 8);
219 finfo->atime = time(NULL);
220 finfo->size = unoct(hb->dbuf.size, sizeof(hb->dbuf.size));
225 /****************************************************************************
226 Write out the tar buffer to tape or wherever
227 ****************************************************************************/
228 static int dotarbuf(int f, char *b, int n)
232 /* This routine and the next one should be the only ones that do write()s */
233 if (tp + n >= tbufsiz)
238 memcpy(tarbuf + tp, b, diff);
239 fail=fail && (1+write(f, tarbuf, tbufsiz));
246 fail=fail && (1 + write(f, b, tbufsiz));
252 memcpy(tarbuf+tp, b, n);
256 return(fail ? writ : 0);
259 /****************************************************************************
260 Write a zeros to buffer / tape
261 ****************************************************************************/
262 static void dozerobuf(int f, int n)
264 /* short routine just to write out n zeros to buffer -
265 * used to round files to nearest block
266 * and to do tar EOFs */
270 memset(tarbuf+tp, 0, tbufsiz-tp);
271 write(f, tarbuf, tbufsiz);
272 memset(tarbuf, 0, (tp+=n-tbufsiz));
276 memset(tarbuf+tp, 0, n);
281 /****************************************************************************
283 ****************************************************************************/
284 static void initarbuf()
286 /* initialize tar buffer */
287 tbufsiz=blocksize*TBLOCK;
288 tarbuf=malloc(tbufsiz);
290 /* reset tar buffer pointer and tar file counter and total dumped */
291 tp=0; ntarf=0; ttarf=0;
294 /****************************************************************************
295 Write two zero blocks at end of file
296 ****************************************************************************/
297 static void dotareof(int f)
300 /* Two zero blocks at end of file, write out full buffer */
302 (void) dozerobuf(f, TBLOCK);
303 (void) dozerobuf(f, TBLOCK);
305 if (fstat(f, &stbuf) == -1)
307 DEBUG(0, ("Couldn't stat file handle\n"));
311 /* Could be a pipe, in which case S_ISREG should fail,
312 * and we should write out at full size */
313 if (tp > 0) write(f, tarbuf, S_ISREG(stbuf.st_mode) ? tp : tbufsiz);
316 /****************************************************************************
317 (Un)mangle DOS pathname, make nonabsolute
318 ****************************************************************************/
319 static void fixtarname(char *tptr, char *fp, int l)
321 /* add a '.' to start of file name, convert from ugly dos \'s in path
322 * to lovely unix /'s :-} */
325 if(lp_client_code_page() == KANJI_CODEPAGE)
328 if (is_shift_jis (*fp)) {
332 } else if (is_kana (*fp)) {
335 } else if (*fp == '\\') {
349 *tptr=(*fp == '\\') ? '/' : *fp;
356 /****************************************************************************
357 Convert from decimal to octal string
358 ****************************************************************************/
359 static void oct_it (register long value, register int ndgs, register char *p)
361 /* Converts long to octal string, pads with leading zeros */
363 /* skip final null, but do final space */
367 /* Loop does at least one digit */
369 p[--ndgs] = '0' + (char) (value & 7);
372 while (ndgs > 0 && value != 0);
374 /* Do leading zeros */
379 /****************************************************************************
380 Convert from octal string to long
381 ***************************************************************************/
382 static long unoct(char *p, int ndgs)
385 /* Converts octal string to long, ignoring any non-digit */
390 value = (value << 3) | (long) (*p - '0');
398 /****************************************************************************
399 Compare two strings in a slash insensitive way, allowing s1 to match s2
400 if s1 is an "initial" string (up to directory marker). Thus, if s2 is
401 a file in any subdirectory of s1, declare a match.
402 ***************************************************************************/
403 static int strslashcmp(char *s1, char *s2)
409 || tolower(*s1) == tolower(*s2)
410 || (*s1 == '\\' && *s2=='/')
411 || (*s1 == '/' && *s2=='\\'))) {
415 /* if s1 has a trailing slash, it compared equal, so s1 is an "initial"
418 if (!*s1 && s1 != s1_0 && (*(s1-1) == '/' || *(s1-1) == '\\')) return 0;
420 /* ignore trailing slash on s1 */
421 if (!*s2 && (*s1 == '/' || *s1 == '\\') && !*(s1+1)) return 0;
423 /* check for s1 is an "initial" string of s2 */
424 if (*s2 == '/' || *s2 == '\\') return 0;
430 * general smb utility functions
432 /****************************************************************************
433 Set DOS file attributes
434 ***************************************************************************/
435 static int do_setrattr(char *fname, int attr, int setit)
438 * First get the existing attribs from existing file
449 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
450 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
452 if (!inbuf || !outbuf)
454 DEBUG(0,("out of memory\n"));
458 /* send an smb getatr message */
460 memset(outbuf,0,smb_size);
461 set_message(outbuf,0,2 + strlen(fname),True);
462 CVAL(outbuf,smb_com) = SMBgetatr;
463 SSVAL(outbuf,smb_tid,cnum);
469 p += (strlen(fname)+1);
474 send_smb(Client,outbuf);
475 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
477 if (CVAL(inbuf,smb_rcls) != 0)
478 DEBUG(5,("getatr: %s\n",smb_errstr(inbuf)));
481 DEBUG(5,("\nattr 0x%X time %d size %d\n",
482 (int)CVAL(inbuf,smb_vwv0),
483 SVAL(inbuf,smb_vwv1),
484 SVAL(inbuf,smb_vwv3)));
487 fattr=CVAL(inbuf,smb_vwv0);
489 /* combine found attributes with bits to be set or reset */
491 attr=setit ? (fattr | attr) : (fattr & ~attr);
493 /* now try and set attributes by sending smb reset message */
495 /* clear out buffer and start again */
496 memset(outbuf,0,smb_size);
497 set_message(outbuf,8,4 + strlen(fname),True);
498 CVAL(outbuf,smb_com) = SMBsetatr;
499 SSVAL(outbuf,smb_tid,cnum);
502 SSVAL(outbuf,smb_vwv0,attr);
507 p += (strlen(fname)+1);
512 send_smb(Client,outbuf);
513 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
515 if (CVAL(inbuf,smb_rcls) != 0)
517 DEBUG(0,("%s setting attributes on file %s\n",
518 smb_errstr(inbuf), fname));
519 free(inbuf);free(outbuf);
523 free(inbuf);free(outbuf);
527 /****************************************************************************
528 Create a file on a share
529 ***************************************************************************/
530 static BOOL smbcreat(file_info finfo, int *fnum, char *inbuf, char *outbuf)
533 /* *must* be called with buffer ready malloc'ed */
534 /* open remote file */
536 memset(outbuf,0,smb_size);
537 set_message(outbuf,3,2 + strlen(finfo.name),True);
538 CVAL(outbuf,smb_com) = SMBcreate;
539 SSVAL(outbuf,smb_tid,cnum);
542 SSVAL(outbuf,smb_vwv0,finfo.mode);
543 put_dos_date3(outbuf,smb_vwv1,finfo.mtime);
547 strcpy(p,finfo.name);
549 send_smb(Client,outbuf);
550 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
552 if (CVAL(inbuf,smb_rcls) != 0)
554 DEBUG(0,("%s opening remote file %s\n",smb_errstr(inbuf),
559 *fnum = SVAL(inbuf,smb_vwv0);
563 /****************************************************************************
564 Write a file to a share
565 ***************************************************************************/
566 static BOOL smbwrite(int fnum, int n, int low, int high, int left,
567 char *bufferp, char *inbuf, char *outbuf)
569 /* *must* be called with buffer ready malloc'ed */
571 memset(outbuf,0,smb_size);
572 set_message(outbuf,5,n + 3,True);
574 memcpy(smb_buf(outbuf)+3, bufferp, n);
576 set_message(outbuf,5,n + 3, False);
577 CVAL(outbuf,smb_com) = SMBwrite;
578 SSVAL(outbuf,smb_tid,cnum);
581 SSVAL(outbuf,smb_vwv0,fnum);
582 SSVAL(outbuf,smb_vwv1,n);
583 SIVAL(outbuf,smb_vwv2,low);
584 SSVAL(outbuf,smb_vwv4,left);
585 CVAL(smb_buf(outbuf),0) = 1;
586 SSVAL(smb_buf(outbuf),1,n);
588 send_smb(Client,outbuf);
589 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
591 if (CVAL(inbuf,smb_rcls) != 0)
593 DEBUG(0,("%s writing remote file\n",smb_errstr(inbuf)));
597 if (n != SVAL(inbuf,smb_vwv0))
599 DEBUG(0,("Error: only wrote %d bytes out of %d\n",
600 SVAL(inbuf,smb_vwv0), n));
607 /****************************************************************************
608 Close a file on a share
609 ***************************************************************************/
610 static BOOL smbshut(file_info finfo, int fnum, char *inbuf, char *outbuf)
612 /* *must* be called with buffer ready malloc'ed */
614 memset(outbuf,0,smb_size);
615 set_message(outbuf,3,0,True);
616 CVAL(outbuf,smb_com) = SMBclose;
617 SSVAL(outbuf,smb_tid,cnum);
620 SSVAL(outbuf,smb_vwv0,fnum);
621 put_dos_date3(outbuf,smb_vwv1,finfo.mtime);
623 DEBUG(3,("Setting date to %s (0x%X)",
624 asctime(LocalTime(&finfo.mtime)),
627 send_smb(Client,outbuf);
628 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
630 if (CVAL(inbuf,smb_rcls) != 0)
632 DEBUG(0,("%s closing remote file %s\n",smb_errstr(inbuf),
640 /****************************************************************************
641 Verify existence of path on share
642 ***************************************************************************/
643 static BOOL smbchkpath(char *fname, char *inbuf, char *outbuf)
647 memset(outbuf,0,smb_size);
648 set_message(outbuf,0,4 + strlen(fname),True);
649 CVAL(outbuf,smb_com) = SMBchkpth;
650 SSVAL(outbuf,smb_tid,cnum);
657 send_smb(Client,outbuf);
658 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
660 DEBUG(5,("smbchkpath: %s\n",smb_errstr(inbuf)));
662 return(CVAL(inbuf,smb_rcls) == 0);
665 /****************************************************************************
666 Make a directory on share
667 ***************************************************************************/
668 static BOOL smbmkdir(char *fname, char *inbuf, char *outbuf)
670 /* *must* be called with buffer ready malloc'ed */
673 memset(outbuf,0,smb_size);
674 set_message(outbuf,0,2 + strlen(fname),True);
676 CVAL(outbuf,smb_com) = SMBmkdir;
677 SSVAL(outbuf,smb_tid,cnum);
684 send_smb(Client,outbuf);
685 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
687 if (CVAL(inbuf,smb_rcls) != 0)
689 DEBUG(0,("%s making remote directory %s\n",
690 smb_errstr(inbuf),fname));
697 /****************************************************************************
698 Ensure a remote path exists (make if necessary)
699 ***************************************************************************/
700 static BOOL ensurepath(char *fname, char *inbuf, char *outbuf)
702 /* *must* be called with buffer ready malloc'ed */
703 /* ensures path exists */
705 pstring partpath, ffname;
706 char *p=fname, *basehack;
710 /* fname copied to ffname so can strtok */
712 strcpy(ffname, fname);
714 /* do a `basename' on ffname, so don't try and make file name directory */
715 if ((basehack=strrchr(ffname, '\\')) == NULL)
720 p=strtok(ffname, "\\");
726 if (!smbchkpath(partpath, inbuf, outbuf)) {
727 if (!smbmkdir(partpath, inbuf, outbuf))
729 DEBUG(0, ("Error mkdirhiering\n"));
733 DEBUG(3, ("mkdirhiering %s\n", partpath));
737 strcat(partpath, "\\");
738 p = strtok(NULL,"/\\");
744 int padit(char *buf, int bufsize, int padsize)
749 DEBUG(0, ("Padding with %d zeros\n", padsize));
750 memset(buf, 0, bufsize);
751 while( !berr && padsize > 0 ) {
752 bytestowrite= MIN(bufsize, padsize);
753 berr = dotarbuf(tarhandle, buf, bytestowrite) != bytestowrite;
754 padsize -= bytestowrite;
761 * smbclient functions
763 /****************************************************************************
764 append one remote file to the tar file
765 ***************************************************************************/
766 static void do_atar(char *rname,char *lname,file_info *finfo1)
773 BOOL close_done = False;
774 BOOL shallitime=True;
775 BOOL ignore_close_error = False;
779 struct timeval tp_start;
780 GetTimeOfDay(&tp_start);
787 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
788 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
790 if (!inbuf || !outbuf)
792 DEBUG(0,("out of memory\n"));
796 memset(outbuf,0,smb_size);
797 set_message(outbuf,15,1 + strlen(rname),True);
799 CVAL(outbuf,smb_com) = SMBopenX;
800 SSVAL(outbuf,smb_tid,cnum);
803 SSVAL(outbuf,smb_vwv0,0xFF);
804 SSVAL(outbuf,smb_vwv2,1);
805 SSVAL(outbuf,smb_vwv3,(DENY_NONE<<4));
806 SSVAL(outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
807 SSVAL(outbuf,smb_vwv5,aSYSTEM | aHIDDEN);
808 SSVAL(outbuf,smb_vwv8,1);
812 p = skip_string(p,1);
814 dos_clean_name(rname);
816 /* do a chained openX with a readX? */
819 SSVAL(outbuf,smb_vwv0,SMBreadX);
820 SSVAL(outbuf,smb_vwv1,PTR_DIFF(p,outbuf) - 4);
824 SSVAL(p,smb_vwv0,0xFF);
825 SSVAL(p,smb_vwv5,MIN(max_xmit-500,finfo.size));
826 SSVAL(p,smb_vwv9,MIN(0xFFFF,finfo.size));
827 smb_setlen(outbuf,smb_len(outbuf)+11*2+1);
830 send_smb(Client,outbuf);
831 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
833 if (CVAL(inbuf,smb_rcls) != 0)
835 if (CVAL(inbuf,smb_rcls) == ERRSRV &&
836 SVAL(inbuf,smb_err) == ERRnoresource &&
837 reopen_connection(inbuf,outbuf))
839 do_atar(rname,lname,finfo1);
840 free(inbuf);free(outbuf);
844 DEBUG(0,("%s opening remote file %s\n",smb_errstr(inbuf),rname));
845 free(inbuf);free(outbuf);
849 strcpy(finfo.name,rname);
852 finfo.mode = SVAL(inbuf,smb_vwv3);
853 finfo.size = IVAL(inbuf,smb_vwv4);
854 finfo.mtime = make_unix_date3(inbuf+smb_vwv6);
855 finfo.atime = finfo.ctime = finfo.mtime;
858 DEBUG(3,("file %s attrib 0x%X\n",finfo.name,finfo.mode));
860 fnum = SVAL(inbuf,smb_vwv2);
862 if (tar_inc && !(finfo.mode & aARCH))
864 DEBUG(4, ("skipping %s - archive bit not set\n", finfo.name));
869 if (SVAL(inbuf,smb_vwv0) == SMBreadX)
871 p = (inbuf+4+SVAL(inbuf,smb_vwv1)) - smb_wct;
872 datalen = SVAL(p,smb_vwv5);
873 dataptr = inbuf + 4 + SVAL(p,smb_vwv6);
881 DEBUG(1,("getting file %s of size %d bytes as a tar file %s",
886 /* write a tar header, don't bother with mode - just set to 100644 */
887 writetarheader(tarhandle, rname, finfo.size, finfo.mtime, "100644 \0");
889 while (nread < finfo.size && !close_done)
892 static BOOL can_chain_close=True;
896 DEBUG(3,("nread=%d\n",nread));
898 /* 3 possible read types. readbraw if a large block is required.
899 readX + close if not much left and read if neither is supported */
901 /* we might have already read some data from a chained readX */
902 if (dataptr && datalen>0)
905 /* if we can finish now then readX+close */
906 if (method<0 && can_chain_close && (Protocol >= PROTOCOL_LANMAN1) &&
907 ((finfo.size - nread) <
908 (max_xmit - (2*smb_size + 13*SIZEOFWORD + 300))))
911 /* if we support readraw then use that */
912 if (method<0 && readbraw_supported)
915 /* if we can then use readX */
916 if (method<0 && (Protocol >= PROTOCOL_LANMAN1))
928 /* use readX + close */
929 memset(outbuf,0,smb_size);
930 set_message(outbuf,10,0,True);
931 CVAL(outbuf,smb_com) = SMBreadX;
932 SSVAL(outbuf,smb_tid,cnum);
937 CVAL(outbuf,smb_vwv0) = SMBclose;
938 SSVAL(outbuf,smb_vwv1,PTR_DIFF(smb_buf(outbuf),outbuf) - 4);
941 CVAL(outbuf,smb_vwv0) = 0xFF;
944 SSVAL(outbuf,smb_vwv2,fnum);
945 SIVAL(outbuf,smb_vwv3,nread);
946 SSVAL(outbuf,smb_vwv5,MIN(max_xmit-200,finfo.size - nread));
947 SSVAL(outbuf,smb_vwv6,0);
948 SIVAL(outbuf,smb_vwv7,0);
949 SSVAL(outbuf,smb_vwv9,MIN(0xFFFF,finfo.size-nread));
960 /* now set the total packet length */
961 smb_setlen(outbuf,smb_len(outbuf)+9);
964 send_smb(Client,outbuf);
965 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
967 if (CVAL(inbuf,smb_rcls) != 0)
969 DEBUG(0,("Error %s reading remote file\n",smb_errstr(inbuf)));
974 SVAL(inbuf,smb_vwv0) != SMBclose)
976 /* NOTE: WfWg sometimes just ignores the chained
977 command! This seems to break the spec? */
978 DEBUG(3,("Rejected chained close?\n"));
980 can_chain_close = False;
981 ignore_close_error = True;
984 datalen = SVAL(inbuf,smb_vwv5);
985 dataptr = inbuf + 4 + SVAL(inbuf,smb_vwv6);
992 static int readbraw_size = 0xFFFF;
995 memset(outbuf,0,smb_size);
996 set_message(outbuf,8,0,True);
997 CVAL(outbuf,smb_com) = SMBreadbraw;
998 SSVAL(outbuf,smb_tid,cnum);
1000 SSVAL(outbuf,smb_vwv0,fnum);
1001 SIVAL(outbuf,smb_vwv1,nread);
1002 SSVAL(outbuf,smb_vwv3,MIN(finfo.size-nread,readbraw_size));
1003 SSVAL(outbuf,smb_vwv4,0);
1004 SIVALS(outbuf,smb_vwv5,-1);
1005 send_smb(Client,outbuf);
1007 /* Now read the raw data into the buffer and write it */
1008 if(read_smb_length(Client,inbuf,0) == -1) {
1009 DEBUG(0,("Failed to read length in readbraw\n"));
1013 /* Even though this is not an smb message, smb_len
1014 returns the generic length of an smb message */
1015 datalen = smb_len(inbuf);
1019 /* we got a readbraw error */
1020 DEBUG(4,("readbraw error - reducing size\n"));
1021 readbraw_size = (readbraw_size * 9) / 10;
1023 if (readbraw_size < max_xmit)
1025 DEBUG(0,("disabling readbraw\n"));
1026 readbraw_supported = False;
1033 if(read_data(Client,inbuf,datalen) != datalen) {
1034 DEBUG(0,("Failed to read data in readbraw\n"));
1042 /* we've already read some data with a chained readX */
1046 /* use plain read */
1047 memset(outbuf,0,smb_size);
1048 set_message(outbuf,5,0,True);
1049 CVAL(outbuf,smb_com) = SMBread;
1050 SSVAL(outbuf,smb_tid,cnum);
1053 SSVAL(outbuf,smb_vwv0,fnum);
1054 SSVAL(outbuf,smb_vwv1,MIN(max_xmit-200,finfo.size - nread));
1055 SIVAL(outbuf,smb_vwv2,nread);
1056 SSVAL(outbuf,smb_vwv4,finfo.size - nread);
1058 send_smb(Client,outbuf);
1059 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
1061 if (CVAL(inbuf,smb_rcls) != 0)
1063 DEBUG(0,("Error %s reading remote file\n",smb_errstr(inbuf)));
1067 datalen = SVAL(inbuf,smb_vwv0);
1068 dataptr = smb_buf(inbuf) + 3;
1073 /* add received bits of file to buffer - dotarbuf will
1074 * write out in 512 byte intervals */
1075 if (dotarbuf(tarhandle,dataptr,datalen) != datalen)
1077 DEBUG(0,("Error writing local file\n"));
1084 DEBUG(0,("Error reading file %s. Got 0 bytes\n", rname));
1092 /* pad tar file with zero's if we couldn't get entire file */
1093 if (nread < finfo.size)
1095 DEBUG(0, ("Didn't get entire file. size=%d, nread=%d\n", finfo.size, nread));
1096 if (padit(inbuf, BUFFER_SIZE, finfo.size - nread))
1097 DEBUG(0,("Error writing local file\n"));
1100 /* round tar file to nearest block */
1101 if (finfo.size % TBLOCK)
1102 dozerobuf(tarhandle, TBLOCK - (finfo.size % TBLOCK));
1104 ttarf+=finfo.size + TBLOCK - (finfo.size % TBLOCK);
1110 memset(outbuf,0,smb_size);
1111 set_message(outbuf,3,0,True);
1112 CVAL(outbuf,smb_com) = SMBclose;
1113 SSVAL(outbuf,smb_tid,cnum);
1116 SSVAL(outbuf,smb_vwv0,fnum);
1117 SIVALS(outbuf,smb_vwv1,-1);
1119 send_smb(Client,outbuf);
1120 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
1122 if (!ignore_close_error && CVAL(inbuf,smb_rcls) != 0)
1124 DEBUG(0,("Error %s closing remote file\n",smb_errstr(inbuf)));
1125 free(inbuf);free(outbuf);
1132 struct timeval tp_end;
1135 /* if shallitime is true then we didn't skip */
1136 if (tar_reset) (void) do_setrattr(finfo.name, aARCH, ATTRRESET);
1138 GetTimeOfDay(&tp_end);
1140 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1141 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1142 get_total_time_ms += this_time;
1143 get_total_size += finfo.size;
1145 /* Thanks to Carel-Jan Engel (ease@mail.wirehub.nl) for this one */
1146 DEBUG(1,("(%g kb/s) (average %g kb/s)\n",
1147 finfo.size / MAX(0.001, (1.024*this_time)),
1148 get_total_size / MAX(0.001, (1.024*get_total_time_ms))));
1151 free(inbuf);free(outbuf);
1154 /****************************************************************************
1155 Append single file to tar file (or not)
1156 ***************************************************************************/
1157 static void do_tar(file_info *finfo)
1161 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1164 /* Is it on the exclude list ? */
1165 if (!tar_excl && clipn) {
1168 strcpy(exclaim, cur_dir);
1169 *(exclaim+strlen(exclaim)-1)='\0';
1171 strcat(exclaim, "\\");
1172 strcat(exclaim, finfo->name);
1174 if (clipfind(cliplist, clipn, exclaim)) {
1175 DEBUG(3,("Skipping file %s\n", exclaim));
1180 if (finfo->mode & aDIR)
1182 pstring saved_curdir;
1184 char *inbuf,*outbuf;
1186 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
1187 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
1189 if (!inbuf || !outbuf)
1191 DEBUG(0,("out of memory\n"));
1195 strcpy(saved_curdir,cur_dir);
1197 strcat(cur_dir,finfo->name);
1198 strcat(cur_dir,"\\");
1200 /* write a tar directory, don't bother with mode - just set it to
1202 writetarheader(tarhandle, cur_dir, 0, finfo->mtime, "040755 \0");
1203 strcpy(mtar_mask,cur_dir);
1204 strcat(mtar_mask,"*");
1206 do_dir((char *)inbuf,(char *)outbuf,mtar_mask,attribute,do_tar,recurse);
1207 strcpy(cur_dir,saved_curdir);
1208 free(inbuf);free(outbuf);
1212 strcpy(rname,cur_dir);
1213 strcat(rname,finfo->name);
1214 do_atar(rname,finfo->name,finfo);
1218 /****************************************************************************
1219 Convert from UNIX to DOS file names
1220 ***************************************************************************/
1221 static void unfixtarname(char *tptr, char *fp, int l)
1223 /* remove '.' from start of file name, convert from unix /'s to
1224 * dos \'s in path. Kill any absolute path names.
1227 if (*fp == '.') fp++;
1228 if (*fp == '\\' || *fp == '/') fp++;
1230 if(lp_client_code_page() == KANJI_CODEPAGE)
1233 if (is_shift_jis (*fp)) {
1237 } else if (is_kana (*fp)) {
1240 } else if (*fp == '/') {
1254 *tptr=(*fp == '/') ? '\\' : *fp;
1261 static void do_tarput()
1264 int nread=0, bufread;
1265 char *inbuf,*outbuf;
1268 struct timeval tp_start;
1269 BOOL tskip=False; /* We'll take each file as it comes */
1271 GetTimeOfDay(&tp_start);
1273 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
1274 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
1276 if (!inbuf || !outbuf)
1278 DEBUG(0,("out of memory\n"));
1283 * Must read in tbufsiz dollops
1286 /* These should be the only reads in clitar.c */
1287 while ((bufread=read(tarhandle, tarbuf, tbufsiz))>0) {
1288 char *bufferp, *endofbuffer;
1291 /* Code to handle a short read.
1292 * We always need a TBLOCK full of stuff
1294 if (bufread % TBLOCK) {
1295 int lchunk=TBLOCK-(bufread % TBLOCK);
1298 /* It's a shorty - a short read that is */
1299 DEBUG(3, ("Short read, read %d so far (need %d)\n", bufread, lchunk));
1301 while ((lread=read(tarhandle, tarbuf+bufread, lchunk))>0) {
1303 if (!(lchunk-=lread)) break;
1306 /* If we've reached EOF then that must be a short file */
1307 if (lread<=0) break;
1311 endofbuffer=tarbuf+bufread;
1314 if (fsize<bufread) {
1319 if (fsize==bufread) tskip=False;
1328 switch (readtarheader((union hblock *) bufferp, &finfo, cur_dir))
1330 case -2: /* something dodgy but not fatal about this */
1331 DEBUG(0, ("skipping %s...\n", finfo.name));
1332 bufferp+=TBLOCK; /* header - like a link */
1335 DEBUG(0, ("abandoning restore\n"));
1336 free(inbuf); free(outbuf);
1338 case 0: /* chksum is zero - we assume that one all zero
1339 *header block will do for eof */
1341 ("total of %d tar files restored to share\n", ntarf));
1342 free(inbuf); free(outbuf);
1349 && (clipfind(cliplist, clipn, finfo.name) ^ tar_excl);
1352 if (finfo.mode & aDIR)
1354 else if ((fsize=finfo.size) % TBLOCK) {
1355 fsize+=TBLOCK-(fsize%TBLOCK);
1357 if (fsize<endofbuffer-bufferp) {
1362 fsize-=endofbuffer-bufferp;
1367 if (finfo.mode & aDIR)
1369 if (!smbchkpath(finfo.name, inbuf, outbuf)
1370 && !smbmkdir(finfo.name, inbuf, outbuf))
1372 DEBUG(0, ("abandoning restore\n"));
1373 free(inbuf); free(outbuf);
1385 if (ensurepath(finfo.name, inbuf, outbuf)
1386 && !smbcreat(finfo, &fnum, inbuf, outbuf))
1388 DEBUG(0, ("abandoning restore\n"));
1389 free(inbuf);free(outbuf);
1393 DEBUG(0,("restore tar file %s of size %d bytes\n",
1394 finfo.name,finfo.size));
1397 if ((bufferp+=TBLOCK) >= endofbuffer) break;
1400 /* write out the file in chunk sized chunks - don't
1401 * go past end of buffer though */
1402 chunk=(fsize-nread < endofbuffer - bufferp)
1403 ? fsize - nread : endofbuffer - bufferp;
1406 int minichunk=MIN(chunk, max_xmit-200);
1408 if (!smbwrite(fnum, /* file descriptor */
1410 nread, /* offset low */
1411 0, /* offset high - not implemented */
1412 fsize-nread, /* left - only hint to server */
1417 DEBUG(0, ("Error writing remote file\n"));
1418 free(inbuf); free(outbuf);
1421 DEBUG(5, ("chunk writing fname=%s fnum=%d nread=%d minichunk=%d chunk=%d size=%d\n", finfo.name, fnum, nread, minichunk, chunk, fsize));
1423 bufferp+=minichunk; nread+=minichunk;
1429 if (!smbshut(finfo, fnum, inbuf, outbuf))
1431 DEBUG(0, ("Error closing remote file\n"));
1432 free(inbuf);free(outbuf);
1435 if (fsize % TBLOCK) bufferp+=TBLOCK - (fsize % TBLOCK);
1436 DEBUG(5, ("bufferp is now %d (psn=%d)\n",
1437 (long) bufferp, (long)(bufferp - tarbuf)));
1441 } while (bufferp < endofbuffer);
1444 DEBUG(0, ("premature eof on tar file ?\n"));
1445 DEBUG(0,("total of %d tar files restored to share\n", ntarf));
1447 free(inbuf); free(outbuf);
1451 * samba interactive commands
1454 /****************************************************************************
1456 ***************************************************************************/
1457 void cmd_block(void)
1462 if (!next_token(NULL,buf,NULL))
1464 DEBUG(0, ("blocksize <n>\n"));
1469 if (block < 0 || block > 65535)
1471 DEBUG(0, ("blocksize out of range"));
1476 DEBUG(1,("blocksize is now %d\n", blocksize));
1479 /****************************************************************************
1480 command to set incremental / reset mode
1481 ***************************************************************************/
1482 void cmd_tarmode(void)
1486 while (next_token(NULL,buf,NULL)) {
1487 if (strequal(buf, "full"))
1489 else if (strequal(buf, "inc"))
1491 else if (strequal(buf, "reset"))
1493 else if (strequal(buf, "noreset"))
1495 else DEBUG(0, ("tarmode: unrecognised option %s\n", buf));
1498 DEBUG(0, ("tarmode is now %s, %s\n",
1499 tar_inc ? "incremental" : "full",
1500 tar_reset ? "reset" : "noreset"));
1503 /****************************************************************************
1504 Feeble attrib command
1505 ***************************************************************************/
1506 void cmd_setmode(void)
1514 attra[0] = attra[1] = 0;
1516 if (!next_token(NULL,buf,NULL))
1518 DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
1522 strcpy(fname, cur_dir);
1525 while (next_token(NULL,buf,NULL)) {
1534 case 'r': attra[direct]|=aRONLY;
1536 case 'h': attra[direct]|=aHIDDEN;
1538 case 's': attra[direct]|=aSYSTEM;
1540 case 'a': attra[direct]|=aARCH;
1542 default: DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
1547 if (attra[ATTRSET]==0 && attra[ATTRRESET]==0)
1549 DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
1553 DEBUG(1, ("\nperm set %d %d\n", attra[ATTRSET], attra[ATTRRESET]));
1554 (void) do_setrattr(fname, attra[ATTRSET], ATTRSET);
1555 (void) do_setrattr(fname, attra[ATTRRESET], ATTRRESET);
1558 /****************************************************************************
1559 Principal command for creating / extracting
1560 ***************************************************************************/
1561 void cmd_tar(char *inbuf, char *outbuf)
1567 if (!next_token(NULL,buf,NULL))
1569 DEBUG(0,("tar <c|x>[IXbga] <filename>\n"));
1573 argl=toktocliplist(&argcl, NULL);
1574 if (!tar_parseargs(argcl, argl, buf, 0))
1577 process_tar(inbuf, outbuf);
1582 /****************************************************************************
1583 Command line (option) version
1584 ***************************************************************************/
1585 int process_tar(char *inbuf, char *outbuf)
1596 if (clipn && tar_excl) {
1600 for (i=0; i<clipn; i++) {
1601 DEBUG(0,("arg %d = %s\n", i, cliplist[i]));
1603 if (*(cliplist[i]+strlen(cliplist[i])-1)=='\\') {
1604 *(cliplist[i]+strlen(cliplist[i])-1)='\0';
1607 if (strrchr(cliplist[i], '\\')) {
1610 strcpy(saved_dir, cur_dir);
1612 if (*cliplist[i]=='\\') {
1613 strcpy(tarmac, cliplist[i]);
1615 strcpy(tarmac, cur_dir);
1616 strcat(tarmac, cliplist[i]);
1618 strcpy(cur_dir, tarmac);
1619 *(strrchr(cur_dir, '\\')+1)='\0';
1621 do_dir((char *)inbuf,(char *)outbuf,tarmac,attribute,do_tar,recurse);
1622 strcpy(cur_dir,saved_dir);
1624 strcpy(tarmac, cur_dir);
1625 strcat(tarmac, cliplist[i]);
1626 do_dir((char *)inbuf,(char *)outbuf,tarmac,attribute,do_tar,recurse);
1631 strcpy(mask,cur_dir);
1633 do_dir((char *)inbuf,(char *)outbuf,mask,attribute,do_tar,recurse);
1636 if (ntarf) dotareof(tarhandle);
1640 DEBUG(0, ("tar: dumped %d tar files\n", ntarf));
1641 DEBUG(0, ("Total bytes written: %d\n", ttarf));
1648 /****************************************************************************
1649 Find a token (filename) in a clip list
1650 ***************************************************************************/
1651 int clipfind(char **aret, int ret, char *tok)
1653 if (aret==NULL) return 0;
1655 /* ignore leading slashes or dots in token */
1656 while(strchr("/\\.", *tok)) tok++;
1661 /* ignore leading slashes or dots in list */
1662 while(strchr("/\\.", *pkey)) pkey++;
1664 if (!strslashcmp(pkey, tok)) return 1;
1670 /****************************************************************************
1671 Parse tar arguments. Sets tar_type, tar_excl, etc.
1672 ***************************************************************************/
1673 int tar_parseargs(int argc, char *argv[], char *Optarg, int Optind)
1675 char tar_clipfl='\0';
1677 /* Reset back to defaults - could be from interactive version
1678 * reset mode and archive mode left as they are though
1689 if (tar_type=='c') {
1690 printf("Tar must be followed by only one of c or x.\n");
1696 if (Optind>=argc || !(blocksize=atoi(argv[Optind]))) {
1697 DEBUG(0,("Option b must be followed by valid blocksize\n"));
1708 DEBUG(0,("Option N must be followed by valid file name\n"));
1712 extern time_t newer_than;
1714 if (sys_stat(argv[Optind], &stbuf) == 0) {
1715 newer_than = stbuf.st_mtime;
1716 DEBUG(1,("Getting files newer than %s",
1717 asctime(LocalTime(&newer_than))));
1720 DEBUG(0,("Error setting newer-than time\n"));
1730 DEBUG(0,("Only one of I,X must be specified\n"));
1737 DEBUG(0,("Only one of I,X must be specified\n"));
1743 DEBUG(0,("Unknown tar option\n"));
1748 printf("Option T must be followed by one of c or x.\n");
1752 tar_excl=tar_clipfl!='X';
1753 if (Optind+1<argc) {
1754 cliplist=argv+Optind+1;
1755 clipn=argc-Optind-1;
1757 if (Optind>=argc || !strcmp(argv[Optind], "-")) {
1758 /* Sets tar handle to either 0 or 1, as appropriate */
1759 tarhandle=(tar_type=='c');
1761 if ((tar_type=='x' && (tarhandle = open(argv[Optind], O_RDONLY)) == -1)
1762 || (tar_type=='c' && (tarhandle=creat(argv[Optind], 0644)) < 0))
1764 DEBUG(0,("Error opening local file %s\n",argv[Optind]));