1 /* SmoothWall setup program.
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
6 * (c) Lawrence Manning, 2001
7 * Stuff for setting up the DHCP server from the setup prog.
9 * $Id: dhcp.c,v 1.9.2.3 2004/12/03 06:46:50 eoberlander Exp $
17 #define START_ADDRESS 0
20 #define SECONDARY_DNS 3
21 #define DEFAULT_LEASE_TIME 4
22 #define MAX_LEASE_TIME 5
23 #define DOMAIN_NAME_SUFFIX 6
33 newtComponent dhcpform
;
34 newtComponent entries
[MAX_BOXES
];
35 newtComponent enabledcheckbox
;
37 void dhcpdialogcallbackdhcp(newtComponent cm
, void *data
);
41 char *results
[MAX_BOXES
];
44 struct newtExitStruct es
;
46 newtComponent labels
[MAX_BOXES
];
47 newtComponent ok
, cancel
;
49 char *labeltexts
[MAX_BOXES
] = { ctr
[TR_START_ADDRESS
], ctr
[TR_END_ADDRESS
],
50 ctr
[TR_PRIMARY_DNS
], ctr
[TR_SECONDARY_DNS
], ctr
[TR_DEFAULT_LEASE
],
51 ctr
[TR_MAX_LEASE
], ctr
[TR_DOMAIN_NAME_SUFFIX
] };
52 char *varnames
[MAX_BOXES
] = { "START_ADDR_GREEN", "END_ADDR_GREEN",
53 "DNS1_GREEN", "DNS2_GREEN",
54 "DEFAULT_LEASE_TIME_GREEN", "MAX_LEASE_TIME_GREEN",
56 char defaults
[MAX_BOXES
][STRING_SIZE
];
59 char temp
[STRING_SIZE
];
60 struct keyvalue
*mainkv
= initkeyvalues();
61 struct keyvalue
*dhcpkv
= initkeyvalues();
62 struct keyvalue
*ethernetkv
= initkeyvalues();
65 char greenaddress
[STRING_SIZE
];
66 char greennetaddress
[STRING_SIZE
];
67 char greennetmask
[STRING_SIZE
];
69 memset(defaults
, 0, sizeof(char) * STRING_SIZE
* MAX_BOXES
);
71 if (!(readkeyvalues(dhcpkv
, CONFIG_ROOT
"/dhcp/settings")))
73 freekeyvalues(dhcpkv
);
74 freekeyvalues(ethernetkv
);
75 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
78 if (!(readkeyvalues(ethernetkv
, CONFIG_ROOT
"/ethernet/settings")))
80 freekeyvalues(dhcpkv
);
81 freekeyvalues(ethernetkv
);
82 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
85 if (!(readkeyvalues(mainkv
, CONFIG_ROOT
"/main/settings")))
87 freekeyvalues(dhcpkv
);
88 freekeyvalues(ethernetkv
);
89 freekeyvalues(mainkv
);
90 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
94 /* Set default values. */
95 findkey(ethernetkv
, "GREEN_ADDRESS", defaults
[PRIMARY_DNS
]);
96 findkey(mainkv
, "DOMAINNAME", defaults
[DOMAIN_NAME_SUFFIX
]);
97 strcpy(defaults
[DEFAULT_LEASE_TIME
], "60");
98 strcpy(defaults
[MAX_LEASE_TIME
], "120");
100 sprintf(message
, ctr
[TR_DHCP_SERVER_CONFIGURATION
]);
101 newtCenteredWindow(55, 18, message
);
103 dhcpform
= newtForm(NULL
, NULL
, 0);
105 sprintf(message
, ctr
[TR_CONFIGURE_DHCP
]);
106 header
= newtTextboxReflowed(1, 1, message
, 52, 0, 0, 0);
107 newtFormAddComponent(dhcpform
, header
);
109 strcpy(temp
, ""); findkey(dhcpkv
, "ENABLE_GREEN", temp
);
110 if (strcmp(temp
, "on") == 0)
114 enabledcheckbox
= newtCheckbox(2, TOP
+ 0, ctr
[TR_ENABLED
], startenabled
, " *", &enabledresult
);
115 newtFormAddComponent(dhcpform
, enabledcheckbox
);
116 newtComponentAddCallback(enabledcheckbox
, dhcpdialogcallbackdhcp
, NULL
);
118 for (c
= 0; c
< MAX_BOXES
; c
++)
120 labels
[c
] = newtTextbox(2, TOP
+ 2 + c
, 33, 1, 0);
121 newtTextboxSetText(labels
[c
], labeltexts
[c
]);
122 newtFormAddComponent(dhcpform
, labels
[c
]);
123 strcpy(temp
, defaults
[c
]); findkey(dhcpkv
, varnames
[c
], temp
);
124 entries
[c
] = newtEntry(34, TOP
+ 2 + c
, temp
, 18, &results
[c
], 0);
125 newtFormAddComponent(dhcpform
, entries
[c
]);
126 if (startenabled
== ' ')
127 newtEntrySetFlags(entries
[c
], NEWT_FLAG_DISABLED
, NEWT_FLAGS_SET
);
131 ok
= newtButton(10, c
+ 7, ctr
[TR_OK
]);
132 cancel
= newtButton(34, c
+ 7, ctr
[TR_CANCEL
]);
134 newtFormAddComponents(dhcpform
, ok
, cancel
, NULL
);
139 newtFormRun(dhcpform
, &es
);
143 /* OK was pressed; verify the contents of each entry. */
144 if (enabledresult
== '*')
146 strcpy(message
, ctr
[TR_INVALID_FIELDS
]);
147 if (inet_addr(results
[START_ADDRESS
]) == INADDR_NONE
)
149 strcat(message
, ctr
[TR_START_ADDRESS_CR
]);
152 if (inet_addr(results
[END_ADDRESS
]) == INADDR_NONE
)
154 strcat(message
, ctr
[TR_END_ADDRESS_CR
]);
157 if (strlen(results
[SECONDARY_DNS
]))
159 if (inet_addr(results
[PRIMARY_DNS
]) == INADDR_NONE
)
161 strcat(message
, ctr
[TR_PRIMARY_DNS_CR
]);
165 if (strlen(results
[SECONDARY_DNS
]))
167 if (inet_addr(results
[SECONDARY_DNS
]) == INADDR_NONE
)
169 strcat(message
, ctr
[TR_SECONDARY_DNS_CR
]);
173 if (!(atol(results
[DEFAULT_LEASE_TIME
])))
175 strcat(message
, ctr
[TR_DEFAULT_LEASE_CR
]);
178 if (!(atol(results
[MAX_LEASE_TIME
])))
180 strcat(message
, ctr
[TR_MAX_LEASE_CR
]);
189 for (c
= 0; c
< MAX_BOXES
; c
++)
190 replacekeyvalue(dhcpkv
, varnames
[c
], results
[c
]);
191 if (enabledresult
== '*')
193 replacekeyvalue(dhcpkv
, "ENABLE_GREEN", "on");
194 fclose(fopen(CONFIG_ROOT
"/dhcp/enable_green", "w"));
195 chown(CONFIG_ROOT
"/dhcp/enable_green", 99, 99);
199 replacekeyvalue(dhcpkv
, "ENABLE_GREEN", "off");
200 unlink(CONFIG_ROOT
"/dhcp/enable_green");
202 replacekeyvalue(dhcpkv
, "VALID", "yes");
203 writekeyvalues(dhcpkv
, CONFIG_ROOT
"/dhcp/settings");
205 findkey(ethernetkv
, "GREEN_ADDRESS", greenaddress
);
206 findkey(ethernetkv
, "GREEN_NETADDRESS", greennetaddress
);
207 findkey(ethernetkv
, "GREEN_NETMASK", greennetmask
);
209 file
= fopen(CONFIG_ROOT
"/dhcp/dhcpd.conf", "w");
210 fprintf(file
, "ddns-update-style none;\n");
211 fprintf(file
, "subnet %s netmask %s\n", greennetaddress
, greennetmask
);
212 fprintf(file
, "{\n");
213 fprintf(file
, "\toption subnet-mask %s;\n", greennetmask
);
214 fprintf(file
, "\toption domain-name \"%s\";\n", results
[DOMAIN_NAME_SUFFIX
]);
215 fprintf(file
, "\toption routers %s;\n", greenaddress
);
216 if (strlen(results
[PRIMARY_DNS
]))
218 fprintf(file
, "\toption domain-name-servers ");
219 fprintf(file
, "%s", results
[PRIMARY_DNS
]);
220 if (strlen(results
[SECONDARY_DNS
]))
221 fprintf(file
, ", %s", results
[SECONDARY_DNS
]);
222 fprintf(file
, ";\n");
225 fprintf(file
, "\trange %s %s;\n", results
[START_ADDRESS
], results
[END_ADDRESS
]);
226 fprintf(file
, "\tdefault-lease-time %d;\n", (int) atol(results
[DEFAULT_LEASE_TIME
]) * 60);
227 fprintf(file
, "\tmax-lease-time %d;\n", (int) atol(results
[MAX_LEASE_TIME
]) * 60);
228 fprintf(file
, "}\n");
230 chown(CONFIG_ROOT
"/dhcp/dhcpd.conf", 99, 99);
232 mysystem("/usr/local/bin/restartdhcp");
241 newtFormDestroy(dhcpform
);
244 freekeyvalues(dhcpkv
);
245 freekeyvalues(ethernetkv
);
246 freekeyvalues(mainkv
);
251 /* Called when enabled flag is toggled. Toggle disabled state of other 3
253 void dhcpdialogcallbackdhcp(newtComponent cm
, void *data
)
257 for (c
= 0; c
< MAX_BOXES
; c
++)
258 newtEntrySetFlags(entries
[c
], NEWT_FLAG_DISABLED
, NEWT_FLAGS_TOGGLE
);
261 newtDrawForm(dhcpform
);