avto4certbot.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #!/bin/bash
  2. #
  3. # author: Koshuba V.O.
  4. # license: GPL 2.0
  5. # create 2022
  6. #
  7. version="0.5.0";
  8. sname="avto4certbot";
  9. # script path
  10. path_script=$( cd -- $( dirname -- "${BASH_SOURCE[0]}" ) &> /dev/null && pwd );
  11. source "$path_script/avto4certbot.conf";
  12. event_sw=0;
  13. mode="";
  14. reports=();
  15. ##--@S static values
  16. # depends
  17. pkgdep=("curl" "certbot" "letsencrypt") # packages
  18. get_tools=("curl" "certbot" "letsencrypt")
  19. # - options
  20. cmd=$1;
  21. # - for LAMP server
  22. opt=$2;
  23. #--@F Get info area
  24. function getInfo() {
  25. ## test - null values
  26. if [ $tmp_dir == "" ]; then
  27. tmp_dir="/tmp";
  28. fi
  29. web_dir="$tmp_dir/www"
  30. conf_dir="$tmp_dir/conf"
  31. if [ $log_file == "" ]; then
  32. log_file="/var/log/syslog";
  33. fi
  34. if [ $sites_nginx == "" ]; then
  35. sites_nginx="/etc/nginx/sites-enabled";
  36. fi
  37. if [ $sites_apache == "" ]; then
  38. sites_apache="/etc/apache2/sites-enabled";
  39. fi
  40. if [ $path_ssl == "" ]; then
  41. path_ssl="/etc/ssl";
  42. fi
  43. if [ $path_cert == "" ]; then
  44. path_cert="/etc/letsencrypt/live";
  45. fi
  46. ## create temp directory
  47. if [ ! -d $tmp_dir ]; then
  48. mkdir -p $tmp_dir;
  49. fi
  50. ## create web directory
  51. if [ ! -d "$web_dir/.well-known/acme-challenge" ]; then
  52. mkdir -p $web_dir/.well-known/acme-challenge;
  53. chown -R www-data:www-data $web_dir;
  54. fi
  55. ## create conf directory
  56. if [ ! -d $conf_dir ]; then
  57. mkdir -p $conf_dir;
  58. fi
  59. ##
  60. if [[ $opt != "nginx" ]] || [[ "$opt" == "apache" ]]; then
  61. find $sites_apache/* -maxdepth 0 -type l -printf '%f\n' >$tmp_dir/active_sites.inf;
  62. fi
  63. if [[ $opt != "apache" ]] || [[ "$opt" == "nginx" ]]; then
  64. find $sites_nginx/* -maxdepth 0 -type l -printf '%f\n' >$tmp_dir/active_sites.inf;
  65. fi
  66. }
  67. #--@F Check the program dependency
  68. function checkDep() {
  69. # - msg debug
  70. echo "check depends..."
  71. if [ ! "$lang" ]; then
  72. lang="C.UTF-8"
  73. fi
  74. for ((itools = 0; itools != ${#get_tools[@]}; itools++)); do
  75. checktool=$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')
  76. if [[ $checktool = "" ]]; then
  77. sudo apt install ${pkgdep[$itools]}
  78. fi
  79. checktool=$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')
  80. if [[ $checktool != "" ]]; then
  81. eval get_${get_tools[$itools]}=$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')
  82. list_tools[${#list_tools[@]}]="$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')"
  83. else
  84. ## lang messages if yes then lang else us...
  85. reports=()
  86. reports[${#reports[@]}]="Sorry, there are no required packages to work, please install:${pkgdep[@]}"
  87. makeErr
  88. exit
  89. fi
  90. done
  91. }
  92. ##--@F make all errors
  93. function makeErr() {
  94. for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
  95. do
  96. echo "$rdate $sname: ${reports[$rpt_index]}">>$log_file;
  97. echo "${reports[$rpt_index]}";
  98. done
  99. exit 0;
  100. }
  101. ##--@F exec task
  102. function execTask(){
  103. for ((xd=0; xd != ${#domains[@]}; xd++)); do
  104. local site_data=( $(echo -e ${domains[$xd]}|sed 's/ /\n /g') );
  105. site_name="${site_data[0]}";
  106. site_owner="${site_data[1]}";
  107. site_port="${site_data[2]}";
  108. if [[ "$mode" !="" ]] || [[ "$mode" == "create"]]; then
  109. echo
  110. fi
  111. if [[ "$mode" !="" ]] || [[ "$mode" == "update"]]; then
  112. fi
  113. if [[ "$mode" !="" ]] || [[ "$mode" == "flist"]]; then
  114. fi
  115. done
  116. ## if event - yes
  117. if [ $event_sw != 0 ];then
  118. :>/etc/ssl/crt-list.txt
  119. for ((icrt=0; icrt != ${#domains[@]}; icrt++))
  120. do
  121. echo "$path_ssl/${domains[$icrt]}.pem">>/etc/ssl/crt-list.txt
  122. done
  123. fi
  124. }
  125. ##--@F create configs
  126. function createConf(){
  127. ## apache2 config
  128. if [[ $opt != "nginx" ]] || [[ "$opt" == "apache" ]]; then
  129. echo >$conf_dir/$site_name.conf;
  130. echo -e '<VirtualHost *:'"$site_port"'>' >>$conf_dir/$site_name.conf;
  131. echo -e 'ServerName '"$site_name"'' >>$conf_dir/$site_name.conf;
  132. echo -e 'ServerAlias '"$site_name"'' >>$conf_dir/$site_name.conf;
  133. echo -e 'DocumentRoot '"$web_dir"'' >>$conf_dir/$site_name.conf;
  134. echo -e '\n' >>$conf_dir/$site_name.conf;
  135. echo -e '<Directory'"$web_dir"' >' >>$conf_dir/$site_name.conf;
  136. echo -e 'Options -Indexes +FollowSymLinks +MultiViews' >>$conf_dir/$site_name.conf;
  137. echo -e 'AllowOverride All' >>$conf_dir/$site_name.conf;
  138. echo -e 'Require all granted' >>$conf_dir/$site_name.conf;
  139. echo -e '</Directory>' >>$conf_dir/$site_name.conf;
  140. echo -e '\n' >>$conf_dir/$site_name.conf;
  141. echo -e 'ErrorLog ${APACHE_LOG_DIR}/error.log' >>$conf_dir/$site_name.conf;
  142. echo -e 'CustomLog ${APACHE_LOG_DIR}/access.log combined' >>$conf_dir/$site_name.conf;
  143. echo -e '</VirtualHost>' >>$conf_dir/$site_name.conf;
  144. ln -s $conf_dir/$site_name.conf $sites_apache/$site_name.conf
  145. fi
  146. ## nginx config
  147. if [[ $opt != "apache" ]] || [[ "$opt" == "nginx" ]]; then
  148. echo >$conf_dir/$site_name.conf;
  149. echo -e 'server { listen 0.0.0.0:'"$site_port"';' >>$conf_dir/$site_name.conf;
  150. echo -e 'server_name '"$site_name"';' >>$conf_dir/$site_name.conf;
  151. echo -e '\n' >>$conf_dir/$site_name.conf;
  152. echo -e 'location /.well-known/acme-challenge {' >>$conf_dir/$site_name.conf;
  153. echo -e ' allow all;' >>$conf_dir/$site_name.conf;
  154. echo -e ' autoindex off;' >>$conf_dir/$site_name.conf;
  155. echo -e ' default_type "text/plain";' >>$conf_dir/$site_name.conf;
  156. echo -e ' root '"$web_dir"';' >>$conf_dir/$site_name.conf;
  157. echo -e '}' >>$conf_dir/$site_name.conf;
  158. echo -e 'location = /.well-known {' >>$conf_dir/$site_name.conf;
  159. echo -e ' return 404;' >>$conf_dir/$site_name.conf;
  160. echo -e '}' >>$conf_dir/$site_name.conf;
  161. echo -e 'error_page 404 /404.html;' >>$conf_dir/$site_name.conf;
  162. echo -e 'error_page 500 502 503 504 /50x.html;' >>$conf_dir/$site_name.conf;
  163. echo -e '\n' >>$conf_dir/$site_name.conf;
  164. echo -e 'error_log /var/log/nginx/err-certbot.log;' >>$conf_dir/$site_name.conf;
  165. echo -e 'access_log /var/log/nginx/access-certbot.log;' >>$conf_dir/$site_name.conf;
  166. echo -e '}' >>$conf_dir/$site_name.conf;
  167. ln -s $conf_dir/$site_name.conf $sites_nginx/$site_name.conf
  168. fi
  169. }
  170. ##--@F create configs
  171. function pHelp(){
  172. echo "$sname:$version"
  173. echo "please input pameters: avto4certbot.sh --create [apache & nginx]| --update [apache & nginx] | --flist [apache & nginx]";
  174. echo "avto4certbot.sh --create; create new certificate or --create [apache & nginx]; create new certificate "
  175. echo "avto4certbot.sh --update; update certificates or --update [apache & nginx]; update [apache & nginx];"
  176. echo "avto4certbot.sh --flist; update certificates from ssl or --flist [apache & nginx]; rescan list certificates;"
  177. echo "avto4certbot.sh --help; this help"
  178. echo "* examples:"
  179. echo "avtocertbot.sh --test apache"
  180. echo "or"
  181. echo "avtocertbot.sh --test nginx"
  182. }
  183. case "$cmd" in
  184. ## create cert
  185. "--create" | "--create" )
  186. if [ "$opt" != "" ]]; then
  187. mode="create";
  188. execTask;
  189. else
  190. echo "no parameter specified - nginx or apache?"
  191. fi
  192. ;;
  193. ## update cert
  194. "--update" | "--update" )
  195. if [ "$opt" != "" ]]; then
  196. mode="update";
  197. execTask;
  198. else
  199. echo "no parameter specified - nginx or apache?"
  200. fi
  201. ;;
  202. ## update cert
  203. "--test" | "--test" )
  204. if [ "$opt" != "" ]]; then
  205. mode="test";
  206. execTask;
  207. else
  208. echo "no parameter specified - nginx or apache?"
  209. fi
  210. ;;
  211. ## update cert force
  212. "--flist" | "--flist" )
  213. if [ "$opt" != "" ]]; then
  214. mode="flist";
  215. execTask;
  216. else
  217. echo "no parameter specified - nginx or apache?"
  218. fi
  219. ;;
  220. ## start defaults
  221. * )
  222. checkDep;
  223. pHelp;
  224. ;;
  225. esac
  226. exit