#!/usr/local/cpanel/3rdparty/bin/perl
# cpanel - autofixer2/hidepid
#                                                  Copyright 2023 cPanel, L.L.C.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package autofixer2::hidepid;

use strict;
use warnings;

use Cpanel::SafeRun::Object ();

exit run() unless caller;

sub run {
    return 0 unless supported_on_this_major( 116, 118 );    # Set this to the Min/Max we support this autofixer on.

    my $sysctl_file = '/etc/sysctl.d/99-cpanel-proc-can-see-other-uid.conf';
    return 0 unless -f $sysctl_file;

    # Change the active sysctl value:
    my ( $sysctl_param, $sysctl_value ) = ( 'fs.proc_can_see_other_uid', 0 );
    my $run = Cpanel::SafeRun::Object->new( 'program' => "/usr/sbin/sysctl", 'args' => ["${sysctl_param}=${sysctl_value}"] );

    if ( $run->CHILD_ERROR() ) {
        warn $run->autopsy();
        warn $run->stderr();
        return 1;
    }

    # You HAVE to run this before remount_proc or it'll put fs.proc_can_see_other_uid back to 1
    unlink $sysctl_file;    # Won't run again.

    # Re-mount /proc after changing the sysctl value:
    $run = Cpanel::SafeRun::Object->new_or_die( 'program' => "/usr/share/cloudlinux/remount_proc.py" );

    return 0;               # exit(0);
}

sub supported_on_this_major {
    my ( $min_ver, $max_ver ) = @_;

    my $major = get_major_version();

    return 0 if $major < $min_ver;
    return 0 if $major > $max_ver;

    return 1;
}

sub get_major_version {
    my $major_version;

    if ( open( my $fh, '<', '/usr/local/cpanel/version' ) ) {
        my $full_version = <$fh>;
        chomp $full_version;
        close($fh);
        ($major_version) = $full_version =~ /^[0-9]+\.([0-9]+)/;
    }

    # Safe default.
    return $major_version || 30;
}

1;
