domain.func 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. # Checking domain existance
  2. is_domain_new() {
  3. config_type="$1"
  4. dom=${2-domain}
  5. check_all=$(grep -w $dom $V_USERS/*/*.conf)
  6. if [ ! -z "$check_all" ]; then
  7. check_ownership=$(grep -w $dom $V_USERS/$user/*.conf)
  8. if [ ! -z "$check_ownership" ]; then
  9. check_type=$(grep -w $dom $V_USERS/$user/$config_type.conf)
  10. if [ ! -z "$check_type" ]; then
  11. echo "Error: $dom exist"
  12. log_event 'debug' "$E_EXISTS $V_EVENT"
  13. exit $E_EXISTS
  14. fi
  15. else
  16. echo "Error: $dom exist"
  17. log_event 'debug' "$E_EXISTS $V_EVENT"
  18. exit $E_EXISTS
  19. fi
  20. fi
  21. }
  22. is_domain_valid() {
  23. # Parsing domain values
  24. check_domain=$(grep -F "DOMAIN='$domain'" $V_USERS/$user/$1.conf)
  25. # Checking result
  26. if [ -z "$check_domain" ]; then
  27. echo "Error: domain not exist"
  28. log_event 'debug' "$E_NOTEXIST $V_EVENT"
  29. exit $E_NOTEXIST
  30. fi
  31. }
  32. is_domain_suspended() {
  33. config_type="$1"
  34. # Parsing domain values
  35. check_domain=$(grep "DOMAIN='$domain'" $V_USERS/$user/$config_type.conf|\
  36. grep "SUSPENDED='yes'")
  37. # Checking result
  38. if [ ! -z "$check_domain" ]; then
  39. echo "Error: domain suspended"
  40. log_event 'debug' "$E_SUSPENDED $V_EVENT"
  41. exit $E_SUSPENDED
  42. fi
  43. }
  44. is_domain_unsuspended() {
  45. config_type="$1"
  46. # Parsing domain values
  47. check_domain=$(grep "DOMAIN='$domain'" $V_USERS/$user/$config_type.conf|\
  48. grep "SUSPENDED='no'")
  49. # Checking result
  50. if [ ! -z "$check_domain" ]; then
  51. echo "Error: domain unsuspended"
  52. log_event 'debug' "$E_UNSUSPENDED $V_EVENT"
  53. exit $E_UNSUSPENDED
  54. fi
  55. }
  56. update_domain_zone() {
  57. # Definigng variables
  58. line=$(grep "DOMAIN='$domain'" $V_USERS/$user/dns.conf)
  59. fields='$RECORD\t$TTL\tIN\t$TYPE\t$VALUE'
  60. # Checking serial
  61. if [ -e $conf ]; then
  62. zn_serial=$(head $conf|grep 'SOA' -A1|tail -n 1|sed -e "s/ //g")
  63. s_date=$(echo ${zn_serial:0:8})
  64. c_date=$(date +'%Y%m%d')
  65. if [ "$s_date" == "$c_date" ]; then
  66. cur_value=$(echo ${zn_serial:8} )
  67. new_value=$(expr $cur_value + 1 )
  68. len_value=$(expr length $new_value)
  69. if [ 1 -eq "$len_value" ]; then
  70. new_value='0'$new_value
  71. fi
  72. serial="$c_date""$new_value"
  73. else
  74. serial="$(date +'%Y%m%d01')"
  75. fi
  76. else
  77. serial="$(date +'%Y%m%d01')"
  78. fi
  79. # Parsing dns domains conf
  80. eval $line
  81. # Converting SOA to ascii
  82. SOA=$(idn --quiet -a -t "$SOA")
  83. # Adding zone header
  84. echo "\$TTL $TTL
  85. @ IN SOA $SOA. root.$domain_idn. (
  86. $serial
  87. 7200
  88. 3600
  89. 1209600
  90. 180 )
  91. " > $conf
  92. # Adding zone records
  93. while read line ; do
  94. # Defining new delimeter
  95. IFS=$'\n'
  96. # Parsing key=value
  97. for key in $(echo $line|sed -e "s/' /'\n/g"); do
  98. eval ${key%%=*}="${key#*=}"
  99. done
  100. # Converting utf records to ascii
  101. RECORD=$(idn --quiet -a -t "$RECORD")
  102. VALUE=$(idn --quiet -a -t "$VALUE")
  103. eval echo -e "\"$fields\""|sed -e "s/%quote%/'/g" >> $conf
  104. done < $V_USERS/$user/dns/$domain
  105. }
  106. get_next_dns_record() {
  107. # Parsing config
  108. curr_str=$(grep "ID=" $V_USERS/$user/dns/$domain|cut -f 2 -d \'|\
  109. sort -n|tail -n1)
  110. # Print result
  111. echo "$((curr_str +1))"
  112. }
  113. is_dns_record_free() {
  114. # Checking record id
  115. check_id=$(grep "ID='$id'" $V_USERS/$user/dns/$domain)
  116. if [ ! -z "$check_id" ]; then
  117. echo "Error: ID exist"
  118. log_event 'debug' "$E_EXISTS $V_EVENT"
  119. exit $E_EXISTS
  120. fi
  121. }
  122. sort_dns_records() {
  123. # Defining conf
  124. conf="$V_USERS/$user/dns/$domain"
  125. cat $conf |sort -n -k 2 -t \' >$conf.tmp
  126. mv -f $conf.tmp $conf
  127. }
  128. add_web_config() {
  129. # Adding template to config
  130. cat $tpl_file | \
  131. sed -e "s/%ip%/$ip/g" \
  132. -e "s/%web_port%/$WEB_PORT/g" \
  133. -e "s/%web_ssl_port%/$WEB_SSL_PORT/g" \
  134. -e "s/%proxy_string%/${proxy_string////\/}/g" \
  135. -e "s/%proxy_port%/$PROXY_PORT/g" \
  136. -e "s/%proxy_ssl_port%/$PROXY_SSL_PORT/g" \
  137. -e "s/%domain_idn%/$domain_idn/g" \
  138. -e "s/%domain%/$domain/g" \
  139. -e "s/%user%/$user/g" \
  140. -e "s/%group%/$group/g" \
  141. -e "s/%home%/${V_HOME////\/}/g" \
  142. -e "s/%docroot%/${docroot////\/}/g" \
  143. -e "s/%docroot_string%/${docroot_string////\/}/g" \
  144. -e "s/%email%/$email/g" \
  145. -e "s/%alias_string%/$alias_string/g" \
  146. -e "s/%alias_idn%/${aliases_idn//,/ }/g" \
  147. -e "s/%alias%/${aliases//,/ }/g" \
  148. -e "s/%ssl_crt%/${ssl_crt////\/}/g" \
  149. -e "s/%ssl_key%/${ssl_key////\/}/g" \
  150. -e "s/%ssl_pem%/${ssl_pem////\/}/g" \
  151. -e "s/%ssl_ca_str%/${ssl_ca_str////\/}/g" \
  152. -e "s/%nginx_extentions%/${NGINX_EXT//,/|}/g" \
  153. -e "s/%elog%/$elog/g" \
  154. -e "s/%cgi%/$cgi/g" \
  155. -e "s/%cgi_option%/$cgi_option/g" \
  156. >> $conf
  157. }
  158. get_web_config_brds() {
  159. # Defining template borders
  160. serv_line=$(grep -ni 'Name %domain_idn%' "$tpl_file" |cut -f 1 -d :)
  161. if [ -z "$serv_line" ]; then
  162. log_event 'debug' "$E_PARSING $V_EVENT"
  163. return $E_PARSING
  164. fi
  165. # Template lines
  166. last_line=$(wc -l $tpl_file|cut -f 1 -d ' ')
  167. bfr_line=$((serv_line - 1))
  168. aftr_line=$((last_line - serv_line - 1))
  169. # Config lines
  170. str=$(grep -ni "Name $domain_idn" $conf | cut -f 1 -d :)
  171. top_line=$((str - serv_line + 1))
  172. bottom_line=$((top_line + last_line -1))
  173. # Check for multialias (8k alias issue)
  174. multi=$(sed -n "$top_line,$bottom_line p" $conf |grep ServerAlias |wc -l)
  175. if [ "$multi" -ge 2 ]; then
  176. bottom_line=$((bottom_line + multi -1))
  177. fi
  178. }
  179. change_web_config() {
  180. # Get config borders
  181. get_web_config_brds || exit $?
  182. # Parsing config
  183. vhost=$(grep -A $aftr_line -B $bfr_line -ni "Name $domain_idn" $conf)
  184. str=$(echo "$vhost" | grep -F "$search_phrase" | head -n 1)
  185. # Parsing string position and content
  186. str_numb=$(echo "$str" | sed -e "s/-/=/" | cut -f 1 -d '=')
  187. str_cont=$(echo "$str" | sed -e "s/-/=/" | cut -f 2 -d '=')
  188. # Escaping chars
  189. str_repl=$(echo "$str_repl" | sed \
  190. -e 's/\\/\\\\/g' \
  191. -e 's/&/\\&/g' \
  192. -e 's/\//\\\//g')
  193. # Changing config
  194. if [ ! -z "$str" ]; then
  195. sed -i "$str_numb s/.*/$str_repl/" $conf
  196. fi
  197. }
  198. replace_web_config() {
  199. # Get config borders
  200. get_web_config_brds || exit $?
  201. # Escaping chars
  202. clean_new=$(echo "$new" | sed \
  203. -e 's/\\/\\\\/g' \
  204. -e 's/&/\\&/g' \
  205. -e 's/\//\\\//g')
  206. clean_old=$(echo "$old" | sed \
  207. -e 's/\\/\\\\/g' \
  208. -e 's/&/\\&/g' \
  209. -e 's/\//\\\//g')
  210. # Replacing string in config
  211. sed -i "$top_line,$bottom_line s/$clean_old/$clean_new/" $conf
  212. }
  213. get_domain_value() {
  214. domain_type="$1"
  215. key="$2"
  216. # Parsing domains
  217. string=$( grep "DOMAIN='$domain'" $V_USERS/$user/$domain_type.conf )
  218. # Parsing key=value
  219. eval $string
  220. # Self reference
  221. eval value="$key"
  222. # Print value
  223. echo "$value"
  224. }
  225. get_domain_values() {
  226. # Defining domain parameters
  227. for line in $(grep "DOMAIN='$domain'" $V_USERS/$user/$1.conf); do
  228. # Assing key=value
  229. eval $line
  230. done
  231. }
  232. update_domain_value() {
  233. domain_type="$1"
  234. key="$2"
  235. value="$3"
  236. # Defining conf
  237. conf="$V_USERS/$user/$domain_type.conf"
  238. # Parsing conf
  239. domain_str=$(grep -n "DOMAIN='$domain'" $conf)
  240. str_number=$(echo $domain_str | cut -f 1 -d ':')
  241. str=$(echo $domain_str | cut -f 2 -d ':')
  242. # Reading key=values
  243. eval $str
  244. # Defining clean key
  245. c_key=$(echo "${key//$/}")
  246. eval old="${key}"
  247. # Escaping slashes
  248. old=$(echo "$old" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
  249. new=$(echo "$value" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
  250. # Updating conf
  251. sed -i "$str_number s/$c_key='${old//\*/\\*}'/$c_key='${new//\*/\\*}'/g"\
  252. $conf
  253. }
  254. is_domain_key_empty() {
  255. domain_type="$1"
  256. key="$2"
  257. # Parsing domains
  258. string=$( grep "DOMAIN='$domain'" $V_USERS/$user/$domain_type.conf )
  259. # Parsing key=value
  260. eval $string
  261. # Self reference
  262. eval value="$key"
  263. # Checkng key
  264. if [ ! -z "$value" ] && [ "$value" != 'no' ]; then
  265. echo "Error: value is not empty = $value"
  266. log_event 'debug' "$E_EXISTS $V_EVENT"
  267. exit $E_EXISTS
  268. fi
  269. }
  270. is_web_domain_cert_valid() {
  271. # Checking file existance
  272. if [ ! -e "$ssl_dir/$domain.crt" ] || [ ! -e "$ssl_dir/$domain.key" ]; then
  273. echo "Error: ssl certificate not exist"
  274. log_event 'debug' "$E_NOTEXIST $V_EVENT"
  275. exit $E_NOTEXIST
  276. fi
  277. # Checking certificate
  278. crt=$(openssl verify $ssl_dir/$domain.crt 2>/dev/null |grep '/C=')
  279. if [ -z "$crt" ]; then
  280. echo "Error: ssl certificate invalid"
  281. log_event 'debug' "$E_INVALID $V_EVENT"
  282. exit $E_INVALID
  283. fi
  284. # Checking certificate key
  285. openssl rsa -in "$ssl_dir/$domain.key" -check >/dev/null 2>/dev/null
  286. if [ "$?" -ne 0 ]; then
  287. echo "Error: ssl key invalid"
  288. log_event 'debug' "$E_INVALID $V_EVENT"
  289. exit $E_INVALID
  290. fi
  291. # Checking certificate authority
  292. if [ -e "$ssl_dir/$domain.ca" ]; then
  293. ca=$(openssl verify $ssl_dir/$domain.ca 2>/dev/null |grep '/C=')
  294. if [ -z "$ca" ]; then
  295. echo "Error: ssl certificate invalid"
  296. log_event 'debug' "$E_INVALID $V_EVENT"
  297. exit $E_INVALID
  298. fi
  299. fi
  300. # Checking server
  301. openssl s_server -quiet \
  302. -cert $ssl_dir/$domain.crt -key $ssl_dir/$domain.key &
  303. pid=$!
  304. sleep 1
  305. disown > /dev/null 2>&1
  306. kill $pid > /dev/null 2>&1
  307. result=$?
  308. if [ "$result" -ne '0' ]; then
  309. echo "Error: ssl certificate key pair invalid"
  310. log_event 'debug' "$E_INVALID $V_EVENT"
  311. exit $E_INVALID
  312. fi
  313. }
  314. is_dns_record_valid() {
  315. # Checking record id
  316. check_id=$(grep "^ID='$id'" $V_USERS/$user/dns/$domain)
  317. if [ -z "$check_id" ]; then
  318. echo "Error: ID not exist"
  319. log_event 'debug' "$E_NOTEXIST $V_EVENT"
  320. exit $E_NOTEXIST
  321. fi
  322. }
  323. is_domain_value_exist() {
  324. domain_type="$1"
  325. key="$2"
  326. # Parsing domains
  327. string=$( grep "DOMAIN='$domain'" $V_USERS/$user/$domain_type.conf )
  328. # Parsing key=value
  329. eval $string
  330. # Self reference
  331. eval value="$key"
  332. # Checking result
  333. if [ -z "$value" ] || [ "$value" = 'no' ]; then
  334. echo "Error: ${key//$/} is empty"
  335. log_event 'debug' "$E_NOTEXIST $V_EVENT"
  336. exit $E_NOTEXIST
  337. fi
  338. }
  339. del_web_config() {
  340. # Get config borders
  341. get_web_config_brds || exit $?
  342. # Deleting lines from config
  343. sed -i "$top_line,$bottom_line d" $conf
  344. }
  345. del_dns_domain() {
  346. conf="$V_USERS/$user/dns.conf"
  347. # Parsing domains
  348. string=$( grep -n "DOMAIN='$domain'" $conf | cut -f 1 -d : )
  349. if [ -z "$string" ]; then
  350. echo "Error: parse error"
  351. log_event 'debug' "$E_PARSING $V_EVENT"
  352. exit $E_PARSING
  353. fi
  354. sed -i "$string d" $conf
  355. rm -f $V_USERS/$user/dns/$domain
  356. }
  357. del_web_domain() {
  358. conf="$V_USERS/$user/web.conf"
  359. # Parsing domains
  360. string=$( grep -n "DOMAIN='$domain'" $conf | cut -f 1 -d : )
  361. if [ -z "$string" ]; then
  362. echo "Error: parse error"
  363. log_event 'debug' "$E_PARSING $V_EVENT"
  364. exit $E_PARSING
  365. fi
  366. sed -i "$string d" $conf
  367. }
  368. dom_clear_search(){
  369. # Defining delimeter
  370. IFS=$'\n'
  371. # Reading file line by line
  372. for line in $(grep $search_string $conf); do
  373. # Parsing key=val
  374. eval $line
  375. # Print result line
  376. eval echo "$field"
  377. done
  378. }
  379. dom_clear_list() {
  380. # Reading file line by line
  381. while read line ; do
  382. # Parsing key=value
  383. eval $line
  384. # Print result line
  385. eval echo "$field"
  386. done < $conf
  387. }
  388. namehost_ip_support() {
  389. # Checking httpd config for NameHost string number
  390. if [ "$WEB_SYSTEM" = 'apache' ]; then
  391. conf_line=$(grep -n "NameVirtual" $conf|tail -n 1|cut -f 1 -d ':')
  392. if [ ! -z "$conf_line" ]; then
  393. conf_ins=$((conf_line + 1))
  394. else
  395. conf_ins='1'
  396. fi
  397. # Checking ssl support
  398. if [ "$WEB_SSL" = 'mod_ssl' ]; then
  399. sed -i "$conf_ins i NameVirtualHost $ip:$WEB_SSL_PORT" $conf
  400. sed -i "$conf_ins i Listen $ip:$WEB_SSL_PORT" $conf
  401. fi
  402. sed -i "$conf_ins i NameVirtualHost $ip:$WEB_PORT" $conf
  403. sed -i "$conf_ins i Listen $ip:$WEB_PORT" $conf
  404. # Checking proxy support
  405. if [ "$PROXY_SYSTEM" = 'nginx' ]; then
  406. cat $V_WEBTPL/ngingx_ip.tpl | sed -e "s/%ip%/$ip/g" \
  407. -e "s/%web_port%/$WEB_PORT/g" \
  408. -e "s/%proxy_port%/$PROXY_PORT/g" >>$nconf
  409. # Adding to rpaf ip pool as well
  410. ips=$(grep 'RPAFproxy_ips' $rconf)
  411. sed -i "s/$ips/$ips $ip/g" $rconf
  412. fi
  413. # Scheduling restart
  414. web_restart='yes'
  415. fi
  416. }
  417. namehost_ip_disable() {
  418. #Checking web system
  419. if [ "$WEB_SYSTEM" = 'apache' ]; then
  420. sed -i "/NameVirtualHost $ip:/d" $conf
  421. sed -i "/Listen $ip:/d" $conf
  422. # Checking proxy support
  423. if [ "$PROXY_SYSTEM" = 'nginx' ]; then
  424. tpl_ln=$(wc -l $V_WEBTPL/ngingx_ip.tpl | cut -f 1 -d ' ')
  425. ip_line=$(grep -n "%ip%" $V_WEBTPL/ngingx_ip.tpl |head -n1 |\
  426. cut -f 1 -d :)
  427. conf_line=$(grep -n -w $ip $nconf|head -n1|cut -f 1 -d :)
  428. # Checking parsed lines
  429. if [ -z "$tpl_ln" ] || [ -z "$ip_line" ] || [ -z "$conf_line" ]
  430. then
  431. echo "Error: nginx config paring error"
  432. log_event 'debug' "$E_PARSING $V_EVENT"
  433. exit $E_PARSING
  434. fi
  435. up_line=$((ip_line - 1))
  436. first_line=$((conf_line - up_line))
  437. last_line=$((conf_line - ip_line + tpl_ln))
  438. # Checking parsed lines
  439. if [ -z "$first_line" ] || [ -z "$last_line" ]; then
  440. echo "Error: nginx config paring error"
  441. log_event 'debug' "$E_PARSING $V_EVENT"
  442. exit $E_PARSING
  443. fi
  444. sed -i "$first_line,$last_line d" $nconf
  445. # Deleting from rpaf ip pool as well
  446. ips=$(grep 'RPAFproxy_ips' $rconf)
  447. new_ips=$(echo "$ips"|sed -e "s/$ip//")
  448. sed -i "s/$ips/$new_ips/g" $rconf
  449. fi
  450. # Scheduling restart
  451. web_restart='yes'
  452. fi
  453. }
  454. upd_web_domain_values() {
  455. ip=$IP
  456. group="$user"
  457. email="$user@$domain"
  458. docroot="$V_HOME/$user/web/$domain/public_html"
  459. docroot_string="DocumentRoot $docroot"
  460. proxy_string="proxy_pass http://$ip:$WEB_PORT;"
  461. # Parsing domain aliases
  462. i=1
  463. j=1
  464. OLD_IFS="$IFS"
  465. IFS=','
  466. server_alias=''
  467. alias_string=''
  468. for dalias in $ALIAS; do
  469. dalias=$(idn -t --quiet -a $dalias)
  470. # Spliting ServerAlias lines
  471. check_8k="$server_alias $dalias"
  472. if [ "${#check_8k}" -ge '8100' ]; then
  473. if [ "$j" -eq 1 ]; then
  474. alias_string="ServerAlias $server_alias"
  475. else
  476. alias_string="$alias_string\n ServerAlias $server_alias"
  477. fi
  478. j=2
  479. server_alias=''
  480. fi
  481. if [ "$i" -eq 1 ]; then
  482. aliases_idn="$dalias"
  483. server_alias="$dalias"
  484. alias_string="ServerAlias $server_alias"
  485. else
  486. aliases_idn="$aliases_idn,$dalias"
  487. server_alias="$server_alias $dalias"
  488. fi
  489. i=2
  490. done
  491. if [ $j -gt 1 ]; then
  492. alias_string="$alias_string\n ServerAlias $server_alias"
  493. else
  494. alias_string="ServerAlias $server_alias"
  495. fi
  496. IFS=$OLD_IFS
  497. # Checking error log status
  498. if [ "$ELOG" = 'no' ]; then
  499. elog='#'
  500. else
  501. elog=''
  502. fi
  503. # Checking cgi
  504. if [ "$CGI" != 'yes' ]; then
  505. cgi='#'
  506. cgi_option='-ExecCGI'
  507. else
  508. cgi=''
  509. cgi_option='+ExecCGI'
  510. fi
  511. # Checking suspend
  512. if [ "$SUSPENDED" = 'yes' ]; then
  513. docroot_string="Redirect / http://$url"
  514. proxy_string="rewrite ^(.*)\$ http://$url;"
  515. fi
  516. # Defining SSL vars
  517. ssl_crt="$V_HOME/$user/conf/web/ssl.$domain.crt"
  518. ssl_key="$V_HOME/$user/conf/web/ssl.$domain.key"
  519. ssl_pem="$V_HOME/$user/conf/web/ssl.$domain.pem"
  520. ssl_ca="$V_HOME/$user/conf/web/ssl.$domain.ca"
  521. if [ ! -e "$V_USERS/$user/ssl/$domain.ca" ]; then
  522. ssl_ca_str='#'
  523. fi
  524. case $SSL_HOME in
  525. single) docroot="$V_HOME/$user/web/$domain/public_shtml" ;;
  526. same) docroot="$V_HOME/$user/web/$domain/public_html" ;;
  527. esac
  528. }