main.sh 48 KB

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