Skip to content

Commit 9a1fde8

Browse files
committed
add user package
1 parent 650c3f8 commit 9a1fde8

File tree

12 files changed

+470
-11
lines changed

12 files changed

+470
-11
lines changed

bin/v_add_user

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ is_package_valid "$package"
5555
#----------------------------------------------------------#
5656

5757
# Parsing package data
58-
package_data=$(cat $VESTA/data/packages/$package.pkg)
58+
pkg_data=$(cat $VESTA/data/packages/$package.pkg |grep -v TIME |grep -v DATE)
5959

6060
# Checking shell
61-
shell_conf=$(echo "$package_data" | grep 'SHELL' | cut -f 2 -d \')
61+
shell_conf=$(echo "$pkg_data" | grep 'SHELL' | cut -f 2 -d \')
6262
shell=$(/usr/bin/chsh --list-shells | grep -w "$shell_conf" |head -n1)
6363

6464
# Adding user
@@ -98,7 +98,7 @@ chmod -R a+x $HOMEDIR/$user
9898

9999
# Checking quota
100100
if [ ! -z "$DISK_QUOTA" ] && [ "$DISK_QUOTA" != 'no' ]; then
101-
DISK_QUOTA=$(echo "$package_data" | grep 'DISK_QUOTA' | cut -f 2 -d \')
101+
DISK_QUOTA=$(echo "$pkg_data" | grep 'DISK_QUOTA' | cut -f 2 -d \')
102102
#$BIN/v_add_user_quota "$user" "$DISK_QUOTA"
103103
fi
104104

@@ -169,7 +169,7 @@ fi
169169
echo "FNAME='$fname'
170170
LNAME='$lname'
171171
PACKAGE='$package'
172-
$package_data
172+
$pkg_data
173173
CONTACT='$email'
174174
CRON_REPORTS='yes'
175175
MD5='$(awk -v user=$user -F : 'user == $1 {print $2}' /etc/shadow)'

bin/v_add_user_package

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
# info: adding user package
3+
# options: pkg_dir package [rewrite]
4+
#
5+
# The function adds new user package to the system.
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Argument defenition
13+
pkg_dir=$1
14+
package=$2
15+
rewrite=$3
16+
17+
# Includes
18+
source $VESTA/conf/vesta.conf
19+
source $VESTA/func/main.sh
20+
21+
# Functions
22+
is_package_new() {
23+
if [ -e "$VESTA/data/packages/$package.pkg" ]; then
24+
echo "Error: package $package already exists."
25+
log_event "$E_EXISTS" "$EVENT"
26+
exit $E_EXISTS
27+
fi
28+
}
29+
30+
is_package_consistent() {
31+
source $pkg_dir/$package.pkg
32+
validate_format_int $WEB_DOMAINS
33+
validate_format_int $WEB_ALIASES
34+
validate_format_int $DNS_DOMAINS
35+
validate_format_int $DNS_RECORDS
36+
validate_format_int $MAIL_DOMAINS
37+
validate_format_int $MAIL_ACCOUNTS
38+
validate_format_int $DATABASES
39+
validate_format_int $CRON_JOBS
40+
validate_format_int $DISK_QUOTA
41+
validate_format_int $BACKUPS
42+
validate_format_shell $SHELL
43+
}
44+
45+
46+
#----------------------------------------------------------#
47+
# Verifications #
48+
#----------------------------------------------------------#
49+
50+
check_args '2' "$#" 'pkg_dir package' 'rewrite'
51+
validate_format 'pkg_dir' 'package'
52+
if [ "$rewrite" != 'yes' ]; then
53+
is_package_new
54+
fi
55+
is_package_valid "$pkg_dir"
56+
is_package_consistent
57+
58+
59+
#----------------------------------------------------------#
60+
# Action #
61+
#----------------------------------------------------------#
62+
63+
cp -f $pkg_dir/$package.pkg $VESTA/data/packages/
64+
chmod 644 $VESTA/data/packages/$package.pkg
65+
66+
67+
#----------------------------------------------------------#
68+
# Vesta #
69+
#----------------------------------------------------------#
70+
71+
# Logging
72+
log_event "$OK" "$EVENT"
73+
74+
exit

bin/v_change_user_package

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ is_package_avalable() {
3232
DISK_QUOTA='0'
3333
BANDWIDTH='0'
3434

35-
pkg_data=$(cat $VESTA/data/packages/$package.pkg)
35+
pkg_data=$(cat $VESTA/data/packages/$package.pkg |grep -v TIME |\
36+
grep -v DATE)
3637
eval $pkg_data
3738

3839
# Comparing user data with package

bin/v_list_user_package

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/bash
2+
# info: list user package
3+
# options: package [format]
4+
#
5+
# The function for getting the list of system ip parameters.
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Argument defenition
13+
package=$1
14+
PACKAGE=$package
15+
format=${2-shell}
16+
17+
# Includes
18+
source $VESTA/func/main.sh
19+
20+
# Json function
21+
json_list_package() {
22+
i=1
23+
fileds_count=$(echo "$fields" | wc -w)
24+
pkg_data=$(cat $VESTA/data/packages/$PACKAGE.pkg)
25+
echo '{'
26+
eval $pkg_data
27+
for field in $fields; do
28+
eval value=$field
29+
if [ $i -eq 1 ]; then
30+
echo -e "\t\"$value\": {"
31+
else
32+
if [ $fileds_count -eq $i ]; then
33+
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
34+
else
35+
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
36+
fi
37+
fi
38+
(( ++i))
39+
done
40+
if [ -n "$value" ]; then
41+
echo -e ' }'
42+
fi
43+
echo -e '}'
44+
}
45+
46+
# Shell function
47+
shell_list_package() {
48+
line=$(cat $VESTA/data/packages/$PACKAGE.pkg)
49+
eval $line
50+
for field in $fields; do
51+
eval key="$field"
52+
if [ -z "$key" ]; then
53+
key='NULL'
54+
fi
55+
echo "${field//$/}: $key "
56+
done
57+
}
58+
59+
60+
#----------------------------------------------------------#
61+
# Verifications #
62+
#----------------------------------------------------------#
63+
64+
# Checking args
65+
check_args '1' "$#" 'package [format]'
66+
validate_format 'package'
67+
is_package_valid
68+
69+
70+
#----------------------------------------------------------#
71+
# Action #
72+
#----------------------------------------------------------#
73+
74+
# Defining fileds to select
75+
fields='$PACKAGE $TEMPLATE $WEB_DOMAINS $WEB_ALIASES $DNS_DOMAINS $DNS_RECORDS
76+
$MAIL_DOMAINS $MAIL_ACCOUNTS $DATABASES $CRON_JOBS $DISK_QUOTA $BANDWIDTH
77+
$NS $SHELL $BACKUPS $TIME $DATE'
78+
79+
# Listing ip
80+
case $format in
81+
json) json_list_package ;;
82+
plain) shell_list_package ;;
83+
shell) shell_list_package | column -t ;;
84+
*) check_args '1' '0' 'ip [format]'
85+
esac
86+
87+
88+
#----------------------------------------------------------#
89+
# Vesta #
90+
#----------------------------------------------------------#
91+
92+
exit

func/main.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ gen_password() {
121121

122122
# Package existance check
123123
is_package_valid() {
124-
if [ ! -e "$VESTA/data/packages/$package.pkg" ]; then
124+
if [ -z "$1" ]; then
125+
pkg_dir="$VESTA/data/packages"
126+
fi
127+
if [ ! -e "$pkg_dir/$package.pkg" ]; then
125128
echo "Error: package $package not exist"
126129
log_event "$E_NOTEXIST $EVENT"
127130
exit $E_NOTEXIST

web/add/package/index.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
// Init
3+
error_reporting(NULL);
4+
ob_start();
5+
session_start();
6+
$TAB = 'PACKAGE';
7+
include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
8+
9+
if (empty($_SESSION['user'])) {
10+
header("Location: /login/");
11+
}
12+
13+
// Header
14+
include($_SERVER['DOCUMENT_ROOT'].'/templates/header.html');
15+
16+
// Panel
17+
top_panel($user,$TAB);
18+
19+
// Are you admin?
20+
if ($_SESSION['user'] == 'admin') {
21+
if (!empty($_POST['ok'])) {
22+
// Check input
23+
if (empty($_POST['v_package'])) $errors[] = 'package';
24+
if (empty($_POST['v_template'])) $errors[] = 'template';
25+
if (empty($_POST['v_shell'])) $errrors[] = 'shell';
26+
if (!isset($_POST['v_web_domains'])) $errors[] = 'web domains';
27+
if (!isset($_POST['v_web_aliases'])) $errors[] = 'web aliases';
28+
if (!isset($_POST['v_dns_domains'])) $errors[] = 'dns domains';
29+
if (!isset($_POST['v_dns_records'])) $errors[] = 'dns records';
30+
if (!isset($_POST['v_mail_domains'])) $errors[] = 'mail domains';
31+
if (!isset($_POST['v_mail_accounts'])) $errors[] = 'mail accounts';
32+
if (!isset($_POST['v_databases'])) $errors[] = 'databases';
33+
if (!isset($_POST['v_cron_jobs'])) $errors[] = 'cron jobs';
34+
if (!isset($_POST['v_backups'])) $errors[] = 'backups';
35+
if (!isset($_POST['v_disk_quota'])) $errors[] = 'quota';
36+
if (!isset($_POST['v_bandwidth'])) $errors[] = 'bandwidth';
37+
if (empty($_POST['v_ns1'])) $errors[] = 'ns1';
38+
if (empty($_POST['v_ns2'])) $errors[] = 'ns2';
39+
40+
41+
// Protect input
42+
$v_package = escapeshellarg($_POST['v_package']);
43+
$v_template = escapeshellarg($_POST['v_template']);
44+
$v_shell = escapeshellarg($_POST['v_shell']);
45+
$v_web_domains = escapeshellarg($_POST['v_web_domains']);
46+
$v_web_aliases = escapeshellarg($_POST['v_web_aliases']);
47+
$v_dns_domains = escapeshellarg($_POST['v_dns_domains']);
48+
$v_dns_records = escapeshellarg($_POST['v_dns_records']);
49+
$v_mail_domains = escapeshellarg($_POST['v_mail_domains']);
50+
$v_mail_accounts = escapeshellarg($_POST['v_mail_accounts']);
51+
$v_databases = escapeshellarg($_POST['v_databases']);
52+
$v_cron_jobs = escapeshellarg($_POST['v_cron_jobs']);
53+
$v_backups = escapeshellarg($_POST['v_backups']);
54+
$v_disk_quota = escapeshellarg($_POST['v_disk_quota']);
55+
$v_bandwidth = escapeshellarg($_POST['v_bandwidth']);
56+
$v_ns1 = trim($_POST['v_ns1'], '.');
57+
$v_ns2 = trim($_POST['v_ns2'], '.');
58+
$v_ns3 = trim($_POST['v_ns3'], '.');
59+
$v_ns4 = trim($_POST['v_ns4'], '.');
60+
$v_ns = $v_ns1.",".$v_ns2;
61+
if (!empty($v_ns3)) $v_ns .= ",".$v_ns3;
62+
if (!empty($v_ns4)) $v_ns .= ",".$v_ns4;
63+
$v_ns = escapeshellarg($v_ns);
64+
$v_time = escapeshellarg(date('H:i:s'));
65+
$v_date = escapeshellarg(date('Y-m-d'));
66+
67+
// Check for errors
68+
if (!empty($errors[0])) {
69+
foreach ($errors as $i => $error) {
70+
if ( $i == 0 ) {
71+
$error_msg = $error;
72+
} else {
73+
$error_msg = $error_msg.", ".$error;
74+
}
75+
}
76+
$_SESSION['error_msg'] = "Error: field ".$error_msg." can not be blank.";
77+
} else {
78+
exec ('mktemp -d', $output, $return_var);
79+
$tmpdir = $output[0];
80+
unset($output);
81+
82+
// Create package
83+
$pkg = "TEMPLATE=".$v_template."\n";
84+
$pkg .= "WEB_DOMAINS=".$v_web_domains."\n";
85+
$pkg .= "WEB_ALIASES=".$v_web_aliases."\n";
86+
$pkg .= "DNS_DOMAINS=".$v_dns_domains."\n";
87+
$pkg .= "DNS_RECORDS=".$v_dns_records."\n";
88+
$pkg .= "MAIL_DOMAINS=".$v_mail_domains."\n";
89+
$pkg .= "MAIL_ACCOUNTS=".$v_mail_accounts."\n";
90+
$pkg .= "DATABASES=".$v_databases."\n";
91+
$pkg .= "CRON_JOBS=".$v_cron_jobs."\n";
92+
$pkg .= "DISK_QUOTA=".$v_disk_quota."\n";
93+
$pkg .= "BANDWIDTH=".$v_bandwidth."\n";
94+
$pkg .= "NS=".$v_ns."\n";
95+
$pkg .= "SHELL=".$v_shell."\n";
96+
$pkg .= "BACKUPS=".$v_backups."\n";
97+
$pkg .= "TIME=".$v_time."\n";
98+
$pkg .= "DATE=".$v_date."\n";
99+
100+
// Write package
101+
$fp = fopen($tmpdir."/".$_POST['v_package'].".pkg", 'w');
102+
fwrite($fp, $pkg);
103+
fclose($fp);
104+
105+
// Add new package
106+
if (empty($_SESSION['error_msg'])) {
107+
exec (VESTA_CMD."v_add_user_package ".$tmpdir." ".$v_package, $output, $return_var);
108+
if ($return_var != 0) {
109+
$error = implode('<br>', $output);
110+
if (empty($error)) $error = 'Error: vesta did not return any output.';
111+
$_SESSION['error_msg'] = $error;
112+
}
113+
unset($output);
114+
}
115+
116+
// Remove tmpdir
117+
exec ('rm -rf '.$tmdir, $output, $return_var);
118+
unset($output);
119+
120+
// Check output
121+
if (empty($_SESSION['error_msg'])) {
122+
$_SESSION['ok_msg'] = "OK: package <b>".$_POST['v_package']."</b> has been created successfully.";
123+
unset($v_package);
124+
}
125+
126+
}
127+
}
128+
129+
130+
exec (VESTA_CMD."v_list_web_templates json", $output, $return_var);
131+
check_error($return_var);
132+
$templates = json_decode(implode('', $output), true);
133+
unset($output);
134+
135+
exec (VESTA_CMD."v_list_sys_shells json", $output, $return_var);
136+
check_error($return_var);
137+
$shells = json_decode(implode('', $output), true);
138+
unset($output);
139+
140+
// Set default values
141+
if (empty($v_template)) $v_template = 'default';
142+
if (empty($v_shell)) $v_shell = 'nologin';
143+
if (empty($v_web_domains)) $v_web_domains = "'0'";
144+
if (empty($v_web_aliases)) $v_web_aliases = "'0'";
145+
if (empty($v_dns_domains)) $v_dns_domains = "'0'";
146+
if (empty($v_dns_records)) $v_dns_records = "'0'";
147+
if (empty($v_mail_domains)) $v_mail_domains = "'0'";
148+
if (empty($v_mail_accounts)) $v_mail_accounts = "'0'";
149+
if (empty($v_databases)) $v_databases = "'0'";
150+
if (empty($v_cron_jobs)) $v_cron_jobs = "'0'";
151+
if (empty($v_backups)) $v_backups = "'0'";
152+
if (empty($v_disk_quota)) $v_disk_quota = "'0'";
153+
if (empty($v_bandwidth)) $v_bandwidth = "'0'";
154+
if (empty($v_ns1)) $v_ns1 = 'ns1.example.ltd';
155+
if (empty($v_ns2)) $v_ns2 = 'ns2.example.ltd';
156+
157+
158+
include($_SERVER['DOCUMENT_ROOT'].'/templates/admin/menu_add_package.html');
159+
include($_SERVER['DOCUMENT_ROOT'].'/templates/admin/add_package.html');
160+
unset($_SESSION['error_msg']);
161+
unset($_SESSION['ok_msg']);
162+
}
163+
164+
// Footer
165+
include($_SERVER['DOCUMENT_ROOT'].'/templates/footer.html');

0 commit comments

Comments
 (0)