📄 client.c
字号:
view); if (sigresult == ISC_R_SUCCESS) tsig = client->message->tsigname; if (allowed(&netaddr, tsig, view->matchclients) && allowed(&destaddr, tsig, view->matchdestinations) && !((client->message->flags & DNS_MESSAGEFLAG_RD) == 0 && view->matchrecursiveonly)) { dns_view_attach(view, &client->view); break; } } } if (view == NULL) { char classname[DNS_RDATACLASS_FORMATSIZE]; /* * Do a dummy TSIG verification attempt so that the * response will have a TSIG if the query did, as * required by RFC2845. */ isc_buffer_t b; isc_region_t *r; dns_message_resetsig(client->message); 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); dns_rdataclass_format(client->message->rdclass, classname, sizeof(classname)); ns_client_log(client, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1), "no matching view in class '%s'", classname); ns_client_dumpmessage(client, "no matching view in class"); ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_REFUSED); goto cleanup; } ns_client_log(client, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(5), "using view '%s'", view->name); /* * Check for a signature. We log bad signatures regardless of * 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; dns_name_init(&client->signername, NULL); result = dns_message_signer(client->message, &client->signername); if (result == ISC_R_SUCCESS) { ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3), "request has valid signature"); client->signer = &client->signername; } else if (result == ISC_R_NOTFOUND) { ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3), "request is not signed"); } else if (result == DNS_R_NOIDENTITY) { ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3), "request is signed by a nonauthoritative key"); } else { char tsigrcode[64]; isc_buffer_t b; dns_name_t *name = NULL; isc_buffer_init(&b, tsigrcode, sizeof(tsigrcode) - 1); RUNTIME_CHECK(dns_tsigrcode_totext(client->message->tsigstatus, &b) == ISC_R_SUCCESS); tsigrcode[isc_buffer_usedlength(&b)] = '\0'; /* There is a signature, but it is bad. */ if (dns_message_gettsig(client->message, &name) != NULL) { char namebuf[DNS_NAME_FORMATSIZE]; dns_name_format(name, namebuf, sizeof(namebuf)); ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT, ISC_LOG_ERROR, "request has invalid signature: " "TSIG %s: %s (%s)", namebuf, isc_result_totext(result), tsigrcode); } else { ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT, ISC_LOG_ERROR, "request has invalid signature: %s (%s)", isc_result_totext(result), tsigrcode); } /* * 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. */ if (!(client->message->tsigstatus == dns_tsigerror_badkey && client->message->opcode == dns_opcode_update)) { ns_client_error(client, sigresult); goto cleanup; } } /* * 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. */ ra = ISC_FALSE; if (client->view->resolver != NULL && client->view->recursion == ISC_TRUE && ns_client_checkaclsilent(client, client->view->recursionacl, ISC_TRUE) == ISC_R_SUCCESS) ra = ISC_TRUE; if (ra == ISC_TRUE) client->attributes |= NS_CLIENTATTR_RA; ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3), ra ? "recursion available" : "recursion not available"); /* * Dispatch the request. */ switch (client->message->opcode) { case dns_opcode_query: CTRACE("query"); ns_query_start(client); break; case dns_opcode_update: CTRACE("update"); ns_client_settimeout(client, 60); ns_update_start(client, sigresult); break; case dns_opcode_notify: CTRACE("notify"); ns_client_settimeout(client, 60); ns_notify_start(client); break; case dns_opcode_iquery: CTRACE("iquery"); ns_client_error(client, DNS_R_NOTIMP); break; default: CTRACE("unknown opcode"); ns_client_error(client, DNS_R_NOTIMP); } cleanup: return;}static voidclient_timeout(isc_task_t *task, isc_event_t *event) { ns_client_t *client; REQUIRE(event != NULL); REQUIRE(event->ev_type == ISC_TIMEREVENT_LIFE || event->ev_type == ISC_TIMEREVENT_IDLE); client = event->ev_arg; REQUIRE(NS_CLIENT_VALID(client)); REQUIRE(task == client->task); REQUIRE(client->timer != NULL); UNUSED(task); CTRACE("timeout"); isc_event_free(&event); if (client->shutdown != NULL) { (client->shutdown)(client->shutdown_arg, ISC_R_TIMEDOUT); client->shutdown = NULL; client->shutdown_arg = NULL; } if (client->newstate > NS_CLIENTSTATE_READY) client->newstate = NS_CLIENTSTATE_READY; (void)exit_check(client);}static isc_result_tclient_create(ns_clientmgr_t *manager, ns_client_t **clientp){ ns_client_t *client; isc_result_t result; /* * Caller must be holding the manager lock. * * Note: creating a client does not add the client to the * manager's client list or set the client's manager pointer. * The caller is responsible for that. */ REQUIRE(clientp != NULL && *clientp == NULL); client = isc_mem_get(manager->mctx, sizeof(*client)); if (client == NULL) return (ISC_R_NOMEMORY); client->task = NULL; result = isc_task_create(manager->taskmgr, 0, &client->task); if (result != ISC_R_SUCCESS) goto cleanup_client; isc_task_setname(client->task, "client", client); 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; client->timerset = ISC_FALSE; 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 */ client->sendevent = (isc_socketevent_t *) isc_event_allocate(manager->mctx, client, ISC_SOCKEVENT_SENDDONE, client_senddone, client, sizeof(isc_socketevent_t)); if (client->sendevent == NULL) { result = ISC_R_NOMEMORY; goto cleanup_message; } client->recvbuf = isc_mem_get(manager->mctx, RECV_BUFFER_SIZE); if (client->recvbuf == NULL) { result = ISC_R_NOMEMORY; goto cleanup_sendevent; } client->recvevent = (isc_socketevent_t *) isc_event_allocate(manager->mctx, client, ISC_SOCKEVENT_RECVDONE, client_request, client, sizeof(isc_socketevent_t)); if (client->recvevent == NULL) { result = ISC_R_NOMEMORY; goto cleanup_recvbuf; } client->magic = NS_CLIENT_MAGIC; client->mctx = manager->mctx; client->manager = NULL; client->state = NS_CLIENTSTATE_INACTIVE; client->newstate = NS_CLIENTSTATE_MAX; client->naccepts = 0; client->nreads = 0; client->nsends = 0; client->nrecvs = 0; client->nupdates = 0; client->nctls = 0; client->references = 0; client->attributes = 0; client->view = NULL; client->dispatch = NULL; client->udpsocket = NULL; client->tcplistener = NULL; client->tcpsocket = NULL; client->tcpmsg_valid = ISC_FALSE; client->tcpbuf = NULL; client->opt = NULL; client->udpsize = 512; client->extflags = 0; client->next = NULL; client->shutdown = NULL; client->shutdown_arg = NULL; dns_name_init(&client->signername, NULL); client->mortal = ISC_FALSE; client->tcpquota = NULL; client->recursionquota = NULL; client->interface = NULL; client->peeraddr_valid = ISC_FALSE; ISC_EVENT_INIT(&client->ctlevent, sizeof(client->ctlevent), 0, NULL, NS_EVENT_CLIENTCONTROL, client_start, client, client, NULL, NULL); /* * 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; ISC_LINK_INIT(client, link); client->list = NULL; /* * 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); if (result != ISC_R_SUCCESS) goto cleanup_recvevent; result = isc_task_onshutdown(client->task, client_shutdown, client); if (result != ISC_R_SUCCESS) goto cleanup_query; CTRACE("create"); *clientp = client; return (ISC_R_SUCCESS); cleanup_query: ns_query_free(client); cleanup_recvevent: isc_event_free((isc_event_t **)&client->recvevent); cleanup_recvbuf: isc_mem_put(manager->mctx, client->recvbuf, RECV_BUFFER_SIZE); cleanup_sendevent: isc_event_free((isc_event_t **)&client->sendevent); client->magic = 0; cleanup_message: dns_message_destroy(&client->message); cleanup_timer: isc_timer_detach(&client->timer); cleanup_task: isc_task_detach(&client->task); cleanup_client: isc_mem_put(manager->mctx, client, sizeof(*client)); return (result);}static voidclient_read(ns_client_t *client) { isc_result_t result; CTRACE("read"); result = dns_tcpmsg_readmessage(&client->tcpmsg, client->task, client_request, client); if (result != ISC_R_SUCCESS) goto fail; /* * Set a timeout to limit the amount of time we will wait * for a request on this TCP connection. */ ns_client_settimeout(client, 30); client->state = client->newstate = NS_CLIENTSTATE_READING; INSIST(client->nreads == 0); INSIST(client->recursionquota == NULL); client->nreads++; return; fail: ns_client_next(client, result);}static voidclient_newconn(isc_task_t *task, isc_event_t *event) { ns_client_t *client = event->ev_arg; isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event; isc_result_t result; REQUIRE(event->ev_type == ISC_SOCKEVENT_NEWCONN); REQUIRE(NS_CLIENT_VALID(client)); REQUIRE(client->task == task); UNUSED(task); INSIST(client->state == NS_CLIENTSTATE_READY); INSIST(client->naccepts == 1); client->naccepts--; LOCK(&client->interface->lock); INSIST(client->interface->ntcpcurrent > 0); client->interface->ntcpcurrent--; UNLOCK(&client->interface->lock); /* * We must take ownership of the new socket before the exit * check to make sure it gets destroyed if we decide to exit. */ if (nevent->result == ISC_R_SUCCESS) { client->tcpsocket = nevent->newsocket; client->state = NS_CLIENTSTATE_READING; INSIST(client->recursionquota == NULL); (void)isc_socket_getpeername(client->tcpsocket, &client->peeraddr); client->peeraddr_valid = ISC_TRUE; ns_client_log(client, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3), "new TCP connection"); } 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. */ ns_client_log(client, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3), "accept failed: %s", isc_result_totext(nevent->result)); } if (exit_check(client)) goto freeevent; if (nevent->result == ISC_R_SUCCESS) { int match; isc_netaddr_t netaddr; 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"); client->newstate = NS_CLIENTSTATE_READY; (void)exit_check(client); goto freeevent; } INSIST(client->tcpmsg_valid == ISC_FALSE); dns_tcpmsg_init(client->mctx, client->tcpsocket, &client->tcpmsg); client->tcpmsg_valid = ISC_TRUE; /* * Let a new client take our place immediately, before * we wait for a request packet. If we don't, * telnetting to port 53 (once per CPU) will * deny service to legititmate TCP clients. */ result = isc_quota_attach(&ns_g_server->tcpquota, &client->tcpquota); if (result == ISC_R_SUCCESS) result = ns_client_replace(client); if (result != ISC_R_SUCCESS) { ns_client_log(client, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_CLIENT, ISC_LOG_WARNING, "no more TCP clients: %s", isc_result_totext(result)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -