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"
29 #define SELF_PID (rdns_provider.id)
33 struct dns_query
*query
; /* Pending DNS query */
36 /* Goinked from old s_auth.c --Elizabeth */
37 static const char *messages
[] =
39 "*** Looking up your hostname...",
40 "*** Couldn't look up your hostname",
41 "*** Your hostname is too long, ignoring hostname",
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
);
55 static int rdns_timeout
= RDNS_TIMEOUT_DEFAULT
;
58 dns_answer_callback(const char *res
, bool status
, query_type type
, void *data
)
60 struct auth_client
*auth
= data
;
61 struct user_query
*query
= get_provider_data(auth
, SELF_PID
);
63 lrb_assert(query
!= NULL
);
65 if(res
== NULL
|| status
== false)
66 client_fail(auth
, REPORT_FAIL
);
67 else if(strlen(res
) > HOSTLEN
)
68 client_fail(auth
, REPORT_TOOLONG
);
71 rb_strlcpy(auth
->hostname
, res
, HOSTLEN
+ 1);
77 client_fail(struct auth_client
*auth
, dns_message report
)
79 struct user_query
*query
= get_provider_data(auth
, SELF_PID
);
81 lrb_assert(query
!= NULL
);
83 rb_strlcpy(auth
->hostname
, "*", sizeof(auth
->hostname
));
85 notice_client(auth
->cid
, messages
[report
]);
86 cancel_query(query
->query
);
90 set_provider_data(auth
, SELF_PID
, NULL
);
91 set_provider_timeout_absolute(auth
, SELF_PID
, 0);
92 provider_done(auth
, SELF_PID
);
94 auth_client_unref(auth
);
98 client_success(struct auth_client
*auth
)
100 struct user_query
*query
= get_provider_data(auth
, SELF_PID
);
102 lrb_assert(query
!= NULL
);
104 notice_client(auth
->cid
, "*** Found your hostname: %s", auth
->hostname
);
105 cancel_query(query
->query
);
109 set_provider_data(auth
, SELF_PID
, NULL
);
110 set_provider_timeout_absolute(auth
, SELF_PID
, 0);
111 provider_done(auth
, SELF_PID
);
113 auth_client_unref(auth
);
119 struct auth_client
*auth
;
120 rb_dictionary_iter iter
;
122 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
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 */
131 rdns_start(struct auth_client
*auth
)
133 struct user_query
*query
= rb_malloc(sizeof(struct user_query
));
135 auth_client_ref(auth
);
137 set_provider_data(auth
, SELF_PID
, query
);
138 set_provider_timeout_relative(auth
, SELF_PID
, rdns_timeout
);
140 query
->query
= lookup_hostname(auth
->c_ip
, dns_answer_callback
, auth
);
142 notice_client(auth
->cid
, messages
[REPORT_LOOKUP
]);
143 set_provider_running(auth
, SELF_PID
);
148 rdns_cancel(struct auth_client
*auth
)
150 struct user_query
*query
= get_provider_data(auth
, SELF_PID
);
153 client_fail(auth
, REPORT_FAIL
);
157 add_conf_dns_timeout(const char *key
, int parc
, const char **parv
)
159 int timeout
= atoi(parv
[0]);
163 warn_opers(L_CRIT
, "rDNS: DNS timeout < 0 (value: %d)", timeout
);
164 exit(EX_PROVIDER_ERROR
);
167 rdns_timeout
= timeout
;
170 struct auth_opts_handler rdns_options
[] =
172 { "rdns_timeout", 1, add_conf_dns_timeout
},
176 struct auth_provider rdns_provider
=
180 .destroy
= rdns_destroy
,
182 .cancel
= rdns_cancel
,
183 .timeout
= rdns_cancel
,
184 .opt_handlers
= rdns_options
,