Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.48 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. use IO::Socket;
  6. use Storable;
  7. my $serverdirectory = './server/';
  8. my $logsdirectory = './server/logs/';
  9. my $struct ;
  10. my $sendstruct;
  11. my $username;
  12. my $server = IO::Socket::INET->new
  13. (
  14. LocalAddr => '127.0.0.1',
  15. LocalPort => 2628,
  16. Type => SOCK_STREAM,
  17. ReuseAddr => 1,
  18. Listen => 5
  19. ); die "could not open port\n" unless $server;
  20.  
  21. warn "server ready waiting for connections..... \n";
  22.  
  23. my $client;
  24.  
  25. while ($client = $server->accept())
  26. {
  27. my $pid;
  28. while (not defined ($pid = fork()))
  29. {
  30. sleep 5;
  31. }
  32. if ($pid)
  33. {
  34.  
  35. close $client;
  36. }
  37. else
  38. {
  39. $client->autoflush(1);
  40.  
  41. close $server;
  42.  
  43. do_your_stuff();
  44. }
  45. }
  46. sub logs
  47. {
  48. my $messagetolog = shift;
  49. open(my $fh, '>>', $logsdirectory.$username.".txt") or die "Can't open file '$username' $!";
  50. print $fh $messagetolog;
  51. close $fh;
  52.  
  53. }
  54. sub do_your_stuff
  55. {
  56. warn "client connected to pid $$\n";
  57.  
  58. for(my $i=0; $i <= 2; $i++)
  59. # while(1)
  60. {
  61.  
  62. my $struct = Storable::fd_retrieve($client);
  63. my $command = $struct->{cmd};
  64.  
  65. if ($command eq "login")
  66. {
  67. if ($struct->{login} eq "admin" and $struct->{ps} eq "admin")
  68. {
  69. $username = "Admin";
  70. print $client "admin\n";
  71. print "pid $$ > Admin logged in"."\n";
  72. logs("pid $$ > Admin logged in"."\n");
  73. }
  74. elsif($struct->{login} eq "user" and $struct->{ps} eq "user")
  75. {
  76. $username = "User";
  77. print $client "user\n";
  78. print "pid $$ > User logged in"."\n";
  79. logs("pid $$ > User logged in"."\n");
  80. }
  81. elsif ($struct->{login} eq "guest" and $struct->{ps} eq "guest")
  82. {
  83. $username = "Guest";
  84. print $client "guest\n";
  85. print "pid $$ > Guest logged in"."\n";
  86. logs("pid $$ > Guest logged in"."\n");
  87. }
  88. else
  89. {
  90. print $client "error\n";
  91. print "pid $$ > error with login"."\n";
  92. logs("pid $$ > error with login"."\n");
  93. }
  94.  
  95.  
  96. }
  97. elsif ($command eq "getlist")
  98. {
  99. my $getdirectory = $struct->{directory};
  100. opendir my $dir, $getdirectory or die "Cannot open directory :$!";
  101. my @files;
  102. my $count = 0;
  103. while (my $file = readdir($dir))
  104. {
  105.  
  106. next if ($file =~ m/^\./ or (-d $getdirectory.$file));
  107.  
  108. ($count++);
  109. push @files, "$count. $file";
  110. }
  111. closedir $dir;
  112. $sendstruct= {
  113. cmd => "getlist",
  114. listoffiles => \@files,
  115. };
  116. Storable::nstore_fd($sendstruct, $client);
  117. logs("$username pid $$ > got the list of files"."\n");
  118. }
  119.  
  120. elsif ($command eq "download")
  121. {
  122.  
  123. my $filename = $struct->{filename};
  124.  
  125. my $file_size = -s $serverdirectory.$filename;
  126. print $client "$file_size\n";
  127. if ( -z $serverdirectory.$filename){
  128. $client->syswrite("\n");
  129. }
  130. else
  131. {
  132. open(my $fh, '<', $serverdirectory.$filename) or die "Could not open file '$filename' $!";
  133. print "$filename downloading...\n";
  134. binmode $fh;
  135. my $row;
  136. while ($row = <$fh>)
  137. {
  138.  
  139. $client->syswrite($row,1024);
  140.  
  141. }
  142.  
  143. close($fh);
  144.  
  145. }
  146.  
  147. print "$username [pid $$] downloaded"." $filename ...\n";
  148. logs("$username [pid $$] downloaded"." $filename ...\n");
  149.  
  150. }
  151. elsif ($command eq "delete")
  152. {
  153.  
  154. my $filename = $struct->{filename};
  155. unlink $serverdirectory.$filename;
  156. print "File $filename was deleted\n";
  157. logs("$username [pid $$] deleted"." $filename ...\n");
  158.  
  159. }
  160.  
  161. elsif ($command eq "showlog")
  162. {
  163.  
  164. my $filename = $struct->{filename};
  165. if ( -z $serverdirectory.$filename){
  166. $client->syswrite("\n");
  167. }
  168. else
  169. {
  170. open(my $fh, '<', $logsdirectory.$filename) or die "Could not open file '$filename' $!";
  171. print "$filename openning the log...\n";
  172.  
  173. while (my $row = <$fh>)
  174. {
  175. $client->syswrite($row);
  176.  
  177. }
  178. close($fh);
  179. }
  180. print "$username [pid $$] log"." $filename ...\n";
  181. logs("$username [pid $$] log"." $filename ...\n");
  182.  
  183. }
  184. elsif ($command eq "upload")
  185. {
  186.  
  187. my $filename = $struct->{filename};
  188. print "$filename\n";
  189. my $size = $struct->{size};
  190. if ( -e $serverdirectory.$filename){
  191. if ($username eq "User")
  192. {
  193. $client->send("0\n", 1024);
  194. }
  195. elsif ($username eq "Admin")
  196. {
  197. print "admin\n";
  198. $client->send("1\n",1024);
  199. $client->recv(my $answer,1024);
  200. if ($answer eq "y\n")
  201. {
  202. open(my $fh, '>', $serverdirectory.$filename) or die "Can't save file $!";
  203. print "$filename uploading...\n";
  204.  
  205. while (sysread($client, my $row , 1024))
  206. {
  207. print $fh $row;
  208. last;
  209. }
  210. close($fh);
  211. print "$filename has been uploaded.\n";
  212. logs("$username [pid $$] uploaded(overwrite)"." $filename ...\n");
  213. }
  214. elsif($answer eq "n\n")
  215. {
  216. print "Canceling the uploading\n";
  217. logs("$username [pid $$] doen't want to overwrite"." $filename ...\n");
  218. }
  219. }
  220.  
  221. }
  222. else
  223. {
  224.  
  225.  
  226. $client->send("2\n",1024);
  227. $client->recv(my $answer,1024);
  228. if ($answer eq "ok\n")
  229. {
  230. open(my $fh, '>', $serverdirectory.$filename) or die "Can't save file $!";
  231. print "$filename uploading...\n";
  232.  
  233. while (sysread($client, my $row , 1024))
  234. {
  235. print $fh $row;
  236. last;
  237. }
  238. close($fh);
  239. print "$filename has been uploaded.\n";
  240. logs("$username [pid $$] uploaded(overwrite)"." $filename ...\n");
  241. }
  242.  
  243. }
  244.  
  245.  
  246. }
  247.  
  248. }
  249. print "$username disconnected.\n";
  250. logs("$username [pid $$] disconnected"."\n");
  251. exit 0;
  252. }
  253. -=-=-=-=-=-=-=-
  254. #!/usr/bin/perl
  255. use strict;
  256. use IO::Socket;
  257. #use warnings;
  258. use Storable;
  259. use File::Basename;
  260. my ( $host, $port, $kidpid, $handle, $name, $pass, @listof_files, $response, $count);
  261.  
  262. ( $host, $port ) = ('127.0.0.1',8888);
  263. sub login()
  264. {
  265. die "Socket is dead $!" unless $handle->connected();
  266. my @array;
  267. push @array, "asd";
  268. push @array, 777;
  269. $name = shift || '';
  270. if($name eq ''){print "What's your name?\n"}
  271. chomp ($name = <>);
  272.  
  273. $pass = shift || '';
  274. if($pass eq ''){print "What's your password?\n"}
  275. chomp ($pass = <>);
  276. my $message= {
  277. cmd => "login",
  278. login => "$name",
  279. ps => "$pass",
  280.  
  281. };
  282. Storable::nstore_fd($message, $handle);
  283. $handle->recv($name, 1024);
  284. showactions();
  285. }
  286.  
  287. sub actions
  288. {
  289.  
  290. my $template = shift;
  291. my $action;
  292.  
  293. $action = <>;
  294. print "$action\n";
  295. if ($action !~/$template/ and $action ne "exit")
  296. {
  297. print "Enter the number from $template:\n";
  298. actions($template);
  299. }
  300. else {
  301. if ($action eq "1\n"){
  302.  
  303.  
  304. getlist("./server/");
  305. downloadfile();
  306. }
  307. elsif ($action eq "2\n"){
  308.  
  309. uploadfile();
  310.  
  311. }
  312. elsif ($action eq "3\n"){
  313. getlist("./server/logs/");
  314. showlogs();
  315. }
  316. elsif ($action eq "4\n")
  317. {
  318. getlist("./server/");
  319. deletefile();
  320. }
  321. elsif ($action eq "exit\n"){ shutdown($handle, 1); exit 0;
  322. }
  323. }
  324. }
  325.  
  326. sub getlist
  327. {
  328.  
  329. my $message= {
  330. cmd => "getlist",
  331. directory => shift,
  332. };
  333. Storable::nstore_fd($message, $handle);
  334. my $struct = Storable::fd_retrieve($handle);
  335. @listof_files = @{ $struct->{listoffiles} };
  336. $count = 0;
  337. foreach (@listof_files)
  338. {
  339. ($count++);
  340. print "$_\n";
  341. }
  342. }
  343.  
  344. sub showactions()
  345. {
  346.  
  347. die "Socket is dead $!" unless $handle->connected();
  348. if ($name eq "error\n")
  349. {
  350. #shutdown($handle, 1);
  351. login();
  352.  
  353. }
  354. elsif ($name eq "admin\n") {
  355. print "Enter the number 1,2,3,4,5:\n 1 - download file; \n 2 - upload file; \n 3 - show logs;\n 4 - remove file.\n";
  356. my $range = '^[1-4]$';
  357. actions($range);
  358. }
  359. elsif($name eq "user\n") {
  360. print "Enter the number 1,2,3:\n 1 - download file; \n 2 - upload file.\n";
  361. my $range = '^[1-3]$';
  362. actions($range);
  363. }
  364. elsif($name eq "guest\n"){
  365. print "Enter the number 1:\n 1 - download file.\n";
  366. my $range = '^[1]$';
  367. actions($range);
  368.  
  369. }
  370. else {print "error\n"; last; shutdown($handle,1); exit 0}
  371.  
  372. }
  373.  
  374.  
  375. sub downloadfile()
  376. {
  377.  
  378. print "Enter the number of filw which you want to download [1..$count]\n";
  379. my $filenumber;
  380. my $temp = "^[1-$count]\$";
  381. chomp ($filenumber = <>);
  382. if ($filenumber !~/$temp/)
  383. {
  384. downloadfile();
  385. }
  386. else {
  387. my $filename = substr(@listof_files[$filenumber-1],3);
  388. my $message= {
  389. cmd => "download",
  390. filename => $filename,
  391. };
  392. my $buff = "true";
  393. print $buff."\n";
  394. if ($buff eq "true"){
  395. Storable::nstore_fd($message, $handle);
  396. $buff = "false";}
  397. my $serversize;
  398. $handle->recv($serversize, 1024);
  399. print $buff."\n";
  400. #my $struct = Storable::fd_retrieve($handle);
  401. print "download\n";
  402.  
  403. open(my $fh, '>', "./Downloads/$filename") or die "Can't save file $!";
  404. binmode $fh;
  405. print "$filename downloading...\n";
  406. my $row;
  407. my $count = 0;
  408. chomp ($serversize);
  409. while (sysread($handle, $row , 1024))
  410. {
  411. print $fh $row;
  412. last
  413. }
  414.  
  415.  
  416. #
  417. close($fh);
  418.  
  419. print "$filename has been downloaded.\n"
  420. }
  421. }
  422.  
  423.  
  424. sub uploadfile()
  425. {
  426. print "Enter the full file name that you wan't to upload\n";
  427. chomp (my $filename = <>);
  428. if (-e $filename)
  429. {
  430. my $size = -s $filename;
  431. my $message= {
  432. cmd => "upload",
  433. filename => basename($filename),
  434. size => $size,
  435. };
  436. Storable::nstore_fd($message, $handle);
  437.  
  438. $handle->recv(my $resp, 1024);
  439.  
  440. print "$resp";
  441.  
  442. if ($resp eq "0\n")
  443. {
  444. print "User can't overwrite files\n";
  445.  
  446. }
  447. elsif ($resp eq "1\n")
  448. {
  449. print "File already exist, do you want to overwrite it y|n?\n";
  450. my $answer = <>;
  451. $handle->send($answer, 1024);
  452. if ($answer eq "y\n")
  453. {
  454. open(my $fh, '<', $filename) or die "Could not open file '$filename' $!";
  455. print "$filename uploading...\n";
  456.  
  457. while (my $row = <$fh>)
  458. {
  459. $handle->syswrite($row);
  460.  
  461. }
  462. close($fh);
  463. print "$filename uploaded...\n";
  464. }
  465. }
  466. elsif ($resp eq "2\n")
  467. {
  468. $handle->send("ok\n", 1024);
  469. open(my $fh, '<', $filename) or die "Could not open file '$filename' $!";
  470. print "$filename uploading...\n";
  471.  
  472. while (my $row = <$fh>)
  473. {
  474. $handle->syswrite($row);
  475.  
  476. }
  477. close($fh);
  478. print "$filename uploaded...\n";
  479. }
  480.  
  481. }
  482. else { print "File doesn't extis\n"; uploadfile();}
  483.  
  484. }
  485. sub deletefile()
  486. {
  487.  
  488. print "Enter the number of file which you want to delete [1..$count]\n";
  489. my $filenumber;
  490. my $temp = "^[1-$count]\$";
  491. chomp ($filenumber = <>);
  492. if ($filenumber !~/$temp/)
  493. {
  494. deletefile()
  495. }
  496. else {
  497. my $filename = substr(@listof_files[$filenumber-1],3);
  498. my $message= {
  499. cmd => "delete",
  500. filename => $filename,
  501. };
  502. Storable::nstore_fd($message, $handle);
  503. }
  504. print "file was deleted\n";
  505.  
  506. }
  507.  
  508. sub showlogs()
  509. {
  510.  
  511. print "Enter the number of file which you want to see [1..$count]\n";
  512. my $filenumber;
  513. my $temp = "^[1-$count]\$";
  514. chomp ($filenumber = <>);
  515. if ($filenumber !~/$temp/)
  516. {
  517. showlogs();
  518. }
  519. else {
  520. my $filename = substr(@listof_files[$filenumber-1],3);
  521. my $message= {
  522. cmd => "showlog",
  523. filename => $filename,
  524. };
  525. Storable::nstore_fd($message, $handle);
  526. #my $struct = Storable::fd_retrieve($handle);
  527. print "show log\n";
  528.  
  529. open(my $fh, '>', "./Downloads/$filename") or die "Can't save file $!";
  530. print "$filename downloading...\n";
  531.  
  532. while (sysread($handle, my $row , 1024))
  533. {
  534. print $row;
  535. #print "$row\n";
  536. last;
  537. }
  538. close($fh);
  539. print "$filename log.\n"
  540. }
  541. }
  542.  
  543.  
  544. $handle = IO::Socket::INET->new(
  545. Proto => "tcp",
  546. PeerAddr => $host,
  547. PeerPort => 2628
  548. )
  549. or die "can't connect to port $port on $host: $!";
  550. $handle->autoflush(1);
  551. print "[Connected to $host:$port]\n";
  552.  
  553. login();
  554. shutdown($handle, 1);
  555. #close($handle);
  556.  
  557.  
  558.  
  559. #my $action;
  560.  
  561. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement