/** * @brief encode originurl and redirect the client to the splash page * @param connection * @param client * @param originurl * @return */ static int encode_and_redirect_to_splashpage(struct MHD_Connection *connection, const char *originurl) { char *splashpageurl = NULL; char encoded[2048]; int ret; s_config *config = config_get_config(); memset(encoded, 0, sizeof(encoded)); if (originurl) { if (uh_urlencode(encoded, 2048, originurl, strlen(originurl)) == -1) { debug(LOG_WARNING, "could not encode url"); } else { debug(LOG_DEBUG, "originurl: %s", originurl); } } if (encoded[0]) safe_asprintf(&splashpageurl, "http://%s:%u%s?redir=%s", config->gw_address , config->gw_port, "/splash.html", encoded); else safe_asprintf(&splashpageurl, "http://%s:%u%s", config->gw_address , config->gw_port, "/splash.html"); debug(LOG_DEBUG, "splashpageurl: %s", splashpageurl); ret = send_redirect_temp(connection, splashpageurl); free(splashpageurl); return ret; }
/** * @brief show_splashpage is called when the client clicked on Ok as well when the client doesn't know us yet. * @param connection * @param client * @return */ static int show_splashpage(struct MHD_Connection *connection, t_client *client) { struct MHD_Response *response; struct templater templor; s_config *config = config_get_config(); int ret = -1; char filename[PATH_MAX]; const char *mimetype; int size = 0, bytes = 0; int splashpage_fd; char *splashpage_result; char *splashpage_tmpl; snprintf(filename, PATH_MAX, "%s/%s",config->webroot ,config->splashpage); splashpage_fd = open(filename, O_RDONLY); if (splashpage_fd < 0) return send_error(connection, 404); mimetype = lookup_mimetype(filename); /* input size */ size = lseek(splashpage_fd, 0, SEEK_END); lseek(splashpage_fd, 0, SEEK_SET); /* we TMPLVAR_SIZE for template variables */ splashpage_tmpl = calloc(1, size); splashpage_result = calloc(1, size + TMPLVAR_SIZE); while (bytes < size) { ret = read(splashpage_fd, splashpage_tmpl+bytes, size-bytes); if (ret < 0) { free(splashpage_result); free(splashpage_tmpl); close(splashpage_fd); return send_error(connection, 503); } bytes += ret; } char *uptime = get_uptime_string(); char *nclients = NULL; char *maxclients = NULL; char *denyaction = NULL; char *authaction = NULL; char *authtarget = NULL; const char *redirect_url = NULL; char redirect_url_encoded[2048]; char *imagesdir = NULL; char *pagesdir = NULL; memset(redirect_url_encoded, 0, sizeof(redirect_url_encoded)); redirect_url = get_redirect_url(connection); if (redirect_url) { uh_urlencode(redirect_url_encoded, sizeof(redirect_url_encoded), redirect_url, strlen(redirect_url)); } safe_asprintf(&nclients, "%d", get_client_list_length()); safe_asprintf(&maxclients, "%d", config->maxclients); safe_asprintf(&denyaction, "http://%s:%d/%s/", config->gw_address, config->gw_port, config->denydir); safe_asprintf(&authaction, "http://%s:%d/%s/", config->gw_address, config->gw_port, config->authdir); safe_asprintf(&authtarget, "http://%s:%d/%s/?token=%s&redir=%s", config->gw_address, config->gw_port, config->authdir, client->token, redirect_url_encoded); safe_asprintf(&authaction, "http://%s:%d/%s/", config->gw_address, config->gw_port, config->authdir); safe_asprintf(&pagesdir, "/%s", config->pagesdir); safe_asprintf(&imagesdir, "/%s", config->imagesdir); tmpl_init_templor(&templor); tmpl_set_variable(&templor, "authaction", authaction); tmpl_set_variable(&templor, "authtarget", authtarget); tmpl_set_variable(&templor, "clientip", client->ip); tmpl_set_variable(&templor, "clientmac", client->mac); // tmpl_set_variable(&templor, "content", VERSION); tmpl_set_variable(&templor, "denyaction", denyaction); tmpl_set_variable(&templor, "error_msg", ""); tmpl_set_variable(&templor, "gatewaymac", config->gw_mac); tmpl_set_variable(&templor, "gatewayname", config->gw_name); tmpl_set_variable(&templor, "imagesdir", imagesdir); tmpl_set_variable(&templor, "pagesdir", pagesdir); tmpl_set_variable(&templor, "maxclients", maxclients); tmpl_set_variable(&templor, "nclients", nclients); tmpl_set_variable(&templor, "redir", redirect_url); tmpl_set_variable(&templor, "tok", client->token); tmpl_set_variable(&templor, "uptime", uptime); tmpl_set_variable(&templor, "version", VERSION); tmpl_parse(&templor, splashpage_result, size + TMPLVAR_SIZE, splashpage_tmpl, size); free(authaction); free(denyaction); free(maxclients); free(nclients); free(uptime); free(splashpage_tmpl); free(imagesdir); response = MHD_create_response_from_buffer(strlen(splashpage_result), (void *)splashpage_result, MHD_RESPMEM_MUST_FREE); if (!response) { close(splashpage_fd); return send_error(connection, 503); } MHD_add_response_header(response, "Content-Type", mimetype); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); close(splashpage_fd); return ret; }