Skip to content

Lock database on ScreenSaver/SessionLock#545

Merged
phoerious merged 13 commits intodevelopfrom
feature/lock-db-on-session-lock-#134
May 17, 2017
Merged

Lock database on ScreenSaver/SessionLock#545
phoerious merged 13 commits intodevelopfrom
feature/lock-db-on-session-lock-#134

Conversation

@TheZ3ro
Copy link
Copy Markdown
Contributor

@TheZ3ro TheZ3ro commented May 4, 2017

Description

This is a rebase of #135 by @rockdreamer

  • Added a configuration entry in security settings called "Close database when session is locked or lid is closed"
  • Added a class called ScreenLockListener with a private implementation class backed by three platform specific implementations:
    • on Mac OS the implementation uses the NSNotificationCenter C API
    • on Linux the implementation listens to DBus signals
    • on Windows the implementation registers for platform specific messages and registers a platform message handler

Motivation and Context

Implementation of issue #134

How Has This Been Tested?

I have tested on the following platforms:

  • Mac OS Sierra, Qt 5.7 from online installer, XCode 8, physical laptop: Lid close triggers lock
  • Ubuntu 16.10, Qt 5.7 from online installer, on Unity desktop, virtual machine: Session lock triggers lock
  • Windows 7, Qt 5.7 from online installer with libraries from MSYS2, virtual machine: Session lock triggers lock

Types of changes

  • ✅ New feature (non-breaking change which adds functionality)

This change brings in the following new dependencies:

  • Mac OS: Foundation framework (always present on the platform), the build requires XCode 8
  • Linux: Qt::DBus (available on most Linux distributions nowadays)
  • Windows: wtsapi32.lib (part of the platform SDK)

Checklist:

  • ✅ I have read the CONTRIBUTING document. [REQUIRED]
  • ✅ My code follows the code style of this project. [REQUIRED]
  • ✅ All new and existing tests passed. [REQUIRED]
  • ✅ I have compiled and verified my code with -DWITH_ASAN=ON. [REQUIRED]

@TheZ3ro TheZ3ro added this to the v2.2.0 milestone May 4, 2017
@TheZ3ro TheZ3ro self-assigned this May 4, 2017
@TheZ3ro
Copy link
Copy Markdown
Contributor Author

TheZ3ro commented May 4, 2017

This needs test on Unity, Gnome, OSX (@weslly) and Windows. I've tested on Xfce and works fine

@TheZ3ro TheZ3ro requested review from droidmonkey, louib and phoerious May 4, 2017 21:25
@weslly
Copy link
Copy Markdown
Contributor

weslly commented May 4, 2017

On OSX it works fine when locking the screen from keychain access or after waking up from sleep but not when I use the fast user switching menu.

Edit: Actually it seems when you use the fast user switching menu you aren't locking your session, even though you still need to type the user password to come back, so it still will work in most cases (when the user closes the lid or puts the computer to sleep).

Q_OBJECT

public:
ScreenLockListener(QWidget* parent=NULL);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this NULL be nullptr instead?

(Same with other files)

@Keisial
Copy link
Copy Markdown

Keisial commented May 4, 2017

Rather than a checkbox, I would consider allowing a delay (eg. waiting another couple of minutes) for db locking after screen lock. Maybe unneeded after #488, though.

@sainaen
Copy link
Copy Markdown

sainaen commented May 6, 2017

Hey! I just tried this on Gnome 3 (specifically 3.22.3-3 on Debian x64 Testing/Sid) with manual lock via menu and just closing the lid. Everything seems to work as expected.

But I noticed that this patch adds two checkboxes:

The first one is in the weird place (outside of the tab widget) and isn't connected to anything. Probably is just an artifact of rebase.


void ScreenLockListenerDBus::gnomeSessionStatusChanged(uint status)
{
if (status != 0) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheZ3ro ok so from what I can see from gnome's doc :

The status parameter must be one of the following:

0: Available
1: Invisible
2: Busy
3: Idle

Should we lock for all of the above statuses except for Available (Busy, for instance).

Also, We could extract 0 as a constant to make it more clear why we are ignoring this status : if (status != gnomeStatusAvailable)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmmh yes, I think I can do that

}
}

void ScreenLockListenerDBus::logindPrepareForSleep(bool beforeSleep)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheZ3ro why logind?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is because org.freedesktop.login1 is the DBus service for logind daemon

</widget>
</item>
<item row="15" column="0">
<widget class="QCheckBox" name="autoCloseOnScreenLockCheckBox">
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheZ3ro where is this used?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checkbox is appearing OUTSIDE the basic settings view. I think this should really be under the Security Settings anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I forgot to remove this after the cherry-pick as this was added by the previous (old) commit

</widget>
</item>
<item row="15" column="0">
<widget class="QCheckBox" name="autoCloseOnScreenLockCheckBox">
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checkbox is appearing OUTSIDE the basic settings view. I think this should really be under the Security Settings anyway.

@TheZ3ro
Copy link
Copy Markdown
Contributor Author

TheZ3ro commented May 6, 2017

@Keisial

Rather than a checkbox, I would consider allowing a delay (eg. waiting another couple of minutes) for db locking after screen lock. Maybe unneeded after #488, though.

We already have an option to lock the database after a delay so this will be partially useless

@sainaen Thanks for the test!
Yes, the option in the General settings is due to the rebase, I've forgot to remove that. I will fix asap

Copy link
Copy Markdown
Member

@droidmonkey droidmonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve for Windows

* See https://msdn.microsoft.com/en-us/library/aa383841(v=vs.85).aspx
* See https://blogs.msdn.microsoft.com/oldnewthing/20060104-50/?p=32783
*/
ScreenLockListenerWin::ScreenLockListenerWin(QWidget *parent)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheZ3ro QWidget* parent

HPOWERNOTIFY hPnotify = RegisterPowerSettingNotification(
reinterpret_cast<HWND>(parent->winId()),
&GUID_LIDSWITCH_STATE_CHANGE, DEVICE_NOTIFY_WINDOW_HANDLE);
m_powernotificationhandle = reinterpret_cast<void*>(hPnotify);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheZ3ro m_powerNotificationHandle

bool ScreenLockListenerWin::nativeEventFilter(const QByteArray &eventType, void *message, long *)
{
if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") {
MSG* m = static_cast<MSG *>(message);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static_cast<MSG*>

@louib
Copy link
Copy Markdown
Member

louib commented May 11, 2017

I tested the feature on Mac, it works great so far! I'll give it a try with Unity as soon as I can.

@louib
Copy link
Copy Markdown
Member

louib commented May 12, 2017

@TheZ3ro tested with Unity, also working great!

Copy link
Copy Markdown
Member

@phoerious phoerious left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works on Arch with KDE. Just a few little style things need fixing.

return m_ptr;
}

void ScreenLockListenerMac::notificationCenterCallBack(CFNotificationCenterRef, void*,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add parameter names, even though they are not used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this brings up some unused warning? Should I use Q_UNUSED?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The names are defined in the header file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know. It's just a style thing.

#if defined(Q_OS_OSX)
#include "ScreenLockListenerMac.h"
#endif
#if defined(Q_OS_LINUX)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#elif


void ScreenLockListenerMac::onSignalReception()
{
Q_EMIT screenLocked();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emit

public:
explicit ScreenLockListenerDBus(QWidget *parent = 0);

private Q_SLOTS:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slots

ScreenLockListener(QWidget* parent = nullptr);
~ScreenLockListener();

Q_SIGNALS:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

signals

void ScreenLockListenerDBus::gnomeSessionStatusChanged(uint status)
{
if (status != 0) {
Q_EMIT screenLocked();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emit

protected:
ScreenLockListenerPrivate(QWidget* parent = 0);

Q_SIGNALS:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

signals

if (setting != nullptr && setting->PowerSetting == GUID_LIDSWITCH_STATE_CHANGE) {
const DWORD* state = reinterpret_cast<const DWORD*>(&setting->Data);
if (*state == 0) {
Q_EMIT screenLocked();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emit

Copy link
Copy Markdown
Contributor

@weslly weslly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works on macOS Sierra

@TheZ3ro TheZ3ro force-pushed the feature/lock-db-on-session-lock-#134 branch from 16e695c to b69b50c Compare May 17, 2017 11:04
@TheZ3ro
Copy link
Copy Markdown
Contributor Author

TheZ3ro commented May 17, 2017

Fixed and rebased @keepassxreboot/core-developers

@phoerious phoerious merged commit c69a978 into develop May 17, 2017
@phoerious phoerious deleted the feature/lock-db-on-session-lock-#134 branch May 17, 2017 20:43
@droidmonkey
Copy link
Copy Markdown
Member

For the record, this PR introduces a dbus interface to KeePassXC.

@droidmonkey droidmonkey mentioned this pull request May 18, 2017
droidmonkey added a commit that referenced this pull request Jun 25, 2017
- Added YubiKey 2FA integration for unlocking databases [#127]
- Added TOTP support [#519]
- Added CSV import tool [#146, #490]
- Added KeePassXC CLI tool [#254]
- Added diceware password generator [#373]
- Added support for entry references [#370, #378]
- Added support for Twofish encryption [#167]
- Enabled DEP and ASLR for in-memory protection [#371]
- Enabled single instance mode [#510]
- Enabled portable mode [#645]
- Enabled database lock on screensaver and session lock [#545]
- Redesigned welcome screen with common features and recent databases [#292]
- Multiple updates to search behavior [#168, #213, #374, #471, #603, #654]
- Added auto-type fields {CLEARFIELD}, {SPACE}, {{}, {}} [#267, #427, #480]
- Fixed auto-type errors on Linux [#550]
- Prompt user prior to executing a cmd:// URL [#235]
- Entry attributes can be protected (hidden) [#220]
- Added extended ascii to password generator [#538]
- Added new database icon to toolbar [#289]
- Added context menu entry to empty recycle bin in databases [#520]
- Added "apply" button to entry and group edit windows [#624]
- Added macOS tray icon and enabled minimize on close [#583]
- Fixed issues with unclean shutdowns [#170, #580]
- Changed keyboard shortcut to create new database to CTRL+SHIFT+N [#515]
- Compare window title to entry URLs [#556]
- Implemented inline error messages [#162]
- Ignore group expansion and other minor changes when making database "dirty" [#464]
- Updated license and copyright information on souce files [#632]
- Added contributors list to about dialog [#629]
@sergeevabc
Copy link
Copy Markdown

sergeevabc commented Apr 4, 2018

I asked myself “what is lid” and ended up here.
For desktop users this phrase might sound confusing.
Let’s think what exactly happens “when lid is closed”?
Actually the computer is about to be suspended.
So it would be appreciated if you change the label appropriately to relate both to laptops and desktops.

@droidmonkey
Copy link
Copy Markdown
Member

Please open a new issue for this.

@Bendodroid
Copy link
Copy Markdown

This doesn't seem to work with i3lock on Manjaro. When unlocking the screen, the Database is still unlocked.
Any suggestions?

@droidmonkey
Copy link
Copy Markdown
Member

@Bendodroid please open an issue with all the details, thank you.

@kiprasmel
Copy link
Copy Markdown

kiprasmel commented Jun 29, 2019

Same here as @Bendodroid - I'll create an issue soon.
Edit: #3339

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: new feature Pull request adds a new feature security

Projects

None yet

Development

Successfully merging this pull request may close these issues.