]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/commitdiff
setup: Write /etc/hosts in initscript
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 25 Jun 2018 10:08:04 +0000 (11:08 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 25 Jun 2018 10:08:04 +0000 (11:08 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/initscripts/system/localnet
src/setup/misc.c

index ff374bb2b1332361da314fab331dfe53b756a205..7ff18d7b473dcdf55e5282baec33fe732bd926c0 100644 (file)
@@ -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
                ;;
index b8e7820e143ee10e93b189d3ef283e7e72f9ce43..f9ba39b8c731dcd5cf6c4a98b921106977994bcc 100644 (file)
@@ -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))
        {