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

Last change on this file since 868 was 593, checked in by Kris Deugau, 12 years ago

/branches/stable

Merge /trunk r517 (merge /branches/htmlform)
Conflicts all resolved towards /trunk.
Fix a minor syntax error with "while (@data..." -> "while (my @data..."
(may cause merge conflicts later)

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 2.6 KB
RevLine 
[188]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) $
[291]6# SVN revision $Rev: 593 $
[188]7# Last update by $Author: kdeugau $
8###
9
10use strict;
11use warnings;
12use DBI;
13use NetAddr::IP;
14
15use Sys::Syslog;
16
[445]17# don't remove! required for GNU/FHS-ish install from tarball
18##uselib##
[188]19
[445]20use MyIPDB;
21
22openlog "IPDBshell","pid","$IPDB::syslog_facility";
23
[188]24# Collect the username from the environment. If undefined, something
25# is Officially Hosed.
26my $authuser;
27if (!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
35my $ip_dbh;
36my $sth;
37my $errstr;
38($ip_dbh,$errstr) = connectDB_My;
[593]39die "Failed to connect to database: $errstr\n"
40 if !$ip_dbh;
41
[188]42checkDBSanity($ip_dbh);
43initIPDBGlobals($ip_dbh);
44
45# Hokay, now we can start to handle the allocation.
46
47my ($cidr, $type, $custid, $city, $desc, $alloc_from);
48# Check ARGV. We need some information to determine what to allocate.
49if (!$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
89print "Allocating $cidr as $type to $custid in $city: '$desc'\n";
90
91my ($code,$msg) = allocateBlock($ip_dbh, $cidr, $alloc_from, $custid, $type, $city,
92 $desc, '', '');
93
94if ($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.
103finish($ip_dbh);
Note: See TracBrowser for help on using the repository browser.