[network-manager-openvpn/th/bgo556134_up_restart: 1/4] helper: improve parsing of command line arguments



commit 0d0b36daf9e9b641f36c2a9e4dc9aacba97fa1d6
Author: Thomas Haller <thaller redhat com>
Date:   Tue Mar 18 15:32:25 2014 +0100

    helper: improve parsing of command line arguments
    
    Allow a separator argument "--" to separate arguments
    provided by openvpn from the arguments provided by the
    plugin.
    
    Signed-off-by: Thomas Haller <thaller redhat com>

 src/nm-openvpn-service-openvpn-helper.c |   36 ++++++++++++++++++++++++++----
 src/nm-openvpn-service.c                |    8 +++---
 2 files changed, 35 insertions(+), 9 deletions(-)
---
diff --git a/src/nm-openvpn-service-openvpn-helper.c b/src/nm-openvpn-service-openvpn-helper.c
index 601229e..6ab56a6 100644
--- a/src/nm-openvpn-service-openvpn-helper.c
+++ b/src/nm-openvpn-service-openvpn-helper.c
@@ -57,6 +57,8 @@ extern char **environ;
 #define DBUS_TYPE_G_IP6_ROUTE              (dbus_g_type_get_struct ("GValueArray", DBUS_TYPE_G_UCHAR_ARRAY, 
G_TYPE_UINT, DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_UINT, G_TYPE_INVALID))
 #define DBUS_TYPE_G_ARRAY_OF_IP6_ROUTE     (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_IP6_ROUTE))
 
+static gboolean helper_debug = FALSE;
+
 static void
 helper_failed (DBusGConnection *connection, const char *reason)
 {
@@ -535,18 +537,27 @@ main (int argc, char *argv[])
        struct in_addr temp_addr;
        gboolean tapdev = FALSE;
        char **iter;
+       int shift = 0;
 
 #if !GLIB_CHECK_VERSION (2, 35, 0)
        g_type_init ();
 #endif
 
-       connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &err);
-       if (!connection) {
-               g_warning ("Could not get the system bus: %s", err->message);
-               exit (1);
+       if (argc > 1) {
+               for (i = 1; i < argc; i++) {
+                       if (!strcmp (argv[i], "--")) {
+                               i++;
+                               break;
+                       }
+                       if (!strcmp (argv[i], "--helper-debug"))
+                               helper_debug = TRUE;
+                       else
+                               break;
+               }
+               shift = i - 1;
        }
 
-       if (argc >= 2 && !g_strcmp0 (argv[1], "--helper-debug")) {
+       if (helper_debug) {
                g_message ("openvpn script environment ---------------------------");
                iter = environ;
                while (iter && *iter)
@@ -554,6 +565,21 @@ main (int argc, char *argv[])
                g_message ("------------------------------------------------------");
        }
 
+       if (shift > 0) {
+               /* shift the arguments to the right leaving only those provided by openvpn
+                * (including the terminating NULL at argv[argc]).
+                */
+               for (i = 1 + shift; i <= argc; i++)
+                       argv[i-shift] = argv[i];
+               argc -= shift;
+       }
+
+       connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &err);
+       if (!connection) {
+               g_warning ("Could not get the system bus: %s", err->message);
+               exit (1);
+       }
+
        config = g_hash_table_new (g_str_hash, g_str_equal);
        ip4config = g_hash_table_new (g_str_hash, g_str_equal);
        ip6config = g_hash_table_new (g_str_hash, g_str_equal);
diff --git a/src/nm-openvpn-service.c b/src/nm-openvpn-service.c
index 66f2118..fc3fd84 100644
--- a/src/nm-openvpn-service.c
+++ b/src/nm-openvpn-service.c
@@ -866,6 +866,7 @@ nm_openvpn_start_openvpn_binary (NMOpenvpnPlugin *plugin,
        GPtrArray *args;
        GSource *openvpn_watch;
        GPid pid;
+       char *stmp;
 
        /* Find openvpn */
        openvpn_binary = nm_find_openvpn ();
@@ -1110,10 +1111,9 @@ nm_openvpn_start_openvpn_binary (NMOpenvpnPlugin *plugin,
 
        /* Up script, called when connection has been established or has been restarted */
        add_openvpn_arg (args, "--up");
-       if (debug)
-               add_openvpn_arg (args, NM_OPENVPN_HELPER_PATH " --helper-debug");
-       else
-               add_openvpn_arg (args, NM_OPENVPN_HELPER_PATH);
+       stmp = g_strdup_printf ("%s%s --", NM_OPENVPN_HELPER_PATH, debug ? " --helper-debug" : "");
+       add_openvpn_arg (args, stmp);
+       g_free (stmp);
        add_openvpn_arg (args, "--up-restart");
 
        /* Keep key and tun if restart is needed */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]