~ubuntu-branches/ubuntu/hardy/bugzilla/hardy-security

« back to all changes in this revision

Viewing changes to debian/bugzilla.postinst

  • Committer: Bazaar Package Importer
  • Author(s): Rémi Perrot
  • Date: 2004-04-02 01:13:32 UTC
  • Revision ID: james.westby@ubuntu.com-20040402011332-pbwk96wk60u5hbil
Tags: 2.16.5-2
Duplicate table creation is now also fixed in bugzilla.postinst
(closes: #224288)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
 
 
3
use strict;
 
4
use vars qw( $dsn $dbh 
 
5
             $mysql_host $mysql_port
 
6
             $mysql_name $mysql_user $mysql_user_pwd
 
7
             $mysql_root_name $mysql_root_pwd );
 
8
 
 
9
use Debconf::Client::ConfModule ':all';
 
10
 
 
11
use DBI;
 
12
 
 
13
if ( get('bugzilla/mysql_available') ne 'true' ) {
 
14
        print STDERR "You answer that there is no MySQL server "
 
15
                    ."suitable to support the bugzilla database. "
 
16
                    ."This makes bugzilla installation to failed\n";
 
17
        exit 1;
 
18
}
 
19
 
 
20
$mysql_host = get('bugzilla/mysql_host');
 
21
$mysql_port = get('bugzilla/mysql_port');
 
22
$mysql_name = get('bugzilla/mysql_name');
 
23
$mysql_user = get('bugzilla/mysql_user');
 
24
$mysql_user_pwd  = get('bugzilla/mysql_user_pwd');
 
25
        
 
26
my $mysql_need_root = get('bugzilla/mysql_need_root');
 
27
 
 
28
if ($mysql_need_root eq 'true') {
 
29
        $mysql_root_name =  get('bugzilla/mysql_root_name');
 
30
        $mysql_root_pwd =  get('bugzilla/mysql_root_pwd');
 
31
        $dsn = "DBI:mysql:;$mysql_host;$mysql_port";
 
32
        $dbh = DBI->connect($dsn, $mysql_root_name, $mysql_root_pwd)
 
33
                or die "Can't connect as admin to the database";
 
34
 
 
35
        create_database();
 
36
        
 
37
        $dbh = DBI->connect($dsn, $mysql_root_name, $mysql_root_pwd)
 
38
                or die "Can't connect as admin to the database";
 
39
 
 
40
        grant();
 
41
 
 
42
        $dbh->disconnect;
 
43
 
 
44
        reset('bugzilla/mysql_root_pwd');
 
45
        set('bugzilla/mysql_need_root','false');
 
46
}
 
47
 
 
48
$dsn = "DBI:mysql:$mysql_name;$mysql_host;$mysql_port";
 
49
$dbh = DBI->connect($dsn, $mysql_user, $mysql_user_pwd) 
 
50
        or die "Can't connect to $mysql_name database as $mysql_user";
 
51
 
 
52
create_profiles_tables();
 
53
populate_profiles();
 
54
 
 
55
$dbh->disconnect;
 
56
 
 
57
alter_localconfig();
 
58
 
 
59
system('/usr/share/bugzilla/lib/checksetup.pl 1>&2') == 0
 
60
        or die "checksetup.pl failed";
 
61
 
 
62
my $temp="set -e\nset -- @ARGV\n" . << 'EOF';
 
63
#DEBHELPER#
 
64
EOF
 
65
 
 
66
system ($temp) / 256 == 0
 
67
        or die "Problem with debhelper scripts: $!";
 
68
 
 
69
fix_www_data_perm('/var/lib/bugzilla');   #this should be done by checksetup.pl
 
70
fix_www_data_perm('/var/cache/bugzilla'); #but I dislike the way this is done.
 
71
 
 
72
 
 
73
exit 0;
 
74
 
 
75
sub alter_localconfig {
 
76
        # TODO: have localconfig rotate
 
77
        # TODO: don't change localconfig if there is no change on it
 
78
        umask 0027; #there is password in localconfig
 
79
        rename ('/etc/bugzilla/localconfig','/etc/bugzilla/localconfig.dpkg.old') 
 
80
                or die "Can't rename /etc/bugzilla/localconfig : $!";
 
81
        open (CONFIG_OLD,"</etc/bugzilla/localconfig.dpkg.old")
 
82
                or die "Can't open /etc/bugzilla/localconfig.dpkg.old : $!";
 
83
        open (CONFIG_NEW,">/etc/bugzilla/localconfig")
 
84
                or die "Cant't open /etc/bugzilla/localconfig : $!";
 
85
        while (<CONFIG_OLD>) {
 
86
                s/(\$db_host\s*=\s*)"[^"]*"/$1"$mysql_host"/; 
 
87
                s/(\$db_port\s*=\s*)\d+/$1$mysql_port/; 
 
88
                s/(\$db_name\s*=\s*)"[^"]*"/$1"$mysql_name"/; 
 
89
                s/(\$db_user\s*=\s*)"[^"]*"/$1"$mysql_user"/; 
 
90
                s/(\$db_pass\s*=\s*)"[^"]*"/$1"\Q$mysql_user_pwd\E"/; 
 
91
                print CONFIG_NEW $_ or die "Can't write in /etc/bugzilla/localconfig : $!" ;
 
92
        }
 
93
        close CONFIG_OLD;
 
94
        close CONFIG_NEW;
 
95
 
 
96
        my @www_pwent = getpwnam("www-data") 
 
97
                or die "Can't find numeric uid/gid of www-data";
 
98
        chown ($www_pwent[2], $www_pwent[3], '/etc/bugzilla/localconfig') 
 
99
                or die "Can't change the owner of /etc/bugzilla/localconfig"; 
 
100
}
 
101
 
 
102
sub fix_www_data_perm {
 
103
        my $path = shift;
 
104
        system(qq{chown -R www-data.www-data $path}) == 0
 
105
                or die "Can't fix owner of files under $path : $!";
 
106
        system(qq{find $path -type f -print0 | xargs -0 chmod 644}) == 0
 
107
                or die "Can't fix $path/* files perm : $!";
 
108
        system(qq{find $path -type d -print0 | xargs -0 chmod 755}) == 0
 
109
                or die "Can't fix $path/* dirs perm : $!";
 
110
}
 
111
 
 
112
sub create_database {
 
113
        my @databases = $dbh->func('_ListDBs');
 
114
        unless (grep /^$mysql_name$/, @databases) {
 
115
                $dbh->func('createdb', $mysql_name, "$mysql_host:$mysql_port",
 
116
                                       $mysql_root_name, $mysql_root_pwd, 'admin')
 
117
                or die "Can't create the $mysql_name";
 
118
        }
 
119
}
 
120
 
 
121
sub grant {
 
122
        my $fqdn;
 
123
        
 
124
        if ( $mysql_host eq "localhost" ) {
 
125
                $fqdn='localhost';
 
126
        } else {
 
127
                $fqdn=`hostname -f`;
 
128
        }
 
129
        $dbh->do("grant all on $mysql_name.* to $mysql_user\@$fqdn identified by '$mysql_user_pwd'")
 
130
                or die "Can't grant or create $mysql_user user";
 
131
}
 
132
 
 
133
sub create_profiles_tables {
 
134
        my $sth = $dbh->table_info(undef, undef, undef, "TABLE");
 
135
        my @tables = @{$dbh->selectcol_arrayref($sth, { Columns => [3] })};
 
136
        unless (grep /^profiles$/, @tables) {
 
137
                $dbh->do('create table profiles(
 
138
                          userid mediumint not null auto_increment primary key,
 
139
                          login_name varchar(255) not null,
 
140
                          cryptpassword varchar(64),
 
141
                          realname varchar(255),
 
142
                          groupset bigint not null,
 
143
                          disabledtext mediumtext,
 
144
                          mybugslink tinyint not null default 1,
 
145
                          blessgroupset bigint not null default 0,
 
146
                          emailflags mediumtext,
 
147
                          unique(login_name))')
 
148
                or die "Can't create profiles table";
 
149
        }
 
150
}
 
151
 
 
152
sub populate_profiles {
 
153
        my $bugzilla_admin_name = get('bugzilla/bugzilla_admin_name');
 
154
        my $bugzilla_admin_real_name = get('bugzilla/bugzilla_admin_real_name');
 
155
        my $bugzilla_admin_pwd = get('bugzilla/bugzilla_admin_pwd');
 
156
 
 
157
 
 
158
        my $sth = $dbh->prepare(q{select userid 
 
159
                                    from profiles
 
160
                                   where login_name=?})
 
161
                        or die "Can't prepare login selection";
 
162
        $sth->execute($bugzilla_admin_name) 
 
163
                or die "Can't execute login selection";
 
164
 
 
165
        (my $userid) = $sth->fetchrow_array;
 
166
        
 
167
        $sth->finish;
 
168
        
 
169
        if ( defined $userid ) {
 
170
                $dbh->do(q{update profiles
 
171
                              set realname= ?,
 
172
                                   cryptpassword= ?,
 
173
                                   groupset=0x7fffffffffffffff
 
174
                             where userid= ? },
 
175
                         undef,
 
176
                         $bugzilla_admin_real_name,Crypt($bugzilla_admin_pwd),$userid )
 
177
                        or die "Can't update bugzilla admin profile";
 
178
        } else {
 
179
                $dbh->do(q{insert into profiles
 
180
                                ( login_name,
 
181
                                     realname,
 
182
                                        cryptpassword,
 
183
                                           groupset)
 
184
                          values( ?,
 
185
                                     ?,
 
186
                                        ?,
 
187
                                           0x7fffffffffffffff)},
 
188
                         undef,
 
189
                                  $bugzilla_admin_name,
 
190
                                     $bugzilla_admin_real_name,
 
191
                                        Crypt($bugzilla_admin_pwd)) 
 
192
                        or die "Can't create the bugzilla admin profile";
 
193
        }
 
194
}
 
195
 
 
196
sub Crypt {
 
197
    my ($password) = @_;
 
198
    my @saltchars = (0..9, 'A'..'Z', 'a'..'z', '.', '/');
 
199
    my $salt = '';
 
200
    for ( my $i=0 ; $i < 8 ; ++$i ) {
 
201
        $salt .= $saltchars[rand(64)];
 
202
    }
 
203
 
 
204
    my $cryptedpassword = crypt($password, $salt);
 
205
 
 
206
    return $cryptedpassword;
 
207
}
 
208