]> jfr.im git - solanum.git/blob - authd/providers/rdns.c
authd: mention rDNS in found your hostname message
[solanum.git] / authd / providers / rdns.c
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"
25 #include "notice.h"
26 #include "res.h"
27 #include "dns.h"
28
29 struct user_query
30 {
31 struct dns_query *query; /* Pending DNS query */
32 };
33
34 /* Goinked from old s_auth.c --Elizabeth */
35 static const char *messages[] =
36 {
37 "*** Looking up your hostname...",
38 "*** Couldn't look up your hostname",
39 "*** Your hostname is too long, ignoring hostname",
40 };
41
42 typedef enum
43 {
44 REPORT_LOOKUP,
45 REPORT_FAIL,
46 REPORT_TOOLONG,
47 } dns_message;
48
49 static void client_fail(struct auth_client *auth, dns_message message);
50 static void client_success(struct auth_client *auth);
51 static void dns_answer_callback(const char *res, bool status, query_type type, void *data);
52
53 static int rdns_timeout = 15;
54
55 static void
56 dns_answer_callback(const char *res, bool status, query_type type, void *data)
57 {
58 struct auth_client *auth = data;
59 struct user_query *query = get_provider_data(auth, PROVIDER_RDNS);
60
61 lrb_assert(query != NULL);
62
63 if(res == NULL || status == false)
64 client_fail(auth, REPORT_FAIL);
65 else if(strlen(res) > HOSTLEN)
66 client_fail(auth, REPORT_TOOLONG);
67 else
68 {
69 rb_strlcpy(auth->hostname, res, HOSTLEN + 1);
70 client_success(auth);
71 }
72 }
73
74 static void
75 client_fail(struct auth_client *auth, dns_message report)
76 {
77 struct user_query *query = get_provider_data(auth, PROVIDER_RDNS);
78
79 lrb_assert(query != NULL);
80
81 rb_strlcpy(auth->hostname, "*", sizeof(auth->hostname));
82
83 notice_client(auth->cid, messages[report]);
84 cancel_query(query->query);
85
86 rb_free(query);
87
88 set_provider_data(auth, PROVIDER_RDNS, NULL);
89 set_provider_timeout_absolute(auth, PROVIDER_RDNS, 0);
90 provider_done(auth, PROVIDER_RDNS);
91 }
92
93 static void
94 client_success(struct auth_client *auth)
95 {
96 struct user_query *query = get_provider_data(auth, PROVIDER_RDNS);
97
98 lrb_assert(query != NULL);
99
100 notice_client(auth->cid, "*** Found your hostname: %s", auth->hostname);
101 cancel_query(query->query);
102
103 rb_free(query);
104
105 set_provider_data(auth, PROVIDER_RDNS, NULL);
106 set_provider_timeout_absolute(auth, PROVIDER_RDNS, 0);
107 provider_done(auth, PROVIDER_RDNS);
108 }
109
110 static void
111 rdns_destroy(void)
112 {
113 struct auth_client *auth;
114 rb_dictionary_iter iter;
115
116 RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
117 {
118 if(get_provider_data(auth, PROVIDER_RDNS) != NULL)
119 client_fail(auth, REPORT_FAIL);
120 }
121 }
122
123 static bool
124 rdns_start(struct auth_client *auth)
125 {
126 struct user_query *query = rb_malloc(sizeof(struct user_query));
127
128 set_provider_data(auth, PROVIDER_RDNS, query);
129 set_provider_timeout_relative(auth, PROVIDER_RDNS, rdns_timeout);
130
131 query->query = lookup_hostname(auth->c_ip, dns_answer_callback, auth);
132
133 notice_client(auth->cid, messages[REPORT_LOOKUP]);
134 set_provider_on(auth, PROVIDER_RDNS);
135 return true;
136 }
137
138 static void
139 rdns_cancel(struct auth_client *auth)
140 {
141 struct user_query *query = get_provider_data(auth, PROVIDER_RDNS);
142
143 if(query != NULL)
144 client_fail(auth, REPORT_FAIL);
145 }
146
147 static void
148 add_conf_dns_timeout(const char *key, int parc, const char **parv)
149 {
150 int timeout = atoi(parv[0]);
151
152 if(timeout < 0)
153 {
154 warn_opers(L_CRIT, "rDNS: DNS timeout < 0 (value: %d)", timeout);
155 exit(EX_PROVIDER_ERROR);
156 }
157
158 rdns_timeout = timeout;
159 }
160
161 struct auth_opts_handler rdns_options[] =
162 {
163 { "rdns_timeout", 1, add_conf_dns_timeout },
164 { NULL, 0, NULL },
165 };
166
167 struct auth_provider rdns_provider =
168 {
169 .id = PROVIDER_RDNS,
170 .destroy = rdns_destroy,
171 .start = rdns_start,
172 .cancel = rdns_cancel,
173 .timeout = rdns_cancel,
174 .opt_handlers = rdns_options,
175 };