smtpscript.c 17.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*	$OpenBSD: iobuf.h,v 1.1 2012/01/29 00:32:51 eric Exp $	*/
/*      
 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
 *
 * 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.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
 */

18 19
#include "includes.h"

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/queue.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <netdb.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>

#include "iobuf.h"

#include "smtpscript.h"

void   *ssl_connect(int);
void	ssl_close(void *);

/* XXX */
#define SMTP_LINE_MAX	4096

enum {
	OP_BLOCK,
	OP_REPEAT,
	OP_RANDOM,

	OP_NOOP,

	OP_FAIL,
	OP_CALL,
	OP_CONNECT,
	OP_DISCONNECT,
	OP_STARTTLS,
	OP_SLEEP,
	OP_WRITE,

	OP_EXPECT_DISCONNECT,
	OP_EXPECT_SMTP_RESPONSE,
};

struct op {
	struct op	*next;
	int		 type;
	union {
		struct {
			int		 count;
			struct op	*start;
			struct op	*last;
		}	block;
		struct {
			struct op	*op;
			int		 count;
		}	repeat;
		struct {
			struct op	*block;
		}	random;
		struct {
			char		*reason;
		}	fail;
		struct {
			struct procedure *proc;
		}	call;
		struct {
			char		*hostname;
			int		 portno;
		}	connect;
		struct {
			unsigned int	 ms;
		}	sleep;
		struct {
			const void	*buf;
			size_t		 len;
		} write;
		struct {
			int		 flags;
		} expect_smtp;
	} u;
};

#define RES_OK		0
#define RES_SKIP	1
#define RES_FAIL	2
#define RES_ERROR	3

struct ctx {
	int		 sock;
	void		*ssl;
	struct iobuf	 iobuf;
	int		 lvl;

	int		 result;
	char		*reason;
};

static struct op	* _op_connect;

int		verbose;
int		randomdelay; /* between each testcase */
size_t		rundelay; /* between each testcase */

static size_t	test_pass;
static size_t	test_skip;
static size_t	test_fail;
static size_t	test_error;

static struct op *op_add_child(struct op *, const struct op *);
static void run_testcase(struct procedure *);
static void process_op(struct ctx *, struct op *);
static const char * parse_smtp_response(char *, size_t, char **, int *);

struct procedure *
procedure_create(struct script *scr, char *name)
{
	struct procedure	*p;

	if (procedure_get_by_name(scr, name)) {
		warnx("procedure \"%s\" already exists", name);
		return (NULL);
	}

	p = calloc(1, sizeof *p);
	TAILQ_INIT(&p->vars);
	p->name = strdup(name);

	TAILQ_INSERT_TAIL(&scr->procs, p, entry);

	return (p);
}

struct procedure *
procedure_get_by_name(struct script *scr, const char *name)
{
	struct procedure *p;

	TAILQ_FOREACH(p, &scr->procs, entry)
		if (!strcmp(name, p->name))
			return (p);

	return (NULL);
}

int
proc_getvaridx(struct procedure *proc, char *name)
{
	struct variable	*v;
	int		 n;

	n = 0;
	TAILQ_FOREACH(v, &proc->vars, entry) {
		if (!strcmp(name, v->name))
			return (n);
		n++;
	}

	return (-1);
}

int
proc_addvar(struct procedure *proc, char *name)
{
	struct variable	*v;

	printf("adding variable \"%s\"\n", name);

	if (proc_getvaridx(proc, name) != -1)
		return (-1);
	v = calloc(1, sizeof *v);
	v->name = name;
	TAILQ_INSERT_TAIL(&proc->vars, v, entry);

	return (proc->varcount++);
}

struct op *
op_block(struct op *parent)
{
	struct op	o;

	bzero(&o, sizeof o);
	o.type = OP_BLOCK;

	return (op_add_child(parent, &o));
}

struct op *
op_repeat(struct op *parent, int count, struct op *op)
{
	struct op	o;

	bzero(&o, sizeof o);
	o.type = OP_REPEAT;
	o.u.repeat.count = count;
	o.u.repeat.op = op;

	return (op_add_child(parent, &o));
}

struct op *
op_random(struct op *parent, struct op *op)
{
	struct op	o;

	bzero(&o, sizeof o);
	o.type = OP_RANDOM;
	o.u.random.block = op;

	return (op_add_child(parent, &o));
}

struct op *
op_noop(struct op *parent)
{
	struct op	o;

	bzero(&o, sizeof o);
	o.type = OP_NOOP;

	return (op_add_child(parent, &o));
}

struct op *
op_call(struct op *parent, struct procedure *proc)
{
	struct op	o;

	bzero(&o, sizeof o);
	o.type = OP_CALL;
	o.u.call.proc = proc;

	return (op_add_child(parent, &o));
}

struct op *
op_fail(struct op *parent, char *reason)
{
	struct op	o;

	bzero(&o, sizeof o);
	o.type = OP_FAIL;
	o.u.fail.reason = reason;

	return (op_add_child(parent, &o));
}

struct op *
op_connect(struct op *parent, const char *hostname, int portno)
{
	struct op	o;

	bzero(&o, sizeof o);
	o.type = OP_CONNECT;
	o.u.connect.hostname = strdup(hostname);
	o.u.connect.portno = portno;
	return (op_add_child(parent, &o));
}

struct op *
op_disconnect(struct op *parent)
{
	struct op	o;

	bzero(&o, sizeof o);
	o.type	= OP_DISCONNECT;
	return (op_add_child(parent, &o));
}

struct op *
op_starttls(struct op *parent)
{
	struct op	o;

	bzero(&o, sizeof o);
	o.type	= OP_STARTTLS;
	return (op_add_child(parent, &o));
}

struct op *
op_sleep(struct op *parent, unsigned int ms)
{
	struct op	o;

	bzero(&o, sizeof o);
	o.type	= OP_SLEEP;
	o.u.sleep.ms = ms;
	return (op_add_child(parent, &o));
}

struct op *
op_write(struct op *parent, const void *buf, size_t len)
{
	struct op	o;

	bzero(&o, sizeof o);
	o.type	= OP_WRITE;
	o.u.write.buf = buf;
	o.u.write.len = len;
	return (op_add_child(parent, &o));
}

struct op *
op_printf(struct op *parent, const char *fmt, ...)
{
	va_list		 ap;
	char		*buf;
	int		 len;

	va_start(ap, fmt);
	if ((len = vasprintf(&buf, fmt, ap)) == -1)
		err(1, "vasprintf");
	va_end(ap);

	return op_write(parent, buf, len);
}

struct op *
op_expect_disconnect(struct op *parent)
{
	struct op	o;

	bzero(&o, sizeof o);
	o.type	= OP_EXPECT_DISCONNECT;
	return (op_add_child(parent, &o));
}

struct op *
op_expect_smtp_response(struct op *parent, int flags)
{
	struct op	o;

	bzero(&o, sizeof o);
	o.type	= OP_EXPECT_SMTP_RESPONSE;
	o.u.expect_smtp.flags = flags;
	return (op_add_child(parent, &o));
}

static void
usage(void)
{
	extern const char *__progname;
	errx(1, "usage: [-rv] [-d delay] %s script", __progname);
}

int
main(int argc, char **argv)
{
	struct script		*s;
	struct procedure	*p;
	int			 ch;

	while ((ch = getopt(argc, argv, "d:rv")) != -1) {
		switch(ch) {
		case 'v':
			verbose += 1;
			break;
		case 'd':
			rundelay = atoi(optarg) * 1000;
			break;
		case 'r':
			randomdelay = 1;
			break;
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (argc != 1)
		usage();

	s = parse_script(argv[0]);
	if (s == NULL)
		errx(1, "error reading script file");

	_op_connect = op_connect(NULL, "127.0.0.1", 25);

	TAILQ_FOREACH(p, &s->procs, entry)
		if (p->flags & PROC_TESTCASE)
			run_testcase(p);

	printf("===> all run\n");
	printf("passed: %zu/%zu (skipped: %zu, failed: %zu, error: %zu)\n",
		test_pass,
		test_pass + test_skip + test_fail + test_error,
		test_skip,
		test_fail,
		test_error);

	return (0);
}

static struct op *
op_add_child(struct op *parent, const struct op *op)
{
	struct op	*n;

	n = malloc(sizeof *n);
	if (n == NULL)
		err(1, "malloc");

	memmove(n, op, sizeof *n);
	n->next = NULL;

	/* printf("op:%p type:%i parent: %p\n", n, n->type, parent); */

	if (parent) {
		if (parent->u.block.start == NULL)
			parent->u.block.start = n;
		if (parent->u.block.last)
			parent->u.block.last->next = n;
		parent->u.block.last = n;
		parent->u.block.count += 1;
	}

	return (n);
}

static void
run_testcase(struct procedure *proc)
{
	char		 buf[256];
	struct ctx	 c;
	uint32_t	 rdelay;

	bzero(&c, sizeof c);
	c.sock = -1;
	c.lvl = 1;

	if (rundelay) {
		if (randomdelay)
456
			rdelay = chacha_uniform(rundelay);
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
		else
			rdelay = rundelay;
		usleep(rdelay);
	}
	snprintf(buf, sizeof buf,
	    "===> running test-case \"%s\" ", proc->name);
	printf("%s", buf);
	fflush(stdout);

	if (proc->flags & PROC_SKIP) {
		if (verbose > 1)
			printf(": skip\n\n");
		else
			printf("skip\n");
		test_skip += 1;
		return;
	}

	if (verbose > 1)
		printf("\n");

	if (!(proc->flags & PROC_NOCONNECT))
		process_op(&c, _op_connect);
	process_op(&c, proc->root);

	if (c.sock != -1)
		close(c.sock);
	if (c.ssl)
		ssl_close(c.ssl);
	iobuf_clear(&c.iobuf);

	if (verbose > 1) {
		printf("===> done with test-case \"%s\": ", proc->name);
	}

	switch (c.result) {
	case RES_OK:
		if (proc->flags & PROC_EXPECTFAIL) {
			printf("ok (failed)\n");
			fprintf(stderr, "*** FAIL: should have failed\n");
			test_fail += 1;
		} else {
			printf("ok\n");
			test_pass += 1;
		}
		break;

	case RES_SKIP:
		printf("skip\n");
		test_skip += 1;
		break;

	case RES_FAIL:
		if (proc->flags & PROC_EXPECTFAIL) {
			printf("fail (expected)\n");
			test_pass += 1;
		} else {
			printf("fail\n");
			fprintf(stderr, "*** FAIL: %s\n", c.reason);
			test_fail += 1;
		}
		break;

	case RES_ERROR:
		printf("error\n");
		fprintf(stderr, " *** ERROR: %s\n", c.reason);
		test_error += 1;
		break;
	}

	if (verbose > 1) {
		printf("\n");
	}

}

static size_t
strvisx2(char *dst, const char *src, size_t srclen, int flag)
{
	size_t n, r, i;

	n = strvisx(dst, src, srclen, flag);
	if (n == 0)
		return (0);

	r = n;
	for (i = n - 1; i; i--) {
		if (dst[i] == '\r') {
			memmove(dst + i + 2, dst + i + 1, n + 1 - i);
			dst[i+1] = 'r';
			dst[i] = '\\';
			r++;
		} else if (dst[i] == '"') {
			memmove(dst + i + 2, dst + i + 1, n + 1 - i);
			dst[i+1] = '"';
			dst[i] = '\\';
			r++;
		}
	}

	return (r);
}

static const char *
show_data(const char *src, size_t len, size_t max)
{
	static char	buf[8192 + 3];
	char		tmp[256];
	size_t		l, n;

	l = len;
	if (len > 2048)
		l = 2048;

	buf[0] = '"';
	n = strvisx2(&buf[1], src, l, VIS_SAFE | VIS_NL | VIS_TAB | VIS_CSTYLE);
	if (n >= max) {
		snprintf(tmp, sizeof tmp, "...\" [%zu]", l);
		buf[max - strlen(tmp)] = '\0';
		strlcat(buf, tmp, sizeof(buf));
	} else {
		strlcat(buf, "\"", sizeof(buf));
	}

	return (buf);
}

static void
print_op(struct op *op, int lvl)
{


	if (op->type == OP_BLOCK)
		return;

	while (lvl--)
		printf("  ");

	switch(op->type) {

	case OP_REPEAT:
		printf("=> repeat: %i\n", op->u.repeat.count);
		break;

	case OP_RANDOM:
		printf("=> random: %i\n", op->u.random.block->u.block.count);
		break;

	case OP_NOOP:
		printf("=> noop\n");
		break;

	case OP_FAIL:
		printf("=> fail: %s\n", op->u.fail.reason);
		break;

	case OP_CALL:
		printf("=> call: %s\n", op->u.call.proc->name);
		break;
	
	case OP_CONNECT:
		printf("=> connect %s:%i\n",
		    op->u.connect.hostname,
		    op->u.connect.portno);
		break;

	case OP_DISCONNECT:
		printf("=> disconnect\n");
		break;

	case OP_STARTTLS:
		printf("=> starttls\n");
		break;

	case OP_SLEEP:
		printf("=> sleep %ims\n", op->u.sleep.ms);
		break;

	case OP_WRITE:
		printf("=> write %s\n",
		    show_data(op->u.write.buf, op->u.write.len, 70));
		break;

	case OP_EXPECT_DISCONNECT:
		printf("<= disconnect\n");
		break;

	case OP_EXPECT_SMTP_RESPONSE:
		printf("<= smtp-response 0x%04x\n", op->u.expect_smtp.flags);
		break;

	default:
		printf("<> ??? %i;\n", op->type);
		break;
	}
}


static void
set_failure(struct ctx *ctx, int res, const char *fmt, ...)
{
	va_list		 ap;
	int		 len;

	ctx->result = res;
	va_start(ap, fmt);
	if ((len = vasprintf(&ctx->reason, fmt, ap)) == -1)
		err(1, "vasprintf");
	va_end(ap);
}

static void
process_op(struct ctx *ctx, struct op *op)
{
	struct addrinfo	 hints, *a, *ai;
	struct op	*o;
	struct iobuf	*iobuf;
	int		 i, r, s, save_errno, cont;
	const char	*cause;
	char		 buf[16], *servname, *line;
	ssize_t		 n;
	size_t		 len;
	const char	*e;

	if (verbose > 1)
		print_op(op, ctx->lvl);

	iobuf = &ctx->iobuf;

	switch(op->type) {

	case OP_BLOCK:
		ctx->lvl += 1;
		for (o = op->u.block.start; o; o = o->next) {
			process_op(ctx, o);
			if (ctx->result)
				break;
		}
		ctx->lvl -= 1;
		break;

	case OP_REPEAT:
		ctx->lvl += 1;
		for (i = 0; i < op->u.repeat.count; i++) {
			process_op(ctx, op->u.repeat.op);
			if (ctx->result)
				break;
		}
		ctx->lvl -= 1;
		break;

	case OP_RANDOM:

		if (op->u.random.block->u.block.count == 0)
			return;

		ctx->lvl += 1;

715
		i = chacha_uniform(op->u.random.block->u.block.count);
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
		for (o = op->u.random.block->u.block.start; i; i--, o = o->next)
			;
		process_op(ctx, o);
		if (ctx->result)
			break;
		ctx->lvl -= 1;
		break;

	case OP_NOOP:
		break;

	case OP_FAIL:
		set_failure(ctx, RES_FAIL, op->u.fail.reason);
		break;

	case OP_CALL:
		process_op(ctx, op->u.call.proc->root);
		break;

	case OP_CONNECT:
		if (ctx->sock != -1)
			close(ctx->sock);
		ctx->sock = -1;
		iobuf_clear(iobuf);

		servname = NULL;
		if (op->u.connect.portno) {
			snprintf(buf, sizeof buf, "%i", op->u.connect.portno);
			servname = buf;
		}
		bzero(&hints, sizeof hints);
		hints.ai_socktype = SOCK_STREAM;
		hints.ai_protocol = IPPROTO_TCP;
		r = getaddrinfo(op->u.connect.hostname, servname, &hints, &ai);
		if (r) {
			set_failure(ctx, RES_ERROR,
			    "failed to connect to %s:%s: %s",
			    op->u.connect.hostname, servname, gai_strerror(r));
			return;
		}

		s = -1;
		for(a = ai; a; a = a->ai_next) {
			s = socket(a->ai_family, a->ai_socktype, a->ai_protocol);
			if (s == -1) {
				cause = "socket";
				continue;
			}
			if (connect(s, a->ai_addr, a->ai_addrlen) == -1) {
				cause = "connect";
				save_errno = errno;
				close(s);
				errno = save_errno;
				s = -1;
				continue;
			}
			break;  /* okay we got one */
		}
		freeaddrinfo(ai);
		if (s == -1) {
			set_failure(ctx, RES_ERROR,
			    "failed to connect to %s:%s: %s",
			    op->u.connect.hostname, servname, cause);
		} else {
			ctx->sock = s;
			iobuf_init(iobuf, 0, 0);
		}
		break;

	case OP_DISCONNECT:
		if (ctx->sock != -1)
			close(ctx->sock);
		ctx->sock = -1;
		iobuf_clear(iobuf);
		break;

	case OP_STARTTLS:
		if (ctx->ssl)
			set_failure(ctx, RES_ERROR, "SSL context already here");
		else if ((ctx->ssl = ssl_connect(ctx->sock)) == NULL)
			set_failure(ctx, RES_ERROR, "SSL connection failed");
		break;

	case OP_SLEEP:
		usleep(op->u.sleep.ms * 1000);
		break;

	case OP_WRITE:
		iobuf_queue(iobuf, op->u.write.buf, op->u.write.len);
		if (ctx->ssl)
			r = iobuf_flush_ssl(iobuf, ctx->ssl);
		else
			r = iobuf_flush(iobuf, ctx->sock);
		switch (r) {
		case 0:
			break;
		case IOBUF_CLOSED:
			set_failure(ctx, RES_FAIL, "connection closed");
			break;
		case IOBUF_WANT_WRITE:
			set_failure(ctx, RES_ERROR, "iobuf_write(): WANT_WRITE");
			break;
		case IOBUF_ERROR:
			set_failure(ctx, RES_ERROR, "IO error");
			break;
		case IOBUF_SSLERROR:
			set_failure(ctx, RES_ERROR, "SSL error");
			break;
		default:
			set_failure(ctx, RES_ERROR, "iobuf_write(): bad value");
			break;
		}
		break;

	case OP_EXPECT_DISCONNECT:
		if (iobuf_len(iobuf)) {
			set_failure(ctx, RES_ERROR, "%zu bytes of input left",
			    iobuf_len(iobuf));
			break;
		}
		if (ctx->ssl)
			n = iobuf_read_ssl(iobuf, ctx->ssl);
		else
			n = iobuf_read(iobuf, ctx->sock);
		switch (n) {
		case IOBUF_CLOSED:
			close(ctx->sock);
			ctx->sock = -1;
			if (ctx->ssl)
				ssl_close(ctx->ssl);
			break;
		case IOBUF_WANT_READ:
			set_failure(ctx, RES_ERROR, "iobuf_read(): WANT_READ");
			break;
		case IOBUF_ERROR:
			set_failure(ctx, RES_ERROR, "IO error");
			break;
		case IOBUF_SSLERROR:
			set_failure(ctx, RES_ERROR, "SSL error");
			break;
		default:
			set_failure(ctx, RES_FAIL, "data read: %s",
			    show_data(iobuf_data(iobuf), iobuf_len(iobuf), 70));
			break;
		}
		break;

	case OP_EXPECT_SMTP_RESPONSE:
		line = NULL;
		while (1) {
			line = iobuf_getline(iobuf, &len);
			if (line) {
				e = parse_smtp_response(line, len, NULL, &cont);
				if (e) {
					set_failure(ctx, RES_FAIL, e);
					return;
				}
				if (!cont) {
					iobuf_normalize(iobuf);
					break;
				}
				if (!(op->u.expect_smtp.flags
				    & RESP_SMTP_MULTILINE)) {
					set_failure(ctx, RES_FAIL,
					   "single line response expected");
					return;
				}
				continue;
			}

			if (iobuf_len(iobuf) >= SMTP_LINE_MAX) {
				set_failure(ctx, RES_FAIL, "line too long");
				return;
			}

			iobuf_normalize(iobuf);

		    again:
			if (ctx->ssl)
				n = iobuf_read_ssl(iobuf, ctx->ssl);
			else
				n = iobuf_read(iobuf, ctx->sock);
			switch (n) {
			case IOBUF_CLOSED:
				set_failure(ctx, RES_FAIL, "connection closed");
				return;
			case IOBUF_WANT_READ:
				goto again;
			case IOBUF_ERROR:
				set_failure(ctx, RES_ERROR, "io error");
				return;
			case IOBUF_SSLERROR:
				set_failure(ctx, RES_ERROR, "SSL error");
				return;
			default:
				break;
			}
		}

		/* got our response */

		if (verbose > 1) {
			len = ctx->lvl;
			while (len--)
				printf("  ");
			printf("   >>> %s\n", show_data(line, strlen(line), 70));
		}

		switch (line[0]) {
		case '2':
		case '3':
			if (!(op->u.expect_smtp.flags & RESP_SMTP_OK))
				set_failure(ctx, RES_FAIL,
				    "unexpected response code0: %s", line);
			break;
		case '4':
			if (!(op->u.expect_smtp.flags & RESP_SMTP_TEMPFAIL))
				set_failure(ctx, RES_FAIL,
				    "unexpected response code1: %s", line);
			break;
		case '5':
			if (!(op->u.expect_smtp.flags & RESP_SMTP_PERMFAIL))
				set_failure(ctx, RES_FAIL,
				    "unexpected response code2: %s", line);
			break;
		default:
			set_failure(ctx, RES_FAIL,
				    "unexpected response code???: %s", line);
			break;
		}
		break;

	default:
		ctx->result = RES_ERROR;
		ctx->reason = "invalid operator";
		break;
	}
}

static const char *
parse_smtp_response(char *line, size_t len, char **msg, int *cont)
{
	size_t	 i;

	if (len >= SMTP_LINE_MAX)
		return "line too long";

	if (len > 3) {
		if (msg)
			*msg = line + 4;
		if (cont)
			*cont = (line[3] == '-');
	} else if (len == 3) {
		if (msg)
			*msg = line + 3;
		if (cont)
			*cont = 0;
	} else
		return "line too short";

	/* validate reply code */
	if (line[0] < '2' || line[0] > '5' || !isdigit(line[1]) ||
	    !isdigit(line[2]))
		return "reply code out of range";

	/* validate reply message */
	for (i = 0; i < len; i++)
		if (!isprint(line[i]))
			return "non-printable character in reply";

	return NULL;
}