1 | #!/usr/bin/perl |
---|
2 | # Shell-based script to allocate arbitrary block |
---|
3 | ### |
---|
4 | # SVN revision info |
---|
5 | # $Date: 2010-06-30 21:48:03 +0000 (Wed, 30 Jun 2010) $ |
---|
6 | # SVN revision $Rev: 417 $ |
---|
7 | # Last update by $Author: kdeugau $ |
---|
8 | ### |
---|
9 | |
---|
10 | use strict; |
---|
11 | use warnings; |
---|
12 | use DBI; |
---|
13 | use CommonWeb qw(:ALL); |
---|
14 | use NetAddr::IP; |
---|
15 | |
---|
16 | use Sys::Syslog; |
---|
17 | |
---|
18 | # don't remove! required for GNU/FHS-ish install from tarball |
---|
19 | ##uselib## |
---|
20 | |
---|
21 | use MyIPDB; |
---|
22 | |
---|
23 | openlog "IPDBshell","pid","local2"; |
---|
24 | |
---|
25 | # Collect the username from the environment. If undefined, something |
---|
26 | # is Officially Hosed. |
---|
27 | my $authuser; |
---|
28 | if (!defined($ENV{'USER'})) { |
---|
29 | die "Bad environment! USER not defined.\n"; |
---|
30 | } else { |
---|
31 | $authuser = $ENV{'USER'}; |
---|
32 | } |
---|
33 | |
---|
34 | # Why not a global DB handle? (And a global statement handle, as well...) |
---|
35 | # Use the connectDB function, otherwise we end up confusing ourselves |
---|
36 | my $ip_dbh; |
---|
37 | my $sth; |
---|
38 | my $errstr; |
---|
39 | ($ip_dbh,$errstr) = connectDB_My; |
---|
40 | if (!$ip_dbh) { |
---|
41 | printAndExit("Failed to connect to database: $errstr\n"); |
---|
42 | } |
---|
43 | checkDBSanity($ip_dbh); |
---|
44 | initIPDBGlobals($ip_dbh); |
---|
45 | |
---|
46 | # Hokay, now we can start to handle the allocation. |
---|
47 | |
---|
48 | my ($cidr, $type, $custid, $city, $desc, $alloc_from); |
---|
49 | # Check ARGV. We need some information to determine what to allocate. |
---|
50 | if (!$ARGV[1]) { |
---|
51 | # Usage message |
---|
52 | print "Usage: allocate.pl [IP/subnet] [Type] [CustID] [City] [\"Description\"]\n". |
---|
53 | " Further information can be entered via the web interface\n"; |
---|
54 | exit; |
---|
55 | } else { |
---|
56 | $cidr = new NetAddr::IP "$ARGV[0]"; |
---|
57 | $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr'"); |
---|
58 | $sth->execute; |
---|
59 | my @data = $sth->fetchrow_array; |
---|
60 | # User deserves errors if user can't be bothered to find the free block first. |
---|
61 | die "Can't allocate from outside a free block!!\n" |
---|
62 | if !$data[0]; |
---|
63 | $alloc_from = new NetAddr::IP $data[0]; |
---|
64 | $sth->finish; |
---|
65 | $type = $ARGV[1]; |
---|
66 | if (!$ARGV[4]) { |
---|
67 | # Default desc |
---|
68 | $desc = "DEFAULT: $disp_alloctypes{$type}"; |
---|
69 | } else { |
---|
70 | $desc = $ARGV[4]; |
---|
71 | } |
---|
72 | if (!$ARGV[3]) { |
---|
73 | # Default city |
---|
74 | $sth = $ip_dbh->prepare("select city from routed where cidr >>='$cidr'"); |
---|
75 | $sth->execute; |
---|
76 | my @data = $sth->fetchrow_array; |
---|
77 | $city = $data[0]; |
---|
78 | $sth->finish; |
---|
79 | } else { |
---|
80 | $city = $ARGV[3]; |
---|
81 | } |
---|
82 | if (!$ARGV[2]) { |
---|
83 | # Default custid - make it REAL obvious. |
---|
84 | $custid = "FIXME"; |
---|
85 | } else { |
---|
86 | $custid = $ARGV[2]; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | print "Allocating $cidr as $type to $custid in $city: '$desc'\n"; |
---|
91 | |
---|
92 | my ($code,$msg) = allocateBlock($ip_dbh, $cidr, $alloc_from, $custid, $type, $city, |
---|
93 | $desc, '', ''); |
---|
94 | |
---|
95 | if ($code eq 'OK') { |
---|
96 | print "Allocation OK!\n"; |
---|
97 | syslog "notice", "($authuser) Allocated '$cidr' to '$custid' as '$type'"; |
---|
98 | } else { |
---|
99 | print "Allocation failed! IPDB::allocateBlock said:\n$msg\n"; |
---|
100 | syslog "err", "($authuser) Allocation of '$cidr' to '$custid' as '$type' failed: '$msg'"; |
---|
101 | } |
---|
102 | |
---|
103 | # Close it down. |
---|
104 | finish($ip_dbh); |
---|