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