avto4certbot.sh 10 KB

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