Bugfix: avoid opening CAfile when it's NULL.
[openssl.git] / apps / s_server.c
index a78a4bcd7b02dbb5191a4e69ac3cc7c4dcf3a89d..e54593d5514b17f89002715aec6ef03d22a77278 100644 (file)
@@ -56,9 +56,7 @@
  * [including the GNU Public Licence.]
  */
 
-#ifdef APPS_CRLF
-# include <assert.h>
-#endif
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -87,6 +85,10 @@ typedef unsigned int u_int;
 #include <openssl/ssl.h>
 #include "s_apps.h"
 
+#ifdef WINDOWS
+#include <conio.h>
+#endif
+
 #if (defined(VMS) && __VMS_VER < 70000000)
 /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
 #undef FIONBIO
@@ -109,7 +111,9 @@ static void print_stats(BIO *bp,SSL_CTX *ctx);
 static DH *load_dh_param(void );
 static DH *get_dh512(void);
 #endif
-/* static void s_server_init(void);*/
+#ifdef MONOLITH
+static void s_server_init(void);
+#endif
 
 #ifndef S_ISDIR
 # if defined(_S_IFMT) && defined(_S_IFDIR)
@@ -169,9 +173,7 @@ static char *s_dcert_file=NULL,*s_dkey_file=NULL;
 static int s_nbio=0;
 #endif
 static int s_nbio_test=0;
-#ifdef APPS_CRLF
-       int s_crlf=0;
-#endif
+int s_crlf=0;
 static SSL_CTX *ctx=NULL;
 static int www=0;
 
@@ -179,9 +181,12 @@ static BIO *bio_s_out=NULL;
 static int s_debug=0;
 static int s_quiet=0;
 
-#if 0
+static int hack=0;
+
+#ifdef MONOLITH
 static void s_server_init(void)
        {
+       accept_socket=-1;
        cipher=NULL;
        s_server_verify=SSL_VERIFY_NONE;
        s_dcert_file=NULL;
@@ -198,6 +203,7 @@ static void s_server_init(void)
        bio_s_out=NULL;
        s_debug=0;
        s_quiet=0;
+       hack=0;
        }
 #endif
 
@@ -219,9 +225,7 @@ static void sv_usage(void)
        BIO_printf(bio_err," -nbio         - Run with non-blocking IO\n");
 #endif
        BIO_printf(bio_err," -nbio_test    - test with the non-blocking test bio\n");
-#ifdef APPS_CRLF
        BIO_printf(bio_err," -crlf         - convert LF from terminal into CRLF\n");
-#endif
        BIO_printf(bio_err," -debug        - Print more output\n");
        BIO_printf(bio_err," -state        - Print the SSL states\n");
        BIO_printf(bio_err," -CApath arg   - PEM format directory of CA's\n");
@@ -246,7 +250,6 @@ static void sv_usage(void)
 
 static int local_argc=0;
 static char **local_argv;
-static int hack=0;
 
 #ifdef CHARSET_EBCDIC
 static int ebcdic_new(BIO *bi);
@@ -425,8 +428,9 @@ int MAIN(int argc, char *argv[])
        local_argv=argv;
 
        apps_startup();
-       s_quiet=0;
-       s_debug=0;
+#ifdef MONOLITH
+       s_server_init();
+#endif
 
        if (bio_err == NULL)
                bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
@@ -525,10 +529,8 @@ int MAIN(int argc, char *argv[])
                        { hack=1; }
                else if (strcmp(*argv,"-state") == 0)
                        { state=1; }
-#ifdef APPS_CRLF
                else if (strcmp(*argv,"-crlf") == 0)
                        { s_crlf=1; }
-#endif
                else if (strcmp(*argv,"-quiet") == 0)
                        { s_quiet=1; }
                else if (strcmp(*argv,"-bugs") == 0)
@@ -697,7 +699,8 @@ bad:
        SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context,
                sizeof s_server_session_id_context);
 
-       SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(CAfile));
+       if (CAfile != NULL)
+           SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(CAfile));
 
        BIO_printf(bio_s_out,"ACCEPT\n");
        if (www)
@@ -750,6 +753,9 @@ static int sv_body(char *hostname, int s, unsigned char *context)
        unsigned long l;
        SSL *con=NULL;
        BIO *sbio;
+#ifdef WINDOWS
+       struct timeval tv;
+#endif
 
        if ((buf=Malloc(bufsize)) == NULL)
                {
@@ -809,11 +815,23 @@ static int sv_body(char *hostname, int s, unsigned char *context)
                 * the compiler: if you do have a cast then you can either
                 * go for (int *) or (void *).
                 */
-               i=select(width,(void *)&readfds,NULL,NULL,NULL);
-               if (i <= 0) continue;
-               if (FD_ISSET(fileno(stdin),&readfds))
+#ifdef WINDOWS
+               /* Under Windows we can't select on stdin: only
+                * on sockets. As a workaround we timeout the select every
+                * second and check for any keypress. In a proper Windows
+                * application we wouldn't do this because it is inefficient.
+                */
+               tv.tv_sec = 1;
+               tv.tv_usec = 0;
+               i=select(width,(void *)&readfds,NULL,NULL,&tv);
+               if((i < 0) || (!i && !_kbhit() ) )continue;
+               if(_kbhit())
+#else
+               i=select(width,(void *)&readfds,NULL,NULL,NULL);
+               if (i <= 0) continue;
+               if (FD_ISSET(fileno(stdin),&readfds))
+#endif
                        {
-#ifdef APPS_CRLF
                        if (s_crlf)
                                {
                                int j, lf_num;
@@ -837,7 +855,6 @@ static int sv_body(char *hostname, int s, unsigned char *context)
                                assert(lf_num == 0);
                                }
                        else
-#endif
                                i=read(fileno(stdin),buf,bufsize);
                        if (!s_quiet)
                                {