initial check-in
[vuplus_dvbapp-plugin] / setpasswd / src / plugin.py
1 from enigma import eConsoleAppContainer
2
3 from Screens.Screen import Screen
4 from Screens.MessageBox import MessageBox
5
6 from Screens.Setup import SetupSummary
7 from Components.ConfigList import ConfigList
8 from Components.config import config, getConfigListEntry, ConfigSelection, ConfigSubsection, ConfigText
9
10 from Components.ActionMap import ActionMap
11 from Components.Label import Label
12 from Components.Pixmap import Pixmap
13 from Components.Sources.StaticText import StaticText
14 from Components.Sources.List import List
15 from Plugins.Plugin import PluginDescriptor
16
17 import string
18 import sys 
19 import time
20 from random import Random 
21
22 title=_("Change Root Password")
23
24 class ChangePasswdScreen(Screen):
25         skin = """
26                 <screen position="110,145" size="490,260" title="%s" >
27                 <widget name="passwd" position="10,10" size="470,200" scrollbarMode="showOnDemand" />
28                 <ePixmap pixmap="skin_default/div-h.png" position="10,205" size="470,2" transparent="1" alphatest="on" />
29                 <ePixmap pixmap="skin_default/buttons/red.png" position="10,210" size="150,40" alphatest="on" />
30                 <ePixmap pixmap="skin_default/buttons/green.png" position="170,210" size="150,40" alphatest="on" />
31                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="330,210" size="150,40" alphatest="on" />
32                 <widget source="key_red" render="Label" position="10,210" zPosition="1" size="150,40" font="Regular;19" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
33                 <widget source="key_green" render="Label" position="170,210" zPosition="1" size="150,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
34                 <widget source="key_yellow" render="Label" position="330,210" zPosition="1" size="150,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
35         </screen>""" % _("Change Root Password")
36         
37         def __init__(self, session, args = 0):
38                 Screen.__init__(self, session)
39                 self.skin = ChangePasswdScreen.skin
40
41                 self.user="root"
42                 self.output_line = ""
43                 
44                 self.password=self.GeneratePassword()
45                 self.list = []
46                 self.list.append(getConfigListEntry(_('Enter new Password'), ConfigText(default = self.password, fixed_size = False)))      
47
48                 self["passwd"] = ConfigList(self.list)
49                 self["key_red"] = StaticText(_("Cancel"))
50                 self["key_green"] = StaticText(_("Set Password"))
51                 self["key_yellow"] = StaticText(_("new Random"))
52
53                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
54                                 {
55                                                 "green": self.SetPasswd,
56                                                 "red": self.close,
57                                                 "yellow": self.buildList,
58                                                 "cancel": self.close
59                                 }, -1)
60
61         def buildList(self):
62                 self.password=self.GeneratePassword()
63                 self.list = []
64                 self.list.append(getConfigListEntry(_('Enter new Password'), ConfigText(default = self.password, fixed_size = False)))
65                 self["passwd"].setList(self.list)
66                 
67         def GeneratePassword(self): 
68                 passwdChars = string.letters + string.digits
69 #               passwdChars = string.letters + string.digits + '~!@#$%^&*-_=+?'   # use for more security :)
70                 passwdLength = 8
71                 return ''.join(Random().sample(passwdChars, passwdLength)) 
72
73         def SetPasswd(self):
74                 print "Changing password for %s to %s" % (self.user,self.password) 
75                 self.container = eConsoleAppContainer()
76                 self.container.appClosed.append(self.runFinished)
77                 self.container.dataAvail.append(self.dataAvail)
78                 retval = self.container.execute("passwd %s" % self.user)
79                 if retval==0:
80                         self.session.open(MessageBox, _("Sucessfully changed password for root user to:\n%s " % self.password), MessageBox.TYPE_INFO)   
81                 else:
82                         self.session.open(MessageBox, _("Unable to change/reset password for root user"), MessageBox.TYPE_ERROR)        
83
84         def dataAvail(self,data):
85                 self.output_line += data
86                 while True:
87                         i = self.output_line.find('\n')
88                         if i == -1:
89                                 break
90                         self.processOutputLine(self.output_line[:i+1])
91                         self.output_line = self.output_line[i+1:]
92
93         def processOutputLine(self,line):
94                 if line.find('password: '):
95                         self.container.write("%s\n"%self.password)
96
97         def runFinished(self,retval):
98                 del self.container.dataAvail[:]
99                 del self.container.appClosed[:]
100                 del self.container
101                 self.close()
102
103 def startChange(menuid):
104         if menuid != "system": 
105                 return [ ]
106         return [(title, main, "change_root_passwd", 50)]
107
108 def main(session, **kwargs):
109         session.open(ChangePasswdScreen)
110
111 def Plugins(**kwargs):
112         return PluginDescriptor(
113                 name=title, 
114                 description=_("Change or reset the root password of your dreambox"),
115                 where = [PluginDescriptor.WHERE_MENU], fnc = startChange)
116