]> git.ipfire.org Git - ipfire-2.x.git/blame - src/patches/dnsmasq-Add-support-to-read-ISC-DHCP-lease-file.patch
lcdproc: Update to 0.5.7
[ipfire-2.x.git] / src / patches / dnsmasq-Add-support-to-read-ISC-DHCP-lease-file.patch
CommitLineData
fbcc3cb7
MF
1--- a/src/cache.c Wed Dec 16 19:24:12 2015
2+++ b/src/cache.c Wed Dec 16 19:37:37 2015
45e65f1d
MT
3@@ -17,7 +17,7 @@
4 #include "dnsmasq.h"
5
6 static struct crec *cache_head = NULL, *cache_tail = NULL, **hash_table = NULL;
7-#ifdef HAVE_DHCP
8+#if (defined HAVE_DHCP) || (defined HAVE_ISC_READER)
9 static struct crec *dhcp_spare = NULL;
10 #endif
11 static struct crec *new_chain = NULL;
fbcc3cb7 12@@ -217,6 +217,9 @@
45e65f1d
MT
13 crecp->flags &= ~F_BIGNAME;
14 }
15
16+ if (crecp->flags & F_DHCP)
17+ free(crecp->name.namep);
18+
19 #ifdef HAVE_DNSSEC
20 cache_blockdata_free(crecp);
21 #endif
a6cd8b9b 22@@ -1138,7 +1141,7 @@
32e321b2 23
45e65f1d
MT
24 }
25
26-#ifdef HAVE_DHCP
27+#if (defined HAVE_DHCP) || (defined HAVE_ISC_READER)
28 struct in_addr a_record_from_hosts(char *name, time_t now)
29 {
30 struct crec *crecp = NULL;
a6cd8b9b 31@@ -1281,7 +1284,11 @@
45e65f1d
MT
32 else
33 crec->ttd = ttd;
34 crec->addr.addr = *host_address;
35+#ifdef HAVE_ISC_READER
36+ crec->name.namep = strdup(host_name);
37+#else
38 crec->name.namep = host_name;
39+#endif
40 crec->uid = next_uid();
41 cache_hash(crec);
42
fbcc3cb7
MF
43--- a/src/dnsmasq.c Thu Jul 30 20:59:06 2015
44+++ b/src/dnsmasq.c Wed Dec 16 19:38:32 2015
a6cd8b9b 45@@ -1013,6 +1013,11 @@
45e65f1d
MT
46
47 poll_resolv(0, daemon->last_resolv != 0, now);
48 daemon->last_resolv = now;
49+
50+#ifdef HAVE_ISC_READER
51+ if (daemon->lease_file && !daemon->dhcp)
52+ load_dhcp(now);
53+#endif
54 }
32e321b2
MT
55 #endif
56
fbcc3cb7
MF
57--- a/src/dnsmasq.h Wed Dec 16 19:24:12 2015
58+++ b/src/dnsmasq.h Wed Dec 16 19:40:11 2015
a6cd8b9b 59@@ -1514,6 +1514,11 @@
d08045ea
MT
60 void poll_listen(int fd, short event);
61 int do_poll(int timeout);
62
45e65f1d
MT
63+/* isc.c */
64+#ifdef HAVE_ISC_READER
65+void load_dhcp(time_t now);
66+#endif
fbcc3cb7
MF
67+
68 /* rrfilter.c */
69 size_t rrfilter(struct dns_header *header, size_t plen, int mode);
70 u16 *rrfilter_desc(int type);
71 int expand_workspace(unsigned char ***wkspc, int *szp, int new);
72-
73--- /dev/null Wed Dec 16 19:48:08 2015
74+++ b/src/isc.c Wed Dec 16 19:41:35 2015
3da4cc02 75@@ -0,0 +1,251 @@
45e65f1d
MT
76+/* dnsmasq is Copyright (c) 2014 John Volpe, Simon Kelley and
77+ Michael Tremer
78+
79+ This program is free software; you can redistribute it and/or modify
80+ it under the terms of the GNU General Public License as published by
81+ the Free Software Foundation; version 2 dated June, 1991, or
82+ (at your option) version 3 dated 29 June, 2007.
83+
84+ This program is distributed in the hope that it will be useful,
85+ but WITHOUT ANY WARRANTY; without even the implied warranty of
86+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
87+ GNU General Public License for more details.
88+
89+ You should have received a copy of the GNU General Public License
90+ along with this program. If not, see <http://www.gnu.org/licenses/>.
91+
92+ Code in this file is based on contributions by John Volpe and
93+ Simon Kelley. Updated for recent versions of dnsmasq by
94+ Michael Tremer.
95+*/
96+
97+#include "dnsmasq.h"
98+
99+#ifdef HAVE_ISC_READER
100+#define MAXTOK 50
101+
102+struct isc_dhcp_lease {
103+ char* name;
104+ char* fqdn;
105+ time_t expires;
106+ struct in_addr addr;
107+ struct isc_dhcp_lease* next;
108+};
109+
3da4cc02 110+static struct isc_dhcp_lease* dhcp_lease_new(const char* hostname) {
45e65f1d
MT
111+ struct isc_dhcp_lease* lease = whine_malloc(sizeof(*lease));
112+
3da4cc02
MT
113+ lease->name = strdup(hostname);
114+ if (daemon->domain_suffix) {
115+ asprintf(&lease->fqdn, "%s.%s", hostname, daemon->domain_suffix);
116+ }
117+ lease->expires = 0;
45e65f1d
MT
118+ lease->next = NULL;
119+
120+ return lease;
121+}
122+
123+static void dhcp_lease_free(struct isc_dhcp_lease* lease) {
124+ if (!lease)
125+ return;
126+
127+ if (lease->name)
128+ free(lease->name);
129+ if (lease->fqdn)
130+ free(lease->fqdn);
131+ free(lease);
132+}
133+
134+static int next_token(char* token, int buffsize, FILE* fp) {
135+ int c, count = 0;
136+ char* cp = token;
137+
138+ while ((c = getc(fp)) != EOF) {
139+ if (c == '#') {
140+ do {
141+ c = getc(fp);
142+ } while (c != '\n' && c != EOF);
143+ }
144+
145+ if (c == ' ' || c == '\t' || c == '\n' || c == ';') {
146+ if (count)
147+ break;
148+ } else if ((c != '"') && (count < buffsize - 1)) {
149+ *cp++ = c;
150+ count++;
151+ }
152+ }
153+
154+ *cp = 0;
155+ return count ? 1 : 0;
156+}
157+
3da4cc02
MT
158+static long get_utc_offset() {
159+ time_t t = time(NULL);
160+ struct tm* time_struct = localtime(&t);
161+
162+ return time_struct->tm_gmtoff;
163+}
164+
45e65f1d
MT
165+static time_t parse_lease_time(const char* token_date, const char* token_time) {
166+ time_t time = (time_t)(-1);
167+ struct tm lease_time;
168+
45e65f1d 169+ if (sscanf(token_date, "%d/%d/%d", &lease_time.tm_year, &lease_time.tm_mon, &lease_time.tm_mday) == 3) {
3da4cc02
MT
170+ lease_time.tm_year -= 1900;
171+ lease_time.tm_mon -= 1;
172+
45e65f1d 173+ if (sscanf(token_time, "%d:%d:%d", &lease_time.tm_hour, &lease_time.tm_min, &lease_time.tm_sec) == 3) {
3da4cc02 174+ time = mktime(&lease_time) + get_utc_offset();
45e65f1d
MT
175+ }
176+ }
177+
178+ return time;
179+}
180+
3da4cc02
MT
181+static struct isc_dhcp_lease* find_lease(const char* hostname, struct isc_dhcp_lease* leases) {
182+ struct isc_dhcp_lease* lease = leases;
183+
184+ while (lease) {
185+ if (strcmp(hostname, lease->name) == 0) {
186+ return lease;
187+ }
188+ lease = lease->next;
189+ }
190+
191+ return NULL;
192+}
193+
45e65f1d
MT
194+static off_t lease_file_size = (off_t)0;
195+static ino_t lease_file_inode = (ino_t)0;
196+
197+void load_dhcp(time_t now) {
198+ struct isc_dhcp_lease* leases = NULL;
199+
200+ struct stat statbuf;
201+ if (stat(daemon->lease_file, &statbuf) == -1) {
202+ return;
203+ }
204+
205+ /* Do nothing if the lease file has not changed. */
206+ if ((statbuf.st_size <= lease_file_size) && (statbuf.st_ino == lease_file_inode))
207+ return;
208+
209+ lease_file_size = statbuf.st_size;
210+ lease_file_inode = statbuf.st_ino;
211+
212+ FILE* fp = fopen(daemon->lease_file, "r");
213+ if (!fp) {
214+ my_syslog(LOG_ERR, _("failed to load %s:%s"), daemon->lease_file, strerror(errno));
215+ return;
216+ }
217+
218+ my_syslog(LOG_INFO, _("reading %s"), daemon->lease_file);
219+
220+ char* hostname = daemon->namebuff;
221+ struct in_addr host_address;
222+ time_t time_starts = -1;
223+ time_t time_ends = -1;
224+ int nomem;
225+
226+ char token[MAXTOK];
227+ while ((next_token(token, MAXTOK, fp))) {
228+ if (strcmp(token, "lease") == 0) {
229+ hostname[0] = '\0';
230+
231+ if (next_token(token, MAXTOK, fp) && ((host_address.s_addr = inet_addr(token)) != (in_addr_t)-1)) {
232+ if (next_token(token, MAXTOK, fp) && *token == '{') {
233+ while (next_token(token, MAXTOK, fp) && *token != '}') {
234+ if ((strcmp(token, "client-hostname") == 0) || (strcmp(token, "hostname") == 0)) {
235+ if (next_token(hostname, MAXDNAME, fp)) {
236+ if (!canonicalise(hostname, &nomem)) {
237+ *hostname = 0;
238+ my_syslog(LOG_ERR, _("bad name in %s"), daemon->lease_file);
239+ }
240+ }
241+ } else if ((strcmp(token, "starts") == 0) || (strcmp(token, "ends") == 0)) {
242+ char token_date[MAXTOK];
243+ char token_time[MAXTOK];
244+
245+ int is_starts = strcmp(token, "starts") == 0;
246+
247+ // Throw away the weekday and parse the date.
248+ if (next_token(token, MAXTOK, fp) && next_token(token_date, MAXTOK, fp) && next_token(token_time, MAXTOK, fp)) {
249+ time_t time = parse_lease_time(token_date, token_time);
250+
251+ if (is_starts)
252+ time_starts = time;
253+ else
254+ time_ends = time;
255+ }
256+ }
257+ }
258+
259+ if (!*hostname)
260+ continue;
261+
262+ if ((time_starts == -1) || (time_ends == -1))
263+ continue;
264+
265+ if (difftime(now, time_ends) > 0)
266+ continue;
267+
268+ char* dot = strchr(hostname, '.');
269+ if (dot) {
270+ if (!daemon->domain_suffix || hostname_isequal(dot + 1, daemon->domain_suffix)) {
271+ my_syslog(LOG_WARNING,
272+ _("Ignoring DHCP lease for %s because it has an illegal domain part"),
273+ hostname);
274+ continue;
275+ }
276+ *dot = 0;
277+ }
278+
3da4cc02
MT
279+ // Search for an existing lease in the list
280+ // with the given host name and update the data
281+ // if needed.
282+ struct isc_dhcp_lease* lease = find_lease(hostname, leases);
45e65f1d 283+
3da4cc02
MT
284+ // If no lease already exists, we create a new one
285+ // and append it to the list.
286+ if (!lease) {
287+ lease = dhcp_lease_new(hostname);
45e65f1d 288+
3da4cc02 289+ lease->next = leases;
45e65f1d 290+ leases = lease;
3da4cc02
MT
291+ }
292+
293+ // Only update more recent leases.
294+ if (lease->expires > time_ends)
295+ continue;
296+
297+ lease->addr = host_address;
298+ lease->expires = time_ends;
45e65f1d
MT
299+ }
300+ }
301+ }
302+ }
303+
304+ fclose(fp);
305+
306+ // Drop all entries.
307+ cache_unhash_dhcp();
308+
309+ while (leases) {
310+ struct isc_dhcp_lease *lease = leases;
311+ leases = lease->next;
312+
313+ if (lease->fqdn) {
314+ cache_add_dhcp_entry(lease->fqdn, AF_INET, (struct all_addr*)&lease->addr.s_addr, lease->expires);
315+ }
316+
317+ if (lease->name) {
318+ cache_add_dhcp_entry(lease->name, AF_INET, (struct all_addr*)&lease->addr.s_addr, lease->expires);
319+ }
320+
321+ // Cleanup
322+ dhcp_lease_free(lease);
323+ }
324+}
325+
326+#endif
fbcc3cb7
MF
327--- a/src/option.c Wed Dec 16 19:24:12 2015
328+++ b/src/option.c Wed Dec 16 19:42:48 2015
a6cd8b9b 329@@ -1769,7 +1769,7 @@
45e65f1d
MT
330 ret_err(_("bad MX target"));
331 break;
332
333-#ifdef HAVE_DHCP
334+#if (defined HAVE_DHCP) || (defined HAVE_ISC_READER)
335 case 'l': /* --dhcp-leasefile */
336 daemon->lease_file = opt_string_alloc(arg);
337 break;
fbcc3cb7
MF
338--- a/Makefile Wed Dec 16 19:24:12 2015
339+++ b/Makefile Wed Dec 16 19:28:45 2015
340@@ -74,7 +74,7 @@
341 helper.o tftp.o log.o conntrack.o dhcp6.o rfc3315.o \
342 dhcp-common.o outpacket.o radv.o slaac.o auth.o ipset.o \
343 domain.o dnssec.o blockdata.o tables.o loop.o inotify.o \
a6cd8b9b
MF
344- poll.o rrfilter.o edns0.o arp.o
345+ poll.o rrfilter.o edns0.o arp.o isc.o
fbcc3cb7
MF
346
347 hdrs = dnsmasq.h config.h dhcp-protocol.h dhcp6-protocol.h \
348 dns-protocol.h radv-protocol.h ip6addr.h