Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. set -e;
  3.  
  4. VPNM_CONF="${VPNM_CONF:-$HOME/.vpnm}";
  5. VPNM_SUDO="${VPNM_SUDO:-sudo}";
  6. VPNM_GPG="${VPNM_GPG:-gpp2}";
  7. VPNM_CREDS="${VPNM_CREDS:-keyring}";
  8.  
  9. main() {
  10. local action="$1";
  11. shift;
  12.  
  13. case "$action" in
  14. enable)
  15. enable_vpn "$1";
  16. return;
  17. ;;
  18. disable)
  19. disable_vpn "$1";
  20. return;
  21. ;;
  22. start)
  23. start_vpn "$1";
  24. return;
  25. ;;
  26. stop)
  27. stop_vpn "$1";
  28. return;
  29. ;;
  30. status)
  31. status_vpn "$1";
  32. return;
  33. ;;
  34. add)
  35. add_vpn $@;
  36. return;
  37. ;;
  38. edit)
  39. edit_vpn $@;
  40. return;
  41. ;;
  42. list)
  43. list_vpn;
  44. return;
  45. ;;
  46. list-enabled)
  47. list_enabled_vpn;
  48. return;
  49. ;;
  50. set-private-pass)
  51. set_private_pass "$1";
  52. return;
  53. ;;
  54. set-auth)
  55. set_auth "$1";
  56. return;
  57. ;;
  58. boot)
  59. boot;
  60. return;
  61. ;;
  62. help)
  63. print_man;
  64. return;
  65. ;;
  66. *)
  67. echo "Action ${action} doesn't exist"
  68. echo;
  69. print_man;
  70. exit 1;
  71. ;;
  72. esac
  73.  
  74. }
  75.  
  76. print_man () {
  77. cat <<MAN
  78. vpnm - A Bash vpn manager
  79.  
  80. Commands:
  81. - enable {vpn name} enables and starts the vpn
  82. - disable {vpn name} disable and doesnt stop the vpn
  83. - start {vpn name} starts vpn without enabling it
  84. - stop {vpn name} stop vpn without disabling it
  85. - add {vpn name} {openvpn config} [additionial files] add a vpn to the manager
  86. - edit {vpn name} [file] edit the openvpn config or given file of vpn
  87. - list list all vpns in config manager
  88. - list-enabled list all enabled vpns
  89. - set-private-pass {vpn name} save the private key pass in a gpg encrypted file or your keyring
  90. - set-auth {vpn name} save the auth user combo in a gpg encrypted file or your keyring
  91. MAN
  92. }
  93.  
  94. add_vpn () {
  95. local name="$1"; shift;
  96. local config="$1"; shift;
  97.  
  98. ensure_not_empty "name" "${name}";
  99. ensure_not_empty "config" "${config}";
  100.  
  101. if check_vpn_exists "${name}"; then
  102. echo "Vpn with the name '${name}' already exists";
  103. exit 1;
  104. fi
  105.  
  106. local vpndir="$(get_vpn_dir "${name}")";
  107.  
  108. mkdir -p "${vpndir}/config";
  109. cp "${config}" "${vpndir}/config/server.conf";
  110. cp -a $@ "${vpndir}/config/";
  111.  
  112. echo "Succesfully created vpn ${name}";
  113. }
  114.  
  115. enable_vpn () {
  116. local name="$1";
  117. ensure_not_empty "name" "${name}";
  118. ensure_vpn_exists "${name}";
  119. if grep -q "^${name}$" "${VPNM_CONF}/enabled"; then
  120. echo "${name} is already enabled";
  121. exit 1;
  122. fi
  123.  
  124. echo "${name}" >> "${VPNM_CONF}/enabled";
  125. start_vpn "${name}";
  126. }
  127.  
  128. disable_vpn () {
  129. local name="$1";
  130. ensure_not_empty "name" "${name}";
  131. grep -v "^${name}$" "${VPNM_CONF}/enabled" > "${VPNM_CONF}/enabled.new" || true;
  132. rm "${VPNM_CONF}/enabled";
  133. mv "${VPNM_CONF}/enabled.new" "${VPNM_CONF}/enabled";
  134. }
  135.  
  136. boot () {
  137. echo "Booting vpns";
  138.  
  139. while read -r vpn;
  140. do
  141. start_vpn "${vpn}";
  142. done < "${VPNM_CONF}/enabled";
  143. }
  144.  
  145. list_enabled_vpn () {
  146. cat "${VPNM_CONF}/enabled";
  147. }
  148.  
  149. edit_vpn () {
  150. local name="$1";
  151. local config="$2";
  152.  
  153. ensure_not_empty "name" "${name}";
  154. ensure_vpn_exists "${name}";
  155.  
  156. "$EDITOR" "$(get_vpn_dir "${name}")/config/${config:-server.conf}";
  157. }
  158.  
  159. list_vpn () {
  160. ls -1 "${VPNM_CONF}/vpn";
  161. }
  162.  
  163. status_vpn () {
  164. local name="$1";
  165.  
  166. ensure_not_empty "name" "${name}";
  167. ensure_vpn_exists "${name}";
  168.  
  169. local vpndir="$(get_vpn_dir "${name}")";
  170.  
  171. if [ -f "${vpndir}/pid" ] && $VPNM_SUDO kill -0 "$(cat "${vpndir}/pid")" 2>/dev/null; then
  172. echo "Vpn ${name} is running...";
  173. exit 0;
  174. fi;
  175.  
  176. echo "Vpn ${name} is not running...";
  177. exit 1;
  178. }
  179.  
  180. start_vpn () {
  181. local name="$1";
  182.  
  183. ensure_not_empty "name" "${name}";
  184. ensure_vpn_exists "${name}";
  185.  
  186. local vpndir="$(get_vpn_dir "${name}")";
  187.  
  188. if [ -f "${vpndir}/pid" ] && $VPNM_SUDO kill -0 "$(cat "${vpndir}/pid")" 2>/dev/null; then
  189. echo "Vpn ${name} is already running..."
  190. exit 0;
  191. fi;
  192.  
  193. local args="";
  194. if [ -f "${vpndir}/auth" ]; then
  195. local user="$(cat "${vpndir}/auth")";
  196. local pass="$(lookup_pass "${name}" "auth")";
  197.  
  198. [ -f "${vpndir}/.pw" ] && rm "${vpndir}/.pw";
  199. echo "$user" > "${vpndir}/.pw";
  200. echo "$pass" >> "${vpndir}/.pw";
  201.  
  202. args="$args --auth-user-pass ${vpndir}/.pw";
  203. fi
  204.  
  205. if [ -f "${vpndir}/privkeypass" ]; then
  206. args="$args --askpass ${vpndir}/.pk";
  207. lookup_pass "${name}" "private" > "${vpndir}/.pk";
  208. fi;
  209.  
  210. cd "${vpndir}/config";
  211. local ret=0;
  212. $VPNM_SUDO openvpn --writepid "${vpndir}/pid" --daemon --log-append "${vpndir}/log" --config "${vpndir}/config/server.conf" $args || ret=$?;
  213. sleep .5;
  214.  
  215. [ -f "${vpndir}/.pk" ] && rm "${vpndir}/.pk";
  216. [ -f "${vpndir}/.pw" ] && rm "${vpndir}/.pw";
  217.  
  218. if [ $ret != 0 ]; then
  219. echo "Failed to start openvpn";
  220. exit $ret;
  221. fi;
  222.  
  223. $VPNM_SUDO chmod a+r "${vpndir}/log";
  224.  
  225. echo "Started vpn ${name}";
  226. }
  227.  
  228. stop_vpn () {
  229. local name="$1";
  230.  
  231. ensure_not_empty "name" "${name}";
  232. ensure_vpn_exists "${name}";
  233.  
  234. local vpndir="$(get_vpn_dir "${name}")";
  235. local pid="$(cat "${vpndir}/pid")";
  236.  
  237. if [ ! -f "${vpndir}/pid" ] || ! $VPNM_SUDO kill -0 "${pid}" 2>/dev/null; then
  238. echo "Vpn ${name} is not running..."
  239. exit 0;
  240. fi;
  241.  
  242. $VPNM_SUDO kill "${pid}";
  243. $VPNM_SUDO rm "${vpndir}/pid";
  244.  
  245. while $VPNM_SUDO kill -0 "${pid}" 2>/dev/null; do
  246. sleep 0.2;
  247. done;
  248.  
  249. echo "Stopped vpn ${name}";
  250. }
  251.  
  252. set_private_pass () {
  253. local name="$1";
  254.  
  255. ensure_not_empty "name" "${name}";
  256. ensure_vpn_exists "${name}";
  257.  
  258. read -rsp "Please enter the private key password: " pass;
  259. echo;
  260.  
  261. store_pass "$name" "private" "$pass";
  262.  
  263. touch "$(get_vpn_dir "$name")/privkeypass";
  264. }
  265.  
  266. set_auth () {
  267. local name="$1";
  268.  
  269. ensure_not_empty "name" "${name}";
  270. ensure_vpn_exists "${name}";
  271.  
  272. read -rp "Please enter the user for ${name}: " user;
  273. read -rsp "Please enter the password for ${name}: " pass;
  274. echo;
  275.  
  276. store_pass "$name" "auth" "$pass";
  277.  
  278. echo "${user}" > "$(get_vpn_dir "$name")/auth";
  279. }
  280.  
  281. get_vpn_dir () {
  282. local name="$1";
  283. echo "${VPNM_CONF}/vpn/${name}";
  284. }
  285.  
  286. check_vpn_exists () {
  287. local name="$1";
  288. test -d "$(get_vpn_dir "${name}")";
  289. return $?
  290. }
  291.  
  292. ensure_not_empty () {
  293. local name="$1";
  294. local value="$2";
  295. if [ -z "${value}" ]; then
  296. echo "${name} is empty";
  297. exit 1;
  298. fi
  299. }
  300.  
  301. ensure_vpn_exists () {
  302. local name="$1";
  303.  
  304. if ! check_vpn_exists "${name}"; then
  305. echo "Vpn ${name} doesn't exist";
  306. exit 1;
  307. fi
  308. }
  309.  
  310. lookup_pass () {
  311. local name="$1";
  312. local what="$2";
  313.  
  314. case "${VPNM_CREDS}" in
  315. keyring)
  316. secret-tool lookup "vpnm/vpn/${name}" "${what}";
  317. ;;
  318. gpg)
  319. $VPNM_GPG --decrypt "$(get_vpn_dir "${name}")/pass/${what}.gpg";
  320. ;;
  321. pass)
  322. pass show "vpnm/vpn/${name}/${what}";
  323. ;;
  324. *)
  325. echo "Secret storage engine ${VPNM_CREDS}, not available";
  326. exit 1;
  327. esac
  328. }
  329.  
  330. store_pass () {
  331. local name="$1";
  332. local what="$2";
  333. local pass="$3";
  334.  
  335. case "${VPNM_CREDS}" in
  336. keyring)
  337. echo "${pass}" | secret-tool store --label "${what} password for ${name} vpn" "vpnm/vpn/${name}" "${what}";
  338. ;;
  339. gpg)
  340. if [ -z "${VPNM_GPGKEY}" ]; then
  341. echo "No GPG key given to encrypt passwords with";
  342. fi
  343.  
  344. local vpnpassdir="$(get_vpn_dir "${name}")/pass";
  345. [ ! -d "${vpnpassdir}" ] && mkdir "${vpnpassdir}"
  346. echo "${pass}" | $VPNM_GPG --encrypt --armor -r "$VPNM_GPGKEY" | "${vpnpassdir}/${what}.gpg";
  347. ;;
  348. pass)
  349. echo "${pass}" | pass insert -e "vpnm/vpn/${name}/${what}";
  350. ;;
  351. *)
  352. echo "Secret storage engine ${VPNM_CREDS}, not available";
  353. exit 1;
  354. esac
  355. }
  356.  
  357. main $@;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement