Example #1
0
		public void SendToken(IOStream f, int token, MapFile buf, int offset, int n, int toklen)
		{			
			if (!options.doCompression) 
				SimpleSendToken(f,token,buf,offset,n);
			else 
				SendDeflatedToken(f, token, buf, offset, n, toklen);
		}
Example #2
0
		public int ReceiveToken(IOStream f, ref byte[] data, int offset)
		{
			int tok;
			if (!options.doCompression) 
				tok = SimpleReceiveToken(f, ref data, offset);
			else 
				tok = ReceiveDeflatedToken(f, data, offset);
			return tok;
		}
Example #3
0
		public void WriteSumHead(IOStream f, SumStruct sum)
		{
			if(sum == null)
				sum = new SumStruct();
			f.writeInt(sum.count);
			f.writeInt((int)sum.bLength);
			if (options.protocolVersion >= 27)
				f.writeInt(sum.s2Length);
			f.writeInt((int)sum.remainder);
		} 
Example #4
0
 public int ReceiveToken(IOStream ioStream, ref byte[] data, int offset)
 {
     int token;
     if (!options.doCompression)
     {
         token = SimpleReceiveToken(ioStream, ref data, offset);
     }
     else
     {
         token = ReceiveDeflatedToken(ioStream, data, offset);
     }
     return token;
 }
Example #5
0
		public void GenerateFiles(IOStream f, ArrayList fileList, string localName)
		{
			int i;
			int phase = 0;
			

			if (options.verbose > 2) 
				Log.WriteLine("generator starting count=" + fileList.Count);

			for (i = 0; i < fileList.Count; i++) 
			{
				FileStruct file = ((FileStruct)fileList[i]);			 
				if (file.baseName == null)
					continue;
				if(Util.S_ISDIR(file.mode))
				 		continue;
				ReceiveGenerator(localName != null ? localName : file.FNameTo() ,file, i, f);
			}

			phase++;
			checkSum.cSumLength = CheckSum.SUM_LENGTH;
			if (options.verbose > 2)
				Log.WriteLine("GenerateFiles phase=" + phase);
			f.writeInt(-1);

			phase++;
			if (options.verbose > 2)
				Log.WriteLine("GenerateFiles phase=" + phase);

			f.writeInt(-1);

			if (options.protocolVersion >= 29 && !options.delayUpdates)
				f.writeInt(-1);

			/* now we need to fix any directory permissions that were
			* modified during the transfer 
			* */
			for (i = 0; i < fileList.Count; i++) 
			{
				FileStruct file = ((FileStruct)fileList[i]);
				if (file.baseName != null || Util.S_ISDIR(file.mode))
					continue;
				ReceiveGenerator(localName != null ? localName : file.FNameTo() ,file, i, null);
			}

			if (options.verbose > 2)
				Log.WriteLine("GenerateFiles finished");
		} 
Example #6
0
		public int SimpleReceiveToken(IOStream f, ref byte[] data, int offset)
		{
			int n;
			if (residue == 0) 
			{
				int i = f.readInt();
				if (i <= 0)
					return i;
				residue = i;
			}

			n = Math.Min(Match.CHUNK_SIZE,residue);
			residue -= n;
			data = f.ReadBuf(n);			
			return n; 
		}
Example #7
0
        public void HashSearch(IOStream f, SumStruct s, MapFile buf, int len, Sum _sum)
        {
            int    offset, end, backup;
            UInt32 k;
            int    wantI;

            byte[] sum2 = new byte[CheckSum.SUM_LENGTH];
            UInt32 s1, s2, sum;
            int    more;

            byte[] map;

            wantI = 0;
            if (options.verbose > 2)
            {
                Log.WriteLine("hash search ob=" + s.bLength + " len=" + len);
            }

            k = (UInt32)Math.Min(len, s.bLength);
            int off = buf.MapPtr(0, (int)k);

            map = buf.p;

            UInt32 g = s.sums[0].sum1;

            sum = CheckSum.GetChecksum1(map, off, (int)k);
            s1  = sum & 0xFFFF;
            s2  = sum >> 16;
            if (options.verbose > 3)
            {
                Log.WriteLine("sum=" + sum + " k=" + k);
            }

            offset = 0;
            end    = (int)(len + 1 - s.sums[s.count - 1].len);
            if (options.verbose > 3)
            {
                Log.WriteLine("hash search s.bLength=" + s.bLength + " len=" + len + " count=" + s.count);
            }

            do
            {
                UInt32 t         = GetTag2(s1, s2);
                bool   doneCsum2 = false;
                int    j         = tagTable[t];

                if (options.verbose > 4)
                {
                    Log.WriteLine("offset=" + offset + " sum=" + sum);
                }

                if (j == NULL_TAG)
                {
                    goto null_tag;
                }

                sum = (s1 & 0xffff) | (s2 << 16);
                tagHits++;
                do
                {
                    UInt32 l;
                    int    i = ((Target)targets[j]).i;

                    if (sum != s.sums[i].sum1)
                    {
                        continue;
                    }

                    l = (UInt32)Math.Min(s.bLength, len - offset);
                    if (l != s.sums[i].len)
                    {
                        continue;
                    }

                    if (options.verbose > 3)
                    {
                        Log.WriteLine("potential match at " + offset + " target=" + j + " " + i + " sum=" + sum);
                    }

                    if (!doneCsum2)
                    {
                        off = buf.MapPtr(offset, (int)l);
                        map = buf.p;
                        CheckSum cs = new CheckSum(options);
                        sum2      = cs.GetChecksum2(map, off, (int)l);
                        doneCsum2 = true;
                    }

                    if (Util.MemoryCompare(sum2, 0, s.sums[i].sum2, 0, s.s2Length) != 0)
                    {
                        falseAlarms++;
                        continue;
                    }

                    if (i != wantI && wantI < s.count &&
                        (!options.inplace || options.makeBackups || s.sums[wantI].offset >= offset ||
                         (s.sums[wantI].flags & SUMFLG_SAME_OFFSET) != 0) &&
                        sum == s.sums[wantI].sum1 &&
                        Util.MemoryCompare(sum2, 0, s.sums[wantI].sum2, 0, s.s2Length) == 0)
                    {
                        i = wantI;
                    }
                    //set_want_i:
                    wantI = i + 1;

                    Matched(f, s, buf, offset, i, _sum);
                    offset += (int)(s.sums[i].len - 1);
                    k       = (UInt32)Math.Min(s.bLength, len - offset);
                    off     = buf.MapPtr(offset, (int)k);
                    sum     = CheckSum.GetChecksum1(map, off, (int)k);
                    s1      = sum & 0xFFFF;
                    s2      = sum >> 16;
                    matches++;
                    break;
                } while (++j < s.count && ((Target)targets[j]).t == t);
null_tag:
                backup = offset - lastMatch;
                if (backup < 0)
                {
                    backup = 0;
                }

                more = (offset + k) < len ? 1 : 0;
                off  = buf.MapPtr(offset - backup, (int)(k + more + backup)) + backup;
                s1  -= (UInt32)(CheckSum.ToInt(map[off]) + CheckSum.CHAR_OFFSET);
                s2  -= (UInt32)(k * CheckSum.ToInt(map[off]) + CheckSum.CHAR_OFFSET);
                off  = (k + off >= map.Length) ? (int)(map.Length - k - 1) : off;
                if (more != 0)
                {
                    s1 += (UInt32)(CheckSum.ToInt(map[k + off]) + CheckSum.CHAR_OFFSET);
                    s2 += s1;
                }
                else
                {
                    --k;
                }

                if (backup >= CHUNK_SIZE + s.bLength && end - offset > CHUNK_SIZE)
                {
                    Matched(f, s, buf, (int)(offset - s.bLength), -2, _sum);
                }
            } while (++offset < end);

            Matched(f, s, buf, len, -1, _sum);
            buf.MapPtr(len - 1, 1);
        }
Example #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="clientInfo"></param>
        /// <param name="moduleNumber"></param>
        /// <returns></returns>
        public static void RsyncModule(ClientInfo clientInfo, int moduleNumber) //@fixed Why return something if result not used?
        {
            string   path     = Daemon.config.GetModule(moduleNumber).Path;
            string   name     = Daemon.config.GetModuleName(moduleNumber);
            IOStream ioStream = clientInfo.IoStream;
            Options  options  = clientInfo.Options;

            string[] args = new string[Options.MAX_ARGS];
            int      argc = 0, maxArgs = Options.MAX_ARGS;
            string   line = String.Empty;

            if (path[0] == '/')
            {
                path = path.Remove(0, 1);
            }
            path = path.Replace("\n", String.Empty);

            Access ac = new Access();

            if (!ac.AllowAccess(options.remoteAddr, options.remoteHost, Daemon.config.GetHostsAllow(moduleNumber), Daemon.config.GetHostsDeny(moduleNumber)))
            {
                Log.Write("rsync denied on module " + name + " from " + options.remoteHost + " (" + options.remoteAddr + ")");
                ioStream.IOPrintf("@ERROR: access denied to " + name + " from " + options.remoteHost + " (" + options.remoteAddr + ")\n");
                return;
            }

            if (!Authentication.AuthorizeServer(clientInfo, moduleNumber, options.remoteAddr, "@RSYNCD: AUTHREQD "))
            {
                Log.Write("auth failed on module " + name + " from " + options.remoteHost + " (" + options.remoteAddr + ")\n");
                ioStream.IOPrintf("@ERROR: auth failed on module " + name + "\n");
                return;
            }
            // TODO: path length
            if (Directory.Exists(path))
            {
                ioStream.IOPrintf("@RSYNCD: OK\n");
            }
            else
            {
                try
                {
                    // TODO: path length
                    Directory.CreateDirectory(path);
                    ioStream.IOPrintf("@RSYNCD: OK\n");
                }
                catch (Exception)
                {
                    ioStream.IOPrintf("@ERROR: Path not found\n");
                    MainClass.Exit("@ERROR: Path not found: " + path, clientInfo);
                }
            }
            options.amServer = true;    //to fix error in SetupProtocol
            options.dir      = path;

            while (true)
            {
                line = ioStream.ReadLine();
                line = line.Substring(0, line.Length - 1);
                if (line.CompareTo(String.Empty) == 0)
                {
                    break;
                }
                if (argc == maxArgs)
                {
                    maxArgs += Options.MAX_ARGS;
                    MapFile.ExtendArray(ref args, maxArgs);
                }
                args[argc++] = line;
            }
            args[argc++] = path;

            options.verbose = 0;
            int argsNotUsed = CommandLineParser.ParseArguments(args, options);

            if (argsNotUsed == -1)
            {
                MainClass.Exit("Error parsing options", clientInfo);
            }
            string[] args2 = new string[argsNotUsed];
            for (int i = 0; i < argsNotUsed; i++)
            {
                args2[i] = args[args.Length - argsNotUsed + i];
            }

            MainClass.SetupProtocol(clientInfo);
            ioStream.IOStartMultiplexOut();
            Daemon.StartServer(clientInfo, args2);
        }
Example #9
0
        /// <summary>
        /// Receives exclude list from stream
        /// </summary>
        /// <param name="ioStream"></param>
        public void ReceiveExcludeList(IOStream ioStream)
        {
            string line = String.Empty;
            int length;
            while ((length = ioStream.readInt()) != 0)
            {
                if (length >= Options.MAXPATHLEN + 3)
                {
                    Log.Write("Overflow: recv_exclude_list");
                    continue;
                }

                line = ioStream.ReadStringFromBuffer(length);
                AddExclude(ref options.excludeList, line, 0);
            }
        }
Example #10
0
		public void sendFileEntry(FileStruct file, IOStream f, UInt32 baseflags)
		{
			UInt32 flags = baseflags;
			int l1 = 0,l2 = 0;

			if(f == null)
				return;
			if(file == null)
			{
				f.writeByte(0);
				lastName = "";
				return;
			}
			string fileName = file.FNameTo().Replace(":","");
			for (l1 = 0;
				lastName.Length >l1 && (fileName[l1] == lastName[l1]) && (l1 < 255);
				l1++) {}
			l2 = fileName.Substring(l1).Length;

			flags |= Options.XMIT_SAME_NAME;			

			if (l2 > 255)
				flags |= Options.XMIT_LONG_NAME; 
			if(options.protocolVersion >= 28)
			{
				if(flags == 0 && !Util.S_ISDIR(file.mode))
					flags |= Options.XMIT_TOP_DIR; 
				/*if ((flags & 0xFF00) > 0 || flags == 0) 
				{
					flags |= Options.XMIT_EXTENDED_FLAGS;
					f.writeByte((byte)flags);
					f.writeByte((byte)(flags >> 8));
				} 
				else					*/
					f.writeByte((byte)flags); 
			} else 
			{
				if ((flags & 0xFF) == 0 && !Util.S_ISDIR(file.mode))
					flags |= Options.XMIT_TOP_DIR;
				if ((flags & 0xFF) == 0)
					flags |= Options.XMIT_LONG_NAME;
				f.writeByte((byte)flags);
			}
			if ((flags & Options.XMIT_SAME_NAME) != 0)
				f.writeByte((byte)l1);
			if ((flags & Options.XMIT_LONG_NAME) != 0)
				f.writeInt(l2);
			else
				f.writeByte((byte)l2); 	
			
			
			byte[] b =System.Text.ASCIIEncoding.ASCII.GetBytes(fileName);

			f.Write(b, l1, l2);
			f.WriteLongInt(file.length); 

			
			if ((flags & Options.XMIT_SAME_TIME) == 0)
				f.writeInt(file.modTime.Second);
			if ((flags & Options.XMIT_SAME_MODE) == 0)
				f.writeInt((int)file.mode);
			if (options.preserveUID && (flags & Options.XMIT_SAME_UID) == 0) 
			{				
				f.writeInt(file.uid);
			}
			if (options.preserveGID && (flags & Options.XMIT_SAME_GID) == 0) 
			{				
				f.writeInt(file.gid);
			}
			if (options.alwaysChecksum ) 
			{
				byte[] sum;	
				if(!Util.S_ISDIR(file.mode))
					sum = file.sum;				
				else if(options.protocolVersion < 28)
					sum = new byte[16];
				else 
					sum = null;

				if (sum != null ) 
				{										
					f.Write(sum, 0, options.protocolVersion < 21 ? 2 : CheckSum.MD4_SUM_LENGTH);
				}

			}

			lastName = fileName;
		}
Example #11
0
        public int ClientRun(ClientInfo cInfo, int pid, string[] args)
        {
            Options   options = cInfo.Options;
            IOStream  f       = cInfo.IoStream;
            FileList  fList;
            ArrayList fileList;

            MainClass.SetupProtocol(cInfo);
            if (options.protocolVersion >= 23 && !options.readBatch)
            {
                f.IOStartMultiplexIn();
            }
            if (options.amSender)
            {
                f.IOStartBufferingOut();

                if (options.deleteMode && !options.deleteExcluded)
                {
                    Exclude excl = new Exclude(options);
                    excl.SendExcludeList(f);
                }

                if (options.verbose > 3)
                {
                    Log.Write("File list sent\n");
                }
                f.Flush();
                fList    = new FileList(options);
                fileList = fList.sendFileList(cInfo, args);
                if (options.verbose > 3)
                {
                    Log.WriteLine("file list sent");
                }
                f.Flush();
                Sender sender = new Sender(options);
                sender.SendFiles(fileList, cInfo);
                f.Flush();
                f.writeInt(-1);
                return(-1);
            }
            options.dir = args[0];
            if (options.dir.CompareTo("") != 0 && options.dir.IndexOf(':') == -1)
            {
                if (options.dir[0] == '/')
                {
                    options.dir = "c:" + options.dir;
                }
                else if (options.dir.IndexOf('/') != -1)
                {
                    options.dir = options.dir.Insert(options.dir.IndexOf('/') - 1, ":");
                }
            }
            if (!options.readBatch)
            {
                Exclude excl = new Exclude(options);
                excl.SendExcludeList(f);
            }
            fList    = new FileList(options);
            fileList = fList.receiveFileList(cInfo);
            return(Daemon.DoReceive(cInfo, fileList, null));
        }
Example #12
0
 public int ReceiveDeflatedToken(IOStream f, byte[] data, int offset)
 {
     return(0);
 }
Example #13
0
        public void ReceiveGenerator(string fileName, FileStruct file, int i, IOStream f)
        {
            fileName = Path.Combine(options.dir, fileName);

            if (UnchangedFile(fileName, file))
            {
                return;
            }
            if (options.verbose > 2)
            {
                Log.WriteLine("Receive Generator(" + fileName + "," + i + ")\n");
            }
            int statRet;
            FStat st = new FStat();
            if (options.dryRun)
            {
                statRet = -1;
            }
            else
            {
                statRet = 0;
                try
                {
                    FileInfo fi = new FileInfo(fileName);
                    // TODO: path length
                    st.size = fi.Length;
                    // TODO: path length
                    st.mTime = fi.LastWriteTime;
                }
                catch
                {
                    statRet = -1;
                }
            }

            if (options.onlyExisting && statRet == -1)
            {
                /* we only want to update existing files */
                if (options.verbose > 1)
                {
                    Log.WriteLine("not creating new file \"" + fileName + "\"");
                }
                return;
            }
            string fNameCmp = fileName;
            if (options.wholeFile > 0)
            {
                f.writeInt(i);
                WriteSumHead(f, null);
                return;
            }
            FileStream fd;
            try
            {
                fd = new FileStream(fNameCmp, FileMode.Open, FileAccess.Read);
            }
            catch
            {
                if (options.verbose > 3)
                {
                    Log.WriteLine("failed to open " + Util.fullFileName(fNameCmp) + ", continuing");
                }
                f.writeInt(i);
                WriteSumHead(f, null);
                return;
            }

            if (options.verbose > 3)
            {
                Log.WriteLine("gen mapped " + fNameCmp + " of size " + st.size);
            }

            if (options.verbose > 2)
            {
                Log.WriteLine("generating and sending sums for " + i);
            }

            f.writeInt(i);
            Stream fCopy = null;
            GenerateAndSendSums(fd, st.size, f, fCopy);

            if (fCopy != null)
            {
                fCopy.Close();
            }
            fd.Close();
        }
Example #14
0
        public void sendFileEntry(FileStruct file, IOStream f, UInt32 baseflags)
        {
            UInt32 flags = baseflags;
            int    l1 = 0, l2 = 0;

            if (f == null)
            {
                return;
            }
            if (file == null)
            {
                f.writeByte(0);
                lastName = "";
                return;
            }
            string fileName = file.FNameTo().Replace(":", "");

            for (l1 = 0;
                 lastName.Length > l1 && (fileName[l1] == lastName[l1]) && (l1 < 255);
                 l1++)
            {
            }
            l2 = fileName.Substring(l1).Length;

            flags |= Options.XMIT_SAME_NAME;

            if (l2 > 255)
            {
                flags |= Options.XMIT_LONG_NAME;
            }
            if (options.protocolVersion >= 28)
            {
                if (flags == 0 && !Util.S_ISDIR(file.mode))
                {
                    flags |= Options.XMIT_TOP_DIR;
                }

                /*if ((flags & 0xFF00) > 0 || flags == 0)
                 * {
                 *      flags |= Options.XMIT_EXTENDED_FLAGS;
                 *      f.writeByte((byte)flags);
                 *      f.writeByte((byte)(flags >> 8));
                 * }
                 * else					*/
                f.writeByte((byte)flags);
            }
            else
            {
                if ((flags & 0xFF) == 0 && !Util.S_ISDIR(file.mode))
                {
                    flags |= Options.XMIT_TOP_DIR;
                }
                if ((flags & 0xFF) == 0)
                {
                    flags |= Options.XMIT_LONG_NAME;
                }
                f.writeByte((byte)flags);
            }
            if ((flags & Options.XMIT_SAME_NAME) != 0)
            {
                f.writeByte((byte)l1);
            }
            if ((flags & Options.XMIT_LONG_NAME) != 0)
            {
                f.writeInt(l2);
            }
            else
            {
                f.writeByte((byte)l2);
            }


            byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes(fileName);

            f.Write(b, l1, l2);
            f.WriteLongInt(file.length);


            if ((flags & Options.XMIT_SAME_TIME) == 0)
            {
                f.writeInt(file.modTime.Second);
            }
            if ((flags & Options.XMIT_SAME_MODE) == 0)
            {
                f.writeInt((int)file.mode);
            }
            if (options.preserveUID && (flags & Options.XMIT_SAME_UID) == 0)
            {
                f.writeInt(file.uid);
            }
            if (options.preserveGID && (flags & Options.XMIT_SAME_GID) == 0)
            {
                f.writeInt(file.gid);
            }
            if (options.alwaysChecksum)
            {
                byte[] sum;
                if (!Util.S_ISDIR(file.mode))
                {
                    sum = file.sum;
                }
                else if (options.protocolVersion < 28)
                {
                    sum = new byte[16];
                }
                else
                {
                    sum = null;
                }

                if (sum != null)
                {
                    f.Write(sum, 0, options.protocolVersion < 21 ? 2 : CheckSum.MD4_SUM_LENGTH);
                }
            }

            lastName = fileName;
        }
Example #15
0
        public void ReceiveGenerator(string fileName, FileStruct file, int i, IOStream f)
        {
            fileName = Path.Combine(options.dir, fileName);

            if (UnchangedFile(fileName, file))
            {
                return;
            }
            if (options.verbose > 2)
            {
                Log.WriteLine("Receive Generator(" + fileName + "," + i + ")\n");
            }
            int   statRet;
            FStat st = new FStat();

            if (options.dryRun)
            {
                statRet = -1;
            }
            else
            {
                statRet = 0;
                try
                {
                    FileInfo fi = new FileInfo(fileName);
                    // TODO: path length
                    st.size = fi.Length;
                    // TODO: path length
                    st.mTime = fi.LastWriteTime;
                }
                catch
                {
                    statRet = -1;
                }
            }

            if (options.onlyExisting && statRet == -1)
            {
                /* we only want to update existing files */
                if (options.verbose > 1)
                {
                    Log.WriteLine("not creating new file \"" + fileName + "\"");
                }
                return;
            }
            string fNameCmp = fileName;

            if (options.wholeFile > 0)
            {
                f.writeInt(i);
                WriteSumHead(f, null);
                return;
            }
            FileStream fd;

            try
            {
                fd = new FileStream(fNameCmp, FileMode.Open, FileAccess.Read);
            }
            catch
            {
                if (options.verbose > 3)
                {
                    Log.WriteLine("failed to open " + Util.fullFileName(fNameCmp) + ", continuing");
                }
                f.writeInt(i);
                WriteSumHead(f, null);
                return;
            }

            if (options.verbose > 3)
            {
                Log.WriteLine("gen mapped " + fNameCmp + " of size " + st.size);
            }

            if (options.verbose > 2)
            {
                Log.WriteLine("generating and sending sums for " + i);
            }

            f.writeInt(i);
            Stream fCopy = null;

            GenerateAndSendSums(fd, st.size, f, fCopy);

            if (fCopy != null)
            {
                fCopy.Close();
            }
            fd.Close();
        }
Example #16
0
        public void GenerateFiles(IOStream f, List <FileStruct> fileList, string localName)
        {
            int i;
            int phase = 0;


            if (options.verbose > 2)
            {
                Log.WriteLine("generator starting count=" + fileList.Count);
            }

            for (i = 0; i < fileList.Count; i++)
            {
                FileStruct file = (fileList[i]);
                if (file.baseName == null)
                {
                    continue;
                }
                if (Util.S_ISDIR(file.mode))
                {
                    continue;
                }
                ReceiveGenerator(localName != null ? localName : file.GetFullName(), file, i, f);
            }

            phase++;
            checkSum.length = CheckSum.SUM_LENGTH;
            if (options.verbose > 2)
            {
                Log.WriteLine("GenerateFiles phase=" + phase);
            }
            f.writeInt(-1);

            phase++;
            if (options.verbose > 2)
            {
                Log.WriteLine("GenerateFiles phase=" + phase);
            }

            f.writeInt(-1);

            if (options.protocolVersion >= 29 && !options.delayUpdates)
            {
                f.writeInt(-1);
            }

            /* now we need to fix any directory permissions that were
             * modified during the transfer
             * */
            for (i = 0; i < fileList.Count; i++)
            {
                FileStruct file = (fileList[i]);
                if (file.baseName != null || Util.S_ISDIR(file.mode))
                {
                    continue;
                }
                ReceiveGenerator(localName != null ? localName : file.GetFullName(), file, i, null);
            }

            if (options.verbose > 2)
            {
                Log.WriteLine("GenerateFiles finished");
            }
        }
Example #17
0
        public int ReceiveFiles(ClientInfo clientInfo, List <FileStruct> fileList, string localName)
        {
            FStat      st = new FStat();
            FileStruct file;
            IOStream   ioStream = clientInfo.IoStream;

            string fileName;
            string fNameCmp = String.Empty, fNameTmp = String.Empty;
            bool   saveMakeBackups = options.makeBackups;
            int    i, phase = 0;
            bool   recv_ok;

            if (options.verbose > 2)
            {
                Log.WriteLine("ReceiveFiles(" + fileList.Count + ") starting");
            }
            while (true)
            {
                i = ioStream.readInt();
                if (i == -1)
                {
                    if (phase != 0)
                    {
                        break;
                    }

                    phase           = 1;
                    checkSum.length = CheckSum.SUM_LENGTH;
                    if (options.verbose > 2)
                    {
                        Log.WriteLine("ReceiveFiles phase=" + phase);
                    }
                    ioStream.writeInt(0); //send_msg DONE
                    if (options.keepPartial)
                    {
                        options.makeBackups = false;
                    }
                    continue;
                }

                if (i < 0 || i >= fileList.Count)
                {
                    MainClass.Exit("Invalid file index " + i + " in receiveFiles (count=" + fileList.Count + ")", clientInfo);
                }

                file = (fileList[i]);

                Options.stats.currentFileIndex = i;
                Options.stats.numTransferredFiles++;
                Options.stats.totalTransferredSize += file.length;

                if (localName != null && localName.CompareTo(String.Empty) != 0)
                {
                    fileName = localName;
                }
                else
                {
                    fileName = Path.Combine(options.dir, LocalizePath(clientInfo, file.GetFullName().Replace(":", String.Empty)).Replace("\\", "/"));
                    //fileName = Path.Combine(options.dir, file.FNameTo().Replace(":",String.Empty)).Replace("\\", "/");
                    // TODO: path length
                    Directory.CreateDirectory(Path.Combine(options.dir, LocalizePath(clientInfo, file.dirName.Replace(":", String.Empty))).Replace("\\", "/"));
                    Log.WriteLine(Path.Combine(options.dir, file.dirName));
                    //FileSystem.Directory.CreateDirectory(Path.Combine(options.dir,file.dirName.Replace(":",String.Empty)).Replace("\\", "/"));
                }

                if (options.dryRun)
                {
                    if (!options.amServer && options.verbose > 0)
                    {
                        Log.WriteLine(fileName);
                    }
                    continue;
                }

                if (options.verbose > 2)
                {
                    Log.WriteLine("receiveFiles(" + fileName + ")");
                }

                if (options.partialDir != null && options.partialDir.CompareTo(String.Empty) != 0)
                {
                }
                else
                {
                    fNameCmp = fileName;
                }

                FileStream fd1 = null;
                try
                {
                    fd1 = new FileStream(fNameCmp, FileMode.Open, FileAccess.Read);
                }
                catch (FileNotFoundException)
                {
                    fNameCmp = fileName;
                    try
                    {
                        fd1 = new FileStream(fNameCmp, FileMode.Open, FileAccess.Read);
                    }
                    catch (FileNotFoundException)
                    {
                    }
                }
                catch (Exception e)
                {
                    Log.Write(e.Message);
                }
                try
                {
                    FileInfo fi = new FileInfo(fNameCmp);
                    // TODO: path length
                    st.size = fi.Length;
                }
                catch { }

                String     tempFileName = getTmpName(fileName);
                FileStream fd2          = null;
                fd2 = new FileStream(tempFileName, FileMode.OpenOrCreate, FileAccess.Write);

                if (!options.amServer && options.verbose > 0)
                {
                    Log.WriteLine(fileName);
                }

                /* recv file data */
                recv_ok = ReceiveData(clientInfo, fNameCmp, fd1, st.size,
                                      fileName, fd2, file.length);

                if (fd1 != null)
                {
                    fd1.Close();
                }
                if (fd2 != null)
                {
                    fd2.Close();
                }
                // TODO: path length
                File.Copy(tempFileName, fileName, true);
                // TODO: path length
                File.Delete(tempFileName);
                if (recv_ok || options.inplace)
                {
                    FinishTransfer(fileName, fNameTmp, file, recv_ok);
                }
            }
            options.makeBackups = saveMakeBackups;

            if (options.deleteAfter && options.recurse && localName == null && fileList.Count > 0)
            {
                DeleteFiles(fileList);
            }

            if (options.verbose > 2)
            {
                Log.WriteLine("ReceiveFiles finished");
            }

            return(0);
        }
Example #18
0
        /// <summary>
        /// Server-side authorization check
        /// </summary>
        /// <param name="clientInfo"></param>
        /// <param name="moduleNumber"></param>
        /// <param name="addr"></param>
        /// <param name="leader"></param>
        /// <returns></returns>
        public static bool AuthorizeServer(ClientInfo clientInfo, int moduleNumber, string addr, string leader)
        {
            string users = Daemon.config.GetAuthUsers(moduleNumber).Trim();
            //string challenge;
            string   b64Challenge;
            IOStream ioStream = clientInfo.IoStream;
            string   line;

            string user   = String.Empty;
            string secret = String.Empty;
            string pass   = String.Empty;
            string pass2  = String.Empty;

            string[] listUsers;
            string   token = String.Empty;

            /* if no auth list then allow anyone in! */
            if (string.IsNullOrEmpty(users))
            {
                return(true);
            }

            b64Challenge = Base64Encode(GenerateChallenge(addr, clientInfo.Options));
            ioStream.IOPrintf(leader + b64Challenge + "\n");

            line = ioStream.ReadLine();

            if (line.IndexOf(' ') > 0)
            {
                user = line.Substring(0, line.IndexOf(' '));
                pass = line.Substring(line.IndexOf(' ')).Trim('\n').Trim();
            }
            else
            {
                return(false);
            }
            listUsers = users.Split(',');

            for (int i = 0; i < listUsers.Length; i++)
            {
                token = listUsers[i];
                if (user.Equals(token))
                {
                    break;
                }
                token = null;
            }

            if (string.IsNullOrEmpty(token))
            {
                return(false);
            }

            if ((secret = GetSecret(moduleNumber, user)) == null)
            {
                return(false);
            }

            pass2 = GenerateHash(secret, b64Challenge, clientInfo.Options);

            if (pass.Equals(pass2))
            {
                return(true);
            }
            return(false);
        }
Example #19
0
        public static int StartInbandExchange(string user, string path, ClientInfo cInfo, int argc)
        {
            Options  options = cInfo.Options;
            IOStream f       = cInfo.IoStream;

            string[] sargs = new string[Options.MAX_ARGS];
            int      sargc = options.ServerOptions(sargs);

            sargs[sargc++] = ".";
            //if(path != null && path.Length>0)
            //sargs[sargc++] = path;

            if (argc == 0 && !options.amSender)
            {
                options.listOnly = true;
            }
            if (path[0] == '/')
            {
                Log.WriteLine("ERROR: The remote path must start with a module name");
                return(-1);
            }
            f.IOPrintf("@RSYNCD: " + options.protocolVersion + "\n");
            string line = f.ReadLine();

            try
            {
                options.remoteProtocol = Int32.Parse(line.Substring(9, 2));
            }
            catch
            {
                options.remoteProtocol = 0;
            }
            bool isValidstring = line.StartsWith("@RSYNCD: ") && line.EndsWith("\n") && options.remoteProtocol > 0;

            if (!isValidstring)
            {
                f.IOPrintf("@ERROR: protocol startup error\n");
                return(-1);
            }
            if (options.protocolVersion > options.remoteProtocol)
            {
                options.protocolVersion = options.remoteProtocol;
            }
            f.IOPrintf(path + "\n");
            while (true)
            {
                line = f.ReadLine();
                if (line.CompareTo("@RSYNCD: OK\n") == 0)
                {
                    break;
                }
                if (line.Length > 18 && line.Substring(0, 18).CompareTo("@RSYNCD: AUTHREQD ") == 0)
                {
                    string pass = "";
                    if (user.IndexOf(':') != -1)
                    {
                        pass = user.Substring(user.IndexOf(':') + 1);
                        user = user.Substring(0, user.IndexOf(':'));
                    }
                    f.IOPrintf(user + " " + Authentication.auth_client(user, pass, line.Substring(18).Replace("\n", ""), options) + "\n");
                    continue;
                }

                if (line.CompareTo("@RSYNCD: EXIT\n") == 0)
                {
                    MainClass.Exit("@RSYNCD: EXIT", null);
                }

                if (line.StartsWith("@ERROR: "))
                {
                    MainClass.Exit("Server: " + line.Replace("\n", ""), null);
                }
            }

            for (int i = 0; i < sargc; i++)
            {
                f.IOPrintf(sargs[i] + "\n");
            }
            f.IOPrintf("\n");
            return(0);
        }
Example #20
0
        public ArrayList receiveFileList(ClientInfo cInfo)
        {
            IOStream  f        = cInfo.IoStream;
            ArrayList fileList = new ArrayList();

            if (showFileListP())
            {
                startFileListProgress("receiving file list");
            }

            Int64 startRead = Options.stats.totalRead;

            UInt32 flags;

            while ((flags = f.readByte()) != 0)
            {
                if (options.protocolVersion >= 28 && (flags & Options.XMIT_EXTENDED_FLAGS) != 0)
                {
                    flags |= (UInt32)(f.readByte() << 8);
                }
                FileStruct file = receiveFileEntry(flags, cInfo);
                if (file == null)
                {
                    continue;
                }
                fileList.Add(file);
                Options.stats.totalSize += ((FileStruct)fileList[fileList.Count - 1]).length;
                mayBeEmitFileListProgress(fileList);
                if (options.verbose > 2)
                {
                    Log.WriteLine("receiveFileName(" + ((FileStruct)fileList[fileList.Count - 1]).FNameTo() + ")");
                }
            }
            receiveFileEntry(0, null);

            if (options.verbose > 2)
            {
                Log.WriteLine("received " + fileList.Count + " names");
            }

            if (showFileListP())
            {
                finishFileListProgress(fileList);
            }

            cleanFileList(fileList, (options.relativePaths == 0) ? false : true, true);

            if (f != null)
            {
                f.readInt();
            }

            if (options.verbose > 3)
            {
                outputFileList(fileList);
            }
            if (options.listOnly)
            {
                for (int i = 0; i < fileList.Count; i++)
                {
                    listFileEntry((FileStruct)fileList[i]);
                }
            }
            if (options.verbose > 2)
            {
                Log.WriteLine("receiveFileList done");
            }

            Options.stats.flistSize = (int)(Options.stats.totalRead - startRead);
            Options.stats.numFiles  = fileList.Count;
            return(fileList);
        }
Example #21
0
        public void GenerateAndSendSums(Stream fd, long len, IOStream f, Stream fCopy)
        {
            long i;
            MapFile mapBuf;
            SumStruct sum = new SumStruct();
            long offset = 0;

            SumSizesSqroot(sum, (UInt64)len);

            if (len > 0)
            {
                mapBuf = new MapFile(fd, (int)len, Options.MAX_MAP_SIZE, (int)sum.bLength);
            }
            else
            {
                mapBuf = null;
            }

            WriteSumHead(f, sum);

            for (i = 0; i < sum.count; i++)
            {
                UInt32 n1 = (UInt32)Math.Min(len, sum.bLength);
                int off = mapBuf.MapPtr((int)offset, (int)n1);
                byte[] map = mapBuf.p;
                UInt32 sum1 = CheckSum.GetChecksum1(map, off, (int)n1);
                byte[] sum2 = new byte[CheckSum.SUM_LENGTH];

                sum2 = checkSum.GetChecksum2(map, off, (int)n1);
                if (options.verbose > 3)
                {
                    Log.WriteLine("chunk[" + i + "] offset=" + offset + " len=" + n1 + " sum1=" + sum1);
                }
                f.writeInt((int)sum1);
                f.Write(sum2, 0, sum.s2Length);
                len -= n1;
                offset += n1;
            }
            if (mapBuf != null)
            {
                mapBuf = null;
            }
        }
Example #22
0
        public FileStruct receiveFileEntry(UInt32 flags, ClientInfo cInfo)
        {
            if (cInfo == null)
            {
                lastName = "";
                return(null);
            }
            IOStream f = cInfo.IoStream;

            int l1 = 0, l2 = 0;

            if ((flags & Options.XMIT_SAME_NAME) != 0)
            {
                l1 = f.readByte();
            }

            if ((flags & Options.XMIT_LONG_NAME) != 0)
            {
                l2 = f.readInt();
            }
            else
            {
                l2 = f.readByte();
            }
            if (l2 >= Options.MAXPATHLEN - l1)
            {
                MainClass.Exit("overflow: lastname=" + lastName, cInfo);
            }

            string thisName = lastName.Substring(0, l1);

            thisName += f.ReadSBuf(l2);
            lastName  = thisName;

            thisName = Util.cleanFileName(thisName, false);
            if (options.sanitizePath)
            {
                thisName = Util.sanitizePath(thisName, "", 0);
            }

            string baseName = "";
            string dirName  = "";

            if (thisName.LastIndexOf("/") != -1)
            {
                baseName = Path.GetFileName(thisName);
                dirName  = FileSystem.Directory.GetDirectoryName(thisName).Replace(@"\", "/").Replace(":", "");
            }
            else
            {
                baseName = thisName;
                dirName  = null;
            }

            Int64 fileLength = f.ReadLongInt();

            if ((flags & Options.XMIT_SAME_TIME) == 0)
            {
                modTime = DateTime.FromFileTime(f.readInt());
            }
            if ((flags & Options.XMIT_SAME_MODE) == 0)
            {
                mode = (UInt32)f.readInt();
            }

            if (options.preserveUID && (flags & Options.XMIT_SAME_UID) == 0)
            {
                int uid = f.readInt();
            }
            if (options.preserveGID && (flags & Options.XMIT_SAME_GID) == 0)
            {
                int gid = f.readInt();
            }

            byte[] sum = new byte[0];
            if (options.alwaysChecksum && !Util.S_ISDIR(mode))
            {
                sum = new byte[CheckSum.MD4_SUM_LENGTH];
                sum = f.ReadBuf(options.protocolVersion < 21 ? 2 : CheckSum.MD4_SUM_LENGTH);
            }

            FileStruct fs = new FileStruct();

            fs.length   = (int)fileLength;
            fs.baseName = baseName;
            fs.dirName  = dirName;
            fs.sum      = sum;
            fs.mode     = mode;
            fs.modTime  = modTime;
            fs.flags    = flags;
            return(fs);
        }
Example #23
0
        public bool ReceiveData(ClientInfo clientInfo, string fileNameR, Stream fdR, long sizeR, string fileName, Stream fd, int totalSize)
        {
            IOStream f = clientInfo.IoStream;

            byte[]    fileSum1  = new byte[CheckSum.MD4_SUM_LENGTH];
            byte[]    fileSum2  = new byte[CheckSum.MD4_SUM_LENGTH];
            byte[]    data      = new byte[Match.CHUNK_SIZE];
            SumStruct sumStruct = new SumStruct();
            MapFile   mapBuf    = null;
            Sender    sender    = new Sender(options);

            sender.ReadSumHead(clientInfo, ref sumStruct);
            int    offset = 0;
            UInt32 len;

            if (fdR != null && sizeR > 0)
            {
                int mapSize = (int)Math.Max(sumStruct.bLength * 2, 16 * 1024);
                mapBuf = new MapFile(fdR, (int)sizeR, mapSize, (int)sumStruct.bLength);
                if (options.verbose > 2)
                {
                    Log.WriteLine("recv mapped " + fileNameR + " of size " + sizeR);
                }
            }
            Sum sum = new Sum(options);

            sum.Init(options.checksumSeed);

            int   i;
            Token token = new Token(options);

            while ((i = token.ReceiveToken(f, ref data, 0)) != 0)
            {
                if (options.doProgress)
                {
                    Progress.ShowProgress(offset, totalSize);
                }

                if (i > 0)
                {
                    if (options.verbose > 3)
                    {
                        Log.WriteLine("data recv " + i + " at " + offset);
                    }
                    Options.stats.literalData += i;
                    sum.Update(data, 0, i);
                    if (fd != null && FileIO.WriteFile(fd, data, 0, i) != i)
                    {
                        goto report_write_error;
                    }
                    offset += i;
                    continue;
                }

                i = -(i + 1);
                int offset2 = (int)(i * sumStruct.bLength);
                len = sumStruct.bLength;
                if (i == sumStruct.count - 1 && sumStruct.remainder != 0)
                {
                    len = sumStruct.remainder;
                }

                Options.stats.matchedData += len;

                if (options.verbose > 3)
                {
                    Log.WriteLine("chunk[" + i + "] of size " + len + " at " + offset2 + " offset=" + offset);
                }

                byte[] map = null;
                int    off = 0;
                if (mapBuf != null)
                {
                    off = mapBuf.MapPtr(offset2, (int)len);
                    map = mapBuf.p;

                    token.SeeToken(map, offset, (int)len);
                    sum.Update(map, off, (int)len);
                }

                if (options.inplace)
                {
                    if (offset == offset2 && fd != null)
                    {
                        offset += (int)len;
                        if (fd.Seek(len, SeekOrigin.Current) != offset)
                        {
                            MainClass.Exit("seek failed on " + Util.fullFileName(fileName), clientInfo);
                        }
                        continue;
                    }
                }
                if (fd != null && FileIO.WriteFile(fd, map, off, (int)len) != (int)len)
                {
                    goto report_write_error;
                }
                offset += (int)len;
            }

            if (options.doProgress)
            {
                Progress.EndProgress(totalSize);
            }
            if (fd != null && offset > 0 && FileIO.SparseEnd(fd) != 0)
            {
                MainClass.Exit("write failed on " + Util.fullFileName(fileName), clientInfo);
            }

            fileSum1 = sum.End();

            if (mapBuf != null)
            {
                mapBuf = null;
            }

            fileSum2 = f.ReadBuffer(CheckSum.MD4_SUM_LENGTH);
            if (options.verbose > 2)
            {
                Log.WriteLine("got fileSum");
            }
            if (fd != null && Util.MemoryCompare(fileSum1, 0, fileSum2, 0, CheckSum.MD4_SUM_LENGTH) != 0)
            {
                return(false);
            }
            return(true);

report_write_error:
            {
                MainClass.Exit("write failed on " + Util.fullFileName(fileName), clientInfo);
            }
            return(true);
        }
Example #24
0
        public ArrayList sendFileList(ClientInfo cInfo, string[] argv)
        {
            IOStream f = null;

            if (cInfo != null)
            {
                f = cInfo.IoStream;
            }

            string dir = "", olddir = "";
            string lastPath = "";
            string fileName = "";
            bool   useFFFD  = false;

            if (showFileListP() && f != null)
            {
                startFileListProgress("building file list");
            }
            Int64     startWrite = Options.stats.totalWritten;
            ArrayList fileList   = new ArrayList();

            if (f != null)
            {
                f.IOStartBufferingOut();
                if (Options.filesFromFD != null)
                {
                    if (argv[0] != null && argv[0].CompareTo("") != 0 && !Util.pushDir(argv[0]))
                    {
                        MainClass.Exit("pushDir " + Util.fullFileName(argv[0]) + " failed", cInfo);
                    }
                    useFFFD = true;
                }
            }
            while (true)
            {
                if (useFFFD)
                {
                    if ((fileName = f.readFilesFromLine(Options.filesFromFD, options)).Length == 0)
                    {
                        break;
                    }
                }
                else
                {
                    if (argv.Length == 0)
                    {
                        break;
                    }
                    fileName = argv[0];
                    argv     = (string[])Util.ArrayDeleteFirstElement(argv);
                    if (fileName != null && fileName.CompareTo(".") == 0)
                    {
                        continue;
                    }
                    if (fileName != null)
                    {
                        fileName = fileName.Replace(@"\", "/");
                    }
                }
                // TODO: path length
                if (FileSystem.Directory.Exists(fileName) && !options.recurse && options.filesFrom == null)
                {
                    Log.WriteLine("skipping directory " + fileName);
                    continue;
                }

                dir    = null;
                olddir = "";

                if (options.relativePaths == 0)
                {
                    int index = fileName.LastIndexOf('/');
                    if (index != -1)
                    {
                        if (index == 0)
                        {
                            dir = "/";
                        }
                        else
                        {
                            dir = fileName.Substring(0, index);
                        }
                        fileName = fileName.Substring(index + 1);
                    }
                }
                else
                {
                    if (f != null && options.impliedDirs && fileName.LastIndexOf('/') > 0)
                    {
                        string fn    = fileName.Substring(0, fileName.LastIndexOf('/'));
                        string slash = fileName;
                        int    i     = 0;
                        while (i < fn.Length && i < lastPath.Length && fn[i] == lastPath[i])
                        {
                            if (fn[i] == '/')
                            {
                                slash = fileName.Substring(i);
                            }
                            i++;
                        }
                        if (i != fileName.LastIndexOf('/') || (i < lastPath.Length && lastPath[i] != '/'))
                        {
                            bool copyLinksSaved = options.copyLinks;
                            bool recurseSaved   = options.recurse;
                            options.copyLinks = options.copyUnsafeLinks;
                            options.recurse   = true;
                            int j;
                            while ((j = slash.IndexOf('/')) != -1)
                            {
                                sendFileName(f, fileList, fileName.Substring(0, j), false, 0);
                                slash = slash.Substring(0, j) + ' ' + slash.Substring(j + 1);
                            }
                            options.copyLinks = copyLinksSaved;
                            options.recurse   = recurseSaved;
                            lastPath          = fileName.Substring(0, i);
                        }
                    }
                }
                if (dir != null && dir != "")
                {
                    olddir = Util.currDir;
                    if (!Util.pushDir(dir))
                    {
                        Log.WriteLine("pushDir " + Util.fullFileName(dir) + " failed");
                        continue;
                    }
                    if (lastDir != null && lastDir.CompareTo(dir) == 0)
                    {
                        fileListDir = lastDir;
                    }
                    else
                    {
                        fileListDir = lastDir = dir;
                    }
                }
                sendFileName(f, fileList, fileName, options.recurse, Options.XMIT_TOP_DIR);
                if (olddir != null && olddir != "")
                {
                    fileListDir = null;
                    if (Util.popDir(olddir))
                    {
                        MainClass.Exit("pop_dir " + Util.fullFileName(dir) + " failed", cInfo);
                    }
                }
            }
            if (f != null)
            {
                sendFileEntry(null, f, 0);
                if (showFileListP())
                {
                    finishFileListProgress(fileList);
                }
            }
            cleanFileList(fileList, false, false);
            if (f != null)
            {
                f.writeInt(0);
                Options.stats.flistSize = (int)(Options.stats.totalWritten - startWrite);
                Options.stats.numFiles  = fileList.Count;
            }

            if (options.verbose > 3)
            {
                outputFileList(fileList);
            }
            if (options.verbose > 2)
            {
                Log.WriteLine("sendFileList done");
            }
            return(fileList);
        }
Example #25
0
        /// <summary>
        /// Do nothing
        /// </summary>
        /// <param name="f"></param>
        public static void SendListing(IOStream f) //@todo_long empty method
        {

        }
Example #26
0
        public static bool AuthServer(ClientInfo cInfo, int moduleNumber, string addr, string leader)
        {
            string   users = Daemon.config.GetAuthUsers(moduleNumber).Trim();
            string   challenge;
            string   b64_challenge;
            IOStream f = cInfo.IoStream;
            string   line;

            string user   = "";
            string secret = "";
            string pass   = "";
            string pass2  = "";

            string[] listUsers;
            string   tok = "";


            /* if no auth list then allow anyone in! */
            if (users == null || users.CompareTo("") == 0)
            {
                return(true);
            }

            challenge = gen_challenge(addr, cInfo.Options);

            b64_challenge = base64_encode(challenge);

            f.IOPrintf(leader + b64_challenge + "\n");

            line = f.ReadLine();

            if (line.IndexOf(' ') > 0)
            {
                user = line.Substring(0, line.IndexOf(' '));
                pass = line.Substring(line.IndexOf(' ')).Trim('\n').Trim();
            }
            else
            {
                return(false);
            }
            listUsers = users.Split(',');

            for (int i = 0; i < listUsers.Length; i++)
            {
                tok = listUsers[i];
                if (user.CompareTo(tok) == 0)
                {
                    break;
                }
                tok = null;
            }

            if (tok == null || tok.CompareTo("") == 0)
            {
                return(false);
            }

            if ((secret = GetSecret(moduleNumber, user)) == null)
            {
                return(false);
            }

            pass2 = generate_hash(secret, b64_challenge, cInfo.Options);

            if (pass.CompareTo(pass2) == 0)
            {
                return(true);
            }
            return(false);
        }
Example #27
0
		public void MatchSums(IOStream f, SumStruct s, MapFile buf, int len)
		{
			byte[] fileSum = new byte[CheckSum.MD4_SUM_LENGTH];

			lastMatch = 0;
			falseAlarms = 0;
			tagHits = 0;
			matches = 0;
			dataTransfer = 0;

			Sum sum = new Sum(options);
			sum.Init(options.checksumSeed);

			if (len > 0 && s.count>0) 
			{
				BuildHashTable(s);

				if (options.verbose > 2)
					Log.WriteLine("built hash table");

				HashSearch(f,s,buf,len, sum);

				if (options.verbose > 2)
					Log.WriteLine("done hash search");
			} 
			else 
			{
				for (int j = 0; j < len - CHUNK_SIZE; j += CHUNK_SIZE) 
				{
					int n1 = Math.Min(CHUNK_SIZE,(len-CHUNK_SIZE)-j);
					Matched(f,s,buf,j+n1,-2, sum);
				}
				Matched(f,s,buf,len,-1,sum);
			}

			fileSum = sum.End();
			if (buf != null && buf.status)
				fileSum[0]++;

			if (options.verbose > 2)
				Log.WriteLine("sending fileSum");
			f.Write(fileSum, 0, CheckSum.MD4_SUM_LENGTH);

			targets.Clear();

			if (options.verbose > 2)
				Log.WriteLine("falseAlarms=" +  falseAlarms + " tagHits=" + tagHits + " matches=" + matches);

			totalTagHits += tagHits;
			totalFalseAlarms += falseAlarms;
			totalMatches += matches;
			Options.stats.literalData += dataTransfer;
		}
Example #28
0
 public void SendDeflatedToken(IOStream f, int token, MapFile buf, int offset, int nb, int toklen)
 {
 }
Example #29
0
		public void HashSearch(IOStream f,SumStruct s, MapFile buf, int len, Sum _sum)
		{
			int offset, end, backup;
			UInt32 k;
			int wantI;
			byte[] sum2 = new byte[CheckSum.SUM_LENGTH];
			UInt32 s1, s2, sum;
			int more;
			byte[] map;
			
			wantI = 0;
			if (options.verbose > 2)
				Log.WriteLine("hash search ob=" + s.bLength +" len=" + len);

			k = (UInt32)Math.Min(len, s.bLength);
			int off = buf.MapPtr(0, (int)k);
			map = buf.p;
			
			UInt32 g = s.sums[0].sum1;
			sum = CheckSum.GetChecksum1(map, off, (int)k);
			s1 = sum & 0xFFFF;
			s2 = sum >> 16;
			if (options.verbose > 3)
				Log.WriteLine("sum=" + sum +" k=" + k);

			offset = 0;
			end = (int)(len + 1 - s.sums[s.count-1].len);
			if (options.verbose > 3)
				Log.WriteLine("hash search s.bLength=" + s.bLength +" len=" + len +" count=" + s.count);

			do 
			{
				UInt32 t = GetTag2(s1,s2);
				bool doneCsum2 = false;
				int j = tagTable[t];

				if (options.verbose > 4)
					Log.WriteLine("offset=" + offset + " sum=" + sum);

				if (j == NULL_TAG)
					goto null_tag;

				sum = (s1 & 0xffff) | (s2 << 16);
				tagHits++;
				do 
				{
					UInt32 l;
					int i = ((Target)targets[j]).i;

					if (sum != s.sums[i].sum1)
						continue;

					l = (UInt32)Math.Min(s.bLength, len-offset);
					if (l != s.sums[i].len)
						continue;

					if (options.verbose > 3)
						Log.WriteLine("potential match at " + offset + " target=" + j +" " + i + " sum=" + sum);

					if (!doneCsum2) 
					{
						off = buf.MapPtr(offset, (int)l);
						map = buf.p;
						CheckSum cs = new CheckSum(options); 
						sum2 = cs.GetChecksum2(map, off, (int)l);
						doneCsum2 = true;
					}

					if (Util.MemCmp(sum2, 0, s.sums[i].sum2, 0, s.s2Length) != 0) 
					{
						falseAlarms++;
						continue;
					}
					
					if (i != wantI && wantI < s.count
						&& (!options.inplace || options.makeBackups || s.sums[wantI].offset >= offset
						|| (s.sums[wantI].flags & SUMFLG_SAME_OFFSET) != 0)
						&& sum == s.sums[wantI].sum1
						&& Util.MemCmp(sum2, 0, s.sums[wantI].sum2, 0, s.s2Length) == 0) 
					{
						i = wantI;
					}
				set_want_i:
					wantI = i + 1;

					Matched(f,s,buf,offset,i,_sum);
					offset += (int)(s.sums[i].len - 1);
					k = (UInt32)Math.Min(s.bLength, len-offset);
					off = buf.MapPtr(offset, (int)k);					
					sum = CheckSum.GetChecksum1(map, off, (int)k);
					s1 = sum & 0xFFFF;
					s2 = sum >> 16;
					matches++;
					break;
				} while (++j < s.count && ((Target)targets[j]).t == t);								
			null_tag:
				backup = offset - lastMatch;
				if (backup < 0)
					backup = 0;

				more = (offset + k) < len ? 1 : 0;
				off = buf.MapPtr(offset - backup, (int)(k + more + backup))+ backup;				
				s1 -= (UInt32)(CheckSum.ToInt(map[off]) + CheckSum.CHAR_OFFSET);
				s2 -= (UInt32)(k * CheckSum.ToInt(map[off]) + CheckSum.CHAR_OFFSET);				
				off = (k + off >= map.Length) ? (int)(map.Length-k-1) : off;
				if (more != 0) 
				{
					s1 += (UInt32)(CheckSum.ToInt(map[k + off]) + CheckSum.CHAR_OFFSET);
					s2 += s1;
				} 
				else
					--k;

				if (backup >= CHUNK_SIZE + s.bLength && end - offset > CHUNK_SIZE)
					Matched(f,s,buf,(int)(offset - s.bLength), -2, _sum);
			} while (++offset < end);

			Matched(f,s,buf,len,-1, _sum);
			buf.MapPtr(len-1,1);
		}
Example #30
0
		public void sendFileName(IOStream f, ArrayList fileList, string fileName, bool recursive, UInt32 baseFlags)
		{
			FileStruct file = makeFile(fileName, fileList, f == null && options.deleteExcluded ? Options.SERVER_EXCLUDES : Options.ALL_EXCLUDES);
			if(file == null)
				return;
			mayBeEmitFileListProgress(fileList);
			if(file.baseName != null && file.baseName.CompareTo("") != 0)
			{
				fileList.Add(file);
				sendFileEntry(file, f, baseFlags );
				
				if(recursive && Util.S_ISDIR(file.mode) && (file.flags & Options.FLAG_MOUNT_POINT) == 0)
				{								
					options.localExcludeList.Clear();
					sendDirectory(f, fileList, file.FNameTo());
				}
			}
		}
Example #31
0
		public void SendExcludeList(IOStream f)
		{						
			if (options.listOnly && !options.recurse)
				AddExclude(ref options.excludeList,"/*/*",0);				

			foreach (ExcludeStruct ent in options.excludeList) 			
			{
				int l;				
				string p;

				if(ent.pattern.Length == 0 || ent.pattern.Length > Options.MAXPATHLEN)
					continue;
				l = ent.pattern.Length;
				p = ent.pattern;				
				if ((ent.matchFlags & Options.MATCHFLG_DIRECTORY) != 0) 
				{
					p += "/\0";					
				}

				if ((ent.matchFlags & Options.MATCHFLG_INCLUDE) != 0) 
				{
					f.writeInt(l + 2);
					f.IOPrintf("+ ");
				} 
				else if ((p[0] == '-' || p[0] == '+') && p[1] == ' ') 
				{
					f.writeInt(l + 2);
					f.IOPrintf("- ");
				} 
				else 
					f.writeInt(l);
				f.IOPrintf(p);
				
			}
			f.writeInt(0);
		}
Example #32
0
		public void sendDirectory(IOStream f, ArrayList fileList, string dir)
		{
			FileSystem.DirectoryInfo di = new FileSystem.DirectoryInfo(dir);
			if(di.Exists)
			{
				if(options.cvsExclude)
				{
					Exclude excl = new Exclude(options);
					excl.AddExcludeFile(ref options.localExcludeList, dir, (int)(Options.XFLG_WORD_SPLIT & Options.XFLG_WORDS_ONLY));
				}
				FileSystem.FileInfo[] files = di.GetFiles();
				for(int i=0; i< files.Length; i++)
					// TODO: path length
					sendFileName(f, fileList,files[i].FullName.Replace("\\", "/"), options.recurse,0);
				FileSystem.DirectoryInfo[] dirs = di.GetDirectories();
				for(int i=0; i< dirs.Length; i++)
					// TODO: path length
					sendFileName(f, fileList,dirs[i].FullName.Replace("\\", "/"), options.recurse,0);
			} 
			else
			{
				Log.WriteLine("Can't find directory '" + Util.fullFileName(dir) + "'");
				return;
			}
		}
Example #33
0
        public void MatchSums(IOStream f, SumStruct s, MapFile buf, int len)
        {
            byte[] fileSum = new byte[CheckSum.MD4_SUM_LENGTH];

            lastMatch    = 0;
            falseAlarms  = 0;
            tagHits      = 0;
            matches      = 0;
            dataTransfer = 0;

            Sum sum = new Sum(options);

            sum.Init(options.checksumSeed);

            if (len > 0 && s.count > 0)
            {
                BuildHashTable(s);

                if (options.verbose > 2)
                {
                    Log.WriteLine("built hash table");
                }

                HashSearch(f, s, buf, len, sum);

                if (options.verbose > 2)
                {
                    Log.WriteLine("done hash search");
                }
            }
            else
            {
                for (int j = 0; j < len - CHUNK_SIZE; j += CHUNK_SIZE)
                {
                    int n1 = Math.Min(CHUNK_SIZE, (len - CHUNK_SIZE) - j);
                    Matched(f, s, buf, j + n1, -2, sum);
                }
                Matched(f, s, buf, len, -1, sum);
            }

            fileSum = sum.End();
            if (buf != null && buf.status)
            {
                fileSum[0]++;
            }

            if (options.verbose > 2)
            {
                Log.WriteLine("sending fileSum");
            }
            f.Write(fileSum, 0, CheckSum.MD4_SUM_LENGTH);

            targets.Clear();

            if (options.verbose > 2)
            {
                Log.WriteLine("falseAlarms=" + falseAlarms + " tagHits=" + tagHits + " matches=" + matches);
            }

            totalTagHits              += tagHits;
            totalFalseAlarms          += falseAlarms;
            totalMatches              += matches;
            Options.stats.literalData += dataTransfer;
        }
Example #34
0
 public static IOStream OpenSocketOut(string host, int port, string bindAddress)
 {
     TcpClient client = null;
     try
     {
         client = new TcpClient(host, port);
     }
     catch (Exception)
     {
         MainClass.Exit("Can't connect to server", null);
     }
     IOStream stream = new IOStream(client.GetStream());
     return stream;
 }
Example #35
0
		public int ReceiveDeflatedToken(IOStream f,byte[] data, int offset)
		{
			return 0;
		}
Example #36
0
		public void Matched(IOStream f, SumStruct s, MapFile buf, int offset, int i, Sum sum)
		{
			int n = offset - lastMatch;
			int j;

			if (options.verbose > 2 && i >= 0)
				Log.WriteLine("match at " + offset +" last_match=" + lastMatch + " j=" + i + " len=" + s.sums[i].len + " n=" + n);

			Token token = new Token(options);
			token.SendToken(f,i,buf,lastMatch,n,(int)(i<0?0:s.sums[i].len));
			dataTransfer += n;

			if (i >= 0) 
			{
				Options.stats.matchedData += s.sums[i].len;
				n += (int)s.sums[i].len;
			}

			for (j = 0; j < n; j += CHUNK_SIZE) 
			{
				int n1 = Math.Min(CHUNK_SIZE,n-j);
				int off = buf.MapPtr(lastMatch + j, n1);
				sum.Update(buf.p , off, n1);
			}

			if (i >= 0)
				lastMatch = (int)(offset + s.sums[i].len);
			else
				lastMatch = offset;

			if (buf != null && options.doProgress) 
			{
				Progress.ShowProgress(lastMatch, buf.fileSize);
				if (i == -1)
					Progress.EndProgress(buf.fileSize);
			}
		}
Example #37
0
		public void SendDeflatedToken(IOStream f, int token, MapFile buf, int offset, int nb, int toklen)
		{
		}
Example #38
0
		public void MatchReport(IOStream f)
		{
			if (options.verbose <= 1)
				return;

			string report = "total: matches=" + totalMatches + "  tagHits=" + totalTagHits +"  falseAlarms=" +
				totalFalseAlarms + " data=" + Options.stats.literalData;

			Log.WriteLine(report);
			if(options.amServer)
			{
				f.MplexWrite(MsgCode.MSG_INFO,ASCIIEncoding.ASCII.GetBytes(report),report.Length);
			}
		} 
Example #39
0
		public void SimpleSendToken(IOStream f,int token, MapFile buf, int offset, int n)
		{
			if (n > 0) 
			{
				int l = 0;
				while (l < n) 
				{
					int n1 = Math.Min(Match.CHUNK_SIZE,n-l);
					f.writeInt(n1);
					int off = buf.MapPtr(offset + l, n1);
					f.Write(buf.p, off, n1);					
					l += n1;
				}
			}
			if (token != -2) 
				f.writeInt(-(token+1));
		}
Example #40
0
		public void ReceiveExcludeList(IOStream f)		
		{			
			string line = "";
			int l;
			while ((l = f.readInt()) != 0) 
			{
				if (l >= Options.MAXPATHLEN+3)
				{
					Log.Write("overflow: recv_exclude_list");
					continue;
				}

				line = f.ReadSBuf(l);
				AddExclude(ref options.excludeList, line, 0);				
			}
		}
Example #41
0
 /// <summary>
 /// Do nothing
 /// </summary>
 /// <param name="f"></param>
 public static void SendListing(IOStream f) //@todo_long empty method
 {
 }