Skip to content

Commit

Permalink
Shell Execution Prompts (#1955)
Browse files Browse the repository at this point in the history
  • Loading branch information
fundies committed Apr 5, 2020
1 parent 7b5f424 commit 19afb44
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 25 deletions.
5 changes: 5 additions & 0 deletions ENIGMAsystem/SHELL/Platforms/General/PFmain.h
Expand Up @@ -45,6 +45,11 @@ namespace enigma {
void set_room_speed(int rs);
}

namespace enigma_insecure {
void execute_shell(std::string operation, std::string fname, std::string args, bool wait = false);
void execute_shell_for_output(const std::string &command, std::string& res);
}

namespace enigma_user {

extern std::string working_directory;
Expand Down
57 changes: 39 additions & 18 deletions ENIGMAsystem/SHELL/Platforms/General/POSIX/shell.cpp
@@ -1,36 +1,57 @@
#include "Platforms/General/PFmain.h"
#include "Widget_Systems/widgets_mandatory.h"

static bool asked_if_idiot = false;
static bool confirmed_idiot = false;

static void ask_if_idiot() {
if (!asked_if_idiot) {
if (enigma_user::show_question("The following game runs shell functions that are almost always unnecessary\
and can potentionally cause irreparable harm to your system such as deleting you home folder.\
We advise only enabling these functions after careful inspection of the game's source code\
Would you like to enable these dangerous funtions?")) { confirmed_idiot = true; }
asked_if_idiot = true;
}
}

namespace enigma_insecure {
void execute_shell(std::string operation, std::string fname, std::string args, bool wait) {
if (system(NULL)) {
system(("\"" + fname + "\" " + args + (wait ? " &" : "")).c_str());
} else {
DEBUG_MESSAGE("execute_program cannot be used as there is no command processor!", MESSAGE_TYPE::M_ERROR);
return;
}
}

void execute_shell_for_output(const std::string &command, std::string& res) {
char buffer[BUFSIZ];
FILE *pf = popen(command.c_str(), "r");
while (!feof(pf)) {
res.append(buffer, fread(&buffer, sizeof(char), BUFSIZ, pf));
}
pclose(pf);
}
}

namespace enigma_user {

void execute_shell(std::string operation, std::string fname, std::string args) {
if (system(NULL)) {
system(("\"" + fname + "\" " + args + " &").c_str());
} else {
DEBUG_MESSAGE("execute_shell cannot be used as there is no command processor!", MESSAGE_TYPE::M_ERROR);
return;
}
ask_if_idiot();
if (confirmed_idiot) enigma_insecure::execute_shell(operation, fname, args, false);
}

void execute_shell(std::string fname, std::string args) { execute_shell("", fname, args); }

void execute_program(std::string operation, std::string fname, std::string args, bool wait) {
if (system(NULL)) {
system(("\"" + fname + "\" " + args + (wait ? " &" : "")).c_str());
} else {
DEBUG_MESSAGE("execute_program cannot be used as there is no command processor!", MESSAGE_TYPE::M_ERROR);
return;
}
ask_if_idiot();
if (confirmed_idiot) enigma_insecure::execute_shell(operation, fname, args, wait);
}

std::string execute_shell_for_output(const std::string &command) {
ask_if_idiot();
std::string res;
char buffer[BUFSIZ];
FILE *pf = popen(command.c_str(), "r");
while (!feof(pf)) {
res.append(buffer, fread(&buffer, sizeof(char), BUFSIZ, pf));
}
pclose(pf);
if (confirmed_idiot) enigma_insecure::execute_shell_for_output(command, res);
return res;
}

Expand Down
5 changes: 5 additions & 0 deletions ENIGMAsystem/SHELL/Widget_Systems/GTK+/dialogs.cpp
Expand Up @@ -40,6 +40,11 @@ void show_debug_message(string errortext, MESSAGE_TYPE type) {
//TODO: Implement
}

bool show_question(std::string str) {
//TODO: Implement
return false;
}

int get_color(int defcol)
{
gdk_threads_enter();
Expand Down
2 changes: 2 additions & 0 deletions ENIGMAsystem/SHELL/Widget_Systems/widgets_mandatory.h
Expand Up @@ -73,6 +73,8 @@ namespace enigma {

namespace enigma_user {

bool show_question(std::string str);

void show_debug_message(std::string msg, MESSAGE_TYPE type = M_INFO);

// This obviously displays an error message.
Expand Down
3 changes: 0 additions & 3 deletions ENIGMAsystem/SHELL/Widget_Systems/xlib/Info/About.ey
Expand Up @@ -10,6 +10,3 @@ Author: Samuel Venable

Depends:
Build-Platforms: MacOSX, Linux, FreeBSD

Represents:
Build-Platforms: Linux, FreeBSD
5 changes: 3 additions & 2 deletions ENIGMAsystem/SHELL/Widget_Systems/xlib/kdialog.cpp
Expand Up @@ -36,7 +36,7 @@ using enigma_user::filename_name;
using enigma_user::filename_path;

#include "Platforms/General/PFmain.h"
using enigma_user::execute_shell_for_output;
using enigma_insecure::execute_shell_for_output;

#include "Platforms/General/PFwindow.h"
using enigma_user::window_get_caption;
Expand Down Expand Up @@ -65,7 +65,8 @@ static bool message_cancel = false;
static bool question_cancel = false;

static string shellscript_evaluate(string command) {
string result = execute_shell_for_output(command);
string result;
execute_shell_for_output(command, result);
if (result.back() == '\n') result.pop_back();
return result;
}
Expand Down
5 changes: 3 additions & 2 deletions ENIGMAsystem/SHELL/Widget_Systems/xlib/zenity.cpp
Expand Up @@ -36,7 +36,7 @@ using enigma_user::filename_name;
using enigma_user::filename_path;

#include "Platforms/General/PFmain.h"
using enigma_user::execute_shell_for_output;
using enigma_insecure::execute_shell_for_output;

#include "Platforms/General/PFwindow.h"
using enigma_user::window_get_caption;
Expand Down Expand Up @@ -65,7 +65,8 @@ static bool message_cancel = false;
static bool question_cancel = false;

static string shellscript_evaluate(string command) {
string result = execute_shell_for_output(command);
string result;
execute_shell_for_output(command, result);
if (result.back() == '\n') result.pop_back();
return result;
}
Expand Down

0 comments on commit 19afb44

Please sign in to comment.