You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.6 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. #!/usr/bin/env sh
  2. #Here is the script to deploy the cert to your cpanel account by the cpanel APIs.
  3. #returns 0 means success, otherwise error.
  4. #export DEPLOY_CPANEL_USER=myusername
  5. #export DEPLOY_CPANEL_PASSWORD=PASSWORD
  6. #export DEPLOY_CPANEL_HOSTNAME=localhost:2083
  7. ######## Public functions #####################
  8. #domain keyfile certfile cafile fullchain
  9. cpanel_deploy() {
  10. _cdomain="$1"
  11. _ckey="$2"
  12. _ccert="$3"
  13. _cca="$4"
  14. _cfullchain="$5"
  15. _debug _cdomain "$_cdomain"
  16. _debug _ckey "$_ckey"
  17. _debug _ccert "$_ccert"
  18. _debug _cca "$_cca"
  19. _debug _cfullchain "$_cfullchain"
  20. export _ckey _ccert _cdomain
  21. # Perl code taken from https://documentation.cpanel.net/display/SDK/Tutorial+-+Call+UAPI%27s+SSL%3A%3Ainstall_ssl+Function+in+Custom+Code
  22. perl -f <<'END'
  23. # Return errors if Perl experiences problems.
  24. use strict;
  25. use warnings;
  26. # Allow my code to perform web requests.
  27. use LWP::UserAgent;
  28. use LWP::Protocol::https;
  29. # Use the correct encoding to prevent wide character warnings.
  30. use Encode;
  31. use utf8;
  32. # Properly decode JSON.
  33. use JSON;
  34. # Function properly with Base64 authentication headers.
  35. use MIME::Base64;
  36. # Authentication information.
  37. my $username = $ENV{'DEPLOY_CPANEL_USER'};
  38. my $password = $ENV{'DEPLOY_CPANEL_PASSWORD'};
  39. my $hostname = $ENV{'DEPLOY_CPANEL_HOSTNAME'};
  40. # The URL for the SSL::install_ssl UAPI function.
  41. my $request = "https://".$hostname."/execute/SSL/install_ssl";
  42. # Required to allow HTTPS connections to unsigned services.
  43. # Services on localhost are always unsigned.
  44. $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
  45. # Create a useragent object.
  46. my $ua = LWP::UserAgent->new();
  47. # Add authentication headers.
  48. $ua->default_header(
  49. 'Authorization' => 'Basic ' . MIME::Base64::encode("$username:$password"),
  50. );
  51. # Read in the SSL certificate and key file.
  52. my $cert = $ENV{'_ccert'};
  53. my $key = $ENV{'_ckey'};
  54. {
  55. local $/;
  56. open ( my $fh, '<', $cert );
  57. $cert = <$fh>;
  58. close $fh;
  59. open ( $fh, '<', $key );
  60. $key = <$fh>;
  61. close $fh;
  62. }
  63. my $domain = $ENV{'_cdomain'};
  64. # Make the call.
  65. my $response = $ua->post($request,
  66. Content_Type => 'form-data',
  67. Content => [
  68. domain => $domain,
  69. cert => $cert,
  70. key => $key,
  71. ],
  72. );
  73. # Create an object to decode the JSON.
  74. # Sorted by keys and pretty-printed.
  75. my $json_printer = JSON->new->pretty->canonical(1);
  76. # UTF-8 encode before decoding to avoid wide character warnings.
  77. my $content = JSON::decode_json(Encode::encode_utf8($response->decoded_content));
  78. # Print output, UTF-8 encoded to avoid wide character warnings.
  79. print Encode::encode_utf8($json_printer->encode($content));
  80. =pod
  81. {
  82. "data" : {
  83. "action" : "none",
  84. "aliases" : [
  85. "mail.example.com"
  86. ],
  87. "cert_id" : "example_com_xxx_yyy_zzzzzzzzzzzzzzzzzz",
  88. "domain" : "example.com",
  89. "extra_certificate_domains" : [],
  90. "html" : "<br /><b>This certificate was already installed on this host. The system made no changes.</b><br />\n",
  91. "ip" : "127.0.0.1",
  92. "key_id" : "xxx_yyy_zzzzzzzzzzzzzzzz",
  93. "message" : "This certificate was already installed on this host. The system made no changes.",
  94. "servername" : "example.com",
  95. "status" : 1,
  96. "statusmsg" : "This certificate was already installed on this host. The system made no changes.",
  97. "user" : "username",
  98. "warning_domains" : [
  99. "mail.example.com"
  100. ],
  101. "working_domains" : [
  102. "example.com"
  103. ]
  104. },
  105. "errors" : null,
  106. "messages" : [
  107. "The certificate was successfully installed on the domain “example.com”."
  108. ],
  109. "metadata" : {},
  110. "status" : 1
  111. }
  112. =cut
  113. END
  114. }