avto4certbot.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. # service LAMP
  13. web_service="";
  14. # new certificate or renewal event
  15. event_sw=0;
  16. # event begin or end the work script
  17. event_key="1";
  18. # message from errors
  19. reports=();
  20. # work_sites
  21. active_sites=();
  22. ##--@S static values
  23. # depends
  24. pkgdep=("curl" "certbot" "letsencrypt") # packages
  25. get_tools=("curl" "certbot" "letsencrypt")
  26. # - options
  27. cmd=$1;
  28. # - for LAMP server
  29. opt=$2;
  30. #--@F Get info area
  31. function getInfo() {
  32. ## test - null values
  33. if [ $tmp_dir == "" ]; then
  34. tmp_dir="/tmp";
  35. fi
  36. web_dir="$tmp_dir/www"
  37. conf_dir="$tmp_dir/conf"
  38. if [ $log_file == "" ]; then
  39. log_file="/var/log/syslog";
  40. fi
  41. if [ $sites_nginx == "" ]; then
  42. available_nginx="/etc/nginx/sites-available";
  43. fi
  44. if [ $sites_apache == "" ]; then
  45. available_apache="/etc/apache2/sites-available";
  46. fi
  47. if [ $sites_nginx == "" ]; then
  48. sites_nginx="/etc/nginx/sites-enabled";
  49. fi
  50. if [ $sites_apache == "" ]; then
  51. sites_apache="/etc/apache2/sites-enabled";
  52. if [ "$(apachectl -M|grep rewrite|wc -m)" == "0" ]; then
  53. a2enmod rewrite
  54. fi
  55. fi
  56. ## apache2 mode: prefork or worker (multi-instance)
  57. if [ $apache2_service == "" ]; then
  58. apache2_service="apache2";
  59. fi
  60. if [ $path_ssl == "" ]; then
  61. path_ssl="/etc/ssl";
  62. fi
  63. if [ $path_cert == "" ]; then
  64. path_cert="/etc/letsencrypt/live";
  65. fi
  66. ## create temp directory
  67. if [ ! -d $tmp_dir ]; then
  68. mkdir -p $tmp_dir;
  69. fi
  70. ## create web directory
  71. if [ ! -d "$web_dir/.well-known/acme-challenge" ]; then
  72. mkdir -p $web_dir/.well-known/acme-challenge;
  73. chown -R www-data:www-data $web_dir;
  74. fi
  75. ## create conf directory
  76. if [ ! -d $conf_dir ]; then
  77. mkdir -p $conf_dir;
  78. fi
  79. ##
  80. if [[ "$opt" != "" ]] && [[ $opt != "nginx" ]] && [[ "$opt" == "apache" ]]; then
  81. find $sites_apache/* -maxdepth 0 -type l -printf '%f\n' >$tmp_dir/active_sites.inf 2>/dev/null;
  82. get_tools[${#get_tools[@]}]="apache2";
  83. web_service="$apache2_service";
  84. fi
  85. if [[ "$opt" != "" ]] && [[ $opt != "apache" ]] && [[ "$opt" == "nginx" ]]; then
  86. find $sites_nginx/* -maxdepth 0 -type l -printf '%f\n' >$tmp_dir/active_sites.inf 2>/dev/null;
  87. get_tools[${#get_tools[@]}]="nginx";
  88. web_service="nginx";
  89. fi
  90. }
  91. #--@F Check the program dependency
  92. function checkDep() {
  93. # - msg debug
  94. echo "check depends..."
  95. if [ ! "$lang" ]; then
  96. lang="C.UTF-8"
  97. fi
  98. for ((itools = 0; itools != ${#get_tools[@]}; itools++)); do
  99. checktool=$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')
  100. if [[ $checktool = "" ]]; then
  101. sudo apt install ${pkgdep[$itools]}
  102. fi
  103. checktool=$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')
  104. if [[ $checktool != "" ]]; then
  105. eval get_${get_tools[$itools]}=$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')
  106. list_tools[${#list_tools[@]}]="$(whereis -b ${get_tools[$itools]} | awk '/^'${get_tools[$itools]}':/{print $2}')"
  107. else
  108. ## lang messages if yes then lang else us...
  109. reports=()
  110. reports[${#reports[@]}]="Sorry, there are no required packages to work, please install:${pkgdep[@]}"
  111. makeErr
  112. exit
  113. fi
  114. done
  115. }
  116. function swSites(){
  117. ## clear active sites
  118. if [ "$event_key" = "1" ]; then
  119. active_sites=( $(cat $tmp_dir/active_sites.inf) );
  120. for ((xd=0; xd != ${#active_sites[@]}; xd++)); do
  121. if [[ "$opt" != "" ]] && [[ $opt != "nginx" ]] && [[ "$opt" == "apache" ]]; then
  122. if [ -f $sites_apache/${active_sites[$xd]} ]; then
  123. rm $sites_apache/${active_sites[$xd]}
  124. fi
  125. fi
  126. if [[ "$opt" != "" ]] && [[ $opt != "apache" ]] && [[ "$opt" == "nginx" ]]; then
  127. if [ -f $sites_nginx/${active_sites[$xd]} ]; then
  128. rm $sites_nginx/${active_sites[$xd]}
  129. fi
  130. fi
  131. done
  132. fi
  133. ## restore active sites
  134. if [ "$event_key" = "0" ]; then
  135. # clear tmp configs
  136. if [[ "$opt" != "" ]] && [[ $opt != "nginx" ]] && [[ "$opt" == "apache" ]]; then
  137. rm $sites_apache/*.conf
  138. fi
  139. if [[ "$opt" != "" ]] && [[ $opt != "apache" ]] && [[ "$opt" == "nginx" ]]; then
  140. rm $sites_nginx/*.conf
  141. fi
  142. # restore active links
  143. active_sites=( $(cat $tmp_dir/active_sites.inf) );
  144. for ((xd=0; xd != ${#active_sites[@]}; xd++)); do
  145. if [[ "$opt" != "" ]] && [[ $opt != "nginx" ]] && [[ "$opt" == "apache" ]]; then
  146. if [ ! -f $sites_apache/${active_sites[$xd]} ]; then
  147. ln -s $available_apache/${active_sites[$xd]} $sites_apache/${active_sites[$xd]}
  148. fi
  149. fi
  150. if [[ "$opt" != "" ]] && [[ $opt != "apache" ]] && [[ "$opt" == "nginx" ]]; then
  151. if [ ! -f $sites_nginx/${active_sites[$xd]} ]; then
  152. ln -s $available_nginx/${active_sites[$xd]} $sites_nginx/${active_sites[$xd]}
  153. fi
  154. fi
  155. done
  156. fi
  157. }
  158. ##--@F make all errors
  159. function makeErr() {
  160. for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
  161. do
  162. echo "$rdate $sname: ${reports[$rpt_index]}">>$log_file;
  163. echo "${reports[$rpt_index]}";
  164. done
  165. exit 0;
  166. }
  167. function createCert() {
  168. #
  169. for ((xd=0; xd != ${#domains[@]}; xd++)); do
  170. local site_data=( $(echo -e ${domains[$xd]}|sed 's/ /\n /g') );
  171. site_name="${site_data[0]}";
  172. site_owner="${site_data[1]}";
  173. #certbot register -m "$site_owner" -d $site_name
  174. #sleep 2;
  175. certbot -m "$site_owner" certonly --webroot --webroot-path $web_dir -d $site_name
  176. sleep 2;
  177. done
  178. }
  179. ##--@F exec task
  180. function scanSSL(){
  181. ## if event - yes
  182. event_sw=0;
  183. rdate=$(date +%Y-%m-%d);
  184. rtime=$(date +%H:%M);
  185. for ((xd=0; xd != ${#domains[@]}; xd++)); do
  186. local site_data=( $(echo -e ${domains[$xd]}|sed 's/ /\n /g') );
  187. site_name="${site_data[0]}";
  188. if [ -d $path_cert/$site_name ]; then
  189. keydate=$(ls -l --time-style=long-iso $path_cert/$site_name/cert.pem |awk {'print$6'});
  190. keytime=$(ls -l --time-style=long-iso $path_cert/$site_name/cert.pem |awk {'print$7'});
  191. if [[ "$keydate" = "$rdate" ]] && [[ "$keytime" = "$rtime" ]]; then
  192. ((event_sw++));
  193. cat $path_cert/$site_name/privkey.pem > $path_ssl/private/privkey_$site_name.pem;
  194. cat $path_cert/$site_name/fullchain.pem > $path_ssl/private/fullchain_$site_name.pem;
  195. cat $path_cert/$site_name/fullchain.pem > $path_ssl/private/$site_name.pem;
  196. cat $path_cert/$site_name/privkey.pem >> $path_ssl/private/$site_name.pem;
  197. #
  198. cp -f $path_ssl/private/$site_name.pem $path_ssl/certs/$site_name.pem
  199. cd $path_ssl/certs
  200. chmod 600 $site_name.pem
  201. ln -sf $site_name.pem `openssl x509 -noout -hash < $site_name.pem`.0
  202. cd $path_ssl
  203. echo "$(date) - $sname: update cert for $site_name">> $log;
  204. fi
  205. fi
  206. done
  207. if [ $event_sw != 0 ];then
  208. echo>/etc/ssl/crt-list.txt
  209. for ((xd=0; xd != ${#domains[@]}; xd++)); do
  210. local site_data=( $(echo -e ${domains[$xd]}|sed 's/ /\n /g') );
  211. echo "$path_ssl/${site_data[0]}.pem">>/etc/ssl/crt-list.txt
  212. done
  213. fi
  214. }
  215. ##--@F create configs
  216. function createConf(){
  217. for ((xd=0; xd != ${#domains[@]}; xd++)); do
  218. local site_data=( $(echo -e ${domains[$xd]}|sed 's/ /\n /g') );
  219. site_name="${site_data[0]}";
  220. site_owner="${site_data[1]}";
  221. site_port="${site_data[2]}";
  222. ## apache2 config
  223. if [[ "$opt" != "" ]] && [[ $opt != "nginx" ]] && [[ "$opt" == "apache" ]]; then
  224. ## добавить проверку режима apache2 и путь для активации конфигурации
  225. echo >$conf_dir/$site_name.conf;
  226. echo -e '<VirtualHost *:'"$site_port"'>' >>$conf_dir/$site_name.conf;
  227. echo -e ' ServerName '"$site_name"'' >>$conf_dir/$site_name.conf;
  228. echo -e ' ServerAlias '"$site_name"'' >>$conf_dir/$site_name.conf;
  229. echo -e ' DocumentRoot '"$web_dir"'' >>$conf_dir/$site_name.conf;
  230. echo -e ''>>$conf_dir/$site_name.conf;
  231. echo -e ' <Directory '"$web_dir"'>' >>$conf_dir/$site_name.conf;
  232. echo -e ' RewriteEngine On'>>$conf_dir/$site_name.conf;
  233. echo -e ' RewriteCond %{REQUEST_URI} !^/\.well\-known/acme\-challenge/'>>$conf_dir/$site_name.conf;
  234. echo -e ' Options -Indexes +FollowSymLinks +MultiViews' >>$conf_dir/$site_name.conf;
  235. echo -e ' AllowOverride All' >>$conf_dir/$site_name.conf;
  236. echo -e ' Require all granted' >>$conf_dir/$site_name.conf;
  237. echo -e ' </Directory>\n' >>$conf_dir/$site_name.conf;
  238. echo -e ' ErrorLog ${APACHE_LOG_DIR}/error.log' >>$conf_dir/$site_name.conf;
  239. echo -e ' CustomLog ${APACHE_LOG_DIR}/access.log combined' >>$conf_dir/$site_name.conf;
  240. echo -e '</VirtualHost>' >>$conf_dir/$site_name.conf;
  241. if [ ! -f $sites_apache/$site_name.conf ]; then
  242. ln -s $conf_dir/$site_name.conf $sites_apache/$site_name.conf
  243. fi
  244. fi
  245. ## nginx config
  246. if [[ "$opt" != "" ]] && [[ $opt != "apache" ]] && [[ "$opt" == "nginx" ]]; then
  247. echo >$conf_dir/$site_name.conf;
  248. echo -e 'server { listen 0.0.0.0:'"$site_port"';' >>$conf_dir/$site_name.conf;
  249. echo -e ' server_name '"$site_name"';' >>$conf_dir/$site_name.conf;
  250. echo -e ' location /.well-known/acme-challenge {' >>$conf_dir/$site_name.conf;
  251. echo -e ' allow all;' >>$conf_dir/$site_name.conf;
  252. echo -e ' autoindex off;' >>$conf_dir/$site_name.conf;
  253. echo -e ' default_type "text/plain";' >>$conf_dir/$site_name.conf;
  254. echo -e ' root '"$web_dir"';' >>$conf_dir/$site_name.conf;
  255. echo -e ' }' >>$conf_dir/$site_name.conf;
  256. echo -e ' location = /.well-known {' >>$conf_dir/$site_name.conf;
  257. echo -e ' return 404;' >>$conf_dir/$site_name.conf;
  258. echo -e ' }' >>$conf_dir/$site_name.conf;
  259. echo -e ' error_page 404 /404.html;' >>$conf_dir/$site_name.conf;
  260. echo -e ' error_page 500 502 503 504 /50x.html;\n' >>$conf_dir/$site_name.conf;
  261. echo -e ' error_log /var/log/nginx/err-certbot.log;' >>$conf_dir/$site_name.conf;
  262. echo -e ' access_log /var/log/nginx/access-certbot.log;' >>$conf_dir/$site_name.conf;
  263. echo -e '}' >>$conf_dir/$site_name.conf;
  264. if [ ! -f $sites_nginx/$site_name.conf ]; then
  265. ln -s $conf_dir/$site_name.conf $sites_nginx/$site_name.conf
  266. fi
  267. fi
  268. done
  269. }
  270. ##--@F restart services
  271. function updateScs(){
  272. if [[ "${services[@]}" != "" ]] && [[ "${#services[@]}" != "0" ]]; then
  273. for ((scn=0; scn != ${#services[@]}; scn++)); do
  274. systemctl restart ${services[$scn]};
  275. done
  276. fi
  277. }
  278. ##--@F help
  279. function pHelp(){
  280. echo "$sname:$version"
  281. echo "please input pameters: avto4certbot.sh --create [apache & nginx]| --update [apache & nginx] | --flist [apache & nginx]";
  282. echo "avto4certbot.sh --create; create new certificate or --create [apache & nginx]; create new certificate "
  283. echo "avto4certbot.sh --update; update certificates or --update [apache & nginx]; update [apache & nginx];"
  284. echo "avto4certbot.sh --flist; update certificates from ssl or --flist [apache & nginx]; rescan list certificates;"
  285. echo "avto4certbot.sh --help; this help"
  286. echo "* examples:"
  287. echo " avtocertbot.sh --update apache"
  288. echo " or"
  289. echo " avtocertbot.sh --update nginx"
  290. }
  291. case "$cmd" in
  292. ## create cert
  293. "--create" | "--create" )
  294. if [ "$opt" != "" ]; then
  295. getInfo;
  296. checkDep;
  297. event_key="1";
  298. systemctl stop $web_service;
  299. swSites;
  300. createConf;
  301. systemctl start $web_service;
  302. createCert;
  303. scanSSL;
  304. event_key="0";
  305. systemctl stop $web_service;
  306. swSites;
  307. systemctl start $web_service;
  308. updateScs;
  309. else
  310. pHelp;
  311. fi
  312. ;;
  313. ## update cert
  314. "--update" | "--update" )
  315. if [ "$opt" != "" ]; then
  316. getInfo;
  317. checkDep;
  318. event_key="1";
  319. systemctl stop $web_service;
  320. swSites;
  321. createConf;
  322. systemctl start $web_service;
  323. certbot -n renew;
  324. scanSSL;
  325. event_key="0";
  326. systemctl stop $web_service;
  327. swSites;
  328. systemctl start $web_service;
  329. updateScs;
  330. else
  331. pHelp;
  332. fi
  333. ;;
  334. ## create cert
  335. "--flist" | "--flist" )
  336. if [ "$opt" != "" ]; then
  337. getInfo;
  338. checkDep;
  339. scanSSL;
  340. updateScs;
  341. else
  342. pHelp;
  343. fi
  344. ;;
  345. ## start defaults
  346. * )
  347. pHelp;
  348. ;;
  349. esac
  350. exit