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.

228 lines
5.4 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_STATUS=$(curl -w "%{http_code}" -G -s -o $WG_CONFFILE --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. if [ $HTTP_STATUS -ne 200 ]; then
  128. printf "Curl returned HTTP code %s\n" "$HTTP_STATUS"
  129. exit 1;
  130. fi
  131. printf "WG config successfully download at %s\n" "$WG_CONFFILE"
  132. }
  133. _install() {
  134. printf "install : This command does nothing ... yet (TDB)\n"
  135. }
  136. _process() {
  137. while [ ${#} -gt 0 ]; do
  138. case "${1}" in
  139. --help | -h)
  140. _usage
  141. return
  142. ;;
  143. --version | -v)
  144. _version
  145. return
  146. ;;
  147. --download | -d)
  148. _CMD="download"
  149. ;;
  150. --url | -u)
  151. WGPORTAL_URL="$2"
  152. shift
  153. ;;
  154. --filename | -f)
  155. WG_CONFFILE="$2"
  156. shift
  157. ;;
  158. --install | -i)
  159. _CMD="install"
  160. ;;
  161. --debug)
  162. if [ -z "$2" ] || _startswith "$2" "-"; then
  163. DEBUG="$DEBUG_LEVEL_DEFAULT"
  164. else
  165. DEBUG="$2"
  166. shift
  167. fi
  168. ;;
  169. --overwrite)
  170. __OVERWRITE="on"
  171. ;;
  172. *)
  173. printf "Unknown parameter : %s\n" "$1"
  174. return 1
  175. ;;
  176. esac
  177. shift 1
  178. done
  179. if [ ! "$_CMD" ]; then
  180. printf "Nothing to do, please specify a command (see --help)\n"
  181. fi
  182. case "${_CMD}" in
  183. download)
  184. _download
  185. ;;
  186. install)
  187. _install
  188. ;;
  189. *)
  190. if [ "$_CMD" ]; then
  191. printf "Invalid command: %s\n" "$_CMD"
  192. fi
  193. esac
  194. }
  195. main() {
  196. [ -z "$1" ] && _usage && return
  197. if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
  198. }
  199. _ARGS="$*"
  200. main "$@"