summaryrefslogtreecommitdiffstats
path: root/mdkupdate
blob: 1b698e9ec002daa85ff6031faf32d460abb4f5ce (plain)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/perl
################################################################################
# Mdkupdate                                                                    # 
#                                                                              #
# Copyright (C) 2002 MandrakeSoft                                              #
#
# Daouda Lo <daouda@mandrakesoft.com>                                          #
#                                                                              #
# This program is free software; you can redistribute it and/or modify         #
# it under the terms of the GNU General Public License Version 2 as            #
# published by the Free Software Foundation.                                   #
#                                                                              #
# This program is distributed in the hope that it will be useful,              #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with this program; if not, write to the Free Software                  #
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.   #
################################################################################

use POSIX;
use Digest::MD5  qw(md5 md5_hex md5_base64);
use HTTP::Request;
use HTTP::Request::Common;
use LWP::UserAgent;
use Net::HTTPS; #for https connections 
use MIME::Base64 qw(encode_base64);
use MDK::Common;
use lib qw(/usr/lib/libDrakX);
use Locale::gettext;

#- I18N.
setlocale (LC_ALL, "");
textdomain ("mdkupdate");

sub _ {
    my ($format, @params) = @_;
    sprintf(Locale::gettext::gettext($format), @params);
}

my $logfile = "/var/log/mdkupdate.log";
my $url_upload_diff = "https://www.mandrakeonline.net/online_dif.php";
my $url_get_rpms = "https://www.mandrakeonline.net/online_update.php";
my $rpms_rep = "/root/tmp/";
my $VERSION = "0.17";
my $security = grep { /^-?-security$/ } @ARGV;
my $update = grep { /^-?-update$/ } @ARGV;

sub usage {
    print STDERR _("mdkupdate version %s
Copyright (C) 2002 MandrakeSoft.
This is free software and may be redistributed under the terms of the GNU GPL.

usage:
", $VERSION) . _("  --help         - print this help message.
") . _("  --security     - use only security media.
") . _("  --update       - update all information.
") . _("  -v             - verbose mode.
");
    exit(0);
}

grep { $_ !~ /^-?-(v|update|security)$/ } @ARGV and usage;

my %o = getVarsFromSh("/root/.mdkupdate");
update_rpms($o{LOGIN},$o{PASS},$o{BOX},$o{CURRENTKEY});
rpm_qa("/root/rpm_qa_installed_after");
my %new = getVarsFromSh("/root/.mdkupdate");
if (! -s "/root/.mdkupdate.rpms" || $update) {
    #- send configuration and get back key to use...
    $new{CURRENTKEY} = send_config($new{LOGIN},$new{PASS},$new{BOX});
    delete $new{OLDKEY};
    #- save back keys.
    setVarsInSh("/root/.mdkupdate", \%new);
} else {
    #- this can safely be ignored if new configuration is sent.
    send_rpm_dif($new{LOGIN},$new{PASS},$new{BOX},$new{OLDKEY});
}
clean_dir();

################################################################################
# taken from mdkonline to perform complete update of rpm list (by diff), it has
# been decided to do exactly what mdkonline does the first time to update the db.
################################################################################
sub report_config {
    my ($file) = @_;
sub header { "
********************************************************************************
* $_[0]
********************************************************************************";
	 }
open (FILE,"> $file") || die "Couldn't open $file : $!";
map { chomp; print FILE "$_\n" }
    header("scsi"), cat_("/proc/scsi/scsi"),
    header("lsmod"), cat_("/proc/modules"),
    header("cmdline"), cat_("/proc/cmdline"),
    header("pcmcia: stab"), cat_("$prefix/var/lib/pcmcia/stab") || cat_("$prefix/var/run/stab"),
    header("usb"), cat_("/proc/bus/usb/devices"),
    header("partitions"), cat_("/proc/partitions"),
    header("cpuinfo"), cat_("/proc/cpuinfo"),
    header("install.log"), cat_("$prefix/root/drakx/install.log"),
    header("fstab"), cat_("$prefix/etc/fstab"),
    header("lilo.conf"), cat_("$prefix/etc/lilo.conf"),
    header("menu.lst"), cat_("$prefix/boot/grub/menu.lst"),
    header("/etc/modules.conf"), cat_("$prefix/etc/modules.conf"),
    header("rpm -qa"), join ('', sort `rpm -qa`),
    header("mandrake version"), cat_('/etc/redhat-release');
close(FILE);
}
sub send_config {
    # When we arrive here, we're sure the login/passwd is correct
    my ($login, $passwd, $box_name) = @_;
#    print STDERR "Sending config\n";
    my $result = -1;
    
    report_config("/root/$login.$passwd.$box_name.online.log");
    `/usr/bin/bzip2 \\\-9 \\\-f /root/$login.$passwd.$box_name.online.log`;

    # Turn the binary file into a uuencoded ascii file
    open (FILE, "/root/$login.$passwd.$box_name.online.log.bz2") or die "$!";
    my ($chunk, $buffer);
    while (read(FILE, $chunk, 60*57)) {
	$buffer .= $chunk;
    }
    close (FILE);
    open (FILEOUT, "> /root/$login.$passwd.$box_name.online.log.bz2.uue") or die "$!";
	print FILEOUT encode_base64($buffer);
    close (FILEOUT);

    my $ua = LWP::UserAgent->new;
    $ua->agent("MdkOnlineAgent/$VERSION" . $ua->agent);
    my $response = $ua->request(POST 'https://www.mandrakeonline.net/wizard.php',

				Content_Type => 'form-data',
				Content => [submit => "upload_wizard",
					    wizard => ["/root/$login.$passwd.$box_name.online.log.bz2.uue"]
					    ]);
    # Check the outcome of the response
    #print "REPONSE: ".$response->content."\n";
    if ($response->is_success) {
	$result = ($response->content =~ /TRUE(.*)/) ? 0 : -1;
	#print("key is $1\n");
	$key = $1; 
	#- update local copy now.
	unlink "/root/.mdkupdate.rpms";
	rename "/root/rpm_qa_installed_after", "/root/.mdkupdate.rpms";
    } else {
	# pb with the connection ?
	$result = -1;
    }

    unlink "/root/$login.$passwd.$box_name.online.log.bz2";
    unlink "/root/$login.$passwd.$box_name.online.log.bz2.uue";

    $result == 0 && $key;
}
################################################################################

sub update_rpms {
    my ($login,$password,$box_name,$curkey) =@_;
    my $result = -1;
    my $string;
    
    if ($login && $password && $login !~ /\s+/ && $password !~ /\s+/) {
	my $ua = LWP::UserAgent->new;
	$ua->agent("MdkUpdateAgent/$VERSION" . $ua->agent);
	my $request = HTTP::Request->new(GET => $url_get_rpms.'?log='.$login.'&pass='.$password.'&host='.$box_name.'&key='.$curkey);
	my $response = $ua->request($request);
	$string = $response->content;
	if ($response->is_success) {
	    $result = ($response->content =~ /TRUE/) ? 0 : -1;
	} else {
	    log_i(_("Connection problem")."\n"._("MandrakeUpdate could not contact the site, we will try again."));
	    clean_dir();
	    exit 1;
	}
    } else {
	$result = -1; 
    }
    # if correct, return 0
    if (! $result) {
	@str_m = split '\n', $string;
	my %l = getVarsFromSh "/root/.mdkupdate";
	if ($str_m[0] eq 'TRUE') {
	    setVarsInSh("/root/.mdkupdate", {
		OLDKEY => $str_m[2],
		CURRENTKEY => $str_m[1],
		MIRROR => $l{MIRROR},
		VER => $l{VER},
		BOX => $l{BOX},
		PASS => $l{PASS},
		LOGIN => $l{LOGIN} ,
	    });
	    my @junk= splice(@str_m,0,3);
	    my $mir_full = "ftp://$l{MIRROR}/$l{VER}/RPMS/";
	    update_packages($mir_full,@str_m);
	} else { log_i("problem occur $str_m\n"); }
	    	    
    } else {
	log_i(_("Your login or password may be wrong")."\n"._("You need to have an account on MandrakeOnline, or update your subscription.")."\n"._("For any problem send an e-mail to support\@mandrakeonline.net\n"));
	clean_dir();
	exit 1;
    }
}

sub update_packages {
    my ($mir,@str) = @_;
    @str or return;
    eval {
	system "/usr/sbin/urpmi.removemedia", "mdkupdate";
	system "/usr/sbin/urpmi.addmedia", "mdkupdate", $mir;
	$? == 0 or die _("Unable to create mdkupdate medium.\n");
	system "/usr/sbin/urpmi", "--auto", "--media", "mdkupdate", map { /^(.*)\.rpm$/ && $1 } @str;
	$? == 0 or die _("Unable to update packages from mdkupdate medium.\n");
    };
    $@ and die;
#    chdir($rpms_rep) or log_i("\ncannot chdir to $rpms_rep\n");
#    $full_rpm = join(' ',@str);
#    print "@str\n";
#    foreach (@str) {
#	-x "/usr/bin/wget" or die _("wget is missing\n");
#	`/usr/bin/wget -nc $mir$_`;
#    }
#    `urpmi $full_rpm`;
#    foreach $pack (@str) {
#	system("rm -f $pack");
#    }	
}

sub send_rpm_dif {
    my ($login,$password,$box_name,$oldkey) =@_;
    #`sdiff -s /root/rpm_qa_installed_after /root/rpm_qa_installed_before >/root/$login.$password.$box_name.$oldkey.dif`;
    `sdiff -s /root/rpm_qa_installed_after /root/.mdkupdate.rpms >/root/$login.$password.$box_name.$oldkey.dif`;
    my $ua = LWP::UserAgent->new;
    $ua->agent("MdkOnlineAgent/$VERSION" . $ua->agent);
    my $response = $ua->request(POST "https://www.mandrakeonline.net/online_dif.php",

				Content_Type => 'form-data',
				Content => [submit => "upload_dif",
					    dif_file => ["/root/$login.$password.$box_name.$oldkey.dif"]
					    ]);
    #- update .mdkupdate.rpms with newer version just sent.
    if ($response->content =~ /TRUE/) {
	unlink "/root/.mdkupdate.rpms";
	rename "/root/rpm_qa_installed_after", "/root/.mdkupdate.rpms";
    }

    # Check the outcome of the response
    print "REPONSE: ".$response->content."\n";
}

sub clean_dir {
    system("rm -f /root/*.dif /root/rpm_qa_installed_before /root/rpm_qa_installed_after");    
}

sub rpm_qa {
    my ($file) = @_;
    open (FILE,"> $file") || die "Couldn't open $file : $!"; 
    map { chomp; print FILE "$_" } join('',sort `rpm -qa`);
    close FILE;
}

sub log_i {
    local *LOG;
    open LOG, ">>/var/log/mdkupdate.log" or die "can't output to log file\n";
    print LOG @_;
    close LOG;
}

sub fatal {
    my ($comment)=@_;
    printf STDERR "%s\n", $comment; exit($_[0]);
}