Ftp.pm 9.5 KB

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