Example #1
0
        public RESTStatus GetFileList(SQLLib sql, object dummy, NetworkConnectionInfo ni)
        {
            if (ni.HasAcl(ACLFlags.ComputerLogin) == false)
            {
                ni.Error   = "Access denied";
                ni.ErrorID = ErrorFlags.AccessDenied;
                return(RESTStatus.Denied);
            }

            Int64List           = new NetInt64ListSigned();
            Int64List.data      = new NetInt64List2();
            Int64List.data.data = new List <long>();

            lock (ni.sqllock)
            {
                SqlDataReader dr = sql.ExecSQLReader("SELECT ID FROM FileTransfers WHERE MachineID=@mid AND ((Size=ProgressSize AND Direction=0) OR (Direction=1)) ORDER BY RequestOnly ASC, ID ASC",
                                                     new SQLParam("@mid", ni.Username));
                while (dr.Read())
                {
                    Int64List.data.data.Add(Convert.ToInt64(dr["ID"]));
                }
                dr.Close();
            }

            if (Certificates.Sign(Int64List, SettingsManager.Settings.UseCertificate) == false)
            {
                FoxEventLog.WriteEventLog("Cannot sign policy with Certificate " + SettingsManager.Settings.UseCertificate, System.Diagnostics.EventLogEntryType.Warning);
                ni.Error   = "Cannot sign policy with Certificate " + SettingsManager.Settings.UseCertificate;
                ni.ErrorID = ErrorFlags.CannotSign;
                return(RESTStatus.ServerError);
            }

            return(RESTStatus.Success);
        }
        public static bool Verify(NetInt64ListSigned data)
        {
            bool SignedOK = false;

            foreach (FilesystemCertificateData cer in FilesystemData.LoadedCertificates)
            {
                if (Certificates.Verify(data, cer.Certificate) == true)
                {
                    SignedOK = true;
                    break;
                }
            }

            return(SignedOK);
        }
        static void ThreadRunner()
        {
            lock (FileLock)
            {
                FilesystemData.LoadFileTransferStatus();
            }

            while (StopService == false)
            {
                try
                {
                    Network net = Utilities.ConnectNetwork(7);
                    if (net == null)
                    {
                        Wait(60);
                        continue;
                    }

                    NetInt64ListSigned lst = net.File_Agent_GetFileList();
                    if (lst == null)
                    {
                        net.CloseConnection();
                        Wait(2 * 60);
                        continue;
                    }

                    if (ApplicationCertificate.Verify(lst) == false)
                    {
                        FoxEventLog.WriteEventLog("One or more DownloadFS Lists are tampered - no download / uploads will be processed.", System.Diagnostics.EventLogEntryType.Error);
                        net.CloseConnection();
                        Wait(2 * 60);
                        continue;
                    }

                    if (lst.data.data.Count == 0)
                    {
                        net.CloseConnection();
                        Wait(2 * 60);
                        continue;
                    }

                    Int64 RunningID = 0;
                    lock (FileLock)
                    {
                        if (FilesystemData.FileTransferStatus.ServerID != null)
                        {
                            bool Found = false;
                            foreach (Int64 l in lst.data.data)
                            {
                                if (l == FilesystemData.FileTransferStatus.ServerID.Value)
                                {
                                    Found     = true;
                                    RunningID = l;
                                    break;
                                }
                            }

                            if (Found == false)
                            {
                                RunningID = lst.data.data[0];
                            }
                        }
                        else
                        {
                            RunningID = lst.data.data[0];
                        }
                    }

                    Debug.Assert(RunningID != 0);
                    FileUploadDataSigned fud = net.File_Agent_GetFileAnyData(RunningID);
                    if (ApplicationCertificate.Verify(fud) == false)
                    {
                        FoxEventLog.WriteEventLog("One or more DownloadFS Elements are tampered - no download / uploads will be processed.", System.Diagnostics.EventLogEntryType.Error);
                        net.CloseConnection();
                        Wait(2 * 60);
                        continue;
                    }

                    net.CloseConnection();

                    string LocalFilename   = "";
                    bool   WaitandContinue = false;

                    lock (FileLock)
                    {
                        //may be needed for deleting the file (if canceled by the server)
                        LocalFilename = FilesystemData.FileTransferStatus.RemoteFileLocation;

                        if (FilesystemData.FileTransferStatus.ServerID != null)
                        {
                            if (RunningID != FilesystemData.FileTransferStatus.ServerID)
                            {
                                CancelAndDeleteDL = true;
                            }
                            else
                            {
                                if (FilesystemData.FileTransferStatus.Size != fud.Data.Size ||
                                    FilesystemData.FileTransferStatus.RemoteFileLocation != fud.Data.RemoteFileLocation ||
                                    FilesystemData.FileTransferStatus.MD5CheckSum.ToLower() != fud.Data.MD5CheckSum.ToLower() ||
                                    FilesystemData.FileTransferStatus.RequestOnly != fud.Data.RequestOnly)
                                {
                                    CancelAndDeleteDL = true;
                                }
                                else
                                {
                                    if (RunningDLThread == null)
                                    {
                                        RunningDLThread = new Thread(new ThreadStart(DownloadThreadRunner));
                                        RunningDLThread.Start();
                                    }
                                    else
                                    {
                                        if (RunningDLThread.IsAlive == false)
                                        {
                                            RunningDLThread = new Thread(new ThreadStart(DownloadThreadRunner));
                                            RunningDLThread.Start();
                                        }
                                    }
                                }

                                WaitandContinue = true;
                            }
                        }
                    }

                    if (WaitandContinue == true)
                    {
                        Wait(2 * 60);
                        continue;
                    }

                    if (CancelAndDeleteDL == true)
                    {
                        if (RunningDLThread != null)
                        {
                            RunningDLThread.Join();
                        }

                        if (string.IsNullOrWhiteSpace(LocalFilename) == false)
                        {
                            try
                            {
                                if (FilesystemData.FileTransferStatus.Direction == 0)
                                {
                                    if (File.Exists(LocalFilename) == true)
                                    {
                                        File.Delete(LocalFilename);
                                    }
                                }
                            }
                            catch (Exception ee)
                            {
                                FoxEventLog.WriteEventLog("Cannot check / delete " + LocalFilename + "\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error);
                            }
                        }
                    }

                    CancelAndDeleteDL = false;

                    lock (FileLock)
                    {
                        FilesystemData.FileTransferStatus.Size = fud.Data.Size;
                        FilesystemData.FileTransferStatus.RemoteFileLocation        = fud.Data.RemoteFileLocation;
                        FilesystemData.FileTransferStatus.MD5CheckSum               = fud.Data.MD5CheckSum.ToLower();
                        FilesystemData.FileTransferStatus.OverrideMeteredConnection = fud.Data.OverrideMeteredConnection;
                        FilesystemData.FileTransferStatus.Direction    = fud.Data.Direction;
                        FilesystemData.FileTransferStatus.ProgressSize = 0;
                        FilesystemData.FileTransferStatus.ServerID     = RunningID;
                        FilesystemData.FileTransferStatus.RequestOnly  = fud.Data.RequestOnly;
                        FilesystemData.FileTransferStatus.LastModfied  = fud.Data.FileLastModified;
                        FilesystemData.WriteFileTransferStatus();
                    }

                    RunningDLThread = new Thread(new ThreadStart(DownloadThreadRunner));
                    RunningDLThread.Start();

                    Wait(2 * 60);
                    continue;
                }
                catch (Exception ee)
                {
                    FoxEventLog.WriteEventLog("Internal crash in DownloadFS System: " + ee.Message, System.Diagnostics.EventLogEntryType.Error);
                    Wait(10);
                }
                Wait(2 * 60);
            }
            ;
        }