From: Michael Tremer Date: Mon, 25 Jun 2018 10:08:04 +0000 (+0100) Subject: setup: Write /etc/hosts in initscript X-Git-Tag: v2.21-core123~120 X-Git-Url: http://git.ipfire.org/?p=people%2Fpmueller%2Fipfire-2.x.git;a=commitdiff_plain;h=fd52e82a7252a7559c694fce6570aab461c331e3 setup: Write /etc/hosts in initscript Signed-off-by: Michael Tremer --- diff --git a/src/initscripts/system/localnet b/src/initscripts/system/localnet index ff374bb2b1..7ff18d7b47 100644 --- a/src/initscripts/system/localnet +++ b/src/initscripts/system/localnet @@ -22,6 +22,18 @@ write_resolv_conf() { ) > /etc/resolv.conf } +write_hosts() { + eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings) + + ( + echo "127.0.0.1 localhost.localdomain localhost" + + if [ -n "${GREEN_ADDRESS}" ]; then + echo "${GREEN_ADDRESS} ${HOSTNAME}.${DOMAINNAME} ${HOSTNAME}" + fi + ) > /etc/hosts +} + case "${1}" in start) eval $(/usr/local/bin/readhash /var/ipfire/main/settings) @@ -40,6 +52,9 @@ case "${1}" in evaluate_retval fi + # Update hosts + write_hosts + # Update resolv.conf write_resolv_conf ;; diff --git a/src/setup/misc.c b/src/setup/misc.c index b8e7820e14..f9ba39b8c7 100644 --- a/src/setup/misc.c +++ b/src/setup/misc.c @@ -19,31 +19,13 @@ extern char *mylog; extern int automode; -/* This will rewrite /etc/hosts, /etc/hosts.*, and the apache ServerName file. */ int writehostsfiles(void) { - char address[STRING_SIZE] = ""; - char netaddress[STRING_SIZE] = ""; - char netmask[STRING_SIZE] = ""; char message[1000]; - FILE *file, *hosts; struct keyvalue *kv; char hostname[STRING_SIZE]; char domainname[STRING_SIZE] = "localdomain"; char commandstring[STRING_SIZE]; - char buffer[STRING_SIZE]; - - kv = initkeyvalues(); - if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))) - { - freekeyvalues(kv); - errorbox(_("Unable to open settings file")); - return 0; - } - findkey(kv, "GREEN_ADDRESS", address); - findkey(kv, "GREEN_NETADDRESS", netaddress); - findkey(kv, "GREEN_NETMASK", netmask); - freekeyvalues(kv); kv = initkeyvalues(); if (!(readkeyvalues(kv, CONFIG_ROOT "/main/settings"))) @@ -57,51 +39,6 @@ int writehostsfiles(void) findkey(kv, "DOMAINNAME", domainname); freekeyvalues(kv); - if (!(file = fopen(CONFIG_ROOT "/main/hosts", "r"))) - { - errorbox(_("Unable to open main hosts file.")); - return 0; - } - if (!(hosts = fopen("/etc/hosts", "w"))) - { - errorbox(_("Unable to write /etc/hosts.")); - return 0; - } - fprintf(hosts, "127.0.0.1\tlocalhost\n"); - if (strlen(domainname)) - fprintf(hosts, "%s\t%s.%s\t%s\n",address,hostname,domainname,hostname); - else - fprintf(hosts, "%s\t%s\n",address,hostname); - while (fgets(buffer, STRING_SIZE, file)) - { - char *token, *ip, *host, *domain; - - buffer[strlen(buffer) - 1] = 0; - - token = strtok(buffer, ","); - - ip = strtok(NULL, ","); - host = strtok(NULL, ","); - domain = strtok(NULL, ","); - - if (!(ip && host)) - break; - - if (strlen(ip) < 7 || strlen(ip) > 15 - || strspn(ip, "0123456789.") != strlen(ip)) - break; - - if (strspn(host, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-") != strlen(host)) - break; - - if (domain) - fprintf(hosts, "%s\t%s.%s\t%s\n",ip,host,domain,host); - else - fprintf(hosts, "%s\t%s\n",ip,host); - } - fclose(file); - fclose(hosts); - sprintf(commandstring, "/bin/hostname %s.%s", hostname, domainname); if (mysystem(NULL, commandstring)) {