dropshell release 2025.0601.1821
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled

This commit is contained in:
Your Name 2025-06-01 18:21:32 +12:00
parent ab73a47751
commit a5cf9313e9
13 changed files with 59 additions and 8 deletions

View File

@ -153,7 +153,7 @@ _get_container_logs() {
_check_required_env_vars() { _check_required_env_vars() {
local required_vars=("$@") local required_vars=("$@")
for var in "${required_vars[@]}"; do for var in "${required_vars[@]}"; do
if [ -z "${!var}" ]; then if [ -z "${!var:-}" ]; then
_die "Required environment variable $var is not set" _die "Required environment variable $var is not set"
fi fi
done done

View File

@ -118,8 +118,8 @@ _autocommandrun_file() {
echo "Restoring file ${filepath}" echo "Restoring file ${filepath}"
local file_name; local file_name;
file_name=$(basename "${filepath}") file_name=$(basename "${filepath}")
rm -f "${filepath}" || die "Unable to remove existing file ${filepath}, restore failed." rm -f "${filepath}" || return_die "Unable to remove existing file ${filepath}, restore failed."
cp "${backup_folder}/${file_name}" "${filepath}" || die "Unable to copy file ${backup_folder}/${file_name} to ${filepath}, restore failed." cp "${backup_folder}/${file_name}" "${filepath}" || return_die "Unable to copy file ${backup_folder}/${file_name} to ${filepath}, restore failed."
;; ;;
esac esac
} }

View File

@ -64,7 +64,9 @@ esac
# Function to check if a package is installed # Function to check if a package is installed
is_package_installed() { is_package_installed() {
if [ "$OS" = "Alpine Linux" ]; then if [ "$OS" = "Alpine Linux" ]; then
apk info | grep -q "^$1$" # Use apk info <pkg> and check exit status
apk info "$1" >/dev/null 2>&1
return $?
else else
dpkg -l "$1" 2>/dev/null | grep -q "^ii" dpkg -l "$1" 2>/dev/null | grep -q "^ii"
fi fi

View File

@ -103,6 +103,11 @@ namespace dropshell
if (server_name.empty() || template_name.empty() || service_name.empty()) if (server_name.empty() || template_name.empty() || service_name.empty())
return false; return false;
if (!legal_service_name(service_name)) {
error << "Service name contains illegal characters: " << service_name << std::endl;
return false;
}
ServerConfig server_info(server_name); ServerConfig server_info(server_name);
if (!server_info.is_valid()) if (!server_info.is_valid())
{ {

View File

@ -94,11 +94,11 @@ int edit_config()
std::string config_file = localfile::dropshell_json(); std::string config_file = localfile::dropshell_json();
if (!edit_file(config_file, false) || !std::filesystem::exists(config_file)) if (!edit_file(config_file, false) || !std::filesystem::exists(config_file))
return die("Failed to edit config file."); return return_die("Failed to edit config file.");
gConfig().load_config(); gConfig().load_config();
if (!gConfig().is_config_set()) if (!gConfig().is_config_set())
return die("Failed to load and parse edited config file!"); return return_die("Failed to load and parse edited config file!");
gConfig().save_config(true); gConfig().save_config(true);

View File

@ -61,6 +61,12 @@ namespace dropshell
return false; return false;
} }
if (!legal_service_name(service))
{
error << "Service name contains illegal characters: " << service << std::endl;
return false;
}
LocalServiceInfo sinfo = get_service_info(server, service); LocalServiceInfo sinfo = get_service_info(server, service);
if (!SIvalid(sinfo)) if (!SIvalid(sinfo))
{ {

View File

@ -51,6 +51,12 @@ namespace dropshell
return false; return false;
} }
if (!legal_service_name(service))
{
error << "Service name contains illegal characters: " << service << std::endl;
return false;
}
// run the start script. // run the start script.
bool started = server_env.run_remote_template_command(service, "start", {}, false, {}); bool started = server_env.run_remote_template_command(service, "start", {}, false, {});

View File

@ -51,6 +51,12 @@ namespace dropshell
return false; return false;
} }
if (!legal_service_name(service))
{
error << "Service name contains illegal characters: " << service << std::endl;
return false;
}
// run the stop script. // run the stop script.
bool stopped = server_env.run_remote_template_command(service, "stop", {}, false, {}); bool stopped = server_env.run_remote_template_command(service, "stop", {}, false, {});

View File

@ -149,12 +149,20 @@ namespace dropshell
std::string get_user_for_service(const std::string &server, const std::string &service) std::string get_user_for_service(const std::string &server, const std::string &service)
{ {
if (!legal_service_name(service))
{
error << "Service name contains illegal characters: " + service << std::endl;
return "";
}
auto services_info = get_server_services_info(server); auto services_info = get_server_services_info(server);
auto it = std::find_if(services_info.begin(), services_info.end(), auto it = std::find_if(services_info.begin(), services_info.end(),
[&service](const LocalServiceInfo &si) [&service](const LocalServiceInfo &si)
{ return si.service_name == service; }); { return si.service_name == service; });
if (it != services_info.end() && SIvalid(*it)) if (it != services_info.end() && SIvalid(*it))
return it->user; return it->user;
debug << "Couldn't find user for service \"" << service << "\" on server \"" << server << "\"" << std::endl;
return ""; return "";
} }

View File

@ -80,6 +80,9 @@ namespace dropshell
if (server_name.empty() || service_name.empty()) if (server_name.empty() || service_name.empty())
return LocalServiceInfo(); return LocalServiceInfo();
if (!legal_service_name(service_name))
return LocalServiceInfo();
service.service_name = service_name; service.service_name = service_name;
service.local_service_path = localpath::service(server_name, service_name); service.local_service_path = localpath::service(server_name, service_name);

View File

@ -171,6 +171,11 @@
bool template_manager::create_template(const std::string &template_name) const bool template_manager::create_template(const std::string &template_name) const
{ {
if (!legal_service_name(template_name)) {
error << "Template name contains illegal characters: " << template_name << std::endl;
return false;
}
// 1. Create a new directory in the user templates directory // 1. Create a new directory in the user templates directory
std::vector<std::string> local_server_definition_paths = gConfig().get_local_server_definition_paths(); std::vector<std::string> local_server_definition_paths = gConfig().get_local_server_definition_paths();

View File

@ -312,7 +312,7 @@ std::string requote(std::string str) {
} }
int die(const std::string & msg) { int return_die(const std::string & msg) {
error << "Fatal error:" << std::endl; error << "Fatal error:" << std::endl;
error << msg << std::endl; error << msg << std::endl;
return 1; return 1;
@ -650,4 +650,11 @@ bool file_replace_or_add_segment(std::string filepath, std::string segment)
return true; return true;
} }
bool legal_service_name(const std::string &service_name)
{
// legal characters are alphanumeric, and - and _
std::regex legal_chars("^[a-zA-Z0-9_-]+$");
return std::regex_match(service_name, legal_chars);
}
} // namespace dropshell } // namespace dropshell

View File

@ -45,7 +45,7 @@ int count_substring(const std::string &substring, const std::string &text);
std::string random_alphanumeric_string(int length); std::string random_alphanumeric_string(int length);
int die(const std::string & msg); int return_die(const std::string & msg);
std::string safearg(int argc, char *argv[], int index); std::string safearg(int argc, char *argv[], int index);
std::string safearg(const std::vector<std::string> & args, int index); std::string safearg(const std::vector<std::string> & args, int index);
@ -75,4 +75,7 @@ constexpr unsigned int switchhash(const char *s, int off = 0)
return !s[off] ? 5381 : (switchhash(s, off + 1) * 33) ^ s[off]; return !s[off] ? 5381 : (switchhash(s, off + 1) * 33) ^ s[off];
} }
bool legal_service_name(const std::string & service_name);
} // namespace dropshell } // namespace dropshell