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.

130 lines
4.3 KiB

6 years ago
  1. #!/usr/bin/env sh
  2. # Here is a script to deploy cert to routeros router.
  3. # Deploy the cert to remote routeros
  4. #
  5. # ```sh
  6. # acme.sh --deploy -d ftp.example.com --deploy-hook routeros
  7. # ```
  8. #
  9. # Before you can deploy the certificate to router os, you need
  10. # to add the id_rsa.pub key to the routeros and assign a user
  11. # to that key.
  12. #
  13. # The user need to have access to ssh, ftp, read and write.
  14. #
  15. # There are no need to enable ftp service for the script to work,
  16. # as they are transmitted over SCP, however ftp is needed to store
  17. # the files on the router.
  18. #
  19. # Then you need to set the environment variables for the
  20. # deploy script to work.
  21. #
  22. # ```sh
  23. # export ROUTER_OS_USERNAME=certuser
  24. # export ROUTER_OS_HOST=router.example.com
  25. # export ROUTER_OS_PORT=22
  26. #
  27. # acme.sh --deploy -d ftp.example.com --deploy-hook routeros
  28. # ```
  29. #
  30. # The deploy script will remove previously deployed certificates,
  31. # and it does this with an assumption on how RouterOS names imported
  32. # certificates, adding a "cer_0" suffix at the end. This is true for
  33. # versions 6.32 -> 6.41.3, but it is not guaranteed that it will be
  34. # true for future versions when upgrading.
  35. #
  36. # If the router have other certificates with the same name as the one
  37. # beeing deployed, then this script will remove those certificates.
  38. #
  39. # At the end of the script, the services that use those certificates
  40. # could be updated. Currently only the www-ssl service is beeing
  41. # updated, but more services could be added.
  42. #
  43. # For instance:
  44. # ```sh
  45. # export ROUTER_OS_ADDITIONAL_SERVICES="/ip service set api-ssl certificate=$_cdomain.cer_0"
  46. # ```
  47. #
  48. # One optional thing to do as well is to create a script that updates
  49. # all the required services and run that script in a single command.
  50. #
  51. # returns 0 means success, otherwise error.
  52. ######## Public functions #####################
  53. #domain keyfile certfile cafile fullchain
  54. routeros_deploy() {
  55. _cdomain="$1"
  56. _ckey="$2"
  57. _ccert="$3"
  58. _cca="$4"
  59. _cfullchain="$5"
  60. _debug _cdomain "$_cdomain"
  61. _debug _ckey "$_ckey"
  62. _debug _ccert "$_ccert"
  63. _debug _cca "$_cca"
  64. _debug _cfullchain "$_cfullchain"
  65. _getdeployconf ROUTER_OS_HOST
  66. if [ -z "$ROUTER_OS_HOST" ]; then
  67. _debug "Using _cdomain as ROUTER_OS_HOST, please set if not correct."
  68. ROUTER_OS_HOST="$_cdomain"
  69. fi
  70. _getdeployconf ROUTER_OS_USERNAME
  71. if [ -z "$ROUTER_OS_USERNAME" ]; then
  72. _err "Need to set the env variable ROUTER_OS_USERNAME"
  73. return 1
  74. fi
  75. _getdeployconf ROUTER_OS_PORT
  76. if [ -z "$ROUTER_OS_PORT" ]; then
  77. _debug "Using default port 22 as ROUTER_OS_PORT, please set if not correct."
  78. ROUTER_OS_PORT=22
  79. fi
  80. _getdeployconf ROUTER_OS_ADDITIONAL_SERVICES
  81. if [ -z "$ROUTER_OS_ADDITIONAL_SERVICES" ]; then
  82. _debug "Not enabling additional services"
  83. ROUTER_OS_ADDITIONAL_SERVICES=""
  84. fi
  85. _savedeployconf ROUTER_OS_HOST "$ROUTER_OS_HOST"
  86. _savedeployconf ROUTER_OS_USERNAME "$ROUTER_OS_USERNAME"
  87. _savedeployconf ROUTER_OS_PORT "$ROUTER_OS_PORT"
  88. _savedeployconf ROUTER_OS_ADDITIONAL_SERVICES "$ROUTER_OS_ADDITIONAL_SERVICES"
  89. _info "Trying to push key '$_ckey' to router"
  90. scp -P "$ROUTER_OS_PORT" "$_ckey" "$ROUTER_OS_USERNAME@$ROUTER_OS_HOST:$_cdomain.key"
  91. _info "Trying to push cert '$_cfullchain' to router"
  92. scp -P "$ROUTER_OS_PORT" "$_cfullchain" "$ROUTER_OS_USERNAME@$ROUTER_OS_HOST:$_cdomain.cer"
  93. DEPLOY_SCRIPT_CMD="/system script add name=\"LE Cert Deploy - $_cdomain\" owner=admin policy=ftp,read,write,password,sensitive \
  94. source=\"## generated by routeros deploy script in acme.sh;\
  95. \n/certificate remove [ find name=$_cdomain.cer_0 ];\
  96. \n/certificate remove [ find name=$_cdomain.cer_1 ];\
  97. \ndelay 1;\
  98. \n/certificate import file-name=$_cdomain.cer passphrase=\\\"\\\";\
  99. \n/certificate import file-name=$_cdomain.key passphrase=\\\"\\\";\
  100. \ndelay 1;\
  101. \n/file remove $_cdomain.cer;\
  102. \n/file remove $_cdomain.key;\
  103. \ndelay 2;\
  104. \n/ip service set www-ssl certificate=$_cdomain.cer_0;\
  105. \n$ROUTER_OS_ADDITIONAL_SERVICES;\
  106. \n\"
  107. "
  108. # shellcheck disable=SC2029
  109. ssh -p "$ROUTER_OS_PORT" "$ROUTER_OS_USERNAME@$ROUTER_OS_HOST" "$DEPLOY_SCRIPT_CMD"
  110. # shellcheck disable=SC2029
  111. ssh -p "$ROUTER_OS_PORT" "$ROUTER_OS_USERNAME@$ROUTER_OS_HOST" "/system script run \"LE Cert Deploy - $_cdomain\""
  112. # shellcheck disable=SC2029
  113. ssh -p "$ROUTER_OS_PORT" "$ROUTER_OS_USERNAME@$ROUTER_OS_HOST" "/system script remove \"LE Cert Deploy - $_cdomain\""
  114. return 0
  115. }