Commit 84c9ce0d authored by Francesco Giudici's avatar Francesco Giudici

dhclient: always update the DUID in the lease file

We will soon introduce a property to set a custom DUID and we want
to enforce that the provided value is used.
Note that this commit does not cause any change in behavior in current
code.
parent 56865366
......@@ -592,11 +592,11 @@ nm_dhcp_client_start_ip6 (NMDhcpClient *self,
g_return_val_if_fail (priv->addr_family == AF_INET6, FALSE);
g_return_val_if_fail (priv->uuid != NULL, FALSE);
/* If we don't have one yet, read the default DUID for this DHCPv6 client
* from the client-specific persistent configuration.
nm_assert (!priv->duid);
/* Read the default DUID for this DHCPv6 client from the
* client-specific persistent configuration.
*/
if (!priv->duid)
priv->duid = NM_DHCP_CLIENT_GET_CLASS (self)->get_duid (self);
priv->duid = NM_DHCP_CLIENT_GET_CLASS (self)->get_duid (self);
_LOGD ("DUID is '%s'", (str = nm_dhcp_utils_duid_to_string (priv->duid)));
......
......@@ -594,7 +594,8 @@ nm_dhcp_dhclient_save_duid (const char *leasefile,
const char *escaped_duid,
GError **error)
{
char **lines = NULL, **iter, *l;
gs_strfreev char **lines = NULL;
char **iter, *l;
GString *s;
gboolean success;
gsize len = 0;
......@@ -610,19 +611,9 @@ nm_dhcp_dhclient_save_duid (const char *leasefile,
return FALSE;
}
/* If the file already contains an uncommented DUID, leave it */
g_assert (contents);
lines = g_strsplit_set (contents, "\n\r", -1);
g_free (contents);
for (iter = lines; iter && *iter; iter++) {
l = *iter;
while (g_ascii_isspace (*l))
l++;
if (g_str_has_prefix (l, DUID_PREFIX)) {
g_strfreev (lines);
return TRUE;
}
}
}
s = g_string_sized_new (len + 50);
......@@ -631,13 +622,30 @@ nm_dhcp_dhclient_save_duid (const char *leasefile,
/* Preserve existing leasefile contents */
if (lines) {
for (iter = lines; iter && *iter; iter++) {
l = *iter;
while (g_ascii_isspace (*l))
l++;
/* If we find an uncommented DUID in the file, check if
* equal to the one we are going to write: if so, no need
* to update the lease file, otherwise skip the old DUID.
*/
if (g_str_has_prefix (l, DUID_PREFIX)) {
gs_strfreev char **split = NULL;
split = g_strsplit (l, "\"", -1);
if (nm_streq0 (split[1], escaped_duid)) {
g_string_free (s, TRUE);
return TRUE;
}
continue;
}
if (*iter[0])
g_string_append (s, *iter);
/* avoid to add an extra '\n' at the end of file */
if ((iter[1]) != NULL)
g_string_append_c (s, '\n');
}
g_strfreev (lines);
}
success = g_file_set_contents (leasefile, s->str, -1, error);
......
......@@ -785,17 +785,18 @@ static void
test_write_existing_duid (void)
{
const char *duid = "\\000\\001\\000\\001\\023o\\023n\\000\\\"\\372\\214\\326\\302";
const char *expected_contents = "default-duid \"\\000\\001\\000\\001\\027X\\350X\\000#\\025\\010~\\254\";\n";
const char *original_contents = "default-duid \"\\000\\001\\000\\001\\027X\\350X\\000#\\025\\010~\\254\";\n";
const char *expected_contents = "default-duid \"\\000\\001\\000\\001\\023o\\023n\\000\\\"\\372\\214\\326\\302\";\n";
GError *error = NULL;
char *contents = NULL;
gboolean success;
const char *path = "test-dhclient-write-existing-duid.leases";
success = g_file_set_contents (path, expected_contents, -1, &error);
success = g_file_set_contents (path, original_contents, -1, &error);
g_assert_no_error (error);
g_assert (success);
/* Save other DUID; should be a no-op */
/* Save other DUID; should be overwritten */
success = nm_dhcp_dhclient_save_duid (path, duid, &error);
g_assert_no_error (error);
g_assert (success);
......@@ -828,7 +829,7 @@ test_write_existing_commented_duid (void)
g_assert_no_error (error);
g_assert (success);
/* Save other DUID; should be a no-op */
/* Save other DUID; should be saved on top */
success = nm_dhcp_dhclient_save_duid (path, DUID, &error);
g_assert_no_error (error);
g_assert (success);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment