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