1 | #!/usr/bin/perl
|
---|
2 | # Import script for VegaDNS data
|
---|
3 | # $Id: vega-import.pl 171 2011-11-25 23:02:28Z 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;
|
---|
35 |
|
---|
36 | if ($mode eq 'replace') {
|
---|
37 | print "WARNING: Using 'replace' will **DELETE** all existing group,\n".
|
---|
38 | "user, domain, and record data. Once started this cannot be\n".
|
---|
39 | "reversed. Enter 'yes' to proceed: ";
|
---|
40 | my $resp = <STDIN>;
|
---|
41 | chomp $resp;
|
---|
42 | if ($resp ne 'yes') {
|
---|
43 | print "Aborting; no changes made.\n";
|
---|
44 | exit;
|
---|
45 | } else {
|
---|
46 | print "OK, continuing.\n";
|
---|
47 | $newdbh->do("DELETE FROM users") or warn "error deleting users: ".$newdbh->errstr."\n";
|
---|
48 | $newdbh->do("DELETE FROM log") or warn "error deleting log: ".$newdbh->errstr."\n";
|
---|
49 | $newdbh->do("DELETE FROM default_records") or warn "error deleting defrecs: ".$newdbh->errstr."\n";
|
---|
50 | $newdbh->do("DELETE FROM records") or warn "error deleting records: ".$newdbh->errstr."\n";
|
---|
51 | $newdbh->do("DELETE FROM domains") or warn "error deleting domains: ".$newdbh->errstr."\n";
|
---|
52 | $newdbh->do("DELETE FROM groups") or warn "error deleting groups: ".$newdbh->errstr."\n";
|
---|
53 | $newdbh->do("DELETE FROM permissions") or warn "error deleting permissions: ".$newdbh->errstr."\n";
|
---|
54 | # set seq id on permissions, since we merged the two tables, and the new permission IDs will not match the old
|
---|
55 | $newdbh->do("SELECT pg_catalog.setval(pg_catalog.pg_get_serial_sequence('permissions', 'permission_id'),".
|
---|
56 | "1, true)") or warn "couldn't set permission sequence: ".$newdbh->errstr."\n";
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | my $vegadbh = DBI->connect('DBI:mysql:database=vegadns;host=localhost', 'vegadns', 'secret', {
|
---|
61 | PrintError => 1,
|
---|
62 | PrintWarn => 1,
|
---|
63 | AutoCommit => 1
|
---|
64 | });
|
---|
65 |
|
---|
66 | my $grpsubs = $vegadbh->prepare("SELECT group_id FROM groups ORDER BY group_id");
|
---|
67 | my $grpdata = $vegadbh->prepare("SELECT group_id, parent_group_id, name FROM groups WHERE group_id=?");
|
---|
68 | my $grppget = $vegadbh->prepare("SELECT perm_id,inherit_group_perms,accouedit,accoucreate,accoudelete,".
|
---|
69 | "self_edit,group_edit,group_create,group_delete,domain_edit,domain_create,domain_delete,record_edit,".
|
---|
70 | "record_create,record_delete FROM group_permissions WHERE group_id=?");
|
---|
71 | my $userget = $vegadbh->prepare("SELECT user_id, group_id, email, password, first_name, last_name, phone, ".
|
---|
72 | "account_type, status FROM accounts WHERE group_id=?");
|
---|
73 | my $userpget = $vegadbh->prepare("SELECT perm_id,inherit_group_perms,accouedit,accoucreate,accoudelete,".
|
---|
74 | "self_edit,group_edit,group_create,group_delete,domain_edit,domain_create,domain_delete,record_edit,".
|
---|
75 | "record_create,record_delete FROM user_permissions WHERE user_id=?");
|
---|
76 |
|
---|
77 | my $newgrp = $newdbh->prepare("INSERT INTO groups (group_id,parent_group_id,group_name,permission_id) ".
|
---|
78 | "VALUES (?,?,?,?)");
|
---|
79 | my $newgrppset = $newdbh->prepare("INSERT INTO permissions (self_edit, group_create, ".
|
---|
80 | "group_edit, group_delete, user_create, user_edit, user_delete, domain_create, domain_edit, ".
|
---|
81 | "domain_delete, record_create, record_edit, record_delete, group_id) ".
|
---|
82 | "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
|
---|
83 | my $newpid = $newdbh->prepare("SELECT currval('permissions_permission_id_seq')");
|
---|
84 | my $newuser = $newdbh->prepare("INSERT INTO users (user_id, group_id, username, password, firstname, ".
|
---|
85 | "lastname, phone, type, status, permission_id, inherit_perm) VALUES (?,?,?,?,?,?,?,?,?,?,'f')");
|
---|
86 | my $newuserpset = $newdbh->prepare("INSERT INTO permissions (self_edit, group_create, ".
|
---|
87 | "group_edit, group_delete, user_create, user_edit, user_delete, domain_create, domain_edit, ".
|
---|
88 | "domain_delete, record_create, record_edit, record_delete, user_id) ".
|
---|
89 | "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
|
---|
90 |
|
---|
91 | my $foo = qq(
|
---|
92 |
|
---|
93 | perm_id | group_id | inherit_group_perms | accouedit | accoucreate | accoudelete | self_edit | group_edit |
|
---|
94 | group_create | group_delete | domain_edit | domain_create | domain_delegate | domain_delete | record_edit |
|
---|
95 | record_create | record_delete | record_delegate | default_record_edit | default_record_create |
|
---|
96 | default_record_delete | rrtype_allow_n | rrtype_allow_a | rrtype_allow_3 | rrtype_allow_6 | rrtype_allow_m |
|
---|
97 | rrtype_allow_p | rrtype_allow_t | rrtype_allow_v | rrtype_allow_all
|
---|
98 |
|
---|
99 | permission_id | admin | self_edit | group_create | group_edit | group_delete | user_create | user_edit |
|
---|
100 | user_delete | domain_create | domain_edit | domain_delete | record_create | record_edit | record_delete |
|
---|
101 | user_id | group_id
|
---|
102 |
|
---|
103 | );
|
---|
104 |
|
---|
105 | my $stage = 'start';
|
---|
106 | $grpsubs->execute;
|
---|
107 | my $grp = 0;
|
---|
108 | while (($grp) = $grpsubs->fetchrow_array()) {
|
---|
109 | dogroup($grp);
|
---|
110 | }
|
---|
111 | # set group seq to $grp+1
|
---|
112 | exit;
|
---|
113 |
|
---|
114 | sub dogroup {
|
---|
115 | my $grpid = shift;
|
---|
116 | $grpdata->execute($grpid) or warn $grpdata->errstr;
|
---|
117 | my $oldgrp = $grpdata->fetchrow_hashref() or warn $grpdata->errstr;
|
---|
118 | print "group id $grpid, name $oldgrp->{name}\n";
|
---|
119 |
|
---|
120 | $grppget->execute($grpid) or warn $grppget->errstr;
|
---|
121 | my $oldp = $grppget->fetchrow_hashref() or warn $grppget->errstr;
|
---|
122 |
|
---|
123 | # de-nullify
|
---|
124 | foreach my $permtype ('self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
|
---|
125 | 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
|
---|
126 | 'record_edit', 'record_delete') {
|
---|
127 | $oldp->{$permtype} = 'f' if !defined($oldp->{$permtype});
|
---|
128 | }
|
---|
129 | $newgrppset->execute($oldp->{self_edit}, $oldp->{group_create}, $oldp->{group_edit}, $oldp->{group_delete},
|
---|
130 | $oldp->{user_create}, $oldp->{user_edit}, $oldp->{user_delete},
|
---|
131 | $oldp->{domain_create}, $oldp->{domain_edit}, $oldp->{domain_delete},
|
---|
132 | $oldp->{record_create}, $oldp->{record_edit}, $oldp->{record_delete},
|
---|
133 | $grpid) or warn $newgrppset->errstr;
|
---|
134 | $newpid->execute;
|
---|
135 | my ($pid) = $newpid->fetchrow_array;
|
---|
136 | $newgrp->execute($grpid, $oldgrp->{parent_group_id}, $oldgrp->{name}, $pid);
|
---|
137 |
|
---|
138 | ## Users
|
---|
139 | print " users:\n";
|
---|
140 | $userget->execute($grpid);
|
---|
141 | while (my $user = $userget->fetchrow_hashref) {
|
---|
142 | $userpget->execute($user->{user_id}) or warn "failed permission get on ".$user->{user_id}."\n";
|
---|
143 | my $oldp = $userpget->fetchrow_hashref;
|
---|
144 | # de-nullify
|
---|
145 | foreach my $permtype ('self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
|
---|
146 | 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
|
---|
147 | 'record_edit', 'record_delete') {
|
---|
148 | $oldp->{$permtype} = 'f' if !defined($oldp->{$permtype});
|
---|
149 | }
|
---|
150 | $newuserpset->execute($oldp->{self_edit}, $oldp->{group_create}, $oldp->{group_edit}, $oldp->{group_delete},
|
---|
151 | $oldp->{user_create}, $oldp->{user_edit}, $oldp->{user_delete},
|
---|
152 | $oldp->{domain_create}, $oldp->{domain_edit}, $oldp->{domain_delete},
|
---|
153 | $oldp->{record_create}, $oldp->{record_edit}, $oldp->{record_delete},
|
---|
154 | $user->{user_id}) or warn $newgrppset->errstr;
|
---|
155 | $newpid->execute;
|
---|
156 | my ($pid) = $newpid->fetchrow_array;
|
---|
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);
|
---|
161 | $newuser->execute($user->{user_id}, $user->{group_id}, $user->{email}, $user->{password},
|
---|
162 | $user->{first_name}, $user->{last_name}, $user->{phone}, $user->{account_type}, $user->{status}, $pid)
|
---|
163 | or warn " new user ".$user->{email}." (".$user->{user_id}.") failed: ".$newuser->errstr."\n";
|
---|
164 | print " user ".$user->{email}." (".$user->{user_id}.")\n";
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | }
|
---|