[119] | 1 | #!/usr/bin/perl
|
---|
[200] | 2 | # $Id$
|
---|
[119] | 3 | # XMLRPC interface to manipulate most DNS DB entities
|
---|
[200] | 4 | # Copyright (C) 2011 - Kris Deugau <kdeugau@deepnet.cx>
|
---|
[119] | 5 |
|
---|
| 6 | use strict;
|
---|
| 7 | use warnings;
|
---|
| 8 | use DNSDB; # note we're not importing subs; this lets us (ab)use the same sub names here for convenience
|
---|
[121] | 9 | use Data::Dumper;
|
---|
[119] | 10 |
|
---|
| 11 | #use Frontier::RPC2;
|
---|
| 12 | use Frontier::Responder;
|
---|
| 13 |
|
---|
| 14 | ## We need to handle a couple of things globally, rather than pasting the same bit into *every* sub.
|
---|
| 15 | ## So, let's subclass Frontier::RPC2 + Frontier::Responder, so we can override the single sub in each
|
---|
| 16 | ## that needs kicking
|
---|
| 17 | #### hmm. put this in a separate file?
|
---|
| 18 | #package DNSDB::RPC;
|
---|
| 19 | #our @ISA = ("Frontier::RPC2", "Frontier::Responder");
|
---|
| 20 | #package main;
|
---|
| 21 |
|
---|
[191] | 22 | loadConfig();
|
---|
| 23 |
|
---|
[119] | 24 | # need to create a DNSDB object too
|
---|
[191] | 25 | my ($dbh,$msg) = DNSDB::connectDB($DNSDB::config{dbname}, $DNSDB::config{dbuser},
|
---|
| 26 | $DNSDB::config{dbpass}, $DNSDB::config{dbhost});
|
---|
| 27 |
|
---|
[121] | 28 | DNSDB::initGlobals($dbh);
|
---|
[119] | 29 |
|
---|
| 30 | my $methods = {
|
---|
| 31 | 'dnsdb.addDomain' => \&addDomain,
|
---|
| 32 | 'dnsdb.delDomain' => \&delDomain,
|
---|
[121] | 33 | 'dnsdb.addGroup' => \&addGroup,
|
---|
| 34 | 'dnsdb.delGroup' => \&delGroup,
|
---|
| 35 | 'dnsdb.addUser' => \&addUser,
|
---|
| 36 | 'dnsdb.updateUser' => \&updateUser,
|
---|
| 37 | 'dnsdb.delUser' => \&delUser,
|
---|
| 38 | 'dnsdb.getSOA' => \&getSOA,
|
---|
[123] | 39 | 'dnsdb.getRecLine' => \&getRecLine,
|
---|
| 40 | 'dnsdb.getDomRecs' => \&getDomRecs,
|
---|
| 41 | 'dnsdb.getRecCount' => \&getRecCount,
|
---|
| 42 | 'dnsdb.addRec' => \&addRec,
|
---|
| 43 | 'dnsdb.delRec' => \&delRec,
|
---|
| 44 | 'dnsdb.domStatus' => \&domStatus,
|
---|
[121] | 45 |
|
---|
[119] | 46 | 'dnsdb.getMethods' => \&get_method_list
|
---|
| 47 | };
|
---|
| 48 |
|
---|
| 49 | my $res = Frontier::Responder->new(
|
---|
| 50 | methods => $methods
|
---|
| 51 | );
|
---|
| 52 |
|
---|
| 53 | # "Can't do that" errors
|
---|
| 54 | ##fixme: this MUST be loaded from a config file! Also must support multiple IPs
|
---|
| 55 | if ($ENV{REMOTE_ADDR} ne '192.168.2.116') {
|
---|
| 56 | print "Content-type: text/xml\n\n".$res->{_decode}->encode_fault(5, "Access denied");
|
---|
| 57 | exit;
|
---|
| 58 | }
|
---|
| 59 | if (!$dbh) {
|
---|
| 60 | print "Content-type: text/xml\n\n".$res->{_decode}->encode_fault(5, $msg);
|
---|
| 61 | exit;
|
---|
| 62 | }
|
---|
| 63 | ##fixme: fail on missing rpcuser/rpcsystem args
|
---|
| 64 |
|
---|
| 65 | print $res->answer;
|
---|
| 66 |
|
---|
| 67 | exit;
|
---|
| 68 |
|
---|
| 69 | ##
|
---|
| 70 | ## Subs below here
|
---|
| 71 | ##
|
---|
| 72 |
|
---|
| 73 | #sub connectDB {
|
---|
| 74 | #sub finish {
|
---|
| 75 | #sub initGlobals {
|
---|
| 76 | #sub initPermissions {
|
---|
| 77 | #sub getPermissions {
|
---|
| 78 | #sub changePermissions {
|
---|
| 79 | #sub comparePermissions {
|
---|
| 80 | #sub changeGroup {
|
---|
| 81 | #sub _log {
|
---|
| 82 |
|
---|
| 83 | sub addDomain {
|
---|
| 84 | my %args = @_;
|
---|
| 85 |
|
---|
| 86 | # Make sure we've got all the local bits we need
|
---|
| 87 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 88 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
| 89 |
|
---|
| 90 | my ($code, $msg) = DNSDB::addDomain($dbh, $args{domain}, $args{group}, $args{state});
|
---|
| 91 | die $msg if $code eq 'FAIL';
|
---|
| 92 | return $msg; # domain ID
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | sub delDomain {
|
---|
| 96 | my %args = @_;
|
---|
| 97 |
|
---|
| 98 | # Make sure we've got all the local bits we need
|
---|
| 99 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 100 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
| 101 |
|
---|
[121] | 102 | my ($code,$msg);
|
---|
[119] | 103 | # Let's be nice; delete based on domid OR domain name. Saves an RPC call round-trip, maybe.
|
---|
| 104 | if ($args{domain} =~ /^\d+$/) {
|
---|
[121] | 105 | ($code,$msg) = DNSDB::delDomain($dbh, $args{domain});
|
---|
[119] | 106 | } else {
|
---|
| 107 | my $domid = DNSDB::domainID($dbh, $args{domain});
|
---|
| 108 | die "Can't find domain" if !$domid;
|
---|
[121] | 109 | ($code,$msg) = DNSDB::delDomain($dbh, $domid);
|
---|
[119] | 110 | }
|
---|
| 111 | die $msg if $code eq 'FAIL';
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | #sub domainName {
|
---|
| 115 | #sub domainID {
|
---|
| 116 |
|
---|
| 117 | sub addGroup {
|
---|
| 118 | my %args = @_;
|
---|
| 119 |
|
---|
| 120 | # Make sure we've got all the local bits we need
|
---|
| 121 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 122 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
| 123 |
|
---|
| 124 | # not sure how to usefully represent permissions from any further out from DNSDB.pm :/
|
---|
[121] | 125 | # not to mention, permissions are checked at the UI layer, not the DB layer.
|
---|
[119] | 126 | my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
|
---|
| 127 | record_edit => 1, record_create => 1, record_delete => 1
|
---|
| 128 | };
|
---|
| 129 | ## optional $inhert arg?
|
---|
| 130 | my ($code,$msg) = DNSDB::addGroup($dbh, $args{groupname}, $args{parent_id}, $perms);
|
---|
| 131 | die $msg if $code eq 'FAIL';
|
---|
| 132 | return $msg;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | sub delGroup {
|
---|
| 136 | my %args = @_;
|
---|
| 137 |
|
---|
| 138 | # Make sure we've got all the local bits we need
|
---|
| 139 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 140 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
| 141 |
|
---|
[121] | 142 | my ($code,$msg);
|
---|
[119] | 143 | # Let's be nice; delete based on groupid OR group name. Saves an RPC call round-trip, maybe.
|
---|
| 144 | if ($args{group} =~ /^\d+$/) {
|
---|
[121] | 145 | ($code,$msg) = DNSDB::delGroup($dbh, $args{group});
|
---|
[119] | 146 | } else {
|
---|
| 147 | my $grpid = DNSDB::groupID($dbh, $args{group});
|
---|
| 148 | die "Can't find group" if !$grpid;
|
---|
[121] | 149 | ($code,$msg) = DNSDB::delGroup($dbh, $grpid);
|
---|
[119] | 150 | }
|
---|
| 151 | die $msg if $code eq 'FAIL';
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | #sub getChildren {
|
---|
| 155 | #sub groupName {
|
---|
| 156 | #sub groupID {
|
---|
| 157 |
|
---|
| 158 | sub addUser {
|
---|
| 159 | my %args = @_;
|
---|
| 160 |
|
---|
| 161 | # Make sure we've got all the local bits we need
|
---|
| 162 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 163 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
| 164 |
|
---|
[121] | 165 | # not sure how to usefully represent permissions from any further out from DNSDB.pm :/
|
---|
| 166 | # not to mention, permissions are checked at the UI layer, not the DB layer.
|
---|
| 167 | my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
|
---|
| 168 | record_edit => 1, record_create => 1, record_delete => 1
|
---|
| 169 | };
|
---|
[119] | 170 | # bend and twist; get those arguments in in the right order!
|
---|
| 171 | $args{type} = 'u' if !$args{type};
|
---|
| 172 | $args{permstring} = 'i' if !defined($args{permstring});
|
---|
| 173 | my @userargs = ($args{username}, $args{group}, $args{pass}, $args{state}, $args{type}, $args{permstring});
|
---|
| 174 | for my $argname ('fname','lname','phone') {
|
---|
| 175 | last if !$args{$argname};
|
---|
| 176 | push @userargs, $args{$argname};
|
---|
| 177 | }
|
---|
| 178 | my ($code,$msg) = DNSDB::addUser($dbh, @userargs);
|
---|
| 179 | die $msg if $code eq 'FAIL';
|
---|
| 180 | return $msg;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | #sub checkUser {
|
---|
| 184 |
|
---|
| 185 | sub updateUser {
|
---|
| 186 | my %args = @_;
|
---|
| 187 |
|
---|
| 188 | # Make sure we've got all the local bits we need
|
---|
| 189 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 190 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
| 191 |
|
---|
| 192 | die "Missing UID" if !$args{uid};
|
---|
[121] | 193 |
|
---|
| 194 | # not sure how to usefully represent permissions from any further out from DNSDB.pm :/
|
---|
| 195 | # not to mention, permissions are checked at the UI layer, not the DB layer.
|
---|
| 196 | my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
|
---|
| 197 | record_edit => 1, record_create => 1, record_delete => 1
|
---|
| 198 | };
|
---|
[119] | 199 | # bend and twist; get those arguments in in the right order!
|
---|
| 200 | my @userargs = ($args{uid}, $args{username}, $args{group}, $args{pass}, $args{state}, $args{type});
|
---|
| 201 | for my $argname ('fname','lname','phone') {
|
---|
| 202 | last if !$args{$argname};
|
---|
| 203 | push @userargs, $args{$argname};
|
---|
| 204 | }
|
---|
| 205 | ##fixme: also underlying in DNSDB::updateUser(): no way to just update this or that attribute;
|
---|
| 206 | # have to pass them all in to be overwritten
|
---|
| 207 | my ($code,$msg) = DNSDB::addUser($dbh, @userargs);
|
---|
| 208 | die $msg if $code eq 'FAIL';
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | sub delUser {
|
---|
| 212 | my %args = @_;
|
---|
| 213 |
|
---|
| 214 | # Make sure we've got all the local bits we need
|
---|
| 215 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 216 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
| 217 |
|
---|
| 218 | die "Missing UID" if !$args{uid};
|
---|
| 219 | my ($code,$msg) = DNSDB::delUser($dbh, $args{uid});
|
---|
| 220 | die $msg if $code eq 'FAIL';
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | #sub userFullName {
|
---|
| 224 | #sub userStatus {
|
---|
| 225 | #sub getUserData {
|
---|
| 226 |
|
---|
| 227 | sub getSOA {
|
---|
| 228 | my %args = @_;
|
---|
| 229 |
|
---|
| 230 | # Make sure we've got all the local bits we need
|
---|
| 231 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 232 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
[121] | 233 |
|
---|
| 234 | my %ret = DNSDB::getSOA($dbh, $args{def}, $args{id});
|
---|
| 235 | if (!$ret{recid}) {
|
---|
| 236 | if ($args{def} eq 'y') {
|
---|
| 237 | die "No default SOA record in group";
|
---|
| 238 | } else {
|
---|
| 239 | die "No SOA record in domain";
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 | return \%ret;
|
---|
[119] | 243 | }
|
---|
| 244 |
|
---|
| 245 | sub getRecLine {
|
---|
| 246 | my %args = @_;
|
---|
| 247 |
|
---|
| 248 | # Make sure we've got all the local bits we need
|
---|
| 249 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 250 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
[123] | 251 |
|
---|
| 252 | my $ret = DNSDB::getRecLine($dbh, $args{def}, $args{id});
|
---|
| 253 |
|
---|
| 254 | die $DNSDB::errstr if !$ret;
|
---|
| 255 |
|
---|
| 256 | return $ret;
|
---|
[119] | 257 | }
|
---|
| 258 |
|
---|
| 259 | sub getDomRecs {
|
---|
| 260 | my %args = @_;
|
---|
| 261 |
|
---|
| 262 | # Make sure we've got all the local bits we need
|
---|
| 263 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 264 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
[123] | 265 |
|
---|
| 266 | #bleh
|
---|
| 267 | $args{nrecs} = 'all' if !$args{nrecs};
|
---|
| 268 | $args{nstart} = 0 if !$args{nstart};
|
---|
| 269 | ## for order, need to map input to column names
|
---|
| 270 | $args{order} = 'host' if !$args{order};
|
---|
| 271 | $args{direction} = 'ASC' if !$args{direction};
|
---|
| 272 |
|
---|
| 273 | my $ret = DNSDB::getDomRecs($dbh, $args{def}, $args{id}, $args{nrecs}, $args{nstart}, $args{order}, $args{direction});
|
---|
| 274 |
|
---|
| 275 | die $DNSDB::errstr if !$ret;
|
---|
| 276 |
|
---|
| 277 | return $ret;
|
---|
[119] | 278 | }
|
---|
| 279 |
|
---|
[123] | 280 | sub getRecCount {
|
---|
| 281 | my %args = @_;
|
---|
[119] | 282 |
|
---|
[123] | 283 | # Make sure we've got all the local bits we need
|
---|
| 284 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 285 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
| 286 |
|
---|
| 287 | return DNSDB::getRecCount($dbh, $id);
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[119] | 290 | sub addRec {
|
---|
| 291 | my %args = @_;
|
---|
| 292 |
|
---|
| 293 | # Make sure we've got all the local bits we need
|
---|
| 294 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 295 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
[123] | 296 |
|
---|
| 297 | # note dist, weight, port are not reequired on all types; will be ignored if not needed.
|
---|
| 298 | my ($code, $msg) = DNSDB::addRec($dbh, $args{def}, $args{domid}, $args{host}, $typemap{$args{type}},
|
---|
| 299 | $args{val}, $args{ttl}, $args{dist}, $args{weight}, $args{port});
|
---|
| 300 |
|
---|
| 301 | die $msg if $code eq 'FAIL';
|
---|
[119] | 302 | }
|
---|
| 303 |
|
---|
| 304 | sub updateRec {
|
---|
| 305 | my %args = @_;
|
---|
| 306 |
|
---|
| 307 | # Make sure we've got all the local bits we need
|
---|
| 308 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 309 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
[123] | 310 |
|
---|
| 311 | # note dist, weight, port are not reequired on all types; will be ignored if not needed.
|
---|
| 312 | my ($code, $msg) = DNSDB::updateRec($dbh, $args{def}, $args{recid}, $args{host}, $typemap{$args{type}},
|
---|
| 313 | $args{val}, $args{ttl}, $args{dist}, $args{weight}, $args{port});
|
---|
| 314 |
|
---|
| 315 | die $msg if $code eq 'FAIL';
|
---|
[119] | 316 | }
|
---|
| 317 |
|
---|
| 318 | sub delRec {
|
---|
| 319 | my %args = @_;
|
---|
| 320 |
|
---|
| 321 | # Make sure we've got all the local bits we need
|
---|
| 322 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 323 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
[123] | 324 |
|
---|
| 325 | # note dist, weight, port are not reequired on all types; will be ignored if not needed.
|
---|
| 326 | my ($code, $msg) = DNSDB::delRec($dbh, $args{def}, $args{recid});
|
---|
| 327 |
|
---|
| 328 | die $msg if $code eq 'FAIL';
|
---|
[119] | 329 | }
|
---|
| 330 |
|
---|
| 331 | #sub getParents {
|
---|
[123] | 332 |
|
---|
| 333 | sub domStatus {
|
---|
| 334 | my %args = @_;
|
---|
| 335 |
|
---|
| 336 | # Make sure we've got all the local bits we need
|
---|
| 337 | die "Missing remote username" if !$args{rpcuser}; # for logging
|
---|
| 338 | die "Missing remote system name" if !$args{rpcsystem}; # for logging
|
---|
| 339 |
|
---|
| 340 | my @arglist = ($dbh, $args{domid});
|
---|
| 341 | push @arglist, $args{status} if defined($args{status});
|
---|
| 342 |
|
---|
| 343 | my $status = DNSDB::domStatus(@arglist);
|
---|
| 344 | }
|
---|
| 345 |
|
---|
[119] | 346 | #sub importAXFR {
|
---|
| 347 | #sub export {
|
---|
| 348 | #sub __export_tiny {
|
---|
| 349 |
|
---|
| 350 | sub get_method_list {
|
---|
| 351 | my @methods = keys %{$methods};
|
---|
| 352 | return \@methods;
|
---|
| 353 | }
|
---|