source: branches/stable/cgi-bin/ipdb-rpc.cgi@ 607

Last change on this file since 607 was 600, checked in by Kris Deugau, 12 years ago

/branches/stable

Add inital XMLRPC shim. See #40.

We'll start with a method to get a list of static IPs available for
assignment, and a method to allocate a netblock.

  • Property svn:executable set to *
File size: 5.9 KB
Line 
1#!/usr/bin/perl
2# XMLRPC interface to manipulate most DNS DB entities
3###
4# $Id$
5###
6# Copyright (C) 2013 Kris Deugau <kdeugau@deepnet.cx>
7
8use strict;
9use warnings;
10
11use DBI;
12use CustIDCK;
13use NetAddr::IP;
14use FCGI;
15use Frontier::Responder;
16
17use Sys::Syslog;
18
19# don't remove! required for GNU/FHS-ish install from tarball
20##uselib##
21
22use MyIPDB;
23
24openlog "IPDB-rpc","pid","$IPDB::syslog_facility";
25
26##fixme: username source? can we leverage some other auth method?
27# we don't care except for logging here, and Frontier::Client needs
28# a patch that's not well-distributed to use HTTP AUTH.
29
30# Collect the username from HTTP auth. If undefined, we're in
31# a test environment, or called without a username.
32my $authuser;
33if (!defined($ENV{'REMOTE_USER'})) {
34 $authuser = '__temptest';
35} else {
36 $authuser = $ENV{'REMOTE_USER'};
37}
38
39syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
40
41# Why not a global DB handle? (And a global statement handle, as well...)
42# Use the connectDB function, otherwise we end up confusing ourselves
43my $ip_dbh;
44my $sth;
45my $errstr;
46($ip_dbh,$errstr) = connectDB_My;
47initIPDBGlobals($ip_dbh);
48
49my $methods = {
50# 'ipdb.getCityList' => \&rpc_getCityList,
51 'ipdb.getAvailableStatics' => \&rpc_getAvailableStatics,
52 'ipdb.allocateBlock' => \&rpc_allocateBlock,
53};
54
55my $reqcnt = 0;
56
57# main FCGI loop.
58while (FCGI::accept >= 0) {
59 # don't *think* we need any of these...
60 # %disp_alloctypes, %def_custids, %list_alloctypes
61 # @citylist, @poplist
62 # @masterblocks, %allocated, %free, %bigfree, %routed (removed in /trunk)
63 # %IPDBacl
64 #initIPDBGlobals($ip_dbh);
65
66 my $res = Frontier::Responder->new(
67 methods => $methods
68 );
69
70 # "Can't do that" errors
71 if (!$ip_dbh) {
72 print "Content-type: text/xml\n\n".$res->{_decode}->encode_fault(5, $DBI::errstr);
73 } else {
74 print $res->answer;
75 }
76# last if $reqcnt++ > $IPDB::maxfcgi;
77} # while FCGI::accept
78
79exit 0;
80
81
82##
83## Private subs
84##
85
86# Check RPC ACL
87sub _aclcheck {
88 my $subsys = shift;
89 return 1;
90 return 1 if grep /$ENV{REMOTE_ADDR}/, $IPDB::rpcacl{$subsys};
91 warn "$subsys/$ENV{REMOTE_ADDR} not in ACL\n"; # a bit of logging
92 return 0;
93}
94
95sub _commoncheck {
96 my $argref = shift;
97 my $needslog = shift;
98
99 die "Missing remote system name\n" if !$argref->{rpcsystem};
100 die "Access denied\n" if !_aclcheck($argref->{rpcsystem});
101 if ($needslog) {
102 die "Missing remote username\n" if !$argref->{rpcuser};
103 }
104}
105
106# stripped-down copy from from main.cgi. should probably be moved to IPDB.pm
107sub _validateInput {
108 my $argref = shift;
109
110 die "Block/IP is required\n" if !$argref->{block};
111
112 # Alloctype check.
113 chomp $argref->{alloctype};
114
115 die "Invalid allocation type\n" if (!grep /$argref->{alloctype}/, keys %disp_alloctypes);
116
117 # Arguably not quite correct, as the custID won't be checked for
118 # validity if there's a default on the type.
119 if ($def_custids{$argref->{alloctype}} eq '') {
120 # Types without a default custID must have one passed in
121 die "Customer ID is required\n" if !$argref->{custid};
122 # Crosscheck with billing.
123 my $status = CustIDCK->custid_exist($argref->{custid});
124 die "Error verifying customer ID: $CustIDCK::ErrMsg\n" if $CustIDCK::Error;
125 die "Customer ID not valid\n" if !$status;
126 } else {
127 # Types that have a default will use it unless one is specified.
128 if ((!$argref->{custid}) || ($argref->{custid} ne 'STAFF')) {
129 $argref->{custid} = $def_custids{$argref->{alloctype}};
130 }
131 }
132} # end validateInput()
133
134
135##
136## RPC method subs
137##
138
139# Prefixed with rpc_ to not conflict with subs in IPDB.pm
140
141sub rpc_getCityList {
142 my %args = @_;
143
144 _commoncheck(\%args, 'n');
145}
146
147# Get a list of available static IPs of the given type
148sub rpc_getAvailableStatics {
149 my %args = @_;
150
151 _commoncheck(\%args, 'n');
152
153 my ($base,undef) = split //, $args{type};
154 $base .= "_";
155 my @params = ($base);
156
157 # IPDB 2.7
158 my $sql = "SELECT 0 AS id,poolips.ip,0 AS parent_id,pool ".
159 "FROM poolips JOIN allocations ON poolips.pool=allocations.cidr WHERE poolips.type LIKE ?";
160
161 $sql .= " AND allocations.city=?" if $base ne 'd_';
162 push @params, $args{city} if $base ne 'd_';
163
164 $sql .= " AND poolips.available='y'";
165
166 my $ret = $ip_dbh->selectall_arrayref($sql, { Slice => {} }, (@params) );
167 # IPDB 3.0
168 #my $ret = $ipdb->getIPList(arg arg arg);
169 die $ip_dbh->errstr if !$ret;
170 return $ret;
171} # rpc_getAvailableStatics()
172
173sub rpc_allocateBlock {
174 my %args = @_;
175
176 _commoncheck(\%args, 'y');
177
178 _validateInput(\%args);
179
180 # Not required for update, delete
181 die "City is required\n" if !$args{city};
182 die "Block/pool to allocate from is required\n" if !$args{alloc_from};
183
184 # Desc, notes, circid, privdata, node, and vrf are all optional and handled in allocateBlock()
185 my ($code,$msg) = allocateBlock($ip_dbh, $args{block}, $args{alloc_from},
186 $args{custid}, $args{alloctype}, $args{city}, $args{desc}, $args{notes},
187 $args{circid}, $args{privdata}, $args{node}, $args{vrf});
188
189 if ($code eq 'OK' || $code eq 'WARN') {
190 if ($args{alloctype} =~ /^.i$/) {
191 $msg =~ s|/32||;
192 mailNotify($ip_dbh, "a$args{alloctype}", "ADDED: $disp_alloctypes{$args{alloctype}} allocation",
193 "$disp_alloctypes{$args{alloctype}} $msg allocated to customer $args{custid}\n".
194 "Description: $args{desc}\n\nAllocated by: $args{rpcsystem}/$args{rpcuser}\n");
195 } else {
196 my $netblock = new NetAddr::IP $args{block};
197 mailNotify($ip_dbh, "a$args{alloctype}", "ADDED: $disp_alloctypes{$args{alloctype}} allocation",
198 "$disp_alloctypes{$args{alloctype}} $args{block} allocated to customer $args{custid}\n".
199 "Description: $args{desc}\n\nAllocated by: $args{rpcsystem}/$args{rpcuser}\n");
200 }
201 syslog "notice", "$args{rpcsystem}/$args{rpcuser} allocated '$args{block}' to '$args{custid}' as ".
202 "'$args{alloctype}' ($msg)";
203 } else {
204 syslog "err", "Allocation of '$args{block}' to '$args{custid}' as ".
205 "'$args{alloctype}' by $args{rpcsystem}/$args{rpcuser} failed: '$msg'";
206 die "$msg\n";
207 }
208 return $msg;
209} # rpc_allocateBlock()
Note: See TracBrowser for help on using the repository browser.