main.sh 47 KB

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