]>
git.refcnt.org Git - distdns.git/blob - server.cgi
3 # Copyright (c) 2013 Michel Ketterle, Steven Schubiger
5 # This file is part of distdns.
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.
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.
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/>.
27 use File
::Spec
::Functions
qw(catfile rel2abs);
29 use JSON qw(decode_json encode_json);
33 my $conf_file = catfile
($Bin, 'server.conf');
37 my @params = qw(netz pc name debug init list session);
40 foreach my $param (@params) {
41 $params{$param} = $query->param($param);
43 $params{ip
} = $query->remote_addr;
48 print $query->header('application/json');
49 print encode_json
({ entries
=> [], error
=> $_[0] });
54 my @missing_params = grep { not defined $params{$_} && length $params{$_} } @params;
55 if (@missing_params) {
56 my $missing_params = join ', ', map "'$_'", @missing_params;
57 die "Incomplete query: param(s) $missing_params missing or not defined\n";
60 my $config = Config
::Tiny
->new;
61 $config = Config
::Tiny
->read($conf_file);
65 die "Section '$section' missing in $conf_file\n" unless exists $config->{$section};
67 my @options = qw(json_file session_file);
70 @options{@options} = @
{$config->{$section}}{@options};
72 foreach my $option (@options) {
73 die "Option '$option' not set in $conf_file\n" unless defined $options{$option} && length $options{$option};
76 my ($json_file, $session_file) = map rel2abs
($options{$_}, $Bin), @options;
79 die "Delete $session_file first\n" if -e
$session_file;
81 open(my $fh, '>', $session_file) or die "Cannot open $session_file for writing: $!\n";
82 print {$fh} "$params{session}\n";
85 print $query->header('application/json');
86 print encode_json
({ entries
=> [], error
=> undef });
90 open(my $fh, '<', $session_file) or die "Cannot open $session_file for reading: $!\nPerhaps try running --init\n";
91 chomp(my $session = <$fh>);
94 die "Session ID mismatch\n" unless $params{session
} eq $session;
98 my $access_file = "$params{netz}.conf";
100 if (-e
$access_file) {
101 open(my $fh, '<', $access_file) or die "Cannot open $access_file for reading: $!\n";
102 while (my $line = <$fh>) {
104 my ($name, $pc) = split /\s*,\s*/, $line;
105 push @
{$access{$name}}, $pc;
110 die "Access file $access_file does not exist\n";
113 if (exists $access{$params{name
}} && grep /^$params{pc}$/i, @
{$access{$params{name
}}}) {
114 open(my $fh, '+<', $json_file) or die "Cannot open $json_file for read/write: $!\n";
115 flock($fh, LOCK_EX
) or die "Cannot lock $json_file: $!\n";
117 my $json = do { local $/; <$fh> };
119 my $data = defined $json && length $json ? decode_json
($json) : [];
124 print $query->header('application/json');
125 print encode_json
({ entries
=> $data, error
=> undef });
128 for (my $i = 0; $i < @
$data; $i++) {
129 if ($params{netz
} eq $data->[$i]->{netz
}
130 && $params{pc
} eq $data->[$i]->{pc
}
131 && $params{name
} eq $data->[$i]->{name
}) {
132 splice @
$data, $i--, 1;
135 push @
$data, { map { $_ => $params{$_} } qw(netz pc name ip) };
136 $data->[-1]->{time} = time;
138 seek($fh, 0, 0) or die "Cannot seek to start of $json_file: $!\n";
139 truncate($fh, 0) or die "Cannot truncate $json_file: $!\n";
141 print {$fh} encode_json
($data);
145 my @data = grep $_->{netz
} eq $params{netz
}, @
$data;
147 print $query->header('application/json');
148 print encode_json
({ entries
=> \
@data, error
=> undef });
152 die "Access not permitted\n";