Ftp.pm 9.5 KB

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