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 */
34 /* Goinked from old s_auth.c --Elizabeth */
35 static const char *messages
[] =
37 "*** Looking up your hostname...",
38 "*** Couldn't look up your hostname",
39 "*** Your hostname is too long, ignoring hostname",
49 static void client_fail(struct auth_client
*auth
, dns_message message
);
50 static void client_success(struct auth_client
*auth
);
51 static void dns_answer_callback(const char *res
, bool status
, query_type type
, void *data
);
53 static int rdns_timeout
= 15;
56 dns_answer_callback(const char *res
, bool status
, query_type type
, void *data
)
58 struct auth_client
*auth
= data
;
59 struct user_query
*query
= get_provider_data(auth
, PROVIDER_RDNS
);
61 lrb_assert(query
!= NULL
);
63 if(res
== NULL
|| status
== false)
64 client_fail(auth
, REPORT_FAIL
);
65 else if(strlen(res
) > HOSTLEN
)
66 client_fail(auth
, REPORT_TOOLONG
);
69 rb_strlcpy(auth
->hostname
, res
, HOSTLEN
+ 1);
75 client_fail(struct auth_client
*auth
, dns_message report
)
77 struct user_query
*query
= get_provider_data(auth
, PROVIDER_RDNS
);
79 lrb_assert(query
!= NULL
);
81 rb_strlcpy(auth
->hostname
, "*", sizeof(auth
->hostname
));
83 notice_client(auth
->cid
, messages
[report
]);
84 cancel_query(query
->query
);
88 set_provider_data(auth
, PROVIDER_RDNS
, NULL
);
89 set_provider_timeout_absolute(auth
, PROVIDER_RDNS
, 0);
90 provider_done(auth
, PROVIDER_RDNS
);
94 client_success(struct auth_client
*auth
)
96 struct user_query
*query
= get_provider_data(auth
, PROVIDER_RDNS
);
98 lrb_assert(query
!= NULL
);
100 notice_client(auth
->cid
, "*** Found your hostname: %s", auth
->hostname
);
101 cancel_query(query
->query
);
105 set_provider_data(auth
, PROVIDER_RDNS
, NULL
);
106 set_provider_timeout_absolute(auth
, PROVIDER_RDNS
, 0);
107 provider_done(auth
, PROVIDER_RDNS
);
113 struct auth_client
*auth
;
114 rb_dictionary_iter iter
;
116 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
118 if(get_provider_data(auth
, PROVIDER_RDNS
) != NULL
)
119 client_fail(auth
, REPORT_FAIL
);
124 rdns_start(struct auth_client
*auth
)
126 struct user_query
*query
= rb_malloc(sizeof(struct user_query
));
128 set_provider_data(auth
, PROVIDER_RDNS
, query
);
129 set_provider_timeout_relative(auth
, PROVIDER_RDNS
, rdns_timeout
);
131 query
->query
= lookup_hostname(auth
->c_ip
, dns_answer_callback
, auth
);
133 notice_client(auth
->cid
, messages
[REPORT_LOOKUP
]);
134 set_provider_on(auth
, PROVIDER_RDNS
);
139 rdns_cancel(struct auth_client
*auth
)
141 struct user_query
*query
= get_provider_data(auth
, PROVIDER_RDNS
);
144 client_fail(auth
, REPORT_FAIL
);
148 add_conf_dns_timeout(const char *key
, int parc
, const char **parv
)
150 int timeout
= atoi(parv
[0]);
154 warn_opers(L_CRIT
, "rDNS: DNS timeout < 0 (value: %d)", timeout
);
155 exit(EX_PROVIDER_ERROR
);
158 rdns_timeout
= timeout
;
161 struct auth_opts_handler rdns_options
[] =
163 { "rdns_timeout", 1, add_conf_dns_timeout
},
167 struct auth_provider rdns_provider
=
170 .destroy
= rdns_destroy
,
172 .cancel
= rdns_cancel
,
173 .timeout
= rdns_cancel
,
174 .opt_handlers
= rdns_options
,