summaryrefslogtreecommitdiffstats
path: root/abs/core/mythinstall/MythVantage-app/mythinstall/password_manage.cpp
blob: 3fc0e0236f0037e7819b6977cf83c37211e53cca (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <unistd.h>
#include "password_manage.h"

#include <QTextStream>
#include <qapplication.h>
#include "mythmiscutil.h"
#include "mythsystemlegacy.h"

#include <qdir.h>
#include <iostream>
using namespace std;

/****************************************************************************/
typedef enum {
    UseraddPopup_OK = 0,
    UseraddPopup_CANCEL,
    UseraddPopup_DELETE
}
UseraddPopupResult;
bool passtoggle = true;
QString invalid_chars = "&<>/~`;:!";

class AddUserPopup
{
public:
    static UseraddPopupResult showPopup(MythMainWindow *parent, QString title,
                                        QString message, QString& text);
};

UseraddPopupResult AddUserPopup::showPopup(MythMainWindow *parent,
        QString title, QString message, QString& text)
{
    MythPopupBox *popup = new MythPopupBox(parent, title.toLatin1().constData());
    popup->addLabel(message);

    MythLineEdit *textEdit = new MythLineEdit(popup, "chooseEdit");
    textEdit->setText(text);
    popup->addWidget(textEdit);

    popup->addButton(QObject::tr("OK"),     popup, SLOT(accept()));
    popup->addButton(QObject::tr("Cancel"), popup, SLOT(reject()));

    textEdit->setFocus();

    bool ok = (MythDialog::Accepted == popup->ExecPopup());
    if (ok)
        text = textEdit->text();
    popup->hide();
    popup->deleteLater();
    return (ok) ? UseraddPopup_OK : UseraddPopup_CANCEL;
}

/****************************************************************************/


void UserManagement::user_fillselection()
{
    //only add users with UID above 1000
    QString line;
    QString user;
    QString quid;
    int uid;
    QFile file("/etc/passwd");
    if ( file.open(QIODevice::ReadOnly | QIODevice::Text) )
    {
        QTextStream t( &file );        // use a text stream
        while ( !t.atEnd() )
        {
            line = t.readLine();
            user = line.section(":",0,0);
            quid = line.section(":",2,2);
            uid=quid.toInt();
            if ( uid >= 1000 )
                userlist->addSelection(user);
        }
        file.close();
    }

}

bool UserManagement::user_exist_check(QString name)
{
    QString line;
    QString user;
    QString quid;
    bool founduser;
    founduser=false;
    int uid;
    QFile file("/etc/passwd");
    if ( file.open(QIODevice::ReadOnly | QIODevice::Text) )
    {
        QTextStream t( &file );        // use a text stream
        while ( !t.atEnd() )
        {
            line = t.readLine();
            user = line.section(":",0,0);
            quid = line.section(":",2,2);
            uid=quid.toInt();
            if ( user == name )
            {
                founduser=true;
                break;
            }
        }
        file.close();
    }

    return founduser;

}

bool UserManagement::user_valid_check(QString check_string)
{
    QChar current_char;
    int upperlimit;
    bool found_char;
    int i ;
    upperlimit = invalid_chars.length() -1 ;
    for(i= 0; i <= upperlimit; i++)
    {
        found_char = false;
        current_char = invalid_chars.at(i);
        found_char = check_string.contains(current_char, Qt::CaseInsensitive);
        if (  found_char   )
            return false;
    }
    return true ;

}

UserManagement::UserManagement()
{
    GroupSetting* UMsettings = new GroupSetting();
    UMsettings->setLabel(QObject::tr("User Accounts"));
    HostComboBoxSetting* userlist = new HostComboBoxSetting(false);
    userlist->setLabel(QObject::tr("User"));
    userlist->addSelection("root");
    userlist->addSelection("mythtv");
    user_fillselection();
    userlist->setHelpText(QObject::tr("Select the user to change the user password or delete the user account."));

    userpass1= new HostLineEditSetting(true);
    userpass1->setLabel("Password");
    userpass1->SetPasswordEcho(passtoggle);
    userpass1->setHelpText("The following characters will not be accepted: " + invalid_chars);


    passToggleButton = new ButtonStandardSetting;
    passToggleButton->setLabel("Toggle Password View");
    passToggleButton->setHelpText(QObject::tr("Hide or show the password."));

//    info = new TransLabelSetting;
//    info->setValue("");

    userapplyButton = new ButtonStandardSetting;
    userapplyButton->setLabel("Apply New Password");

    usercreateButton = new TransButtonSetting;
    usercreateButton->setLabel("Create New User Account");
    usercreateButton->setHelpText("The following characters will not be accepted: " + invalid_chars);

    userdeleteButton = new TransButtonSetting;
    userdeleteButton->setLabel("Delete User Account");
    ConfigurationGroup *buttonlist = new GridConfigurationGroup(2,false,false);
    buttonlist-> addChild(passToggleButton);
    buttonlist-> addChild(userapplyButton);
    buttonlist-> addChild(usercreateButton);
    buttonlist-> addChild(userdeleteButton);

//    ConfigurationGroup *buttonlist2 = new GridConfigurationGroup(2,false);
//    buttonlist2-> addChild(passToggleButton);
//    buttonlist2-> addChild(userapplyButton);

    rootSSH  = new HostCheckBox("HOSTrootSSH");
    rootSSH->setLabel("Enable remote access for the root user account");
    rootSSH->setHelpText(QObject::tr("If checked, ssh for the root account will be enabled. If you are unsure leave this unchecked."));
    rootSSH->setValue(false);

    addChild(userlist);
    addChild(userpass1);
    addChild(rootSSH);
//    addChild(buttonlist2);
    addChild(buttonlist);
    addChild(info);

    connect(userapplyButton, SIGNAL(pressed()), this,   SLOT(applychanges()));
    connect(userlist, SIGNAL(valueChanged(const QString&)), this,   SLOT(userchanged()));
    connect(usercreateButton, SIGNAL(pressed()), this,   SLOT(usercreatepopup()));
    connect(userdeleteButton, SIGNAL(pressed()), this, SLOT(userdeletepopup()));
    connect(passToggleButton, SIGNAL(pressed()), this, SLOT(togglepass()));
};

void UserManagement::togglepass()
{
    if ( passtoggle)
        passtoggle = false;
    else
        passtoggle = true;

    userpass1->SetPasswordEcho(passtoggle);
}

void UserManagement::applychanges()
{
    QString user;
    QString password;
    QString user_e;

    user=userlist->getValue();
    password=userpass1->getValue();

    user_e = QRegExp::escape( user );
    password = QRegExp::escape( password );
    if ( ! user_valid_check (password))
    {
        info->setValue("ERROR: The password contains invalid characters!");
        return;
    }
    info->setValue("The password has been changed for user " + user + ".");
    QString cmdtxt;
    cmdtxt="sudo ";
    cmdtxt.append("myth_user_call -c pass  -u " + user_e + " -p " + password );
    myth_system(cmdtxt);
}

void UserManagement::userchanged()
{
    info->setValue(" ");
    userpass1->setValue("");
}

void UserManagement::usercreatepopup()
{
    QString name;
    QString name_e;
    int key = 0;
    UseraddPopupResult result = AddUserPopup::showPopup(
                                    GetMythMainWindow(),
                                    tr(""),
                                    tr("Enter New User Name"), name);
    if (result == UseraddPopup_CANCEL)
        return;
    //check if name exist || add it
    if ( user_exist_check(name) )
    {
        info->setValue("User name " + name + " already exists. Cannot create user.");
        userlist->setFocus();
        key = Qt::Key_Down;
        QApplication::postEvent(GetMythMainWindow(),
                                new ExternalKeycodeEvent(key));

        return;
    }

    if ( ! user_valid_check (name))
    {
        info->setValue("ERROR: The user name contains invalid characters!");
        return;
    }
    userlist->addSelection(name);
    userlist->setValue(name);
    //run program to add user right here
    QString cmdtxt;
    name_e = QRegExp::escape( name );

    cmdtxt="sudo ";
    cmdtxt.append("myth_user_call -c add -u " + name_e );
    myth_system(cmdtxt);

    info->setValue("Enter a password for " + name + " and press Apply New Password.");
    //Set focus to userlist, then press down three times to set focus to the password box.
    userlist->setFocus();

    key = Qt::Key_Down;
    QApplication::postEvent(GetMythMainWindow(),
                            new ExternalKeycodeEvent(key));
    key = Qt::Key_Down;
    QApplication::postEvent(GetMythMainWindow(),
                            new ExternalKeycodeEvent(key));
    key = Qt::Key_Down;
    QApplication::postEvent(GetMythMainWindow(),
                            new ExternalKeycodeEvent(key));

}

void UserManagement::userdeletepopup()
{
    QString user;
    QString user_e;
    user=userlist->getValue();
    QString message = "Remove " + user + "?" ;

    DialogCode value = MythPopupBox::Show2ButtonPopup(
                           GetMythMainWindow(), "", message,
                           tr("Remove User"),
                           tr("Do NOT Remove User"),
                           kDialogCodeButton1);

    if (kDialogCodeButton0 == value)
    {
        info->setValue("Removed user: " + user);
        if ( user == "root" || user == "mythtv" )
        {
            info->setValue("Cannot remove user: " + user);
            return;
        }
        userlist->setValue("root");
        userlist->removeSelection(user);
        //run program to remove user
        QString cmdtxt;
        user_e = QRegExp::escape( user );
        cmdtxt="sudo ";
        cmdtxt.append("myth_user_call -c delete -u " + user_e );
        myth_system(cmdtxt);
        info->setValue("Removed user: " + user);
    }
    else
        info->setValue("Did not remove user: " + user);
}

WebPassword::WebPassword():
    TriggeredConfigurationGroup(false,false,true,true,true,true,true,true)
{
    webAuth  = new HostCheckBox("HOSTwebauth");
    webAuth->setLabel("Enable Password");
    webAuth->setHelpText(QObject::tr("Use password protection for the local website."));
    webAuth->setValue(false);
    webuser = new HostLineEdit("Hostwebuser");
    webuser->setLabel("Username");
    webuser->setHelpText("The following characters will not be accepted: " + invalid_chars);

    webpassword = new HostLineEdit("Hostwebpassword");
    webpassword->setLabel("Password");
    webpassword->SetPasswordEcho(passtoggle);
    webpassword->setHelpText("The following characters will not be accepted: " + invalid_chars);

    webpassToggleButton = new TransButtonSetting;
    webpassToggleButton->setLabel("Toggle Password View");
    webpassToggleButton->setHelpText(QObject::tr("Hide or show the password."));

    webapplyButton = new TransButtonSetting;
    webapplyButton->setLabel("Apply New Password");

    info = new TransLabelSetting;
    info->setValue("");

    connect(webpassToggleButton, SIGNAL(pressed()), this, SLOT(togglepass()));
    connect(webapplyButton, SIGNAL(pressed()),this , SLOT(webpassword_gathersettings()));

    ConfigurationGroup* webpassgroup = new VerticalConfigurationGroup(false,false);
    webpassgroup->addChild(webuser);
    webpassgroup->addChild(webpassword);
    webpassgroup->addChild(info);
    webpassgroup->addChild(webpassToggleButton);
    webpassgroup->addChild(webapplyButton);

    addChild(webAuth);
    setTrigger(webAuth);

    addTarget("0", new VerticalConfigurationGroup(false, false));
    addTarget("1", webpassgroup);

}


void WebPassword::togglepass()
{
    if ( passtoggle)
        passtoggle = false;
    else
        passtoggle = true;
    webpassword->SetPasswordEcho(passtoggle);
}

bool WebPassword::valid_check(QString check_string)
{
    QChar current_char;
    int upperlimit;
    bool found_char;
    int i ;
    upperlimit = invalid_chars.length() -1 ;
    for(i= 0; i <= upperlimit; i++)
    {
        found_char = false;
        current_char = invalid_chars.at(i);
        found_char = check_string.contains(current_char, Qt::CaseInsensitive);
        if (  found_char    )
            return false;
    }
    return true ;

}

void WebPassword::webpassword_gathersettings()
{

    QString pass_enabled;
    QString web_user;
    QString web_pass;
    QString webuser_e;
    QString webpass_e;
    pass_enabled=webAuth->getValue();
    web_user=webuser->getValue();
    web_pass=webpassword->getValue();
    webuser_e = QRegExp::escape( web_user );
    webpass_e = QRegExp::escape( web_pass );
    if ( ! valid_check (webuser_e))
    {
        info->setValue("ERROR: The user name contains invalid characters!");
        return;
    }

    if ( ! valid_check (webpass_e))
    {
        info->setValue("ERROR: The password contains invalid characters!");
        return;
    }
    cout << "Running program to make the changes for web password" << endl;
    QString cmdtxt;

    cmdtxt="sudo ";
    cmdtxt.append("myth_user_call -c web -u " + webuser_e + " -p " + webpass_e );
    myth_system(cmdtxt);
    info->setValue("The user name and password have been added.");

}

WebPasswordFrame::WebPasswordFrame():
    VerticalConfigurationGroup(false,false)
    {
        setLabel(QObject::tr("Web Security Settings"));
        WebPassword *webpassword = new WebPassword();
        addChild(webpassword);
    }