source: trunk/vega-import.pl@ 451

Last change on this file since 451 was 320, checked in by Kris Deugau, 13 years ago

/trunk

Administrivia cleanup - overdue to update the copyright dates

Not sure how I managed this, but the copy of the GPL in COPYING
got pasted in the file twice. O_o Recopied from the FSF website,
diff confirms just removal of the duplicate

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