File: dhcp-server.c

package info (click to toggle)
firestarter 1.0.3-11
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,548 kB
  • sloc: ansic: 14,749; sh: 8,068; makefile: 187
file content (144 lines) | stat: -rw-r--r-- 4,505 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*---[ dhcp-server.c ]------------------------------------------------
 * Copyright (C) 2004 Tomas Junnonen (majix@sci.fi)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Functions related to running and configuring the system DHCP server
 *--------------------------------------------------------------------*/

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "dhcp-server.h"
#include "preferences.h"
#include "util.h"

#define DHCPD "/usr/sbin/dhcpd"
#define DHCPD_CONFIG "/etc/dhcpd.conf"
#define DHCP3_CONFIG "/etc/dhcp3/dhcpd.conf"
#define DHCP4_CONFIG "/etc/dhcpd/dhcpd.conf"
#define DHCPD_LOCK_FILE "/var/lock/subsys/dhcpd"

#define DEFAULT_LEASE_TIME "21600"
#define MAX_LEASE_TIME "43200"
#define IP_FORWARDING "off"

#define DHCPD_CONF_TEMPLATE \
	"\n" \
	"subnet ", network, " netmask ", subnet_mask, " {\n" \
	"\toption routers ", router, ";\n" \
	"\toption subnet-mask ", subnet_mask, ";\n" \
	"\toption domain-name-servers ", nameserver, ";\n" \
	"\toption ip-forwarding ", IP_FORWARDING, ";\n" \
	"\trange dynamic-bootp ", lowest_ip, " ", highest_ip, ";\n" \
	"\tdefault-lease-time ", DEFAULT_LEASE_TIME, ";\n" \
	"\tmax-lease-time ", MAX_LEASE_TIME, ";\n" \
	"}\n"
	

gboolean
dhcp_server_exists (void)
{
	return g_file_test (DHCPD, G_FILE_TEST_EXISTS);
}

gboolean
dhcp_server_is_running (void)
{
	return g_file_test (DHCPD_LOCK_FILE, G_FILE_TEST_EXISTS);
}

gboolean
dhcp_server_configuration_exists (void)
{
	return g_file_test (DHCPD_CONFIG, G_FILE_TEST_EXISTS) ||
	       g_file_test (DHCP3_CONFIG, G_FILE_TEST_EXISTS) ||
	       g_file_test (DHCP4_CONFIG, G_FILE_TEST_EXISTS);
}


/* [ calculate_network ]
 * Caluclate the network address given an ip address and subnet mask
 */
static gchar *
calculate_network (char *ip, gchar *subnet)
{
	struct in_addr networkb, ipb, subnetb;

	inet_aton (ip, &ipb);
	inet_aton (subnet, &subnetb);

	networkb.s_addr = ipb.s_addr & subnetb.s_addr;

	return inet_ntoa (networkb);
}

/* [ dhcp_server_create_configuration ]
 * Write the dhcp daemon configuration file
 */
void
dhcp_server_create_configuration (void)
{
	gchar *int_if, *lowest_ip, *highest_ip, *nameserver, *router, *subnet_mask, *network;
	gchar *configuration;
	GIOChannel *out;
	GError *error = NULL;
	gboolean dhcp3_in_use, debian_in_use, dhcp4_in_use;

	/* Load DHCP user configurable settings */
	int_if = preferences_get_string (PREFS_FW_INT_IF);
	lowest_ip = preferences_get_string (PREFS_FW_DHCP_LOWEST_IP);
	highest_ip = preferences_get_string (PREFS_FW_DHCP_HIGHEST_IP);
	nameserver = preferences_get_string (PREFS_FW_DHCP_NAMESERVER);

	/* Load automatically determined settings */
	router = get_ip_of_interface (int_if);
	subnet_mask = get_subnet_of_interface (int_if);
	network = calculate_network (router, subnet_mask);
	
	/* Merge the settings with the config file template */
	dhcp3_in_use = g_file_test (DHCP3_CONFIG, G_FILE_TEST_EXISTS);
	dhcp4_in_use = g_file_test (DHCP4_CONFIG, G_FILE_TEST_EXISTS);
	debian_in_use = g_file_test ("/etc/debian_version", G_FILE_TEST_EXISTS);

	configuration = g_strconcat (
		"# DHCP configuration generated by Firestarter\n", NULL);

	/* Debian doesn't suport ddns unless the dhcp3 package is used */
	if (dhcp3_in_use || dhcp4_in_use || !debian_in_use) {
		configuration = g_strconcat (configuration, 
			"ddns-update-style interim;\n" \
			"ignore client-updates;\n", NULL);
	}
	configuration = g_strconcat (configuration,
		DHCPD_CONF_TEMPLATE, NULL);

	if (dhcp4_in_use)
		out = g_io_channel_new_file (DHCP4_CONFIG, "w+", &error);
	else if (dhcp3_in_use)
		out = g_io_channel_new_file (DHCP3_CONFIG, "w+", &error);
	else
		out = g_io_channel_new_file (DHCPD_CONFIG, "w+", &error);
	if (out == NULL) {
		gchar *error_message = g_strconcat (
			_("Failed to open DHCP server configuration file for writing: "),
			error->message, NULL);
		show_error (error_message);
		g_free (error_message);
	} else {
		if (g_io_channel_write_chars (out, configuration, -1, NULL, &error) == G_IO_STATUS_ERROR) {
			gchar *error_message = g_strconcat (
				_("Failed to write to DHCP server configuration file: "),
				error->message, NULL);
			show_error (error_message);
			g_free (error_message);
		}
		g_free (configuration);
		g_io_channel_shutdown (out, TRUE, NULL);
	}
}