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.

233 lines
5.6 KiB

  1. #!/bin/sh
  2. # TODO :
  3. # - add root user detection
  4. # - verify curl output to prevent overwriting config file with an HTTP answer
  5. # - verify trailling slash existenz at the end of the URL param
  6. VER=0.1
  7. LOG_LEVEL_1=1
  8. LOG_LEVEL_2=2
  9. LOG_LEVEL_3=3
  10. DEFAULT_LOG_LEVEL="$LOG_LEVEL_1"
  11. DEBUG_LEVEL_1=1
  12. DEBUG_LEVEL_2=2
  13. DEBUG_LEVEL_3=3
  14. DEBUG_LEVEL_DEFAULT=$DEBUG_LEVEL_1
  15. DEBUG_LEVEL_NONE=0
  16. _usage() {
  17. echo "
  18. Usage: $0 <command> --url <wgportal_url> [options...]
  19. Commands :
  20. -d, --download only download config file
  21. -i, --install download config file and install the service (UNIMPLEMENTED)
  22. Parameters:
  23. -u, --url <url> Specify your WG Portal base URL
  24. -f, --filename <file> Specify Wireguard filename (default: /etc/wireguard/wg0.conf)
  25. "
  26. _version
  27. }
  28. _version() {
  29. printf "%s version pre-alpha 0.1 (yes we're very cautious)\n" "$0"
  30. printf "USE AT YOUR OWN RISK and read the code before\n"
  31. }
  32. _printargs() {
  33. _exitstatus="$?"
  34. if [ -z "$NO_TIMESTAMP" ] || [ "$NO_TIMESTAMP" = "0" ]; then
  35. printf -- "%s" "[$(date)] "
  36. fi
  37. if [ -z "$2" ]; then
  38. printf -- "%s" "$1"
  39. else
  40. printf -- "%s" "$1='$2'"
  41. fi
  42. printf "\n"
  43. # return the saved exit status
  44. return "$_exitstatus"
  45. }
  46. __debug_bash_helper() {
  47. # At this point only do for --debug 3
  48. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -lt "$DEBUG_LEVEL_3" ]; then
  49. return
  50. fi
  51. # Return extra debug info when running with bash, otherwise return empty
  52. # string.
  53. if [ -z "${BASH_VERSION}" ]; then
  54. return
  55. fi
  56. # We are a bash shell at this point, return the filename, function name, and
  57. # line number as a string
  58. _dbh_saveIFS=$IFS
  59. IFS=" "
  60. # Must use eval or syntax error happens under dash. The eval should use
  61. # single quotes as older versions of busybox had a bug with double quotes and
  62. # eval.
  63. # Use 'caller 1' as we want one level up the stack as we should be called
  64. # by one of the _debug* functions
  65. eval '_dbh_called=($(caller 1))'
  66. IFS=$_dbh_saveIFS
  67. eval '_dbh_file=${_dbh_called[2]}'
  68. if [ -n "${_script_home}" ]; then
  69. # Trim off the _script_home directory name
  70. eval '_dbh_file=${_dbh_file#$_script_home/}'
  71. fi
  72. eval '_dbh_function=${_dbh_called[1]}'
  73. eval '_dbh_lineno=${_dbh_called[0]}'
  74. printf "%-40s " "$_dbh_file:${_dbh_function}:${_dbh_lineno}"
  75. }
  76. _debug() {
  77. #  Log to file not implemented
  78. # if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
  79. # _log "$@"
  80. # fi
  81. # Sending log to syslog not implemented
  82. # if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
  83. # _syslog "$SYSLOG_DEBUG" "$@"
  84. # fi
  85. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
  86. _bash_debug=$(__debug_bash_helper)
  87. _printargs "${_bash_debug}$@" >&2
  88. fi
  89. }
  90. _startswith() {
  91. _str="$1"
  92. _sub="$2"
  93. echo "$_str" | grep "^$_sub" >/dev/null 2>&1
  94. }
  95. _download() {
  96. [ -z "$WGPORTAL_URL" ] && printf "Please set wg-portal URL (see help)\n" && return 1
  97. [ -z "$WG_CONFFILE" ] && WG_CONFFILE=/etc/wireguard/wg0.conf
  98. _debug "Using server: $WGPORTAL_URL"
  99. trap 'stty echo; exit 99;' INT
  100. if [ -f $WG_CONFFILE ]; then
  101. if [ "$__OVERWRITE" = "on" ]; then
  102. _debug "File already exists but overwritting as requested"
  103. else
  104. printf "WARNING : Wireguard config file already exists\n"
  105. printf "Exiting now to prevent override of your actual parameters\n"
  106. printf "You can force config overwriting with --overwrite parameter\n"
  107. exit 1;
  108. fi
  109. fi
  110. printf "Username: "
  111. IFS= read -r username
  112. printf "Password: "
  113. stty -echo
  114. IFS= read -r password
  115. stty echo
  116. printf "\n"
  117. printf "Peer public key: "
  118. IFS= read -r wgpubkey
  119. printf "\n"
  120. WGPORTAL_APIURL=$WGPORTAL_URL"api/v1/provisioning/peer"
  121. _debug "GET request to $WGPORTAL_APIURL"
  122. HTTP_RESPONSE=$(curl -G -s --write-out "HTTPSTATUS:%{http_code}" --user $username:$password --data-urlencode "PublicKey=$wgpubkey" -X GET $WGPORTAL_APIURL -H "accept: text/plain")
  123. res=$?
  124. if [ "$res" != "0" ]; then
  125. echo "the curl command failed with: $res"
  126. fi
  127. HTTP_BODY=$(echo "$HTTP_RESPONSE" | sed -E 's/HTTPSTATUS\:[0-9]{3}$//')
  128. HTTP_STATUS=$(echo "$HTTP_RESPONSE" | tr -d '\n' | sed -E 's/.*HTTPSTATUS:([0-9]{3})$/\1/')
  129. if [ $HTTP_STATUS = "200" ]; then
  130. echo "$HTTP_BODY" > $WG_CONFFILE
  131. printf "WG config successfully downloaded at %s\n" "$WG_CONFFILE"
  132. return
  133. fi
  134. printf "Curl returned HTTP code %s\n" "$HTTP_STATUS"
  135. exit 1;
  136. }
  137. _install() {
  138. printf "install : This command does nothing ... yet (TDB)\n"
  139. }
  140. _process() {
  141. while [ ${#} -gt 0 ]; do
  142. case "${1}" in
  143. --help | -h)
  144. _usage
  145. return
  146. ;;
  147. --version | -v)
  148. _version
  149. return
  150. ;;
  151. --download | -d)
  152. _CMD="download"
  153. ;;
  154. --url | -u)
  155. WGPORTAL_URL="$2"
  156. shift
  157. ;;
  158. --filename | -f)
  159. WG_CONFFILE="$2"
  160. shift
  161. ;;
  162. --install | -i)
  163. _CMD="install"
  164. ;;
  165. --debug)
  166. if [ -z "$2" ] || _startswith "$2" "-"; then
  167. DEBUG="$DEBUG_LEVEL_DEFAULT"
  168. else
  169. DEBUG="$2"
  170. shift
  171. fi
  172. ;;
  173. --overwrite)
  174. __OVERWRITE="on"
  175. ;;
  176. *)
  177. printf "Unknown parameter : %s\n" "$1"
  178. return 1
  179. ;;
  180. esac
  181. shift 1
  182. done
  183. if [ ! "$_CMD" ]; then
  184. printf "Nothing to do, please specify a command (see --help)\n"
  185. fi
  186. case "${_CMD}" in
  187. download)
  188. _download
  189. ;;
  190. install)
  191. _install
  192. ;;
  193. *)
  194. if [ "$_CMD" ]; then
  195. printf "Invalid command: %s\n" "$_CMD"
  196. fi
  197. esac
  198. }
  199. main() {
  200. [ -z "$1" ] && _usage && return
  201. if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
  202. }
  203. _ARGS="$*"
  204. main "$@"