avto4certbot.sh 9.4 KB

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