-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_vhost.php
executable file
·195 lines (153 loc) · 5.23 KB
/
add_vhost.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/php5
<?
#------------------------------------------------------------------#
# database configuration section. Replace the values of the #
# variables below to reflect your environment #
#------------------------------------------------------------------#
$database = array(
'host' => "",
'user' => "",
'pwd' => ""
);
# Assigns command line arguments. This is temporary.
# TODO: Pull in as post
$username = $argv[1];
$password = $argv[2];
$dbusername = $argv[3];
$email = $argv[4];
$home_dir = '/home/vhost/' . $username;
$pass = crypt($password);
$user = array(
'username' => $username,
'password' => $password,
'email' => $email,
'dbusername' => $dbusername,
'home_dir' => $home_dir,
'pass' => $pass,
'plainpass' => $password
);
# sets the mail params
//$response = array('DATA' => array(),'ERROR' => array());
$response = array();
#Begin creating the user
$response = createUser($user,$response);
checkError($response);
//Adds a blank error array to the response array
$response['ERROR']="";
//Echo the response array as json-encoded string
echo json_encode($response);
//Begin functions
function checkError($response) {
#Checks to see if there's any value in the error array. If there is, it will terminate the script and rollback the changes made
if (isset($response['ERROR'])) {
switch($response['ERROR']['CODE']) {
case 1:
//deleteUser();
break;
}
$response['DATA'] = "";
echo json_encode($response);
exit();
}
}
//assignOwner($user);
//createConfig($user);
//createDbUser($database,$user);
//createZone($user);
//sendEmail($user);
function createUser($user,$response) {
# uses system to create the user with hashed password.
$createUser = "useradd -d " . $user['home_dir'] . " -m -k /etc/vhost_skel -p " . $user['pass'] . " " . $user['username'] . " -g vhost";
# Stores the message returned by system to a variable for later user
$message = system(escapeshellcmd($createUser),$retval);
echo $message . "\n" . $retval;
# Checks to ensure the command returned successfully
if ($retval == 0) {
$response['DATA']['FTP_USERNAME'] = $user['username'];
return $response;
} else {
$response['ERROR']['MESSAGE'] = $message;
$response['ERROR']['DEBUG'] = $createUser;
$response['ERROR']['CODE'] = 1;
return $response;
}
}
function assignOwner($user) {
# uses system to assign jail permissions per openssh requirements
system("chown root:root " . $user['home_dir'],$retval);
if ($retval == 0) {
echo "Successfully changed jail permissions\n";
}
}
function createConfig($user) {
# copies the apache config file from template, replaces values, enables site and reloads apache
system("mkdir /var/log/vhosts/" . $user['username'],$retval);
if ($retval == 0) {
echo "Successfully created log directory\n";
}
$template = "conf/apache_template";
$config_file = "/etc/apache2/sites-available/" . $user['username'];
$replace = array('DOMAIN' => $user['username'],'EMAIL' => $user['email']);
$input = file_get_contents($template) or die("Could not open $template");
foreach ($replace as $key => $value) {
$input = str_replace($key, $value, $input);
}
file_put_contents($config_file,$input) or die("Could not write to $config_file");
system("a2ensite " . $user['username']);
system("service apache2 reload");
}
function createDbUser($database,$user) {
#creates a database user. This will be changed to create the db name and user as account number, as the domain name is typically longer than the 16char max
$dbh = mysql_connect($database['host'],$database['user'],$database['pwd']);
$query = "CREATE DATABASE " . $user['dbusername'];
if(mysql_query($query,$dbh)) {
echo "Database user " . $user['dbusername'] . " created\n";
} else {
echo "Database not created: " . mysql_error() . "\n";
}
$query = "GRANT ALL on " . $user['dbusername']. ".* TO '" . $user['dbusername'] ."'@'localhost' IDENTIFIED BY '" . $user['pass'] . "'";
if(mysql_query($query,$dbh)) {
echo "User " . $user['dbusername'] . " assigned to " . $user['dbusername'] .".\n";
} else {
echo "User " . $user['dbusername'] . " not assigned: " . mysql_error() . "\n";
}
}
function createZone($user) {
require_once('linode/class.linode.php');
# This uses the Linode API to create the new zone in DNS.
$linode = new linode();
echo $linode->add_domain($user['username'],$user['email']);
}
function sendEmail($user) {
require_once 'Mail.php';
# sets message parameters
$headers = array (
'From' => "",
'To' => $user['email'],
'Subject' => ""
);
$smtp_params['host']="";
$smtp_params['port']="";
$smtp_params['auth']=true;
$smtp_params['username']="";
$smtp_params['password']="";
$smtp = Mail::factory('smtp',$smtp_params);
$body = "Please find your account information below for ". $user['username'] ."
FTP ACCESS:
Host: . " . $user['username'] . "
Username: " . $user['username'] . "
Password: " . $user['plainpass'] . "
DATABASE ACCESS:
Host: localhost
Username: " . $user['dbusername'] . "
Password: " . $user['plainpass'] . "
We hope you enjoy your services.";
echo "sending email\n";
$mail = $smtp->send($user['email'], $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Welcome message sent\n";
}
}
?>