Translate all file descriptors received from assuan.

This commit is contained in:
Werner Koch 2007-07-12 15:28:30 +00:00
parent b188c246ca
commit 5f97dd2c44
9 changed files with 86 additions and 37 deletions

View File

@ -1,3 +1,9 @@
2007-07-12 Werner Koch <wk@g10code.com>
* sysutils.h (gnupg_fd_t): New.
* sysutils.c (translate_sys2libc_fd): Use that type instead of int.
(translate_sys2libc_fd_int): New.
2007-07-09 Werner Koch <wk@g10code.com>
* t-gettime.c (test_isotime2epoch): Use time_t and not u32.

View File

@ -278,24 +278,36 @@ gnupg_sleep (unsigned int seconds)
translates system file handles to libc file handles. FOR_WRITE
gives the direction of the handle. */
int
translate_sys2libc_fd (int fd, int for_write)
translate_sys2libc_fd (gnupg_fd_t fd, int for_write)
{
#ifdef HAVE_W32_SYSTEM
int x;
if (fd <= 2)
return fd; /* Do not do this for error, stdin, stdout, stderr.
(This also ignores an fd of -1.) */
x = _open_osfhandle (fd, for_write ? 1 : 0);
if (fd == GNUPG_INVALID_FD)
return -1;
/* Note that _open_osfhandle is currently defined to take and return
a long. */
x = _open_osfhandle ((long)fd, for_write ? 1 : 0);
if (x == -1)
log_error ("failed to translate osfhandle %p\n", (void *) fd);
else
{
/* log_info ("_open_osfhandle %p yields %d%s\n", */
/* (void*)fd, x, for_write? " for writing":"" ); */
fd = x;
}
#endif /* HAVE_W32_SYSTEM */
return x;
#else /*!HAVE_W32_SYSTEM */
return fd;
#endif
}
/* This is the same as translate_sys2libc_fd but takes an integer
which is assumet to be such an system handle. */
int
translate_sys2libc_fd_int (int fd, int for_write)
{
#ifdef HAVE_W32_SYSTEM
if (fd <= 2)
return fd; /* Do not do this for error, stdin, stdout, stderr. */
return translate_sys2libc_fd ((void*)fd, for_write);
#else
return fd;
#endif
}

View File

@ -20,13 +20,28 @@
#ifndef GNUPG_COMMON_SYSUTILS_H
#define GNUPG_COMMON_SYSUTILS_H
/* Because we use system handles and not libc low level file
descriptors on W32, we need to declare them as HANDLE (which
actually is a plain pointer). This is required to eventually
support 64 bits Windows systems. */
#ifdef HAVE_W32_SYSTEM
typedef void *gnupg_fd_t;
#define GNUPG_INVALID_FD ((void*)(-1))
#else
typedef int gnupg_fd_t;
#define GNUPG_INVALID_FD (-1)
#endif
void trap_unaligned (void);
int disable_core_dumps (void);
int enable_core_dumps (void);
const unsigned char *get_session_marker (size_t *rlen);
/*int check_permissions (const char *path,int extension,int checkonly);*/
void gnupg_sleep (unsigned int seconds);
int translate_sys2libc_fd (int fd, int for_write);
int translate_sys2libc_fd (gnupg_fd_t fd, int for_write);
int translate_sys2libc_fd_int (int fd, int for_write);
#ifdef HAVE_W32_SYSTEM

View File

@ -1,3 +1,9 @@
2007-07-12 Werner Koch <wk@g10code.com>
* gpg.c (main): Use translate_sys2libc_fd_int when passing an int
value.
* gpgv.c (main): Ditto.
2007-07-05 Werner Koch <wk@g10code.com>
* card-util.c (card_generate_subkey, card_store_subkey): Enable

View File

@ -2160,19 +2160,19 @@ main (int argc, char **argv )
case oDebugLevel: debug_level = pargs.r.ret_str; break;
case oStatusFD:
set_status_fd( translate_sys2libc_fd (pargs.r.ret_int, 1) );
set_status_fd( translate_sys2libc_fd_int (pargs.r.ret_int, 1) );
break;
case oStatusFile:
set_status_fd ( open_info_file (pargs.r.ret_str, 1) );
break;
case oAttributeFD:
set_attrib_fd(translate_sys2libc_fd (pargs.r.ret_int, 1));
set_attrib_fd(translate_sys2libc_fd_int (pargs.r.ret_int, 1));
break;
case oAttributeFile:
set_attrib_fd ( open_info_file (pargs.r.ret_str, 1) );
break;
case oLoggerFD:
log_set_fd (translate_sys2libc_fd (pargs.r.ret_int, 1));
log_set_fd (translate_sys2libc_fd_int (pargs.r.ret_int, 1));
break;
case oLoggerFile:
logfile = pargs.r.ret_str;
@ -2436,14 +2436,14 @@ main (int argc, char **argv )
set_passphrase_from_string(pargs.r.ret_str);
break;
case oPasswdFD:
pwfd = translate_sys2libc_fd (pargs.r.ret_int, 0);
pwfd = translate_sys2libc_fd_int (pargs.r.ret_int, 0);
break;
case oPasswdFile:
pwfd = open_info_file (pargs.r.ret_str, 0);
break;
case oPasswdRepeat: opt.passwd_repeat=pargs.r.ret_int; break;
case oCommandFD:
opt.command_fd = translate_sys2libc_fd (pargs.r.ret_int, 0);
opt.command_fd = translate_sys2libc_fd_int (pargs.r.ret_int, 0);
break;
case oCommandFile:
opt.command_fd = open_info_file (pargs.r.ret_str, 0);

View File

@ -154,7 +154,7 @@ main( int argc, char **argv )
case oKeyring: append_to_strlist( &nrings, pargs.r.ret_str); break;
case oStatusFD: set_status_fd( pargs.r.ret_int ); break;
case oLoggerFD:
log_set_fd (translate_sys2libc_fd (pargs.r.ret_int, 1));
log_set_fd (translate_sys2libc_fd_int (pargs.r.ret_int, 1));
break;
case oHomedir: opt.homedir = pargs.r.ret_str; break;
case oIgnoreTimeConflict: opt.ignore_time_conflict = 1; break;

View File

@ -1,3 +1,10 @@
2007-07-12 Werner Koch <wk@g10code.com>
* gpgsm.c (check_special_filename): Use translate_sys2libc_fd_int
when passing an int value.
* server.c (cmd_encrypt, cmd_decrypt, cmd_verify, cmd_import)
(cmd_export, cmd_message, cmd_genkey): Translate file descriptors.
2007-07-05 Werner Koch <wk@g10code.com>
* Makefile.am (common_libs): Changed order of libs.

View File

@ -1741,7 +1741,7 @@ check_special_filename (const char *fname, int for_write)
for (i=0; isdigit (fname[i]); i++ )
;
if ( !fname[i] )
return translate_sys2libc_fd (atoi (fname), for_write);
return translate_sys2libc_fd_int (atoi (fname), for_write);
}
return -1;
}

View File

@ -29,6 +29,7 @@
#include <assuan.h>
#include "gpgsm.h"
#include "sysutils.h"
#define set_error(e,t) assuan_set_error (ctx, gpg_error (e), (t))
@ -409,14 +410,14 @@ cmd_encrypt (assuan_context_t ctx, char *line)
FILE *out_fp;
int rc;
inp_fd = assuan_get_input_fd (ctx);
inp_fd = translate_sys2libc_fd (assuan_get_input_fd (ctx), 0);
if (inp_fd == -1)
return set_error (GPG_ERR_ASS_NO_INPUT, NULL);
out_fd = assuan_get_output_fd (ctx);
out_fd = translate_sys2libc_fd (assuan_get_output_fd (ctx), 1);
if (out_fd == -1)
return set_error (GPG_ERR_ASS_NO_OUTPUT, NULL);
out_fp = fdopen ( dup(out_fd), "w");
out_fp = fdopen (dup (out_fd), "w");
if (!out_fp)
return set_error (GPG_ERR_ASS_GENERAL, "fdopen() failed");
@ -460,14 +461,14 @@ cmd_decrypt (assuan_context_t ctx, char *line)
FILE *out_fp;
int rc;
inp_fd = assuan_get_input_fd (ctx);
inp_fd = translate_sys2libc_fd (assuan_get_input_fd (ctx), 0);
if (inp_fd == -1)
return set_error (GPG_ERR_ASS_NO_INPUT, NULL);
out_fd = assuan_get_output_fd (ctx);
out_fd = translate_sys2libc_fd (assuan_get_output_fd (ctx), 1);
if (out_fd == -1)
return set_error (GPG_ERR_ASS_NO_OUTPUT, NULL);
out_fp = fdopen ( dup(out_fd), "w");
out_fp = fdopen (dup(out_fd), "w");
if (!out_fp)
return set_error (GPG_ERR_ASS_GENERAL, "fdopen() failed");
rc = gpgsm_decrypt (ctrl, inp_fd, out_fp);
@ -496,8 +497,8 @@ cmd_verify (assuan_context_t ctx, char *line)
{
int rc;
ctrl_t ctrl = assuan_get_pointer (ctx);
int fd = assuan_get_input_fd (ctx);
int out_fd = assuan_get_output_fd (ctx);
int fd = translate_sys2libc_fd (assuan_get_input_fd (ctx), 0);
int out_fd = translate_sys2libc_fd (assuan_get_output_fd (ctx), 1);
FILE *out_fp = NULL;
if (fd == -1)
@ -538,10 +539,10 @@ cmd_sign (assuan_context_t ctx, char *line)
int detached;
int rc;
inp_fd = assuan_get_input_fd (ctx);
inp_fd = translate_sys2libc_fd (assuan_get_input_fd (ctx), 0);
if (inp_fd == -1)
return set_error (GPG_ERR_ASS_NO_INPUT, NULL);
out_fd = assuan_get_output_fd (ctx);
out_fd = translate_sys2libc_fd (assuan_get_output_fd (ctx), 1);
if (out_fd == -1)
return set_error (GPG_ERR_ASS_NO_OUTPUT, NULL);
@ -575,7 +576,7 @@ cmd_import (assuan_context_t ctx, char *line)
{
ctrl_t ctrl = assuan_get_pointer (ctx);
int rc;
int fd = assuan_get_input_fd (ctx);
int fd = translate_sys2libc_fd (assuan_get_input_fd (ctx), 0);
if (fd == -1)
return set_error (GPG_ERR_ASS_NO_INPUT, NULL);
@ -653,7 +654,7 @@ cmd_export (assuan_context_t ctx, char *line)
}
else
{
int fd = assuan_get_output_fd (ctx);
int fd = translate_sys2libc_fd (assuan_get_output_fd (ctx), 1);
FILE *out_fp;
if (fd == -1)
@ -733,12 +734,14 @@ static int
cmd_message (assuan_context_t ctx, char *line)
{
int rc;
assuan_fd_t sysfd;
int fd;
ctrl_t ctrl = assuan_get_pointer (ctx);
rc = assuan_command_parse_fd (ctx, line, &fd);
rc = assuan_command_parse_fd (ctx, line, &sysfd);
if (rc)
return rc;
fd = translate_sys2libc_fd (sysfd, 0);
if (fd == -1)
return set_error (GPG_ERR_ASS_NO_INPUT, NULL);
ctrl->server_local->message_fd = fd;
@ -785,7 +788,7 @@ do_listkeys (assuan_context_t ctx, char *line, int mode)
if (ctrl->server_local->list_to_output)
{
int outfd = assuan_get_output_fd (ctx);
int outfd = translate_sys2libc_fd (assuan_get_output_fd (ctx), 1);
if ( outfd == -1 )
return set_error (GPG_ERR_ASS_NO_OUTPUT, NULL);
@ -853,10 +856,10 @@ cmd_genkey (assuan_context_t ctx, char *line)
FILE *out_fp;
int rc;
inp_fd = assuan_get_input_fd (ctx);
inp_fd = translate_sys2libc_fd (assuan_get_input_fd (ctx), 0);
if (inp_fd == -1)
return set_error (GPG_ERR_ASS_NO_INPUT, NULL);
out_fd = assuan_get_output_fd (ctx);
out_fd = translate_sys2libc_fd (assuan_get_output_fd (ctx), 1);
if (out_fd == -1)
return set_error (GPG_ERR_ASS_NO_OUTPUT, NULL);