aboutsummaryrefslogtreecommitdiff
blob: e269b80d3431bfbbf2560133354f5c44edc74331 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#
# userauth_text.py: text mode authentication setup dialogs
#
# Copyright (C) 2000, 2001, 2002, 2008  Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from snack import *
from constants_text import *
import cracklib

from constants import *
import gettext
_ = lambda x: gettext.ldgettext("anaconda", x)

class RootPasswordWindow:
    def __call__ (self, screen, anaconda):
        toplevel = GridFormHelp(screen, _("Root Password"), "rootpw", 1, 3)

        toplevel.add(TextboxReflowed(37,
                                     _("Pick a root password. You must "
                                       "type it twice to ensure you know "
                                       "it and do not make a typing "
                                       "mistake. ")),
                     0, 0, (0, 0, 0, 1))

        if anaconda.users.rootPassword["isCrypted"]:
            anaconda.users.rootPassword["password"] = ""

        entry1 = Entry(24, password=1,
                       text=anaconda.users.rootPassword["password"])
        entry2 = Entry(24, password=1,
                       text=anaconda.users.rootPassword["password"])
        passgrid = Grid(2, 2)
        passgrid.setField(Label(_("Password:")), 0, 0, (0, 0, 1, 0),
                          anchorLeft=1)
        passgrid.setField(Label(_("Password (confirm):")), 0, 1, (0, 0, 1, 0),
                          anchorLeft=1)
        passgrid.setField(entry1, 1, 0)
        passgrid.setField(entry2, 1, 1)
        toplevel.add(passgrid, 0, 1, (0, 0, 0, 1))

        bb = ButtonBar(screen, (TEXT_OK_BUTTON, TEXT_BACK_BUTTON))
        toplevel.add(bb, 0, 2, growx = 1)

        while 1:
            toplevel.setCurrent(entry1)
            result = toplevel.run()
            rc = bb.buttonPressed(result)
            if rc == TEXT_BACK_CHECK:
                screen.popWindow()
                return INSTALL_BACK
            if len(entry1.value()) < 6:
                ButtonChoiceWindow(screen, _("Password Length"),
                    _("The root password must be at least 6 characters long."),
                    buttons = [ TEXT_OK_BUTTON ], width = 50)
            elif entry1.value() != entry2.value():
                ButtonChoiceWindow(screen, _("Password Mismatch"),
                    _("The passwords you entered were different. Please "
                      "try again."), buttons = [ TEXT_OK_BUTTON ], width = 50)
            elif self.hasBadChars(entry1.value()):
                ButtonChoiceWindow(screen, _("Error with Password"),
                    _("Requested password contains non-ASCII characters, "
                      "which are not allowed."),
                    buttons = [ TEXT_OK_BUTTON ], width = 50)
            else:
                try:
                    cracklib.FascistCheck(entry1.value())
                except ValueError, e:
                    msg = gettext.ldgettext("cracklib", e)
                    ret = anaconda.intf.messageWindow(_("Weak Password"),
                             _("You have provided a weak password: %s\n\n"
                               "Would you like to continue with this password?"
                               % (msg, )),
                             type = "yesno", default="no")
                    if ret == 1:
                        break
                else:
                    break

            entry1.set("")
            entry2.set("")

        screen.popWindow()
        anaconda.users.rootPassword["password"] = entry1.value()
        anaconda.users.rootPassword["isCrypted"] = False
        return INSTALL_OK

    def hasBadChars(self, pw):
        allowed = string.digits + string.ascii_letters + \
                  string.punctuation + " "
        for letter in pw:
            if letter not in allowed:
                return True
        return False

class UserPasswordWindow:
    def __call__ (self, screen, anaconda):
        toplevel = GridFormHelp(screen, _("User configuration"), "rootpw", 1, 3)

        toplevel.add(TextboxReflowed(37,
                                     _("Setup a username for regular "
                                       "(non-administrative) use.")),
                     0, 0, (0, 0, 0, 1))

        from gentoo.const import LIVE_USER
        live_user_data = anaconda.users.otherUsers.get(LIVE_USER, {})

        entry_username = Entry(24, text=live_user_data.get("username", ''))
        entry_fullname = Entry(24, text=live_user_data.get("fullname", ''))

        entry1 = Entry(24, password=1,
                       text=live_user_data.get("password", ''))
        entry2 = Entry(24, password=1,
                       text=live_user_data.get("password", ''))
        passgrid = Grid(2, 4)

        passgrid.setField(Label(_("Username:")), 0, 0, (0, 0, 1, 0),
                          anchorLeft=1)
        passgrid.setField(Label(_("Full name:")), 0, 1, (0, 0, 1, 0),
                          anchorLeft=1)
        passgrid.setField(Label(_("Password:")), 0, 2, (0, 0, 1, 0),
                          anchorLeft=1)
        passgrid.setField(Label(_("Password (confirm):")), 0, 3, (0, 0, 1, 0),
                          anchorLeft=1)

        passgrid.setField(entry_username, 1, 0)
        passgrid.setField(entry_fullname, 1, 1)
        passgrid.setField(entry1, 1, 2)
        passgrid.setField(entry2, 1, 3)

        toplevel.add(passgrid, 0, 1, (0, 0, 0, 1))

        bb = ButtonBar(screen, (TEXT_OK_BUTTON, TEXT_BACK_BUTTON))
        toplevel.add(bb, 0, 2, growx = 1)

        while 1:
            clean_pass = True
            toplevel.setCurrent(entry1)
            result = toplevel.run()
            rc = bb.buttonPressed(result)
            if rc == TEXT_BACK_CHECK:
                screen.popWindow()
                return INSTALL_BACK
            if len(entry1.value()) < 6:
                ButtonChoiceWindow(screen, _("Password Length"),
                    _("User password must be at least 6 characters long."),
                    buttons = [ TEXT_OK_BUTTON ], width = 50)
            elif entry1.value() != entry2.value():
                ButtonChoiceWindow(screen, _("Password Mismatch"),
                    _("The passwords you entered were different. Please "
                      "try again."), buttons = [ TEXT_OK_BUTTON ], width = 50)
            elif self.hasBadChars(entry1.value()):
                ButtonChoiceWindow(screen, _("Error with Password"),
                    _("Requested password contains non-ASCII characters, "
                      "which are not allowed."),
                    buttons = [ TEXT_OK_BUTTON ], width = 50)

            elif len(entry_username.value()) < 2:
                anaconda.intf.messageWindow(_("Error with username"),
                                        _("Username too short"),
                                        custom_icon="error")
                clean_pass = False
            elif self.isUsernameAlreadyAvailable(entry_username.value()):
                self.intf.messageWindow(_("Error with username"),
                                    _("Requested username is already taken."),
                                    custom_icon="error")
                self.usernameError()

            if self.hasBadChars(entry_username.value(), spaces = False):
                anaconda.intf.messageWindow(_("Error with username"),
                                        _("Requested username contains "
                                          "non-ASCII characters or spaces, which are "
                                          "not allowed."),
                                        custom_icon="error")
                clean_pass = False
            else:
                try:
                    cracklib.FascistCheck(entry1.value())
                except ValueError, e:
                    msg = gettext.ldgettext("cracklib", e)
                    ret = anaconda.intf.messageWindow(_("Weak Password"),
                             _("You have provided a weak password: %s\n\n"
                               "Would you like to continue with this password?"
                               % (msg, )),
                             type = "yesno", default="no")
                    if ret == 1:
                        break
                else:
                    break

            if clean_pass:
                entry1.set("")
                entry2.set("")

        screen.popWindow()

        import grp
        def get_all_groups(user):
            for group in grp.getgrall():
                if user in group.gr_mem:
                    yield group.gr_name

        user_data = {
            'fullname': entry_fullname.value(),
            'password': entry1.value(),
            'username': entry_username.value(),
            'groups': list(get_all_groups(LIVE_USER)),
        }
        anaconda.users.otherUsers[LIVE_USER] = user_data

        return INSTALL_OK

    def hasBadChars(self, pw, spaces = True):
        allowed = string.digits + string.ascii_letters + \
                  string.punctuation
        if spaces:
            allowed += " "
        for letter in pw:
            if letter not in allowed:
                return True
        return False

    def isUsernameAlreadyAvailable(self, username):
        import pwd
        return username in [x.pw_name for x in pwd.getpwall()]