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