/** * @brief redirect_to_splashpage * @param connection * @param client * @param host * @param url * @return */ static int redirect_to_splashpage(struct MHD_Connection *connection, t_client *client, const char *host, const char *url) { char *originurl = NULL; char *query = ""; get_query(connection, &query); safe_asprintf(&originurl, "http://%s%s%s%s", host, url, strlen(query) ? "?" : "" , query); return encode_and_redirect_to_splashpage(connection, originurl); }
/** * @brief preauthenticated - called for all request of a client in this state. * @param connection * @param ip_addr * @param mac * @return */ static int preauthenticated(struct MHD_Connection *connection, const char *ip_addr, const char *mac, const char *url, t_client *client) { char *host = NULL; const char *redirect_url; s_config *config = config_get_config(); if (!client) { client = add_client(ip_addr); if (!client) return send_error(connection, 503); } MHD_get_connection_values(connection, MHD_HEADER_KIND, get_host_value_callback, &host); /* check if this is a redirect querty with a foreign host as target */ if(is_foreign_hosts(connection, host)) { return redirect_to_splashpage(connection, client, host, url); } /* request is directed to us */ /* check if client wants to be authenticated */ if(check_authdir_match(url, config->authdir)) { /* Only the first request will redirected to config->redirectURL. * When the client reloads a page when it's authenticated, it should be redirected * to their origin url */ if (config->redirectURL) redirect_url = config->redirectURL; else redirect_url = get_redirect_url(connection); if (try_to_authenticate(connection, client, host, url)) { return authenticate_client(connection, ip_addr, mac, redirect_url, client); } else { /* user used an invalid token, redirect to splashpage but hold query "redir" intact */ return encode_and_redirect_to_splashpage(connection, redirect_url); } } if(is_splashpage(host, url)) { return show_splashpage(connection, client); } /* no special handling left - try to serve static content to the user */ return serve_file(connection, client, url); }
/** * @brief redirect_to_splashpage * @param connection * @param client * @param host * @param url * @return */ static int redirect_to_splashpage(struct MHD_Connection *connection, t_client *client, const char *host, const char *url) { char *originurl = NULL; char *query = NULL; int ret = 0; get_query(connection, &query); if (!query) { /* no mem */ return send_error(connection, 503); } safe_asprintf(&originurl, "http://%s%s%s%s", host, url, strlen(query) ? "?" : "" , query); ret = encode_and_redirect_to_splashpage(connection, originurl); free(originurl); free(query); return ret; }