From 614b78470c5ed3c701abdd32bc65412f363a6fc2 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Tue, 4 Sep 2012 12:01:53 +0300 Subject: added restarter project (code wcreated to restart critical services on server without root access) --- client/main.cpp | 11 ++ client/mainwindow.cpp | 204 +++++++++++++++++++++++++ client/mainwindow.h | 55 +++++++ client/mainwindow.ui | 100 +++++++++++++ client/resources.qrc | 7 + client/restarter.pro | 29 ++++ empty | 0 server/main.cpp | 305 ++++++++++++++++++++++++++++++++++++++ server/restarter_server.project | 106 +++++++++++++ server/restarter_server.workspace | 12 ++ 10 files changed, 829 insertions(+) create mode 100644 client/main.cpp create mode 100644 client/mainwindow.cpp create mode 100644 client/mainwindow.h create mode 100644 client/mainwindow.ui create mode 100644 client/resources.qrc create mode 100644 client/restarter.pro delete mode 100644 empty create mode 100644 server/main.cpp create mode 100644 server/restarter_server.project create mode 100644 server/restarter_server.workspace diff --git a/client/main.cpp b/client/main.cpp new file mode 100644 index 0000000..9ae175b --- /dev/null +++ b/client/main.cpp @@ -0,0 +1,11 @@ +#include +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/client/mainwindow.cpp b/client/mainwindow.cpp new file mode 100644 index 0000000..c6aa5cc --- /dev/null +++ b/client/mainwindow.cpp @@ -0,0 +1,204 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); +#ifdef MINIMAL + ui->halt->hide(); + ui->reboot->hide(); + ui->reboot->hide(); + ui->restart_cups->hide(); + ui->restart_ppp->hide(); + ui->state_lbl->hide(); + this->setFixedSize(ui->restart_vbox_btn->geometry().width() +17, ui->restart_vbox_btn->geometry().height() +17); +#else + this->setFixedSize(this->size()); +#endif + ui->state_lbl->setText(" "); +} + +QSslSocket *MainWindow::get_socket() +{ + QSslSocket *s = new QSslSocket; + QFile in(":/res/ca.crt"); + in.open(QIODevice::ReadOnly); + QByteArray buf = in.readAll(); + in.close(); + QList ca_list; + ca_list.push_back(QSslCertificate(buf)); + s->setCaCertificates(ca_list); + in.setFileName(":/res/client.crt"); + in.open(QIODevice::ReadOnly); + buf = in.readAll(); + in.close(); + QSslCertificate c(buf); + s->setLocalCertificate(c); + in.setFileName(":/res/client.key"); + in.open(QIODevice::ReadOnly); + buf = in.readAll(); + QSslKey key(buf, QSsl::Rsa); + s->setPrivateKey(key); + s->setPeerVerifyMode(QSslSocket::VerifyPeer); + s->setProtocol(QSsl::SslV3); + connect(s, SIGNAL(peerVerifyError(QSslError)), this, SLOT(ssl_verify_error_handler(QSslError))); + + return s; +} + +void MainWindow::ssl_verify_error_handler(const QSslError error) +{ + switch(error.error()) + { + case QSslError::InvalidCaCertificate: case QSslError::NoPeerCertificate: case QSslError::UnspecifiedError: + case QSslError::AuthorityIssuerSerialNumberMismatch: + ui->state_lbl->setText(QString::fromUtf8("Ошибка соединения !")); + sock->abort(); + return; + default: + break; + } + sock->ignoreSslErrors(); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::enable_buttons(bool enable) +{ + ui->restart_vbox_btn->setEnabled(enable); + ui->reboot->setEnabled(enable); + ui->halt->setEnabled(enable); + ui->restart_cups->setEnabled(enable); + ui->restart_ppp->setEnabled(enable); + ui->restart_vbox_btn->setEnabled(enable); +} + + +void MainWindow::on_restart_vbox_btn_clicked() +{ + ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); + sock = get_socket(); + ui->state_lbl->setText(QString::fromUtf8("Соединение..")); + sock->connectToHostEncrypted(host, 1313); + connect(sock, SIGNAL(encrypted()), SLOT(restart_vbox_sock_connected())); + connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); +} + +void MainWindow::restart_vbox_sock_connected() +{ + ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); + enable_buttons(false); + sock->write("restart vbox\0"); +} +void MainWindow::reboot() +{ + ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); + enable_buttons(false); + sock->write("reboot now\0"); +} + +void MainWindow::halt() +{ + ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); + enable_buttons(false); + sock->write("halt now\0"); +} + +void MainWindow::restart_cups() +{ + ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); + enable_buttons(false); + sock->write("restart cups\0"); +} + +void MainWindow::restart_ppp() +{ + ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); + enable_buttons(false); + sock->write("restart ppp\0"); +} + + +void MainWindow::disconnected() +{ + enable_buttons(true); + ui->state_lbl->setText(QString::fromUtf8(" ")); + sock->deleteLater(); +} + +void MainWindow::on_reboot_clicked() +{ +#ifndef MINIMAL + QMessageBox msg; + msg.setText(QString::fromUtf8("Вы видели куда ткнули ?")); + msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + if(msg.exec() == QMessageBox::Yes) + { + QMessageBox msg; + msg.setText(QString::fromUtf8("Уверены что хотите перезагрузить сервер ?")); + msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + if(msg.exec() == QMessageBox::Yes) + { + ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); + sock = get_socket(); + ui->state_lbl->setText(QString::fromUtf8("Соединение..")); + sock->connectToHostEncrypted(host, 1313); + connect(sock, SIGNAL(encrypted()), SLOT(reboot())); + connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); + } + } +#endif +} + +void MainWindow::on_halt_clicked() +{ +#ifndef MINIMAL + QMessageBox msg; + msg.setText(QString::fromUtf8("Вы видели куда ткнули ?")); + msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + if(msg.exec() == QMessageBox::Yes) + { + QMessageBox msg; + msg.setText(QString::fromUtf8("Уверены что хотите выключить сервер ?")); + msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + if(msg.exec() == QMessageBox::Yes) + { + ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); + sock = get_socket(); + ui->state_lbl->setText(QString::fromUtf8("Соединение..")); + sock->connectToHostEncrypted(host, 1313); + connect(sock, SIGNAL(encrypted()), SLOT(halt())); + connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); + } + } +#endif +} + +void MainWindow::on_restart_cups_clicked() +{ +#ifndef MINIMAL + ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); + sock = get_socket(); + ui->state_lbl->setText(QString::fromUtf8("Соединение..")); + sock->connectToHostEncrypted(host, 1313); + connect(sock, SIGNAL(encrypted()), SLOT(restart_cups())); + connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); +#endif +} + +void MainWindow::on_restart_ppp_clicked() +{ +#ifndef MINIMAL + ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); + sock = get_socket(); + ui->state_lbl->setText(QString::fromUtf8("Соединение..")); + sock->connectToHostEncrypted(host, 1313); + connect(sock, SIGNAL(encrypted()), SLOT(restart_ppp())); + connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); +#endif +} diff --git a/client/mainwindow.h b/client/mainwindow.h new file mode 100644 index 0000000..dea9301 --- /dev/null +++ b/client/mainwindow.h @@ -0,0 +1,55 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include +#include +#include + +#ifdef MINIMAL +const QString host = "192.168.0.1"; +#else +const QString host = "gluzskaya.ru"; +#endif + +namespace Ui { + class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +protected slots: + void ssl_verify_error_handler(const QSslError); + +private slots: + void on_restart_vbox_btn_clicked(); + void restart_vbox_sock_connected(); + void reboot(); + void halt(); + void restart_cups(); + void restart_ppp(); + void disconnected(); + + void on_reboot_clicked(); + + void on_halt_clicked(); + + void on_restart_cups_clicked(); + + void on_restart_ppp_clicked(); + +private: + void enable_buttons(bool); + QSslSocket *get_socket(); + Ui::MainWindow *ui; + QSslSocket *sock; +}; + +#endif // MAINWINDOW_H diff --git a/client/mainwindow.ui b/client/mainwindow.ui new file mode 100644 index 0000000..5894198 --- /dev/null +++ b/client/mainwindow.ui @@ -0,0 +1,100 @@ + + + MainWindow + + + + 0 + 0 + 685 + 389 + + + + Окно + + + + + + 10 + 10 + 181 + 25 + + + + Перезапустить vm (1c) + + + + + + 520 + 320 + 161 + 25 + + + + Перезагрузить сервер + + + + + + 520 + 350 + 161 + 25 + + + + Выключить серверер + + + + + + 10 + 40 + 231 + 25 + + + + Перезапустить сервер печати + + + + + + 10 + 70 + 191 + 25 + + + + Перезапустить интернет + + + + + + 10 + 370 + 491 + 16 + + + + TextLabel + + + + + + + + diff --git a/client/resources.qrc b/client/resources.qrc new file mode 100644 index 0000000..34e2d9e --- /dev/null +++ b/client/resources.qrc @@ -0,0 +1,7 @@ + + + res/ca.crt + res/client.key + res/client.crt + + diff --git a/client/restarter.pro b/client/restarter.pro new file mode 100644 index 0000000..31052b5 --- /dev/null +++ b/client/restarter.pro @@ -0,0 +1,29 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2011-12-15T07:02:21 +# +#------------------------------------------------- + +QT += core gui network + +TARGET = restarter +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp + +HEADERS += mainwindow.h + +FORMS += mainwindow.ui + +#DEFINES += MINIMAL + +QMAKE_CXXFLAGS += -Os -fomit-frame-pointer -std=gnu++0x +QMAKE_CFLAGS += -Os -fomit-frame-pointer -std=gnu99 + +LIBS += -Wl,-O1 + +RESOURCES += \ + resources.qrc + diff --git a/empty b/empty deleted file mode 100644 index e69de29..0000000 diff --git a/server/main.cpp b/server/main.cpp new file mode 100644 index 0000000..240ee22 --- /dev/null +++ b/server/main.cpp @@ -0,0 +1,305 @@ +// +// async_tcp_echo_server.cpp +// ~~~~~~~~~~~~~~~~~~~~~~~~~ +// +// Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +using boost::asio::ip::tcp; +typedef boost::asio::ssl::stream ssl_socket; + +class session +{ +public: + session(boost::asio::io_service& io_service, + boost::asio::ssl::context& context) + : socket_(io_service, context) { + } + + ssl_socket::lowest_layer_type& socket() + { + return socket_.lowest_layer(); + } + + void handle_handshake(const boost::system::error_code& error) + { + if (!error) + { + socket_.async_read_some(boost::asio::buffer(data_, max_length), + boost::bind(&session::handle_read, this, + boost::asio::placeholders::error, + boost::asio::placeholders::bytes_transferred)); + } + else + { + delete this; + } + } + + void start() + { + socket_.async_handshake(boost::asio::ssl::stream_base::server, + boost::bind(&session::handle_handshake, this, + boost::asio::placeholders::error)); + } + +private: + void handle_read(const boost::system::error_code& error, + size_t bytes_transferred) + { + if (!error) + { +// std::cout<<"recieved: "<socket(), + boost::bind(&server::handle_accept, this, new_session, + boost::asio::placeholders::error)); + } + std::string get_password() const + { + return ""; + } + + void handle_accept(session* new_session, + const boost::system::error_code& error) + { + if (!error) + { + new_session->start(); + } + else + { + delete new_session; + } + + start_accept(); + } + + boost::asio::io_service& io_service_; + boost::asio::ip::tcp::acceptor acceptor_; + boost::asio::ssl::context context_; +}; + +extern "C" void handle_term(int i) +{ + remove("/var/run/restarter_server.pid"); + exit(1); +} + +int main(int argc, char* argv[]) +{ + if(geteuid()) + { + std::cout<<"Program must be runned with root privilegies\n"; + exit(EXIT_FAILURE); + } + { + std::ifstream in_pid; + in_pid.open("/var/run/restarter_server.pid"); + if(in_pid.is_open() && in_pid.good()) + { + char szpid[16]; + in_pid.read(szpid, 16); + if(!strcmp(szpid, "0")) + remove("/var/run/restarter_server.pid"); + else + { + std::string cmd = "kill "; + for(int i = 0; i < 16 && szpid[i] != '\n' && szpid[i] != '\0'; i++) + cmd.push_back(szpid[i]); + system(cmd.c_str()); + remove("/var/run/restarter_server.pid"); + } + } + } + pid_t pid; + pid = fork(); + if(pid < 0) + { + std::cerr<<"Failed to fork\n"; + exit(EXIT_FAILURE); + } + if(pid > 0) + { + std::cerr<<"Successfuly forked\n"; + std::ofstream out; + out.open("/var/run/restarter_server.pid"); + char szpid[16]; + snprintf(szpid, 15, "%d", pid); + out< + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + None + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + None + + + + + + + + + + + + + + diff --git a/server/restarter_server.workspace b/server/restarter_server.workspace new file mode 100644 index 0000000..9ce2648 --- /dev/null +++ b/server/restarter_server.workspace @@ -0,0 +1,12 @@ + + + + + + + + + + + + -- cgit v1.2.3