Passed
Push — master ( 40f8fd...d44bb9 )
by Vitalii
01:30 queued 21s
created

check_remote_submit_permissions()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
c 0
b 0
f 0
nc 16
nop 2
dl 0
loc 27
rs 9.0444
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
// can user upload files?
58
//
59
function has_file_access($user) {
60
    $us = BoincUserSubmit::lookup_userid($user->id);
61
    if (!$us) return false;
62
    return true;
63
}
64
65
// can user submit to given app?
66
//
67
function has_submit_access($user, $app_id) {
68
    $us = BoincUserSubmit::lookup_userid($user->id);
69
    if (!$us) return false;
70
    if ($us->submit_all) return true;
71
    $usa = BoincUserSubmitApp::lookup("user_id=$user->id and app_id=$app_id");
72
    if (!$usa) return false;
73
    return true;
74
}
75
76
// can user administer given app (or all apps if zero)?
77
//
78
function has_admin_access($user, $app_id) {
79
    $us = BoincUserSubmit::lookup_userid($user->id);
80
    if (!$us) return false;
81
    if ($us->admin_all) return true;
82
    $usa = BoincUserSubmitApp::lookup("user_id=$user->id and app_id=$app_id");
83
    if (!$usa) return false;
84
    return $usa->manage;
85
}
86
87
// check whether user has permissions for a remote job submission
88
// or job file request.
89
// $r is a request message that includes an 'authenticator' field
90
// $app is the app being submitted to (or null if file op)
91
// returns user, or give XML error and quit
92
//
93
function check_remote_submit_permissions($r, $app) {
94
    $auth = (string)$r->authenticator;
95
    if (!$auth) {
96
        log_write("no authenticator");
97
        xml_error(-1, "no authenticator");
98
    }
99
    $auth = BoincDb::escape_string($auth);
100
    $user = BoincUser::lookup("authenticator='$auth'");
101
    if (!$user) {
102
        log_write("bad authenticator");
103
        xml_error(-1, "bad authenticator");
104
    }
105
106
    // check access
107
    //
108
    if ($app) {
109
        if (!has_submit_access($user, $app->id)) {
110
            log_write("no submit access");
111
            xml_error(-1, "no submit access");
112
        }
113
    } else {
114
        if (!has_file_access($user)) {
115
            log_write("no file access");
116
            xml_error(-1, "no file access");
117
        }
118
    }
119
    return $user;
120
}
121
122
// remove all of user's permissions
123
//
124
function delete_remote_submit_user($user) {
125
    BoincUserSubmit::delete_user($user->id);
126
    BoincUserSubmitApp::delete_user($user->id);
127
}
128
129
130
// given its WUs, compute progress of a batch
131
// (fraction done, est completion time etc.)
132
// NOTE: this is inefficient because we need all the WUs.
133
// it could be done by server components
134
// (transitioner, validator etc.) as jobs complete or time out
135
//
136
// 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...
137
//
138
function get_batch_params($batch, $wus) {
139
    if ($batch->state == BATCH_STATE_INIT) {
140
        // a batch in INIT state has no jobs
141
        //
142
        return $batch;
143
    }
144
    $fp_total = 0;
145
    $fp_done = 0;
146
    $completed = true;
147
    $batch->nerror_jobs = 0;
148
    $batch->credit_canonical = 0;
149
    foreach ($wus as $wu) {
150
        $fp_total += $wu->rsc_fpops_est;
151
        if ($wu->canonical_resultid) {
152
            $fp_done += $wu->rsc_fpops_est;
153
            $batch->credit_canonical += $wu->canonical_credit;
154
        } else if ($wu->error_mask) {
155
            $batch->nerror_jobs++;
156
        } else {
157
            $completed = false;
158
        }
159
    }
160
    if ($fp_total) {
161
        $batch->fraction_done = $fp_done / $fp_total;
162
    }
163
    if ($completed && $batch->state == BATCH_STATE_IN_PROGRESS) {
164
        $batch->state = BATCH_STATE_COMPLETE;
165
        $batch->completion_time = time();
166
    }
167
    $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");
168
169
    $batch->credit_estimate = flops_to_credit($fp_total);
170
    return $batch;
171
}
172
173
// get the number of WUs for which we've sent at least 1 instance
174
// TODO: do this more efficiently (single query)
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...
175
//
176
function wus_nsent($wus) {
177
    $n = 0;
178
    foreach ($wus as $wu) {
179
        $res = BoincResult::enum(
180
            sprintf('workunitid=%d and server_state<>%d',
181
                $wu->id, RESULT_SERVER_STATE_UNSENT
182
            )
183
        );
184
        if (count($res) > 0) $n++;
185
    }
186
    return $n;
187
}
188
189
// get the physical names of a result's output files.
190
//
191
function get_outfile_phys_names($result) {
192
    $names = [];
193
    $xml = "<a>".$result->xml_doc_out."</a>";
194
    $r = simplexml_load_string($xml);
195
    if (!$r) return $names;
0 ignored issues
show
introduced by
$r is of type SimpleXMLElement, thus it always evaluated to true.
Loading history...
196
    foreach ($r->file_info as $fi) {
197
        $names[] = (string)($fi->name);
198
    }
199
    return $names;
200
}
201
202
function get_outfile_log_names($result) {
203
    $names = [];
204
    $xml = "<a>".$result->xml_doc_in."</a>";
205
    $r = simplexml_load_string($xml);
206
    if (!$r) return $names;
0 ignored issues
show
introduced by
$r is of type SimpleXMLElement, thus it always evaluated to true.
Loading history...
207
    foreach ($r->result->file_ref as $fr) {
208
        $names[] = (string)($fr->open_name);
209
    }
210
    return $names;
211
}
212
213
function get_outfile_paths($result) {
214
    $fanout = parse_config(get_config(), "<uldl_dir_fanout>");
215
    $upload_dir = parse_config(get_config(), "<upload_dir>");
216
217
    $paths = array();
218
    $xml = "<a>".$result->xml_doc_out."</a>";
219
    $r = simplexml_load_string($xml);
220
    if (!$r) return $paths;
0 ignored issues
show
introduced by
$r is of type SimpleXMLElement, thus it always evaluated to true.
Loading history...
221
    foreach ($r->file_info as $fi) {
222
        $path = dir_hier_path((string)($fi->name), $upload_dir, $fanout);
223
        $paths[] = $path;
224
    }
225
    return $paths;
226
}
227
228
function abort_workunit($wu) {
229
    BoincResult::update_aux(
230
        "server_state=5, outcome=5 where server_state=2 and workunitid=$wu->id"
231
    );
232
    $wu->update("error_mask=error_mask|16");
233
}
234
235
function abort_batch($batch) {
236
    $wus = BoincWorkunit::enum("batch=$batch->id");
237
    foreach ($wus as $wu) {
238
        abort_workunit($wu);
239
    }
240
    $batch->update("state=".BATCH_STATE_ABORTED);
241
    return 0;
242
}
243
244
// mark WUs as assimilated; this lets them be purged
245
//
246
function retire_batch($batch) {
247
    $wus = BoincWorkunit::enum("batch=$batch->id");
248
    $now = time();
249
    foreach ($wus as $wu) {
250
        $wu->update(
251
            "assimilate_state=".ASSIMILATE_DONE.", transition_time=$now"
252
        );
253
        // remove output template if it's a temporary
254
        //
255
        if (strstr($wu->result_template_file, "templates/tmp/")) {
256
            @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

256
            /** @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...
257
        }
258
    }
259
    $batch->update("state=".BATCH_STATE_RETIRED);
260
}
261
262
function expire_batch($batch) {
263
    abort_batch($batch);
264
    retire_batch($batch);
265
    $batch->update("state=".BATCH_STATE_EXPIRED);
266
}
267
268
function batch_state_string($state) {
269
    switch ($state) {
270
    case BATCH_STATE_INIT: return "new";
271
    case BATCH_STATE_IN_PROGRESS: return "in progress";
272
    case BATCH_STATE_COMPLETE: return "completed";
273
    case BATCH_STATE_ABORTED: return "aborted";
274
    case BATCH_STATE_RETIRED: return "retired";
275
    }
276
    return "unknown state $state";
277
}
278
// get the total size of output files of a batch
279
//
280
function batch_output_file_size($batchid) {
281
    $batch_td_size=0;
282
    $wus = BoincWorkunit::enum("batch=$batchid");
283
    $fanout = parse_config(get_config(), "<uldl_dir_fanout>");
284
    $upload_dir = parse_config(get_config(), "<upload_dir>");
285
    foreach ($wus as $wu) {
286
        if (!$wu->canonical_resultid) continue;
287
        $result = BoincResult::lookup_id($wu->canonical_resultid);
288
        $names = get_outfile_phys_names($result);
289
        foreach ($names as $name) {
290
            $path = dir_hier_path($name, $upload_dir, $fanout);
291
            if (is_file($path)) {
292
                $batch_td_size += filesize($path);
293
            }
294
        }
295
    }
296
    return $batch_td_size;
297
}
298
299
function boinc_get_output_file_url($user, $result, $i) {
300
    $name = $result->name;
301
    $auth_str = md5($user->authenticator.$name);
302
    return "get_output.php?cmd=result_file&result_name=$name&file_num=$i&auth_str=$auth_str";
303
}
304
305
function boinc_get_output_files_url($user, $batch_id) {
306
    $auth_str = md5($user->authenticator.$batch_id);
307
    return "get_output.php?cmd=batch_files&batch_id=$batch_id&auth_str=$auth_str";
308
}
309
310
function boinc_get_wu_output_files_url($user, $wu_id) {
311
    $auth_str =  md5($user->authenticator.$wu_id);
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "="; 2 found
Loading history...
312
    return "get_output.php?cmd=workunit_files&wu_id=$wu_id&auth_str=$auth_str";
313
}
314
315
////////////////// FILE INFO FILES //////////////
316
317
// these are used:
318
// 1) in user file sandbox
319
// 2) in BUDA app variant dirs
320
// in each case a file dir/foo has an info file dir/.md5/foo
321
// containing its md5 and size
322
// (same format as .md5 files in download hierarchy)
323
324
// get the MD5 and size of a file
325
//
326
function get_file_info($path) {
327
    $md5 = md5_file($path);
328
    $s = stat($path);
329
    $size = $s['size'];
330
    return [$md5, $size];
331
}
332
333
// write a "info file" containing MD5 and size
334
//
335
function write_info_file($path, $md5, $size) {
336
    file_put_contents($path, "$md5 $size");
337
}
338
339
// parse info file and return [md5, size]
340
//
341
function parse_info_file($path) {
342
    if (!file_exists($path)) return null;
343
    $x = file_get_contents($path);
344
    $n = sscanf($x, "%s %d", $md5, $size);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $size seems to be never defined.
Loading history...
345
    if ($n != 2 || strlen($md5)!=32) {
346
        return null;
347
    }
348
    return [$md5, $size];
349
}
350
351
?>
352