Ftp.pm 9.5 KB

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