tsget 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.1 2006/02/12 23:11:21 ulf 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"]);
  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. $error_string = "unexpected content type returned: $ct";
  90. }
  91. }
  92. return ($ts_body, $error_string);
  93. }
  94. # Print usage information and exists.
  95. sub usage {
  96. print STDERR "usage: $0 -h <server_url> [-e <extension>] [-o <output>] ";
  97. print STDERR "[-v] [-d] [-k <private_key.pem>] [-p <key_password>] ";
  98. print STDERR "[-c <client_cert.pem>] [-C <CA_certs.pem>] [-P <CA_path>] ";
  99. print STDERR "[-r <file:file...>] [-g <EGD_socket>] [<request>]...\n";
  100. exit 1;
  101. }
  102. # ----------------------------------------------------------------------
  103. # Main program
  104. # ----------------------------------------------------------------------
  105. # Getting command-line options (default comes from TSGET environment variable).
  106. my $getopt_arg = "h:e:o:vdk:p:c:C:P:r:g:";
  107. if (exists $ENV{TSGET}) {
  108. my @old_argv = @ARGV;
  109. @ARGV = split /\s+/, $ENV{TSGET};
  110. getopts($getopt_arg, \%options) or usage;
  111. @ARGV = @old_argv;
  112. }
  113. getopts($getopt_arg, \%options) or usage;
  114. # Checking argument consistency.
  115. if (!exists($options{h}) || (@ARGV == 0 && !exists($options{o}))
  116. || (@ARGV > 1 && exists($options{o}))) {
  117. print STDERR "Inconsistent command line options.\n";
  118. usage;
  119. }
  120. # Setting defaults.
  121. @ARGV = ("-") unless @ARGV != 0;
  122. $options{e} = ".tsr" unless defined($options{e});
  123. # Processing requests.
  124. my $curl = create_curl $options{h};
  125. undef $/; # For reading whole files.
  126. REQUEST: foreach (@ARGV) {
  127. my $input = $_;
  128. my ($base, $path) = fileparse($input, '\.[^.]*');
  129. my $output_base = $base . $options{e};
  130. my $output = defined($options{o}) ? $options{o} : $path . $output_base;
  131. STDERR->printflush("$input: ") if $options{v};
  132. # Read request.
  133. my $body;
  134. if ($input eq "-") {
  135. # Read the request from STDIN;
  136. $body = <STDIN>;
  137. } else {
  138. # Read the request from file.
  139. open INPUT, "<" . $input
  140. or warn("$input: could not open input file: $!\n"), next REQUEST;
  141. $body = <INPUT>;
  142. close INPUT
  143. or warn("$input: could not close input file: $!\n"), next REQUEST;
  144. }
  145. # Send request.
  146. STDERR->printflush("sending request") if $options{v};
  147. my ($ts_body, $error) = get_timestamp $curl, \$body;
  148. if (defined($error)) {
  149. die "$input: fatal error: $error\n";
  150. }
  151. STDERR->printflush(", reply received") if $options{v};
  152. # Write response.
  153. if ($output eq "-") {
  154. # Write to STDOUT.
  155. print $ts_body;
  156. } else {
  157. # Write to file.
  158. open OUTPUT, ">", $output
  159. or warn("$output: could not open output file: $!\n"), next REQUEST;
  160. print OUTPUT $ts_body;
  161. close OUTPUT
  162. or warn("$output: could not close output file: $!\n"), next REQUEST;
  163. }
  164. STDERR->printflush(", $output written.\n") if $options{v};
  165. }
  166. $curl->cleanup();
  167. WWW::Curl::easy::global_cleanup();