+typedef struct {
+ int sock;
+ int tls_active;
+#ifdef HAVE_OPENSSL
+ SSL_CTX * ctx;
+ SSL * ssl;
+#endif
+ int sent_starttls;
+} srv_ctx;
+
+static void
+do_file(srv_ctx * srv, FILE * f, int timeout,
+ unsigned char * inbuffer, unsigned bsiz, unsigned char * inptr)
+{
+unsigned char outbuffer[1024 * 20];
+
+while (fgets(CS outbuffer, sizeof(outbuffer), f) != NULL)
+ {
+ int n = (int)strlen(CS outbuffer);
+ int crlf = 1;
+ int rc;
+
+ /* Strip trailing newline */
+ if (outbuffer[n-1] == '\n') outbuffer[--n] = 0;
+
+ /* Expect incoming */
+
+ if ( strncmp(CS outbuffer, "???", 3) == 0
+ && (outbuffer[3] == ' ' || outbuffer[3] == '*' || outbuffer[3] == '?')
+ )
+ {
+ unsigned char *lineptr;
+ unsigned exp_eof = outbuffer[3] == '*';
+ unsigned resp_optional = outbuffer[3] == '?';
+
+ printf("%s\n", outbuffer);
+ n = unescape_buf(outbuffer, n);
+
+nextinput:
+ if (*inptr == 0) /* Refill input buffer */
+ {
+ alarm(timeout);
+ unsigned char *inbufferp = inbuffer;
+ for (;;) {
+ if (srv->tls_active)
+ {
+#ifdef HAVE_OPENSSL
+ int error;
+ DEBUG { printf("call SSL_read\n"); fflush(stdout); }
+ rc = SSL_read(srv->ssl, inbufferp, bsiz - (inbufferp - inbuffer) - 1);
+ DEBUG { printf("SSL_read: %d\n", rc); fflush(stdout); }
+ if (rc <= 0)
+ switch (error = SSL_get_error(srv->ssl, rc))
+ {
+ case SSL_ERROR_ZERO_RETURN:
+ break;
+ case SSL_ERROR_SYSCALL:
+ printf("%s\n", ERR_error_string(ERR_get_error(), NULL));
+ rc = -1;
+ break;
+ case SSL_ERROR_SSL:
+ printf("%s\nTLS terminated\n", ERR_error_string(ERR_get_error(), NULL));
+ SSL_shutdown(srv->ssl);
+ SSL_free(srv->ssl);
+ srv->tls_active = FALSE;
+ { /* OpenSSL leaves it in restartsys mode */
+ struct sigaction act = {.sa_handler = sigalrm_handler_flag, .sa_flags = 0};
+ sigalrm_seen = 1;
+ sigaction(SIGALRM, &act, NULL);
+ }
+ *inptr = 0;
+ DEBUG { printf("go round\n"); fflush(stdout); }
+ goto nextinput;
+ default:
+ printf("SSL error code %d\n", error);
+ }
+#endif
+#ifdef HAVE_GNUTLS
+ retry1:
+ DEBUG { printf("call gnutls_record_recv\n"); fflush(stdout); }
+ rc = gnutls_record_recv(tls_session, CS inbufferp, bsiz - (inbufferp - inbuffer) - 1);
+ if (rc < 0)
+ {
+ DEBUG { printf("gnutls_record_recv: %s\n", gnutls_strerror(rc)); fflush(stdout); }
+ if (rc == GNUTLS_E_INTERRUPTED || rc == GNUTLS_E_AGAIN)
+ goto retry1;
+ printf("%s\n", gnutls_strerror(rc));
+ srv->tls_active = FALSE;
+ *inptr = 0;
+ DEBUG { printf("go round\n"); fflush(stdout); }
+ goto nextinput;
+ }
+ DEBUG { printf("gnutls_record_recv: %d\n", rc); fflush(stdout); }
+#endif
+ }
+ else
+ {
+ DEBUG { printf("call read\n"); fflush(stdout); }
+ rc = read(srv->sock, inbufferp, bsiz - (inbufferp - inbuffer) - 1);
+ DEBUG { printf("read: %d\n", rc); fflush(stdout); }
+ }
+
+ if (rc > 0) inbufferp[rc] = '\0';
+ if (rc <= 0 || strchr(inbufferp, '\n')) break;
+ inbufferp += rc;
+ if (inbufferp >= inbuffer + bsiz) {
+ printf("Input buffer overrun, need more than %d bytes input buffer\n", bsiz);
+ exit(73);
+ }
+ DEBUG { printf("read more\n"); }
+ }
+ alarm(0);
+
+ if (rc < 0)
+ {
+ if (errno == EINTR && sigalrm_seen && resp_optional)
+ continue; /* next scriptline */
+ printf("Read error: %s\n", strerror(errno));
+ exit(81);
+ }
+ else if (rc == 0)
+ if (exp_eof)
+ {
+ printf("Expected EOF read\n");
+ continue;
+ }
+ else if (resp_optional)
+ continue; /* next scriptline */
+ else
+ {
+ printf("Unexpected EOF read\n");
+ close(srv->sock);
+ exit(80);
+ }
+ else if (exp_eof)
+ {
+ printf("Expected EOF not read\n");
+ close(srv->sock);
+ exit(74);
+ }
+ else
+ inptr = inbuffer;
+ }
+ DEBUG { printf("read: '%s'\n", inptr); fflush(stdout); }
+
+ lineptr = inptr;
+ while (*inptr != 0 && *inptr != '\r' && *inptr != '\n') inptr++;
+ if (*inptr != 0)
+ {
+ *inptr++ = 0;
+ if (*inptr == '\n') inptr++;
+ }
+
+ if (strncmp(CS lineptr, CS outbuffer + 4, n - 4) != 0)
+ if (resp_optional)
+ {
+ inptr = lineptr; /* consume scriptline, not inputline */
+ continue;
+ }
+ else
+ {
+ printf("<<< %s\n", lineptr);
+ printf("\n******** Input mismatch ********\n");
+ exit(79);
+ }
+
+ /* Input matched script. Output the inputline, unless optional */
+ DEBUG { printf("read matched\n"); fflush(stdout); }
+
+ if (!resp_optional)
+ printf("<<< %s\n", lineptr);
+ else
+
+ /* If there is further input after this line, consume inputline but not
+ scriptline in case there are several matching. Nonmatches are dealt with
+ above. */
+
+ if (*inptr != 0)
+ goto nextinput;
+
+ #ifdef HAVE_TLS
+ if (srv->sent_starttls)
+ {
+ if (lineptr[0] == '2')
+ {
+ unsigned int verify;
+
+ printf("Attempting to start TLS\n");
+ fflush(stdout);
+
+ #ifdef HAVE_OPENSSL
+ srv->tls_active = tls_start(srv->sock, &srv->ssl, srv->ctx);
+ #endif
+
+ #ifdef HAVE_GNUTLS
+ {
+ int rc;
+ fd_set rfd;
+ struct timeval tv = { 0, 2000 };
+
+ sigalrm_seen = FALSE;
+ alarm(timeout);
+ do {
+ rc = gnutls_handshake(tls_session);
+ } while (rc < 0 && gnutls_error_is_fatal(rc) == 0);
+ srv->tls_active = rc >= 0;
+ alarm(0);
+
+ if (!srv->tls_active) printf("%s\n", gnutls_strerror(rc));
+
+ /* look for an error on the TLS conn */
+ FD_ZERO(&rfd);
+ FD_SET(srv->sock, &rfd);
+ if (select(srv->sock+1, &rfd, NULL, NULL, &tv) > 0)
+ {
+ retry2:
+ DEBUG { printf("call gnutls_record_recv\n"); fflush(stdout); }
+ rc = gnutls_record_recv(tls_session, CS inbuffer, bsiz - 1);
+ if (rc < 0)
+ {
+ DEBUG { printf("gnutls_record_recv: %s\n", gnutls_strerror(rc)); fflush(stdout); }
+ if (rc == GNUTLS_E_INTERRUPTED || rc == GNUTLS_E_AGAIN)
+ goto retry2;
+ printf("%s\n", gnutls_strerror(rc));
+ srv->tls_active = FALSE;
+ }
+ DEBUG { printf("gnutls_record_recv: %d\n", rc); fflush(stdout); }
+ }
+ }
+ #endif
+
+ if (!srv->tls_active)
+ {
+ printf("Failed to start TLS\n");
+ fflush(stdout);
+ }
+ #ifdef HAVE_GNUTLS
+ else if (ocsp_stapling)
+ {
+ if ((rc= gnutls_certificate_verify_peers2(tls_session, &verify)) < 0)
+ {
+ printf("Failed to verify certificate: %s\n", gnutls_strerror(rc));
+ fflush(stdout);
+ }
+ else if (verify & (GNUTLS_CERT_INVALID|GNUTLS_CERT_REVOKED))
+ {
+ printf("Bad certificate\n");
+ fflush(stdout);
+ }
+ #ifdef HAVE_OCSP
+ else if (gnutls_ocsp_status_request_is_checked(tls_session, 0) == 0)
+ {
+ printf("Failed to verify certificate status\n");
+ {
+ gnutls_datum_t stapling;
+ gnutls_ocsp_resp_t resp;
+ gnutls_datum_t printed;
+ if ( (rc= gnutls_ocsp_status_request_get(tls_session, &stapling)) == 0
+ && (rc= gnutls_ocsp_resp_init(&resp)) == 0
+ && (rc= gnutls_ocsp_resp_import(resp, &stapling)) == 0
+ && (rc= gnutls_ocsp_resp_print(resp, GNUTLS_OCSP_PRINT_FULL, &printed)) == 0
+ )
+ {
+ fprintf(stderr, "%.4096s", printed.data);
+ gnutls_free(printed.data);
+ }
+ else
+ (void) fprintf(stderr,"ocsp decode: %s", gnutls_strerror(rc));
+ }
+ fflush(stdout);
+ }
+ #endif
+ }
+ #endif
+ else
+ printf("Succeeded in starting TLS\n");
+ }
+ else printf("Abandoning TLS start attempt\n");
+ }
+ srv->sent_starttls = 0;
+ #endif
+ }
+
+ /* Wait for a bit before proceeding */
+
+ else if (strncmp(CS outbuffer, "+++ ", 4) == 0)
+ {
+ printf("%s\n", outbuffer);
+ sleep(atoi(CS outbuffer + 4));
+ }
+
+ /* Stack new input file */
+
+ else if (strncmp(CS outbuffer, "<<< ", 4) == 0)
+ {
+ FILE * new_f;
+ if (!(new_f = fopen((const char *)outbuffer+4 , "r")))
+ {
+ printf("Unable to open '%s': %s", inptr, strerror(errno));
+ exit(74);
+ }
+ do_file(srv, new_f, timeout, inbuffer, bsiz, inptr);
+ }
+
+
+ /* Send line outgoing, but barf if unconsumed incoming */
+
+ else
+ {
+ unsigned char * out = outbuffer;
+
+ if (strncmp(CS outbuffer, ">>> ", 4) == 0)
+ {
+ crlf = 0;
+ out += 4;
+ n -= 4;
+ }
+
+ if (*inptr != 0)
+ {
+ printf("Unconsumed input: %s", inptr);
+ printf(" About to send: %s\n", out);
+ exit(78);
+ }
+
+ #ifdef HAVE_TLS
+
+ /* Shutdown TLS */
+
+ if (strcmp(CS out, "stoptls") == 0 ||
+ strcmp(CS out, "STOPTLS") == 0)
+ {
+ if (!srv->tls_active)
+ {
+ printf("STOPTLS read when TLS not active\n");
+ exit(77);
+ }
+ printf("Shutting down TLS encryption\n");
+
+ #ifdef HAVE_OPENSSL
+ SSL_shutdown(srv->ssl);
+ SSL_free(srv->ssl);
+ #endif
+
+ #ifdef HAVE_GNUTLS
+ gnutls_bye(tls_session, GNUTLS_SHUT_WR);
+ gnutls_deinit(tls_session);
+ tls_session = NULL;
+ gnutls_global_deinit();
+ #endif
+
+ srv->tls_active = 0;
+ continue;
+ }
+
+ /* Remember that we sent STARTTLS */
+
+ srv->sent_starttls = (strcmp(CS out, "starttls") == 0 ||
+ strcmp(CS out, "STARTTLS") == 0);
+
+ /* Fudge: if the command is "starttls_wait", we send the starttls bit,
+ but we haven't set the flag, so that there is no negotiation. This is for
+ testing the server's timeout. */
+
+ if (strcmp(CS out, "starttls_wait") == 0)
+ {
+ out[8] = 0;
+ n = 8;
+ }
+ #endif
+
+ printf(">>> %s\n", out);
+ if (crlf)
+ {
+ strcpy(CS out + n, "\r\n");
+ n += 2;
+ }
+
+ n = unescape_buf(out, n);
+
+ /* OK, do it */
+
+ alarm(timeout);
+ if (srv->tls_active)
+ {
+ #ifdef HAVE_OPENSSL
+ rc = SSL_write (srv->ssl, out, n);
+ #endif
+ #ifdef HAVE_GNUTLS
+ if ((rc = gnutls_record_send(tls_session, CS out, n)) < 0)
+ {
+ printf("GnuTLS write error: %s\n", gnutls_strerror(rc));
+ exit(76);
+ }
+ #endif
+ }
+ else
+ rc = write(srv->sock, out, n);
+ alarm(0);
+
+ if (rc < 0)
+ {
+ printf("Write error: %s\n", strerror(errno));
+ exit(75);
+ }
+ }
+ }
+}