-while (fgets(CS outbuffer, sizeof(outbuffer), stdin) != NULL)
- {
- int n = (int)strlen(CS outbuffer);
-
- /* Strip trailing newline */
- if (outbuffer[n-1] == '\n') outbuffer[--n] = 0;
-
- /* Expect incoming */
-
- if ( strncmp(CS outbuffer, "???", 3) == 0
- && (outbuffer[3] == ' ' || outbuffer[3] == '*')
- )
- {
- unsigned char *lineptr;
- unsigned exp_eof = outbuffer[3] == '*';
-
- printf("%s\n", outbuffer);
-
- if (*inptr == 0) /* Refill input buffer */
- {
- if (tls_active)
- {
- #ifdef HAVE_OPENSSL
- rc = SSL_read (ssl, inbuffer, sizeof(inbuffer) - 1);
- #endif
- #ifdef HAVE_GNUTLS
- rc = gnutls_record_recv(tls_session, CS inbuffer, sizeof(inbuffer) - 1);
- #endif
- }
- else
- {
- alarm(timeout);
- rc = read(sock, inbuffer, sizeof(inbuffer));
- alarm(0);
- }
-
- if (rc < 0)
- {
- printf("Read error %s\n", strerror(errno));
- exit(81);
- }
- else if (rc == 0)
- if (exp_eof)
- {
- printf("Expected EOF read\n");
- continue;
- }
- else
- {
- printf("Enexpected EOF read\n");
- close(sock);
- exit(80);
- }
- else if (exp_eof)
- {
- printf("Expected EOF not read\n");
- close(sock);
- exit(74);
- }
- else
- {
- inbuffer[rc] = 0;
- inptr = inbuffer;
- }
- }
-
- lineptr = inptr;
- while (*inptr != 0 && *inptr != '\r' && *inptr != '\n') inptr++;
- if (*inptr != 0)
- {
- *inptr++ = 0;
- if (*inptr == '\n') inptr++;
- }
-
- printf("<<< %s\n", lineptr);
- if (strncmp(CS lineptr, CS outbuffer + 4, (int)strlen(CS outbuffer) - 4) != 0)
- {
- printf("\n******** Input mismatch ********\n");
- exit(79);
- }
-
- #ifdef HAVE_TLS
- if (sent_starttls)
- {
- if (lineptr[0] == '2')
- {
-int rc;
- unsigned int verify;
-
- printf("Attempting to start TLS\n");
- fflush(stdout);
-
- #ifdef HAVE_OPENSSL
- tls_active = tls_start(sock, &ssl, ctx);
- #endif
-
- #ifdef HAVE_GNUTLS
- {
- int rc;
- sigalrm_seen = FALSE;
- alarm(timeout);
- do {
- rc = gnutls_handshake(tls_session);
- } while (rc < 0 && gnutls_error_is_fatal(rc) == 0);
- tls_active = rc >= 0;
- alarm(0);
-
- if (!tls_active) printf("%s\n", gnutls_strerror(rc));
- }
- #endif
-
- if (!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");
- }
- 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));
- }
-
- /* Send outgoing, but barf if unconsumed incoming */
-
- else
- {
- unsigned char *escape;
-
- if (*inptr != 0)
- {
- printf("Unconsumed input: %s", inptr);
- printf(" About to send: %s\n", outbuffer);
- exit(78);
- }
-
- #ifdef HAVE_TLS
-
- /* Shutdown TLS */
-
- if (strcmp(CS outbuffer, "stoptls") == 0 ||
- strcmp(CS outbuffer, "STOPTLS") == 0)
- {
- if (!tls_active)
- {
- printf("STOPTLS read when TLS not active\n");
- exit(77);
- }
- printf("Shutting down TLS encryption\n");
-
- #ifdef HAVE_OPENSSL
- SSL_shutdown(ssl);
- SSL_free(ssl);
- #endif
-
- #ifdef HAVE_GNUTLS
- gnutls_bye(tls_session, GNUTLS_SHUT_WR);
- gnutls_deinit(tls_session);
- tls_session = NULL;
- gnutls_global_deinit();
- #endif
-
- tls_active = 0;
- continue;
- }
-
- /* Remember that we sent STARTTLS */
-
- sent_starttls = (strcmp(CS outbuffer, "starttls") == 0 ||
- strcmp(CS outbuffer, "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 outbuffer, "starttls_wait") == 0)
- {
- outbuffer[8] = 0;
- n = 8;
- }
- #endif
-
- printf(">>> %s\n", outbuffer);
- strcpy(CS outbuffer + n, "\r\n");
-
- /* Turn "\n" and "\r" into the relevant characters. This is a hack. */
-
- while ((escape = US strstr(CS outbuffer, "\\r")) != NULL)
- {
- *escape = '\r';
- memmove(escape + 1, escape + 2, (n + 2) - (escape - outbuffer) - 2);
- n--;
- }
-
- while ((escape = US strstr(CS outbuffer, "\\n")) != NULL)
- {
- *escape = '\n';
- memmove(escape + 1, escape + 2, (n + 2) - (escape - outbuffer) - 2);
- n--;
- }
-
- /* OK, do it */
-
- alarm(timeout);
- if (tls_active)
- {
- #ifdef HAVE_OPENSSL
- rc = SSL_write (ssl, outbuffer, n + 2);
- #endif
- #ifdef HAVE_GNUTLS
- rc = gnutls_record_send(tls_session, CS outbuffer, n + 2);
- if (rc < 0)
- {
- printf("GnuTLS write error: %s\n", gnutls_strerror(rc));
- exit(76);
- }
- #endif
- }
- else
- {
- rc = write(sock, outbuffer, n + 2);
- }
- alarm(0);
-
- if (rc < 0)
- {
- printf("Write error: %s\n", strerror(errno));
- exit(75);
- }
- }
- }