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.

Edit hosts file via a cPanel plugin!

Discussion in 'cPanel Developers' started by NixTree, Mar 2, 2012.

  1. NixTree

    NixTree Well-Known Member

    Joined:
    Aug 19, 2010
    Messages:
    386
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Gods Own Country
    cPanel Access Level:
    Root Administrator
    Hello,

    I have to edit /etc/hosts file via a cpanel plugin. That is I will have to add an entry in to /etc/hosts file for a domain that I will read from cPanel user input ( read from a textbox ) via a plugin - later need to delete it as well upon the completion of the task Is there anyway to achieve this in a cpanel plugin?

    If someone can provide a piece of sample code, it will be great!

    I am using PHP to write this up!

    Thank you,
    Nibin.
     
    #1 NixTree, Mar 2, 2012
    Last edited: Mar 2, 2012
  2. andreas.r

    andreas.r Member
    PartnerNOC

    Joined:
    Nov 1, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    DataCenter Provider
    I think it'll be safe not to let a user directly write to /etc/hosts file but save it into some other place and next time you via WHM install required entries by hand. I think it'll be safe.
     
  3. NixTree

    NixTree Well-Known Member

    Joined:
    Aug 19, 2010
    Messages:
    386
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Gods Own Country
    cPanel Access Level:
    Root Administrator
    Hello :)

    Thanks for your reply. Yes, I know it isn't good to set world write permission to hosts file and if that is the case, it is pretty easy. I need real time editing of hosts file and need to know the safest method to do it.

    Thank you,
    Nibin.
     
  4. Infopro

    Infopro cPanel Sr. Product Evangelist
    Staff Member

    Joined:
    May 20, 2003
    Messages:
    14,465
    Likes Received:
    195
    Trophy Points:
    63
    Location:
    Pennsylvania
    cPanel Access Level:
    Root Administrator
    Twitter:
    What is it exactly that you need this for, if I may ask?
     
  5. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    I agree with infopro, what is the problem you are looking to solve by doing this? Just asking because there may be an alternative (likely more secure) way of resolving that problem.

    Also, to clarify - you mean cPanel plugin (the cPanel interface), not WHM plugin - correct?
     
  6. NixTree

    NixTree Well-Known Member

    Joined:
    Aug 19, 2010
    Messages:
    386
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Gods Own Country
    cPanel Access Level:
    Root Administrator
    Hello,

    Yes, it is a cPanel plugin. What i have done now is - I have created a binary ( in C ) with SetUID to post an entry to hosts file for the domain that I read. It won't be a security issue I guess?

    What I need is, I need to read a domain from cpanel and resolve that domain to the server itself ( to the IP its hosted on ). No matter to where that domain actaully points to via its DNS.

    Thank you,
    Nibin.
     
  7. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi Nibin,

    IIRC, by default (via Apache [vhost] config), you should be able to resolve domains hosted on the server by cPanel account name.

    So, consider the following example:

    - my cPanel account is on a box with IP 1.1.1.1 whose FQDN is cpanelbox.tld
    - my cPanel account is 'dave' whose primary domain is 'dave.tld'
    - normally, if someone browses to dave.tld, DNS will send them to 1.1.1.1, and Apache will serve content from /home/dave/public_html/.
    - However, you're saying the we can't/won't rely on DNS. This becomes slightly complicated because if we point directly to the box, how will Apache know the domain vhost to lookup and which directory to server?

    The answer is pretty simple:
    Code:
    ## use tilde character followed by username
    http://1.1.1.1/~dave/
    ## OR ##
    http://cpanelbox.tld/~dave/
    
    Further more, if you need to resolve sub/parked/addon domains, you can make an API call(s) to determine the literal path of that domain content.
    Code:
    ## serve my simply-mapped subdomain lil.dave.tld
    http://1.1.1.1/~dave/lil/
    
    You might even be able to use the loopback address if Apache binds to that (which is likely), instead of the external IP, <shrug/>. The only caveats, other than if you've alter that default Apache vhost behavior, is that mod_rewrite rules *might* behave funny and the fact that many sites are designed to hand an alternating URL base. e.g., links will not be generated that follow the schema necessary to browse the site (without proper DNS resolution), etc.

    With this solution, there's no need to hack at /etc/hosts or anything else. Absolutely no security concerns to system.

    Regards,
    -DavidN
     
  8. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    ...regardless if my previous post will work for your particular situation...If you're determined to edit /etc/hosts (or anything like that...cpuser needs to mod something that's normally outside their privilege/permissions) then yes, the proper solution is to use a SetUID binary. You mentioned you've already created one, but for anyone who needs that, cPanel provides the C source for you to create your own: Privilege Escalation with cPanel API Calls.

    -DavidN
     
Loading...
Similar Threads - Edit hosts file
  1. Saeb
    Replies:
    17
    Views:
    1,532

Share This Page