avto4certbot.sh 11 KB

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