| [188] | 1 | #!/usr/bin/perl | 
|---|
|  | 2 | # Shell-based script to allocate arbitrary block | 
|---|
|  | 3 | ### | 
|---|
|  | 4 | # SVN revision info | 
|---|
|  | 5 | # $Date: 2017-08-15 17:53:23 +0000 (Tue, 15 Aug 2017) $ | 
|---|
|  | 6 | # SVN revision $Rev: 906 $ | 
|---|
|  | 7 | # Last update by $Author: kdeugau $ | 
|---|
|  | 8 | ### | 
|---|
|  | 9 |  | 
|---|
|  | 10 | use strict; | 
|---|
|  | 11 | use warnings; | 
|---|
|  | 12 | use DBI; | 
|---|
|  | 13 | use NetAddr::IP; | 
|---|
|  | 14 |  | 
|---|
|  | 15 | use Sys::Syslog; | 
|---|
|  | 16 |  | 
|---|
| [417] | 17 | # don't remove!  required for GNU/FHS-ish install from tarball | 
|---|
|  | 18 | ##uselib## | 
|---|
|  | 19 |  | 
|---|
| [906] | 20 | # push "the directory the script is in" into @INC | 
|---|
|  | 21 | use FindBin; | 
|---|
|  | 22 | use lib "$FindBin::RealBin/"; | 
|---|
|  | 23 |  | 
|---|
| [417] | 24 | use MyIPDB; | 
|---|
|  | 25 |  | 
|---|
| [431] | 26 | openlog "IPDBshell","pid","$IPDB::syslog_facility"; | 
|---|
| [188] | 27 |  | 
|---|
|  | 28 | # Collect the username from the environment.  If undefined, something | 
|---|
|  | 29 | # is Officially Hosed. | 
|---|
|  | 30 | my $authuser; | 
|---|
|  | 31 | if (!defined($ENV{'USER'})) { | 
|---|
|  | 32 | die "Bad environment!  USER not defined.\n"; | 
|---|
|  | 33 | } else { | 
|---|
|  | 34 | $authuser = $ENV{'USER'}; | 
|---|
|  | 35 | } | 
|---|
|  | 36 |  | 
|---|
|  | 37 | # Why not a global DB handle?  (And a global statement handle, as well...) | 
|---|
|  | 38 | # Use the connectDB function, otherwise we end up confusing ourselves | 
|---|
|  | 39 | my $ip_dbh; | 
|---|
|  | 40 | my $sth; | 
|---|
|  | 41 | my $errstr; | 
|---|
|  | 42 | ($ip_dbh,$errstr) = connectDB_My; | 
|---|
| [517] | 43 | die "Failed to connect to database: $errstr\n" | 
|---|
|  | 44 | if !$ip_dbh; | 
|---|
|  | 45 |  | 
|---|
| [188] | 46 | checkDBSanity($ip_dbh); | 
|---|
|  | 47 | initIPDBGlobals($ip_dbh); | 
|---|
|  | 48 |  | 
|---|
|  | 49 | # Hokay, now we can start to handle the allocation. | 
|---|
|  | 50 |  | 
|---|
| [895] | 51 | my ($cidr, $type, $vrf, $custid, $city, $desc) = @ARGV; | 
|---|
|  | 52 | my ($fbid, $fbparent); | 
|---|
| [188] | 53 | # Check ARGV.  We need some information to determine what to allocate. | 
|---|
| [895] | 54 | if (!defined($vrf)) { | 
|---|
| [188] | 55 | # Usage message | 
|---|
| [895] | 56 | print "Usage:  allocate.pl <IP/subnet> <Type> <VRF> [CustID] [City] [\"Description\"]\n". | 
|---|
|  | 57 | "       IP/subnet, Type, and VRF are required\n". | 
|---|
| [188] | 58 | "       Further information can be entered via the web interface\n"; | 
|---|
|  | 59 | exit; | 
|---|
|  | 60 | } else { | 
|---|
| [895] | 61 |  | 
|---|
|  | 62 | $cidr = new NetAddr::IP $cidr; | 
|---|
|  | 63 | ($fbid,$fbparent) = $ip_dbh->selectrow_array( | 
|---|
|  | 64 | "SELECT id,parent_id FROM freeblocks WHERE cidr >>= ? AND vrf = ?", undef, "$cidr", $vrf) | 
|---|
|  | 65 | or die "Couldn't find a free block to match '$cidr' in $vrf\n"; | 
|---|
|  | 66 | if (!$desc) { | 
|---|
| [188] | 67 | # Default desc | 
|---|
|  | 68 | $desc = "DEFAULT: $disp_alloctypes{$type}"; | 
|---|
|  | 69 | } | 
|---|
| [895] | 70 | if (!$city) { | 
|---|
|  | 71 | # Default city taken from parent allocation | 
|---|
|  | 72 | ($city) = $ip_dbh->selectrow_array("SELECT city FROM allocations WHERE id = ?", undef, $fbparent); | 
|---|
| [188] | 73 | } | 
|---|
| [895] | 74 | if (!$custid) { | 
|---|
|  | 75 | # See if the type has a default custID.... | 
|---|
|  | 76 | $custid = $ip_dbh->selectrow_array("SELECT def_custid FROM alloctypes WHERE type = ?", undef, $type); | 
|---|
|  | 77 | # ... and if not, make it REAL obvious this needs to be fixed. | 
|---|
|  | 78 | $custid = "FIXME" if !$custid; | 
|---|
| [188] | 79 | } | 
|---|
|  | 80 | } | 
|---|
|  | 81 |  | 
|---|
|  | 82 | print "Allocating $cidr as $type to $custid in $city: '$desc'\n"; | 
|---|
|  | 83 |  | 
|---|
| [895] | 84 | my %insert_args = ( | 
|---|
|  | 85 | cidr            => "$cidr", | 
|---|
|  | 86 | fbid            => $fbid, | 
|---|
|  | 87 | parent          => $fbparent, | 
|---|
|  | 88 | custid          => $custid, | 
|---|
|  | 89 | type            => $type, | 
|---|
|  | 90 | city            => $city, | 
|---|
|  | 91 | desc            => $desc, | 
|---|
|  | 92 | vrf             => $vrf, | 
|---|
|  | 93 | user            => $authuser, | 
|---|
|  | 94 | ); | 
|---|
| [188] | 95 |  | 
|---|
| [895] | 96 | my ($code,$msg) = allocateBlock($ip_dbh, %insert_args); | 
|---|
|  | 97 |  | 
|---|
| [188] | 98 | if ($code eq 'OK') { | 
|---|
|  | 99 | print "Allocation OK!\n"; | 
|---|
|  | 100 | syslog "notice", "($authuser) Allocated '$cidr' to '$custid' as '$type'"; | 
|---|
|  | 101 | } else { | 
|---|
|  | 102 | print "Allocation failed!  IPDB::allocateBlock said:\n$msg\n"; | 
|---|
|  | 103 | syslog "err", "($authuser) Allocation of '$cidr' to '$custid' as '$type' failed: '$msg'"; | 
|---|
|  | 104 | } | 
|---|
|  | 105 |  | 
|---|
|  | 106 | # Close it down. | 
|---|
|  | 107 | finish($ip_dbh); | 
|---|