diff options
Diffstat (limited to 'g10/openfile.c')
-rw-r--r-- | g10/openfile.c | 91 |
1 files changed, 60 insertions, 31 deletions
diff --git a/g10/openfile.c b/g10/openfile.c index 73fd5db33..ac4596b83 100644 --- a/g10/openfile.c +++ b/g10/openfile.c @@ -199,7 +199,7 @@ open_outfile( const char *iname, int mode, IOBUF *a ) else { char *buf = NULL; const char *name; - + if ( opt.dry_run ) { #ifdef HAVE_W32_SYSTEM @@ -224,12 +224,12 @@ open_outfile( const char *iname, int mode, IOBUF *a ) char *dot; const char *newsfx = mode==1 ? ".asc" : mode==2 ? ".sig" : ".gpg"; - + buf = xmalloc(strlen(iname)+4+1); strcpy(buf,iname); dot = strrchr(buf, '.' ); if ( dot && dot > buf && dot[1] && strlen(dot) <= 4 - && CMP_FILENAME(newsfx, dot) + && CMP_FILENAME(newsfx, dot) && !(strchr (dot, '/') || strchr (dot, '\\'))) { /* There is a dot, the dot is not the first character, @@ -272,7 +272,7 @@ open_outfile( const char *iname, int mode, IOBUF *a ) xfree (buf); name = buf = tmp; } - + if( !rc ) { if (is_secured_filename (name) ) @@ -300,41 +300,70 @@ open_outfile( const char *iname, int mode, IOBUF *a ) } +/* Find a matching data file for the signature file SIGFILENAME and + return it as a malloced string. If no matching data file is found, + return NULL. */ +char * +get_matching_datafile (const char *sigfilename) +{ + char *fname = NULL; + size_t len; + + if (iobuf_is_pipe_filename (sigfilename)) + return NULL; + + len = strlen (sigfilename); + if (len > 4 + && (!strcmp (sigfilename + len - 4, EXTSEP_S "sig") + || (len > 5 && !strcmp(sigfilename + len - 5, EXTSEP_S "sign")) + || !strcmp(sigfilename + len - 4, EXTSEP_S "asc"))) + { + + fname = xstrdup (sigfilename); + fname[len-(fname[len-1]=='n'?5:4)] = 0 ; + if (access (fname, R_OK )) + { + /* Not found or other error. */ + xfree (fname); + fname = NULL; + } + } + + return fname; +} + + /**************** * Try to open a file without the extension ".sig" or ".asc" * Return NULL if such a file is not available. */ IOBUF -open_sigfile( const char *iname, progress_filter_context_t *pfx ) +open_sigfile (const char *sigfilename, progress_filter_context_t *pfx) { - IOBUF a = NULL; - size_t len; - - if( !iobuf_is_pipe_filename (iname) ) { - len = strlen(iname); - if( len > 4 && ( !strcmp(iname + len - 4, EXTSEP_S "sig") - || ( len > 5 && !strcmp(iname + len - 5, EXTSEP_S "sign") ) - || !strcmp(iname + len - 4, EXTSEP_S "asc")) ) { - char *buf; - buf = xstrdup(iname); - buf[len-(buf[len-1]=='n'?5:4)] = 0 ; - a = iobuf_open( buf ); - if (a && is_secured_file (iobuf_get_fd (a))) - { - iobuf_close (a); - a = NULL; - errno = EPERM; - } - if( a && opt.verbose ) - log_info(_("assuming signed data in `%s'\n"), buf ); - if (a && pfx) - handle_progress (pfx, a, buf); - xfree(buf); - } + iobuf_t a = NULL; + char *buf; + + buf = get_matching_datafile (sigfilename); + if (buf) + { + a = iobuf_open (buf); + if (a && is_secured_file (iobuf_get_fd (a))) + { + iobuf_close (a); + a = NULL; + errno = EPERM; + } + if (a) + log_info (_("assuming signed data in `%s'\n"), buf); + if (a && pfx) + handle_progress (pfx, a, buf); + xfree (buf); } - return a; + + return a; } + /**************** * Copy the option file skeleton to the given directory. */ @@ -398,7 +427,7 @@ copy_options_file( const char *destdir ) ; else if (c == '#') esc = 2; - else + else any_option = 1; } } |