main.sh 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768
  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=$(( $? & 3 )) # Filter BIT 0 and 1 from error codes for IPV4 format
  726. if [ "$ip_format" = "4" ]; then
  727. return $ret_code
  728. else
  729. if [ $ret_code -ne 0 ]; then
  730. check_result "$E_INVALID" "invalid $object_name format :: $1"
  731. return $ret_code
  732. else
  733. check_result "$E_INVALID" "ipv6 but not ipv4 format :: $1"
  734. return 3
  735. fi
  736. fi
  737. }
  738. # IPv6 format validator
  739. is_ipv6_format_valid() {
  740. object_name=${2-ipv6}
  741. local ip_format=""
  742. local ret_code=0
  743. ip_format="$(get_ip_format ${1} ${object_name})"
  744. ret_code=$(( $? & 12 )) # Filter BIT 2 and 3 from error codes for IPV6 format
  745. # ret_code=$?
  746. if [ "$ip_format" = "6" ]; then
  747. return $ret_code
  748. else
  749. if [ "$ip_format" = "4" ]; then
  750. check_result "$E_INVALID" "ipv4 but not ipv6 format :: $1"
  751. return 12
  752. else
  753. check_result "$E_INVALID" "invalid $object_name format :: $1"
  754. return $ret_code
  755. fi
  756. fi
  757. }
  758. is_ip46_format_valid() {
  759. t_ip=$(echo $1 | awk -F / '{print $1}')
  760. t_cidr=$(echo $1 | awk -F / '{print $2}')
  761. valid_octets=0
  762. valid_cidr=1
  763. for octet in ${t_ip//./ }; do
  764. if [[ $octet =~ ^[0-9]{1,3}$ ]] && [[ $octet -le 255 ]]; then
  765. ((++valid_octets))
  766. fi
  767. done
  768. if [ -n "$(echo $1 | grep '/')" ]; then
  769. if [[ "$t_cidr" -lt 0 ]] || [[ "$t_cidr" -gt 32 ]]; then
  770. valid_cidr=0
  771. fi
  772. if ! [[ "$t_cidr" =~ ^[0-9]+$ ]]; then
  773. valid_cidr=0
  774. fi
  775. fi
  776. if [ "$valid_octets" -lt 4 ] || [ "$valid_cidr" -eq 0 ]; then
  777. #Check IPV6
  778. ipv6_valid=""
  779. WORD="[0-9A-Fa-f]\{1,4\}"
  780. # flat address, no compressed words
  781. FLAT="^${WORD}\(:${WORD}\)\{7\}$"
  782. COMP2="^\(${WORD}:\)\{1,1\}\(:${WORD}\)\{1,6\}$"
  783. COMP3="^\(${WORD}:\)\{1,2\}\(:${WORD}\)\{1,5\}$"
  784. COMP4="^\(${WORD}:\)\{1,3\}\(:${WORD}\)\{1,4\}$"
  785. COMP5="^\(${WORD}:\)\{1,4\}\(:${WORD}\)\{1,3\}$"
  786. COMP6="^\(${WORD}:\)\{1,5\}\(:${WORD}\)\{1,2\}$"
  787. COMP7="^\(${WORD}:\)\{1,6\}\(:${WORD}\)\{1,1\}$"
  788. # trailing :: edge case, includes case of only :: (all 0's)
  789. EDGE_TAIL="^\(\(${WORD}:\)\{1,7\}\|:\):$"
  790. # leading :: edge case
  791. EDGE_LEAD="^:\(:${WORD}\)\{1,7\}$"
  792. echo $t_ip | grep --silent "\(${FLAT}\)\|\(${COMP2}\)\|\(${COMP3}\)\|\(${COMP4}\)\|\(${COMP5}\)\|\(${COMP6}\)\|\(${COMP7}\)\|\(${EDGE_TAIL}\)\|\(${EDGE_LEAD}\)"
  793. if [ $? -ne 0 ]; then
  794. ipv6_valid="INVALID"
  795. fi
  796. if [ -n "$(echo $1 | grep '/')" ]; then
  797. if [[ "$t_cidr" -lt 0 ]] || [[ "$t_cidr" -gt 128 ]]; then
  798. valid_cidr=0
  799. fi
  800. if ! [[ "$t_cidr" =~ ^[0-9]+$ ]]; then
  801. valid_cidr=0
  802. fi
  803. fi
  804. if [ -n "$ipv6_valid" ] || [ "$valid_cidr" -eq 0 ]; then
  805. check_result "$E_INVALID" "invalid IP format :: $1"
  806. fi
  807. fi
  808. }
  809. # Proxy extention format validator
  810. is_extention_format_valid() {
  811. exclude="[!|#|$|^|&|(|)|+|=|{|}|:|@|<|>|?|/|\|\"|'|;|%|\`| ]"
  812. if [[ "$1" =~ $exclude ]]; then
  813. check_result "$E_INVALID" "invalid proxy extention format :: $1"
  814. fi
  815. is_no_new_line_format "$1"
  816. }
  817. # Number format validator
  818. is_number_format_valid() {
  819. object_name=${2-number}
  820. if ! [[ "$1" =~ ^[0-9]+$ ]]; then
  821. check_result "$E_INVALID" "invalid $object_name format :: $1"
  822. fi
  823. }
  824. # Autoreply format validator
  825. is_autoreply_format_valid() {
  826. if [ 10240 -le ${#1} ]; then
  827. check_result "$E_INVALID" "invalid autoreply format :: $1"
  828. fi
  829. }
  830. # Boolean format validator
  831. is_boolean_format_valid() {
  832. if [ "$1" != 'yes' ] && [ "$1" != 'no' ]; then
  833. check_result "$E_INVALID" "invalid $2 format :: $1"
  834. fi
  835. }
  836. # Refresh IPset format validator
  837. is_refresh_ipset_format_valid() {
  838. if [ "$1" != 'load' ] && [ "$1" != 'yes' ] && [ "$1" != 'no' ]; then
  839. check_result "$E_INVALID" "invalid $2 format :: $1"
  840. fi
  841. }
  842. # Common format validator
  843. is_common_format_valid() {
  844. exclude="[!|#|$|^|&|(|)|+|=|{|}|:|<|>|?|/|\|\"|'|;|%|\`| ]"
  845. if [[ "$1" =~ $exclude ]]; then
  846. check_result "$E_INVALID" "invalid $2 format :: $1"
  847. fi
  848. if [ 400 -le ${#1} ]; then
  849. check_result "$E_INVALID" "invalid $2 format :: $1"
  850. fi
  851. if [[ "$1" =~ @ ]] && [ ${#1} -gt 1 ]; then
  852. check_result "$E_INVALID" "invalid $2 format :: $1"
  853. fi
  854. if [[ $1 =~ \* ]]; then
  855. if [[ "$(echo $1 | grep -o '\*\.' | wc -l)" -eq 0 ]] && [[ $1 != '*' ]]; then
  856. check_result "$E_INVALID" "invalid $2 format :: $1"
  857. fi
  858. fi
  859. if [[ $(echo -n "$1" | tail -c 1) =~ [^a-zA-Z0-9_*@.] ]]; then
  860. check_result "$E_INVALID" "invalid $2 format :: $1"
  861. fi
  862. if [[ $(echo -n "$1" | grep -c '\.\.') -gt 0 ]]; then
  863. check_result "$E_INVALID" "invalid $2 format :: $1"
  864. fi
  865. if [[ $(echo -n "$1" | head -c 1) =~ [^a-zA-Z0-9_*@] ]]; then
  866. check_result "$E_INVALID" "invalid $2 format :: $1"
  867. fi
  868. if [[ $(echo -n "$1" | grep -c '\-\-') -gt 0 ]]; then
  869. check_result "$E_INVALID" "invalid $2 format :: $1"
  870. fi
  871. if [[ $(echo -n "$1" | grep -c '\_\_') -gt 0 ]]; then
  872. check_result "$E_INVALID" "invalid $2 format :: $1"
  873. fi
  874. is_no_new_line_format "$1"
  875. }
  876. is_no_new_line_format() {
  877. test=$(echo "$1" | head -n1)
  878. if [[ "$test" != "$1" ]]; then
  879. check_result "$E_INVALID" "invalid value :: $1"
  880. fi
  881. }
  882. is_string_format_valid() {
  883. exclude="[!|#|$|^|&|(|)|+|=|{|}|:|<|>|?|/|\|\"|'|;|%|\`]"
  884. if [[ "$1" =~ $exclude ]]; then
  885. check_result "$E_INVALID" "invalid $2 format :: $1"
  886. fi
  887. is_no_new_line_format "$1"
  888. }
  889. # Database format validator
  890. is_database_format_valid() {
  891. exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|/|\|\"|'|;|%|\`| ]"
  892. if [[ "$1" =~ $exclude ]] || [ 64 -le ${#1} ]; then
  893. check_result "$E_INVALID" "invalid $2 format :: $1"
  894. fi
  895. is_no_new_line_format "$1"
  896. }
  897. # Date format validator
  898. is_date_format_valid() {
  899. if ! [[ "$1" =~ ^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$ ]]; then
  900. check_result "$E_INVALID" "invalid date format :: $1"
  901. fi
  902. }
  903. # Database user validator
  904. is_dbuser_format_valid() {
  905. exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|/|\|\"|'|;|%|\`| ]"
  906. if [ 33 -le ${#1} ]; then
  907. check_result "$E_INVALID" "mysql username can be up to 32 characters long"
  908. fi
  909. if [[ "$1" =~ $exclude ]]; then
  910. check_result "$E_INVALID" "invalid $2 format :: $1"
  911. fi
  912. is_no_new_line_format "$1"
  913. }
  914. # DNS record type validator
  915. is_dns_type_format_valid() {
  916. known_dnstype='A,AAAA,NS,CNAME,MX,TXT,SRV,DNSKEY,KEY,IPSECKEY,PTR,SPF,TLSA,CAA,DS'
  917. if [ -z "$(echo $known_dnstype | grep -w $1)" ]; then
  918. check_result "$E_INVALID" "invalid dns record type format :: $1"
  919. fi
  920. }
  921. # DNS record validator
  922. is_dns_record_format_valid() {
  923. if [ "$rtype" = 'A' ]; then
  924. is_ip_format_valid "$1"
  925. fi
  926. if [ "$rtype" = 'NS' ]; then
  927. is_domain_format_valid "${1::-1}" 'ns_record'
  928. fi
  929. if [ "$rtype" = 'MX' ]; then
  930. is_domain_format_valid "${1::-1}" 'mx_record'
  931. is_int_format_valid "$priority" 'priority_record'
  932. fi
  933. is_no_new_line_format "$1"
  934. }
  935. # Email format validator
  936. is_email_format_valid() {
  937. if [[ ! "$1" =~ ^[A-Za-z0-9._%+-]+@[[:alnum:].-]+\.[A-Za-z]{2,63}$ ]]; then
  938. if [[ ! "$1" =~ ^[A-Za-z0-9._%+-]+@[[:alnum:].-]+\.(xn--)[[:alnum:]]{2,63}$ ]]; then
  939. check_result "$E_INVALID" "invalid email format :: $1"
  940. fi
  941. fi
  942. }
  943. # Firewall action validator
  944. is_fw_action_format_valid() {
  945. if [ "$1" != "ACCEPT" ] && [ "$1" != 'DROP' ]; then
  946. check_result "$E_INVALID" "invalid action format :: $1"
  947. fi
  948. }
  949. # Firewall protocol validator
  950. is_fw_protocol_format_valid() {
  951. if [ "$1" != "ICMP" ] && [ "$1" != 'UDP' ] && [ "$1" != 'TCP' ]; then
  952. check_result "$E_INVALID" "invalid protocol format :: $1"
  953. fi
  954. }
  955. # Firewall port validator
  956. is_fw_port_format_valid() {
  957. if [ "${#1}" -eq 1 ]; then
  958. if ! [[ "$1" =~ [0-9] ]]; then
  959. check_result "$E_INVALID" "invalid port format :: $1"
  960. fi
  961. else
  962. if ! [[ "$1" =~ ^[0-9][-|,|:|0-9]{0,30}[0-9]$ ]]; then
  963. check_result "$E_INVALID" "invalid port format :: $1"
  964. fi
  965. fi
  966. }
  967. # Integer validator
  968. is_int_format_valid() {
  969. if ! [[ "$1" =~ ^[0-9]+$ ]]; then
  970. check_result "$E_INVALID" "invalid $2 format :: $1"
  971. fi
  972. }
  973. # Interface validator
  974. is_interface_format_valid() {
  975. netdevices=$(cat /proc/net/dev | grep : | cut -f 1 -d : | tr -d ' ')
  976. if [ -z $(echo "$netdevices" | grep -x ${1%:*}) ]; then
  977. check_result "$E_INVALID" "invalid interface format :: $1"
  978. fi
  979. }
  980. # IP status validator
  981. is_ip_status_format_valid() {
  982. if [ -z "$(echo shared,dedicated | grep -w $1)" ]; then
  983. check_result "$E_INVALID" "invalid status format :: $1"
  984. fi
  985. }
  986. # Cron validator
  987. is_cron_format_valid() {
  988. limit=59
  989. check_format=''
  990. if [ "$2" = 'hour' ]; then
  991. limit=23
  992. fi
  993. if [ "$2" = 'day' ]; then
  994. limit=31
  995. fi
  996. if [ "$2" = 'month' ]; then
  997. limit=12
  998. fi
  999. if [ "$2" = 'wday' ]; then
  1000. limit=7
  1001. fi
  1002. if [ "$1" = '*' ]; then
  1003. check_format='ok'
  1004. fi
  1005. if [[ "$1" =~ ^[\*]+[/]+[0-9] ]]; then
  1006. if [ "$(echo $1 | cut -f 2 -d /)" -lt $limit ]; then
  1007. check_format='ok'
  1008. fi
  1009. fi
  1010. if [[ "$1" =~ ^[0-9][-|,|0-9]{0,70}[\/][0-9]$ ]]; then
  1011. check_format='ok'
  1012. crn_values=${1//,/ }
  1013. crn_values=${crn_values//-/ }
  1014. crn_values=${crn_values//\// }
  1015. for crn_vl in $crn_values; do
  1016. if [ "$crn_vl" -gt $limit ]; then
  1017. check_format='invalid'
  1018. fi
  1019. done
  1020. fi
  1021. crn_values=$(echo $1 | tr "," " " | tr "-" " ")
  1022. for crn_vl in $crn_values; do
  1023. if [[ "$crn_vl" =~ ^[0-9]+$ ]] && [ "$crn_vl" -le $limit ]; then
  1024. check_format='ok'
  1025. fi
  1026. done
  1027. if [ "$check_format" != 'ok' ]; then
  1028. check_result "$E_INVALID" "invalid $2 format :: $1"
  1029. fi
  1030. }
  1031. is_object_name_format_valid() {
  1032. if ! [[ "$1" =~ ^[-|\ |\.|_[:alnum:]]{0,50}$ ]]; then
  1033. check_result "$E_INVALID" "invalid $2 format :: $1"
  1034. fi
  1035. }
  1036. # Name validator
  1037. is_name_format_valid() {
  1038. exclude="['|\"|<|>]"
  1039. if [[ "$1" =~ $exclude ]]; then
  1040. check_result "$E_INVALID" "Invalid $2 contains qoutes (\" or ') :: $1"
  1041. fi
  1042. is_no_new_line_format "$1"
  1043. }
  1044. # Object validator
  1045. is_object_format_valid() {
  1046. if ! [[ "$1" =~ ^[[:alnum:]][-|\.|_[:alnum:]]{0,64}[[:alnum:]]$ ]]; then
  1047. check_result "$E_INVALID" "invalid $2 format :: $1"
  1048. fi
  1049. }
  1050. # Role validator
  1051. is_role_valid() {
  1052. if ! [[ "$1" =~ ^admin$|^user$|^dns-cluster$ ]]; then
  1053. check_result "$E_INVALID" "invalid $2 format :: $1"
  1054. fi
  1055. }
  1056. # Password validator
  1057. is_password_format_valid() {
  1058. if [ "${#1}" -lt '6' ]; then
  1059. check_result "$E_INVALID" "invalid password format :: $1"
  1060. fi
  1061. }
  1062. # Missing function -
  1063. # Before: validate_format_shell
  1064. # After: is_format_valid_shell
  1065. is_format_valid_shell() {
  1066. if [ -z "$(grep -w $1 /etc/shells)" ]; then
  1067. echo "Error: shell $1 is not valid"
  1068. log_event "$E_INVALID" "$EVENT"
  1069. exit $E_INVALID
  1070. fi
  1071. }
  1072. # Service name validator
  1073. is_service_format_valid() {
  1074. if ! [[ "$1" =~ ^[[:alnum:]][-|\.|_[:alnum:]]{0,64}$ ]]; then
  1075. check_result "$E_INVALID" "invalid $2 format :: $1"
  1076. fi
  1077. }
  1078. is_hash_format_valid() {
  1079. if ! [[ "$1" =~ ^[[:alnum:]|\:|\=|_|-]{1,80}$ ]]; then
  1080. check_result "$E_INVALID" "invalid $2 format :: $1"
  1081. fi
  1082. }
  1083. # Format validation controller
  1084. is_format_valid() {
  1085. for arg_name in $*; do
  1086. eval arg=\$$arg_name
  1087. if [ -n "$arg" ]; then
  1088. case $arg_name in
  1089. access_key_id) is_access_key_id_format_valid "$arg" "$arg_name" ;;
  1090. account) is_user_format_valid "$arg" "$arg_name" '64' ;;
  1091. action) is_fw_action_format_valid "$arg" ;;
  1092. active) is_boolean_format_valid "$arg" 'active' ;;
  1093. aliases) is_alias_format_valid "$arg" ;;
  1094. alias) is_alias_format_valid "$arg" ;;
  1095. antispam) is_boolean_format_valid "$arg" 'antispam' ;;
  1096. antivirus) is_boolean_format_valid "$arg" 'antivirus' ;;
  1097. autoreply) is_autoreply_format_valid "$arg" ;;
  1098. backup) is_object_format_valid "$arg" 'backup' ;;
  1099. charset) is_object_format_valid "$arg" "$arg_name" ;;
  1100. charsets) is_common_format_valid "$arg" 'charsets' ;;
  1101. chain) is_object_format_valid "$arg" 'chain' ;;
  1102. cidr) is_ip_format_valid "$arg" 'cidr' ;;
  1103. comment) is_object_format_valid "$arg" 'comment' ;;
  1104. database) is_database_format_valid "$arg" 'database' ;;
  1105. day) is_cron_format_valid "$arg" $arg_name ;;
  1106. dbpass) is_password_format_valid "$arg" ;;
  1107. dbuser) is_dbuser_format_valid "$arg" 'dbuser' ;;
  1108. dkim) is_boolean_format_valid "$arg" 'dkim' ;;
  1109. dkim_size) is_int_format_valid "$arg" ;;
  1110. domain) is_domain_format_valid "$arg" ;;
  1111. dom_alias) is_alias_format_valid "$arg" ;;
  1112. dvalue) is_dns_record_format_valid "$arg" ;;
  1113. email) is_email_format_valid "$arg" ;;
  1114. email_forward) is_email_format_valid "$arg" ;;
  1115. exp) is_date_format_valid "$arg" ;;
  1116. extentions) is_common_format_valid "$arg" 'extentions' ;;
  1117. format) is_type_valid 'plain json shell csv' "$arg" ;;
  1118. ftp_password) is_password_format_valid "$arg" ;;
  1119. ftp_user) is_user_format_valid "$arg" "$arg_name" ;;
  1120. hash) is_hash_format_valid "$arg" "$arg_name" ;;
  1121. host) is_object_format_valid "$arg" "$arg_name" ;;
  1122. hour) is_cron_format_valid "$arg" $arg_name ;;
  1123. id) is_int_format_valid "$arg" 'id' ;;
  1124. iface) is_interface_format_valid "$arg" ;;
  1125. ip) is_ip_format_valid "$arg" ;;
  1126. ipv6) is_ipv6_format_valid "$arg" ;;
  1127. ip46) is_ip46_format_valid "$arg" ;;
  1128. ip_name) is_domain_format_valid "$arg" 'IP name' ;;
  1129. ip_status) is_ip_status_format_valid "$arg" ;;
  1130. job) is_int_format_valid "$arg" 'job' ;;
  1131. key) is_common_format_valid "$arg" "$arg_name" ;;
  1132. malias) is_user_format_valid "$arg" "$arg_name" '64' ;;
  1133. max_db) is_int_format_valid "$arg" 'max db' ;;
  1134. min) is_cron_format_valid "$arg" $arg_name ;;
  1135. month) is_cron_format_valid "$arg" $arg_name ;;
  1136. name) is_name_format_valid "$arg" "name" ;;
  1137. nat_ip) is_ip_format_valid "$arg" ;;
  1138. netmask) is_ip_format_valid "$arg" 'netmask' ;;
  1139. newid) is_int_format_valid "$arg" 'id' ;;
  1140. ns1) is_domain_format_valid "$arg" 'ns1' ;;
  1141. ns2) is_domain_format_valid "$arg" 'ns2' ;;
  1142. ns3) is_domain_format_valid "$arg" 'ns3' ;;
  1143. ns4) is_domain_format_valid "$arg" 'ns4' ;;
  1144. ns5) is_domain_format_valid "$arg" 'ns5' ;;
  1145. ns6) is_domain_format_valid "$arg" 'ns6' ;;
  1146. ns7) is_domain_format_valid "$arg" 'ns7' ;;
  1147. ns8) is_domain_format_valid "$arg" 'ns8' ;;
  1148. object) is_object_name_format_valid "$arg" 'object' ;;
  1149. package) is_object_format_valid "$arg" "$arg_name" ;;
  1150. password) is_password_format_valid "$arg" ;;
  1151. port) is_int_format_valid "$arg" 'port' ;;
  1152. port_ext) is_fw_port_format_valid "$arg" ;;
  1153. prefix_length) is_ipv6_format_valid "$arg" 'prefix_length' ;;
  1154. protocol) is_fw_protocol_format_valid "$arg" ;;
  1155. proxy_ext) is_extention_format_valid "$arg" ;;
  1156. quota) is_int_format_valid "$arg" 'quota' ;;
  1157. rate) is_int_format_valid "$arg" 'rate' ;;
  1158. record) is_common_format_valid "$arg" 'record' ;;
  1159. reject) is_boolean_format_valid "$arg" 'reject' ;;
  1160. restart) is_restart_format_valid "$arg" 'restart' ;;
  1161. role) is_role_valid "$arg" 'role' ;;
  1162. rtype) is_dns_type_format_valid "$arg" ;;
  1163. rule) is_int_format_valid "$arg" "rule id" ;;
  1164. service) is_service_format_valid "$arg" "$arg_name" ;;
  1165. secret_access_key) is_secret_access_key_format_valid "$arg" "$arg_name" ;;
  1166. soa) is_domain_format_valid "$arg" 'SOA' ;;
  1167. #missing command: is_format_valid_shell
  1168. shell) is_format_valid_shell "$arg" ;;
  1169. ssl_dir) is_folder_exists "$arg" "$arg_name" ;;
  1170. stats_pass) is_password_format_valid "$arg" ;;
  1171. stats_user) is_user_format_valid "$arg" "$arg_name" ;;
  1172. template) is_object_format_valid "$arg" "$arg_name" ;;
  1173. theme) is_common_format_valid "$arg" "$arg_name" ;;
  1174. ttl) is_int_format_valid "$arg" 'ttl' ;;
  1175. user) is_user_format_valid "$arg" $arg_name ;;
  1176. wday) is_cron_format_valid "$arg" $arg_name ;;
  1177. value) is_common_format_valid "$arg" $arg_name ;;
  1178. esac
  1179. fi
  1180. done
  1181. }
  1182. is_folder_exists() {
  1183. if [ ! -d "$1" ]; then
  1184. check_result "$E_NOTEXIST" "folder $1 does not exist"
  1185. fi
  1186. }
  1187. is_command_valid_format() {
  1188. if [[ ! "$1" =~ ^v-[[:alnum:]][-|\.|_[:alnum:]]{0,64}[[:alnum:]]$ ]]; then
  1189. check_result "$E_INVALID" "Invalid command format"
  1190. fi
  1191. if [[ -n $(echo "$1" | grep -e '\-\-') ]]; then
  1192. check_result "$E_INVALID" "Invalid command format"
  1193. fi
  1194. }
  1195. # Check access_key_id name
  1196. # Don't work with legacy key format
  1197. is_access_key_id_format_valid() {
  1198. local hash="$1"
  1199. # ACCESS_KEY_ID format validation
  1200. if ! [[ "$hash" =~ ^[[:alnum:]]{20}$ ]]; then
  1201. check_result "$E_INVALID" "invalid $2 format :: $hash"
  1202. fi
  1203. }
  1204. # SECRET_ACCESS_KEY format validation
  1205. is_secret_access_key_format_valid() {
  1206. local hash="$1"
  1207. if ! [[ "$hash" =~ ^[[:alnum:]|_|\.|\+|/|\^|~|=|%|\-]{40}$ ]]; then
  1208. check_result "$E_INVALID" "invalid $2 format"
  1209. fi
  1210. }
  1211. # Checks if the secret belongs to the access key
  1212. check_access_key_secret() {
  1213. local access_key_id="$(basename "$1")"
  1214. local secret_access_key=$2
  1215. local -n key_user=$3
  1216. if [[ -z "$access_key_id" || ! -f "$HESTIA/data/access-keys/${access_key_id}" ]]; then
  1217. check_result "$E_PASSWORD" "Access key $access_key_id doesn't exist"
  1218. fi
  1219. if [[ -z "$secret_access_key" ]]; then
  1220. check_result "$E_PASSWORD" "Secret key not provided for key $access_key_id"
  1221. elif ! [[ "$secret_access_key" =~ ^[[:alnum:]|_|\.|\+|/|\^|~|=|%|\-]{40}$ ]]; then
  1222. check_result "$E_PASSWORD" "Invalid secret key for key $access_key_id"
  1223. else
  1224. SECRET_ACCESS_KEY=""
  1225. source_conf "$HESTIA/data/access-keys/${access_key_id}"
  1226. if [[ -z "$SECRET_ACCESS_KEY" || "$SECRET_ACCESS_KEY" != "$secret_access_key" ]]; then
  1227. check_result "$E_PASSWORD" "Invalid secret key for key $access_key_id"
  1228. fi
  1229. fi
  1230. key_user="$USER"
  1231. }
  1232. # Checks if the key belongs to the user
  1233. check_access_key_user() {
  1234. local access_key_id="$(basename "$1")"
  1235. local user=$2
  1236. if [[ -z "$access_key_id" || ! -f "$HESTIA/data/access-keys/${access_key_id}" ]]; then
  1237. check_result "$E_FORBIDEN" "Access key $access_key_id doesn't exist"
  1238. fi
  1239. if [[ -z "$user" ]]; then
  1240. check_result "$E_FORBIDEN" "User not provided"
  1241. else
  1242. USER=""
  1243. source_conf "$HESTIA/data/access-keys/${access_key_id}"
  1244. if [[ -z "$USER" || "$USER" != "$user" ]]; then
  1245. check_result "$E_FORBIDEN" "key $access_key_id does not belong to the user $user"
  1246. fi
  1247. fi
  1248. }
  1249. # Checks if the key is allowed to run the command
  1250. check_access_key_cmd() {
  1251. local access_key_id="$(basename "$1")"
  1252. local cmd=$2
  1253. local -n user_arg_position=$3
  1254. if [[ "$DEBUG_MODE" = "true" ]]; then
  1255. new_timestamp
  1256. echo "[$date:$time] $1 $2" >> /var/log/hestia/api.log
  1257. fi
  1258. if [[ -z "$access_key_id" || ! -f "$HESTIA/data/access-keys/${access_key_id}" ]]; then
  1259. check_result "$E_FORBIDEN" "Access key $access_key_id doesn't exist"
  1260. fi
  1261. if [[ -z "$cmd" ]]; then
  1262. check_result "$E_FORBIDEN" "Command not provided"
  1263. elif [[ "$cmd" = 'v-make-tmp-file' ]]; then
  1264. USER="" PERMISSIONS=""
  1265. source_conf "${HESTIA}/data/access-keys/${access_key_id}"
  1266. local allowed_commands
  1267. if [[ -n "$PERMISSIONS" ]]; then
  1268. allowed_commands="$(get_apis_commands "$PERMISSIONS")"
  1269. if [[ -z "$(echo ",${allowed_commands}," | grep ",${hst_command},")" ]]; then
  1270. check_result "$E_FORBIDEN" "Key $access_key_id don't have permission to run the command $hst_command"
  1271. fi
  1272. elif [[ -z "$PERMISSIONS" && "$USER" != "admin" ]]; then
  1273. check_result "$E_FORBIDEN" "Key $access_key_id don't have permission to run the command $hst_command"
  1274. fi
  1275. user_arg_position="0"
  1276. elif [[ ! -e "$BIN/$cmd" ]]; then
  1277. check_result "$E_FORBIDEN" "Command $cmd not found"
  1278. else
  1279. USER="" PERMISSIONS=""
  1280. source_conf "${HESTIA}/data/access-keys/${access_key_id}"
  1281. local allowed_commands
  1282. if [[ -n "$PERMISSIONS" ]]; then
  1283. allowed_commands="$(get_apis_commands "$PERMISSIONS")"
  1284. if [[ -z "$(echo ",${allowed_commands}," | grep ",${hst_command},")" ]]; then
  1285. check_result "$E_FORBIDEN" "Key $access_key_id don't have permission to run the command $hst_command"
  1286. fi
  1287. elif [[ -z "$PERMISSIONS" && "$USER" != "admin" ]]; then
  1288. check_result "$E_FORBIDEN" "Key $access_key_id don't have permission to run the command $hst_command"
  1289. fi
  1290. if [[ "$USER" == "admin" ]]; then
  1291. # Admin can run commands for any user
  1292. user_arg_position="0"
  1293. else
  1294. user_arg_position="$(search_command_arg_position "$hst_command" "USER")"
  1295. if ! [[ "$user_arg_position" =~ ^[0-9]+$ ]]; then
  1296. check_result "$E_FORBIDEN" "Command $hst_command not found"
  1297. fi
  1298. fi
  1299. fi
  1300. }
  1301. # Domain argument formatting
  1302. format_domain() {
  1303. if [[ "$domain" = *[![:ascii:]]* ]]; then
  1304. if [[ "$domain" =~ [[:upper:]] ]]; then
  1305. domain=$(echo "$domain" | sed 's/[[:upper:]].*/\L&/')
  1306. fi
  1307. else
  1308. if [[ "$domain" =~ [[:upper:]] ]]; then
  1309. domain=$(echo "$domain" | tr '[:upper:]' '[:lower:]')
  1310. fi
  1311. fi
  1312. if [[ "$domain" =~ ^www\..* ]]; then
  1313. domain=$(echo "$domain" | sed -e "s/^www.//")
  1314. fi
  1315. if [[ "$domain" =~ .*\.$ ]]; then
  1316. domain=$(echo "$domain" | sed -e "s/[.]*$//g")
  1317. fi
  1318. if [[ "$domain" =~ ^\. ]]; then
  1319. domain=$(echo "$domain" | sed -e "s/^[.]*//")
  1320. fi
  1321. }
  1322. format_domain_idn() {
  1323. if [ -z "$domain_idn" ]; then
  1324. domain_idn=$domain
  1325. fi
  1326. if [[ "$domain_idn" = *[![:ascii:]]* ]]; then
  1327. domain_idn=$(idn2 --quiet $domain_idn)
  1328. fi
  1329. }
  1330. format_aliases() {
  1331. if [ -n "$aliases" ] && [ "$aliases" != 'none' ]; then
  1332. aliases=$(echo $aliases | tr '[:upper:]' '[:lower:]' | tr ',' '\n')
  1333. aliases=$(echo "$aliases" | sed -e "s/\.$//" | sort -u)
  1334. aliases=$(echo "$aliases" | tr -s '.')
  1335. aliases=$(echo "$aliases" | sed -e "s/[.]*$//g")
  1336. aliases=$(echo "$aliases" | sed -e "s/^[.]*//")
  1337. aliases=$(echo "$aliases" | sed -e "/^$/d")
  1338. aliases=$(echo "$aliases" | tr '\n' ',' | sed -e "s/,$//")
  1339. fi
  1340. }
  1341. is_restart_format_valid() {
  1342. if [ "$1" != 'yes' ] && [ "$1" != 'no' ] && [ "$1" != 'ssl' ] && [ "$1" != 'reload' ] && [ "$1" != 'updatessl' ]; then
  1343. check_result "$E_INVALID" "invalid $2 format :: $1"
  1344. fi
  1345. }
  1346. check_backup_conditions() {
  1347. # Checking load average
  1348. la=$(cat /proc/loadavg | cut -f 1 -d ' ' | cut -f 1 -d '.')
  1349. # i=0
  1350. while [ "$la" -ge "$BACKUP_LA_LIMIT" ]; do
  1351. echo -e "$(date "+%F %T") Load Average $la"
  1352. sleep 60
  1353. la=$(cat /proc/loadavg | cut -f 1 -d ' ' | cut -f 1 -d '.')
  1354. done
  1355. }
  1356. # Define download function
  1357. download_file() {
  1358. local url=$1
  1359. local destination=$2
  1360. local force=$3
  1361. # Default destination is the curent working directory
  1362. local dstopt=""
  1363. if [ -n "$(echo "$url" | grep -E "\.(gz|gzip|bz2|zip|xz)$")" ]; then
  1364. # When an archive file is downloaded it will be first saved localy
  1365. dstopt="--directory-prefix=$ARCHIVE_DIR"
  1366. local is_archive="true"
  1367. local filename="${url##*/}"
  1368. if [ -z "$filename" ]; then
  1369. echo >&2 "[!] No filename was found in url, exiting ($url)"
  1370. exit 1
  1371. fi
  1372. if [ -n "$force" ] && [ -f "$ARCHIVE_DIR/$filename" ]; then
  1373. rm -f $ARCHIVE_DIR/$filename
  1374. fi
  1375. elif [ -n "$destination" ]; then
  1376. # Plain files will be written to specified location
  1377. dstopt="-O $destination"
  1378. fi
  1379. # check for corrupted archive
  1380. if [ -f "$ARCHIVE_DIR/$filename" ] && [ "$is_archive" = "true" ]; then
  1381. tar -tzf "$ARCHIVE_DIR/$filename" > /dev/null 2>&1
  1382. if [ $? -ne 0 ]; then
  1383. echo >&2 "[!] Archive $ARCHIVE_DIR/$filename is corrupted, redownloading"
  1384. rm -f $ARCHIVE_DIR/$filename
  1385. fi
  1386. fi
  1387. if [ ! -f "$ARCHIVE_DIR/$filename" ]; then
  1388. wget $url -q $dstopt --show-progress --progress=bar:force --limit-rate=3m
  1389. fi
  1390. if [ -n "$destination" ] && [ "$is_archive" = "true" ]; then
  1391. if [ "$destination" = "-" ]; then
  1392. cat "$ARCHIVE_DIR/$filename"
  1393. elif [ -d "$(dirname $destination)" ]; then
  1394. cp "$ARCHIVE_DIR/$filename" "$destination"
  1395. fi
  1396. fi
  1397. }
  1398. check_hestia_demo_mode() {
  1399. demo_mode=$(grep DEMO_MODE /usr/local/hestia/conf/hestia.conf | cut -d '=' -f2 | sed "s|'||g")
  1400. if [ -n "$demo_mode" ] && [ "$demo_mode" = "yes" ]; then
  1401. echo "ERROR: Unable to perform operation due to security restrictions that are in place."
  1402. exit 1
  1403. fi
  1404. }
  1405. multiphp_count() {
  1406. $BIN/v-list-sys-php plain | wc -l
  1407. }
  1408. multiphp_versions() {
  1409. local -a php_versions_list
  1410. local php_ver
  1411. if [ "$(multiphp_count)" -gt 0 ]; then
  1412. for php_ver in $($BIN/v-list-sys-php plain); do
  1413. [ ! -d "/etc/php/$php_ver/fpm/pool.d/" ] && continue
  1414. php_versions_list+=($php_ver)
  1415. done
  1416. echo "${php_versions_list[@]}"
  1417. fi
  1418. }
  1419. multiphp_default_version() {
  1420. # Get system wide default php version (set by update-alternatives)
  1421. local sys_phpversion=$(php -r "echo substr(phpversion(),0,3);")
  1422. # Check if the system php also has php-fpm enabled, otherwise return
  1423. # the most recent php version which does have it installed.
  1424. if [ ! -d "/etc/php/$sys_phpversion/fpm/pool.d/" ]; then
  1425. local all_versions="$(multiphp_versions)"
  1426. if [ -n "$all_versions" ]; then
  1427. sys_phpversion="${all_versions##*\ }"
  1428. fi
  1429. fi
  1430. echo "$sys_phpversion"
  1431. }
  1432. is_hestia_package() {
  1433. check=false
  1434. for pkg in $1; do
  1435. if [ "$pkg" == "$2" ]; then
  1436. check="true"
  1437. fi
  1438. done
  1439. if [ "$check" != "true" ]; then
  1440. check_result $E_INVALID "$2 package is not controlled by hestiacp"
  1441. fi
  1442. }
  1443. # Run arbitrary cli commands with dropped privileges
  1444. # Note: setpriv --init-groups is not available on debian9 (util-linux 2.29.2)
  1445. # Input:
  1446. # - $user : Vaild hestia user
  1447. user_exec() {
  1448. is_object_valid 'user' 'USER' "$user"
  1449. local user_groups=$(id -G "$user")
  1450. user_groups=${user_groups//\ /,}
  1451. setpriv --groups "$user_groups" --reuid "$user" --regid "$user" -- "${@}"
  1452. }
  1453. # Simple chmod wrapper that skips symlink files after glob expand
  1454. no_symlink_chmod() {
  1455. local filemode=$1
  1456. shift
  1457. for i in "$@"; do
  1458. [[ -L ${i} ]] && continue
  1459. chmod "${filemode}" "${i}"
  1460. done
  1461. }
  1462. source_conf() {
  1463. while IFS='= ' read -r lhs rhs; do
  1464. if [[ ! $lhs =~ ^\ *# && -n $lhs ]]; then
  1465. rhs="${rhs%%^\#*}" # Del in line right comments
  1466. rhs="${rhs%%*( )}" # Del trailing spaces
  1467. rhs="${rhs%\'*}" # Del opening string quotes
  1468. rhs="${rhs#\'*}" # Del closing string quotes
  1469. declare -g $lhs="$rhs"
  1470. fi
  1471. done < $1
  1472. }
  1473. format_no_quotes() {
  1474. exclude="['|\"]"
  1475. if [[ "$1" =~ $exclude ]]; then
  1476. check_result "$E_INVALID" "Invalid $2 contains qoutes (\" or ') :: $1"
  1477. fi
  1478. is_no_new_line_format "$1"
  1479. }
  1480. is_username_format_valid() {
  1481. if [[ ! "$1" =~ ^[A-Za-z0-9._%+-]+@[[:alnum:].-]+\.[A-Za-z]{2,63}$ ]]; then
  1482. is_string_format_valid "$1" "$2"
  1483. fi
  1484. }
  1485. change_sys_value() {
  1486. check_ckey=$(grep "^$1='" "$HESTIA/conf/hestia.conf")
  1487. if [ -z "$check_ckey" ]; then
  1488. echo "$1='$2'" >> "$HESTIA/conf/hestia.conf"
  1489. else
  1490. sed -i "s|^$1=.*|$1='$2'|g" "$HESTIA/conf/hestia.conf"
  1491. fi
  1492. }
  1493. # Checks the format of APIs that will be allowed for the key
  1494. is_key_permissions_format_valid() {
  1495. local permissions="$1"
  1496. local user="$2"
  1497. if [[ "$user" != "admin" && -z "$permissions" ]]; then
  1498. check_result "$E_INVALID" "Non-admin users need a permission list"
  1499. fi
  1500. while IFS=',' read -ra permissions_arr; do
  1501. for permission in "${permissions_arr[@]}"; do
  1502. permission="$(basename "$permission" | sed -E "s/^\s*|\s*$//g")"
  1503. # if [[ -z "$(echo "$permission" | grep -E "^v-")" ]]; then
  1504. if [[ ! -e "$HESTIA/data/api/$permission" ]]; then
  1505. check_result "$E_NOTEXIST" "API $permission doesn't exist"
  1506. fi
  1507. source_conf "$HESTIA/data/api/$permission"
  1508. if [ "$ROLE" = "admin" ] && [ "$user" != "admin" ]; then
  1509. check_result "$E_INVALID" "Only the admin can run this API"
  1510. fi
  1511. # elif [[ ! -e "$BIN/$permission" ]]; then
  1512. # check_result "$E_NOTEXIST" "Command $permission doesn't exist"
  1513. # fi
  1514. done
  1515. done <<< "$permissions"
  1516. }
  1517. # Remove whitespaces, and bin path from commands
  1518. cleanup_key_permissions() {
  1519. local permissions="$1"
  1520. local final quote
  1521. while IFS=',' read -ra permissions_arr; do
  1522. for permission in "${permissions_arr[@]}"; do
  1523. permission="$(basename "$permission" | sed -E "s/^\s*|\s*$//g")"
  1524. # Avoid duplicate items
  1525. if [[ -z "$(echo ",${final}," | grep ",${permission},")" ]]; then
  1526. final+="${quote}${permission}"
  1527. quote=','
  1528. fi
  1529. done
  1530. done <<< "$permissions"
  1531. echo "$final"
  1532. }
  1533. # Extract all allowed commands from a permission list
  1534. get_apis_commands() {
  1535. local permissions="$1"
  1536. local allowed_commands quote commands_to_add
  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. commands_to_add=""
  1541. # if [[ -n "$(echo "$permission" | grep -E "^v-")" ]]; then
  1542. # commands_to_add="$permission"
  1543. # el
  1544. if [[ -e "$HESTIA/data/api/$permission" ]]; then
  1545. source_conf "$HESTIA/data/api/$permission"
  1546. commands_to_add="$COMMANDS"
  1547. fi
  1548. if [[ -n "$commands_to_add" ]]; then
  1549. allowed_commands+="${quote}${commands_to_add}"
  1550. quote=','
  1551. fi
  1552. done
  1553. done <<< "$permissions"
  1554. cleanup_key_permissions "$allowed_commands"
  1555. }
  1556. # Get the position of an argument by name in a hestia command using the command's documentation comment.
  1557. #
  1558. # Return:
  1559. # * 0: It doesn't have the argument;
  1560. # * 1-9: The position of the argument in the command.
  1561. search_command_arg_position() {
  1562. local hst_command="$(basename "$1")"
  1563. local arg_name="$2"
  1564. local command_path="$BIN/$hst_command"
  1565. if [[ -z "$hst_command" || ! -e "$command_path" ]]; then
  1566. echo "-1"
  1567. return
  1568. fi
  1569. local position=0
  1570. local count=0
  1571. local command_options="$(sed -En 's/^# options: (.+)/\1/p' "$command_path")"
  1572. while IFS=' ' read -ra options_arr; do
  1573. for option in "${options_arr[@]}"; do
  1574. count=$((count + 1))
  1575. option_name="$(echo " $option " | sed -E 's/^(\s|\[)*|(\s|\])*$//g')"
  1576. if [[ "${option_name^^}" == "$arg_name" ]]; then
  1577. position=$count
  1578. fi
  1579. done
  1580. done <<< "$command_options"
  1581. echo "$position"
  1582. }