Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Looking for a better way to track forwards

Discussion in 'cPanel Developers' started by rorymckinley, Apr 15, 2019.

Tags:
  1. rorymckinley

    rorymckinley Registered

    Joined:
    Jul 4, 2018
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    South Africa
    cPanel Access Level:
    Root Administrator
    I have implemented a script that uses a standardized hook (UAPI::Email::add_forwarder) to track certain details relating to the addition of a forwarder to an email account.

    This is working fine, but I have come across one limitation - the data passed to my methods by the hook does not include the IP address of the actor who added the forward (I am guessing that the IP is missing because hooks can fire in a context where an IP is not present?).

    As I need the IP info, I am forced to scrape the access logs to find matching entries from which I can extract an IP. This solution is less robust than I would like and so I am curious if there is a better way to get the IP address as well as the forward details?
     
  2. cPanelMichael

    cPanelMichael Technical Support Community Manager Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    47,009
    Likes Received:
    2,123
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
    Jean Boudreau likes this.
  3. rorymckinley

    rorymckinley Registered

    Joined:
    Jul 4, 2018
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    South Africa
    cPanel Access Level:
    Root Administrator
    Hello @cPanelMichael - thank you for the speedy response and thank you for the pointer to LastLogin - I was not aware of it.

    If I could just steal a little more of your time I would just like to confirm the behaviour of get_last_or_current_logged_in_ip() in the following scenario:

    Actor A (10.0.0.1) logs in to Webmail.

    A small amount of time passes and then Actor B (10.0.0.2) logs in to Webmail.

    Soon afterwards, Actor A adds a forward to their mailbox.

    If I was to call get_last_or_current_logged_in_ip() at this point would I be correct in assuming that it would return the IP for Actor B (10.0.0.2)?
     
  4. cPanelMichael

    cPanelMichael Technical Support Community Manager Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    47,009
    Likes Received:
    2,123
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello @rorymckinley,

    Thank you for taking the time to provide more information. The LastLogin::get_last_or_current_logged_in_ip UAPI function is useful if you need to find the IP address of the last user to access the cPanel UI. However, the functionality of this UAPI function does not extend to Webmail.

    It's not something I've personally tested, but you may want to look into using the $ip variable documented on the link below:

    Guide to cPanel Variables - Developer Documentation - cPanel Documentation

    Additionally, the document below offers some information on how to use hook action codes:

    Guide to Standardized Hooks - The describe() Method - Developer Documentation - cPanel Documentation

    Let me know if you have any questions.

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. rorymckinley

    rorymckinley Registered

    Joined:
    Jul 4, 2018
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    South Africa
    cPanel Access Level:
    Root Administrator
    Thanks @cPanelMichael - that definitely looks like it would do the trick!

    Sadly, I have found a wrinkle. The standardized hooks documentation states "Hook action code in a custom Perl module can access cPanel environment variables" and I have implemented it as per the docs - but I cannot get an instance of Cpanel::LiveAPI.

    Looking at the code in combination with some rudimentary debugging, it appears that the failure to instantiate is caused by a missing socketfile. Is there something I should be doing to create this socketfile?

    Code:
    my $socketfile = $ENV{'CPANEL_CONNECT_SOCKET'} || $ENV{'CPANEL_PHPCONNECT_SOCKET'};

    Side note: The documentation for the variables shows example usage as follows:

    Code:
    $cpliveapi->cpanelprint('$appname'});
    Is the closing '}' a typo?
     
  6. cPanelMichael

    cPanelMichael Technical Support Community Manager Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    47,009
    Likes Received:
    2,123
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello @rorymckinley,

    Upon further review, I don't believe the $ip variable is best suited for your use-case. The LiveAPI System let's you setup custom environments for applications in the cPanel interface. However, in your case, you want to fetch the Webmail user's IP address directly through the hook action code instead of the interface.

    I recommend opening a feature request for the inclusion of a feature that tracks Webmail user's IP addresses:

    Submit A Feature Request

    In the meantime, you will likely need to implement a method for your script to obtain that IP address from /var/log/maillog.

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice