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.

204 lines
6.0 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. # To adopt parameters to `scp` and/or `ssh` set the optional
  52. # `ROUTER_OS_SSH_CMD` and `ROUTER_OS_SCP_CMD` variables accordingly,
  53. # see ssh(1) and scp(1) for parameters to those commands.
  54. #
  55. # Example:
  56. # ```ssh
  57. # export ROUTER_OS_SSH_CMD="ssh -i /acme.sh/.ssh/router.example.com -o UserKnownHostsFile=/acme.sh/.ssh/known_hosts"
  58. # export ROUTER_OS_SCP_CMD="scp -i /acme.sh/.ssh/router.example.com -o UserKnownHostsFile=/acme.sh/.ssh/known_hosts"
  59. # ````
  60. #
  61. # returns 0 means success, otherwise error.
  62. ######## Public functions #####################
  63. #domain keyfile certfile cafile fullchain
  64. routeros_deploy() {
  65. _cdomain="$1"
  66. _ckey="$2"
  67. _ccert="$3"
  68. _cca="$4"
  69. _cfullchain="$5"
  70. _err_code=0
  71. _debug _cdomain "$_cdomain"
  72. _debug _ckey "$_ckey"
  73. _debug _ccert "$_ccert"
  74. _debug _cca "$_cca"
  75. _debug _cfullchain "$_cfullchain"
  76. _getdeployconf ROUTER_OS_HOST
  77. if [ -z "$ROUTER_OS_HOST" ]; then
  78. _debug "Using _cdomain as ROUTER_OS_HOST, please set if not correct."
  79. ROUTER_OS_HOST="$_cdomain"
  80. fi
  81. _getdeployconf ROUTER_OS_USERNAME
  82. if [ -z "$ROUTER_OS_USERNAME" ]; then
  83. _err "Need to set the env variable ROUTER_OS_USERNAME"
  84. return 1
  85. fi
  86. _getdeployconf ROUTER_OS_PORT
  87. if [ -z "$ROUTER_OS_PORT" ]; then
  88. _debug "Using default port 22 as ROUTER_OS_PORT, please set if not correct."
  89. ROUTER_OS_PORT=22
  90. fi
  91. _getdeployconf ROUTER_OS_SSH_CMD
  92. if [ -z "$ROUTER_OS_SSH_CMD" ]; then
  93. _debug "Use default ssh setup."
  94. ROUTER_OS_SSH_CMD="ssh -p $ROUTER_OS_PORT"
  95. fi
  96. _getdeployconf ROUTER_OS_SCP_CMD
  97. if [ -z "$ROUTER_OS_SCP_CMD" ]; then
  98. _debug "USe default scp setup."
  99. ROUTER_OS_SCP_CMD="scp -P $ROUTER_OS_PORT"
  100. fi
  101. _getdeployconf ROUTER_OS_ADDITIONAL_SERVICES
  102. if [ -z "$ROUTER_OS_ADDITIONAL_SERVICES" ]; then
  103. _debug "Not enabling additional services"
  104. ROUTER_OS_ADDITIONAL_SERVICES=""
  105. fi
  106. _savedeployconf ROUTER_OS_HOST "$ROUTER_OS_HOST"
  107. _savedeployconf ROUTER_OS_USERNAME "$ROUTER_OS_USERNAME"
  108. _savedeployconf ROUTER_OS_PORT "$ROUTER_OS_PORT"
  109. _savedeployconf ROUTER_OS_SSH_CMD "$ROUTER_OS_SSH_CMD"
  110. _savedeployconf ROUTER_OS_SCP_CMD "$ROUTER_OS_SCP_CMD"
  111. _savedeployconf ROUTER_OS_ADDITIONAL_SERVICES "$ROUTER_OS_ADDITIONAL_SERVICES"
  112. # push key to routeros
  113. if ! _scp_certificate "$_ckey" "$ROUTER_OS_USERNAME@$ROUTER_OS_HOST:$_cdomain.key"; then
  114. return $_err_code
  115. fi
  116. # push certificate chain to routeros
  117. if ! _scp_certificate "$_cfullchain" "$ROUTER_OS_USERNAME@$ROUTER_OS_HOST:$_cdomain.cer"; then
  118. return $_err_code
  119. fi
  120. DEPLOY_SCRIPT_CMD=":do {/system script remove \"LECertDeploy-$_cdomain\" } on-error={ }; \
  121. /system script add name=\"LECertDeploy-$_cdomain\" owner=$ROUTER_OS_USERNAME \
  122. comment=\"generated by routeros deploy script in acme.sh\" \
  123. source=\"/certificate remove [ find name=$_cdomain.cer_0 ];\
  124. \n/certificate remove [ find name=$_cdomain.cer_1 ];\
  125. \n/certificate remove [ find name=$_cdomain.cer_2 ];\
  126. \ndelay 1;\
  127. \n/certificate import file-name=$_cdomain.cer passphrase=\\\"\\\";\
  128. \n/certificate import file-name=$_cdomain.key passphrase=\\\"\\\";\
  129. \ndelay 1;\
  130. \n:do {/file remove $_cdomain.cer; } on-error={ }\
  131. \n:do {/file remove $_cdomain.key; } on-error={ }\
  132. \ndelay 2;\
  133. \n/ip service set www-ssl certificate=$_cdomain.cer_0;\
  134. \n$ROUTER_OS_ADDITIONAL_SERVICES;\
  135. \n\"
  136. "
  137. if ! _ssh_remote_cmd "$DEPLOY_SCRIPT_CMD"; then
  138. return $_err_code
  139. fi
  140. if ! _ssh_remote_cmd "/system script run \"LECertDeploy-$_cdomain\""; then
  141. return $_err_code
  142. fi
  143. if ! _ssh_remote_cmd "/system script remove \"LECertDeploy-$_cdomain\""; then
  144. return $_err_code
  145. fi
  146. return 0
  147. }
  148. # inspired by deploy/ssh.sh
  149. _ssh_remote_cmd() {
  150. _cmd="$1"
  151. _secure_debug "Remote commands to execute: $_cmd"
  152. _info "Submitting sequence of commands to routeros"
  153. # quotations in bash cmd below intended. Squash travis spellcheck error
  154. # shellcheck disable=SC2029
  155. $ROUTER_OS_SSH_CMD "$ROUTER_OS_USERNAME@$ROUTER_OS_HOST" "$_cmd"
  156. _err_code="$?"
  157. if [ "$_err_code" != "0" ]; then
  158. _err "Error code $_err_code returned from routeros"
  159. fi
  160. return $_err_code
  161. }
  162. _scp_certificate() {
  163. _src="$1"
  164. _dst="$2"
  165. _secure_debug "scp '$_src' to '$_dst'"
  166. _info "Push key '$_src' to routeros"
  167. $ROUTER_OS_SCP_CMD "$_src" "$_dst"
  168. _err_code="$?"
  169. if [ "$_err_code" != "0" ]; then
  170. _err "Error code $_err_code returned from scp"
  171. fi
  172. return $_err_code
  173. }