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
;
62 if(res
== NULL
|| status
== false)
63 client_fail(auth
, REPORT_FAIL
);
64 else if(strlen(res
) > HOSTLEN
)
65 client_fail(auth
, REPORT_TOOLONG
);
68 rb_strlcpy(auth
->hostname
, res
, HOSTLEN
+ 1);
74 client_fail(struct auth_client
*auth
, dns_message report
)
76 struct user_query
*query
= get_provider_data(auth
, SELF_PID
);
78 lrb_assert(query
!= NULL
);
80 rb_strlcpy(auth
->hostname
, "*", sizeof(auth
->hostname
));
82 notice_client(auth
->cid
, messages
[report
]);
83 cancel_query(query
->query
);
87 set_provider_data(auth
, SELF_PID
, NULL
);
88 set_provider_timeout_absolute(auth
, SELF_PID
, 0);
89 provider_done(auth
, SELF_PID
);
91 auth_client_unref(auth
);
95 client_success(struct auth_client
*auth
)
97 struct user_query
*query
= get_provider_data(auth
, SELF_PID
);
99 lrb_assert(query
!= NULL
);
101 notice_client(auth
->cid
, "*** Found your hostname: %s", auth
->hostname
);
102 cancel_query(query
->query
);
106 set_provider_data(auth
, SELF_PID
, NULL
);
107 set_provider_timeout_absolute(auth
, SELF_PID
, 0);
108 provider_done(auth
, SELF_PID
);
110 auth_client_unref(auth
);
116 struct auth_client
*auth
;
117 rb_dictionary_iter iter
;
119 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
121 if(get_provider_data(auth
, SELF_PID
) != NULL
)
122 client_fail(auth
, REPORT_FAIL
);
123 /* auth is now invalid as we have no reference */
128 rdns_start(struct auth_client
*auth
)
130 struct user_query
*query
= rb_malloc(sizeof(struct user_query
));
132 auth_client_ref(auth
);
134 set_provider_data(auth
, SELF_PID
, query
);
135 set_provider_timeout_relative(auth
, SELF_PID
, rdns_timeout
);
137 query
->query
= lookup_hostname(auth
->c_ip
, dns_answer_callback
, auth
);
139 notice_client(auth
->cid
, messages
[REPORT_LOOKUP
]);
140 set_provider_running(auth
, SELF_PID
);
145 rdns_cancel(struct auth_client
*auth
)
147 struct user_query
*query
= get_provider_data(auth
, SELF_PID
);
150 client_fail(auth
, REPORT_FAIL
);
154 add_conf_dns_timeout(const char *key
, int parc
, const char **parv
)
156 int timeout
= atoi(parv
[0]);
160 warn_opers(L_CRIT
, "rDNS: DNS timeout < 0 (value: %d)", timeout
);
161 exit(EX_PROVIDER_ERROR
);
164 rdns_timeout
= timeout
;
167 struct auth_opts_handler rdns_options
[] =
169 { "rdns_timeout", 1, add_conf_dns_timeout
},
173 struct auth_provider rdns_provider
=
177 .destroy
= rdns_destroy
,
179 .cancel
= rdns_cancel
,
180 .timeout
= rdns_cancel
,
181 .opt_handlers
= rdns_options
,