1 #if !defined(lint) && !defined(SABER)
2 static const char rcsid[] = "$Id: ctl_srvr.c,v 1.8.246.1 2008/02/18 04:10:16 marka Exp $";
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1998,1999 by Internet Software Consortium.
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include "port_before.h"
26 #include <sys/param.h>
28 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
33 #include <arpa/inet.h>
47 #include <isc/assertions.h>
49 #include <isc/eventlib.h>
51 #include <isc/logging.h>
52 #include <isc/memcluster.h>
56 #include "port_after.h"
59 # define SPRINTF(x) strlen(sprintf/**/x)
61 # define SPRINTF(x) ((size_t)sprintf x)
66 #define lastverb_p(verb) (verb->name == NULL || verb->func == NULL)
67 #define address_expr ctl_sa_ntop((struct sockaddr *)&sess->sa, \
68 tmp, sizeof tmp, ctx->logger)
73 available = 0, initializing, writing, reading, reading_data,
74 processing, idling, quitting, closing
78 struct sockaddr_in in;
79 #ifndef NO_SOCKADDR_UN
80 struct sockaddr_un un;
85 LINK(struct ctl_sess) link;
86 struct ctl_sctx * ctx;
95 struct ctl_buf outbuf;
96 const struct ctl_verb * verb;
100 ctl_srvrdone donefunc;
110 const struct ctl_verb * verbs;
111 const struct ctl_verb * connverb;
115 struct timespec timeout;
118 LIST(struct ctl_sess) sess;
123 static void ctl_accept(evContext, void *, int,
126 static void ctl_close(struct ctl_sess *);
127 static void ctl_new_state(struct ctl_sess *,
130 static void ctl_start_read(struct ctl_sess *);
131 static void ctl_stop_read(struct ctl_sess *);
132 static void ctl_readable(evContext, void *, int, int);
133 static void ctl_rdtimeout(evContext, void *,
136 static void ctl_wrtimeout(evContext, void *,
139 static void ctl_docommand(struct ctl_sess *);
140 static void ctl_writedone(evContext, void *, int, int);
141 static void ctl_morehelp(struct ctl_sctx *,
143 const struct ctl_verb *,
145 u_int, const void *, void *);
146 static void ctl_signal_done(struct ctl_sctx *,
151 static const char * state_names[] = {
152 "available", "initializing", "writing", "reading",
153 "reading_data", "processing", "idling", "quitting", "closing"
156 static const char space[] = " ";
158 static const struct ctl_verb fakehelpverb = {
159 "fakehelp", ctl_morehelp , NULL
167 * create, condition, and start a listener on the control port.
170 ctl_server(evContext lev, const struct sockaddr *sap, size_t sap_len,
171 const struct ctl_verb *verbs,
172 u_int unkncode, u_int timeoutcode,
173 u_int timeout, int backlog, int max_sess,
174 ctl_logfunc logger, void *uctx)
176 static const char me[] = "ctl_server";
177 static const int on = 1;
178 const struct ctl_verb *connverb;
179 struct ctl_sctx *ctx;
184 for (connverb = verbs;
185 connverb->name != NULL && connverb->func != NULL;
187 if (connverb->name[0] == '\0')
189 if (connverb->func == NULL) {
190 (*logger)(ctl_error, "%s: no connection verb found", me);
193 ctx = memget(sizeof *ctx);
195 (*logger)(ctl_error, "%s: getmem: %s", me, strerror(errno));
200 ctx->unkncode = unkncode;
201 ctx->timeoutcode = timeoutcode;
203 ctx->timeout = evConsTime(timeout, 0);
204 ctx->logger = logger;
205 ctx->connverb = connverb;
206 ctx->max_sess = max_sess;
208 INIT_LIST(ctx->sess);
209 ctx->sock = socket(sap->sa_family, SOCK_STREAM, PF_UNSPEC);
210 if (ctx->sock > evHighestFD(ctx->ev)) {
216 (*ctx->logger)(ctl_error, "%s: socket: %s",
217 me, strerror(errno));
218 memput(ctx, sizeof *ctx);
222 if (ctx->sock > evHighestFD(lev)) {
224 (*ctx->logger)(ctl_error, "%s: file descriptor > evHighestFD");
226 memput(ctx, sizeof *ctx);
229 #ifdef NO_UNIX_REUSEADDR
230 if (sap->sa_family != AF_UNIX)
232 if (setsockopt(ctx->sock, SOL_SOCKET, SO_REUSEADDR,
233 (const char *)&on, sizeof on) != 0) {
234 (*ctx->logger)(ctl_warning,
235 "%s: setsockopt(REUSEADDR): %s",
236 me, strerror(errno));
238 if (bind(ctx->sock, sap, sap_len) < 0) {
241 (*ctx->logger)(ctl_error, "%s: bind: %s: %s",
242 me, ctl_sa_ntop((const struct sockaddr *)sap,
243 tmp, sizeof tmp, ctx->logger),
244 strerror(save_errno));
246 memput(ctx, sizeof *ctx);
250 if (fcntl(ctx->sock, F_SETFD, 1) < 0) {
251 (*ctx->logger)(ctl_warning, "%s: fcntl: %s", me,
254 if (evListen(lev, ctx->sock, backlog, ctl_accept, ctx,
257 (*ctx->logger)(ctl_error, "%s: evListen(fd %d): %s",
258 me, ctx->sock, strerror(errno));
260 memput(ctx, sizeof *ctx);
264 (*ctx->logger)(ctl_debug, "%s: new ctx %p, sock %d",
272 * if the control listener is open, close it. clean out all eventlib
273 * stuff. close all active sessions.
276 ctl_endserver(struct ctl_sctx *ctx) {
277 static const char me[] = "ctl_endserver";
278 struct ctl_sess *this, *next;
280 (*ctx->logger)(ctl_debug, "%s: ctx %p, sock %d, acID %p, sess %p",
281 me, ctx, ctx->sock, ctx->acID.opaque, ctx->sess);
282 if (ctx->acID.opaque != NULL) {
283 (void)evCancelConn(ctx->ev, ctx->acID);
284 ctx->acID.opaque = NULL;
286 if (ctx->sock != -1) {
287 (void) close(ctx->sock);
290 for (this = HEAD(ctx->sess); this != NULL; this = next) {
291 next = NEXT(this, link);
294 memput(ctx, sizeof *ctx);
298 * If body is non-NULL then it we add a "." line after it.
299 * Caller must have escaped lines with leading ".".
302 ctl_response(struct ctl_sess *sess, u_int code, const char *text,
303 u_int flags, const void *respctx, ctl_srvrdone donefunc,
304 void *uap, const char *body, size_t bodylen)
306 static const char me[] = "ctl_response";
307 struct iovec iov[3], *iovp = iov;
308 struct ctl_sctx *ctx = sess->ctx;
309 char tmp[MAX_NTOP], *pc;
312 REQUIRE(sess->state == initializing ||
313 sess->state == processing ||
314 sess->state == reading_data ||
315 sess->state == writing);
316 REQUIRE(sess->wrtiID.opaque == NULL);
317 REQUIRE(sess->wrID.opaque == NULL);
318 ctl_new_state(sess, writing, me);
319 sess->donefunc = donefunc;
321 if (!allocated_p(sess->outbuf) &&
322 ctl_bufget(&sess->outbuf, ctx->logger) < 0) {
323 (*ctx->logger)(ctl_error, "%s: %s: cant get an output buffer",
327 if (sizeof "000-\r\n" + strlen(text) > (size_t)MAX_LINELEN) {
328 (*ctx->logger)(ctl_error, "%s: %s: output buffer ovf, closing",
332 sess->outbuf.used = SPRINTF((sess->outbuf.text, "%03d%c%s\r\n",
333 code, (flags & CTL_MORE) != 0 ? '-' : ' ',
335 for (pc = sess->outbuf.text, n = 0;
336 n < (int)sess->outbuf.used-2; pc++, n++)
337 if (!isascii((unsigned char)*pc) ||
338 !isprint((unsigned char)*pc))
340 *iovp++ = evConsIovec(sess->outbuf.text, sess->outbuf.used);
344 *iovp++ = evConsIovec(tmp, bodylen);
345 DE_CONST(".\r\n", tmp);
346 *iovp++ = evConsIovec(tmp, 3);
348 (*ctx->logger)(ctl_debug, "%s: [%d] %s", me,
349 sess->outbuf.used, sess->outbuf.text);
350 if (evWrite(ctx->ev, sess->sock, iov, iovp - iov,
351 ctl_writedone, sess, &sess->wrID) < 0) {
352 (*ctx->logger)(ctl_error, "%s: %s: evWrite: %s", me,
353 address_expr, strerror(errno));
356 if (evSetIdleTimer(ctx->ev, ctl_wrtimeout, sess, ctx->timeout,
359 (*ctx->logger)(ctl_error, "%s: %s: evSetIdleTimer: %s", me,
360 address_expr, strerror(errno));
363 if (evTimeRW(ctx->ev, sess->wrID, sess->wrtiID) < 0) {
364 (*ctx->logger)(ctl_error, "%s: %s: evTimeRW: %s", me,
365 address_expr, strerror(errno));
367 ctl_signal_done(ctx, sess);
371 sess->respctx = respctx;
372 sess->respflags = flags;
376 ctl_sendhelp(struct ctl_sess *sess, u_int code) {
377 static const char me[] = "ctl_sendhelp";
378 struct ctl_sctx *ctx = sess->ctx;
380 sess->helpcode = code;
381 sess->verb = &fakehelpverb;
382 ctl_morehelp(ctx, sess, NULL, me, CTL_MORE,
383 (const void *)ctx->verbs, NULL);
387 ctl_getcsctx(struct ctl_sess *sess) {
388 return (sess->csctx);
392 ctl_setcsctx(struct ctl_sess *sess, void *csctx) {
393 void *old = sess->csctx;
399 /* Private functions. */
402 ctl_accept(evContext lev, void *uap, int fd,
403 const void *lav, int lalen,
404 const void *rav, int ralen)
406 static const char me[] = "ctl_accept";
407 struct ctl_sctx *ctx = uap;
408 struct ctl_sess *sess = NULL;
416 (*ctx->logger)(ctl_error, "%s: accept: %s",
417 me, strerror(errno));
420 if (ctx->cur_sess == ctx->max_sess) {
421 (*ctx->logger)(ctl_error, "%s: %s: too many control sessions",
422 me, ctl_sa_ntop((const struct sockaddr *)rav,
428 sess = memget(sizeof *sess);
430 (*ctx->logger)(ctl_error, "%s: memget: %s", me,
435 if (fcntl(fd, F_SETFD, 1) < 0) {
436 (*ctx->logger)(ctl_warning, "%s: fcntl: %s", me,
440 INIT_LINK(sess, link);
441 APPEND(ctx->sess, sess, link);
444 sess->wrID.opaque = NULL;
445 sess->rdID.opaque = NULL;
446 sess->wrtiID.opaque = NULL;
447 sess->rdtiID.opaque = NULL;
448 sess->respctx = NULL;
450 if (((const struct sockaddr *)rav)->sa_family == AF_UNIX)
451 ctl_sa_copy((const struct sockaddr *)lav,
452 (struct sockaddr *)&sess->sa);
454 ctl_sa_copy((const struct sockaddr *)rav,
455 (struct sockaddr *)&sess->sa);
456 sess->donefunc = NULL;
457 buffer_init(sess->inbuf);
458 buffer_init(sess->outbuf);
459 sess->state = available;
460 ctl_new_state(sess, initializing, me);
461 sess->verb = ctx->connverb;
462 (*ctx->logger)(ctl_debug, "%s: %s: accepting (fd %d)",
463 me, address_expr, sess->sock);
464 (*ctx->connverb->func)(ctx, sess, ctx->connverb, "", 0,
465 (const struct sockaddr *)rav, ctx->uctx);
469 ctl_new_state(struct ctl_sess *sess, enum state new_state, const char *reason)
471 static const char me[] = "ctl_new_state";
472 struct ctl_sctx *ctx = sess->ctx;
475 (*ctx->logger)(ctl_debug, "%s: %s: %s -> %s (%s)",
477 state_names[sess->state],
478 state_names[new_state], reason);
479 sess->state = new_state;
483 ctl_close(struct ctl_sess *sess) {
484 static const char me[] = "ctl_close";
485 struct ctl_sctx *ctx = sess->ctx;
488 REQUIRE(sess->state == initializing ||
489 sess->state == writing ||
490 sess->state == reading ||
491 sess->state == processing ||
492 sess->state == reading_data ||
493 sess->state == idling);
494 REQUIRE(sess->sock != -1);
495 if (sess->state == reading || sess->state == reading_data)
497 else if (sess->state == writing) {
498 if (sess->wrID.opaque != NULL) {
499 (void) evCancelRW(ctx->ev, sess->wrID);
500 sess->wrID.opaque = NULL;
502 if (sess->wrtiID.opaque != NULL) {
503 (void) evClearIdleTimer(ctx->ev, sess->wrtiID);
504 sess->wrtiID.opaque = NULL;
507 ctl_new_state(sess, closing, me);
508 (void) close(sess->sock);
509 if (allocated_p(sess->inbuf))
510 ctl_bufput(&sess->inbuf);
511 if (allocated_p(sess->outbuf))
512 ctl_bufput(&sess->outbuf);
513 (*ctx->logger)(ctl_debug, "%s: %s: closed (fd %d)",
514 me, address_expr, sess->sock);
515 UNLINK(ctx->sess, sess, link);
516 memput(sess, sizeof *sess);
521 ctl_start_read(struct ctl_sess *sess) {
522 static const char me[] = "ctl_start_read";
523 struct ctl_sctx *ctx = sess->ctx;
526 REQUIRE(sess->state == initializing ||
527 sess->state == writing ||
528 sess->state == processing ||
529 sess->state == idling);
530 REQUIRE(sess->rdtiID.opaque == NULL);
531 REQUIRE(sess->rdID.opaque == NULL);
532 sess->inbuf.used = 0;
533 if (evSetIdleTimer(ctx->ev, ctl_rdtimeout, sess, ctx->timeout,
536 (*ctx->logger)(ctl_error, "%s: %s: evSetIdleTimer: %s", me,
537 address_expr, strerror(errno));
541 if (evSelectFD(ctx->ev, sess->sock, EV_READ,
542 ctl_readable, sess, &sess->rdID) < 0) {
543 (*ctx->logger)(ctl_error, "%s: %s: evSelectFD: %s", me,
544 address_expr, strerror(errno));
547 ctl_new_state(sess, reading, me);
551 ctl_stop_read(struct ctl_sess *sess) {
552 static const char me[] = "ctl_stop_read";
553 struct ctl_sctx *ctx = sess->ctx;
555 REQUIRE(sess->state == reading || sess->state == reading_data);
556 REQUIRE(sess->rdID.opaque != NULL);
557 (void) evDeselectFD(ctx->ev, sess->rdID);
558 sess->rdID.opaque = NULL;
559 if (sess->rdtiID.opaque != NULL) {
560 (void) evClearIdleTimer(ctx->ev, sess->rdtiID);
561 sess->rdtiID.opaque = NULL;
563 ctl_new_state(sess, idling, me);
567 ctl_readable(evContext lev, void *uap, int fd, int evmask) {
568 static const char me[] = "ctl_readable";
569 struct ctl_sess *sess = uap;
570 struct ctl_sctx *ctx;
571 char *eos, tmp[MAX_NTOP];
574 REQUIRE(sess != NULL);
576 REQUIRE(evmask == EV_READ);
577 REQUIRE(sess->state == reading || sess->state == reading_data);
580 evTouchIdleTimer(lev, sess->rdtiID);
581 if (!allocated_p(sess->inbuf) &&
582 ctl_bufget(&sess->inbuf, ctx->logger) < 0) {
583 (*ctx->logger)(ctl_error, "%s: %s: cant get an input buffer",
588 n = read(sess->sock, sess->inbuf.text + sess->inbuf.used,
589 MAX_LINELEN - sess->inbuf.used);
591 (*ctx->logger)(ctl_debug, "%s: %s: read: %s",
593 (n == 0) ? "Unexpected EOF" : strerror(errno));
597 sess->inbuf.used += n;
598 eos = memchr(sess->inbuf.text, '\n', sess->inbuf.used);
599 if (eos != NULL && eos != sess->inbuf.text && eos[-1] == '\r') {
601 if ((sess->respflags & CTL_DATA) != 0) {
602 INSIST(sess->verb != NULL);
603 (*sess->verb->func)(sess->ctx, sess, sess->verb,
605 CTL_DATA, sess->respctx,
611 sess->inbuf.used -= ((eos - sess->inbuf.text) + 1);
612 if (sess->inbuf.used == 0U)
613 ctl_bufput(&sess->inbuf);
615 memmove(sess->inbuf.text, eos + 1, sess->inbuf.used);
618 if (sess->inbuf.used == (size_t)MAX_LINELEN) {
619 (*ctx->logger)(ctl_error, "%s: %s: line too long, closing",
626 ctl_wrtimeout(evContext lev, void *uap,
630 static const char me[] = "ctl_wrtimeout";
631 struct ctl_sess *sess = uap;
632 struct ctl_sctx *ctx = sess->ctx;
639 REQUIRE(sess->state == writing);
640 sess->wrtiID.opaque = NULL;
641 (*ctx->logger)(ctl_warning, "%s: %s: write timeout, closing",
643 if (sess->wrID.opaque != NULL) {
644 (void) evCancelRW(ctx->ev, sess->wrID);
645 sess->wrID.opaque = NULL;
647 ctl_signal_done(ctx, sess);
648 ctl_new_state(sess, processing, me);
653 ctl_rdtimeout(evContext lev, void *uap,
657 static const char me[] = "ctl_rdtimeout";
658 struct ctl_sess *sess = uap;
659 struct ctl_sctx *ctx = sess->ctx;
666 REQUIRE(sess->state == reading);
667 sess->rdtiID.opaque = NULL;
668 (*ctx->logger)(ctl_warning, "%s: %s: timeout, closing",
670 if (sess->state == reading || sess->state == reading_data)
672 ctl_signal_done(ctx, sess);
673 ctl_new_state(sess, processing, me);
674 ctl_response(sess, ctx->timeoutcode, "Timeout.", CTL_EXIT, NULL,
675 NULL, NULL, NULL, 0);
679 ctl_docommand(struct ctl_sess *sess) {
680 static const char me[] = "ctl_docommand";
681 char *name, *rest, tmp[MAX_NTOP];
682 struct ctl_sctx *ctx = sess->ctx;
683 const struct ctl_verb *verb;
685 REQUIRE(allocated_p(sess->inbuf));
686 (*ctx->logger)(ctl_debug, "%s: %s: \"%s\" [%u]",
688 sess->inbuf.text, (u_int)sess->inbuf.used);
689 ctl_new_state(sess, processing, me);
690 name = sess->inbuf.text + strspn(sess->inbuf.text, space);
691 rest = name + strcspn(name, space);
694 rest += strspn(rest, space);
696 for (verb = ctx->verbs;
697 verb != NULL && verb->name != NULL && verb->func != NULL;
699 if (verb->name[0] != '\0' && strcasecmp(name, verb->name) == 0)
701 if (verb != NULL && verb->name != NULL && verb->func != NULL) {
703 (*verb->func)(ctx, sess, verb, rest, 0, NULL, ctx->uctx);
707 if (sizeof "Unrecognized command \"\" (args \"\")" +
708 strlen(name) + strlen(rest) > sizeof buf)
709 strcpy(buf, "Unrecognized command (buf ovf)");
712 "Unrecognized command \"%s\" (args \"%s\")",
714 ctl_response(sess, ctx->unkncode, buf, 0, NULL, NULL, NULL,
720 ctl_writedone(evContext lev, void *uap, int fd, int bytes) {
721 static const char me[] = "ctl_writedone";
722 struct ctl_sess *sess = uap;
723 struct ctl_sctx *ctx = sess->ctx;
725 int save_errno = errno;
730 REQUIRE(sess->state == writing);
731 REQUIRE(fd == sess->sock);
732 REQUIRE(sess->wrtiID.opaque != NULL);
733 sess->wrID.opaque = NULL;
734 (void) evClearIdleTimer(ctx->ev, sess->wrtiID);
735 sess->wrtiID.opaque = NULL;
737 (*ctx->logger)(ctl_error, "%s: %s: %s",
738 me, address_expr, strerror(save_errno));
743 INSIST(allocated_p(sess->outbuf));
744 ctl_bufput(&sess->outbuf);
745 if ((sess->respflags & CTL_EXIT) != 0) {
746 ctl_signal_done(ctx, sess);
749 } else if ((sess->respflags & CTL_MORE) != 0) {
750 INSIST(sess->verb != NULL);
751 (*sess->verb->func)(sess->ctx, sess, sess->verb, "",
752 CTL_MORE, sess->respctx, sess->ctx->uctx);
754 ctl_signal_done(ctx, sess);
755 ctl_start_read(sess);
760 ctl_morehelp(struct ctl_sctx *ctx, struct ctl_sess *sess,
761 const struct ctl_verb *verb, const char *text,
762 u_int respflags, const void *respctx, void *uctx)
764 const struct ctl_verb *this = respctx, *next = this + 1;
771 REQUIRE(!lastverb_p(this));
772 REQUIRE((respflags & CTL_MORE) != 0);
773 if (lastverb_p(next))
774 respflags &= ~CTL_MORE;
775 ctl_response(sess, sess->helpcode, this->help, respflags, next,
776 NULL, NULL, NULL, 0);
780 ctl_signal_done(struct ctl_sctx *ctx, struct ctl_sess *sess) {
781 if (sess->donefunc != NULL) {
782 (*sess->donefunc)(ctx, sess, sess->uap);
783 sess->donefunc = NULL;