Repository navigation
Expand file tree
/
Copy pathfix-permissions.sh
More file actions
271 lines (248 loc) · 8.85 KB
/
Copy pathfix-permissions.sh
File metadata and controls
271 lines (248 loc) · 8.85 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
263
264
265
266
267
268
269
270
271
#!/usr/bin/env php
<?php
/**
* fix-permissions.php — make every /home/USER/public_html site behave like cPanel/Softaculous.
* - One UNIX user per site (directory name under /home)
* - PHP-FPM pool runs AS that user, with its own unix socket
* - Apache proxies PHP for that docroot to that socket
* - Filesystem: dirs 755, files 644, owner USER:USER
* - Idempotent: prints OK when no changes are needed
*
* Run as root: php fix-permissions.php (or chmod +x and run directly)
*/
error_reporting(E_ALL);
ini_set('display_errors', 'stderr');
function sh($cmd, &$out=null, &$code=null) {
$out = [];
exec($cmd . ' 2>&1', $out, $code);
return $code === 0;
}
function file_put_contents_if_changed($path, $content, $mode = 0644, $ownerUser=null, $ownerGroup=null) {
$changed = !is_file($path) || file_get_contents($path) !== $content;
if ($changed) {
if (!is_dir(dirname($path))) {
mkdir(dirname($path), 0755, true);
}
file_put_contents($path, $content);
chmod($path, $mode);
if ($ownerUser !== null && $ownerGroup !== null) {
@chown($path, $ownerUser);
@chgrp($path, $ownerGroup);
}
echo "[FIXED] wrote ". $path . PHP_EOL;
} else {
echo "[OK] ". $path . PHP_EOL;
}
return $changed;
}
function is_debian_like() {
return is_file('/etc/debian_version') || is_dir('/etc/apt');
}
function is_rhel_like() {
return is_file('/etc/redhat-release') || is_dir('/etc/yum.repos.d') || is_dir('/etc/dnf');
}
function detect_web_group() {
// Only used for socket file readability; Apache still runs as www-data/apache.
if (is_debian_like()) return 'www-data';
if (is_rhel_like()) return 'apache';
// fallback
return 'www-data';
}
function detect_php_fpm_version() {
// Prefer highest installed FPM on Debian-style /etc/php/*/fpm
$candidates = [];
foreach (glob('/etc/php/*/fpm', GLOB_ONLYDIR) ?: [] as $dir) {
$v = basename(dirname($dir)); // e.g., 8.3
if (preg_match('/^\d+\.\d+$/', $v)) $candidates[] = $v;
}
if ($candidates) {
usort($candidates, 'version_compare');
return end($candidates); // highest
}
// RHEL-ish: try to read `php-fpm -v`
$out = [];
if (sh('php-fpm -v', $out)) {
if (preg_match('/PHP\s+(\d+\.\d+)\./', implode("\n", $out), $m)) return $m[1];
}
// fallback to 8.3
return '8.3';
}
function service_reload($name) {
// Try systemctl, then service
$out=[]; $code=0;
if (sh("systemctl reload $name", $out, $code)) return true;
if (sh("systemctl restart $name", $out, $code)) return true;
if (sh("service $name reload", $out, $code)) return true;
if (sh("service $name restart", $out, $code)) return true;
echo "[WARN] Failed to reload/restart $name: ".implode(' | ',$out).PHP_EOL;
return false;
}
function ensure_mode($path, $mode) {
if (!file_exists($path)) return false;
$cur = fileperms($path) & 0777;
if ($cur !== $mode) {
chmod($path, $mode);
echo "[FIXED] chmod ".decoct($mode)." $path".PHP_EOL;
return true;
}
echo "[OK] mode ".decoct($mode)." $path".PHP_EOL;
return false;
}
function ensure_owner($path, $user, $group, $recursive=false) {
if (!file_exists($path)) return false;
$changed = false;
if ($recursive) {
// chown/chgrp recursively via shell for speed
sh("chown -R ".escapeshellarg("$user:$group")." ".escapeshellarg($path));
echo "[FIXED] chown -R $user:$group $path".PHP_EOL;
return true;
} else {
$stat = @stat($path);
$u = $stat ? posix_getpwuid($stat['uid'])['name'] ?? null : null;
$g = $stat ? posix_getgrgid($stat['gid'])['name'] ?? null : null;
if ($u !== $user || $g !== $group) {
@chown($path, $user);
@chgrp($path, $group);
echo "[FIXED] chown $user:$group $path".PHP_EOL;
$changed = true;
} else {
echo "[OK] owner $user:$group $path".PHP_EOL;
}
return $changed;
}
}
if (posix_geteuid() !== 0) {
fwrite(STDERR, "Run as root.\n");
exit(1);
}
$webGroup = detect_web_group();
$phpMinor = detect_php_fpm_version();
$phpFpmService = is_debian_like() ? "php{$phpMinor}-fpm" : "php-fpm";
$apacheService = is_debian_like() ? "apache2" : "httpd";
$apacheConfDir = is_debian_like() ? "/etc/apache2" : "/etc/httpd";
$perSiteConfDir = is_debian_like() ? "$apacheConfDir/conf-available" : "$apacheConfDir/conf.d";
$perSiteConfEnableCmd = function($confFileBasename) use ($apacheConfDir) {
// Debian a2enconf, RHEL auto-loads conf.d
if (is_debian_like()) {
sh("a2enconf ".escapeshellarg($confFileBasename));
}
return true;
};
// Enable Apache modules when on Debian/Ubuntu
if (is_debian_like()) {
sh('a2enmod proxy proxy_fcgi setenvif', $o, $c);
}
// Walk /home/* and process any dir that contains public_html
$homes = glob('/home/*', GLOB_ONLYDIR) ?: [];
$changedPools = false;
$changedApache = false;
foreach ($homes as $home) {
$user = basename($home);
$docroot = "$home/public_html";
if (!is_dir($docroot)) {
echo "[SKIP] $user (no public_html)\n";
continue;
}
echo "=== Processing $user ($docroot) ===\n";
// 1) Baseline ownership and perms (cpanel style)
ensure_owner($home, $user, $user, true); // recursive
// parent traversal
ensure_mode('/home', 0755);
ensure_mode($home, 0755);
if (!is_dir($docroot)) { mkdir($docroot, 0755, true); echo "[FIXED] mkdir $docroot\n"; }
ensure_mode($docroot, 0755);
// Directories 755, Files 644 (under docroot)
sh("find ".escapeshellarg($docroot)." -type d -exec chmod 755 {} \\;");
sh("find ".escapeshellarg($docroot)." -type f -exec chmod 644 {} \\;");
echo "[OK] normalized perms under $docroot (dirs 755, files 644)\n";
// 2) Per-site temp/log dirs
$tmp = "$home/tmp";
$sess = "$home/tmp/sessions";
$logs = "$home/logs";
foreach ([$tmp, $sess, $logs] as $d) {
if (!is_dir($d)) { mkdir($d, 0700, true); echo "[FIXED] mkdir $d\n"; }
ensure_owner($d, $user, $user);
ensure_mode($d, 0700);
}
// 3) Ensure UserSpice common dirs exist (no file creation)
$usersc = "$docroot/usersc";
$includes = "$usersc/includes";
$plugins = "$usersc/plugins";
$widgets = "$usersc/widgets";
foreach ([$usersc, $includes, $plugins, $widgets] as $d) {
if (!is_dir($d)) { mkdir($d, 0755, true); echo "[FIXED] mkdir $d\n"; }
ensure_owner($d, $user, $user);
ensure_mode($d, 0755);
}
// 4) Per-site PHP-FPM pool (runs AS the site user)
if (is_debian_like()) {
$poolDir = "/etc/php/{$phpMinor}/fpm/pool.d";
$poolFile = "$poolDir/{$user}.conf";
} else {
// RHEL-like: still use pool.d, versionless
$poolDir = "/etc/php-fpm.d";
$poolFile = "$poolDir/{$user}.conf";
}
if (!is_dir($poolDir)) { mkdir($poolDir, 0755, true); echo "[FIXED] mkdir $poolDir\n"; }
$sock = "/run/php-fpm-{$user}.sock";
$pool = <<<CONF
[{$user}]
user = {$user}
group = {$user}
listen = {$sock}
listen.owner = {$webGroup}
listen.group = {$webGroup}
pm = ondemand
pm.max_children = 10
pm.process_idle_timeout = 10s
chdir = {$docroot}
php_admin_value[open_basedir] = {$docroot}:/tmp
php_admin_value[upload_tmp_dir] = {$home}/tmp
php_admin_value[session.save_path] = {$home}/tmp/sessions
php_admin_value[error_log] = {$home}/logs/php-error.log
CONF;
if (file_put_contents_if_changed($poolFile, $pool, 0644)) {
$changedPools = true;
}
// 5) Apache routing for this docroot → that pool
// We add a tiny include that only targets this docroot’s PHP files.
if (is_debian_like()) {
$siteConfBase = "php-fpm-{$user}.conf";
$siteConfPath = "{$perSiteConfDir}/{$siteConfBase}";
} else {
$siteConfBase = "php-fpm-{$user}.conf";
$siteConfPath = "{$perSiteConfDir}/{$siteConfBase}";
}
// Use FilesMatch inside a Directory block so it’s scoped to this docroot only.
$apacheSnippet = <<<APC
# Managed by fix-permissions.php
<Directory "{$docroot}">
AllowOverride All
Require all granted
<FilesMatch "\\.php$">
SetHandler "proxy:unix:{$sock}|fcgi://localhost/"
</FilesMatch>
</Directory>
APC;
if (file_put_contents_if_changed($siteConfPath, $apacheSnippet, 0644)) {
$changedApache = true;
if (is_debian_like()) {
$perSiteConfEnableCmd(basename($siteConfPath));
}
}
echo "=== Done $user ===\n\n";
}
// Reload services only if needed
if ($changedPools) {
echo "[INFO] Reloading PHP-FPM ($phpFpmService)\n";
service_reload($phpFpmService);
} else {
echo "[OK] PHP-FPM pools already up to date\n";
}
if ($changedApache) {
echo "[INFO] Reloading Apache\n";
service_reload($apacheService);
} else {
echo "[OK] Apache config already up to date\n";
}
echo "All done.\n";