Menambahkan custom hook di cPanel memungkinkan administrator sistem atau pengembang untuk mendaftarkan skrip ke dalam cPanel sehingga dapat dijalankan secara otomatis sesuai dengan event hook yang dipilih.
Panduan ini ditujukan untuk pengguna cPanel yang memiliki akses root ke server.
Bash script #
Buat file skrip di /usr/local/cpanel/3rdparty/bin
.
nano /usr/local/cpanel/3rdparty/bin/customscript.sh
Lalu tambahkan baris berikut.
#!/bin/bash
cat "${1:-/dev/stdin}" | jq >> /root/debugoutput.txt
Simpan file dan ubah permission agar dapat dieksekusi.
chmod 755 /usr/local/cpanel/3rdparty/bin/customscript.sh
Selanjutnya daftarkan skrip ke hook cPanel dan atur event ke Api2::Fileman::fileop
.
/usr/local/cpanel/bin/manage_hooks add script /usr/local/cpanel/3rdparty/bin/customscript.pl \
--manual \
--category=Cpanel \
--event=Api2::Fileman::fileop \
--stage=post \
--escalateprivs
Jadi saat ada user cPanel yang melakukan aktivitas di file manager seperti extract
, delete
, create
, atau move
, skrip akan dijalankan dan menyimpan output ke
/root/debugoutput.txt
.
# cat /root/debugoutput.txt
{
"context": {
"event": "Api2::Fileman::fileop",
"category": "Cpanel",
"stage": "post",
"point": "main",
"escalateprivs": 1
},
"hook": {
"weight": 100,
"escalateprivs": 1,
"stage": "post",
"hook": "/usr/local/cpanel/3rdparty/bin/customscript.sh",
"exectype": "script",
"id": "6dd07497-42e6-42c7-b016-f11d9ddecfbe"
},
"data": {
"user": "pccmy",
"output": [
{
"output": "Archive: /home/pccmy/public_html/mencoba/test.zip\n creating: cgi-bin/\n inflating: error_log \n creating: Id/\n inflating: Id/index.php \n inflating: Id/mau.txt \n inflating: Id/googledef7769ea26eb891.html \n inflating: Id/googlefbc7836424788349.html \n inflating: Id/sitemap.xml \n extracting: index.php \n creating: situs-toto/\n inflating: situs-toto/index.php \n inflating: situs-toto/sitemap.xml \n inflating: situs-toto/googlefbc7836424788349.html \n",
"src": "/home/pccmy/public_html/mencoba/test.zip",
"result": 1,
"dest": "/home/pccmy/public_html/mencoba"
}
],
"args": {
"cpanel_jsonapi_func": "fileop",
"op": "extract",
"cpanel_jsonapi_module": "Fileman",
"sourcefiles": "/home/pccmy/public_html/mencoba/test.zip",
"doubledecode": "0",
"filelist": "1",
"destfiles": "/public_html/mencoba",
"metadata": "undefined",
"cpanel_jsonapi_apiversion": "2",
"multiform": "1"
}
}
}
Perl script #
Dengan menggunakan Perl, kita dapat dengan mudah mengakses beberapa modul yang disediakan oleh cPanel/WHM.
Buat file MyHook.pm
di salah satu lokasi berikut.
- /opt/cpanel/perl5/514/site_lib/x86_64-linux-64int
- /opt/cpanel/perl5/514/site_lib
- /var/cpanel/perl5/lib
Lalu edit file dengan menambahkan baris berikut
#!/usr/bin/perl
# Package this module.
package MyHook;
# Return errors if Perl experiences problems.
use strict;
use warnings;
# Use cPanel's error logging module.
use IO::Select ();
use Cpanel::Logger ();
use Try::Tiny;
# Properly decode JSON.
use JSON;
# Instantiate the cPanel logging object.
my $MAX_ERROR_LOG_SIZE = ( 1024**2 * 5 ); # 5 MEG
my $MAX_MAIN_LOG_SIZE = ( 1024**2 * 5 ); # 5 MEG
our @LOGS = (
{ 'key' => 'mainlog', 'file' => '/usr/local/cpanel/logs/myhook.log', 'max_size' => $MAX_MAIN_LOG_SIZE, 'fh' => \*STDOUT },
);
# Embed hook attributes alongside the action code.
sub describe {
my $hooks = [
{
'category' => 'Cpanel',
'event' => 'Api2::Fileman::fileop',
'stage' => 'post',
'hook' => 'MyHook::debug',
'exectype' => 'module',
}
];
return $hooks;
}
sub debug {
# Get the data that the system passes to the hook.
my ( $context, $data ) = @_;
# Cari hash reference yang memiliki 'key' => 'mainlog'
my ($log_ref) = grep { $_->{'key'} eq 'mainlog' } @LOGS;
if ($log_ref) {
my $log_file = $log_ref->{'file'};
my $max_size = $log_ref->{'max_size'};
open my $fh, '>>', $log_file or die "Cannot open log file '$log_file' for appending: $!";
# Add a log entry for debugging.
# example /usr/local/cpanel/Cpanel/Hooks.pm
if ( defined $data ) {
require Cpanel::JSON;
my $json_dump = "Error in JSON dump :";
eval { $json_dump = Cpanel::JSON::Dump($data); };
if ($@) {
$json_dump .= "\n\t$@";
}
print $fh "$json_dump\n";
close $fh;
}
}
};
1;
Simpan file lalu setting permissionnya agar dapat dieksekusi.
chmod 755 /var/cpanel/perl5/lib/MyHook.pm
Kemudian daftarkan skrip ke hook cPanel.
/usr/local/cpanel/bin/manage_hooks add module MyHook
Sama seperti Bash script sebelumnya, jika ada aktivitas di file manager maka skrip akan dijalankan dan menyimpan output ke /usr/local/cpanel/logs/myhook.log
.
# cat /usr/local/cpanel/logs/myhook.log
{"args":{"cpanel_jsonapi_module":"Fileman","sourcefiles":"/home/pccmy/public_html/mencoba/test.zip","cpanel_jsonapi_func":"fileop","op":"extract","metadata":"undefined","destfiles":"/public_html/mencoba","cpanel_jsonapi_apiversion":"2","multiform":"1","doubledecode":"0","filelist":"1"},"user":"pccmy","output":[{"result":1,"src":"/home/pccmy/public_html/mencoba/test.zip","dest":"/home/pccmy/public_html/mencoba","output":"Archive: /home/pccmy/public_html/mencoba/test.zip\n creating: cgi-bin/\n inflating: error_log \n creating: Id/\n inflating: Id/index.php \n inflating: Id/mau.txt \n inflating: Id/googledef7769ea26eb891.html \n inflating: Id/googlefbc7836424788349.html \n inflating: Id/sitemap.xml \n extracting: index.php \n creating: situs-toto/\n inflating: situs-toto/index.php \n inflating: situs-toto/sitemap.xml \n inflating: situs-toto/googlefbc7836424788349.html \n"}]}
Referensi: