• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/perl
2
3# dnslist - Read state file from dnsmasq and create a nice web page to display
4#           a list of DHCP clients.
5#
6# Copyright (C) 2004  Thomas Tuttle
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program*; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21#
22# * The license is in fact included at the end of this file, and can
23#   either be viewed by reading everything after "__DATA__" or by
24#   running dnslist with the '-l' option.
25#
26# Version: 0.2
27# Author:  Thomas Tuttle
28# Email:   dnslist.20.thinkinginbinary@spamgourmet.org
29# License: GNU General Public License, version 2.0
30#
31# v. 0.0: Too ugly to publish, thrown out.
32#
33# v. 0.1: First rewrite.
34#         Added master host list so offline hosts can still be displayed.
35#         Fixed modification detection (a newer modification time is lower.)
36#
37# v. 0.2: Fixed Client ID = "*" => "None"
38#         Fixed HTML entities (a client ID of ????<? screwed it up)
39#         Fixed command-line argument processing (apparently, "shift @ARGV" !=
40#             "$_ = shift @ARGV"...)
41#         Added license information.
42
43use Template;
44
45# Location of state file.  (This is the dnsmasq default.)
46# Change with -s <file>
47my $dnsmasq_state_file = '/var/lib/misc/dnsmasq.leases';
48# Location of template.  (Assumed to be in current directory.)
49# Change with -t <file>
50my $html_template_file = 'dnslist.tt2';
51# File to write HTML page to.  (This is where Slackware puts WWW pages.  It may
52# be different on other systems.  Make sure the permissions are set correctly
53# for it.)
54my $html_output_file = '/var/www/htdocs/dhcp.html';
55# Time to wait after each page update.  (The state file is checked for changes
56# before each update but is not read in each time, in case it is very big.  The
57# page is rewritten just so the "(updated __/__ __:__:__)" text changes ;-)
58my $wait_time = 2;
59
60# Read command-line arguments.
61while ($_ = shift @ARGV) {
62	if (/-s/) { $dnsmasq_state_file = shift; next; }
63	if (/-t/) { $html_template_file = shift; next; }
64	if (/-o/) { $html_output_file = shift;   next; }
65	if (/-d/) { $wait_time = shift;          next; }
66	if (/-l/) { show_license();              exit; }
67	die "usage: dnslist [-s state_file] [-t template_file] [-o output_file] [-d delay_time]\n";
68}
69
70# Master list of clients, offline and online.
71my $list = {};
72# Sorted host list.  (It's actually sorted by IP--the sub &byip() compares two
73# IP addresses, octet by octet, and figures out which is higher.)
74my @hosts = ();
75# Last time the state file was changed.
76my $last_state_change;
77
78# Check for a change to the state file.
79sub check_state {
80	if (defined $last_state_change) {
81		if (-M $dnsmasq_state_file < $last_state_change) {
82			print "check_state: state file has been changed.\n";
83			$last_state_change = -M $dnsmasq_state_file;
84			return 1;
85		} else {
86			return 0;
87		}
88	} else {
89		# Last change undefined, so we are running for the first time.
90		print "check_state: reading state file at startup.\n";
91		read_state();
92		$last_state_change = -M $dnsmasq_state_file;
93		return 1;
94	}
95}
96
97# Read data in state file.
98sub read_state {
99	my $old;
100	my $new;
101	# Open file.
102	unless (open STATE, $dnsmasq_state_file) {
103		warn "read_state: can't open $dnsmasq_state_file!\n";
104		return 0;
105	}
106	# Mark all hosts as offline, saving old state.
107	foreach $ether (keys %{$list}) {
108		$list->{$ether}->{'old_online'} = $list->{$ether}->{'online'};
109		$list->{$ether}->{'online'} = 0;
110	}
111	# Read hosts.
112	while (<STATE>) {
113		chomp;
114		@host{qw/raw_lease ether_addr ip_addr hostname raw_client_id/} = split /\s+/;
115		$ether = $host{ether_addr};
116		# Mark each online host as online.
117		$list->{$ether}->{'online'} = 1;
118		# Copy data to master list.
119		foreach $key (keys %host) {
120			$list->{$ether}->{$key} = $host{$key};
121		}
122	}
123	close STATE;
124	# Handle changes in offline/online state.  (The sub &do_host() handles
125	# all of the extra stuff to do with a host's data once it is read.
126	foreach $ether (keys %{$list}) {
127		$old = $list->{$ether}->{'old_online'};
128		$new = $list->{$ether}->{'online'};
129		if (not $old) {
130			if (not $new) {
131				do_host($ether, 'offline');
132			} else {
133				do_host($ether, 'join');
134			}
135		} else {
136			if (not $new) {
137				do_host($ether, 'leave');
138			} else {
139				do_host($ether, 'online');
140			}
141		}
142	}
143	# Sort hosts by IP ;-)
144	@hosts = sort byip values %{$list};
145	# Copy sorted list to template data store.
146	$data->{'hosts'} = [ @hosts ];
147}
148
149# Do stuff per host.
150sub do_host {
151	my ($ether, $status) = @_;
152
153	# Find textual representation of DHCP client ID.
154	if ($list->{$ether}->{'raw_client_id'} eq '*') {
155		$list->{$ether}->{'text_client_id'} = 'None';
156	} else {
157		my $text = "";
158		foreach $char (split /:/, $list->{$ether}->{'raw_client_id'}) {
159			$char = pack('H2', $char);
160			if (ord($char) >= 32 and ord($char) <= 127) {
161				$text .= $char;
162			} else {
163				$text .= "?";
164			}
165		}
166		$list->{$ether}->{'text_client_id'} = $text;
167	}
168
169	# Convert lease expiration date/time to text.
170	if ($list->{$ether}->{'raw_lease'} == 0) {
171		$list->{$ether}->{'text_lease'} = 'Never';
172	} else {
173		$list->{$ether}->{'text_lease'} = nice_time($list->{$ether}->{'raw_lease'});
174	}
175
176	if ($status eq 'offline') {
177		# Nothing to do.
178	} elsif ($status eq 'online') {
179		# Nothing to do.
180	} elsif ($status eq 'join') {
181		# Update times for joining host.
182		print "do_host: $ether joined the network.\n";
183		$list->{$ether}->{'join_time'} = time;
184		$list->{$ether}->{'since'} = nice_time(time);
185	} elsif ($status eq 'leave') {
186		# Update times for leaving host.
187		print "do_host: $ether left the network.\n";
188		$list->{$ether}->{'leave_time'} = time;
189		$list->{$ether}->{'since'} = nice_time(time);
190	}
191
192}
193
194# Convert time to a string representation.
195sub nice_time {
196	my $time = shift;
197	my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $dst) = localtime($time);
198	$sec = pad($sec, '0', 2);
199	$min = pad($min, '0', 2);
200	$hour = pad($hour, '0', 2);
201	$mon = pad($mon, '0', 2);
202	$mday = pad($mday, '0', 2);
203	return "$mon/$mday $hour:$min:$sec";
204}
205
206# Pad string to a certain length by repeatedly prepending another string.
207sub pad {
208	my ($text, $pad, $length) = @_;
209	while (length($text) < $length) {
210		$text = "$pad$text";
211	}
212	return $text;
213}
214
215# Compare two IP addresses.  (Uses $a and $b from sort.)
216sub byip {
217	# Split into octets.
218	my @a = split /\./, $a->{ip_addr};
219	my @b = split /\./, $b->{ip_addr};
220	# Compare octets.
221	foreach $n (0..3) {
222		return $a[$n] <=> $b[$n] if ($a[$n] != $b[$n]);
223	}
224	# If we get here there is no difference.
225	return 0;
226}
227
228# Output HTML file.
229sub write_output {
230	# Create new template object.
231	my $template = Template->new(
232		{
233			ABSOLUTE => 1, # /var/www/... is an absolute path
234			OUTPUT => $html_output_file # put it here, not STDOUT
235		}
236	);
237	$data->{'updated'} = nice_time(time); # add "(updated ...)" to file
238	unless ($template->process($html_template_file, $data)) { # do it
239		warn "write_output: Template Toolkit error: " . $template->error() . "\n";
240		return 0;
241	}
242	print "write_output: page updated.\n";
243	return 1;
244}
245
246sub show_license {
247	while (<DATA>) {
248		print;
249		$line++;
250		if ($line == 24) { <>; $line = 1; }
251	}
252}
253
254# Main loop.
255while (1) {
256	# Check for state change.
257	if (check_state()) {
258		read_state();
259		sleep 1; # Sleep for a second just so we don't wear anything
260		         # out.  (By not sleeping the whole time after a change
261			 # we can detect rapid changes more easily--like if 300
262			 # hosts all come back online, they show up quicker.)
263	} else {
264		sleep $wait_time; # Take a nap.
265	}
266	write_output(); # Write the file anyway.
267}
268__DATA__
269		    GNU GENERAL PUBLIC LICENSE
270		       Version 2, June 1991
271
272 Copyright (C) 1989, 1991 Free Software Foundation, Inc.
273                       59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
274 Everyone is permitted to copy and distribute verbatim copies
275 of this license document, but changing it is not allowed.
276
277			    Preamble
278
279  The licenses for most software are designed to take away your
280freedom to share and change it.  By contrast, the GNU General Public
281License is intended to guarantee your freedom to share and change free
282software--to make sure the software is free for all its users.  This
283General Public License applies to most of the Free Software
284Foundation's software and to any other program whose authors commit to
285using it.  (Some other Free Software Foundation software is covered by
286the GNU Library General Public License instead.)  You can apply it to
287your programs, too.
288
289  When we speak of free software, we are referring to freedom, not
290price.  Our General Public Licenses are designed to make sure that you
291have the freedom to distribute copies of free software (and charge for
292this service if you wish), that you receive source code or can get it
293if you want it, that you can change the software or use pieces of it
294in new free programs; and that you know you can do these things.
295
296  To protect your rights, we need to make restrictions that forbid
297anyone to deny you these rights or to ask you to surrender the rights.
298These restrictions translate to certain responsibilities for you if you
299distribute copies of the software, or if you modify it.
300
301  For example, if you distribute copies of such a program, whether
302gratis or for a fee, you must give the recipients all the rights that
303you have.  You must make sure that they, too, receive or can get the
304source code.  And you must show them these terms so they know their
305rights.
306
307  We protect your rights with two steps: (1) copyright the software, and
308(2) offer you this license which gives you legal permission to copy,
309distribute and/or modify the software.
310
311  Also, for each author's protection and ours, we want to make certain
312that everyone understands that there is no warranty for this free
313software.  If the software is modified by someone else and passed on, we
314want its recipients to know that what they have is not the original, so
315that any problems introduced by others will not reflect on the original
316authors' reputations.
317
318  Finally, any free program is threatened constantly by software
319patents.  We wish to avoid the danger that redistributors of a free
320program will individually obtain patent licenses, in effect making the
321program proprietary.  To prevent this, we have made it clear that any
322patent must be licensed for everyone's free use or not licensed at all.
323
324  The precise terms and conditions for copying, distribution and
325modification follow.
326
327		    GNU GENERAL PUBLIC LICENSE
328   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
329
330  0. This License applies to any program or other work which contains
331a notice placed by the copyright holder saying it may be distributed
332under the terms of this General Public License.  The "Program", below,
333refers to any such program or work, and a "work based on the Program"
334means either the Program or any derivative work under copyright law:
335that is to say, a work containing the Program or a portion of it,
336either verbatim or with modifications and/or translated into another
337language.  (Hereinafter, translation is included without limitation in
338the term "modification".)  Each licensee is addressed as "you".
339
340Activities other than copying, distribution and modification are not
341covered by this License; they are outside its scope.  The act of
342running the Program is not restricted, and the output from the Program
343is covered only if its contents constitute a work based on the
344Program (independent of having been made by running the Program).
345Whether that is true depends on what the Program does.
346
347  1. You may copy and distribute verbatim copies of the Program's
348source code as you receive it, in any medium, provided that you
349conspicuously and appropriately publish on each copy an appropriate
350copyright notice and disclaimer of warranty; keep intact all the
351notices that refer to this License and to the absence of any warranty;
352and give any other recipients of the Program a copy of this License
353along with the Program.
354
355You may charge a fee for the physical act of transferring a copy, and
356you may at your option offer warranty protection in exchange for a fee.
357
358  2. You may modify your copy or copies of the Program or any portion
359of it, thus forming a work based on the Program, and copy and
360distribute such modifications or work under the terms of Section 1
361above, provided that you also meet all of these conditions:
362
363    a) You must cause the modified files to carry prominent notices
364    stating that you changed the files and the date of any change.
365
366    b) You must cause any work that you distribute or publish, that in
367    whole or in part contains or is derived from the Program or any
368    part thereof, to be licensed as a whole at no charge to all third
369    parties under the terms of this License.
370
371    c) If the modified program normally reads commands interactively
372    when run, you must cause it, when started running for such
373    interactive use in the most ordinary way, to print or display an
374    announcement including an appropriate copyright notice and a
375    notice that there is no warranty (or else, saying that you provide
376    a warranty) and that users may redistribute the program under
377    these conditions, and telling the user how to view a copy of this
378    License.  (Exception: if the Program itself is interactive but
379    does not normally print such an announcement, your work based on
380    the Program is not required to print an announcement.)
381
382These requirements apply to the modified work as a whole.  If
383identifiable sections of that work are not derived from the Program,
384and can be reasonably considered independent and separate works in
385themselves, then this License, and its terms, do not apply to those
386sections when you distribute them as separate works.  But when you
387distribute the same sections as part of a whole which is a work based
388on the Program, the distribution of the whole must be on the terms of
389this License, whose permissions for other licensees extend to the
390entire whole, and thus to each and every part regardless of who wrote it.
391
392Thus, it is not the intent of this section to claim rights or contest
393your rights to work written entirely by you; rather, the intent is to
394exercise the right to control the distribution of derivative or
395collective works based on the Program.
396
397In addition, mere aggregation of another work not based on the Program
398with the Program (or with a work based on the Program) on a volume of
399a storage or distribution medium does not bring the other work under
400the scope of this License.
401
402  3. You may copy and distribute the Program (or a work based on it,
403under Section 2) in object code or executable form under the terms of
404Sections 1 and 2 above provided that you also do one of the following:
405
406    a) Accompany it with the complete corresponding machine-readable
407    source code, which must be distributed under the terms of Sections
408    1 and 2 above on a medium customarily used for software interchange; or,
409
410    b) Accompany it with a written offer, valid for at least three
411    years, to give any third party, for a charge no more than your
412    cost of physically performing source distribution, a complete
413    machine-readable copy of the corresponding source code, to be
414    distributed under the terms of Sections 1 and 2 above on a medium
415    customarily used for software interchange; or,
416
417    c) Accompany it with the information you received as to the offer
418    to distribute corresponding source code.  (This alternative is
419    allowed only for noncommercial distribution and only if you
420    received the program in object code or executable form with such
421    an offer, in accord with Subsection b above.)
422
423The source code for a work means the preferred form of the work for
424making modifications to it.  For an executable work, complete source
425code means all the source code for all modules it contains, plus any
426associated interface definition files, plus the scripts used to
427control compilation and installation of the executable.  However, as a
428special exception, the source code distributed need not include
429anything that is normally distributed (in either source or binary
430form) with the major components (compiler, kernel, and so on) of the
431operating system on which the executable runs, unless that component
432itself accompanies the executable.
433
434If distribution of executable or object code is made by offering
435access to copy from a designated place, then offering equivalent
436access to copy the source code from the same place counts as
437distribution of the source code, even though third parties are not
438compelled to copy the source along with the object code.
439
440  4. You may not copy, modify, sublicense, or distribute the Program
441except as expressly provided under this License.  Any attempt
442otherwise to copy, modify, sublicense or distribute the Program is
443void, and will automatically terminate your rights under this License.
444However, parties who have received copies, or rights, from you under
445this License will not have their licenses terminated so long as such
446parties remain in full compliance.
447
448  5. You are not required to accept this License, since you have not
449signed it.  However, nothing else grants you permission to modify or
450distribute the Program or its derivative works.  These actions are
451prohibited by law if you do not accept this License.  Therefore, by
452modifying or distributing the Program (or any work based on the
453Program), you indicate your acceptance of this License to do so, and
454all its terms and conditions for copying, distributing or modifying
455the Program or works based on it.
456
457  6. Each time you redistribute the Program (or any work based on the
458Program), the recipient automatically receives a license from the
459original licensor to copy, distribute or modify the Program subject to
460these terms and conditions.  You may not impose any further
461restrictions on the recipients' exercise of the rights granted herein.
462You are not responsible for enforcing compliance by third parties to
463this License.
464
465  7. If, as a consequence of a court judgment or allegation of patent
466infringement or for any other reason (not limited to patent issues),
467conditions are imposed on you (whether by court order, agreement or
468otherwise) that contradict the conditions of this License, they do not
469excuse you from the conditions of this License.  If you cannot
470distribute so as to satisfy simultaneously your obligations under this
471License and any other pertinent obligations, then as a consequence you
472may not distribute the Program at all.  For example, if a patent
473license would not permit royalty-free redistribution of the Program by
474all those who receive copies directly or indirectly through you, then
475the only way you could satisfy both it and this License would be to
476refrain entirely from distribution of the Program.
477
478If any portion of this section is held invalid or unenforceable under
479any particular circumstance, the balance of the section is intended to
480apply and the section as a whole is intended to apply in other
481circumstances.
482
483It is not the purpose of this section to induce you to infringe any
484patents or other property right claims or to contest validity of any
485such claims; this section has the sole purpose of protecting the
486integrity of the free software distribution system, which is
487implemented by public license practices.  Many people have made
488generous contributions to the wide range of software distributed
489through that system in reliance on consistent application of that
490system; it is up to the author/donor to decide if he or she is willing
491to distribute software through any other system and a licensee cannot
492impose that choice.
493
494This section is intended to make thoroughly clear what is believed to
495be a consequence of the rest of this License.
496
497  8. If the distribution and/or use of the Program is restricted in
498certain countries either by patents or by copyrighted interfaces, the
499original copyright holder who places the Program under this License
500may add an explicit geographical distribution limitation excluding
501those countries, so that distribution is permitted only in or among
502countries not thus excluded.  In such case, this License incorporates
503the limitation as if written in the body of this License.
504
505  9. The Free Software Foundation may publish revised and/or new versions
506of the General Public License from time to time.  Such new versions will
507be similar in spirit to the present version, but may differ in detail to
508address new problems or concerns.
509
510Each version is given a distinguishing version number.  If the Program
511specifies a version number of this License which applies to it and "any
512later version", you have the option of following the terms and conditions
513either of that version or of any later version published by the Free
514Software Foundation.  If the Program does not specify a version number of
515this License, you may choose any version ever published by the Free Software
516Foundation.
517
518  10. If you wish to incorporate parts of the Program into other free
519programs whose distribution conditions are different, write to the author
520to ask for permission.  For software which is copyrighted by the Free
521Software Foundation, write to the Free Software Foundation; we sometimes
522make exceptions for this.  Our decision will be guided by the two goals
523of preserving the free status of all derivatives of our free software and
524of promoting the sharing and reuse of software generally.
525
526			    NO WARRANTY
527
528  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
529FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
530OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
531PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
532OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
533MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
534TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
535PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
536REPAIR OR CORRECTION.
537
538  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
539WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
540REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
541INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
542OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
543TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
544YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
545PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
546POSSIBILITY OF SUCH DAMAGES.
547
548		     END OF TERMS AND CONDITIONS
549
550	    How to Apply These Terms to Your New Programs
551
552  If you develop a new program, and you want it to be of the greatest
553possible use to the public, the best way to achieve this is to make it
554free software which everyone can redistribute and change under these terms.
555
556  To do so, attach the following notices to the program.  It is safest
557to attach them to the start of each source file to most effectively
558convey the exclusion of warranty; and each file should have at least
559the "copyright" line and a pointer to where the full notice is found.
560
561    <one line to give the program's name and a brief idea of what it does.>
562    Copyright (C) <year>  <name of author>
563
564    This program is free software; you can redistribute it and/or modify
565    it under the terms of the GNU General Public License as published by
566    the Free Software Foundation; either version 2 of the License, or
567    (at your option) any later version.
568
569    This program is distributed in the hope that it will be useful,
570    but WITHOUT ANY WARRANTY; without even the implied warranty of
571    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
572    GNU General Public License for more details.
573
574    You should have received a copy of the GNU General Public License
575    along with this program; if not, write to the Free Software
576    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
577
578
579Also add information on how to contact you by electronic and paper mail.
580
581If the program is interactive, make it output a short notice like this
582when it starts in an interactive mode:
583
584    Gnomovision version 69, Copyright (C) year name of author
585    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
586    This is free software, and you are welcome to redistribute it
587    under certain conditions; type `show c' for details.
588
589The hypothetical commands `show w' and `show c' should show the appropriate
590parts of the General Public License.  Of course, the commands you use may
591be called something other than `show w' and `show c'; they could even be
592mouse-clicks or menu items--whatever suits your program.
593
594You should also get your employer (if you work as a programmer) or your
595school, if any, to sign a "copyright disclaimer" for the program, if
596necessary.  Here is a sample; alter the names:
597
598  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
599  `Gnomovision' (which makes passes at compilers) written by James Hacker.
600
601  <signature of Ty Coon>, 1 April 1989
602  Ty Coon, President of Vice
603
604This General Public License does not permit incorporating your program into
605proprietary programs.  If your program is a subroutine library, you may
606consider it more useful to permit linking proprietary applications with the
607library.  If this is what you want to do, use the GNU Library General
608Public License instead of this License.
609