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