/// <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); }
private void UpdateInfoData() { if (dgvActionBrowser.Rows.Count <= 0) { return; } if (dgvActionBrowser.SelectedRows.Count <= 0) { return; } string conlogID = dgvActionBrowser.SelectedRows[0].Cells["ID"].Value.ToString(); string sqlQuery = $"SELECT *, " + $"(SELECT COUNT(*) FROM qd_assigns WHERE qd_assigns.UserID = qd_conlog.UserID) AS AssignedDriveCount, " + $"(SELECT COUNT(*) FROM (SELECT * FROM qd_conlog GROUP BY qd_conlog.UserID) AS TMP WHERE TMP.DeviceID = qd_devices.ID) AS UserCount " + $"FROM qd_conlog " + $"INNER JOIN qd_users ON qd_conlog.UserID = qd_users.ID " + $"INNER JOIN qd_devices ON qd_conlog.DeviceID = qd_devices.ID " + $"WHERE qd_conlog.ID = ?"; using (WrapMySQL mysql = new WrapMySQL(DBData)) { if (!QDLib.ManagedDBOpen(mysql)) { QDLib.DBOpenFailed(); return; } using (MySqlDataReader reader = (MySqlDataReader)mysql.ExecuteQuery(sqlQuery, conlogID)) { while (reader.Read()) { lblDateTime.Text = Convert.ToString(reader["LogTime"]); lblActionType.Text = Convert.ToString((QDLogAction)Convert.ToInt32(reader["LogAction"])); lblActionDescription.Text = QDLib.GetLogDescriptionFromAction((QDLogAction)Convert.ToInt32(reader["LogAction"])); lblDisplayName.Text = Convert.ToString(reader["Name"]); lblUsername.Text = Convert.ToString(reader["Username"]); lblAssignedDrives.Text = Convert.ToString(reader["AssignedDriveCount"]); lblDeviceName.Text = Convert.ToString(reader["DeviceName"]); lblLogonName.Text = Convert.ToString(reader["LogonName"]); lblMacAddress.Text = Convert.ToString(reader["MacAddress"]); lblUserCount.Text = QDLib.UserCountAtDevice(Convert.ToString(reader["DeviceID"]), DBData).ToString(); } } mysql.Close(); } }
/// <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); }
/// <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); }
private void UpdateDatagrid() { QDLoader qdLoader = new QDLoader(); qdLoader.Show(); BrowserSort sort = BrowserSort.None; if (!QDLib.ManagedDBOpen(mysql)) { QDLib.DBOpenFailed(); qdLoader.Close(); return; } totalEntryCount = mysql.ExecuteScalar <int>("SELECT COUNT(*) FROM qd_conlog"); bool abort = false; if (string.IsNullOrEmpty(SelectedObjectID)) { sort = BrowserSort.AllEntries; } else if (SelectedObjectID.StartsWith("ACT=")) { sort = BrowserSort.SortedByActionType; } else if (mysql.ExecuteScalar <int>("SELECT COUNT(*) FROM qd_users WHERE ID = ?", SelectedObjectID) != 0) { sort = BrowserSort.SortedByUsers; } else if (mysql.ExecuteScalar <int>("SELECT COUNT(*) FROM qd_devices WHERE ID = ?", SelectedObjectID) != 0) { sort = BrowserSort.SortedByDevice; } else { abort = true; } mysql.Close(); if (abort) { qdLoader.Close(); return; } switch (cbxEntryLimit.SelectedIndex) { case 0: listLimitSize = 50; break; case 1: listLimitSize = 100; break; case 2: listLimitSize = 250; break; case 3: listLimitSize = 500; break; case 4: listLimitSize = 1000; break; case 5: listLimitSize = 5000; break; default: listLimitSize = -1; break; } if (listOffset <= 0 || listLimitSize == -1) { btnJumpToFirst.Enabled = false; btnJumpToFirst.BackColor = Color.DarkGray; } else { btnJumpToFirst.Enabled = true; btnJumpToFirst.BackColor = Color.Gainsboro; } if (listOffset <= 0 || listLimitSize == -1) { btnJumpBack.Enabled = false; btnJumpBack.BackColor = Color.DarkGray; } else { btnJumpBack.Enabled = true; btnJumpBack.BackColor = Color.Gainsboro; } if (listOffset + listLimitSize > totalEntryCount || listLimitSize == -1) { btnJumpToNext.Enabled = false; btnJumpToNext.BackColor = Color.DarkGray; } else { btnJumpToNext.Enabled = true; btnJumpToNext.BackColor = Color.Gainsboro; } maxOffset = 0; while (listLimitSize + maxOffset < totalEntryCount) { maxOffset += listLimitSize; } if (listOffset >= maxOffset) { btnJumpToLast.Enabled = false; btnJumpToLast.BackColor = Color.DarkGray; } else { btnJumpToLast.Enabled = true; btnJumpToLast.BackColor = Color.Gainsboro; } string limitString; if (listLimitSize == -1) { limitString = ""; } else { limitString = $"LIMIT {listOffset},{listLimitSize}"; } string sqlQuery = ""; switch (sort) { case BrowserSort.AllEntries: sqlQuery = $"SELECT " + $"*, " + $"qd_conlog.ID AS MainID, " + $"CONCAT(qd_users.Name, ' (', qd_users.Username, ')') AS UserDisplay, " + $"CONCAT(qd_devices.LogonName, ' @ ', qd_devices.DeviceName) AS DeviceDisplay " + $"FROM qd_conlog " + $"INNER JOIN qd_users ON qd_conlog.UserID = qd_users.ID " + $"INNER JOIN qd_devices ON qd_conlog.DeviceID = qd_devices.ID " + $"ORDER BY LogTime DESC " + limitString; lblActionDescriptor.Text = "Showing all recorded actions."; break; case BrowserSort.SortedByActionType: sqlQuery = $"SELECT " + $"*, " + $"qd_conlog.ID AS MainID, " + $"CONCAT(qd_users.Name, ' (', qd_users.Username, ')') AS UserDisplay, " + $"CONCAT(qd_devices.LogonName, ' @ ', qd_devices.DeviceName) AS DeviceDisplay " + $"FROM qd_conlog " + $"INNER JOIN qd_users ON qd_conlog.UserID = qd_users.ID " + $"INNER JOIN qd_devices ON qd_conlog.DeviceID = qd_devices.ID " + $"WHERE qd_conlog.LogAction = ? " + $"ORDER BY LogTime DESC " + limitString; lblActionDescriptor.Text = $"Showing all recorded actions of type \"{(QDLogAction)Convert.ToInt32(SelectedObjectID.Replace("ACT=", ""))}\"."; break; case BrowserSort.SortedByDevice: sqlQuery = $"SELECT " + $"*, " + $"qd_conlog.ID AS MainID, " + $"CONCAT(qd_users.Name, ' (', qd_users.Username, ')') AS UserDisplay, " + $"CONCAT(qd_devices.LogonName, ' @ ', qd_devices.DeviceName) AS DeviceDisplay " + $"FROM qd_conlog " + $"INNER JOIN qd_users ON qd_conlog.UserID = qd_users.ID " + $"INNER JOIN qd_devices ON qd_conlog.DeviceID = qd_devices.ID " + $"WHERE qd_devices.ID = ? " + $"ORDER BY LogTime DESC " + limitString; lblActionDescriptor.Text = $"Showing all recorded actions of device \"{mysql.ExecuteScalarACon<string>("SELECT CONCAT(LogonName, ' @ ', DeviceName) FROM qd_devices WHERE ID = ?", SelectedObjectID)}\"."; break; case BrowserSort.SortedByUsers: sqlQuery = $"SELECT " + $"*, " + $"qd_conlog.ID AS MainID, " + $"CONCAT(qd_users.Name, ' (', qd_users.Username, ')') AS UserDisplay, " + $"CONCAT(qd_devices.LogonName, ' @ ', qd_devices.DeviceName) AS DeviceDisplay " + $"FROM qd_conlog " + $"INNER JOIN qd_users ON qd_conlog.UserID = qd_users.ID " + $"INNER JOIN qd_devices ON qd_conlog.DeviceID = qd_devices.ID " + $"WHERE qd_users.ID = ? " + $"ORDER BY LogTime DESC " + limitString; lblActionDescriptor.Text = $"Showing all recorded actions of user \"{mysql.ExecuteScalarACon<string>("SELECT CONCAT(Name, ' (', Username, ')') FROM qd_users WHERE ID = ?", SelectedObjectID)}\"."; break; } dgvActionBrowser.Rows.Clear(); if (!QDLib.ManagedDBOpen(mysql)) { QDLib.DBOpenFailed(); qdLoader.Close(); return; } using (MySqlDataReader reader = (MySqlDataReader)mysql.ExecuteQuery(sqlQuery, SelectedObjectID.Replace("ACT=", ""))) { while (reader.Read()) { dgvActionBrowser.Rows.Add(new string[] { Convert.ToString(reader["MainID"]), Convert.ToString(reader["LogTime"]), Convert.ToString(reader["UserDisplay"]), Convert.ToString(reader["DeviceDisplay"]), Convert.ToString((QDLogAction)Convert.ToInt32(reader["LogAction"])), }); } } mysql.Close(); lblResultRange.Text = $"Showing entries {listOffset + 1} to {listOffset + dgvActionBrowser.Rows.Count} ({totalEntryCount} entries in total)"; qdLoader.Close(); }
private void QDAddPublicDrive_Load(object sender, EventArgs e) { ImageList imgList = new ImageList() { ImageSize = new Size(60, 60), ColorDepth = ColorDepth.Depth32Bit, }; imgList.Images.Add("PublicUp", Properties.Resources.QDriveOnlinePublicUp); grvPublicDrives.SmallImageList = imgList; grvPublicDrives.GroupViewItems.Clear(); using (WrapMySQL sql = new WrapMySQL(DBData)) { if (!QDLib.ManagedDBOpen(sql)) { QDLib.DBOpenFailed(); return; } using (MySqlDataReader reader = (MySqlDataReader)sql.ExecuteQuery("SELECT * FROM qd_drives WHERE IsPublic = 1 ORDER BY DefaultDriveLetter ASC")) { while (reader.Read()) { grvPublicDrives.GroupViewItems.Add( new GroupViewItemEx( $"({Convert.ToString(reader["DefaultDriveLetter"])}:\\) {Convert.ToString(reader["DefaultName"])}\r\n({Convert.ToString(reader["LocalPath"])})", new DriveViewItem( Convert.ToString(reader["ID"]), Convert.ToString(reader["DefaultName"]), Convert.ToString(reader["LocalPath"]), Convert.ToString(reader["DefaultDriveLetter"]), false, true, "", "", "" ), 0 ) ); } } sql.Close(); } if (grvPublicDrives.GroupViewItems.Count == 0) { pbxNoDrivesFound.Visible = true; btnSubmit.Enabled = false; } else { pbxNoDrivesFound.Visible = false; } if (!string.IsNullOrEmpty(Username)) { txbUsername.Text = Username; } if (!string.IsNullOrEmpty(Password)) { txbPassword.Text = Password; } if (!string.IsNullOrEmpty(Domain)) { txbDomainName.Text = Domain; } if (ForceAutofill && !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password) && !string.IsNullOrEmpty(Domain)) { txbUsername.ReadOnly = true; txbPassword.ReadOnly = true; txbDomainName.ReadOnly = true; txbUsername.Enabled = false; txbPassword.Enabled = false; txbDomainName.Enabled = false; } // Set values for edit mode if (DBEntryID != null) { txbDisplayName.Text = CustomDriveName; txbUsername.Text = Username; txbPassword.Text = Password; txbDomainName.Text = Domain; for (int i = 0; i < cbxDriveLetter.Items.Count; i++) { if (cbxDriveLetter.Items[i].ToString()[0].ToString() == CustomDriveLetter) { cbxDriveLetter.SelectedIndex = i; } } for (int i = 0; i < grvPublicDrives.GroupViewItems.Count; i++) { if ((grvPublicDrives.GroupViewItems[i] as GroupViewItemEx).Drive.ID == DriveID) { grvPublicDrives.SelectedItem = i; txbDrivePath.Text = (grvPublicDrives.GroupViewItems[i] as GroupViewItemEx).Drive.DrivePath; } } this.Text = "Edit public drive"; btnSubmit.Text = "Update Drive"; } }