]>
Commit | Line | Data |
---|---|---|
5b2a12ff 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 | * Stuff for setting up the DHCP server from the setup prog. | |
8 | * | |
9 | */ | |
e1457ba0 MT |
10 | |
11 | // Translation | |
12 | #include <libintl.h> | |
13 | #define _(x) dgettext("setup", x) | |
14 | ||
5b2a12ff MT |
15 | #include "setup.h" |
16 | ||
17 | #define TOP 4 | |
18 | ||
19 | #define START_ADDRESS 0 | |
20 | #define END_ADDRESS 1 | |
21 | #define PRIMARY_DNS 2 | |
22 | #define SECONDARY_DNS 3 | |
23 | #define DEFAULT_LEASE_TIME 4 | |
24 | #define MAX_LEASE_TIME 5 | |
25 | #define DOMAIN_NAME_SUFFIX 6 | |
26 | #define MAX_BOXES 7 | |
27 | ||
28 | extern FILE *flog; | |
29 | extern char *mylog; | |
30 | ||
5b2a12ff MT |
31 | extern int automode; |
32 | ||
33 | newtComponent dhcpform; | |
34 | newtComponent entries[MAX_BOXES]; | |
35 | newtComponent enabledcheckbox; | |
36 | ||
37 | void dhcpdialogcallbackdhcp(newtComponent cm, void *data); | |
38 | ||
39 | int handledhcp(void) | |
40 | { | |
41 | char *results[MAX_BOXES]; | |
42 | char enabledresult; | |
43 | char startenabled; | |
44 | struct newtExitStruct es; | |
45 | newtComponent header; | |
46 | newtComponent labels[MAX_BOXES]; | |
47 | newtComponent ok, cancel; | |
48 | char message[1000]; | |
e1457ba0 MT |
49 | char *labeltexts[MAX_BOXES] = { |
50 | _("Start address:"), | |
51 | _("End address:"), | |
52 | _("Primary DNS:"), | |
53 | _("Secondary DNS:"), | |
54 | _("Default lease (mins):"), | |
55 | _("Max lease (mins):"), | |
56 | _("Domain name suffix:") | |
57 | }; | |
58 | char *varnames[MAX_BOXES] = { | |
59 | "START_ADDR_GREEN", | |
60 | "END_ADDR_GREEN", | |
61 | "DNS1_GREEN", | |
62 | "DNS2_GREEN", | |
63 | "DEFAULT_LEASE_TIME_GREEN", | |
64 | "MAX_LEASE_TIME_GREEN", | |
65 | "DOMAIN_NAME_GREEN" | |
66 | }; | |
5b2a12ff MT |
67 | char defaults[MAX_BOXES][STRING_SIZE]; |
68 | int result; | |
69 | int c; | |
70 | char temp[STRING_SIZE]; | |
71 | struct keyvalue *mainkv = initkeyvalues(); | |
72 | struct keyvalue *dhcpkv = initkeyvalues(); | |
73 | struct keyvalue *ethernetkv = initkeyvalues(); | |
74 | int error; | |
75 | FILE *file; | |
76 | char greenaddress[STRING_SIZE]; | |
77 | char greennetaddress[STRING_SIZE]; | |
78 | char greennetmask[STRING_SIZE]; | |
79 | ||
80 | memset(defaults, 0, sizeof(char) * STRING_SIZE * MAX_BOXES); | |
81 | ||
82 | if (!(readkeyvalues(dhcpkv, CONFIG_ROOT "/dhcp/settings"))) | |
83 | { | |
84 | freekeyvalues(dhcpkv); | |
85 | freekeyvalues(ethernetkv); | |
e1457ba0 | 86 | errorbox(_("Unable to open settings file")); |
5b2a12ff MT |
87 | return 0; |
88 | } | |
89 | if (!(readkeyvalues(ethernetkv, CONFIG_ROOT "/ethernet/settings"))) | |
90 | { | |
91 | freekeyvalues(dhcpkv); | |
92 | freekeyvalues(ethernetkv); | |
e1457ba0 | 93 | errorbox(_("Unable to open settings file")); |
5b2a12ff MT |
94 | return 0; |
95 | } | |
96 | if (!(readkeyvalues(mainkv, CONFIG_ROOT "/main/settings"))) | |
97 | { | |
98 | freekeyvalues(dhcpkv); | |
99 | freekeyvalues(ethernetkv); | |
100 | freekeyvalues(mainkv); | |
e1457ba0 | 101 | errorbox(_("Unable to open settings file")); |
5b2a12ff MT |
102 | return 0; |
103 | } | |
104 | ||
105 | /* Set default values. */ | |
106 | findkey(ethernetkv, "GREEN_ADDRESS", defaults[PRIMARY_DNS]); | |
107 | findkey(mainkv, "DOMAINNAME", defaults[DOMAIN_NAME_SUFFIX]); | |
108 | strcpy(defaults[DEFAULT_LEASE_TIME], "60"); | |
109 | strcpy(defaults[MAX_LEASE_TIME], "120"); | |
110 | ||
e1457ba0 | 111 | newtCenteredWindow(55, 18, _("DHCP server configuration")); |
5b2a12ff MT |
112 | |
113 | dhcpform = newtForm(NULL, NULL, 0); | |
e1457ba0 MT |
114 | |
115 | header = newtTextboxReflowed(1, 1, | |
116 | _("Configure the DHCP server by entering the settings information."), | |
117 | 52, 0, 0, 0); | |
5b2a12ff MT |
118 | newtFormAddComponent(dhcpform, header); |
119 | ||
120 | strcpy(temp, ""); findkey(dhcpkv, "ENABLE_GREEN", temp); | |
121 | if (strcmp(temp, "on") == 0) | |
122 | startenabled = '*'; | |
123 | else | |
124 | startenabled = ' '; | |
e1457ba0 | 125 | enabledcheckbox = newtCheckbox(2, TOP + 0, _("Enabled"), startenabled, " *", &enabledresult); |
5b2a12ff MT |
126 | newtFormAddComponent(dhcpform, enabledcheckbox); |
127 | newtComponentAddCallback(enabledcheckbox, dhcpdialogcallbackdhcp, NULL); | |
128 | ||
129 | for (c = 0; c < MAX_BOXES; c++) | |
130 | { | |
131 | labels[c] = newtTextbox(2, TOP + 2 + c, 33, 1, 0); | |
132 | newtTextboxSetText(labels[c], labeltexts[c]); | |
133 | newtFormAddComponent(dhcpform, labels[c]); | |
134 | strcpy(temp, defaults[c]); findkey(dhcpkv, varnames[c], temp); | |
135 | entries[c] = newtEntry(34, TOP + 2 + c, temp, 18, &results[c], 0); | |
136 | newtFormAddComponent(dhcpform, entries[c]); | |
137 | if (startenabled == ' ') | |
138 | newtEntrySetFlags(entries[c], NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); | |
139 | ||
140 | } | |
141 | ||
e1457ba0 MT |
142 | ok = newtButton(10, c + 7, _("OK")); |
143 | cancel = newtButton(34, c + 7, _("Cancel")); | |
5b2a12ff MT |
144 | |
145 | newtFormAddComponents(dhcpform, ok, cancel, NULL); | |
146 | ||
e1457ba0 | 147 | do { |
5b2a12ff MT |
148 | error = 0; |
149 | newtFormRun(dhcpform, &es); | |
150 | ||
151 | if (es.u.co == ok) | |
152 | { | |
153 | /* OK was pressed; verify the contents of each entry. */ | |
154 | if (enabledresult == '*') | |
155 | { | |
e1457ba0 | 156 | strcpy(message, _("The following fields are invalid:\n\n")); |
5b2a12ff MT |
157 | if (inet_addr(results[START_ADDRESS]) == INADDR_NONE) |
158 | { | |
e1457ba0 MT |
159 | strcat(message, _("Start address")); |
160 | strcat(message, "\n"); | |
5b2a12ff MT |
161 | error = 1; |
162 | } | |
163 | if (inet_addr(results[END_ADDRESS]) == INADDR_NONE) | |
164 | { | |
e1457ba0 MT |
165 | strcat(message, _("End address")); |
166 | strcat(message, "\n"); | |
5b2a12ff MT |
167 | error = 1; |
168 | } | |
169 | if (strlen(results[SECONDARY_DNS])) | |
170 | { | |
171 | if (inet_addr(results[PRIMARY_DNS]) == INADDR_NONE) | |
172 | { | |
e1457ba0 MT |
173 | strcat(message, _("Primary DNS")); |
174 | strcat(message, "\n"); | |
5b2a12ff MT |
175 | error = 1; |
176 | } | |
177 | } | |
178 | if (strlen(results[SECONDARY_DNS])) | |
179 | { | |
180 | if (inet_addr(results[SECONDARY_DNS]) == INADDR_NONE) | |
181 | { | |
e1457ba0 MT |
182 | strcat(message, _("Secondary DNS")); |
183 | strcat(message, "\n"); | |
5b2a12ff MT |
184 | error = 1; |
185 | } | |
186 | } | |
187 | if (!(atol(results[DEFAULT_LEASE_TIME]))) | |
188 | { | |
e1457ba0 MT |
189 | strcat(message, _("Default lease time")); |
190 | strcat(message, "\n"); | |
5b2a12ff MT |
191 | error = 1; |
192 | } | |
193 | if (!(atol(results[MAX_LEASE_TIME]))) | |
194 | { | |
e1457ba0 MT |
195 | strcat(message, _("Max. lease time")); |
196 | strcat(message, "\n"); | |
5b2a12ff MT |
197 | error = 1; |
198 | } | |
199 | } | |
200 | ||
201 | if (error) | |
202 | errorbox(message); | |
203 | else | |
204 | { | |
205 | for (c = 0; c < MAX_BOXES; c++) | |
206 | replacekeyvalue(dhcpkv, varnames[c], results[c]); | |
207 | if (enabledresult == '*') | |
208 | { | |
209 | replacekeyvalue(dhcpkv, "ENABLE_GREEN", "on"); | |
210 | fclose(fopen(CONFIG_ROOT "/dhcp/enable_green", "w")); | |
211 | chown(CONFIG_ROOT "/dhcp/enable_green", 99, 99); | |
46b56e20 | 212 | mysystem(NULL, "/usr/local/bin/dhcpctrl enable"); |
5b2a12ff MT |
213 | } |
214 | else | |
215 | { | |
216 | replacekeyvalue(dhcpkv, "ENABLE_GREEN", "off"); | |
217 | unlink(CONFIG_ROOT "/dhcp/enable_green"); | |
46b56e20 | 218 | mysystem(NULL, "/usr/local/bin/dhcpctrl disable"); |
5b2a12ff MT |
219 | } |
220 | replacekeyvalue(dhcpkv, "VALID", "yes"); | |
221 | writekeyvalues(dhcpkv, CONFIG_ROOT "/dhcp/settings"); | |
222 | ||
223 | findkey(ethernetkv, "GREEN_ADDRESS", greenaddress); | |
224 | findkey(ethernetkv, "GREEN_NETADDRESS", greennetaddress); | |
225 | findkey(ethernetkv, "GREEN_NETMASK", greennetmask); | |
226 | ||
227 | file = fopen(CONFIG_ROOT "/dhcp/dhcpd.conf", "w"); | |
228 | fprintf(file, "ddns-update-style none;\n"); | |
7b6548b2 | 229 | fprintf(file, "authoritative;\n"); |
5b2a12ff MT |
230 | fprintf(file, "subnet %s netmask %s\n", greennetaddress, greennetmask); |
231 | fprintf(file, "{\n"); | |
232 | fprintf(file, "\toption subnet-mask %s;\n", greennetmask); | |
233 | fprintf(file, "\toption domain-name \"%s\";\n", results[DOMAIN_NAME_SUFFIX]); | |
234 | fprintf(file, "\toption routers %s;\n", greenaddress); | |
235 | if (strlen(results[PRIMARY_DNS])) | |
236 | { | |
237 | fprintf(file, "\toption domain-name-servers "); | |
238 | fprintf(file, "%s", results[PRIMARY_DNS]); | |
239 | if (strlen(results[SECONDARY_DNS])) | |
240 | fprintf(file, ", %s", results[SECONDARY_DNS]); | |
241 | fprintf(file, ";\n"); | |
242 | } | |
243 | ||
244 | fprintf(file, "\trange %s %s;\n", results[START_ADDRESS], results[END_ADDRESS]); | |
245 | fprintf(file, "\tdefault-lease-time %d;\n", (int) atol(results[DEFAULT_LEASE_TIME]) * 60); | |
246 | fprintf(file, "\tmax-lease-time %d;\n", (int) atol(results[MAX_LEASE_TIME]) * 60); | |
247 | fprintf(file, "}\n"); | |
248 | fclose(file); | |
249 | chown(CONFIG_ROOT "/dhcp/dhcpd.conf", 99, 99); | |
250 | if (automode == 0) | |
46b56e20 | 251 | mysystem(NULL, "/usr/local/bin/dhcpctrl enable"); |
5b2a12ff MT |
252 | } |
253 | result = 1; | |
254 | } | |
255 | else | |
256 | result = 0; | |
e1457ba0 | 257 | } while (error); |
5b2a12ff MT |
258 | |
259 | newtFormDestroy(dhcpform); | |
260 | newtPopWindow(); | |
261 | ||
262 | freekeyvalues(dhcpkv); | |
263 | freekeyvalues(ethernetkv); | |
264 | freekeyvalues(mainkv); | |
265 | ||
266 | return result; | |
267 | } | |
268 | ||
269 | /* Called when enabled flag is toggled. Toggle disabled state of other 3 | |
270 | * controls. */ | |
271 | void dhcpdialogcallbackdhcp(newtComponent cm, void *data) | |
272 | { | |
273 | int c; | |
274 | ||
275 | for (c = 0; c < MAX_BOXES; c++) | |
276 | newtEntrySetFlags(entries[c], NEWT_FLAG_DISABLED, NEWT_FLAGS_TOGGLE); | |
277 | ||
278 | newtRefresh(); | |
279 | newtDrawForm(dhcpform); | |
280 | } |