serverhelp.pm 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. #
  10. # This software is licensed as described in the file COPYING, which
  11. # you should have received as part of this distribution. The terms
  12. # are also available at https://curl.se/docs/copyright.html.
  13. #
  14. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. # copies of the Software, and permit persons to whom the Software is
  16. # furnished to do so, under the terms of the COPYING file.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. #***************************************************************************
  22. package serverhelp;
  23. use strict;
  24. use warnings;
  25. use Exporter;
  26. #***************************************************************************
  27. # Global symbols allowed without explicit package name
  28. #
  29. use vars qw(
  30. @ISA
  31. @EXPORT_OK
  32. );
  33. #***************************************************************************
  34. # Inherit Exporter's capabilities
  35. #
  36. @ISA = qw(Exporter);
  37. #***************************************************************************
  38. # Global symbols this module will export upon request
  39. #
  40. @EXPORT_OK = qw(
  41. serverfactors
  42. servername_id
  43. servername_str
  44. servername_canon
  45. server_pidfilename
  46. server_portfilename
  47. server_logfilename
  48. server_cmdfilename
  49. server_inputfilename
  50. server_outputfilename
  51. mainsockf_pidfilename
  52. mainsockf_logfilename
  53. datasockf_pidfilename
  54. datasockf_logfilename
  55. );
  56. #***************************************************************************
  57. # Just for convenience, test harness uses 'https' and 'httptls' literals as
  58. # values for 'proto' variable in order to differentiate different servers.
  59. # 'https' literal is used for stunnel based https test servers, and 'httptls'
  60. # is used for non-stunnel https test servers.
  61. #***************************************************************************
  62. # Return server characterization factors given a server id string.
  63. #
  64. sub serverfactors {
  65. my $server = $_[0];
  66. my $proto;
  67. my $ipvnum;
  68. my $idnum;
  69. if($server =~
  70. /^((ftp|http|imap|pop3|smtp|http-pipe)s?)(\d*)(-ipv6|)$/) {
  71. $proto = $1;
  72. $idnum = ($3 && ($3 > 1)) ? $3 : 1;
  73. $ipvnum = ($4 && ($4 =~ /6$/)) ? 6 : 4;
  74. }
  75. elsif($server =~
  76. /^(tftp|sftp|socks|ssh|rtsp|gopher|httptls)(\d*)(-ipv6|)$/) {
  77. $proto = $1;
  78. $idnum = ($2 && ($2 > 1)) ? $2 : 1;
  79. $ipvnum = ($3 && ($3 =~ /6$/)) ? 6 : 4;
  80. }
  81. else {
  82. die "invalid server id: '$server'"
  83. }
  84. return($proto, $ipvnum, $idnum);
  85. }
  86. #***************************************************************************
  87. # Return server name string formatted for presentation purposes
  88. #
  89. sub servername_str {
  90. my ($proto, $ipver, $idnum) = @_;
  91. $proto = uc($proto) if($proto);
  92. die "unsupported protocol: '$proto'" unless($proto &&
  93. ($proto =~ /^(((FTP|HTTP|HTTP\/2|IMAP|POP3|GOPHER|SMTP|HTTP-PIPE)S?)|(TFTP|SFTP|SOCKS|SSH|RTSP|HTTPTLS|DICT|SMB|SMBS|TELNET|MQTT))$/));
  94. $ipver = (not $ipver) ? 'ipv4' : lc($ipver);
  95. die "unsupported IP version: '$ipver'" unless($ipver &&
  96. ($ipver =~ /^(4|6|ipv4|ipv6|-ipv4|-ipv6|unix)$/));
  97. $ipver = ($ipver =~ /6$/) ? '-IPv6' : (($ipver =~ /unix$/) ? '-unix' : '');
  98. $idnum = 1 if(not $idnum);
  99. die "unsupported ID number: '$idnum'" unless($idnum &&
  100. ($idnum =~ /^(\d+)$/));
  101. $idnum = '' unless($idnum > 1);
  102. return "${proto}${idnum}${ipver}";
  103. }
  104. #***************************************************************************
  105. # Return server name string formatted for identification purposes
  106. #
  107. sub servername_id {
  108. my ($proto, $ipver, $idnum) = @_;
  109. return lc(servername_str($proto, $ipver, $idnum));
  110. }
  111. #***************************************************************************
  112. # Return server name string formatted for file name purposes
  113. #
  114. sub servername_canon {
  115. my ($proto, $ipver, $idnum) = @_;
  116. my $string = lc(servername_str($proto, $ipver, $idnum));
  117. $string =~ tr/-/_/;
  118. $string =~ s/\//_v/;
  119. return $string;
  120. }
  121. #***************************************************************************
  122. # Return file name for server pid file.
  123. #
  124. sub server_pidfilename {
  125. my ($proto, $ipver, $idnum) = @_;
  126. my $trailer = '_server.pid';
  127. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  128. }
  129. #***************************************************************************
  130. # Return file name for server port file.
  131. #
  132. sub server_portfilename {
  133. my ($proto, $ipver, $idnum) = @_;
  134. my $trailer = '_server.port';
  135. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  136. }
  137. #***************************************************************************
  138. # Return file name for server log file.
  139. #
  140. sub server_logfilename {
  141. my ($logdir, $proto, $ipver, $idnum) = @_;
  142. my $trailer = '_server.log';
  143. $trailer = '_stunnel.log' if(lc($proto) =~ /^(ftp|http|imap|pop3|smtp)s$/);
  144. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  145. }
  146. #***************************************************************************
  147. # Return file name for server commands file.
  148. #
  149. sub server_cmdfilename {
  150. my ($logdir, $proto, $ipver, $idnum) = @_;
  151. my $trailer = '_server.cmd';
  152. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  153. }
  154. #***************************************************************************
  155. # Return file name for server input file.
  156. #
  157. sub server_inputfilename {
  158. my ($logdir, $proto, $ipver, $idnum) = @_;
  159. my $trailer = '_server.input';
  160. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  161. }
  162. #***************************************************************************
  163. # Return file name for server output file.
  164. #
  165. sub server_outputfilename {
  166. my ($logdir, $proto, $ipver, $idnum) = @_;
  167. my $trailer = '_server.output';
  168. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  169. }
  170. #***************************************************************************
  171. # Return file name for main or primary sockfilter pid file.
  172. #
  173. sub mainsockf_pidfilename {
  174. my ($proto, $ipver, $idnum) = @_;
  175. die "unsupported protocol: '$proto'" unless($proto &&
  176. (lc($proto) =~ /^(ftp|imap|pop3|smtp)s?$/));
  177. my $trailer = (lc($proto) =~ /^ftps?$/) ? '_sockctrl.pid':'_sockfilt.pid';
  178. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  179. }
  180. #***************************************************************************
  181. # Return file name for main or primary sockfilter log file.
  182. #
  183. sub mainsockf_logfilename {
  184. my ($logdir, $proto, $ipver, $idnum) = @_;
  185. die "unsupported protocol: '$proto'" unless($proto &&
  186. (lc($proto) =~ /^(ftp|imap|pop3|smtp)s?$/));
  187. my $trailer = (lc($proto) =~ /^ftps?$/) ? '_sockctrl.log':'_sockfilt.log';
  188. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  189. }
  190. #***************************************************************************
  191. # Return file name for data or secondary sockfilter pid file.
  192. #
  193. sub datasockf_pidfilename {
  194. my ($proto, $ipver, $idnum) = @_;
  195. die "unsupported protocol: '$proto'" unless($proto &&
  196. (lc($proto) =~ /^ftps?$/));
  197. my $trailer = '_sockdata.pid';
  198. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  199. }
  200. #***************************************************************************
  201. # Return file name for data or secondary sockfilter log file.
  202. #
  203. sub datasockf_logfilename {
  204. my ($logdir, $proto, $ipver, $idnum) = @_;
  205. die "unsupported protocol: '$proto'" unless($proto &&
  206. (lc($proto) =~ /^ftps?$/));
  207. my $trailer = '_sockdata.log';
  208. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  209. }
  210. #***************************************************************************
  211. # End of library
  212. 1;