]> git.ipfire.org Git - ipfire-2.x.git/blame - src/setup/misc.c
apache: Write hostname into configuration at boot time
[ipfire-2.x.git] / src / setup / misc.c
CommitLineData
bba7212c
MT
1/* SmoothWall setup program.
2 *
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
5 *
6 * (c) Lawrence Manning, 2001
7 * Misc. stuff for the lib.
8 *
9 */
e1457ba0
MT
10
11// Translation
12#include <libintl.h>
13#define _(x) dgettext("setup", x)
14
bba7212c
MT
15#include "setup.h"
16
17extern FILE *flog;
18extern char *mylog;
19
bba7212c
MT
20extern int automode;
21
22/* This will rewrite /etc/hosts, /etc/hosts.*, and the apache ServerName file. */
23int writehostsfiles(void)
24{
25 char address[STRING_SIZE] = "";
26 char netaddress[STRING_SIZE] = "";
27 char netmask[STRING_SIZE] = "";
28 char message[1000];
29 FILE *file, *hosts;
30 struct keyvalue *kv;
31 char hostname[STRING_SIZE];
8dc9629e 32 char domainname[STRING_SIZE] = "localdomain";
bba7212c
MT
33 char commandstring[STRING_SIZE];
34 char buffer[STRING_SIZE];
35
36 kv = initkeyvalues();
37 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
38 {
39 freekeyvalues(kv);
e1457ba0 40 errorbox(_("Unable to open settings file"));
bba7212c
MT
41 return 0;
42 }
43 findkey(kv, "GREEN_ADDRESS", address);
44 findkey(kv, "GREEN_NETADDRESS", netaddress);
45 findkey(kv, "GREEN_NETMASK", netmask);
46 freekeyvalues(kv);
47
48 kv = initkeyvalues();
49 if (!(readkeyvalues(kv, CONFIG_ROOT "/main/settings")))
50 {
51 freekeyvalues(kv);
e1457ba0 52 errorbox(_("Unable to open settings file"));
bba7212c
MT
53 return 0;
54 }
55 strcpy(hostname, SNAME );
56 findkey(kv, "HOSTNAME", hostname);
57 findkey(kv, "DOMAINNAME", domainname);
58 freekeyvalues(kv);
59
bba7212c
MT
60 if (!(file = fopen(CONFIG_ROOT "/main/hosts", "r")))
61 {
e1457ba0 62 errorbox(_("Unable to open main hosts file."));
bba7212c
MT
63 return 0;
64 }
65 if (!(hosts = fopen("/etc/hosts", "w")))
66 {
e1457ba0 67 errorbox(_("Unable to write /etc/hosts."));
bba7212c
MT
68 return 0;
69 }
70 fprintf(hosts, "127.0.0.1\tlocalhost\n");
71 if (strlen(domainname))
72 fprintf(hosts, "%s\t%s.%s\t%s\n",address,hostname,domainname,hostname);
73 else
74 fprintf(hosts, "%s\t%s\n",address,hostname);
75 while (fgets(buffer, STRING_SIZE, file))
76 {
77 char *token, *ip, *host, *domain;
78
79 buffer[strlen(buffer) - 1] = 0;
80
81 token = strtok(buffer, ",");
82
83 ip = strtok(NULL, ",");
84 host = strtok(NULL, ",");
85 domain = strtok(NULL, ",");
86
87 if (!(ip && host))
88 break;
89
90 if (strlen(ip) < 7 || strlen(ip) > 15
91 || strspn(ip, "0123456789.") != strlen(ip))
92 break;
93
94 if (strspn(host, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-") != strlen(host))
95 break;
96
97 if (domain)
98 fprintf(hosts, "%s\t%s.%s\t%s\n",ip,host,domain,host);
99 else
100 fprintf(hosts, "%s\t%s\n",ip,host);
101 }
102 fclose(file);
103 fclose(hosts);
104
105 /* TCP wrappers stuff. */
106 if (!(file = fopen("/etc/hosts.deny", "w")))
107 {
e1457ba0 108 errorbox(_("Unable to write /etc/hosts.deny."));
bba7212c
MT
109 return 0;
110 }
111 fprintf(file, "ALL : ALL\n");
112 fclose(file);
113
114 if (!(file = fopen("/etc/hosts.allow", "w")))
115 {
e1457ba0 116 errorbox(_("Unable to write /etc/hosts.allow."));
bba7212c
MT
117 return 0;
118 }
119 fprintf(file, "sshd : ALL\n");
120 fprintf(file, "ALL : localhost\n");
121 fprintf(file, "ALL : %s/%s\n", netaddress, netmask);
122 fclose(file);
123
124 sprintf(commandstring, "/bin/hostname %s.%s", hostname, domainname);
46b56e20 125 if (mysystem(NULL, commandstring))
bba7212c 126 {
e1457ba0 127 errorbox(_("Unable to set hostname."));
bba7212c
MT
128 return 0;
129 }
130
131 return 1;
132}