avto4certbot.sh 9.4 KB

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