summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/mythinstall/password_manage.cpp
blob: 51566369fe81a05847fe9c1f67f9ddeed17eda5f (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
435
436
437
438
439
440
441
442
443
444
445
#include <mythcontext.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include "settings.h"
#include "installsettings.h"
#include "installdialog.h"
#include "password_manage.h"
#include <qsqldatabase.h>
#include <qsqlquery.h>

#include <stdlib.h>
#include <cstdlib>
#include <mythtv/mythdbcon.h>
#include <qdir.h>
#include <qapplication.h>
#include "util.h"
#include <qregexp.h>
/****************************************************************************/
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);
    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 = QDeepCopy<QString>(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(IO_ReadOnly | IO_Translate) )
    {
        QTextStream t( &file );        // use a text stream
        while ( !t.eof() )
        {
            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_exisit_check(QString name)
{
   QString line;
   QString user;
   QString quid;
   bool founduser;
   founduser=false;
   int uid;
   QFile file("/etc/passwd");
    if ( file.open(IO_ReadOnly | IO_Translate) )
    {
        QTextStream t( &file );        // use a text stream
        while ( !t.eof() )
        {
            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;
    int found_char;
    int i ;
    upperlimit = invalid_chars.length() -1 ;
    for(i= 0; i <= upperlimit; i++)
    {
        found_char = -1;
        current_char = invalid_chars.at(i);
        found_char = check_string.contains(current_char,FALSE);
        if ( found_char  > 0  )
            return FALSE;
    }
    return TRUE ;

}

UserManagement::UserManagement():
    VerticalConfigurationGroup(false,false,false,false)
    {
      setLabel(QObject::tr("Password Management"));
      userlist = new TransComboBoxSetting(false);
      userlist->setLabel(QObject::tr("Current Accounts"));
      userlist->addSelection("root");
      userlist->addSelection("mythtv");
      user_fillselection();
      userlist->setHelpText(QObject::tr("Select the account for password reset"));

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


      passToggleButton = new TransButtonSetting;
      passToggleButton->setLabel("Toggle password view");
      passToggleButton->setHelpText(QObject::tr("Hide or show the password"));

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

       userapplyButton = new TransButtonSetting;
       userapplyButton->setLabel("Apply new password");

       usercreateButton = new TransButtonSetting;
       usercreateButton->setLabel("Create a new user account");
       usercreateButton->setHelpText(QObject::tr("The following characters will not be accepted: " + invalid_chars));

       userdeleteButton = new TransButtonSetting;
       userdeleteButton->setLabel("Delete current account");
       ConfigurationGroup *buttonlist = new GridConfigurationGroup(2,false);
            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 account.");
    rootSSH->setHelpText(QObject::tr("Checking this will enable ssh for the root account. Leave this disabled unless you know what your doing."));
    rootSSH->setValue(false);

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

    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("    Password contains invalid characters!");
         return;
    }
    info->setValue("    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(
                gContext->GetMainWindow(),
                tr(""),
                tr("Enter a new user account name"), name);
    if (result == UseraddPopup_CANCEL)
        return;
    //check if name exisit || add it
    if ( user_exisit_check(name) )
    {
        info->setValue(name + " Already present");
        userlist->setFocus();
        key = Qt::Key_Down;
        QApplication::postEvent(gContext->GetMainWindow(),
                            new ExternalKeycodeEvent(key));

        return;
    }

    if ( ! user_valid_check (name))
    {
         info->setValue("  Username 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("Now enter a password for " + name + " and press the apply button.");
    //Set focus to userlist, then press down three times to set focus to the password box.
    userlist->setFocus();

    key = Qt::Key_Down;
    QApplication::postEvent(gContext->GetMainWindow(),
                            new ExternalKeycodeEvent(key));
    key = Qt::Key_Down;
    QApplication::postEvent(gContext->GetMainWindow(),
                                new ExternalKeycodeEvent(key));
    key = Qt::Key_Down;
    QApplication::postEvent(gContext->GetMainWindow(),
                             new ExternalKeycodeEvent(key));

}

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

    DialogCode value = MythPopupBox::Show2ButtonPopup(
        gContext->GetMainWindow(), "", message,
        tr("Remove this account"),
        tr("Do not remove this account"),
        kDialogCodeButton1);

   if (kDialogCodeButton0 == value)
   {
         info->setValue("removed account: " + user);
         if ( user == "root" || user == "mythtv" )
         {
            info->setValue("Can not remove the account");
            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 account: " + user);
   }
    else
        info->setValue("Did not remove account: " + user);
}

WebPassword::WebPassword():
    TriggeredConfigurationGroup(true,false,true,true,true,true,true,true)
{

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

    webpassword = new HostLineEdit("Hostwebpassword");
    webpassword->setLabel("Password");
    webpassword->SetPasswordEcho(passtoggle);
    webpassword->setHelpText(QObject::tr("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);
        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)
{
   // QString invalid_chars;
    QChar current_char;
    int upperlimit;
    int found_char;
    int i ;
    //invalid_chars = "&<>/~`;";
    upperlimit = invalid_chars.length() -1 ;
    for(i= 0; i <= upperlimit; i++)
    {
        found_char = -1;
        current_char = invalid_chars.at(i);
        found_char = check_string.contains(current_char,FALSE);
        if ( found_char  > 0  )
            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("    Username contains invalid characters!");
         return;
    }

    if ( ! valid_check (webpass_e))
    {
         info->setValue("    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("   Changes applied");

}

WebPasswordFrame::WebPasswordFrame():
    VerticalConfigurationGroup()
{
    WebPassword *webpassword = new WebPassword();
    addChild(webpassword);
}