main.sh 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  1. #!/usr/bin/env bash
  2. #===========================================================================#
  3. # #
  4. # Hestia Control Panel - Core Function Library #
  5. # #
  6. #===========================================================================#
  7. # Internal variables
  8. HOMEDIR='/home'
  9. BACKUP='/backup'
  10. BACKUP_GZIP=9
  11. BACKUP_DISK_LIMIT=95
  12. BACKUP_LA_LIMIT=$(cat /proc/cpuinfo | grep processor | wc -l)
  13. RRD_STEP=300
  14. BIN=$HESTIA/bin
  15. HESTIA_INSTALL_DIR="$HESTIA/install/deb"
  16. HESTIA_COMMON_DIR="$HESTIA/install/common"
  17. HESTIA_BACKUP="/root/hst_backups/$(date +%d%m%Y%H%M)"
  18. HESTIA_PHP="$HESTIA/php/bin/php"
  19. USER_DATA=$HESTIA/data/users/$user
  20. WEBTPL=$HESTIA/data/templates/web
  21. MAILTPL=$HESTIA/data/templates/mail
  22. DNSTPL=$HESTIA/data/templates/dns
  23. RRD=$HESTIA/web/rrd
  24. SENDMAIL="$HESTIA/web/inc/mail-wrapper.php"
  25. HESTIA_GIT_REPO="https://raw.githubusercontent.com/hestiacp/hestiacp"
  26. HESTIA_THEMES="$HESTIA/web/css/themes"
  27. HESTIA_THEMES_CUSTOM="$HESTIA/web/css/themes/custom"
  28. SCRIPT="$(basename $0)"
  29. CHECK_RESULT_CALLBACK=""
  30. # Return codes
  31. OK=0
  32. E_ARGS=1
  33. E_INVALID=2
  34. E_NOTEXIST=3
  35. E_EXISTS=4
  36. E_SUSPENDED=5
  37. E_UNSUSPENDED=6
  38. E_INUSE=7
  39. E_LIMIT=8
  40. E_PASSWORD=9
  41. E_FORBIDEN=10
  42. E_DISABLED=11
  43. E_PARSING=12
  44. E_DISK=13
  45. E_LA=14
  46. E_CONNECT=15
  47. E_FTP=16
  48. E_DB=17
  49. E_RRD=18
  50. E_UPDATE=19
  51. E_RESTART=20
  52. # Detect operating system
  53. detect_os() {
  54. if [ -e "/etc/os-release" ]; then
  55. get_os_type=$(grep "^ID=" /etc/os-release | cut -f 2 -d '=')
  56. if [ "$get_os_type" = "ubuntu" ]; then
  57. if [ -e '/usr/bin/lsb_release' ]; then
  58. OS_VERSION="$(lsb_release -s -r)"
  59. OS_TYPE='Ubuntu'
  60. fi
  61. elif [ "$get_os_type" = "debian" ]; then
  62. OS_TYPE='Debian'
  63. OS_VERSION=$(cat /etc/debian_version | grep -o "[0-9]\{1,2\}" | head -n1)
  64. fi
  65. else
  66. OS_TYPE="Unsupported OS"
  67. OS_VERSION="Unknown"
  68. fi
  69. }
  70. # Generate time stamp
  71. new_timestamp() {
  72. time_n_date=$(date +'%T %F')
  73. time=$(echo "$time_n_date" | cut -f 1 -d \ )
  74. date=$(echo "$time_n_date" | cut -f 2 -d \ )
  75. }
  76. # Event string for logger
  77. ARGS=("$@")
  78. for ((I = 1; I <= $#; I++)); do
  79. if [[ "$HIDE" != "$I" ]]; then
  80. ARGUMENTS="$ARGUMENTS '${ARGS[${I} - 1]}'"
  81. else
  82. ARGUMENTS="$ARGUMENTS '******'"
  83. fi
  84. done
  85. # Log event function
  86. log_event() {
  87. if [ -z "$time" ]; then
  88. LOG_TIME="$(date +'%F %T') $(basename $0)"
  89. else
  90. LOG_TIME="$date $time $(basename $0)"
  91. fi
  92. if [ "$1" -eq 0 ]; then
  93. echo "$LOG_TIME $2" >> $HESTIA/log/system.log
  94. else
  95. echo "$LOG_TIME $2 [Error $1]" >> $HESTIA/log/error.log
  96. fi
  97. }
  98. # Log user history
  99. log_history() {
  100. message=${1//\'/\´} # Avoid single quotes broken the log
  101. evt_level=${2:-$event_level}
  102. log_user=${3-$user}
  103. evt_category=${4:-$event_category}
  104. # Set default event level and category if not specified
  105. if [ -z "$evt_level" ]; then
  106. evt_level="Info"
  107. fi
  108. if [ -z "$evt_category" ]; then
  109. evt_category="System"
  110. fi
  111. # Log system events to system log file
  112. if [ "$log_user" = "system" ]; then
  113. log=$HESTIA/data/users/admin/system.log
  114. else
  115. if ! $BIN/v-list-user "$log_user" > /dev/null; then
  116. return $E_NOTEXIST
  117. fi
  118. log=$HESTIA/data/users/$log_user/history.log
  119. fi
  120. touch $log
  121. if [ '750' -lt "$(wc -l $log | cut -f 1 -d ' ')" ]; then
  122. tail -n 499 $log > $log.moved
  123. mv -f $log.moved $log
  124. chmod 660 $log
  125. fi
  126. if [ -z "$date" ]; then
  127. time_n_date=$(date +'%T %F')
  128. time=$(echo "$time_n_date" | cut -f 1 -d \ )
  129. date=$(echo "$time_n_date" | cut -f 2 -d \ )
  130. fi
  131. curr_str=$(tail -n1 $log | grep "ID=" --text | cut -f2 -d \')
  132. id="$((curr_str + 1))"
  133. echo "ID='$id' DATE='$date' TIME='$time' LEVEL='$evt_level' CATEGORY='$evt_category' MESSAGE='$message'" >> $log
  134. }
  135. # Result checker
  136. check_result() {
  137. if [ $1 -ne 0 ]; then
  138. local err_code="${3:-$1}"
  139. if [[ -n "$CHECK_RESULT_CALLBACK" && "$(type -t "$CHECK_RESULT_CALLBACK")" == 'function' ]]; then
  140. $CHECK_RESULT_CALLBACK "$err_code" "$2"
  141. else
  142. echo "Error: $2"
  143. log_event "$err_code" "$ARGUMENTS"
  144. fi
  145. exit $err_code
  146. fi
  147. }
  148. # Argument list checker
  149. check_args() {
  150. if [ "$1" -gt "$2" ]; then
  151. echo "Usage: $(basename $0) $3"
  152. check_result "$E_ARGS" "not enought arguments" > /dev/null
  153. fi
  154. }
  155. # Subsystem checker
  156. is_system_enabled() {
  157. if [ -z "$1" ] || [ "$1" = no ]; then
  158. check_result "$E_DISABLED" "$2 is not enabled"
  159. fi
  160. }
  161. # User package check
  162. is_package_full() {
  163. case "$1" in
  164. WEB_DOMAINS) used=$(wc -l $USER_DATA/web.conf) ;;
  165. WEB_ALIASES) used=$(echo $aliases | tr ',' '\n' | wc -l) ;;
  166. DNS_DOMAINS) used=$(wc -l $USER_DATA/dns.conf) ;;
  167. DNS_RECORDS) used=$(wc -l $USER_DATA/dns/$domain.conf) ;;
  168. MAIL_DOMAINS) used=$(wc -l $USER_DATA/mail.conf) ;;
  169. MAIL_ACCOUNTS) used=$(wc -l $USER_DATA/mail/$domain.conf) ;;
  170. DATABASES) used=$(wc -l $USER_DATA/db.conf) ;;
  171. CRON_JOBS) used=$(wc -l $USER_DATA/cron.conf) ;;
  172. esac
  173. used=$(echo "$used" | cut -f 1 -d \ )
  174. limit=$(grep "^$1=" $USER_DATA/user.conf | cut -f 2 -d \')
  175. if [ "$1" = WEB_ALIASES ]; then
  176. # Used is always calculated with the new alias added
  177. if [ "$limit" != 'unlimited' ] && [[ "$used" -gt "$limit" ]]; then
  178. check_result "$E_LIMIT" "$1 limit is reached :: upgrade user package"
  179. fi
  180. else
  181. if [ "$limit" != 'unlimited' ] && [[ "$used" -ge "$limit" ]]; then
  182. check_result "$E_LIMIT" "$1 limit is reached :: upgrade user package"
  183. fi
  184. fi
  185. }
  186. # User owner for reseller plugin
  187. get_user_owner() {
  188. if [ -z "$RESELLER_KEY" ]; then
  189. owner='admin'
  190. else
  191. owner=$(grep "^OWNER" $USER_DATA/user.conf | cut -f 2 -d \')
  192. if [ -z "$owner" ]; then
  193. owner='admin'
  194. fi
  195. fi
  196. }
  197. # Random password generator
  198. generate_password() {
  199. matrix=$1
  200. length=$2
  201. if [ -z "$matrix" ]; then
  202. matrix="A-Za-z0-9"
  203. fi
  204. if [ -z "$length" ]; then
  205. length=16
  206. fi
  207. head /dev/urandom | tr -dc $matrix | head -c$length
  208. }
  209. # Package existence check
  210. is_package_valid() {
  211. if [ -z $1 ]; then
  212. if [ ! -e "$HESTIA/data/packages/$package.pkg" ]; then
  213. check_result "$E_NOTEXIST" "package $package doesn't exist"
  214. fi
  215. else
  216. if [ ! -e "$HESTIA/data/packages/$1.pkg" ]; then
  217. check_result "$E_NOTEXIST" "package $1 doesn't exist"
  218. fi
  219. fi
  220. }
  221. is_package_new() {
  222. if [ -e "$HESTIA/data/packages/$1.pkg" ]; then
  223. echo "Error: package $1 already exists."
  224. log_event "$E_EXISTS" "$ARGUMENTS"
  225. exit "$E_EXISTS"
  226. fi
  227. }
  228. # Validate system type
  229. is_type_valid() {
  230. if [ -z "$(echo $1 | grep -w $2)" ]; then
  231. check_result "$E_INVALID" "$2 type is invalid"
  232. fi
  233. }
  234. # Check user backup settings
  235. is_backup_enabled() {
  236. BACKUPS=$(grep "^BACKUPS=" $USER_DATA/user.conf | cut -f2 -d \')
  237. if [ -z "$BACKUPS" ] || [[ "$BACKUPS" -le '0' ]]; then
  238. check_result "$E_DISABLED" "user backup is disabled"
  239. fi
  240. }
  241. # Check user backup settings
  242. is_backup_scheduled() {
  243. if [ -e "$HESTIA/data/queue/backup.pipe" ]; then
  244. check_q=$(grep " $user " $HESTIA/data/queue/backup.pipe | grep $1)
  245. if [ -n "$check_q" ]; then
  246. check_result "$E_EXISTS" "$1 is already scheduled"
  247. fi
  248. fi
  249. }
  250. # Check if object is new
  251. is_object_new() {
  252. if [ $2 = 'USER' ]; then
  253. if [ -d "$USER_DATA" ]; then
  254. object="OK"
  255. fi
  256. else
  257. object=$(grep "$2='$3'" $USER_DATA/$1.conf)
  258. fi
  259. if [ -n "$object" ]; then
  260. check_result "$E_EXISTS" "$2=$3 already exists"
  261. fi
  262. }
  263. # Check if object is valid
  264. is_object_valid() {
  265. if [ $2 = 'USER' ]; then
  266. tstpath="$(readlink -f "$HESTIA/data/users/$3")"
  267. if [ "$(dirname "$tstpath")" != "$(readlink -f "$HESTIA/data/users")" ] || [ ! -d "$HESTIA/data/users/$3" ]; then
  268. check_result "$E_NOTEXIST" "$1 $3 doesn't exist"
  269. fi
  270. elif [ $2 = 'KEY' ]; then
  271. local key="$(basename "$3")"
  272. if [[ -z "$key" || ${#key} -lt 16 ]] || [[ ! -f "$HESTIA/data/access-keys/${key}" && ! -f "$HESTIA/data/access-keys/$key" ]]; then
  273. check_result "$E_NOTEXIST" "$1 $3 doesn't exist"
  274. fi
  275. else
  276. object=$(grep "$2='$3'" $HESTIA/data/users/$user/$1.conf)
  277. if [ -z "$object" ]; then
  278. arg1=$(basename $1)
  279. arg2=$(echo $2 | tr '[:upper:]' '[:lower:]')
  280. check_result "$E_NOTEXIST" "$arg1 $arg2 $3 doesn't exist"
  281. fi
  282. fi
  283. }
  284. # Check if a object string with key values pairs has the correct format and load it afterwards
  285. parse_object_kv_list_non_eval() {
  286. local str
  287. local objkv obj_key obj_val
  288. local OLD_IFS="$IFS"
  289. str=${@//$'\n'/ }
  290. str=${str//\"/\\\"}
  291. str=${str//$/\\$}
  292. IFS=$'\n'
  293. # Extract and loop trough each key-value pair. (Regex test: https://regex101.com/r/eiMufk/5)
  294. for objkv in $(echo "$str" | perl -n -e "while(/\b([a-zA-Z]+[\w]*)='(.*?)'(\s|\$)/g) {print \$1.'='.\$2 . \"\n\" }"); do
  295. if ! [[ "$objkv" =~ ^([[:alnum:]][_[:alnum:]]{0,64}[[:alnum:]])=(\'?[^\']+?\'?)?$ ]]; then
  296. check_result "$E_INVALID" "Invalid key value format [$objkv]"
  297. fi
  298. obj_key=${objkv%%=*} # strip everything after first '=' char
  299. obj_val=${objkv#*=} # strip everything before first '=' char
  300. declare -g $obj_key="$obj_val"
  301. done
  302. IFS="$OLD_IFS"
  303. }
  304. # Check if a object string with key values pairs has the correct format and load it afterwards
  305. parse_object_kv_list() {
  306. local str
  307. local objkv
  308. local suboutput
  309. local OLD_IFS="$IFS"
  310. str=${@//$'\n'/ }
  311. str=${str//\"/\\\"}
  312. str=${str//$/\\$}
  313. IFS=$'\n'
  314. suboutput=$(setpriv --clear-groups --reuid nobody --regid nogroup bash -c "PS4=''; set -xe; eval \"${str}\"" 2>&1)
  315. check_result $? "Invalid object format: ${str}" $E_INVALID
  316. for objkv in $suboutput; do
  317. if [[ "$objkv" =~ ^'eval ' ]]; then
  318. continue
  319. fi
  320. if ! [[ "$objkv" =~ ^([[:alnum:]][_[:alnum:]]{0,64}[[:alnum:]])=(\'?[^\']+?\'?)?$ ]]; then
  321. check_result "$E_INVALID" "Invalid key value format [$objkv]"
  322. fi
  323. eval "$objkv"
  324. done
  325. IFS="$OLD_IFS"
  326. }
  327. # Check if object is supended
  328. is_object_suspended() {
  329. if [ $2 = 'USER' ]; then
  330. spnd=$(cat $USER_DATA/$1.conf | grep "SUSPENDED='yes'")
  331. else
  332. spnd=$(grep "$2='$3'" $USER_DATA/$1.conf | grep "SUSPENDED='yes'")
  333. fi
  334. if [ -z "$spnd" ]; then
  335. check_result "$E_UNSUSPENDED" "$(basename $1) $3 is not suspended"
  336. fi
  337. }
  338. # Check if object is unsupended
  339. is_object_unsuspended() {
  340. if [ $2 = 'USER' ]; then
  341. spnd=$(cat $USER_DATA/$1.conf | grep "SUSPENDED='yes'")
  342. else
  343. spnd=$(grep "$2='$3'" $USER_DATA/$1.conf | grep "SUSPENDED='yes'")
  344. fi
  345. if [ -n "$spnd" ]; then
  346. check_result "$E_SUSPENDED" "$(basename $1) $3 is suspended"
  347. fi
  348. }
  349. # Check if object value is empty
  350. is_object_value_empty() {
  351. str=$(grep "$2='$3'" $USER_DATA/$1.conf)
  352. parse_object_kv_list "$str"
  353. eval value=$4
  354. if [ -n "$value" ] && [ "$value" != 'no' ]; then
  355. check_result "$E_EXISTS" "${4//$/}=$value already exists"
  356. fi
  357. }
  358. # Check if object value is empty
  359. is_object_value_exist() {
  360. str=$(grep "$2='$3'" $USER_DATA/$1.conf)
  361. parse_object_kv_list "$str"
  362. eval value=$4
  363. if [ -z "$value" ] || [ "$value" = 'no' ]; then
  364. check_result "$E_NOTEXIST" "${4//$/}=$value doesn't exist"
  365. fi
  366. }
  367. # Check if password is transmitted via file
  368. is_password_valid() {
  369. if [[ "$password" =~ ^/tmp/ ]]; then
  370. if ! [[ "$password" == *../* ]]; then
  371. if [ -f "$password" ]; then
  372. password="$(head -n1 $password)"
  373. fi
  374. fi
  375. fi
  376. }
  377. # Check if hash is transmitted via file
  378. is_hash_valid() {
  379. if [[ "$hash" =~ ^/tmp/ ]]; then
  380. if ! [[ "$hash" == *../* ]]; then
  381. if [ -f "$hash" ]; then
  382. hash="$(head -n1 $hash)"
  383. fi
  384. fi
  385. fi
  386. }
  387. # Check if directory is a symlink
  388. is_dir_symlink() {
  389. if [[ -L "$1" ]]; then
  390. check_result "$E_FORBIDEN" "$1 directory is a symlink"
  391. fi
  392. }
  393. # Get object value
  394. get_object_value() {
  395. object=$(grep "$2='$3'" $USER_DATA/$1.conf)
  396. parse_object_kv_list "$object"
  397. eval echo $4
  398. }
  399. get_object_values() {
  400. parse_object_kv_list $(grep "$2='$3'" $USER_DATA/$1.conf)
  401. }
  402. # Update object value
  403. update_object_value() {
  404. row=$(grep -nF "$2='$3'" $USER_DATA/$1.conf)
  405. lnr=$(echo $row | cut -f 1 -d ':')
  406. object=$(echo $row | sed "s/^$lnr://")
  407. parse_object_kv_list "$object"
  408. eval old="$4"
  409. old=$(echo "$old" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
  410. new=$(echo "$5" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
  411. sed -i "$lnr s/${4//$/}='${old//\*/\\*}'/${4//$/}='${new//\*/\\*}'/g" \
  412. $USER_DATA/$1.conf
  413. }
  414. # Add object key
  415. add_object_key() {
  416. row=$(grep -n "$2='$3'" $USER_DATA/$1.conf)
  417. lnr=$(echo $row | cut -f 1 -d ':')
  418. object=$(echo $row | sed "s/^$lnr://")
  419. if [ -z "$(echo $object | grep $4=)" ]; then
  420. eval old="$4"
  421. sed -i "$lnr s/$5='/$4='' $5='/" $USER_DATA/$1.conf
  422. fi
  423. }
  424. # Search objects
  425. search_objects() {
  426. OLD_IFS="$IFS"
  427. IFS=$'\n'
  428. if [ -f $USER_DATA/$1.conf ]; then
  429. for line in $(grep $2=\'$3\' $USER_DATA/$1.conf); do
  430. parse_object_kv_list "$line"
  431. eval echo \$$4
  432. done
  433. fi
  434. IFS="$OLD_IFS"
  435. }
  436. # Get user value
  437. get_user_value() {
  438. grep "^${1//$/}=" $USER_DATA/user.conf | head -1 | awk -F "'" '{print $2}'
  439. }
  440. # Update user value in user.conf
  441. update_user_value() {
  442. key="${2//$/}"
  443. lnr=$(grep -n "^$key='" $HESTIA/data/users/$1/user.conf | cut -f 1 -d ':')
  444. if [ -n "$lnr" ]; then
  445. sed -i "$lnr d" $HESTIA/data/users/$1/user.conf
  446. sed -i "$lnr i\\$key='${3}'" $HESTIA/data/users/$1/user.conf
  447. fi
  448. }
  449. # Increase user counter
  450. increase_user_value() {
  451. key="${2//$/}"
  452. factor="${3-1}"
  453. conf="$HESTIA/data/users/$1/user.conf"
  454. old=$(grep "$key=" $conf | cut -f 2 -d \')
  455. if [ -z "$old" ]; then
  456. old=0
  457. fi
  458. new=$((old + factor))
  459. sed -i "s/$key='$old'/$key='$new'/g" $conf
  460. }
  461. # Decrease user counter
  462. decrease_user_value() {
  463. key="${2//$/}"
  464. factor="${3-1}"
  465. conf="$HESTIA/data/users/$1/user.conf"
  466. old=$(grep "$key=" $conf | cut -f 2 -d \')
  467. if [ -z "$old" ]; then
  468. old=0
  469. fi
  470. if [ "$old" -le 1 ]; then
  471. new=0
  472. else
  473. new=$((old - factor))
  474. fi
  475. if [ "$new" -lt 0 ]; then
  476. new=0
  477. fi
  478. sed -i "s/$key='$old'/$key='$new'/g" $conf
  479. }
  480. # Notify user
  481. send_notice() {
  482. topic=$1
  483. notice=$2
  484. if [ "$notify" = 'yes' ]; then
  485. touch $USER_DATA/notifications.conf
  486. chmod 660 $USER_DATA/notifications.conf
  487. time_n_date=$(date +'%T %F')
  488. time=$(echo "$time_n_date" | cut -f 1 -d \ )
  489. date=$(echo "$time_n_date" | cut -f 2 -d \ )
  490. nid=$(grep "NID=" $USER_DATA/notifications.conf | cut -f 2 -d \')
  491. nid=$(echo "$nid" | sort -n | tail -n1)
  492. if [ -n "$nid" ]; then
  493. nid="$((nid + 1))"
  494. else
  495. nid=1
  496. fi
  497. str="NID='$nid' TOPIC='$topic' NOTICE='$notice' TYPE='$type'"
  498. str="$str ACK='no' TIME='$time' DATE='$date'"
  499. echo "$str" >> $USER_DATA/notifications.conf
  500. if [ -z "$(grep NOTIFICATIONS $USER_DATA/user.conf)" ]; then
  501. sed -i "s/^TIME/NOTIFICATIONS='yes'\nTIME/g" $USER_DATA/user.conf
  502. else
  503. update_user_value "$user" '$NOTIFICATIONS' "yes"
  504. fi
  505. fi
  506. }
  507. # Recalculate U_DISK value
  508. recalc_user_disk_usage() {
  509. u_usage=0
  510. if [ -f "$USER_DATA/web.conf" ]; then
  511. usage=0
  512. dusage=$(grep 'U_DISK=' $USER_DATA/web.conf \
  513. | awk -F "U_DISK='" '{print $2}' | cut -f 1 -d \')
  514. for disk_usage in $dusage; do
  515. usage=$((usage + disk_usage))
  516. done
  517. d=$(grep "U_DISK_WEB='" $USER_DATA/user.conf | cut -f 2 -d \')
  518. sed -i "s/U_DISK_WEB='$d'/U_DISK_WEB='$usage'/g" $USER_DATA/user.conf
  519. u_usage=$((u_usage + usage))
  520. fi
  521. if [ -f "$USER_DATA/mail.conf" ]; then
  522. usage=0
  523. dusage=$(grep 'U_DISK=' $USER_DATA/mail.conf \
  524. | awk -F "U_DISK='" '{print $2}' | cut -f 1 -d \')
  525. for disk_usage in $dusage; do
  526. usage=$((usage + disk_usage))
  527. done
  528. d=$(grep "U_DISK_MAIL='" $USER_DATA/user.conf | cut -f 2 -d \')
  529. sed -i "s/U_DISK_MAIL='$d'/U_DISK_MAIL='$usage'/g" $USER_DATA/user.conf
  530. u_usage=$((u_usage + usage))
  531. fi
  532. if [ -f "$USER_DATA/db.conf" ]; then
  533. usage=0
  534. dusage=$(grep 'U_DISK=' $USER_DATA/db.conf \
  535. | awk -F "U_DISK='" '{print $2}' | cut -f 1 -d \')
  536. for disk_usage in $dusage; do
  537. usage=$((usage + disk_usage))
  538. done
  539. d=$(grep "U_DISK_DB='" $USER_DATA/user.conf | cut -f 2 -d \')
  540. sed -i "s/U_DISK_DB='$d'/U_DISK_DB='$usage'/g" $USER_DATA/user.conf
  541. u_usage=$((u_usage + usage))
  542. fi
  543. usage=$(grep 'U_DISK_DIRS=' $USER_DATA/user.conf | cut -f 2 -d "'")
  544. u_usage=$((u_usage + usage))
  545. old=$(grep "U_DISK='" $USER_DATA/user.conf | cut -f 2 -d \')
  546. sed -i "s/U_DISK='$old'/U_DISK='$u_usage'/g" $USER_DATA/user.conf
  547. }
  548. # Recalculate U_BANDWIDTH value
  549. recalc_user_bandwidth_usage() {
  550. usage=0
  551. bandwidth_usage=$(grep 'U_BANDWIDTH=' $USER_DATA/web.conf \
  552. | awk -F "U_BANDWIDTH='" '{print $2}' | cut -f 1 -d \')
  553. for bandwidth in $bandwidth_usage; do
  554. usage=$((usage + bandwidth))
  555. done
  556. old=$(grep "U_BANDWIDTH='" $USER_DATA/user.conf | cut -f 2 -d \')
  557. sed -i "s/U_BANDWIDTH='$old'/U_BANDWIDTH='$usage'/g" $USER_DATA/user.conf
  558. }
  559. # Get next cron job id
  560. get_next_cronjob() {
  561. if [ -z "$job" ]; then
  562. curr_str=$(grep "JOB=" $USER_DATA/cron.conf | cut -f 2 -d \' \
  563. | sort -n | tail -n1)
  564. job="$((curr_str + 1))"
  565. fi
  566. }
  567. # Sort cron jobs by id
  568. sort_cron_jobs() {
  569. cat $USER_DATA/cron.conf | sort -n -k 2 -t \' > $USER_DATA/cron.tmp
  570. mv -f $USER_DATA/cron.tmp $USER_DATA/cron.conf
  571. }
  572. # Sync cronjobs with system cron
  573. sync_cron_jobs() {
  574. source_conf "$USER_DATA/user.conf"
  575. if [ -e "/var/spool/cron/crontabs" ]; then
  576. crontab="/var/spool/cron/crontabs/$user"
  577. else
  578. crontab="/var/spool/cron/$user"
  579. fi
  580. # remove file if exists
  581. if [ -e "$crontab" ]; then
  582. rm -f $crontab
  583. fi
  584. # touch new crontab file
  585. touch $crontab
  586. if [ "$CRON_REPORTS" = 'yes' ]; then
  587. echo "MAILTO=$CONTACT" > $crontab
  588. echo 'CONTENT_TYPE="text/plain; charset=utf-8"' >> $crontab
  589. else
  590. echo 'MAILTO=""' > $crontab
  591. fi
  592. while read line; do
  593. parse_object_kv_list "$line"
  594. if [ "$SUSPENDED" = 'no' ]; then
  595. echo "$MIN $HOUR $DAY $MONTH $WDAY $CMD" \
  596. | sed -e "s/%quote%/'/g" -e "s/%dots%/:/g" \
  597. >> $crontab
  598. fi
  599. done < $USER_DATA/cron.conf
  600. chown $user:$user $crontab
  601. chmod 600 $crontab
  602. }
  603. # User format validator
  604. is_user_format_valid() {
  605. if [ ${#1} -eq 1 ]; then
  606. if ! [[ "$1" =~ ^^[[:alnum:]]$ ]]; then
  607. check_result "$E_INVALID" "invalid $2 format :: $1"
  608. fi
  609. else
  610. if [ -n "$3" ]; then
  611. maxlenght=$(($3 - 2))
  612. if ! [[ "$1" =~ ^[[:alnum:]][-|\.|_[:alnum:]]{0,$maxlenght}[[:alnum:]]$ ]]; then
  613. check_result "$E_INVALID" "invalid $2 format :: $1"
  614. fi
  615. else
  616. if ! [[ "$1" =~ ^[[:alnum:]][-|\.|_[:alnum:]]{0,28}[[:alnum:]]$ ]]; then
  617. check_result "$E_INVALID" "invalid $2 format :: $1"
  618. fi
  619. fi
  620. fi
  621. if [ "$1" != "${1//[^[:ascii:]]/}" ]; then
  622. check_result "$E_INVALID" "invalid $2 format :: $1"
  623. fi
  624. }
  625. # Domain format validator
  626. is_domain_format_valid() {
  627. object_name=${2-domain}
  628. exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|_|/|\|\"|'|;|%|\`| ]"
  629. if [[ $1 =~ $exclude ]] || [[ $1 =~ ^[0-9]+$ ]] || [[ $1 =~ \.\. ]] || [[ $1 =~ $(printf '\t') ]] || [[ "$1" = "www" ]]; then
  630. check_result "$E_INVALID" "invalid $object_name format :: $1"
  631. fi
  632. is_no_new_line_format "$1"
  633. }
  634. # Alias format validator
  635. is_alias_format_valid() {
  636. for object in ${1//,/ }; do
  637. exclude="[!|@|#|$|^|&|(|)|+|=|{|}|:|<|>|?|_|/|\|\"|'|;|%|\`| ]"
  638. if [[ "$object" =~ $exclude ]]; then
  639. check_result "$E_INVALID" "invalid alias format :: $object"
  640. fi
  641. if [[ "$object" =~ [*] ]] && ! [[ "$object" =~ ^[*]\..* ]]; then
  642. check_result "$E_INVALID" "invalid alias format :: $object"
  643. fi
  644. done
  645. }
  646. # Get IP format
  647. get_ip_format() {
  648. object_name=${2-ipv4}
  649. local ret_code=0
  650. local ret_string=""
  651. # IPV4 check
  652. ip_regex='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
  653. ip_clean=$(echo "${1%/*}")
  654. if [[ $ip_clean =~ ^$ip_regex\.$ip_regex\.$ip_regex\.$ip_regex$ ]]; then
  655. ret_string="4"
  656. else
  657. ret_code=1 # BIT 0: invalid IPV4 format
  658. fi
  659. if [ $1 != "$ip_clean" ]; then
  660. ret_string=""
  661. ip_cidr="$ip_clean/"
  662. ip_cidr=$(echo "${1#$ip_cidr}")
  663. if [[ "$ip_cidr" =~ ^[0-9]+$ ]]; then
  664. if [ $ip_cidr -le 32 ]; then
  665. ret_string="4"
  666. else
  667. ret_code=$(( $ret_code | 2 )) # BIT 1: invalid cidr for IPV4 format
  668. fi
  669. else
  670. ret_code=$(( $ret_code | 2 )) # BIT 1: invalid cidr for IPV4 format
  671. fi
  672. fi
  673. if [ -n "$ret_string" ]; then
  674. echo "$ret_string"
  675. return $ret_code
  676. fi
  677. # IPV6 check
  678. t_ipv6=$(echo $1 |awk -F / '{print $1}')
  679. t_prefixlen=$(echo $1 |awk -F / '{print $2}')
  680. valid_prefixlen=1
  681. if [[ "$object_name" != "prefix_length" ]] || [[ -n "$t_ipv6" ]]; then
  682. # check for ipv6 address format excluding "prefix length only" mode
  683. WORD="[0-9A-Fa-f]\{1,4\}"
  684. # flat address, no compressed words
  685. FLAT="^${WORD}\(:${WORD}\)\{7\}$"
  686. COMP2="^\(${WORD}:\)\{1,1\}\(:${WORD}\)\{1,6\}$"
  687. COMP3="^\(${WORD}:\)\{1,2\}\(:${WORD}\)\{1,5\}$"
  688. COMP4="^\(${WORD}:\)\{1,3\}\(:${WORD}\)\{1,4\}$"
  689. COMP5="^\(${WORD}:\)\{1,4\}\(:${WORD}\)\{1,3\}$"
  690. COMP6="^\(${WORD}:\)\{1,5\}\(:${WORD}\)\{1,2\}$"
  691. COMP7="^\(${WORD}:\)\{1,6\}\(:${WORD}\)\{1,1\}$"
  692. # trailing :: edge case, includes case of only :: (all 0's)
  693. EDGE_TAIL="^\(\(${WORD}:\)\{1,7\}\|:\):$"
  694. # leading :: edge case
  695. EDGE_LEAD="^:\(:${WORD}\)\{1,7\}$"
  696. echo $t_ipv6 | grep --silent "\(${FLAT}\)\|\(${COMP2}\)\|\(${COMP3}\)\|\(${COMP4}\)\|\(${COMP5}\)\|\(${COMP6}\)\|\(${COMP7}\)\|\(${EDGE_TAIL}\)\|\(${EDGE_LEAD}\)"
  697. if [ $? -ne 0 ]; then
  698. ret_code=$(( $ret_code | 4 )) # BIT 2: invalid IPV6 format
  699. echo "$ret_string"
  700. return $ret_code
  701. fi
  702. fi
  703. ret_string="6"
  704. # ipv6 prefix length checks
  705. if [ -n "$(echo $1|grep '/')" ]; then
  706. # introducing slash as prefix length attribute detected
  707. if [[ "$t_prefixlen" -lt 0 ]] || [[ "$t_prefixlen" -gt 128 ]]; then
  708. ret_code=$(( $ret_code | 8 )) # BIT 3: invalid prefix lenght for IPV6 format
  709. ret_string=""
  710. fi
  711. if ! [[ "$t_prefixlen" =~ ^[0-9]+$ ]]; then
  712. ret_code=$(( $ret_code | 8 )) # BIT 3: invalid prefix lenght for IPV6 format
  713. ret_string=""
  714. fi
  715. fi
  716. echo "$ret_string"
  717. [ -n "$ret_string" ] && return 0 || return $ret_code
  718. }
  719. # IP format validator
  720. is_ip_format_valid() {
  721. object_name=${2-ipv4}
  722. local ip_format=""
  723. local ret_code=0
  724. ip_format="$(get_ip_format ${1} ${object_name})"
  725. ret_code=$?
  726. if [ "$object_name" = "ipv6" ]; then
  727. ret_code=$(( $ret_code & 12 )) # Filter BIT 2 and 3 from error codes for IPV6 format
  728. if [ "$ip_format" = "6" ]; then
  729. return $ret_code
  730. else
  731. if [ "$ip_format" = "4" ]; then
  732. check_result "$E_INVALID" "ipv4 but not ipv6 format :: $1"
  733. return 12
  734. else
  735. check_result "$E_INVALID" "invalid $object_name format :: $1"
  736. return $ret_code
  737. fi
  738. fi
  739. else
  740. ret_code=$(( $ret_code & 3 )) # Filter BIT 0 and 1 from error codes for IPV4 format
  741. if [ "$ip_format" = "4" ]; then
  742. return $ret_code
  743. else
  744. if [ $ret_code -ne 0 ]; then
  745. check_result "$E_INVALID" "invalid $object_name format :: $1"
  746. return $ret_code
  747. else
  748. check_result "$E_INVALID" "ipv6 but not ipv4 format :: $1"
  749. return 3
  750. fi
  751. fi
  752. fi
  753. }
  754. # IPv6 format validator
  755. is_ipv6_format_valid() {
  756. object_name=${2-ipv6}
  757. local ip_format=""
  758. local ret_code=0
  759. ip_format="$(get_ip_format ${1} ${object_name})"
  760. ret_code=$(( $? & 12 )) # Filter BIT 2 and 3 from error codes for IPV6 format
  761. # ret_code=$?
  762. if [ "$ip_format" = "6" ]; then
  763. return $ret_code
  764. else
  765. if [ "$ip_format" = "4" ]; then
  766. check_result "$E_INVALID" "ipv4 but not ipv6 format :: $1"
  767. return 12
  768. else
  769. check_result "$E_INVALID" "invalid $object_name format :: $1"
  770. return $ret_code
  771. fi
  772. fi
  773. }
  774. is_ip46_format_valid() {
  775. t_ip=$(echo $1 | awk -F / '{print $1}')
  776. t_cidr=$(echo $1 | awk -F / '{print $2}')
  777. valid_octets=0
  778. valid_cidr=1
  779. for octet in ${t_ip//./ }; do
  780. if [[ $octet =~ ^[0-9]{1,3}$ ]] && [[ $octet -le 255 ]]; then
  781. ((++valid_octets))
  782. fi
  783. done
  784. if [ -n "$(echo $1 | grep '/')" ]; then
  785. if [[ "$t_cidr" -lt 0 ]] || [[ "$t_cidr" -gt 32 ]]; then
  786. valid_cidr=0
  787. fi
  788. if ! [[ "$t_cidr" =~ ^[0-9]+$ ]]; then
  789. valid_cidr=0
  790. fi
  791. fi
  792. if [ "$valid_octets" -lt 4 ] || [ "$valid_cidr" -eq 0 ]; then
  793. #Check IPV6
  794. ipv6_valid=""
  795. WORD="[0-9A-Fa-f]\{1,4\}"
  796. # flat address, no compressed words
  797. FLAT="^${WORD}\(:${WORD}\)\{7\}$"
  798. COMP2="^\(${WORD}:\)\{1,1\}\(:${WORD}\)\{1,6\}$"
  799. COMP3="^\(${WORD}:\)\{1,2\}\(:${WORD}\)\{1,5\}$"
  800. COMP4="^\(${WORD}:\)\{1,3\}\(:${WORD}\)\{1,4\}$"
  801. COMP5="^\(${WORD}:\)\{1,4\}\(:${WORD}\)\{1,3\}$"
  802. COMP6="^\(${WORD}:\)\{1,5\}\(:${WORD}\)\{1,2\}$"
  803. COMP7="^\(${WORD}:\)\{1,6\}\(:${WORD}\)\{1,1\}$"
  804. # trailing :: edge case, includes case of only :: (all 0's)
  805. EDGE_TAIL="^\(\(${WORD}:\)\{1,7\}\|:\):$"
  806. # leading :: edge case
  807. EDGE_LEAD="^:\(:${WORD}\)\{1,7\}$"
  808. echo $t_ip | grep --silent "\(${FLAT}\)\|\(${COMP2}\)\|\(${COMP3}\)\|\(${COMP4}\)\|\(${COMP5}\)\|\(${COMP6}\)\|\(${COMP7}\)\|\(${EDGE_TAIL}\)\|\(${EDGE_LEAD}\)"
  809. if [ $? -ne 0 ]; then
  810. ipv6_valid="INVALID"
  811. fi
  812. if [ -n "$(echo $1 | grep '/')" ]; then
  813. if [[ "$t_cidr" -lt 0 ]] || [[ "$t_cidr" -gt 128 ]]; then
  814. valid_cidr=0
  815. fi
  816. if ! [[ "$t_cidr" =~ ^[0-9]+$ ]]; then
  817. valid_cidr=0
  818. fi
  819. fi
  820. if [ -n "$ipv6_valid" ] || [ "$valid_cidr" -eq 0 ]; then
  821. check_result "$E_INVALID" "invalid IP format :: $1"
  822. fi
  823. fi
  824. }
  825. # Proxy extention format validator
  826. is_extention_format_valid() {
  827. exclude="[!|#|$|^|&|(|)|+|=|{|}|:|@|<|>|?|/|\|\"|'|;|%|\`| ]"
  828. if [[ "$1" =~ $exclude ]]; then
  829. check_result "$E_INVALID" "invalid proxy extention format :: $1"
  830. fi
  831. is_no_new_line_format "$1"
  832. }
  833. # Number format validator
  834. is_number_format_valid() {
  835. object_name=${2-number}
  836. if ! [[ "$1" =~ ^[0-9]+$ ]]; then
  837. check_result "$E_INVALID" "invalid $object_name format :: $1"
  838. fi
  839. }
  840. # Autoreply format validator
  841. is_autoreply_format_valid() {
  842. if [ 10240 -le ${#1} ]; then
  843. check_result "$E_INVALID" "invalid autoreply format :: $1"
  844. fi
  845. }
  846. # Boolean format validator
  847. is_boolean_format_valid() {
  848. if [ "$1" != 'yes' ] && [ "$1" != 'no' ]; then
  849. check_result "$E_INVALID" "invalid $2 format :: $1"
  850. fi
  851. }
  852. # Refresh IPset format validator
  853. is_refresh_ipset_format_valid() {
  854. if [ "$1" != 'load' ] && [ "$1" != 'yes' ] && [ "$1" != 'no' ]; then
  855. check_result "$E_INVALID" "invalid $2 format :: $1"
  856. fi
  857. }
  858. # Common format validator
  859. is_common_format_valid() {
  860. exclude="[!|#|$|^|&|(|)|+|=|{|}|:|<|>|?|/|\|\"|'|;|%|\`| ]"
  861. if [[ "$1" =~ $exclude ]]; then
  862. check_result "$E_INVALID" "invalid $2 format :: $1"
  863. fi
  864. if [ 400 -le ${#1} ]; then
  865. check_result "$E_INVALID" "invalid $2 format :: $1"
  866. fi
  867. if [[ "$1" =~ @ ]] && [ ${#1} -gt 1 ]; then
  868. check_result "$E_INVALID" "invalid $2 format :: $1"
  869. fi
  870. if [[ $1 =~ \* ]]; then
  871. if [[ "$(echo $1 | grep -o '\*\.' | wc -l)" -eq 0 ]] && [[ $1 != '*' ]]; then
  872. check_result "$E_INVALID" "invalid $2 format :: $1"
  873. fi
  874. fi
  875. if [[ $(echo -n "$1" | tail -c 1) =~ [^a-zA-Z0-9_*@.] ]]; then
  876. check_result "$E_INVALID" "invalid $2 format :: $1"
  877. fi
  878. if [[ $(echo -n "$1" | grep -c '\.\.') -gt 0 ]]; then
  879. check_result "$E_INVALID" "invalid $2 format :: $1"
  880. fi
  881. if [[ $(echo -n "$1" | head -c 1) =~ [^a-zA-Z0-9_*@] ]]; then
  882. check_result "$E_INVALID" "invalid $2 format :: $1"
  883. fi
  884. if [[ $(echo -n "$1" | grep -c '\-\-') -gt 0 ]]; then
  885. check_result "$E_INVALID" "invalid $2 format :: $1"
  886. fi
  887. if [[ $(echo -n "$1" | grep -c '\_\_') -gt 0 ]]; then
  888. check_result "$E_INVALID" "invalid $2 format :: $1"
  889. fi
  890. is_no_new_line_format "$1"
  891. }
  892. is_no_new_line_format() {
  893. test=$(echo "$1" | head -n1)
  894. if [[ "$test" != "$1" ]]; then
  895. check_result "$E_INVALID" "invalid value :: $1"
  896. fi
  897. }
  898. is_string_format_valid() {
  899. exclude="[!|#|$|^|&|(|)|+|=|{|}|:|<|>|?|/|\|\"|'|;|%|\`]"
  900. if [[ "$1" =~ $exclude ]]; then
  901. check_result "$E_INVALID" "invalid $2 format :: $1"
  902. fi
  903. is_no_new_line_format "$1"
  904. }
  905. # Database format validator
  906. is_database_format_valid() {
  907. exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|/|\|\"|'|;|%|\`| ]"
  908. if [[ "$1" =~ $exclude ]] || [ 64 -le ${#1} ]; then
  909. check_result "$E_INVALID" "invalid $2 format :: $1"
  910. fi
  911. is_no_new_line_format "$1"
  912. }
  913. # Date format validator
  914. is_date_format_valid() {
  915. if ! [[ "$1" =~ ^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$ ]]; then
  916. check_result "$E_INVALID" "invalid date format :: $1"
  917. fi
  918. }
  919. # Database user validator
  920. is_dbuser_format_valid() {
  921. exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|/|\|\"|'|;|%|\`| ]"
  922. if [ 33 -le ${#1} ]; then
  923. check_result "$E_INVALID" "mysql username can be up to 32 characters long"
  924. fi
  925. if [[ "$1" =~ $exclude ]]; then
  926. check_result "$E_INVALID" "invalid $2 format :: $1"
  927. fi
  928. is_no_new_line_format "$1"
  929. }
  930. # DNS record type validator
  931. is_dns_type_format_valid() {
  932. known_dnstype='A,AAAA,NS,CNAME,MX,TXT,SRV,DNSKEY,KEY,IPSECKEY,PTR,SPF,TLSA,CAA,DS'
  933. if [ -z "$(echo $known_dnstype | grep -w $1)" ]; then
  934. check_result "$E_INVALID" "invalid dns record type format :: $1"
  935. fi
  936. }
  937. # DNS record validator
  938. is_dns_record_format_valid() {
  939. if [ "$rtype" = 'A' ]; then
  940. is_ip_format_valid "$1"
  941. fi
  942. if [ "$rtype" = 'NS' ]; then
  943. is_domain_format_valid "${1::-1}" 'ns_record'
  944. fi
  945. if [ "$rtype" = 'MX' ]; then
  946. is_domain_format_valid "${1::-1}" 'mx_record'
  947. is_int_format_valid "$priority" 'priority_record'
  948. fi
  949. is_no_new_line_format "$1"
  950. }
  951. # Email format validator
  952. is_email_format_valid() {
  953. if [[ ! "$1" =~ ^[A-Za-z0-9._%+-]+@[[:alnum:].-]+\.[A-Za-z]{2,63}$ ]]; then
  954. if [[ ! "$1" =~ ^[A-Za-z0-9._%+-]+@[[:alnum:].-]+\.(xn--)[[:alnum:]]{2,63}$ ]]; then
  955. check_result "$E_INVALID" "invalid email format :: $1"
  956. fi
  957. fi
  958. }
  959. # Firewall action validator
  960. is_fw_action_format_valid() {
  961. if [ "$1" != "ACCEPT" ] && [ "$1" != 'DROP' ]; then
  962. check_result "$E_INVALID" "invalid action format :: $1"
  963. fi
  964. }
  965. # Firewall protocol validator
  966. is_fw_protocol_format_valid() {
  967. if [ "$1" != "ICMP" ] && [ "$1" != 'UDP' ] && [ "$1" != 'TCP' ]; then
  968. check_result "$E_INVALID" "invalid protocol format :: $1"
  969. fi
  970. }
  971. # Firewall port validator
  972. is_fw_port_format_valid() {
  973. if [ "${#1}" -eq 1 ]; then
  974. if ! [[ "$1" =~ [0-9] ]]; then
  975. check_result "$E_INVALID" "invalid port format :: $1"
  976. fi
  977. else
  978. if ! [[ "$1" =~ ^[0-9][-|,|:|0-9]{0,30}[0-9]$ ]]; then
  979. check_result "$E_INVALID" "invalid port format :: $1"
  980. fi
  981. fi
  982. }
  983. # Integer validator
  984. is_int_format_valid() {
  985. if ! [[ "$1" =~ ^[0-9]+$ ]]; then
  986. check_result "$E_INVALID" "invalid $2 format :: $1"
  987. fi
  988. }
  989. # Interface validator
  990. is_interface_format_valid() {
  991. netdevices=$(cat /proc/net/dev | grep : | cut -f 1 -d : | tr -d ' ')
  992. if [ -z $(echo "$netdevices" | grep -x ${1%:*}) ]; then
  993. check_result "$E_INVALID" "invalid interface format :: $1"
  994. fi
  995. }
  996. # IP status validator
  997. is_ip_status_format_valid() {
  998. if [ -z "$(echo shared,dedicated | grep -w $1)" ]; then
  999. check_result "$E_INVALID" "invalid status format :: $1"
  1000. fi
  1001. }
  1002. # Cron validator
  1003. is_cron_format_valid() {
  1004. limit=59
  1005. check_format=''
  1006. if [ "$2" = 'hour' ]; then
  1007. limit=23
  1008. fi
  1009. if [ "$2" = 'day' ]; then
  1010. limit=31
  1011. fi
  1012. if [ "$2" = 'month' ]; then
  1013. limit=12
  1014. fi
  1015. if [ "$2" = 'wday' ]; then
  1016. limit=7
  1017. fi
  1018. if [ "$1" = '*' ]; then
  1019. check_format='ok'
  1020. fi
  1021. if [[ "$1" =~ ^[\*]+[/]+[0-9] ]]; then
  1022. if [ "$(echo $1 | cut -f 2 -d /)" -lt $limit ]; then
  1023. check_format='ok'
  1024. fi
  1025. fi
  1026. if [[ "$1" =~ ^[0-9][-|,|0-9]{0,70}[\/][0-9]$ ]]; then
  1027. check_format='ok'
  1028. crn_values=${1//,/ }
  1029. crn_values=${crn_values//-/ }
  1030. crn_values=${crn_values//\// }
  1031. for crn_vl in $crn_values; do
  1032. if [ "$crn_vl" -gt $limit ]; then
  1033. check_format='invalid'
  1034. fi
  1035. done
  1036. fi
  1037. crn_values=$(echo $1 | tr "," " " | tr "-" " ")
  1038. for crn_vl in $crn_values; do
  1039. if [[ "$crn_vl" =~ ^[0-9]+$ ]] && [ "$crn_vl" -le $limit ]; then
  1040. check_format='ok'
  1041. fi
  1042. done
  1043. if [ "$check_format" != 'ok' ]; then
  1044. check_result "$E_INVALID" "invalid $2 format :: $1"
  1045. fi
  1046. }
  1047. is_object_name_format_valid() {
  1048. if ! [[ "$1" =~ ^[-|\ |\.|_[:alnum:]]{0,50}$ ]]; then
  1049. check_result "$E_INVALID" "invalid $2 format :: $1"
  1050. fi
  1051. }
  1052. # Name validator
  1053. is_name_format_valid() {
  1054. exclude="['|\"|<|>]"
  1055. if [[ "$1" =~ $exclude ]]; then
  1056. check_result "$E_INVALID" "Invalid $2 contains qoutes (\" or ') :: $1"
  1057. fi
  1058. is_no_new_line_format "$1"
  1059. }
  1060. # Object validator
  1061. is_object_format_valid() {
  1062. if ! [[ "$1" =~ ^[[:alnum:]][-|\.|_[:alnum:]]{0,64}[[:alnum:]]$ ]]; then
  1063. check_result "$E_INVALID" "invalid $2 format :: $1"
  1064. fi
  1065. }
  1066. # Role validator
  1067. is_role_valid() {
  1068. if ! [[ "$1" =~ ^admin$|^user$|^dns-cluster$ ]]; then
  1069. check_result "$E_INVALID" "invalid $2 format :: $1"
  1070. fi
  1071. }
  1072. # Password validator
  1073. is_password_format_valid() {
  1074. if [ "${#1}" -lt '6' ]; then
  1075. check_result "$E_INVALID" "invalid password format :: $1"
  1076. fi
  1077. }
  1078. # Missing function -
  1079. # Before: validate_format_shell
  1080. # After: is_format_valid_shell
  1081. is_format_valid_shell() {
  1082. if [ -z "$(grep -w $1 /etc/shells)" ]; then
  1083. echo "Error: shell $1 is not valid"
  1084. log_event "$E_INVALID" "$EVENT"
  1085. exit $E_INVALID
  1086. fi
  1087. }
  1088. # Service name validator
  1089. is_service_format_valid() {
  1090. if ! [[ "$1" =~ ^[[:alnum:]][-|\.|_[:alnum:]]{0,64}$ ]]; then
  1091. check_result "$E_INVALID" "invalid $2 format :: $1"
  1092. fi
  1093. }
  1094. is_hash_format_valid() {
  1095. if ! [[ "$1" =~ ^[[:alnum:]|\:|\=|_|-]{1,80}$ ]]; then
  1096. check_result "$E_INVALID" "invalid $2 format :: $1"
  1097. fi
  1098. }
  1099. # Format validation controller
  1100. is_format_valid() {
  1101. for arg_name in $*; do
  1102. eval arg=\$$arg_name
  1103. if [ -n "$arg" ]; then
  1104. case $arg_name in
  1105. access_key_id) is_access_key_id_format_valid "$arg" "$arg_name" ;;
  1106. account) is_user_format_valid "$arg" "$arg_name" '64' ;;
  1107. action) is_fw_action_format_valid "$arg" ;;
  1108. active) is_boolean_format_valid "$arg" 'active' ;;
  1109. aliases) is_alias_format_valid "$arg" ;;
  1110. alias) is_alias_format_valid "$arg" ;;
  1111. antispam) is_boolean_format_valid "$arg" 'antispam' ;;
  1112. antivirus) is_boolean_format_valid "$arg" 'antivirus' ;;
  1113. autoreply) is_autoreply_format_valid "$arg" ;;
  1114. backup) is_object_format_valid "$arg" 'backup' ;;
  1115. charset) is_object_format_valid "$arg" "$arg_name" ;;
  1116. charsets) is_common_format_valid "$arg" 'charsets' ;;
  1117. chain) is_object_format_valid "$arg" 'chain' ;;
  1118. cidr) is_ip_format_valid "$arg" 'cidr' ;;
  1119. comment) is_object_format_valid "$arg" 'comment' ;;
  1120. database) is_database_format_valid "$arg" 'database' ;;
  1121. day) is_cron_format_valid "$arg" $arg_name ;;
  1122. dbpass) is_password_format_valid "$arg" ;;
  1123. dbuser) is_dbuser_format_valid "$arg" 'dbuser' ;;
  1124. dkim) is_boolean_format_valid "$arg" 'dkim' ;;
  1125. dkim_size) is_int_format_valid "$arg" ;;
  1126. domain) is_domain_format_valid "$arg" ;;
  1127. dom_alias) is_alias_format_valid "$arg" ;;
  1128. dvalue) is_dns_record_format_valid "$arg" ;;
  1129. email) is_email_format_valid "$arg" ;;
  1130. email_forward) is_email_format_valid "$arg" ;;
  1131. exp) is_date_format_valid "$arg" ;;
  1132. extentions) is_common_format_valid "$arg" 'extentions' ;;
  1133. format) is_type_valid 'plain json shell csv' "$arg" ;;
  1134. ftp_password) is_password_format_valid "$arg" ;;
  1135. ftp_user) is_user_format_valid "$arg" "$arg_name" ;;
  1136. hash) is_hash_format_valid "$arg" "$arg_name" ;;
  1137. host) is_object_format_valid "$arg" "$arg_name" ;;
  1138. hour) is_cron_format_valid "$arg" $arg_name ;;
  1139. id) is_int_format_valid "$arg" 'id' ;;
  1140. iface) is_interface_format_valid "$arg" ;;
  1141. ip) is_ip_format_valid "$arg" ;;
  1142. ipv6) is_ipv6_format_valid "$arg" ;;
  1143. ip46) is_ip46_format_valid "$arg" ;;
  1144. ip_name) is_domain_format_valid "$arg" 'IP name' ;;
  1145. ip_status) is_ip_status_format_valid "$arg" ;;
  1146. job) is_int_format_valid "$arg" 'job' ;;
  1147. key) is_common_format_valid "$arg" "$arg_name" ;;
  1148. malias) is_user_format_valid "$arg" "$arg_name" '64' ;;
  1149. max_db) is_int_format_valid "$arg" 'max db' ;;
  1150. min) is_cron_format_valid "$arg" $arg_name ;;
  1151. month) is_cron_format_valid "$arg" $arg_name ;;
  1152. name) is_name_format_valid "$arg" "name" ;;
  1153. nat_ip) is_ip_format_valid "$arg" ;;
  1154. netmask) is_ip_format_valid "$arg" 'netmask' ;;
  1155. newid) is_int_format_valid "$arg" 'id' ;;
  1156. ns1) is_domain_format_valid "$arg" 'ns1' ;;
  1157. ns2) is_domain_format_valid "$arg" 'ns2' ;;
  1158. ns3) is_domain_format_valid "$arg" 'ns3' ;;
  1159. ns4) is_domain_format_valid "$arg" 'ns4' ;;
  1160. ns5) is_domain_format_valid "$arg" 'ns5' ;;
  1161. ns6) is_domain_format_valid "$arg" 'ns6' ;;
  1162. ns7) is_domain_format_valid "$arg" 'ns7' ;;
  1163. ns8) is_domain_format_valid "$arg" 'ns8' ;;
  1164. object) is_object_name_format_valid "$arg" 'object' ;;
  1165. package) is_object_format_valid "$arg" "$arg_name" ;;
  1166. password) is_password_format_valid "$arg" ;;
  1167. port) is_int_format_valid "$arg" 'port' ;;
  1168. port_ext) is_fw_port_format_valid "$arg" ;;
  1169. prefix_length) is_ipv6_format_valid "$arg" 'prefix_length' ;;
  1170. protocol) is_fw_protocol_format_valid "$arg" ;;
  1171. proxy_ext) is_extention_format_valid "$arg" ;;
  1172. quota) is_int_format_valid "$arg" 'quota' ;;
  1173. rate) is_int_format_valid "$arg" 'rate' ;;
  1174. record) is_common_format_valid "$arg" 'record' ;;
  1175. reject) is_boolean_format_valid "$arg" 'reject' ;;
  1176. restart) is_restart_format_valid "$arg" 'restart' ;;
  1177. role) is_role_valid "$arg" 'role' ;;
  1178. rtype) is_dns_type_format_valid "$arg" ;;
  1179. rule) is_int_format_valid "$arg" "rule id" ;;
  1180. service) is_service_format_valid "$arg" "$arg_name" ;;
  1181. secret_access_key) is_secret_access_key_format_valid "$arg" "$arg_name" ;;
  1182. soa) is_domain_format_valid "$arg" 'SOA' ;;
  1183. #missing command: is_format_valid_shell
  1184. shell) is_format_valid_shell "$arg" ;;
  1185. ssl_dir) is_folder_exists "$arg" "$arg_name" ;;
  1186. stats_pass) is_password_format_valid "$arg" ;;
  1187. stats_user) is_user_format_valid "$arg" "$arg_name" ;;
  1188. template) is_object_format_valid "$arg" "$arg_name" ;;
  1189. theme) is_common_format_valid "$arg" "$arg_name" ;;
  1190. ttl) is_int_format_valid "$arg" 'ttl' ;;
  1191. user) is_user_format_valid "$arg" $arg_name ;;
  1192. wday) is_cron_format_valid "$arg" $arg_name ;;
  1193. value) is_common_format_valid "$arg" $arg_name ;;
  1194. esac
  1195. fi
  1196. done
  1197. }
  1198. is_folder_exists() {
  1199. if [ ! -d "$1" ]; then
  1200. check_result "$E_NOTEXIST" "folder $1 does not exist"
  1201. fi
  1202. }
  1203. is_command_valid_format() {
  1204. if [[ ! "$1" =~ ^v-[[:alnum:]][-|\.|_[:alnum:]]{0,64}[[:alnum:]]$ ]]; then
  1205. check_result "$E_INVALID" "Invalid command format"
  1206. fi
  1207. if [[ -n $(echo "$1" | grep -e '\-\-') ]]; then
  1208. check_result "$E_INVALID" "Invalid command format"
  1209. fi
  1210. }
  1211. # Check access_key_id name
  1212. # Don't work with legacy key format
  1213. is_access_key_id_format_valid() {
  1214. local hash="$1"
  1215. # ACCESS_KEY_ID format validation
  1216. if ! [[ "$hash" =~ ^[[:alnum:]]{20}$ ]]; then
  1217. check_result "$E_INVALID" "invalid $2 format :: $hash"
  1218. fi
  1219. }
  1220. # SECRET_ACCESS_KEY format validation
  1221. is_secret_access_key_format_valid() {
  1222. local hash="$1"
  1223. if ! [[ "$hash" =~ ^[[:alnum:]|_|\.|\+|/|\^|~|=|%|\-]{40}$ ]]; then
  1224. check_result "$E_INVALID" "invalid $2 format"
  1225. fi
  1226. }
  1227. # Checks if the secret belongs to the access key
  1228. check_access_key_secret() {
  1229. local access_key_id="$(basename "$1")"
  1230. local secret_access_key=$2
  1231. local -n key_user=$3
  1232. if [[ -z "$access_key_id" || ! -f "$HESTIA/data/access-keys/${access_key_id}" ]]; then
  1233. check_result "$E_PASSWORD" "Access key $access_key_id doesn't exist"
  1234. fi
  1235. if [[ -z "$secret_access_key" ]]; then
  1236. check_result "$E_PASSWORD" "Secret key not provided for key $access_key_id"
  1237. elif ! [[ "$secret_access_key" =~ ^[[:alnum:]|_|\.|\+|/|\^|~|=|%|\-]{40}$ ]]; then
  1238. check_result "$E_PASSWORD" "Invalid secret key for key $access_key_id"
  1239. else
  1240. SECRET_ACCESS_KEY=""
  1241. source_conf "$HESTIA/data/access-keys/${access_key_id}"
  1242. if [[ -z "$SECRET_ACCESS_KEY" || "$SECRET_ACCESS_KEY" != "$secret_access_key" ]]; then
  1243. check_result "$E_PASSWORD" "Invalid secret key for key $access_key_id"
  1244. fi
  1245. fi
  1246. key_user="$USER"
  1247. }
  1248. # Checks if the key belongs to the user
  1249. check_access_key_user() {
  1250. local access_key_id="$(basename "$1")"
  1251. local user=$2
  1252. if [[ -z "$access_key_id" || ! -f "$HESTIA/data/access-keys/${access_key_id}" ]]; then
  1253. check_result "$E_FORBIDEN" "Access key $access_key_id doesn't exist"
  1254. fi
  1255. if [[ -z "$user" ]]; then
  1256. check_result "$E_FORBIDEN" "User not provided"
  1257. else
  1258. USER=""
  1259. source_conf "$HESTIA/data/access-keys/${access_key_id}"
  1260. if [[ -z "$USER" || "$USER" != "$user" ]]; then
  1261. check_result "$E_FORBIDEN" "key $access_key_id does not belong to the user $user"
  1262. fi
  1263. fi
  1264. }
  1265. # Checks if the key is allowed to run the command
  1266. check_access_key_cmd() {
  1267. local access_key_id="$(basename "$1")"
  1268. local cmd=$2
  1269. local -n user_arg_position=$3
  1270. if [[ "$DEBUG_MODE" = "true" ]]; then
  1271. new_timestamp
  1272. echo "[$date:$time] $1 $2" >> /var/log/hestia/api.log
  1273. fi
  1274. if [[ -z "$access_key_id" || ! -f "$HESTIA/data/access-keys/${access_key_id}" ]]; then
  1275. check_result "$E_FORBIDEN" "Access key $access_key_id doesn't exist"
  1276. fi
  1277. if [[ -z "$cmd" ]]; then
  1278. check_result "$E_FORBIDEN" "Command not provided"
  1279. elif [[ "$cmd" = 'v-make-tmp-file' ]]; then
  1280. USER="" PERMISSIONS=""
  1281. source_conf "${HESTIA}/data/access-keys/${access_key_id}"
  1282. local allowed_commands
  1283. if [[ -n "$PERMISSIONS" ]]; then
  1284. allowed_commands="$(get_apis_commands "$PERMISSIONS")"
  1285. if [[ -z "$(echo ",${allowed_commands}," | grep ",${hst_command},")" ]]; then
  1286. check_result "$E_FORBIDEN" "Key $access_key_id don't have permission to run the command $hst_command"
  1287. fi
  1288. elif [[ -z "$PERMISSIONS" && "$USER" != "admin" ]]; then
  1289. check_result "$E_FORBIDEN" "Key $access_key_id don't have permission to run the command $hst_command"
  1290. fi
  1291. user_arg_position="0"
  1292. elif [[ ! -e "$BIN/$cmd" ]]; then
  1293. check_result "$E_FORBIDEN" "Command $cmd not found"
  1294. else
  1295. USER="" PERMISSIONS=""
  1296. source_conf "${HESTIA}/data/access-keys/${access_key_id}"
  1297. local allowed_commands
  1298. if [[ -n "$PERMISSIONS" ]]; then
  1299. allowed_commands="$(get_apis_commands "$PERMISSIONS")"
  1300. if [[ -z "$(echo ",${allowed_commands}," | grep ",${hst_command},")" ]]; then
  1301. check_result "$E_FORBIDEN" "Key $access_key_id don't have permission to run the command $hst_command"
  1302. fi
  1303. elif [[ -z "$PERMISSIONS" && "$USER" != "admin" ]]; then
  1304. check_result "$E_FORBIDEN" "Key $access_key_id don't have permission to run the command $hst_command"
  1305. fi
  1306. if [[ "$USER" == "admin" ]]; then
  1307. # Admin can run commands for any user
  1308. user_arg_position="0"
  1309. else
  1310. user_arg_position="$(search_command_arg_position "$hst_command" "USER")"
  1311. if ! [[ "$user_arg_position" =~ ^[0-9]+$ ]]; then
  1312. check_result "$E_FORBIDEN" "Command $hst_command not found"
  1313. fi
  1314. fi
  1315. fi
  1316. }
  1317. # Domain argument formatting
  1318. format_domain() {
  1319. if [[ "$domain" = *[![:ascii:]]* ]]; then
  1320. if [[ "$domain" =~ [[:upper:]] ]]; then
  1321. domain=$(echo "$domain" | sed 's/[[:upper:]].*/\L&/')
  1322. fi
  1323. else
  1324. if [[ "$domain" =~ [[:upper:]] ]]; then
  1325. domain=$(echo "$domain" | tr '[:upper:]' '[:lower:]')
  1326. fi
  1327. fi
  1328. if [[ "$domain" =~ ^www\..* ]]; then
  1329. domain=$(echo "$domain" | sed -e "s/^www.//")
  1330. fi
  1331. if [[ "$domain" =~ .*\.$ ]]; then
  1332. domain=$(echo "$domain" | sed -e "s/[.]*$//g")
  1333. fi
  1334. if [[ "$domain" =~ ^\. ]]; then
  1335. domain=$(echo "$domain" | sed -e "s/^[.]*//")
  1336. fi
  1337. }
  1338. format_domain_idn() {
  1339. if [ -z "$domain_idn" ]; then
  1340. domain_idn=$domain
  1341. fi
  1342. if [[ "$domain_idn" = *[![:ascii:]]* ]]; then
  1343. domain_idn=$(idn2 --quiet $domain_idn)
  1344. fi
  1345. }
  1346. format_aliases() {
  1347. if [ -n "$aliases" ] && [ "$aliases" != 'none' ]; then
  1348. aliases=$(echo $aliases | tr '[:upper:]' '[:lower:]' | tr ',' '\n')
  1349. aliases=$(echo "$aliases" | sed -e "s/\.$//" | sort -u)
  1350. aliases=$(echo "$aliases" | tr -s '.')
  1351. aliases=$(echo "$aliases" | sed -e "s/[.]*$//g")
  1352. aliases=$(echo "$aliases" | sed -e "s/^[.]*//")
  1353. aliases=$(echo "$aliases" | sed -e "/^$/d")
  1354. aliases=$(echo "$aliases" | tr '\n' ',' | sed -e "s/,$//")
  1355. fi
  1356. }
  1357. is_restart_format_valid() {
  1358. if [ "$1" != 'yes' ] && [ "$1" != 'no' ] && [ "$1" != 'ssl' ] && [ "$1" != 'reload' ] && [ "$1" != 'updatessl' ]; then
  1359. check_result "$E_INVALID" "invalid $2 format :: $1"
  1360. fi
  1361. }
  1362. check_backup_conditions() {
  1363. # Checking load average
  1364. la=$(cat /proc/loadavg | cut -f 1 -d ' ' | cut -f 1 -d '.')
  1365. # i=0
  1366. while [ "$la" -ge "$BACKUP_LA_LIMIT" ]; do
  1367. echo -e "$(date "+%F %T") Load Average $la"
  1368. sleep 60
  1369. la=$(cat /proc/loadavg | cut -f 1 -d ' ' | cut -f 1 -d '.')
  1370. done
  1371. }
  1372. # Define download function
  1373. download_file() {
  1374. local url=$1
  1375. local destination=$2
  1376. local force=$3
  1377. # Default destination is the curent working directory
  1378. local dstopt=""
  1379. if [ -n "$(echo "$url" | grep -E "\.(gz|gzip|bz2|zip|xz)$")" ]; then
  1380. # When an archive file is downloaded it will be first saved localy
  1381. dstopt="--directory-prefix=$ARCHIVE_DIR"
  1382. local is_archive="true"
  1383. local filename="${url##*/}"
  1384. if [ -z "$filename" ]; then
  1385. echo >&2 "[!] No filename was found in url, exiting ($url)"
  1386. exit 1
  1387. fi
  1388. if [ -n "$force" ] && [ -f "$ARCHIVE_DIR/$filename" ]; then
  1389. rm -f $ARCHIVE_DIR/$filename
  1390. fi
  1391. elif [ -n "$destination" ]; then
  1392. # Plain files will be written to specified location
  1393. dstopt="-O $destination"
  1394. fi
  1395. # check for corrupted archive
  1396. if [ -f "$ARCHIVE_DIR/$filename" ] && [ "$is_archive" = "true" ]; then
  1397. tar -tzf "$ARCHIVE_DIR/$filename" > /dev/null 2>&1
  1398. if [ $? -ne 0 ]; then
  1399. echo >&2 "[!] Archive $ARCHIVE_DIR/$filename is corrupted, redownloading"
  1400. rm -f $ARCHIVE_DIR/$filename
  1401. fi
  1402. fi
  1403. if [ ! -f "$ARCHIVE_DIR/$filename" ]; then
  1404. wget $url -q $dstopt --show-progress --progress=bar:force --limit-rate=3m
  1405. fi
  1406. if [ -n "$destination" ] && [ "$is_archive" = "true" ]; then
  1407. if [ "$destination" = "-" ]; then
  1408. cat "$ARCHIVE_DIR/$filename"
  1409. elif [ -d "$(dirname $destination)" ]; then
  1410. cp "$ARCHIVE_DIR/$filename" "$destination"
  1411. fi
  1412. fi
  1413. }
  1414. check_hestia_demo_mode() {
  1415. demo_mode=$(grep DEMO_MODE /usr/local/hestia/conf/hestia.conf | cut -d '=' -f2 | sed "s|'||g")
  1416. if [ -n "$demo_mode" ] && [ "$demo_mode" = "yes" ]; then
  1417. echo "ERROR: Unable to perform operation due to security restrictions that are in place."
  1418. exit 1
  1419. fi
  1420. }
  1421. multiphp_count() {
  1422. $BIN/v-list-sys-php plain | wc -l
  1423. }
  1424. multiphp_versions() {
  1425. local -a php_versions_list
  1426. local php_ver
  1427. if [ "$(multiphp_count)" -gt 0 ]; then
  1428. for php_ver in $($BIN/v-list-sys-php plain); do
  1429. [ ! -d "/etc/php/$php_ver/fpm/pool.d/" ] && continue
  1430. php_versions_list+=($php_ver)
  1431. done
  1432. echo "${php_versions_list[@]}"
  1433. fi
  1434. }
  1435. multiphp_default_version() {
  1436. # Get system wide default php version (set by update-alternatives)
  1437. local sys_phpversion=$(php -r "echo substr(phpversion(),0,3);")
  1438. # Check if the system php also has php-fpm enabled, otherwise return
  1439. # the most recent php version which does have it installed.
  1440. if [ ! -d "/etc/php/$sys_phpversion/fpm/pool.d/" ]; then
  1441. local all_versions="$(multiphp_versions)"
  1442. if [ -n "$all_versions" ]; then
  1443. sys_phpversion="${all_versions##*\ }"
  1444. fi
  1445. fi
  1446. echo "$sys_phpversion"
  1447. }
  1448. is_hestia_package() {
  1449. check=false
  1450. for pkg in $1; do
  1451. if [ "$pkg" == "$2" ]; then
  1452. check="true"
  1453. fi
  1454. done
  1455. if [ "$check" != "true" ]; then
  1456. check_result $E_INVALID "$2 package is not controlled by hestiacp"
  1457. fi
  1458. }
  1459. # Run arbitrary cli commands with dropped privileges
  1460. # Note: setpriv --init-groups is not available on debian9 (util-linux 2.29.2)
  1461. # Input:
  1462. # - $user : Vaild hestia user
  1463. user_exec() {
  1464. is_object_valid 'user' 'USER' "$user"
  1465. local user_groups=$(id -G "$user")
  1466. user_groups=${user_groups//\ /,}
  1467. setpriv --groups "$user_groups" --reuid "$user" --regid "$user" -- "${@}"
  1468. }
  1469. # Simple chmod wrapper that skips symlink files after glob expand
  1470. no_symlink_chmod() {
  1471. local filemode=$1
  1472. shift
  1473. for i in "$@"; do
  1474. [[ -L ${i} ]] && continue
  1475. chmod "${filemode}" "${i}"
  1476. done
  1477. }
  1478. source_conf() {
  1479. while IFS='= ' read -r lhs rhs; do
  1480. if [[ ! $lhs =~ ^\ *# && -n $lhs ]]; then
  1481. rhs="${rhs%%^\#*}" # Del in line right comments
  1482. rhs="${rhs%%*( )}" # Del trailing spaces
  1483. rhs="${rhs%\'*}" # Del opening string quotes
  1484. rhs="${rhs#\'*}" # Del closing string quotes
  1485. declare -g $lhs="$rhs"
  1486. fi
  1487. done < $1
  1488. }
  1489. format_no_quotes() {
  1490. exclude="['|\"]"
  1491. if [[ "$1" =~ $exclude ]]; then
  1492. check_result "$E_INVALID" "Invalid $2 contains qoutes (\" or ') :: $1"
  1493. fi
  1494. is_no_new_line_format "$1"
  1495. }
  1496. is_username_format_valid() {
  1497. if [[ ! "$1" =~ ^[A-Za-z0-9._%+-]+@[[:alnum:].-]+\.[A-Za-z]{2,63}$ ]]; then
  1498. is_string_format_valid "$1" "$2"
  1499. fi
  1500. }
  1501. change_sys_value() {
  1502. check_ckey=$(grep "^$1='" "$HESTIA/conf/hestia.conf")
  1503. if [ -z "$check_ckey" ]; then
  1504. echo "$1='$2'" >> "$HESTIA/conf/hestia.conf"
  1505. else
  1506. sed -i "s|^$1=.*|$1='$2'|g" "$HESTIA/conf/hestia.conf"
  1507. fi
  1508. }
  1509. # Checks the format of APIs that will be allowed for the key
  1510. is_key_permissions_format_valid() {
  1511. local permissions="$1"
  1512. local user="$2"
  1513. if [[ "$user" != "admin" && -z "$permissions" ]]; then
  1514. check_result "$E_INVALID" "Non-admin users need a permission list"
  1515. fi
  1516. while IFS=',' read -ra permissions_arr; do
  1517. for permission in "${permissions_arr[@]}"; do
  1518. permission="$(basename "$permission" | sed -E "s/^\s*|\s*$//g")"
  1519. # if [[ -z "$(echo "$permission" | grep -E "^v-")" ]]; then
  1520. if [[ ! -e "$HESTIA/data/api/$permission" ]]; then
  1521. check_result "$E_NOTEXIST" "API $permission doesn't exist"
  1522. fi
  1523. source_conf "$HESTIA/data/api/$permission"
  1524. if [ "$ROLE" = "admin" ] && [ "$user" != "admin" ]; then
  1525. check_result "$E_INVALID" "Only the admin can run this API"
  1526. fi
  1527. # elif [[ ! -e "$BIN/$permission" ]]; then
  1528. # check_result "$E_NOTEXIST" "Command $permission doesn't exist"
  1529. # fi
  1530. done
  1531. done <<< "$permissions"
  1532. }
  1533. # Remove whitespaces, and bin path from commands
  1534. cleanup_key_permissions() {
  1535. local permissions="$1"
  1536. local final quote
  1537. while IFS=',' read -ra permissions_arr; do
  1538. for permission in "${permissions_arr[@]}"; do
  1539. permission="$(basename "$permission" | sed -E "s/^\s*|\s*$//g")"
  1540. # Avoid duplicate items
  1541. if [[ -z "$(echo ",${final}," | grep ",${permission},")" ]]; then
  1542. final+="${quote}${permission}"
  1543. quote=','
  1544. fi
  1545. done
  1546. done <<< "$permissions"
  1547. echo "$final"
  1548. }
  1549. # Extract all allowed commands from a permission list
  1550. get_apis_commands() {
  1551. local permissions="$1"
  1552. local allowed_commands quote commands_to_add
  1553. while IFS=',' read -ra permissions_arr; do
  1554. for permission in "${permissions_arr[@]}"; do
  1555. permission="$(basename "$permission" | sed -E "s/^\s*|\s*$//g")"
  1556. commands_to_add=""
  1557. # if [[ -n "$(echo "$permission" | grep -E "^v-")" ]]; then
  1558. # commands_to_add="$permission"
  1559. # el
  1560. if [[ -e "$HESTIA/data/api/$permission" ]]; then
  1561. source_conf "$HESTIA/data/api/$permission"
  1562. commands_to_add="$COMMANDS"
  1563. fi
  1564. if [[ -n "$commands_to_add" ]]; then
  1565. allowed_commands+="${quote}${commands_to_add}"
  1566. quote=','
  1567. fi
  1568. done
  1569. done <<< "$permissions"
  1570. cleanup_key_permissions "$allowed_commands"
  1571. }
  1572. # Get the position of an argument by name in a hestia command using the command's documentation comment.
  1573. #
  1574. # Return:
  1575. # * 0: It doesn't have the argument;
  1576. # * 1-9: The position of the argument in the command.
  1577. search_command_arg_position() {
  1578. local hst_command="$(basename "$1")"
  1579. local arg_name="$2"
  1580. local command_path="$BIN/$hst_command"
  1581. if [[ -z "$hst_command" || ! -e "$command_path" ]]; then
  1582. echo "-1"
  1583. return
  1584. fi
  1585. local position=0
  1586. local count=0
  1587. local command_options="$(sed -En 's/^# options: (.+)/\1/p' "$command_path")"
  1588. while IFS=' ' read -ra options_arr; do
  1589. for option in "${options_arr[@]}"; do
  1590. count=$((count + 1))
  1591. option_name="$(echo " $option " | sed -E 's/^(\s|\[)*|(\s|\])*$//g')"
  1592. if [[ "${option_name^^}" == "$arg_name" ]]; then
  1593. position=$count
  1594. fi
  1595. done
  1596. done <<< "$command_options"
  1597. echo "$position"
  1598. }