Debian Codegraph @

troubleshoot/CheckNetworkServerSanity.py

raw
1 #!/usr/bin/python3
2
3 ## Printing troubleshooter
4
5 ## Copyright (C) 2008, 2009, 2010, 2011, 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 from gi.repository import Gtk
24
25 import cups
26 import os
27 import smburi
28 import socket
29 import subprocess
30 from timedops import TimedSubprocess, TimedOperation
31 from .base import *
32
33 try:
34 import smbc
35 except:
36 pass
37
38 class CheckNetworkServerSanity(Question):
39 def __init__ (self, troubleshooter):
40 Question.__init__ (self, troubleshooter, "Check network server sanity")
41 troubleshooter.new_page (Gtk.Label (), self)
42
43 def display (self):
44 # Collect useful information.
45
46 self.answers = {}
47 answers = self.troubleshooter.answers
48 if ('remote_server_name' not in answers and
49 'remote_server_ip_address' not in answers):
50 return False
51
52 parent = self.troubleshooter.get_window ()
53
54 server_name = answers['remote_server_name']
55 server_port = answers.get('remote_server_port', 631)
56 try_connect = False
57 if server_name:
58 # Try resolving the hostname.
59 try:
60 ai = socket.getaddrinfo (server_name, server_port)
61 resolves = [family_socktype_proto_canonname_sockaddr[4][0] for
62 family_socktype_proto_canonname_sockaddr in ai]
63 try_connect = True
64 except socket.gaierror:
65 resolves = False
66
67 self.answers['remote_server_name_resolves'] = resolves
68
69 ipaddr = answers.get ('remote_server_ip_address', '')
70 if resolves:
71 if ipaddr:
72 try:
73 resolves.index (ipaddr)
74 except ValueError:
75 # The IP address given doesn't match the server name.
76 # Use the IP address instead of the name.
77 server_name = ipaddr
78 try_connect = True
79 elif ipaddr:
80 server_name = ipaddr
81 try_connect = True
82 else:
83 server_name = answers['remote_server_ip_address']
84 # Validate it.
85 try:
86 ai = socket.getaddrinfo (server_name, server_port)
87 resolves = [family_socktype_proto_canonname_sockaddr1[4][0] for
88 family_socktype_proto_canonname_sockaddr1 in ai]
89 except socket.gaierror:
90 resolves = False
91
92 self.answers['remote_server_name_resolves'] = resolves
93 try_connect = True
94
95 self.answers['remote_server_try_connect'] = server_name
96
97 if (try_connect and
98 answers.get ('cups_device_uri_scheme', 'ipp') in ['ipp',
99 'http',
100 'https']):
101 if answers.get ('cups_device_uri_scheme') == 'https':
102 encryption = cups.HTTP_ENCRYPT_REQUIRED
103 else:
104 encryption = cups.HTTP_ENCRYPT_IF_REQUESTED
105
106 try:
107 self.op = TimedOperation (cups.Connection,
108 kwargs={"host": server_name,
109 "port": server_port,
110 "encryption": encryption},
111 parent=parent)
112 c = self.op.run ()
113 ipp_connect = True
114 except RuntimeError:
115 ipp_connect = False
116
117 self.answers['remote_server_connect_ipp'] = ipp_connect
118
119 if ipp_connect:
120 try:
121 self.op = TimedOperation (c.getPrinters, parent=parent)
122 self.op.run ()
123 cups_server = True
124 except:
125 cups_server = False
126
127 self.answers['remote_server_cups'] = cups_server
128
129 if cups_server:
130 cups_printer_dict = answers.get ('cups_printer_dict', {})
131 uri = cups_printer_dict.get ('device-uri', None)
132 if uri:
133 try:
134 self.op = TimedOperation (c.getPrinterAttributes,
135 kwargs={"uri": uri},
136 parent=parent)
137 attr = self.op.run ()
138 self.answers['remote_cups_queue_attributes'] = attr
139 except:
140 pass
141
142 if try_connect:
143 # Try to see if we can connect using smbc.
144 context = None
145 try:
146 context = smbc.Context ()
147 name = self.answers['remote_server_try_connect']
148 self.op = TimedOperation (context.opendir,
149 args=("smb://%s/" % name,),
150 parent=parent)
151 dir = self.op.run ()
152 self.op = TimedOperation (dir.getdents, parent=parent)
153 shares = self.op.run ()
154 self.answers['remote_server_smb'] = True
155 self.answers['remote_server_smb_shares'] = shares
156 except NameError:
157 # No smbc support
158 pass
159 except RuntimeError as e:
160 (e, s) = e.args
161 self.answers['remote_server_smb_shares'] = (e, s)
162
163 if context is not None and 'cups_printer_dict' in answers:
164 uri = answers['cups_printer_dict'].get ('device-uri', '')
165 u = smburi.SMBURI (uri)
166 (group, host, share, user, password) = u.separate ()
167 accessible = False
168 try:
169 self.op = TimedOperation (context.open,
170 args=("smb://%s/%s" % (host,
171 share),
172 os.O_RDWR,
173 0o777),
174 parent=parent)
175 f = self.op.run ()
176 accessible = True
177 except RuntimeError as e:
178 (e, s) = e.args
179 accessible = (e, s)
180
181 self.answers['remote_server_smb_share_anon_access'] = accessible
182
183 # Try traceroute if we haven't already.
184 if (try_connect and
185 'remote_server_traceroute' not in answers):
186 try:
187 self.op = TimedSubprocess (parent=parent, close_fds=True,
188 args=['traceroute', '-w', '1',
189 server_name],
190 stdin=subprocess.DEVNULL,
191 stdout=subprocess.PIPE,
192 stderr=subprocess.PIPE)
193 self.answers['remote_server_traceroute'] = self.op.run ()
194 except:
195 # Problem executing command.
196 pass
197
198 return False
199
200 def collect_answer (self):
201 return self.answers
202
203 def cancel_operation (self):
204 self.op.cancel ()