1999-07-24 01:17:44 +00:00
|
|
|
/*
|
2001-01-09 22:01:04 +00:00
|
|
|
* Copyright (C) 1999-2001 Internet Software Consortium.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
1999-07-24 01:17:44 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2000-07-27 09:55:03 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
|
|
|
|
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
|
|
|
|
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
|
|
|
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1999-07-24 01:17:44 +00:00
|
|
|
*/
|
|
|
|
|
2001-12-06 04:38:51 +00:00
|
|
|
/* $Id: client.c,v 1.200 2001/12/06 04:38:45 marka Exp $ */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2001-08-08 22:54:55 +00:00
|
|
|
#include <isc/formatcheck.h>
|
2000-08-25 01:08:07 +00:00
|
|
|
#include <isc/mutex.h>
|
2001-05-25 07:39:48 +00:00
|
|
|
#include <isc/once.h>
|
2001-01-22 18:58:36 +00:00
|
|
|
#include <isc/print.h>
|
2001-05-25 07:39:48 +00:00
|
|
|
#include <isc/stdio.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2001-01-22 18:58:36 +00:00
|
|
|
#include <isc/task.h>
|
1999-07-24 01:17:44 +00:00
|
|
|
#include <isc/timer.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-10-11 17:44:18 +00:00
|
|
|
#include <dns/db.h>
|
1999-07-24 01:17:44 +00:00
|
|
|
#include <dns/dispatch.h>
|
|
|
|
#include <dns/events.h>
|
|
|
|
#include <dns/message.h>
|
1999-09-02 01:52:31 +00:00
|
|
|
#include <dns/rdata.h>
|
2000-11-15 19:15:25 +00:00
|
|
|
#include <dns/rdataclass.h>
|
1999-09-02 01:52:31 +00:00
|
|
|
#include <dns/rdatalist.h>
|
|
|
|
#include <dns/rdataset.h>
|
2000-09-12 07:48:28 +00:00
|
|
|
#include <dns/tsig.h>
|
1999-08-05 22:14:43 +00:00
|
|
|
#include <dns/view.h>
|
2000-01-27 01:00:16 +00:00
|
|
|
#include <dns/zone.h>
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
#include <named/interfacemgr.h>
|
1999-10-22 19:35:19 +00:00
|
|
|
#include <named/log.h>
|
2000-02-10 22:16:56 +00:00
|
|
|
#include <named/notify.h>
|
1999-12-16 23:11:07 +00:00
|
|
|
#include <named/server.h>
|
1999-08-20 06:03:02 +00:00
|
|
|
#include <named/update.h>
|
2000-02-10 22:16:56 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
*** Client
|
|
|
|
***/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Important note!
|
|
|
|
*
|
|
|
|
* All client state changes, other than that from idle to listening, occur
|
|
|
|
* as a result of events. This guarantees serialization and avoids the
|
|
|
|
* need for locking.
|
|
|
|
*
|
|
|
|
* If a routine is ever created that allows someone other than the client's
|
|
|
|
* task to change the client, then the client will have to be locked.
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
#define NS_CLIENT_TRACE
|
|
|
|
#ifdef NS_CLIENT_TRACE
|
2000-04-04 18:28:51 +00:00
|
|
|
#define CTRACE(m) ns_client_log(client, \
|
1999-10-22 19:35:19 +00:00
|
|
|
NS_LOGCATEGORY_CLIENT, \
|
|
|
|
NS_LOGMODULE_CLIENT, \
|
1999-11-23 20:55:02 +00:00
|
|
|
ISC_LOG_DEBUG(3), \
|
2000-04-04 18:28:51 +00:00
|
|
|
"%s", (m))
|
1999-10-22 19:35:19 +00:00
|
|
|
#define MTRACE(m) isc_log_write(ns_g_lctx, \
|
|
|
|
NS_LOGCATEGORY_GENERAL, \
|
|
|
|
NS_LOGMODULE_CLIENT, \
|
1999-11-23 20:55:02 +00:00
|
|
|
ISC_LOG_DEBUG(3), \
|
2000-04-04 18:28:51 +00:00
|
|
|
"clientmgr @%p: %s", manager, (m))
|
2000-03-29 18:57:36 +00:00
|
|
|
#else
|
|
|
|
#define CTRACE(m) ((void)(m))
|
|
|
|
#define MTRACE(m) ((void)(m))
|
1999-07-24 01:17:44 +00:00
|
|
|
#endif
|
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
#define TCP_CLIENT(c) (((c)->attributes & NS_CLIENTATTR_TCP) != 0)
|
|
|
|
|
2000-06-22 00:05:11 +00:00
|
|
|
#define TCP_BUFFER_SIZE (65535 + 2)
|
2000-11-16 00:18:13 +00:00
|
|
|
#define SEND_BUFFER_SIZE 4096
|
|
|
|
#define RECV_BUFFER_SIZE 4096
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
struct ns_clientmgr {
|
|
|
|
/* Unlocked. */
|
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t * mctx;
|
|
|
|
isc_taskmgr_t * taskmgr;
|
|
|
|
isc_timermgr_t * timermgr;
|
|
|
|
isc_mutex_t lock;
|
|
|
|
/* Locked by lock. */
|
|
|
|
isc_boolean_t exiting;
|
2000-01-28 23:35:53 +00:00
|
|
|
client_list_t active; /* Active clients */
|
2001-10-24 03:10:18 +00:00
|
|
|
client_list_t recursing; /* Recursing clients */
|
2000-02-10 22:16:56 +00:00
|
|
|
client_list_t inactive; /* To be recycled */
|
1999-07-24 01:17:44 +00:00
|
|
|
};
|
|
|
|
|
2001-06-04 19:33:39 +00:00
|
|
|
#define MANAGER_MAGIC ISC_MAGIC('N', 'S', 'C', 'm')
|
|
|
|
#define VALID_MANAGER(m) ISC_MAGIC_VALID(m, MANAGER_MAGIC)
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
/*
|
|
|
|
* Client object states. Ordering is significant: higher-numbered
|
|
|
|
* states are generally "more active", meaning that the client can
|
|
|
|
* have more dynamically allocated data, outstanding events, etc.
|
|
|
|
* In the list below, any such properties listed for state N
|
|
|
|
* also apply to any state > N.
|
|
|
|
*
|
|
|
|
* To force the client into a less active state, set client->newstate
|
|
|
|
* to that state and call exit_check(). This will cause any
|
|
|
|
* activities defined for higher-numbered states to be aborted.
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
#define NS_CLIENTSTATE_FREED 0
|
|
|
|
/*
|
|
|
|
* The client object no longer exists.
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
#define NS_CLIENTSTATE_INACTIVE 1
|
|
|
|
/*
|
2000-08-01 01:33:37 +00:00
|
|
|
* The client object exists and has a task and timer.
|
2000-02-10 22:16:56 +00:00
|
|
|
* Its "query" struct and sendbuf are initialized.
|
|
|
|
* It is on the client manager's list of inactive clients.
|
|
|
|
* It has a message and OPT, both in the reset state.
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
#define NS_CLIENTSTATE_READY 2
|
1999-07-24 01:17:44 +00:00
|
|
|
/*
|
2000-02-10 22:16:56 +00:00
|
|
|
* The client object is either a TCP or a UDP one, and
|
|
|
|
* it is associated with a network interface. It is on the
|
|
|
|
* client manager's list of active clients.
|
1999-07-24 01:17:44 +00:00
|
|
|
*
|
2000-02-10 22:16:56 +00:00
|
|
|
* If it is a TCP client object, it has a TCP listener socket
|
2001-01-27 02:08:07 +00:00
|
|
|
* and an outstanding TCP listen request.
|
1999-07-24 01:17:44 +00:00
|
|
|
*
|
2001-01-27 02:08:07 +00:00
|
|
|
* If it is a UDP client object, it has a UDP listener socket
|
|
|
|
* and an outstanding UDP receive request.
|
1999-07-24 01:17:44 +00:00
|
|
|
*/
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
#define NS_CLIENTSTATE_READING 3
|
|
|
|
/*
|
|
|
|
* The client object is a TCP client object that has received
|
2000-08-01 01:33:37 +00:00
|
|
|
* a connection. It has a tcpsocket, tcpmsg, TCP quota, and an
|
2000-02-10 22:16:56 +00:00
|
|
|
* outstanding TCP read request. This state is not used for
|
|
|
|
* UDP client objects.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define NS_CLIENTSTATE_WORKING 4
|
|
|
|
/*
|
|
|
|
* The client object has received a request and is working
|
2001-10-12 23:54:03 +00:00
|
|
|
* on it. It has a view, and it may have any of a non-reset OPT,
|
2001-01-27 02:08:07 +00:00
|
|
|
* recursion quota, and an outstanding write request.
|
2000-08-01 01:33:37 +00:00
|
|
|
*/
|
2000-02-10 22:16:56 +00:00
|
|
|
|
|
|
|
#define NS_CLIENTSTATE_MAX 9
|
|
|
|
/*
|
|
|
|
* Sentinel value used to indicate "no state". When client->newstate
|
|
|
|
* has this value, we are not attempting to exit the current state.
|
|
|
|
* Must be greater than any valid state.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
static void client_read(ns_client_t *client);
|
|
|
|
static void client_accept(ns_client_t *client);
|
2001-01-27 02:08:07 +00:00
|
|
|
static void client_udprecv(ns_client_t *client);
|
2000-02-10 22:16:56 +00:00
|
|
|
static void clientmgr_destroy(ns_clientmgr_t *manager);
|
|
|
|
static isc_boolean_t exit_check(ns_client_t *client);
|
|
|
|
static void ns_client_endrequest(ns_client_t *client);
|
2000-03-06 17:35:13 +00:00
|
|
|
static void ns_client_checkactive(ns_client_t *client);
|
2000-07-26 17:39:12 +00:00
|
|
|
static void client_start(isc_task_t *task, isc_event_t *event);
|
|
|
|
static void client_request(isc_task_t *task, isc_event_t *event);
|
2001-05-25 07:39:48 +00:00
|
|
|
static void ns_client_dumpmessage(ns_client_t *client, const char *reason);
|
2000-01-15 00:36:26 +00:00
|
|
|
|
2001-10-24 03:10:18 +00:00
|
|
|
void
|
|
|
|
ns_client_recursing(ns_client_t *client, isc_boolean_t killoldest) {
|
|
|
|
ns_client_t *oldest;
|
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
|
|
|
|
LOCK(&client->manager->lock);
|
|
|
|
if (killoldest) {
|
|
|
|
oldest = ISC_LIST_HEAD(client->manager->recursing);
|
|
|
|
if (oldest != NULL) {
|
|
|
|
ns_query_cancel(oldest);
|
|
|
|
ISC_LIST_UNLINK(*oldest->list, oldest, link);
|
|
|
|
ISC_LIST_APPEND(client->manager->active, oldest, link);
|
|
|
|
oldest->list = &client->manager->active;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ISC_LIST_UNLINK(*client->list, client, link);
|
|
|
|
ISC_LIST_APPEND(client->manager->recursing, client, link);
|
|
|
|
client->list = &client->manager->recursing;
|
|
|
|
UNLOCK(&client->manager->lock);
|
|
|
|
}
|
|
|
|
|
2001-01-29 19:49:52 +00:00
|
|
|
void
|
|
|
|
ns_client_settimeout(ns_client_t *client, unsigned int seconds) {
|
2000-02-14 23:56:47 +00:00
|
|
|
isc_result_t result;
|
|
|
|
isc_interval_t interval;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-14 23:56:47 +00:00
|
|
|
isc_interval_set(&interval, seconds, 0);
|
|
|
|
result = isc_timer_reset(client->timer, isc_timertype_once, NULL,
|
|
|
|
&interval, ISC_FALSE);
|
2001-01-29 19:49:52 +00:00
|
|
|
client->timerset = ISC_TRUE;
|
2000-02-14 23:56:47 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
2000-02-14 23:56:47 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
|
2000-07-13 00:21:27 +00:00
|
|
|
"setting timeout: %s",
|
2000-02-14 23:56:47 +00:00
|
|
|
isc_result_totext(result));
|
|
|
|
/* Continue anyway. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
/*
|
|
|
|
* Check for a deactivation or shutdown request and take appropriate
|
|
|
|
* action. Returns ISC_TRUE if either is in progress; in this case
|
|
|
|
* the caller must no longer use the client object as it may have been
|
|
|
|
* freed.
|
|
|
|
*/
|
|
|
|
static isc_boolean_t
|
|
|
|
exit_check(ns_client_t *client) {
|
2001-11-14 22:00:22 +00:00
|
|
|
ns_clientmgr_t *locked_manager = NULL;
|
|
|
|
ns_clientmgr_t *destroy_manager = NULL;
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
|
|
|
|
if (client->state <= client->newstate)
|
|
|
|
return (ISC_FALSE); /* Business as usual. */
|
|
|
|
|
2000-02-14 23:56:47 +00:00
|
|
|
INSIST(client->newstate < NS_CLIENTSTATE_WORKING);
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
/*
|
|
|
|
* We need to detach from the view early when shutting down
|
|
|
|
* the server to break the following vicious circle:
|
|
|
|
*
|
|
|
|
* - The resolver will not shut down until the view refcount is zero
|
|
|
|
* - The view refcount does not go to zero until all clients detach
|
2000-02-11 20:56:19 +00:00
|
|
|
* - The client does not detach from the view until references is zero
|
|
|
|
* - references does not go to zero until the resolver has shut down
|
2000-02-10 22:16:56 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
if (client->newstate == NS_CLIENTSTATE_FREED && client->view != NULL)
|
2000-08-01 01:33:37 +00:00
|
|
|
dns_view_detach(&client->view);
|
2000-02-10 22:16:56 +00:00
|
|
|
|
|
|
|
if (client->state == NS_CLIENTSTATE_WORKING) {
|
|
|
|
INSIST(client->newstate <= NS_CLIENTSTATE_READING);
|
|
|
|
/*
|
|
|
|
* We are trying to abort request processing.
|
|
|
|
*/
|
|
|
|
if (client->nsends > 0) {
|
|
|
|
isc_socket_t *socket;
|
|
|
|
if (TCP_CLIENT(client))
|
|
|
|
socket = client->tcpsocket;
|
|
|
|
else
|
2001-01-27 02:08:07 +00:00
|
|
|
socket = client->udpsocket;
|
2000-02-10 22:16:56 +00:00
|
|
|
isc_socket_cancel(socket, client->task,
|
|
|
|
ISC_SOCKCANCEL_SEND);
|
|
|
|
}
|
2000-02-14 23:56:47 +00:00
|
|
|
|
2001-01-27 02:08:07 +00:00
|
|
|
if (! (client->nsends == 0 && client->nrecvs == 0 &&
|
|
|
|
client->references == 0))
|
|
|
|
{
|
2000-02-10 22:16:56 +00:00
|
|
|
/*
|
|
|
|
* Still waiting for I/O cancel completion.
|
|
|
|
* or lingering references.
|
|
|
|
*/
|
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* I/O cancel is complete. Burn down all state
|
|
|
|
* related to the current request.
|
|
|
|
*/
|
|
|
|
ns_client_endrequest(client);
|
|
|
|
|
|
|
|
client->state = NS_CLIENTSTATE_READING;
|
2000-11-27 23:54:12 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
2000-02-10 22:16:56 +00:00
|
|
|
if (NS_CLIENTSTATE_READING == client->newstate) {
|
|
|
|
client_read(client);
|
|
|
|
client->newstate = NS_CLIENTSTATE_MAX;
|
|
|
|
return (ISC_TRUE); /* We're done. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->state == NS_CLIENTSTATE_READING) {
|
|
|
|
/*
|
|
|
|
* We are trying to abort the current TCP connection,
|
|
|
|
* if any.
|
|
|
|
*/
|
2000-11-27 23:54:12 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
2000-02-10 22:16:56 +00:00
|
|
|
INSIST(client->newstate <= NS_CLIENTSTATE_READY);
|
|
|
|
if (client->nreads > 0)
|
|
|
|
dns_tcpmsg_cancelread(&client->tcpmsg);
|
|
|
|
if (! client->nreads == 0) {
|
|
|
|
/* Still waiting for read cancel completion. */
|
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->tcpmsg_valid) {
|
|
|
|
dns_tcpmsg_invalidate(&client->tcpmsg);
|
|
|
|
client->tcpmsg_valid = ISC_FALSE;
|
|
|
|
}
|
2000-04-20 17:07:46 +00:00
|
|
|
if (client->tcpsocket != NULL) {
|
|
|
|
CTRACE("closetcp");
|
2000-02-10 22:16:56 +00:00
|
|
|
isc_socket_detach(&client->tcpsocket);
|
2000-04-20 17:07:46 +00:00
|
|
|
}
|
2000-02-10 22:16:56 +00:00
|
|
|
|
|
|
|
if (client->tcpquota != NULL)
|
|
|
|
isc_quota_detach(&client->tcpquota);
|
2000-02-14 23:56:47 +00:00
|
|
|
|
2001-01-29 19:49:52 +00:00
|
|
|
if (client->timerset) {
|
2001-11-14 19:11:06 +00:00
|
|
|
(void)isc_timer_reset(client->timer,
|
|
|
|
isc_timertype_inactive,
|
|
|
|
NULL, NULL, ISC_TRUE);
|
2001-01-29 19:49:52 +00:00
|
|
|
client->timerset = ISC_FALSE;
|
|
|
|
}
|
2000-03-06 17:35:13 +00:00
|
|
|
|
2000-04-04 18:28:51 +00:00
|
|
|
client->peeraddr_valid = ISC_FALSE;
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
client->state = NS_CLIENTSTATE_READY;
|
2000-11-27 23:54:12 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
2000-03-06 17:35:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now the client is ready to accept a new TCP connection
|
|
|
|
* or UDP request, but we may have enough clients doing
|
|
|
|
* that already. Check whether this client needs to remain
|
|
|
|
* active and force it to go inactive if not.
|
|
|
|
*/
|
|
|
|
ns_client_checkactive(client);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
if (NS_CLIENTSTATE_READY == client->newstate) {
|
|
|
|
if (TCP_CLIENT(client)) {
|
|
|
|
client_accept(client);
|
2001-01-27 02:08:07 +00:00
|
|
|
} else
|
|
|
|
client_udprecv(client);
|
2000-02-10 22:16:56 +00:00
|
|
|
client->newstate = NS_CLIENTSTATE_MAX;
|
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->state == NS_CLIENTSTATE_READY) {
|
2000-08-01 01:33:37 +00:00
|
|
|
INSIST(client->newstate <= NS_CLIENTSTATE_INACTIVE);
|
2000-02-10 22:16:56 +00:00
|
|
|
/*
|
|
|
|
* We are trying to enter the inactive state.
|
|
|
|
*/
|
|
|
|
if (client->naccepts > 0)
|
|
|
|
isc_socket_cancel(client->tcplistener, client->task,
|
|
|
|
ISC_SOCKCANCEL_ACCEPT);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
if (! (client->naccepts == 0)) {
|
|
|
|
/* Still waiting for accept cancel completion. */
|
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
/* Accept cancel is complete. */
|
2001-01-27 02:08:07 +00:00
|
|
|
|
|
|
|
if (client->nrecvs > 0)
|
|
|
|
isc_socket_cancel(client->udpsocket, client->task,
|
|
|
|
ISC_SOCKCANCEL_RECV);
|
|
|
|
if (! (client->nrecvs == 0)) {
|
|
|
|
/* Still waiting for recv cancel completion. */
|
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
/* Recv cancel is complete. */
|
|
|
|
|
2001-11-14 22:00:22 +00:00
|
|
|
if (client->nctls > 0) {
|
|
|
|
/* Still waiting for control event to be delivered */
|
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deactivate the client. */
|
|
|
|
if (client->interface)
|
|
|
|
ns_interface_detach(&client->interface);
|
|
|
|
|
|
|
|
INSIST(client->naccepts == 0);
|
|
|
|
INSIST(client->recursionquota == NULL);
|
|
|
|
if (client->tcplistener != NULL)
|
|
|
|
isc_socket_detach(&client->tcplistener);
|
|
|
|
|
|
|
|
if (client->udpsocket != NULL)
|
|
|
|
isc_socket_detach(&client->udpsocket);
|
|
|
|
|
|
|
|
if (client->dispatch != NULL)
|
|
|
|
dns_dispatch_detach(&client->dispatch);
|
|
|
|
|
|
|
|
client->attributes = 0;
|
|
|
|
client->mortal = ISC_FALSE;
|
|
|
|
|
|
|
|
LOCK(&client->manager->lock);
|
|
|
|
/*
|
|
|
|
* Put the client on the inactive list. If we are aiming for
|
|
|
|
* the "freed" state, it will be removed from the inactive
|
|
|
|
* list shortly, and we need to keep the manager locked until
|
|
|
|
* that has been done, lest the manager decide to reactivate
|
|
|
|
* the dying client inbetween.
|
|
|
|
*/
|
|
|
|
locked_manager = client->manager;
|
|
|
|
ISC_LIST_UNLINK(*client->list, client, link);
|
|
|
|
ISC_LIST_APPEND(client->manager->inactive, client, link);
|
|
|
|
client->list = &client->manager->inactive;
|
2000-02-10 22:16:56 +00:00
|
|
|
client->state = NS_CLIENTSTATE_INACTIVE;
|
2000-11-27 23:54:12 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
2001-11-14 22:00:22 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
if (client->state == client->newstate) {
|
|
|
|
client->newstate = NS_CLIENTSTATE_MAX;
|
2001-11-14 22:00:22 +00:00
|
|
|
goto unlock;
|
2000-02-10 22:16:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->state == NS_CLIENTSTATE_INACTIVE) {
|
|
|
|
INSIST(client->newstate == NS_CLIENTSTATE_FREED);
|
|
|
|
/*
|
|
|
|
* We are trying to free the client.
|
2001-11-14 22:00:22 +00:00
|
|
|
*
|
|
|
|
* When "shuttingdown" is true, either the task has received
|
|
|
|
* its shutdown event or no shutdown event has ever been
|
|
|
|
* set up. Thus, we have no outstanding shutdown
|
|
|
|
* event at this point.
|
2000-02-10 22:16:56 +00:00
|
|
|
*/
|
2001-11-14 22:00:22 +00:00
|
|
|
REQUIRE(client->state == NS_CLIENTSTATE_INACTIVE);
|
|
|
|
|
|
|
|
INSIST(client->recursionquota == NULL);
|
|
|
|
|
|
|
|
ns_query_free(client);
|
|
|
|
isc_mem_put(client->mctx, client->recvbuf, RECV_BUFFER_SIZE);
|
|
|
|
isc_event_free((isc_event_t **)&client->sendevent);
|
|
|
|
isc_event_free((isc_event_t **)&client->recvevent);
|
|
|
|
isc_timer_detach(&client->timer);
|
|
|
|
|
|
|
|
if (client->tcpbuf != NULL)
|
|
|
|
isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
|
|
|
|
if (client->opt != NULL) {
|
|
|
|
INSIST(dns_rdataset_isassociated(client->opt));
|
|
|
|
dns_rdataset_disassociate(client->opt);
|
|
|
|
dns_message_puttemprdataset(client->message, &client->opt);
|
|
|
|
}
|
|
|
|
dns_message_destroy(&client->message);
|
|
|
|
if (client->manager != NULL) {
|
|
|
|
ns_clientmgr_t *manager = client->manager;
|
|
|
|
if (locked_manager == NULL) {
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
locked_manager = manager;
|
|
|
|
}
|
|
|
|
ISC_LIST_UNLINK(*client->list, client, link);
|
|
|
|
client->list = NULL;
|
|
|
|
if (manager->exiting &&
|
|
|
|
ISC_LIST_EMPTY(manager->active) &&
|
|
|
|
ISC_LIST_EMPTY(manager->inactive) &&
|
|
|
|
ISC_LIST_EMPTY(manager->recursing))
|
|
|
|
destroy_manager = manager;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Detaching the task must be done after unlinking from
|
|
|
|
* the manager's lists because the manager accesses
|
|
|
|
* client->task.
|
|
|
|
*/
|
|
|
|
if (client->task != NULL)
|
|
|
|
isc_task_detach(&client->task);
|
|
|
|
|
|
|
|
CTRACE("free");
|
|
|
|
client->magic = 0;
|
|
|
|
isc_mem_put(client->mctx, client, sizeof(*client));
|
|
|
|
|
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock:
|
|
|
|
if (locked_manager != NULL) {
|
|
|
|
UNLOCK(&locked_manager->lock);
|
|
|
|
locked_manager = NULL;
|
2000-02-10 22:16:56 +00:00
|
|
|
}
|
|
|
|
|
2001-11-14 22:00:22 +00:00
|
|
|
/*
|
|
|
|
* Only now is it safe to destroy the client manager (if needed),
|
|
|
|
* because we have accessed its lock for the last time.
|
|
|
|
*/
|
|
|
|
if (destroy_manager != NULL)
|
|
|
|
clientmgr_destroy(destroy_manager);
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
|
2000-07-26 17:39:12 +00:00
|
|
|
/*
|
|
|
|
* The client's task has received the client's control event
|
|
|
|
* as part of the startup process.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
client_start(isc_task_t *task, isc_event_t *event) {
|
|
|
|
ns_client_t *client = (ns_client_t *) event->ev_arg;
|
|
|
|
|
|
|
|
INSIST(task == client->task);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-07-26 17:39:12 +00:00
|
|
|
UNUSED(task);
|
|
|
|
|
2001-11-14 22:00:22 +00:00
|
|
|
INSIST(client->nctls == 1);
|
|
|
|
client->nctls--;
|
|
|
|
|
|
|
|
if (exit_check(client))
|
|
|
|
return;
|
|
|
|
|
2000-07-26 17:39:12 +00:00
|
|
|
if (TCP_CLIENT(client)) {
|
|
|
|
client_accept(client);
|
|
|
|
} else {
|
2001-01-27 02:08:07 +00:00
|
|
|
client_udprecv(client);
|
2000-07-26 17:39:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
/*
|
|
|
|
* The client's task has received a shutdown event.
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
static void
|
|
|
|
client_shutdown(isc_task_t *task, isc_event_t *event) {
|
|
|
|
ns_client_t *client;
|
|
|
|
|
|
|
|
REQUIRE(event != NULL);
|
2000-04-17 19:22:44 +00:00
|
|
|
REQUIRE(event->ev_type == ISC_TASKEVENT_SHUTDOWN);
|
|
|
|
client = event->ev_arg;
|
1999-07-24 01:17:44 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(task == client->task);
|
|
|
|
|
2000-05-02 01:16:21 +00:00
|
|
|
UNUSED(task);
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
CTRACE("shutdown");
|
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
|
2000-02-14 23:56:47 +00:00
|
|
|
if (client->shutdown != NULL) {
|
|
|
|
(client->shutdown)(client->shutdown_arg, ISC_R_SHUTTINGDOWN);
|
|
|
|
client->shutdown = NULL;
|
|
|
|
client->shutdown_arg = NULL;
|
|
|
|
}
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
client->newstate = NS_CLIENTSTATE_FREED;
|
2000-04-17 19:22:44 +00:00
|
|
|
(void)exit_check(client);
|
2000-02-10 22:16:56 +00:00
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
static void
|
|
|
|
ns_client_endrequest(ns_client_t *client) {
|
2000-01-28 23:35:53 +00:00
|
|
|
INSIST(client->naccepts == 0);
|
|
|
|
INSIST(client->nreads == 0);
|
|
|
|
INSIST(client->nsends == 0);
|
2001-01-27 02:08:07 +00:00
|
|
|
INSIST(client->nrecvs == 0);
|
2000-02-10 22:16:56 +00:00
|
|
|
INSIST(client->state == NS_CLIENTSTATE_WORKING);
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-05-02 01:16:21 +00:00
|
|
|
CTRACE("endrequest");
|
|
|
|
|
1999-08-03 01:21:00 +00:00
|
|
|
if (client->next != NULL) {
|
2000-02-10 22:16:56 +00:00
|
|
|
(client->next)(client);
|
1999-08-03 01:21:00 +00:00
|
|
|
client->next = NULL;
|
|
|
|
}
|
|
|
|
|
1999-08-05 22:14:43 +00:00
|
|
|
if (client->view != NULL)
|
|
|
|
dns_view_detach(&client->view);
|
1999-11-24 21:05:45 +00:00
|
|
|
if (client->opt != NULL) {
|
|
|
|
INSIST(dns_rdataset_isassociated(client->opt));
|
|
|
|
dns_rdataset_disassociate(client->opt);
|
|
|
|
dns_message_puttemprdataset(client->message, &client->opt);
|
|
|
|
}
|
2000-01-07 19:20:25 +00:00
|
|
|
|
1999-10-07 19:43:18 +00:00
|
|
|
client->udpsize = 512;
|
2000-11-13 21:34:03 +00:00
|
|
|
client->extflags = 0;
|
1999-07-24 01:17:44 +00:00
|
|
|
dns_message_reset(client->message, DNS_MESSAGE_INTENTPARSE);
|
2000-01-15 00:36:26 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
if (client->recursionquota != NULL)
|
|
|
|
isc_quota_detach(&client->recursionquota);
|
2000-07-17 23:19:14 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-01 01:33:37 +00:00
|
|
|
* Clear all client attributes that are specific to
|
2000-07-17 23:19:14 +00:00
|
|
|
* the request; that's all except the TCP flag.
|
|
|
|
*/
|
|
|
|
client->attributes &= NS_CLIENTATTR_TCP;
|
2000-03-06 17:35:13 +00:00
|
|
|
}
|
2000-01-15 00:36:26 +00:00
|
|
|
|
2000-03-06 17:35:13 +00:00
|
|
|
static void
|
|
|
|
ns_client_checkactive(ns_client_t *client) {
|
2000-01-11 21:18:22 +00:00
|
|
|
if (client->mortal) {
|
|
|
|
/*
|
2000-02-10 22:16:56 +00:00
|
|
|
* This client object should normally go inactive
|
2000-08-01 01:33:37 +00:00
|
|
|
* at this point, but if we have fewer active client
|
2000-02-10 22:16:56 +00:00
|
|
|
* objects than desired due to earlier quota exhaustion,
|
|
|
|
* keep it active to make up for the shortage.
|
2000-01-11 21:18:22 +00:00
|
|
|
*/
|
|
|
|
isc_boolean_t need_another_client = ISC_FALSE;
|
|
|
|
if (TCP_CLIENT(client)) {
|
|
|
|
LOCK(&client->interface->lock);
|
|
|
|
if (client->interface->ntcpcurrent <
|
2000-08-01 01:33:37 +00:00
|
|
|
client->interface->ntcptarget)
|
2000-01-11 21:18:22 +00:00
|
|
|
need_another_client = ISC_TRUE;
|
|
|
|
UNLOCK(&client->interface->lock);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The UDP client quota is enforced by making
|
|
|
|
* requests fail rather than by not listening
|
|
|
|
* for new ones. Therefore, there is always a
|
|
|
|
* full set of UDP clients listening.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
if (! need_another_client) {
|
2000-01-28 23:35:53 +00:00
|
|
|
/*
|
|
|
|
* We don't need this client object. Recycle it.
|
|
|
|
*/
|
2000-02-10 22:16:56 +00:00
|
|
|
if (client->newstate >= NS_CLIENTSTATE_INACTIVE)
|
|
|
|
client->newstate = NS_CLIENTSTATE_INACTIVE;
|
2000-01-11 21:18:22 +00:00
|
|
|
}
|
2000-01-07 19:20:25 +00:00
|
|
|
}
|
2000-02-10 22:16:56 +00:00
|
|
|
}
|
2000-01-15 00:36:26 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
void
|
|
|
|
ns_client_next(ns_client_t *client, isc_result_t result) {
|
|
|
|
int newstate;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(client->state == NS_CLIENTSTATE_WORKING);
|
2000-05-02 01:16:21 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
CTRACE("next");
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-05-22 16:03:31 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
2000-02-10 22:16:56 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
|
|
|
"request failed: %s", isc_result_totext(result));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An error processing a TCP request may have left
|
|
|
|
* the connection out of sync. To be safe, we always
|
|
|
|
* sever the connection when result != ISC_R_SUCCESS.
|
|
|
|
*/
|
|
|
|
if (result == ISC_R_SUCCESS && TCP_CLIENT(client))
|
|
|
|
newstate = NS_CLIENTSTATE_READING;
|
|
|
|
else
|
|
|
|
newstate = NS_CLIENTSTATE_READY;
|
|
|
|
|
|
|
|
if (client->newstate > newstate)
|
|
|
|
client->newstate = newstate;
|
2001-11-14 19:11:06 +00:00
|
|
|
(void)exit_check(client);
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
static void
|
|
|
|
client_senddone(isc_task_t *task, isc_event_t *event) {
|
|
|
|
ns_client_t *client;
|
2000-01-28 23:35:53 +00:00
|
|
|
isc_socketevent_t *sevent = (isc_socketevent_t *) event;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
REQUIRE(sevent != NULL);
|
2000-04-17 19:22:44 +00:00
|
|
|
REQUIRE(sevent->ev_type == ISC_SOCKEVENT_SENDDONE);
|
|
|
|
client = sevent->ev_arg;
|
1999-07-24 01:17:44 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(task == client->task);
|
2001-02-12 21:45:37 +00:00
|
|
|
REQUIRE(sevent == client->sendevent);
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-07-26 17:39:12 +00:00
|
|
|
UNUSED(task);
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
CTRACE("senddone");
|
|
|
|
|
2000-05-22 16:03:31 +00:00
|
|
|
if (sevent->result != ISC_R_SUCCESS)
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
|
|
|
|
"error sending response: %s",
|
|
|
|
isc_result_totext(sevent->result));
|
|
|
|
|
1999-07-24 03:02:57 +00:00
|
|
|
INSIST(client->nsends > 0);
|
1999-07-24 01:17:44 +00:00
|
|
|
client->nsends--;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-06-22 00:05:11 +00:00
|
|
|
if (client->tcpbuf != NULL) {
|
2000-06-22 02:21:06 +00:00
|
|
|
INSIST(TCP_CLIENT(client));
|
2000-06-22 00:05:11 +00:00
|
|
|
isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
|
|
|
|
client->tcpbuf = NULL;
|
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
if (exit_check(client))
|
2000-01-25 19:36:13 +00:00
|
|
|
return;
|
|
|
|
|
2000-01-28 23:35:53 +00:00
|
|
|
ns_client_next(client, ISC_R_SUCCESS);
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
2001-03-06 04:18:42 +00:00
|
|
|
/*
|
|
|
|
* We only want to fail with ISC_R_NOSPACE when called from
|
|
|
|
* ns_client_sendraw() and not when called from ns_client_send(),
|
|
|
|
* tcpbuffer is NULL when called from ns_client_sendraw() and
|
|
|
|
* length != 0. tcpbuffer != NULL when called from ns_client_send()
|
|
|
|
* and length == 0.
|
|
|
|
*/
|
|
|
|
|
2000-09-13 03:15:01 +00:00
|
|
|
static isc_result_t
|
|
|
|
client_allocsendbuf(ns_client_t *client, isc_buffer_t *buffer,
|
|
|
|
isc_buffer_t *tcpbuffer, isc_uint32_t length,
|
2001-03-06 01:24:40 +00:00
|
|
|
unsigned char *sendbuf, unsigned char **datap)
|
2000-09-13 03:15:01 +00:00
|
|
|
{
|
2000-09-13 01:30:34 +00:00
|
|
|
unsigned char *data;
|
2000-09-13 03:15:01 +00:00
|
|
|
isc_uint32_t bufsize;
|
|
|
|
isc_result_t result;
|
2000-09-13 01:30:34 +00:00
|
|
|
|
2000-09-13 03:15:01 +00:00
|
|
|
INSIST(datap != NULL);
|
2001-03-06 04:18:42 +00:00
|
|
|
INSIST((tcpbuffer == NULL && length != 0) ||
|
|
|
|
(tcpbuffer != NULL && length == 0));
|
2000-09-13 01:30:34 +00:00
|
|
|
|
|
|
|
if (TCP_CLIENT(client)) {
|
|
|
|
INSIST(client->tcpbuf == NULL);
|
2001-03-06 04:18:42 +00:00
|
|
|
if (length + 2 > TCP_BUFFER_SIZE) {
|
2000-09-13 01:30:34 +00:00
|
|
|
result = ISC_R_NOSPACE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
client->tcpbuf = isc_mem_get(client->mctx, TCP_BUFFER_SIZE);
|
|
|
|
if (client->tcpbuf == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
data = client->tcpbuf;
|
2000-09-13 03:15:01 +00:00
|
|
|
if (tcpbuffer != NULL) {
|
|
|
|
isc_buffer_init(tcpbuffer, data, TCP_BUFFER_SIZE);
|
|
|
|
isc_buffer_init(buffer, data + 2, TCP_BUFFER_SIZE - 2);
|
|
|
|
} else {
|
|
|
|
isc_buffer_init(buffer, data, TCP_BUFFER_SIZE);
|
2001-03-12 22:27:15 +00:00
|
|
|
INSIST(length <= 0xffff);
|
|
|
|
isc_buffer_putuint16(buffer, (isc_uint16_t)length);
|
2000-09-13 03:15:01 +00:00
|
|
|
}
|
2000-09-13 01:30:34 +00:00
|
|
|
} else {
|
2001-03-06 01:24:40 +00:00
|
|
|
data = sendbuf;
|
2000-09-13 01:30:34 +00:00
|
|
|
if (client->udpsize < SEND_BUFFER_SIZE)
|
|
|
|
bufsize = client->udpsize;
|
|
|
|
else
|
|
|
|
bufsize = SEND_BUFFER_SIZE;
|
2001-03-06 04:18:42 +00:00
|
|
|
if (length > bufsize) {
|
2000-09-13 01:30:34 +00:00
|
|
|
result = ISC_R_NOSPACE;
|
|
|
|
goto done;
|
|
|
|
}
|
2000-09-13 03:15:01 +00:00
|
|
|
isc_buffer_init(buffer, data, bufsize);
|
2000-09-13 01:30:34 +00:00
|
|
|
}
|
2000-09-13 03:15:01 +00:00
|
|
|
*datap = data;
|
|
|
|
result = ISC_R_SUCCESS;
|
2000-09-13 01:30:34 +00:00
|
|
|
|
2000-09-13 03:15:01 +00:00
|
|
|
done:
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
client_sendpkg(ns_client_t *client, isc_buffer_t *buffer) {
|
|
|
|
struct in6_pktinfo *pktinfo;
|
|
|
|
isc_result_t result;
|
|
|
|
isc_region_t r;
|
|
|
|
isc_sockaddr_t *address;
|
|
|
|
isc_socket_t *socket;
|
2000-11-03 02:45:55 +00:00
|
|
|
isc_netaddr_t netaddr;
|
|
|
|
int match;
|
2001-03-06 01:24:40 +00:00
|
|
|
unsigned int sockflags = ISC_SOCKFLAG_IMMEDIATE;
|
2000-09-13 01:30:34 +00:00
|
|
|
|
|
|
|
if (TCP_CLIENT(client)) {
|
|
|
|
socket = client->tcpsocket;
|
|
|
|
address = NULL;
|
|
|
|
} else {
|
2001-01-27 02:08:07 +00:00
|
|
|
socket = client->udpsocket;
|
|
|
|
address = &client->peeraddr;
|
2000-11-03 02:45:55 +00:00
|
|
|
|
|
|
|
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
|
|
|
|
if (ns_g_server->blackholeacl != NULL &&
|
|
|
|
dns_acl_match(&netaddr, NULL,
|
|
|
|
ns_g_server->blackholeacl,
|
2001-03-26 21:33:07 +00:00
|
|
|
&ns_g_server->aclenv,
|
|
|
|
&match, NULL) == ISC_R_SUCCESS &&
|
2000-11-03 02:45:55 +00:00
|
|
|
match > 0)
|
|
|
|
return (DNS_R_BLACKHOLED);
|
2001-03-06 01:24:40 +00:00
|
|
|
sockflags |= ISC_SOCKFLAG_NORETRY;
|
2000-09-13 01:30:34 +00:00
|
|
|
}
|
2000-09-13 03:15:01 +00:00
|
|
|
|
2001-10-09 02:39:03 +00:00
|
|
|
if ((client->attributes & NS_CLIENTATTR_PKTINFO) != 0 &&
|
2001-10-13 00:44:24 +00:00
|
|
|
(client->attributes & NS_CLIENTATTR_MULTICAST) == 0)
|
2000-09-13 01:30:34 +00:00
|
|
|
pktinfo = &client->pktinfo;
|
|
|
|
else
|
|
|
|
pktinfo = NULL;
|
2000-09-13 03:15:01 +00:00
|
|
|
|
|
|
|
isc_buffer_usedregion(buffer, &r);
|
|
|
|
|
|
|
|
CTRACE("sendto");
|
2000-11-03 02:45:55 +00:00
|
|
|
|
2001-02-12 21:45:37 +00:00
|
|
|
result = isc_socket_sendto2(socket, &r, client->task,
|
|
|
|
address, pktinfo,
|
2001-03-06 01:24:40 +00:00
|
|
|
client->sendevent, sockflags);
|
2001-02-12 21:45:37 +00:00
|
|
|
if (result == ISC_R_SUCCESS || result == ISC_R_INPROGRESS) {
|
2000-09-13 01:30:34 +00:00
|
|
|
client->nsends++;
|
2001-02-12 21:45:37 +00:00
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
client_senddone(client->task,
|
|
|
|
(isc_event_t *)client->sendevent);
|
2001-02-14 01:46:59 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
2000-09-13 01:30:34 +00:00
|
|
|
}
|
2000-09-13 03:15:01 +00:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_client_sendraw(ns_client_t *client, dns_message_t *message) {
|
|
|
|
isc_result_t result;
|
|
|
|
unsigned char *data;
|
|
|
|
isc_buffer_t buffer;
|
|
|
|
isc_region_t r;
|
|
|
|
isc_region_t *mr;
|
2001-03-06 01:24:40 +00:00
|
|
|
unsigned char sendbuf[SEND_BUFFER_SIZE];
|
2000-09-13 03:15:01 +00:00
|
|
|
|
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
|
|
|
|
CTRACE("sendraw");
|
|
|
|
|
|
|
|
mr = dns_message_getrawmessage(message);
|
|
|
|
if (mr == NULL) {
|
|
|
|
result = ISC_R_UNEXPECTEDEND;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2001-03-06 01:24:40 +00:00
|
|
|
result = client_allocsendbuf(client, &buffer, NULL, mr->length,
|
|
|
|
sendbuf, &data);
|
2000-09-13 03:15:01 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy message to buffer and fixup id.
|
|
|
|
*/
|
|
|
|
isc_buffer_availableregion(&buffer, &r);
|
|
|
|
result = isc_buffer_copyregion(&buffer, mr);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
r.base[0] = (client->message->id >> 8) & 0xff;
|
|
|
|
r.base[1] = client->message->id & 0xff;
|
|
|
|
|
|
|
|
result = client_sendpkg(client, &buffer);
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
return;
|
2000-09-13 01:30:34 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
if (client->tcpbuf != NULL) {
|
|
|
|
isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
|
|
|
|
client->tcpbuf = NULL;
|
|
|
|
}
|
|
|
|
ns_client_next(client, result);
|
|
|
|
}
|
|
|
|
|
1999-07-28 02:20:36 +00:00
|
|
|
void
|
|
|
|
ns_client_send(ns_client_t *client) {
|
1999-07-24 01:17:44 +00:00
|
|
|
isc_result_t result;
|
|
|
|
unsigned char *data;
|
|
|
|
isc_buffer_t buffer;
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_buffer_t tcpbuffer;
|
1999-07-24 01:17:44 +00:00
|
|
|
isc_region_t r;
|
2001-03-05 21:15:47 +00:00
|
|
|
dns_compress_t cctx;
|
|
|
|
isc_boolean_t cleanup_cctx = ISC_FALSE;
|
2001-03-06 01:24:40 +00:00
|
|
|
unsigned char sendbuf[SEND_BUFFER_SIZE];
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
|
|
|
|
CTRACE("send");
|
|
|
|
|
1999-12-10 23:58:04 +00:00
|
|
|
if ((client->attributes & NS_CLIENTATTR_RA) != 0)
|
|
|
|
client->message->flags |= DNS_MESSAGEFLAG_RA;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-07-28 02:20:36 +00:00
|
|
|
/*
|
2000-10-13 23:38:45 +00:00
|
|
|
* XXXRTH The following doesn't deal with TCP buffer resizing.
|
1999-07-28 02:20:36 +00:00
|
|
|
*/
|
2001-03-06 01:24:40 +00:00
|
|
|
result = client_allocsendbuf(client, &buffer, &tcpbuffer, 0,
|
|
|
|
sendbuf, &data);
|
2000-09-13 03:15:01 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
1999-08-05 01:51:32 +00:00
|
|
|
|
2001-03-05 21:15:47 +00:00
|
|
|
result = dns_compress_init(&cctx, -1, client->mctx);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
cleanup_cctx = ISC_TRUE;
|
|
|
|
|
|
|
|
result = dns_message_renderbegin(client->message, &cctx, &buffer);
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
1999-09-02 01:52:31 +00:00
|
|
|
if (client->opt != NULL) {
|
|
|
|
result = dns_message_setopt(client->message, client->opt);
|
1999-11-24 21:05:45 +00:00
|
|
|
/*
|
|
|
|
* XXXRTH dns_message_setopt() should probably do this...
|
|
|
|
*/
|
|
|
|
client->opt = NULL;
|
2001-02-14 02:51:12 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
1999-09-02 01:52:31 +00:00
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
result = dns_message_rendersection(client->message,
|
1999-12-22 03:22:59 +00:00
|
|
|
DNS_SECTION_QUESTION, 0);
|
2001-01-08 20:36:14 +00:00
|
|
|
if (result == ISC_R_NOSPACE) {
|
|
|
|
client->message->flags |= DNS_MESSAGEFLAG_TC;
|
|
|
|
goto renderend;
|
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
1999-07-28 02:20:36 +00:00
|
|
|
result = dns_message_rendersection(client->message,
|
2001-06-05 09:02:16 +00:00
|
|
|
DNS_SECTION_ANSWER,
|
|
|
|
DNS_MESSAGERENDER_PARTIAL);
|
1999-09-01 18:25:05 +00:00
|
|
|
if (result == ISC_R_NOSPACE) {
|
|
|
|
client->message->flags |= DNS_MESSAGEFLAG_TC;
|
|
|
|
goto renderend;
|
|
|
|
}
|
1999-07-28 02:20:36 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
result = dns_message_rendersection(client->message,
|
2001-06-05 09:02:16 +00:00
|
|
|
DNS_SECTION_AUTHORITY,
|
|
|
|
DNS_MESSAGERENDER_PARTIAL);
|
1999-09-01 18:25:05 +00:00
|
|
|
if (result == ISC_R_NOSPACE) {
|
|
|
|
client->message->flags |= DNS_MESSAGEFLAG_TC;
|
|
|
|
goto renderend;
|
|
|
|
}
|
1999-07-28 02:20:36 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
result = dns_message_rendersection(client->message,
|
1999-12-22 03:22:59 +00:00
|
|
|
DNS_SECTION_ADDITIONAL, 0);
|
1999-07-28 02:20:36 +00:00
|
|
|
if (result != ISC_R_SUCCESS && result != ISC_R_NOSPACE)
|
|
|
|
goto done;
|
1999-09-01 18:25:05 +00:00
|
|
|
renderend:
|
1999-07-24 01:17:44 +00:00
|
|
|
result = dns_message_renderend(client->message);
|
2000-05-26 22:43:36 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
1999-08-05 01:51:32 +00:00
|
|
|
|
2001-03-05 21:15:47 +00:00
|
|
|
if (cleanup_cctx) {
|
|
|
|
dns_compress_invalidate(&cctx);
|
|
|
|
cleanup_cctx = ISC_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
if (TCP_CLIENT(client)) {
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_usedregion(&buffer, &r);
|
2000-02-10 22:16:56 +00:00
|
|
|
isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t) r.length);
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_buffer_add(&tcpbuffer, r.length);
|
2000-09-13 03:15:01 +00:00
|
|
|
result = client_sendpkg(client, &tcpbuffer);
|
|
|
|
} else
|
|
|
|
result = client_sendpkg(client, &buffer);
|
|
|
|
if (result == ISC_R_SUCCESS)
|
2000-01-28 23:35:53 +00:00
|
|
|
return;
|
2000-06-22 00:05:11 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
done:
|
2000-06-22 00:05:11 +00:00
|
|
|
if (client->tcpbuf != NULL) {
|
|
|
|
isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
|
|
|
|
client->tcpbuf = NULL;
|
|
|
|
}
|
2001-03-05 21:15:47 +00:00
|
|
|
|
|
|
|
if (cleanup_cctx)
|
|
|
|
dns_compress_invalidate(&cctx);
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
ns_client_next(client, result);
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
void
|
|
|
|
ns_client_error(ns_client_t *client, isc_result_t result) {
|
|
|
|
dns_rcode_t rcode;
|
1999-08-03 01:21:00 +00:00
|
|
|
dns_message_t *message;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
|
|
|
|
CTRACE("error");
|
|
|
|
|
1999-08-03 01:21:00 +00:00
|
|
|
message = client->message;
|
1999-07-24 01:17:44 +00:00
|
|
|
rcode = dns_result_torcode(result);
|
|
|
|
|
1999-07-28 02:20:36 +00:00
|
|
|
/*
|
2000-06-22 23:04:27 +00:00
|
|
|
* Message may be an in-progress reply that we had trouble
|
1999-07-28 02:20:36 +00:00
|
|
|
* with, in which case QR will be set. We need to clear QR before
|
|
|
|
* calling dns_message_reply() to avoid triggering an assertion.
|
|
|
|
*/
|
1999-08-03 01:21:00 +00:00
|
|
|
message->flags &= ~DNS_MESSAGEFLAG_QR;
|
1999-10-07 19:43:18 +00:00
|
|
|
/*
|
|
|
|
* AA and AD shouldn't be set.
|
|
|
|
*/
|
|
|
|
message->flags &= ~(DNS_MESSAGEFLAG_AA | DNS_MESSAGEFLAG_AD);
|
1999-08-03 01:21:00 +00:00
|
|
|
result = dns_message_reply(message, ISC_TRUE);
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
1999-08-03 01:21:00 +00:00
|
|
|
/*
|
|
|
|
* It could be that we've got a query with a good header,
|
|
|
|
* but a bad question section, so we try again with
|
|
|
|
* want_question_section set to ISC_FALSE.
|
|
|
|
*/
|
|
|
|
result = dns_message_reply(message, ISC_FALSE);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
ns_client_next(client, result);
|
|
|
|
return;
|
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
1999-08-03 01:21:00 +00:00
|
|
|
message->rcode = rcode;
|
2001-03-19 20:52:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FORMERR loop avoidance: If we sent a FORMERR message
|
|
|
|
* with the same ID to the same client less than two
|
|
|
|
* seconds ago, assume that we are in an infinite error
|
|
|
|
* packet dialog with a server for some protocol whose
|
|
|
|
* error responses look enough like DNS queries to
|
|
|
|
* elicit a FORMERR response. Drop a packet to break
|
|
|
|
* the loop.
|
|
|
|
*/
|
|
|
|
if (rcode == dns_rcode_formerr) {
|
|
|
|
if (isc_sockaddr_equal(&client->peeraddr,
|
|
|
|
&client->formerrcache.addr) &&
|
|
|
|
message->id == client->formerrcache.id &&
|
|
|
|
client->requesttime - client->formerrcache.time < 2) {
|
|
|
|
/* Drop packet. */
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
|
|
|
|
"possible error packet loop, "
|
|
|
|
"FORMERR dropped");
|
|
|
|
ns_client_next(client, result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
client->formerrcache.addr = client->peeraddr;
|
|
|
|
client->formerrcache.time = client->requesttime;
|
|
|
|
client->formerrcache.id = message->id;
|
|
|
|
}
|
1999-07-28 02:20:36 +00:00
|
|
|
ns_client_send(client);
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
1999-09-02 01:52:31 +00:00
|
|
|
static inline isc_result_t
|
|
|
|
client_addopt(ns_client_t *client) {
|
|
|
|
dns_rdataset_t *rdataset;
|
|
|
|
dns_rdatalist_t *rdatalist;
|
|
|
|
dns_rdata_t *rdata;
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
REQUIRE(client->opt == NULL); /* XXXRTH free old. */
|
|
|
|
|
|
|
|
rdatalist = NULL;
|
|
|
|
result = dns_message_gettemprdatalist(client->message, &rdatalist);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
rdata = NULL;
|
|
|
|
result = dns_message_gettemprdata(client->message, &rdata);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
1999-10-20 23:20:30 +00:00
|
|
|
rdataset = NULL;
|
|
|
|
result = dns_message_gettemprdataset(client->message, &rdataset);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
dns_rdataset_init(rdataset);
|
1999-09-02 01:52:31 +00:00
|
|
|
|
|
|
|
rdatalist->type = dns_rdatatype_opt;
|
|
|
|
rdatalist->covers = 0;
|
|
|
|
|
|
|
|
/*
|
2001-01-23 18:47:33 +00:00
|
|
|
* Set the maximum UDP buffer size.
|
1999-09-02 01:52:31 +00:00
|
|
|
*/
|
2000-06-22 00:05:11 +00:00
|
|
|
rdatalist->rdclass = RECV_BUFFER_SIZE;
|
1999-09-02 01:52:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set EXTENDED-RCODE, VERSION, and Z to 0.
|
|
|
|
*/
|
2001-10-24 21:47:12 +00:00
|
|
|
rdatalist->ttl = (client->extflags & DNS_MESSAGEEXTFLAG_REPLYPRESERVE);
|
1999-09-02 01:52:31 +00:00
|
|
|
|
|
|
|
/*
|
2000-10-11 17:44:18 +00:00
|
|
|
* No ENDS options in the default case.
|
1999-09-02 01:52:31 +00:00
|
|
|
*/
|
|
|
|
rdata->data = NULL;
|
|
|
|
rdata->length = 0;
|
2000-03-29 18:57:36 +00:00
|
|
|
rdata->rdclass = rdatalist->rdclass;
|
|
|
|
rdata->type = rdatalist->type;
|
2000-10-25 04:26:57 +00:00
|
|
|
rdata->flags = 0;
|
1999-09-02 01:52:31 +00:00
|
|
|
|
|
|
|
ISC_LIST_INIT(rdatalist->rdata);
|
|
|
|
ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
|
2001-11-30 01:59:49 +00:00
|
|
|
RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset)
|
|
|
|
== ISC_R_SUCCESS);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-09-02 01:52:31 +00:00
|
|
|
client->opt = rdataset;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2001-07-26 20:42:46 +00:00
|
|
|
static inline isc_boolean_t
|
2001-12-06 04:38:51 +00:00
|
|
|
allowed(isc_netaddr_t *addr, dns_name_t *signer, dns_acl_t *acl) {
|
2001-07-26 20:42:46 +00:00
|
|
|
int match;
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
if (acl == NULL)
|
|
|
|
return (ISC_TRUE);
|
2001-12-06 04:38:51 +00:00
|
|
|
result = dns_acl_match(addr, signer, acl, &ns_g_server->aclenv,
|
2001-07-26 20:42:46 +00:00
|
|
|
&match, NULL);
|
|
|
|
if (result == ISC_R_SUCCESS && match > 0)
|
|
|
|
return (ISC_TRUE);
|
|
|
|
return (ISC_FALSE);
|
|
|
|
}
|
2000-10-11 17:44:18 +00:00
|
|
|
|
2000-01-06 01:09:27 +00:00
|
|
|
/*
|
2001-01-27 02:08:07 +00:00
|
|
|
* Handle an incoming request event from the socket (UDP case)
|
2000-01-06 01:09:27 +00:00
|
|
|
* or tcpmsg (TCP case).
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
static void
|
1999-08-05 01:51:32 +00:00
|
|
|
client_request(isc_task_t *task, isc_event_t *event) {
|
1999-07-24 01:17:44 +00:00
|
|
|
ns_client_t *client;
|
2001-01-27 02:08:07 +00:00
|
|
|
isc_socketevent_t *sevent;
|
1999-07-24 01:17:44 +00:00
|
|
|
isc_result_t result;
|
2001-12-06 04:38:51 +00:00
|
|
|
isc_result_t sigresult = ISC_R_SUCCESS;
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_buffer_t *buffer;
|
2001-01-27 02:08:07 +00:00
|
|
|
isc_buffer_t tbuffer;
|
1999-08-05 22:14:43 +00:00
|
|
|
dns_view_t *view;
|
1999-09-02 01:52:31 +00:00
|
|
|
dns_rdataset_t *opt;
|
1999-12-10 23:58:04 +00:00
|
|
|
isc_boolean_t ra; /* Recursion available. */
|
2001-04-16 21:18:10 +00:00
|
|
|
isc_netaddr_t netaddr;
|
2001-11-16 20:01:57 +00:00
|
|
|
isc_netaddr_t destaddr;
|
2001-04-16 21:18:10 +00:00
|
|
|
int match;
|
2001-05-09 23:13:03 +00:00
|
|
|
dns_messageid_t id;
|
|
|
|
unsigned int flags;
|
2001-10-09 02:05:55 +00:00
|
|
|
isc_boolean_t notimp;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
REQUIRE(event != NULL);
|
2000-04-17 19:22:44 +00:00
|
|
|
client = event->ev_arg;
|
1999-07-24 01:17:44 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(task == client->task);
|
|
|
|
|
2000-04-28 01:24:18 +00:00
|
|
|
UNUSED(task);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-01-15 00:36:26 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
2000-01-27 01:00:16 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
INSIST(client->state ==
|
|
|
|
TCP_CLIENT(client) ?
|
|
|
|
NS_CLIENTSTATE_READING :
|
|
|
|
NS_CLIENTSTATE_READY);
|
|
|
|
|
2001-01-27 02:08:07 +00:00
|
|
|
if (event->ev_type == ISC_SOCKEVENT_RECVDONE) {
|
2000-03-14 03:30:52 +00:00
|
|
|
INSIST(!TCP_CLIENT(client));
|
2001-01-27 02:08:07 +00:00
|
|
|
sevent = (isc_socketevent_t *)event;
|
2001-02-12 21:45:37 +00:00
|
|
|
REQUIRE(sevent == client->recvevent);
|
2001-01-27 02:08:07 +00:00
|
|
|
isc_buffer_init(&tbuffer, sevent->region.base, sevent->n);
|
|
|
|
isc_buffer_add(&tbuffer, sevent->n);
|
|
|
|
buffer = &tbuffer;
|
|
|
|
result = sevent->result;
|
2001-05-14 21:12:32 +00:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
client->peeraddr = sevent->address;
|
|
|
|
client->peeraddr_valid = ISC_TRUE;
|
|
|
|
}
|
2001-01-27 02:08:07 +00:00
|
|
|
if ((sevent->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0) {
|
2000-03-14 03:30:52 +00:00
|
|
|
client->attributes |= NS_CLIENTATTR_PKTINFO;
|
2001-01-27 02:08:07 +00:00
|
|
|
client->pktinfo = sevent->pktinfo;
|
2000-03-14 03:30:52 +00:00
|
|
|
}
|
2001-01-27 02:08:07 +00:00
|
|
|
if ((sevent->attributes & ISC_SOCKEVENTATTR_MULTICAST) != 0)
|
2000-06-15 18:26:28 +00:00
|
|
|
client->attributes |= NS_CLIENTATTR_MULTICAST;
|
2001-01-27 02:08:07 +00:00
|
|
|
client->nrecvs--;
|
1999-08-05 01:51:32 +00:00
|
|
|
} else {
|
2000-03-14 03:30:52 +00:00
|
|
|
INSIST(TCP_CLIENT(client));
|
2000-04-17 19:22:44 +00:00
|
|
|
REQUIRE(event->ev_type == DNS_EVENT_TCPMSG);
|
|
|
|
REQUIRE(event->ev_sender == &client->tcpmsg);
|
1999-08-05 01:51:32 +00:00
|
|
|
buffer = &client->tcpmsg.buffer;
|
|
|
|
result = client->tcpmsg.result;
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->nreads == 1);
|
2000-02-17 18:18:24 +00:00
|
|
|
/*
|
|
|
|
* client->peeraddr was set when the connection was accepted.
|
|
|
|
*/
|
1999-12-22 16:59:05 +00:00
|
|
|
client->nreads--;
|
1999-08-05 01:51:32 +00:00
|
|
|
}
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
if (exit_check(client))
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
2000-06-22 01:14:38 +00:00
|
|
|
client->state = client->newstate = NS_CLIENTSTATE_WORKING;
|
2000-06-15 18:26:28 +00:00
|
|
|
|
1999-12-16 23:29:07 +00:00
|
|
|
isc_stdtime_get(&client->requesttime);
|
1999-10-29 16:16:54 +00:00
|
|
|
client->now = client->requesttime;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2001-05-14 21:12:32 +00:00
|
|
|
if (TCP_CLIENT(client)) {
|
1999-08-05 01:51:32 +00:00
|
|
|
ns_client_next(client, result);
|
2001-05-14 21:12:32 +00:00
|
|
|
} else {
|
2001-05-14 21:33:45 +00:00
|
|
|
if (result != ISC_R_CANCELED)
|
|
|
|
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT,
|
|
|
|
ISC_LOG_ERROR,
|
|
|
|
"UDP client handler shutting "
|
|
|
|
"down due to fatal receive "
|
|
|
|
"error: %s",
|
|
|
|
isc_result_totext(result));
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_task_shutdown(client->task);
|
2001-05-14 21:12:32 +00:00
|
|
|
}
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
2001-04-16 21:18:10 +00:00
|
|
|
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
|
|
|
|
|
2001-05-14 21:12:32 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
|
|
|
"%s request",
|
|
|
|
TCP_CLIENT(client) ? "TCP" : "UDP");
|
|
|
|
|
2001-04-16 21:18:10 +00:00
|
|
|
/*
|
|
|
|
* Check the blackhole ACL for UDP only, since TCP is done in
|
|
|
|
* client_newconn.
|
|
|
|
*/
|
|
|
|
if (!TCP_CLIENT(client)) {
|
|
|
|
|
|
|
|
if (ns_g_server->blackholeacl != NULL &&
|
|
|
|
dns_acl_match(&netaddr, NULL, ns_g_server->blackholeacl,
|
|
|
|
&ns_g_server->aclenv,
|
|
|
|
&match, NULL) == ISC_R_SUCCESS &&
|
|
|
|
match > 0)
|
|
|
|
{
|
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
|
2001-04-16 22:10:44 +00:00
|
|
|
"blackholed UDP datagram");
|
2001-04-16 21:18:10 +00:00
|
|
|
ns_client_next(client, ISC_R_SUCCESS);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-09 04:29:19 +00:00
|
|
|
/*
|
|
|
|
* Silently drop multicast requests for the present.
|
|
|
|
* XXXMPA look at when/if mDNS spec stabilizes.
|
|
|
|
*/
|
|
|
|
if ((client->attributes & NS_CLIENTATTR_MULTICAST) != 0) {
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(2),
|
|
|
|
"dropping multicast request");
|
|
|
|
ns_client_next(client, DNS_R_REFUSED);
|
|
|
|
}
|
|
|
|
|
2001-05-09 23:13:03 +00:00
|
|
|
result = dns_message_peekheader(buffer, &id, &flags);
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2001-05-09 23:13:03 +00:00
|
|
|
/*
|
|
|
|
* There isn't enough header to determine whether
|
|
|
|
* this was a request or a response. Drop it.
|
|
|
|
*/
|
|
|
|
ns_client_next(client, result);
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
1999-12-20 23:06:17 +00:00
|
|
|
|
|
|
|
/*
|
2001-05-09 23:13:03 +00:00
|
|
|
* The client object handles requests, not responses.
|
|
|
|
* If this is a UDP response, forward it to the dispatcher.
|
|
|
|
* If it's a TCP response, discard it here.
|
1999-12-20 23:06:17 +00:00
|
|
|
*/
|
2001-05-09 23:13:03 +00:00
|
|
|
if ((flags & DNS_MESSAGEFLAG_QR) != 0) {
|
2001-01-27 02:08:07 +00:00
|
|
|
if (TCP_CLIENT(client)) {
|
|
|
|
CTRACE("unexpected response");
|
|
|
|
ns_client_next(client, DNS_R_FORMERR);
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
2001-01-27 02:08:07 +00:00
|
|
|
} else {
|
2001-02-10 02:00:11 +00:00
|
|
|
dns_dispatch_importrecv(client->dispatch, event);
|
2001-01-27 02:08:07 +00:00
|
|
|
ns_client_next(client, ISC_R_SUCCESS);
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
2001-01-27 02:08:07 +00:00
|
|
|
}
|
1999-12-20 23:06:17 +00:00
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2001-05-09 23:13:03 +00:00
|
|
|
/*
|
|
|
|
* It's a request. Parse it.
|
|
|
|
*/
|
|
|
|
result = dns_message_parse(client->message, buffer, 0);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
/*
|
|
|
|
* Parsing the request failed. Send a response
|
|
|
|
* (typically FORMERR or SERVFAIL).
|
|
|
|
*/
|
|
|
|
ns_client_error(client, result);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2001-10-09 02:05:55 +00:00
|
|
|
switch (client->message->opcode) {
|
|
|
|
case dns_opcode_query:
|
|
|
|
case dns_opcode_update:
|
|
|
|
case dns_opcode_notify:
|
|
|
|
notimp = ISC_FALSE;
|
|
|
|
break;
|
|
|
|
case dns_opcode_iquery:
|
|
|
|
default:
|
|
|
|
notimp = ISC_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-09-19 00:02:26 +00:00
|
|
|
client->message->rcode = dns_rcode_noerror;
|
|
|
|
|
2001-10-13 01:10:26 +00:00
|
|
|
/* RFC1123 section 6.1.3.2 */
|
|
|
|
if ((client->attributes & NS_CLIENTATTR_MULTICAST) != 0)
|
|
|
|
client->message->flags &= ~DNS_MESSAGEFLAG_RD;
|
|
|
|
|
1999-09-02 01:52:31 +00:00
|
|
|
/*
|
|
|
|
* Deal with EDNS.
|
|
|
|
*/
|
|
|
|
opt = dns_message_getopt(client->message);
|
|
|
|
if (opt != NULL) {
|
|
|
|
unsigned int version;
|
|
|
|
|
1999-10-07 19:43:18 +00:00
|
|
|
/*
|
|
|
|
* Set the client's UDP buffer size.
|
|
|
|
*/
|
|
|
|
client->udpsize = opt->rdclass;
|
2000-11-13 21:34:03 +00:00
|
|
|
|
2001-04-19 18:29:53 +00:00
|
|
|
/*
|
|
|
|
* If the requested UDP buffer size is less than 512,
|
|
|
|
* ignore it and use 512.
|
|
|
|
*/
|
|
|
|
if (client->udpsize < 512)
|
|
|
|
client->udpsize = 512;
|
|
|
|
|
2000-12-02 05:57:16 +00:00
|
|
|
/*
|
|
|
|
* Get the flags out of the OPT record.
|
|
|
|
*/
|
2001-03-12 22:27:15 +00:00
|
|
|
client->extflags = (isc_uint16_t)(opt->ttl & 0xFFFF);
|
1999-10-07 19:43:18 +00:00
|
|
|
|
1999-09-02 01:52:31 +00:00
|
|
|
/*
|
|
|
|
* Create an OPT for our reply.
|
|
|
|
*/
|
|
|
|
result = client_addopt(client);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
ns_client_error(client, result);
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
1999-09-02 01:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do we understand this version of ENDS?
|
|
|
|
*
|
|
|
|
* XXXRTH need library support for this!
|
|
|
|
*/
|
|
|
|
version = (opt->ttl & 0x00FF0000) >> 16;
|
|
|
|
if (version != 0) {
|
|
|
|
ns_client_error(client, DNS_R_BADVERS);
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
1999-09-02 01:52:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-21 23:45:05 +00:00
|
|
|
if (client->message->rdclass == 0) {
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
2001-06-28 02:39:46 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
|
2000-08-21 23:45:05 +00:00
|
|
|
"message class could not be determined");
|
2001-05-25 07:39:48 +00:00
|
|
|
ns_client_dumpmessage(client,
|
|
|
|
"message class could not be determined");
|
2001-10-09 02:05:55 +00:00
|
|
|
ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_FORMERR);
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
2000-08-21 23:45:05 +00:00
|
|
|
}
|
|
|
|
|
2001-11-16 20:01:57 +00:00
|
|
|
/*
|
|
|
|
* Determine the destination address. For IPv6, we get this from the
|
|
|
|
* pktinfo structure (if supported). For IPv4, we have to make do with
|
|
|
|
* the address of the interface where the request was received.
|
|
|
|
*/
|
|
|
|
if (client->interface->addr.type.sa.sa_family == AF_INET6) {
|
|
|
|
if ((client->attributes & NS_CLIENTATTR_PKTINFO) != 0)
|
|
|
|
isc_netaddr_fromin6(&destaddr, &client->pktinfo.ipi6_addr);
|
|
|
|
else
|
|
|
|
isc_netaddr_any6(&destaddr);
|
|
|
|
} else {
|
|
|
|
isc_netaddr_fromsockaddr(&destaddr, &client->interface->addr);
|
|
|
|
}
|
|
|
|
|
2000-01-21 20:24:49 +00:00
|
|
|
/*
|
2000-04-06 17:29:47 +00:00
|
|
|
* Find a view that matches the client's source address.
|
2000-01-21 20:24:49 +00:00
|
|
|
*/
|
|
|
|
for (view = ISC_LIST_HEAD(ns_g_server->viewlist);
|
|
|
|
view != NULL;
|
|
|
|
view = ISC_LIST_NEXT(view, link)) {
|
2000-01-24 21:29:12 +00:00
|
|
|
if (client->message->rdclass == view->rdclass ||
|
|
|
|
client->message->rdclass == dns_rdataclass_any)
|
|
|
|
{
|
2001-12-06 04:38:51 +00:00
|
|
|
dns_name_t *tsig = NULL;
|
|
|
|
sigresult = dns_message_rechecksig(client->message,
|
|
|
|
view);
|
|
|
|
if (tsigname == ISC_R_SUCCESS)
|
|
|
|
tsig = client->message->tsigname;
|
|
|
|
|
|
|
|
if (allowed(&netaddr, tsig, view->matchclients) &&
|
|
|
|
allowed(&destaddr, tsig, view->matchdestinations) &&
|
2001-10-13 01:10:26 +00:00
|
|
|
!((client->message->flags & DNS_MESSAGEFLAG_RD)
|
|
|
|
== 0 && view->matchrecursiveonly))
|
2000-04-06 17:29:47 +00:00
|
|
|
{
|
|
|
|
dns_view_attach(view, &client->view);
|
|
|
|
break;
|
|
|
|
}
|
2000-01-21 20:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (view == NULL) {
|
2000-11-15 19:15:25 +00:00
|
|
|
char classname[DNS_RDATACLASS_FORMATSIZE];
|
|
|
|
|
2000-09-18 22:54:08 +00:00
|
|
|
/*
|
|
|
|
* Do a dummy TSIG verification attempt so that the
|
|
|
|
* response will have a TSIG if the query did, as
|
|
|
|
* required by RFC2845.
|
|
|
|
*/
|
2000-09-12 07:48:28 +00:00
|
|
|
isc_buffer_t b;
|
|
|
|
isc_region_t *r;
|
2001-12-06 04:38:51 +00:00
|
|
|
|
|
|
|
dns_message_resetsig(client->message);
|
|
|
|
|
2000-09-12 07:48:28 +00:00
|
|
|
r = dns_message_getrawmessage(client->message);
|
|
|
|
isc_buffer_init(&b, r->base, r->length);
|
|
|
|
isc_buffer_add(&b, r->length);
|
|
|
|
(void)dns_tsig_verify(&b, client->message, NULL, NULL);
|
|
|
|
|
2000-11-15 19:15:25 +00:00
|
|
|
dns_rdataclass_format(client->message->rdclass, classname,
|
|
|
|
sizeof(classname));
|
2000-04-06 17:29:47 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
2001-06-28 02:39:46 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
|
2000-11-15 19:15:25 +00:00
|
|
|
"no matching view in class '%s'", classname);
|
2001-05-25 07:39:48 +00:00
|
|
|
ns_client_dumpmessage(client, "no matching view in class");
|
2001-10-09 02:05:55 +00:00
|
|
|
ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_REFUSED);
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
2000-01-21 20:24:49 +00:00
|
|
|
}
|
|
|
|
|
2000-04-06 20:26:39 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(5),
|
|
|
|
"using view '%s'", view->name);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-12-10 18:14:49 +00:00
|
|
|
/*
|
2000-08-01 01:33:37 +00:00
|
|
|
* Check for a signature. We log bad signatures regardless of
|
1999-12-10 18:14:49 +00:00
|
|
|
* whether they ultimately cause the request to be rejected or
|
|
|
|
* not. We do not log the lack of a signature unless we are
|
|
|
|
* debugging.
|
|
|
|
*/
|
|
|
|
client->signer = NULL;
|
2000-01-21 20:24:49 +00:00
|
|
|
dns_name_init(&client->signername, NULL);
|
1999-12-10 18:14:49 +00:00
|
|
|
result = dns_message_signer(client->message, &client->signername);
|
2000-04-06 22:03:35 +00:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
1999-12-10 18:14:49 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
|
|
|
"request has valid signature");
|
|
|
|
client->signer = &client->signername;
|
2000-04-06 22:03:35 +00:00
|
|
|
} else if (result == ISC_R_NOTFOUND) {
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
1999-12-10 18:14:49 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
|
|
|
"request is not signed");
|
2000-01-21 20:24:49 +00:00
|
|
|
} else if (result == DNS_R_NOIDENTITY) {
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
2000-01-21 20:24:49 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
|
|
|
"request is signed by a nonauthoritative key");
|
1999-12-10 18:14:49 +00:00
|
|
|
} else {
|
|
|
|
/* There is a signature, but it is bad. */
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
1999-12-10 18:14:49 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
|
|
|
|
"request has invalid signature: %s",
|
|
|
|
isc_result_totext(result));
|
2001-10-12 04:01:40 +00:00
|
|
|
/*
|
|
|
|
* Accept update messages signed by unknown keys so that
|
|
|
|
* update forwarding works transparently through slaves
|
|
|
|
* that don't have all the same keys as the master.
|
|
|
|
*/
|
2000-11-03 04:45:03 +00:00
|
|
|
if (!(client->message->tsigstatus == dns_tsigerror_badkey &&
|
|
|
|
client->message->opcode == dns_opcode_update)) {
|
2000-09-13 01:30:34 +00:00
|
|
|
ns_client_error(client, sigresult);
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
2000-09-13 01:30:34 +00:00
|
|
|
}
|
1999-12-10 18:14:49 +00:00
|
|
|
}
|
|
|
|
|
1999-12-10 23:58:04 +00:00
|
|
|
/*
|
|
|
|
* Decide whether recursive service is available to this client.
|
|
|
|
* We do this here rather than in the query code so that we can
|
|
|
|
* set the RA bit correctly on all kinds of responses, not just
|
|
|
|
* responses to ordinary queries.
|
|
|
|
*/
|
2000-04-06 20:26:39 +00:00
|
|
|
ra = ISC_FALSE;
|
|
|
|
if (client->view->resolver != NULL &&
|
|
|
|
client->view->recursion == ISC_TRUE &&
|
|
|
|
/* XXX this will log too much too early */
|
2001-05-25 07:39:48 +00:00
|
|
|
ns_client_checkacl(client, "recursion available:",
|
2000-04-06 23:59:19 +00:00
|
|
|
client->view->recursionacl,
|
2000-09-12 18:50:36 +00:00
|
|
|
ISC_TRUE, ISC_LOG_DEBUG(1)) == ISC_R_SUCCESS)
|
1999-12-10 23:58:04 +00:00
|
|
|
ra = ISC_TRUE;
|
2000-04-06 20:26:39 +00:00
|
|
|
|
1999-12-10 23:58:04 +00:00
|
|
|
if (ra == ISC_TRUE)
|
|
|
|
client->attributes |= NS_CLIENTATTR_RA;
|
2000-01-07 19:20:25 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
/*
|
|
|
|
* Dispatch the request.
|
|
|
|
*/
|
|
|
|
switch (client->message->opcode) {
|
|
|
|
case dns_opcode_query:
|
|
|
|
CTRACE("query");
|
|
|
|
ns_query_start(client);
|
|
|
|
break;
|
1999-08-20 06:03:02 +00:00
|
|
|
case dns_opcode_update:
|
|
|
|
CTRACE("update");
|
2001-01-29 19:49:52 +00:00
|
|
|
ns_client_settimeout(client, 60);
|
2000-09-13 01:30:34 +00:00
|
|
|
ns_update_start(client, sigresult);
|
1999-08-20 06:03:02 +00:00
|
|
|
break;
|
|
|
|
case dns_opcode_notify:
|
|
|
|
CTRACE("notify");
|
2001-01-29 19:49:52 +00:00
|
|
|
ns_client_settimeout(client, 60);
|
1999-12-16 01:23:17 +00:00
|
|
|
ns_notify_start(client);
|
1999-08-20 06:03:02 +00:00
|
|
|
break;
|
1999-07-24 01:17:44 +00:00
|
|
|
case dns_opcode_iquery:
|
|
|
|
CTRACE("iquery");
|
1999-11-30 22:35:43 +00:00
|
|
|
ns_client_error(client, DNS_R_NOTIMP);
|
2000-02-04 00:46:58 +00:00
|
|
|
break;
|
1999-07-24 01:17:44 +00:00
|
|
|
default:
|
|
|
|
CTRACE("unknown opcode");
|
|
|
|
ns_client_error(client, DNS_R_NOTIMP);
|
|
|
|
}
|
2000-01-27 01:00:16 +00:00
|
|
|
|
2001-02-14 03:54:53 +00:00
|
|
|
cleanup:
|
|
|
|
return;
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
client_timeout(isc_task_t *task, isc_event_t *event) {
|
|
|
|
ns_client_t *client;
|
|
|
|
|
|
|
|
REQUIRE(event != NULL);
|
2000-04-17 19:22:44 +00:00
|
|
|
REQUIRE(event->ev_type == ISC_TIMEREVENT_LIFE ||
|
|
|
|
event->ev_type == ISC_TIMEREVENT_IDLE);
|
|
|
|
client = event->ev_arg;
|
1999-07-24 01:17:44 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(task == client->task);
|
|
|
|
REQUIRE(client->timer != NULL);
|
|
|
|
|
2000-04-28 01:24:18 +00:00
|
|
|
UNUSED(task);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
CTRACE("timeout");
|
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
|
2000-02-14 23:56:47 +00:00
|
|
|
if (client->shutdown != NULL) {
|
|
|
|
(client->shutdown)(client->shutdown_arg, ISC_R_TIMEDOUT);
|
|
|
|
client->shutdown = NULL;
|
|
|
|
client->shutdown_arg = NULL;
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
if (client->newstate > NS_CLIENTSTATE_READY)
|
|
|
|
client->newstate = NS_CLIENTSTATE_READY;
|
2001-11-14 19:11:06 +00:00
|
|
|
(void)exit_check(client);
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2000-01-28 23:35:53 +00:00
|
|
|
client_create(ns_clientmgr_t *manager, ns_client_t **clientp)
|
1999-07-24 01:17:44 +00:00
|
|
|
{
|
|
|
|
ns_client_t *client;
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Caller must be holding the manager lock.
|
|
|
|
*
|
2000-08-01 01:33:37 +00:00
|
|
|
* Note: creating a client does not add the client to the
|
1999-12-22 16:59:05 +00:00
|
|
|
* manager's client list or set the client's manager pointer.
|
|
|
|
* The caller is responsible for that.
|
1999-07-24 01:17:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(clientp != NULL && *clientp == NULL);
|
|
|
|
|
2001-09-19 23:08:24 +00:00
|
|
|
client = isc_mem_get(manager->mctx, sizeof(*client));
|
1999-07-24 01:17:44 +00:00
|
|
|
if (client == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
client->task = NULL;
|
2000-04-12 01:41:21 +00:00
|
|
|
result = isc_task_create(manager->taskmgr, 0, &client->task);
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_client;
|
2000-01-25 19:31:23 +00:00
|
|
|
isc_task_setname(client->task, "client", client);
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
client->timer = NULL;
|
|
|
|
result = isc_timer_create(manager->timermgr, isc_timertype_inactive,
|
|
|
|
NULL, NULL, client->task, client_timeout,
|
|
|
|
client, &client->timer);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_task;
|
2001-01-29 19:49:52 +00:00
|
|
|
client->timerset = ISC_FALSE;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
client->message = NULL;
|
|
|
|
result = dns_message_create(manager->mctx, DNS_MESSAGE_INTENTPARSE,
|
|
|
|
&client->message);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_timer;
|
|
|
|
|
|
|
|
/* XXXRTH Hardwired constants */
|
|
|
|
|
2001-02-12 21:45:37 +00:00
|
|
|
client->sendevent = (isc_socketevent_t *)
|
|
|
|
isc_event_allocate(manager->mctx, client,
|
|
|
|
ISC_SOCKEVENT_SENDDONE,
|
|
|
|
client_senddone, client,
|
|
|
|
sizeof(isc_socketevent_t));
|
2001-02-22 23:16:59 +00:00
|
|
|
if (client->sendevent == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
2001-03-06 01:24:40 +00:00
|
|
|
goto cleanup_message;
|
2001-02-22 23:16:59 +00:00
|
|
|
}
|
2001-02-12 21:45:37 +00:00
|
|
|
|
2001-01-27 02:08:07 +00:00
|
|
|
client->recvbuf = isc_mem_get(manager->mctx, RECV_BUFFER_SIZE);
|
2001-02-22 23:16:59 +00:00
|
|
|
if (client->recvbuf == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
2001-02-12 21:45:37 +00:00
|
|
|
goto cleanup_sendevent;
|
2001-02-22 23:16:59 +00:00
|
|
|
}
|
2001-02-12 21:45:37 +00:00
|
|
|
|
|
|
|
client->recvevent = (isc_socketevent_t *)
|
|
|
|
isc_event_allocate(manager->mctx, client,
|
|
|
|
ISC_SOCKEVENT_RECVDONE,
|
|
|
|
client_request, client,
|
|
|
|
sizeof(isc_socketevent_t));
|
2001-02-22 23:16:59 +00:00
|
|
|
if (client->recvevent == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
2001-02-12 21:45:37 +00:00
|
|
|
goto cleanup_recvbuf;
|
2001-02-22 23:16:59 +00:00
|
|
|
}
|
2001-01-27 02:08:07 +00:00
|
|
|
|
1999-07-28 02:20:36 +00:00
|
|
|
client->magic = NS_CLIENT_MAGIC;
|
1999-07-29 00:55:35 +00:00
|
|
|
client->mctx = manager->mctx;
|
1999-12-22 16:59:05 +00:00
|
|
|
client->manager = NULL;
|
2000-02-10 22:16:56 +00:00
|
|
|
client->state = NS_CLIENTSTATE_INACTIVE;
|
|
|
|
client->newstate = NS_CLIENTSTATE_MAX;
|
1999-12-22 16:59:05 +00:00
|
|
|
client->naccepts = 0;
|
|
|
|
client->nreads = 0;
|
|
|
|
client->nsends = 0;
|
2001-01-27 02:08:07 +00:00
|
|
|
client->nrecvs = 0;
|
2001-11-14 22:00:22 +00:00
|
|
|
client->nctls = 0;
|
2000-02-11 20:56:19 +00:00
|
|
|
client->references = 0;
|
1999-07-24 01:17:44 +00:00
|
|
|
client->attributes = 0;
|
1999-08-05 22:14:43 +00:00
|
|
|
client->view = NULL;
|
1999-07-24 01:17:44 +00:00
|
|
|
client->dispatch = NULL;
|
2001-01-27 02:08:07 +00:00
|
|
|
client->udpsocket = NULL;
|
1999-08-05 01:51:32 +00:00
|
|
|
client->tcplistener = NULL;
|
|
|
|
client->tcpsocket = NULL;
|
1999-12-22 16:59:05 +00:00
|
|
|
client->tcpmsg_valid = ISC_FALSE;
|
2000-06-22 00:05:11 +00:00
|
|
|
client->tcpbuf = NULL;
|
1999-09-02 01:52:31 +00:00
|
|
|
client->opt = NULL;
|
1999-10-07 19:43:18 +00:00
|
|
|
client->udpsize = 512;
|
2000-11-13 21:34:03 +00:00
|
|
|
client->extflags = 0;
|
1999-08-03 01:21:00 +00:00
|
|
|
client->next = NULL;
|
2000-01-12 18:01:12 +00:00
|
|
|
client->shutdown = NULL;
|
|
|
|
client->shutdown_arg = NULL;
|
1999-12-10 18:14:49 +00:00
|
|
|
dns_name_init(&client->signername, NULL);
|
2000-01-11 21:18:22 +00:00
|
|
|
client->mortal = ISC_FALSE;
|
2000-01-15 00:36:26 +00:00
|
|
|
client->tcpquota = NULL;
|
|
|
|
client->recursionquota = NULL;
|
2000-01-11 21:18:22 +00:00
|
|
|
client->interface = NULL;
|
2000-04-04 18:28:51 +00:00
|
|
|
client->peeraddr_valid = ISC_FALSE;
|
2000-07-26 17:39:12 +00:00
|
|
|
ISC_EVENT_INIT(&client->ctlevent, sizeof(client->ctlevent), 0, NULL,
|
2000-07-26 23:45:55 +00:00
|
|
|
NS_EVENT_CLIENTCONTROL, client_start, client, client,
|
2000-07-26 17:39:12 +00:00
|
|
|
NULL, NULL);
|
2001-03-19 20:52:21 +00:00
|
|
|
/*
|
|
|
|
* Initialize FORMERR cache to sentinel value that will not match
|
|
|
|
* any actual FORMERR response.
|
|
|
|
*/
|
|
|
|
isc_sockaddr_any(&client->formerrcache.addr);
|
|
|
|
client->formerrcache.time = 0;
|
|
|
|
client->formerrcache.id = 0;
|
1999-07-24 01:17:44 +00:00
|
|
|
ISC_LINK_INIT(client, link);
|
2000-01-28 23:35:53 +00:00
|
|
|
client->list = NULL;
|
1999-07-28 02:20:36 +00:00
|
|
|
|
1999-07-29 00:55:35 +00:00
|
|
|
/*
|
|
|
|
* We call the init routines for the various kinds of client here,
|
|
|
|
* after we have created an otherwise valid client, because some
|
|
|
|
* of them call routines that REQUIRE(NS_CLIENT_VALID(client)).
|
|
|
|
*/
|
|
|
|
result = ns_query_init(client);
|
1999-07-28 02:20:36 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
2001-02-12 21:45:37 +00:00
|
|
|
goto cleanup_recvevent;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2001-10-30 00:48:25 +00:00
|
|
|
result = isc_task_onshutdown(client->task, client_shutdown, client);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_query;
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
CTRACE("create");
|
|
|
|
|
|
|
|
*clientp = client;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
2001-10-30 00:48:25 +00:00
|
|
|
cleanup_query:
|
|
|
|
ns_query_free(client);
|
|
|
|
|
2001-02-12 21:45:37 +00:00
|
|
|
cleanup_recvevent:
|
|
|
|
isc_event_free((isc_event_t **)&client->recvevent);
|
|
|
|
|
2001-01-27 02:08:07 +00:00
|
|
|
cleanup_recvbuf:
|
|
|
|
isc_mem_put(manager->mctx, client->recvbuf, RECV_BUFFER_SIZE);
|
|
|
|
|
2001-02-12 21:45:37 +00:00
|
|
|
cleanup_sendevent:
|
|
|
|
isc_event_free((isc_event_t **)&client->sendevent);
|
|
|
|
|
1999-07-29 00:55:35 +00:00
|
|
|
client->magic = 0;
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
cleanup_message:
|
|
|
|
dns_message_destroy(&client->message);
|
|
|
|
|
|
|
|
cleanup_timer:
|
|
|
|
isc_timer_detach(&client->timer);
|
|
|
|
|
|
|
|
cleanup_task:
|
|
|
|
isc_task_detach(&client->task);
|
|
|
|
|
|
|
|
cleanup_client:
|
2001-09-19 23:08:24 +00:00
|
|
|
isc_mem_put(manager->mctx, client, sizeof(*client));
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
static void
|
|
|
|
client_read(ns_client_t *client) {
|
|
|
|
isc_result_t result;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
CTRACE("read");
|
|
|
|
|
|
|
|
result = dns_tcpmsg_readmessage(&client->tcpmsg, client->task,
|
|
|
|
client_request, client);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
2000-02-10 22:16:56 +00:00
|
|
|
goto fail;
|
|
|
|
|
2000-02-14 23:56:47 +00:00
|
|
|
/*
|
|
|
|
* Set a timeout to limit the amount of time we will wait
|
|
|
|
* for a request on this TCP connection.
|
|
|
|
*/
|
2001-01-29 19:49:52 +00:00
|
|
|
ns_client_settimeout(client, 30);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
client->state = client->newstate = NS_CLIENTSTATE_READING;
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->nreads == 0);
|
2000-11-27 23:54:12 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
1999-12-22 16:59:05 +00:00
|
|
|
client->nreads++;
|
2000-02-10 22:16:56 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
fail:
|
2000-08-01 01:33:37 +00:00
|
|
|
ns_client_next(client, result);
|
1999-08-05 01:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
client_newconn(isc_task_t *task, isc_event_t *event) {
|
2000-04-17 19:22:44 +00:00
|
|
|
ns_client_t *client = event->ev_arg;
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event;
|
2000-01-11 21:18:22 +00:00
|
|
|
isc_result_t result;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
REQUIRE(event->ev_type == ISC_SOCKEVENT_NEWCONN);
|
1999-08-05 01:51:32 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(client->task == task);
|
|
|
|
|
2000-04-28 01:24:18 +00:00
|
|
|
UNUSED(task);
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
INSIST(client->state == NS_CLIENTSTATE_READY);
|
2000-07-26 17:39:12 +00:00
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->naccepts == 1);
|
|
|
|
client->naccepts--;
|
2000-01-11 21:18:22 +00:00
|
|
|
|
|
|
|
LOCK(&client->interface->lock);
|
|
|
|
INSIST(client->interface->ntcpcurrent > 0);
|
|
|
|
client->interface->ntcpcurrent--;
|
|
|
|
UNLOCK(&client->interface->lock);
|
|
|
|
|
2000-02-10 00:28:36 +00:00
|
|
|
/*
|
|
|
|
* We must take ownership of the new socket before the exit
|
2000-02-17 18:18:24 +00:00
|
|
|
* check to make sure it gets destroyed if we decide to exit.
|
2000-02-10 00:28:36 +00:00
|
|
|
*/
|
2000-02-17 18:18:24 +00:00
|
|
|
if (nevent->result == ISC_R_SUCCESS) {
|
|
|
|
client->tcpsocket = nevent->newsocket;
|
|
|
|
client->state = NS_CLIENTSTATE_READING;
|
2000-11-27 23:54:12 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
2000-02-17 18:18:24 +00:00
|
|
|
|
2001-11-14 19:11:06 +00:00
|
|
|
(void)isc_socket_getpeername(client->tcpsocket,
|
|
|
|
&client->peeraddr);
|
2000-04-04 18:28:51 +00:00
|
|
|
client->peeraddr_valid = ISC_TRUE;
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
|
|
|
"new TCP connection");
|
2000-02-17 18:18:24 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* XXXRTH What should we do? We're trying to accept but
|
|
|
|
* it didn't work. If we just give up, then TCP
|
|
|
|
* service may eventually stop.
|
|
|
|
*
|
|
|
|
* For now, we just go idle.
|
|
|
|
*
|
|
|
|
* Going idle is probably the right thing if the
|
|
|
|
* I/O was canceled.
|
|
|
|
*/
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
2000-02-17 18:18:24 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
2000-04-04 18:28:51 +00:00
|
|
|
"accept failed: %s",
|
2000-02-17 18:18:24 +00:00
|
|
|
isc_result_totext(nevent->result));
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-17 18:18:24 +00:00
|
|
|
if (exit_check(client))
|
|
|
|
goto freeevent;
|
|
|
|
|
|
|
|
if (nevent->result == ISC_R_SUCCESS) {
|
2000-11-03 02:45:55 +00:00
|
|
|
int match;
|
|
|
|
isc_netaddr_t netaddr;
|
|
|
|
|
2001-04-16 21:18:10 +00:00
|
|
|
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
|
|
|
|
|
|
|
|
if (ns_g_server->blackholeacl != NULL &&
|
|
|
|
dns_acl_match(&netaddr, NULL,
|
|
|
|
ns_g_server->blackholeacl,
|
|
|
|
&ns_g_server->aclenv,
|
|
|
|
&match, NULL) == ISC_R_SUCCESS &&
|
|
|
|
match > 0)
|
|
|
|
{
|
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
|
|
|
|
"blackholed connection attempt");
|
|
|
|
goto freeevent;
|
|
|
|
}
|
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->tcpmsg_valid == ISC_FALSE);
|
1999-08-05 01:51:32 +00:00
|
|
|
dns_tcpmsg_init(client->mctx, client->tcpsocket,
|
|
|
|
&client->tcpmsg);
|
1999-12-22 16:59:05 +00:00
|
|
|
client->tcpmsg_valid = ISC_TRUE;
|
2000-01-07 19:20:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Let a new client take our place immediately, before
|
|
|
|
* we wait for a request packet. If we don't,
|
2000-11-03 02:45:55 +00:00
|
|
|
* telnetting to port 53 (once per CPU) will
|
2000-01-07 19:20:25 +00:00
|
|
|
* deny service to legititmate TCP clients.
|
|
|
|
*/
|
2000-01-15 00:36:26 +00:00
|
|
|
result = isc_quota_attach(&ns_g_server->tcpquota,
|
|
|
|
&client->tcpquota);
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
result = ns_client_replace(client);
|
2000-01-11 21:18:22 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
2000-01-11 21:18:22 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
|
|
|
|
"no more TCP clients: %s",
|
|
|
|
isc_result_totext(result));
|
|
|
|
}
|
2000-11-03 02:45:55 +00:00
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
client_read(client);
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-17 18:18:24 +00:00
|
|
|
freeevent:
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_event_free(&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
client_accept(ns_client_t *client) {
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
CTRACE("accept");
|
|
|
|
|
|
|
|
result = isc_socket_accept(client->tcplistener, client->task,
|
|
|
|
client_newconn, client);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"isc_socket_accept() failed: %s",
|
|
|
|
isc_result_totext(result));
|
|
|
|
/*
|
|
|
|
* XXXRTH What should we do? We're trying to accept but
|
|
|
|
* it didn't work. If we just give up, then TCP
|
|
|
|
* service may eventually stop.
|
|
|
|
*
|
|
|
|
* For now, we just go idle.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->naccepts == 0);
|
|
|
|
client->naccepts++;
|
2000-01-11 21:18:22 +00:00
|
|
|
LOCK(&client->interface->lock);
|
|
|
|
client->interface->ntcpcurrent++;
|
|
|
|
UNLOCK(&client->interface->lock);
|
1999-12-22 16:59:05 +00:00
|
|
|
}
|
|
|
|
|
2001-01-27 02:08:07 +00:00
|
|
|
static void
|
|
|
|
client_udprecv(ns_client_t *client) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_region_t r;
|
|
|
|
|
|
|
|
CTRACE("udprecv");
|
|
|
|
|
|
|
|
r.base = client->recvbuf;
|
|
|
|
r.length = RECV_BUFFER_SIZE;
|
2001-02-12 21:45:37 +00:00
|
|
|
result = isc_socket_recv2(client->udpsocket, &r, 1,
|
|
|
|
client->task, client->recvevent, 0);
|
2001-01-27 02:08:07 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"isc_socket_recv() failed: %s",
|
|
|
|
isc_result_totext(result));
|
|
|
|
/*
|
2001-02-23 22:38:28 +00:00
|
|
|
* This cannot happen in the current implementation, since
|
2001-11-14 02:03:43 +00:00
|
|
|
* isc_socket_recv2() cannot fail if flags == 0.
|
2001-01-27 02:08:07 +00:00
|
|
|
*
|
2001-02-23 22:38:28 +00:00
|
|
|
* If this does fail, we just go idle.
|
2001-01-27 02:08:07 +00:00
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
INSIST(client->nrecvs == 0);
|
|
|
|
client->nrecvs++;
|
|
|
|
}
|
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
void
|
2000-02-11 20:56:19 +00:00
|
|
|
ns_client_attach(ns_client_t *source, ns_client_t **targetp) {
|
|
|
|
REQUIRE(NS_CLIENT_VALID(source));
|
|
|
|
REQUIRE(targetp != NULL && *targetp == NULL);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-11 20:56:19 +00:00
|
|
|
source->references++;
|
2000-09-13 01:30:34 +00:00
|
|
|
ns_client_log(source, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
|
|
|
|
"ns_client_attach: ref = %d", source->references);
|
2000-02-11 20:56:19 +00:00
|
|
|
*targetp = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_client_detach(ns_client_t **clientp) {
|
|
|
|
ns_client_t *client = *clientp;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-11 20:56:19 +00:00
|
|
|
client->references--;
|
|
|
|
INSIST(client->references >= 0);
|
|
|
|
*clientp = NULL;
|
2000-09-13 01:30:34 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
|
|
|
|
"ns_client_detach: ref = %d", client->references);
|
2001-11-14 19:11:06 +00:00
|
|
|
(void)exit_check(client);
|
1999-12-22 16:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_boolean_t
|
2000-01-06 01:09:27 +00:00
|
|
|
ns_client_shuttingdown(ns_client_t *client) {
|
2000-05-14 04:02:10 +00:00
|
|
|
return (ISC_TF(client->newstate == NS_CLIENTSTATE_FREED));
|
2000-01-06 01:09:27 +00:00
|
|
|
}
|
|
|
|
|
2000-01-11 21:18:22 +00:00
|
|
|
isc_result_t
|
2000-01-15 00:36:26 +00:00
|
|
|
ns_client_replace(ns_client_t *client) {
|
2000-01-07 19:20:25 +00:00
|
|
|
isc_result_t result;
|
2000-05-02 01:16:21 +00:00
|
|
|
|
2000-01-07 19:20:25 +00:00
|
|
|
CTRACE("replace");
|
|
|
|
|
2000-01-17 23:48:15 +00:00
|
|
|
result = ns_clientmgr_createclients(client->manager,
|
|
|
|
1, client->interface,
|
|
|
|
(TCP_CLIENT(client) ?
|
|
|
|
ISC_TRUE : ISC_FALSE));
|
2000-01-07 19:20:25 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
|
|
|
/*
|
2000-08-01 01:33:37 +00:00
|
|
|
* The responsibility for listening for new requests is hereby
|
|
|
|
* transferred to the new client. Therefore, the old client
|
2000-01-17 23:48:15 +00:00
|
|
|
* should refrain from listening for any more requests.
|
2000-01-07 19:20:25 +00:00
|
|
|
*/
|
2000-01-11 21:18:22 +00:00
|
|
|
client->mortal = ISC_TRUE;
|
2000-01-07 19:20:25 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
/***
|
|
|
|
*** Client Manager
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
|
|
|
clientmgr_destroy(ns_clientmgr_t *manager) {
|
2000-01-28 23:35:53 +00:00
|
|
|
REQUIRE(ISC_LIST_EMPTY(manager->active));
|
|
|
|
REQUIRE(ISC_LIST_EMPTY(manager->inactive));
|
2001-10-24 03:10:18 +00:00
|
|
|
REQUIRE(ISC_LIST_EMPTY(manager->recursing));
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
MTRACE("clientmgr_destroy");
|
|
|
|
|
2000-09-26 22:09:20 +00:00
|
|
|
DESTROYLOCK(&manager->lock);
|
1999-07-24 01:17:44 +00:00
|
|
|
manager->magic = 0;
|
2001-09-19 23:08:24 +00:00
|
|
|
isc_mem_put(manager->mctx, manager, sizeof(*manager));
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
ns_clientmgr_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,
|
|
|
|
isc_timermgr_t *timermgr, ns_clientmgr_t **managerp)
|
|
|
|
{
|
|
|
|
ns_clientmgr_t *manager;
|
|
|
|
isc_result_t result;
|
|
|
|
|
2001-09-19 23:08:24 +00:00
|
|
|
manager = isc_mem_get(mctx, sizeof(*manager));
|
1999-07-24 01:17:44 +00:00
|
|
|
if (manager == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
result = isc_mutex_init(&manager->lock);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_manager;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
manager->mctx = mctx;
|
|
|
|
manager->taskmgr = taskmgr;
|
|
|
|
manager->timermgr = timermgr;
|
|
|
|
manager->exiting = ISC_FALSE;
|
2000-01-28 23:35:53 +00:00
|
|
|
ISC_LIST_INIT(manager->active);
|
|
|
|
ISC_LIST_INIT(manager->inactive);
|
2001-10-24 03:10:18 +00:00
|
|
|
ISC_LIST_INIT(manager->recursing);
|
1999-07-24 01:17:44 +00:00
|
|
|
manager->magic = MANAGER_MAGIC;
|
|
|
|
|
|
|
|
MTRACE("create");
|
|
|
|
|
|
|
|
*managerp = manager;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
cleanup_manager:
|
2001-09-19 23:08:24 +00:00
|
|
|
isc_mem_put(manager->mctx, manager, sizeof(*manager));
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_clientmgr_destroy(ns_clientmgr_t **managerp) {
|
|
|
|
ns_clientmgr_t *manager;
|
|
|
|
ns_client_t *client;
|
|
|
|
isc_boolean_t need_destroy = ISC_FALSE;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
REQUIRE(managerp != NULL);
|
|
|
|
manager = *managerp;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
|
|
|
MTRACE("destroy");
|
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
|
|
|
|
manager->exiting = ISC_TRUE;
|
|
|
|
|
2001-10-24 03:10:18 +00:00
|
|
|
for (client = ISC_LIST_HEAD(manager->recursing);
|
|
|
|
client != NULL;
|
|
|
|
client = ISC_LIST_NEXT(client, link))
|
|
|
|
isc_task_shutdown(client->task);
|
|
|
|
|
2000-01-28 23:35:53 +00:00
|
|
|
for (client = ISC_LIST_HEAD(manager->active);
|
1999-07-24 01:17:44 +00:00
|
|
|
client != NULL;
|
|
|
|
client = ISC_LIST_NEXT(client, link))
|
|
|
|
isc_task_shutdown(client->task);
|
|
|
|
|
2000-01-28 23:35:53 +00:00
|
|
|
for (client = ISC_LIST_HEAD(manager->inactive);
|
|
|
|
client != NULL;
|
|
|
|
client = ISC_LIST_NEXT(client, link))
|
|
|
|
isc_task_shutdown(client->task);
|
|
|
|
|
|
|
|
if (ISC_LIST_EMPTY(manager->active) &&
|
2001-10-24 03:10:18 +00:00
|
|
|
ISC_LIST_EMPTY(manager->inactive) &&
|
|
|
|
ISC_LIST_EMPTY(manager->recursing))
|
1999-07-24 01:17:44 +00:00
|
|
|
need_destroy = ISC_TRUE;
|
|
|
|
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
if (need_destroy)
|
|
|
|
clientmgr_destroy(manager);
|
|
|
|
|
|
|
|
*managerp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2000-01-17 23:48:15 +00:00
|
|
|
ns_clientmgr_createclients(ns_clientmgr_t *manager, unsigned int n,
|
|
|
|
ns_interface_t *ifp, isc_boolean_t tcp)
|
1999-07-24 01:17:44 +00:00
|
|
|
{
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
unsigned int i;
|
|
|
|
ns_client_t *client;
|
|
|
|
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
REQUIRE(n > 0);
|
|
|
|
|
2000-01-17 23:48:15 +00:00
|
|
|
MTRACE("createclients");
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We MUST lock the manager lock for the entire client creation
|
|
|
|
* process. If we didn't do this, then a client could get a
|
|
|
|
* shutdown event and disappear out from under us.
|
|
|
|
*/
|
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
2000-07-26 17:39:12 +00:00
|
|
|
isc_event_t *ev;
|
2000-01-28 23:35:53 +00:00
|
|
|
/*
|
|
|
|
* Allocate a client. First try to get a recycled one;
|
|
|
|
* if that fails, make a new one.
|
|
|
|
*/
|
|
|
|
client = ISC_LIST_HEAD(manager->inactive);
|
|
|
|
if (client != NULL) {
|
|
|
|
MTRACE("recycle");
|
|
|
|
ISC_LIST_UNLINK(manager->inactive, client, link);
|
|
|
|
client->list = NULL;
|
|
|
|
} else {
|
|
|
|
MTRACE("create new");
|
|
|
|
result = client_create(manager, &client);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ns_interface_attach(ifp, &client->interface);
|
2000-09-22 00:13:08 +00:00
|
|
|
client->state = NS_CLIENTSTATE_READY;
|
2000-11-27 23:54:12 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
2000-01-28 23:35:53 +00:00
|
|
|
|
2000-01-17 23:48:15 +00:00
|
|
|
if (tcp) {
|
|
|
|
client->attributes |= NS_CLIENTATTR_TCP;
|
2000-02-10 22:16:56 +00:00
|
|
|
isc_socket_attach(ifp->tcpsocket,
|
|
|
|
&client->tcplistener);
|
2000-01-17 23:48:15 +00:00
|
|
|
} else {
|
2001-01-27 02:08:07 +00:00
|
|
|
isc_socket_t *sock;
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
dns_dispatch_attach(ifp->udpdispatch,
|
|
|
|
&client->dispatch);
|
2001-01-27 02:08:07 +00:00
|
|
|
sock = dns_dispatch_getsocket(client->dispatch);
|
|
|
|
isc_socket_attach(sock, &client->udpsocket);
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
1999-12-22 16:59:05 +00:00
|
|
|
client->manager = manager;
|
2000-01-28 23:35:53 +00:00
|
|
|
ISC_LIST_APPEND(manager->active, client, link);
|
|
|
|
client->list = &manager->active;
|
2000-07-26 17:39:12 +00:00
|
|
|
|
2001-11-14 22:00:22 +00:00
|
|
|
INSIST(client->nctls == 0);
|
|
|
|
client->nctls++;
|
2000-07-26 17:39:12 +00:00
|
|
|
ev = &client->ctlevent;
|
|
|
|
isc_task_send(client->task, &ev);
|
1999-08-05 01:51:32 +00:00
|
|
|
}
|
|
|
|
if (i != 0) {
|
|
|
|
/*
|
|
|
|
* We managed to create at least one client, so we
|
|
|
|
* declare victory.
|
|
|
|
*/
|
|
|
|
result = ISC_R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
1999-11-30 02:49:38 +00:00
|
|
|
isc_sockaddr_t *
|
|
|
|
ns_client_getsockaddr(ns_client_t *client) {
|
2000-02-17 18:18:24 +00:00
|
|
|
return (&client->peeraddr);
|
1999-11-30 02:49:38 +00:00
|
|
|
}
|
2000-02-22 21:24:24 +00:00
|
|
|
|
|
|
|
isc_result_t
|
2001-06-15 23:28:29 +00:00
|
|
|
ns_client_checkaclsilent(ns_client_t *client, dns_acl_t *acl,
|
|
|
|
isc_boolean_t default_allow)
|
2000-02-22 21:24:24 +00:00
|
|
|
{
|
|
|
|
isc_result_t result;
|
|
|
|
int match;
|
|
|
|
isc_netaddr_t netaddr;
|
|
|
|
|
|
|
|
if (acl == NULL) {
|
|
|
|
if (default_allow)
|
|
|
|
goto allow;
|
|
|
|
else
|
|
|
|
goto deny;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-22 21:24:24 +00:00
|
|
|
result = dns_acl_match(&netaddr, client->signer, acl,
|
|
|
|
&ns_g_server->aclenv,
|
|
|
|
&match, NULL);
|
2000-04-06 22:03:35 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
2000-02-22 21:24:24 +00:00
|
|
|
goto deny; /* Internal error, already logged. */
|
|
|
|
if (match > 0)
|
|
|
|
goto allow;
|
|
|
|
goto deny; /* Negative match or no match. */
|
|
|
|
|
|
|
|
allow:
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2000-02-22 21:24:24 +00:00
|
|
|
|
|
|
|
deny:
|
|
|
|
return (DNS_R_REFUSED);
|
|
|
|
}
|
|
|
|
|
2001-06-15 23:28:29 +00:00
|
|
|
isc_result_t
|
|
|
|
ns_client_checkacl(ns_client_t *client,
|
|
|
|
const char *opname, dns_acl_t *acl,
|
|
|
|
isc_boolean_t default_allow, int log_level)
|
|
|
|
{
|
|
|
|
isc_result_t result =
|
|
|
|
ns_client_checkaclsilent(client, acl, default_allow);
|
|
|
|
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
|
|
|
"%s approved", opname);
|
|
|
|
else
|
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
|
|
|
NS_LOGMODULE_CLIENT,
|
|
|
|
log_level, "%s denied", opname);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2001-05-25 07:39:48 +00:00
|
|
|
static void
|
|
|
|
ns_client_name(ns_client_t *client, char *peerbuf, size_t len) {
|
|
|
|
if (client->peeraddr_valid)
|
|
|
|
isc_sockaddr_format(&client->peeraddr, peerbuf, len);
|
|
|
|
else
|
|
|
|
snprintf(peerbuf, len, "@%p", client);
|
|
|
|
}
|
|
|
|
|
2001-08-08 22:54:55 +00:00
|
|
|
static void
|
|
|
|
ns_client_logv(ns_client_t *client, isc_logcategory_t *category,
|
|
|
|
isc_logmodule_t *module, int level, const char *fmt, va_list ap)
|
|
|
|
ISC_FORMAT_PRINTF(5, 0);
|
|
|
|
|
2000-04-04 18:28:51 +00:00
|
|
|
static void
|
|
|
|
ns_client_logv(ns_client_t *client, isc_logcategory_t *category,
|
|
|
|
isc_logmodule_t *module, int level, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
char msgbuf[2048];
|
2000-12-19 19:21:39 +00:00
|
|
|
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
|
2000-04-04 18:28:51 +00:00
|
|
|
|
|
|
|
vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
|
2001-09-19 23:08:24 +00:00
|
|
|
ns_client_name(client, peerbuf, sizeof(peerbuf));
|
2000-04-04 18:28:51 +00:00
|
|
|
|
2001-05-25 07:39:48 +00:00
|
|
|
isc_log_write(ns_g_lctx, category, module, level,
|
|
|
|
"client %s: %s", peerbuf, msgbuf);
|
2000-04-04 18:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_client_log(ns_client_t *client, isc_logcategory_t *category,
|
|
|
|
isc_logmodule_t *module, int level, const char *fmt, ...)
|
|
|
|
{
|
2000-05-02 01:16:21 +00:00
|
|
|
va_list ap;
|
|
|
|
|
2000-08-08 23:56:10 +00:00
|
|
|
if (! isc_log_wouldlog(ns_g_lctx, level))
|
2000-07-13 00:21:27 +00:00
|
|
|
return;
|
|
|
|
|
2000-04-04 18:28:51 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
ns_client_logv(client, category, module, level, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2001-03-11 06:19:39 +00:00
|
|
|
void
|
|
|
|
ns_client_aclmsg(const char *msg, dns_name_t *name, dns_rdataclass_t rdclass,
|
|
|
|
char *buf, size_t len)
|
|
|
|
{
|
|
|
|
char namebuf[DNS_NAME_FORMATSIZE];
|
|
|
|
char classbuf[DNS_RDATACLASS_FORMATSIZE];
|
|
|
|
|
|
|
|
dns_name_format(name, namebuf, sizeof(namebuf));
|
|
|
|
dns_rdataclass_format(rdclass, classbuf, sizeof(classbuf));
|
|
|
|
(void)snprintf(buf, len, "%s '%s/%s'", msg, namebuf, classbuf);
|
|
|
|
}
|
2001-05-25 07:39:48 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
ns_client_dumpmessage(ns_client_t *client, const char *reason) {
|
|
|
|
isc_buffer_t buffer;
|
|
|
|
char *buf = NULL;
|
|
|
|
int len = 1024;
|
|
|
|
isc_result_t result;
|
|
|
|
|
2001-05-28 05:17:05 +00:00
|
|
|
/*
|
2001-06-15 22:35:42 +00:00
|
|
|
* Note that these are multiline debug messages. We want a newline
|
2001-05-28 05:17:05 +00:00
|
|
|
* to appear in the log after each message.
|
|
|
|
*/
|
2001-05-25 07:39:48 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
buf = isc_mem_get(client->mctx, len);
|
|
|
|
if (buf == NULL)
|
|
|
|
break;
|
|
|
|
isc_buffer_init(&buffer, buf, len);
|
|
|
|
result = dns_message_totext(client->message,
|
|
|
|
&dns_master_style_debug,
|
|
|
|
0, &buffer);
|
|
|
|
if (result == ISC_R_NOSPACE) {
|
|
|
|
isc_mem_put(client->mctx, buf, len);
|
|
|
|
len += 1024;
|
|
|
|
} else if (result == ISC_R_SUCCESS)
|
2001-05-28 05:17:05 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_UNMATCHED,
|
2001-06-28 02:39:46 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
|
2001-05-28 05:17:05 +00:00
|
|
|
"%s\n%.*s", reason,
|
|
|
|
(int)isc_buffer_usedlength(&buffer),
|
|
|
|
buf);
|
2001-05-25 07:39:48 +00:00
|
|
|
} while (result == ISC_R_NOSPACE);
|
|
|
|
|
|
|
|
if (buf != NULL)
|
|
|
|
isc_mem_put(client->mctx, buf, len);
|
|
|
|
}
|