Пример #1
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?
        {
            var path = Daemon.Config.GetModule(moduleNumber).Path;
            var name = Daemon.Config.GetModuleName(moduleNumber);
            var ioStream = clientInfo.IoStream;
            var options = clientInfo.Options;
            var args = new string[Options.MaxArgs];
            int argc = 0, maxArgs = Options.MaxArgs;
            var line = String.Empty;

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

            var 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");
                    WinRsync.Exit("@ERROR: Path not found: " + path, clientInfo);
                }
            }
            options.AmServer = true; //to fix error in SetupProtocol
            options.Dir      = path;

            do
            {
                line = ioStream.ReadLine();
                line = line.Substring(0, line.Length - 1);
                if (argc == maxArgs)
                {
                    maxArgs += Options.MaxArgs;
                    MapFile.ExtendArray(ref args, maxArgs);
                }
                args[argc++] = line;
            } while (!string.IsNullOrEmpty(line));

            args[argc++] = path;

            options.Verbose = 0;



            var argsNotUsed = CommandLineParser.ParseArguments(args, options);

            if (argsNotUsed == -1)
            {
                WinRsync.Exit("Error parsing options", clientInfo);
            }

            var args2 = new string[argsNotUsed];

            for (var i = 0; i < argsNotUsed; i++)
            {
                args2[i] = args[args.Length - argsNotUsed + i];
            }



            WinRsync.SetupProtocol(clientInfo);
            ioStream.IoStartMultiplexOut();
            Daemon.StartServer(clientInfo, args2);
        }
Пример #2
0
        /// <summary>
        /// Reads socket into buffer
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public int ReadSocketUnbuffered(byte[] buffer, int offset, int count)
        {
            int tag, result = 0;

            byte[] line = new byte[1024];

            if (IOBufIn == null)
            {
                return(socketIn.Read(buffer, offset, count));
            }

            if (!IOMultiplexingIn && remaining == 0)
            {
                Flush();
                remaining    = socketIn.Read(IOBufIn, 0, IOBufInSize);
                IOBufInIndex = 0;
            }

            while (result == 0)
            {
                if (remaining != 0)
                {
                    count = Math.Min(count, remaining);
                    Util.MemoryCopy(buffer, offset, IOBufIn, IOBufInIndex, count);
                    IOBufInIndex += count;
                    remaining    -= count;
                    result        = count;
                    break;
                }

                ReadLoop(line, 4);
                tag = (int)IVAL(line, 0);

                remaining = tag & 0xFFFFFF;
                tag       = (tag >> 24) - MPLEX_BASE;

                switch ((MsgCode)tag)
                {
                case MsgCode.MSG_DATA:
                    if (remaining > IOBufInSize)
                    {
                        MapFile.ExtendArray(ref IOBufIn, remaining);
                        IOBufInSize = remaining;
                    }
                    ReadLoop(IOBufIn, remaining);
                    IOBufInIndex = 0;
                    break;

                case MsgCode.MSG_INFO:
                case MsgCode.MSG_ERROR:
                    if (remaining >= line.Length)
                    {
                        throw new Exception("Multiplexing overflow " + tag + ":" + remaining);
                    }
                    ReadLoop(line, remaining);
                    remaining = 0;
                    break;

                default:
                    throw new Exception("Read unknown message from stream");
                }
            }

            if (remaining == 0)
            {
                Flush();
            }

            return(result);
        }
Пример #3
0
        /// <summary>
        /// Reads socket into buffer
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public int ReadSocketUnbuffered(byte[] buffer, int offset, int count)
        {
            int tag, result = 0;
            var line = new byte[1024];

            if (_ioBufIn == null)
            {
                return(_socketIn.Read(buffer, offset, count));
            }

            if (!_ioMultiplexingIn && _remaining == 0)
            {
                Flush();
                _remaining    = _socketIn.Read(_ioBufIn, 0, _ioBufInSize);
                _ioBufInIndex = 0;
            }

            while (result == 0)
            {
                if (_remaining != 0)
                {
                    count = Math.Min(count, _remaining);
                    Util.MemoryCopy(buffer, offset, _ioBufIn, _ioBufInIndex, count);
                    _ioBufInIndex += count;
                    _remaining    -= count;
                    result         = count;
                    break;
                }

                ReadLoop(line, 4);
                tag = (int)Ival(line, 0);

                _remaining = tag & 0xFFFFFF;
                tag        = (tag >> 24) - MplexBase;

                switch ((MsgCode)tag)
                {
                case MsgCode.MsgData:
                    if (_remaining > _ioBufInSize)
                    {
                        MapFile.ExtendArray(ref _ioBufIn, _remaining);
                        _ioBufInSize = _remaining;
                    }
                    ReadLoop(_ioBufIn, _remaining);
                    _ioBufInIndex = 0;
                    break;

                case MsgCode.MsgInfo:
                case MsgCode.MsgError:
                    if (_remaining >= line.Length)
                    {
                        throw new Exception("Multiplexing overflow " + tag + ":" + _remaining);
                    }
                    ReadLoop(line, _remaining);
                    _remaining = 0;
                    break;

                default:
                    throw new Exception("Read unknown message from stream");
                }
            }

            if (_remaining == 0)
            {
                Flush();
            }

            return(result);
        }