avto4certbot.sh 11 KB

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