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

Last change on this file since 835 was 610, checked in by Kris Deugau, 11 years ago

/branches/stable

Cleanup on some configuration for RPC and FastCGI

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