static int check_token_is_valid(struct MHD_Connection *connection, t_client *client) { /* token check */ struct collect_query_key query_key = { .key = "token" }; MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, &collect_query_key, &query_key); /* token not found in query string */ if (!query_key.value) return 0; /* token doesn't match */ if (strcmp(client->token, query_key.value)) return 0; return 1; } /** * @brief try_to_authenticate * @param connection * @param client * @param host * @param url * @return */ static int try_to_authenticate(struct MHD_Connection *connection, t_client *client, const char *host, const char *url) { /* a successful auth looks like * http://192.168.42.1:2050/nodogsplash_auth/?redir=http%3A%2F%2Fberlin.freifunk.net%2F&tok=94c4cdd2 * when authaction -> http://192.168.42.1:2050/nodogsplash_auth/ */ s_config *config = config_get_config(); /* we are checking here for the second '/' of /denydir/ */ if (check_authdir_match(url, config->authdir)) { /* matched to authdir */ if (check_token_is_valid(connection, client)) { return 1; /* valid token */ } } else if (check_authdir_match(url, config->denydir)) { /* matched to deauth */ /* TODO: do we need denydir? */ return 0; } return 0; }
static int check_token_is_valid(struct MHD_Connection *connection, t_client *client) { /* token check */ struct collect_query_key token_key = { .key = "token" }; struct collect_query_key tok_key = { .key = "tok" }; MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, &collect_query_key, &token_key); MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, &collect_query_key, &tok_key); /* token not found in query string */ if (!token_key.value && !tok_key.value) return 0; if (token_key.value && !strcmp(client->token, token_key.value)) return 1; if (tok_key.value && !strcmp(client->token, tok_key.value)) return 1; return 0; } /** * @brief try_to_authenticate * @param connection * @param client * @param host * @param url * @return */ static int try_to_authenticate(struct MHD_Connection *connection, t_client *client, const char *host, const char *url) { /* a successful auth looks like * http://192.168.42.1:2050/nodogsplash_auth/?redir=http%3A%2F%2Fberlin.freifunk.net%2F&tok=94c4cdd2 * when authaction -> http://192.168.42.1:2050/nodogsplash_auth/ */ s_config *config = config_get_config(); /* we are checking here for the second '/' of /denydir/ */ if (check_authdir_match(url, config->authdir)) { /* matched to authdir */ if (check_token_is_valid(connection, client)) { return 1; /* valid token */ } } else if (check_authdir_match(url, config->denydir)) { /* matched to deauth */ /* TODO: do we need denydir? */ return 0; } return 0; } /** * @brief authenticate the client and redirect them * @param connection * @param ip_addr - needs to be freed * @param mac - needs to be freed * @param redirect_url - redirect the client to this url * @return */ static int authenticate_client(struct MHD_Connection *connection, const char *ip_addr, const char *mac, const char *redirect_url, t_client *client) { /* TODO: handle redirect_url == NULL */ auth_client_action(ip_addr, mac, AUTH_MAKE_AUTHENTICATED); if (redirect_url) return send_redirect_temp(connection, redirect_url); else return send_error(connection, 200); }