tsget 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/usr/bin/perl -w
  2. # Written by Zoltan Glozik <zglozik@stones.com>.
  3. # Copyright (c) 2002 The OpenTSA Project. All rights reserved.
  4. $::version = '$Id: tsget,v 1.3 2009/09/07 17:57:18 steve Exp $';
  5. use strict;
  6. use IO::Handle;
  7. use Getopt::Std;
  8. use File::Basename;
  9. use WWW::Curl::Easy;
  10. use vars qw(%options);
  11. # Callback for reading the body.
  12. sub read_body {
  13. my ($maxlength, $state) = @_;
  14. my $return_data = "";
  15. my $data_len = length ${$state->{data}};
  16. if ($state->{bytes} < $data_len) {
  17. $data_len = $data_len - $state->{bytes};
  18. $data_len = $maxlength if $data_len > $maxlength;
  19. $return_data = substr ${$state->{data}}, $state->{bytes}, $data_len;
  20. $state->{bytes} += $data_len;
  21. }
  22. return $return_data;
  23. }
  24. # Callback for writing the body into a variable.
  25. sub write_body {
  26. my ($data, $pointer) = @_;
  27. ${$pointer} .= $data;
  28. return length($data);
  29. }
  30. # Initialise a new Curl object.
  31. sub create_curl {
  32. my $url = shift;
  33. # Create Curl object.
  34. my $curl = WWW::Curl::Easy::new();
  35. # Error-handling related options.
  36. $curl->setopt(CURLOPT_VERBOSE, 1) if $options{d};
  37. $curl->setopt(CURLOPT_FAILONERROR, 1);
  38. $curl->setopt(CURLOPT_USERAGENT, "OpenTSA tsget.pl/" . (split / /, $::version)[2]);
  39. # Options for POST method.
  40. $curl->setopt(CURLOPT_UPLOAD, 1);
  41. $curl->setopt(CURLOPT_CUSTOMREQUEST, "POST");
  42. $curl->setopt(CURLOPT_HTTPHEADER,
  43. ["Content-Type: application/timestamp-query",
  44. "Accept: application/timestamp-reply,application/timestamp-response"]);
  45. $curl->setopt(CURLOPT_READFUNCTION, \&read_body);
  46. $curl->setopt(CURLOPT_HEADERFUNCTION, sub { return length($_[0]); });
  47. # Options for getting the result.
  48. $curl->setopt(CURLOPT_WRITEFUNCTION, \&write_body);
  49. # SSL related options.
  50. $curl->setopt(CURLOPT_SSLKEYTYPE, "PEM");
  51. $curl->setopt(CURLOPT_SSL_VERIFYPEER, 1); # Verify server's certificate.
  52. $curl->setopt(CURLOPT_SSL_VERIFYHOST, 2); # Check server's CN.
  53. $curl->setopt(CURLOPT_SSLKEY, $options{k}) if defined($options{k});
  54. $curl->setopt(CURLOPT_SSLKEYPASSWD, $options{p}) if defined($options{p});
  55. $curl->setopt(CURLOPT_SSLCERT, $options{c}) if defined($options{c});
  56. $curl->setopt(CURLOPT_CAINFO, $options{C}) if defined($options{C});
  57. $curl->setopt(CURLOPT_CAPATH, $options{P}) if defined($options{P});
  58. $curl->setopt(CURLOPT_RANDOM_FILE, $options{r}) if defined($options{r});
  59. $curl->setopt(CURLOPT_EGDSOCKET, $options{g}) if defined($options{g});
  60. # Setting destination.
  61. $curl->setopt(CURLOPT_URL, $url);
  62. return $curl;
  63. }
  64. # Send a request and returns the body back.
  65. sub get_timestamp {
  66. my $curl = shift;
  67. my $body = shift;
  68. my $ts_body;
  69. local $::error_buf;
  70. # Error-handling related options.
  71. $curl->setopt(CURLOPT_ERRORBUFFER, "::error_buf");
  72. # Options for POST method.
  73. $curl->setopt(CURLOPT_INFILE, {data => $body, bytes => 0});
  74. $curl->setopt(CURLOPT_INFILESIZE, length(${$body}));
  75. # Options for getting the result.
  76. $curl->setopt(CURLOPT_FILE, \$ts_body);
  77. # Send the request...
  78. my $error_code = $curl->perform();
  79. my $error_string;
  80. if ($error_code != 0) {
  81. my $http_code = $curl->getinfo(CURLINFO_HTTP_CODE);
  82. $error_string = "could not get timestamp";
  83. $error_string .= ", http code: $http_code" unless $http_code == 0;
  84. $error_string .= ", curl code: $error_code";
  85. $error_string .= " ($::error_buf)" if defined($::error_buf);
  86. } else {
  87. my $ct = $curl->getinfo(CURLINFO_CONTENT_TYPE);
  88. if (lc($ct) ne "application/timestamp-reply"
  89. && lc($ct) ne "application/timestamp-response") {
  90. $error_string = "unexpected content type returned: $ct";
  91. }
  92. }
  93. return ($ts_body, $error_string);
  94. }
  95. # Print usage information and exists.
  96. sub usage {
  97. print STDERR "usage: $0 -h <server_url> [-e <extension>] [-o <output>] ";
  98. print STDERR "[-v] [-d] [-k <private_key.pem>] [-p <key_password>] ";
  99. print STDERR "[-c <client_cert.pem>] [-C <CA_certs.pem>] [-P <CA_path>] ";
  100. print STDERR "[-r <file:file...>] [-g <EGD_socket>] [<request>]...\n";
  101. exit 1;
  102. }
  103. # ----------------------------------------------------------------------
  104. # Main program
  105. # ----------------------------------------------------------------------
  106. # Getting command-line options (default comes from TSGET environment variable).
  107. my $getopt_arg = "h:e:o:vdk:p:c:C:P:r:g:";
  108. if (exists $ENV{TSGET}) {
  109. my @old_argv = @ARGV;
  110. @ARGV = split /\s+/, $ENV{TSGET};
  111. getopts($getopt_arg, \%options) or usage;
  112. @ARGV = @old_argv;
  113. }
  114. getopts($getopt_arg, \%options) or usage;
  115. # Checking argument consistency.
  116. if (!exists($options{h}) || (@ARGV == 0 && !exists($options{o}))
  117. || (@ARGV > 1 && exists($options{o}))) {
  118. print STDERR "Inconsistent command line options.\n";
  119. usage;
  120. }
  121. # Setting defaults.
  122. @ARGV = ("-") unless @ARGV != 0;
  123. $options{e} = ".tsr" unless defined($options{e});
  124. # Processing requests.
  125. my $curl = create_curl $options{h};
  126. undef $/; # For reading whole files.
  127. REQUEST: foreach (@ARGV) {
  128. my $input = $_;
  129. my ($base, $path) = fileparse($input, '\.[^.]*');
  130. my $output_base = $base . $options{e};
  131. my $output = defined($options{o}) ? $options{o} : $path . $output_base;
  132. STDERR->printflush("$input: ") if $options{v};
  133. # Read request.
  134. my $body;
  135. if ($input eq "-") {
  136. # Read the request from STDIN;
  137. $body = <STDIN>;
  138. } else {
  139. # Read the request from file.
  140. open INPUT, "<" . $input
  141. or warn("$input: could not open input file: $!\n"), next REQUEST;
  142. $body = <INPUT>;
  143. close INPUT
  144. or warn("$input: could not close input file: $!\n"), next REQUEST;
  145. }
  146. # Send request.
  147. STDERR->printflush("sending request") if $options{v};
  148. my ($ts_body, $error) = get_timestamp $curl, \$body;
  149. if (defined($error)) {
  150. die "$input: fatal error: $error\n";
  151. }
  152. STDERR->printflush(", reply received") if $options{v};
  153. # Write response.
  154. if ($output eq "-") {
  155. # Write to STDOUT.
  156. print $ts_body;
  157. } else {
  158. # Write to file.
  159. open OUTPUT, ">", $output
  160. or warn("$output: could not open output file: $!\n"), next REQUEST;
  161. print OUTPUT $ts_body;
  162. close OUTPUT
  163. or warn("$output: could not close output file: $!\n"), next REQUEST;
  164. }
  165. STDERR->printflush(", $output written.\n") if $options{v};
  166. }
  167. $curl->cleanup();
  168. WWW::Curl::Easy::global_cleanup();