1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
#!/usr/bin/python3
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
# Mugshot - Lightweight user configuration utility
# Copyright (C) 2013-2020 Sean Davis <sean@bluesabre.org>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
import os
from locale import gettext as _
from gi.repository import Gtk, GdkPixbuf
import pexpect
gtk_version = (Gtk.get_major_version(),
Gtk.get_minor_version(),
Gtk.get_micro_version())
def check_gtk_version(major_version, minor_version, micro=0):
"""Return true if running gtk >= requested version"""
return gtk_version >= (major_version, minor_version, micro)
# Check if the LANG variable needs to be set
use_env = False
def check_dependencies(commands=[]):
"""Check for the existence of required commands, and sudo access"""
# Check for sudo
if pexpect.which("sudo") is None:
return False
# Check for required commands
for command in commands:
if pexpect.which(command) is None:
return False
# Check for LANG requirements
child = None
try:
child = env_spawn('sudo', ['-v'], 1)
if child.expect([".*ssword.*", "Sorry",
pexpect.EOF,
pexpect.TIMEOUT]) == 3:
global use_env
use_env = True
child.close()
except OSError:
if child is not None:
child.close()
return False
# Check for sudo rights
child = env_spawn('sudo', ['-v'], 1)
try:
index = child.expect([".*ssword.*", "Sorry",
pexpect.EOF, pexpect.TIMEOUT])
child.close()
if index == 0 or index == 2:
# User in sudoers, or already admin
return True
elif index == 1 or index == 3:
# User not in sudoers
return False
except:
# Something else went wrong.
child.close()
return False
def env_spawn(command, args, timeout):
"""Use pexpect.spawn, adapt for timeout and env requirements."""
env = os.environ
env["LANG"] = "C"
if use_env:
child = pexpect.spawn(command, args, env)
else:
child = pexpect.spawn(command, args)
child.timeout = timeout
return child
class SudoDialog(Gtk.Dialog):
'''
Creates a new SudoDialog. This is a replacement for using gksudo which
provides additional flexibility when performing sudo commands.
Only used to verify password by issuing 'sudo /bin/true'.
Keyword arguments:
- parent: Optional parent Gtk.Window
- icon: Optional icon name or path to image file.
- message: Optional message to be displayed instead of the defaults.
- name: Optional name to be displayed, for when message is not used.
- retries: Optional maximum number of password attempts. -1 is unlimited.
Signals emitted by run():
- NONE: Dialog closed.
- CANCEL: Dialog cancelled.
- REJECT: Password invalid.
- ACCEPT: Password valid.
'''
def __init__(self, title=None, parent=None, icon=None, message=None,
name=None, retries=-1):
"""Initialize the SudoDialog."""
# initialize the dialog
super(SudoDialog, self).__init__(title=title,
transient_for=parent,
modal=True,
destroy_with_parent=True)
#
self.connect("show", self.on_show)
if title is None:
title = _("Password Required")
self.set_title(title)
self.set_border_width(5)
# Content Area
content_area = self.get_content_area()
grid = Gtk.Grid.new()
grid.set_row_spacing(6)
grid.set_column_spacing(12)
grid.set_margin_left(5)
grid.set_margin_right(5)
content_area.add(grid)
# Icon
self.dialog_icon = Gtk.Image.new_from_icon_name("dialog-password",
Gtk.IconSize.DIALOG)
grid.attach(self.dialog_icon, 0, 0, 1, 2)
# Text
self.primary_text = Gtk.Label.new("")
self.primary_text.set_use_markup(True)
self.primary_text.set_halign(Gtk.Align.START)
self.secondary_text = Gtk.Label.new("")
self.secondary_text.set_use_markup(True)
self.secondary_text.set_halign(Gtk.Align.START)
self.secondary_text.set_margin_top(6)
grid.attach(self.primary_text, 1, 0, 1, 1)
grid.attach(self.secondary_text, 1, 1, 1, 1)
# Infobar
self.infobar = Gtk.InfoBar.new()
self.infobar.set_margin_top(12)
self.infobar.set_message_type(Gtk.MessageType.WARNING)
content_area = self.infobar.get_content_area()
infobar_icon = Gtk.Image.new_from_icon_name("dialog-warning",
Gtk.IconSize.BUTTON)
label = Gtk.Label.new(_("Incorrect password... try again."))
content_area.add(infobar_icon)
content_area.add(label)
grid.attach(self.infobar, 0, 2, 2, 1)
content_area.show_all()
self.infobar.set_no_show_all(True)
# Password
label = Gtk.Label.new("")
label.set_use_markup(True)
label.set_markup("<b>%s</b>" % _("Password:"))
label.set_halign(Gtk.Align.START)
label.set_margin_top(12)
self.password_entry = Gtk.Entry()
self.password_entry.set_visibility(False)
self.password_entry.set_activates_default(True)
self.password_entry.set_margin_top(12)
grid.attach(label, 0, 3, 1, 1)
grid.attach(self.password_entry, 1, 3, 1, 1)
# Buttons
button = self.add_button(_("Cancel"), Gtk.ResponseType.CANCEL)
button_box = button.get_parent()
button_box.set_margin_top(24)
ok_button = Gtk.Button.new_with_label(_("OK"))
ok_button.connect("clicked", self.on_ok_clicked)
ok_button.set_receives_default(True)
ok_button.set_can_default(True)
ok_button.set_sensitive(False)
self.set_default(ok_button)
if check_gtk_version(3, 12):
button_box.pack_start(ok_button, True, True, 0)
else:
button_box.pack_start(ok_button, False, False, 0)
self.password_entry.connect("changed", self.on_password_changed,
ok_button)
self.set_dialog_icon(icon)
# add primary and secondary text
if message:
primary_text = message
secondary_text = None
else:
primary_text = _("Enter your password to\n"
"perform administrative tasks.")
secondary_text = _("The application '%s' lets you\n"
"modify essential parts of your system." % name)
self.format_primary_text(primary_text)
self.format_secondary_text(secondary_text)
self.attempted_logins = 0
self.max_attempted_logins = retries
self.show_all()
def on_password_changed(self, widget, button):
"""Set the apply button sensitivity based on password input."""
button.set_sensitive(len(widget.get_text()) > 0)
def format_primary_text(self, message_format):
'''
Format the primary text widget.
'''
self.primary_text.set_markup("<big><b>%s</b></big>" % message_format)
def format_secondary_text(self, message_format):
'''
Format the secondary text widget.
'''
self.secondary_text.set_markup(message_format)
def set_dialog_icon(self, icon=None):
'''
Set the icon for the dialog. If the icon variable is an absolute
path, the icon is from an image file. Otherwise, set the icon from an
icon name.
'''
# default icon size is dialog.
icon_size = Gtk.IconSize.DIALOG
if icon:
if os.path.isfile(os.path.abspath(icon)):
# icon is a filename, so load it into a pixbuf to an image
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(icon,
icon_size,
icon_size)
self.dialog_icon.set_from_pixbuf(pixbuf)
self.set_icon_from_file(icon)
else:
# icon is an named icon, so load it directly to an image
self.dialog_icon.set_from_icon_name(icon, icon_size)
self.set_icon_name(icon)
else:
# fallback on password icon
self.dialog_icon.set_from_icon_name('dialog-password', icon_size)
self.set_icon_name('dialog-password')
def on_show(self, widget):
'''When the dialog is displayed, clear the password.'''
self.set_password('')
self.password_valid = False
def on_ok_clicked(self, widget):
'''
When the OK button is clicked, attempt to use sudo with the currently
entered password. If successful, emit the response signal with ACCEPT.
If unsuccessful, try again until reaching maximum attempted logins,
then emit the response signal with REJECT.
'''
if self.attempt_login():
self.password_valid = True
self.emit("response", Gtk.ResponseType.ACCEPT)
else:
self.password_valid = False
# Adjust the dialog for attactiveness.
self.infobar.show()
self.password_entry.grab_focus()
if self.attempted_logins == self.max_attempted_logins:
self.attempted_logins = 0
self.emit("response", Gtk.ResponseType.REJECT)
def get_password(self):
'''Return the currently entered password, or None if blank.'''
if not self.password_valid:
return None
password = self.password_entry.get_text()
if password == '':
return None
return password
def set_password(self, text=None):
'''Set the password entry to the defined text.'''
if text is None:
text = ''
self.password_entry.set_text(text)
self.password_valid = False
def attempt_login(self):
'''
Try to use sudo with the current entered password.
Return True if successful.
'''
# Set the pexpect variables and spawn the process.
child = env_spawn('sudo', ['/bin/true'], 1)
try:
# Check for password prompt or program exit.
child.expect([".*ssword.*", pexpect.EOF])
child.sendline(self.password_entry.get_text())
child.expect(pexpect.EOF)
except pexpect.TIMEOUT:
# If we timeout, that means the password was unsuccessful.
pass
# Close the child process if it is still open.
child.close()
# Exit status 0 means success, anything else is an error.
if child.exitstatus == 0:
self.attempted_logins = 0
return True
self.attempted_logins += 1
return False
|