405 Method Not Allowed"; exit(0); } //print_r($GLOBALS); // Check if the user is a valid ADMIN user $opts = array( 'http' => array( 'method'=>"GET", 'header'=>"Accept-language: en\r\n" . "User-Agent: Softaculous\r\n" . "Cookie: session={$_SERVER['SESSION_ID']}; key={$_SERVER['SESSION_KEY']}\r\n" ) ); //Create a context $context = stream_context_create($opts); // SECURITY : The admin/reseller authorisation below is decided by asking DirectAdmin // "what is the usertype of this session ?". That question MUST reach the real DirectAdmin // daemon. We therefore take its listening port (and SSL flag) from the trusted // directadmin.conf, NOT from $_SERVER['SERVER_PORT'] / $_SERVER['SSL'] which are attacker // controlled when bin/soft is invoked directly from a shell. A forged port lets any local // user run a listener that answers "usertype=admin" and be handed the root admin panel. // This CGI runs as root so the conf (mode 600) is readable; if the port cannot be // determined we fail closed. function da_api_endpoint(){ global $globals; static $endpoint = NULL; if($endpoint !== NULL){ return $endpoint; } $endpoint = array('port' => '', 'ssl' => ''); $conf_file = '/usr/local/directadmin/conf/directadmin.conf'; if(is_readable($conf_file)){ $cdata = file_get_contents($conf_file); if(preg_match('/^\\s*port\\s*=\\s*(\\d+)\\s*$/im', $cdata, $m)){ $endpoint['port'] = $m[1]; } // DirectAdmin speaks HTTPS on its port when ssl=1 (unless told to ignore SSL for local calls) if(preg_match('/^\\s*ssl\\s*=\\s*1\\s*$/im', $cdata) && !preg_match('/^\\s*ssl_ignore_when_local\\s*=\\s*1\\s*$/im', $cdata)){ $endpoint['ssl'] = 's'; } } if(empty($endpoint['port']) && !empty($globals['cp_port'])){ $endpoint['port'] = $globals['cp_port']; } if(empty($endpoint['port'])){ $endpoint['port'] = 2222; } return $endpoint; } function da_verify_query($command, $parse = true, $retry = 0){ $endpoint = da_api_endpoint(); // Fail closed : without the real DirectAdmin port we cannot safely authorise an admin if(empty($endpoint['port'])){ echo "HTTP/1.1 200 OK\n"; echo "Content-Type: text/html\n"; echo "\n"; die("Could not verify the DirectAdmin session. This has been reported to the ADMIN ! \n"); } $ssl = $endpoint['ssl']; // Only the scheme may be toggled on retry (https -> http); the port stays the trusted one if(!empty($retry)){ if($retry == 1){ $ssl = ''; }else{ $ssl = 's'; } } $url = 'http'.$ssl.'://127.0.0.1:'.$endpoint['port'].'/'.$command; $allow_url_fopen = ini_get('allow_url_fopen'); // Is curl there ? if(function_exists('curl_init')){ // Set the curl parameters. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); // Turn off the server and peer verification (TrustManager Concept). curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // UserAgent and Cookies curl_setopt($ch, CURLOPT_USERAGENT, 'Softaculous'); curl_setopt($ch, CURLOPT_COOKIE, "session={$_SERVER['SESSION_ID']}; key={$_SERVER['SESSION_KEY']}"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Get response from the server. $chunk = curl_exec($ch); //echo curl_error($ch); // Try FOPEN }elseif(!empty($allow_url_fopen)){ $fp = fopen($url, 'r', false, $GLOBALS['context']); $chunk = ''; if($fp){ while ( !feof($fp) ){ $chunk .= fgets($fp,1024); } fclose($fp); } }else{ $chunk = ''; } // If it failed with https we will retry with http (same trusted port, scheme downgraded) if(empty($chunk) && empty($retry) && !empty($ssl)){ return da_verify_query($command, $parse, 1); } // If it failed with http we will retry with https (same trusted port, scheme upgrade) if(!empty($chunk) && preg_match('/use https/is', $chunk) && empty($retry) && empty($ssl)){ return da_verify_query($command, $parse, 2); } if($parse){ parse_str($chunk, $ctmp); return $ctmp; }else{ return $chunk; } } $response = da_verify_query('CMD_API_SHOW_USER_CONFIG'); //print_r($response); //print_r($argv); if($response['usertype'] == 'reseller'){ define('IS_DA_RESELLER', 1); }elseif($response['usertype'] == 'admin'){ define('IS_DA_ADMIN', 1); }else{ echo "HTTP/1.1 200 OK\n"; echo "Content-Type: text/html\n"; echo "\n"; die("You are not allowed to access this. This has been reported to the ADMIN ! \n"); } unset($opts); unset($context); if(!empty($_SERVER['QUERY_STRING'])){ parse_str($_SERVER['QUERY_STRING'], $_GET); foreach($_GET as $k => $v){ $_REQUEST[$k] = $v; } } if(!empty($_SERVER['POST'])){ parse_str($_SERVER['POST'], $_POST); foreach($_POST as $k => $v){ $_REQUEST[$k] = $v; } } echo "HTTP/1.1 200 OK\n"; echo "Content-Type: text/html\n"; if(@$_GET['act'] == 'import_export' && !empty($_GET['download']) && $response['usertype'] == 'admin' && substr_count($_GET['download'], './') == 0){ echo "Content-Disposition: attachment; filename=\"".$_GET['download']."\""."\n"; } // Kept for panel code (e.g. Manage Sets upload) that needs to resolve the currently // logged-in DA admin/reseller's own home directory -- this process stays root for its // entire lifetime (no posix_setuid happens for admin/reseller CGIs), so it cannot be // derived from the process's own uid the way user/index.html does it. if(!empty($response['username'])){ define('DA_LOGGED_IN_USERNAME', $response['username']); } unset($response); putenv('soft_egid'); putenv('soft_euid'); echo "\n"; define('SOFTACULOUS', 1); include(dirname(dirname(__FILE__)).'/enduser/universal.php'); if(file_exists($globals['path'].'/conf/universal.json') && is_readable($globals['path'].'/conf/universal.json') && (filesize($globals['path'].'/conf/universal.json') > 0)){ $secure_file = @file_get_contents($globals['path'].'/conf/universal.json'); if(empty($secure_file)){ die('Failed to load universal.json'); } $secure_val = json_decode($secure_file, true); if(is_array($secure_val)){ $globals = array_merge($globals, $secure_val); } } $da_version = @shell_exec('/usr/local/directadmin/directadmin v'); include_once($globals['path'].'/index.php'); ?>