]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Fix access to job resources if no job assigned to user
authorMarcin Haba <marcin.haba@bacula.pl>
Sat, 7 Nov 2020 04:46:11 +0000 (05:46 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Sat, 7 Nov 2020 04:46:11 +0000 (05:46 +0100)
21 files changed:
gui/baculum/protected/API/Class/Bconsole.php
gui/baculum/protected/API/Class/Database.php
gui/baculum/protected/API/Class/JobManager.php
gui/baculum/protected/API/Pages/API/Job.php
gui/baculum/protected/API/Pages/API/JobBandwidthLimit.php
gui/baculum/protected/API/Pages/API/JobEstimate.php
gui/baculum/protected/API/Pages/API/JobFiles.php
gui/baculum/protected/API/Pages/API/JobListFiles.php
gui/baculum/protected/API/Pages/API/JobLog.php
gui/baculum/protected/API/Pages/API/JobResNames.php
gui/baculum/protected/API/Pages/API/JobRun.php
gui/baculum/protected/API/Pages/API/JobShow.php
gui/baculum/protected/API/Pages/API/JobTotals.php
gui/baculum/protected/API/Pages/API/Jobs.php
gui/baculum/protected/API/Pages/API/JobsForClient.php
gui/baculum/protected/API/Pages/API/JobsOnVolume.php
gui/baculum/protected/API/Pages/API/JobsRecent.php
gui/baculum/protected/API/Pages/API/JobsShow.php
gui/baculum/protected/API/Pages/API/Pool.php
gui/baculum/protected/API/Pages/API/Pools.php
gui/baculum/protected/API/Pages/API/VolumesRequired.php

index 2998b85bc14dd44d0b52b337d360174fad460706..b4cfcc90273ea255029d5135c3eec5287d0a9e5a 100644 (file)
@@ -174,7 +174,7 @@ class Bconsole extends APIModule {
                return (object)array('output' => $output, 'exitcode' => (integer)$exitcode);
        }
 
-       public function bconsoleCommand($director, array $command, $ptype = null) {
+       public function bconsoleCommand($director, array $command, $ptype = null, $without_cmd = false) {
                $result = null;
                if (count($this->config) > 0 && $this->config['enabled'] !== '1') {
                        throw new BConsoleException(
@@ -185,6 +185,9 @@ class Bconsole extends APIModule {
                $base_command = count($command) > 0 ? $command[0] : null;
                if($this->isCommandValid($base_command) === true) {
                        $result = $this->execCommand($director, $command, $ptype);
+                       if ($without_cmd) {
+                               array_shift($result->output);
+                       }
                } else {
                        throw new BConsoleException(
                                BconsoleError::MSG_ERROR_INVALID_COMMAND,
index 21ff0fb6194383c60e3d07e59af75f4ffff0097c..62d2cae1aab81e1088ea41e690d29be682b65050 100644 (file)
@@ -158,7 +158,7 @@ class Database extends APIModule {
                return $dbsize;
        }
 
-       public static function getWhere(array $params) {
+       public static function getWhere(array $params, $without_where = false) {
                $where = '';
                $parameters = array();
                if (count($params) > 0) {
@@ -181,7 +181,12 @@ class Database extends APIModule {
                                        $parameters[$pkey] = $pval;
                                }
                        }
-                       $where = ' WHERE (' . implode(') AND (' , $condition) . ')';
+                       if (count($condition) > 0) {
+                               $where = ' (' . implode(') AND (' , $condition) . ')';
+                               if ($without_where === false)  {
+                                       $where = ' WHERE ' . $where;
+                               }
+                       }
                }
                return array('where' => $where, 'params' => $parameters);
        }
index f0abf6eb59e8e3e2d3907465a8a6c6f68d46e78d..0bf804a03963f4371f01e27c11ffe5c31b04b5cc 100644 (file)
@@ -227,10 +227,19 @@ WHERE JobMedia.MediaId='$mediaid' $jobs_criteria";
         * @return array jobs for specific client
         */
        public function getJobsForClient($clientid, $allowed_jobs = array()) {
-               $jobs_criteria = '';
+               $where = '';
                if (count($allowed_jobs) > 0) {
-                       $jobs_sql = implode("', '", $allowed_jobs);
-                       $jobs_criteria = " AND Job.Name IN ('" . $jobs_sql . "')";
+                       $criteria = [
+                               'Job.Name' => [
+                                       'vals' => $allowed_jobs,
+                                       'operator' => 'OR'
+                               ]
+                       ];
+                       $where = Database::getWhere($criteria, true);
+                       $wh = '';
+                       if (count($where['params']) > 0) {
+                               $wh = ' AND ' . $where['where'];
+                       }
                }
                $sql = "SELECT DISTINCT Job.*, 
 Client.Name as client, 
@@ -240,8 +249,8 @@ FROM Job
 LEFT JOIN Client USING (ClientId) 
 LEFT JOIN Pool USING (PoolId) 
 LEFT JOIN FileSet USING (FilesetId) 
-WHERE Client.ClientId='$clientid' $jobs_criteria";
-               return JobRecord::finder()->findAllBySql($sql);
+WHERE Client.ClientId='$clientid' $wh";
+               return JobRecord::finder()->findAllBySql($sql, $where['params']);
        }
 
        /**
index 32f5f3550ef371d7a39df2568ac2e642d0ddaec3..f8d5958b78eed2a201fd3e7c9b3f205b3f75d251 100644 (file)
@@ -32,10 +32,11 @@ class Job extends BaculumAPIServer {
                $jobid = $this->Request->contains('id') ? intval($this->Request['id']) : 0;
                $result = $this->getModule('bconsole')->bconsoleCommand(
                        $this->director,
-                       array('.jobs')
+                       ['.jobs'],
+                       null,
+                       true
                );
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
                        $job = $this->getModule('job')->getJobById($jobid);
                        if (is_object($job) && in_array($job->name, $result->output)) {
                                $this->output = $job;
@@ -54,10 +55,11 @@ class Job extends BaculumAPIServer {
                $jobid = intval($id);
                $result = $this->getModule('bconsole')->bconsoleCommand(
                        $this->director,
-                       array('.jobs')
+                       ['.jobs'],
+                       null,
+                       true
                );
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
                        $job = $this->getModule('job')->getJobById($jobid);
                        if(is_object($job) && in_array($job->name, $result->output)) {
                                $result = $this->getModule('bconsole')->bconsoleCommand(
index 4f188fd338d474ab2e871918104b0983a5e4dc6a..a175f8ff703abd7f8f01b07cec6d8c3c5bb9af83 100644 (file)
@@ -36,9 +36,13 @@ class JobBandwidthLimit extends BaculumAPIServer {
                }
 
                $jobid = null;
-               $result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.jobs'));
+               $result = $this->getModule('bconsole')->bconsoleCommand(
+                       $this->director,
+                       ['.jobs'],
+                       null,
+                       true
+               );
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
                        if(is_object($job) && in_array($job->name, $result->output)) {
                                $jobid = $job->jobid;
                        }
index 7655069c2ead31fcf457556a976748ba0dce704b..a6743505506d9f57a994d0705409c312e0ec4aa8 100644 (file)
@@ -32,7 +32,7 @@ Prado::using('Application.API.Class.Bconsole');
 class JobEstimate extends BaculumAPIServer {
 
        public function get() {
-               $output = array();
+               $output = [];
                $misc = $this->getModule('misc');
                if ($this->Request->contains('out_id') && $misc->isValidAlphaNumeric($this->Request->itemAt('out_id'))) {
                        $out_id = $this->Request->itemAt('out_id');
@@ -78,9 +78,13 @@ class JobEstimate extends BaculumAPIServer {
                        $this->error = JobError::ERROR_JOB_DOES_NOT_EXISTS;
                        return;
                } else {
-                       $result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.jobs'));
+                       $result = $this->getModule('bconsole')->bconsoleCommand(
+                               $this->director,
+                               ['.jobs'],
+                               null,
+                               true
+                       );
                        if ($result->exitcode === 0) {
-                               array_shift($result->output);
                                if (!in_array($job, $result->output)) {
                                        $this->output = JobError::MSG_ERROR_JOB_DOES_NOT_EXISTS;
                                        $this->error = JobError::ERROR_JOB_DOES_NOT_EXISTS;
index 9a0ff932e058328b80b54693fb37702734d9d80e..7fd5222f8d1d654073c87fe2f2569295663ea908 100644 (file)
@@ -57,14 +57,21 @@ class JobFiles extends BaculumAPIServer {
 
                $result = $this->getModule('bconsole')->bconsoleCommand(
                        $this->director,
-                       array('.jobs')
+                       ['.jobs'],
+                       null,
+                       true
                );
 
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
-                       $job = $this->getModule('job')->getJobsByFilename($clientid, $filename, $strict_mode, $result->output);
-                       $this->output = $job;
-                       $this->error = JobError::ERROR_NO_ERRORS;
+                       if (count($result->output) == 0) {
+                               // no allowed jobs means that user has no job resource assigned.
+                               $this->output = [];
+                               $this->error = JobError::ERROR_NO_ERRORS;
+                       } else {
+                               $job = $this->getModule('job')->getJobsByFilename($clientid, $filename, $strict_mode, $result->output);
+                               $this->output = $job;
+                               $this->error = JobError::ERROR_NO_ERRORS;
+                       }
                } else {
                        $result = is_array($result->output) ? implode('', $result->output) : $result->output;
                        $this->output = JobError::MSG_ERROR_WRONG_EXITCODE . $result;
index 01ee7e96f897977daa0c93c7aa7edecd30f9e684..716b80ed0694c0d860822cdf181e373906b85eb7 100644 (file)
@@ -40,10 +40,11 @@ class JobListFiles extends BaculumAPIServer {
 
                $result = $this->getModule('bconsole')->bconsoleCommand(
                        $this->director,
-                       array('.jobs')
+                       ['.jobs'],
+                       null,
+                       true
                );
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
                        $job = $this->getModule('job')->getJobById($jobid);
                        if (is_object($job) && in_array($job->name, $result->output)) {
                                if ($details) {
index ea53912b0773ab2bf5d79c34f4a248ab177ab46b..4e6c1fe60b60630159c6469a19ce74fe0a0ffe42 100644 (file)
@@ -36,10 +36,11 @@ class JobLog extends BaculumAPIServer {
                }
                $result = $this->getModule('bconsole')->bconsoleCommand(
                        $this->director,
-                       array('.jobs')
+                       ['.jobs'],
+                       null,
+                       true
                );
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
                        $job = $this->getModule('job')->getJobById($jobid);
                        if (is_object($job) && in_array($job->name, $result->output)) {
                                $log = $this->getModule('joblog')->getLogByJobId($job->jobid, $show_time);
index 50291d8d18be86c9baf170863e717a43ff7ccf5b..38e08f78d037c0f52aaa41eed4796a844d9d8270 100644 (file)
@@ -30,7 +30,7 @@
 class JobResNames extends BaculumAPIServer {
        public function get() {
                $limit = $this->Request->contains('limit') ? intval($this->Request['limit']) : 0;
-               $jobs_cmd = array('.jobs');
+               $jobs_cmd = ['.jobs'];
                $types = $this->getModule('misc')->job_types;
                if ($this->Request->contains('type') && key_exists($this->Request['type'], $types)) {
                        array_push($jobs_cmd, 'type="' . $this->Request['type']. '"');
@@ -42,19 +42,22 @@ class JobResNames extends BaculumAPIServer {
                        $this->error = $directors->exitcode;
                        return;
                }
-               $jobs = array();
+               $jobs = [];
                $error = false;
                $error_obj = null;
                for ($i = 0; $i < count($directors->output); $i++) {
-                       $job_list = $this->getModule('bconsole')->bconsoleCommand($directors->output[$i], $jobs_cmd);
+                       $job_list = $this->getModule('bconsole')->bconsoleCommand(
+                               $directors->output[$i],
+                               $jobs_cmd,
+                               null,
+                               true
+                       );
                        if ($job_list->exitcode != 0) {
                                $error_obj = $job_list;
                                $error = true;
                                break;
                        }
-                       // shift command
-                       array_shift($job_list->output);
-                       $jobs[$directors->output[$i]] = array();
+                       $jobs[$directors->output[$i]] = [];
                        for ($j = 0; $j < count($job_list->output); $j++) {
                                $jobs[$directors->output[$i]][] = $job_list->output[$j];
 
index cb49e238038f7905718aedbcbda7ba98ec03e318..1a98a6255561ddf53b38a1edf89d9458f633bf00 100644 (file)
@@ -94,9 +94,13 @@ class JobRun extends BaculumAPIServer {
                        $this->error = JobError::ERROR_JOB_DOES_NOT_EXISTS;
                        return;
                } else {
-                       $result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.jobs'));
+                       $result = $this->getModule('bconsole')->bconsoleCommand(
+                               $this->director,
+                               ['.jobs'],
+                               null,
+                               true
+                       );
                        if ($result->exitcode === 0) {
-                               array_shift($result->output);
                                if (!in_array($job, $result->output)) {
                                        $this->output = JobError::MSG_ERROR_JOB_DOES_NOT_EXISTS;
                                        $this->error = JobError::ERROR_JOB_DOES_NOT_EXISTS;
index 004708c65bb9e0488432bb2efef17452dcadeb55..5d6b8380c860ce8db8a5dc2f2ffe96f207202fee 100644 (file)
@@ -32,10 +32,11 @@ class JobShow extends BaculumAPIServer {
                $jobid = $this->Request->contains('id') ? intval($this->Request['id']) : 0;
                $result = $this->getModule('bconsole')->bconsoleCommand(
                        $this->director,
-                       array('.jobs')
+                       ['.jobs'],
+                       null,
+                       true
                );
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
                        $job = $this->getModule('job')->getJobById($jobid);
                        if (is_object($job) && in_array($job->name, $result->output)) {
                                $result = $this->getModule('bconsole')->bconsoleCommand(
index 39debc5f50998ea691c03daba2f50d61caf79d8f..daf3f075452c4832a81b71209e39bf8983ea266a 100644 (file)
 class JobTotals extends BaculumAPIServer {
        public function get() {
                $error = false;
-               $allowed = array();
-               $result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.jobs'));
+               $allowed = [];
+               $result = $this->getModule('bconsole')->bconsoleCommand(
+                       $this->director,
+                       ['.jobs'],
+                       null,
+                       true
+               );
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
                        $allowed = $result->output;
+                       if (count($allowed) == 0) {
+                               // no $allowed means that user has no job resource assigned.
+                               $error = true;
+                               $this->output = [];
+                               $this->error = JobError::ERROR_NO_ERRORS;
+                       }
                } else {
                        $error = true;
                        $this->output = $result->output;
index f50920b31b7d5f2c173d0fcd55b8e8931a60c818..9639e4c4e54ce47054822f10696e1c74241633d1 100644 (file)
@@ -50,7 +50,7 @@ class Jobs extends BaculumAPIServer {
                        return;
                }
 
-               $params = array();
+               $params = [];
                $jobstatuses = array_keys($misc->getJobState());
                $sts = str_split($jobstatus);
                for ($i = 0; $i < count($sts); $i++) {
@@ -69,16 +69,27 @@ class Jobs extends BaculumAPIServer {
                        $params['Job.Type']['operator'] = '';
                        $params['Job.Type']['vals'] = $type;
                }
-               $allowed = array();
-               $result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.jobs'));
+               $allowed = [];
+               $result = $this->getModule('bconsole')->bconsoleCommand(
+                       $this->director,
+                       ['.jobs'],
+                       null,
+                       true
+               );
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
-                       $vals = array();
+                       $vals = [];
                        if (!empty($jobname) && in_array($jobname, $result->output)) {
-                               $vals = array($jobname);
+                               $vals = [$jobname];
                        } else {
                                $vals = $result->output;
                        }
+                       if (count($vals) == 0) {
+                               // no $vals criteria means that user has no job resources assigned.
+                               $this->output = [];
+                               $this->error = JobError::ERROR_NO_ERRORS;
+                               return;
+                       }
+
                        $params['Job.Name']['operator'] = 'OR';
                        $params['Job.Name']['vals'] = $vals;
 
@@ -96,7 +107,7 @@ class Jobs extends BaculumAPIServer {
                                        }
                                        if (is_object($cli) && in_array($cli->name, $result->output)) {
                                                $params['Job.ClientId']['operator'] = 'AND';
-                                               $params['Job.ClientId']['vals'] = array($cli->clientid);
+                                               $params['Job.ClientId']['vals'] = [$cli->clientid];
                                        } else {
                                                $error = true;
                                                $this->output = JobError::MSG_ERROR_CLIENT_DOES_NOT_EXISTS;
index 17482797dcec397d796d71520933ede5ce8fea4b..043982135d5e1793e2a380bb1084bde770836d73 100644 (file)
@@ -33,10 +33,20 @@ class JobsForClient extends BaculumAPIServer {
                $allowed_jobs = array();
                $clientid = $this->Request->contains('id') ? intval($this->Request['id']) : 0;
                $error = false;
-               $result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.jobs'));
+               $result = $this->getModule('bconsole')->bconsoleCommand(
+                       $this->director,
+                       ['.jobs'],
+                       null,
+                       true
+               );
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
                        $allowed_jobs = $result->output;
+                       if (count($allowed_jobs) == 0) {
+                               // no $allowed_jobs means that user has no job resources assigned.
+                               $error = true;
+                               $this->output = [];
+                               $this->error = JobError::ERROR_NO_ERRORS;
+                       }
                } else {
                        $error = true;
                        $this->output = $result->output;
index 8c56fe93907f5beec8d6da9774969b6dfbde61b9..62b08643eda20f7af6f128d4f485df15535a6519 100644 (file)
@@ -34,11 +34,18 @@ class JobsOnVolume extends BaculumAPIServer {
                $error = false;
                $result = $this->getModule('bconsole')->bconsoleCommand(
                        $this->director,
-                       array('.jobs')
+                       ['.jobs'],
+                       null,
+                       true
                );
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
                        $allowed = $result->output;
+                       if (count($allowed) == 0) {
+                               // no $allowed means that user has no job resources assigned.
+                               $error = true;
+                               $this->output = [];
+                               $this->error = JobError::ERROR_NO_ERRORS;
+                       }
                } else {
                        $error = true;
                        $this->output = $result->output;
index eb94c3c72ad6078e37c8cec329cc2f687a623458..c42915a739ed6c967578b91d2e05807f4831c48b 100644 (file)
@@ -59,9 +59,13 @@ class JobsRecent extends BaculumAPIServer {
                        $this->output = FileSetError::MSG_ERROR_FILESET_DOES_NOT_EXISTS;
                        $this->error = FileSetError::ERROR_FILESET_DOES_NOT_EXISTS;
                } else {
-                       $result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.jobs'));
+                       $result = $this->getModule('bconsole')->bconsoleCommand(
+                               $this->director,
+                               ['.jobs'],
+                               null,
+                               true
+                       );
                        if ($result->exitcode === 0) {
-                               array_shift($result->output);
                                if (in_array($jobname, $result->output)) {
                                        $jobs = $this->getModule('job')->getRecentJobids($jobname, $clientid, $filesetid, $inc_copy_job);
                                        if (is_array($jobs)) {
index 3538ee35a03871d7d4ed2f5c3397849ede82f953..eb4b140d2696970636a81a67900482e057b7b16c 100644 (file)
@@ -32,11 +32,12 @@ class JobsShow extends BaculumAPIServer {
        public function get() {
                $result = $this->getModule('bconsole')->bconsoleCommand(
                        $this->director,
-                       array('.jobs')
+                       ['.jobs'],
+                       null,
+                       true
                );
                $job = null;
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
                        if ($this->Request->contains('name')) {
                                if (in_array($this->Request['name'], $result->output)) {
                                        $job = $this->Request['name'];
index 05031127152dce0d6cc4d87ec04f91107951f7d1..03648de6188dc9b2d31fccd2d0eb4c43663289cc 100644 (file)
@@ -32,10 +32,11 @@ class Pool extends BaculumAPIServer {
                $poolid = $this->Request->contains('id') ? intval($this->Request['id']) : 0;
                $result = $this->getModule('bconsole')->bconsoleCommand(
                        $this->director,
-                       array('.pool')
+                       ['.pool'],
+                       null,
+                       true
                );
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
                        $pool = $this->getModule('pool')->getPoolById($poolid);
                        if(!is_null($pool) && in_array($pool->name, $result->output)) {
                                $this->output = $pool;
index b088176febdbc6e5a91fd5c8f5530b88d6fc4b8d..86da3c0e88e1120ebb839b3222695f885c2f0599 100644 (file)
@@ -31,9 +31,13 @@ class Pools extends BaculumAPIServer {
        public function get() {
                $limit = $this->Request->contains('limit') ? intval($this->Request['limit']) : 0;
                $pools = $this->getModule('pool')->getPools($limit);
-               $result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.pool'));
+               $result = $this->getModule('bconsole')->bconsoleCommand(
+                       $this->director,
+                       ['.pool'],
+                       null,
+                       true
+               );
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
                        if (is_array($pools) && count($pools) > 0) {
                                $pools_output = array();
                                foreach($pools as $pool) {
index 3216f8085e5191f759f6759b0c9fa46f8f5b3e3f..1bbeab11fa10a5d463766b63ddfacbfbcd7ddc5f 100644 (file)
@@ -34,10 +34,11 @@ class VolumesRequired extends BaculumAPIServer {
                $fileid = $this->Request->contains('fileid') ? intval($this->Request['fileid']) : 0;
                $result = $this->getModule('bconsole')->bconsoleCommand(
                        $this->director,
-                       array('.jobs')
+                       ['.jobs'],
+                       null,
+                       true
                );
                if ($result->exitcode === 0) {
-                       array_shift($result->output);
                        $job = $this->getModule('job')->getJobById($jobid);
                        if(is_object($job) && in_array($job->name, $result->output)) {
                                $volumes = $this->getModule('volume')->getVolumesForJob($jobid, $fileid);