From ede2a7326d927aec82af5e4b9f4f1dd93d3e593d Mon Sep 17 00:00:00 2001 From: Jan Kupec Date: Thu, 12 Jul 2007 10:21:26 +0000 Subject: [PATCH] - display a non-root error message for commands which require root --- src/zypper-sources.cc | 2 +- src/zypper.cc | 159 +++++++++++++++++++++++++++++++------------------- 2 files changed, 99 insertions(+), 62 deletions(-) diff --git a/src/zypper-sources.cc b/src/zypper-sources.cc index b6c4590..2568ab7 100644 --- a/src/zypper-sources.cc +++ b/src/zypper-sources.cc @@ -435,7 +435,7 @@ void rename_repo(const std::string & alias, const std::string & newalias) catch (const Exception & ex) { cerr << _("Error while modifying the repository:") << endl; - cerr << ex.asUserString(); + cerr << ex.asUserString() << endl; cerr << format(_("Leaving repository %s unchanged.")) % alias << endl; ERR << "Error while modifying the repository:" << ex.asUserString() << endl; diff --git a/src/zypper.cc b/src/zypper.cc index 3b2805f..f8fcefc 100644 --- a/src/zypper.cc +++ b/src/zypper.cc @@ -626,14 +626,6 @@ int one_command(const ZypperCommand & command, int argc, char **argv) if (ghelp) { cout << specific_help << endl; return !ghelp; } // if (ghelp) display_command_help() - //! \todo modify according to final decition on /etc/zypp/repos.d - /* - if ( geteuid() != 0 ) - { - cerr << _("Root privileges are required for viewing system sources.") << endl; - return ZYPPER_EXIT_ERR_PRIVILEGES; - }*/ - list_repos(); return ZYPPER_EXIT_OK; } @@ -642,6 +634,19 @@ int one_command(const ZypperCommand & command, int argc, char **argv) else if (command == ZypperCommand::ADD_REPO) { + if (ghelp) + { + cout << specific_help; + return ZYPPER_EXIT_OK; + } + + // check root user + if (geteuid() != 0) + { + cerr << _("Root privileges are required for modifying system repositories.") << endl; + return ZYPPER_EXIT_ERR_PRIVILEGES; + } + tribool enabled(indeterminate); tribool refresh(indeterminate); @@ -660,20 +665,12 @@ int one_command(const ZypperCommand & command, int argc, char **argv) string type = copts.count("type") ? copts["type"].front() : ""; // display help message if insufficient info was given - if (ghelp || arguments.size() < 2) + if (arguments.size() < 2) { - if (ghelp) - { - cout << specific_help; - return ZYPPER_EXIT_OK; - } - else - { - cerr << _("Too few arguments. At least URL and alias are required.") << endl; - ERR << "Too few arguments. At least URL and alias are required." << endl; - cout_n << specific_help; - return ZYPPER_EXIT_ERR_INVALID_ARGS; - } + cerr << _("Too few arguments. At least URL and alias are required.") << endl; + ERR << "Too few arguments. At least URL and alias are required." << endl; + cout_n << specific_help; + return ZYPPER_EXIT_ERR_INVALID_ARGS; } Url url = make_url (arguments[0]); @@ -702,20 +699,25 @@ int one_command(const ZypperCommand & command, int argc, char **argv) else if (command == ZypperCommand::REMOVE_REPO) { - if (ghelp || arguments.size() < 1) + if (ghelp) { - if (ghelp) - { - cout << specific_help; - return ZYPPER_EXIT_OK; - } - else - { - cerr << _("Required argument missing.") << endl; - ERR << "Required argument missing." << endl; - cout_n << specific_help; - return ZYPPER_EXIT_ERR_INVALID_ARGS; - } + cout << specific_help; + return ZYPPER_EXIT_OK; + } + + // check root user + if (geteuid() != 0) + { + cerr << _("Root privileges are required for modifying system repositories.") << endl; + return ZYPPER_EXIT_ERR_PRIVILEGES; + } + + if (arguments.size() < 1) + { + cerr << _("Required argument missing.") << endl; + ERR << "Required argument missing." << endl; + cout_n << specific_help; + return ZYPPER_EXIT_ERR_INVALID_ARGS; } warn_if_zmd (); @@ -737,20 +739,25 @@ int one_command(const ZypperCommand & command, int argc, char **argv) else if (command == ZypperCommand::RENAME_REPO) { - if (ghelp || arguments.size() < 2) + if (ghelp) { - if (ghelp) - { - cout << specific_help; - return ZYPPER_EXIT_OK; - } - else - { - cerr << _("Too few arguments. At least URL and alias are required.") << endl; - ERR << "Too few arguments. At least URL and alias are required." << endl; - cout_n << specific_help; - return ZYPPER_EXIT_ERR_INVALID_ARGS; - } + cout << specific_help; + return ZYPPER_EXIT_OK; + } + + // check root user + if (geteuid() != 0) + { + cerr << _("Root privileges are required for modifying system repositories.") << endl; + return ZYPPER_EXIT_ERR_PRIVILEGES; + } + + if (arguments.size() < 2) + { + cerr << _("Too few arguments. At least URL and alias are required.") << endl; + ERR << "Too few arguments. At least URL and alias are required." << endl; + cout_n << specific_help; + return ZYPPER_EXIT_ERR_INVALID_ARGS; } // cond_init_target (); @@ -772,20 +779,25 @@ int one_command(const ZypperCommand & command, int argc, char **argv) else if (command == ZypperCommand::MODIFY_REPO) { - if (ghelp || arguments.size() < 1) + if (ghelp) { - if (ghelp) - { - cout << specific_help; - return ZYPPER_EXIT_OK; - } - else - { - cerr << _("Alias is a required argument.") << endl; - ERR << "Na alias argument given." << endl; - cout_n << specific_help; - return ZYPPER_EXIT_ERR_INVALID_ARGS; - } + cout << specific_help; + return ZYPPER_EXIT_OK; + } + + // check root user + if (geteuid() != 0) + { + cerr << _("Root privileges are required for modifying system repositories.") << endl; + return ZYPPER_EXIT_ERR_PRIVILEGES; + } + + if (arguments.size() < 1) + { + cerr << _("Alias is a required argument.") << endl; + ERR << "Na alias argument given." << endl; + cout_n << specific_help; + return ZYPPER_EXIT_ERR_INVALID_ARGS; } modify_repo(arguments[0], copts); @@ -795,7 +807,18 @@ int one_command(const ZypperCommand & command, int argc, char **argv) else if (command == ZypperCommand::REFRESH) { - if (ghelp) { cout << specific_help; return !ghelp; } + if (ghelp) + { + cout << specific_help; + return ZYPPER_EXIT_OK; + } + + // check root user + if (geteuid() != 0) + { + cerr << _("Root privileges are required for refreshing system repositories.") << endl; + return ZYPPER_EXIT_ERR_PRIVILEGES; + } refresh_repos(); } @@ -823,6 +846,13 @@ int one_command(const ZypperCommand & command, int argc, char **argv) gData.packages_to_uninstall = arguments; } + // check root user + if (geteuid() != 0) + { + cerr << _("Root privileges are required for installing or uninstalling packages.") << endl; + return ZYPPER_EXIT_ERR_PRIVILEGES; + } + // read resolvable type string skind = copts.count("type")? copts["type"].front() : "package"; kind = string_to_kind (skind); @@ -994,6 +1024,13 @@ int one_command(const ZypperCommand & command, int argc, char **argv) else if (command == ZypperCommand::UPDATE) { if (ghelp) { cout << specific_help; return !ghelp; } + // check root user + if (geteuid() != 0) + { + cerr << _("Root privileges are required for updating packages.") << endl; + return ZYPPER_EXIT_ERR_PRIVILEGES; + } + string skind = copts.count("type")? copts["type"].front() : gSettings.is_rug_compatible? "package" : "patch"; kind = string_to_kind (skind); -- 2.7.4