3 # Upgrade usermin if possible
5 require './usermin-lib.pl';
6 $access{'upgrade'} || &error($text{'acl_ecannot'});
7 &foreign_require("proc", "proc-lib.pl");
9 &get_usermin_miniserv_config(\%miniserv);
11 &ui_print_unbuffered_header(undef, $in{'install'} ? $text{'upgrade_title2'} : $text{'upgrade_title'}, "");
13 if ($in{'source'} == 0) {
15 &error_setup(&text('upgrade_err1', $in{'file'}));
17 if (!(-r $file)) { &inst_error($text{'upgrade_efile'}); }
19 elsif ($in{'source'} == 1) {
21 &error_setup($text{'upgrade_err2'});
25 &inst_error($text{'upgrade_ebrowser'});
27 &open_tempfile(MOD, ">$file", 0, 1);
28 &print_tempfile(MOD, $in{'upload'});
31 elsif ($in{'source'} == 2) {
32 # find latest version at www.webmin.com by looking at index page
33 &error_setup($text{'upgrade_err3'});
35 &http_download('www.webmin.com', 80, '/index6.html', $file, \$error);
36 $error && &inst_error($error);
39 if (/usermin-([0-9\.]+)\.tar\.gz/) {
47 if ($version == &get_usermin_version()) {
48 &inst_error(&text('upgrade_elatest', $version));
50 elsif ($version <= &get_usermin_version()) {
51 &inst_error(&text('upgrade_eversion', $version));
55 # Work out the current Usermin type (webmail or regular)
57 if (&has_command("rpm") && $in{'mode'} eq 'rpm' &&
58 &execute_command("rpm -q usermin-webmail") == 0) {
59 $product = "usermin-webmail";
61 elsif (&has_command("dpkg") && $in{'mode'} eq 'deb' &&
62 &execute_command("dpkg --list usermin-webmail") == 0) {
63 $product = "usermin-webmail";
66 if ($in{'mode'} eq 'rpm') {
67 $progress_callback_url = &convert_osdn_url(
68 "http://$webmin::osdn_host/webadmin/${product}-${version}-1.noarch.rpm");
70 elsif ($in{'mode'} eq 'deb') {
71 $progress_callback_url = &convert_osdn_url(
72 "http://$webmin::osdn_host/webadmin/${product}_${version}_all.deb");
75 $progress_callback_url = &convert_osdn_url(
76 "http://$webmin::osdn_host/webadmin/${product}-${version}.tar.gz");
78 ($host, $port, $page, $ssl) = &parse_http_url($progress_callback_url);
79 &http_download($host, $port, $page, $file, \$error,
80 \&progress_callback, $ssl);
81 $error && &inst_error($error);
84 elsif ($in{'source'} == 5) {
85 # Download from some URL
86 &error_setup(&text('upgrade_err5', $in{'url'}));
88 $progress_callback_url = $in{'url'};
89 if ($in{'url'} =~ /^(http|https):\/\/([^\/]+)(\/.*)$/) {
91 $host = $2; $page = $3; $port = $ssl ? 443 : 80;
92 if ($host =~ /^(.*):(\d+)$/) { $host = $1; $port = $2; }
93 &http_download($host, $port, $page, $file, \$error,
94 \&progress_callback, $ssl);
96 elsif ($in{'url'} =~ /^ftp:\/\/([^\/]+)(:21)?\/(.*)$/) {
97 $host = $1; $ffile = $3;
98 &ftp_download($host, $ffile, $file,
99 \$error, \&progress_callback);
101 else { &inst_error($text{'upgrade_eurl'}); }
103 $error && &inst_error($error);
105 $qfile = quotemeta($file);
107 # gunzip the file if needed
111 if ($two eq "\037\213") {
112 if (!&has_command("gunzip")) {
113 &inst_error($text{'upgrade_egunzip'});
115 $newfile = &transname();
116 $out = `gunzip -c $file 2>&1 >$newfile`;
119 &inst_error(&text('upgrade_egzip', "<tt>$out</tt>"));
121 unlink($file) if ($need_unlink);
125 $qfile = quotemeta($file);
127 # Get list of updates
128 $updatestemp = &transname();
129 &http_download($update_host, $update_port, $update_page, $updatestemp,
132 if ($in{'mode'} eq 'rpm') {
133 # Check if it is an RPM package
134 $out = `rpm -qp $qfile`;
135 $out =~ /^usermin-(\d+\.\d+)/ ||
136 $out =~ /^usermin-webmail-(\d+\.\d+)/ ||
137 &inst_error($text{'upgrade_erpm'});
139 if ($version <= &get_usermin_version() && !$in{'force'}) {
140 &inst_error(&text('upgrade_eversion', $version));
145 $cmd = "rpm -U --force $qfile";
148 $cmd = "rpm -U --ignoreos --ignorearch --nodeps $qfile";
150 print "<p>",&text($in{'install'} ? 'upgrade_setup2' : 'upgrade_setup',
151 "<tt>$cmd</tt>"),"<br>\n";
153 &proc::safe_process_exec($cmd, 0, 0, STDOUT, undef, 1);
155 unlink($file) if ($need_unlink);
157 elsif ($in{'mode'} eq 'deb') {
158 # Check if it is a Debian package
159 $out = `dpkg --info $qfile`;
160 $out =~ /Package:\s+(\S+)/ &&
161 ($1 eq "usermin" || $1 eq "usermin-webmail") ||
162 &inst_error($text{'upgrade_edeb'});
163 $out =~ /Version:\s+(\S+)/ ||
164 &inst_error($text{'upgrade_edeb'});
166 if ($version <= &get_usermin_version() && !$in{'force'}) {
167 &inst_error(&text('upgrade_eversion', $version));
170 # Install the package
171 $cmd = "dpkg --install $qfile";
172 print "<p>",&text($in{'install'} ? 'upgrade_setup2' : 'upgrade_setup',
173 "<tt>$cmd</tt>"),"<br>\n";
175 &proc::safe_process_exec($cmd, 0, 0, STDOUT, undef, 1);
177 unlink($file) if ($need_unlink);
180 # Check if it is a usermin tarfile
181 open(TAR, "tar tf $file 2>&1 |");
183 if (/^usermin-([0-9\.]+)\//) {
186 if (/^webmin-([0-9\.]+)\//) {
187 $webmin_version = $1;
189 if (/^[^\/]+\/(\S+)$/) {
194 if ($webmin_version) {
195 &inst_error(&text('upgrade_ewebmin', $webmin_version));
198 if ($hasfile{'module.info'}) {
199 &inst_error(&text('upgrade_emod', 'edit_mods.cgi'));
202 &inst_error($text{'upgrade_etar'});
206 if ($version <= &get_usermin_version()) {
207 &inst_error(&text('upgrade_eversion', $version));
213 if ($in{'install'}) {
214 # Installing .. extract it in /usr/local and run setup.sh
215 local $root = "/usr/local";
217 $out = `cd $root ; tar xf $file 2>&1 >/dev/null`;
219 &inst_error(&text('upgrade_euntar', "<tt>$out</tt>"));
221 unlink($file) if ($need_unlink);
222 $ENV{'config_dir'} = $standard_usermin_dir;
223 $ENV{'var_dir'} = "/var/usermin";
224 $perl = &get_perl_path();
225 $ENV{'perl'} = $perl;
227 $ENV{'port'} = 20000;
229 $ENV{'os_type'} = $gconfig{'os_type'};
230 $ENV{'os_version'} = $gconfig{'os_version'};
231 $ENV{'real_os_type'} = $gconfig{'real_os_type'};
232 $ENV{'real_os_version'} = $gconfig{'real_os_version'};
233 $cmd = "(cd $root/usermin-$version && ./setup.sh)";
234 print "<p>",&text('upgrade_setup2', "<tt>setup.sh</tt>"),"<br>\n";
237 # Upgrading .. work out where to extract
239 # Since we are currently installed in a fixed directory,
240 # just extract to a temporary location
241 $extract = &transname();
242 mkdir($extract, 0755);
245 # Next to the current directory
246 $extract = "$miniserv{'root'}/..";
249 # Extract it next to the current directory and run setup.sh
250 $out = `cd $extract ; tar xf $file 2>&1 >/dev/null`;
252 &inst_error(&text('upgrade_euntar', "<tt>$out</tt>"));
254 unlink($file) if ($need_unlink);
255 $ENV{'config_dir'} = $config{'usermin_dir'};
256 $ENV{'webmin_upgrade'} = 1;
257 $ENV{'autothird'} = 1;
258 $setup = $in{'dir'} ? "./setup.sh '$in{'dir'}'" : "./setup.sh";
260 $ENV{'deletedold'} = 1;
261 $cmd = "(cd $extract/usermin-$version && $setup && rm -rf \"$miniserv{'root'}\")";
264 $cmd = "(cd $extract/usermin-$version && $setup)";
266 print "<p>",&text('upgrade_setup', "<tt>setup.sh</tt>"),"<br>\n";
269 &proc::safe_process_exec($cmd, 0, 0, STDOUT, undef, 1);
272 # Can delete the temporary source directory
273 system("rm -rf \"$extract\"");
277 # Notify Webmin that this module might now be usable
278 &foreign_require("webmin", "webmin-lib.pl");
279 ($inst, $changed) = &webmin::build_installed_modules(0, 'usermin');
280 if (@$changed && defined(&theme_post_change_modules)) {
281 &theme_post_change_modules();
284 &webmin_log($in{'install'} ? "uinstall" : "upgrade", undef, undef,
285 { 'version' => $version, 'mode' => $in{'mode'} });
287 # Find out about any updates for this new version.
288 if ($updates_error) {
289 print "<br>",&text('upgrade_eupdates', $updates_error),"<p>\n";
292 open(UPDATES, $updatestemp);
294 if (/^([^\t]+)\t+([^\t]+)\t+([^\t]+)\t+([^\t]+)\t+(.*)/) {
295 push(@updates, [ $1, $2, $3, $4, $5 ]);
299 unlink($updatestemp);
300 $bversion = &base_version($version);
301 foreach $u (@updates) {
302 next if ($u->[1] >= $bversion + .01 || $u->[1] <= $bversion ||
303 $u->[1] <= $version);
304 local $osinfo = { 'os_support' => $u->[3] };
305 next if (!&check_usermin_os_support($osinfo));
309 print "<br>",&text('upgrade_updates', $ucount,
310 "update.cgi?source=0&show=0&missing=0"),"<p>\n";
314 &ui_print_footer("", $text{'index_return'});
318 unlink($file) if ($need_unlink);
319 print "<br><b>$whatfailed : $_[0]</b> <p>\n";
320 &ui_print_footer("", $text{'index_return'});