1 from enigma import eConsoleAppContainer
3 from Screens.Screen import Screen
4 from Screens.MessageBox import MessageBox
5 from Screens.ChoiceBox import ChoiceBox
6 from Screens.InputBox import InputBox
7 from Tools.Log import Log
9 from Plugins.Plugin import PluginDescriptor
12 from random import Random
15 class ChangePasswordScreen(ChoiceBox):
16 WINDOW_TITLE = _("Change Password")
22 def __init__(self, session, user="root"):
24 (_("Enter a new password"), self.KEY_SET),
25 (_("Generate a random password"), self.KEY_RANDOM),
26 (_("Disable password-based login"), self.KEY_LOCK),
27 (_("Remove password protection (DANGEROUS!)"), self.KEY_REMOVE),
29 ChoiceBox.__init__(self, session, title=_("If you want to login to your Dreambox using SSH, FTP or a remote web browser, you need to configure a password first.\nThe username will be '%s'.") %(user), list=options, windowTitle=ChangePasswordScreen.WINDOW_TITLE)
32 self._wasLocked = False
33 self._container = eConsoleAppContainer()
34 self.__appClosed_conn = self._container.appClosed.connect(self._commandFinished)
37 selected = self["list"].l.getCurrentSelection()
39 selected = selected and selected[0]
42 selected = selected[1]
43 if selected == self.KEY_SET:
44 self.session.openWithCallback(self._onPasswordInputFinished, InputBox, title=_("Please enter a new password for %s") %(self._user), windowTitle=_("New Password"), text=self._getRandom())
46 elif selected == self.KEY_RANDOM:
47 self._apply(self._getRandom())
48 elif selected == self.KEY_LOCK:
50 elif selected == self.KEY_REMOVE:
53 def _apply(self, password):
54 Log.w("Changing password for %s" % (self._user,))
55 self._password = password
57 self._container.execute("echo \"%s:%s\" | chpasswd" % (self._user, password))
59 self._container.execute("passwd -d %s" % self._user)
62 passwdChars = string.letters + string.digits
64 return ''.join(Random().sample(passwdChars, passwdLength))
67 Log.w("Removing password for %s" % (self._user))
69 self._wasLocked = True
70 self._container.execute("passwd -l %s" % self._user)
72 def _commandFinished(self,retval):
74 type=MessageBox.TYPE_INFO
75 windowTitle=_("Password changed")
77 message = _("The password for '%s' was successfully changed to:\n\n%s") % (self._user, self._password)
79 type = MessageBox.TYPE_WARNING
81 windowTitle=_("Password locked")
82 message = _("The password for '%s' is now disabled!") % (self._user,)
84 windowTitle=_("Password removed")
85 message = _("The password protection for '%s' was removed!") %(self._user,)
87 windowTitle=_("Password change failed!")
88 message=_("Unable to set new password for '%s'") % self._user
89 type=MessageBox.TYPE_ERROR
90 self.session.open(MessageBox, message , type, windowTitle=windowTitle)
93 def _onPasswordInputFinished(self, password):
97 def startChange(menuid):
98 if menuid != "system":
100 return [(_("Password"), main, "change_root_passwd", 50)]
102 def main(session, **kwargs):
103 session.open(ChangePasswordScreen)
105 def Plugins(**kwargs):
106 return PluginDescriptor(
107 name=ChangePasswordScreen.WINDOW_TITLE,
108 description=_("Change or reset the root password of your dreambox"),
109 where = [PluginDescriptor.WHERE_MENU], fnc = startChange)