avto4certbot.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #!/bin/bash -x
  2. #
  3. # author: Koshuba V.O.
  4. # license: GPL 2.0
  5. # create 2022
  6. #
  7. version="0.5.7";
  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. # - for proxy mode
  31. sw_proxy=$3;
  32. #--@F Get info area
  33. function getInfo() {
  34. ## test - null values
  35. if [ $tmp_dir == "" ]; then
  36. tmp_dir="/tmp";
  37. fi
  38. web_dir="$tmp_dir/www"
  39. conf_dir="$tmp_dir/conf"
  40. if [ $log_file == "" ]; then
  41. log_file="/var/log/syslog";
  42. fi
  43. if [ $sites_nginx == "" ]; then
  44. available_nginx="/etc/nginx/sites-available";
  45. fi
  46. if [ $sites_apache == "" ]; then
  47. available_apache="/etc/apache2/sites-available";
  48. fi
  49. if [ $sites_nginx == "" ]; then
  50. sites_nginx="/etc/nginx/sites-enabled";
  51. fi
  52. if [ $sites_apache == "" ]; then
  53. sites_apache="/etc/apache2/sites-enabled";
  54. if [ "$(apachectl -M|grep rewrite|wc -m)" == "0" ]; then
  55. a2enmod rewrite
  56. fi
  57. fi
  58. ## apache2 mode: prefork or worker (multi-instance)
  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. ## create info active config sites
  82. if [[ "$opt" != "" ]] && [[ $opt != "nginx" ]] && [[ "$opt" == "apache" ]]; then
  83. ls -F -n $sites_apache/*|awk '{print$9":"$11}' >$tmp_dir/active_sites.inf
  84. get_tools[${#get_tools[@]}]="apache2";
  85. web_service="$apache2_service";
  86. fi
  87. if [[ "$opt" != "" ]] && [[ $opt != "apache" ]] && [[ "$opt" == "nginx" ]]; then
  88. ls -F -n $sites_nginx/*|awk '{print$9":"$11}' >$tmp_dir/active_sites.inf
  89. get_tools[${#get_tools[@]}]="nginx";
  90. web_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 0
  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|sed 's/:/ /g'|awk '{print$1}') );
  122. for ((xd=0; xd != ${#active_sites[@]}; xd++)); do
  123. if [[ "$opt" != "" ]] && [[ $opt != "nginx" ]] && [[ "$opt" == "apache" ]]; then
  124. if [ -f ${active_sites[$xd]} ]; then
  125. rm ${active_sites[$xd]}
  126. fi
  127. fi
  128. if [[ "$opt" != "" ]] && [[ $opt != "apache" ]] && [[ "$opt" == "nginx" ]]; then
  129. if [ -f ${active_sites[$xd]} ]; then
  130. rm ${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. get_enable=$(echo -e ${active_sites[$xd]}|sed 's/:/ /g'|awk '{print$1}');
  149. get_available=$(echo -e ${active_sites[$xd]}|sed 's/:/ /g'|awk '{print$2}');
  150. if [ ! -f $get_enable ]; then
  151. ln -s $get_available $get_enable;
  152. fi
  153. fi
  154. if [[ "$opt" != "" ]] && [[ $opt != "apache" ]] && [[ "$opt" == "nginx" ]]; then
  155. get_enable=$(echo -e ${active_sites[$xd]}|sed 's/:/ /g'|awk '{print$1}');
  156. get_available=$(echo -e ${active_sites[$xd]}|sed 's/:/ /g'|awk '{print$2}');
  157. if [ ! -f $get_enable ]; then
  158. ln -s $get_available $get_enable;
  159. fi
  160. fi
  161. done
  162. fi
  163. }
  164. ##--@F make all errors
  165. function makeErr() {
  166. for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
  167. do
  168. echo "$rdate $sname: ${reports[$rpt_index]}">>$log_file;
  169. echo "${reports[$rpt_index]}";
  170. done
  171. exit 0;
  172. }
  173. function createCert() {
  174. #
  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. site_owner="${site_data[1]}";
  179. certbot -m "$site_owner" certonly --webroot --webroot-path $web_dir -d $site_name
  180. sleep 2;
  181. done
  182. }
  183. ##--@F exec task
  184. function scanSSL(){
  185. ## if event - yes
  186. event_sw=0;
  187. rdate=$(date +%Y-%m-%d);
  188. rtime=$(date +%H:%M);
  189. for ((xd=0; xd != ${#domains[@]}; xd++)); do
  190. local site_data=( $(echo -e ${domains[$xd]}|sed 's/ /\n /g') );
  191. site_name="${site_data[0]}";
  192. if [ -d $path_cert/$site_name ]; then
  193. keydate=$(ls -l --time-style=long-iso $path_cert/$site_name/cert.pem |awk {'print$6'});
  194. keytime=$(ls -l --time-style=long-iso $path_cert/$site_name/cert.pem |awk {'print$7'});
  195. if [ ! -f $path_ssl/certs/$site_name.pem ]; then
  196. ((event_sw++));
  197. if [ -f $path_ssl/private/$site_name.pem ]; then
  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_file;
  204. fi
  205. fi
  206. if [[ -f $path_ssl/private/$site_name.pem ]] && [[ "$keydate" = "$rdate" ]] && [[ "$keytime" = "$rtime" ]]; then
  207. ((event_sw++));
  208. cp -f $path_ssl/private/$site_name.pem $path_ssl/certs/$site_name.pem
  209. cd $path_ssl/certs
  210. chmod 600 $site_name.pem
  211. ln -sf $site_name.pem `openssl x509 -noout -hash < $site_name.pem`.0
  212. cd $path_ssl
  213. echo "$(date) - $sname: update cert for $site_name">> $log_file;
  214. fi
  215. fi
  216. done
  217. if [ $event_sw != 0 ];then
  218. flistCerts;
  219. fi
  220. }
  221. ##--@F create from ssl
  222. function flistCerts(){
  223. echo>/etc/ssl/crt-list.txt
  224. for ((xd=0; xd != ${#domains[@]}; xd++)); do
  225. local site_data=( $(echo -e ${domains[$xd]}|sed 's/ /\n /g') );
  226. site_name="${site_data[0]}";
  227. if [ -d $path_cert/$site_name ]; then
  228. cat $path_cert/$site_name/privkey.pem > $path_ssl/private/privkey_$site_name.pem;
  229. chmod 0640 $path_ssl/private/privkey_$site_name.pem;
  230. cat $path_cert/$site_name/fullchain.pem > $path_ssl/private/fullchain_$site_name.pem;
  231. cat $path_cert/$site_name/fullchain.pem > $path_ssl/private/$site_name.pem;
  232. cat $path_cert/$site_name/privkey.pem >> $path_ssl/private/$site_name.pem;
  233. echo "$path_ssl/$site_name.pem">>/etc/ssl/crt-list.txt
  234. cp -f $path_ssl/private/$site_name.pem $path_ssl/certs/$site_name.pem
  235. cd $path_ssl/certs
  236. chmod 600 $site_name.pem
  237. ln -sf $site_name.pem `openssl x509 -noout -hash < $site_name.pem`.0
  238. cd $path_ssl
  239. fi
  240. done
  241. }
  242. ##--@F create configs
  243. function createConf(){
  244. for ((xd=0; xd != ${#domains[@]}; xd++)); do
  245. local site_data=( $(echo -e ${domains[$xd]}|sed 's/ /\n /g') );
  246. site_name="${site_data[0]}";
  247. site_owner="${site_data[1]}";
  248. site_port="${site_data[2]}";
  249. ## apache2 config
  250. if [[ "$opt" != "" ]] && [[ $opt != "nginx" ]] && [[ "$opt" == "apache" ]]; then
  251. ## добавить проверку режима apache2 и путь для активации конфигурации
  252. echo >$conf_dir/$site_name.conf;
  253. echo -e '<VirtualHost *:'"$site_port"'>' >>$conf_dir/$site_name.conf;
  254. echo -e ' ServerName '"$site_name"'' >>$conf_dir/$site_name.conf;
  255. echo -e ' ServerAlias '"$site_name"'' >>$conf_dir/$site_name.conf;
  256. echo -e ' DocumentRoot '"$web_dir"'' >>$conf_dir/$site_name.conf;
  257. echo -e ''>>$conf_dir/$site_name.conf;
  258. echo -e ' <Directory '"$web_dir"'>' >>$conf_dir/$site_name.conf;
  259. echo -e ' RewriteEngine On'>>$conf_dir/$site_name.conf;
  260. echo -e ' RewriteCond %{REQUEST_URI} !^/\.well\-known/acme\-challenge/'>>$conf_dir/$site_name.conf;
  261. echo -e ' Options -Indexes +FollowSymLinks +MultiViews' >>$conf_dir/$site_name.conf;
  262. echo -e ' AllowOverride All' >>$conf_dir/$site_name.conf;
  263. echo -e ' Require all granted' >>$conf_dir/$site_name.conf;
  264. echo -e ' </Directory>\n' >>$conf_dir/$site_name.conf;
  265. echo -e ' ErrorLog ${APACHE_LOG_DIR}/error.log' >>$conf_dir/$site_name.conf;
  266. echo -e ' CustomLog ${APACHE_LOG_DIR}/access.log combined' >>$conf_dir/$site_name.conf;
  267. echo -e '</VirtualHost>' >>$conf_dir/$site_name.conf;
  268. if [ ! -f $sites_apache/$site_name.conf ]; then
  269. ln -s $conf_dir/$site_name.conf $sites_apache/$site_name.conf
  270. fi
  271. fi
  272. ## nginx config
  273. if [[ "$opt" != "" ]] && [[ $opt != "apache" ]] && [[ "$opt" == "nginx" ]]; then
  274. echo >$conf_dir/$site_name.conf;
  275. echo -e 'server { listen 0.0.0.0:'"$site_port"';' >>$conf_dir/$site_name.conf;
  276. echo -e ' server_name '"$site_name"';' >>$conf_dir/$site_name.conf;
  277. echo -e ' location /.well-known/acme-challenge {' >>$conf_dir/$site_name.conf;
  278. echo -e ' allow all;' >>$conf_dir/$site_name.conf;
  279. echo -e ' autoindex off;' >>$conf_dir/$site_name.conf;
  280. echo -e ' default_type "text/plain";' >>$conf_dir/$site_name.conf;
  281. echo -e ' root '"$web_dir"';' >>$conf_dir/$site_name.conf;
  282. echo -e ' }' >>$conf_dir/$site_name.conf;
  283. echo -e ' location = /.well-known {' >>$conf_dir/$site_name.conf;
  284. echo -e ' return 404;' >>$conf_dir/$site_name.conf;
  285. echo -e ' }' >>$conf_dir/$site_name.conf;
  286. echo -e ' error_page 404 /404.html;' >>$conf_dir/$site_name.conf;
  287. echo -e ' error_page 500 502 503 504 /50x.html;\n' >>$conf_dir/$site_name.conf;
  288. echo -e ' error_log /var/log/nginx/err-certbot.log;' >>$conf_dir/$site_name.conf;
  289. echo -e ' access_log /var/log/nginx/access-certbot.log;' >>$conf_dir/$site_name.conf;
  290. echo -e '}' >>$conf_dir/$site_name.conf;
  291. if [ ! -f $sites_nginx/$site_name.conf ]; then
  292. ln -s $conf_dir/$site_name.conf $sites_nginx/$site_name.conf
  293. fi
  294. fi
  295. done
  296. }
  297. ##--@F restart services
  298. function updateScs(){
  299. if [[ "${services[@]}" != "" ]] && [[ "${#services[@]}" != "0" ]]; then
  300. for ((scn=0; scn != ${#services[@]}; scn++)); do
  301. systemctl restart ${services[$scn]};
  302. done
  303. fi
  304. }
  305. ##--@F help
  306. function pHelp(){
  307. echo "$sname:$version"
  308. echo "please input pameters: avto4certbot.sh --create [apache & nginx && proxy]| --update [apache & nginx] | --flist [apache & nginx]";
  309. echo "avto4certbot.sh --create; create new certificate or --create [apache & nginx && proxy]; create new certificate "
  310. echo "avto4certbot.sh --update; update certificates or --update [apache & nginx && proxy]; update [apache & nginx];"
  311. echo "avto4certbot.sh --flist; update certificates from ssl or --flist [apache & nginx && proxy]; rescan list certificates;"
  312. echo "avto4certbot.sh --help; this help"
  313. echo "* examples:"
  314. echo " avtocertbot.sh --update apache"
  315. echo " or"
  316. echo " avtocertbot.sh --update nginx"
  317. echo " or"
  318. echo " avtocertbot.sh --update apache proxy"
  319. }
  320. case "$cmd" in
  321. ## create cert
  322. "--create" | "--create" )
  323. if [ "$opt" != "" ]; then
  324. getInfo;
  325. checkDep;
  326. event_key="1";
  327. if [ "$sw_proxy" == "proxy" ]; then
  328. if [[ "$http_proxy" != "" ]] && [[ "$(systemctl list-units|grep "$http_proxy"|wc -m)" != "0" ]]; then
  329. systemctl stop $http_proxy
  330. createConf;
  331. systemctl start $web_service;
  332. sleep 2;
  333. createCert;
  334. scanSSL;
  335. event_key="0";
  336. systemctl stop $web_service;
  337. swSites;
  338. updateScs;
  339. systemctl start $http_proxy
  340. else
  341. reports=()
  342. reports[${#reports[@]}]="Sorry, there are not found proxy: $http_proxy"
  343. makeErr
  344. exit
  345. fi
  346. else
  347. systemctl stop $web_service;
  348. swSites;
  349. createConf;
  350. systemctl start $web_service;
  351. sleep 2;
  352. createCert;
  353. scanSSL;
  354. event_key="0";
  355. systemctl stop $web_service;
  356. swSites;
  357. systemctl start $web_service;
  358. updateScs;
  359. fi
  360. else
  361. pHelp;
  362. fi
  363. ;;
  364. ## update cert
  365. "--update" | "--update" )
  366. if [ "$opt" != "" ]; then
  367. getInfo;
  368. checkDep;
  369. event_key="1";
  370. if [ "$sw_proxy" == "proxy" ]; then
  371. if [[ "$http_proxy" != "" ]] && [[ "$(systemctl list-units|grep "$http_proxy"|wc -m)" != "0" ]]; then
  372. systemctl stop $http_proxy
  373. createConf;
  374. systemctl start $web_service;
  375. sleep 2;
  376. certbot -n renew;
  377. scanSSL;
  378. event_key="0";
  379. systemctl stop $web_service;
  380. swSites;
  381. updateScs;
  382. systemctl start $http_proxy
  383. else
  384. reports=()
  385. reports[${#reports[@]}]="Sorry, there are not found proxy: $http_proxy"
  386. makeErr
  387. exit
  388. fi
  389. else
  390. systemctl stop $web_service;
  391. swSites;
  392. createConf;
  393. systemctl start $web_service;
  394. sleep 2;
  395. certbot -n renew;
  396. scanSSL;
  397. event_key="0";
  398. systemctl stop $web_service;
  399. swSites;
  400. systemctl start $web_service;
  401. updateScs;
  402. fi
  403. else
  404. pHelp;
  405. fi
  406. ;;
  407. ## create cert
  408. "--flist" | "--flist" )
  409. if [ "$opt" != "" ]; then
  410. getInfo;
  411. checkDep;
  412. if [ "$sw_proxy" == "proxy" ]; then
  413. if [[ "$http_proxy" != "" ]] && [[ "$(systemctl list-units|grep "$http_proxy"|wc -m)" != "0" ]]; then
  414. flistCerts;
  415. systemctl restart $http_proxy
  416. updateScs;
  417. else
  418. reports=()
  419. reports[${#reports[@]}]="Sorry, there are not found proxy: $http_proxy"
  420. makeErr
  421. exit
  422. fi
  423. else
  424. flistCerts;
  425. systemctl restart $web_service;
  426. updateScs;
  427. fi
  428. else
  429. pHelp;
  430. fi
  431. ;;
  432. ## start defaults
  433. * )
  434. pHelp;
  435. ;;
  436. esac
  437. exit