avto4certbot.sh 11 KB

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