1 /* authd/providers/rdns.c - rDNS lookup provider for authd
2 * Copyright (c) 2016 Elizabeth Myers <elizabeth@interlinked.me>
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.
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.
22 #include "rb_commio.h"
31 struct dns_query
*query
; /* Pending DNS query */
32 time_t timeout
; /* When the request times out */
35 /* Goinked from old s_auth.c --Elizabeth */
36 static const char *messages
[] =
38 "*** Looking up your hostname...",
39 "*** Found your hostname",
40 "*** Couldn't look up your hostname",
41 "*** Your hostname is too long, ignoring hostname",
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
);
56 static struct ev_entry
*timeout_ev
;
57 static EVH timeout_dns_queries_event
;
58 static int rdns_timeout
= 15;
61 dns_answer_callback(const char *res
, bool status
, query_type type
, void *data
)
63 struct auth_client
*auth
= data
;
64 struct user_query
*query
= auth
->data
[PROVIDER_RDNS
];
66 if(query
== NULL
|| res
== NULL
|| status
== false)
67 client_fail(auth
, REPORT_FAIL
);
68 else if(strlen(res
) > HOSTLEN
)
69 client_fail(auth
, REPORT_TOOLONG
);
72 rb_strlcpy(auth
->hostname
, res
, HOSTLEN
+ 1);
77 /* Timeout outstanding queries */
79 timeout_dns_queries_event(void *notused
)
81 struct auth_client
*auth
;
82 rb_dictionary_iter iter
;
84 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
86 struct user_query
*query
= auth
->data
[PROVIDER_RDNS
];
88 if(query
!= NULL
&& query
->timeout
< rb_current_time())
90 client_fail(auth
, REPORT_FAIL
);
97 client_fail(struct auth_client
*auth
, dns_message report
)
99 struct user_query
*query
= auth
->data
[PROVIDER_RDNS
];
104 rb_strlcpy(auth
->hostname
, "*", sizeof(auth
->hostname
));
106 notice_client(auth
->cid
, messages
[report
]);
107 cancel_query(query
->query
);
110 auth
->data
[PROVIDER_RDNS
] = NULL
;
112 provider_done(auth
, PROVIDER_RDNS
);
116 client_success(struct auth_client
*auth
)
118 struct user_query
*query
= auth
->data
[PROVIDER_RDNS
];
120 notice_client(auth
->cid
, messages
[REPORT_FOUND
]);
121 cancel_query(query
->query
);
124 auth
->data
[PROVIDER_RDNS
] = NULL
;
126 provider_done(auth
, PROVIDER_RDNS
);
132 timeout_ev
= rb_event_addish("timeout_dns_queries_event", timeout_dns_queries_event
, NULL
, 1);
133 return (timeout_ev
!= NULL
);
139 struct auth_client
*auth
;
140 rb_dictionary_iter iter
;
142 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
144 if(auth
->data
[PROVIDER_RDNS
] != NULL
)
145 client_fail(auth
, REPORT_FAIL
);
148 rb_event_delete(timeout_ev
);
152 rdns_start(struct auth_client
*auth
)
154 struct user_query
*query
= rb_malloc(sizeof(struct user_query
));
156 query
->timeout
= rb_current_time() + rdns_timeout
;
158 auth
->data
[PROVIDER_RDNS
] = query
;
160 query
->query
= lookup_hostname(auth
->c_ip
, dns_answer_callback
, auth
);
162 notice_client(auth
->cid
, messages
[REPORT_LOOKUP
]);
163 set_provider_on(auth
, PROVIDER_RDNS
);
168 rdns_cancel(struct auth_client
*auth
)
170 struct user_query
*query
= auth
->data
[PROVIDER_RDNS
];
173 client_fail(auth
, REPORT_FAIL
);
177 add_conf_dns_timeout(const char *key
, int parc
, const char **parv
)
179 int timeout
= atoi(parv
[0]);
183 warn_opers(L_CRIT
, "BUG: DNS timeout < 0 (value: %d)", timeout
);
187 rdns_timeout
= timeout
;
190 struct auth_opts_handler rdns_options
[] =
192 { "rdns_timeout", 1, add_conf_dns_timeout
},
196 struct auth_provider rdns_provider
=
200 .destroy
= rdns_destroy
,
202 .cancel
= rdns_cancel
,
204 .opt_handlers
= rdns_options
,