The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

UAPI Root

Discussion in 'cPanel Developers' started by manoj_ghimire, Jan 20, 2015.

  1. manoj_ghimire

    manoj_ghimire Registered

    Joined:
    Sep 26, 2014
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    hello
    how can I check if the user executing UAPI is a root user or not?
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,724
    Likes Received:
    660
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Hello :)

    Could you elaborate on this question? For instance, per it's description:

    This is documented here:

    Guide to UAPI

    How are you authenticating?

    Thank you.
     
  3. manoj_ghimire

    manoj_ghimire Registered

    Joined:
    Sep 26, 2014
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    I have realised that UAPI is like an app for all cpanel users. I was going in different direction.
    we can call whm api accountsummary likethis ==>
    Code:
    hostname.com:2087/json-api/accountsummary?api.version=1&user=username
    from perl script. What I actually want to make is a custom api getspamdstat, so that I can call my custom module
    Code:
    hostname.com:2087/json-api/getspamstat?api.version=1&month=jan
    to get the JSON format data. Can you please help me on this? I have looked on https://github.com/CpanelInc/Custom-cPanel-Module but this seems rather old.
     
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,724
    Likes Received:
    660
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
  5. cPAndrewHodge

    cPAndrewHodge cPanel UI Developer
    Staff Member

    Joined:
    Dec 10, 2012
    Messages:
    52
    Likes Received:
    3
    Trophy Points:
    8
    cPanel Access Level:
    Root Administrator
    Looks like what you want is to make a custom WHM API 1 function.

    Unfortunately we do not have full modular support for 3rd party API development in WHM like we do in cPanel.

    That said, if you want to make something that will work I can recommend going about this route but there are caveats. The first to be aware of is that any updates to your system will likely overwrite the modifications you make that are necessary. Second is this is not fully supported so you have to go into it knowing there is the possibility that you will break your server and we may not be able to help you. That out of the way....

    To add a new WHM API 1 call you just need to add in a snippit of code similar to this to my %APPLIST = (....

    Code:
    'getspamstat' => [
        {
            'version' => 1,
            'args'    => [qw(any args you need)], #this is not required if this requires no args
            'code'    => sub {
                my ( $metadata, $args ) = @_;
                return Path::To::Module::somefunction( $args, $metadata );
            },
            #Put this in if you want only root to use it, remove it if you want anyone with WHM access to use it.
            'check' => sub {
                return Whostmgr::ACLS::hasroot();
            },
        },
    ],
    
    And for your somefuncton you want to return something similar to this

    Code:
    sub somefunction {
        my ( $args, $metadata ) = @_;
        ...some code...
        $metadata->{'result'} = 1; #if success, if failure use 0
        $metadata->{'reason'} = 'OK';
        return { 'somekey' => $ref_to_data_to_return };
    }
    

    That should be enough to get you going on this. Good luck and please feel free to respond if you run into any hiccups!
     
  6. manoj_ghimire

    manoj_ghimire Registered

    Joined:
    Sep 26, 2014
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    cPAndrewHodge
    Thanks I will try it. is %APPLIST global variable for cpanel.

    cPanelMichael
    I am basically trying to get the stats of the spamassasin. Stats about total mails scanned from date1 to date 2, total spams at the same date range and total clean mails. Here is the function i am working on:

    Code:
    sub get_spam_stat {
        #Spamd Log File
        my ($args, $metadata) = @_;
        my $spamcount;
        #my $log_facility = $opt{'syslog'} || '/var/log/spamlog';
        #Spamd executable Path
        my $spamd_bin = '/usr/local/cpanel/3rdparty/bin/spamd';
        my $fh = IO::File->new();
        my @array;
        if ($fh->open("< $spamd_bin")) {
    	while (<$fh>) {
    	    if (/^my \$log_facility/) {
    		last;
    	    }
    	}
    	@array = (split/\|\|/);
    	if ($array[1]) {
    	    chomp($array[1]);
    	    $array[1] =~ s/['; ]//g;
    	} else {
    	    die "Cannot parse message.";
    	}
    	$fh->close();
        } else {
    	$metadata->{'result'} = 0;
    	$metadata->{'resaon'} = "$!";
    	return;
        }
        if ($array[1] =~ /var|log|[\w]+/i) {
    	my ($spam_count, $spam_checked, $clean_mails);
    	$fh = IO::File->new();
    	if ($fh->open("< $array[1]")) {
    	    while (<$fh>) {
    		if (/spamd:.*identified/) {
    		    $spam_count++;
    		}
    		if (/spamd:.*checking.*message/) {
    		    $spam_checked++;
    		}
    		if (/spamd:.*clean.*message/) {
    		    $clean_mails++;
    		}
    	    }
    	    $fh->close();
    	}
    	$metadata->{'result'} = 1;
    	$metadata->{'reason'} = 'OK';
    	return {'spam_stats' => [{'total_spams'=> $spam_count, 'total_msg_checked' => $spam_checked, 'clean_msg' => $clean_mails}]};
        }
    }
     
    #6 manoj_ghimire, Jan 22, 2015
    Last edited by a moderator: Jan 22, 2015
Loading...

Share This Page