uoj-judger-lib.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. global $uojSupportedLanguages, $uojMainJudgerWorkPath;
  3. $uojSupportedLanguages = array('C', 'C++14', 'C++17', 'C++17ubsan', 'Python3');
  4. $uojMainJudgerWorkPath = "/opt/uoj/judger/uoj_judger";
  5. function authenticateJudger() {
  6. if (!is_string($_POST['judger_name']) || !is_string($_POST['password'])) {
  7. return false;
  8. }
  9. $esc_judger_name = DB::escape($_POST['judger_name']);
  10. $judger = DB::selectFirst("select password from judger_info where judger_name = '$esc_judger_name'");
  11. if ($judger == null) {
  12. return false;
  13. }
  14. return $judger['password'] == $_POST['password'];
  15. }
  16. function judgerCodeStr($code) {
  17. switch ($code) {
  18. case 0:
  19. return "Accepted";
  20. case 1:
  21. return "Wrong Answer";
  22. case 2:
  23. return "Runtime Error";
  24. case 3:
  25. return "Memory Limit Exceeded";
  26. case 4:
  27. return "Time Limit Exceeded";
  28. case 5:
  29. return "Output Limit Exceeded";
  30. case 6:
  31. return "Dangerous Syscalls";
  32. case 7:
  33. return "Judgement Failed";
  34. default:
  35. return "No Comment";
  36. }
  37. }
  38. class StrictFileReader {
  39. private $f;
  40. private $buf = '', $off = 0;
  41. public function __construct($file_name) {
  42. $this->f = fopen($file_name, 'r');
  43. }
  44. public function failed() {
  45. return $this->f === false;
  46. }
  47. public function readChar() {
  48. if (isset($this->buf[$this->off])) {
  49. return $this->buf[$this->off++];
  50. }
  51. return fgetc($this->f);
  52. }
  53. public function unreadChar($c) {
  54. $this->buf .= $c;
  55. if ($this->off > 1000) {
  56. $this->buf = substr($this->buf, $this->off);
  57. $this->off = 0;
  58. }
  59. }
  60. public function readString() {
  61. $str = '';
  62. while (true) {
  63. $c = $this->readChar();
  64. if ($c === false) {
  65. break;
  66. } elseif ($c === " " || $c === "\n" || $c === "\r") {
  67. $this->unreadChar($c);
  68. break;
  69. } else {
  70. $str .= $c;
  71. }
  72. }
  73. return $str;
  74. }
  75. public function ignoreWhite() {
  76. while (true) {
  77. $c = $this->readChar();
  78. if ($c === false) {
  79. break;
  80. } elseif ($c === " " || $c === "\n" || $c === "\r") {
  81. continue;
  82. } else {
  83. $this->unreadChar($c);
  84. break;
  85. }
  86. }
  87. }
  88. public function eof() {
  89. return feof($this->f);
  90. }
  91. public function close() {
  92. fclose($this->f);
  93. }
  94. }
  95. function getUOJConf($file_name) {
  96. $reader = new StrictFileReader($file_name);
  97. if ($reader->failed()) {
  98. return -1;
  99. }
  100. $conf = array();
  101. while (!$reader->eof()) {
  102. $reader->ignoreWhite();
  103. $key = $reader->readString();
  104. if ($key === '') {
  105. break;
  106. }
  107. $reader->ignoreWhite();
  108. $val = $reader->readString();
  109. if ($val === '') {
  110. break;
  111. }
  112. if (isset($conf[$key])) {
  113. return -2;
  114. }
  115. $conf[$key] = $val;
  116. }
  117. $reader->close();
  118. return $conf;
  119. }
  120. function putUOJConf($file_name, $conf) {
  121. $f = fopen($file_name, 'w');
  122. foreach ($conf as $key => $val) {
  123. fwrite($f, "$key $val\n");
  124. }
  125. fclose($f);
  126. }
  127. function getUOJConfVal($conf, $key, $default_val) {
  128. if (isset($conf[$key])) {
  129. return $conf[$key];
  130. } else {
  131. return $default_val;
  132. }
  133. }
  134. // problem.conf can also arrive inside a zip that the problem's controller
  135. // uploads, so *every* value in it is attacker-controlled: the field checks in
  136. // problem_data_manage.php are not a security boundary, because the zip upload
  137. // path never goes through them. input_pre / input_suf / output_pre /
  138. // output_suf used to be concatenated straight into a filesystem path, so
  139. //
  140. // input_pre x/../../../../../opt/uoj/web/shell.
  141. // input_suf php
  142. //
  143. // made dataAddExtraTest() move an uploaded hack input to
  144. // /opt/uoj/web/shell.1.php -- an arbitrary-content arbitrary-path write into
  145. // the document root, i.e. remote code execution as the web user.
  146. //
  147. // Only a bare filename component is ever legitimate here. Anything else
  148. // (separators, "..", absolute paths, ...) falls back to the default instead of
  149. // escaping the problem's own data directory. Same idea as the whitelist that
  150. // already guards use_builtin_checker in uoj-data-lib.php.
  151. function getUOJConfFileNamePart($conf, $key, $default_val) {
  152. $val = getUOJConfVal($conf, $key, $default_val);
  153. if (!is_string($val) || !preg_match('/^[A-Za-z0-9_.-]*$/', $val) || strpos($val, '..') !== false) {
  154. error_log("uoj: ignoring unsafe problem.conf value for '$key'");
  155. return $default_val;
  156. }
  157. return $val;
  158. }
  159. function getUOJProblemInputFileName($problem_conf, $num) {
  160. return getUOJConfFileNamePart($problem_conf, 'input_pre', 'input') . $num . '.' . getUOJConfFileNamePart($problem_conf, 'input_suf', 'txt');
  161. }
  162. function getUOJProblemOutputFileName($problem_conf, $num) {
  163. return getUOJConfFileNamePart($problem_conf, 'output_pre', 'output') . $num . '.' . getUOJConfFileNamePart($problem_conf, 'output_suf', 'txt');
  164. }
  165. function getUOJProblemExtraInputFileName($problem_conf, $num) {
  166. return 'ex_' . getUOJConfFileNamePart($problem_conf, 'input_pre', 'input') . $num . '.' . getUOJConfFileNamePart($problem_conf, 'input_suf', 'txt');
  167. }
  168. function getUOJProblemExtraOutputFileName($problem_conf, $num) {
  169. return 'ex_' . getUOJConfFileNamePart($problem_conf, 'output_pre', 'output') . $num . '.' . getUOJConfFileNamePart($problem_conf, 'output_suf', 'txt');
  170. }
  171. function rejudgeProblem($problem) {
  172. DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where problem_id = ${problem['id']}");
  173. }
  174. function rejudgeProblemAC($problem) {
  175. DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where problem_id = ${problem['id']} and score = 100");
  176. }
  177. function rejudgeProblemGe97($problem) {
  178. DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where problem_id = ${problem['id']} and score >= 97");
  179. }
  180. function rejudgeSubmission($submission) {
  181. DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where id = ${submission['id']}");
  182. }
  183. function updateBestACSubmissions($username, $problem_id) {
  184. $best = DB::selectFirst("select id, used_time, used_memory, tot_size from submissions where submitter = '$username' and problem_id = $problem_id and score = 100 order by used_time, used_memory, tot_size asc limit 1");
  185. $shortest = DB::selectFirst("select id, used_time, used_memory, tot_size from submissions where submitter = '$username' and problem_id = $problem_id and score = 100 order by tot_size, used_time, used_memory asc limit 1");
  186. DB::delete("delete from best_ac_submissions where submitter = '$username' and problem_id = $problem_id");
  187. if ($best) {
  188. DB::insert("insert into best_ac_submissions (problem_id, submitter, submission_id, used_time, used_memory, tot_size, shortest_id, shortest_used_time, shortest_used_memory, shortest_tot_size) values ($problem_id, '$username', ${best['id']}, ${best['used_time']}, ${best['used_memory']}, ${best['tot_size']}, ${shortest['id']}, ${shortest['used_time']}, ${shortest['used_memory']}, ${shortest['tot_size']})");
  189. }
  190. $cnt = DB::selectCount("select count(*) from best_ac_submissions where submitter='$username'");
  191. DB::update("update user_info set ac_num = $cnt where username='$username'");
  192. DB::update("update problems set ac_num = (select count(*) from submissions where problem_id = problems.id and score = 100), submit_num = (select count(*) from submissions where problem_id = problems.id) where id = $problem_id");
  193. }
  194. ?>