1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2011 Red Hat, Inc.
*/
#include "nm-default.h"
#include "nm-dhcp-dhclient-utils.h"
#include <ctype.h>
#include <arpa/inet.h>
#include <net/if.h>
#include "nm-glib-aux/nm-dedup-multi.h"
#include "nm-dhcp-utils.h"
#include "nm-ip4-config.h"
#include "nm-utils.h"
#include "platform/nm-platform.h"
#include "NetworkManagerUtils.h"
#define TIMEOUT_TAG "timeout "
#define RETRY_TAG "retry "
#define CLIENTID_TAG "send dhcp-client-identifier"
#define HOSTNAME4_TAG "send host-name"
#define HOSTNAME4_FORMAT HOSTNAME4_TAG " \"%s\"; # added by NetworkManager"
#define FQDN_TAG_PREFIX "send fqdn."
#define FQDN_TAG FQDN_TAG_PREFIX "fqdn"
#define FQDN_FORMAT FQDN_TAG " \"%s\"; # added by NetworkManager"
#define ALSOREQ_TAG "also request "
#define REQ_TAG "request "
#define MUDURLv4_DEF "option mudurl code 161 = text;\n"
#define MUDURLv4_FMT "send mudurl \"%s\";\n"
#define MUDURLv6_DEF "option dhcp6.mudurl code 112 = text;\n"
#define MUDURLv6_FMT "send dhcp6.mudurl \"%s\";\n"
static void
add_request (GPtrArray *array, const char *item)
{
guint i;
for (i = 0; i < array->len; i++) {
if (nm_streq (array->pdata[i], item))
return;
}
g_ptr_array_add (array, g_strdup (item));
}
static gboolean
grab_request_options (GPtrArray *store, const char* line)
{
gs_free const char **line_v = NULL;
gsize i;
/* Grab each 'request' or 'also request' option and save for later */
line_v = nm_utils_strsplit_set (line, "\t ,");
for (i = 0; line_v && line_v[i]; i++) {
const char *ss = nm_str_skip_leading_spaces (line_v[i]);
gsize l;
gboolean end = FALSE;
if (!ss[0])
continue;
if (ss[0] == ';') {
/* all done */
return TRUE;
}
if (!g_ascii_isalnum (ss[0]))
continue;
l = strlen (ss);
while ( l > 0
&& g_ascii_isspace (ss[l - 1])) {
((char *) ss)[l - 1] = '\0';
l--;
}
if ( l > 0
&& ss[l - 1] == ';') {
/* Remove the EOL marker */
((char *) ss)[l - 1] = '\0';
end = TRUE;
}
if (ss[0])
add_request (store, ss);
if (end)
return TRUE;
}
return FALSE;
}
static void
add_ip4_config (GString *str,
GBytes *client_id,
const char *hostname,
gboolean use_fqdn,
NMDhcpHostnameFlags hostname_flags)
{
if (client_id) {
const char *p;
gsize l;
guint i;
p = g_bytes_get_data (client_id, &l);
nm_assert (p);
/* Allow type 0 (non-hardware address) to be represented as a string
* as long as all the characters are printable.
*/
for (i = 1; (p[0] == 0) && i < l; i++) {
if (!g_ascii_isprint (p[i]) || p[i] == '\\' || p[i] == '"')
break;
}
g_string_append (str, CLIENTID_TAG " ");
if (i < l) {
/* Unprintable; convert to a hex string */
for (i = 0; i < l; i++) {
if (i > 0)
g_string_append_c (str, ':');
g_string_append_printf (str, "%02x", (guint8) p[i]);
}
} else {
/* Printable; just add to the line with type 0 */
g_string_append_c (str, '"');
g_string_append (str, "\\x00");
g_string_append_len (str, p + 1, l - 1);
g_string_append_c (str, '"');
}
g_string_append (str, "; # added by NetworkManager\n");
}
if (hostname) {
if (use_fqdn) {
g_string_append_printf (str, FQDN_FORMAT "\n", hostname);
g_string_append_printf (str, FQDN_TAG_PREFIX "encoded %s;\n",
(hostname_flags & NM_DHCP_HOSTNAME_FLAG_FQDN_ENCODED)
? "on"
: "off");
g_string_append_printf (str, FQDN_TAG_PREFIX "server-update %s;\n",
(hostname_flags & NM_DHCP_HOSTNAME_FLAG_FQDN_SERV_UPDATE)
? "on"
: "off");
g_string_append_printf (str, FQDN_TAG_PREFIX "no-client-update %s;\n",
(hostname_flags & NM_DHCP_HOSTNAME_FLAG_FQDN_NO_UPDATE)
? "on"
: "off");
} else
g_string_append_printf (str, HOSTNAME4_FORMAT "\n", hostname);
}
g_string_append_c (str, '\n');
/* Define options for classless static routes */
g_string_append (str,
"option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;\n");
g_string_append (str,
"option ms-classless-static-routes code 249 = array of unsigned integer 8;\n");
/* Web Proxy Auto-Discovery option (bgo #368423) */
g_string_append (str, "option wpad code 252 = string;\n");
g_string_append_c (str, '\n');
}
static void
add_hostname6 (GString *str,
const char *hostname,
NMDhcpHostnameFlags hostname_flags)
{
if (hostname) {
g_string_append_printf (str, FQDN_FORMAT "\n", hostname);
if (hostname_flags & NM_DHCP_HOSTNAME_FLAG_FQDN_SERV_UPDATE)
g_string_append (str, FQDN_TAG_PREFIX "server-update on;\n");
if (hostname_flags & NM_DHCP_HOSTNAME_FLAG_FQDN_NO_UPDATE)
g_string_append (str, FQDN_TAG_PREFIX "no-client-update on;\n");
g_string_append_c (str, '\n');
}
}
static void add_mud_url_config(GString *str, const char *mud_url, int addr_family)
{
if (mud_url) {
if (addr_family == AF_INET) {
g_string_append (str, MUDURLv4_DEF);
g_string_append_printf (str, MUDURLv4_FMT, mud_url);
} else {
g_string_append (str, MUDURLv6_DEF);
g_string_append_printf (str, MUDURLv6_FMT, mud_url);
}
}
}
static GBytes *
read_client_id (const char *str)
{
gs_free char *s = NULL;
char *p;
int i = 0, j = 0;
nm_assert (!strncmp (str, CLIENTID_TAG, NM_STRLEN (CLIENTID_TAG)));
str += NM_STRLEN (CLIENTID_TAG);
if (!g_ascii_isspace (*str))
return NULL;
while (g_ascii_isspace (*str))
str++;
if (*str == '"') {
/* Parse string literal with escape sequences */
s = g_strdup (str + 1);
p = strrchr (s, '"');
if (p)
*p = '\0';
else
return NULL;
if (!s[0])
return NULL;
while (s[i]) {
if ( s[i] == '\\'
&& s[i + 1] == 'x'
&& g_ascii_isxdigit (s[i + 2])
&& g_ascii_isxdigit (s[i + 3])) {
s[j++] = (g_ascii_xdigit_value (s[i + 2]) << 4)
+ g_ascii_xdigit_value (s[i + 3]);
i += 4;
continue;
}
if ( s[i] == '\\'
&& s[i + 1] >= '0' && s[i + 1] <= '7'
&& s[1 + 2] >= '0' && s[i + 2] <= '7'
&& s[1 + 3] >= '0' && s[i + 3] <= '7') {
s[j++] = ((s[i + 1] - '0') << 6)
+ ((s[i + 2] - '0') << 3)
+ ( s[i + 3] - '0');
i += 4;
continue;
}
s[j++] = s[i++];
}
return g_bytes_new_take (g_steal_pointer (&s), j);
}
/* Otherwise, try to read a hexadecimal sequence */
s = g_strdup (str);
g_strchomp (s);
if (s[strlen (s) - 1] == ';')
s[strlen (s) - 1] = '\0';
return nm_utils_hexstr2bin (s);
}
static gboolean
read_interface (const char *line, char *interface, guint size)
{
gs_free char *dup = g_strdup (line + NM_STRLEN ("interface"));
char *ptr = dup, *end;
while (g_ascii_isspace (*ptr))
ptr++;
if (*ptr == '"') {
ptr++;
end = strchr (ptr, '"');
if (!end)
return FALSE;
*end = '\0';
} else {
end = strchr (ptr, ' ');
if (!end)
end = strchr (ptr, '{');
if (!end)
return FALSE;
*end = '\0';
}
if ( ptr[0] == '\0'
|| strlen (ptr) + 1 > size)
return FALSE;
snprintf (interface, size, "%s", ptr);
return TRUE;
}
char *
nm_dhcp_dhclient_create_config (const char *interface,
int addr_family,
GBytes *client_id,
const char *anycast_addr,
const char *hostname,
guint32 timeout,
gboolean use_fqdn,
NMDhcpHostnameFlags hostname_flags,
const char *mud_url,
const char *const *reject_servers,
const char *orig_path,
const char *orig_contents,
GBytes **out_new_client_id)
{
nm_auto_free_gstring GString *new_contents = NULL;
gs_unref_ptrarray GPtrArray *fqdn_opts = NULL;
gs_unref_ptrarray GPtrArray *reqs = NULL;
gboolean reset_reqlist = FALSE;
int i;
g_return_val_if_fail (!anycast_addr || nm_utils_hwaddr_valid (anycast_addr, ETH_ALEN), NULL);
g_return_val_if_fail (NM_IN_SET (addr_family, AF_INET, AF_INET6), NULL);
g_return_val_if_fail (!reject_servers || addr_family == AF_INET, NULL);
nm_assert (!out_new_client_id || !*out_new_client_id);
new_contents = g_string_new (_("# Created by NetworkManager\n"));
reqs = g_ptr_array_new_full (5, g_free);
if (orig_contents) {
gs_free const char **lines = NULL;
gsize line_i;
nm_auto_free_gstring GString *blocks_stack = NULL;
guint blocks_skip = 0;
gboolean in_alsoreq = FALSE;
gboolean in_req = FALSE;
char intf[IFNAMSIZ];
blocks_stack = g_string_new (NULL);
g_string_append_printf (new_contents, _("# Merged from %s\n\n"), orig_path);
intf[0] = '\0';
lines = nm_utils_strsplit_set (orig_contents, "\n\r");
for (line_i = 0; lines && lines[line_i]; line_i++) {
const char *line = nm_str_skip_leading_spaces (lines[line_i]);
const char *p;
if (line[0] == '\0')
continue;
g_strchomp ((char *) line);
p = line;
if (in_req) {
/* pass */
} else if (strchr (p, '{')) {
if ( NM_STR_HAS_PREFIX (p, "lease")
|| NM_STR_HAS_PREFIX (p, "alias")
|| NM_STR_HAS_PREFIX (p, "interface")
|| NM_STR_HAS_PREFIX (p, "pseudo")) {
/* skip over these blocks, except 'interface' when it
* matches the current interface */
blocks_skip++;
g_string_append_c (blocks_stack, 'b');
if ( !intf[0]
&& NM_STR_HAS_PREFIX (p, "interface")) {
if (read_interface (p, intf, sizeof (intf)))
continue;
}
} else {
/* allow other blocks (conditionals) */
if (!strchr (p, '}')) /* '} else {' */
g_string_append_c (blocks_stack, 'c');
}
} else if (strchr (p, '}')) {
if (blocks_stack->len > 0) {
if (blocks_stack->str[blocks_stack->len - 1] == 'b') {
g_string_truncate (blocks_stack, blocks_stack->len - 1);
nm_assert(blocks_skip > 0);
blocks_skip--;
intf[0] = '\0';
continue;
}
g_string_truncate (blocks_stack, blocks_stack->len - 1);
}
}
if (blocks_skip > 0 && !intf[0])
continue;
if (intf[0] && !nm_streq (intf, interface))
continue;
/* Some timing parameters in dhclient should not be imported (timeout, retry).
* The retry parameter will be simply not used as we will exit on first failure.
* The timeout one instead may affect NetworkManager behavior: if the timeout
* elapses before dhcp-timeout dhclient will report failure and cause NM to
* fail the dhcp process before dhcp-timeout. So, always skip importing timeout
* as we will need to add one greater than dhcp-timeout.
*/
if ( !strncmp (p, TIMEOUT_TAG, strlen (TIMEOUT_TAG))
|| !strncmp (p, RETRY_TAG, strlen (RETRY_TAG)))
continue;
if (!strncmp (p, CLIENTID_TAG, strlen (CLIENTID_TAG))) {
/* Override config file "dhcp-client-id" and use one from the connection */
if (client_id)
continue;
/* Otherwise, capture and return the existing client id */
if (out_new_client_id)
nm_clear_pointer (out_new_client_id, g_bytes_unref);
NM_SET_OUT (out_new_client_id, read_client_id (p));
}
/* Override config file hostname and use one from the connection */
if (hostname) {
if (strncmp (p, HOSTNAME4_TAG, strlen (HOSTNAME4_TAG)) == 0)
continue;
if (strncmp (p, FQDN_TAG, strlen (FQDN_TAG)) == 0)
continue;
}
/* To let user's FQDN options (except "fqdn.fqdn") override the
* default ones set by NM, add them later
*/
if (!strncmp (p, FQDN_TAG_PREFIX, NM_STRLEN (FQDN_TAG_PREFIX))) {
if (!fqdn_opts)
fqdn_opts = g_ptr_array_new_full (5, g_free);
g_ptr_array_add (fqdn_opts, g_strdup (p + NM_STRLEN (FQDN_TAG_PREFIX)));
continue;
}
/* Ignore 'script' since we pass our own */
if (g_str_has_prefix (p, "script "))
continue;
/* Check for "request" */
if (!strncmp (p, REQ_TAG, strlen (REQ_TAG))) {
in_req = TRUE;
p += strlen (REQ_TAG);
g_ptr_array_set_size (reqs, 0);
reset_reqlist = TRUE;
}
/* Save all request options for later use */
if (in_req) {
in_req = !grab_request_options (reqs, p);
continue;
}
/* Check for "also require" */
if (!strncmp (p, ALSOREQ_TAG, strlen (ALSOREQ_TAG))) {
in_alsoreq = TRUE;
p += strlen (ALSOREQ_TAG);
}
if (in_alsoreq) {
in_alsoreq = !grab_request_options (reqs, p);
continue;
}
/* Existing configuration line is OK, add it to new configuration */
g_string_append (new_contents, line);
g_string_append_c (new_contents, '\n');
}
} else
g_string_append_c (new_contents, '\n');
/* ensure dhclient timeout is greater than dhcp-timeout: as dhclient timeout default value is
* 60 seconds, we need this only if dhcp-timeout is greater than 60.
*/
if (timeout >= 60) {
timeout = timeout < G_MAXINT32 ? timeout + 1 : G_MAXINT32;
g_string_append_printf (new_contents, "timeout %u;\n", timeout);
}
add_mud_url_config (new_contents, mud_url, addr_family);
if ( reject_servers
&& reject_servers[0]) {
g_string_append (new_contents, "reject ");
for (i = 0; reject_servers[i]; i++) {
if (i != 0)
g_string_append (new_contents, ", ");
g_string_append (new_contents, reject_servers[i]);
}
g_string_append (new_contents, ";\n");
}
if (addr_family == AF_INET) {
add_ip4_config (new_contents, client_id, hostname, use_fqdn, hostname_flags);
add_request (reqs, "rfc3442-classless-static-routes");
add_request (reqs, "ms-classless-static-routes");
add_request (reqs, "static-routes");
add_request (reqs, "wpad");
add_request (reqs, "ntp-servers");
add_request (reqs, "root-path");
} else {
add_hostname6 (new_contents, hostname, hostname_flags);
add_request (reqs, "dhcp6.name-servers");
add_request (reqs, "dhcp6.domain-search");
/* FIXME: internal client does not support requesting client-id option. Does this even work? */
add_request (reqs, "dhcp6.client-id");
}
if (reset_reqlist)
g_string_append (new_contents, "request; # override dhclient defaults\n");
/* And add it to the dhclient configuration */
for (i = 0; i < reqs->len; i++)
g_string_append_printf (new_contents, "also request %s;\n", (char *) reqs->pdata[i]);
if (fqdn_opts) {
for (i = 0; i < fqdn_opts->len; i++) {
const char *t = fqdn_opts->pdata[i];
if (i == 0)
g_string_append_printf (new_contents, "\n# FQDN options from %s\n", orig_path);
g_string_append_printf (new_contents, FQDN_TAG_PREFIX "%s\n", t);
}
}
g_string_append_c (new_contents, '\n');
if (anycast_addr) {
g_string_append_printf (new_contents, "interface \"%s\" {\n"
" initial-interval 1; \n"
" anycast-mac ethernet %s;\n"
"}\n",
interface, anycast_addr);
}
return g_string_free (g_steal_pointer (&new_contents), FALSE);
}
/* Roughly follow what dhclient's quotify_buf() and pretty_escape() functions do */
char *
nm_dhcp_dhclient_escape_duid (GBytes *duid)
{
char *escaped;
const guint8 *s, *s0;
gsize len;
char *d;
g_return_val_if_fail (duid, NULL);
s0 = g_bytes_get_data (duid, &len);
s = s0;
d = escaped = g_malloc ((len * 4) + 1);
while (s < (s0 + len)) {
if (!g_ascii_isprint (*s)) {
*d++ = '\\';
*d++ = '0' + ((*s >> 6) & 0x7);
*d++ = '0' + ((*s >> 3) & 0x7);
*d++ = '0' + (*s++ & 0x7);
} else if (*s == '"' || *s == '\'' || *s == '$' ||
*s == '`' || *s == '\\' || *s == '|' ||
*s == '&') {
*d++ = '\\';
*d++ = *s++;
} else
*d++ = *s++;
}
*d++ = '\0';
return escaped;
}
static gboolean
isoctal (const guint8 *p)
{
return ( p[0] >= '0' && p[0] <= '3'
&& p[1] >= '0' && p[1] <= '7'
&& p[2] >= '0' && p[2] <= '7');
}
GBytes *
nm_dhcp_dhclient_unescape_duid (const char *duid)
{
GByteArray *unescaped;
const guint8 *p = (const guint8 *) duid;
guint i, len;
guint8 octal;
/* FIXME: it's wrong to have an "unescape-duid" function. dhclient
* defines a file format with escaping. So we need a general unescape
* function that can handle dhclient syntax. */
len = strlen (duid);
unescaped = g_byte_array_sized_new (len);
for (i = 0; i < len; i++) {
if (p[i] == '\\') {
i++;
if (isdigit (p[i])) {
/* Octal escape sequence */
if (i + 2 >= len || !isoctal (p + i))
goto error;
octal = ((p[i] - '0') << 6) + ((p[i + 1] - '0') << 3) + (p[i + 2] - '0');
g_byte_array_append (unescaped, &octal, 1);
i += 2;
} else {
/* FIXME: don't warn on untrusted data. Either signal an error, or accept
* it silently. */
/* One of ", ', $, `, \, |, or & */
g_warn_if_fail (p[i] == '"' || p[i] == '\'' || p[i] == '$' ||
p[i] == '`' || p[i] == '\\' || p[i] == '|' ||
p[i] == '&');
g_byte_array_append (unescaped, &p[i], 1);
}
} else
g_byte_array_append (unescaped, &p[i], 1);
}
return g_byte_array_free_to_bytes (unescaped);
error:
g_byte_array_free (unescaped, TRUE);
return NULL;
}
#define DUID_PREFIX "default-duid \""
/* Beware: @error may be unset even if the function returns %NULL. */
GBytes *
nm_dhcp_dhclient_read_duid (const char *leasefile, GError **error)
{
gs_free char *contents = NULL;
gs_free const char **contents_v = NULL;
gsize i;
if (!g_file_test (leasefile, G_FILE_TEST_EXISTS))
return NULL;
if (!g_file_get_contents (leasefile, &contents, NULL, error))
return NULL;
contents_v = nm_utils_strsplit_set (contents, "\n\r");
for (i = 0; contents_v && contents_v[i]; i++) {
const char *p = nm_str_skip_leading_spaces (contents_v[i]);
GBytes *duid;
if (!NM_STR_HAS_PREFIX (p, DUID_PREFIX))
continue;
p += NM_STRLEN (DUID_PREFIX);
g_strchomp ((char *) p);
if (!NM_STR_HAS_SUFFIX (p, "\";"))
continue;
((char *) p)[strlen (p) - 2] = '\0';
duid = nm_dhcp_dhclient_unescape_duid (p);
if (duid)
return duid;
}
return NULL;
}
gboolean
nm_dhcp_dhclient_save_duid (const char *leasefile,
GBytes *duid,
GError **error)
{
gs_free char *escaped_duid = NULL;
gs_free const char **lines = NULL;
nm_auto_free_gstring GString *s = NULL;
const char *const*iter;
gsize len = 0;
g_return_val_if_fail (leasefile != NULL, FALSE);
if (!duid) {
nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN,
"missing duid");
g_return_val_if_reached (FALSE);
}
escaped_duid = nm_dhcp_dhclient_escape_duid (duid);
nm_assert (escaped_duid);
if (g_file_test (leasefile, G_FILE_TEST_EXISTS)) {
gs_free char *contents = NULL;
if (!g_file_get_contents (leasefile, &contents, &len, error)) {
g_prefix_error (error, "failed to read lease file %s: ", leasefile);
return FALSE;
}
lines = nm_utils_strsplit_set_with_empty (contents, "\n\r");
}
s = g_string_sized_new (len + 50);
g_string_append_printf (s, DUID_PREFIX "%s\";\n", escaped_duid);
/* Preserve existing leasefile contents */
if (lines) {
for (iter = lines; *iter; iter++) {
const char *str = *iter;
const char *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.
*/
l = nm_str_skip_leading_spaces (str);
if (g_str_has_prefix (l, DUID_PREFIX)) {
gs_strfreev char **split = NULL;
split = g_strsplit (l, "\"", -1);
if ( split[0]
&& nm_streq0 (split[1], escaped_duid))
return TRUE;
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_c (s, '\n');
}
}
if (!g_file_set_contents (leasefile, s->str, -1, error)) {
g_prefix_error (error, "failed to set DUID in lease file %s: ", leasefile);
return FALSE;
}
return TRUE;
}
|