예제 #1
0
파일: bson_utils.c 프로젝트: NUOG/ejudge
void
ej_bson_append_ip(
        struct _bson *b,
        const unsigned char *key,
        const ej_ip_t *p_ip)
{
    bson_append_string(b, key, xml_unparse_ipv6(p_ip), -1);
}
예제 #2
0
파일: new-server.c 프로젝트: NUOG/ejudge
static void
cmd_http_request(
        struct server_framework_state *state,
        struct client_state *p,
        size_t pkt_size,
        const struct new_server_prot_packet *pkt_gen)
{
  enum
  {
    MAX_PARAM_NUM = 10000,
    MAX_PARAM_SIZE = 128 * 1024 * 1024,
  };

  const struct new_server_prot_http_request *pkt;
  size_t in_size;
  const ej_size_t *arg_sizes, *env_sizes, *param_name_sizes, *param_sizes;
  unsigned long bptr;
  const unsigned char ** args;
  const unsigned char ** envs;
  const unsigned char ** param_names;
  const unsigned char ** params;
  size_t *my_param_sizes;
  int i;
  char *out_txt = 0;
  size_t out_size = 0;
  FILE *out_f = 0;
  struct http_request_info hr;
  unsigned char info_buf[1024];
  unsigned char *pbuf = info_buf;

  memset(&hr, 0, sizeof(hr));
  hr.id = p->id;
  hr.client_state = p;
  hr.fw_state = state;
  gettimeofday(&hr.timestamp1, 0);
  hr.current_time = hr.timestamp1.tv_sec;
  hr.locale_id = -1;

  if (pkt_size < sizeof(*pkt))
    return nsf_err_packet_too_small(state, p, pkt_size, sizeof(*pkt));
  pkt = (const struct new_server_prot_http_request *) pkt_gen;

  if (pkt->arg_num < 0 || pkt->arg_num > MAX_PARAM_NUM)
    return nsf_err_protocol_error(state, p);
  if (pkt->env_num < 0 || pkt->env_num > MAX_PARAM_NUM)
    return nsf_err_protocol_error(state, p);
  if (pkt->param_num < 0 || pkt->param_num > MAX_PARAM_NUM)
    return nsf_err_protocol_error(state, p);

  in_size = sizeof(*pkt);
  in_size += pkt->arg_num * sizeof(ej_size_t);
  in_size += pkt->env_num * sizeof(ej_size_t);
  in_size += pkt->param_num * 2 * sizeof(ej_size_t);
  if (pkt_size < in_size)
    return nsf_err_packet_too_small(state, p, pkt_size, in_size);

  XALLOCAZ(args, pkt->arg_num);
  XALLOCAZ(envs, pkt->env_num);
  XALLOCAZ(param_names, pkt->param_num);
  XALLOCAZ(params, pkt->param_num);
  XALLOCAZ(my_param_sizes, pkt->param_num);

  bptr = (unsigned long) pkt;
  bptr += sizeof(*pkt);
  arg_sizes = (const ej_size_t *) bptr;
  bptr += pkt->arg_num * sizeof(ej_size_t);
  env_sizes = (const ej_size_t *) bptr;
  bptr += pkt->env_num * sizeof(ej_size_t);
  param_name_sizes = (const ej_size_t *) bptr;
  bptr += pkt->param_num * sizeof(ej_size_t);
  param_sizes = (const ej_size_t *) bptr;
  bptr += pkt->param_num * sizeof(ej_size_t);

  for (i = 0; i < pkt->arg_num; i++) {
    if (arg_sizes[i] > MAX_PARAM_SIZE) return nsf_err_protocol_error(state, p);
    in_size += arg_sizes[i] + 1;
  }
  for (i = 0; i < pkt->env_num; i++) {
    if (env_sizes[i] > MAX_PARAM_SIZE) return nsf_err_protocol_error(state, p);
    in_size += env_sizes[i] + 1;
  }
  for (i = 0; i < pkt->param_num; i++) {
    if (param_name_sizes[i] > MAX_PARAM_SIZE)
      return nsf_err_protocol_error(state, p);
    if (param_sizes[i] > MAX_PARAM_SIZE)
      return nsf_err_protocol_error(state, p);
    in_size += param_name_sizes[i] + 1;
    in_size += param_sizes[i] + 1;
  }
  if (pkt_size != in_size)
    return nsf_err_bad_packet_length(state, p, pkt_size, in_size);

  for (i = 0; i < pkt->arg_num; i++) {
    args[i] = (const unsigned char*) bptr;
    bptr += arg_sizes[i] + 1;
    if (strlen(args[i]) != arg_sizes[i])
      return nsf_err_protocol_error(state, p);
  }
  for (i = 0; i < pkt->env_num; i++) {
    envs[i] = (const unsigned char*) bptr;
    bptr += env_sizes[i] + 1;
    if (strlen(envs[i]) != env_sizes[i])
      return nsf_err_protocol_error(state, p);
  }
  for (i = 0; i < pkt->param_num; i++) {
    param_names[i] = (const unsigned char*) bptr;
    bptr += param_name_sizes[i] + 1;
    if (strlen(param_names[i]) != param_name_sizes[i])
      return nsf_err_protocol_error(state, p);
    params[i] = (const unsigned char *) bptr;
    my_param_sizes[i] = param_sizes[i];
    bptr += param_sizes[i] + 1;
  }

  hr.arg_num = pkt->arg_num;
  hr.args = args;
  hr.env_num = pkt->env_num;
  hr.envs = envs;
  hr.param_num = pkt->param_num;
  hr.param_names = param_names;
  hr.param_sizes = my_param_sizes;
  hr.params = params;
  hr.config = ejudge_config;

  // ok, generate HTML
  out_f = open_memstream(&out_txt, &out_size);
  ns_handle_http_request(state, p, out_f, &hr);
  close_memstream(out_f); out_f = 0;

  *pbuf = 0;
  // report IP?
  if (hr.ssl_flag) {
    pbuf = stpcpy(pbuf, "HTTPS:");
  } else {
    pbuf = stpcpy(pbuf, "HTTPS:");
  }
  pbuf = stpcpy(pbuf, xml_unparse_ipv6(&hr.ip));
  *pbuf++ = ':';
  if (hr.role_name) {
    pbuf = stpcpy(pbuf, hr.role_name);
  }
  if (hr.action > 0 && hr.action < NEW_SRV_ACTION_LAST && ns_symbolic_action_table[hr.action]) {
    *pbuf++ = '/';
    pbuf = stpcpy(pbuf, ns_symbolic_action_table[hr.action]);
  }
  if (hr.session_id) {
    *pbuf++ = '/';
    *pbuf++ = 'S';
    pbuf += sprintf(pbuf, "%016llx", hr.session_id);
    if (hr.client_key) {
      *pbuf++ = '-';
      pbuf += sprintf(pbuf, "%016llx", hr.client_key);
    }
  }
  if (hr.user_id > 0) {
    *pbuf++ = '/';
    *pbuf++ = 'U';
    pbuf += sprintf(pbuf, "%d", hr.user_id);
  }
  if (hr.contest_id > 0) {
    *pbuf++ = '/';
    *pbuf++ = 'C';
    pbuf += sprintf(pbuf, "%d", hr.contest_id);
  }

  // no reply now
  if (hr.no_reply) goto cleanup;

  if (hr.protocol_reply) {
    xfree(out_txt); out_txt = 0;
    info("%d:%s -> %d", p->id, info_buf, hr.protocol_reply);
    nsf_close_client_fds(p);
    nsf_send_reply(state, p, hr.protocol_reply);
    goto cleanup;
  }

  //
  if (hr.content_type && hr.content_type[0]) {
    // generate header
    char *hdr_t = NULL;
    size_t hdr_z = 0;
    FILE *hdr_f = open_memstream(&hdr_t, &hdr_z);

    fprintf(hdr_f, "Content-Type: %s\n", hr.content_type);
    fprintf(hdr_f, "Cache-Control: no-cache\n");
    fprintf(hdr_f, "Pragma: no-cache\n");
    if (hr.client_key) {
      fprintf(hdr_f, "Set-Cookie: EJSID=%016llx; Path=/\n", hr.client_key);
    }
    putc('\n', hdr_f);
    if (out_size > 0) {
      fwrite(out_txt, 1, out_size, hdr_f);
    }
    fclose(hdr_f); hdr_f = NULL;
    free(out_txt);
    out_txt = hdr_t;
    out_size = hdr_z;
  }

  if (!out_txt || !*out_txt) {
    xfree(out_txt); out_txt = 0;
    if (hr.allow_empty_output) {
      info("%d:%s -> OK", p->id, info_buf);
      nsf_close_client_fds(p);
      nsf_send_reply(state, p, NEW_SRV_RPL_OK);
      goto cleanup;
    }
    out_f = open_memstream(&out_txt, &out_size);
    ns_html_err_internal_error(out_f, &hr, 0, "empty output generated");
    close_memstream(out_f); out_f = 0;
    xfree(out_txt); out_txt = 0;
  }

  nsf_new_autoclose(state, p, out_txt, out_size);
  info("%d:%s -> OK, %zu", p->id, info_buf, out_size);
  nsf_send_reply(state, p, NEW_SRV_RPL_OK);

 cleanup:
  if (hr.log_f) fclose(hr.log_f);
  xfree(hr.log_t);
  xfree(hr.login);
  xfree(hr.name);
  xfree(hr.name_arm);
  xfree(hr.script_part);
  xfree(hr.body_attr);
}
예제 #3
0
void
ns_html_err_no_perm(FILE *fout,
                    struct http_request_info *phr,
                    int priv_mode,
                    const char *format, ...)
{
  const struct contest_desc *cnts = 0;
  struct contest_extra *extra = 0;
  const unsigned char *header = 0, *footer = 0, *separator = 0;
  const unsigned char *copyright = 0;
  time_t cur_time = time(0);
  unsigned char buf[1024];
  va_list args;
  struct html_armor_buffer ab = HTML_ARMOR_INITIALIZER;

  va_start(args, format);
  vsnprintf(buf, sizeof(buf), format, args);
  va_end(args);
  err("%d: permission denied: %s", phr->id, buf);

  if (phr->contest_id > 0) contests_get(phr->contest_id, &cnts);
  if (cnts) extra = ns_get_contest_extra(phr->contest_id);
  if (extra && !priv_mode) {
    watched_file_update(&extra->header, cnts->team_header_file, cur_time);
    watched_file_update(&extra->separator, cnts->team_separator_file, cur_time);
    watched_file_update(&extra->footer, cnts->team_footer_file, cur_time);
    watched_file_update(&extra->copyright, cnts->copyright_file, cur_time);
    header = extra->header.text;
    separator = extra->separator.text;
    footer = extra->footer.text;
    copyright = extra->copyright.text;
  } else if (extra && priv_mode) {
    watched_file_update(&extra->priv_header, cnts->priv_header_file, cur_time);
    watched_file_update(&extra->priv_footer, cnts->priv_footer_file, cur_time);
    header = extra->priv_header.text;
    footer = extra->priv_footer.text;
  }
  if (!priv_mode) {
    if (!header || !footer) {
      header = ns_fancy_header;
      separator = ns_fancy_separator;
      if (copyright) footer = ns_fancy_footer_2;
      else footer = ns_fancy_footer;
    }
  } else {
    if (!header || !footer) {
      header = ns_fancy_priv_header;
      separator = ns_fancy_priv_separator;
      footer = ns_fancy_priv_footer;
    }    
  }
  l10n_setlocale(phr->locale_id);
  ns_header(fout, header, 0, 0, 0, 0, phr->locale_id, cnts, NULL_CLIENT_KEY, _("Permission denied"));
  if (separator && *separator) {
    fprintf(fout, "%s", ns_fancy_empty_status);
    ns_separator(fout, separator, cnts);
  }
  fprintf(fout, "<p>%s</p>\n",
          _("Permission denied. The possible reasons are as follows."));
  fprintf(fout, "<ul>\n");
  fprintf(fout, _("<li>You have typed an invalid login (<tt>%s</tt>).</li>\n"),
          ARMOR(phr->login));
  fprintf(fout, _("<li>You have typed an invalid password.</li>\n"));
  if (!priv_mode) {
    if (cnts) {
      fprintf(fout, _("<li>You are not registered for contest %s.</li>\n"),
              ARMOR(cnts->name));
    } else {
      fprintf(fout, _("<li>You are not registered for contest %d.</li>\n"),
              phr->contest_id);
    }
    fprintf(fout, _("<li>Your registration was not confirmed.</li>\n"));
    fprintf(fout, _("<li>You were banned by the administrator.</li>\n"));
    fprintf(fout, _("<li>Your IP-address (<tt>%s</tt>) or protocol (<tt>%s</tt>) is banned for participation.</li>"), xml_unparse_ipv6(&phr->ip),
            ns_ssl_flag_str[phr->ssl_flag]);
    fprintf(fout, _("<li>The contest is closed for participation.</li>\n"));
    //fprintf(fout, _("<li>The server might be overloaded.</li>\n"));
  } else {
    fprintf(fout, _("<li>Your IP-address (<tt>%s</tt>) or protocol (<tt>%s</tt>) is banned for participation.</li>"), xml_unparse_ipv6(&phr->ip), ns_ssl_flag_str[phr->ssl_flag]);
    fprintf(fout, _("<li>You do not have permissions to login using the specified role.</li>"));
  }
  fprintf(fout, "</ul>\n");
  fprintf(fout, _("<p>Note, that the exact reason is not reported due to security reasons.</p>"));

  fprintf(fout, "<p><big><a href=\"%s?contest_id=%d&amp;locale_id=%d\">%s</a></big></p>", phr->self_url, phr->contest_id, phr->locale_id,
          _("Try again"));

  ns_footer(fout, footer, copyright, phr->locale_id);
  l10n_setlocale(0);
  html_armor_free(&ab);
}
예제 #4
0
void
ns_html_err_inv_session(FILE *fout,
                        struct http_request_info *phr,
                        int priv_mode,
                        const char *format, ...)
{
  const struct contest_desc *cnts = 0;
  struct contest_extra *extra = 0;
  const unsigned char *header = 0, *footer = 0, *separator = 0;
  const unsigned char *copyright = 0;
  time_t cur_time = time(0);
  unsigned char buf[1024];
  va_list args;

  if (format && *format) {
    va_start(args, format);
    vsnprintf(buf, sizeof(buf), format, args);
    va_end(args);
    err("%d: invalid session: %s", phr->id, buf);
  } else {
    err("%d: invalid session", phr->id);
  }

  if (phr->contest_id > 0) contests_get(phr->contest_id, &cnts);
  if (cnts) extra = ns_get_contest_extra(phr->contest_id);
  if (extra && !priv_mode) {
    watched_file_update(&extra->header, cnts->team_header_file, cur_time);
    watched_file_update(&extra->separator, cnts->team_separator_file, cur_time);
    watched_file_update(&extra->footer, cnts->team_footer_file, cur_time);
    watched_file_update(&extra->copyright, cnts->copyright_file, cur_time);
    header = extra->header.text;
    separator = extra->separator.text;
    footer = extra->footer.text;
    copyright = extra->copyright.text;
  } else if (extra && priv_mode) {
    watched_file_update(&extra->priv_header, cnts->priv_header_file, cur_time);
    watched_file_update(&extra->priv_footer, cnts->priv_footer_file, cur_time);
    header = extra->priv_header.text;
    footer = extra->priv_footer.text;
  }
  if (!priv_mode) {
    if (!header || !footer) {
      header = ns_fancy_header;
      separator = ns_fancy_separator;
      if (copyright) footer = ns_fancy_footer_2;
      else footer = ns_fancy_footer;
    }
  } else {
    if (!header || !footer) {
      header = ns_fancy_priv_header;
      separator = ns_fancy_priv_separator;
      footer = ns_fancy_priv_footer;
    }    
  }
  l10n_setlocale(phr->locale_id);
  ns_header(fout, header, 0, 0, 0, 0, phr->locale_id, cnts, NULL_CLIENT_KEY, _("Invalid session"));
  if (separator && *separator) {
    fprintf(fout, "%s", ns_fancy_empty_status);
    ns_separator(fout, separator, cnts);
  }
  fprintf(fout, "<p>%s</p>\n",
          _("Invalid session identifier. The possible reasons are as follows."));
  fprintf(fout, "<ul>\n");
  fprintf(fout, _("<li>The specified session does not exist.</li>\n"));
  fprintf(fout, _("<li>The specified session has expired.</li>\n"));
  fprintf(fout, _("<li>The session was created from a different IP-address or protocol, that yours (%s,%s).</li>\n"), xml_unparse_ipv6(&phr->ip), ns_ssl_flag_str[phr->ssl_flag]);
  fprintf(fout, _("<li>The session was removed by an administrator.</li>"));
  fprintf(fout, "</ul>\n");
  fprintf(fout, _("<p>Note, that the exact reason is not reported due to security reasons.</p>"));
  ns_footer(fout, footer, copyright, phr->locale_id);
  l10n_setlocale(0);
}
예제 #5
0
파일: rldb_mysql.c 프로젝트: mezun7/ejudge
static void
generate_update_entry_clause(
        struct rldb_mysql_state *state,
        FILE *f,
        const struct run_entry *re,
        int flags)
{
  struct timeval curtime;
  const unsigned char *sep = "";
  const unsigned char *comma = ", ";

  if ((flags & RE_SIZE)) {
    fprintf(f, "%ssize = %d", sep, re->size);
    sep = comma;
  }
  if ((flags & RE_USER_ID)) {
    fprintf(f, "%suser_id = %d", sep, re->user_id);
    sep = comma;
  }
  if ((flags & RE_PROB_ID)) {
    fprintf(f, "%sprob_id = %d", sep, re->prob_id);
    sep = comma;
  }
  if ((flags & RE_LANG_ID)) {
    fprintf(f, "%slang_id = %d", sep, re->lang_id);
    sep = comma;
  }
  if ((flags & RE_STATUS)) {
    fprintf(f, "%sstatus = %d", sep, re->status);
    sep = comma;
  }
  if ((flags & RE_SSL_FLAG)) {
    fprintf(f, "%sssl_flag = %d", sep, re->ssl_flag);
    sep = comma;
  }
  if ((flags & RE_IP)) {
    int ip_version = 4;
    if (re->ipv6_flag) ip_version = 6;
    fprintf(f, "%sip_version = %d", sep, ip_version);
    sep = comma;
    ej_ip_t ipv6;
    run_entry_to_ipv6(re, &ipv6);
    fprintf(f, "%sip = '%s'", sep, xml_unparse_ipv6(&ipv6));
  }
  if ((flags & RE_SHA1)) {
    if (!re->sha1[0] && !re->sha1[1] && !re->sha1[2]
        && !re->sha1[3] && !re->sha1[4]) {
      fprintf(f, "%shash = NULL", sep);
    } else {
      fprintf(f, "%shash = '%s'", sep, unparse_sha1(re->sha1));
    }
    sep =comma;
  }
  if ((flags & RE_RUN_UUID)) {
#if CONF_HAS_LIBUUID - 0 != 0
    if (!re->run_uuid.v[0] && !re->run_uuid.v[1] && !re->run_uuid.v[2] && !re->run_uuid.v[3]) {
      fprintf(f, "%srun_uuid = NULL", sep);
    } else {
      char uuid_buf[40];
      uuid_unparse((void*) &re->run_uuid, uuid_buf);
      fprintf(f, "%srun_uuid = '%s'", sep, uuid_buf);
    }
    sep =comma;
#endif
  }
  if ((flags & RE_SCORE)) {
    fprintf(f, "%sscore = %d", sep, re->score);
    sep = comma;
  }
  if ((flags & RE_TEST)) {
    fprintf(f, "%stest_num = %d", sep, re->test);
    sep = comma;
  }
  if ((flags & RE_SCORE_ADJ)) {
    fprintf(f, "%sscore_adj = %d", sep, re->score_adj);
    sep = comma;
  }
  if ((flags & RE_LOCALE_ID)) {
    fprintf(f, "%slocale_id = %d", sep, re->locale_id);
    sep = comma;
  }
  if ((flags & RE_JUDGE_ID)) {
    fprintf(f, "%sjudge_id = %d", sep, re->judge_id);
    sep = comma;
  }
  if ((flags & RE_VARIANT)) {
    fprintf(f, "%svariant = %d", sep, re->variant);
    sep = comma;
  }
  if ((flags & RE_PAGES)) {
    fprintf(f, "%spages = %d", sep, re->pages);
    sep = comma;
  }
  if ((flags & RE_IS_IMPORTED)) {
    fprintf(f, "%sis_imported = %d", sep, re->is_imported);
    sep = comma;
  }
  if ((flags & RE_IS_HIDDEN)) {
    fprintf(f, "%sis_hidden = %d", sep, re->is_hidden);
    sep = comma;
  }
  if ((flags & RE_IS_READONLY)) {
    fprintf(f, "%sis_readonly = %d", sep, re->is_readonly);
    sep = comma;
  }
  if ((flags & RE_MIME_TYPE)) {
    if (re->mime_type > 0) {
      fprintf(f, "%smime_type = '%s'", sep, mime_type_get_type(re->mime_type));
    } else {
      fprintf(f, "%smime_type = NULL", sep);
    }
    sep = comma;
  }
  if ((flags & RE_IS_MARKED)) {
    fprintf(f, "%sis_marked = %d", sep, re->is_marked);
    sep = comma;
  }
  if ((flags & RE_IS_SAVED)) {
    fprintf(f, "%sis_saved = %d", sep, re->is_saved);
    sep = comma;
  }
  if ((flags & RE_SAVED_STATUS)) {
    fprintf(f, "%ssaved_status = %d", sep, re->saved_status);
    sep = comma;
  }
  if ((flags & RE_SAVED_SCORE)) {
    fprintf(f, "%ssaved_score = %d", sep, re->saved_score);
    sep = comma;
  }
  if ((flags & RE_SAVED_TEST)) {
    fprintf(f, "%ssaved_test = %d", sep, re->saved_test);
    sep = comma;
  }
  if ((flags & RE_PASSED_MODE)) {
    fprintf(f, "%spassed_mode = %d", sep, !!re->passed_mode);
    sep = comma;
  }
  if ((flags & RE_EOLN_TYPE)) {
    fprintf(f, "%seoln_type = %d", sep, re->eoln_type);
    sep = comma;
  }
  if ((flags & RE_STORE_FLAGS)) {
    fprintf(f, "%sstore_flags = %d", sep, re->store_flags);
    sep = comma;
  }
  if ((flags & RE_TOKEN_FLAGS)) {
    fprintf(f, "%stoken_flags = %d", sep, re->token_flags);
    sep = comma;
  }
  if ((flags & RE_TOKEN_COUNT)) {
    fprintf(f, "%stoken_count = %d", sep, re->token_count);
    sep = comma;
  }

  gettimeofday(&curtime, 0);
  fprintf(f, "%slast_change_time = ", sep);
  state->mi->write_timestamp(state->md, f, 0, curtime.tv_sec);
  sep = comma;
  fprintf(f, "%slast_change_nsec = %ld", sep, curtime.tv_usec * 1000);
}