Ftp.pm 9.5 KB

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