From 2ea569c7ed70a40cccaaf32b3f8ea2e7127ac062 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 27 Jan 2012 15:44:45 +0100 Subject: Initial commit --- .gitignore | 2 + Idle.py | 47 ++++++++++++++++ LockWindow.py | 69 +++++++++++++++++++++++ Locker.py | 38 +++++++++++++ pam.py | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pylock.py | 51 +++++++++++++++++ unlock.ui | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 553 insertions(+) create mode 100644 .gitignore create mode 100644 Idle.py create mode 100644 LockWindow.py create mode 100644 Locker.py create mode 100644 pam.py create mode 100644 pylock.py create mode 100644 unlock.ui diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b5e2e8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*~ +__pycache__ diff --git a/Idle.py b/Idle.py new file mode 100644 index 0000000..38137ea --- /dev/null +++ b/Idle.py @@ -0,0 +1,47 @@ +import ctypes +import ctypes.util + +from gi.repository import GdkX11 + + +class XScreenSaverInfo(ctypes.Structure): + _fields_ = [ + ('window', ctypes.c_ulong), + ('state', ctypes.c_int), + ('kind', ctypes.c_int), + ('til_or_since', ctypes.c_ulong), + ('idle', ctypes.c_ulong), + ('eventMask', ctypes.c_ulong) + ] +XScreenSaverInfo_p = ctypes.POINTER(XScreenSaverInfo) + +display_p = ctypes.c_void_p +xid = ctypes.c_ulong +c_int_p = ctypes.POINTER(ctypes.c_int) + +libXsspath = ctypes.util.find_library('Xss') +if libXsspath == None: + raise OSError('libXss could not be found.') +libXss = ctypes.cdll.LoadLibrary(libXsspath) +libXss.XScreenSaverQueryExtension.argtypes = display_p, c_int_p, c_int_p +libXss.XScreenSaverAllocInfo.restype = XScreenSaverInfo_p +libXss.XScreenSaverQueryInfo.argtypes = (display_p, xid, XScreenSaverInfo_p) + +dpy_p = hash(GdkX11.x11_get_default_xdisplay()) +rootwindow = GdkX11.x11_get_default_root_xwindow() + +_event_basep = ctypes.c_int() +_error_basep = ctypes.c_int() +if libXss.XScreenSaverQueryExtension(ctypes.c_void_p(dpy_p), ctypes.byref(_event_basep), + ctypes.byref(_error_basep)) == 0: + raise OSError('XScreenSaver Extension not available on display.') + +xss_info_p = libXss.XScreenSaverAllocInfo() +if xss_info_p == None: + raise OSError('XScreenSaverAllocInfo: Out of Memory.') + +def getIdleSec(): + if libXss.XScreenSaverQueryInfo(dpy_p, rootwindow, xss_info_p) == 0: + return 0 + else: + return int(xss_info_p.contents.idle) / 1000 diff --git a/LockWindow.py b/LockWindow.py new file mode 100644 index 0000000..98b8e86 --- /dev/null +++ b/LockWindow.py @@ -0,0 +1,69 @@ +import sys + +from gi.repository import Gtk, Gdk, GObject, GLib + + +class LockWindow(Gtk.Window): + __gsignals__ = { + 'tryUnlock': (GObject.SIGNAL_RUN_FIRST, None, (str,)) + } + + def __init__(self): + Gtk.Window.__init__(self) + + bg = Gtk.Image.new_from_file("bg.svg") + self.add(bg) + + self.set_decorated(False) + self.set_skip_taskbar_hint(True) + self.set_skip_pager_hint(True) + self.set_keep_above(True) + self.fullscreen() + + builder = Gtk.Builder() + + if not builder.add_from_file("unlock.ui"): + sys.exit(1) + + self.unlockWindow = builder.get_object('unlock_window') + self.promptEntry = builder.get_object('prompt_entry') + self.messageLabel = builder.get_object('message_label') + self.logoutButton = builder.get_object('logout_button') + self.unlockButton = builder.get_object('unlock_button') + + self.unlockWindow.set_position(Gtk.WindowPosition.CENTER_ALWAYS) + self.unlockWindow.set_transient_for(self) + self.unlockWindow.set_modal(True) + + self.connect('delete-event', lambda w, e: True) + self.unlockWindow.connect('delete-event', lambda w, e: True) + + self.promptEntry.connect('activate', lambda w: self._tryUnlock()) + self.unlockButton.connect('clicked', lambda w: self._tryUnlock()) + + self.reset() + + def _tryUnlock(self): + self.promptEntry.set_sensitive(False) + GLib.idle_add(lambda: self.emit('tryUnlock', self.promptEntry.get_text())) + + def reset(self): + self.promptEntry.set_text('') + self.promptEntry.set_sensitive(True) + self.promptEntry.grab_focus() + self.messageLabel.set_label('') + + def setMessage(self, message): + self.messageLabel.set_label(message) + + def lock(self): + self.show_all() + self.unlockWindow.show_all() + + self.promptEntry.grab_focus() + + def unlock(self): + self.unlockWindow.hide() + self.hide() + + self.reset() diff --git a/Locker.py b/Locker.py new file mode 100644 index 0000000..7871307 --- /dev/null +++ b/Locker.py @@ -0,0 +1,38 @@ +from gi.repository import Gtk, GLib + +import Idle + + +class Locker(object): + def __init__(self, lockTimeout, doLock, doUnlock, logoutTimeout = 0, doLogout = None): + self.locked = False + self.lockTimeout = lockTimeout + self.logoutTimeout = logoutTimeout + self.doLock = doLock + self.doUnlock = doUnlock + self.doLogout = doLogout + + GLib.timeout_add_seconds(1, self.checkLock) + + def checkLock(self): + if self.locked: + return False + + idle = Idle.getIdleSec() + if (idle >= self.lockTimeout): + GLib.idle_add(self.lock) + return False + else: + return True + + def lock(self): + if not self.locked: + self.doLock() + self.locked = True + + def unlock(self): + if self.locked: + self.doUnlock() + self.locked = False + + GLib.timeout_add(100, self.checkLock) diff --git a/pam.py b/pam.py new file mode 100644 index 0000000..d72489d --- /dev/null +++ b/pam.py @@ -0,0 +1,170 @@ +# (c) 2007 Chris AtLee +# Licensed under the MIT license: +# http://www.opensource.org/licenses/mit-license.php +""" +PAM module for python + +Provides an authenticate function that will allow the caller to authenticate +a user against the Pluggable Authentication Modules (PAM) on the system. + +Implemented using ctypes, so no compilation is necessary. + +--- +modified to work [better] with python3.2, 2011-12-6, david ford, +i haven't paid any attention to making sure things work in python2. there may be +problems in my_conv() +--- +Just some encoding fixes for python3, 2012-01-27 Matthias Schiffer +""" + +import sys +import locale +if sys.version_info >= (3,): + py3k = True +else: + py3k = False + +from ctypes import CDLL, POINTER, Structure, CFUNCTYPE, cast, pointer, sizeof +from ctypes import c_void_p, c_uint, c_char_p, c_char, c_int +from ctypes import memmove +from ctypes.util import find_library + +class PamHandle(Structure): + """wrapper class for pam_handle_t""" + _fields_ = [ + ("handle", c_void_p) + ] + + def __init__(self): + Structure.__init__(self) + self.handle = 0 + +class PamMessage(Structure): + """wrapper class for pam_message structure""" + _fields_ = [ + ("msg_style", c_int), + ("msg", c_char_p), + ] + + def __repr__(self): + return "" % (self.msg_style, self.msg) + +class PamResponse(Structure): + """wrapper class for pam_response structure""" + _fields_ = [ + ("resp", c_char_p), + ("resp_retcode", c_int), + ] + + def __repr__(self): + return "" % (self.resp_retcode, self.resp) + +CONV_FUNC = CFUNCTYPE( + c_int, + c_int, + POINTER(POINTER(PamMessage)), + POINTER(POINTER(PamResponse)), + c_void_p) + +class PamConv(Structure): + """wrapper class for pam_conv structure""" + _fields_ = [ + ("conv", CONV_FUNC), + ("appdata_ptr", c_void_p) + ] + +# Various constants +PAM_PROMPT_ECHO_OFF = 1 +PAM_PROMPT_ECHO_ON = 2 +PAM_ERROR_MSG = 3 +PAM_TEXT_INFO = 4 + +LIBC = CDLL(find_library("c")) +LIBPAM = CDLL(find_library("pam")) + +CALLOC = LIBC.calloc +CALLOC.restype = c_void_p +CALLOC.argtypes = [c_uint, c_uint] + +PAM_START = LIBPAM.pam_start +PAM_START.restype = c_int +PAM_START.argtypes = [c_char_p, c_char_p, POINTER(PamConv), POINTER(PamHandle)] + +PAM_STRERROR = LIBPAM.pam_strerror +PAM_STRERROR.restype = c_char_p +PAM_STRERROR.argtypes = [POINTER(PamHandle), c_int] + +PAM_AUTHENTICATE = LIBPAM.pam_authenticate +PAM_AUTHENTICATE.restype = c_int +PAM_AUTHENTICATE.argtypes = [PamHandle, c_int] + +class pam(): + code = 0 + reason = None + + def __init__(self): + pass + + def authenticate(self, username, password, service='login'): + """username and password authenticate for the given service. + + Returns True for success, or False. self.code is the integer + value representing the numerice failure reason, or 0 on success. + self.reason is the textual reason. + + Python3 expects bytes() for ctypes inputs. This function will make + necessary conversions using the latin-1 coding. + + username: the username to authenticate + password: the password in plain text + service: the PAM service to authenticate against. + Defaults to 'login' """ + + @CONV_FUNC + def my_conv(n_messages, messages, p_response, app_data): + """Simple conversation function that responds to any + prompt where the echo is off with the supplied password""" + # Create an array of n_messages response objects + addr = CALLOC(n_messages, sizeof(PamResponse)) + p_response[0] = cast(addr, POINTER(PamResponse)) + for i in range(n_messages): + if messages[i].contents.msg_style == PAM_PROMPT_ECHO_OFF: + cs = c_char_p(password) + dst = CALLOC(sizeof(c_char_p), len(password)+1) + memmove(dst , cs, len(password)) + p_response.contents[i].resp = dst + p_response.contents[i].resp_retcode = 0 + return 0 + + # python3 ctypes prefers bytes, pretend everyone will be happy using latin-1 + if py3k: + if isinstance(username, str): + username = bytes(username, locale.getpreferredencoding()) + if isinstance(password, str): + password = bytes(password, locale.getpreferredencoding()) + if isinstance(service, str): + service = bytes(service, locale.getpreferredencoding()) + + handle = PamHandle() + conv = PamConv(my_conv, 0) + retval = PAM_START(service, username, pointer(conv), pointer(handle)) + + if retval != 0: + # This is not an authentication error, something has gone wrong starting up PAM + self.code = retval + self.reason = str(PAM_STRERROR(pointer(handle), retval), locale.getpreferredencoding()) + return False + + retval = PAM_AUTHENTICATE(handle, 0) + + if retval == 0: + # success + logic = True + else: + logic = False + + # store information to inform the caller why we failed + self.code = retval + self.reason = str(PAM_STRERROR(pointer(handle), retval), locale.getpreferredencoding()) + + return logic diff --git a/pylock.py b/pylock.py new file mode 100644 index 0000000..d42aa46 --- /dev/null +++ b/pylock.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +import signal +import sys +import os +import pwd + +from gi.repository import Gtk, Gdk + +from Locker import Locker +from LockWindow import LockWindow +import pam + + +def get_username(): + return pwd.getpwuid(os.getuid())[0] + + +theme = 'UzL-login' +timeout = 5 +username = get_username() + + +def handler(signum, frame): + Gtk.main_quit() + + +signal.signal(signal.SIGINT, handler) +signal.signal(signal.SIGTERM, handler) +signal.signal(signal.SIGQUIT, handler) + + +Gtk.Settings.get_default().set_property('gtk-theme-name', theme) + +window = LockWindow() +locker = Locker(timeout, lambda: window.lock(), lambda: window.unlock()) +pamAuth = pam.pam() + + +def tryUnlock(w, password): + if pamAuth.authenticate(username, password): + locker.unlock() + else: + window.reset() + window.setMessage(pamAuth.reason) + + return True + +window.connect('tryUnlock', tryUnlock) + +Gtk.main() diff --git a/unlock.ui b/unlock.ui new file mode 100644 index 0000000..d958485 --- /dev/null +++ b/unlock.ui @@ -0,0 +1,176 @@ + + + + + False + False + False + False + + + + 700 + True + True + False + + + True + False + 40 + + + True + False + siegel.png + + + False + True + 0 + + + + + True + False + 12 + 12 + + + True + False + [hostname] + + + + + + True + True + 1 + + + + + False + 6 + + + False + 6 + + + True + False + Password: + + + False + True + 0 + + + + + True + True + False + + False + False + + + + True + True + 1 + + + + + True + True + 0 + + + + + False + [message] + + + False + True + 1 + + + + + True + False + 20 + + + Unlock + False + True + True + True + False + + + + True + True + end + 1 + + + + + Logout + False + True + True + True + False + + + + True + True + end + 2 + + + + + True + True + 2 + + + + + False + True + 3 + + + + + False + True + 1 + + + + + + + + + + + -- cgit v1.2.3