Ftp.pm 8.7 KB

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