FTP-User directory change for customers

Discussion in 'Developers' Forum' started by Croydon, May 29, 2009.

  1. Croydon

    Croydon Member Howtoforge Staff Moderator HowtoForge Supporter ISPConfig Developer

    I needed customers to be able to create ftp accounts with special home directories inside the web-dir.
    As an administrator is able to set the dir to whatever he likes (e.g. /etc) i made a set of changes to some files.

    Maybe this is useful for some of you (and maybe it is good enough to be integrated into some of the next releases).


    So here are the changes:

    server/modules-available/web_module.inc.php
    PHP:
        $app->modules->registerTableHook('web_domain','web_module','process');
    // ADDED
    $app->modules->registerTableHook('ftp_user','web_module','process');
    // END ADDED
    $app->modules->registerTableHook('shell_user','web_module','process');
    new file: server/plugins-available/ftpuser_base_plugin.inc.php
    (remember to set a symlink to server/plugins-enabled/ directory!)
    PHP:
    <?php

    /*
    Copyright (c) 2007, Till Brehm, projektfarm Gmbh
    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
    are permitted provided that the following conditions are met:

        * Redistributions of source code must retain the above copyright notice,
          this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright notice,
          this list of conditions and the following disclaimer in the documentation
          and/or other materials provided with the distribution.
        * Neither the name of ISPConfig nor the names of its contributors
          may be used to endorse or promote products derived from this software without
          specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    */

    class ftpuser_base_plugin {
        
        var 
    $plugin_name 'ftpuser_base_plugin';
        var 
    $class_name 'ftpuser_base_plugin';
        
        
    //* This function is called during ispconfig installation to determine
        //  if a symlink shall be created for this plugin.
        
    function onInstall() {
            global 
    $conf;
            
            if(
    $conf['services']['web'] == true) {
                return 
    true;
            } else {
                return 
    false;
            }
            
        }
        
            
        
    /*
             This function is called when the plugin is loaded
        */
        
        
    function onLoad() {
            global 
    $app;
            
            
    /*
            Register for the events
            */
            
            
    $app->plugins->registerEvent('ftp_user_insert',$this->plugin_name,'insert');
            
    $app->plugins->registerEvent('ftp_user_update',$this->plugin_name,'update');
            
    $app->plugins->registerEvent('ftp_user_delete',$this->plugin_name,'delete');

            
        }
        
        
        function 
    insert($event_name,$data) {
            global 
    $app$conf;
            
        if(!
    is_dir($data['new']['dir'])) {
          
    $app->log("FTP User directory '".$data['new']['dir']."' does not exist. Creating it now.",LOGLEVEL_DEBUG);
          
          
    $web $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".intval($data['new']['parent_domain_id']));
          
          
    exec('mkdir -p '.escapeshellcmd($data['new']['dir']));
          
    exec('chown '.escapeshellcmd($web["system_user"]).':'.escapeshellcmd($web['system_group']).' '.$data['new']['dir']);
          
          
    $app->log("Added ftpuser_dir: ".$data['new']['dir'],LOGLEVEL_DEBUG);
        }
        
        }
        
        function 
    update($event_name,$data) {
            global 
    $app$conf;
            
        if(!
    is_dir($data['new']['dir'])) {
          
    $app->log("FTP User directory '".$data['new']['dir']."' does not exist. Creating it now.",LOGLEVEL_DEBUG);
          
          
    $web $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".intval($data['new']['parent_domain_id']));
          
          
    exec('mkdir -p '.escapeshellcmd($data['new']['dir']));
          
    exec('chown '.escapeshellcmd($web["system_user"]).':'.escapeshellcmd($web['system_group']).' '.$data['new']['dir']);
          
          
    $app->log("Added ftpuser_dir: ".$data['new']['dir'],LOGLEVEL_DEBUG);
        }
        }
        
        function 
    delete($event_name,$data) {
            global 
    $app$conf;
            
        
    $app->log("Ftpuser:".$data['new']['username']." deleted.",LOGLEVEL_DEBUG);
            
        }
        
        
        

    // end class

    ?>

    new file: interface/web/sites/templates/ftp_user_advanced_client.htm
    HTML:
    <h2><tmpl_var name="list_head_txt"></h2>
    <p><tmpl_var name="list_desc_txt"></p>
    
    <div class="panel panel_ftp_user">
    
      <div class="pnl_formsarea">
        <fieldset class="inlineLabels">
          <div class="ctrlHolder">
          	<label for="dir">{tmpl_var name='dir_txt'}</label>
            <input name="dir" id="dir" value="{tmpl_var name='dir'}" size="30" maxlength="255" type="text" class="textInput" />
    			</div>
        </fieldset>
    
        <input type="hidden" name="id" value="{tmpl_var name='id'}">
    
        <div class="buttonHolder buttons">
          <button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitForm('pageForm','sites/ftp_user_edit.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
          <button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('sites/ftp_user_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
        </div>
      </div>
      
    </div>
    
    new language entry in "interface/web/sites/lib/lang/en_ftp_user.lng"
    PHP:
    $wb['directory_error_notinweb'] = 'Directory not inside of web root directory.';
    in "interface/web/sites/form/ftp_user.tform.php" add the following code near the bottom (else block for the "if is admin" check)

    PHP:
    .......
    } else {

    $form["tabs"]['advanced'] = array (
        
    'title'     => "Options",
        
    'width'     => 100,
        
    'template'  => "templates/ftp_user_advanced_client.htm",
        
    'fields'    => array (
        
    ##################################
        # Begin Datatable fields
        ##################################
            
    'dir' => array (
                
    'datatype'  => 'VARCHAR',
                
    'formtype'  => 'TEXT',
                
    'validators'    => array (  => array (    'type'  => 'NOTEMPTY',
                                                            
    'errmsg'=> 'directory_error_empty'),
                                            
    => array (    'type'  => 'CUSTOM',
                                                            
    'class' => 'validate_ftpuser',
                                                            
    'function' => 'ftp_dir',
                                                            
    'errmsg' => 'directory_error_notinweb'),
                                        ),
                
    'default'   => '',
                
    'value'     => '',
                
    'width'     => '30',
                
    'maxlength' => '255'
            
    ),
        
    ##################################
        # ENDE Datatable fields
        ##################################
        
    )
    );

    }

    create a new validation class file "interface/lib/classes/validate_ftpuser.inc.php"
    PHP:
    <?php

    /*
    Copyright (c) 2007, Till Brehm, projektfarm Gmbh
    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
    are permitted provided that the following conditions are met:

        * Redistributions of source code must retain the above copyright notice,
          this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright notice,
          this list of conditions and the following disclaimer in the documentation
          and/or other materials provided with the distribution.
        * Neither the name of ISPConfig nor the names of its contributors
          may be used to endorse or promote products derived from this software without
          specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    */

    class validate_ftpuser {
        
        
    /*
            Validator function to check if a given dir is ok.
        */
        
    function ftp_dir($field_name$field_value$validator) {
            global 
    $app;
            
            if(
    $app->tform->primary_id == 0) {
                
    $errmsg $validator['errmsg'];
                if(isset(
    $app->tform->wordbook[$errmsg])) {
                    return 
    $app->tform->wordbook[$errmsg]."<br>\r\n";
                } else {
                    return 
    $errmsg."<br>\r\n";
                }
            }
            
            
            
    $ftp_data $app->db->queryOneRecord("SELECT parent_domain_id FROM ftp_user WHERE ftp_user_id = '".$app->db->quote($app->tform->primary_id)."'");
            if(!
    $ftp_data["parent_domain_id"]) {
                
    $errmsg $validator['errmsg'];
                if(isset(
    $app->tform->wordbook[$errmsg])) {
                    return 
    $app->tform->wordbook[$errmsg]."<br>\r\n";
                } else {
                    return 
    $errmsg."<br>\r\n";
                }
            }
            
            
    $domain_data $app->db->queryOneRecord("SELECT domain_id, document_root FROM web_domain WHERE domain_id = '".$app->db->quote($ftp_data["parent_domain_id"])."'");
            if(!
    $domain_data["domain_id"]) {
                
    $errmsg $validator['errmsg'];
                if(isset(
    $app->tform->wordbook[$errmsg])) {
                    return 
    $app->tform->wordbook[$errmsg]."<br>\r\n";
                } else {
                    return 
    $errmsg."<br>\r\n";
                }
            }
            
            
    $doc_root $domain_data["document_root"];
            
    $is_ok false;
            if(
    $doc_root == $field_value$is_ok true;
            
            
    $doc_root .= "/";
            if(
    substr($field_value0strlen($doc_root)) == $doc_root$is_ok true;
            
            if(
    $is_ok == false) {
                
    $errmsg $validator['errmsg'];
                if(isset(
    $app->tform->wordbook[$errmsg])) {
                    return 
    $app->tform->wordbook[$errmsg]."<br>\r\n";
                } else {
                    return 
    $errmsg."<br>\r\n";
                }
            }
        }
        
        
        
        
    }

    this should be all.

    Note, that changing the server module and adding the server plugin is optional. It is only for automatically creating the new ftp dir if it doesnt exist.
     
  2. cf3d

    cf3d New Member

    Fantastic!!!!!
     
  3. cf3d

    cf3d New Member

    This is a great "plugin" if I can call it that.

    I was wondering if this could be do. It's kinda hard for me to explain so just tell me if I sound stupid. lol.

    I would like for the root path to be set to something like this:

    /var/www/clients/client1/web1/web/

    and have it hidden to where the user only sees a blank line and all they have to enter is the folder name. This would ensure that they do not try to go above the root.
     
  4. Edek7

    Edek7 New Member

    That is simple to do:

    go to /usr/local/ispconfig/interface/web/sites/ftp_user_edit.php and add the following code:

    $dir="{$dir}/web"

    after two lines in code that are :

    $dir = $web["document_root"];

    From this time you don`t have to manually add /web :)
     

Share This Page