[WebInterface] - forward opener for externals to have access to functions of WebInterface
[enigma2-plugins.git] / setpasswd / src / plugin.py
1 from enigma import eConsoleAppContainer
2
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
8
9 from Plugins.Plugin import PluginDescriptor
10
11 import string
12 from random import Random 
13
14
15 class ChangePasswordScreen(ChoiceBox):
16         WINDOW_TITLE = _("Change Password")
17         KEY_SET = "set"
18         KEY_RANDOM = "random"
19         KEY_LOCK = "lock"
20         KEY_REMOVE = "remove"
21
22         def __init__(self, session, user="root"):
23                 options = [
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),
28                         ]
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)
30                 self._user = user
31                 self._password = ""
32                 self._wasLocked = False
33                 self._container = eConsoleAppContainer()
34                 self.__appClosed_conn = self._container.appClosed.connect(self._commandFinished)
35
36         def go(self):
37                 selected = self["list"].l.getCurrentSelection()
38                 Log.w(selected)
39                 selected = selected and selected[0]
40                 if not selected:
41                         return
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())
45                         return
46                 elif selected == self.KEY_RANDOM:
47                         self._apply(self._getRandom())
48                 elif selected == self.KEY_LOCK:
49                         self._lock()
50                 elif selected == self.KEY_REMOVE:
51                         self._apply("")
52
53         def _apply(self, password):
54                 Log.w("Changing password for %s" % (self._user,))
55                 self._password = password
56                 if password:
57                         self._container.execute("echo \"%s:%s\" | chpasswd" % (self._user, password))
58                 else:
59                         self._container.execute("passwd -d %s" % self._user)
60
61         def _getRandom(self):
62                 passwdChars = string.letters + string.digits
63                 passwdLength = 10
64                 return ''.join(Random().sample(passwdChars, passwdLength)) 
65
66         def _lock(self):
67                 Log.w("Removing password for %s" % (self._user))
68                 self._password = ""
69                 self._wasLocked = True
70                 self._container.execute("passwd -l %s" % self._user)
71
72         def _commandFinished(self,retval):
73                 if retval==0:
74                         type=MessageBox.TYPE_INFO
75                         windowTitle=_("Password changed")
76                         if self._password:
77                                 message = _("The password for '%s' was successfully changed to:\n\n%s") % (self._user, self._password)
78                         else:
79                                 type = MessageBox.TYPE_WARNING
80                                 if self._wasLocked:
81                                         windowTitle=_("Password locked")
82                                         message = _("The password for '%s' is now disabled!") % (self._user,)
83                                 else:
84                                         windowTitle=_("Password removed")
85                                         message = _("The password protection for '%s' was removed!") %(self._user,)
86                 else:
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)
91                 self.close()
92
93         def _onPasswordInputFinished(self, password):
94                 if password:
95                         self._apply(password)
96
97 def startChange(menuid):
98         if menuid != "system": 
99                 return [ ]
100         return [(_("Password"), main, "change_root_passwd", 50)]
101
102 def main(session, **kwargs):
103         session.open(ChangePasswordScreen)
104
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)