| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
## Printing troubleshooter |
| 4 |
|
| 5 |
## Copyright (C) 2008, 2009, 2010, 2014 Red Hat, Inc. |
| 6 |
## Authors: |
| 7 |
## Tim Waugh <[email protected]> |
| 8 |
|
| 9 |
## This program is free software; you can redistribute it and/or modify |
| 10 |
## it under the terms of the GNU General Public License as published by |
| 11 |
## the Free Software Foundation; either version 2 of the License, or |
| 12 |
## (at your option) any later version. |
| 13 |
|
| 14 |
## This program is distributed in the hope that it will be useful, |
| 15 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
## GNU General Public License for more details. |
| 18 |
|
| 19 |
## You should have received a copy of the GNU General Public License |
| 20 |
## along with this program; if not, write to the Free Software |
| 21 |
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 22 |
|
| 23 |
import glob |
| 24 |
import os |
| 25 |
import subprocess |
| 26 |
from timedops import TimedSubprocess |
| 27 |
import urllib.parse |
| 28 |
from .base import * |
| 29 |
from gi.repository import Gtk |
| 30 |
|
| 31 |
class CheckUSBPermissions(Question): |
| 32 |
def __init__ (self, troubleshooter): |
| 33 |
Question.__init__ (self, troubleshooter, "Check USB permissions") |
| 34 |
troubleshooter.new_page (Gtk.Label (), self) |
| 35 |
|
| 36 |
def display (self): |
| 37 |
self.answers = {} |
| 38 |
answers = self.troubleshooter.answers |
| 39 |
if answers['cups_queue_listed']: |
| 40 |
if answers['is_cups_class']: |
| 41 |
return False |
| 42 |
|
| 43 |
cups_printer_dict = answers['cups_printer_dict'] |
| 44 |
device_uri = cups_printer_dict['device-uri'] |
| 45 |
elif answers.get ('cups_device_listed', False): |
| 46 |
device_uri = answers['cups_device_uri'] |
| 47 |
else: |
| 48 |
return False |
| 49 |
|
| 50 |
(scheme, rest) = urllib.parse.splittype (device_uri) |
| 51 |
if scheme not in ['hp', 'hpfax', 'usb', 'hal']: |
| 52 |
return False |
| 53 |
|
| 54 |
LSUSB = "/sbin/lsusb" |
| 55 |
if not os.access (LSUSB, os.X_OK): |
| 56 |
return False |
| 57 |
|
| 58 |
GETFACL = "/usr/bin/getfacl" |
| 59 |
if not os.access (GETFACL, os.X_OK): |
| 60 |
return False |
| 61 |
|
| 62 |
new_environ = os.environ.copy() |
| 63 |
new_environ['LC_ALL'] = "C" |
| 64 |
|
| 65 |
# Run lsusb |
| 66 |
parent = self.troubleshooter.get_window () |
| 67 |
try: |
| 68 |
self.op = TimedSubprocess (parent=parent, |
| 69 |
args=[LSUSB, "-v"], |
| 70 |
close_fds=True, |
| 71 |
env=new_environ, |
| 72 |
stdin=subprocess.DEVNULL, |
| 73 |
stdout=subprocess.PIPE, |
| 74 |
stderr=subprocess.PIPE) |
| 75 |
(lsusb_stdout, lsusb_stderr, result) = self.op.run () |
| 76 |
except: |
| 77 |
# Problem executing command. |
| 78 |
return False |
| 79 |
|
| 80 |
# Now parse it. |
| 81 |
dev_by_id = {} |
| 82 |
this_dev = None |
| 83 |
for line in lsusb_stdout: |
| 84 |
if (this_dev is not None and |
| 85 |
((line.find ("bInterfaceClass") != -1 and |
| 86 |
line.find ("7 Printer") != -1) or |
| 87 |
(line.find ("bInterfaceSubClass") != -1 and |
| 88 |
line.find ("1 Printer") != -1))): |
| 89 |
mfr = dev_by_id.get (this_mfr_id, {}) |
| 90 |
mdl = mfr.get (this_mdl_id, []) |
| 91 |
mdl.append (this_dev) |
| 92 |
mfr[this_mdl_id] = mdl |
| 93 |
dev_by_id[this_mfr_id] = mfr |
| 94 |
this_dev = None |
| 95 |
continue |
| 96 |
|
| 97 |
separators = [ ('Bus ', 3), |
| 98 |
(' Device ', 3), |
| 99 |
(': ID ', 4), |
| 100 |
(':', 4), |
| 101 |
(' ', -1)] |
| 102 |
fields = [] |
| 103 |
i = 0 |
| 104 |
p = line |
| 105 |
while i < len (separators): |
| 106 |
(sep, length) = separators[i] |
| 107 |
if not p.startswith (sep): |
| 108 |
break |
| 109 |
start = len (sep) |
| 110 |
if length == -1: |
| 111 |
end = len (p) |
| 112 |
fields.append (p[start:]) |
| 113 |
else: |
| 114 |
end = start + length |
| 115 |
fields.append (p[start:end]) |
| 116 |
|
| 117 |
p = p[end:] |
| 118 |
i += 1 |
| 119 |
|
| 120 |
if i < len (separators): |
| 121 |
continue |
| 122 |
|
| 123 |
if not scheme.startswith ('hp') and fields[2] != '03f0': |
| 124 |
# Skip non-HP printers if we know we're using HPLIP. |
| 125 |
continue |
| 126 |
|
| 127 |
this_dev = { 'bus': fields[0], |
| 128 |
'dev': fields[1], |
| 129 |
'name': fields[4], |
| 130 |
'full': line } |
| 131 |
this_mfr_id = fields[2] |
| 132 |
this_mdl_id = fields[3] |
| 133 |
|
| 134 |
infos = {} |
| 135 |
paths = [] |
| 136 |
if not scheme.startswith ('hp'): |
| 137 |
paths.extend (glob.glob ("/dev/usb/lp?")) |
| 138 |
for mfr_id, mdls in dev_by_id.items (): |
| 139 |
for mdl_id, devs in mdls.items (): |
| 140 |
for dev in devs: |
| 141 |
path = "/dev/bus/usb/%s/%s" % (dev['bus'], dev['dev']) |
| 142 |
paths.append (path) |
| 143 |
infos[path] = dev['full'] |
| 144 |
|
| 145 |
perms = [] |
| 146 |
for path in paths: |
| 147 |
try: |
| 148 |
self.op = TimedSubprocess (parent=parent, |
| 149 |
args=[GETFACL, path], |
| 150 |
close_fds=True, |
| 151 |
env=new_environ, |
| 152 |
stdin=subprocess.DEVNULL, |
| 153 |
stdout=subprocess.PIPE, |
| 154 |
stderr=subprocess.PIPE) |
| 155 |
(getfacl_stdout, getfacl_stderr, result) = self.op.run () |
| 156 |
output = [x for x in getfacl_stdout if len (x) > 0] |
| 157 |
except: |
| 158 |
# Problem executing command. |
| 159 |
output = [] |
| 160 |
|
| 161 |
info = infos.get (path, path) |
| 162 |
perms.append ((info, output)) |
| 163 |
|
| 164 |
self.answers['getfacl_output'] = perms |
| 165 |
|
| 166 |
# Don't actually display anything, just collect information. |
| 167 |
return False |
| 168 |
|
| 169 |
def collect_answer (self): |
| 170 |
return self.answers |
| 171 |
|
| 172 |
def cancel_operation (self): |
| 173 |
self.op.cancel () |