serverhelp.pm 8.1 KB

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