source: trunk/cgi-bin/IPDB.pm@ 66

Last change on this file since 66 was 66, checked in by Kris Deugau, 20 years ago

/trunk

Shell of allocateBlock() function added
mailNotify function added

File size: 2.4 KB
Line 
1# ipdb/cgi-bin/IPDB.pm
2# Contains functions for IPDB - database access, subnet mangling, block allocation, etc
3###
4# SVN revision info
5# $Date$
6# SVN revision $Rev$
7# Last update by $Author$
8###
9# Copyright (C) 2004 - Kris Deugau
10
11package IPDB;
12
13use strict;
14use warnings;
15use Exporter;
16use Net::SMTP;
17use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
18
19$VERSION = 1.0;
20@ISA = qw(Exporter);
21@EXPORT_OK = qw(&connectDB &checkDBSanity &allocateBlock);
22
23@EXPORT = (); # Export nothing by default.
24%EXPORT_TAGS = ( ALL => [qw( &connectDB &checkDBSanity &allocateBlock )]
25 );
26
27
28# Creates connection to IPDB.
29# Default is a PostgreSQL db; could be any DBMS with the
30# right changes. MySQL in comments. Note that some DBMS's don't
31# support transactions, this is a Bad Thing!
32# Returns a handle to the db.
33sub connectDB {
34 my $dbh;
35 my $DSN = "DBI:Pg:dbname=ipdb";
36 my $user = 'ipdb';
37 my $pw = 'ipdbpwd';
38
39# Note that we want to autocommit by default, and we will turn it off locally as necessary.
40 $dbh = DBI->connect($DSN, $user, $pw, { AutoCommit => 1 } )
41 or return undef if(!$dbh);
42
43 return $dbh;
44} # end connectDB
45
46# Quick check to see if the db is responding. A full integrity
47# check will have to be a separate tool to walk the IP allocation trees.
48sub checkDBSanity {
49 my $dbh = connectDB();
50
51 if (!$dbh) {
52 print "Cannot connect to the database!";
53 } else {
54 # it connects, try a stmt.
55 my $sth = $dbh->prepare('select cidr from masterblocks');
56 my $err = $sth->execute();
57
58 if ($sth->fetchrow()) {
59 # all is well.
60 return 1;
61 } else {
62 print "Connected to the database, but could not execute test statement. ".$sth->errstr();
63 }
64 }
65 # Clean up after ourselves.
66 $dbh->disconnect;
67} # end checkDBSanity
68
69
70# allocateBlock()
71# Does all of the magic of actually allocating a netblock
72sub allocateBlock($) {
73}
74
75
76# mailNotify()
77# Sends notification mail to recipients regarding an IPDB operation
78sub mailNotify ($$$) {
79 my ($recip,$subj,$message) = @_;
80 my $mailer = new Net::SMTP "smtp.example.com";
81
82 $mailer->mail('ipdb@example.com');
83 $mailer->to($recip);
84 $mailer->data("X-Mailer: IPDB Notify v$IPDB::Version\n",
85 "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z\n", localtime)."\n",
86 "Organization: Example Corp.",
87 "From: \"IP Database\" <ipdb\@example.com>\n",
88 "Subject: $subj\n",
89 "\n",
90 "$message\n");
91 $mailer->quit;
92}
93
94# Indicates module loaded OK. Required by Perl.
951;
Note: See TracBrowser for help on using the repository browser.