Exemplo n.º 1
0
        /// <summary>
        /// Verifies if the password entered is correct
        /// </summary>
        /// <param name="pIsLocalConnection">Local connection flag</param>
        /// <param name="pUsername">Username</param>
        /// <param name="pPassword">Password</param>
        /// <param name="pUserID">Returns the user-ID</param>
        /// <param name="pDBData">DB connection data</param>
        /// <returns>True if the authentication was successfull</returns>
        public static bool VerifyPassword(bool pIsLocalConnection, string pUsername, string pPassword, out string pUserID, WrapMySQLData pDBData)
        {
            pUserID = "";

            bool passwordValid = false;

            if (pIsLocalConnection)
            {
                bool errorEncountered = false;
                using (WrapSQLite sqlite = new WrapSQLite(QDInfo.ConfigFile))
                {
                    try
                    {
                        if (!QDLib.ManagedDBOpen(sqlite))
                        {
                            QDLib.DBOpenFailed(); return(false);
                        }
                        string dbUsername = sqlite.ExecuteScalar <string>("SELECT QDValue FROM qd_info WHERE QDKey = ?", QDInfo.DBL.DefaultUsername);
                        string dbCipher   = sqlite.ExecuteScalar <string>("SELECT QDValue FROM qd_info WHERE QDKey = ?", QDInfo.DBL.DefaultPassword);
                        sqlite.Close();

                        string pwDecrypt = Cipher.Decrypt(dbCipher, QDInfo.LocalCipherKey);
                        if (dbUsername == pUsername && pwDecrypt == pPassword)
                        {
                            passwordValid = true;
                        }
                    }
                    catch
                    {
                        errorEncountered = true;
                    }
                }

                if (errorEncountered)
                {
                    MessageBox.Show("An error occured whilst trying to authenticate the user.", "Authentication error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                using (WrapMySQL mysql = new WrapMySQL(pDBData))
                {
                    if (!QDLib.ManagedDBOpen(mysql))
                    {
                        QDLib.DBOpenFailed(); return(false);
                    }
                    using (MySqlDataReader reader = (MySqlDataReader)mysql.ExecuteQuery("SELECT * FROM qd_users WHERE Username = ? AND Password = ?", pUsername, QDLib.HashPassword(pPassword)))
                    {
                        while (reader.Read())
                        {
                            pUserID       = Convert.ToString(reader["ID"]);
                            passwordValid = true;
                        }
                    }
                    mysql.Close();
                }
            }

            return(passwordValid);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Loggs user-actions to the online log
        /// </summary>
        /// <param name="pUserID">User-ID</param>
        /// <param name="pLogAction">Log Action</param>
        /// <param name="pDBData">DB connection data</param>
        /// <param name="pLogUserActionAllowed">Global flag to allow or deny the logging of data</param>
        public static void LogUserConnection(string pUserID, QDLogAction pLogAction, WrapMySQLData pDBData, bool pLogUserActionAllowed)
        {
            if (string.IsNullOrEmpty(pUserID))
            {
                return;
            }

            try
            {
                using (WrapMySQL mysql = new WrapMySQL(pDBData))
                {
                    string deviceID;
                    string deviceMac = GetMACAddress();

                    if (!QDLib.ManagedDBOpen(mysql))
                    {
                        QDLib.DBOpenFailed(); return;
                    }
                    mysql.TransactionBegin();

                    try
                    {
                        // Add device to DB if it doesn't exist. Get device ID
                        if (mysql.ExecuteScalar <int>("SELECT COUNT(*) FROM qd_devices WHERE MacAddress = ? AND LogonName = ? AND DeviceName = ?", deviceMac, Environment.UserName, Environment.MachineName) == 0)
                        {
                            deviceID = Guid.NewGuid().ToString();
                            mysql.ExecuteNonQuery("INSERT INTO qd_devices (ID, MacAddress, LogonName, DeviceName) VALUES (?,?,?,?)",
                                                  deviceID,
                                                  deviceMac,
                                                  Environment.UserName,
                                                  Environment.MachineName
                                                  );
                        }
                        else
                        {
                            deviceID = mysql.ExecuteScalar <string>("SELECT ID FROM qd_devices WHERE MacAddress = ? AND LogonName = ? AND DeviceName = ?", deviceMac, Environment.UserName, Environment.MachineName);
                        }

                        // Log device action.
                        if (pLogUserActionAllowed)
                        {
                            mysql.ExecuteNonQuery("INSERT INTO qd_conlog (ID, UserID, DeviceID, LogTime, LogAction) VALUES (?,?,?, NOW() ,?)",
                                                  Guid.NewGuid(),
                                                  pUserID,
                                                  deviceID,
                                                  pLogAction
                                                  );
                        }

                        mysql.TransactionCommit();
                    }
                    catch
                    {
                        mysql.TransactionRollback();
                    }

                    mysql.Close();
                }
            }
            catch { }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Connect to all Network drives of a specified QD-User
        /// </summary>
        /// <param name="pUserID">User-ID of the user</param>
        /// <param name="pUserPassword">User-password</param>
        /// <param name="pDBData">DB connection data</param>
        /// <param name="pLogUserData">Log user data</param>
        /// <param name="pDisconnectFirst">Disconnect all drives before reconecting</param>
        /// <param name="drives">Drive-List</param>
        /// <param name="ConnectOnlyIfNotAvailable">Only reconnect to a drive if it isn't already connected</param>
        /// <returns></returns>
        public static int ConnectQDDrives(string pUserID, string pUserPassword, WrapMySQLData pDBData, bool pLogUserData, bool pDisconnectFirst = true, List <DriveViewItem> drives = null, bool ConnectOnlyIfNotAvailable = false)
        {
            int connectCtr = 0;

            // Disconnect all current drives
            if (pDisconnectFirst)
            {
                DisconnectAllDrives(drives);
                if (!string.IsNullOrEmpty(pUserID))
                {
                    LogUserConnection(pUserID, QDLogAction.QDDrivesDisconnect, pDBData, pLogUserData);
                }
            }

            // Connect online-drives (online-synced)
            if (!string.IsNullOrEmpty(pUserID))
            {
                try
                {
                    using (WrapMySQL sql = new WrapMySQL(pDBData))
                    {
                        if (!QDLib.ManagedDBOpen(sql))
                        {
                            QDLib.DBOpenFailed(); return(-1);
                        }
                        // Connect local network drives
                        using (MySqlDataReader reader = (MySqlDataReader)sql.ExecuteQuery("SELECT * FROM qd_drives INNER JOIN qd_assigns ON qd_drives.ID = qd_assigns.DriveID INNER JOIN qd_users ON qd_assigns.UserID = qd_users.ID WHERE qd_assigns.UserID = ?", pUserID))
                        {
                            while (reader.Read())
                            {
                                try
                                {
                                    if (!ConnectOnlyIfNotAvailable || (ConnectOnlyIfNotAvailable && !Directory.Exists($@"{Convert.ToChar(reader["CustomDriveLetter"])}:\")))
                                    {
                                        //MessageBox.Show("Try to connect " + Convert.ToString(reader["CustomDriveName"]));

                                        ConnectDrive(
                                            Convert.ToChar(reader["CustomDriveLetter"]),
                                            Convert.ToString(reader["LocalPath"]),
                                            Cipher.Decrypt(Convert.ToString(reader["DUsername"]), pUserPassword),
                                            Cipher.Decrypt(Convert.ToString(reader["DPassword"]), pUserPassword),
                                            Convert.ToString(reader["CustomDriveName"]),
                                            Cipher.Decrypt(Convert.ToString(reader["DDomain"]), pUserPassword)
                                            );

                                        connectCtr++;
                                    }
                                }
                                catch
                                {
                                    return(5);
                                }
                            }
                        }

                        sql.Close();

                        // Conenct remote network drives
                        // TODO
                    }
                }
                catch
                {
                    return(4);
                }
            }


            // Connect Private drives (not online-synced)
            try
            {
                if (!File.Exists(QDInfo.ConfigFile))
                {
                    return(1);
                }

                using (WrapSQLite sqlite = new WrapSQLite(QDInfo.ConfigFile))
                {
                    if (!QDLib.ManagedDBOpen(sqlite))
                    {
                        QDLib.DBOpenFailed(); return(-1);
                    }
                    // Connect local network drives
                    using (SQLiteDataReader reader = (SQLiteDataReader)sqlite.ExecuteQuery("SELECT * FROM qd_drives"))
                    {
                        while (reader.Read())
                        {
                            try
                            {
                                if (!ConnectOnlyIfNotAvailable || (ConnectOnlyIfNotAvailable && Directory.Exists($@"{Convert.ToChar(reader["CustomDriveLetter"])}:\")))
                                {
                                    //MessageBox.Show("Try to connect " + Convert.ToString(reader["CustomDriveName"]));

                                    ConnectDrive(
                                        Convert.ToChar(reader["DriveLetter"]),
                                        Convert.ToString(reader["LocalPath"]),
                                        Cipher.Decrypt(Convert.ToString(reader["Username"]), QDInfo.LocalCipherKey),
                                        Cipher.Decrypt(Convert.ToString(reader["Password"]), QDInfo.LocalCipherKey),
                                        Convert.ToString(reader["DriveName"]),
                                        Cipher.Decrypt(Convert.ToString(reader["Domain"]), QDInfo.LocalCipherKey)
                                        );

                                    connectCtr++;
                                }
                            }
                            catch
                            {
                                return(3);
                            }
                        }
                    }
                    sqlite.Close();

                    // Conenct remote network drives
                    // TODO
                }
            }
            catch
            {
                return(2);
            }

            // Log only if not local. Do not log if no drives connected
            if (!string.IsNullOrEmpty(pUserID) && connectCtr > 0)
            {
                LogUserConnection(pUserID, QDLogAction.QDDrivesConnect, pDBData, pLogUserData);
            }

            return(0);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a list with all drives of a specified user
        /// </summary>
        /// <param name="pIsLocalConnection">Determines if the connection is a local connection</param>
        /// <param name="pUserID">User-ID of the target user. Blank if local connection</param>
        /// <param name="pUserPassword">Password of the user</param>
        /// <param name="pDBConDat">DB connection data</param>
        /// <returns>Drive-List</returns>
        public static List <DriveViewItem> CreateDriveList(bool pIsLocalConnection, string pUserID, string pUserPassword, WrapMySQLData pDBConDat)
        {
            List <DriveViewItem> driveList = new List <DriveViewItem>();

            using (WrapSQLite sqlite = new WrapSQLite(QDInfo.ConfigFile))
            {
                if (!QDLib.ManagedDBOpen(sqlite))
                {
                    QDLib.DBOpenFailed(); return(null);
                }
                using (SQLiteDataReader reader = (SQLiteDataReader)sqlite.ExecuteQuery("SELECT * FROM qd_drives"))
                {
                    while (reader.Read())
                    {
                        driveList.Add(new DriveViewItem(
                                          Convert.ToString(reader["ID"]),
                                          Convert.ToString(reader["DriveName"]),
                                          Convert.ToString(reader["LocalPath"]),
                                          Convert.ToString(reader["DriveLetter"]),
                                          true,
                                          false,
                                          Cipher.Decrypt(Convert.ToString(reader["Username"]), QDInfo.LocalCipherKey),
                                          Cipher.Decrypt(Convert.ToString(reader["Password"]), QDInfo.LocalCipherKey),
                                          Cipher.Decrypt(Convert.ToString(reader["Domain"]), QDInfo.LocalCipherKey)
                                          ));
                    }
                }
                sqlite.Close();
            }

            if (!pIsLocalConnection)
            {
                try
                {
                    using (WrapMySQL mysql = new WrapMySQL(pDBConDat))
                    {
                        if (!QDLib.ManagedDBOpen(mysql))
                        {
                            QDLib.DBOpenFailed(); return(null);
                        }
                        using (MySqlDataReader reader = (MySqlDataReader)mysql.ExecuteQuery("SELECT *, qd_assigns.ID as AID, qd_drives.ID AS DID FROM qd_drives INNER JOIN qd_assigns ON qd_drives.ID = qd_assigns.DriveID WHERE qd_assigns.UserID = ?", pUserID))
                        {
                            while (reader.Read())
                            {
                                driveList.Add(new DriveViewItem(
                                                  Convert.ToString(reader["AID"]),
                                                  Convert.ToString(reader["CustomDriveName"]),
                                                  Convert.ToString(reader["LocalPath"]),
                                                  Convert.ToString(reader["CustomDriveLetter"]),
                                                  false,
                                                  Convert.ToBoolean(Convert.ToInt16(reader["IsPublic"])),
                                                  Cipher.Decrypt(Convert.ToString(reader["DUsername"]), pUserPassword),
                                                  Cipher.Decrypt(Convert.ToString(reader["DPassword"]), pUserPassword),
                                                  Cipher.Decrypt(Convert.ToString(reader["DDomain"]), pUserPassword),
                                                  Convert.ToString(reader["DID"])
                                                  ));
                            }
                        }
                        mysql.Close();
                    }
                }
                catch { }
            }

            driveList.Sort();

            return(driveList);
        }