chiark / gitweb /
polkit: don't spawn local client if we access a remote system
[elogind.git] / src / hostname / hostnamectl.c
index d8657fa4dd76e0d6820aeee5e2b96d9e129473ad..523177b27d4d412d54d7734c25130956a1aea4a4 100644 (file)
@@ -48,6 +48,18 @@ static bool arg_transient = false;
 static bool arg_pretty = false;
 static bool arg_static = false;
 
+static void polkit_agent_open_if_enabled(void) {
+
+        /* Open the polkit agent as a child process if necessary */
+        if (!arg_ask_password)
+                return;
+
+        if (arg_transport != BUS_TRANSPORT_LOCAL)
+                return;
+
+        polkit_agent_open();
+}
+
 typedef struct StatusInfo {
         char *hostname;
         char *static_hostname;
@@ -192,12 +204,9 @@ static int show_status(sd_bus *bus, char **args, unsigned n) {
 
 static int set_simple_string(sd_bus *bus, const char *method, const char *value) {
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        static bool first=true;
         int r = 0;
 
-        if (first && arg_ask_password)
-                polkit_agent_open();
-        first = false;
+        polkit_agent_open_if_enabled();
 
         r = sd_bus_call_method(
                         bus,
@@ -314,13 +323,13 @@ static int parse_argv(int argc, char *argv[]) {
         static const struct option options[] = {
                 { "help",            no_argument,       NULL, 'h'                 },
                 { "version",         no_argument,       NULL, ARG_VERSION         },
-                { "transient",       no_argument,       NULL, ARG_TRANSIENT   },
-                { "static",          no_argument,       NULL, ARG_STATIC      },
-                { "pretty",          no_argument,       NULL, ARG_PRETTY      },
+                { "transient",       no_argument,       NULL, ARG_TRANSIENT       },
+                { "static",          no_argument,       NULL, ARG_STATIC          },
+                { "pretty",          no_argument,       NULL, ARG_PRETTY          },
                 { "host",            required_argument, NULL, 'H'                 },
                 { "machine",         required_argument, NULL, 'M'                 },
                 { "no-ask-password", no_argument,       NULL, ARG_NO_ASK_PASSWORD },
-                { NULL,              0,                 NULL, 0                   }
+                {}
         };
 
         int c;
@@ -333,8 +342,7 @@ static int parse_argv(int argc, char *argv[]) {
                 switch (c) {
 
                 case 'h':
-                        help();
-                        return 0;
+                        return help();
 
                 case ARG_VERSION:
                         puts(PACKAGE_STRING);
@@ -371,8 +379,7 @@ static int parse_argv(int argc, char *argv[]) {
                         return -EINVAL;
 
                 default:
-                        log_error("Unknown option code %c", c);
-                        return -EINVAL;
+                        assert_not_reached("Unhandled option");
                 }
         }