gdiprmclt.pl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/perl
  2. #####################################################
  3. # gdiprmclt.pl
  4. #
  5. # This script is used to test the remote maintenance
  6. # daemon (gdiprmaint.pl).
  7. #
  8. # See COPYING for licensing information.
  9. #
  10. #####################################################
  11. # PERL modules
  12. use strict;
  13. use Getopt::Std;
  14. # global variables
  15. use vars qw($conf $gnudipdir);
  16. # locate ourselves
  17. use FindBin;
  18. BEGIN {
  19. $gnudipdir = '';
  20. if ($FindBin::Bin =~ /(.*)\/.+?/) {
  21. $gnudipdir = $1;
  22. }
  23. }
  24. use lib "$gnudipdir/lib";
  25. # GnuDIP common subroutines
  26. use gdiplib;
  27. # process command line
  28. sub usage {
  29. print STDERR <<"EOQ";
  30. usage: gdiprmclt.pl [ -h ] host port
  31. usage: Test remote maintenance daemon.
  32. usage: Request is read from standard input with final line containing
  33. usage: just "end".
  34. usage: -h: Print this usage message.
  35. EOQ
  36. }
  37. use vars qw/ $opt_h /;
  38. if (!getopts('h')) {
  39. usage();
  40. exit 2;
  41. }
  42. if ($opt_h) {
  43. usage();
  44. exit;
  45. }
  46. if (@ARGV ne 2) {
  47. usage();
  48. exit 2;
  49. }
  50. my $serverhost = $ARGV[0];
  51. my $serverport = $ARGV[1];
  52. # get preferences from config file
  53. $conf = getconf("$gnudipdir/etc/gdiprmclt.conf");
  54. if (!$conf) {
  55. print STDERR "Exiting - getconf returned nothing\n";
  56. exit 2;
  57. }
  58. # rmaint password defined?
  59. my $password = $$conf{"rmaint_password.$serverhost"};
  60. if (!$password) {
  61. print STDERR
  62. "Configuration parameter \"rmaint_password.$serverhost\" not defined\n";
  63. exit 2;
  64. }
  65. # look up host
  66. my $inet_addr = gethostbyname($serverhost);
  67. if (!$inet_addr) {
  68. print STDERR "Could not do DNS lookup for $serverhost\n";
  69. exit 2;
  70. }
  71. my $paddr = sockaddr_in($serverport, $inet_addr);
  72. # connect to server
  73. socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
  74. if (!connect(SERVER, $paddr)) {
  75. print STDERR "Could not connect to $serverhost:$serverport\n";
  76. exit 2;
  77. }
  78. # set autoflush on
  79. select(SERVER);
  80. $| = 1;
  81. select(STDOUT);
  82. # retrieve the salt
  83. my $salt = <SERVER>;
  84. $salt = '' if ! defined $salt;
  85. chomp($salt);
  86. # got a salt?
  87. if (!$salt) {
  88. print STDERR "Server did not send salt\n";
  89. close SERVER;
  90. exit 2;
  91. }
  92. # salt and digest password
  93. $password = md5_hex("$password.$salt");
  94. # test clear text messages
  95. print SERVER "0\n";
  96. # send hashed password
  97. print SERVER "$password\n";
  98. # read and send lines of request
  99. print STDERR "Enter request - terminate with \"end\"\n";
  100. while (my $line = <STDIN>) {
  101. chomp($line);
  102. print SERVER "$line\n";
  103. last if ($line eq 'end');
  104. }
  105. # get response
  106. print STDERR "\nResponse ...\n";
  107. while (my $line = <SERVER>) {
  108. chomp($line);
  109. print STDOUT "$line\n";
  110. }
  111. close SERVER;