1 """The su (switch user) module allows you to execute a command as some
2 other user (normally 'root'). It supports a variety of methods to perform
3 the switch (using su, xsu, sudo, etc) so you don't have to worry about
4 which ones are available on the current platform."""
8 from rox
import g
, _
, master_proxy
10 from select
import select
17 _my_dir
= os
.path
.abspath(os
.path
.dirname(__file__
))
18 _child_script
= os
.path
.join(_my_dir
, 'suchild.sh')
20 def create_su_proxy(message
, uid
= 0, confirm
= True):
21 """Creates a new master_proxy.MasterObject and starts the child
22 process. If necessary, the user is prompted for a password. If no
23 password is required, the user is simply asked to confirm,
24 unless 'confirm' is False.
25 Raises UserAbort if the user clicks Cancel."""
26 method
= default_method(message
, uid
, confirm
)
27 return method
.get_master().root
30 need_interaction
= True
32 def __init__(self
, uid
):
36 raise NotImplemented() # Abstract
39 """Contains Python file objects for two pipe ends.
40 Wrapping the FDs in this way ensures that they will be freed on error."""
47 self
.readable
= os
.fdopen(r
, 'r')
52 self
.writeable
= os
.fdopen(w
, 'w')
57 class XtermMethod(Method
):
60 def __init__(self
, message
, uid
, confirm
):
61 Method
.__init
__(self
, uid
)
62 self
.message
= message
65 assert self
._master
is None
73 to_child
.writeable
.close()
74 from_child
.readable
.close()
75 self
.exec_child(from_child
.writeable
,
81 from_child
.writeable
.close()
82 to_child
.readable
.close()
84 assert self
._master
is None
85 self
._master
= master_proxy
.MasterProxy(to_child
.writeable
,
89 def exec_child(self
, to_parent
, from_parent
):
90 fcntl
.fcntl(to_parent
, fcntl
.F_SETFD
, 0)
91 fcntl
.fcntl(from_parent
, fcntl
.F_SETFD
, 0)
92 os
.execlp('xterm', 'xterm',
94 '-title', 'Enter password',
99 str(to_parent
.fileno()),
100 str(from_parent
.fileno()))
102 default_method
= XtermMethod