Commit ffa10187 authored by Andy Polyakov's avatar Andy Polyakov
Browse files

Eliminate dependency on read/write/stat in apps under _WIN32.

parent 4d24b4c4
Loading
Loading
Loading
Loading
+89 −0
Original line number Diff line number Diff line
@@ -130,6 +130,14 @@
#endif
#include <openssl/bn.h>

#ifdef _WIN32
#include <windows.h>
#ifdef fileno
#undef fileno
#define fileno(a) (int)_fileno(a)
#endif
#endif

#define NON_MAIN
#include "apps.h"
#undef NON_MAIN
@@ -773,7 +781,9 @@ X509 *load_cert(BIO *err, const char *file, int format,

	if (file == NULL)
		{
#ifdef _IONBF
		setvbuf(stdin, NULL, _IONBF, 0);
#endif
		BIO_set_fp(cert,stdin,BIO_NOCLOSE);
		}
	else
@@ -865,7 +875,9 @@ EVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin,
		}
	if (file == NULL && maybe_stdin)
		{
#ifdef _IONBF
		setvbuf(stdin, NULL, _IONBF, 0);
#endif
		BIO_set_fp(key,stdin,BIO_NOCLOSE);
		}
	else
@@ -947,7 +959,9 @@ EVP_PKEY *load_pubkey(BIO *err, const char *file, int format, int maybe_stdin,
		}
	if (file == NULL && maybe_stdin)
		{
#ifdef _IONBF
		setvbuf(stdin, NULL, _IONBF, 0);
#endif
		BIO_set_fp(key,stdin,BIO_NOCLOSE);
		}
	else
@@ -2358,3 +2372,78 @@ void policies_print(BIO *out, X509_STORE_CTX *ctx)
	if (free_out)
		BIO_free(out);
	}

#if defined(_WIN32)
int app_isdir(const char *name)
	{
	HANDLE		hList;
	WIN32_FIND_DATA	FileData;
#if defined(UNICODE) || defined(_UNICODE)
	size_t i, len_0 = strlen(name)+1;

	if (len_0 > sizeof(FileData.cFileName)/sizeof(FileData.cFileName[0]))
		return -1;

#if !defined(_WIN32_WCE) || _WIN32_WCE>=101
	if (!MultiByteToWideChar(CP_ACP,0,name,len_0,FileData.cFileName,len_0))
#endif
		for (i=0;i<len_0;i++)
			FileData.cFileName[i] = (WCHAR)name[i];

	hList = FindFirstFile(FileData.cFileName,&FileData);
#else
	hList = FindFirstFile(name,&FileData);
#endif
	if (hList == INVALID_HANDLE_VALUE)	return -1;
	FindClose(hList);
	return ((FileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)!=0);
	}
#else
#include <sys/stat.h>
#ifndef S_ISDIR
# if defined(_S_IFMT) && defined(_S_IFDIR)
#  define S_ISDIR(a)   (((a) & _S_IFMT) == _S_IFDIR)
# else 
#  define S_ISDIR(a)   (((a) & S_IFMT) == S_IFDIR)
# endif 
#endif 

int app_isdir(const char *name)
	{
#if defined(S_ISDIR)
	struct stat st;

	if (stat(name,&st)==0)	return S_ISDIR(st.st_mode);
	else			return -1;
#else
	return -1;
#endif
	}
#endif

#if defined(_WIN32) && defined(STD_INPUT_HANDLE)
int raw_read_stdin(void *buf,int siz)
	{
	DWORD n;
	if (ReadFile(GetStdHandle(STD_INPUT_HANDLE),buf,siz,&n,NULL))
		return (n);
	else	return (-1);
	}
#else
int raw_read_stdin(void *buf,int siz)
	{	return read(fileno(stdin),buf,siz);	}
#endif

#if defined(_WIN32) && defined(STD_OUTPUT_HANDLE)
int raw_write_stdout(void *buf,int siz)
	{
	DWORD n;
	if (WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),buf,siz,&n,NULL))
		return (n);
	else	return (-1);
	}
#else
int raw_write_stdout(const void *buf,int siz)
	{	return write(fileno(stdout),buf,siz);	}
#endif
+3 −0
Original line number Diff line number Diff line
@@ -316,4 +316,7 @@ void policies_print(BIO *out, X509_STORE_CTX *ctx);

#define SERIAL_RAND_BITS	64

int app_isdir(const char *);
int raw_read_stdin(void *,int);
int raw_write_stdout(const void *,int);
#endif
+1 −11
Original line number Diff line number Diff line
@@ -63,7 +63,6 @@
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <openssl/conf.h>
#include <openssl/bio.h>
#include <openssl/err.h>
@@ -826,7 +825,6 @@ bad:
	/* lookup where to write new certificates */
	if ((outdir == NULL) && (req))
		{
		struct stat sb;

		if ((outdir=NCONF_get_string(conf,section,ENV_NEW_CERTS_DIR))
			== NULL)
@@ -852,20 +850,12 @@ bad:
			goto err;
			}

		if (stat(outdir,&sb) != 0)
			{
			BIO_printf(bio_err,"unable to stat(%s)\n",outdir);
			perror(outdir);
			goto err;
			}
#ifdef S_IFDIR
		if (!(sb.st_mode & S_IFDIR))
		if (app_isdir(outdir)<=0)
			{
			BIO_printf(bio_err,"%s need to be a directory\n",outdir);
			perror(outdir);
			goto err;
			}
#endif
#endif
		}

+0 −8
Original line number Diff line number Diff line
@@ -63,7 +63,6 @@
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "apps.h"
#include <openssl/err.h>
#include <openssl/evp.h>
@@ -295,19 +294,12 @@ end:
 */
static int add_certs_from_file(STACK_OF(X509) *stack, char *certfile)
	{
	struct stat st;
	BIO *in=NULL;
	int count=0;
	int ret= -1;
	STACK_OF(X509_INFO) *sk=NULL;
	X509_INFO *xi;

	if ((stat(certfile,&st) != 0))
		{
		BIO_printf(bio_err,"unable to load the file, %s\n",certfile);
		goto end;
		}

	in=BIO_new(BIO_s_file());
	if ((in == NULL) || (BIO_read_filename(in,certfile) <= 0))
		{
+4 −13
Original line number Diff line number Diff line
@@ -137,15 +137,6 @@ typedef unsigned int u_int;
#include "s_apps.h"
#include "timeouts.h"

#ifdef OPENSSL_SYS_WINCE
/* Windows CE incorrectly defines fileno as returning void*, so to avoid problems below... */
#ifdef fileno
#undef fileno
#endif
#define fileno(a) (int)_fileno(a)
#endif


#if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
/* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
#undef FIONBIO
@@ -926,7 +917,7 @@ re_start:
#ifdef CHARSET_EBCDIC
			ascii2ebcdic(&(sbuf[sbuf_off]),&(sbuf[sbuf_off]),sbuf_len);
#endif
			i=write(fileno(stdout),&(sbuf[sbuf_off]),sbuf_len);
			i=raw_write_stdout(&(sbuf[sbuf_off]),sbuf_len);

			if (i <= 0)
				{
@@ -1013,7 +1004,7 @@ printf("read=%d pending=%d peek=%d\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240
				{
				int j, lf_num;

				i=read(fileno(stdin),cbuf,BUFSIZZ/2);
				i=raw_read_stdin(cbuf,BUFSIZZ/2);
				lf_num = 0;
				/* both loops are skipped when i <= 0 */
				for (j = 0; j < i; j++)
@@ -1032,7 +1023,7 @@ printf("read=%d pending=%d peek=%d\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240
				assert(lf_num == 0);
				}
			else
				i=read(fileno(stdin),cbuf,BUFSIZZ);
				i=raw_read_stdin(cbuf,BUFSIZZ);

			if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q')))
				{
Loading