2
Copyright (c) 2009 Eucalyptus Systems, Inc.
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, only version 3 of the License.
8
This file is distributed in the hope that it will be useful, but WITHOUT
9
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
You should have received a copy of the GNU General Public License along
14
with this program. If not, see <http://www.gnu.org/licenses/>.
16
Please contact Eucalyptus Systems, Inc., 130 Castilian
17
Dr., Goleta, CA 93101 USA or visit <http://www.eucalyptus.com/licenses/>
18
if you need additional information or have any questions.
20
This file may incorporate work covered under the following copyright and
23
Software License Agreement (BSD License)
25
Copyright (c) 2008, Regents of the University of California
28
Redistribution and use of this software in source and binary forms, with
29
or without modification, are permitted provided that the following
32
Redistributions of source code must retain the above copyright notice,
33
this list of conditions and the following disclaimer.
35
Redistributions in binary form must reproduce the above copyright
36
notice, this list of conditions and the following disclaimer in the
37
documentation and/or other materials provided with the distribution.
39
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
40
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
42
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
43
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
45
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
46
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
47
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
48
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
49
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. USERS OF
50
THIS SOFTWARE ACKNOWLEDGE THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE
51
LICENSED MATERIAL, COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS
52
SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
53
IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
54
BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
55
THE REGENTS’ DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
56
OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
57
WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
58
ANY SUCH LICENSES OR RIGHTS.
62
#include <string.h> /* strdup */
63
#include <sys/types.h> /* open */
64
#include <sys/stat.h> /* open */
65
#include <fcntl.h> /* open */
66
#include <errno.h> /* perror */
70
int initDHCP(dhcpConfig *dhcpconfig, char *path, char *deamon, char *pubmode, char *pubInterface, char *pubSubnet, char *pubSubnetMask, char *pubBroadcastAddress, char *pubRouter,char *pubDNS, char *pubRangeMin, char *pubRangeMax, char *privSubnet, char *privSubnetMask, char *privBroadcastAddress) {
71
char newpath[1024], cmd[1024];
77
if (!dhcpconfig->initialized) {
79
bzero(dhcpconfig, sizeof(dhcpConfig));
82
strncpy(dhcpconfig->pubmode, pubmode, 32);
84
strncpy(dhcpconfig->pubmode, "SYSTEM", 32);
91
snprintf(newpath, 1024, "%s/euca-dhcp.conf", path);
92
snprintf(cmd, 1024, "mkdir -p %s; touch %s", path, newpath);
94
strncpy(dhcpconfig->config, newpath, 1024);
96
snprintf(newpath, 1024, "%s/euca-dhcp.pid", path);
97
snprintf(cmd, 1024, "mkdir -p %s; touch %s", path, newpath);
99
strncpy(dhcpconfig->pidfile, newpath, 1024);
101
snprintf(newpath, 1024, "%s/euca-dhcp.leases", path);
102
snprintf(cmd, 1024, "mkdir -p %s; touch %s", path, newpath);
104
strncpy(dhcpconfig->leases, newpath, 1024);
106
snprintf(newpath, 1024, "%s/euca-dhcp.trace", path);
107
snprintf(cmd, 1024, "mkdir -p %s; touch %s", path, newpath);
109
strncpy(dhcpconfig->trace, newpath, 1024);
112
strncpy(dhcpconfig->deamon, deamon, 32);
114
strncpy(dhcpconfig->deamon, "/usr/sbin/dhcpd", 32);
118
strncpy(dhcpconfig->privSubnet, privSubnet, 32);
120
strncpy(dhcpconfig->privSubnet, "192.168.0.0", 32);
123
if (privSubnetMask) {
124
strncpy(dhcpconfig->privSubnetMask, privSubnetMask, 32);
126
strncpy(dhcpconfig->privSubnetMask, "255.255.0.0", 32);
129
if (privBroadcastAddress) {
130
strncpy(dhcpconfig->privBroadcastAddress, privBroadcastAddress, 32);
132
strncpy(dhcpconfig->privBroadcastAddress, "192.168.255.255", 32);
135
if (!strcmp(dhcpconfig->pubmode, "STATIC") || !strcmp(dhcpconfig->pubmode, "DYNAMIC")) {
137
strncpy(dhcpconfig->pubInterface, pubInterface, 32);
139
strncpy(dhcpconfig->pubInterface, "eth0", 32);
143
strncpy(dhcpconfig->pubSubnet, pubSubnet, 32);
145
strncpy(dhcpconfig->pubSubnet, "10.0.0.0", 32);
149
strncpy(dhcpconfig->pubSubnetMask, pubSubnetMask, 32);
151
strncpy(dhcpconfig->pubSubnetMask, "255.0.0.0", 32);
154
if (pubBroadcastAddress) {
155
strncpy(dhcpconfig->pubBroadcastAddress, pubBroadcastAddress, 32);
157
strncpy(dhcpconfig->pubBroadcastAddress, "10.255.255.255", 32);
161
strncpy(dhcpconfig->pubRouter, pubRouter, 32);
163
strncpy(dhcpconfig->pubRouter, "10.1.1.1", 32);
167
strncpy(dhcpconfig->pubDNS, pubDNS, 32);
169
strncpy(dhcpconfig->pubDNS, "10.1.1.1", 32);
174
strncpy(dhcpconfig->pubRangeMin, pubRangeMin, 32);
176
strncpy(dhcpconfig->pubRangeMin, "10.128.1.1", 32);
180
strncpy(dhcpconfig->pubRangeMax, pubRangeMax, 32);
182
strncpy(dhcpconfig->pubRangeMax, "10.128.1.254", 32);
186
writeDHCPConfig(dhcpconfig);
187
dhcpconfig->initialized=1;
191
int writeDHCPConfig(dhcpConfig *dhcpconfig) {
195
fp = fopen(dhcpconfig->config, "w");
200
fprintf(fp, "# automatically generated config file for DHCP server\ndefault-lease-time 1200;\nmax-lease-time 1200;\nddns-update-style none;\n\n");
202
fprintf(fp, "subnet %s netmask %s {\n option subnet-mask %s;\n option broadcast-address %s;\n}\n", dhcpconfig->privSubnet, dhcpconfig->privSubnetMask, dhcpconfig->privSubnetMask, dhcpconfig->privBroadcastAddress);
204
logprintf("public dhcp mode set to %s\n", dhcpconfig->pubmode);
206
if (!strcmp(dhcpconfig->pubmode, "STATIC") || !strcmp(dhcpconfig->pubmode, "DYNAMIC")) {
207
fprintf(fp, "subnet %s netmask %s {\n option subnet-mask %s;\n option broadcast-address %s;\n option domain-name-servers %s;\n option routers %s;\n", dhcpconfig->pubSubnet, dhcpconfig->pubSubnetMask, dhcpconfig->pubSubnetMask, dhcpconfig->pubBroadcastAddress, dhcpconfig->pubDNS, dhcpconfig->pubRouter);
209
if (!strcmp(dhcpconfig->pubmode, "DYNAMIC")) {
210
fprintf(fp, " range %s %s;\n", dhcpconfig->pubRangeMin, dhcpconfig->pubRangeMax);
215
for (i=0; i<2048; i++) {
218
if (dhcpconfig->macs[i][0] != '\0') {
219
mac = dhcpconfig->macs[i];
220
ip = dhcpconfig->ips[i];
221
fprintf(fp, "\nhost node-%s {\n hardware ethernet %s;\n fixed-address %s;\n}\n", ip, mac, ip);
230
int addDHCPDev(dhcpConfig *dhcpconfig, char *dev) {
231
int i, done=0, found;
233
if (!dhcpconfig || !dev) {
238
for (i=0; i<128 && !done; i++) {
239
if (!found && dhcpconfig->etherdevs[i][0] == '\0') {
241
} else if (!strncmp(dhcpconfig->etherdevs[i], dev, 32)) {
246
if (found && !done) {
247
strncpy(dhcpconfig->etherdevs[found], dev, 32);
249
kickDHCPDaemon(dhcpconfig);
253
int delDHCPDev(dhcpConfig *dhcpconfig, char *dev) {
256
if (!dhcpconfig || !dev) {
260
for (i=0; i<128 && !done; i++) {
261
if (!strncmp(dhcpconfig->etherdevs[i], dev, 32)) {
262
bzero(dhcpconfig->etherdevs[i], 32);
267
kickDHCPDaemon(dhcpconfig);
271
int addDHCPHost(dhcpConfig *dhcpconfig, char *hostmac, char *hostip) {
274
if (!dhcpconfig || !hostmac || !hostip) {
278
for (i=0; i<2048 && !done; i++) {
279
if (dhcpconfig->macs[i][0] == '\0') {
280
strncpy(dhcpconfig->macs[i], hostmac, 32);
281
strncpy(dhcpconfig->ips[i], hostip, 32);
285
kickDHCPDaemon(dhcpconfig);
286
// writeDHCPConfig(dhcpconfig);
290
int delDHCPHost(dhcpConfig *dhcpconfig, char *hostmac, char *hostip) {
293
for (i=0; i<2048 && !done; i++) {
294
if ((!hostmac || !strncmp(dhcpconfig->macs[i], hostmac, 32)) && (!hostip || !strncmp(dhcpconfig->ips[i], hostip, 32))) {
295
bzero(dhcpconfig->macs[i], 32);
296
bzero(dhcpconfig->ips[i], 32);
301
kickDHCPDaemon(dhcpconfig);
302
// writeDHCPConfig(dhcpconfig);
306
int kickDHCPDaemon(dhcpConfig *dhcpconfig) {
307
char dstring [512] = "";
311
if (dhcpconfig->pubInterface) {
312
snprintf(dstring, 512, "%s", dhcpconfig->pubInterface);
315
rc = writeDHCPConfig(dhcpconfig);
317
logprintf ("failed to (re)create %s\n", dhcpconfig->config);
321
for (i=0; i<128; i++) {
322
if (dhcpconfig->etherdevs[i][0] != '\0') {
323
strncat (dstring, " ", 512);
324
strncat (dstring, dhcpconfig->etherdevs[i], 32);
328
/* force dhcpd to reload the conf */
329
snprintf (buf, 512, "rm -f %s", dhcpconfig->trace);
330
logprintf ("executing: %s\n", buf);
333
snprintf (buf, 512, "kill `cat %s`", dhcpconfig->pidfile);
334
logprintf ("executing: %s\n", buf);
337
snprintf (buf, 512, "%s -cf %s -lf %s -pf %s -tf %s %s",
338
dhcpconfig->deamon, dhcpconfig->config, dhcpconfig->leases, dhcpconfig->pidfile, dhcpconfig->trace, dstring);
339
logprintf ("executing: %s\n", buf);
341
logprintf("\tRC from cmd: %d\n", rc);