setup: Write /etc/hosts in initscript

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
This commit is contained in:
Michael Tremer
2018-06-25 11:08:04 +01:00
parent d97ba75fe5
commit fd52e82a72
2 changed files with 15 additions and 63 deletions

View 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
;;

View 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))
{