#!/usr/bin/env python # from gi.repository import GObject from gi.repository import GLib from gi.repository import Gio from gi.repository import Gtk from gi.repository import Gdk from gi.repository import GdkPixbuf from gi.repository import LightDM import sys import platform import subprocess import pwd import time import os.path from optparse import OptionParser import ConfigParser import io KIOSK_LAUNCH_CMD="/usr/lib/debathena-kiosk/lightdm-launch-kiosk" PICKBOARD_CMD="/usr/bin/onboard" UI_FILE="/usr/share/debathena-lightdm-config/debathena-lightdm-greeter.ui" CONFIG_FILE="/etc/debathena-lightdm-greeter.ini" CONFIG_DEFAULTS={"minimum_uid": 1, "nologin_file": "/var/run/athena-nologin", "background_image": "/usr/share/debathena-lightdm-config/background.jpg", "base_logo_file": "/usr/share/debathena-lightdm-config/debathena.png", "extra_logo_frames": 8, "motd_filename": "/afs/athena.mit.edu/system/config/motd/login.debathena", "append_codename_to_motd": "true", "time_pedantry": "true"} # See below, and then go cry. _OBJS_TO_RENAME=["miShutdown", "miRestart", "miSuspend", "miHibernate"] class DebathenaGreeter: animation_loop_frames = 300 def _debug(self, *args): if self.debugMode: if type(args[0]) is str and len(args) > 1: print >> sys.stderr, "D: " + args[0], args[1:] else: print >> sys.stderr, "D: ", args[0] def __init__(self, options, config): self.debugMode = options.debug if self.debugMode: # Sigh. In theory, APPMENU_DISPLAY_BOTH=1 should give me both # an appmenu and the "real" menubar. # In reality, it doesn't. # QT_X11_NO_NATIVE_MENUBAR=1 is the KDE equivalent self._debug("Attempting to turn off appmenu...") os.putenv('UBUNTU_MENUPROXY', '') # Load the configuration, with type checking try: self.timePedantry = config.getboolean('Greeter', 'time_pedantry') except: self.timePedantry = config.getboolean('DEFAULT', 'time_pedantry') try: self.appendCodenameToMotd = config.getboolean('Greeter', 'append_codename_to_motd') except: self.appendCodenameToMotd = config.getboolean('DEFAULT', 'append_codename_to_motd') try: self.minimumUID = config.getint('Greeter', 'minimum_uid') except: self.minimumUID = config.getint('DEFAULT', 'minimum_uid') try: extraLogoFrames = config.getint('Greeter', 'extra_logo_frames') except: extraLogoFrames = config.getint('DEFAULT', 'extra_logo_frames') baseFile = config.get('Greeter', 'base_logo_file') self.logoFiles = [baseFile, ] fileName, fileExt = os.path.splitext(baseFile) if extraLogoFrames > 0: for i in range(1,extraLogoFrames + 1): self.logoFiles.append("%s%d%s" % (fileName, i, fileExt)) # No need to check these, they get checked later. self.backgroundImageFile = config.get('Greeter', 'background_image') self.motdFilename = config.get('Greeter', 'motd_filename') self.nologinFile = config.get('Greeter', 'nologin_file') # Set up and connect to the greeter self.greeter = LightDM.Greeter() self.greeter.connect("authentication-complete", self.cbAuthenticationComplete) self.greeter.connect("show-message", self.cbShowMessage) self.greeter.connect("show-prompt", self.cbShowPrompt) self.greeter.connect_sync() # Gtk signal handlers handlers = { "login_cb": self.cbLogin, "cancel_cb": self.cancelLogin, "kpEvent": self.cbKeyPress, "power_cb": self.doPowerOperation, "contrast_cb": self.toggleContrast, "pickboard_cb": self.togglePickboard, "large_font_cb": self.toggleLargeFont, "browse_cb": self.spawnBrowser } # Sigh. Pre lightdm-1.1, cancel_authentication() calls the # authentication-complete callback, so when we're in that # callback, we need to know if we cancelled, or if we typed # the password wrong. The lightdm daemon does in fact know # the difference (return codes of 7 or 10), but gir has no way # to get that info, AFAICT # So beingCancelled is set to True when the user hits Cancel # (or Esc) and set back to False in the authentication-complete # callback or before we try and send anything else to the greeter # This only controls UI, and has no effect on whether LightDM # thinks the authentication process is being cancelled. self.beingCancelled=False # Save the screen size for various window operations defaultScreen = Gdk.Screen.get_default() # Kids these days have too many monitors self.monitorGeometry = defaultScreen.get_monitor_geometry(defaultScreen.get_primary_monitor()) # Don't use this for window centering calculations self.screenSize = (self.monitorGeometry.x + self.monitorGeometry.width, self.monitorGeometry.y + self.monitorGeometry.height) self.get_workstation_information() # Load the UI and get objects we care about self.builder = Gtk.Builder() try: self.builder.add_from_file(options.ui_file) except GLib.GError, e: print >> sys.stderr, "FATAL: Unable to load UI: ", e sys.exit(-1) # This is just ridiculous. Due to a GtkBuilder bug, it scribbles over # the widget's name when instantiating them (via get_object()), but we # can then call set_name to re-name them. In a perfect world, this # would be a no-op. for obj in _OBJS_TO_RENAME: self.builder.get_object(obj).set_name(obj) # For the pickboard self.keyboardWindow = None # The login window self.winLogin = self.builder.get_object("winLogin") # A box containing the prompt label, entry, and a spinner self.prompt_box = self.builder.get_object("boxPrompt") self.prompt_label = self.builder.get_object("lblPrompt") self.prompt_entry = self.builder.get_object("entryPrompt") self.loginSpinner = self.builder.get_object("loginSpinner") # A label where we display messages received from the greeter self.message_label = self.builder.get_object("lblMessage") # The owl self.imgDebathena = self.builder.get_object("imgDebathena") # The workstation's hostname lblHostname = self.builder.get_object("lblHostname") lblHostname.set_text(LightDM.get_hostname()) # The buttons self.btnCancel = self.builder.get_object("btnCancel") self.btnLogin = self.builder.get_object("btnLogin") # The session combo box self.cmbSession = self.builder.get_object("cmbSession") self.sessionBox = self.builder.get_object("sessionBox") # Sigh. Needed for Oneiric. No-op on Precise # See GNOME Bugzilla #650369 and 653579 # GtkBuilder calls g_object_new, not gtk_combo_box_text_new() # so the properties don't get set. self.cmbSession.set_entry_text_column(0); self.cmbSession.set_id_column(1); for s in LightDM.get_sessions(): self.cmbSession.append(s.get_key(), s.get_name()) # Select the first session # TODO: Select the configured default session or the user's session self.cmbSession.set_active(0) self.loginNotebook = self.builder.get_object("notebook1") # Scaling factor for smaller displays logoScale = 0.75 if self.screenSize[1] <= 768 else 1.0 self.animate = self.setup_owl(logoScale) self.winLogin.set_position(Gtk.WindowPosition.CENTER) self.winLogin.show() self.initBackgroundWindow() self.initBrandingWindow() self.afsMonitor = Gio.File.new_for_path("/afs/athena").monitor_directory(Gio.FileMonitorFlags.WATCH_MOUNTS, None) self.afsAvailable = os.path.isdir("/afs/athena") self.afsMonitor.connect("changed", self.afsStatusChanged) self.initMotdWindow() # Connect Gtk+ signal handlers self.builder.connect_signals(handlers) # GNOME 3 turns off button images by default. Turn it on # for the "Panel" window self.gtkSettings = Gtk.Settings.get_default() self.gtkSettings.set_property('gtk-button-images', True) self.origTheme = self.gtkSettings.get_property('gtk-theme-name') # Set a cursor for the root window, otherwise there isn't one rw = Gdk.get_default_root_window() rw.set_cursor(Gdk.Cursor(Gdk.CursorType.LEFT_PTR)) self.noLoginMonitor = Gio.File.new_for_path(self.nologinFile).monitor_file(Gio.FileMonitorFlags.NONE, None) # Check if the file is there right now... if os.path.isfile(self.nologinFile): self.loginNotebook.set_current_page(1) self.builder.get_object("lblUpdTime").set_text("Update started at %s" % (time.strftime("%Y-%m-%d %H:%M"))) # and then connect self.noLoginMonitor.connect("changed", self._file_changed) if not os.path.exists(KIOSK_LAUNCH_CMD): self.builder.get_object("mnuBrowse").hide() # Setup the login window for first login self.resetLoginWindow() def initMotdWindow(self): self.winMotd = self.builder.get_object("winMotd") motdFile=None if self.appendCodenameToMotd: try: codename = subprocess.Popen(["lsb_release", "-s", "-c"], stdout=subprocess.PIPE).communicate()[0] if codename and os.path.exists(self.motdFilename + "." + codename.strip()): self.motdFilename += "." + codename.strip() except OSError: print >>sys.stderr, "Couldn't get codename to append to motd_filename. Oh well..." try: motdFile = open(self.motdFilename, "r") except IOError, e: print >>sys.stderr, "Can't open MOTD file %s: %s" % (self.motdFilename, str(e)) motdTxt = '' # Avoid huge files messing up the greeter # At most 10 lines of 80 characters per line # Pango ellipsizing and geometry hints won't accomplish this if motdFile: lines=0 while lines <= 10: line = motdFile.readline() if not line: break lines += 1 if len(line) > 80: line = line[:74] + " [...]\n" motdTxt += line if motdFile.read(): motdTxt += "[...]\n" motdFile.close() if motdTxt: self.builder.get_object('lblMotd').set_markup(motdTxt.strip()) width, height = self.winMotd.get_size() self.winMotd.set_gravity(Gdk.Gravity.SOUTH) self.winMotd.move(self.monitorGeometry.x + ((self.monitorGeometry.width - width )/ 2), self.screenSize[1] - height - 10) self.winMotd.show_all() def initBackgroundWindow(self): # The background image self.winBg = self.builder.get_object("winBg") self.imgBg = self.builder.get_object("imgBg") try: bg_pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.backgroundImageFile) bg_scaled = bg_pixbuf.scale_simple(self.screenSize[0], self.screenSize[1], GdkPixbuf.InterpType.BILINEAR) except GLib.GError, e: print >> sys.stderr, "Glib Error while loading background image:", e # Just a plain black background bg_scaled = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, False, 8, self.screenSize[0], self.screenSize[1]) self.imgBg.set_from_pixbuf(bg_scaled) # The menubar # LightDM checks with PolKit for the various "get_can_foo()" functions # We call .set_name() here because of a GtkBuilder bug self.builder.get_object("miShutdown").set_sensitive(LightDM.get_can_shutdown()) self.builder.get_object("miRestart").set_sensitive(LightDM.get_can_restart()) # self.builder.get_object("miRestart").set_name('restart') # We don't allow suspend/hibernate on cluster self.builder.get_object("miHibernate").set_sensitive(LightDM.get_can_hibernate() and self.metapackage != "debathena-cluster") self.builder.get_object("miSuspend").set_sensitive(LightDM.get_can_suspend() and self.metapackage != "debathena-cluster") # We just want images. A Glade bug removes the "label" property # completely if it's null, which results in bad redrawing artifacts # So we set the label to text we don't care about in Glade, and # change it here. for menu in ("mnuPower", "mnuAccess", "mnuBrowse"): self.builder.get_object(menu).set_property('label', '') # Used in the updateTime callback self.mnuTime = self.builder.get_object("mnuClock") self.builder.get_object("miPickboard").set_sensitive(os.path.exists(PICKBOARD_CMD)) self.winBg.show_all() def initBrandingWindow(self): # The "branding window", in the bottom right winBranding = self.builder.get_object("winBranding") lblBranding = self.builder.get_object("lblBranding") arch = platform.machine() if arch != "x86_64": arch = "" + arch + "" # Possibly no longer needed, workaround for a Glade bug in Gtk+ 2 lblBranding.set_property('can_focus', False) winBranding.set_property('can_focus', False) lblBranding.set_markup(self.metapackage + "\n" + self.baseos + "\n" + arch) winBranding.set_gravity(Gdk.Gravity.SOUTH_EAST) width, height = winBranding.get_size() winBranding.move(self.screenSize[0] - width, self.screenSize[1] - height) winBranding.show_all() def doPowerOperation(self, widget): # This only works because of the calls to .set_name() above. # Other stupid ideas include: # if widget == self.builder.get_object('whatever'): # # N.B. user_data for GtkBuilder is a complete mess and all # kinds of wrong (you can only specify objects defined in the # builder xml, and it forces the 'swap' flag when # autoconnected, AND the object you get _replaces_ the widget # in the callback) actions = {"miShutdown": LightDM.shutdown, "miRestart": LightDM.restart, "miHibernate": LightDM.hibernate, "miSuspend": LightDM.suspend} try: actions[widget.get_name()]() except KeyError: # "won't" happen print >>sys.stderr, "ERR: No action for widget name: "+ widget.get_name() except GLib.GError, e: # It's possible we should look at the error text to see if # it's ConsoleKit that's whining? Because you get a # (valid) error from UPower if you try to suspend when # your hardware doesn't support it self.errDialog("An error occurred while trying to perform a power-related operation. The most common cause of this is trying to shutdown, reboot, or suspend the machine when someone else is logged in remotely or on one of the virtual terminals. The full error text appears below:\n\n" + str(e)) def togglePickboard(self, widget): if not widget.get_active(): if self.keyboardWindow: self.keyboardWindow.destroy() if self.onboardProc: self.onboardProc.terminate() else: self.onboardProc = None xid = None try: self.onboardProc = subprocess.Popen([PICKBOARD_CMD, "--xid"], stdout=subprocess.PIPE) xid = int(self.onboardProc.stdout.readline()) except OSError, e: print >>sys.stderr, "Failed to spawn /usr/bin/onboard", str(e) except ValueError: print >>sys.stderr, "onboard didn't return an integer xid (shouldn't happen)" self.onboardProc.kill() if self.onboardProc is None or xid is None: self.errDialog("An error occurred while starting the on-screen keyboard.") widget.set_sensitive(False) # Remember, this will call this callback again widget.set_active(False) return self.keyboardWindow = Gtk.Window() self.keyboardWindow.show() self.keyboardWindow.accept_focus = False; self.keyboardWindow.focus_on_map = False; keyboardSocket = Gtk.Socket() keyboardSocket.show() self.keyboardWindow.add(keyboardSocket) keyboardSocket.add_id(xid) self.keyboardWindow.move(0, self.screenSize[1] - 200) self.keyboardWindow.resize(int(self.screenSize[0] / 3), 200) def toggleLargeFont(self, widget): pass def toggleContrast(self, widget): if widget.get_active(): self.gtkSettings.set_property('gtk-theme-name', 'HighContrastInverse') else: self.gtkSettings.set_property('gtk-theme-name', self.origTheme) def afsStatusChanged(self, monitor, file1, file2, evt_type): if evt_type == Gio.FileMonitorEvent.CREATED: if not self.winMotd.get_property("visible"): self.initMotdWindow() self.afsAvailable = True if evt_type == Gio.FileMonitorEvent.UNMOUNTED: self.afsAvailable = False def _file_changed(self, monitor, file1, file2, evt_type): if evt_type == Gio.FileMonitorEvent.CREATED: self.loginNotebook.set_current_page(1) self.builder.get_object("lblUpdTime").set_text("Update started at %s" % (time.strftime("%Y-%m-%d %H:%M"))) if evt_type == Gio.FileMonitorEvent.DELETED: self.loginNotebook.set_current_page(0) # Update the time in the "panel" def updateTime(self): timeFmt="%a, %b %e %Y %l:%M" + ":%S" if self.timePedantry else "" # every second counts timeFmt=timeFmt + " %p" self.mnuTime.set_label(time.strftime(timeFmt, time.localtime(time.time()))) return True # Reset the UI and prepare for a new login def resetLoginWindow(self): self.spin(False) self.clearMessage() self.btnCancel.hide() self.sessionBox.hide() self.prompted=False self.prompt_label.set_text("") self.prompt_entry.set_text("") self.prompt_box.hide() self.btnLogin.grab_focus() # Because there's no WM, we need to focus the actual X window Gdk.Window.focus(self.winLogin.get_window(), Gdk.CURRENT_TIME) def getSelectedSession(self): i = self.cmbSession.get_active_iter() session_name = self.cmbSession.get_model().get_value(i, 1) self._debug("selected session is " + session_name) return session_name def startOver(self): self.greeter.cancel_authentication() self.greeter.authenticate(None) # LightDM Callbacks # The workflow is this: # - call .authenticate() with a username # - lightdm responds with a prompt for password # - call .respond with whatever the user provides # - lightdm responds with authentication-complete # N.B. complete != successful # - .cancel_authentication will cancel the authentication in progress # call .authenticate with a new username to restart it # # Calling .authenticate with None (or NULL in C) will cause lightdm # to first prompt for a username, then a password. This means two # show-prompt callbacks and thus two .respond calls # This callback is called when the authentication process is # complete. "complete" means a username and password have been # received, and PAM has done its thing. "complete" does not # mean "successful". def cbAuthenticationComplete(self, greeter): self.spin(False) self._debug("cbAuthenticationComplete: received authentication-complete message") if greeter.get_is_authenticated(): self.spin(True) self._debug("Authentication was successful.") session_name = self.getSelectedSession() #FIXME: Make sure it's a valid session self._debug("User has selected '%s' session" % (session_name)) if not greeter.start_session_sync(session_name): self._debug("Failed to start session") print >> sys.stderr, "Failed to start session" elif not self.beingCancelled: self._debug("Authentication failed.") self.displayMessage("Authentication failed, please try again") self.greeter.authenticate(None) else: self.beingCancelled=False self.resetLoginWindow() # The show-prompt message is emitted when LightDM wants you to # show a prompt to the user, and respond with the user's response. # Currently, the prompts we care about are "login:" and # "Password: " (yes, with the trailing space), which ask for the # username and password respectively. promptType is one of # LightDM.PromptType.SECRET or LightDM.PromptType.QUESTION, which # mean that the text of the user's response should or should not be # masked/invisible, respectively. def cbShowPrompt(self, greeter, text, promptType): self._debug("cbShowPrompt: Received show-prompt message: ", text, promptType) self.prompted=True # Make things pretty if text == "login:": text = "Username: " # Sanity check the username currUser = self.greeter.get_authentication_user() if currUser: self._debug("Current user being authenticated is " + currUser) # See if the user exists try: passwd=pwd.getpwnam(currUser) except KeyError: # Why are we not using the message label here? # Because what will happen is that someone will quickly # typo their username, and then type their password without # looking at the screen, which would otherwise result in the # window resetting after the first error, and then they end # up typing their password into the username box. self.errDialog("The username '%s' is invalid.\n\n(Tip: Please ensure you're typing your username in lowercase letters.\nDo not add '@mit.edu' or any other suffix to your username.)" % (currUser)) self.startOver() return True # There's probably a better way if passwd.pw_uid < self.minimumUID: self.errDialog("Logging in as '%s' disallowed by configuation" % (currUser)) self.startOver() return True if not self.afsAvailable and (passwd.pw_dir.startswith("/mit") or passwd.pw_dir.startswith("/afs")): self.errDialog("Your AFS home directory does not appear to be available. This may indicate a problem with this workstation, or with the AFS servers. Please try another workstation.") self.startOver() return True # Set the label to the value of the prompt self.prompt_label.set_text(text) # clear the entry and get focus self.prompt_entry.set_text("") self.prompt_entry.set_sensitive(True) self.prompt_box.show() self.prompt_entry.grab_focus() # Mask the input if requested if promptType == LightDM.PromptType.SECRET: self.prompt_entry.set_visibility(False) else: self.prompt_entry.set_visibility(True) self.spin(False) # show-message is emitted when LightDM has something to say to the user # Typically, these are emitted by PAM modules (e.g. pam_echo) # Note that this is _not_ "authentication failed" (unless a PAM module # specifically says that). # # The docs which say to check .is_authenticated() in the # authentication-complete callback to determine login success or # failure. # # messageType is one of LightDM.MessageType.{ERROR,INFO} def cbShowMessage(self, greeter, text, messageType): self._debug("cbShowMessage: Received show-messsage message", text, messageType) # TODO: Wrap text self.displayMessage(text) self.spin(False) def cbKeyPress(self, widget, event): if event.keyval == Gdk.KEY_Escape: self.cancelLogin(widget) def cancelLogin(self, widget=None): self._debug("Cancelling authentication. User=", self.greeter.get_authentication_user()) self.beingCancelled=True self.greeter.cancel_authentication() self.resetLoginWindow() def displayMessage(self, msg): self.message_label.set_text(msg) self.message_label.show() def clearMessage(self): self.message_label.set_text("") self.message_label.hide() def spawnBrowser(self, event): subprocess.call(KIOSK_LAUNCH_CMD) def errDialog(self, errText): dlg = Gtk.MessageDialog(self.winLogin, Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, errText) dlg.run() dlg.destroy() def spin(self, start): if start: self.loginSpinner.show() self.loginSpinner.start() else: self.loginSpinner.stop() self.loginSpinner.hide() # Some greeter implementations check .get_is_authenticated() here # and then start the session. I think that's only relevant # dealing with a user-picker and passwordless users (that is, you # would call .authenticate(joeuser), and then click the button, # and you'd just be logged in. But we disable the user picker, so # that's not relevant. def cbLogin(self, widget): # Because we just entered some text and are about to send it, # we're no longer in the middle of a cancellation self.beingCancelled=False self.clearMessage() self._debug("In cbLogin") if self.prompted: response = self.prompt_entry.get_text() self._debug("Sending response to prompt", response if self.prompt_entry.get_visibility() else "[redacted]") self.spin(True) self.greeter.respond(response) self.prompted=False else: self._debug("No prompt. Beginning new authentication process.") # Show the "Cancel" button" self.sessionBox.show() self.btnCancel.show() self.greeter.authenticate(None) # Load the Debathena owl image and generate self.logo_pixbufs, the list of # animation frames. Returns True if successful, False otherwise. def setup_owl(self,logoScale): self.logo_pixbufs = [] num_pixbufs = 0 # Eyes go closed. for img in self.logoFiles: try: pixbuf = GdkPixbuf.Pixbuf.new_from_file(img) self.logo_pixbufs.append(pixbuf.scale_simple(int(pixbuf.get_width() * logoScale), int(pixbuf.get_height() * logoScale), GdkPixbuf.InterpType.BILINEAR)) num_pixbufs += 1 except GLib.GError, e: print >> sys.stderr, "Glib Error:", e return False # Eyes come open. for pixbuf in self.logo_pixbufs[::-1]: self.logo_pixbufs.append(pixbuf) num_pixbufs += 1 # Eyes stay open. self.logo_pixbufs.extend([None] * (self.animation_loop_frames - num_pixbufs)) self.img_idx = -1 # Set it to the first image so that the window can size itself # accordingly self.imgDebathena.set_from_pixbuf(self.logo_pixbufs[0]) self._debug("Owl setup done") return True def update_owl(self): if not self.animate: self._debug("Owl loading failed, ending update_owl timer") return False self.img_idx = (self.img_idx + 1) % self.animation_loop_frames pixbuf = self.logo_pixbufs[self.img_idx] if pixbuf is not None: self.imgDebathena.set_from_pixbuf(pixbuf) return True def get_workstation_information(self): try: self.metapackage = subprocess.Popen(["machtype", "-L"], stdout=subprocess.PIPE).communicate()[0].rstrip() except OSError: self.metapackage = '(unknown metapackage)' try: self.baseos = subprocess.Popen(["machtype", "-E"], stdout=subprocess.PIPE).communicate()[0].rstrip() except OSError: self.baseos = '(unknown OS)' if __name__ == '__main__': parser = OptionParser() parser.set_defaults(debug=False) parser.add_option("--debug", action="store_true", dest="debug") parser.add_option("--ui", action="store", type="string", default=UI_FILE, dest="ui_file") parser.add_option("--cfg", action="store", type="string", default=CONFIG_FILE, dest="config_file") (options, args) = parser.parse_args() config = ConfigParser.RawConfigParser(CONFIG_DEFAULTS) # Hack to create a 'Greeter' section so that we can just use that # in any calls config.readfp(io.BytesIO("[Greeter]\n")) config.read(options.config_file) Gtk.init(None); main_loop = GObject.MainLoop () dagreeter = DebathenaGreeter(options, config) # Add a timeout for the owl animation GObject.timeout_add(50, dagreeter.update_owl) # Add a timeout for the clock in the panel GObject.timeout_add(30, dagreeter.updateTime) main_loop.run ()