]>
Commit | Line | Data |
---|---|---|
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" | |
25 | #include "res.h" | |
26 | #include "dns.h" | |
27 | ||
28 | struct user_query | |
29 | { | |
30 | rb_dlink_node node; | |
31 | ||
32 | struct auth_client *auth; /* Our client */ | |
33 | struct dns_query *query; /* Pending DNS query */ | |
34 | time_t timeout; /* When the request times out */ | |
35 | }; | |
36 | ||
37 | /* Goinked from old s_auth.c --Elizabeth */ | |
38 | static const char *messages[] = | |
39 | { | |
40 | "*** Looking up your hostname...", | |
41 | "*** Found your hostname", | |
42 | "*** Couldn't look up your hostname", | |
43 | "*** Your hostname is too long, ignoring hostname", | |
44 | }; | |
45 | ||
46 | typedef enum | |
47 | { | |
48 | REPORT_LOOKUP, | |
49 | REPORT_FOUND, | |
50 | REPORT_FAIL, | |
51 | REPORT_TOOLONG, | |
52 | } dns_message; | |
53 | ||
54 | static void client_fail(struct user_query *query, dns_message message); | |
55 | static void client_success(struct user_query *query); | |
56 | static void get_dns_answer(const char *res, bool status, query_type type, void *data); | |
57 | ||
58 | static struct ev_entry *timeout_ev; | |
59 | static EVH timeout_dns_queries_event; | |
60 | static int rdns_timeout = 30; | |
61 | static rb_dlink_list queries; /* Stored here for easy timeout */ | |
62 | ||
63 | ||
64 | bool client_dns_init(void) | |
65 | { | |
66 | timeout_ev = rb_event_addish("timeout_dns_queries_event", timeout_dns_queries_event, NULL, 5); | |
67 | return (timeout_ev != NULL); | |
68 | } | |
69 | ||
70 | void client_dns_destroy(void) | |
71 | { | |
72 | rb_dlink_node *ptr, *nptr; | |
73 | struct user_query *query; | |
74 | ||
75 | RB_DLINK_FOREACH_SAFE(ptr, nptr, queries.head) | |
76 | { | |
77 | client_fail(ptr->data, REPORT_FAIL); | |
78 | rb_dlinkDelete(ptr, &queries); | |
79 | rb_free(ptr); | |
80 | } | |
81 | ||
82 | rb_event_delete(timeout_ev); | |
83 | } | |
84 | ||
85 | bool client_dns_start(struct auth_client *auth) | |
86 | { | |
87 | struct user_query *query = rb_malloc(sizeof(struct user_query)); | |
88 | ||
89 | query->auth = auth; | |
90 | query->timeout = rb_current_time() + rdns_timeout; | |
91 | ||
92 | query->query = lookup_hostname(auth->c_ip, get_dns_answer, query); | |
93 | ||
94 | notice_client(auth, messages[REPORT_LOOKUP]); | |
95 | set_provider(auth, PROVIDER_RDNS); | |
96 | return true; | |
97 | } | |
98 | ||
99 | void client_dns_cancel(struct auth_client *auth) | |
100 | { | |
101 | rb_dlink_node *ptr, *nptr; | |
102 | ||
103 | RB_DLINK_FOREACH_SAFE(ptr, nptr, queries.head) | |
104 | { | |
105 | struct user_query *query = ptr->data; | |
106 | ||
107 | if(query->auth == auth) | |
108 | { | |
109 | /* Victim found */ | |
110 | client_fail(query, REPORT_FAIL); | |
111 | rb_dlinkDelete(ptr, &queries); | |
112 | rb_free(query); | |
113 | return; | |
114 | } | |
115 | } | |
116 | } | |
117 | ||
118 | static void | |
119 | get_dns_answer(const char *res, bool status, query_type type, void *data) | |
120 | { | |
121 | struct user_query *query = data; | |
122 | struct auth_client *auth = query->auth; | |
123 | bool fail = false; | |
124 | dns_message response; | |
125 | rb_dlink_node *ptr, *nptr; | |
126 | ||
127 | if(res == NULL || status == false) | |
128 | { | |
129 | response = REPORT_FAIL; | |
130 | fail = true; | |
131 | goto cleanup; | |
132 | } | |
133 | else if(strlen(res) > HOSTLEN) | |
134 | { | |
135 | /* Ah well. */ | |
136 | response = REPORT_TOOLONG; | |
137 | fail = true; | |
138 | goto cleanup; | |
139 | } | |
140 | ||
141 | rb_strlcpy(auth->hostname, res, HOSTLEN + 1); | |
142 | ||
143 | cleanup: | |
144 | /* Clean us up off the pending queries list */ | |
145 | RB_DLINK_FOREACH_SAFE(ptr, nptr, queries.head) | |
146 | { | |
147 | struct user_query *query_l = ptr->data; | |
148 | ||
149 | if(query == query_l) | |
150 | { | |
151 | /* Found */ | |
152 | if(fail) | |
153 | client_fail(query, response); | |
154 | else | |
155 | client_success(query); | |
156 | ||
157 | rb_dlinkDelete(ptr, &queries); | |
158 | rb_free(query); | |
159 | return; | |
160 | } | |
161 | } | |
162 | } | |
163 | ||
164 | /* Timeout outstanding queries */ | |
165 | static void timeout_dns_queries_event(void *notused) | |
166 | { | |
167 | rb_dlink_node *ptr, *nptr; | |
168 | ||
169 | RB_DLINK_FOREACH_SAFE(ptr, nptr, queries.head) | |
170 | { | |
171 | struct user_query *query = ptr->data; | |
172 | ||
173 | if(query->auth && query->timeout < rb_current_time()) | |
174 | { | |
175 | client_fail(query, REPORT_FAIL); | |
176 | rb_dlinkDelete(ptr, &queries); | |
177 | rb_free(query); | |
178 | return; | |
179 | } | |
180 | } | |
181 | } | |
182 | ||
183 | static void client_fail(struct user_query *query, dns_message report) | |
184 | { | |
185 | struct auth_client *auth = query->auth; | |
186 | ||
187 | rb_strlcpy(auth->hostname, "*", sizeof(auth->hostname)); | |
188 | notice_client(auth, messages[report]); | |
189 | cancel_query(query->query); | |
190 | provider_done(auth, PROVIDER_RDNS); | |
191 | } | |
192 | ||
193 | static void client_success(struct user_query *query) | |
194 | { | |
195 | struct auth_client *auth = query->auth; | |
196 | ||
197 | notice_client(auth, messages[REPORT_FOUND]); | |
198 | cancel_query(query->query); | |
199 | provider_done(auth, PROVIDER_RDNS); | |
200 | } | |
201 | ||
202 | struct auth_provider rdns_provider = | |
203 | { | |
204 | .id = PROVIDER_RDNS, | |
205 | .init = client_dns_init, | |
206 | .destroy = client_dns_destroy, | |
207 | .start = client_dns_start, | |
208 | .cancel = client_dns_cancel, | |
209 | .completed = NULL, | |
210 | }; |