[network-manager-libreswan/th/vpn-plugin-debug-bgo766872: 7/20] shared: refactor write_config_option() to treat trailing newline separate



commit ccb40c90b8b594051bae8396750e1a09e5fc5015
Author: Thomas Haller <thaller redhat com>
Date:   Thu May 26 11:21:30 2016 +0200

    shared: refactor write_config_option() to treat trailing newline separate

 shared/utils.c             |   57 ++++++++++++++++++++-----------------------
 shared/utils.h             |   26 ++++++++++++++-----
 src/nm-libreswan-service.c |    4 +-
 3 files changed, 48 insertions(+), 39 deletions(-)
---
diff --git a/shared/utils.c b/shared/utils.c
index d040e9d..626936f 100644
--- a/shared/utils.c
+++ b/shared/utils.c
@@ -57,59 +57,56 @@ nm_libreswan_config_write (gint fd,
 
        leftid = nm_setting_vpn_get_data_item (s_vpn, NM_LIBRESWAN_LEFTID);
 
-       write_config_option (fd, "conn %s\n", con_name);
+       write_config_option (fd, "conn %s", con_name);
        if (leftid) {
-               write_config_option (fd, " aggrmode=yes\n");
-               write_config_option (fd, " leftid= %s\n", leftid);
+               write_config_option (fd, " aggrmode=yes");
+               write_config_option (fd, " leftid= %s", leftid);
        }
-       write_config_option (fd, " authby=secret\n");
-       write_config_option (fd, " left=%%defaultroute\n");
-       write_config_option (fd, " leftxauthclient=yes\n");
-       write_config_option (fd, " leftmodecfgclient=yes\n");
+       write_config_option (fd, " authby=secret");
+       write_config_option (fd, " left=%%defaultroute");
+       write_config_option (fd, " leftxauthclient=yes");
+       write_config_option (fd, " leftmodecfgclient=yes");
 
        if (bus_name)
-               write_config_option (fd, " leftupdown=\"" NM_LIBRESWAN_HELPER_PATH " --bus-name %s\"\n", 
bus_name);
+               write_config_option (fd, " leftupdown=\"" NM_LIBRESWAN_HELPER_PATH " --bus-name %s\"", 
bus_name);
 
        default_username = nm_setting_vpn_get_user_name (s_vpn);
        props_username = nm_setting_vpn_get_data_item (s_vpn, NM_LIBRESWAN_LEFTXAUTHUSER);
        if (props_username && strlen (props_username))
-               write_config_option (fd, " leftxauthusername=%s\n", props_username);
+               write_config_option (fd, " leftxauthusername=%s", props_username);
        else if (default_username && strlen (default_username))
-               write_config_option (fd, " leftxauthusername=%s\n", default_username);
+               write_config_option (fd, " leftxauthusername=%s", default_username);
 
-       write_config_option (fd, " right=%s\n", nm_setting_vpn_get_data_item (s_vpn, NM_LIBRESWAN_RIGHT));
-       write_config_option (fd, " remote_peer_type=cisco\n");
-       write_config_option (fd, " rightxauthserver=yes\n");
-       write_config_option (fd, " rightmodecfgserver=yes\n");
-       write_config_option (fd, " modecfgpull=yes\n");
-       write_config_option (fd, " rightsubnet=0.0.0.0/0\n");
+       write_config_option (fd, " right=%s", nm_setting_vpn_get_data_item (s_vpn, NM_LIBRESWAN_RIGHT));
+       write_config_option (fd, " remote_peer_type=cisco");
+       write_config_option (fd, " rightxauthserver=yes");
+       write_config_option (fd, " rightmodecfgserver=yes");
+       write_config_option (fd, " modecfgpull=yes");
+       write_config_option (fd, " rightsubnet=0.0.0.0/0");
 
        phase1_alg_str = nm_setting_vpn_get_data_item (s_vpn, NM_LIBRESWAN_IKE);
        if (!phase1_alg_str || !strlen (phase1_alg_str))
-               write_config_option (fd, " ike=aes-sha1\n");
+               write_config_option (fd, " ike=aes-sha1");
        else
-               write_config_option (fd, " ike=%s\n", phase1_alg_str);
+               write_config_option (fd, " ike=%s", phase1_alg_str);
 
        phase2_alg_str = nm_setting_vpn_get_data_item (s_vpn, NM_LIBRESWAN_ESP);
        if (!phase2_alg_str || !strlen (phase2_alg_str))
-               write_config_option (fd, " esp=aes-sha1;modp1024\n");
+               write_config_option (fd, " esp=aes-sha1;modp1024");
        else
-               write_config_option (fd, " esp=%s\n", phase2_alg_str);
+               write_config_option (fd, " esp=%s", phase2_alg_str);
 
-       write_config_option (fd, " rekey=yes\n");
-       write_config_option (fd, " salifetime=24h\n");
-       write_config_option (fd, " ikelifetime=24h\n");
-       write_config_option (fd, " keyingtries=1\n");
+       write_config_option (fd, " rekey=yes");
+       write_config_option (fd, " salifetime=24h");
+       write_config_option (fd, " ikelifetime=24h");
+       write_config_option (fd, " keyingtries=1");
        if (!openswan && g_strcmp0 (nm_setting_vpn_get_data_item (s_vpn, NM_LIBRESWAN_VENDOR), "Cisco") == 0)
-               write_config_option (fd, " cisco-unity=yes\n");
-       write_config_option (fd, " auto=add");
+               write_config_option (fd, " cisco-unity=yes");
 
        /* openswan requires a terminating \n (otherwise it segfaults) while
         * libreswan fails parsing the configuration if you include the \n.
         * WTF?
         */
-       if (openswan || !bus_name)
-               (void) write (fd, "\n", 1);
-       if (debug)
-               g_print ("\n");
+       write_config_option_newline (fd, (openswan || !bus_name), " auto=add");
+
 }
diff --git a/shared/utils.h b/shared/utils.h
index f488a08..c7e0379 100644
--- a/shared/utils.h
+++ b/shared/utils.h
@@ -26,24 +26,36 @@
 
 extern gboolean debug;
 
+__attribute__((__format__ (__printf__, 3, 4)))
 static inline void
-write_config_option (int fd, const char *format, ...)
+write_config_option_newline (int fd, gboolean new_line, const char *format, ...)
 {
-       char *string;
+       gs_free char *string = NULL;
        va_list args;
+       gsize l;
 
        va_start (args, format);
        string = g_strdup_vprintf (format, args);
+       va_end (args);
 
        if (debug)
-               g_print ("Config: %s", string);
+               g_print ("Config: %s\n", string);
 
-       if (write (fd, string, strlen (string)) == -1)
-               g_warning ("nm-libreswan: error in write_config_option");
+       l = strlen (string);
+       if (new_line) {
+               gs_free char *s = string;
 
-       g_free (string);
-       va_end (args);
+               string = g_new (char, l + 1 + 1);
+               memcpy (string, s, l);
+               string[l] = '\n';
+               string[l + 1] = '\0';
+               l++;
+       }
+
+       if (write (fd, string, l) <= 0)
+               g_warning ("nm-libreswan: error in write_config_option");
 }
+#define write_config_option(fd, ...) write_config_option_newline((fd), TRUE, __VA_ARGS__)
 
 void
 nm_libreswan_config_write (gint fd,
diff --git a/src/nm-libreswan-service.c b/src/nm-libreswan-service.c
index 07709e0..fcf4f8a 100644
--- a/src/nm-libreswan-service.c
+++ b/src/nm-libreswan-service.c
@@ -693,11 +693,11 @@ nm_libreswan_config_psk_write (NMSettingVpn *s_vpn,
 
        leftid = nm_setting_vpn_get_data_item (s_vpn, NM_LIBRESWAN_LEFTID);
        if (leftid) {
-               write_config_option (fd, "@%s: PSK \"%s\"\n", leftid, psk);
+               write_config_option (fd, "@%s: PSK \"%s\"", leftid, psk);
        } else {
                right = nm_setting_vpn_get_data_item (s_vpn, NM_LIBRESWAN_RIGHT);
                g_assert (right);
-               write_config_option (fd, "%s %%any: PSK \"%s\"\n", right, psk);
+               write_config_option (fd, "%s %%any: PSK \"%s\"", right, psk);
        }
 
        close (fd);


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