Ftp.pm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. # This program is free software; you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License as published by
  3. # the Free Software Foundation; version 2 of the License.
  4. #
  5. # This program is distributed in the hope that it will be useful,
  6. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. # GNU General Public License for more details.
  9. #
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. package Dselect::Ftp;
  13. use Net::FTP;
  14. use Data::Dumper;
  15. use strict;
  16. use warnings;
  17. use Exporter qw(import);
  18. our @EXPORT = qw(%config yesno do_connect do_mdtm add_site edit_site
  19. edit_config read_config store_config view_mirrors nb);
  20. our %config;
  21. sub nb {
  22. my $nb = shift;
  23. if ($nb > 1024**2) {
  24. return sprintf("%.2fM", $nb / 1024**2);
  25. } elsif ($nb > 1024) {
  26. return sprintf("%.2fk", $nb / 1024);
  27. } else {
  28. return sprintf("%.2fb", $nb);
  29. }
  30. }
  31. sub read_config {
  32. my $vars = shift;
  33. my ($code, $conf);
  34. local($/);
  35. open(my $vars_fh, '<', $vars) ||
  36. die "Couldn't open $vars : $!\n" .
  37. "Try to relaunch the 'Access' step in dselect, thanks.\n";
  38. $code = <$vars_fh>;
  39. close $vars_fh;
  40. my $VAR1;
  41. $conf = eval $code;
  42. die "Couldn't eval $vars content : $@\n" if ($@);
  43. if (ref($conf) =~ /HASH/) {
  44. foreach (keys %{$conf}) {
  45. $config{$_} = $conf->{$_};
  46. }
  47. } else {
  48. print "Bad $vars file : removing it.\n";
  49. print "Please relaunch the 'Access' step in dselect. Thanks.\n";
  50. unlink $vars;
  51. exit 0;
  52. }
  53. }
  54. sub store_config {
  55. my $vars = shift;
  56. # Check that config is completed
  57. return if not $config{'done'};
  58. open(my $vars_fh, '>', $vars) ||
  59. die "Couldn't open $vars in write mode : $!\n";
  60. print $vars_fh Dumper(\%config);
  61. close $vars_fh;
  62. }
  63. sub view_mirrors {
  64. if (-f '/usr/lib/dpkg/methods/ftp/README.mirrors.txt') {
  65. system('pager', '/usr/lib/dpkg/methods/ftp/README.mirrors.txt');
  66. } elsif (-f '/usr/lib/dpkg/methods/ftp/README.mirrors.txt.gz') {
  67. system('gzip -dc /usr/lib/dpkg/methods/ftp/README.mirrors.txt.gz | pager');
  68. } else {
  69. print "/usr/lib/dpkg/methods/ftp/README.mirrors.txt(.gz): file not found.\n";
  70. }
  71. }
  72. sub edit_config {
  73. my $methdir = shift;
  74. my $i;
  75. #Get a config for ftp sites
  76. while(1) {
  77. $i = 1;
  78. print "\n\nList of selected ftp sites :\n";
  79. foreach (@{$config{'site'}}) {
  80. print "$i. ftp://$_->[0]$_->[1] @{$_->[2]}\n";
  81. $i++;
  82. }
  83. print "\nEnter a command (a=add e=edit d=delete q=quit m=mirror list) \n";
  84. print "eventually followed by a site number : ";
  85. chomp($_ = <STDIN>);
  86. /q/i && last;
  87. /a/i && add_site();
  88. /d\s*(\d+)/i &&
  89. do { splice(@{$config{'site'}}, $1-1, 1) if ($1 <= @{$config{'site'}});
  90. next;};
  91. /e\s*(\d+)/i &&
  92. do { edit_site($config{'site'}[$1-1]) if ($1 <= @{$config{'site'}});
  93. next; };
  94. m#m#i && view_mirrors();
  95. }
  96. print "\n";
  97. $config{'use_auth_proxy'} = yesno($config{'use_auth_proxy'} ? "y" : "n",
  98. "Go through an authenticated proxy");
  99. if ($config{'use_auth_proxy'}) {
  100. print "\nEnter proxy hostname [$config{'proxyhost'}] : ";
  101. chomp($_ = <STDIN>);
  102. $config{'proxyhost'} = $_ || $config{'proxyhost'};
  103. print "\nEnter proxy log name [$config{'proxylogname'}] : ";
  104. chomp($_ = <STDIN>);
  105. $config{'proxylogname'} = $_ || $config{'proxylogname'};
  106. print "\nEnter proxy password [$config{'proxypassword'}] : ";
  107. chomp ($_ = <STDIN>);
  108. $config{'proxypassword'} = $_ || $config{'proxypassword'};
  109. }
  110. print "\nEnter directory to download binary package files to\n";
  111. print "(relative to $methdir)\n";
  112. while(1) {
  113. print "[$config{'dldir'}] : ";
  114. chomp($_ = <STDIN>);
  115. s{/$}{};
  116. $config{'dldir'} = $_ if ($_);
  117. last if -d "$methdir/$config{'dldir'}";
  118. print "$methdir/$config{'dldir'} is not a directory !\n";
  119. }
  120. }
  121. sub add_site {
  122. my $pas = 1;
  123. my $user = "anonymous";
  124. my $email = `whoami`;
  125. chomp $email;
  126. $email .= '@' . `cat /etc/mailname || dnsdomainname`;
  127. chomp $email;
  128. my $dir = "/debian";
  129. push (@{$config{'site'}}, [ "", $dir, [ "dists/stable/main",
  130. "dists/stable/contrib",
  131. "dists/stable/non-free" ],
  132. $pas, $user, $email ]);
  133. edit_site($config{'site'}[@{$config{'site'}} - 1]);
  134. }
  135. sub edit_site {
  136. my $site = shift;
  137. local($_);
  138. print "\nEnter ftp site [$site->[0]] : ";
  139. chomp($_ = <STDIN>);
  140. $site->[0] = $_ || $site->[0];
  141. print "\nUse passive mode [" . ($site->[3] ? "y" : "n") ."] : ";
  142. chomp($_ = <STDIN>);
  143. $site->[3] = (/y/i ? 1 : 0) if ($_);
  144. print "\nEnter username [$site->[4]] : ";
  145. chomp($_ = <STDIN>);
  146. $site->[4] = $_ || $site->[4];
  147. print <<'EOF';
  148. If you're using anonymous ftp to retrieve files, enter your email
  149. address for use as a password. Otherwise enter your password,
  150. or "?" if you want dselect-ftp to prompt you each time.
  151. EOF
  152. print "Enter password [$site->[5]] : ";
  153. chomp($_ = <STDIN>);
  154. $site->[5] = $_ || $site->[5];
  155. print "\nEnter debian directory [$site->[1]] : ";
  156. chomp($_ = <STDIN>);
  157. $site->[1] = $_ || $site->[1];
  158. print "\nEnter space separated list of distributions to get\n";
  159. print "[@{$site->[2]}] : ";
  160. chomp($_ = <STDIN>);
  161. $site->[2] = [ split(/\s+/) ] if $_;
  162. }
  163. sub yesno($$) {
  164. my ($d, $msg) = @_;
  165. my ($res, $r);
  166. $r = -1;
  167. $r = 0 if $d eq "n";
  168. $r = 1 if $d eq "y";
  169. die "Incorrect usage of yesno, stopped" if $r == -1;
  170. while (1) {
  171. print $msg, " [$d]: ";
  172. $res = <STDIN>;
  173. $res =~ /^[Yy]/ and return 1;
  174. $res =~ /^[Nn]/ and return 0;
  175. $res =~ /^[ \t]*$/ and return $r;
  176. print "Please enter one of the letters 'y' or 'n'\n";
  177. }
  178. }
  179. ##############################
  180. sub do_connect {
  181. my($ftpsite,$username,$pass,$ftpdir,$passive,
  182. $useproxy,$proxyhost,$proxylogname,$proxypassword) = @_;
  183. my($rpass,$remotehost,$remoteuser,$ftp);
  184. TRY_CONNECT:
  185. while(1) {
  186. my $exit = 0;
  187. if ($useproxy) {
  188. $remotehost = $proxyhost;
  189. $remoteuser = $username . "@" . $ftpsite;
  190. } else {
  191. $remotehost = $ftpsite;
  192. $remoteuser = $username;
  193. }
  194. print "Connecting to $ftpsite...\n";
  195. $ftp = Net::FTP->new($remotehost, Passive => $passive);
  196. if(!$ftp || !$ftp->ok) {
  197. print "Failed to connect\n";
  198. $exit=1;
  199. }
  200. if (!$exit) {
  201. # $ftp->debug(1);
  202. if ($useproxy) {
  203. print "Login on $proxyhost...\n";
  204. $ftp->_USER($proxylogname);
  205. $ftp->_PASS($proxypassword);
  206. }
  207. print "Login as $username...\n";
  208. if ($pass eq "?") {
  209. print "Enter password for ftp: ";
  210. system("stty", "-echo");
  211. $rpass = <STDIN>;
  212. chomp $rpass;
  213. print "\n";
  214. system("stty", "echo");
  215. } else {
  216. $rpass = $pass;
  217. }
  218. if(!$ftp->login($remoteuser, $rpass))
  219. { print $ftp->message() . "\n"; $exit=1; }
  220. }
  221. if (!$exit) {
  222. print "Setting transfer mode to binary...\n";
  223. if(!$ftp->binary()) { print $ftp->message . "\n"; $exit=1; }
  224. }
  225. if (!$exit) {
  226. print "Cd to '$ftpdir'...\n";
  227. if(!$ftp->cwd($ftpdir)) { print $ftp->message . "\n"; $exit=1; }
  228. }
  229. if ($exit) {
  230. if (yesno ("y", "Retry connection at once")) {
  231. next TRY_CONNECT;
  232. } else {
  233. die "error";
  234. }
  235. }
  236. last TRY_CONNECT;
  237. }
  238. # if(!$ftp->pasv()) { print $ftp->message . "\n"; die "error"; }
  239. return $ftp;
  240. }
  241. ##############################
  242. # assume server supports MDTM - will be adjusted if needed
  243. my $has_mdtm = 1;
  244. my %months = ('Jan', 0,
  245. 'Feb', 1,
  246. 'Mar', 2,
  247. 'Apr', 3,
  248. 'May', 4,
  249. 'Jun', 5,
  250. 'Jul', 6,
  251. 'Aug', 7,
  252. 'Sep', 8,
  253. 'Oct', 9,
  254. 'Nov', 10,
  255. 'Dec', 11);
  256. sub do_mdtm {
  257. my ($ftp, $file) = @_;
  258. my ($time);
  259. #if ($has_mdtm) {
  260. $time = $ftp->mdtm($file);
  261. # my $code=$ftp->code(); my $message=$ftp->message();
  262. # print " [ $code: $message ] ";
  263. if ($ftp->code() == 502 # MDTM not implemented
  264. || $ftp->code() == 500 # command not understood (SUN firewall)
  265. ) {
  266. $has_mdtm = 0;
  267. } elsif (!$ftp->ok()) {
  268. return;
  269. }
  270. #}
  271. if (! $has_mdtm) {
  272. use Time::Local;
  273. my @files = $ftp->dir($file);
  274. if (($#files == -1) || ($ftp->code == 550)) { # No such file or directory
  275. return;
  276. }
  277. # my $code=$ftp->code(); my $message=$ftp->message();
  278. # print " [ $code: $message ] ";
  279. # print "[$#files]";
  280. # get the date components from the output of "ls -l"
  281. if ($files[0] =~
  282. /([^ ]+ *){5}[^ ]+ ([A-Z][a-z]{2}) ([ 0-9][0-9]) ([0-9 ][0-9][:0-9][0-9]{2})/) {
  283. my($month_name, $day, $yearOrTime, $month, $hours, $minutes,
  284. $year);
  285. # what we can read
  286. $month_name = $2;
  287. $day = 0 + $3;
  288. $yearOrTime = $4;
  289. # translate the month name into number
  290. $month = $months{$month_name};
  291. # recognize time or year, and compute missing one
  292. if ($yearOrTime =~ /([0-9]{2}):([0-9]{2})/) {
  293. $hours = 0 + $1; $minutes = 0 + $2;
  294. my @this_date = gmtime(time());
  295. my $this_month = $this_date[4];
  296. my $this_year = $this_date[5];
  297. if ($month > $this_month) {
  298. $year = $this_year - 1;
  299. } else {
  300. $year = $this_year;
  301. }
  302. } elsif ($yearOrTime =~ / [0-9]{4}/) {
  303. $hours = 0; $minutes = 0;
  304. $year = $yearOrTime - 1900;
  305. } else {
  306. die "Cannot parse year-or-time";
  307. }
  308. # build a system time
  309. $time = timegm (0, $minutes, $hours, $day, $month, $year);
  310. } else {
  311. die "Regexp match failed on LIST output";
  312. }
  313. }
  314. return $time;
  315. }
  316. 1;
  317. __END__