2016-03-17 22:23:27 +01: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.
|
|
|
|
*/
|
|
|
|
|
2016-07-01 05:04:00 +02:00
|
|
|
#include <ircd/stdinc.h>
|
2016-03-17 22:23:27 +01:00
|
|
|
#include "authd.h"
|
|
|
|
#include "provider.h"
|
2016-03-26 03:29:44 +01:00
|
|
|
#include "notice.h"
|
2016-03-17 22:23:27 +01:00
|
|
|
#include "res.h"
|
|
|
|
#include "dns.h"
|
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
#define SELF_PID (rdns_provider.id)
|
|
|
|
|
2016-03-17 22:23:27 +01:00
|
|
|
struct user_query
|
|
|
|
{
|
|
|
|
struct dns_query *query; /* Pending DNS query */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Goinked from old s_auth.c --Elizabeth */
|
|
|
|
static const char *messages[] =
|
|
|
|
{
|
|
|
|
"*** Looking up your hostname...",
|
|
|
|
"*** Couldn't look up your hostname",
|
|
|
|
"*** Your hostname is too long, ignoring hostname",
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
REPORT_LOOKUP,
|
|
|
|
REPORT_FAIL,
|
|
|
|
REPORT_TOOLONG,
|
|
|
|
} dns_message;
|
|
|
|
|
2016-03-23 01:13:54 +01:00
|
|
|
static void client_fail(struct auth_client *auth, dns_message message);
|
|
|
|
static void client_success(struct auth_client *auth);
|
2016-03-24 03:59:48 +01:00
|
|
|
static void dns_answer_callback(const char *res, bool status, query_type type, void *data);
|
2016-03-17 22:23:27 +01:00
|
|
|
|
2016-04-07 11:47:48 +02:00
|
|
|
static int rdns_timeout = RDNS_TIMEOUT_DEFAULT;
|
2016-03-17 22:23:27 +01:00
|
|
|
|
|
|
|
static void
|
2016-03-24 03:59:48 +01:00
|
|
|
dns_answer_callback(const char *res, bool status, query_type type, void *data)
|
2016-03-17 22:23:27 +01:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct auth_client *auth = (auth_client *)data;
|
|
|
|
struct user_query *query = (user_query *)get_provider_data(auth, SELF_PID);
|
2016-03-17 22:23:27 +01:00
|
|
|
|
2016-04-04 11:32:55 +02:00
|
|
|
lrb_assert(query != NULL);
|
|
|
|
|
|
|
|
if(res == NULL || status == false)
|
2016-03-23 01:13:54 +01:00
|
|
|
client_fail(auth, REPORT_FAIL);
|
2016-03-17 22:23:27 +01:00
|
|
|
else if(strlen(res) > HOSTLEN)
|
2016-03-23 01:13:54 +01:00
|
|
|
client_fail(auth, REPORT_TOOLONG);
|
|
|
|
else
|
2016-03-17 22:23:27 +01:00
|
|
|
{
|
2016-03-23 01:13:54 +01:00
|
|
|
rb_strlcpy(auth->hostname, res, HOSTLEN + 1);
|
|
|
|
client_success(auth);
|
2016-03-17 22:23:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 21:36:12 +01:00
|
|
|
static void
|
|
|
|
client_fail(struct auth_client *auth, dns_message report)
|
2016-03-17 22:23:27 +01:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct user_query *query = (user_query *)get_provider_data(auth, SELF_PID);
|
2016-03-23 01:13:54 +01:00
|
|
|
|
2016-04-04 11:32:55 +02:00
|
|
|
lrb_assert(query != NULL);
|
2016-03-17 22:23:27 +01:00
|
|
|
|
|
|
|
rb_strlcpy(auth->hostname, "*", sizeof(auth->hostname));
|
2016-03-23 01:13:54 +01:00
|
|
|
|
2016-03-26 03:29:44 +01:00
|
|
|
notice_client(auth->cid, messages[report]);
|
2016-03-17 22:23:27 +01:00
|
|
|
cancel_query(query->query);
|
2016-03-23 01:13:54 +01:00
|
|
|
|
|
|
|
rb_free(query);
|
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
set_provider_data(auth, SELF_PID, NULL);
|
|
|
|
set_provider_timeout_absolute(auth, SELF_PID, 0);
|
|
|
|
provider_done(auth, SELF_PID);
|
2016-05-01 12:31:05 +02:00
|
|
|
|
|
|
|
auth_client_unref(auth);
|
2016-03-17 22:23:27 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 21:36:12 +01:00
|
|
|
static void
|
|
|
|
client_success(struct auth_client *auth)
|
2016-03-17 22:23:27 +01:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct user_query *query = (user_query *)get_provider_data(auth, SELF_PID);
|
2016-04-04 11:32:55 +02:00
|
|
|
|
|
|
|
lrb_assert(query != NULL);
|
2016-03-17 22:23:27 +01:00
|
|
|
|
2016-04-05 07:56:43 +02:00
|
|
|
notice_client(auth->cid, "*** Found your hostname: %s", auth->hostname);
|
2016-03-17 22:23:27 +01:00
|
|
|
cancel_query(query->query);
|
2016-03-23 01:13:54 +01:00
|
|
|
|
|
|
|
rb_free(query);
|
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
set_provider_data(auth, SELF_PID, NULL);
|
|
|
|
set_provider_timeout_absolute(auth, SELF_PID, 0);
|
|
|
|
provider_done(auth, SELF_PID);
|
2016-05-01 12:31:05 +02:00
|
|
|
|
|
|
|
auth_client_unref(auth);
|
2016-03-17 22:23:27 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 21:36:12 +01:00
|
|
|
static void
|
2016-03-26 22:37:04 +01:00
|
|
|
rdns_destroy(void)
|
2016-03-26 21:36:12 +01:00
|
|
|
{
|
2016-05-01 02:20:12 +02:00
|
|
|
struct auth_client *auth;
|
|
|
|
rb_dictionary_iter iter;
|
2016-03-26 21:36:12 +01:00
|
|
|
|
2016-07-13 07:17:21 +02:00
|
|
|
void *elem;
|
|
|
|
RB_DICTIONARY_FOREACH(elem, &iter, auth_clients)
|
2016-03-26 21:36:12 +01:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
auth = (auth_client *)elem;
|
2016-04-05 11:31:22 +02:00
|
|
|
if(get_provider_data(auth, SELF_PID) != NULL)
|
2016-03-26 21:36:12 +01:00
|
|
|
client_fail(auth, REPORT_FAIL);
|
2016-05-01 12:31:05 +02:00
|
|
|
/* auth is now invalid as we have no reference */
|
2016-03-26 21:36:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2016-03-26 22:37:04 +01:00
|
|
|
rdns_start(struct auth_client *auth)
|
2016-03-26 21:36:12 +01:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct user_query *query = (user_query *)rb_malloc(sizeof(struct user_query));
|
2016-03-26 21:36:12 +01:00
|
|
|
|
2016-05-01 12:31:05 +02:00
|
|
|
auth_client_ref(auth);
|
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
set_provider_data(auth, SELF_PID, query);
|
|
|
|
set_provider_timeout_relative(auth, SELF_PID, rdns_timeout);
|
2016-03-26 21:36:12 +01:00
|
|
|
|
|
|
|
query->query = lookup_hostname(auth->c_ip, dns_answer_callback, auth);
|
|
|
|
|
|
|
|
notice_client(auth->cid, messages[REPORT_LOOKUP]);
|
2016-04-05 11:31:22 +02:00
|
|
|
set_provider_running(auth, SELF_PID);
|
2016-03-26 21:36:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-03-26 22:37:04 +01:00
|
|
|
rdns_cancel(struct auth_client *auth)
|
2016-03-26 21:36:12 +01:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct user_query *query = (user_query *)get_provider_data(auth, SELF_PID);
|
2016-03-26 21:36:12 +01:00
|
|
|
|
|
|
|
if(query != NULL)
|
|
|
|
client_fail(auth, REPORT_FAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_conf_dns_timeout(const char *key, int parc, const char **parv)
|
|
|
|
{
|
|
|
|
int timeout = atoi(parv[0]);
|
|
|
|
|
|
|
|
if(timeout < 0)
|
|
|
|
{
|
2016-03-29 02:22:02 +02:00
|
|
|
warn_opers(L_CRIT, "rDNS: DNS timeout < 0 (value: %d)", timeout);
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
2016-03-26 21:36:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rdns_timeout = timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct auth_opts_handler rdns_options[] =
|
|
|
|
{
|
2016-03-28 08:55:54 +02:00
|
|
|
{ "rdns_timeout", 1, add_conf_dns_timeout },
|
2016-03-26 21:36:12 +01:00
|
|
|
{ NULL, 0, NULL },
|
|
|
|
};
|
|
|
|
|
2016-07-23 00:11:39 +02:00
|
|
|
struct auth_provider rdns_provider =
|
|
|
|
[]{
|
2016-07-13 07:17:21 +02:00
|
|
|
auth_provider ap {0};
|
|
|
|
ap.name = "rdns";
|
|
|
|
ap.letter = 'R';
|
|
|
|
ap.destroy = rdns_destroy;
|
|
|
|
ap.start = rdns_start;
|
|
|
|
ap.cancel = rdns_cancel;
|
|
|
|
ap.timeout = rdns_cancel;
|
|
|
|
ap.opt_handlers = rdns_options;
|
|
|
|
return ap;
|
2016-07-23 00:11:39 +02:00
|
|
|
}();
|