diff options
Diffstat (limited to 'abs/core/mythinstall/MythVantage-app/mythinstall/password_manage.cpp')
-rwxr-xr-x | abs/core/mythinstall/MythVantage-app/mythinstall/password_manage.cpp | 436 |
1 files changed, 436 insertions, 0 deletions
diff --git a/abs/core/mythinstall/MythVantage-app/mythinstall/password_manage.cpp b/abs/core/mythinstall/MythVantage-app/mythinstall/password_manage.cpp new file mode 100755 index 0000000..4acfb9d --- /dev/null +++ b/abs/core/mythinstall/MythVantage-app/mythinstall/password_manage.cpp @@ -0,0 +1,436 @@ +#include <unistd.h> +#include "password_manage.h" +//Added by qt3to4: +#include <Q3TextStream> + +#include <qapplication.h> +#include "mythmiscutil.h" +#include "mythsystemlegacy.h" + +#include <qdir.h> +#include <iostream> + +/****************************************************************************/ +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 = 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) ) + { + Q3TextStream 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(QIODevice::ReadOnly | QIODevice::Text) ) + { + Q3TextStream 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; + 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,FALSE); + if ( found_char ) + return FALSE; + } + return TRUE ; + +} + +UserManagement::UserManagement(): + VerticalConfigurationGroup(false,false,false,false) +{ + setLabel(QObject::tr("User Accounts")); + userlist = new TransComboBoxSetting(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 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 New User Account"); + usercreateButton->setHelpText(QObject::tr("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 exisit || add it + if ( user_exisit_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) +{ + setLabel(QObject::tr("Web Security Settings")); + 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(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; + bool found_char; + int i ; + //invalid_chars = "&<>/~`;"; + 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,FALSE); + 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() +{ + WebPassword *webpassword = new WebPassword(); + addChild(webpassword); +} |