From d8715124677bb3a4443c127bc987575d075fba03 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Wed, 9 Feb 2011 20:44:27 -0600 Subject: [PATCH] ifcfg-rh: handle different connections with the same ID Since ifcfg-rh uses the connection's ID as the filename by default, we could run into a situation where two connections with the same ID are visible to different users. We don't want one connection overwriting the other in that case, so we need to pick a new name for the one we're about to write. --- system-settings/plugins/ifcfg-rh/writer.c | 27 ++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/system-settings/plugins/ifcfg-rh/writer.c b/system-settings/plugins/ifcfg-rh/writer.c index f66a1ec6b0..ccb4676b5e 100644 --- a/system-settings/plugins/ifcfg-rh/writer.c +++ b/system-settings/plugins/ifcfg-rh/writer.c @@ -1603,8 +1603,33 @@ write_connection (NMConnection *connection, escaped = escape_id (nm_setting_connection_get_id (s_con)); ifcfg_name = g_strdup_printf ("%s/ifcfg-%s", ifcfg_dir, escaped); - ifcfg = svCreateFile (ifcfg_name); + + /* If a file with this path already exists then we need another name. + * Multiple connections can have the same ID (ie if two connections with + * the same ID are visible to different users) but of course can't have + * the same path. + */ + if (g_file_test (ifcfg_name, G_FILE_TEST_EXISTS)) { + guint32 idx = 0; + + g_free (ifcfg_name); + while (idx++ < 500) { + ifcfg_name = g_strdup_printf ("%s/ifcfg-%s %u", ifcfg_dir, escaped, idx); + if (g_file_test (ifcfg_name, G_FILE_TEST_EXISTS) == FALSE) + break; + g_free (ifcfg_name); + ifcfg_name = NULL; + } + } g_free (escaped); + + if (ifcfg_name == NULL) { + g_set_error_literal (error, IFCFG_PLUGIN_ERROR, 0, + "Failed to find usable ifcfg file name"); + return FALSE; + } + + ifcfg = svCreateFile (ifcfg_name); } if (!ifcfg) {