avto4certbot.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 2>/dev/null;
  62. get_tools[${#get_tools[@]}]="apache2";
  63. fi
  64. if [[ $opt != "apache" ]] || [[ "$opt" == "nginx" ]]; then
  65. find $sites_nginx/* -maxdepth 0 -type l -printf '%f\n' >$tmp_dir/active_sites.inf 2>/dev/null;
  66. get_tools[${#get_tools[@]}]="nginx";
  67. fi
  68. }
  69. #--@F Check the program dependency
  70. function checkDep() {
  71. # - msg debug
  72. echo "check depends..."
  73. if [ ! "$lang" ]; then
  74. lang="C.UTF-8"
  75. fi
  76. for ((itools = 0; itools != ${#get_tools[@]}; itools++)); do
  77. checktool=$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')
  78. if [[ $checktool = "" ]]; then
  79. sudo apt install ${pkgdep[$itools]}
  80. fi
  81. checktool=$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')
  82. if [[ $checktool != "" ]]; then
  83. eval get_${get_tools[$itools]}=$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')
  84. list_tools[${#list_tools[@]}]="$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')"
  85. else
  86. ## lang messages if yes then lang else us...
  87. reports=()
  88. reports[${#reports[@]}]="Sorry, there are no required packages to work, please install:${pkgdep[@]}"
  89. makeErr
  90. exit
  91. fi
  92. done
  93. }
  94. ##--@F make all errors
  95. function makeErr() {
  96. for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
  97. do
  98. echo "$rdate $sname: ${reports[$rpt_index]}">>$log_file;
  99. echo "${reports[$rpt_index]}";
  100. done
  101. exit 0;
  102. }
  103. ##--@F exec task
  104. function execTask(){
  105. for ((xd=0; xd != ${#domains[@]}; xd++)); do
  106. local site_data=( $(echo -e ${domains[$xd]}|sed 's/ /\n /g') );
  107. site_name="${site_data[0]}";
  108. site_owner="${site_data[1]}";
  109. site_port="${site_data[2]}";
  110. case "$cmd" in
  111. ## create cert
  112. "--create" | "--create" )
  113. echo "ok1"
  114. ;;
  115. ## create cert
  116. "--update" | "--update" )
  117. echo "ok2"
  118. ;;
  119. ## create cert
  120. "--flist" | "--flist" )
  121. echo "ok3"
  122. ;;
  123. ## start defaults
  124. * )
  125. reports=()
  126. reports[${#reports[@]}]="error option!"
  127. makeErr;
  128. ;;
  129. esac
  130. done
  131. ## if event - yes
  132. if [ $event_sw != 0 ];then
  133. echo>/etc/ssl/crt-list.txt
  134. for ((xt=0; xt != ${#domains[@]}; xt++)); do
  135. local site_data=( $(echo -e ${domains[$xt]}|sed 's/ /\n /g') );
  136. echo "$path_ssl/${site_data[0]}.pem">>/etc/ssl/crt-list.txt
  137. done
  138. fi
  139. }
  140. ##--@F create configs
  141. function createConf(){
  142. ## apache2 config
  143. if [[ $opt != "nginx" ]] || [[ "$opt" == "apache" ]]; then
  144. echo >$conf_dir/$site_name.conf;
  145. echo -e '<VirtualHost *:'"$site_port"'>' >>$conf_dir/$site_name.conf;
  146. echo -e 'ServerName '"$site_name"'' >>$conf_dir/$site_name.conf;
  147. echo -e 'ServerAlias '"$site_name"'' >>$conf_dir/$site_name.conf;
  148. echo -e 'DocumentRoot '"$web_dir"'' >>$conf_dir/$site_name.conf;
  149. echo -e '\n' >>$conf_dir/$site_name.conf;
  150. echo -e '<Directory'"$web_dir"' >' >>$conf_dir/$site_name.conf;
  151. echo -e 'Options -Indexes +FollowSymLinks +MultiViews' >>$conf_dir/$site_name.conf;
  152. echo -e 'AllowOverride All' >>$conf_dir/$site_name.conf;
  153. echo -e 'Require all granted' >>$conf_dir/$site_name.conf;
  154. echo -e '</Directory>' >>$conf_dir/$site_name.conf;
  155. echo -e '\n' >>$conf_dir/$site_name.conf;
  156. echo -e 'ErrorLog ${APACHE_LOG_DIR}/error.log' >>$conf_dir/$site_name.conf;
  157. echo -e 'CustomLog ${APACHE_LOG_DIR}/access.log combined' >>$conf_dir/$site_name.conf;
  158. echo -e '</VirtualHost>' >>$conf_dir/$site_name.conf;
  159. ln -s $conf_dir/$site_name.conf $sites_apache/$site_name.conf
  160. fi
  161. ## nginx config
  162. if [[ $opt != "apache" ]] || [[ "$opt" == "nginx" ]]; then
  163. echo >$conf_dir/$site_name.conf;
  164. echo -e 'server { listen 0.0.0.0:'"$site_port"';' >>$conf_dir/$site_name.conf;
  165. echo -e 'server_name '"$site_name"';' >>$conf_dir/$site_name.conf;
  166. echo -e '\n' >>$conf_dir/$site_name.conf;
  167. echo -e 'location /.well-known/acme-challenge {' >>$conf_dir/$site_name.conf;
  168. echo -e ' allow all;' >>$conf_dir/$site_name.conf;
  169. echo -e ' autoindex off;' >>$conf_dir/$site_name.conf;
  170. echo -e ' default_type "text/plain";' >>$conf_dir/$site_name.conf;
  171. echo -e ' root '"$web_dir"';' >>$conf_dir/$site_name.conf;
  172. echo -e '}' >>$conf_dir/$site_name.conf;
  173. echo -e 'location = /.well-known {' >>$conf_dir/$site_name.conf;
  174. echo -e ' return 404;' >>$conf_dir/$site_name.conf;
  175. echo -e '}' >>$conf_dir/$site_name.conf;
  176. echo -e 'error_page 404 /404.html;' >>$conf_dir/$site_name.conf;
  177. echo -e 'error_page 500 502 503 504 /50x.html;' >>$conf_dir/$site_name.conf;
  178. echo -e '\n' >>$conf_dir/$site_name.conf;
  179. echo -e 'error_log /var/log/nginx/err-certbot.log;' >>$conf_dir/$site_name.conf;
  180. echo -e 'access_log /var/log/nginx/access-certbot.log;' >>$conf_dir/$site_name.conf;
  181. echo -e '}' >>$conf_dir/$site_name.conf;
  182. ln -s $conf_dir/$site_name.conf $sites_nginx/$site_name.conf
  183. fi
  184. }
  185. ##--@F create configs
  186. function pHelp(){
  187. echo "$sname:$version"
  188. echo "please input pameters: avto4certbot.sh --create [apache & nginx]| --update [apache & nginx] | --flist [apache & nginx]";
  189. echo "avto4certbot.sh --create; create new certificate or --create [apache & nginx]; create new certificate "
  190. echo "avto4certbot.sh --update; update certificates or --update [apache & nginx]; update [apache & nginx];"
  191. echo "avto4certbot.sh --flist; update certificates from ssl or --flist [apache & nginx]; rescan list certificates;"
  192. echo "avto4certbot.sh --help; this help"
  193. echo "* examples:"
  194. echo " avtocertbot.sh --update apache"
  195. echo " or"
  196. echo " avtocertbot.sh --update nginx"
  197. }
  198. if [ "$opt" != "" ]; then
  199. getInfo;
  200. checkDep;
  201. execTask;
  202. else
  203. pHelp;
  204. fi
  205. exit