-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebdav.php
More file actions
262 lines (223 loc) · 10.1 KB
/
webdav.php
File metadata and controls
262 lines (223 loc) · 10.1 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
* Extend the HestiaCP Pluginable object with our WebDAV object for
* providing a WebDAV server for each HestiaCP user account; enabling
* users to access their web folder for all their domains.
*
* @version 1.0.0
* @license GPL-3.0
* @link https://github.com/virtuosoft-dev/hcpp-webdav
*
*/
if ( ! class_exists( 'WebDAV') ) {
class WebDAV {
/**
* Constructor, listen for add, update, or remove users.
*/
public function __construct() {
global $hcpp;
$hcpp->webdav = $this;
$hcpp->add_action( 'dev_pw_generate_website_cert', [ $this, 'dev_pw_generate_website_cert' ] );
$hcpp->add_action( 'post_change_user_shell', [ $this, 'post_change_user_shell' ] );
$hcpp->add_action( 'hcpp_invoke_plugin', [ $this, 'hcpp_invoke_plugin' ] );
$hcpp->add_action( 'post_delete_user', [ $this, 'post_delete_user' ] );
$hcpp->add_action( 'priv_delete_user', [ $this, 'priv_delete_user' ] );
$hcpp->add_action( 'post_add_user', [ $this, 'post_add_user' ] );
$hcpp->add_action( 'hcpp_rebooted', [ $this, 'hcpp_rebooted' ] );
$hcpp->add_action( 'hcpp_plugin_disabled', [ $this, 'hcpp_plugin_disabled' ] );
$hcpp->add_action( 'hcpp_plugin_enabled', [ $this, 'hcpp_plugin_enabled' ] );
}
// Stop services on plugin disabled.
public function hcpp_plugin_disabled( $plugin ) {
if ( $plugin === 'webdav') $this->stop();
return $plugin;
}
// Start services on plugin enabled.
public function hcpp_plugin_enabled( $plugin ) {
if ( $plugin == 'webdav' ) $this->start();
return $plugin;
}
// Intercept the certificate generation and copy over ssl certs for the webdav domain.
public function dev_pw_generate_website_cert( $cmd ) {
if ( strpos( $cmd, '/webdav-' ) !== false && strpos( $cmd, '/dev_pw_ssl && ') !== false ) {
// Omit the v-delete-web-domain-ssl, v-add-web-domain-ssl, and v-add-web-domain-ssl-force cmds.
global $hcpp;
$path = $hcpp->delLeftMost( $cmd, '/usr/local/hestia/bin/v-add-web-domain-ssl' );
$path = '/home' . $hcpp->delLeftMost( $path, '/home' );
$path = $hcpp->delRightMost( $path, '/dev_pw_ssl &&' );
$cmd = $hcpp->delRightMost( $cmd, '/usr/local/hestia/bin/v-delete-web-domain-ssl ' );
$cmd .= " mkdir -p $path/ssl ; cp -r $path/dev_pw_ssl/* $path/ssl ";
$cmd = $hcpp->do_action( 'webdav_generate_website_cert', $cmd );
}
return $cmd;
}
// Setup WebDAV for all users on reboot.
public function hcpp_rebooted() {
$this->start();
}
// Respond to invoke-plugin webdav_restart.
public function hcpp_invoke_plugin( $args ) {
if ( $args[0] === 'webdav_restart' ) {
$this->restart();
}
return $args;
}
// Get the base domain; cache it for future use.
public function get_base_domain() {
global $hcpp;
// Get the domain.
if ( ! property_exists( $hcpp, 'domain' ) ) {
$hcpp->domain = trim( shell_exec( 'hostname -d' ) );
}
return $hcpp->domain;
}
// Restart WebDAV services when user added.
public function post_add_user( $args ) {
global $hcpp;
$hcpp->log( $hcpp->run( 'invoke-plugin webdav_restart' ) );
return $args;
}
// Restart WebDAV services when shell changes.
public function post_change_user_shell( $args ) {
global $hcpp;
$hcpp->log( $hcpp->run( 'invoke-plugin webdav_restart' ) );
return $args;
}
// Restart WebDAV services.
public function restart() {
$this->stop();
$this->start();
}
// Start all WebDAV services.
public function start() {
// Gather list of all users
$cmd = "/usr/local/hestia/bin/v-list-users json";
$result = shell_exec( $cmd );
try {
$result = json_decode( $result, true, 512, JSON_THROW_ON_ERROR );
} catch (Exception $e) {
var_dump( $e );
return;
}
// Setup WebDAV for each valid user
foreach( $result as $key=> $value ) {
if ( $key === 'admin') continue;
if ( $value['SHELL'] !== 'bash' ) continue;
$this->setup( $key );
}
// Restart nginx
global $hcpp;
$cmd = '(service nginx restart) > /dev/null 2>&1 &';
$cmd = $hcpp->do_action( 'webdav_nginx_reload', $cmd );
shell_exec( $cmd );
}
// Stop all WebDAV services.
public function stop() {
// Find all rclone webdav processes for the /home folder
$cmd = 'ps ax | grep "rclone serve webdav" | grep "/home" | grep -v grep';
$processes = explode( PHP_EOL, shell_exec( $cmd ) );
// Loop through each process and extract the process ID (PID)
foreach ($processes as $process) {
$pid = preg_replace('/^\s*(\d+).*$/', '$1', $process);
// Kill the process
shell_exec( "kill $pid" );
global $hcpp;
$hcpp->log( "Killed rclone webdav process $pid" );
}
// Remove service link and reload nginx
global $hcpp;
$cmd = '(rm -f /etc/nginx/conf.d/domains/webdav-* ; service nginx restart) > /dev/null 2>&1 &';
$cmd = $hcpp->do_action( 'webdav_nginx_restart', $cmd );
shell_exec( $cmd );
}
// Setup WebDAV services for user.
public function setup( $user ) {
global $hcpp;
$hcpp->log( "Setting up WebDAV for $user" );
$domain = $this->get_base_domain();
// Get user account first IP address.
$ip = array_key_first(
json_decode( shell_exec( '/usr/local/hestia/bin/v-list-user-ips ' . $user . ' json' ), true )
);
// Get a port for the WebDAV service.
$port = $hcpp->allocate_port( 'webdav', $user );
// Create the configuration folder.
if ( ! is_dir( "/home/$user/conf/web/webdav-$user.$domain" ) ) {
mkdir( "/home/$user/conf/web/webdav-$user.$domain" );
}
// Create the password file.
$pw_hash = trim( shell_exec( "grep '^$user:' /etc/shadow" ) );
file_put_contents( "/home/$user/conf/web/webdav-$user.$domain/.htpasswd", $pw_hash );
// Create the nginx.conf file.
$conf = "/home/$user/conf/web/webdav-$user.$domain/nginx.conf";
$content = file_get_contents( __DIR__ . '/conf-web/nginx.conf' );
$content = str_replace(
['%ip%', '%user%', '%domain%', '%port%'],
[$ip, $user, $domain, $port],
$content
);
file_put_contents( $conf, $content );
// Create the nginx.ssl.conf file.
$ssl_conf = "/home/$user/conf/web/webdav-$user.$domain/nginx.ssl.conf";
$content = file_get_contents( __DIR__ . '/conf-web/nginx.ssl.conf' );
$content = str_replace(
['%ip%', '%user%', '%domain%', '%port%'],
[$ip, $user, $domain, $port],
$content
);
file_put_contents( $ssl_conf, $content );
// Generate website cert if it doesn't exist for Devstia Personal Web edition.
if ( property_exists( $hcpp, 'dev_pw' ) ) {
if ( !is_dir( "/home/$user/conf/web/webdav-$user.$domain/ssl" ) ) {
$hcpp->dev_pw->generate_website_cert( $user, ["webdav-$user.$domain"] );
}
}else{
// Force SSL on non-Devstia Personal Web edition.
$force_ssl_conf = "/home/$user/conf/web/webdav-$user.$domain/nginx.forcessl.conf";
$content = "return 301 https://\$host\$request_uri;";
file_put_contents( $force_ssl_conf, $content );
// TODO: support for LE
}
// Create the nginx.conf configuration symbolic links.
$link = "/etc/nginx/conf.d/domains/webdav-$user.$domain.conf";
if ( ! is_link( $link ) ) {
symlink( $conf, $link );
}
// Create the nginx.ssl.conf configuration symbolic links.
$link = "/etc/nginx/conf.d/domains/webdav-$user.$domain.ssl.conf";
if ( ! is_link( $link ) ) {
symlink( $ssl_conf, $link );
}
// Start the WebDAV service on the given port.
$cmd = 'runuser -l ' . $user . ' -c "';
$cmd .= "(rclone serve webdav --config none --addr $ip:$port /home/$user/web) > /dev/null 2>&1 &";
$cmd .= '"';
$cmd = $hcpp->do_action( 'webdav_rclone_cmd', $cmd );
shell_exec( $cmd );
}
// Delete the NGINX configuration reference and server when the user is deleted.
public function priv_delete_user( $args ) {
global $hcpp;
$user = $args[0];
$domain = $this->get_base_domain();
$link = "/etc/nginx/conf.d/domains/webdav-$user.$domain.conf";
if ( is_link( $link ) ) {
unlink( $link );
}
$link = "/etc/nginx/conf.d/domains/webdav-$user.$domain.ssl.conf";
if ( is_link( $link ) ) {
unlink( $link );
}
// Delete user port
$hcpp->delete_port( 'webdav', $user );
return $args;
}
// Restart the WebDAV service when a user is deleted.
public function post_delete_user( $args ) {
global $hcpp;
$hcpp->log( $hcpp->run( 'invoke-plugin webdav_restart' ) );
return $args;
}
}
new WebDAV();
}