Debian Codegraph @

pysmb.py

raw
1 #!/usr/bin/python3
2
3 ## system-config-printer
4 ## CUPS backend
5
6 ## Copyright (C) 2002, 2003, 2006, 2007, 2008, 2010, 2012, 2013 Red Hat, Inc.
7 ## Authors:
8 ## Tim Waugh <[email protected]>
9
10 ## This program is free software; you can redistribute it and/or modify
11 ## it under the terms of the GNU General Public License as published by
12 ## the Free Software Foundation; either version 2 of the License, or
13 ## (at your option) any later version.
14
15 ## This program is distributed in the hope that it will be useful,
16 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ## GNU General Public License for more details.
19
20 ## You should have received a copy of the GNU General Public License
21 ## along with this program; if not, write to the Free Software
22 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
24 import errno
25 import config
26 import gettext
27 gettext.install(domain=config.PACKAGE, localedir=config.localedir)
28 from gi.repository import Gtk
29 import os
30 import pwd
31 import smbc
32 from debug import *
33
34 class _None(RuntimeError):
35 pass
36
37 try:
38 NoEntryError = smbc.NoEntryError
39 PermissionError = smbc.PermissionError
40 ExistsError = smbc.ExistsError
41 NotEmptyError = smbc.NotEmptyError
42 TimedOutError = smbc.TimedOutError
43 NoSpaceError = smbc.NoSpaceError
44 except AttributeError:
45 NoEntryError = PermissionError = ExistsError = _None
46 NotEmptyError = TimedOutError = NoSpaceError = _None
47
48 class AuthContext:
49 def __init__ (self, parent=None, workgroup='', user='', passwd=''):
50 self.passes = 0
51 self.has_failed = False
52 self.auth_called = False
53 self.tried_guest = False
54 self.cancel = False
55 self.use_user = user
56 self.use_password = passwd
57 self.use_workgroup = workgroup
58 self.dialog_shown = False
59 self.parent = parent
60
61 def perform_authentication (self):
62 self.passes += 1
63 if self.passes == 1:
64 return 1
65
66 if not self.has_failed:
67 return 0
68
69 debugprint ("pysmb: authentication pass: %d" % self.passes)
70 if not self.auth_called:
71 debugprint ("pysmb: auth callback not called?!")
72 self.cancel = True
73 return 0
74
75 self.has_failed = False
76 if self.auth_called and not self.tried_guest:
77 self.use_user = 'guest'
78 self.use_password = ''
79 self.tried_guest = True
80 debugprint ("pysmb: try auth as guest")
81 return 1
82
83 self.auth_called = False
84
85 if self.dialog_shown:
86 d = Gtk.MessageDialog (transient_for=self.parent,
87 modal=True, destroy_with_parent=True,
88 message_type=Gtk.MessageType.ERROR,
89 buttons=Gtk.ButtonsType.CLOSE)
90 d.set_title (_("Not authorized"))
91 d.set_markup ('<span weight="bold" size="larger">' +
92 _("Not authorized") + '</span>\n\n' +
93 _("The password may be incorrect."))
94 d.run ()
95 d.destroy ()
96
97 # After that, prompt
98 d = Gtk.Dialog (title=_("Authentication"),
99 transient_for=self.parent,
100 modal=True)
101 d.add_buttons (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
102 Gtk.STOCK_OK, Gtk.ResponseType.OK)
103 d.set_default_response (Gtk.ResponseType.OK)
104 d.set_border_width (6)
105 d.set_resizable (False)
106 hbox = Gtk.HBox.new (False, 12)
107 hbox.set_border_width (6)
108 image = Gtk.Image ()
109 image.set_from_stock (Gtk.STOCK_DIALOG_AUTHENTICATION,
110 Gtk.IconSize.DIALOG)
111 hbox.pack_start (image, False, False, 0)
112 vbox = Gtk.VBox (False, 12)
113 label = Gtk.Label(label='<span weight="bold" size="larger">' +
114 _("You must log in to access %s.")
115 % self.for_server +
116 '</span>')
117 label.set_use_markup (True)
118 label.set_alignment (0, 0)
119 label.set_line_wrap (True)
120 vbox.pack_start (label, False, False, 0)
121
122 grid = Gtk.Grid()
123 grid.set_row_spacing (6)
124 grid.set_column_spacing (6)
125 grid.attach (Gtk.Label(label=_("Username:")), 0, 0, 1, 1)
126 username_entry = Gtk.Entry ()
127 grid.attach (username_entry, 1, 0, 1, 1)
128 grid.attach (Gtk.Label(label=_("Domain:")), 0, 1, 1, 1)
129 domain_entry = Gtk.Entry ()
130 grid.attach (domain_entry, 1, 1, 1, 1)
131 grid.attach (Gtk.Label(label=_("Password:")), 0, 2, 1, 1)
132 password_entry = Gtk.Entry ()
133 password_entry.set_activates_default (True)
134 password_entry.set_visibility (False)
135 grid.attach (password_entry, 1, 2, 1, 1)
136 vbox.pack_start (grid, False, False, 0)
137 hbox.pack_start (vbox, False, False, 0)
138 d.vbox.pack_start (hbox, False, False, 0)
139 self.dialog_shown = True
140 d.show_all ()
141 d.show_now ()
142
143 if self.use_user == 'guest':
144 self.use_user = pwd.getpwuid (os.getuid ())[0]
145 debugprint ("pysmb: try as %s" % self.use_user)
146 username_entry.set_text (self.use_user)
147 domain_entry.set_text (self.use_workgroup)
148
149 d.set_keep_above (True)
150 response = d.run ()
151
152 if response == Gtk.ResponseType.CANCEL:
153 self.cancel = True
154 d.destroy ()
155 return -1
156
157 self.use_user = username_entry.get_text ()
158 self.use_password = password_entry.get_text ()
159 self.use_workgroup = domain_entry.get_text ()
160 d.destroy ()
161 return 1
162
163 def initial_authentication (self):
164 try:
165 context = smbc.Context ()
166 self.use_workgroup = context.workgroup
167 except:
168 pass
169
170 def failed (self, exc=None):
171 self.has_failed = True
172 debugprint ("pysmb: operation failed: %s" % repr (exc))
173
174 if exc:
175 if (self.cancel or
176 (type (exc) in [NoEntryError, ExistsError, NotEmptyError,
177 TimedOutError, NoSpaceError]) or
178 (type (exc) == RuntimeError and
179 not (exc.args[0] in [errno.EACCES, errno.EPERM]))):
180 raise exc
181
182 def callback (self, server, share, workgroup, user, password):
183 debugprint ("pysmb: got password callback")
184 self.auth_called = True
185 self.for_server = server
186 self.for_share = share
187 if self.passes == 1:
188 self.initial_authentication ()
189
190 if self.use_user:
191 if self.use_workgroup:
192 workgroup = self.use_workgroup
193
194 return (workgroup, self.use_user, self.use_password)
195
196 user = ''
197 password = ''
198 return (workgroup, user, password)