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.
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;
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();
80 char **toktocliplist();
82 /* restore functions */
83 static long readtarheader();
85 static void do_tarput();
86 static void unfixtarname();
89 * tar specific utitlities
92 /****************************************************************************
93 Write a tar header to buffer
94 ****************************************************************************/
95 static void writetarheader(int f, char *aname, int size, time_t mtime,
102 memset(hb.dummy, 0, sizeof(hb.dummy));
107 DEBUG(0, ("tar file %s name length exceeds NAMSIZ\n", aname));
110 /* use l + 1 to do the null too */
111 fixtarname(hb.dbuf.name, aname, (l >= NAMSIZ) ? NAMSIZ : l + 1);
114 strlower(hb.dbuf.name);
116 /* write out a "standard" tar format header */
118 hb.dbuf.name[NAMSIZ-1]='\0';
119 strcpy(hb.dbuf.mode, amode);
120 oct_it(0L, 8, hb.dbuf.uid);
121 oct_it(0L, 8, hb.dbuf.gid);
122 oct_it((long) size, 13, hb.dbuf.size);
123 oct_it((long) mtime, 13, hb.dbuf.mtime);
124 memcpy(hb.dbuf.chksum, " ", sizeof(hb.dbuf.chksum));
125 hb.dbuf.linkflag='0';
126 memset(hb.dbuf.linkname, 0, NAMSIZ);
128 for (chk=0, i=sizeof(hb.dummy), jp=hb.dummy; --i>=0;) chk+=(0xFF & *jp++);
130 oct_it((long) chk, 8, hb.dbuf.chksum);
131 hb.dbuf.chksum[6] = '\0';
133 (void) dotarbuf(f, hb.dummy, sizeof(hb.dummy));
136 /****************************************************************************
137 Read a tar header into a hblock structure, and validate
138 ***************************************************************************/
139 static long readtarheader(union hblock *hb, file_info *finfo, char *prefix)
146 * read in a "standard" tar format header - we're not that interested
147 * in that many fields, though
150 /* check the checksum */
151 for (chk=0, i=sizeof(hb->dummy), jp=hb->dummy; --i>=0;) chk+=(0xFF & *jp++);
156 /* compensate for blanks in chksum header */
157 for (i=sizeof(hb->dbuf.chksum), jp=hb->dbuf.chksum; --i>=0;)
160 chk += ' ' * sizeof(hb->dbuf.chksum);
162 fchk=unoct(hb->dbuf.chksum, sizeof(hb->dbuf.chksum));
164 DEBUG(5, ("checksum totals chk=%d fchk=%d chksum=%s\n",
165 chk, fchk, hb->dbuf.chksum));
169 DEBUG(0, ("checksums don't match %d %d\n", fchk, chk));
173 strcpy(finfo->name, prefix);
175 /* use l + 1 to do the null too; do prefix - prefcnt to zap leading slash */
176 unfixtarname(finfo->name + strlen(prefix), hb->dbuf.name,
177 strlen(hb->dbuf.name) + 1);
179 /* can't handle links at present */
180 if (hb->dbuf.linkflag != '0') {
181 if (hb->dbuf.linkflag == 0) {
182 DEBUG(6, ("Warning: NULL link flag (gnu tar archive ?) %s\n",
185 DEBUG(0, ("this tar file appears to contain some kind of link - ignoring\n"));
190 if ((unoct(hb->dbuf.mode, sizeof(hb->dbuf.mode)) & S_IFDIR)
191 || (*(finfo->name+strlen(finfo->name)-1) == '\\'))
196 finfo->mode=0; /* we don't care about mode at the moment, we'll
197 * just make it a regular file */
199 * Bug fix by richard@sj.co.uk
201 * REC: restore times correctly (as does tar)
202 * We only get the modification time of the file; set the creation time
203 * from the mod. time, and the access time to current time
205 finfo->mtime = finfo->ctime = strtol(hb->dbuf.mtime, NULL, 8);
206 finfo->atime = time(NULL);
207 finfo->size = unoct(hb->dbuf.size, sizeof(hb->dbuf.size));
212 /****************************************************************************
213 Write out the tar buffer to tape or wherever
214 ****************************************************************************/
215 static int dotarbuf(int f, char *b, int n)
219 /* This routine and the next one should be the only ones that do write()s */
220 if (tp + n >= tbufsiz)
225 memcpy(tarbuf + tp, b, diff);
226 fail=fail && (1+write(f, tarbuf, tbufsiz));
233 fail=fail && (1 + write(f, b, tbufsiz));
239 memcpy(tarbuf+tp, b, n);
243 return(fail ? writ : 0);
246 /****************************************************************************
247 Write a zeros to buffer / tape
248 ****************************************************************************/
249 static void dozerobuf(int f, int n)
251 /* short routine just to write out n zeros to buffer -
252 * used to round files to nearest block
253 * and to do tar EOFs */
257 memset(tarbuf+tp, 0, tbufsiz-tp);
258 write(f, tarbuf, tbufsiz);
259 memset(tarbuf, 0, (tp+=n-tbufsiz));
263 memset(tarbuf+tp, 0, n);
268 /****************************************************************************
270 ****************************************************************************/
271 static void initarbuf()
273 /* initialize tar buffer */
274 tbufsiz=blocksize*TBLOCK;
275 tarbuf=malloc(tbufsiz);
277 /* reset tar buffer pointer and tar file counter */
281 /****************************************************************************
282 Write two zero blocks at end of file
283 ****************************************************************************/
284 static void dotareof(int f)
287 /* Two zero blocks at end of file, write out full buffer */
289 (void) dozerobuf(f, TBLOCK);
290 (void) dozerobuf(f, TBLOCK);
292 if (fstat(f, &stbuf) == -1)
294 DEBUG(0, ("Couldn't stat file handle\n"));
298 /* Could be a pipe, in which case S_ISREG should fail,
299 * and we should write out at full size */
300 if (tp > 0) write(f, tarbuf, S_ISREG(stbuf.st_mode) ? tp : tbufsiz);
303 /****************************************************************************
304 (Un)mangle DOS pathname, make nonabsolute
305 ****************************************************************************/
306 static void fixtarname(char *tptr, char *fp, int l)
308 /* add a '.' to start of file name, convert from ugly dos \'s in path
309 * to lovely unix /'s :-} */
314 if (is_shift_jis (*fp)) {
318 } else if (is_kana (*fp)) {
321 } else if (*fp == '\\') {
331 while (l--) { *tptr=(*fp == '\\') ? '/' : *fp; tptr++; fp++; }
335 /****************************************************************************
336 Convert from decimal to octal string
337 ****************************************************************************/
338 static void oct_it (register long value, register int ndgs, register char *p)
340 /* Converts long to octal string, pads with leading zeros */
342 /* skip final null, but do final space */
346 /* Loop does at least one digit */
348 p[--ndgs] = '0' + (char) (value & 7);
351 while (ndgs > 0 && value != 0);
353 /* Do leading zeros */
358 /****************************************************************************
359 Convert from octal string to long
360 ***************************************************************************/
361 static long unoct(char *p, int ndgs)
364 /* Converts octal string to long, ignoring any non-digit */
369 value = (value << 3) | (long) (*p - '0');
377 /****************************************************************************
378 Compare two strings in a slash insensitive way
379 ***************************************************************************/
380 int strslashcmp(const char *s1, const char *s2)
384 || tolower(*s1) == tolower(*s2)
385 || (*s1 == '\\' && *s2=='/')
386 || (*s1 == '/' && *s2=='\\'))) {
394 * general smb utility functions
396 /****************************************************************************
397 Set DOS file attributes
398 ***************************************************************************/
399 static int do_setrattr(char *fname, int attr, int setit)
402 * First get the existing attribs from existing file
413 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
414 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
416 if (!inbuf || !outbuf)
418 DEBUG(0,("out of memory\n"));
422 /* send an smb getatr message */
424 memset(outbuf,0,smb_size);
425 set_message(outbuf,0,2 + strlen(fname),True);
426 CVAL(outbuf,smb_com) = SMBgetatr;
427 SSVAL(outbuf,smb_tid,cnum);
433 p += (strlen(fname)+1);
438 send_smb(Client,outbuf);
439 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
441 if (CVAL(inbuf,smb_rcls) != 0)
442 DEBUG(5,("getatr: %s\n",smb_errstr(inbuf)));
445 DEBUG(5,("\nattr 0x%X time %d size %d\n",
446 (int)CVAL(inbuf,smb_vwv0),
447 SVAL(inbuf,smb_vwv1),
448 SVAL(inbuf,smb_vwv3)));
451 fattr=CVAL(inbuf,smb_vwv0);
453 /* combine found attributes with bits to be set or reset */
455 attr=setit ? (fattr | attr) : (fattr & ~attr);
457 /* now try and set attributes by sending smb reset message */
459 /* clear out buffer and start again */
460 memset(outbuf,0,smb_size);
461 set_message(outbuf,8,4 + strlen(fname),True);
462 CVAL(outbuf,smb_com) = SMBsetatr;
463 SSVAL(outbuf,smb_tid,cnum);
466 SSVAL(outbuf,smb_vwv0,attr);
471 p += (strlen(fname)+1);
476 send_smb(Client,outbuf);
477 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
479 if (CVAL(inbuf,smb_rcls) != 0)
481 DEBUG(0,("%s setting attributes on file %s\n",
482 smb_errstr(inbuf), fname));
483 free(inbuf);free(outbuf);
487 free(inbuf);free(outbuf);
491 /****************************************************************************
492 Create a file on a share
493 ***************************************************************************/
494 static BOOL smbcreat(file_info finfo, int *fnum, char *inbuf, char *outbuf)
497 /* *must* be called with buffer ready malloc'ed */
498 /* open remote file */
500 memset(outbuf,0,smb_size);
501 set_message(outbuf,3,2 + strlen(finfo.name),True);
502 CVAL(outbuf,smb_com) = SMBcreate;
503 SSVAL(outbuf,smb_tid,cnum);
506 SSVAL(outbuf,smb_vwv0,finfo.mode);
507 put_dos_date3(outbuf,smb_vwv1,finfo.mtime);
511 strcpy(p,finfo.name);
513 send_smb(Client,outbuf);
514 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
516 if (CVAL(inbuf,smb_rcls) != 0)
518 DEBUG(0,("%s opening remote file %s\n",smb_errstr(inbuf),
523 *fnum = SVAL(inbuf,smb_vwv0);
527 /****************************************************************************
528 Write a file to a share
529 ***************************************************************************/
530 static BOOL smbwrite(int fnum, int n, int low, int high, int left,
531 char *bufferp, char *inbuf, char *outbuf)
533 /* *must* be called with buffer ready malloc'ed */
535 memset(outbuf,0,smb_size);
536 set_message(outbuf,5,n + 3,True);
538 memcpy(smb_buf(outbuf)+3, bufferp, n);
540 set_message(outbuf,5,n + 3, False);
541 CVAL(outbuf,smb_com) = SMBwrite;
542 SSVAL(outbuf,smb_tid,cnum);
545 SSVAL(outbuf,smb_vwv0,fnum);
546 SSVAL(outbuf,smb_vwv1,n);
547 SIVAL(outbuf,smb_vwv2,low);
548 SSVAL(outbuf,smb_vwv4,left);
549 CVAL(smb_buf(outbuf),0) = 1;
550 SSVAL(smb_buf(outbuf),1,n);
552 send_smb(Client,outbuf);
553 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
555 if (CVAL(inbuf,smb_rcls) != 0)
557 DEBUG(0,("%s writing remote file\n",smb_errstr(inbuf)));
561 if (n != SVAL(inbuf,smb_vwv0))
563 DEBUG(0,("Error: only wrote %d bytes out of %d\n",
564 SVAL(inbuf,smb_vwv0), n));
571 /****************************************************************************
572 Close a file on a share
573 ***************************************************************************/
574 static BOOL smbshut(file_info finfo, int fnum, char *inbuf, char *outbuf)
576 /* *must* be called with buffer ready malloc'ed */
578 memset(outbuf,0,smb_size);
579 set_message(outbuf,3,0,True);
580 CVAL(outbuf,smb_com) = SMBclose;
581 SSVAL(outbuf,smb_tid,cnum);
584 SSVAL(outbuf,smb_vwv0,fnum);
585 put_dos_date3(outbuf,smb_vwv1,finfo.mtime);
587 DEBUG(3,("Setting date to %s (0x%X)",
588 asctime(LocalTime(&finfo.mtime)),
591 send_smb(Client,outbuf);
592 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
594 if (CVAL(inbuf,smb_rcls) != 0)
596 DEBUG(0,("%s closing remote file %s\n",smb_errstr(inbuf),
604 /****************************************************************************
605 Verify existence of path on share
606 ***************************************************************************/
607 static BOOL smbchkpath(char *fname, char *inbuf, char *outbuf)
611 memset(outbuf,0,smb_size);
612 set_message(outbuf,0,4 + strlen(fname),True);
613 CVAL(outbuf,smb_com) = SMBchkpth;
614 SSVAL(outbuf,smb_tid,cnum);
621 send_smb(Client,outbuf);
622 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
624 DEBUG(5,("smbchkpath: %s\n",smb_errstr(inbuf)));
626 return(CVAL(inbuf,smb_rcls) == 0);
629 /****************************************************************************
630 Make a directory on share
631 ***************************************************************************/
632 static BOOL smbmkdir(char *fname, char *inbuf, char *outbuf)
634 /* *must* be called with buffer ready malloc'ed */
637 memset(outbuf,0,smb_size);
638 set_message(outbuf,0,2 + strlen(fname),True);
640 CVAL(outbuf,smb_com) = SMBmkdir;
641 SSVAL(outbuf,smb_tid,cnum);
648 send_smb(Client,outbuf);
649 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
651 if (CVAL(inbuf,smb_rcls) != 0)
653 DEBUG(0,("%s making remote directory %s\n",
654 smb_errstr(inbuf),fname));
661 /****************************************************************************
662 Ensure a remote path exists (make if necessary)
663 ***************************************************************************/
664 static BOOL ensurepath(char *fname, char *inbuf, char *outbuf)
666 /* *must* be called with buffer ready malloc'ed */
667 /* ensures path exists */
669 pstring partpath, ffname;
670 char *p=fname, *basehack;
674 /* fname copied to ffname so can strtok */
676 strcpy(ffname, fname);
678 /* do a `basename' on ffname, so don't try and make file name directory */
679 if ((basehack=strrchr(ffname, '\\')) == NULL)
684 p=strtok(ffname, "\\");
690 if (!smbchkpath(partpath, inbuf, outbuf)) {
691 if (!smbmkdir(partpath, inbuf, outbuf))
693 DEBUG(0, ("Error mkdirhiering\n"));
697 DEBUG(3, ("mkdirhiering %s\n", partpath));
701 strcat(partpath, "\\");
702 p = strtok(NULL,"/\\");
709 * smbclient functions
711 /****************************************************************************
712 append one remote file to the tar file
713 ***************************************************************************/
714 static void do_atar(char *rname,char *lname,file_info *finfo1)
721 BOOL close_done = False;
722 BOOL shallitime=True;
723 BOOL ignore_close_error = False;
727 struct timeval tp_start;
728 GetTimeOfDay(&tp_start);
735 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
736 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
738 if (!inbuf || !outbuf)
740 DEBUG(0,("out of memory\n"));
744 memset(outbuf,0,smb_size);
745 set_message(outbuf,15,1 + strlen(rname),True);
747 CVAL(outbuf,smb_com) = SMBopenX;
748 SSVAL(outbuf,smb_tid,cnum);
751 SSVAL(outbuf,smb_vwv0,0xFF);
752 SSVAL(outbuf,smb_vwv2,1);
753 SSVAL(outbuf,smb_vwv3,(DENY_NONE<<4));
754 SSVAL(outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
755 SSVAL(outbuf,smb_vwv5,aSYSTEM | aHIDDEN);
756 SSVAL(outbuf,smb_vwv8,1);
760 p = skip_string(p,1);
762 dos_clean_name(rname);
764 /* do a chained openX with a readX? */
767 SSVAL(outbuf,smb_vwv0,SMBreadX);
768 SSVAL(outbuf,smb_vwv1,PTR_DIFF(p,outbuf) - 4);
772 SSVAL(p,smb_vwv0,0xFF);
773 SSVAL(p,smb_vwv5,MIN(max_xmit-500,finfo.size));
774 SSVAL(p,smb_vwv9,MIN(0xFFFF,finfo.size));
775 smb_setlen(outbuf,smb_len(outbuf)+11*2+1);
778 send_smb(Client,outbuf);
779 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
781 if (CVAL(inbuf,smb_rcls) != 0)
783 if (CVAL(inbuf,smb_rcls) == ERRSRV &&
784 SVAL(inbuf,smb_err) == ERRnoresource &&
785 reopen_connection(inbuf,outbuf))
787 do_atar(rname,lname,finfo1);
788 free(inbuf);free(outbuf);
792 DEBUG(0,("%s opening remote file %s\n",smb_errstr(inbuf),rname));
793 free(inbuf);free(outbuf);
797 strcpy(finfo.name,rname);
800 finfo.mode = SVAL(inbuf,smb_vwv3);
801 finfo.size = IVAL(inbuf,smb_vwv4);
802 finfo.mtime = make_unix_date3(inbuf+smb_vwv6);
803 finfo.atime = finfo.ctime = finfo.mtime;
806 DEBUG(3,("file %s attrib 0x%X\n",finfo.name,finfo.mode));
808 fnum = SVAL(inbuf,smb_vwv2);
810 if (tar_inc && !(finfo.mode & aARCH))
812 DEBUG(4, ("skipping %s - archive bit not set\n", finfo.name));
817 if (SVAL(inbuf,smb_vwv0) == SMBreadX)
819 p = (inbuf+4+SVAL(inbuf,smb_vwv1)) - smb_wct;
820 datalen = SVAL(p,smb_vwv5);
821 dataptr = inbuf + 4 + SVAL(p,smb_vwv6);
829 DEBUG(2,("getting file %s of size %d bytes as a tar file %s",
834 /* write a tar header, don't bother with mode - just set to 100644 */
835 writetarheader(tarhandle, rname, finfo.size, finfo.mtime, "100644 \0");
837 while (nread < finfo.size && !close_done)
840 static BOOL can_chain_close=True;
844 DEBUG(3,("nread=%d\n",nread));
846 /* 3 possible read types. readbraw if a large block is required.
847 readX + close if not much left and read if neither is supported */
849 /* we might have already read some data from a chained readX */
850 if (dataptr && datalen>0)
853 /* if we can finish now then readX+close */
854 if (method<0 && can_chain_close && (Protocol >= PROTOCOL_LANMAN1) &&
855 ((finfo.size - nread) <
856 (max_xmit - (2*smb_size + 13*SIZEOFWORD + 300))))
859 /* if we support readraw then use that */
860 if (method<0 && readbraw_supported)
863 /* if we can then use readX */
864 if (method<0 && (Protocol >= PROTOCOL_LANMAN1))
876 /* use readX + close */
877 memset(outbuf,0,smb_size);
878 set_message(outbuf,10,0,True);
879 CVAL(outbuf,smb_com) = SMBreadX;
880 SSVAL(outbuf,smb_tid,cnum);
885 CVAL(outbuf,smb_vwv0) = SMBclose;
886 SSVAL(outbuf,smb_vwv1,PTR_DIFF(smb_buf(outbuf),outbuf) - 4);
889 CVAL(outbuf,smb_vwv0) = 0xFF;
892 SSVAL(outbuf,smb_vwv2,fnum);
893 SIVAL(outbuf,smb_vwv3,nread);
894 SSVAL(outbuf,smb_vwv5,MIN(max_xmit-200,finfo.size - nread));
895 SSVAL(outbuf,smb_vwv6,0);
896 SIVAL(outbuf,smb_vwv7,0);
897 SSVAL(outbuf,smb_vwv9,MIN(0xFFFF,finfo.size-nread));
908 /* now set the total packet length */
909 smb_setlen(outbuf,smb_len(outbuf)+9);
912 send_smb(Client,outbuf);
913 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
915 if (CVAL(inbuf,smb_rcls) != 0)
917 DEBUG(0,("Error %s reading remote file\n",smb_errstr(inbuf)));
922 SVAL(inbuf,smb_vwv0) != SMBclose)
924 /* NOTE: WfWg sometimes just ignores the chained
925 command! This seems to break the spec? */
926 DEBUG(3,("Rejected chained close?\n"));
928 can_chain_close = False;
929 ignore_close_error = True;
932 datalen = SVAL(inbuf,smb_vwv5);
933 dataptr = inbuf + 4 + SVAL(inbuf,smb_vwv6);
940 static int readbraw_size = 0xFFFF;
943 memset(outbuf,0,smb_size);
944 set_message(outbuf,8,0,True);
945 CVAL(outbuf,smb_com) = SMBreadbraw;
946 SSVAL(outbuf,smb_tid,cnum);
948 SSVAL(outbuf,smb_vwv0,fnum);
949 SIVAL(outbuf,smb_vwv1,nread);
950 SSVAL(outbuf,smb_vwv3,MIN(finfo.size-nread,readbraw_size));
951 SSVAL(outbuf,smb_vwv4,0);
952 SIVALS(outbuf,smb_vwv5,-1);
953 send_smb(Client,outbuf);
955 /* Now read the raw data into the buffer and write it */
956 if(read_smb_length(Client,inbuf,0) == -1) {
957 DEBUG(0,("Failed to read length in readbraw\n"));
961 /* Even though this is not an smb message, smb_len
962 returns the generic length of an smb message */
963 datalen = smb_len(inbuf);
967 /* we got a readbraw error */
968 DEBUG(4,("readbraw error - reducing size\n"));
969 readbraw_size = (readbraw_size * 9) / 10;
971 if (readbraw_size < max_xmit)
973 DEBUG(0,("disabling readbraw\n"));
974 readbraw_supported = False;
981 if(read_data(Client,inbuf,datalen) != datalen) {
982 DEBUG(0,("Failed to read data in readbraw\n"));
990 /* we've already read some data with a chained readX */
995 memset(outbuf,0,smb_size);
996 set_message(outbuf,5,0,True);
997 CVAL(outbuf,smb_com) = SMBread;
998 SSVAL(outbuf,smb_tid,cnum);
1001 SSVAL(outbuf,smb_vwv0,fnum);
1002 SSVAL(outbuf,smb_vwv1,MIN(max_xmit-200,finfo.size - nread));
1003 SIVAL(outbuf,smb_vwv2,nread);
1004 SSVAL(outbuf,smb_vwv4,finfo.size - nread);
1006 send_smb(Client,outbuf);
1007 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
1009 if (CVAL(inbuf,smb_rcls) != 0)
1011 DEBUG(0,("Error %s reading remote file\n",smb_errstr(inbuf)));
1015 datalen = SVAL(inbuf,smb_vwv0);
1016 dataptr = smb_buf(inbuf) + 3;
1021 /* add received bits of file to buffer - dotarbuf will
1022 * write out in 512 byte intervals */
1023 if (dotarbuf(tarhandle,dataptr,datalen) != datalen)
1025 DEBUG(0,("Error writing local file\n"));
1032 DEBUG(0,("Error reading file %s. Got 0 bytes\n", rname));
1040 /* round tar file to nearest block */
1041 if (finfo.size % TBLOCK)
1042 dozerobuf(tarhandle, TBLOCK - (finfo.size % TBLOCK));
1049 memset(outbuf,0,smb_size);
1050 set_message(outbuf,3,0,True);
1051 CVAL(outbuf,smb_com) = SMBclose;
1052 SSVAL(outbuf,smb_tid,cnum);
1055 SSVAL(outbuf,smb_vwv0,fnum);
1056 SIVALS(outbuf,smb_vwv1,-1);
1058 send_smb(Client,outbuf);
1059 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
1061 if (!ignore_close_error && CVAL(inbuf,smb_rcls) != 0)
1063 DEBUG(0,("Error %s closing remote file\n",smb_errstr(inbuf)));
1064 free(inbuf);free(outbuf);
1071 struct timeval tp_end;
1074 /* if shallitime is true then we didn't skip */
1075 if (tar_reset) (void) do_setrattr(finfo.name, aARCH, ATTRRESET);
1077 GetTimeOfDay(&tp_end);
1079 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1080 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1081 get_total_time_ms += this_time;
1082 get_total_size += finfo.size;
1084 /* Thanks to Carel-Jan Engel (ease@mail.wirehub.nl) for this one */
1085 DEBUG(2,("(%g kb/s) (average %g kb/s)\n",
1086 finfo.size / MAX(0.001, (1.024*this_time)),
1087 get_total_size / MAX(0.001, (1.024*get_total_time_ms))));
1090 free(inbuf);free(outbuf);
1093 /****************************************************************************
1094 Append single file to tar file (or not)
1095 ***************************************************************************/
1096 static void do_tar(file_info *finfo)
1100 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1103 /* Is it on the exclude list ? */
1104 if (!tar_excl && clipn) {
1107 strcpy(exclaim, cur_dir);
1108 *(exclaim+strlen(exclaim)-1)='\0';
1110 if (clipfind(cliplist, clipn, exclaim)) {
1111 DEBUG(3,("Skipping directory %s\n", exclaim));
1115 strcat(exclaim, "\\");
1116 strcat(exclaim, finfo->name);
1118 if (clipfind(cliplist, clipn, exclaim)) {
1119 DEBUG(3,("Skipping file %s\n", exclaim));
1124 if (finfo->mode & aDIR)
1126 pstring saved_curdir;
1128 char *inbuf,*outbuf;
1130 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
1131 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
1133 if (!inbuf || !outbuf)
1135 DEBUG(0,("out of memory\n"));
1139 strcpy(saved_curdir,cur_dir);
1141 strcat(cur_dir,finfo->name);
1142 strcat(cur_dir,"\\");
1144 /* write a tar directory, don't bother with mode - just set it to
1146 writetarheader(tarhandle, cur_dir, 0, finfo->mtime, "040755 \0");
1147 strcpy(mtar_mask,cur_dir);
1148 strcat(mtar_mask,"*");
1150 do_dir((char *)inbuf,(char *)outbuf,mtar_mask,attribute,do_tar,recurse);
1151 strcpy(cur_dir,saved_curdir);
1152 free(inbuf);free(outbuf);
1156 strcpy(rname,cur_dir);
1157 strcat(rname,finfo->name);
1158 do_atar(rname,finfo->name,finfo);
1162 /****************************************************************************
1163 Convert from UNIX to DOS file names
1164 ***************************************************************************/
1165 static void unfixtarname(char *tptr, char *fp, int l)
1167 /* remove '.' from start of file name, convert from unix /'s to
1168 * dos \'s in path. Kill any absolute path names.
1171 if (*fp == '.') fp++;
1172 if (*fp == '\\' || *fp == '/') fp++;
1176 if (is_shift_jis (*fp)) {
1180 } else if (is_kana (*fp)) {
1183 } else if (*fp == '/') {
1193 while (l--) { *tptr=(*fp == '/') ? '\\' : *fp; tptr++; fp++; }
1197 static void do_tarput()
1200 int nread=0, bufread;
1201 char *inbuf,*outbuf;
1204 struct timeval tp_start;
1205 BOOL tskip=False; /* We'll take each file as it comes */
1207 GetTimeOfDay(&tp_start);
1209 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
1210 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
1212 if (!inbuf || !outbuf)
1214 DEBUG(0,("out of memory\n"));
1219 * Must read in tbufsiz dollops
1222 /* These should be the only reads in clitar.c */
1223 while ((bufread=read(tarhandle, tarbuf, tbufsiz))>0) {
1224 char *bufferp, *endofbuffer;
1227 /* Code to handle a short read.
1228 * We always need a TBLOCK full of stuff
1230 if (bufread % TBLOCK) {
1231 int lchunk=TBLOCK-(bufread % TBLOCK);
1234 /* It's a shorty - a short read that is */
1235 DEBUG(3, ("Short read, read %d so far (need %d)\n", bufread, lchunk));
1237 while ((lread=read(tarhandle, tarbuf+bufread, lchunk))>0) {
1239 if (!(lchunk-=lread)) break;
1242 /* If we've reached EOF then that must be a short file */
1243 if (lread<=0) break;
1247 endofbuffer=tarbuf+bufread;
1250 if (fsize<bufread) {
1255 if (fsize==bufread) tskip=False;
1264 switch (readtarheader((union hblock *) bufferp, &finfo, cur_dir))
1266 case -2: /* something dodgy but not fatal about this */
1267 DEBUG(0, ("skipping %s...\n", finfo.name));
1268 bufferp+=TBLOCK; /* header - like a link */
1271 DEBUG(0, ("abandoning restore\n"));
1272 free(inbuf); free(outbuf);
1274 case 0: /* chksum is zero - we assume that one all zero
1275 *header block will do for eof */
1277 ("total of %d tar files restored to share\n", ntarf));
1278 free(inbuf); free(outbuf);
1285 && (clipfind(cliplist, clipn, finfo.name) ^ tar_excl);
1288 if (finfo.mode & aDIR)
1290 else if ((fsize=finfo.size) % TBLOCK) {
1291 fsize+=TBLOCK-(fsize%TBLOCK);
1293 if (fsize<endofbuffer-bufferp) {
1298 fsize-=endofbuffer-bufferp;
1303 if (finfo.mode & aDIR)
1305 if (!smbchkpath(finfo.name, inbuf, outbuf)
1306 && !smbmkdir(finfo.name, inbuf, outbuf))
1308 DEBUG(0, ("abandoning restore\n"));
1309 free(inbuf); free(outbuf);
1321 if (ensurepath(finfo.name, inbuf, outbuf)
1322 && !smbcreat(finfo, &fnum, inbuf, outbuf))
1324 DEBUG(0, ("abandoning restore\n"));
1325 free(inbuf);free(outbuf);
1329 DEBUG(0,("restore tar file %s of size %d bytes\n",
1330 finfo.name,finfo.size));
1333 if ((bufferp+=TBLOCK) >= endofbuffer) break;
1336 /* write out the file in chunk sized chunks - don't
1337 * go past end of buffer though */
1338 chunk=(fsize-nread < endofbuffer - bufferp)
1339 ? fsize - nread : endofbuffer - bufferp;
1342 int minichunk=MIN(chunk, max_xmit-200);
1344 if (!smbwrite(fnum, /* file descriptor */
1346 nread, /* offset low */
1347 0, /* offset high - not implemented */
1348 fsize-nread, /* left - only hint to server */
1353 DEBUG(0, ("Error writing remote file\n"));
1354 free(inbuf); free(outbuf);
1357 DEBUG(5, ("chunk writing fname=%s fnum=%d nread=%d minichunk=%d chunk=%d size=%d\n", finfo.name, fnum, nread, minichunk, chunk, fsize));
1359 bufferp+=minichunk; nread+=minichunk;
1365 if (!smbshut(finfo, fnum, inbuf, outbuf))
1367 DEBUG(0, ("Error closing remote file\n"));
1368 free(inbuf);free(outbuf);
1371 if (fsize % TBLOCK) bufferp+=TBLOCK - (fsize % TBLOCK);
1372 DEBUG(5, ("bufferp is now %d (psn=%d)\n",
1373 (long) bufferp, (long)(bufferp - tarbuf)));
1377 } while (bufferp < endofbuffer);
1380 DEBUG(0, ("premature eof on tar file ?\n"));
1381 DEBUG(0,("total of %d tar files restored to share\n", ntarf));
1383 free(inbuf); free(outbuf);
1387 * samba interactive commands
1390 /****************************************************************************
1392 ***************************************************************************/
1393 void cmd_block(void)
1398 if (!next_token(NULL,buf,NULL))
1400 DEBUG(0, ("blocksize <n>\n"));
1405 if (block < 0 || block > 65535)
1407 DEBUG(0, ("blocksize out of range"));
1412 DEBUG(2,("blocksize is now %d\n", blocksize));
1415 /****************************************************************************
1416 command to set incremental / reset mode
1417 ***************************************************************************/
1418 void cmd_tarmode(void)
1422 while (next_token(NULL,buf,NULL)) {
1423 if (strequal(buf, "full"))
1425 else if (strequal(buf, "inc"))
1427 else if (strequal(buf, "reset"))
1429 else if (strequal(buf, "noreset"))
1431 else DEBUG(0, ("tarmode: unrecognised option %s\n", buf));
1434 DEBUG(0, ("tarmode is now %s, %s\n",
1435 tar_inc ? "incremental" : "full",
1436 tar_reset ? "reset" : "noreset"));
1439 /****************************************************************************
1440 Feeble attrib command
1441 ***************************************************************************/
1442 void cmd_setmode(void)
1450 attra[0] = attra[1] = 0;
1452 if (!next_token(NULL,buf,NULL))
1454 DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
1458 strcpy(fname, cur_dir);
1461 while (next_token(NULL,buf,NULL)) {
1470 case 'r': attra[direct]|=aRONLY;
1472 case 'h': attra[direct]|=aHIDDEN;
1474 case 's': attra[direct]|=aSYSTEM;
1476 case 'a': attra[direct]|=aARCH;
1478 default: DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
1483 if (attra[ATTRSET]==0 && attra[ATTRRESET]==0)
1485 DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
1489 DEBUG(2, ("\nperm set %d %d\n", attra[ATTRSET], attra[ATTRRESET]));
1490 (void) do_setrattr(fname, attra[ATTRSET], ATTRSET);
1491 (void) do_setrattr(fname, attra[ATTRRESET], ATTRRESET);
1494 /****************************************************************************
1495 Principal command for creating / extracting
1496 ***************************************************************************/
1497 void cmd_tar(char *inbuf, char *outbuf)
1503 if (!next_token(NULL,buf,NULL))
1505 DEBUG(0,("tar <c|x>[IXbga] <filename>\n"));
1509 argl=toktocliplist(&argcl, NULL);
1510 if (!tar_parseargs(argcl, argl, buf, 0))
1513 process_tar(inbuf, outbuf);
1518 /****************************************************************************
1519 Command line (option) version
1520 ***************************************************************************/
1521 int process_tar(char *inbuf, char *outbuf)
1532 if (clipn && tar_excl) {
1536 for (i=0; i<clipn; i++) {
1537 DEBUG(0,("arg %d = %s\n", i, cliplist[i]));
1539 if (*(cliplist[i]+strlen(cliplist[i])-1)=='\\') {
1540 *(cliplist[i]+strlen(cliplist[i])-1)='\0';
1543 if (strrchr(cliplist[i], '\\')) {
1546 strcpy(saved_dir, cur_dir);
1548 if (*cliplist[i]=='\\') {
1549 strcpy(tarmac, cliplist[i]);
1551 strcpy(tarmac, cur_dir);
1552 strcat(tarmac, cliplist[i]);
1554 strcpy(cur_dir, tarmac);
1555 *(strrchr(cur_dir, '\\')+1)='\0';
1557 do_dir((char *)inbuf,(char *)outbuf,tarmac,attribute,do_tar,recurse);
1558 strcpy(cur_dir,saved_dir);
1560 strcpy(tarmac, cur_dir);
1561 strcat(tarmac, cliplist[i]);
1562 do_dir((char *)inbuf,(char *)outbuf,tarmac,attribute,do_tar,recurse);
1567 strcpy(mask,cur_dir);
1569 do_dir((char *)inbuf,(char *)outbuf,mask,attribute,do_tar,recurse);
1572 if (ntarf) dotareof(tarhandle);
1576 DEBUG(0, ("tar: dumped %d tar files\n", ntarf));
1583 /****************************************************************************
1584 Find a token (filename) in a clip list
1585 ***************************************************************************/
1586 int clipfind(char **aret, int ret, char *tok)
1588 if (aret==NULL) return 0;
1590 /* ignore leading slashes or dots in token */
1591 while(strchr("/\\.", *tok)) tok++;
1596 /* ignore leading slashes or dots in list */
1597 while(strchr("/\\.", *pkey)) pkey++;
1599 if (!strslashcmp(pkey, tok)) return 1;
1605 /****************************************************************************
1606 Parse tar arguments. Sets tar_type, tar_excl, etc.
1607 ***************************************************************************/
1608 int tar_parseargs(int argc, char *argv[], char *Optarg, int Optind)
1610 char tar_clipfl='\0';
1612 /* Reset back to defaults - could be from interactive version
1613 * reset mode and archive mode left as they are though
1624 if (tar_type=='c') {
1625 printf("Tar must be followed by only one of c or x.\n");
1631 if (Optind>=argc || !(blocksize=atoi(argv[Optind]))) {
1632 DEBUG(0,("Option b must be followed by valid blocksize\n"));
1643 DEBUG(0,("Option N must be followed by valid file name\n"));
1647 extern time_t newer_than;
1649 if (sys_stat(argv[Optind], &stbuf) == 0) {
1650 newer_than = stbuf.st_mtime;
1651 DEBUG(1,("Getting files newer than %s",
1652 asctime(LocalTime(&newer_than))));
1655 DEBUG(0,("Error setting newer-than time\n"));
1665 DEBUG(0,("Only one of I,X must be specified\n"));
1672 DEBUG(0,("Only one of I,X must be specified\n"));
1678 DEBUG(0,("Unknown tar option\n"));
1683 printf("Option T must be followed by one of c or x.\n");
1687 if (Optind>=argc || !strcmp(argv[Optind], "-")) {
1688 /* Sets tar handle to either 0 or 1, as appropriate */
1689 tarhandle=(tar_type=='c');
1691 tar_excl=tar_clipfl!='X';
1693 if (Optind+1<argc) {
1694 cliplist=argv+Optind+1;
1695 clipn=argc-Optind-1;
1698 if ((tar_type=='x' && (tarhandle = open(argv[Optind], O_RDONLY)) == -1)
1699 || (tar_type=='c' && (tarhandle=creat(argv[Optind], 0644)) < 0))
1701 DEBUG(0,("Error opening local file %s\n",argv[Optind]));