Lines 24-29
Link Here
|
24 |
#include <errno.h> |
24 |
#include <errno.h> |
25 |
#include <netdb.h> |
25 |
#include <netdb.h> |
26 |
#include <ctype.h> |
26 |
#include <ctype.h> |
|
|
27 |
#include <regex.h> |
27 |
|
28 |
|
28 |
#include "NetworkManagerPolicy.h" |
29 |
#include "NetworkManagerPolicy.h" |
29 |
#include "NetworkManagerUtils.h" |
30 |
#include "NetworkManagerUtils.h" |
Lines 332-337
Link Here
|
332 |
g_string_free (new_contents, TRUE); |
333 |
g_string_free (new_contents, TRUE); |
333 |
return success; |
334 |
return success; |
334 |
} |
335 |
} |
|
|
336 |
static char *findHostNameMatch(const char *inputString) { |
337 |
if(!inputString) return NULL; |
338 |
char pattern[50]="HOSTNAME[ ]*=[ \"]*([a-zA-z0-9]+)[\"]*"; |
339 |
regmatch_t pmatch[2]; |
340 |
int start=0, end=0; |
341 |
regex_t predicate; |
342 |
if(regcomp(&predicate, pattern, REG_EXTENDED) || |
343 |
regexec(&predicate, inputString, 2, pmatch, 0) || |
344 |
pmatch[1].rm_so < 0 || pmatch[1].rm_eo < 0 ) |
345 |
return NULL; |
346 |
start=pmatch[1].rm_so; |
347 |
end=pmatch[1].rm_eo; |
348 |
if(start>=end || end > strlen(inputString) ) return NULL; |
349 |
return strndup(&inputString[start], end-start); |
350 |
} |
335 |
|
351 |
|
336 |
static void |
352 |
static void |
337 |
set_system_hostname (const char *new_hostname, const char *msg) |
353 |
set_system_hostname (const char *new_hostname, const char *msg) |
Lines 339-344
Link Here
|
339 |
char old_hostname[HOST_NAME_MAX + 1]; |
355 |
char old_hostname[HOST_NAME_MAX + 1]; |
340 |
int ret = 0; |
356 |
int ret = 0; |
341 |
const char *name = new_hostname ? new_hostname : FALLBACK_HOSTNAME; |
357 |
const char *name = new_hostname ? new_hostname : FALLBACK_HOSTNAME; |
|
|
358 |
if (!new_hostname) { |
359 |
static char* sysconf_hostname = NULL; |
360 |
char *contents = NULL; |
361 |
gsize contents_len; |
362 |
GError *error = NULL; |
363 |
if (!sysconf_hostname && !g_file_get_contents (SYSCONFDIR "/conf.d/hostname", &contents, &contents_len, &error)) { |
364 |
nm_warning ("%s: couldn't read " SYSCONFDIR "/conf.d/hostname: (%d) %s", |
365 |
__func__, error ? error->code : 0, |
366 |
(error && error->message) ? error->message : "(unknown)"); |
367 |
} else if (!sysconf_hostname) { |
368 |
sysconf_hostname = g_strstrip(contents); |
369 |
sysconf_hostname = strstr(sysconf_hostname, "HOSTNAME"); |
370 |
sysconf_hostname = findHostNameMatch(sysconf_hostname); |
371 |
} |
372 |
if(sysconf_hostname) |
373 |
name = sysconf_hostname; |
374 |
} |
375 |
|
342 |
|
376 |
|
343 |
old_hostname[HOST_NAME_MAX] = '\0'; |
377 |
old_hostname[HOST_NAME_MAX] = '\0'; |
344 |
errno = 0; |
378 |
errno = 0; |