source: trunk/vega-import.pl@ 865

Last change on this file since 865 was 797, checked in by Kris Deugau, 4 years ago

/trunk

Clean up a lingering nuisance with Perl's default include path; all scripts
should now run correctly no matter what the caller's current directory.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 18.0 KB
Line 
1#!/usr/bin/perl
2# Import script for VegaDNS data
3##
4# $Id: vega-import.pl 797 2020-11-03 20:38:37Z kdeugau $
5# Copyright 2011-2013,2020 Kris Deugau <kdeugau@deepnet.cx>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19##
20
21# Runs in one of two modes:
22# - Wipe the contents and import the VegaDNS data with (almost) all entity-ID info as-is
23# - Add the contents to the current DB, as a subgroup of the root parent group 1
24
25use strict;
26use warnings;
27
28use DBI;
29use Data::Dumper;
30
31# push "the directory the script is in" into @INC
32use FindBin;
33use lib "$FindBin::RealBin/";
34
35use DNSDB;
36
37my $dnsdb = new DNSDB;
38
39my $mode = 'add';
40
41if ($ARGV[0] && $ARGV[0] !~ /^(add|replace)$/) {
42 die "Usage: vega-import.pl [add|replace]\n".
43 " add Import VegaDNS data as a subgroup of the default root group (default)\n".
44 " replace Wipe all existing data and import VegaDNS data into the root group\n";
45}
46
47$mode = $ARGV[0] if $ARGV[0];
48
49my $newdbh = $dnsdb->{dbh};
50$newdbh->{PrintError} = 1;
51$newdbh->{PrintWarn} = 1;
52
53my %vegatypes = ('S' => 'SOA', 'N' => 'NS', 'A' => 'A', 'T' => 'TXT',
54 'V' => 'SRV', 'P' => 'PTR', 'M' => 'MX', 'C' => 'CNAME',
55 '3' => 'AAAA' );
56
57if ($mode eq 'replace') {
58 print "WARNING: Using 'replace' will **DELETE** all existing group,\n".
59 "user, domain, and record data in DeepNet DNS Administrator.\n".
60 "Once started this cannot be reversed. Enter 'yes' to proceed: ";
61 my $resp = <STDIN>;
62 chomp $resp;
63 if ($resp ne 'yes') {
64 print "Aborting; no changes made.\n";
65 exit;
66 } else {
67 print "OK, continuing.\n";
68 $newdbh->do("DELETE FROM users") or warn "error deleting users: ".$newdbh->errstr."\n";
69 $newdbh->do("DELETE FROM log") or warn "error deleting log: ".$newdbh->errstr."\n";
70 $newdbh->do("DELETE FROM default_records") or warn "error deleting defrecs: ".$newdbh->errstr."\n";
71 $newdbh->do("DELETE FROM records") or warn "error deleting records: ".$newdbh->errstr."\n";
72 $newdbh->do("DELETE FROM domains") or warn "error deleting domains: ".$newdbh->errstr."\n";
73 $newdbh->do("DELETE FROM groups") or warn "error deleting groups: ".$newdbh->errstr."\n";
74 $newdbh->do("DELETE FROM permissions") or warn "error deleting permissions: ".$newdbh->errstr."\n";
75 # set seq id on permissions, since we merged the two tables, and the new permission IDs will not match the old
76 $newdbh->do("SELECT pg_catalog.setval(pg_catalog.pg_get_serial_sequence('permissions', 'permission_id'),".
77 "1, false)") or warn "couldn't set permission sequence: ".$newdbh->errstr."\n";
78 # set seq id on default_records since we're inserting more than we retrieved
79 $newdbh->do("SELECT pg_catalog.setval(pg_catalog.pg_get_serial_sequence('default_records', 'record_id'),".
80 "1, false)") or warn "couldn't set defrec sequence: ".$newdbh->errstr."\n";
81 }
82}
83
84my $vegadbh = DBI->connect('DBI:mysql:database=vegadns;host=localhost', 'vegadns', 'secret', {
85 PrintError => 1,
86 PrintWarn => 1,
87 AutoCommit => 0
88 });
89
90my $grpsubs = $vegadbh->prepare("SELECT group_id FROM groups ORDER BY group_id");
91my $grpdata = $vegadbh->prepare("SELECT group_id, parent_group_id, name FROM groups WHERE group_id=?");
92my $grppget = $vegadbh->prepare("SELECT perm_id,inherit_group_perms,accouedit,accoucreate,accoudelete,".
93 "self_edit,group_edit,group_create,group_delete,domain_edit,domain_create,domain_delete,record_edit,".
94 "record_create,record_delete FROM group_permissions WHERE group_id=?");
95my $userget = $vegadbh->prepare("SELECT user_id, group_id, email, password, first_name, last_name, phone, ".
96 "account_type, status FROM accounts WHERE group_id=?");
97my $userpget = $vegadbh->prepare("SELECT perm_id,inherit_group_perms,accouedit,accoucreate,accoudelete,".
98 "self_edit,group_edit,group_create,group_delete,domain_edit,domain_create,domain_delete,record_edit,".
99 "record_create,record_delete FROM user_permissions WHERE user_id=?");
100my $domget = $vegadbh->prepare("SELECT domain_id,domain,description,status FROM domains WHERE group_id=?");
101my $recget = $vegadbh->prepare("SELECT record_id,host,type,val,distance,weight,port,ttl,description ".
102 "FROM records WHERE domain_id=?");
103my $defrecget = $vegadbh->prepare("SELECT group_id, host, type, val, distance, weight, port, ttl, ".
104 "description FROM default_records WHERE group_id=?");
105my $logget = $vegadbh->prepare("SELECT domain_id, user_id, group_id, email, name, entry, time FROM log ".
106 "WHERE group_id=?");
107
108my $newgrp = $newdbh->prepare("INSERT INTO groups (group_id,parent_group_id,group_name,permission_id) ".
109 "VALUES (?,?,?,?)");
110my $newgrppset = $newdbh->prepare("INSERT INTO permissions (self_edit, group_create, ".
111 "group_edit, group_delete, user_create, user_edit, user_delete, domain_create, domain_edit, ".
112 "domain_delete, record_create, record_edit, record_delete, group_id) ".
113 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
114my $newpid = $newdbh->prepare("SELECT currval('permissions_permission_id_seq')");
115my $newuser = $newdbh->prepare("INSERT INTO users (user_id, group_id, username, password, firstname, ".
116 "lastname, phone, type, status, permission_id, inherit_perm) VALUES (?,?,?,?,?,?,?,?,?,?,'f')");
117my $newuserpset = $newdbh->prepare("INSERT INTO permissions (admin, self_edit, group_create, ".
118 "group_edit, group_delete, user_create, user_edit, user_delete, domain_create, domain_edit, ".
119 "domain_delete, record_create, record_edit, record_delete, user_id) ".
120 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
121my $newdom = $newdbh->prepare("INSERT INTO domains (domain_id, domain, group_id, description, status) VALUES ".
122 "(?,?,?,?,?)");
123my $newrec = $newdbh->prepare("INSERT INTO records (domain_id,record_id,host,type,val,distance,weight,port,".
124 "ttl,description) VALUES (?,?,?,?,?,?,?,?,?,?)");
125my $newdefrec = $newdbh->prepare("INSERT INTO default_records (group_id,host,type,val,distance,".
126 "weight,port,ttl,description) VALUES (?,?,?,?,?,?,?,?,?)");
127my $logput = $newdbh->prepare("INSERT INTO log (domain_id, user_id, group_id, email, name, entry, stamp) ".
128 "VALUES (?,?,?,?,?,?,?)");
129
130my $newgrpid = $newdbh->prepare("SELECT currval('groups_group_id_seq')");
131my $newpgidup = $newdbh->prepare("UPDATE groups SET permission_id=? WHERE group_id=?");
132my $newuserid = $newdbh->prepare("SELECT currval('users_user_id_seq')");
133my $newpuidup = $newdbh->prepare("UPDATE users SET permission_id=? WHERE user_id=?");
134my $newdomid = $newdbh->prepare("SELECT currval('domains_domain_id_seq')");
135my $newrecid = $newdbh->prepare("SELECT currval('records_record_id_seq')");
136
137my $newgrp_add = $newdbh->prepare("INSERT INTO groups (parent_group_id,group_name) VALUES (?,?)");
138my $newuser_add = $newdbh->prepare("INSERT INTO users (group_id, username, password, firstname, ".
139 "lastname, phone, type, status, inherit_perm) VALUES (?,?,?,?,?,?,?,?,'f')");
140my $newdom_add = $newdbh->prepare("INSERT INTO domains (domain, group_id, description, status) VALUES ".
141 "(?,?,?,?)");
142my $newrec_add = $newdbh->prepare("INSERT INTO records (domain_id,host,type,val,distance,weight,port,".
143 "ttl,description) VALUES (?,?,?,?,?,?,?,?,?)");
144
145my $foo = qq(
146
147perm_id | group_id | inherit_group_perms | accouedit | accoucreate | accoudelete | self_edit | group_edit |
148group_create | group_delete | domain_edit | domain_create | domain_delegate | domain_delete | record_edit |
149record_create | record_delete | record_delegate | default_record_edit | default_record_create |
150default_record_delete | rrtype_allow_n | rrtype_allow_a | rrtype_allow_3 | rrtype_allow_6 | rrtype_allow_m |
151rrtype_allow_p | rrtype_allow_t | rrtype_allow_v | rrtype_allow_all
152
153permission_id | admin | self_edit | group_create | group_edit | group_delete | user_create | user_edit |
154user_delete | domain_create | domain_edit | domain_delete | record_create | record_edit | record_delete |
155user_id | group_id
156
157);
158
159my $maxgrp = 0;
160my %pcmap; # used to map parent/child relations
161
162if ($mode eq 'replace') {
163
164 my $stage = 'start';
165 $grpsubs->execute;
166 my $grp = 0;
167 while (($grp) = $grpsubs->fetchrow_array()) {
168 $maxgrp = $grp if $grp > $maxgrp;
169 dogroup_replace($grp);
170 }
171
172 # Fix up sequence numbers to prevent insert problems
173 my %idmap = ('groups' => 'group_id', 'users' => 'user_id', 'domains' => 'domain_id',
174 'records' => 'record_id');
175 for my $table ('groups','users','domains','records') {
176 $newdbh->do("SELECT pg_catalog.setval(pg_catalog.pg_get_serial_sequence('$table','$idmap{$table}'),".
177 "(SELECT max($idmap{$table}) FROM $table),true)");
178 }
179
180} else {
181
182 $grpsubs->execute;
183 my $grp = 0;
184 while (($grp) = $grpsubs->fetchrow_array()) {
185 $maxgrp = $grp if $grp > $maxgrp;
186 dogroup_add($grp);
187 }
188
189}
190
191# and done.
192exit;
193
194sub dogroup_replace {
195 my $grpid = shift;
196 $grpdata->execute($grpid) or warn $grpdata->errstr;
197 my $oldgrp = $grpdata->fetchrow_hashref() or warn $grpdata->errstr;
198 print "group id $grpid, name $oldgrp->{name}\n";
199
200 $grppget->execute($grpid) or warn $grppget->errstr;
201 my $oldp;
202 unless ($oldp = $grppget->fetchrow_hashref()) {
203 foreach my $permtype ('self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
204 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
205 'record_edit', 'record_delete') { $oldp->{$permtype} = 't'; }
206 }
207
208 # de-nullify
209 foreach my $permtype ('self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
210 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
211 'record_edit', 'record_delete') {
212 $oldp->{$permtype} = 'f' if !defined($oldp->{$permtype});
213 }
214 $newgrppset->execute($oldp->{self_edit}, $oldp->{group_create}, $oldp->{group_edit}, $oldp->{group_delete},
215 $oldp->{user_create}, $oldp->{user_edit}, $oldp->{user_delete},
216 $oldp->{domain_create}, $oldp->{domain_edit}, $oldp->{domain_delete},
217 $oldp->{record_create}, $oldp->{record_edit}, $oldp->{record_delete},
218 $grpid) or warn $newgrppset->errstr;
219 $newpid->execute;
220 my ($pid) = $newpid->fetchrow_array;
221 $newgrp->execute($grpid, $oldgrp->{parent_group_id}, $oldgrp->{name}, $pid);
222
223 ## Users
224 print " users: ";
225 $userget->execute($grpid);
226 while (my $user = $userget->fetchrow_hashref) {
227 # fiddle user data
228 $user->{account_type} = 'S' if $user->{account_type} eq 'senior_admin';
229 $user->{account_type} = 'u' if $user->{account_type} eq 'user';
230 $user->{status} = ($user->{status} eq 'active' ? 1 : 0);
231 $userpget->execute($user->{user_id}) or warn "failed permission get on ".$user->{user_id}."\n";
232 my $oldp = $userpget->fetchrow_hashref;
233 # de-nullify
234 foreach my $permtype ('admin', 'self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
235 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
236 'record_edit', 'record_delete') {
237 $oldp->{$permtype} = 'f' if !defined($oldp->{$permtype});
238 }
239 $newuserpset->execute(($user->{account_type} eq 'S' ? 't' : 'f'), $oldp->{self_edit},
240 $oldp->{group_create}, $oldp->{group_edit}, $oldp->{group_delete},
241 $oldp->{user_create}, $oldp->{user_edit}, $oldp->{user_delete},
242 $oldp->{domain_create}, $oldp->{domain_edit}, $oldp->{domain_delete},
243 $oldp->{record_create}, $oldp->{record_edit}, $oldp->{record_delete},
244 $user->{user_id}) or warn $newgrppset->errstr;
245 $newpid->execute;
246 my ($pid) = $newpid->fetchrow_array;
247 $newuser->execute($user->{user_id}, $user->{group_id}, $user->{email}, $user->{password},
248 $user->{first_name}, $user->{last_name}, $user->{phone}, $user->{account_type}, $user->{status}, $pid)
249 or warn " new user ".$user->{email}." (".$user->{user_id}.") failed: ".$newuser->errstr."\n";
250 print $user->{email}." (".$user->{user_id}."), ";
251 }
252
253 ## Domains
254 print "\n domains: ";
255 $domget->execute($grpid);
256 while (my ($id,$dom,$desc,$status) = $domget->fetchrow_array) {
257 $status = ($status eq 'active' ? 1 : 0);
258 $newdom->execute($id, $dom, $grpid, $desc, $status);
259 print "$dom ($id), ";
260 $recget->execute($id);
261 while (my @rec = $recget->fetchrow_array) {
262 $rec[2] = $reverse_typemap{$vegatypes{$rec[2]}};
263 $rec[4] = 0 if !$rec[4];
264 $rec[5] = 0 if !$rec[5];
265 $rec[6] = 0 if !$rec[6];
266 $newrec->execute($id,@rec);
267 }
268 }
269
270 ## Default records
271 print "\n default records: ";
272 $defrecget->execute(1); # Vega 1.1.5/1.1.6 do not have default records for all groups;
273 # there is only support for one set of default records coded.
274 while (my @rec = $defrecget->fetchrow_array) {
275 $rec[0] = $grpid;
276 $rec[2] = $reverse_typemap{$vegatypes{$rec[2]}};
277 $rec[4] = 0 if !$rec[4];
278 $rec[5] = 0 if !$rec[5];
279 $rec[6] = 0 if !$rec[6];
280 $newdefrec->execute(@rec);
281 }
282
283 ## Log entries
284 print "\n log entries: ";
285 $logget->execute($grpid);
286 while (my ($did,$uid,$gid,$email,$name,$entry,$stamp) = $logget->fetchrow_array) {
287 $stamp = localtime($stamp).'';
288 $logput->execute($did,$uid,$gid,$email,$name,'[Vega] '.$entry,$stamp);
289 }
290
291 print "\n done\n";
292}
293
294sub dogroup_add {
295 my $oldgrpid = shift;
296
297 $grpdata->execute($oldgrpid) or warn $grpdata->errstr;
298 my $oldgrp = $grpdata->fetchrow_hashref() or warn $grpdata->errstr;
299 print "group id $oldgrpid, name $oldgrp->{name}\n";
300
301 my $newgrpparent = $pcmap{g}{$oldgrp->{parent_group_id}};
302 $newgrpparent = 1 if $oldgrpid == 1;
303
304 # do in the same order as dnsdb
305 $newgrp_add->execute($newgrpparent, $oldgrp->{name});
306 $newgrpid->execute;
307 my ($newgid) = $newgrpid->fetchrow_array;
308 $pcmap{g}{$oldgrpid} = $newgid;
309
310 $grppget->execute($oldgrpid) or warn $grppget->errstr;
311 my $oldp;
312 unless ($oldp = $grppget->fetchrow_hashref()) {
313 foreach my $permtype ('self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
314 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
315 'record_edit', 'record_delete') { $oldp->{$permtype} = 't'; }
316 }
317
318 # de-nullify
319 foreach my $permtype ('self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
320 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
321 'record_edit', 'record_delete') {
322 $oldp->{$permtype} = 'f' if !defined($oldp->{$permtype});
323 }
324 $newgrppset->execute($oldp->{self_edit}, $oldp->{group_create}, $oldp->{group_edit}, $oldp->{group_delete},
325 $oldp->{user_create}, $oldp->{user_edit}, $oldp->{user_delete},
326 $oldp->{domain_create}, $oldp->{domain_edit}, $oldp->{domain_delete},
327 $oldp->{record_create}, $oldp->{record_edit}, $oldp->{record_delete},
328 $newgid) or warn $newgrppset->errstr;
329 $newpid->execute;
330 my ($pid) = $newpid->fetchrow_array;
331 $newpgidup->execute($pid,$newgid);
332
333 ## Users
334 print " users: ";
335 $userget->execute($oldgrpid);
336 while (my $user = $userget->fetchrow_hashref) {
337 # fiddle user data
338 $user->{account_type} = 'S' if $user->{account_type} eq 'senior_admin';
339 $user->{account_type} = 'u' if $user->{account_type} eq 'user';
340 $user->{status} = ($user->{status} eq 'active' ? 1 : 0);
341 $userpget->execute($user->{user_id}) or warn "failed permission get on ".$user->{user_id}."\n";
342
343 $newuser_add->execute($newgid, $user->{email}, $user->{password}, $user->{first_name},
344 $user->{last_name}, $user->{phone}, $user->{account_type}, $user->{status})
345 or warn " new user ".$user->{email}." (".$user->{user_id}.") failed: ".$newuser->errstr."\n";
346 print " user ".$user->{email}." (".$user->{user_id}."), ";
347 $newuserid->execute;
348 my ($newuid) = $newuserid->fetchrow_array;
349 $pcmap{u}{$user->{user_id}} = $newuid;
350
351 my $oldp = $userpget->fetchrow_hashref;
352 # de-nullify
353 foreach my $permtype ('admin', 'self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
354 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
355 'record_edit', 'record_delete') {
356 $oldp->{$permtype} = 'f' if !defined($oldp->{$permtype});
357 }
358 $newuserpset->execute(($user->{account_type} eq 'S' ? 't' : 'f'), $oldp->{self_edit},
359 $oldp->{group_create}, $oldp->{group_edit}, $oldp->{group_delete},
360 $oldp->{user_create}, $oldp->{user_edit}, $oldp->{user_delete},
361 $oldp->{domain_create}, $oldp->{domain_edit}, $oldp->{domain_delete},
362 $oldp->{record_create}, $oldp->{record_edit}, $oldp->{record_delete},
363 $newuid) or warn $newgrppset->errstr;
364 $newpid->execute;
365 my ($pid) = $newpid->fetchrow_array;
366 $newpuidup->execute($pid,$newuid);
367 }
368
369 ## Domains
370 print "\n domains: ";
371 $domget->execute($oldgrpid);
372 while (my ($id,$dom,$desc,$status) = $domget->fetchrow_array) {
373 $status = ($status eq 'active' ? 1 : 0);
374 $newdom_add->execute($dom, $newgid, $desc, $status);
375 print "$dom ($id), ";
376 $newdomid->execute;
377 my ($newdid) = $newdomid->fetchrow_array;
378 $pcmap{d}{$id} = $newdid;
379 $recget->execute($id);
380 while (my @rec = $recget->fetchrow_array) {
381 $rec[0] = $newdid;
382 $rec[2] = $reverse_typemap{$vegatypes{$rec[2]}};
383 $rec[4] = 0 if !$rec[4];
384 $rec[5] = 0 if !$rec[5];
385 $rec[6] = 0 if !$rec[6];
386 $newrec_add->execute(@rec);
387 }
388 }
389
390 ## Default records
391 print "\n default records: ";
392 $defrecget->execute(1); # Vega 1.1.5/1.1.6 do not have default records for all groups;
393 # there is only support for one set of default records coded.
394 while (my @rec = $defrecget->fetchrow_array) {
395 $rec[0] = $newgid;
396 $rec[2] = $reverse_typemap{$vegatypes{$rec[2]}};
397 $rec[4] = 0 if !$rec[4];
398 $rec[5] = 0 if !$rec[5];
399 $rec[6] = 0 if !$rec[6];
400 $newdefrec->execute(@rec);
401 }
402
403 ## Log entries
404 print "\n log entries: ";
405 $logget->execute($oldgrpid);
406 while (my ($did,$uid,$gid,$email,$name,$entry,$stamp) = $logget->fetchrow_array) {
407 $did = $pcmap{d}{$did};
408 $uid = $pcmap{u}{$uid};
409 $gid = $pcmap{g}{$gid};
410 $stamp = localtime($stamp).'';
411 $logput->execute($did,$uid,$gid,$email,$name,'[V] '.$entry,$stamp);
412 }
413
414 print "\n done\n";
415}
Note: See TracBrowser for help on using the repository browser.