avto4certbot.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. ##--@S static values
  13. # depends
  14. pkgdep=("curl" "certbot" "letsencrypt") # packages
  15. get_tools=("curl" "certbot" "letsencrypt")
  16. # - options
  17. cmd=$1;
  18. # - for LAMP server
  19. opt=$2;
  20. #--@F Get info area
  21. function getInfo() {
  22. if [ $opt = "apache" ];then
  23. find $sites_apache/* -maxdepth 0 -type l -printf '%f\n' 2>$tmp_dir/active_sites.inf;
  24. fi
  25. if [ $opt = "nginx" ];then
  26. find $sites_nginx/* -maxdepth 0 -type l -printf '%f\n' 2>$tmp_dir/active_sites.inf;
  27. fi
  28. }
  29. #--@F Check the program dependency
  30. function checkDep() {
  31. # - msg debug
  32. echo "check depends..."
  33. if [ ! "$lang" ]; then
  34. lang="C.UTF-8"
  35. fi
  36. for ((itools = 0; itools != ${#get_tools[@]}; itools++)); do
  37. checktool=$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')
  38. if [[ $checktool = "" ]]; then
  39. sudo apt install ${pkgdep[$itools]}
  40. fi
  41. checktool=$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')
  42. if [[ $checktool != "" ]]; then
  43. eval get_${get_tools[$itools]}=$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')
  44. list_tools[${#list_tools[@]}]="$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')"
  45. else
  46. ## lang messages if yes then lang else us...
  47. reports=()
  48. reports[${#reports[@]}]="Sorry, there are no required packages to work, please install:${pkgdep[@]}"
  49. makeErr
  50. exit
  51. fi
  52. done
  53. }
  54. ##--@F make all errors
  55. function makeErr() {
  56. for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
  57. do
  58. echo "$rdate $sname: ${reports[$rpt_index]}">>$log;
  59. echo "${reports[$rpt_index]}";
  60. done
  61. exit 0;
  62. }
  63. function createCert() {
  64. #
  65. for ((dmn=0; dmn != ${#domains[@]}; dmn++))
  66. do
  67. eval local dreg="(" $(echo -e ${domains[$dmn]}) ")";
  68. if [ "$cmd" == "--create" ];
  69. then
  70. certbot -m "${dreg[1]}";
  71. else
  72. certbot --update-registration -m "${dreg[1]}";
  73. fi
  74. ##
  75. ## example manual: certbot certonly --webroot --webroot-path /tmp/letsencrypt -d mydomen.ru
  76. certbot certonly --webroot --webroot-path $www_root -d ${dreg[0]}
  77. done
  78. }
  79. function renew() {
  80. certbot renew;
  81. valtrue=0;
  82. rdate=$(date +%Y-%m-%d);
  83. rtime=$(date +%H:%M);
  84. for ((dmn=0; dmn != ${#domains[@]}; dmn++))
  85. do
  86. eval local dreg="(" $(echo -e ${domains[$dmn]}) ")";
  87. keydate=$(ls -l --time-style=long-iso $path_cert/${dreg[0]}/cert.pem |awk {'print$6'});
  88. keytime=$(ls -l --time-style=long-iso $path_cert/${dreg[0]}/cert.pem |awk {'print$7'});
  89. if [[ "$keydate" = "$rdate" ]] && [[ "$keytime" = "$rtime" ]]; then
  90. ((valtrue++));
  91. if [ -d $path_cert/${dreg[0]} ]; then
  92. cat $path_cert/${dreg[0]}/privkey.pem > $path_ssl/private/privkey_${dreg[0]}.pem;
  93. cat $path_cert/${dreg[0]}/fullchain.pem > $path_ssl/private/fullchain_${dreg[0]}.pem;
  94. cat $path_cert/${dreg[0]}/fullchain.pem > $path_ssl/private/${dreg[0]}.pem;
  95. cat $path_cert/${dreg[0]}/privkey.pem >> $path_ssl/private/${dreg[0]}.pem;
  96. #
  97. cp -f $path_ssl/private/${dreg[0]}.pem $path_ssl/certs/${dreg[0]}.pem
  98. cd $path_ssl/certs
  99. chmod 600 ${dreg[0]}.pem
  100. ln -sf ${dreg[0]}.pem `openssl x509 -noout -hash < ${dreg[0]}.pem`.0
  101. cd $path_ssl
  102. echo "$(date) - $sname: update cert for ${domains[$dmn]}">> $log;
  103. fi
  104. fi
  105. done
  106. if [ $valtrue != 0 ];then
  107. :>/etc/ssl/crt-list.txt
  108. for ((icrt=0; icrt != ${#domains[@]}; icrt++))
  109. do
  110. echo "$path_ssl/${domains[$icrt]}.pem">>/etc/ssl/crt-list.txt
  111. done
  112. fi
  113. }
  114. function toSSL() {
  115. if [ -d $path_cert ];
  116. then
  117. for ((dmn=0; dmn != ${#domains[@]}; dmn++))
  118. do
  119. eval local dreg="(" $(echo -e ${domains[$dmn]}) ")";
  120. ((valtrue++));
  121. if [ -d $path_cert/${dreg[0]} ]; then
  122. cat $path_cert/${dreg[0]}/privkey.pem > $path_ssl/private/privkey_${dreg[0]}.pem;
  123. cat $path_cert/${dreg[0]}/fullchain.pem > $path_ssl/private/fullchain_${dreg[0]}.pem;
  124. cat $path_cert/${dreg[0]}/fullchain.pem > $path_ssl/private/${dreg[0]}.pem;
  125. cat $path_cert/${dreg[0]}/privkey.pem >> $path_ssl/private/${dreg[0]}.pem;
  126. #
  127. cp -f $path_ssl/private/${dreg[0]}.pem $path_ssl/certs/${dreg[0]}.pem
  128. cd $path_ssl/certs
  129. chmod 600 ${dreg[0]}.pem
  130. ln -sf ${dreg[0]}.pem `openssl x509 -noout -hash < ${dreg[0]}.pem`.0
  131. cd $path_ssl
  132. echo "$(date) - $sname: update certlist for ${domains[$dmn]}">> $log;
  133. fi
  134. done
  135. if [ $valtrue != 0 ]; then
  136. echo >/etc/ssl/crt-list.txt
  137. for ((icrt=0; icrt != ${#domains[@]}; icrt++))
  138. do
  139. eval local dcrt="(" $(echo -e ${domains[$icrt]}) ")";
  140. echo "$path_ssl/private/${dcrt[0]}.pem">>/etc/ssl/crt-list.txt
  141. done
  142. fi
  143. else
  144. echo "Ошибка - отсутствует $path_cert!"
  145. echo "$(date) - $sname: Ошибка - отсутствует $path_cert!">> $log;
  146. fi
  147. }
  148. function downSite(){
  149. sudo systemctl stop nginx.service;
  150. eval list_www="(" $(find $nginx_enable/* -maxdepth 0 -type l -printf '%f\n' 2>/dev/null) ")";
  151. if [ ${#list_www[@]} != 0 ]; then
  152. for ((dwx=0; dwx != ${#list_www[@]}; dwx++))
  153. do
  154. rm $nginx_enable/${list_www[dwx]};
  155. done
  156. fi
  157. }
  158. function upSite(){
  159. sudo systemctl stop nginx.service;
  160. eval cert_bot="(" $(find $nginx_enable/* -maxdepth 0 -type l -printf '%f\n' 2>/dev/null) ")";
  161. for ((cr=0; cr != ${#cert_bot[@]}; cr++))
  162. do
  163. rm $nginx_enable/${cert_bot[cr]};
  164. done
  165. for ((dnm=0; dnm != ${#domains[@]}; dnm++))
  166. do
  167. eval local dcert="(" $(echo -e ${domains[$dnm]}) ")";
  168. sitename="${dcert[0]}";
  169. siteport="${dcert[2]}";
  170. createConf;
  171. done
  172. sudo systemctl start nginx.service;
  173. }
  174. function restoreSite() {
  175. sudo systemctl stop nginx.service;
  176. eval list_www="(" $(find $nginx_enable/* -maxdepth 0 -type l -printf '%f\n' 2>/dev/null) ")";
  177. if [ ${#list_www[@]} != 0 ]; then
  178. for ((dwx=0; dwx != ${#list_www[@]}; dwx++))
  179. do
  180. rm $nginx_enable/${list_www[dwx]};
  181. done
  182. fi
  183. for ((dwx=0; dwx != ${#enable_www[@]}; dwx++))
  184. do
  185. ln -s $nginx_available/${enable_www[dwx]} $nginx_enable/${enable_www[dwx]};
  186. done
  187. sudo systemctl start nginx.service;
  188. }
  189. function createConf(){
  190. if [ ! -d $path_tmp ];
  191. then
  192. mkdir -p $path_tmp;
  193. fi
  194. if [ ! -d $www_root ];
  195. then
  196. mkdir -p $www_root/.well-known/acme-challenge;
  197. chown -R www-data:www-data $www_root;
  198. fi
  199. echo >$path_tmp/$sitename.conf;
  200. echo -e 'server { listen 0.0.0.0:'"$siteport"';' >>$path_tmp/$sitename.conf;
  201. echo -e '\n' >>$path_tmp/$sitename.conf;
  202. echo -e 'server_name '"$sitename"';' >>$path_tmp/$sitename.conf;
  203. echo -e '\n' >>$path_tmp/$sitename.conf;
  204. echo -e 'location /.well-known/acme-challenge {' >>$path_tmp/$sitename.conf;
  205. echo -e ' allow all;' >>$path_tmp/$sitename.conf;
  206. echo -e ' autoindex off;' >>$path_tmp/$sitename.conf;
  207. echo -e ' default_type "text/plain";' >>$path_tmp/$sitename.conf;
  208. echo -e ' root '"$www_root"';' >>$path_tmp/$sitename.conf;
  209. echo -e '}' >>$path_tmp/$sitename.conf;
  210. echo -e '\n' >>$path_tmp/$sitename.conf;
  211. echo -e 'location = /.well-known {' >>$path_tmp/$sitename.conf;
  212. echo -e ' return 404;' >>$path_tmp/$sitename.conf;
  213. echo -e '}' >>$path_tmp/$sitename.conf;
  214. echo -e '\n' >>$path_tmp/$sitename.conf;
  215. echo -e 'error_page 404 /404.html;' >>$path_tmp/$sitename.conf;
  216. echo -e 'error_page 500 502 503 504 /50x.html;' >>$path_tmp/$sitename.conf;
  217. echo -e '\n' >>$path_tmp/$sitename.conf;
  218. echo -e 'error_log /var/log/nginx/err-certbot.log;' >>$path_tmp/$sitename.conf;
  219. echo -e 'access_log /var/log/nginx/access-certbot.log;' >>$path_tmp/$sitename.conf;
  220. echo -e '}' >>$path_tmp/$sitename.conf;
  221. ln -s $path_tmp/$sitename.conf $nginx_enable/$sitename.conf
  222. }
  223. function restartService(){
  224. for ((scn=0; scn != ${#set_service[@]}; scn++))
  225. do
  226. /etc/init.d/${set_service[$scn]} restart;
  227. # systemctl restart ${set_services[$scn]};
  228. done
  229. }
  230. case "$cmd" in
  231. ## create cert
  232. "--create" | "--create" )
  233. downSite;
  234. upSite;
  235. createCert;
  236. toSSL;
  237. downSite;
  238. if [ "$opt" == "srv" ]; then
  239. restartService;
  240. else
  241. restoreSite;
  242. fi
  243. ;;
  244. ## update cert
  245. "--update" | "--update" )
  246. downSite;
  247. upSite;
  248. renew;
  249. downSite;
  250. if [[ "$opt" == "srv" ]] && [[ $valtrue != 0 ]]; then
  251. restartService;
  252. else
  253. restoreSite;
  254. fi
  255. ;;
  256. ## update cert
  257. "--test" | "--test" )
  258. getInfo;
  259. ;;
  260. ## update cert force
  261. "--flist" | "--flist" )
  262. toSSL;
  263. if [ "$opt" == "srv" ]; then
  264. restartService;
  265. fi
  266. ;;
  267. ## start defaults
  268. * )
  269. checkDep;
  270. echo "$sname:$version"
  271. echo "please input pameters: avto4certbot.sh --create [apache & nginx]| --update [apache & nginx] | --flist [apache & nginx]";
  272. echo "avto4certbot.sh --create; create new certificate or --create [apache & nginx]; create new certificate "
  273. echo "avto4certbot.sh --update; update certificates or --update [apache & nginx]; update [apache & nginx];"
  274. echo "avto4certbot.sh --flist; update certificates from ssl or --flist [apache & nginx]; rescan list certificates;"
  275. ;;
  276. esac
  277. exit