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



commit 8a41f0af3d435ef17ab77d0394e92fba33a6b9d5
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 |   30 +++++++++++++++++++++++++-----
 src/nm-openvpn-service.c                |    8 ++++----
 2 files changed, 29 insertions(+), 9 deletions(-)
---
diff --git a/src/nm-openvpn-service-openvpn-helper.c b/src/nm-openvpn-service-openvpn-helper.c
index 601229e..3b752b3 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,25 @@ 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);
+       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 +563,17 @@ main (int argc, char *argv[])
                g_message ("------------------------------------------------------");
        }
 
+       /* shift the arguments to the right leaving only those provided by openvpn */
+       argv[shift] = argv[0];
+       argv += shift;
+       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]