Changeset 128


Ignore:
Timestamp:
09/21/11 18:13:16 (13 years ago)
Author:
Kris Deugau
Message:

/trunk

Code review/cleanup

  • Move configuration of DB host, user, pass, name into an outside file.
  • Fail more gracefully on DB connection failure. Includes minor CSS tweak that probably doesn't really do what I think it's doing.
Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r123 r128  
    2828        &initPermissions &getPermissions &changePermissions &comparePermissions
    2929        &changeGroup
    30         &connectDB &finish
     30        &loadConfig &connectDB &finish
    3131        &addDomain &delDomain &domainName &domainID
    3232        &addGroup &delGroup &getChildren &groupName
     
    3838        &domStatus &importAXFR
    3939        &export
    40         %typemap %reverse_typemap
     40        %typemap %reverse_typemap %config
    4141        %permissions @permtypes $permlist
    4242        );
     
    4747                &initPermissions &getPermissions &changePermissions &comparePermissions
    4848                &changeGroup
    49                 &connectDB &finish
     49                &loadConfig &connectDB &finish
    5050                &addDomain &delDomain &domainName &domainID
    5151                &addGroup &delGroup &getChildren &groupName
     
    5757                &domStatus &importAXFR
    5858                &export
    59                 %typemap %reverse_typemap
     59                %typemap %reverse_typemap %config
    6060                %permissions @permtypes $permlist
    6161                )]
     
    9797our %permissions;
    9898
     99# Prepopulate a basic config.  Note some of these *will* cause errors if left unset.
     100our %config = (
     101                # Database connection info
     102                dbname  => 'dnsdb',
     103                dbuser  => 'dnsdb',
     104                dbpass  => 'secret',
     105                dbhost  => '',
     106
     107                # Email notice settings
     108                mailhost        => 'smtp.example.com',
     109                mailsender      => 'dnsdb@example.com',
     110                mailname        => 'DNS Administration',
     111
     112                # Template directory
     113                templatedir     => 'templates/',
     114# fmeh.  this is a real web path, not a logical internal one.  hm..
     115#               cssdir  => 'templates/';
     116        );
     117
     118
    99119##
    100120## Initialization and cleanup subs
    101121##
     122
     123
     124## DNSDB::loadConfig()
     125# Load the minimum required initial state (DB connect info) from a config file
     126# Load misc other bits while we're at it.
     127# Takes an optional basename and config path to look for
     128# Populates the %config and %def hashes
     129sub loadConfig {
     130  my $basename = shift || '';   # this will work OK
     131
     132  my $deferr = '';      # place to put error from default config file in case we can't find either one
     133
     134  my $configroot = '/etc/dnsdb';
     135  $configroot = '' if $basename =~ m|^/|;
     136  $basename .= ".conf" if $basename !~ /\.conf$/;
     137  my $defconfig = "$configroot/dnsdb.conf";
     138  my $siteconfig = "$configroot/$basename";
     139
     140  # System defaults
     141  __cfgload("$configroot/dnsdb.conf") or $deferr = $errstr;
     142
     143  # Per-site-ish settings
     144  unless (__cfgload("$configroot/$basename")) {
     145    $errstr = ($deferr ? "Error opening default config file $defconfig: $deferr\n" : '').
     146        "Error opening site config file $siteconfig";
     147    return;
     148  }
     149
     150  # All good, clear the error and go home.
     151  $errstr = '';
     152  return 1;
     153} # end loadConfig()
     154
     155
     156## DNSDB::__cfgload()
     157# Private sub to parse a config file and load it into %config
     158# Takes a file handle on an open config file
     159sub __cfgload {
     160  $errstr = '';
     161  my $cfgfile = shift;
     162  if (open CFG, "<$cfgfile") {
     163#  my $mode = '';
     164    while (<CFG>) {
     165      chomp;
     166      s/^\s*//;
     167      next if /^#/;
     168      next if /^$/;
     169# hmm.  more complex bits in this file might require [heading] headers, maybe?
     170#    $mode = $1 if /^\[(a-z)+]/;
     171    # DB connect info
     172      $config{dbname}   = $1 if /^dbname\s*=\s*([a-z0-9_.-]+)/i;
     173      $config{dbuser}   = $1 if /^dbuser\s*=\s*([a-z0-9_.-]+)/i;
     174      $config{dbpass}   = $1 if /^dbpass\s*=\s*([a-z0-9_.-]+)/i;
     175      $config{dbhost}   = $1 if /^dbhost\s*=\s*([a-z0-9_.-]+)/i;
     176      # SOA defaults
     177      $def{contact}     = $1 if /^contact\s*=\s*([a-z0-9_.-]+)/i;
     178      $def{prins}       = $1 if /^prins\s*=\s*([a-z0-9_.-]+)/i;
     179      $def{soattl}      = $1 if /^soattl\s*=\s*([a-z0-9_.-]+)/i;
     180      $def{refresh}     = $1 if /^refresh\s*=\s*([a-z0-9_.-]+)/i;
     181      $def{retry}       = $1 if /^retry\s*=\s*([a-z0-9_.-]+)/i;
     182      $def{expire}      = $1 if /^expire\s*=\s*([a-z0-9_.-]+)/i;
     183      $def{minttl}      = $1 if /^minttl\s*=\s*([a-z0-9_.-]+)/i;
     184      $def{ttl}         = $1 if /^ttl\s*=\s*([a-z0-9_.-]+)/i;
     185      # Mail settings
     186      $config{mailhost}         = $1 if /^mailhost\s*=\s*([a-z0-9_.-]+)/i;
     187      $config{mailsender}       = $1 if /^mailsender\s*=\s*([a-z0-9_.@-]+)/i;
     188      $config{mailname}         = $1 if /^mailname\s*=\s*([a-z0-9\s_.-]+)/i;
     189    }
     190    close CFG;
     191  } else {
     192    $errstr = $!;
     193    return;
     194  }
     195  return 1;
     196} # end __cfgload()
    102197
    103198
  • trunk/dns.cgi

    r126 r128  
    130130my $sortorder = "ASC";
    131131
    132 #my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret","newdbhost");
     132# now load some local system defaults (mainly DB connect info)
     133# note this is not *absolutely* fatal, since there's a default dbname/user/pass in DNSDB.pm
     134# we'll catch a bad DB connect string a little further down.
     135if (!loadConfig()) {
     136  warn "Using default configuration;  unable to load custom settings: $DNSDB::errstr";
     137}
     138
     139##fixme: quit throwing the database handle around, and put all the SQL and direct DB fiddling into DNSDB.pm
    133140# dbname, user, pass, host (optional)
    134 my ($dbh,$msg) = connectDB("dnsdb", "dnsdb", "secret", "dnsdbhost");
    135 #my $dbh = DBI->connect("DBI:mysql:database=vegadns","vegadns","secret",
    136 #       { AutoCommit => 0 }) or die $DBI::errstr;
    137 
    138 ##fixme.  PLEASE!  <G>
    139 print $msg if !$dbh;
    140 
    141 # fiddle hardcoded "defaults" as per system/user (?) prefs
     141my ($dbh,$msg) = connectDB($config{dbname}, $config{dbuser}, $config{dbpass}, $config{dbhost});
     142
     143if (!$dbh) {
     144  print "Content-type: text/html\n\n";
     145  print $header->output;
     146  my $errpage = HTML::Template->new(filename => "$templatedir/dberr.tmpl");
     147  $errpage->param(errmsg => $msg);
     148  print $errpage->output;
     149  print $footer->output;
     150  exit;
     151}
     152
     153# Load config pieces from the database.  Ideally all but the DB user/pass/etc should be loaded here.
    142154initGlobals($dbh);
    143155
  • trunk/templates/dns.css

    r117 r128  
    181181        text-align: center;
    182182}
     183/* not sure this really does what I think it does.  is it really not
     184   possible to center an arbitrary <blah> in some other arbitrary <foo>? */
     185.loccenter {
     186        margin-left: 10%;
     187        margin-right: 10%;
     188}
    183189.maintitle {
    184190        font-size: 1.3em;
Note: See TracChangeset for help on using the changeset viewer.