diff --git a/include/class-FTP.php b/include/class-FTP.php index bee0e9f..580c057 100644 --- a/include/class-FTP.php +++ b/include/class-FTP.php @@ -1,190 +1,200 @@ . // load dependent traits class_exists( 'Domain' ); /** * Methods for an FTP class */ trait FTPTrait { use DomainTrait; /** * Get the FTP login username * * @return string E-mail */ public function getFTPLogin() { return $this->get( 'ftp_login' ); } /** * Check if this FTP account is active * * @return bool */ public function isFTPActive() { return $this->get( 'ftp_active' ); } /** * Get the sanitized raw directory * * This value is sanitized before saving but it can be a malicious string * if the database is overtaken so it's sanitized again. * * It always start with a filesystem directory separator. * * @return string */ public function getFTPRawDirectory() { // return a default '/' if empty $path = $this->get( 'ftp_directory' ); if( !$path ) { return '/'; } // even if it was sanitized during creation, double-sanitize // to protect against hacked databases validate_subdirectory( $path ); return $path; } /** * Get the sanitized absolute FTP directory pathname * * @return string */ public function getFTPAbsoluteDirectory() { // get the sanitized domain base path $absolute_base = $this->getDomainBasePath(); // get the sanitized FTP raw directory $relative_path = $this->getFTPRawDirectory(); // at this point the absolute realpath does not end with a slash // at this point the relative path always start with a slash return $absolute_base . __ . $relative_path; } /** * Get the mailbox permalink * * @return string */ public function getFTPPermalink( $absolute = false ) { return FTP::permalink( $this->getDomainName(), $this->getFTPLogin() ); } protected function normalizeFTP() { $this->normalizeDomain(); $this->booleans( 'ftp_active' ); $this->integers( 'ftp_ulbandwidth', 'ftp_dlbandwidth', 'ftp_quotasize', 'ftp_quotafiles' ); } } /** * An FTP user */ class FTP extends Queried { use FTPTrait; /** * Table name */ const T = 'ftp'; /** * Constructor * * Normalize the object obtained from the database */ public function __construct() { $this->normalizeFTP(); } /** * Get the FTP permalink from domain name and FTP login * * @param string $domain Domain name * @param string $login FTP user login * @return string */ public static function permalink( $domain, $login = null ) { $url = sprintf( '%s/%s', ROOT . '/ftp.php', $domain ); if( $login ) { $url .= "/$login"; } return $url; } /** * Encrypt an FTP user password * * @param $password string Clear text password * @return string One-way encrypted password */ public static function encryptPassword( $password ) { global $HOSTING_CONFIG; // the FTP password encryption mechanism can be customized if( isset( $HOSTING_CONFIG->FTP_ENCRYPT_PWD ) ) { return call_user_func( $HOSTING_CONFIG->FTP_ENCRYPT_PWD, $password ); } // or then just a default behaviour /** * The default behaviour is to adopt the crypt() encryption mechanism * with SHA512 and some random salt. It's strong enough nowadays. * * Read your FTP server documentation, whatever you are using. * We don't know how your infrastructure works, so we don't know * how you want your password encrypted in the database and what kind * of password encryption mechanisms your FTP server supports. * * In short if you are using PureFTPd this default configuration may work * because you may have PureFTPd configured as follow: * ... * MYSQLCrypt crypt * ... * * You can read more here: * https://download.pureftpd.org/pub/pure-ftpd/doc/README.MySQL * * Anyway you can use whatever FTP server that talks with a MySQL database * and so you should adopt the most stronger encryption mechanism available. */ $salt = bin2hex( openssl_random_pseudo_bytes( 3 ) ); return '{SHA512-CRYPT}' . crypt( $password, "$6$$salt" ); } + + /** + * Normalize the FTP username + * + * @param string $username + */ + public function normalizeUsername( $username ) { + return luser_input( $username, 64 ); + } + } diff --git a/www/ftp.php b/www/ftp.php index 2cc58e0..adebb83 100644 --- a/www/ftp.php +++ b/www/ftp.php @@ -1,208 +1,209 @@ . /* * This is the single FTP account creation/edit page */ // load framework require '../load.php'; // wanted informations $domain = null; $ftp = null; $ftp_password = null; // URL paramenters (maximum both domain and FTP login, minimum just domain) list( $domain_name, $ftp_login ) = url_parts( 2, 1 ); // eventually retrieve mailforward from database if( $ftp_login ) { $ftp = ( new FTPAPI() ) ->select( [ 'domain.domain_ID', 'domain_name', 'ftp_login', 'ftp_directory', ] ) ->joinFTPDomain() ->whereDomainName( $domain_name ) ->whereFTPLogin( $ftp_login ) ->whereDomainIsEditable() ->queryRow(); // 404 $ftp or PageNotFound::spawn(); // recycle the mailforward object that has domain informations $domain = $ftp; } // eventually retrieve domain from database if( ! $domain ) { $domain = ( new DomainAPI() ) ->select( [ 'domain.domain_ID', 'domain.domain_name', ] ) ->whereDomainName( $domain_name ) ->whereDomainIsEditable() ->queryRow(); // 404 $domain or PageNotFound::spawn(); } if( ! $ftp ) { // to create an FTP user, must edit all FTP users require_permission( 'edit-ftp-all' ); } // save destination action if( is_action( 'ftp-save' ) ) { $data = []; // during creation require FTP login if( !$ftp ) { + + // require all the submitted data if( !isset( $_POST['ftp_login'] ) || !is_string( $_POST['ftp_login'] ) ) { - BadRequest::spawn( __( "missing parameter" ) ); + BadRequest::spawn(); } // generate the username - MUST start with domain name $username = generate_slug( $domain->getDomainName() ) . '_' . $_POST['ftp_login']; - $username = luser_input( $username, 128 ); + $username = FTP::normalizeUsername( $username, 128 ); // validate username if( !validate_mailbox_username( $username ) ) { BadRequest::spawn( __( "invalid username" ) ); } // check existence $ftp_exists = ( new FTPAPI() ) ->select( 1 ) ->whereDomain( $domain ) ->whereFTPLogin( $username ) ->queryRow(); // die if exists if( $ftp_exists ) { BadRequest::spawn( __( "FTP account already existing" ) ); } // save these fields $data['domain_ID'] = $domain->getDomainID(); $data['ftp_login'] = $username; } else { $username = $ftp->getFTPLogin(); } // no FTP directory no party $ftp_directory = $_POST['ftp_directory'] ?? ''; // validate the FTP directory and save or die try { validate_subdirectory( $ftp_directory ); } catch( Exception $e ) { error_log( $e->getMessage() ); BadRequest::spawn( sprintf( __( "invalid Sub-Directory: %s" ), $e->getMessage() ) ); } // at this point the directory is safe $data['ftp_directory'] = $ftp_directory ?? null; // during creation generate a random password if( !$ftp ) { $ftp_password = generate_password(); $data['ftp_password'] = FTP::encryptPassword( $ftp_password ); } // insert or update if( $ftp ) { ( new FTPAPI() ) ->whereFTP( $ftp ) ->update( $data ); } else { ( new FTPAPI() ) ->insertRow( $data ); } // POST/redirect/GET http_redirect( FTP::permalink( $domain->getDomainName(), $username, true ), 303 ); } // change password action if( $ftp && is_action( 'ftp-password-reset' ) ) { // generate a password and die $ftp_password = generate_password(); $ftp_password_safe = FTP::encryptPassword( $ftp_password ); // update its password ( new FTPAPI() ) ->whereFTP( $ftp ) ->update( [ 'ftp_password' => $ftp_password_safe, ] ); } -// delete action +// you can delete only if it exists if( $ftp ) { // action fired when deleting a whole mailforward if( is_action( 'ftp-delete' ) ) { // delete the account ( new FTPAPI() ) ->whereFTP( $ftp ) ->delete(); // POST/redirect/GET http_redirect( $domain->getDomainPermalink( true ), 303 ); - } } // spawn header Header::spawn( [ 'uid' => false, 'title-prefix' => __( "FTP user" ), 'title' => $ftp ? $ftp->getFTPLogin() : __( "create" ), 'breadcrumb' => [ new MenuEntry( null, $domain->getDomainPermalink(), $domain->getDomainName() ), ], ] ); // spawn the page content template( 'ftp', [ 'domain' => $domain, 'ftp' => $ftp, 'ftp_password' => $ftp_password, ] ); // spawn the footer Footer::spawn();