source: branches/stable/cgi-bin/allocate.pl@ 482

Last change on this file since 482 was 445, checked in by Kris Deugau, 14 years ago

/branches/stable

Bring /branches/stable up to date with /trunk. See #13.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 2.7 KB
Line 
1#!/usr/bin/perl
2# Shell-based script to allocate arbitrary block
3###
4# SVN revision info
5# $Date: 2010-07-26 21:00:00 +0000 (Mon, 26 Jul 2010) $
6# SVN revision $Rev: 445 $
7# Last update by $Author: kdeugau $
8###
9
10use strict;
11use warnings;
12use DBI;
13use CommonWeb qw(:ALL);
14use NetAddr::IP;
15
16use Sys::Syslog;
17
18# don't remove! required for GNU/FHS-ish install from tarball
19##uselib##
20
21use MyIPDB;
22
23openlog "IPDBshell","pid","$IPDB::syslog_facility";
24
25# Collect the username from the environment. If undefined, something
26# is Officially Hosed.
27my $authuser;
28if (!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
36my $ip_dbh;
37my $sth;
38my $errstr;
39($ip_dbh,$errstr) = connectDB_My;
40if (!$ip_dbh) {
41 printAndExit("Failed to connect to database: $errstr\n");
42}
43checkDBSanity($ip_dbh);
44initIPDBGlobals($ip_dbh);
45
46# Hokay, now we can start to handle the allocation.
47
48my ($cidr, $type, $custid, $city, $desc, $alloc_from);
49# Check ARGV. We need some information to determine what to allocate.
50if (!$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
90print "Allocating $cidr as $type to $custid in $city: '$desc'\n";
91
92my ($code,$msg) = allocateBlock($ip_dbh, $cidr, $alloc_from, $custid, $type, $city,
93 $desc, '', '');
94
95if ($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.
104finish($ip_dbh);
Note: See TracBrowser for help on using the repository browser.