_common.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/bin/bash
  2. #
  3. # Common variables and helpers
  4. #
  5. pkg_dependencies="sipcalc dnsutils openvpn curl fake-hwclock"
  6. service_name="ynh-vpnclient"
  7. service_checker_name=$service_name"-checker"
  8. # Operations needed by both 'install' and 'upgrade' scripts
  9. function vpnclient_deploy_files_and_services()
  10. {
  11. # Ensure vpnclient_ynh has its own system user
  12. if ! ynh_system_user_exists ${app}
  13. then
  14. ynh_system_user_create ${app}
  15. fi
  16. # Install command-line cube file loader
  17. install -o root -g root -m 0755 ../conf/$service_name-loadcubefile.sh /usr/local/bin/
  18. # Copy confs
  19. chown root:${app} /etc/openvpn/
  20. chmod 775 /etc/openvpn/
  21. mkdir -pm 0755 /etc/yunohost/hooks.d/post_iptable_rules/
  22. mkdir -pm 0755 /etc/systemd/system/openvpn@.service.d/
  23. install -b -o root -g ${app} -m 0644 ../conf/openvpn_client.conf.tpl /etc/openvpn/client.conf.tpl
  24. install -b -o root -g root -m 0755 ../conf/hook_post-iptable-rules /etc/yunohost/hooks.d/90-vpnclient.tpl
  25. install -b -o root -g root -m 0644 ../conf/openvpn@.service /etc/systemd/system/openvpn@.service.d/override.conf
  26. # Create certificates directory
  27. mkdir -pm 0770 /etc/openvpn/keys/
  28. chown root:${app} /etc/openvpn/keys/
  29. # Create scripts directory
  30. mkdir -pm 0775 /etc/openvpn/scripts
  31. mkdir -pm 0775 /etc/openvpn/scripts/route-up.d
  32. mkdir -pm 0775 /etc/openvpn/scripts/route-down.d
  33. install -b -o root -g root -m 0755 ../conf/openvpn_run-parts.sh /etc/openvpn/scripts/run-parts.sh
  34. #=================================================
  35. # Copy init script
  36. install -o root -g root -m 0755 ../conf/$service_name /usr/local/bin/
  37. # Copy checker timer
  38. install -o root -g root -m 0755 ../conf/$service_checker_name.sh /usr/local/bin/
  39. install -o root -g root -m 0644 ../conf/$service_checker_name.timer /etc/systemd/system/
  40. systemctl daemon-reload
  41. #=================================================
  42. # SETUP SYSTEMD
  43. #=================================================
  44. ynh_print_info "Configuring a systemd service..."
  45. ynh_add_systemd_config $service_name "$service_name.service"
  46. ynh_add_systemd_config $service_checker_name "$service_checker_name.service"
  47. }
  48. function read_cube() {
  49. local config_file="$1"
  50. local key="$2"
  51. local tmp_dir=$(dirname "$config_file")
  52. setting_value="$(jq --raw-output ".$key" "$config_file")"
  53. if [[ "$setting_value" == "null" ]]
  54. then
  55. setting_value=''
  56. # Save file in tmp dir
  57. elif [[ "$key" == "crt_"* ]]
  58. then
  59. if [ -n "${setting_value}" ]
  60. then
  61. echo "${setting_value}" | sed 's/|/\n/g' > "$tmp_dir/$key"
  62. setting_value="$tmp_dir/$key"
  63. fi
  64. fi
  65. echo $setting_value
  66. }
  67. function convert_cube_file()
  68. {
  69. local config_file="$1"
  70. local tmp_dir=$(dirname "$config_file")
  71. ynh_print_info --message="Transforming .cube into OVPN file"
  72. server_name="$(read_cube $config_file server_name)"
  73. server_port="$(read_cube $config_file server_port)"
  74. server_proto="$(read_cube $config_file server_proto)"
  75. ip6_net="$(read_cube $config_file ip6_net)"
  76. ip6_addr="$(read_cube $config_file ip6_addr)"
  77. login_user="$(read_cube $config_file login_user)"
  78. login_passphrase="$(read_cube $config_file login_passphrase)"
  79. dns0="$(read_cube $config_file dns0)"
  80. dns1="$(read_cube $config_file dns1)"
  81. crt_server_ca="$(read_cube $config_file crt_server_ca)"
  82. crt_client="$(read_cube $config_file crt_client)"
  83. crt_client_key="$(read_cube $config_file crt_client_key)"
  84. crt_client_ta="$(read_cube $config_file crt_client_ta)"
  85. if [[ -z "$dns0" && -z "$dns1" ]]; then
  86. dns_method="yunohost"
  87. else
  88. dns_method="custom"
  89. nameservers="$dns0,$dns1"
  90. fi
  91. # Build specific OVPN template
  92. config_template="$tmp_dir/client.conf.tpl"
  93. cp -f /etc/yunohost/apps/vpnclient/conf/openvpn_client.conf.tpl "$config_template"
  94. # Remove some lines
  95. jq --raw-output '.openvpn_rm[]' "${config_file}" | while read -r rm_regex
  96. do
  97. if [ ! -z "${rm_regex}" ]; then
  98. sed -i "/${rm_regex/\//\\\/}/d" "$config_template"
  99. fi
  100. done
  101. # Add some other lines
  102. echo "# Custom additions from .cube" >> "$config_template"
  103. jq --raw-output ".openvpn_add[]" "${config_file}" >> "$config_template"
  104. # Temporarily tweak sever_proto for template hydratation
  105. if [ "$server_proto" == tcp ]; then
  106. server_proto=tcp-client
  107. fi
  108. # Define other needed vars for template hydratation
  109. [ -e "$crt_client_key" ] && cert_comment="" || cert_comment="#"
  110. [ -e "$crt_client_ta" ] && ta_comment="" || ta_comment="#"
  111. [[ "$server_proto" =~ udp ]] && udp_comment="" || udp_comment="#"
  112. [ -n "$login_user" ] && login_comment="" || login_comment="#"
  113. # Actually generate/hydrate the final configuration
  114. ynh_add_config --template="$config_template" --destination="$config_file"
  115. if [ "$server_proto" == tcp-client ]; then
  116. server_proto=tcp
  117. fi
  118. }
  119. function convert_ovpn_file()
  120. {
  121. local config_file="$1"
  122. local tmp_dir=$(dirname "$config_file")
  123. ynh_print_info --message="Extracting TLS keys from .ovpn file"
  124. if grep -q '^\s*<ca>' ${config_file}
  125. then
  126. grep -Poz '(?<=<ca>)(.*\n)*.*(?=</ca>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_server_ca
  127. crt_server_ca=$tmp_dir/crt_server_ca
  128. sed -i '/^\s*<ca>/,/\s*<\/ca>/d' ${config_file}
  129. sed -i '/^\s*ca\s/d' ${config_file}
  130. echo -e "\nca /etc/openvpn/keys/ca-server.crt" >> $config_file
  131. fi
  132. if grep -q '^\s*<cert>' ${config_file}
  133. then
  134. grep -Poz '(?<=<cert>)(.*\n)*.*(?=</cert>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client
  135. crt_client=$tmp_dir/crt_client
  136. sed -i '/^\s*<cert>/,/\s*<\/cert>/d' ${config_file}
  137. sed -i '/^\s*cert\s/d' ${config_file}
  138. echo -e "\ncert /etc/openvpn/keys/user.crt" >> ${config_file}
  139. elif ! grep -q '^\s*cert\s' ${config_file}
  140. then
  141. crt_client=""
  142. fi
  143. if grep -q '^\s*<key>' ${config_file}
  144. then
  145. grep -Poz '(?<=<key>)(.*\n)*.*(?=</key>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client_key
  146. crt_client_key=$tmp_dir/crt_client_key
  147. sed -i '/^\s*<key>/,/\s*<\/key>/d' ${config_file}
  148. sed -i '/^\s*key\s/d' ${config_file}
  149. echo -e "\nkey /etc/openvpn/keys/user.key" >> ${config_file}
  150. elif ! grep -q '^\s*key\s' ${config_file}
  151. then
  152. crt_client_key=""
  153. fi
  154. if grep -q '^\s*<tls-auth>' ${config_file}
  155. then
  156. grep -Poz '(?<=<tls-auth>)(.*\n)*.*(?=</tls-auth>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client_ta
  157. crt_client_ta=$tmp_dir/crt_client_ta
  158. sed -i '/^\s*<tls-auth>/,/\s*<\/tls-auth>/d' ${config_file}
  159. sed -i '/^\s*tls-auth\s/d' ${config_file}
  160. echo -e "\ntls-auth /etc/openvpn/keys/user_ta.key 1" >> ${config_file}
  161. elif ! grep -q '^\s*tls-auth\s' ${config_file}
  162. then
  163. crt_client_ta=""
  164. fi
  165. sed -i 's@^\s*ca\s.*$@ca /etc/openvpn/keys/ca-server.crt@g' ${config_file}
  166. sed -i 's@^\s*cert\s.*$@cert /etc/openvpn/keys/user.crt@g' ${config_file}
  167. sed -i 's@^\s*key\s.*$@key /etc/openvpn/keys/user.key@g' ${config_file}
  168. sed -i 's@^\s*tls-auth\s.*$@tls-auth /etc/openvpn/keys/user_ta.key 1@g' ${config_file}
  169. if ! grep -q '^\s*route-up "/etc/openvpn/scripts/run-parts.sh route-up"' ${config_file}
  170. then
  171. echo -e 'route-up "/etc/openvpn/scripts/run-parts.sh route-up"' >> ${config_file}
  172. fi
  173. if ! grep -q '^\s*down "/etc/openvpn/scripts/run-parts.sh route-down"' ${config_file}
  174. then
  175. echo -e 'down "/etc/openvpn/scripts/run-parts.sh route-down"' >> ${config_file}
  176. fi
  177. # Currently we need root priviledge to create tun0
  178. sed -i '/^\s*user\s/d' ${config_file}
  179. sed -i '/^\s*group\s/d' ${config_file}
  180. }