Ftp.pm 9.3 KB

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