File: bugzilla.postinst

package info (click to toggle)
bugzilla 2.14.2-0woody4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,460 kB
  • ctags: 435
  • sloc: perl: 19,533; sh: 235; makefile: 180
file content (199 lines) | stat: -rw-r--r-- 5,965 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/perl -w

use strict;
use vars qw( $dsn $dbh 
	     $mysql_host $mysql_port
	     $mysql_name $mysql_user $mysql_user_pwd
	     $mysql_root_name $mysql_root_pwd );

use Debconf::Client::ConfModule ':all';

use DBI;

# TODO
# Upgrade from pre 2.13+cvs20010819 will make clear passwd to
# be loss so it seems to be a good idea to dump database
# before the running checksetup.pl.
# Downgrade to pre 2.13+cvs20010819 will make all account unsuable
# while there is no way to get back the clear password

$mysql_host = get('bugzilla/mysql_host');
$mysql_port = get('bugzilla/mysql_port');
$mysql_name = get('bugzilla/mysql_name');
$mysql_user = get('bugzilla/mysql_user');
$mysql_user_pwd  = get('bugzilla/mysql_user_pwd');
	
my $mysql_need_root = get('bugzilla/mysql_need_root');

if ($mysql_need_root eq 'true') {
	$mysql_root_name =  get('bugzilla/mysql_root_name');
	$mysql_root_pwd =  get('bugzilla/mysql_root_pwd');
	$dsn = "DBI:mysql:;$mysql_host;$mysql_port";
	$dbh = DBI->connect($dsn, $mysql_root_name, $mysql_root_pwd)
		or die "Can't connect as admin to the database";

	create_database();
	
	$dbh = DBI->connect($dsn, $mysql_root_name, $mysql_root_pwd)
		or die "Can't connect as admin to the database";

	grant();

	$dbh->disconnect;

	reset('bugzilla/mysql_root_pwd');
	set('bugzilla/mysql_need_root','false');
}

$dsn = "DBI:mysql:$mysql_name;$mysql_host;$mysql_port";
$dbh = DBI->connect($dsn, $mysql_user, $mysql_user_pwd) 
	or die "Can't connect to $mysql_name database as $mysql_user";

create_profiles_tables();
populate_profiles();

$dbh->disconnect;

alter_localconfig();

system('/usr/share/bugzilla/lib/checksetup.pl 1>&2') == 0
	or die "checksetup.pl failed";

my $temp="set -e\nset -- @ARGV\n" . << 'EOF';
#DEBHELPER#
EOF

system ($temp) / 256 == 0
	or die "Problem with debhelper scripts: $!";

fix_var_perm(); #this should be done by checksetup.pl


exit 0;

sub alter_localconfig {
	# TODO: have localconfig rotate
	# TODO: don't change localconfig if there is no change on it
	umask 0027; #there is password in localconfig
	rename ('/etc/bugzilla/localconfig','/etc/bugzilla/localconfig.dpkg.old') 
		or die "Can't rename /etc/bugzilla/localconfig : $!";
	open (CONFIG_OLD,"</etc/bugzilla/localconfig.dpkg.old")
		or die "Can't open /etc/bugzilla/localconfig.dpkg.old : $!";
	open (CONFIG_NEW,">/etc/bugzilla/localconfig")
		or die "Cant't open /etc/bugzilla/localconfig : $!";
	while (<CONFIG_OLD>) {
		s/(\$db_host\s*=\s*)"[^"]*"/$1"$mysql_host"/; 
		s/(\$db_port\s*=\s*)\d+/$1$mysql_port/; 
		s/(\$db_name\s*=\s*)"[^"]*"/$1"$mysql_name"/; 
		s/(\$db_user\s*=\s*)"[^"]*"/$1"$mysql_user"/; 
		s/(\$db_pass\s*=\s*)"[^"]*"/$1"$mysql_user_pwd"/; 
		print CONFIG_NEW $_ or die "Can't write in /etc/bugzilla/localconfig : $!" ;
	}
	close CONFIG_OLD;
	close CONFIG_NEW;

	my @www_pwent = getpwnam("www-data") 
		or die "Can't find numeric uid/gid of www-data";
	chown ($www_pwent[2], $www_pwent[3], '/etc/bugzilla/localconfig') 
		or die "Can't change the owner of /etc/bugzilla/localconfig"; 
}

sub fix_var_perm {
	system('chown -R www-data.www-data /var/lib/bugzilla/') == 0
		or die "Can't fix owner of files under /var/lib/bugzilla/ : $!";
	system('find /var/lib/bugzilla/ -type f -printf "\'%p\'\n" | xargs chmod 644') == 0
		or die "Can't fix /var/lib/bugzilla/* files perm : $!";
	system('find /var/lib/bugzilla/ -type d | xargs chmod 755') == 0
		or die "Can't fix /var/lib/bugzilla/* dirs perm : $!";
}

sub create_database {
	my @databases = $dbh->func('_ListDBs');
	unless (grep /^$mysql_name$/, @databases) {
		$dbh->func('createdb', $mysql_name, "$mysql_host:$mysql_port",
				       $mysql_root_name, $mysql_root_pwd, 'admin')
            	or die "Can't create the $mysql_name";
	}
}

sub grant {
	my $fqdn;
	
	if ( $mysql_host eq "localhost" ) {
		$fqdn='localhost';
	} else {
		$fqdn=`hostname -f`;
	}
	$dbh->do("grant all on $mysql_name.* to $mysql_user\@$fqdn identified by '$mysql_user_pwd'")
		or die "Can't grant or create $mysql_user user";
}

sub create_profiles_tables {
	my @tables = $dbh->func('_ListTables');
	unless (grep /^profiles$/, @tables) {
		$dbh->do('create table profiles(
			  userid mediumint not null auto_increment primary key,
	    		  login_name varchar(255) not null,
    			  cryptpassword varchar(64),
	    		  realname varchar(255),
    			  groupset bigint not null,
  	  		  disabledtext mediumtext,
    			  mybugslink tinyint not null default 1,
    			  blessgroupset bigint not null default 0,
    			  emailflags mediumtext,
			  unique(login_name))')
		or die "Can't create profiles table";
	}
}

sub populate_profiles {
	my $bugzilla_admin_name = get('bugzilla/bugzilla_admin_name');
	my $bugzilla_admin_real_name = get('bugzilla/bugzilla_admin_real_name');
	my $bugzilla_admin_pwd = get('bugzilla/bugzilla_admin_pwd');


	my $sth = $dbh->prepare(q{select userid 
				    from profiles
				   where login_name=?})
			or die "Can't prepare login selection";
	$sth->execute($bugzilla_admin_name) 
		or die "Can't execute login selection";

	(my $userid) = $sth->fetchrow_array;
	
	$sth->finish;
	
	if ( defined $userid ) {
		$dbh->do("update profiles
			      set realname='$bugzilla_admin_real_name',
				   cryptpassword='".Crypt($bugzilla_admin_pwd)."',
				   groupset=0x7fffffffffffffff
			     where userid=$userid")
			or die "Can't update bugzilla admin profile";
	} else {
		$dbh->do("insert into profiles
				( login_name,
				     realname,
					cryptpassword,
					   groupset)
			  values( '$bugzilla_admin_name',
			  	     '$bugzilla_admin_real_name',
					'".Crypt($bugzilla_admin_pwd)."',
					   0x7fffffffffffffff)") 
			or die "Can't create the bugzilla admin profile";
	}
}

sub Crypt {
    my ($password) = @_;
    my @saltchars = (0..9, 'A'..'Z', 'a'..'z', '.', '/');
    my $salt = '';
    for ( my $i=0 ; $i < 8 ; ++$i ) {
        $salt .= $saltchars[rand(64)];
    }

    my $cryptedpassword = crypt($password, $salt);

    return $cryptedpassword;
}