Passed
Pull Request — master (#4722)
by David
09:30
created

check_remote_submit_permissions()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 24
nop 2
dl 0
loc 25
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
// This file is part of BOINC.
4
// http://boinc.berkeley.edu
5
// Copyright (C) 2011 University of California
6
//
7
// BOINC is free software; you can redistribute it and/or modify it
8
// under the terms of the GNU Lesser General Public License
9
// as published by the Free Software Foundation,
10
// either version 3 of the License, or (at your option) any later version.
11
//
12
// BOINC is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
// See the GNU Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public License
18
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
19
20
// server-side utility functions for remote job submissions and control
21
22
require_once("../inc/submit_db.inc");
23
24
// write status and error messages to log
25
//
26
function log_write($x) {
27
    static $enabled, $log_file;
28
29
    if (!isset($enabled)) {
30
        $enabled = false;
31
        $filename = parse_config(get_config(), "<remote_submit_log>");
32
        if (!$filename) {
33
            return;
34
        }
35
        $log_dir = parse_config(get_config(), "<log_dir>");
36
        if (!$log_dir) {
37
            return;
38
        }
39
        $log_file = fopen("$log_dir/$filename", "a");
40
        if (!$log_file) return;
0 ignored issues
show
introduced by
$log_file is of type resource, thus it always evaluated to false.
Loading history...
41
        $enabled = true;
42
    }
43
    if (!$enabled) return;
44
    fwrite($log_file, sprintf("%s: %s\n", strftime("%c"), $x));
45
    fflush($log_file);
46
}
47
48
// in remote job submission,
49
// for input files of type local, semilocal, and inline,
50
// we need to give a unique physical name based on its content.
51
// Prepend the jf_ to make the origin of the file clear
52
//
53
function job_file_name($md5) {
54
    return "jf_$md5";
55
}
56
57
// does user have submit permissions?
58
//
59
function submit_permissions($user) {
60
    return BoincUserSubmit::lookup_userid($user->id);
61
}
62
63
// does user have submit permissions for given app?
64
//
65
function submit_permissions_app($user, $app) {
66
    return BoincUserSubmitApp::lookup("user_id=$user->id and app_id=$app->id");
67
}
68
69
// check whether user has permissions for a remote job submission
70
// or job file request.
71
// $r is a request message that includes an 'authenticator' field
72
// $app is the app being submitted to (or null if file op)
73
// returns [user, UserSubmit], or give XML error
74
//
75
function check_remote_submit_permissions($r, $app) {
76
    $auth = (string)$r->authenticator;
77
    if (!$auth) {
78
        log_write("no authenticator");
79
        xml_error(-1, "no authenticator");
80
    }
81
    $auth = BoincDb::escape_string($auth);
82
    $user = BoincUser::lookup("authenticator='$auth'");
83
    if (!$user) {
84
        log_write("bad authenticator");
85
        xml_error(-1, "bad authenticator");
86
    }
87
    $user_submit = submit_permissions($user);
88
    if (!$user_submit) {
89
        log_write("no submit access");
90
        xml_error(-1, "no submit access");
91
    }
92
    if ($app && !$user_submit->submit_all) {
93
        $usa = submit_permissions_app($user, $app);
94
        if (!$usa) {
95
            log_write("no app submit access");
96
            xml_error(-1, "no app submit access");
97
        }
98
    }
99
    return array($user, $user_submit);
100
}
101
102
function delete_remote_submit_user($user) {
103
    BoincUserSubmit::delete_user($user->id);
104
    BoincUserSubmitApp::delete_user($user->id);
105
}
106
107
108
// given its WUs, compute progress of a batch
109
// (fraction done, est completion time etc.)
110
// NOTE: this is inefficient because we need all the WUs.
111
// it could be done by server components
112
// (transitioner, validator etc.) as jobs complete or time out
113
//
114
// TODO: update est_completion_time
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
115
//
116
function get_batch_params($batch, $wus) {
117
    if ($batch->state == BATCH_STATE_INIT) {
118
        // a batch in INIT state has no jobs
119
        //
120
        return $batch;
121
    }
122
    $fp_total = 0;
123
    $fp_done = 0;
124
    $completed = true;
125
    $batch->nerror_jobs = 0;
126
    $batch->credit_canonical = 0;
127
    foreach ($wus as $wu) {
128
        $fp_total += $wu->rsc_fpops_est;
129
        if ($wu->canonical_resultid) {
130
            $fp_done += $wu->rsc_fpops_est;
131
            $batch->credit_canonical += $wu->canonical_credit;
132
        } else if ($wu->error_mask) {
133
            $batch->nerror_jobs++;
134
        } else {
135
            $completed = false;
136
        }
137
    }
138
    if ($fp_total) {
139
        $batch->fraction_done = $fp_done / $fp_total;
140
    }
141
    if ($completed && $batch->state == BATCH_STATE_IN_PROGRESS) {
142
        $batch->state = BATCH_STATE_COMPLETE;
143
        $batch->completion_time = time();
144
    }
145
    $batch->update("fraction_done = $batch->fraction_done, nerror_jobs = $batch->nerror_jobs, state=$batch->state, completion_time = $batch->completion_time, credit_canonical = $batch->credit_canonical");
146
147
    $batch->credit_estimate = flops_to_credit($fp_total);
148
    return $batch;
149
}
150
151
function get_outfile_names($result) {
152
    $names = array();
153
    $xml = "<a>".$result->xml_doc_out."</a>";
154
    $r = simplexml_load_string($xml);
155
    if (!$r) return $names;
0 ignored issues
show
introduced by
$r is of type SimpleXMLElement, thus it always evaluated to true.
Loading history...
156
    foreach ($r->file_info as $fi) {
157
        $names[] = (string)($fi->name);
158
    }
159
    return $names;
160
}
161
162
function get_outfile_paths($result) {
163
    $fanout = parse_config(get_config(), "<uldl_dir_fanout>");
164
    $upload_dir = parse_config(get_config(), "<upload_dir>");
165
166
    $paths = array();
167
    $xml = "<a>".$result->xml_doc_out."</a>";
168
    $r = simplexml_load_string($xml);
169
    if (!$r) return $paths;
0 ignored issues
show
introduced by
$r is of type SimpleXMLElement, thus it always evaluated to true.
Loading history...
170
    foreach ($r->file_info as $fi) {
171
        $path = dir_hier_path((string)($fi->name), $upload_dir, $fanout);
172
        $paths[] = $path;
173
    }
174
    return $paths;
175
}
176
177
function abort_workunit($wu) {
178
    BoincResult::update_aux(
179
        "server_state=5, outcome=5 where server_state=2 and workunitid=$wu->id"
180
    );
181
    $wu->update("error_mask=error_mask|16");
182
}
183
184
function abort_batch($batch) {
185
    $wus = BoincWorkunit::enum("batch=$batch->id");
186
    foreach ($wus as $wu) {
187
        abort_workunit($wu);
188
    }
189
    $batch->update("state=".BATCH_STATE_ABORTED);
190
    return 0;
191
}
192
193
// mark WUs as assimilated; this lets them be purged
194
//
195
function retire_batch($batch) {
196
    $wus = BoincWorkunit::enum("batch=$batch->id");
197
    $now = time();
198
    foreach ($wus as $wu) {
199
        $wu->update(
200
            "assimilate_state=".ASSIMILATE_DONE.", transition_time=$now"
201
        );
202
        // remove output template if it's a temporary
203
        //
204
        if (strstr($wu->result_template_file, "templates/tmp/")) {
205
            @unlink($wu->result_template_file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

205
            /** @scrutinizer ignore-unhandled */ @unlink($wu->result_template_file);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
206
        }
207
    }
208
    $batch->update("state=".BATCH_STATE_RETIRED);
209
}
210
211
function expire_batch($batch) {
212
    abort_batch($batch);
213
    retire_batch($batch);
214
    $batch->update("state=".BATCH_STATE_EXPIRED);
215
}
216
217
function batch_state_string($state) {
218
    switch ($state) {
219
    case BATCH_STATE_INIT: return "new";
220
    case BATCH_STATE_IN_PROGRESS: return "in progress";
221
    case BATCH_STATE_COMPLETE: return "completed";
222
    case BATCH_STATE_ABORTED: return "aborted";
223
    case BATCH_STATE_RETIRED: return "retired";
224
    }
225
    return "unknown state $state";
226
}
227
// get the total size of output files of a batch
228
//
229
function batch_output_file_size($batchid) {
230
    $batch_td_size=0;
231
    $wus = BoincWorkunit::enum("batch=$batchid");
232
    $fanout = parse_config(get_config(), "<uldl_dir_fanout>");
233
    $upload_dir = parse_config(get_config(), "<upload_dir>");
234
    foreach ($wus as $wu) {
235
        if (!$wu->canonical_resultid) continue;
236
        $result = BoincResult::lookup_id($wu->canonical_resultid);
237
        $names = get_outfile_names($result);
238
        foreach ($names as $name) {
239
            $path = dir_hier_path($name, $upload_dir, $fanout);
240
            if (is_file($path)) {
241
                $s=stat($path);
242
                $size=$s['size'];
243
                $batch_td_size+=$size;
244
            }
245
        }
246
    }
247
    return $batch_td_size;
248
}
249
250
function boinc_get_output_file_url($user, $result, $i) {
251
    $name = $result->name;
252
    $auth_str = md5($user->authenticator.$name);
253
    return "get_output.php?cmd=result_file&result_name=$name&file_num=$i&auth_str=$auth_str";
254
}
255
256
function boinc_get_output_files_url($user, $batch_id) {
257
    $auth_str = md5($user->authenticator.$batch_id);
258
    return "get_output.php?cmd=batch_files&batch_id=$batch_id&auth_str=$auth_str";
259
}
260
261
function boinc_get_wu_output_files_url($user, $wu_id) {
262
    $auth_str =  md5($user->authenticator.$wu_id);
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "="; 2 found
Loading history...
263
    return "get_output.php?cmd=workunit_files&wu_id=$wu_id&auth_str=$auth_str";
264
}
265
266
?>
267