[2] | 1 | # dns/trunk/DNSDB.pm
|
---|
| 2 | # Abstraction functions for DNS administration
|
---|
| 3 | ###
|
---|
| 4 | # SVN revision info
|
---|
| 5 | # $Date: 2011-09-30 21:29:57 +0000 (Fri, 30 Sep 2011) $
|
---|
| 6 | # SVN revision $Rev: 129 $
|
---|
| 7 | # Last update by $Author: kdeugau $
|
---|
| 8 | ###
|
---|
| 9 | # Copyright (C) 2008 - Kris Deugau <kdeugau@deepnet.cx>
|
---|
| 10 |
|
---|
| 11 | package DNSDB;
|
---|
| 12 |
|
---|
| 13 | use strict;
|
---|
| 14 | use warnings;
|
---|
| 15 | use Exporter;
|
---|
| 16 | use DBI;
|
---|
[33] | 17 | use Net::DNS;
|
---|
[65] | 18 | use Crypt::PasswdMD5;
|
---|
[2] | 19 | #use Net::SMTP;
|
---|
| 20 | #use NetAddr::IP qw( Compact );
|
---|
| 21 | #use POSIX;
|
---|
| 22 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
|
---|
| 23 |
|
---|
| 24 | $VERSION = 0.1;
|
---|
| 25 | @ISA = qw(Exporter);
|
---|
| 26 | @EXPORT_OK = qw(
|
---|
[67] | 27 | &initGlobals
|
---|
| 28 | &initPermissions &getPermissions &changePermissions &comparePermissions
|
---|
[112] | 29 | &changeGroup
|
---|
[128] | 30 | &loadConfig &connectDB &finish
|
---|
[91] | 31 | &addDomain &delDomain &domainName &domainID
|
---|
[22] | 32 | &addGroup &delGroup &getChildren &groupName
|
---|
[83] | 33 | &addUser &updateUser &delUser &userFullName &userStatus &getUserData
|
---|
[91] | 34 | &getSOA &getRecLine &getDomRecs &getRecCount
|
---|
[22] | 35 | &addRec &updateRec &delRec
|
---|
[117] | 36 | &getParents
|
---|
| 37 | &isParent
|
---|
[34] | 38 | &domStatus &importAXFR
|
---|
[103] | 39 | &export
|
---|
[128] | 40 | %typemap %reverse_typemap %config
|
---|
[66] | 41 | %permissions @permtypes $permlist
|
---|
[2] | 42 | );
|
---|
| 43 |
|
---|
| 44 | @EXPORT = (); # Export nothing by default.
|
---|
| 45 | %EXPORT_TAGS = ( ALL => [qw(
|
---|
[67] | 46 | &initGlobals
|
---|
| 47 | &initPermissions &getPermissions &changePermissions &comparePermissions
|
---|
[112] | 48 | &changeGroup
|
---|
[128] | 49 | &loadConfig &connectDB &finish
|
---|
[91] | 50 | &addDomain &delDomain &domainName &domainID
|
---|
[22] | 51 | &addGroup &delGroup &getChildren &groupName
|
---|
[83] | 52 | &addUser &updateUser &delUser &userFullName &userStatus &getUserData
|
---|
[91] | 53 | &getSOA &getRecLine &getDomRecs &getRecCount
|
---|
[22] | 54 | &addRec &updateRec &delRec
|
---|
[117] | 55 | &getParents
|
---|
| 56 | &isParent
|
---|
[34] | 57 | &domStatus &importAXFR
|
---|
[103] | 58 | &export
|
---|
[128] | 59 | %typemap %reverse_typemap %config
|
---|
[66] | 60 | %permissions @permtypes $permlist
|
---|
[2] | 61 | )]
|
---|
| 62 | );
|
---|
| 63 |
|
---|
| 64 | our $group = 1;
|
---|
| 65 | our $errstr = '';
|
---|
| 66 |
|
---|
| 67 | # Halfway sane defaults for SOA, TTL, etc.
|
---|
[101] | 68 | # serial defaults to 0 for convenience.
|
---|
| 69 | # value will be either YYYYMMDDNN for BIND/etc, or auto-internal for tinydns
|
---|
[2] | 70 | our %def = qw (
|
---|
| 71 | contact hostmaster.DOMAIN
|
---|
| 72 | prins ns1.myserver.com
|
---|
[101] | 73 | serial 0
|
---|
[2] | 74 | soattl 86400
|
---|
| 75 | refresh 10800
|
---|
| 76 | retry 3600
|
---|
| 77 | expire 604800
|
---|
| 78 | minttl 10800
|
---|
| 79 | ttl 10800
|
---|
| 80 | );
|
---|
| 81 |
|
---|
[66] | 82 | # Arguably defined wholly in the db, but little reason to change without supporting code changes
|
---|
| 83 | our @permtypes = qw (
|
---|
| 84 | group_edit group_create group_delete
|
---|
| 85 | user_edit user_create user_delete
|
---|
| 86 | domain_edit domain_create domain_delete
|
---|
| 87 | record_edit record_create record_delete
|
---|
| 88 | self_edit admin
|
---|
| 89 | );
|
---|
| 90 | our $permlist = join(',',@permtypes);
|
---|
| 91 |
|
---|
[2] | 92 | # DNS record type map and reverse map.
|
---|
| 93 | # loaded from the database, from http://www.iana.org/assignments/dns-parameters
|
---|
| 94 | our %typemap;
|
---|
| 95 | our %reverse_typemap;
|
---|
| 96 |
|
---|
[65] | 97 | our %permissions;
|
---|
[55] | 98 |
|
---|
[128] | 99 | # Prepopulate a basic config. Note some of these *will* cause errors if left unset.
|
---|
| 100 | our %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 |
|
---|
[2] | 119 | ##
|
---|
| 120 | ## Initialization and cleanup subs
|
---|
| 121 | ##
|
---|
| 122 |
|
---|
[55] | 123 |
|
---|
[128] | 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
|
---|
| 129 | sub 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
|
---|
| 159 | sub __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()
|
---|
| 197 |
|
---|
| 198 |
|
---|
[2] | 199 | ## DNSDB::connectDB()
|
---|
| 200 | # Creates connection to DNS database.
|
---|
| 201 | # Requires the database name, username, and password.
|
---|
| 202 | # Returns a handle to the db.
|
---|
| 203 | # Set up for a PostgreSQL db; could be any transactional DBMS with the
|
---|
| 204 | # right changes.
|
---|
| 205 | sub connectDB {
|
---|
| 206 | $errstr = '';
|
---|
[15] | 207 | my $dbname = shift;
|
---|
| 208 | my $user = shift;
|
---|
| 209 | my $pass = shift;
|
---|
[2] | 210 | my $dbh;
|
---|
| 211 | my $DSN = "DBI:Pg:dbname=$dbname";
|
---|
| 212 |
|
---|
| 213 | my $host = shift;
|
---|
| 214 | $DSN .= ";host=$host" if $host;
|
---|
| 215 |
|
---|
| 216 | # Note that we want to autocommit by default, and we will turn it off locally as necessary.
|
---|
| 217 | # We may not want to print gobbledygook errors; YMMV. Have to ponder that further.
|
---|
| 218 | $dbh = DBI->connect($DSN, $user, $pass, {
|
---|
| 219 | AutoCommit => 1,
|
---|
| 220 | PrintError => 0
|
---|
| 221 | })
|
---|
| 222 | or return (undef, $DBI::errstr) if(!$dbh);
|
---|
| 223 |
|
---|
| 224 | # Return here if we can't select. Note that this indicates a
|
---|
| 225 | # problem executing the select.
|
---|
| 226 | my $sth = $dbh->prepare("select group_id from groups limit 1");
|
---|
| 227 | $sth->execute();
|
---|
| 228 | return (undef,$DBI::errstr) if ($sth->err);
|
---|
| 229 |
|
---|
| 230 | # See if the select returned anything (or null data). This should
|
---|
| 231 | # succeed if the select executed, but...
|
---|
| 232 | $sth->fetchrow();
|
---|
| 233 | return (undef,$DBI::errstr) if ($sth->err);
|
---|
| 234 |
|
---|
| 235 | $sth->finish;
|
---|
| 236 |
|
---|
| 237 | # If we get here, we should be OK.
|
---|
| 238 | return ($dbh,"DB connection OK");
|
---|
| 239 | } # end connectDB
|
---|
| 240 |
|
---|
| 241 |
|
---|
| 242 | ## DNSDB::finish()
|
---|
| 243 | # Cleans up after database handles and so on.
|
---|
| 244 | # Requires a database handle
|
---|
| 245 | sub finish {
|
---|
| 246 | my $dbh = $_[0];
|
---|
| 247 | $dbh->disconnect;
|
---|
| 248 | } # end finish
|
---|
| 249 |
|
---|
| 250 |
|
---|
| 251 | ## DNSDB::initGlobals()
|
---|
| 252 | # Initialize global variables
|
---|
| 253 | # NB: this does NOT include web-specific session variables!
|
---|
| 254 | # Requires a database handle
|
---|
| 255 | sub initGlobals {
|
---|
| 256 | my $dbh = shift;
|
---|
| 257 |
|
---|
| 258 | # load system-wide site defaults and things from config file
|
---|
[29] | 259 | if (open SYSDEFAULTS, "</etc/dnsdb.conf") {
|
---|
[2] | 260 | ##fixme - error check!
|
---|
[29] | 261 | while (<SYSDEFAULTS>) {
|
---|
| 262 | next if /^\s*#/;
|
---|
| 263 | $def{contact} = $1 if /contact ?= ?([a-z0-9_.-]+)/i;
|
---|
| 264 | $def{prins} = $1 if /prins ?= ?([a-z0-9_.-]+)/i;
|
---|
| 265 | $def{soattl} = $1 if /soattl ?= ?([a-z0-9_.-]+)/i;
|
---|
| 266 | $def{refresh} = $1 if /refresh ?= ?([a-z0-9_.-]+)/i;
|
---|
| 267 | $def{retry} = $1 if /retry ?= ?([a-z0-9_.-]+)/i;
|
---|
| 268 | $def{expire} = $1 if /expire ?= ?([a-z0-9_.-]+)/i;
|
---|
| 269 | $def{minttl} = $1 if /minttl ?= ?([a-z0-9_.-]+)/i;
|
---|
| 270 | $def{ttl} = $1 if /ttl ?= ?([a-z0-9_.-]+)/i;
|
---|
[2] | 271 | ##fixme? load DB user/pass from config file?
|
---|
[29] | 272 | }
|
---|
[2] | 273 | }
|
---|
| 274 | # load from database
|
---|
| 275 | my $sth = $dbh->prepare("select val,name from rectypes");
|
---|
| 276 | $sth->execute;
|
---|
| 277 | while (my ($recval,$recname) = $sth->fetchrow_array()) {
|
---|
| 278 | $typemap{$recval} = $recname;
|
---|
| 279 | $reverse_typemap{$recname} = $recval;
|
---|
| 280 | }
|
---|
| 281 | } # end initGlobals
|
---|
| 282 |
|
---|
| 283 |
|
---|
[65] | 284 | ## DNSDB::initPermissions()
|
---|
| 285 | # Set up permissions global
|
---|
| 286 | # Takes database handle and UID
|
---|
| 287 | sub initPermissions {
|
---|
| 288 | my $dbh = shift;
|
---|
| 289 | my $uid = shift;
|
---|
| 290 |
|
---|
| 291 | # %permissions = $(getPermissions($dbh,'user',$uid));
|
---|
| 292 | getPermissions($dbh, 'user', $uid, \%permissions);
|
---|
| 293 |
|
---|
| 294 | } # end initPermissions()
|
---|
| 295 |
|
---|
| 296 |
|
---|
| 297 | ## DNSDB::getPermissions()
|
---|
| 298 | # Get permissions from DB
|
---|
| 299 | # Requires DB handle, group or user flag, ID, and hashref.
|
---|
| 300 | sub getPermissions {
|
---|
| 301 | my $dbh = shift;
|
---|
| 302 | my $type = shift;
|
---|
| 303 | my $id = shift;
|
---|
| 304 | my $hash = shift;
|
---|
| 305 |
|
---|
| 306 | my $sql = qq(
|
---|
| 307 | SELECT
|
---|
| 308 | p.admin,p.self_edit,
|
---|
| 309 | p.group_create,p.group_edit,p.group_delete,
|
---|
| 310 | p.user_create,p.user_edit,p.user_delete,
|
---|
| 311 | p.domain_create,p.domain_edit,p.domain_delete,
|
---|
| 312 | p.record_create,p.record_edit,p.record_delete
|
---|
| 313 | FROM permissions p
|
---|
| 314 | );
|
---|
| 315 | if ($type eq 'group') {
|
---|
| 316 | $sql .= qq(
|
---|
| 317 | JOIN groups g ON g.permission_id=p.permission_id
|
---|
| 318 | WHERE g.group_id=?
|
---|
| 319 | );
|
---|
| 320 | } else {
|
---|
| 321 | $sql .= qq(
|
---|
| 322 | JOIN users u ON u.permission_id=p.permission_id
|
---|
| 323 | WHERE u.user_id=?
|
---|
| 324 | );
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | my $sth = $dbh->prepare($sql);
|
---|
| 328 |
|
---|
| 329 | $sth->execute($id) or die "argh: ".$sth->errstr;
|
---|
| 330 |
|
---|
| 331 | # my $permref = $sth->fetchrow_hashref;
|
---|
| 332 | # return $permref;
|
---|
| 333 | # $hash = $permref;
|
---|
| 334 | # Eww. Need to learn how to forcibly drop a hashref onto an existing hash.
|
---|
| 335 | ($hash->{admin},$hash->{self_edit},
|
---|
| 336 | $hash->{group_create},$hash->{group_edit},$hash->{group_delete},
|
---|
| 337 | $hash->{user_create},$hash->{user_edit},$hash->{user_delete},
|
---|
| 338 | $hash->{domain_create},$hash->{domain_edit},$hash->{domain_delete},
|
---|
| 339 | $hash->{record_create},$hash->{record_edit},$hash->{record_delete})
|
---|
| 340 | = $sth->fetchrow_array;
|
---|
| 341 |
|
---|
| 342 | } # end getPermissions()
|
---|
| 343 |
|
---|
| 344 |
|
---|
| 345 | ## DNSDB::changePermissions()
|
---|
| 346 | # Update an ACL entry
|
---|
| 347 | # Takes a db handle, type, owner-id, and hashref for the changed permissions.
|
---|
| 348 | sub changePermissions {
|
---|
| 349 | my $dbh = shift;
|
---|
| 350 | my $type = shift;
|
---|
| 351 | my $id = shift;
|
---|
| 352 | my $newperms = shift;
|
---|
[87] | 353 | my $inherit = shift || 0;
|
---|
[65] | 354 |
|
---|
[78] | 355 | my $failmsg = '';
|
---|
[66] | 356 |
|
---|
[87] | 357 | # see if we're switching from inherited to custom. for bonus points,
|
---|
| 358 | # snag the permid and parent permid anyway, since we'll need the permid
|
---|
| 359 | # to set/alter custom perms, and both if we're switching from custom to
|
---|
| 360 | # inherited.
|
---|
| 361 | my $sth = $dbh->prepare("SELECT (u.permission_id=g.permission_id) AS was_inherited,u.permission_id,g.permission_id".
|
---|
[65] | 362 | " FROM ".($type eq 'user' ? 'users' : 'groups')." u ".
|
---|
[66] | 363 | " JOIN groups g ON u.".($type eq 'user' ? '' : 'parent_')."group_id=g.group_id ".
|
---|
[65] | 364 | " WHERE u.".($type eq 'user' ? 'user' : 'group')."_id=?");
|
---|
| 365 | $sth->execute($id);
|
---|
| 366 |
|
---|
[87] | 367 | my ($wasinherited,$permid,$parpermid) = $sth->fetchrow_array;
|
---|
[66] | 368 |
|
---|
[78] | 369 | # hack phtoui
|
---|
| 370 | # group id 1 is "special" in that it's it's own parent (err... possibly.)
|
---|
| 371 | # may make its parent id 0 which doesn't exist, and as a bonus is Perl-false.
|
---|
| 372 | $wasinherited = 0 if ($type eq 'group' && $id == 1);
|
---|
| 373 |
|
---|
[66] | 374 | local $dbh->{AutoCommit} = 0;
|
---|
| 375 | local $dbh->{RaiseError} = 1;
|
---|
| 376 |
|
---|
| 377 | # Wrap all the SQL in a transaction
|
---|
| 378 | eval {
|
---|
[87] | 379 | if ($inherit) {
|
---|
| 380 |
|
---|
| 381 | $dbh->do("UPDATE ".($type eq 'user' ? 'users' : 'groups')." SET inherit_perm='t',permission_id=? ".
|
---|
| 382 | "WHERE ".($type eq 'user' ? 'user' : 'group')."_id=?", undef, ($parpermid, $id) );
|
---|
| 383 | $dbh->do("DELETE FROM permissions WHERE permission_id=?", undef, ($permid) );
|
---|
| 384 |
|
---|
| 385 | } else {
|
---|
| 386 |
|
---|
| 387 | if ($wasinherited) { # munge new permission entry in if we're switching from inherited perms
|
---|
[66] | 388 | ##fixme: need to add semirecursive bit to properly munge inherited permission ID on subgroups and users
|
---|
[87] | 389 | # ... if'n'when we have groups with fully inherited permissions.
|
---|
| 390 | # SQL is coo
|
---|
| 391 | $dbh->do("INSERT INTO permissions ($permlist,".($type eq 'user' ? 'user' : 'group')."_id) ".
|
---|
| 392 | "SELECT $permlist,? FROM permissions WHERE permission_id=?", undef, ($id,$permid) );
|
---|
| 393 | ($permid) = $dbh->selectrow_array("SELECT permission_id FROM permissions ".
|
---|
| 394 | "WHERE ".($type eq 'user' ? 'user' : 'group')."_id=?", undef, ($id) );
|
---|
| 395 | $dbh->do("UPDATE ".($type eq 'user' ? 'users' : 'groups')." SET inherit_perm='f',permission_id=? ".
|
---|
| 396 | "WHERE ".($type eq 'user' ? 'user' : 'group')."_id=?", undef, ($permid, $id) );
|
---|
[66] | 397 | }
|
---|
[78] | 398 |
|
---|
[87] | 399 | # and now set the permissions we were passed
|
---|
| 400 | foreach (@permtypes) {
|
---|
| 401 | if (defined ($newperms->{$_})) {
|
---|
| 402 | $dbh->do("UPDATE permissions SET $_=? WHERE permission_id=?", undef, ($newperms->{$_},$permid) );
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | } # (inherited->)? custom
|
---|
| 407 |
|
---|
[66] | 408 | $dbh->commit;
|
---|
| 409 | }; # end eval
|
---|
| 410 | if ($@) {
|
---|
| 411 | my $msg = $@;
|
---|
| 412 | eval { $dbh->rollback; };
|
---|
[87] | 413 | return ('FAIL',"$failmsg: $msg ($permid)");
|
---|
[66] | 414 | } else {
|
---|
| 415 | return ('OK',$permid);
|
---|
| 416 | }
|
---|
| 417 |
|
---|
[65] | 418 | } # end changePermissions()
|
---|
| 419 |
|
---|
| 420 |
|
---|
[67] | 421 | ## DNSDB::comparePermissions()
|
---|
| 422 | # Compare two permission hashes
|
---|
| 423 | # Returns '>', '<', '=', '!'
|
---|
| 424 | sub comparePermissions {
|
---|
| 425 | my $p1 = shift;
|
---|
| 426 | my $p2 = shift;
|
---|
| 427 |
|
---|
| 428 | my $retval = '='; # assume equality until proven otherwise
|
---|
| 429 |
|
---|
| 430 | no warnings "uninitialized";
|
---|
| 431 |
|
---|
| 432 | foreach (@permtypes) {
|
---|
| 433 | next if $p1->{$_} == $p2->{$_}; # equal is good
|
---|
| 434 | if ($p1->{$_} && !$p2->{$_}) {
|
---|
| 435 | if ($retval eq '<') { # if we've already found an unequal pair where
|
---|
| 436 | $retval = '!'; # $p2 has more access, and we now find a pair
|
---|
| 437 | last; # where $p1 has more access, the overall access
|
---|
| 438 | } # is neither greater or lesser, it's unequal.
|
---|
| 439 | $retval = '>';
|
---|
| 440 | }
|
---|
| 441 | if (!$p1->{$_} && $p2->{$_}) {
|
---|
| 442 | if ($retval eq '>') { # if we've already found an unequal pair where
|
---|
| 443 | $retval = '!'; # $p1 has more access, and we now find a pair
|
---|
| 444 | last; # where $p2 has more access, the overall access
|
---|
| 445 | } # is neither greater or lesser, it's unequal.
|
---|
| 446 | $retval = '<';
|
---|
| 447 | }
|
---|
| 448 | }
|
---|
| 449 | return $retval;
|
---|
| 450 | } # end comparePermissions()
|
---|
| 451 |
|
---|
| 452 |
|
---|
[112] | 453 | ## DNSDB::changeGroup()
|
---|
| 454 | # Change group ID of an entity
|
---|
| 455 | # Takes a database handle, entity type, entity ID, and new group ID
|
---|
| 456 | sub changeGroup {
|
---|
| 457 | my $dbh = shift;
|
---|
| 458 | my $type = shift;
|
---|
| 459 | my $id = shift;
|
---|
| 460 | my $newgrp = shift;
|
---|
| 461 |
|
---|
| 462 | ##fixme: fail on not enough args
|
---|
| 463 | #return ('FAIL', "Missing
|
---|
| 464 |
|
---|
| 465 | if ($type eq 'domain') {
|
---|
| 466 | $dbh->do("UPDATE domains SET group_id=? WHERE domain_id=?", undef, ($newgrp, $id))
|
---|
| 467 | or return ('FAIL','Group change failed: '.$dbh->errstr);
|
---|
| 468 | } elsif ($type eq 'user') {
|
---|
| 469 | $dbh->do("UPDATE users SET group_id=? WHERE user_id=?", undef, ($newgrp, $id))
|
---|
| 470 | or return ('FAIL','Group change failed: '.$dbh->errstr);
|
---|
| 471 | } elsif ($type eq 'group') {
|
---|
| 472 | $dbh->do("UPDATE groups SET parent_group_id=? WHERE group_id=?", undef, ($newgrp, $id))
|
---|
| 473 | or return ('FAIL','Group change failed: '.$dbh->errstr);
|
---|
| 474 | }
|
---|
| 475 | return ('OK','OK');
|
---|
| 476 | } # end changeGroup()
|
---|
| 477 |
|
---|
| 478 |
|
---|
[55] | 479 | ## DNSDB::_log()
|
---|
| 480 | # Log an action
|
---|
| 481 | # Internal sub
|
---|
| 482 | # Takes a database handle, <foo>, <bar>
|
---|
| 483 | sub _log {
|
---|
| 484 | } # end _log
|
---|
| 485 |
|
---|
| 486 |
|
---|
[2] | 487 | ##
|
---|
| 488 | ## Processing subs
|
---|
| 489 | ##
|
---|
| 490 |
|
---|
| 491 | ## DNSDB::addDomain()
|
---|
| 492 | # Add a domain
|
---|
| 493 | # Takes a database handle, domain name, numeric group, and boolean(ish) state (active/inactive)
|
---|
| 494 | # Returns a status code and message
|
---|
| 495 | sub addDomain {
|
---|
| 496 | $errstr = '';
|
---|
| 497 | my $dbh = shift;
|
---|
| 498 | return ('FAIL',"Need database handle") if !$dbh;
|
---|
| 499 | my $domain = shift;
|
---|
[91] | 500 | return ('FAIL',"Domain must not be blank") if !$domain;
|
---|
[2] | 501 | my $group = shift;
|
---|
| 502 | return ('FAIL',"Need group") if !defined($group);
|
---|
| 503 | my $state = shift;
|
---|
| 504 | return ('FAIL',"Need domain status") if !defined($state);
|
---|
| 505 |
|
---|
[116] | 506 | $state = 1 if $state =~ /^active$/;
|
---|
| 507 | $state = 1 if $state =~ /^on$/;
|
---|
| 508 | $state = 0 if $state =~ /^inactive$/;
|
---|
| 509 | $state = 0 if $state =~ /^off$/;
|
---|
| 510 |
|
---|
| 511 | return ('FAIL',"Invalid domain status") if $state !~ /^\d+$/;
|
---|
| 512 |
|
---|
[38] | 513 | my $sth = $dbh->prepare("SELECT domain_id FROM domains WHERE domain=?");
|
---|
[3] | 514 | my $dom_id;
|
---|
| 515 |
|
---|
[38] | 516 | # quick check to start to see if we've already got one
|
---|
| 517 | $sth->execute($domain);
|
---|
| 518 | ($dom_id) = $sth->fetchrow_array;
|
---|
| 519 |
|
---|
| 520 | return ('FAIL', "Domain already exists") if $dom_id;
|
---|
| 521 |
|
---|
[2] | 522 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 523 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 524 | local $dbh->{AutoCommit} = 0;
|
---|
| 525 | local $dbh->{RaiseError} = 1;
|
---|
| 526 |
|
---|
| 527 | # Wrap all the SQL in a transaction
|
---|
| 528 | eval {
|
---|
| 529 | # insert the domain...
|
---|
| 530 | my $sth = $dbh->prepare("insert into domains (domain,group_id,status) values (?,?,?)");
|
---|
| 531 | $sth->execute($domain,$group,$state);
|
---|
| 532 |
|
---|
| 533 | # get the ID...
|
---|
| 534 | $sth = $dbh->prepare("select domain_id from domains where domain='$domain'");
|
---|
| 535 | $sth->execute;
|
---|
[3] | 536 | ($dom_id) = $sth->fetchrow_array();
|
---|
[2] | 537 |
|
---|
| 538 | # ... and now we construct the standard records from the default set. NB: group should be variable.
|
---|
[3] | 539 | $sth = $dbh->prepare("select host,type,val,distance,weight,port,ttl from default_records where group_id=$group");
|
---|
| 540 | my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,weight,port,ttl)".
|
---|
| 541 | " values ($dom_id,?,?,?,?,?,?,?)");
|
---|
[2] | 542 | $sth->execute;
|
---|
[3] | 543 | while (my ($host,$type,$val,$dist,$weight,$port,$ttl) = $sth->fetchrow_array()) {
|
---|
[2] | 544 | $host =~ s/DOMAIN/$domain/g;
|
---|
[37] | 545 | $val =~ s/DOMAIN/$domain/g;
|
---|
[3] | 546 | $sth_in->execute($host,$type,$val,$dist,$weight,$port,$ttl);
|
---|
[2] | 547 | }
|
---|
| 548 |
|
---|
| 549 | # once we get here, we should have suceeded.
|
---|
| 550 | $dbh->commit;
|
---|
| 551 | }; # end eval
|
---|
| 552 |
|
---|
| 553 | if ($@) {
|
---|
| 554 | my $msg = $@;
|
---|
| 555 | eval { $dbh->rollback; };
|
---|
| 556 | return ('FAIL',$msg);
|
---|
| 557 | } else {
|
---|
[3] | 558 | return ('OK',$dom_id);
|
---|
[2] | 559 | }
|
---|
| 560 | } # end addDomain
|
---|
| 561 |
|
---|
| 562 |
|
---|
[3] | 563 | ## DNSDB::delDomain()
|
---|
| 564 | # Delete a domain.
|
---|
| 565 | # for now, just delete the records, then the domain.
|
---|
| 566 | # later we may want to archive it in some way instead (status code 2, for example?)
|
---|
| 567 | sub delDomain {
|
---|
| 568 | my $dbh = shift;
|
---|
[5] | 569 | my $domid = shift;
|
---|
[3] | 570 |
|
---|
| 571 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 572 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 573 | local $dbh->{AutoCommit} = 0;
|
---|
| 574 | local $dbh->{RaiseError} = 1;
|
---|
| 575 |
|
---|
[23] | 576 | my $failmsg = '';
|
---|
| 577 |
|
---|
[3] | 578 | # Wrap all the SQL in a transaction
|
---|
| 579 | eval {
|
---|
[5] | 580 | my $sth = $dbh->prepare("delete from records where domain_id=?");
|
---|
[23] | 581 | $failmsg = "Failure removing domain records";
|
---|
[5] | 582 | $sth->execute($domid);
|
---|
| 583 | $sth = $dbh->prepare("delete from domains where domain_id=?");
|
---|
[23] | 584 | $failmsg = "Failure removing domain";
|
---|
[5] | 585 | $sth->execute($domid);
|
---|
[3] | 586 |
|
---|
| 587 | # once we get here, we should have suceeded.
|
---|
[23] | 588 | $dbh->commit;
|
---|
[3] | 589 | }; # end eval
|
---|
| 590 |
|
---|
| 591 | if ($@) {
|
---|
| 592 | my $msg = $@;
|
---|
| 593 | eval { $dbh->rollback; };
|
---|
[23] | 594 | return ('FAIL',"$failmsg: $msg");
|
---|
[3] | 595 | } else {
|
---|
| 596 | return ('OK','OK');
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | } # end delDomain()
|
---|
| 600 |
|
---|
| 601 |
|
---|
[2] | 602 | ## DNSDB::domainName()
|
---|
| 603 | # Return the domain name based on a domain ID
|
---|
| 604 | # Takes a database handle and the domain ID
|
---|
| 605 | # Returns the domain name or undef on failure
|
---|
| 606 | sub domainName {
|
---|
| 607 | $errstr = '';
|
---|
| 608 | my $dbh = shift;
|
---|
| 609 | my $domid = shift;
|
---|
[91] | 610 | my ($domname) = $dbh->selectrow_array("SELECT domain FROM domains WHERE domain_id=?", undef, ($domid) );
|
---|
[2] | 611 | $errstr = $DBI::errstr if !$domname;
|
---|
| 612 | return $domname if $domname;
|
---|
[91] | 613 | } # end domainName()
|
---|
[2] | 614 |
|
---|
| 615 |
|
---|
[91] | 616 | ## DNSDB::domainID()
|
---|
| 617 | # Takes a database handle and domain name
|
---|
| 618 | # Returns the domain ID number
|
---|
| 619 | sub domainID {
|
---|
| 620 | $errstr = '';
|
---|
| 621 | my $dbh = shift;
|
---|
| 622 | my $domain = shift;
|
---|
| 623 | my ($domid) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE domain=?", undef, ($domain) );
|
---|
| 624 | $errstr = $DBI::errstr if !$domid;
|
---|
| 625 | return $domid if $domid;
|
---|
| 626 | } # end domainID()
|
---|
| 627 |
|
---|
| 628 |
|
---|
[18] | 629 | ## DNSDB::addGroup()
|
---|
| 630 | # Add a group
|
---|
[66] | 631 | # Takes a database handle, group name, parent group, hashref for permissions,
|
---|
| 632 | # and optional template-vs-cloneme flag
|
---|
[18] | 633 | # Returns a status code and message
|
---|
| 634 | sub addGroup {
|
---|
| 635 | $errstr = '';
|
---|
| 636 | my $dbh = shift;
|
---|
[20] | 637 | my $groupname = shift;
|
---|
| 638 | my $pargroup = shift;
|
---|
[66] | 639 | my $permissions = shift;
|
---|
[18] | 640 |
|
---|
[66] | 641 | # 0 indicates "custom", hardcoded.
|
---|
[18] | 642 | # Any other value clones that group's default records, if it exists.
|
---|
[66] | 643 | my $inherit = shift || 0;
|
---|
| 644 | ##fixme: need a flag to indicate clone records or <?> ?
|
---|
[18] | 645 |
|
---|
| 646 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 647 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 648 | local $dbh->{AutoCommit} = 0;
|
---|
| 649 | local $dbh->{RaiseError} = 1;
|
---|
| 650 |
|
---|
[38] | 651 | my $sth = $dbh->prepare("SELECT group_id FROM groups WHERE group_name=?");
|
---|
| 652 | my $group_id;
|
---|
| 653 |
|
---|
| 654 | # quick check to start to see if we've already got one
|
---|
| 655 | $sth->execute($groupname);
|
---|
| 656 | ($group_id) = $sth->fetchrow_array;
|
---|
| 657 |
|
---|
| 658 | return ('FAIL', "Group already exists") if $group_id;
|
---|
| 659 |
|
---|
[18] | 660 | # Wrap all the SQL in a transaction
|
---|
| 661 | eval {
|
---|
[38] | 662 | $sth = $dbh->prepare("INSERT INTO groups (parent_group_id,group_name) VALUES (?,?)");
|
---|
[20] | 663 | $sth->execute($pargroup,$groupname);
|
---|
[18] | 664 |
|
---|
| 665 | $sth = $dbh->prepare("SELECT group_id FROM groups WHERE group_name=?");
|
---|
[20] | 666 | $sth->execute($groupname);
|
---|
| 667 | my ($groupid) = $sth->fetchrow_array();
|
---|
[18] | 668 |
|
---|
[66] | 669 | # Permissions
|
---|
| 670 | if ($inherit) {
|
---|
| 671 | } else {
|
---|
| 672 | my @permvals;
|
---|
| 673 | foreach (@permtypes) {
|
---|
| 674 | if (!defined ($permissions->{$_})) {
|
---|
| 675 | push @permvals, 0;
|
---|
| 676 | } else {
|
---|
| 677 | push @permvals, $permissions->{$_};
|
---|
| 678 | }
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | $sth = $dbh->prepare("INSERT INTO permissions (group_id,$permlist) values (?".',?'x($#permtypes+1).")");
|
---|
| 682 | $sth->execute($groupid,@permvals);
|
---|
| 683 |
|
---|
| 684 | $sth = $dbh->prepare("SELECT permission_id FROM permissions WHERE group_id=?");
|
---|
| 685 | $sth->execute($groupid);
|
---|
| 686 | my ($permid) = $sth->fetchrow_array();
|
---|
| 687 |
|
---|
| 688 | $dbh->do("UPDATE groups SET permission_id=$permid WHERE group_id=$groupid");
|
---|
| 689 | } # done permission fiddling
|
---|
| 690 |
|
---|
| 691 | # Default records
|
---|
[18] | 692 | $sth = $dbh->prepare("INSERT INTO default_records (group_id,host,type,val,distance,weight,port,ttl) ".
|
---|
[20] | 693 | "VALUES ($groupid,?,?,?,?,?,?,?)");
|
---|
[66] | 694 | if ($inherit) {
|
---|
[87] | 695 | # Duplicate records from parent. Actually relying on inherited records feels
|
---|
| 696 | # very fragile, and it would be problematic to roll over at a later time.
|
---|
[18] | 697 | my $sth2 = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM default_records WHERE group_id=?");
|
---|
[87] | 698 | $sth2->execute($pargroup);
|
---|
[18] | 699 | while (my @clonedata = $sth2->fetchrow_array) {
|
---|
| 700 | $sth->execute(@clonedata);
|
---|
| 701 | }
|
---|
| 702 | } else {
|
---|
[66] | 703 | ##fixme: Hardcoding is Bad, mmmmkaaaay?
|
---|
[18] | 704 | # reasonable basic defaults for SOA, MX, NS, and minimal hosting
|
---|
| 705 | # could load from a config file, but somewhere along the line we need hardcoded bits.
|
---|
| 706 | $sth->execute('ns1.example.com:hostmaster.example.com', 6, '10800:3600:604800:10800', 0, 0, 0, 86400);
|
---|
| 707 | $sth->execute('DOMAIN', 1, '192.168.4.2', 0, 0, 0, 7200);
|
---|
| 708 | $sth->execute('DOMAIN', 15, 'mx.example.com', 10, 0, 0, 7200);
|
---|
| 709 | $sth->execute('DOMAIN', 2, 'ns1.example.com', 0, 0, 0, 7200);
|
---|
| 710 | $sth->execute('DOMAIN', 2, 'ns2.example.com', 0, 0, 0, 7200);
|
---|
| 711 | $sth->execute('www.DOMAIN', 5, 'DOMAIN', 0, 0, 0, 7200);
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | # once we get here, we should have suceeded.
|
---|
| 715 | $dbh->commit;
|
---|
| 716 | }; # end eval
|
---|
| 717 |
|
---|
| 718 | if ($@) {
|
---|
| 719 | my $msg = $@;
|
---|
| 720 | eval { $dbh->rollback; };
|
---|
| 721 | return ('FAIL',$msg);
|
---|
| 722 | } else {
|
---|
| 723 | return ('OK','OK');
|
---|
| 724 | }
|
---|
| 725 |
|
---|
| 726 | } # end addGroup()
|
---|
| 727 |
|
---|
| 728 |
|
---|
[22] | 729 | ## DNSDB::delGroup()
|
---|
| 730 | # Delete a group.
|
---|
| 731 | # Takes a group ID
|
---|
| 732 | # Returns a status code and message
|
---|
| 733 | sub delGroup {
|
---|
| 734 | my $dbh = shift;
|
---|
| 735 | my $groupid = shift;
|
---|
| 736 |
|
---|
| 737 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 738 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 739 | local $dbh->{AutoCommit} = 0;
|
---|
| 740 | local $dbh->{RaiseError} = 1;
|
---|
| 741 |
|
---|
| 742 | ##fixme: locate "knowable" error conditions and deal with them before the eval
|
---|
[23] | 743 | # ... or inside, whatever.
|
---|
[22] | 744 | # -> domains still exist in group
|
---|
| 745 | # -> ...
|
---|
[23] | 746 | my $failmsg = '';
|
---|
[22] | 747 |
|
---|
| 748 | # Wrap all the SQL in a transaction
|
---|
| 749 | eval {
|
---|
[23] | 750 | my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?");
|
---|
[22] | 751 | $sth->execute($groupid);
|
---|
[23] | 752 | my ($domcnt) = $sth->fetchrow_array;
|
---|
| 753 | $failmsg = "Can't remove group ".groupName($dbh,$groupid);
|
---|
| 754 | die "$domcnt domains still in group\n" if $domcnt;
|
---|
| 755 |
|
---|
| 756 | $sth = $dbh->prepare("delete from default_records where group_id=?");
|
---|
| 757 | $failmsg = "Failed to delete default records for ".groupName($dbh,$groupid);
|
---|
| 758 | $sth->execute($groupid);
|
---|
[22] | 759 | $sth = $dbh->prepare("delete from groups where group_id=?");
|
---|
[23] | 760 | $failmsg = "Failed to remove group ".groupName($dbh,$groupid);
|
---|
[22] | 761 | $sth->execute($groupid);
|
---|
| 762 |
|
---|
| 763 | # once we get here, we should have suceeded.
|
---|
| 764 | $dbh->commit;
|
---|
| 765 | }; # end eval
|
---|
| 766 |
|
---|
| 767 | if ($@) {
|
---|
| 768 | my $msg = $@;
|
---|
| 769 | eval { $dbh->rollback; };
|
---|
[23] | 770 | return ('FAIL',"$failmsg: $msg");
|
---|
[22] | 771 | } else {
|
---|
| 772 | return ('OK','OK');
|
---|
| 773 | }
|
---|
| 774 | } # end delGroup()
|
---|
| 775 |
|
---|
| 776 |
|
---|
[19] | 777 | ## DNSDB::getChildren()
|
---|
| 778 | # Get a list of all groups whose parent^n is group <n>
|
---|
[24] | 779 | # Takes a database handle, group ID, reference to an array to put the group IDs in,
|
---|
| 780 | # and an optional flag to return only immediate children or all children-of-children
|
---|
| 781 | # default to returning all children
|
---|
[19] | 782 | # Calls itself
|
---|
| 783 | sub getChildren {
|
---|
| 784 | $errstr = '';
|
---|
| 785 | my $dbh = shift;
|
---|
[20] | 786 | my $rootgroup = shift;
|
---|
| 787 | my $groupdest = shift;
|
---|
[24] | 788 | my $immed = shift || 'all';
|
---|
[19] | 789 |
|
---|
| 790 | # special break for default group; otherwise we get stuck.
|
---|
[20] | 791 | if ($rootgroup == 1) {
|
---|
[19] | 792 | # by definition, group 1 is the Root Of All Groups
|
---|
[24] | 793 | my $sth = $dbh->prepare("SELECT group_id FROM groups WHERE NOT (group_id=1)".
|
---|
| 794 | ($immed ne 'all' ? " AND parent_group_id=1" : ''));
|
---|
[19] | 795 | $sth->execute;
|
---|
| 796 | while (my @this = $sth->fetchrow_array) {
|
---|
[20] | 797 | push @$groupdest, @this;
|
---|
[19] | 798 | }
|
---|
| 799 | } else {
|
---|
| 800 | my $sth = $dbh->prepare("SELECT group_id FROM groups WHERE parent_group_id=?");
|
---|
[20] | 801 | $sth->execute($rootgroup);
|
---|
[19] | 802 | return if $sth->rows == 0;
|
---|
[20] | 803 | my @grouplist;
|
---|
| 804 | while (my ($group) = $sth->fetchrow_array) {
|
---|
| 805 | push @$groupdest, $group;
|
---|
[24] | 806 | getChildren($dbh,$group,$groupdest) if $immed eq 'all';
|
---|
[19] | 807 | }
|
---|
| 808 | }
|
---|
| 809 | } # end getChildren()
|
---|
| 810 |
|
---|
| 811 |
|
---|
[20] | 812 | ## DNSDB::groupName()
|
---|
[17] | 813 | # Return the group name based on a group ID
|
---|
| 814 | # Takes a database handle and the group ID
|
---|
| 815 | # Returns the group name or undef on failure
|
---|
[20] | 816 | sub groupName {
|
---|
[13] | 817 | $errstr = '';
|
---|
| 818 | my $dbh = shift;
|
---|
[20] | 819 | my $groupid = shift;
|
---|
| 820 | my $sth = $dbh->prepare("SELECT group_name FROM groups WHERE group_id=?");
|
---|
| 821 | $sth->execute($groupid);
|
---|
| 822 | my ($groupname) = $sth->fetchrow_array();
|
---|
| 823 | $errstr = $DBI::errstr if !$groupname;
|
---|
| 824 | return $groupname if $groupname;
|
---|
| 825 | } # end groupName
|
---|
[13] | 826 |
|
---|
| 827 |
|
---|
[118] | 828 | ## DNSDB::groupID()
|
---|
| 829 | # Return the group ID based on the group name
|
---|
| 830 | # Takes a database handle and the group name
|
---|
| 831 | # Returns the group ID or undef on failure
|
---|
| 832 | sub groupID {
|
---|
| 833 | $errstr = '';
|
---|
| 834 | my $dbh = shift;
|
---|
| 835 | my $group = shift;
|
---|
| 836 | my ($grpid) = $dbh->selectrow_array("SELECT group_id FROM groups WHERE group=?", undef, ($group) );
|
---|
| 837 | $errstr = $DBI::errstr if !$grpid;
|
---|
| 838 | return $grpid if $grpid;
|
---|
| 839 | } # end groupID()
|
---|
| 840 |
|
---|
| 841 |
|
---|
[24] | 842 | ## DNSDB::addUser()
|
---|
[87] | 843 | # Add a user.
|
---|
| 844 | # Takes a DB handle, username, group ID, password, state (active/inactive).
|
---|
| 845 | # Optionally accepts:
|
---|
| 846 | # user type (user/admin) - defaults to user
|
---|
| 847 | # permissions string - defaults to inherit from group
|
---|
| 848 | # three valid forms:
|
---|
| 849 | # i - Inherit permissions
|
---|
| 850 | # c:<user_id> - Clone permissions from <user_id>
|
---|
| 851 | # C:<permission list> - Set these specific permissions
|
---|
| 852 | # first name - defaults to username
|
---|
| 853 | # last name - defaults to blank
|
---|
| 854 | # phone - defaults to blank (could put other data within column def)
|
---|
[90] | 855 | # Returns (OK,<uid>) on success, (FAIL,<message>) on failure
|
---|
[24] | 856 | sub addUser {
|
---|
| 857 | $errstr = '';
|
---|
| 858 | my $dbh = shift;
|
---|
| 859 | my $username = shift;
|
---|
| 860 | my $group = shift;
|
---|
| 861 | my $pass = shift;
|
---|
| 862 | my $state = shift;
|
---|
[25] | 863 |
|
---|
[90] | 864 | return ('FAIL', "Missing one or more required entries") if !defined($state);
|
---|
| 865 | return ('FAIL', "Username must not be blank") if !$username;
|
---|
[87] | 866 |
|
---|
[25] | 867 | my $type = shift || 'u'; # create limited users by default - fwiw, not sure yet how this will interact with ACLs
|
---|
| 868 |
|
---|
[67] | 869 | my $permstring = shift || 'i'; # default is to inhert permissions from group
|
---|
| 870 |
|
---|
[25] | 871 | my $fname = shift || $username;
|
---|
[24] | 872 | my $lname = shift || '';
|
---|
[25] | 873 | my $phone = shift || ''; # not going format-check
|
---|
[24] | 874 |
|
---|
[38] | 875 | my $sth = $dbh->prepare("SELECT user_id FROM users WHERE username=?");
|
---|
[24] | 876 | my $user_id;
|
---|
| 877 |
|
---|
[38] | 878 | # quick check to start to see if we've already got one
|
---|
| 879 | $sth->execute($username);
|
---|
| 880 | ($user_id) = $sth->fetchrow_array;
|
---|
| 881 |
|
---|
| 882 | return ('FAIL', "User already exists") if $user_id;
|
---|
| 883 |
|
---|
[24] | 884 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 885 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 886 | local $dbh->{AutoCommit} = 0;
|
---|
| 887 | local $dbh->{RaiseError} = 1;
|
---|
| 888 |
|
---|
[94] | 889 | my $failmsg = '';
|
---|
| 890 |
|
---|
[24] | 891 | # Wrap all the SQL in a transaction
|
---|
| 892 | eval {
|
---|
[87] | 893 | # insert the user... note we set inherited perms by default since
|
---|
| 894 | # it's simple and cleans up some other bits of state
|
---|
| 895 | my $sth = $dbh->prepare("INSERT INTO users ".
|
---|
| 896 | "(group_id,username,password,firstname,lastname,phone,type,status,permission_id,inherit_perm) ".
|
---|
| 897 | "VALUES (?,?,?,?,?,?,?,?,(SELECT permission_id FROM permissions WHERE group_id=?),'t')");
|
---|
| 898 | $sth->execute($group,$username,unix_md5_crypt($pass),$fname,$lname,$phone,$type,$state,$group);
|
---|
[24] | 899 |
|
---|
| 900 | # get the ID...
|
---|
[94] | 901 | ($user_id) = $dbh->selectrow_array("SELECT currval('users_user_id_seq')");
|
---|
[24] | 902 |
|
---|
[87] | 903 | # Permissions! Gotta set'em all!
|
---|
| 904 | die "Invalid permission string $permstring"
|
---|
| 905 | if $permstring !~ /^(?:
|
---|
| 906 | i # inherit
|
---|
| 907 | |c:\d+ # clone
|
---|
| 908 | # custom. no, the leading , is not a typo
|
---|
[111] | 909 | |C:(?:,(?:group|user|domain|record|self)_(?:edit|create|delete))*
|
---|
[87] | 910 | )$/x;
|
---|
| 911 | # bleh. I'd call another function to do my dirty work, but we're in the middle of a transaction already.
|
---|
| 912 | if ($permstring ne 'i') {
|
---|
| 913 | # for cloned or custom permissions, we have to create a new permissions entry.
|
---|
| 914 | my $clonesrc = $group;
|
---|
| 915 | if ($permstring =~ /^c:(\d+)/) { $clonesrc = $1; }
|
---|
| 916 | $dbh->do("INSERT INTO permissions ($permlist,user_id) ".
|
---|
| 917 | "SELECT $permlist,? FROM permissions WHERE permission_id=".
|
---|
| 918 | "(SELECT permission_id FROM permissions WHERE ".($permstring =~ /^c:/ ? 'user' : 'group')."_id=?)",
|
---|
| 919 | undef, ($user_id,$clonesrc) );
|
---|
| 920 | $dbh->do("UPDATE users SET permission_id=".
|
---|
| 921 | "(SELECT permission_id FROM permissions WHERE user_id=?) ".
|
---|
| 922 | "WHERE user_id=?", undef, ($user_id, $user_id) );
|
---|
| 923 | }
|
---|
| 924 | if ($permstring =~ /^C:/) {
|
---|
| 925 | # finally for custom permissions, we set the passed-in permissions (and unset
|
---|
| 926 | # any that might have been brought in by the clone operation above)
|
---|
| 927 | my ($permid) = $dbh->selectrow_array("SELECT permission_id FROM permissions WHERE user_id=?",
|
---|
| 928 | undef, ($user_id) );
|
---|
| 929 | foreach (@permtypes) {
|
---|
| 930 | if ($permstring =~ /,$_/) {
|
---|
| 931 | $dbh->do("UPDATE permissions SET $_='t' WHERE permission_id=?", undef, ($permid) );
|
---|
| 932 | } else {
|
---|
| 933 | $dbh->do("UPDATE permissions SET $_='f' WHERE permission_id=?", undef, ($permid) );
|
---|
| 934 | }
|
---|
| 935 | }
|
---|
| 936 | }
|
---|
| 937 |
|
---|
| 938 | $dbh->do("UPDATE users SET inherit_perm='n' WHERE user_id=?", undef, ($user_id) );
|
---|
| 939 |
|
---|
[25] | 940 | ##fixme: add another table to hold name/email for log table?
|
---|
| 941 |
|
---|
[24] | 942 | # once we get here, we should have suceeded.
|
---|
| 943 | $dbh->commit;
|
---|
| 944 | }; # end eval
|
---|
| 945 |
|
---|
| 946 | if ($@) {
|
---|
| 947 | my $msg = $@;
|
---|
| 948 | eval { $dbh->rollback; };
|
---|
[87] | 949 | return ('FAIL',$msg." $failmsg");
|
---|
[24] | 950 | } else {
|
---|
| 951 | return ('OK',$user_id);
|
---|
| 952 | }
|
---|
| 953 | } # end addUser
|
---|
| 954 |
|
---|
| 955 |
|
---|
[55] | 956 | ## DNSDB::checkUser()
|
---|
| 957 | # Check user/pass combo on login
|
---|
| 958 | sub checkUser {
|
---|
| 959 | my $dbh = shift;
|
---|
| 960 | my $user = shift;
|
---|
[56] | 961 | my $inpass = shift;
|
---|
[55] | 962 |
|
---|
| 963 | my $sth = $dbh->prepare("SELECT user_id,group_id,password,firstname,lastname FROM users WHERE username=?");
|
---|
| 964 | $sth->execute($user);
|
---|
| 965 | my ($uid,$gid,$pass,$fname,$lname) = $sth->fetchrow_array;
|
---|
| 966 | my $loginfailed = 1 if !defined($uid);
|
---|
| 967 |
|
---|
| 968 | if ($pass =~ m|^\$1\$([A-Za-z0-9/.]+)\$|) {
|
---|
[56] | 969 | $loginfailed = 1 if $pass ne unix_md5_crypt($inpass,$1);
|
---|
[55] | 970 | } else {
|
---|
[56] | 971 | $loginfailed = 1 if $pass ne $inpass;
|
---|
[55] | 972 | }
|
---|
| 973 |
|
---|
| 974 | # nnnngggg
|
---|
| 975 | return ($uid, $gid);
|
---|
| 976 | } # end checkUser
|
---|
| 977 |
|
---|
| 978 |
|
---|
[83] | 979 | ## DNSDB:: updateUser()
|
---|
[90] | 980 | # Update general data about user
|
---|
[83] | 981 | sub updateUser {
|
---|
| 982 | my $dbh = shift;
|
---|
[118] | 983 |
|
---|
| 984 | ##fixme: tweak calling convention so that we can update any given bit of data
|
---|
[83] | 985 | my $uid = shift;
|
---|
| 986 | my $username = shift;
|
---|
| 987 | my $group = shift;
|
---|
| 988 | my $pass = shift;
|
---|
| 989 | my $state = shift;
|
---|
[87] | 990 | my $type = shift || 'u';
|
---|
[83] | 991 | my $fname = shift || $username;
|
---|
| 992 | my $lname = shift || '';
|
---|
| 993 | my $phone = shift || ''; # not going format-check
|
---|
| 994 |
|
---|
| 995 | my $failmsg = '';
|
---|
| 996 |
|
---|
| 997 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 998 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 999 | local $dbh->{AutoCommit} = 0;
|
---|
| 1000 | local $dbh->{RaiseError} = 1;
|
---|
| 1001 |
|
---|
| 1002 | my $sth;
|
---|
| 1003 |
|
---|
| 1004 | # Password can be left blank; if so we assume there's one on file.
|
---|
| 1005 | # Actual blank passwords are bad, mm'kay?
|
---|
| 1006 | if (!$pass) {
|
---|
| 1007 | $sth = $dbh->prepare("SELECT password FROM users WHERE user_id=?");
|
---|
| 1008 | $sth->execute($uid);
|
---|
| 1009 | ($pass) = $sth->fetchrow_array;
|
---|
| 1010 | } else {
|
---|
| 1011 | $pass = unix_md5_crypt($pass);
|
---|
| 1012 | }
|
---|
| 1013 |
|
---|
| 1014 | eval {
|
---|
| 1015 | my $sth = $dbh->prepare(q(
|
---|
| 1016 | UPDATE users
|
---|
| 1017 | SET username=?, password=?, firstname=?, lastname=?, phone=?, type=?, status=?
|
---|
| 1018 | WHERE user_id=?
|
---|
| 1019 | )
|
---|
| 1020 | );
|
---|
| 1021 | $sth->execute($username, $pass, $fname, $lname, $phone, $type, $state, $uid);
|
---|
| 1022 | $dbh->commit;
|
---|
| 1023 | };
|
---|
| 1024 | if ($@) {
|
---|
| 1025 | my $msg = $@;
|
---|
| 1026 | eval { $dbh->rollback; };
|
---|
| 1027 | return ('FAIL',"$failmsg: $msg");
|
---|
| 1028 | } else {
|
---|
| 1029 | return ('OK','OK');
|
---|
| 1030 | }
|
---|
| 1031 | } # end updateUser()
|
---|
| 1032 |
|
---|
| 1033 |
|
---|
[24] | 1034 | ## DNSDB::delUser()
|
---|
| 1035 | #
|
---|
| 1036 | sub delUser {
|
---|
[25] | 1037 | my $dbh = shift;
|
---|
| 1038 | return ('FAIL',"Need database handle") if !$dbh;
|
---|
| 1039 | my $userid = shift;
|
---|
| 1040 | return ('FAIL',"Missing userid") if !defined($userid);
|
---|
| 1041 |
|
---|
| 1042 | my $sth = $dbh->prepare("delete from users where user_id=?");
|
---|
| 1043 | $sth->execute($userid);
|
---|
| 1044 |
|
---|
| 1045 | return ('FAIL',"Couldn't remove user: ".$sth->errstr) if $sth->err;
|
---|
| 1046 |
|
---|
| 1047 | return ('OK','OK');
|
---|
| 1048 |
|
---|
[24] | 1049 | } # end delUser
|
---|
| 1050 |
|
---|
| 1051 |
|
---|
[25] | 1052 | ## DNSDB::userFullName()
|
---|
| 1053 | # Return a pretty string!
|
---|
| 1054 | # Takes a user_id and optional printf-ish string to indicate which pieces where:
|
---|
| 1055 | # %u for the username
|
---|
| 1056 | # %f for the first name
|
---|
| 1057 | # %l for the last name
|
---|
| 1058 | # All other text in the passed string will be left as-is.
|
---|
| 1059 | ##fixme: need a "smart" option too, so that missing/null/blank first/last names don't give funky output
|
---|
| 1060 | sub userFullName {
|
---|
| 1061 | $errstr = '';
|
---|
| 1062 | my $dbh = shift;
|
---|
| 1063 | my $userid = shift;
|
---|
| 1064 | my $fullformat = shift || '%f %l (%u)';
|
---|
| 1065 | my $sth = $dbh->prepare("select username,firstname,lastname from users where user_id=?");
|
---|
| 1066 | $sth->execute($userid);
|
---|
| 1067 | my ($uname,$fname,$lname) = $sth->fetchrow_array();
|
---|
| 1068 | $errstr = $DBI::errstr if !$uname;
|
---|
| 1069 |
|
---|
| 1070 | $fullformat =~ s/\%u/$uname/g;
|
---|
| 1071 | $fullformat =~ s/\%f/$fname/g;
|
---|
| 1072 | $fullformat =~ s/\%l/$lname/g;
|
---|
| 1073 |
|
---|
| 1074 | return $fullformat;
|
---|
| 1075 | } # end userFullName
|
---|
| 1076 |
|
---|
| 1077 |
|
---|
[51] | 1078 | ## DNSDB::userStatus()
|
---|
| 1079 | # Sets and/or returns a user's status
|
---|
| 1080 | # Takes a database handle, user ID and optionally a status argument
|
---|
| 1081 | # Returns undef on errors.
|
---|
| 1082 | sub userStatus {
|
---|
| 1083 | my $dbh = shift;
|
---|
| 1084 | my $id = shift;
|
---|
| 1085 | my $newstatus = shift;
|
---|
| 1086 |
|
---|
| 1087 | return undef if $id !~ /^\d+$/;
|
---|
| 1088 |
|
---|
| 1089 | my $sth;
|
---|
| 1090 |
|
---|
| 1091 | # ooo, fun! let's see what we were passed for status
|
---|
| 1092 | if ($newstatus) {
|
---|
| 1093 | $sth = $dbh->prepare("update users set status=? where user_id=?");
|
---|
| 1094 | # ass-u-me caller knows what's going on in full
|
---|
| 1095 | if ($newstatus =~ /^[01]$/) { # only two valid for now.
|
---|
| 1096 | $sth->execute($newstatus,$id);
|
---|
| 1097 | } elsif ($newstatus =~ /^usero(?:n|ff)$/) {
|
---|
| 1098 | $sth->execute(($newstatus eq 'useron' ? 1 : 0),$id);
|
---|
| 1099 | }
|
---|
| 1100 | }
|
---|
| 1101 |
|
---|
| 1102 | $sth = $dbh->prepare("select status from users where user_id=?");
|
---|
| 1103 | $sth->execute($id);
|
---|
| 1104 | my ($status) = $sth->fetchrow_array;
|
---|
| 1105 | return $status;
|
---|
| 1106 | } # end userStatus()
|
---|
| 1107 |
|
---|
| 1108 |
|
---|
[83] | 1109 | ## DNSDB::getUserData()
|
---|
| 1110 | # Get misc user data for display
|
---|
| 1111 | sub getUserData {
|
---|
| 1112 | my $dbh = shift;
|
---|
| 1113 | my $uid = shift;
|
---|
| 1114 |
|
---|
| 1115 | my $sth = $dbh->prepare("SELECT group_id,username,firstname,lastname,phone,type,status,inherit_perm ".
|
---|
| 1116 | "FROM users WHERE user_id=?");
|
---|
| 1117 | $sth->execute($uid);
|
---|
| 1118 | return $sth->fetchrow_hashref();
|
---|
| 1119 |
|
---|
| 1120 | } # end getUserData()
|
---|
| 1121 |
|
---|
| 1122 |
|
---|
[2] | 1123 | ## DNSDB::getSOA()
|
---|
| 1124 | # Return all suitable fields from an SOA record in separate elements of a hash
|
---|
| 1125 | # Takes a database handle, default/live flag, and group (default) or domain (live) ID
|
---|
| 1126 | sub getSOA {
|
---|
| 1127 | $errstr = '';
|
---|
| 1128 | my $dbh = shift;
|
---|
| 1129 | my $def = shift;
|
---|
| 1130 | my $id = shift;
|
---|
| 1131 | my %ret;
|
---|
| 1132 |
|
---|
[101] | 1133 | # (ab)use distance and weight columns to store SOA data
|
---|
| 1134 |
|
---|
| 1135 | my $sql = "SELECT record_id,host,val,ttl,distance from";
|
---|
[2] | 1136 | if ($def eq 'def' or $def eq 'y') {
|
---|
[101] | 1137 | $sql .= " default_records WHERE group_id=? AND type=$reverse_typemap{SOA}";
|
---|
[2] | 1138 | } else {
|
---|
| 1139 | # we're editing a live SOA record; find based on domain
|
---|
[101] | 1140 | $sql .= " records WHERE domain_id=? AND type=$reverse_typemap{SOA}";
|
---|
[2] | 1141 | }
|
---|
| 1142 | my $sth = $dbh->prepare($sql);
|
---|
[101] | 1143 | $sth->execute($id);
|
---|
[2] | 1144 |
|
---|
[121] | 1145 | my ($recid,$host,$val,$ttl,$serial) = $sth->fetchrow_array() or return;
|
---|
[2] | 1146 | my ($prins,$contact) = split /:/, $host;
|
---|
| 1147 | my ($refresh,$retry,$expire,$minttl) = split /:/, $val;
|
---|
| 1148 |
|
---|
| 1149 | $ret{recid} = $recid;
|
---|
| 1150 | $ret{ttl} = $ttl;
|
---|
[101] | 1151 | $ret{serial} = $serial;
|
---|
[2] | 1152 | $ret{prins} = $prins;
|
---|
| 1153 | $ret{contact} = $contact;
|
---|
| 1154 | $ret{refresh} = $refresh;
|
---|
| 1155 | $ret{retry} = $retry;
|
---|
| 1156 | $ret{expire} = $expire;
|
---|
| 1157 | $ret{minttl} = $minttl;
|
---|
| 1158 |
|
---|
| 1159 | return %ret;
|
---|
| 1160 | } # end getSOA()
|
---|
| 1161 |
|
---|
| 1162 |
|
---|
| 1163 | ## DNSDB::getRecLine()
|
---|
| 1164 | # Return all data fields for a zone record in separate elements of a hash
|
---|
| 1165 | # Takes a database handle, default/live flag, and record ID
|
---|
| 1166 | sub getRecLine {
|
---|
| 1167 | $errstr = '';
|
---|
| 1168 | my $dbh = shift;
|
---|
| 1169 | my $def = shift;
|
---|
| 1170 | my $id = shift;
|
---|
| 1171 |
|
---|
[90] | 1172 | my $sql = "SELECT r.record_id,r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl,r.longrec_id,l.recdata".
|
---|
| 1173 | (($def eq 'def' or $def eq 'y') ? ',r.group_id FROM default_' : ',r.domain_id FROM ').
|
---|
| 1174 | "records r LEFT OUTER JOIN longrecs l ON r.longrec_id=l.longrec_id WHERE record_id=?";
|
---|
[123] | 1175 | my $ret = $dbh->selectrow_hashref($sql, undef, ($id) );
|
---|
[2] | 1176 |
|
---|
[90] | 1177 | if ($dbh->err) {
|
---|
[2] | 1178 | $errstr = $DBI::errstr;
|
---|
| 1179 | return undef;
|
---|
| 1180 | }
|
---|
| 1181 |
|
---|
[123] | 1182 | if (!$ret) {
|
---|
| 1183 | $errstr = "No such record";
|
---|
| 1184 | return undef;
|
---|
| 1185 | }
|
---|
| 1186 |
|
---|
[90] | 1187 | $ret->{val} = $ret->{recdata} if $ret->{longrec_id}; # put the long data in the real value space
|
---|
| 1188 | delete $ret->{longrec_id}; # remove these since they shouldn't be exposed - the caller
|
---|
| 1189 | delete $ret->{recdata}; # should not care about "long records" vs normal ones.
|
---|
[107] | 1190 | $ret->{parid} = (($def eq 'def' or $def eq 'y') ? $ret->{group_id} : $ret->{domain_id});
|
---|
[90] | 1191 |
|
---|
| 1192 | return $ret;
|
---|
[2] | 1193 | }
|
---|
| 1194 |
|
---|
| 1195 |
|
---|
| 1196 | ##fixme: should use above (getRecLine()) to get lines for below?
|
---|
| 1197 | ## DNSDB::getDomRecs()
|
---|
| 1198 | # Return records for a domain
|
---|
| 1199 | # Takes a database handle, default/live flag, group/domain ID, start,
|
---|
| 1200 | # number of records, sort field, and sort order
|
---|
| 1201 | # Returns a reference to an array of hashes
|
---|
| 1202 | sub getDomRecs {
|
---|
| 1203 | $errstr = '';
|
---|
| 1204 | my $dbh = shift;
|
---|
| 1205 | my $type = shift;
|
---|
| 1206 | my $id = shift;
|
---|
[4] | 1207 | my $nrecs = shift || 'all';
|
---|
| 1208 | my $nstart = shift || 0;
|
---|
[2] | 1209 |
|
---|
[4] | 1210 | ## for order, need to map input to column names
|
---|
| 1211 | my $order = shift || 'host';
|
---|
[72] | 1212 | my $direction = shift || 'ASC';
|
---|
[4] | 1213 |
|
---|
[90] | 1214 | $type = 'y' if $type eq 'def';
|
---|
| 1215 |
|
---|
| 1216 | my $sql = "SELECT r.record_id,r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl,r.longrec_id,l.recdata FROM ";
|
---|
| 1217 | $sql .= "default_" if $type eq 'y';
|
---|
| 1218 | $sql .= "records r ";
|
---|
[104] | 1219 | $sql .= "INNER JOIN rectypes t ON r.type=t.val "; # for sorting by type alphabetically
|
---|
[90] | 1220 | $sql .= "LEFT OUTER JOIN longrecs l ON r.longrec_id=l.longrec_id ";
|
---|
| 1221 | if ($type eq 'y') {
|
---|
| 1222 | $sql .= "WHERE r.group_id=?";
|
---|
[2] | 1223 | } else {
|
---|
[90] | 1224 | $sql .= "WHERE r.domain_id=?";
|
---|
[2] | 1225 | }
|
---|
[104] | 1226 | $sql .= " AND NOT r.type=$reverse_typemap{SOA}";
|
---|
| 1227 | # use alphaorder column for "correct" ordering of sort-by-type instead of DNS RR type number
|
---|
| 1228 | $sql .= " ORDER BY ".($order eq 'type' ? 't.alphaorder' : "r.$order")." $direction";
|
---|
[90] | 1229 | $sql .= " LIMIT $nrecs OFFSET ".($nstart*$nrecs) if $nstart ne 'all';
|
---|
[4] | 1230 |
|
---|
[90] | 1231 | my $sth = $dbh->prepare($sql) or warn $dbh->errstr;
|
---|
| 1232 | $sth->execute($id) or warn "$sql: ".$sth->errstr;
|
---|
[2] | 1233 |
|
---|
| 1234 | my @retbase;
|
---|
| 1235 | while (my $ref = $sth->fetchrow_hashref()) {
|
---|
[90] | 1236 | $ref->{val} = $ref->{recdata} if $ref->{longrec_id}; # put the long data in the real value space
|
---|
| 1237 | delete $ref->{longrec_id}; # remove these since they shouldn't be exposed - the caller
|
---|
| 1238 | delete $ref->{recdata}; # should not care about "long records" vs normal ones.
|
---|
[2] | 1239 | push @retbase, $ref;
|
---|
| 1240 | }
|
---|
| 1241 |
|
---|
| 1242 | my $ret = \@retbase;
|
---|
| 1243 | return $ret;
|
---|
| 1244 | } # end getDomRecs()
|
---|
| 1245 |
|
---|
| 1246 |
|
---|
[91] | 1247 | ## DNSDB::getRecCount()
|
---|
| 1248 | # Return count of non-SOA records in domain (or default records in a group)
|
---|
| 1249 | # Takes a database handle, default/live flag and group/domain ID
|
---|
| 1250 | # Returns the count
|
---|
| 1251 | sub getRecCount {
|
---|
| 1252 | my $dbh = shift;
|
---|
| 1253 | my $defrec = shift;
|
---|
| 1254 | my $id = shift;
|
---|
| 1255 |
|
---|
| 1256 | my ($count) = $dbh->selectrow_array("SELECT count(*) FROM ".
|
---|
| 1257 | ($defrec eq 'y' ? 'default_' : '')."records ".
|
---|
| 1258 | "WHERE ".($defrec eq 'y' ? 'group' : 'domain')."_id=? ".
|
---|
| 1259 | "AND NOT type=$reverse_typemap{SOA}", undef, ($id) );
|
---|
| 1260 |
|
---|
| 1261 | return $count;
|
---|
| 1262 |
|
---|
| 1263 | } # end getRecCount()
|
---|
| 1264 |
|
---|
| 1265 |
|
---|
[3] | 1266 | ## DNSDB::addRec()
|
---|
[2] | 1267 | # Add a new record to a domain or a group's default records
|
---|
| 1268 | # Takes a database handle, default/live flag, group/domain ID,
|
---|
| 1269 | # host, type, value, and TTL
|
---|
| 1270 | # Some types require additional detail: "distance" for MX and SRV,
|
---|
| 1271 | # and weight/port for SRV
|
---|
| 1272 | # Returns a status code and detail message in case of error
|
---|
| 1273 | sub addRec {
|
---|
| 1274 | $errstr = '';
|
---|
| 1275 | my $dbh = shift;
|
---|
| 1276 | my $defrec = shift;
|
---|
| 1277 | my $id = shift;
|
---|
| 1278 |
|
---|
| 1279 | my $host = shift;
|
---|
| 1280 | my $rectype = shift;
|
---|
| 1281 | my $val = shift;
|
---|
| 1282 | my $ttl = shift;
|
---|
| 1283 |
|
---|
[129] | 1284 | # Validation
|
---|
| 1285 | if ($rectype == $reverse_typemap{A} or $rectype == $reverse_typemap{AAAA}) {
|
---|
| 1286 | my $tmpip = new NetAddr::IP $val or
|
---|
| 1287 | return ("FAIL", "Address must be a valid IP address");
|
---|
| 1288 | }
|
---|
| 1289 |
|
---|
[2] | 1290 | my $fields = ($defrec eq 'y' ? 'group_id' : 'domain_id').",host,type,val,ttl";
|
---|
[24] | 1291 | my $vallen = "?,?,?,?,?";
|
---|
| 1292 | my @vallist = ($id,$host,$rectype,$val,$ttl);
|
---|
[2] | 1293 |
|
---|
| 1294 | my $dist;
|
---|
| 1295 | if ($rectype == $reverse_typemap{MX} or $rectype == $reverse_typemap{SRV}) {
|
---|
| 1296 | $dist = shift;
|
---|
| 1297 | return ('FAIL',"Need distance for $typemap{$rectype} record") if !defined($dist);
|
---|
| 1298 | $fields .= ",distance";
|
---|
[24] | 1299 | $vallen .= ",?";
|
---|
| 1300 | push @vallist, $dist;
|
---|
[2] | 1301 | }
|
---|
| 1302 | my $weight;
|
---|
| 1303 | my $port;
|
---|
| 1304 | if ($rectype == $reverse_typemap{SRV}) {
|
---|
[24] | 1305 | # check for _service._protocol. NB: RFC2782 does not say "MUST"... nor "SHOULD"...
|
---|
| 1306 | # it just says (paraphrased) "... is prepended with _ to prevent DNS collisions"
|
---|
| 1307 | return ('FAIL',"SRV records must begin with _service._protocol")
|
---|
| 1308 | if $host !~ /^_[A-Za-z]+\._[A-Za-z]+\.[a-z0-9-]+/;
|
---|
[2] | 1309 | $weight = shift;
|
---|
| 1310 | $port = shift;
|
---|
| 1311 | return ('FAIL',"Need weight and port for SRV record") if !defined($weight) or !defined($port);
|
---|
| 1312 | $fields .= ",weight,port";
|
---|
[24] | 1313 | $vallen .= ",?,?";
|
---|
| 1314 | push @vallist, ($weight,$port);
|
---|
[2] | 1315 | }
|
---|
| 1316 |
|
---|
[90] | 1317 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 1318 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 1319 | local $dbh->{AutoCommit} = 0;
|
---|
| 1320 | local $dbh->{RaiseError} = 1;
|
---|
[2] | 1321 |
|
---|
[90] | 1322 | eval {
|
---|
| 1323 | if (length($val) > 100 ) {
|
---|
| 1324 | # extralong records get an entry in a separate table.
|
---|
| 1325 | $dbh->do("INSERT INTO longrecs (recdata) VALUES (?)", undef, ($val) );
|
---|
| 1326 | my ($longid) = $dbh->selectrow_array("SELECT longrec_id FROM longrecs WHERE recdata=?", undef, ($val) );
|
---|
| 1327 | $fields .= ",longrec_id";
|
---|
| 1328 | $vallen .= ",?";
|
---|
| 1329 | push @vallist, $longid;
|
---|
| 1330 | $vallist[3] = ''; # so we don't barf when we insert the main record
|
---|
| 1331 | }
|
---|
| 1332 | $dbh->do("INSERT INTO ".($defrec eq 'y' ? 'default_' : '')."records ($fields) VALUES ($vallen)",
|
---|
| 1333 | undef, @vallist);
|
---|
| 1334 | $dbh->commit;
|
---|
| 1335 | };
|
---|
| 1336 | if ($@) {
|
---|
| 1337 | my $msg = $@;
|
---|
| 1338 | eval { $dbh->rollback; };
|
---|
| 1339 | return ('FAIL',$msg);
|
---|
| 1340 | }
|
---|
[2] | 1341 |
|
---|
| 1342 | return ('OK','OK');
|
---|
[90] | 1343 |
|
---|
[2] | 1344 | } # end addRec()
|
---|
| 1345 |
|
---|
| 1346 |
|
---|
[16] | 1347 | ## DNSDB::updateRec()
|
---|
| 1348 | # Update a record
|
---|
| 1349 | sub updateRec {
|
---|
| 1350 | $errstr = '';
|
---|
[17] | 1351 |
|
---|
[16] | 1352 | my $dbh = shift;
|
---|
| 1353 | my $defrec = shift;
|
---|
| 1354 | my $id = shift;
|
---|
| 1355 |
|
---|
| 1356 | # all records have these
|
---|
| 1357 | my $host = shift;
|
---|
| 1358 | my $type = shift;
|
---|
| 1359 | my $val = shift;
|
---|
| 1360 | my $ttl = shift;
|
---|
| 1361 |
|
---|
| 1362 | return('FAIL',"Missing standard argument(s)") if !defined($ttl);
|
---|
| 1363 |
|
---|
| 1364 | # only MX and SRV will use these
|
---|
| 1365 | my $dist = 0;
|
---|
| 1366 | my $weight = 0;
|
---|
| 1367 | my $port = 0;
|
---|
| 1368 |
|
---|
| 1369 | if ($type == $reverse_typemap{MX} || $type == $reverse_typemap{SRV}) {
|
---|
[17] | 1370 | $dist = shift;
|
---|
| 1371 | return ('FAIL',"MX or SRV requires distance") if !defined($dist);
|
---|
[16] | 1372 | if ($type == $reverse_typemap{SRV}) {
|
---|
[17] | 1373 | $weight = shift;
|
---|
| 1374 | return ('FAIL',"SRV requires weight") if !defined($weight);
|
---|
| 1375 | $port = shift;
|
---|
| 1376 | return ('FAIL',"SRV requires port") if !defined($port);
|
---|
[16] | 1377 | }
|
---|
| 1378 | }
|
---|
| 1379 |
|
---|
[90] | 1380 | # my $sql = "SELECT r.record_id,r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl,r.longrec_id,l.recdata FROM ";
|
---|
| 1381 | # $sql .= "default_" if $type eq 'y';
|
---|
| 1382 | # $sql .= "records r ";
|
---|
| 1383 | # $sql .= "LEFT OUTER JOIN longrecs l ON r.longrec_id=l.longrec_id ";
|
---|
[16] | 1384 |
|
---|
[90] | 1385 | # get the long record ID, if any
|
---|
| 1386 | my ($longid) = $dbh->selectrow_array("SELECT longrec_id FROM ".($defrec eq 'y' ? 'default_' : '')."records ".
|
---|
| 1387 | "WHERE record_id=?", undef, ($id) );
|
---|
[16] | 1388 |
|
---|
[90] | 1389 | local $dbh->{AutoCommit} = 0;
|
---|
| 1390 | local $dbh->{RaiseError} = 1;
|
---|
| 1391 |
|
---|
| 1392 | eval {
|
---|
| 1393 | # there's really no tidy way to squash this down. :/
|
---|
| 1394 | if (length($val) > 100) {
|
---|
| 1395 | if ($longid) {
|
---|
| 1396 | $dbh->do("UPDATE longrecs SET recdata=? WHERE longrec_id=?", undef, ($val, $longid) );
|
---|
| 1397 | } else {
|
---|
| 1398 | ##fixme: has to be a better way to be sure we get the right recid back once inserted...
|
---|
| 1399 | $dbh->do("INSERT INTO longrecs (recdata) VALUES (?)", undef, ($val) );
|
---|
| 1400 | my ($newlongid) = $dbh->selectrow_array("SELECT currval('longrecs_longrec_id_seq')");
|
---|
| 1401 | $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records SET val=?,longrec_id=? ".
|
---|
| 1402 | "WHERE record_id=?", undef, ('', $newlongid, $id) );
|
---|
| 1403 | }
|
---|
| 1404 | } else {
|
---|
| 1405 | if ($longid) {
|
---|
| 1406 | $dbh->do("DELETE FROM longrecs WHERE longrec_id=?", undef, ($longid) );
|
---|
| 1407 | $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records SET val=?,longrec_id=NULL ".
|
---|
| 1408 | "WHERE record_id=?", undef, ($val, $id) );
|
---|
| 1409 | } else {
|
---|
| 1410 | $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records SET val=? ".
|
---|
| 1411 | "WHERE record_id=?", undef, ($val, $id) );
|
---|
| 1412 | }
|
---|
| 1413 | }
|
---|
| 1414 |
|
---|
| 1415 | $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records ".
|
---|
| 1416 | "SET host=?,type=?,ttl=?,distance=?,weight=?,port=? ".
|
---|
| 1417 | "WHERE record_id=?", undef, ($host, $type, $ttl, $dist, $weight, $port, $id) );
|
---|
| 1418 |
|
---|
| 1419 | };
|
---|
| 1420 | if ($@) {
|
---|
| 1421 | my $msg = $@;
|
---|
| 1422 | $dbh->rollback;
|
---|
| 1423 | return ('FAIL', $msg);
|
---|
| 1424 | }
|
---|
| 1425 | # return ('FAIL',$sth->errstr."<br>\n$errstr<br>\n") if $sth->err;
|
---|
| 1426 |
|
---|
[16] | 1427 | return ('OK','OK');
|
---|
| 1428 | } # end updateRec()
|
---|
| 1429 |
|
---|
| 1430 |
|
---|
[3] | 1431 | ## DNSDB::delRec()
|
---|
| 1432 | # Delete a record.
|
---|
| 1433 | sub delRec {
|
---|
| 1434 | $errstr = '';
|
---|
| 1435 | my $dbh = shift;
|
---|
| 1436 | my $defrec = shift;
|
---|
| 1437 | my $id = shift;
|
---|
| 1438 |
|
---|
[62] | 1439 | my $sth = $dbh->prepare("DELETE FROM ".($defrec eq 'y' ? 'default_' : '')."records WHERE record_id=?");
|
---|
[3] | 1440 | $sth->execute($id);
|
---|
| 1441 |
|
---|
[23] | 1442 | return ('FAIL',"Couldn't remove record: ".$sth->errstr) if $sth->err;
|
---|
[3] | 1443 |
|
---|
| 1444 | return ('OK','OK');
|
---|
| 1445 | } # end delRec()
|
---|
| 1446 |
|
---|
| 1447 |
|
---|
[117] | 1448 | # Reference hashes.
|
---|
| 1449 | my %par_tbl = (
|
---|
| 1450 | group => 'groups',
|
---|
| 1451 | user => 'users',
|
---|
| 1452 | defrec => 'default_records',
|
---|
| 1453 | domain => 'domains',
|
---|
| 1454 | record => 'records'
|
---|
| 1455 | );
|
---|
| 1456 | my %id_col = (
|
---|
| 1457 | group => 'group_id',
|
---|
| 1458 | user => 'user_id',
|
---|
| 1459 | defrec => 'record_id',
|
---|
| 1460 | domain => 'domain_id',
|
---|
| 1461 | record => 'record_id'
|
---|
| 1462 | );
|
---|
| 1463 | my %par_col = (
|
---|
| 1464 | group => 'parent_group_id',
|
---|
| 1465 | user => 'group_id',
|
---|
| 1466 | defrec => 'group_id',
|
---|
| 1467 | domain => 'group_id',
|
---|
| 1468 | record => 'domain_id'
|
---|
| 1469 | );
|
---|
| 1470 | my %par_type = (
|
---|
| 1471 | group => 'group',
|
---|
| 1472 | user => 'group',
|
---|
| 1473 | defrec => 'group',
|
---|
| 1474 | domain => 'group',
|
---|
| 1475 | record => 'domain'
|
---|
| 1476 | );
|
---|
| 1477 |
|
---|
[116] | 1478 | ## DNSDB::getParents()
|
---|
| 1479 | # Find out which entities are parent to the requested id
|
---|
| 1480 | # Returns arrayref containing hash pairs of id/type
|
---|
| 1481 | sub getParents {
|
---|
| 1482 | my $dbh = shift;
|
---|
| 1483 | my $id = shift;
|
---|
| 1484 | my $type = shift;
|
---|
[117] | 1485 | my $depth = shift || 'all'; # valid values: 'all', 'immed', <int> (stop at this group ID)
|
---|
[116] | 1486 |
|
---|
[117] | 1487 | my @parlist;
|
---|
[116] | 1488 |
|
---|
[117] | 1489 | while (1) {
|
---|
| 1490 | my $result = $dbh->selectrow_hashref("SELECT $par_col{$type} FROM $par_tbl{$type} WHERE $id_col{$type} = ?",
|
---|
| 1491 | undef, ($id) );
|
---|
| 1492 | unshift @parlist, ($result->{$par_col{$type}} => $par_type{$type});
|
---|
| 1493 | last if $result->{$par_col{$type}} == 1; # group 1 is its own parent
|
---|
| 1494 | $type = $par_type{$type};
|
---|
| 1495 | $id = $result->{$par_col{$type}};
|
---|
[116] | 1496 | }
|
---|
| 1497 |
|
---|
[117] | 1498 | return \@parlist;
|
---|
[116] | 1499 |
|
---|
| 1500 | } # end getParents()
|
---|
| 1501 |
|
---|
| 1502 |
|
---|
[117] | 1503 | ## DNSDB::isParent()
|
---|
| 1504 | # Returns true if $id1 is a parent of $id2, false otherwise
|
---|
| 1505 | sub isParent {
|
---|
| 1506 | my $dbh = shift;
|
---|
| 1507 | my $id1 = shift;
|
---|
| 1508 | my $type1 = shift;
|
---|
| 1509 | my $id2 = shift;
|
---|
| 1510 | my $type2 = shift;
|
---|
| 1511 | ##todo: immediate, secondary, full (default)
|
---|
| 1512 |
|
---|
| 1513 | # Return false on impossible relations
|
---|
| 1514 | return 0 if $type1 eq 'record'; # nothing may be a child of a record
|
---|
| 1515 | return 0 if $type1 eq 'defrec'; # nothing may be a child of a record
|
---|
| 1516 | return 0 if $type1 eq 'user'; # nothing may be child of a user
|
---|
| 1517 | return 0 if $type1 eq 'domain' && $type2 ne 'record'; # domain may not be a parent of anything other than a record
|
---|
| 1518 |
|
---|
| 1519 | # group 1 is the ultimate root parent
|
---|
| 1520 | return 1 if $type1 eq 'group' && $id1 == 1;
|
---|
| 1521 |
|
---|
| 1522 | # almost the same loop as getParents() above
|
---|
| 1523 | my $id = $id2;
|
---|
| 1524 | my $type = $type2;
|
---|
| 1525 | my $foundparent = 0;
|
---|
| 1526 | my $tmp = 0;
|
---|
| 1527 | while (1) {
|
---|
| 1528 | my $sql = "SELECT $par_col{$type} FROM $par_tbl{$type} WHERE $id_col{$type} = ?";
|
---|
| 1529 | my $result = $dbh->selectrow_hashref($sql,
|
---|
| 1530 | undef, ($id) ) or warn $dbh->errstr." $sql";
|
---|
| 1531 | if ($result->{$par_col{$type}} == $id1) {
|
---|
| 1532 | $foundparent = 1;
|
---|
| 1533 | last;
|
---|
| 1534 | }
|
---|
| 1535 | # group 1 is its own parent. need this here more to break strange loops than for detecting a parent
|
---|
| 1536 | last if $result->{$par_col{$type}} == 1;
|
---|
| 1537 | $type = $par_type{$type};
|
---|
| 1538 | $id = $result->{$par_col{$type}};
|
---|
| 1539 | last if $tmp++ > 10;
|
---|
| 1540 | }
|
---|
| 1541 |
|
---|
| 1542 | return $foundparent;
|
---|
| 1543 | } # end isParent()
|
---|
| 1544 |
|
---|
| 1545 |
|
---|
[3] | 1546 | ## DNSDB::domStatus()
|
---|
| 1547 | # Sets and/or returns a domain's status
|
---|
| 1548 | # Takes a database handle, domain ID and optionally a status argument
|
---|
| 1549 | # Returns undef on errors.
|
---|
| 1550 | sub domStatus {
|
---|
| 1551 | my $dbh = shift;
|
---|
| 1552 | my $id = shift;
|
---|
| 1553 | my $newstatus = shift;
|
---|
| 1554 |
|
---|
| 1555 | return undef if $id !~ /^\d+$/;
|
---|
| 1556 |
|
---|
| 1557 | my $sth;
|
---|
| 1558 |
|
---|
| 1559 | # ooo, fun! let's see what we were passed for status
|
---|
| 1560 | if ($newstatus) {
|
---|
| 1561 | $sth = $dbh->prepare("update domains set status=? where domain_id=?");
|
---|
| 1562 | # ass-u-me caller knows what's going on in full
|
---|
| 1563 | if ($newstatus =~ /^[01]$/) { # only two valid for now.
|
---|
| 1564 | $sth->execute($newstatus,$id);
|
---|
| 1565 | } elsif ($newstatus =~ /^domo(?:n|ff)$/) {
|
---|
| 1566 | $sth->execute(($newstatus eq 'domon' ? 1 : 0),$id);
|
---|
| 1567 | }
|
---|
| 1568 | }
|
---|
| 1569 |
|
---|
| 1570 | $sth = $dbh->prepare("select status from domains where domain_id=?");
|
---|
| 1571 | $sth->execute($id);
|
---|
| 1572 | my ($status) = $sth->fetchrow_array;
|
---|
| 1573 | return $status;
|
---|
| 1574 | } # end domStatus()
|
---|
| 1575 |
|
---|
| 1576 |
|
---|
[33] | 1577 | ## DNSDB::importAXFR
|
---|
| 1578 | # Import a domain via AXFR
|
---|
[37] | 1579 | # Takes AXFR host, domain to transfer, group to put the domain in,
|
---|
| 1580 | # and optionally:
|
---|
| 1581 | # - active/inactive state flag (defaults to active)
|
---|
| 1582 | # - overwrite-SOA flag (defaults to off)
|
---|
| 1583 | # - overwrite-NS flag (defaults to off, doesn't affect subdomain NS records)
|
---|
| 1584 | # Returns a status code (OK, WARN, or FAIL) and message - message should be blank
|
---|
| 1585 | # if status is OK, but WARN includes conditions that are not fatal but should
|
---|
| 1586 | # really be reported.
|
---|
[33] | 1587 | sub importAXFR {
|
---|
| 1588 | my $dbh = shift;
|
---|
[35] | 1589 | my $ifrom_in = shift;
|
---|
[33] | 1590 | my $domain = shift;
|
---|
| 1591 | my $group = shift;
|
---|
| 1592 | my $status = shift || 1;
|
---|
| 1593 | my $rwsoa = shift || 0;
|
---|
| 1594 | my $rwns = shift || 0;
|
---|
[37] | 1595 |
|
---|
[33] | 1596 | ##fixme: add mode to delete&replace, merge+overwrite, merge new?
|
---|
| 1597 |
|
---|
[37] | 1598 | my $nrecs = 0;
|
---|
| 1599 | my $soaflag = 0;
|
---|
| 1600 | my $nsflag = 0;
|
---|
| 1601 | my $warnmsg = '';
|
---|
| 1602 | my $ifrom;
|
---|
[33] | 1603 |
|
---|
[35] | 1604 | # choke on possible bad setting in ifrom
|
---|
[37] | 1605 | # IPv4 and v6, and valid hostnames!
|
---|
[35] | 1606 | ($ifrom) = ($ifrom_in =~ /^([0-9a-f\:.]+|[0-9a-z_.-]+)$/i);
|
---|
| 1607 | return ('FAIL', "Bad AXFR source host $ifrom")
|
---|
| 1608 | unless ($ifrom) = ($ifrom_in =~ /^([0-9a-f\:.]+|[0-9a-z_.-]+)$/i);
|
---|
| 1609 |
|
---|
[33] | 1610 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 1611 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 1612 | local $dbh->{AutoCommit} = 0;
|
---|
| 1613 | local $dbh->{RaiseError} = 1;
|
---|
| 1614 |
|
---|
[37] | 1615 | my $sth = $dbh->prepare("SELECT domain_id FROM domains WHERE domain=?");
|
---|
[34] | 1616 | my $dom_id;
|
---|
| 1617 |
|
---|
[35] | 1618 | # quick check to start to see if we've already got one
|
---|
[37] | 1619 | $sth->execute($domain);
|
---|
| 1620 | ($dom_id) = $sth->fetchrow_array;
|
---|
[35] | 1621 |
|
---|
| 1622 | return ('FAIL', "Domain already exists") if $dom_id;
|
---|
| 1623 |
|
---|
[33] | 1624 | eval {
|
---|
| 1625 | # can't do this, can't nest transactions. sigh.
|
---|
[35] | 1626 | #my ($dcode, $dmsg) = addDomain(dbh, domain, group, status);
|
---|
[33] | 1627 |
|
---|
| 1628 | ##fixme: serial
|
---|
[37] | 1629 | my $sth = $dbh->prepare("INSERT INTO domains (domain,group_id,status) VALUES (?,?,?)");
|
---|
| 1630 | $sth->execute($domain,$group,$status);
|
---|
[33] | 1631 |
|
---|
[35] | 1632 | ## bizarre DBI<->Net::DNS interaction bug:
|
---|
| 1633 | ## sometimes a zone will cause an immediate commit-and-exit (sort of) of the while()
|
---|
[37] | 1634 | ## fixed, apparently I was doing *something* odd, but not certain what it was that
|
---|
| 1635 | ## caused a commit instead of barfing
|
---|
[35] | 1636 |
|
---|
[33] | 1637 | # get domain id so we can do the records
|
---|
[37] | 1638 | $sth = $dbh->prepare("SELECT domain_id FROM domains WHERE domain=?");
|
---|
| 1639 | $sth->execute($domain);
|
---|
| 1640 | ($dom_id) = $sth->fetchrow_array();
|
---|
[33] | 1641 |
|
---|
[34] | 1642 | my $res = Net::DNS::Resolver->new;
|
---|
[35] | 1643 | $res->nameservers($ifrom);
|
---|
| 1644 | $res->axfr_start($domain)
|
---|
| 1645 | or die "Couldn't begin AXFR\n";
|
---|
[34] | 1646 |
|
---|
[35] | 1647 | while (my $rr = $res->axfr_next()) {
|
---|
[33] | 1648 | my $type = $rr->type;
|
---|
[35] | 1649 |
|
---|
[34] | 1650 | my $sql = "INSERT INTO records (domain_id,host,type,ttl,val";
|
---|
[33] | 1651 | my $vallen = "?,?,?,?,?";
|
---|
| 1652 |
|
---|
[37] | 1653 | $soaflag = 1 if $type eq 'SOA';
|
---|
| 1654 | $nsflag = 1 if $type eq 'NS';
|
---|
[35] | 1655 |
|
---|
| 1656 | my @vallist = ($dom_id, $rr->name, $reverse_typemap{$type}, $rr->ttl);
|
---|
[34] | 1657 |
|
---|
| 1658 | # "Primary" types:
|
---|
| 1659 | # A, NS, CNAME, SOA, PTR(warn in forward), MX, TXT, AAAA, SRV, A6(ob), SPF
|
---|
| 1660 | # maybe KEY
|
---|
| 1661 |
|
---|
[35] | 1662 | # nasty big ugly case-like thing here, since we have to do *some* different
|
---|
| 1663 | # processing depending on the record. le sigh.
|
---|
| 1664 |
|
---|
[105] | 1665 | ##fixme: what record types other than TXT can/will have >255-byte payloads?
|
---|
| 1666 |
|
---|
[34] | 1667 | if ($type eq 'A') {
|
---|
| 1668 | push @vallist, $rr->address;
|
---|
| 1669 | } elsif ($type eq 'NS') {
|
---|
[37] | 1670 | # hmm. should we warn here if subdomain NS'es are left alone?
|
---|
| 1671 | next if ($rwns && ($rr->name eq $domain));
|
---|
[34] | 1672 | push @vallist, $rr->nsdname;
|
---|
[35] | 1673 | $nsflag = 1;
|
---|
[34] | 1674 | } elsif ($type eq 'CNAME') {
|
---|
| 1675 | push @vallist, $rr->cname;
|
---|
| 1676 | } elsif ($type eq 'SOA') {
|
---|
[37] | 1677 | next if $rwsoa;
|
---|
[34] | 1678 | $vallist[1] = $rr->mname.":".$rr->rname;
|
---|
| 1679 | push @vallist, ($rr->refresh.":".$rr->retry.":".$rr->expire.":".$rr->minimum);
|
---|
[35] | 1680 | $soaflag = 1;
|
---|
[34] | 1681 | } elsif ($type eq 'PTR') {
|
---|
[105] | 1682 | push @vallist, $rr->ptrdname;
|
---|
[34] | 1683 | # hmm. PTR records should not be in forward zones.
|
---|
| 1684 | } elsif ($type eq 'MX') {
|
---|
[33] | 1685 | $sql .= ",distance";
|
---|
| 1686 | $vallen .= ",?";
|
---|
[34] | 1687 | push @vallist, $rr->exchange;
|
---|
| 1688 | push @vallist, $rr->preference;
|
---|
| 1689 | } elsif ($type eq 'TXT') {
|
---|
| 1690 | ##fixme: Net::DNS docs say this should be deprecated for rdatastr() or char_str_list(),
|
---|
| 1691 | ## but don't really seem enthusiastic about it.
|
---|
[105] | 1692 | my $rrdata = $rr->txtdata;
|
---|
| 1693 | if (length($rrdata) > 100 ) {
|
---|
| 1694 | # extralong records get an entry in a separate table.
|
---|
| 1695 | $dbh->do("INSERT INTO longrecs (recdata) VALUES (?)", undef, ($rrdata) );
|
---|
| 1696 | my ($longid) = $dbh->selectrow_array("SELECT longrec_id FROM longrecs WHERE recdata=?", undef, ($rrdata) );
|
---|
| 1697 | $sql .= ",longrec_id";
|
---|
| 1698 | $vallen .= ",?";
|
---|
| 1699 | push @vallist, '';
|
---|
| 1700 | push @vallist, $longid;
|
---|
| 1701 | } else {
|
---|
| 1702 | push @vallist, $rrdata;
|
---|
| 1703 | }
|
---|
[34] | 1704 | } elsif ($type eq 'SPF') {
|
---|
| 1705 | ##fixme: and the same caveat here, since it is apparently a clone of ::TXT
|
---|
[105] | 1706 | my $rrdata = $rr->txtdata;
|
---|
| 1707 | if (length($rrdata) > 100 ) {
|
---|
| 1708 | # extralong records get an entry in a separate table.
|
---|
| 1709 | $dbh->do("INSERT INTO longrecs (recdata) VALUES (?)", undef, ($rrdata) );
|
---|
| 1710 | my ($longid) = $dbh->selectrow_array("SELECT longrec_id FROM longrecs WHERE recdata=?", undef, ($rrdata) );
|
---|
| 1711 | $sql .= ",longrec_id";
|
---|
| 1712 | $vallen .= ",?";
|
---|
| 1713 | push @vallist, '';
|
---|
| 1714 | push @vallist, $longid;
|
---|
| 1715 | } else {
|
---|
| 1716 | push @vallist, $rrdata;
|
---|
| 1717 | }
|
---|
[34] | 1718 | } elsif ($type eq 'AAAA') {
|
---|
| 1719 | push @vallist, $rr->address;
|
---|
| 1720 | } elsif ($type eq 'SRV') {
|
---|
| 1721 | $sql .= ",distance,weight,port" if $type eq 'SRV';
|
---|
| 1722 | $vallen .= ",?,?,?" if $type eq 'SRV';
|
---|
[37] | 1723 | push @vallist, $rr->target;
|
---|
[34] | 1724 | push @vallist, $rr->priority;
|
---|
| 1725 | push @vallist, $rr->weight;
|
---|
| 1726 | push @vallist, $rr->port;
|
---|
| 1727 | } elsif ($type eq 'KEY') {
|
---|
[35] | 1728 | # we don't actually know what to do with these...
|
---|
[34] | 1729 | push @vallist, ($rr->flags.":".$rr->protocol.":".$rr->algorithm.":".$rr->key.":".$rr->keytag.":".$rr->privatekeyname);
|
---|
[35] | 1730 | } else {
|
---|
[105] | 1731 | my $rrdata = $rr->rdatastr;
|
---|
| 1732 | if (length($rrdata) > 100 ) {
|
---|
| 1733 | # extralong records get an entry in a separate table.
|
---|
| 1734 | $dbh->do("INSERT INTO longrecs (recdata) VALUES (?)", undef, ($rrdata) );
|
---|
| 1735 | my ($longid) = $dbh->selectrow_array("SELECT longrec_id FROM longrecs WHERE recdata=?", undef, ($rrdata) );
|
---|
| 1736 | $sql .= ",longrec_id";
|
---|
| 1737 | $vallen .= ",?";
|
---|
| 1738 | push @vallist, '';
|
---|
| 1739 | push @vallist, $longid;
|
---|
| 1740 | } else {
|
---|
| 1741 | push @vallist, $rrdata;
|
---|
| 1742 | }
|
---|
[35] | 1743 | # Finding a different record type is not fatal.... just problematic.
|
---|
[37] | 1744 | # We may not be able to export it correctly.
|
---|
[35] | 1745 | $warnmsg .= "Unusual record ".$rr->name." ($type) found\n";
|
---|
[33] | 1746 | }
|
---|
| 1747 |
|
---|
[34] | 1748 | # BIND supports:
|
---|
| 1749 | # A CNAME HINFO MB(ex) MD(ob) MF(ob) MG(ex) MINFO(ex) MR(ex) MX NS NULL
|
---|
| 1750 | # PTR SOA TXT WKS AFSDB(ex) ISDN(ex) RP(ex) RT(ex) X25(ex) PX
|
---|
| 1751 | # ... if one can ever find the right magic to format them correctly
|
---|
| 1752 |
|
---|
| 1753 | # Net::DNS supports:
|
---|
| 1754 | # RRSIG SIG NSAP NS NIMLOC NAPTR MX MR MINFO MG MB LOC ISDN IPSECKEY HINFO
|
---|
| 1755 | # EID DNAME CNAME CERT APL AFSDB AAAA A DS NXT NSEC3PARAM NSEC3 NSEC KEY
|
---|
| 1756 | # DNSKEY DLV X25 TXT TSIG TKEY SSHFP SRV SPF SOA RT RP PX PTR NULL APL::AplItem
|
---|
| 1757 |
|
---|
[37] | 1758 | $sth = $dbh->prepare($sql.") VALUES (".$vallen.")") or die "problem preparing record insert SQL\n";
|
---|
| 1759 | $sth->execute(@vallist) or die "failed to insert ".$rr->string.": ".$sth->errstr."\n";
|
---|
[34] | 1760 |
|
---|
[37] | 1761 | $nrecs++;
|
---|
[34] | 1762 |
|
---|
[37] | 1763 | } # while axfr_next
|
---|
| 1764 |
|
---|
| 1765 | # Overwrite SOA record
|
---|
| 1766 | if ($rwsoa) {
|
---|
| 1767 | $soaflag = 1;
|
---|
| 1768 | my $sthgetsoa = $dbh->prepare("SELECT host,val,ttl FROM default_records WHERE group_id=? AND type=?");
|
---|
| 1769 | my $sthputsoa = $dbh->prepare("INSERT INTO records (domain_id,host,type,val,ttl) VALUES (?,?,?,?,?)");
|
---|
| 1770 | $sthgetsoa->execute($group,$reverse_typemap{SOA});
|
---|
| 1771 | while (my ($host,$val,$ttl) = $sthgetsoa->fetchrow_array()) {
|
---|
| 1772 | $host =~ s/DOMAIN/$domain/g;
|
---|
| 1773 | $val =~ s/DOMAIN/$domain/g;
|
---|
| 1774 | $sthputsoa->execute($dom_id,$host,$reverse_typemap{SOA},$val,$ttl);
|
---|
[34] | 1775 | }
|
---|
[37] | 1776 | }
|
---|
[34] | 1777 |
|
---|
[37] | 1778 | # Overwrite NS records
|
---|
| 1779 | if ($rwns) {
|
---|
| 1780 | $nsflag = 1;
|
---|
| 1781 | my $sthgetns = $dbh->prepare("SELECT host,val,ttl FROM default_records WHERE group_id=? AND type=?");
|
---|
| 1782 | my $sthputns = $dbh->prepare("INSERT INTO records (domain_id,host,type,val,ttl) VALUES (?,?,?,?,?)");
|
---|
| 1783 | $sthgetns->execute($group,$reverse_typemap{NS});
|
---|
| 1784 | while (my ($host,$val,$ttl) = $sthgetns->fetchrow_array()) {
|
---|
| 1785 | $host =~ s/DOMAIN/$domain/g;
|
---|
| 1786 | $val =~ s/DOMAIN/$domain/g;
|
---|
| 1787 | $sthputns->execute($dom_id,$host,$reverse_typemap{NS},$val,$ttl);
|
---|
| 1788 | }
|
---|
| 1789 | }
|
---|
[34] | 1790 |
|
---|
[35] | 1791 | die "No records found; either $ifrom is not authoritative or doesn't allow transfers\n" if !$nrecs;
|
---|
| 1792 | die "Bad zone: No SOA record!\n" if !$soaflag;
|
---|
| 1793 | die "Bad zone: No NS records!\n" if !$nsflag;
|
---|
| 1794 |
|
---|
[37] | 1795 | $dbh->commit;
|
---|
[35] | 1796 |
|
---|
[33] | 1797 | };
|
---|
| 1798 |
|
---|
| 1799 | if ($@) {
|
---|
| 1800 | my $msg = $@;
|
---|
| 1801 | eval { $dbh->rollback; };
|
---|
[34] | 1802 | return ('FAIL',$msg." $warnmsg");
|
---|
[33] | 1803 | } else {
|
---|
[35] | 1804 | return ('WARN', $warnmsg) if $warnmsg;
|
---|
[91] | 1805 | return ('OK',"Imported OK");
|
---|
[33] | 1806 | }
|
---|
| 1807 |
|
---|
[37] | 1808 | # it should be impossible to get here.
|
---|
[34] | 1809 | return ('WARN',"OOOK!");
|
---|
[33] | 1810 | } # end importAXFR()
|
---|
| 1811 |
|
---|
| 1812 |
|
---|
[103] | 1813 | ## DNSDB::export()
|
---|
| 1814 | # Export the DNS database, or a part of it
|
---|
| 1815 | # Takes database handle, export type, optional arguments depending on type
|
---|
| 1816 | # Writes zone data to targets as appropriate for type
|
---|
| 1817 | sub export {
|
---|
| 1818 | my $dbh = shift;
|
---|
| 1819 | my $target = shift;
|
---|
| 1820 |
|
---|
| 1821 | if ($target eq 'tiny') {
|
---|
| 1822 | __export_tiny($dbh,@_);
|
---|
| 1823 | }
|
---|
| 1824 | # elsif ($target eq 'foo') {
|
---|
| 1825 | # __export_foo($dbh,@_);
|
---|
| 1826 | #}
|
---|
| 1827 | # etc
|
---|
| 1828 |
|
---|
| 1829 | } # end export()
|
---|
| 1830 |
|
---|
| 1831 |
|
---|
| 1832 | ## DNSDB::__export_tiny
|
---|
| 1833 | # Internal sub to implement tinyDNS (compatible) export
|
---|
| 1834 | # Takes database handle, filehandle to write export to, optional argument(s)
|
---|
| 1835 | # to determine which data gets exported
|
---|
| 1836 | sub __export_tiny {
|
---|
| 1837 | my $dbh = shift;
|
---|
| 1838 | my $datafile = shift;
|
---|
| 1839 |
|
---|
| 1840 | ##fixme: slurp up further options to specify particular zone(s) to export
|
---|
| 1841 |
|
---|
| 1842 | ## Convert a bare number into an octal-coded pair of octets.
|
---|
| 1843 | # Take optional arg to indicate a decimal or hex input. Defaults to hex.
|
---|
| 1844 | sub octalize {
|
---|
| 1845 | my $tmp = shift;
|
---|
| 1846 | my $srctype = shift || 'h'; # default assumes hex string
|
---|
| 1847 | $tmp = sprintf "%0.4x", hex($tmp) if $srctype eq 'h'; # 0-pad hex to 4 digits
|
---|
| 1848 | $tmp = sprintf "%0.4x", $tmp if $srctype eq 'd'; # 0-pad decimal to 4 hex digits
|
---|
| 1849 | my @o = ($tmp =~ /^(..)(..)$/); # split into octets
|
---|
| 1850 | return sprintf "\\%0.3o\\%0.3o", hex($o[0]), hex($o[1]);;
|
---|
| 1851 | }
|
---|
| 1852 |
|
---|
| 1853 | ##fixme: fail if $datafile isn't an open, writable file
|
---|
| 1854 |
|
---|
| 1855 | # easy case - export all evarything
|
---|
| 1856 | # not-so-easy case - export item(s) specified
|
---|
| 1857 | # todo: figure out what kind of list we use to export items
|
---|
| 1858 |
|
---|
| 1859 | my $domsth = $dbh->prepare("SELECT domain_id,domain,status FROM domains WHERE status=1");
|
---|
| 1860 | my $recsth = $dbh->prepare("SELECT r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl,l.recdata ".
|
---|
| 1861 | "FROM records r LEFT OUTER JOIN longrecs l ON r.longrec_id=l.longrec_id WHERE domain_id=?");
|
---|
| 1862 | $domsth->execute();
|
---|
| 1863 | while (my ($domid,$dom,$domstat) = $domsth->fetchrow_array) {
|
---|
| 1864 | $recsth->execute($domid);
|
---|
| 1865 | while (my ($host,$type,$val,$dist,$weight,$port,$ttl,$lval) = $recsth->fetchrow_array) {
|
---|
[108] | 1866 | ##fixme: need to store location in the db, and retrieve it here.
|
---|
| 1867 | # temporarily hardcoded to empty so we can include it further down.
|
---|
| 1868 | my $loc = '';
|
---|
| 1869 |
|
---|
| 1870 | ##fixme: record validity timestamp. tinydns supports fiddling with timestamps.
|
---|
| 1871 | # note $ttl must be set to 0 if we want to use tinydns's auto-expiring timestamps.
|
---|
| 1872 | # timestamps are TAI64
|
---|
| 1873 | # ~~ 2^62 + time()
|
---|
| 1874 | my $stamp = '';
|
---|
| 1875 |
|
---|
[103] | 1876 | $val = $lval if $lval;
|
---|
| 1877 |
|
---|
| 1878 | # raw packet in unknown format: first byte indicates length
|
---|
| 1879 | # of remaining data, allows up to 255 raw bytes
|
---|
| 1880 |
|
---|
| 1881 | ##fixme? append . to all host/val hostnames
|
---|
| 1882 | if ($typemap{$type} eq 'SOA') {
|
---|
| 1883 |
|
---|
| 1884 | # host contains pri-ns:responsible
|
---|
| 1885 | # val is abused to contain refresh:retry:expire:minttl
|
---|
| 1886 | ##fixme: "manual" serial vs tinydns-autoserial
|
---|
[108] | 1887 | print $datafile "Z$host"."::$val:$ttl:$stamp:$loc\n";
|
---|
[103] | 1888 |
|
---|
| 1889 | } elsif ($typemap{$type} eq 'A') {
|
---|
| 1890 |
|
---|
[108] | 1891 | print $datafile "+$host:$val:$ttl:$stamp:$loc\n";
|
---|
[103] | 1892 |
|
---|
| 1893 | } elsif ($typemap{$type} eq 'NS') {
|
---|
| 1894 |
|
---|
[108] | 1895 | print $datafile "\&$host"."::$val:$ttl:$stamp:$loc\n";
|
---|
[103] | 1896 |
|
---|
| 1897 | } elsif ($typemap{$type} eq 'AAAA') {
|
---|
| 1898 |
|
---|
| 1899 | print $datafile ":$host:28:";
|
---|
| 1900 | my $altgrp = 0;
|
---|
| 1901 | my @altconv;
|
---|
[108] | 1902 | # Split in to up to 8 groups of hex digits (allows for IPv6 :: 0-collapsing)
|
---|
[103] | 1903 | foreach (split /:/, $val) {
|
---|
| 1904 | if (/^$/) {
|
---|
| 1905 | # flag blank entry; this is a series of 0's of (currently) unknown length
|
---|
| 1906 | $altconv[$altgrp++] = 's';
|
---|
| 1907 | } else {
|
---|
| 1908 | # call sub to convert 1-4 hex digits to 2 string-rep octal bytes
|
---|
| 1909 | $altconv[$altgrp++] = octalize($_)
|
---|
| 1910 | }
|
---|
| 1911 | }
|
---|
| 1912 | foreach my $octet (@altconv) {
|
---|
| 1913 | # if not 's', output
|
---|
| 1914 | print $datafile $octet unless $octet =~ /^s$/;
|
---|
| 1915 | # if 's', output (9-array length)x literal '\000\000'
|
---|
| 1916 | print $datafile '\000\000'x(9-$altgrp) if $octet =~ /^s$/;
|
---|
| 1917 | }
|
---|
[108] | 1918 | print $datafile ":$ttl:$stamp:$loc\n";
|
---|
[103] | 1919 |
|
---|
| 1920 | } elsif ($typemap{$type} eq 'MX') {
|
---|
| 1921 |
|
---|
[108] | 1922 | print $datafile "\@$host"."::$val:$dist:$ttl:$stamp:$loc\n";
|
---|
[103] | 1923 |
|
---|
| 1924 | } elsif ($typemap{$type} eq 'TXT') {
|
---|
| 1925 |
|
---|
| 1926 | ##fixme: split v-e-r-y long TXT strings? will need to do so for BIND export, at least
|
---|
| 1927 | $val =~ s/:/\\072/g; # may need to replace other symbols
|
---|
[108] | 1928 | print $datafile "'$host:$val:$ttl:$stamp:$loc\n";
|
---|
[103] | 1929 |
|
---|
| 1930 | # by-hand TXT
|
---|
| 1931 | #:deepnet.cx:16:2v\075spf1\040a\040a\072bacon.deepnet.cx\040a\072home.deepnet.cx\040-all:3600
|
---|
| 1932 | #@ IN TXT "v=spf1 a a:bacon.deepnet.cx a:home.deepnet.cx -all"
|
---|
| 1933 | #'deepnet.cx:v=spf1 a a\072bacon.deepnet.cx a\072home.deepnet.cx -all:3600
|
---|
| 1934 |
|
---|
| 1935 | #txttest IN TXT "v=foo bar:bob kn;ob' \" !@#$%^&*()-=_+[]{}<>?"
|
---|
| 1936 | #:txttest.deepnet.cx:16:\054v\075foo\040bar\072bob\040kn\073ob\047\040\042\040\041\100\043\044\045\136\046\052\050\051-\075\137\053\133\135\173\175\074\076\077:3600
|
---|
| 1937 |
|
---|
| 1938 | # very long TXT record as brought in by axfr-get
|
---|
| 1939 | # note tinydns does not support >512-byte RR data, need axfr-dns (for TCP support) for that
|
---|
| 1940 | # also note, tinydns does not seem to support <512, >256-byte RRdata from axfr-get either. :/
|
---|
| 1941 | #:longtxt.deepnet.cx:16:
|
---|
| 1942 | #\170this is a very long txt record. it is really long. long. very long. really very long. this is a very long txt record.
|
---|
| 1943 | #\263 it is really long. long. very long. really very long. this is a very long txt record. it is really long. long. very long. really very long. this is a very long txt record.
|
---|
| 1944 | #\351 it is really long. long. very long. really very long.this is a very long txt record. it is really long. long. very long. really very long. this is a very long txt record. it is really long. long. very long. really very long.
|
---|
| 1945 | #:3600
|
---|
| 1946 |
|
---|
| 1947 | } elsif ($typemap{$type} eq 'CNAME') {
|
---|
| 1948 |
|
---|
[108] | 1949 | print $datafile "C$host:$val:$ttl:$stamp:$loc\n";
|
---|
[103] | 1950 |
|
---|
| 1951 | } elsif ($typemap{$type} eq 'SRV') {
|
---|
| 1952 |
|
---|
| 1953 | # data is two-byte values for priority, weight, port, in that order,
|
---|
| 1954 | # followed by length/string data
|
---|
| 1955 |
|
---|
| 1956 | print $datafile ":$host:33:".octalize($dist,'d').octalize($weight,'d').octalize($port,'d');
|
---|
| 1957 |
|
---|
| 1958 | $val .= '.' if $val !~ /\.$/;
|
---|
| 1959 | foreach (split /\./, $val) {
|
---|
| 1960 | printf $datafile "\\%0.3o%s", length($_), $_;
|
---|
| 1961 | }
|
---|
[108] | 1962 | print $datafile "\\000:$ttl:$stamp:$loc\n";
|
---|
[103] | 1963 |
|
---|
| 1964 | } elsif ($typemap{$type} eq 'RP') {
|
---|
| 1965 |
|
---|
| 1966 | # RP consists of two mostly free-form strings.
|
---|
| 1967 | # The first is supposed to be an email address with @ replaced by . (as with the SOA contact)
|
---|
| 1968 | # The second is the "hostname" of a TXT record with more info.
|
---|
| 1969 | print $datafile ":$host:17:";
|
---|
| 1970 | my ($who,$what) = split /\s/, $val;
|
---|
| 1971 | foreach (split /\./, $who) {
|
---|
| 1972 | printf $datafile "\\%0.3o%s", length($_), $_;
|
---|
| 1973 | }
|
---|
| 1974 | print $datafile '\000';
|
---|
| 1975 | foreach (split /\./, $what) {
|
---|
| 1976 | printf $datafile "\\%0.3o%s", length($_), $_;
|
---|
| 1977 | }
|
---|
[108] | 1978 | print $datafile "\\000:$ttl:$stamp:$loc\n";
|
---|
[103] | 1979 |
|
---|
| 1980 | } elsif ($typemap{$type} eq 'PTR') {
|
---|
| 1981 |
|
---|
| 1982 | # must handle both IPv4 and IPv6
|
---|
| 1983 | ##work
|
---|
[108] | 1984 | # data should already be in suitable reverse order.
|
---|
| 1985 | print $datafile "^$host:$val:$ttl:$stamp:$loc\n";
|
---|
[103] | 1986 |
|
---|
[108] | 1987 | } else {
|
---|
| 1988 | # raw record. we don't know what's in here, so we ASS-U-ME the user has
|
---|
| 1989 | # put it in correctly, since either the user is messing directly with the
|
---|
| 1990 | # database, or the record was imported via AXFR
|
---|
| 1991 | # <split by char>
|
---|
| 1992 | # convert anything not a-zA-Z0-9.- to octal coding
|
---|
| 1993 |
|
---|
| 1994 | ##fixme: add flag to export "unknown" record types - note we'll probably end up
|
---|
| 1995 | # mangling them since they were written to the DB from Net::DNS::RR::<type>->rdatastr.
|
---|
| 1996 | #print $datafile ":$host:$type:$val:$ttl:$stamp:$loc\n";
|
---|
| 1997 |
|
---|
[103] | 1998 | } # record type if-else
|
---|
| 1999 |
|
---|
| 2000 | } # while ($recsth)
|
---|
| 2001 | } # while ($domsth)
|
---|
| 2002 | } # end __export_tiny()
|
---|
| 2003 |
|
---|
| 2004 |
|
---|
[2] | 2005 | # shut Perl up
|
---|
| 2006 | 1;
|
---|