2003-06-05 09:14:21 +02:00
|
|
|
/* openfile.c
|
2006-04-19 13:26:11 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
|
|
|
|
* 2005 Free Software Foundation, Inc.
|
2003-06-05 09:14:21 +02:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-05 09:14:21 +02:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-04 21:49:40 +02:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-06-05 09:14:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2003-06-18 21:56:13 +02:00
|
|
|
|
|
|
|
#include "gpg.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
#include "util.h"
|
|
|
|
#include "ttyio.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "status.h"
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
#ifdef USE_ONLY_8DOT3
|
|
|
|
#define SKELEXT ".skl"
|
|
|
|
#else
|
|
|
|
#define SKELEXT EXTSEP_S "skel"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined (HAVE_DRIVE_LETTERS) || defined (__riscos__)
|
|
|
|
#define CMP_FILENAME(a,b) ascii_strcasecmp( (a), (b) )
|
|
|
|
#else
|
|
|
|
#define CMP_FILENAME(a,b) strcmp( (a), (b) )
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MKDIR_TAKES_ONE_ARG
|
|
|
|
#undef mkdir
|
|
|
|
#define mkdir(a,b) mkdir(a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* FIXME: Implement opt.interactive. */
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Check whether FNAME exists and ask if it's okay to overwrite an
|
|
|
|
* existing one.
|
|
|
|
* Returns: True: it's okay to overwrite or the file does not exist
|
|
|
|
* False: Do not overwrite
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
overwrite_filep( const char *fname )
|
|
|
|
{
|
2006-04-19 13:26:11 +02:00
|
|
|
if( iobuf_is_pipe_filename (fname) )
|
|
|
|
return 1; /* Writing to stdout is always okay */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if( access( fname, F_OK ) )
|
|
|
|
return 1; /* does not exist */
|
|
|
|
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
|
|
|
if ( !strcmp ( fname, "/dev/null" ) )
|
|
|
|
return 1; /* does not do any harm */
|
|
|
|
#endif
|
2007-05-07 21:49:12 +02:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2007-05-09 13:01:33 +02:00
|
|
|
if ( !strcmp ( fname, "nul" ) )
|
2007-05-07 21:49:12 +02:00
|
|
|
return 1;
|
|
|
|
#endif
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
/* fixme: add some backup stuff in case of overwrite */
|
|
|
|
if( opt.answer_yes )
|
|
|
|
return 1;
|
|
|
|
if( opt.answer_no || opt.batch )
|
|
|
|
return 0; /* do not overwrite */
|
|
|
|
|
|
|
|
tty_printf(_("File `%s' exists. "), fname);
|
2006-04-19 13:26:11 +02:00
|
|
|
if( cpr_enabled () )
|
|
|
|
tty_printf ("\n");
|
2003-06-05 09:14:21 +02:00
|
|
|
if( cpr_get_answer_is_yes("openfile.overwrite.okay",
|
2006-04-19 13:26:11 +02:00
|
|
|
_("Overwrite? (y/N) ")) )
|
2003-06-05 09:14:21 +02:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
2006-11-27 17:41:32 +01:00
|
|
|
* Strip known extensions from iname and return a newly allocated
|
2003-06-05 09:14:21 +02:00
|
|
|
* filename. Return NULL if we can't do that.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
make_outfile_name( const char *iname )
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
if ( iobuf_is_pipe_filename (iname) )
|
|
|
|
return xstrdup("-");
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
n = strlen(iname);
|
|
|
|
if( n > 4 && ( !CMP_FILENAME(iname+n-4, EXTSEP_S "gpg")
|
|
|
|
|| !CMP_FILENAME(iname+n-4, EXTSEP_S "pgp")
|
|
|
|
|| !CMP_FILENAME(iname+n-4, EXTSEP_S "sig")
|
|
|
|
|| !CMP_FILENAME(iname+n-4, EXTSEP_S "asc") ) ) {
|
2006-04-19 13:26:11 +02:00
|
|
|
char *buf = xstrdup( iname );
|
2003-06-05 09:14:21 +02:00
|
|
|
buf[n-4] = 0;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
else if( n > 5 && !CMP_FILENAME(iname+n-5, EXTSEP_S "sign") ) {
|
2006-04-19 13:26:11 +02:00
|
|
|
char *buf = xstrdup( iname );
|
2003-06-05 09:14:21 +02:00
|
|
|
buf[n-5] = 0;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_info(_("%s: unknown suffix\n"), iname );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-27 17:41:32 +01:00
|
|
|
/* Ask for an output filename; use the given one as default. Return
|
|
|
|
NULL if no file has been given or if it is not possible to ask the
|
|
|
|
user. NAME is the template len which might conatin enbedded Nuls.
|
|
|
|
NAMELEN is its actual length.
|
2003-06-05 09:14:21 +02:00
|
|
|
*/
|
|
|
|
char *
|
|
|
|
ask_outfile_name( const char *name, size_t namelen )
|
|
|
|
{
|
2006-11-27 17:41:32 +01:00
|
|
|
size_t n;
|
|
|
|
const char *s;
|
|
|
|
char *prompt;
|
|
|
|
char *fname;
|
|
|
|
char *defname;
|
|
|
|
|
|
|
|
if ( opt.batch )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
defname = name && namelen? make_printable_string (name, namelen, 0) : NULL;
|
|
|
|
|
|
|
|
s = _("Enter new filename");
|
|
|
|
n = strlen(s) + (defname?strlen (defname):0) + 10;
|
|
|
|
prompt = xmalloc (n);
|
|
|
|
if (defname)
|
|
|
|
snprintf (prompt, n-1, "%s [%s]: ", s, defname );
|
|
|
|
else
|
|
|
|
snprintf (prompt, n-1, "%s: ", s );
|
|
|
|
tty_enable_completion(NULL);
|
|
|
|
fname = cpr_get ("openfile.askoutname", prompt );
|
|
|
|
cpr_kill_prompt ();
|
|
|
|
tty_disable_completion ();
|
|
|
|
xfree (prompt);
|
|
|
|
if ( !*fname )
|
|
|
|
{
|
|
|
|
xfree (fname);
|
|
|
|
fname = defname;
|
|
|
|
defname = NULL;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2006-11-27 17:41:32 +01:00
|
|
|
xfree (defname);
|
|
|
|
if (fname)
|
|
|
|
trim_spaces (fname);
|
|
|
|
return fname;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Make an output filename for the inputfile INAME.
|
2006-04-19 13:26:11 +02:00
|
|
|
* Returns an IOBUF and an errorcode
|
2003-06-05 09:14:21 +02:00
|
|
|
* Mode 0 = use ".gpg"
|
|
|
|
* 1 = use ".asc"
|
|
|
|
* 2 = use ".sig"
|
|
|
|
*/
|
|
|
|
int
|
2006-04-19 13:26:11 +02:00
|
|
|
open_outfile( const char *iname, int mode, IOBUF *a )
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
*a = NULL;
|
2006-04-19 13:26:11 +02:00
|
|
|
if( iobuf_is_pipe_filename (iname) && !opt.outfile ) {
|
|
|
|
*a = iobuf_create(NULL);
|
|
|
|
if( !*a ) {
|
2006-09-14 18:50:33 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2006-04-19 13:26:11 +02:00
|
|
|
log_error(_("can't open `%s': %s\n"), "[stdout]", strerror(errno) );
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
else if( opt.verbose )
|
|
|
|
log_info(_("writing to stdout\n"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *buf = NULL;
|
|
|
|
const char *name;
|
|
|
|
|
2007-05-07 21:49:12 +02:00
|
|
|
if ( opt.dry_run )
|
|
|
|
{
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2007-05-09 13:01:33 +02:00
|
|
|
name = "nul";
|
2007-05-07 21:49:12 +02:00
|
|
|
#else
|
|
|
|
name = "/dev/null";
|
|
|
|
#endif
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
else if( opt.outfile )
|
|
|
|
name = opt.outfile;
|
|
|
|
else {
|
|
|
|
#ifdef USE_ONLY_8DOT3
|
|
|
|
if (opt.mangle_dos_filenames)
|
|
|
|
{
|
|
|
|
/* It is quite common DOS system to have only one dot in a
|
|
|
|
* a filename So if we have something like this, we simple
|
|
|
|
* replace the suffix execpt in cases where the suffix is
|
|
|
|
* larger than 3 characters and not the same as.
|
|
|
|
* We should really map the filenames to 8.3 but this tends to
|
|
|
|
* be more complicated and is probaly a duty of the filesystem
|
|
|
|
*/
|
|
|
|
char *dot;
|
|
|
|
const char *newsfx = mode==1 ? ".asc" :
|
|
|
|
mode==2 ? ".sig" : ".gpg";
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
buf = xmalloc(strlen(iname)+4+1);
|
2003-06-05 09:14:21 +02:00
|
|
|
strcpy(buf,iname);
|
|
|
|
dot = strchr(buf, '.' );
|
|
|
|
if ( dot && dot > buf && dot[1] && strlen(dot) <= 4
|
|
|
|
&& CMP_FILENAME(newsfx, dot) )
|
|
|
|
{
|
|
|
|
strcpy(dot, newsfx );
|
|
|
|
}
|
|
|
|
else if ( dot && !dot[1] ) /* don't duplicate a dot */
|
|
|
|
strcpy( dot, newsfx+1 );
|
|
|
|
else
|
|
|
|
strcat ( buf, newsfx );
|
|
|
|
}
|
|
|
|
if (!buf)
|
|
|
|
#endif /* USE_ONLY_8DOT3 */
|
|
|
|
{
|
2006-04-19 13:26:11 +02:00
|
|
|
buf = xmalloc(strlen(iname)+4+1);
|
2003-06-05 09:14:21 +02:00
|
|
|
strcpy(stpcpy(buf,iname), mode==1 ? EXTSEP_S "asc" :
|
|
|
|
mode==2 ? EXTSEP_S "sig" : EXTSEP_S "gpg");
|
|
|
|
}
|
|
|
|
name = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
while( !overwrite_filep (name) )
|
|
|
|
{
|
|
|
|
char *tmp = ask_outfile_name (NULL, 0);
|
|
|
|
if ( !tmp || !*tmp )
|
|
|
|
{
|
2003-06-18 21:56:13 +02:00
|
|
|
xfree (tmp);
|
2006-04-19 13:26:11 +02:00
|
|
|
rc = gpg_error (GPG_ERR_EEXIST);
|
2003-06-05 09:14:21 +02:00
|
|
|
break;
|
|
|
|
}
|
2003-06-18 21:56:13 +02:00
|
|
|
xfree (buf);
|
2003-06-05 09:14:21 +02:00
|
|
|
name = buf = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !rc )
|
|
|
|
{
|
2006-04-19 13:26:11 +02:00
|
|
|
if (is_secured_filename (name) )
|
|
|
|
{
|
|
|
|
*a = NULL;
|
|
|
|
errno = EPERM;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*a = iobuf_create( name );
|
|
|
|
if( !*a )
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2006-09-14 18:50:33 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2006-04-19 13:26:11 +02:00
|
|
|
log_error(_("can't create `%s': %s\n"), name, strerror(errno) );
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
else if( opt.verbose )
|
|
|
|
log_info(_("writing to `%s'\n"), name );
|
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(buf);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
if (*a)
|
|
|
|
iobuf_ioctl (*a,3,1,NULL); /* disable fd caching */
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Try to open a file without the extension ".sig" or ".asc"
|
|
|
|
* Return NULL if such a file is not available.
|
|
|
|
*/
|
2006-04-19 13:26:11 +02:00
|
|
|
IOBUF
|
2003-06-05 09:14:21 +02:00
|
|
|
open_sigfile( const char *iname, progress_filter_context_t *pfx )
|
|
|
|
{
|
2006-04-19 13:26:11 +02:00
|
|
|
IOBUF a = NULL;
|
2003-06-05 09:14:21 +02:00
|
|
|
size_t len;
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
if( !iobuf_is_pipe_filename (iname) ) {
|
2003-06-05 09:14:21 +02:00
|
|
|
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;
|
2006-04-19 13:26:11 +02:00
|
|
|
buf = xstrdup(iname);
|
2003-06-05 09:14:21 +02:00
|
|
|
buf[len-(buf[len-1]=='n'?5:4)] = 0 ;
|
|
|
|
a = iobuf_open( buf );
|
2006-04-19 13:26:11 +02:00
|
|
|
if (a && is_secured_file (iobuf_get_fd (a)))
|
|
|
|
{
|
|
|
|
iobuf_close (a);
|
|
|
|
a = NULL;
|
|
|
|
errno = EPERM;
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
if( a && opt.verbose )
|
|
|
|
log_info(_("assuming signed data in `%s'\n"), buf );
|
|
|
|
if (a && pfx)
|
|
|
|
handle_progress (pfx, a, buf);
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(buf);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Copy the option file skeleton to the given directory.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
copy_options_file( const char *destdir )
|
|
|
|
{
|
2007-06-14 19:05:07 +02:00
|
|
|
const char *datadir = gnupg_datadir ();
|
2003-06-05 09:14:21 +02:00
|
|
|
char *fname;
|
|
|
|
FILE *src, *dst;
|
|
|
|
int linefeeds=0;
|
|
|
|
int c;
|
|
|
|
mode_t oldmask;
|
|
|
|
int esc = 0;
|
|
|
|
int any_option = 0;
|
|
|
|
|
|
|
|
if( opt.dry_run )
|
|
|
|
return;
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
fname = xmalloc( strlen(datadir) + strlen(destdir) + 15 );
|
2004-08-31 09:57:33 +02:00
|
|
|
strcpy(stpcpy(fname, datadir), DIRSEP_S "gpg-conf" SKELEXT );
|
2003-06-05 09:14:21 +02:00
|
|
|
src = fopen( fname, "r" );
|
2006-04-19 13:26:11 +02:00
|
|
|
if (src && is_secured_file (fileno (src)))
|
|
|
|
{
|
|
|
|
fclose (src);
|
|
|
|
src = NULL;
|
|
|
|
errno = EPERM;
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
if( !src ) {
|
2006-12-08 12:30:13 +01:00
|
|
|
log_info (_("can't open `%s': %s\n"), fname, strerror(errno) );
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(fname);
|
2003-06-05 09:14:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
strcpy(stpcpy(fname, destdir), DIRSEP_S "gpg" EXTSEP_S "conf" );
|
|
|
|
oldmask=umask(077);
|
2006-04-19 13:26:11 +02:00
|
|
|
if ( is_secured_filename (fname) )
|
|
|
|
{
|
|
|
|
dst = NULL;
|
|
|
|
errno = EPERM;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dst = fopen( fname, "w" );
|
2003-06-05 09:14:21 +02:00
|
|
|
umask(oldmask);
|
|
|
|
if( !dst ) {
|
2006-12-08 12:30:13 +01:00
|
|
|
log_info (_("can't create `%s': %s\n"), fname, strerror(errno) );
|
2003-06-05 09:14:21 +02:00
|
|
|
fclose( src );
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(fname);
|
2003-06-05 09:14:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while( (c=getc(src)) != EOF ) {
|
|
|
|
if( linefeeds < 3 ) {
|
|
|
|
if( c == '\n' )
|
|
|
|
linefeeds++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
putc( c, dst );
|
|
|
|
if (c== '\n')
|
|
|
|
esc = 1;
|
|
|
|
else if (esc == 1) {
|
|
|
|
if (c == ' ' || c == '\t')
|
|
|
|
;
|
|
|
|
else if (c == '#')
|
|
|
|
esc = 2;
|
|
|
|
else
|
|
|
|
any_option = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose( dst );
|
|
|
|
fclose( src );
|
|
|
|
log_info(_("new configuration file `%s' created\n"), fname );
|
|
|
|
if (any_option)
|
|
|
|
log_info (_("WARNING: options in `%s'"
|
|
|
|
" are not yet active during this run\n"),
|
|
|
|
fname);
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(fname);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2007-06-26 15:48:44 +02:00
|
|
|
try_make_homedir (const char *fname)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2007-06-26 15:48:44 +02:00
|
|
|
const char *defhome = standard_homedir ();
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2007-06-26 15:48:44 +02:00
|
|
|
/* Create the directory only if the supplied directory name is the
|
|
|
|
same as the default one. This way we avoid to create arbitrary
|
|
|
|
directories when a non-default home directory is used. To cope
|
|
|
|
with HOME, we do compare only the suffix if we see that the
|
|
|
|
default homedir does start with a tilde. */
|
|
|
|
if ( opt.dry_run || opt.no_homedir_creation )
|
|
|
|
return;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2007-06-26 15:48:44 +02:00
|
|
|
if (
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
( !compare_filenames (fname, defhome) )
|
|
|
|
#else
|
|
|
|
( *defhome == '~'
|
|
|
|
&& (strlen(fname) >= strlen (defhome+1)
|
|
|
|
&& !strcmp(fname+strlen(fname)-strlen(defhome+1), defhome+1 ) ))
|
|
|
|
|| (*defhome != '~' && !compare_filenames( fname, defhome ) )
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if ( mkdir (fname, S_IRUSR|S_IWUSR|S_IXUSR) )
|
|
|
|
log_fatal ( _("can't create directory `%s': %s\n"),
|
|
|
|
fname, strerror(errno) );
|
|
|
|
else if (!opt.quiet )
|
|
|
|
log_info ( _("directory `%s' created\n"), fname );
|
|
|
|
copy_options_file( fname );
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
}
|