#!/usr/bin/perl
# cpanel - disable_cphulkd                        Copyright(c) 2007 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use strict;

if ( !-e '/etc/pam.d/system-auth' ) {
    print "Unable to locate the pam configuration.\n";
    exit 1;
}

if ( open my $pam_fh, '+<', '/etc/pam.d/system-auth' ) {
    my @pam_conf;
    my $needs_rewrite = 0;
    while ( my $line = readline $pam_fh ) {
        if ( $line =~ m/pam_hulk\.so\s*$/ ) {
            print "Located cphulkd pam module.\n";
            $needs_rewrite = 1;
            next;
        }
        push @pam_conf, $line;
    }
    if ($needs_rewrite) {
        seek( $pam_fh, 0, 0 );
        print {$pam_fh} join( '', @pam_conf );
        truncate( $pam_fh, tell($pam_fh) );
        print "Pam configuration updated.\n";
    }
    close $pam_fh;
}
else {
    print "Failed to open the pam configuration file: $!";
    exit 1;
}
