Code / Perl / .fishsrv.pl

  1. #!/usr/bin/perl
  2. =pod
  3. This file was transferred by kio_fish, a network client part of the
  4. KDE project. You may safely delete it, it will be transferred again
  5. when needed. It's only purpose is to make kio_fish access faster and
  6. more reliable.
  7. =cut
  8. use Fcntl;
  9. $|++;
  10. #open(DEBUG,">/tmp/kio_fish.debug.$$.log");
  11. if (defined $code) {
  12. unlink('.fishsrv.pl');
  13. sysopen(FH,'.fishsrv.pl',O_WRONLY|O_CREAT|O_EXCL);
  14. print FH $code;
  15. close(FH);
  16. chmod(0444,'.fishsrv.pl');
  17. } elsif ($ARGV[0] ne "abcc4c517321c96aa2184bf291d3dcb7") {
  18. $|=1;
  19. print "### 100 transfer fish servern";
  20. while(<STDIN>) {
  21. last">last if /^__END__/;
  22. $code.=$_;
  23. }
  24. exit(eval($code));
  25. }
  26. print "### 200n";
  27. use strict;
  28. use POSIX qw(getcwd dup2 strftime);
  29. $SIG{'CHLD'} = 'IGNORE';
  30. $| = 1;
  31. MAIN: while (<STDIN>) {
  32. next">next if !length($_) || substr($_,0,1) ne '#';
  33. #print DEBUG "$_n";
  34. s/^#//;
  35. /^VER / && do {
  36. print "VER 0.0.3 copy lscount lslinks lsmime exec statn### 200n";
  37. next">next;
  38. };
  39. /^PWD$/ && do {
  40. print getcwd(),"n### 200n";
  41. next">next;
  42. };
  43. /^SYMLINKs+((?:\.|[^\])*?)s+((?:\.|[^\])*?)s*$/ && do {
  44. my">my $ofn = unquote($1);
  45. my">my $fn = unquote($2);
  46. print (symlink($ofn,$fn)?"### 200n":"### 500 $!n");
  47. next">next;
  48. };
  49. /^COPYs+((?:\.|[^\])*?)s+((?:\.|[^\])*?)s*$/ && do {
  50. my">my $ofn = unquote($1);
  51. my">my $fn = unquote($2);
  52. my">my ($size) = (stat($ofn))[7];
  53. my">my $read = 1;
  54. if (-l $ofn) {
  55. my">my $dest = readlink($ofn);
  56. unlink($fn);
  57. symlink($dest,$fn) || ($read = 0);
  58. } else {
  59. sysopen(FH,$ofn,O_RDONLY) || do { print "### 500 $!n"; next">next; };
  60. sysopen(OFH,$fn,O_WRONLY|O_CREAT|O_TRUNC) || do { close(FH); print "### 500 $!n"; next">next; };
  61. local $/ = undef;
  62. my">my $buffer = '';
  63. while ($size > 32768 && ($read = sysread(FH,$buffer,32768)) > 0) {
  64. $size -= $read;
  65. if (syswrite(OFH,$buffer,$read) != $read) {
  66. close(FH); close(OFH);
  67. print "### 500 $!n";
  68. next MAIN;
  69. }
  70. }
  71. while ($size > 0 && ($read = sysread(FH,$buffer,$size)) > 0) {
  72. $size -= $read;
  73. if (syswrite(OFH,$buffer,$read) != $read) {
  74. close(FH); close(OFH);
  75. print "### 500 $!n";
  76. next MAIN;
  77. }
  78. }
  79. close(FH);
  80. close(OFH);
  81. }
  82. if ($read > 0) {
  83. print "### 200n";
  84. } else {
  85. print "### 500 $!n";
  86. }
  87. next;
  88. };
  89. /^LINKs+((?:\.|[^\])*?)s+((?:\.|[^\])*?)s*$/ && do {
  90. my $ofn = unquote($1);
  91. my $fn = unquote($2);
  92. print (link($ofn,$fn)?"### 200n":"### 500 $!n");
  93. next;
  94. };
  95. /^RENAMEs+((?:\.|[^\])*?)s+((?:\.|[^\])*?)s*$/ && do {
  96. my $ofn = unquote($1);
  97. my $fn = unquote($2);
  98. print (rename($ofn,$fn)?"### 200n":"### 500 $!n");
  99. next;
  100. };
  101. /^CHGRPs+(d+)s+((?:\.|[^\])*?)s*$/ && do {
  102. my $fn = unquote($2);
  103. print (chown(-1,int($1),$fn)?"### 200n":"### 500 $!n");
  104. next;
  105. };
  106. /^CHOWNs+(d+)s+((?:\.|[^\])*?)s*$/ && do {
  107. my $fn = unquote($2);
  108. print (chown(int($1),-1,$fn)?"### 200n":"### 500 $!n");
  109. next;
  110. };
  111. /^CHMODs+([0-7]+)s+((?:\.|[^\])*?)s*$/ && do {
  112. my $fn = unquote($2);
  113. print (chmod(oct($1),$fn)?"### 200n":"### 500 $!n");
  114. next;
  115. };
  116. /^DELEs+((?:\.|[^\])*?)s*$/ && do {
  117. my $fn = unquote($1);
  118. print (unlink($fn)?"### 200n":"### 500 $!n");
  119. next;
  120. };
  121. /^RMDs+((?:\.|[^\])*?)s*$/ && do {
  122. my $dn = unquote($1);
  123. print (rmdir($dn)?"### 200n":"### 500 $!n");
  124. next;
  125. };
  126. /^MKDs+((?:\.|[^\])*?)s*$/ && do {
  127. my $dn = unquote($1);
  128. if (mkdir($dn,0777)) {
  129. print "### 200n";
  130. } else {
  131. my $err = $!;
  132. print (chdir($dn)?"### 501 $errn":"### 500 $errn");
  133. }
  134. next;
  135. };
  136. /^CWDs+((?:\.|[^\])*?)s*$/ && do {
  137. my $dn = unquote($1);
  138. print (chdir($dn)?"### 200n":"### 500 $!n");
  139. next;
  140. };
  141. /^LISTs+((?:\.|[^\])*?)s*$/ && do {
  142. list($1, 1);
  143. next;
  144. };
  145. /^STATs+((?:\.|[^\])*?)s*$/ && do {
  146. list($1, 0);
  147. next;
  148. };
  149. /^WRITEs+(d+)s+(d+)s+((?:\.|[^\])*?)s*$/ && do {
  150. write_loop($2,$3,O_WRONLY|O_CREAT,$1);
  151. next;
  152. };
  153. /^APPENDs+(d+)s+((?:\.|[^\])*?)s*$/ && do {
  154. write_loop($1,$2,O_WRONLY|O_APPEND);
  155. next;
  156. };
  157. /^STORs+(d+)s+((?:\.|[^\])*?)s*$/ && do {
  158. write_loop($1,$2,O_WRONLY|O_CREAT|O_TRUNC);
  159. next;
  160. };
  161. /^RETRs+((?:\.|[^\])*?)s*$/ && do {
  162. read_loop($1);
  163. next;
  164. };
  165. /^READs+(d+)s+(d+)s+((?:\.|[^\])*?)s*$/ && do {
  166. read_loop($3,$2,$1);
  167. next;
  168. };
  169. /^EXECs+((?:\.|[^\])*?)s+((?:\.|[^\])*?)s*$/ && do {
  170. my $tempfile = unquote($2);
  171. my $command = unquote($1);
  172. $command = $command . ";echo "###RESULT: $?"";
  173. print("### 500 $!n"), next
  174. if (!sysopen(FH,$tempfile,O_CREAT|O_EXCL|O_WRONLY,0600));
  175. my $pid = fork();
  176. print("### 500 $!n"), next
  177. if (!defined $pid);
  178. if ($pid == 0) {
  179. open(STDOUT,'>>&FH');
  180. open(STDERR,'>>&FH');
  181. open(STDIN,'</dev/null'); # not sure here, ms windows anyone?
  182. exec('/bin/sh','-c',$command);
  183. print STDERR "Couldn't exec /bin/sh: $!n";
  184. exit(255);
  185. }
  186. waitpid($pid,0);
  187. close(FH);
  188. print "### 200n";
  189. next">next;
  190. };
  191. }
  192. exit(0);
  193. sub list {
  194. my">my $dn = unquote($_[0]);
  195. my">my @entries;
  196. if (!-e $dn) {
  197. print "### 404 File does not existn";
  198. } elsif ($_[1] && -d _) {
  199. opendir(DIR,$dn) || do { print "### 500 $!n"; return; };
  200. @entries = readdir(DIR);
  201. closedir(DIR);
  202. } else {
  203. ($dn, @entries) = $dn =~ m{(.*)/(.*)};
  204. $dn = '/' if (!length($dn));
  205. }
  206. print scalar(@entries),"n### 100n";
  207. my">my $cwd = getcwd();
  208. chdir($dn) || do { print "### 500 $!n"; return; };
  209. foreach (@entries) {
  210. my">my $link = readlink;
  211. my">my ($mode,$uid,$gid,$size,$mtime) = (lstat)[2,4,5,7,9];
  212. print filetype($mode,$link,$uid,$gid);
  213. print "S$sizen";
  214. print strftime("D%Y %m %d %H %M %Sn",localtime($mtime));
  215. print ":$_n";
  216. print "L$linkn" if defined $link;
  217. print mimetype($_);
  218. print "n";
  219. }
  220. chdir($cwd);
  221. print "### 200n";
  222. }
  223. sub read_loop {
  224. my">my $fn = unquote($_[0]);
  225. my">my ($size) = ($_[1]?int($_[1]):(stat($fn))[7]);
  226. my">my $error = '';
  227. print "### 501 Is directoryn" and return if -d $fn;
  228. sysopen(FH,$fn,O_RDONLY) || ($error = $!);
  229. if ($_[2]) {
  230. sysseek(FH,int($_[2]),0) || do { close(FH); $error ||= $!; };
  231. }
  232. print "### 500 $errorn" and return if $error;
  233. if (@_ < 2) {
  234. print "$sizen";
  235. }
  236. print "### 100n";
  237. my $buffer = '';
  238. my $read = 1;
  239. while ($size > 32768 && ($read = sysread(FH,$buffer,32768)) > 0) {
  240. #print DEBUG "$size left, $read readn";
  241. $size -= $read;
  242. print $buffer;
  243. }
  244. while ($size > 0 && ($read = sysread(FH,$buffer,$size)) > 0) {
  245. #print DEBUG "$size left, $read readn";
  246. $size -= $read;
  247. print $buffer;
  248. }
  249. while ($size > 0) {
  250. print ' ';
  251. $size--;
  252. }
  253. $error ||= $! if $read <= 0;
  254. close(FH);
  255. if (!$error) {
  256. print "### 200n";
  257. } else {
  258. print "### 500 $errorn";
  259. }
  260. }
  261. sub write_loop {
  262. my $size = int($_[0]);
  263. my $fn = unquote($_[1]);
  264. #print DEBUG "write_loop called $size size, $fn fn, $_[2]n";
  265. my $error = '';
  266. sysopen(FH,$fn,$_[2]) || do { print "### 400 $!n"; return; };
  267. eval { flock(FH,2); };
  268. if ($_[3]) {
  269. sysseek(FH,int($_[3]),0) || do { close(FH);print "### 400 $!n"; return; };
  270. }
  271. <STDIN>;
  272. print "### 100n";
  273. my $buffer = '';
  274. my $read = 1;
  275. while ($size > 32768 && ($read = read(STDIN,$buffer,32768)) > 0) {
  276. #print DEBUG "$size left, $read readn";
  277. $size -= $read;
  278. $error ||= $! if (syswrite(FH,$buffer,$read) != $read);
  279. }
  280. while ($size > 0 && ($read = read(STDIN,$buffer,$size)) > 0) {
  281. #print DEBUG "$size left, $read readn";
  282. $size -= $read;
  283. $error ||= $! if (syswrite(FH,$buffer,$read) != $read);
  284. }
  285. close(FH);
  286. if (!$error) {
  287. print "### 200n";
  288. } else {
  289. print "### 500 $errorn";
  290. }
  291. }
  292. sub unquote { $_ = shift; s/\(.)/$1/g; return $_; }
  293. sub filetype {
  294. my ($mode,$link,$uid,$gid) = @_;
  295. my $result = 'P';
  296. while (1) {
  297. -f _ && do { $result .= '-'; last; };
  298. -d _ && do { $result .= 'd'; last; };
  299. defined($link) && do { $result .= 'l'; last; };
  300. -c _ && do { $result .= 'c'; last; };
  301. -b _ && do { $result .= 'b'; last; };
  302. -S _ && do { $result .= 's'; last; };
  303. -p _ && do { $result .= 'p'; last; };
  304. $result .= '?'; last;
  305. }
  306. $result .= ($mode & 0400?'r':'-');
  307. $result .= ($mode & 0200?'w':'-');
  308. $result .= ($mode & 0100?($mode&04000?'s':'x'):($mode&04000?'S':'-'));
  309. $result .= ($mode & 0040?'r':'-');
  310. $result .= ($mode & 0020?'w':'-');
  311. $result .= ($mode & 0010?($mode&02000?'s':'x'):($mode&02000?'S':'-'));
  312. $result .= ($mode & 0004?'r':'-');
  313. $result .= ($mode & 0002?'w':'-');
  314. $result .= ($mode & 0001?($mode&01000?'t':'x'):($mode&01000?'T':'-'));
  315. $result .= ' ';
  316. $result .= (getpwuid($uid)||$uid);
  317. $result .= '.';
  318. $result .= (getgrgid($gid)||$gid);
  319. $result .= "n";
  320. return $result;
  321. }
  322. sub mimetype {
  323. my $fn = shift;
  324. return "Minode/directoryn" if -d $fn;
  325. pipe(IN,OUT);
  326. my $pid = fork();
  327. return '' if (!defined $pid);
  328. if ($pid) {
  329. close(OUT);
  330. my $type = <IN>;
  331. close(IN);
  332. chomp $type;
  333. chomp $type;
  334. $type =~ s/[,; ].*//;
  335. return '' if ($type !~ m///);
  336. return "M$typen"
  337. }
  338. close(IN);
  339. sysopen(NULL,'/dev/null',O_RDWR);
  340. dup2(fileno(NULL),fileno(STDIN));
  341. dup2(fileno(OUT),fileno(STDOUT));
  342. dup2(fileno(NULL),fileno(STDERR));
  343. exec('/usr/bin/file','-i','-b','-L',$fn);
  344. exit(0);
  345. }
  346.  

Download as Text : January 6th, 2009