]> git.refcnt.org Git - distdns.git/blob - server.cgi
Indicate client/server for error messages
[distdns.git] / server.cgi
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2013 Michel Ketterle, Steven Schubiger
4 #
5 # This file is part of distdns.
6 #
7 # distdns is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # distdns is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with distdns. If not, see <http://www.gnu.org/licenses/>.
19
20 use strict;
21 use warnings;
22 use lib qw(lib);
23
24 use CGI ();
25 use Config::Tiny ();
26 use Fcntl ':flock';
27 use File::Spec::Functions qw(catfile rel2abs);
28 use FindBin qw($Bin);
29 use JSON qw(decode_json encode_json);
30
31 my $VERSION = '0.05';
32
33 my $conf_file = catfile($Bin, 'server.conf');
34
35 my $query = CGI->new;
36
37 my @params = qw(netz pc name debug init session);
38 my %params;
39
40 foreach my $param (@params) {
41 $params{$param} = $query->param($param);
42 }
43 $params{ip} = $query->remote_addr;
44
45 if ($params{debug}) {
46 $SIG{__DIE__} = sub
47 {
48 print $query->header('application/json');
49 print encode_json({ entries => [], error => $_[0] });
50 exit;
51 };
52 }
53
54 my $config = Config::Tiny->new;
55 $config = Config::Tiny->read($conf_file);
56
57 my $section = 'path';
58
59 die "Section $section missing in $conf_file\n" unless exists $config->{$section};
60
61 my @options = qw(json_file session_file);
62
63 my %options;
64 @options{@options} = @{$config->{$section}}{@options};
65
66 foreach my $option (@options) {
67 die "Option $option not set in $conf_file\n" unless defined $options{$option} && length $options{$option};
68 }
69
70 my ($json_file, $session_file) = map rel2abs($options{$_}, $Bin), @options;
71
72 if ($params{init}) {
73 die "Delete $session_file first\n" if -e $session_file;
74
75 open(my $fh, '>', $session_file) or die "Cannot open $session_file for writing: $!\n";
76 print {$fh} "$params{session}\n";
77 close($fh);
78 }
79 else {
80 open(my $fh, '<', $session_file) or die "Cannot open $session_file for reading: $!\nPerhaps try running --init\n";
81 my $session = do { local $/; <$fh> };
82 chomp $session;
83 close($fh);
84
85 die "Session ID mismatch\n" unless $params{session} eq $session;
86 }
87
88 my @missing_params = grep { not defined $params{$_} && length $params{$_} } @params;
89 if (@missing_params) {
90 my $missing_params = join ', ', map "'$_'", @missing_params;
91 die "Incomplete query: param(s) $missing_params missing or not defined\n";
92 }
93
94 my %access;
95 my $access_file = "$params{netz}.conf";
96
97 if (-e $access_file) {
98 open(my $fh, '<', $access_file) or die "Cannot open $access_file for reading: $!\n";
99 while (my $line = <$fh>) {
100 chomp $line;
101 my ($name, $pc) = split /\s*,\s*/, $line;
102 push @{$access{$name}}, $pc;
103 }
104 close($fh);
105 }
106 else {
107 die "Access file $access_file does not exist\n";
108 }
109
110 if (exists $access{$params{name}} && grep /^$params{pc}$/i, @{$access{$params{name}}}) {
111 open(my $fh, '+<', $json_file) or die "Cannot open $json_file for read/write: $!\n";
112 flock($fh, LOCK_EX) or die "Cannot lock $json_file: $!\n";
113
114 my $json = do { local $/; <$fh> };
115
116 my $data = defined $json && length $json ? decode_json($json) : [];
117
118 for (my $i = 0; $i < @$data; $i++) {
119 if ($params{netz} eq $data->[$i]->{netz}
120 && $params{pc} eq $data->[$i]->{pc}
121 && $params{name} eq $data->[$i]->{name}) {
122 splice @$data, $i--, 1;
123 }
124 }
125 push @$data, { map { $_ => $params{$_} } qw(netz pc name ip) };
126
127 seek($fh, 0, 0) or die "Cannot seek to start of $json_file: $!\n";
128 truncate($fh, 0) or die "Cannot truncate $json_file: $!\n";
129
130 print {$fh} encode_json($data);
131
132 close($fh);
133
134 my @data = grep $_->{netz} eq $params{netz}, @$data;
135
136 print $query->header('application/json');
137 print encode_json({ entries => \@data, error => undef });
138 }
139 else {
140 die "Access not permitted\n";
141 }