2016-03-17 21:23:27 +00:00
|
|
|
/* authd/providers/rdns.c - rDNS lookup provider for authd
|
|
|
|
* Copyright (c) 2016 Elizabeth Myers <elizabeth@interlinked.me>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdinc.h"
|
|
|
|
#include "rb_commio.h"
|
|
|
|
#include "authd.h"
|
|
|
|
#include "provider.h"
|
|
|
|
#include "res.h"
|
|
|
|
#include "dns.h"
|
|
|
|
|
|
|
|
struct user_query
|
|
|
|
{
|
|
|
|
struct dns_query *query; /* Pending DNS query */
|
|
|
|
time_t timeout; /* When the request times out */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Goinked from old s_auth.c --Elizabeth */
|
|
|
|
static const char *messages[] =
|
|
|
|
{
|
|
|
|
"*** Looking up your hostname...",
|
|
|
|
"*** Found your hostname",
|
|
|
|
"*** Couldn't look up your hostname",
|
|
|
|
"*** Your hostname is too long, ignoring hostname",
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
REPORT_LOOKUP,
|
|
|
|
REPORT_FOUND,
|
|
|
|
REPORT_FAIL,
|
|
|
|
REPORT_TOOLONG,
|
|
|
|
} dns_message;
|
|
|
|
|
2016-03-23 00:13:54 +00:00
|
|
|
static void client_fail(struct auth_client *auth, dns_message message);
|
|
|
|
static void client_success(struct auth_client *auth);
|
2016-03-24 02:59:48 +00:00
|
|
|
static void dns_answer_callback(const char *res, bool status, query_type type, void *data);
|
2016-03-17 21:23:27 +00:00
|
|
|
|
|
|
|
static struct ev_entry *timeout_ev;
|
|
|
|
static EVH timeout_dns_queries_event;
|
2016-03-26 00:47:54 +00:00
|
|
|
static int rdns_timeout = 15;
|
2016-03-17 21:23:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool client_dns_init(void)
|
|
|
|
{
|
2016-03-26 00:47:54 +00:00
|
|
|
timeout_ev = rb_event_addish("timeout_dns_queries_event", timeout_dns_queries_event, NULL, 1);
|
2016-03-17 21:23:27 +00:00
|
|
|
return (timeout_ev != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void client_dns_destroy(void)
|
|
|
|
{
|
2016-03-23 00:13:54 +00:00
|
|
|
struct auth_client *auth;
|
2016-03-23 14:03:37 +00:00
|
|
|
rb_dictionary_iter iter;
|
2016-03-17 21:23:27 +00:00
|
|
|
|
2016-03-23 19:29:27 +00:00
|
|
|
RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
|
2016-03-17 21:23:27 +00:00
|
|
|
{
|
2016-03-23 00:13:54 +00:00
|
|
|
if(auth->data[PROVIDER_RDNS] != NULL)
|
|
|
|
client_fail(auth, REPORT_FAIL);
|
2016-03-17 21:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_event_delete(timeout_ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool client_dns_start(struct auth_client *auth)
|
|
|
|
{
|
|
|
|
struct user_query *query = rb_malloc(sizeof(struct user_query));
|
|
|
|
|
|
|
|
query->timeout = rb_current_time() + rdns_timeout;
|
|
|
|
|
2016-03-23 00:13:54 +00:00
|
|
|
auth->data[PROVIDER_RDNS] = query;
|
|
|
|
|
2016-03-24 02:59:48 +00:00
|
|
|
query->query = lookup_hostname(auth->c_ip, dns_answer_callback, auth);
|
2016-03-17 21:23:27 +00:00
|
|
|
|
|
|
|
notice_client(auth, messages[REPORT_LOOKUP]);
|
|
|
|
set_provider(auth, PROVIDER_RDNS);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void client_dns_cancel(struct auth_client *auth)
|
|
|
|
{
|
2016-03-23 00:13:54 +00:00
|
|
|
struct user_query *query = auth->data[PROVIDER_RDNS];
|
2016-03-17 21:23:27 +00:00
|
|
|
|
2016-03-23 00:13:54 +00:00
|
|
|
if(query != NULL)
|
|
|
|
client_fail(auth, REPORT_FAIL);
|
2016-03-17 21:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-03-24 02:59:48 +00:00
|
|
|
dns_answer_callback(const char *res, bool status, query_type type, void *data)
|
2016-03-17 21:23:27 +00:00
|
|
|
{
|
2016-03-23 00:13:54 +00:00
|
|
|
struct auth_client *auth = data;
|
|
|
|
struct user_query *query = auth->data[PROVIDER_RDNS];
|
2016-03-17 21:23:27 +00:00
|
|
|
|
2016-03-23 00:13:54 +00:00
|
|
|
if(query == NULL || res == NULL || status == false)
|
|
|
|
client_fail(auth, REPORT_FAIL);
|
2016-03-17 21:23:27 +00:00
|
|
|
else if(strlen(res) > HOSTLEN)
|
2016-03-23 00:13:54 +00:00
|
|
|
client_fail(auth, REPORT_TOOLONG);
|
|
|
|
else
|
2016-03-17 21:23:27 +00:00
|
|
|
{
|
2016-03-23 00:13:54 +00:00
|
|
|
rb_strlcpy(auth->hostname, res, HOSTLEN + 1);
|
|
|
|
client_success(auth);
|
2016-03-17 21:23:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Timeout outstanding queries */
|
|
|
|
static void timeout_dns_queries_event(void *notused)
|
|
|
|
{
|
2016-03-23 00:13:54 +00:00
|
|
|
struct auth_client *auth;
|
2016-03-23 14:03:37 +00:00
|
|
|
rb_dictionary_iter iter;
|
2016-03-17 21:23:27 +00:00
|
|
|
|
2016-03-23 19:29:27 +00:00
|
|
|
RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
|
2016-03-17 21:23:27 +00:00
|
|
|
{
|
2016-03-23 00:13:54 +00:00
|
|
|
struct user_query *query = auth->data[PROVIDER_RDNS];
|
2016-03-17 21:23:27 +00:00
|
|
|
|
2016-03-23 00:13:54 +00:00
|
|
|
if(query != NULL && query->timeout < rb_current_time())
|
2016-03-17 21:23:27 +00:00
|
|
|
{
|
2016-03-23 00:13:54 +00:00
|
|
|
client_fail(auth, REPORT_FAIL);
|
2016-03-17 21:23:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 00:13:54 +00:00
|
|
|
static void client_fail(struct auth_client *auth, dns_message report)
|
2016-03-17 21:23:27 +00:00
|
|
|
{
|
2016-03-23 00:13:54 +00:00
|
|
|
struct user_query *query = auth->data[PROVIDER_RDNS];
|
|
|
|
|
|
|
|
if(query == NULL)
|
|
|
|
return;
|
2016-03-17 21:23:27 +00:00
|
|
|
|
|
|
|
rb_strlcpy(auth->hostname, "*", sizeof(auth->hostname));
|
2016-03-23 00:13:54 +00:00
|
|
|
|
2016-03-17 21:23:27 +00:00
|
|
|
notice_client(auth, messages[report]);
|
|
|
|
cancel_query(query->query);
|
2016-03-23 00:13:54 +00:00
|
|
|
|
|
|
|
rb_free(query);
|
|
|
|
auth->data[PROVIDER_RDNS] = NULL;
|
|
|
|
|
2016-03-17 21:23:27 +00:00
|
|
|
provider_done(auth, PROVIDER_RDNS);
|
|
|
|
}
|
|
|
|
|
2016-03-23 00:13:54 +00:00
|
|
|
static void client_success(struct auth_client *auth)
|
2016-03-17 21:23:27 +00:00
|
|
|
{
|
2016-03-23 00:13:54 +00:00
|
|
|
struct user_query *query = auth->data[PROVIDER_RDNS];
|
2016-03-17 21:23:27 +00:00
|
|
|
|
|
|
|
notice_client(auth, messages[REPORT_FOUND]);
|
|
|
|
cancel_query(query->query);
|
2016-03-23 00:13:54 +00:00
|
|
|
|
|
|
|
rb_free(query);
|
|
|
|
auth->data[PROVIDER_RDNS] = NULL;
|
|
|
|
|
2016-03-17 21:23:27 +00:00
|
|
|
provider_done(auth, PROVIDER_RDNS);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct auth_provider rdns_provider =
|
|
|
|
{
|
|
|
|
.id = PROVIDER_RDNS,
|
|
|
|
.init = client_dns_init,
|
|
|
|
.destroy = client_dns_destroy,
|
|
|
|
.start = client_dns_start,
|
|
|
|
.cancel = client_dns_cancel,
|
|
|
|
.completed = NULL,
|
|
|
|
};
|