Advertisement
Guest User

Untitled

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