[171] | 1 | #!/usr/bin/perl
|
---|
| 2 | # Import script for VegaDNS data
|
---|
| 3 | # $Id: vega-import.pl 172 2011-11-29 21:52:25Z kdeugau $
|
---|
| 4 | # Copyright (C) 2011 Kris Deugau <kdeugau@deepnet.cx>
|
---|
| 5 |
|
---|
| 6 | # Runs in one of two modes:
|
---|
| 7 | # - Wipe the contents and import the VegaDNS data with (almost) all entity-ID info as-is
|
---|
| 8 | # - Add the contents to the current DB, as a subgroup of the root parent group 1
|
---|
| 9 |
|
---|
| 10 | use strict;
|
---|
| 11 | use warnings;
|
---|
| 12 |
|
---|
| 13 | use DBI;
|
---|
| 14 | use Data::Dumper;
|
---|
| 15 |
|
---|
| 16 | use DNSDB qw(:ALL);
|
---|
| 17 |
|
---|
| 18 | if (!loadConfig()) {
|
---|
| 19 | warn "Using default configuration; unable to load custom settings: $DNSDB::errstr";
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | my $mode = 'add';
|
---|
| 23 |
|
---|
| 24 | if ($ARGV[0] && $ARGV[0] !~ /^(add|replace)$/) {
|
---|
| 25 | die "Usage: vega-import.pl [add|replace]\n".
|
---|
| 26 | " add Import VegaDNS data as a subgroup of the default root group\n".
|
---|
| 27 | " replace Wipe all existing data and import VegaDNS data into the root group\n";
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | $mode = $ARGV[0] if $ARGV[0];
|
---|
| 31 |
|
---|
| 32 | my ($newdbh,$msg) = connectDB($config{dbname}, $config{dbuser}, $config{dbpass}, $config{dbhost});
|
---|
| 33 | $newdbh->{PrintError} = 1;
|
---|
| 34 | $newdbh->{PrintWarn} = 1;
|
---|
[172] | 35 | initGlobals($newdbh);
|
---|
[171] | 36 |
|
---|
[172] | 37 | my %vegatypes = ('S' => 'SOA', 'N' => 'NS', 'A' => 'A', 'T' => 'TXT',
|
---|
| 38 | 'V' => 'SRV', 'P' => 'PTR', 'M' => 'MX', 'C' => 'CNAME' );
|
---|
| 39 |
|
---|
[171] | 40 | if ($mode eq 'replace') {
|
---|
| 41 | print "WARNING: Using 'replace' will **DELETE** all existing group,\n".
|
---|
[172] | 42 | "user, domain, and record data in DeepNet DNS Administrator.\n".
|
---|
| 43 | "Once started this cannot be reversed. Enter 'yes' to proceed: ";
|
---|
[171] | 44 | my $resp = <STDIN>;
|
---|
| 45 | chomp $resp;
|
---|
| 46 | if ($resp ne 'yes') {
|
---|
| 47 | print "Aborting; no changes made.\n";
|
---|
| 48 | exit;
|
---|
| 49 | } else {
|
---|
| 50 | print "OK, continuing.\n";
|
---|
| 51 | $newdbh->do("DELETE FROM users") or warn "error deleting users: ".$newdbh->errstr."\n";
|
---|
| 52 | $newdbh->do("DELETE FROM log") or warn "error deleting log: ".$newdbh->errstr."\n";
|
---|
| 53 | $newdbh->do("DELETE FROM default_records") or warn "error deleting defrecs: ".$newdbh->errstr."\n";
|
---|
| 54 | $newdbh->do("DELETE FROM records") or warn "error deleting records: ".$newdbh->errstr."\n";
|
---|
| 55 | $newdbh->do("DELETE FROM domains") or warn "error deleting domains: ".$newdbh->errstr."\n";
|
---|
| 56 | $newdbh->do("DELETE FROM groups") or warn "error deleting groups: ".$newdbh->errstr."\n";
|
---|
| 57 | $newdbh->do("DELETE FROM permissions") or warn "error deleting permissions: ".$newdbh->errstr."\n";
|
---|
| 58 | # set seq id on permissions, since we merged the two tables, and the new permission IDs will not match the old
|
---|
| 59 | $newdbh->do("SELECT pg_catalog.setval(pg_catalog.pg_get_serial_sequence('permissions', 'permission_id'),".
|
---|
| 60 | "1, true)") or warn "couldn't set permission sequence: ".$newdbh->errstr."\n";
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | my $vegadbh = DBI->connect('DBI:mysql:database=vegadns;host=localhost', 'vegadns', 'secret', {
|
---|
| 65 | PrintError => 1,
|
---|
| 66 | PrintWarn => 1,
|
---|
| 67 | AutoCommit => 1
|
---|
| 68 | });
|
---|
| 69 |
|
---|
| 70 | my $grpsubs = $vegadbh->prepare("SELECT group_id FROM groups ORDER BY group_id");
|
---|
| 71 | my $grpdata = $vegadbh->prepare("SELECT group_id, parent_group_id, name FROM groups WHERE group_id=?");
|
---|
| 72 | my $grppget = $vegadbh->prepare("SELECT perm_id,inherit_group_perms,accouedit,accoucreate,accoudelete,".
|
---|
| 73 | "self_edit,group_edit,group_create,group_delete,domain_edit,domain_create,domain_delete,record_edit,".
|
---|
| 74 | "record_create,record_delete FROM group_permissions WHERE group_id=?");
|
---|
| 75 | my $userget = $vegadbh->prepare("SELECT user_id, group_id, email, password, first_name, last_name, phone, ".
|
---|
| 76 | "account_type, status FROM accounts WHERE group_id=?");
|
---|
| 77 | my $userpget = $vegadbh->prepare("SELECT perm_id,inherit_group_perms,accouedit,accoucreate,accoudelete,".
|
---|
| 78 | "self_edit,group_edit,group_create,group_delete,domain_edit,domain_create,domain_delete,record_edit,".
|
---|
| 79 | "record_create,record_delete FROM user_permissions WHERE user_id=?");
|
---|
[172] | 80 | my $domget = $vegadbh->prepare("SELECT domain_id,domain,description,status FROM domains WHERE group_id=?");
|
---|
| 81 | my $recget = $vegadbh->prepare("SELECT record_id,host,type,val,distance,weight,port,ttl,description ".
|
---|
| 82 | "FROM records WHERE domain_id=?");
|
---|
| 83 | my $defrecget = $vegadbh->prepare("SELECT record_id, group_id, host, type, val, distance, weight, port, ttl, ".
|
---|
| 84 | "description FROM default_records WHERE group_id=?");
|
---|
[171] | 85 |
|
---|
| 86 | my $newgrp = $newdbh->prepare("INSERT INTO groups (group_id,parent_group_id,group_name,permission_id) ".
|
---|
| 87 | "VALUES (?,?,?,?)");
|
---|
| 88 | my $newgrppset = $newdbh->prepare("INSERT INTO permissions (self_edit, group_create, ".
|
---|
| 89 | "group_edit, group_delete, user_create, user_edit, user_delete, domain_create, domain_edit, ".
|
---|
| 90 | "domain_delete, record_create, record_edit, record_delete, group_id) ".
|
---|
| 91 | "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
|
---|
| 92 | my $newpid = $newdbh->prepare("SELECT currval('permissions_permission_id_seq')");
|
---|
| 93 | my $newuser = $newdbh->prepare("INSERT INTO users (user_id, group_id, username, password, firstname, ".
|
---|
| 94 | "lastname, phone, type, status, permission_id, inherit_perm) VALUES (?,?,?,?,?,?,?,?,?,?,'f')");
|
---|
[172] | 95 | my $newuserpset = $newdbh->prepare("INSERT INTO permissions (admin, self_edit, group_create, ".
|
---|
[171] | 96 | "group_edit, group_delete, user_create, user_edit, user_delete, domain_create, domain_edit, ".
|
---|
| 97 | "domain_delete, record_create, record_edit, record_delete, user_id) ".
|
---|
[172] | 98 | "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
|
---|
| 99 | my $newdom = $newdbh->prepare("INSERT INTO domains (domain_id, domain, group_id, description, status) VALUES ".
|
---|
| 100 | "(?,?,?,?,?)");
|
---|
| 101 | my $newrec = $newdbh->prepare("INSERT INTO records (domain_id,record_id,host,type,val,distance,weight,port,".
|
---|
| 102 | "ttl,description) VALUES (?,?,?,?,?,?,?,?,?,?)");
|
---|
| 103 | my $newdefrec = $newdbh->prepare("INSERT INTO default_records (record_id,group_id,host,type,val,distance,".
|
---|
| 104 | "weight,port,ttl,description) VALUES (?,?,?,?,?,?,?,?,?,?)");
|
---|
[171] | 105 |
|
---|
| 106 | my $foo = qq(
|
---|
| 107 |
|
---|
| 108 | perm_id | group_id | inherit_group_perms | accouedit | accoucreate | accoudelete | self_edit | group_edit |
|
---|
| 109 | group_create | group_delete | domain_edit | domain_create | domain_delegate | domain_delete | record_edit |
|
---|
| 110 | record_create | record_delete | record_delegate | default_record_edit | default_record_create |
|
---|
| 111 | default_record_delete | rrtype_allow_n | rrtype_allow_a | rrtype_allow_3 | rrtype_allow_6 | rrtype_allow_m |
|
---|
| 112 | rrtype_allow_p | rrtype_allow_t | rrtype_allow_v | rrtype_allow_all
|
---|
| 113 |
|
---|
| 114 | permission_id | admin | self_edit | group_create | group_edit | group_delete | user_create | user_edit |
|
---|
| 115 | user_delete | domain_create | domain_edit | domain_delete | record_create | record_edit | record_delete |
|
---|
| 116 | user_id | group_id
|
---|
| 117 |
|
---|
| 118 | );
|
---|
| 119 |
|
---|
| 120 | my $stage = 'start';
|
---|
| 121 | $grpsubs->execute;
|
---|
| 122 | my $grp = 0;
|
---|
| 123 | while (($grp) = $grpsubs->fetchrow_array()) {
|
---|
| 124 | dogroup($grp);
|
---|
| 125 | }
|
---|
| 126 | # set group seq to $grp+1
|
---|
| 127 | exit;
|
---|
| 128 |
|
---|
| 129 | sub dogroup {
|
---|
| 130 | my $grpid = shift;
|
---|
| 131 | $grpdata->execute($grpid) or warn $grpdata->errstr;
|
---|
| 132 | my $oldgrp = $grpdata->fetchrow_hashref() or warn $grpdata->errstr;
|
---|
| 133 | print "group id $grpid, name $oldgrp->{name}\n";
|
---|
| 134 |
|
---|
| 135 | $grppget->execute($grpid) or warn $grppget->errstr;
|
---|
| 136 | my $oldp = $grppget->fetchrow_hashref() or warn $grppget->errstr;
|
---|
| 137 |
|
---|
| 138 | # de-nullify
|
---|
| 139 | foreach my $permtype ('self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
|
---|
| 140 | 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
|
---|
| 141 | 'record_edit', 'record_delete') {
|
---|
| 142 | $oldp->{$permtype} = 'f' if !defined($oldp->{$permtype});
|
---|
| 143 | }
|
---|
| 144 | $newgrppset->execute($oldp->{self_edit}, $oldp->{group_create}, $oldp->{group_edit}, $oldp->{group_delete},
|
---|
| 145 | $oldp->{user_create}, $oldp->{user_edit}, $oldp->{user_delete},
|
---|
| 146 | $oldp->{domain_create}, $oldp->{domain_edit}, $oldp->{domain_delete},
|
---|
| 147 | $oldp->{record_create}, $oldp->{record_edit}, $oldp->{record_delete},
|
---|
| 148 | $grpid) or warn $newgrppset->errstr;
|
---|
| 149 | $newpid->execute;
|
---|
| 150 | my ($pid) = $newpid->fetchrow_array;
|
---|
| 151 | $newgrp->execute($grpid, $oldgrp->{parent_group_id}, $oldgrp->{name}, $pid);
|
---|
| 152 |
|
---|
| 153 | ## Users
|
---|
| 154 | print " users:\n";
|
---|
| 155 | $userget->execute($grpid);
|
---|
| 156 | while (my $user = $userget->fetchrow_hashref) {
|
---|
[172] | 157 | # fiddle user data
|
---|
| 158 | $user->{account_type} = 'S' if $user->{account_type} eq 'senior_admin';
|
---|
| 159 | $user->{account_type} = 'u' if $user->{account_type} eq 'user';
|
---|
| 160 | $user->{status} = ($user->{status} eq 'active' ? 1 : 0);
|
---|
[171] | 161 | $userpget->execute($user->{user_id}) or warn "failed permission get on ".$user->{user_id}."\n";
|
---|
| 162 | my $oldp = $userpget->fetchrow_hashref;
|
---|
| 163 | # de-nullify
|
---|
[172] | 164 | foreach my $permtype ('admin', 'self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
|
---|
[171] | 165 | 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
|
---|
| 166 | 'record_edit', 'record_delete') {
|
---|
| 167 | $oldp->{$permtype} = 'f' if !defined($oldp->{$permtype});
|
---|
| 168 | }
|
---|
[172] | 169 | $newuserpset->execute(($user->{account_type} eq 'S' ? 't' : 'f'), $oldp->{self_edit},
|
---|
| 170 | $oldp->{group_create}, $oldp->{group_edit}, $oldp->{group_delete},
|
---|
[171] | 171 | $oldp->{user_create}, $oldp->{user_edit}, $oldp->{user_delete},
|
---|
| 172 | $oldp->{domain_create}, $oldp->{domain_edit}, $oldp->{domain_delete},
|
---|
| 173 | $oldp->{record_create}, $oldp->{record_edit}, $oldp->{record_delete},
|
---|
| 174 | $user->{user_id}) or warn $newgrppset->errstr;
|
---|
| 175 | $newpid->execute;
|
---|
| 176 | my ($pid) = $newpid->fetchrow_array;
|
---|
| 177 | $newuser->execute($user->{user_id}, $user->{group_id}, $user->{email}, $user->{password},
|
---|
| 178 | $user->{first_name}, $user->{last_name}, $user->{phone}, $user->{account_type}, $user->{status}, $pid)
|
---|
| 179 | or warn " new user ".$user->{email}." (".$user->{user_id}.") failed: ".$newuser->errstr."\n";
|
---|
[172] | 180 | print " user ".$user->{email}." (".$user->{user_id}.")\n";
|
---|
[171] | 181 | }
|
---|
[172] | 182 | print " done\n";
|
---|
[171] | 183 |
|
---|
[172] | 184 | ## Domains
|
---|
| 185 | print " domains: \n";
|
---|
| 186 | $domget->execute($grpid);
|
---|
| 187 | while (my ($id,$dom,$desc,$status) = $domget->fetchrow_array) {
|
---|
| 188 | $status = ($status eq 'active' ? 1 : 0);
|
---|
| 189 | $newdom->execute($id, $dom, $grpid, $desc, $status);
|
---|
| 190 | print " domain $dom ($id)\n";
|
---|
| 191 | $recget->execute($id);
|
---|
| 192 | while (my @rec = $recget->fetchrow_array) {
|
---|
| 193 | $rec[2] = $reverse_typemap{$vegatypes{$rec[2]}};
|
---|
| 194 | $rec[4] = 0 if !$rec[4];
|
---|
| 195 | $rec[5] = 0 if !$rec[5];
|
---|
| 196 | $rec[6] = 0 if !$rec[6];
|
---|
| 197 | $newrec->execute($id,@rec);
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 | print " done\n";
|
---|
[171] | 201 |
|
---|
[172] | 202 | ## Default records
|
---|
| 203 | print " default records: ";
|
---|
| 204 | $defrecget->execute($grpid);
|
---|
| 205 | while (my @rec = $defrecget->fetchrow_array) {
|
---|
| 206 | $rec[3] = $reverse_typemap{$vegatypes{$rec[3]}};
|
---|
| 207 | $rec[5] = 0 if !$rec[5];
|
---|
| 208 | $rec[6] = 0 if !$rec[6];
|
---|
| 209 | $rec[7] = 0 if !$rec[7];
|
---|
| 210 | $newdefrec->execute(@rec);
|
---|
| 211 | }
|
---|
| 212 | print " done\n";
|
---|
[171] | 213 | }
|
---|