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