]> jfr.im git - solanum.git/blob - authd/providers/rdns.c
authd: split out notices stuff for backporting to master.
[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 time_t timeout; /* When the request times out */
33 };
34
35 /* Goinked from old s_auth.c --Elizabeth */
36 static const char *messages[] =
37 {
38 "*** Looking up your hostname...",
39 "*** Found your hostname",
40 "*** Couldn't look up your hostname",
41 "*** Your hostname is too long, ignoring hostname",
42 };
43
44 typedef enum
45 {
46 REPORT_LOOKUP,
47 REPORT_FOUND,
48 REPORT_FAIL,
49 REPORT_TOOLONG,
50 } dns_message;
51
52 static void client_fail(struct auth_client *auth, dns_message message);
53 static void client_success(struct auth_client *auth);
54 static void dns_answer_callback(const char *res, bool status, query_type type, void *data);
55
56 static struct ev_entry *timeout_ev;
57 static EVH timeout_dns_queries_event;
58 static int rdns_timeout = 15;
59
60
61 bool client_dns_init(void)
62 {
63 timeout_ev = rb_event_addish("timeout_dns_queries_event", timeout_dns_queries_event, NULL, 1);
64 return (timeout_ev != NULL);
65 }
66
67 void client_dns_destroy(void)
68 {
69 struct auth_client *auth;
70 rb_dictionary_iter iter;
71
72 RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
73 {
74 if(auth->data[PROVIDER_RDNS] != NULL)
75 client_fail(auth, REPORT_FAIL);
76 }
77
78 rb_event_delete(timeout_ev);
79 }
80
81 bool client_dns_start(struct auth_client *auth)
82 {
83 struct user_query *query = rb_malloc(sizeof(struct user_query));
84
85 query->timeout = rb_current_time() + rdns_timeout;
86
87 auth->data[PROVIDER_RDNS] = query;
88
89 query->query = lookup_hostname(auth->c_ip, dns_answer_callback, auth);
90
91 notice_client(auth->cid, messages[REPORT_LOOKUP]);
92 set_provider_on(auth, PROVIDER_RDNS);
93 return true;
94 }
95
96 void client_dns_cancel(struct auth_client *auth)
97 {
98 struct user_query *query = auth->data[PROVIDER_RDNS];
99
100 if(query != NULL)
101 client_fail(auth, REPORT_FAIL);
102 }
103
104 static void
105 dns_answer_callback(const char *res, bool status, query_type type, void *data)
106 {
107 struct auth_client *auth = data;
108 struct user_query *query = auth->data[PROVIDER_RDNS];
109
110 if(query == NULL || res == NULL || status == false)
111 client_fail(auth, REPORT_FAIL);
112 else if(strlen(res) > HOSTLEN)
113 client_fail(auth, REPORT_TOOLONG);
114 else
115 {
116 rb_strlcpy(auth->hostname, res, HOSTLEN + 1);
117 client_success(auth);
118 }
119 }
120
121 /* Timeout outstanding queries */
122 static void timeout_dns_queries_event(void *notused)
123 {
124 struct auth_client *auth;
125 rb_dictionary_iter iter;
126
127 RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
128 {
129 struct user_query *query = auth->data[PROVIDER_RDNS];
130
131 if(query != NULL && query->timeout < rb_current_time())
132 {
133 client_fail(auth, REPORT_FAIL);
134 return;
135 }
136 }
137 }
138
139 static void client_fail(struct auth_client *auth, dns_message report)
140 {
141 struct user_query *query = auth->data[PROVIDER_RDNS];
142
143 if(query == NULL)
144 return;
145
146 rb_strlcpy(auth->hostname, "*", sizeof(auth->hostname));
147
148 notice_client(auth->cid, messages[report]);
149 cancel_query(query->query);
150
151 rb_free(query);
152 auth->data[PROVIDER_RDNS] = NULL;
153
154 provider_done(auth, PROVIDER_RDNS);
155 }
156
157 static void client_success(struct auth_client *auth)
158 {
159 struct user_query *query = auth->data[PROVIDER_RDNS];
160
161 notice_client(auth->cid, messages[REPORT_FOUND]);
162 cancel_query(query->query);
163
164 rb_free(query);
165 auth->data[PROVIDER_RDNS] = NULL;
166
167 provider_done(auth, PROVIDER_RDNS);
168 }
169
170 struct auth_provider rdns_provider =
171 {
172 .id = PROVIDER_RDNS,
173 .init = client_dns_init,
174 .destroy = client_dns_destroy,
175 .start = client_dns_start,
176 .cancel = client_dns_cancel,
177 .completed = NULL,
178 };