dhcp/dhclient: better handle "\r\n" line breaks in dhclient lease file

Splitting by any of "\r\n" and then joining the lines with "\n"
leads to double-newlines. That's certainly wrong.

Maybe we shouldn't care about "\r", I don't know why this was done. But
handle it differently.

(cherry picked from commit c990d6a81a)
This commit is contained in:
Thomas Haller 2022-12-19 09:58:50 +01:00
parent 9a2d2c8522
commit b7d343af05
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 22 additions and 16 deletions

View file

@ -673,7 +673,7 @@ nm_dhcp_dhclient_save_duid(const char *leasefile, GBytes *duid, GError **error)
return FALSE;
}
lines = nm_strsplit_set_with_empty(contents, "\n\r");
lines = nm_strsplit_set_with_empty(contents, "\n");
}
s = g_string_sized_new(contents_len + 50);
@ -684,27 +684,33 @@ nm_dhcp_dhclient_save_duid(const char *leasefile, GBytes *duid, GError **error)
for (iter = lines; *iter; iter++) {
const char *str = *iter;
const char *l;
gboolean ends_with_r;
gsize l_len;
gsize prefix_len;
/* 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.
*/
l = nm_str_skip_leading_spaces(str);
if (g_str_has_prefix(l, DEFAULT_DUID_PREFIX)) {
gs_strfreev char **split = NULL;
l = nm_str_skip_leading_spaces(str);
l_len = strlen(l);
prefix_len = l - str;
split = g_strsplit(l, "\"", -1);
if (split[0] && nm_streq0(split[1], escaped_duid))
return TRUE;
ends_with_r = l_len > 0 && l[l_len - 1u] == '\r';
if (ends_with_r) {
((char *) l)[--l_len] = '\0';
}
if (NM_STR_HAS_PREFIX(l, DEFAULT_DUID_PREFIX)) {
/* We always add our line on top. This line can be skipped. */
continue;
}
if (str)
g_string_append(s, str);
/* avoid to add an extra '\n' at the end of file */
if ((iter[1]) != NULL)
g_string_append_len(s, str, prefix_len);
g_string_append(s, l);
if (ends_with_r) {
g_string_append_c(s, '\r');
g_string_append_c(s, '\n');
} else if ((iter[1]) != NULL) {
/* avoid to add an extra '\n' at the end of file */
g_string_append_c(s, '\n');
}
}
}

View file

@ -1039,7 +1039,7 @@ test_write_duid(void)
"aa:b:cc:d:ee:f;\n option dhcp6.server-id 0:1:0:1:2b:2c:4d:1d:0:0:0:0:0:0;\n option "
"dhcp6.name-servers 192:168:121:0:ce0f:f1ff:fece:1;\n option dhcp6.fqdn "
"1:c:64:66:66:36:64:65:34:66:63:62:30:66;\n option dhcp6.status-code success "
"\"success\";\n\n}\n");
"\"success\";\r\n}\n");
_check_duid(
_DUID(0xaa, 0xb, 0xcc, 0xd, 0xee, 0xe),