Beispiel #1
0
        /// <summary>
        /// Insert new or update existing row
        /// </summary>
        /// <param name="data"></param>
        /// <param name="status"></param>
        private void UpdateDatabase(ClamWinScan.FilterNotifyData data, string status)
        {
            int    result  = 0;
            string command = "SELECT * FROM FilterNotifications WHERE Path = '" + data.FileName + "'";

            command += " AND Message = '" + data.Message + "' AND Time = '" + data.Time.ToBinary().ToString() + "';";

            ArrayList items;

            ClamWinDatabase.ExecReader(command, out items);

            if (items.Count == 0)
            {
                // Insert new item
                command  = "INSERT INTO FilterNotifications(Path,Message,Status,Time) VALUES(";
                command += "'" + data.FileName + "',";
                command += "'" + data.Message + "',";
                command += "'" + status + "',";
                command += "'" + data.Time.ToBinary().ToString() + "');";
                ClamWinDatabase.ExecCommand(command, out result);
            }
            else
            {
                // Update existing
                command  = "UPDATE FilterNotifications SET Status = '" + status;
                command += "' WHERE Path = '" + data.FileName + "' AND Message = '" + data.Message + "'";
                command += " AND Time = '" + data.Time.ToBinary().ToString() + "';";
                ClamWinDatabase.ExecCommand(command, out result);
            }
        }
        /// <summary>
        /// Remove current notification
        /// </summary>
        public static void CurrentNotifcationDone()
        {
            lock (MainLocker)
            {
                if (CurrentNotification < 0 || CurrentNotification >= Notifications.Count)
                {
                    return;
                }

                NotificationData data = (NotificationData)Notifications[CurrentNotification];

                string command = "DELETE FROM MainFormNotifications WHERE id = '" + data.ID.ToString() + "';";
                int    result;
                ClamWinDatabase.ExecCommand(command, out result);

                Notifications.RemoveAt(CurrentNotification);

                if (Notifications.Count == 0)
                {
                    CurrentNotification = -1;
                }
                else if (CurrentNotification >= Notifications.Count)
                {
                    CurrentNotification = Notifications.Count - 1;
                }

                PostCurrentChanged();
            }
        }
        /// <summary>
        /// Close and save notifications
        /// </summary>
        public static void Close()
        {
            lock (MainLocker)
            {
                string command = "DELETE FROM MainFormNotifications;";
                int    result;
                ClamWinDatabase.ExecCommand(command, out result);

                while (true)
                {
                    NotificationData data;
                    if (Notifications.Count == 0)
                    {
                        CurrentNotification = -1;
                        return;
                    }

                    data = (NotificationData)Notifications[0];
                    Notifications.RemoveAt(0);


                    command  = "INSERT INTO MainFormNotifications(Message,Code,Type,Time) VALUES(";
                    command += "'" + data.Message + "',";
                    command += "'" + ((int)data.Code).ToString() + "',";
                    command += "'" + ((int)data.Type).ToString() + "',";
                    command += "'" + data.Time.ToBinary().ToString() + "');";

                    ClamWinDatabase.ExecCommand(command, out result);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Clean-up statistics table
        /// </summary>
        /// <returns></returns>
        private static bool StatisticsTableCleanUp()
        {
            string command = "DELETE FROM Statistics;";

            int result;

            return(ClamWinDatabase.ExecCommand(command, out result));
        }
Beispiel #5
0
        /// <summary>
        /// Saves file info to database and removes file
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        private static bool PostCompressProcessing(string FileName, string QuarantineName)
        {
            long Size = 0;

            try
            {
                FileInfo fi = new FileInfo(QuarantineName);
                if (!fi.Exists)
                {
                    return(false);
                }

                fi = new FileInfo(FileName);

                if (!fi.Exists)
                {
                    return(false);
                }

                Size = fi.Length;
            }
            catch
            {
                return(false);
            }

            string command = "INSERT INTO QuarantineItems(InitialPath,QuarantinePath,QuarantineTime,Size) VALUES(";

            command += "'" + FileName + "',";
            command += "'" + QuarantineName + "',";
            command += "'" + DateTime.Now.ToBinary().ToString() + "',";
            command += "'" + Size.ToString() + "');";

            int result;

            ClamWinDatabase.ExecCommand(command, out result);

            // Let we ensure record has been inserted successfully
            command = "SELECT * FROM QuarantineItems WHERE InitialPath='" + FileName + "';";
            ArrayList list;

            if (ClamWinDatabase.ExecReader(command, out list))
            {
                if (list.Count != 0)
                {
                    RemoveFile(FileName);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
        /// <summary>
        /// Remove specified notification
        /// </summary>
        public static void NotificationDone(NotificationCodes code)
        {
            lock (MainLocker)
            {
                int CurrID = -1;
                if (CurrentNotification != -1)
                {
                    CurrID = ((NotificationData)Notifications[CurrentNotification]).ID;
                }

                NotificationData data = new NotificationData();
                data.ID = -1;

                bool ok = false;
                for (int i = 0; i < Notifications.Count; i++)
                {
                    data = (NotificationData)Notifications[i];

                    if (data.Code == code)
                    {
                        ok = true;
                        Notifications.RemoveAt(i);
                        break;
                    }
                }

                if (!ok)
                {
                    return;
                }

                string command = "DELETE FROM MainFormNotifications WHERE id = '" + data.ID.ToString() + "';";
                int    result;
                ClamWinDatabase.ExecCommand(command, out result);

                CurrentNotification = -1;
                for (int i = 0; i < Notifications.Count; i++)
                {
                    data = (NotificationData)Notifications[i];

                    if (data.ID == CurrID)
                    {
                        CurrentNotification = i;
                        break;
                    }
                }

                if (CurrentNotification == -1 && Notifications.Count != 0)
                {
                    CurrentNotification = Notifications.Count - 1;
                }

                PostCurrentChanged();
            }
        }
        /// <summary>
        /// Add new notifcation
        /// </summary>
        public static int AddNotification(string message, NotificationCodes code, NotificationType type)
        {
            lock (MainLocker)
            {
                for (int i = 0; i < Notifications.Count; i++)
                {
                    NotificationData item = (NotificationData)Notifications[i];
                    if (code == item.Code)
                    {
                        //Already exist
                        CurrentNotification = i;
                        PostCurrentChanged();

                        return(-1);
                    }
                }
                NotificationData data = new NotificationData();
                data.Message = message;
                data.Code    = code;
                data.Type    = type;
                data.Time    = DateTime.Now;

                string command = "INSERT INTO MainFormNotifications(Message,Code,Type,Time) VALUES(";
                command += "'" + data.Message + "',";
                command += "'" + ((int)data.Code).ToString() + "',";
                command += "'" + ((int)data.Type).ToString() + "',";
                command += "'" + data.Time.ToBinary().ToString() + "');";

                int result;
                ClamWinDatabase.ExecCommand(command, out result);

                command = "SELECT * FROM MainFormNotifications WHERE Time=(SELECT max(Time) FROM MainFormNotifications);";

                ArrayList Items;
                if (!ClamWinDatabase.ExecReader(command, out Items))
                {
                    return(-1);
                }
                else if (Items.Count == 0)
                {
                    return(-1);
                }

                data.ID = int.Parse((string)Items[0]);

                Notifications.Add(data);

                CurrentNotification = Notifications.Count - 1;

                PostCurrentChanged();

                return(data.ID);
            }
        }
        /// <summary>
        /// Opens notifications
        /// </summary>
        public static bool Open(IntPtr Handle)
        {
            string    command = "SELECT * FROM MainFormNotifications;";
            ArrayList Items;

            if (!ClamWinDatabase.ExecReader(command, out Items))
            {
                return(false);
            }

            if (Items.Count > 0)
            {
                const int FieldsPerRecord = ClamWinDatabase.MainFormNotificationsFPR;

                int RecordsCount = Items.Count / FieldsPerRecord;

                for (int i = 0; i < RecordsCount; i++)
                {
                    NotificationData Data = new NotificationData();
                    // [ID]
                    Data.ID = int.Parse((string)Items[i * FieldsPerRecord + 0]);
                    // [Message]
                    Data.Message = (string)Items[i * FieldsPerRecord + 1];
                    // [Code]
                    Data.Code = (NotificationCodes)int.Parse((string)Items[i * FieldsPerRecord + 2]);
                    // [Type]
                    Data.Type = (NotificationType)int.Parse((string)Items[i * FieldsPerRecord + 3]);
                    // [Time]
                    Data.Time = DateTime.FromBinary(long.Parse((string)Items[i * FieldsPerRecord + 4]));

                    lock (MainLocker)
                    {
                        Notifications.Add(Data);
                    }
                }
                lock (MainLocker)
                {
                    CurrentNotification = Notifications.Count - 1;
                }
            }

            lock (MainLocker)
            {
                MainFormHandle = Handle;
            }

            PostCurrentChanged();

            return(true);
        }
Beispiel #9
0
        /// <summary>
        /// Check exctracted file size and removes database recod
        /// </summary>
        /// <returns></returns>
        private static bool PostExtractProcessing(string CompressedFileName, string FileName)
        {
            // Let we ensure record has been inserted successfully
            string    command = "SELECT * FROM QuarantineItems WHERE InitialPath='" + FileName + "';";
            ArrayList list;

            if (ClamWinDatabase.ExecReader(command, out list))
            {
                if (list.Count != 0)
                {
                    long size = long.Parse((string)list[4]);
                    try
                    {
                        FileInfo fi = new FileInfo(FileName);
                        if (!fi.Exists)
                        {
                            return(false);
                        }

                        if (fi.Length != size)
                        {
                            return(false);
                        }
                    }
                    catch
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }

            command = "DELETE FROM QuarantineItems WHERE QuarantinePath='" + CompressedFileName + "';";
            int result;

            ClamWinDatabase.ExecCommand(command, out result);

            RemoveFile(CompressedFileName);

            return(true);
        }
Beispiel #10
0
        /// <summary>
        /// Save FileAntiVirus statistics to data base
        /// </summary>
        /// <returns>true - on success, false - if failed</returns>
        public static bool FlushFileAntiVirus()
        {
            lock (MainLock)
            {
                //string command = "INSERT INTO Statistics(Object,Scanned,Threats,Deleted,MovedToQuarantine,Archived,Packed,PasswordProtected,Corrupted,LastScanned,ScanID) VALUES(";
                string command     = "UPDATE Statistics SET Scanned = '" + FileAntiVirus.Scanned.ToString() + "', ";
                string LastScanned = FileAntiVirus.LastScanned.Replace("'", "''");
                command += "Threats = '" + FileAntiVirus.Threats.ToString() + "', ";
                command += "Deleted = '" + FileAntiVirus.Deleted.ToString() + "', ";
                command += "MovedToQuarantine = '" + FileAntiVirus.MovedToQuarantine.ToString() + "', ";
                command += "Archived = '" + FileAntiVirus.Archived.ToString() + "', ";
                command += "Packed = '" + FileAntiVirus.Packed.ToString() + "', ";
                command += "PasswordProtected = '" + FileAntiVirus.PasswordProtected.ToString() + "', ";
                command += "Corrupted = '" + FileAntiVirus.Corrupted.ToString() + "', ";
                command += "LastScanned = '" + LastScanned + "', ";
                command += "ScanID = '-1'";
                command += " WHERE Object = '" + FileAntiVirus.Object + "';";

                int result;
                ClamWinDatabase.ExecCommand(command, out result);

                if (result == 0)
                {
                    //UPDATE failed
                    command     = "INSERT INTO Statistics(Object,Scanned,Threats,Deleted,MovedToQuarantine,Archived,Packed,PasswordProtected,Corrupted,LastScanned,ScanID) VALUES(";
                    LastScanned = FileAntiVirus.LastScanned.Replace("'", "''");
                    command    += "'" + FileAntiVirus.Object + "',";
                    command    += "'" + FileAntiVirus.Scanned.ToString() + "',";
                    command    += "'" + FileAntiVirus.Threats.ToString() + "',";
                    command    += "'" + FileAntiVirus.Deleted.ToString() + "',";
                    command    += "'" + FileAntiVirus.MovedToQuarantine.ToString() + "',";
                    command    += "'" + FileAntiVirus.Archived.ToString() + "',";
                    command    += "'" + FileAntiVirus.Packed.ToString() + "',";
                    command    += "'" + FileAntiVirus.PasswordProtected.ToString() + "',";
                    command    += "'" + FileAntiVirus.Corrupted.ToString() + "',";
                    command    += "'" + LastScanned + "',";
                    command    += "'-1');";

                    if (!ClamWinDatabase.ExecCommand(command, out result))
                    {
                        return(false);
                    }
                }

                return(true);
            }
        }
Beispiel #11
0
        /// <summary>
        /// Fill quarantine items list with database records
        /// </summary>
        private void UpdateQuarantineItems()
        {
            listViewQuarantineItems.Items.Clear();

            //ListViewItem item = listViewQuarantineItems.Items

            string command = "SELECT * FROM QuarantineItems;";

            ArrayList list;

            ClamWinDatabase.ExecReader(command, out list);

            if (list.Count == 0)
            {
                return;
            }

            int FieldsPerRecord = ClamWinDatabase.QuarantineItemsFPR;
            int RecordsCount    = list.Count / FieldsPerRecord;

            for (int i = 0; i < RecordsCount; i++)
            {
                string   PathName           = (string)list[i * FieldsPerRecord + 1];
                string   QuarantinePathName = (string)list[i * FieldsPerRecord + 2];
                string   time = (string)list[i * FieldsPerRecord + 3];
                DateTime Time = DateTime.FromBinary(long.Parse(time));
                long     Size = long.Parse((string)list[i * FieldsPerRecord + 4]);

                if (!ClamWinQuarantine.CheckQuarantinedFile(QuarantinePathName, Size))
                {
                    continue;
                }


                string FileName = System.IO.Path.GetFileName(PathName);

                ListViewItem item = listViewQuarantineItems.Items.Add(FileName);
                item.SubItems.Add(PathName);
                item.SubItems.Add(Time.ToShortDateString() + " " + Time.ToShortTimeString());
                item.SubItems.Add(Size.ToString());
            }
        }
Beispiel #12
0
        /// <summary>
        /// Find out if file is quarantined
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public static bool IsFileInQuarantine(string FileName)
        {
            string command = "SELECT * FROM QuarantineItems WHERE InitialPath='" + FileName + "';";
            ArrayList list;
            if (ClamWinDatabase.ExecReader(command, out list))
            {
                if (list.Count == 0)
                {
                    return false;
                }
            }
            else
            {
                return false;
            }

            string QuarantineName = (string)list[2];

            return FileExists(QuarantineName);
        }
Beispiel #13
0
        /// <summary>
        /// Check quarantined  file
        /// </summary>
        /// <param name="FilePathName"></param>
        /// <returns></returns>
        public static bool CheckQuarantinedFile(string QuarantinePathName, long SavedSize)
        {
            try
            {
                FileInfo fi = new FileInfo(QuarantinePathName);
                if (!fi.Exists)
                {
                    throw new SystemException();
                }

                return(true);
            }
            catch
            {
            }

            // Something is wrong, perform database and quarantined file clean up

            string command = "DELETE FROM QuarantineItems WHERE QuarantinePath='" + QuarantinePathName + "';";
            int    result;

            ClamWinDatabase.ExecCommand(command, out result);

            try
            {
                FileInfo fi = new FileInfo(QuarantinePathName);

                if (fi.Exists)
                {
                    fi.Delete();
                }
            }
            catch
            {
            }


            return(false);
        }
Beispiel #14
0
        /// <summary>
        /// Load FileAntiVirus statistics from database
        /// </summary>
        /// <returns></returns>
        private static bool LoadFileAntiVirusStatistics()
        {
            lock (MainLock)
            {
                ArrayList list;
                string    command = "SELECT * FROM Statistics WHERE ScanID='-1' AND Object='FileAntiVirus';";
                ClamWinDatabase.ExecReader(command, out list);

                if (list.Count == ClamWinDatabase.StatisticsFPR)
                {
                    // [Object]
                    FileAntiVirus.Object = (string)list[1];
                    // [Scanned]
                    FileAntiVirus.Scanned = ulong.Parse((string)list[2]);
                    // [Threats]
                    FileAntiVirus.Threats = ulong.Parse((string)list[3]);
                    // [Deleted]
                    FileAntiVirus.Deleted = ulong.Parse((string)list[4]);
                    // [MovedToQuarantine]
                    FileAntiVirus.MovedToQuarantine = ulong.Parse((string)list[5]);
                    // [Archived]
                    FileAntiVirus.Archived = ulong.Parse((string)list[6]);
                    // [Packed]
                    FileAntiVirus.Packed = ulong.Parse((string)list[7]);
                    // [PasswordProtected]
                    FileAntiVirus.PasswordProtected = ulong.Parse((string)list[8]);
                    // [Corrupted]
                    FileAntiVirus.Corrupted = ulong.Parse((string)list[9]);
                    // [LastScanned]
                    FileAntiVirus.LastScanned = (string)list[10];

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Beispiel #15
0
        /// <summary>
        /// Cleanup current staitsitcs and delete records from "Statistics" table
        /// </summary>

        /*public static void Reset()
         * {
         *  lock (MainLock)
         *  {
         *      Items.Clear();
         *      StatisticItem item = new StatisticItem(TotalString);
         *      Items.Insert(0, item);
         *  }
         *
         *  StatisticsTableCleanUp();
         * }*/
        /// <summary>
        /// Save items to data base, and clear Items
        /// </summary>
        /// <returns>true - on success, false - if failed</returns>
        public static bool FlushItems(int ScanID)
        {
            lock (MainLock)
            {
                if (!Opened)
                {
                    return(false);
                }

                string ID = ScanID.ToString();
                foreach (StatisticItem item in Items)
                {
                    // Specified record not found , try to insert new record
                    string command = "INSERT INTO Statistics(Object,Scanned,Threats,Deleted,MovedToQuarantine,Archived,Packed,PasswordProtected,Corrupted,LastScanned,ScanID) VALUES(";
                    string Object  = item.Object.Replace("'", "''");
                    command += "'" + Object + "',";
                    command += "'" + item.Scanned.ToString() + "',";
                    command += "'" + item.Threats.ToString() + "',";
                    command += "'" + item.Deleted.ToString() + "',";
                    command += "'" + item.MovedToQuarantine.ToString() + "',";
                    command += "'" + item.Archived.ToString() + "',";
                    command += "'" + item.Packed.ToString() + "',";
                    command += "'" + item.PasswordProtected.ToString() + "',";
                    command += "'" + item.Corrupted.ToString() + "',";
                    command += "'" + item.LastScanned + "',";
                    command += "'" + ID + "');";

                    int result;
                    if (!ClamWinDatabase.ExecCommand(command, out result))
                    {
                        return(false);
                    }
                }
            }

            Items.Clear();

            return(true);
        }
Beispiel #16
0
        /// <summary>
        /// Loads undefined notifications
        /// </summary>
        private void LoadNotifications()
        {
            int    result  = 0;
            string command = "SELECT * FROM FilterNotifications WHERE Status = 'Undefined';";

            ArrayList list;

            ClamWinDatabase.ExecReader(command, out list);

            if (list.Count == 0)
            {
                return;
            }

            const int FieldsPerRecord = ClamWinDatabase.FilterNotificationsFPR;

            int RecordsCount = list.Count / FieldsPerRecord;

            FilterNotifications.Clear();

            for (int i = 0; i < RecordsCount; i++)
            {
                ClamWinScan.FilterNotifyData data = new ClamWinScan.FilterNotifyData();
                // [id]

                // [Path]
                data.FileName = list[i * FieldsPerRecord + 1].ToString();
                // [Message]
                data.Message = list[i * FieldsPerRecord + 2].ToString();
                // [Status]

                // [Time]
                data.Time = DateTime.FromBinary(long.Parse(list[i * FieldsPerRecord + 4].ToString()));

                AddNotifyData(data);
            }
        }
Beispiel #17
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="Listeners"></param>
        /// <returns></returns>
        public static QuarantineResults UnquarantineFile(string FileName, IntPtr[] Listeners, bool Temp)
        {
            lock (MainLock)
            {
                if (QueueLocked)
                {
                    return QuarantineResults.FailedQueueLocked;
                }
            }

            if (!IsFileInQuarantine(FileName))
            {
                return QuarantineResults.FailedFileIsNotInQuarantine;
            }

            long InitialSize = 0;
            string ComressedFileName = "";

            long id = 0;
            lock (MainLock)
            {
                foreach (QuarantineItem queued in ItemsQueue)
                {
                    if (queued.FileName == FileName && queued.Quarantine == false)
                    {
                        return QuarantineResults.FailedAlreadyQueued;
                    }
                }

                string command = "SELECT * FROM QuarantineItems WHERE InitialPath='" + FileName + "';";
                ArrayList list;
                if (ClamWinDatabase.ExecReader(command, out list))
                {
                    if (list.Count != 0)
                    {
                        InitialSize = long.Parse((string)list[4]);
                        ComressedFileName = (string)list[2];
                    }
                    else
                    {
                        return QuarantineResults.Failed;
                    }
                }
                else
                {
                    return QuarantineResults.Failed;
                }

                id = ItemsCounter++;
                QuarantineItem item = new QuarantineItem();
                item.FileName = Temp ? GetQuarantineTempFolder() + Path.GetFileName(FileName) : FileName;
                item.Listeners = Listeners;
                item.Quarantine = false;
                item.InitialSize = InitialSize;
                item.CompressedFileName = ComressedFileName;
                item.Temp = Temp;
                item.ID = id;

                ItemsQueue.Add(item);

                Win32API.SetEvent(NewItemAddedEvent);
            }

            NotifyData data = new NotifyData();
            data.FileName = FileName;
            data.Quarantine = false;
            data.Size = InitialSize;
            data.ID = id;

            SendQuarantineNotify(UM_QUARANTINE_QUEUE_NEW_ITEM, data, Listeners);

            return QuarantineResults.Success;
        }