]> jfr.im git - solanum.git/blame - authd/providers/rdns.c
check bans and quiets for cmode -n/nonmember PRIVMSG
[solanum.git] / authd / providers / rdns.c
CommitLineData
2b0cc3d3
EM
1/* authd/providers/rdns.c - rDNS lookup provider for authd
2 * Copyright (c) 2016 Elizabeth Myers <elizabeth@interlinked.me>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice is present in all copies.
7 *
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
12 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
16 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
17 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18 * POSSIBILITY OF SUCH DAMAGE.
19 */
20
21#include "stdinc.h"
22#include "rb_commio.h"
23#include "authd.h"
24#include "provider.h"
db821ee9 25#include "notice.h"
2b0cc3d3
EM
26#include "res.h"
27#include "dns.h"
28
731d1289
EM
29#define SELF_PID (rdns_provider.id)
30
2b0cc3d3
EM
31struct user_query
32{
2b0cc3d3 33 struct dns_query *query; /* Pending DNS query */
2b0cc3d3
EM
34};
35
36/* Goinked from old s_auth.c --Elizabeth */
37static const char *messages[] =
38{
39 "*** Looking up your hostname...",
2b0cc3d3
EM
40 "*** Couldn't look up your hostname",
41 "*** Your hostname is too long, ignoring hostname",
42};
43
44typedef enum
45{
46 REPORT_LOOKUP,
2b0cc3d3
EM
47 REPORT_FAIL,
48 REPORT_TOOLONG,
49} dns_message;
50
3e875f62
EM
51static void client_fail(struct auth_client *auth, dns_message message);
52static void client_success(struct auth_client *auth);
410fcc23 53static void dns_answer_callback(const char *res, bool status, query_type type, void *data);
2b0cc3d3 54
9057170c 55static int rdns_timeout = RDNS_TIMEOUT_DEFAULT;
2b0cc3d3 56
2b0cc3d3 57static void
410fcc23 58dns_answer_callback(const char *res, bool status, query_type type, void *data)
2b0cc3d3 59{
3e875f62 60 struct auth_client *auth = data;
bdddd9ba
EM
61
62 if(res == NULL || status == false)
3e875f62 63 client_fail(auth, REPORT_FAIL);
2b0cc3d3 64 else if(strlen(res) > HOSTLEN)
3e875f62
EM
65 client_fail(auth, REPORT_TOOLONG);
66 else
2b0cc3d3 67 {
3e875f62
EM
68 rb_strlcpy(auth->hostname, res, HOSTLEN + 1);
69 client_success(auth);
2b0cc3d3
EM
70 }
71}
72
646e6567
EM
73static void
74client_fail(struct auth_client *auth, dns_message report)
2b0cc3d3 75{
731d1289 76 struct user_query *query = get_provider_data(auth, SELF_PID);
3e875f62 77
bdddd9ba 78 lrb_assert(query != NULL);
2b0cc3d3
EM
79
80 rb_strlcpy(auth->hostname, "*", sizeof(auth->hostname));
3e875f62 81
db821ee9 82 notice_client(auth->cid, messages[report]);
2b0cc3d3 83 cancel_query(query->query);
3e875f62
EM
84
85 rb_free(query);
3e875f62 86
731d1289
EM
87 set_provider_data(auth, SELF_PID, NULL);
88 set_provider_timeout_absolute(auth, SELF_PID, 0);
89 provider_done(auth, SELF_PID);
a5f52774
SA
90
91 auth_client_unref(auth);
2b0cc3d3
EM
92}
93
646e6567
EM
94static void
95client_success(struct auth_client *auth)
2b0cc3d3 96{
731d1289 97 struct user_query *query = get_provider_data(auth, SELF_PID);
bdddd9ba
EM
98
99 lrb_assert(query != NULL);
2b0cc3d3 100
d4756226 101 notice_client(auth->cid, "*** Found your hostname: %s", auth->hostname);
2b0cc3d3 102 cancel_query(query->query);
3e875f62
EM
103
104 rb_free(query);
3e875f62 105
731d1289
EM
106 set_provider_data(auth, SELF_PID, NULL);
107 set_provider_timeout_absolute(auth, SELF_PID, 0);
108 provider_done(auth, SELF_PID);
a5f52774
SA
109
110 auth_client_unref(auth);
2b0cc3d3
EM
111}
112
646e6567 113static void
6ced6a1f 114rdns_destroy(void)
646e6567 115{
a71b65b1
AC
116 struct auth_client *auth;
117 rb_dictionary_iter iter;
646e6567 118
a71b65b1 119 RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
646e6567 120 {
731d1289 121 if(get_provider_data(auth, SELF_PID) != NULL)
646e6567 122 client_fail(auth, REPORT_FAIL);
a5f52774 123 /* auth is now invalid as we have no reference */
646e6567 124 }
646e6567
EM
125}
126
127static bool
6ced6a1f 128rdns_start(struct auth_client *auth)
646e6567
EM
129{
130 struct user_query *query = rb_malloc(sizeof(struct user_query));
131
a5f52774
SA
132 auth_client_ref(auth);
133
731d1289
EM
134 set_provider_data(auth, SELF_PID, query);
135 set_provider_timeout_relative(auth, SELF_PID, rdns_timeout);
646e6567
EM
136
137 query->query = lookup_hostname(auth->c_ip, dns_answer_callback, auth);
138
139 notice_client(auth->cid, messages[REPORT_LOOKUP]);
646e6567
EM
140 return true;
141}
142
143static void
6ced6a1f 144rdns_cancel(struct auth_client *auth)
646e6567 145{
731d1289 146 struct user_query *query = get_provider_data(auth, SELF_PID);
646e6567
EM
147
148 if(query != NULL)
149 client_fail(auth, REPORT_FAIL);
150}
151
152static void
153add_conf_dns_timeout(const char *key, int parc, const char **parv)
154{
155 int timeout = atoi(parv[0]);
156
157 if(timeout < 0)
158 {
34b96d7f
EM
159 warn_opers(L_CRIT, "rDNS: DNS timeout < 0 (value: %d)", timeout);
160 exit(EX_PROVIDER_ERROR);
646e6567
EM
161 }
162
163 rdns_timeout = timeout;
164}
165
166struct auth_opts_handler rdns_options[] =
167{
bd7c2037 168 { "rdns_timeout", 1, add_conf_dns_timeout },
646e6567
EM
169 { NULL, 0, NULL },
170};
171
2b0cc3d3
EM
172struct auth_provider rdns_provider =
173{
731d1289
EM
174 .name = "rdns",
175 .letter = 'R',
6ced6a1f
EM
176 .destroy = rdns_destroy,
177 .start = rdns_start,
178 .cancel = rdns_cancel,
15c49abb 179 .timeout = rdns_cancel,
646e6567 180 .opt_handlers = rdns_options,
2b0cc3d3 181};