@@ -9398,6 +9398,8 @@ error_page(
93989398 const unsigned char * const * error_names = external_unpriv_error_names;
93999399 ExternalActionState **error_states = external_unpriv_error_states;
94009400
9401+ if (error_code < 0) error_code = -error_code;
9402+
94019403 if (phr->json_reply) {
94029404 struct html_armor_buffer ab = HTML_ARMOR_INITIALIZER;
94039405 if (phr->log_f) {
@@ -9427,7 +9429,6 @@ error_page(
94279429 xfree(phr->log_t); phr->log_t = NULL; phr->log_z = 0;
94289430 }
94299431
9430- if (error_code < 0) error_code = -error_code;
94319432 if (error_code <= 0 || error_code >= NEW_SRV_ERR_LAST) error_code = NEW_SRV_ERR_UNKNOWN_ERROR;
94329433 phr->error_code = error_code;
94339434
@@ -9566,7 +9567,7 @@ copy_cti_to_phr(
95669567 cti->access_time = current_time;
95679568}
95689569
9569- static __attribute__((unused)) int
9570+ static int
95709571priv_check_cached_key(struct http_request_info *phr)
95719572{
95729573 long long tsc_start;
@@ -9721,7 +9722,7 @@ priv_check_cached_key(struct http_request_info *phr)
97219722 return 0;
97229723}
97239724
9724- static __attribute__((unused)) int
9725+ static int
97259726priv_check_cached_session(struct http_request_info *phr)
97269727{
97279728 long long tsc_start;
@@ -9896,59 +9897,9 @@ privileged_entry_point(
98969897 goto cleanup;
98979898 }
98989899
9899- if (ns_open_ul_connection(phr->fw_state) < 0) {
9900- error_page(fout, phr, 0, -NEW_SRV_ERR_USERLIST_SERVER_DOWN);
9901- goto cleanup;
9902- }
9903- struct userlist_api_key in_api_key = {};
9904- memcpy(in_api_key.token, phr->token, 32);
9905- in_api_key.contest_id = phr->contest_id;
9906- struct userlist_contest_info cnts_info = {};
9907- struct userlist_api_key *out_keys = NULL;
9908- int out_count = 0;
9909- r = userlist_clnt_api_key_request(ul_conn, ULS_GET_API_KEY, 1, &in_api_key, &out_count, &out_keys, &cnts_info);
9910- phr->login = cnts_info.login; cnts_info.login = NULL;
9911- phr->name = cnts_info.name; cnts_info.name = NULL;
9912- phr->contest_id = cnts_info.contest_id;
9913- phr->user_id = cnts_info.user_id;
9914-
9915- if (r <= 0) {
9916- fprintf(phr->log_f, "invalid token\n");
9917- error_page(fout, phr, 0, -NEW_SRV_ERR_PERMISSION_DENIED);
9918- goto cleanup;
9919- }
9920- if (out_count != 1) {
9921- error_page(fout, phr, 0, -NEW_SRV_ERR_INTERNAL);
9922- goto cleanup;
9923- }
9924- if (cnts_info.user_id == 0 || cnts_info.contest_id == 0) {
9925- fprintf(phr->log_f, "invalid user_id or contest_id\n");
9926- error_page(fout, phr, 0, -NEW_SRV_ERR_PERMISSION_DENIED);
9927- goto cleanup;
9928- }
9929- if (phr->contest_id > 0 && cnts_info.contest_id != phr->contest_id) {
9930- fprintf(phr->log_f, "invalid contest_id\n");
9931- error_page(fout, phr, 0, -NEW_SRV_ERR_PERMISSION_DENIED);
9932- goto cleanup;
9933- }
9934- if (cnts_info.reg_status != USERLIST_REG_OK) {
9935- fprintf(phr->log_f, "user not registered\n");
9936- error_page(fout, phr, 0, -NEW_SRV_ERR_PERMISSION_DENIED);
9937- goto cleanup;
9938- }
9939- if ((cnts_info.reg_flags & (USERLIST_UC_BANNED | USERLIST_UC_LOCKED | USERLIST_UC_DISQUALIFIED)) != 0) {
9940- fprintf(phr->log_f, "user banned\n");
9941- error_page(fout, phr, 0, -NEW_SRV_ERR_PERMISSION_DENIED);
9942- goto cleanup;
9943- }
9944- if (phr->role > out_keys[0].role ) {
9945- fprintf(phr->log_f, "invalid role\n");
9946- error_page(fout, phr, 0, -NEW_SRV_ERR_PERMISSION_DENIED);
9947- goto cleanup;
9948- }
9949- if (out_keys[0].expiry_time > 0 && cur_time >= out_keys[0].expiry_time) {
9950- fprintf(phr->log_f, "token expired\n");
9951- error_page(fout, phr, 0, -NEW_SRV_ERR_PERMISSION_DENIED);
9900+ r = priv_check_cached_key(phr);
9901+ if (r < 0) {
9902+ error_page(fout, phr, 0, r);
99529903 goto cleanup;
99539904 }
99549905 } else {
@@ -9958,36 +9909,11 @@ privileged_entry_point(
99589909 if (!phr->session_id || phr->action == NEW_SRV_ACTION_LOGIN_PAGE)
99599910 return privileged_page_login(fout, phr);
99609911
9961- // validate cookie
9962- if (ns_open_ul_connection(phr->fw_state) < 0) {
9963- error_page(fout, phr, 1, NEW_SRV_ERR_USERLIST_SERVER_DOWN );
9912+ r = priv_check_cached_session(phr);
9913+ if (r < 0) {
9914+ error_page(fout, phr, 1, r );
99649915 goto cleanup;
99659916 }
9966- if ((r = userlist_clnt_get_cookie(ul_conn, ULS_PRIV_GET_COOKIE,
9967- &phr->ip, phr->ssl_flag,
9968- phr->session_id,
9969- phr->client_key,
9970- &phr->user_id, &phr->contest_id,
9971- &phr->locale_id, 0, &phr->role, 0, 0, 0,
9972- NULL /* p_passwd_method */,
9973- NULL /* p_is_ws */,
9974- NULL /* p_is_job */,
9975- NULL /* p_expire */,
9976- &phr->login, &phr->name)) < 0) {
9977- switch (-r) {
9978- case ULS_ERR_NO_COOKIE:
9979- fprintf(phr->log_f, "priv_get_cookie failed: %s\n", userlist_strerror(-r));
9980- error_page(fout, phr, 1, NEW_SRV_ERR_INV_SESSION);
9981- goto cleanup;
9982- case ULS_ERR_DISCONNECT:
9983- error_page(fout, phr, 1, NEW_SRV_ERR_USERLIST_SERVER_DOWN);
9984- goto cleanup;
9985- default:
9986- fprintf(phr->log_f, "priv_get_cookie failed: %s\n", userlist_strerror(-r));
9987- error_page(fout, phr, 1, NEW_SRV_ERR_INTERNAL);
9988- goto cleanup;
9989- }
9990- }
99919917 }
99929918
99939919 if (phr->locale_id < 0) phr->locale_id = 0;
0 commit comments