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