2
* Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
2
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3
* 2005 Free Software Foundation, Inc.
4
5
* This file is part of GnuPG.
6
7
* GnuPG is free software; you can redistribute it and/or modify
7
8
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* the Free Software Foundation; either version 3 of the License, or
9
10
* (at your option) any later version.
11
12
* GnuPG is distributed in the hope that it will be useful,
14
15
* GNU General Public License for more details.
16
17
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18
* along with this program; if not, see <http://www.gnu.org/licenses/>.
21
21
#include <config.h>
67
66
overwrite_filep( const char *fname )
69
if( !fname || (*fname == '-' && !fname[1]) )
70
return 1; /* writing to stdout is always okay */
68
if( iobuf_is_pipe_filename (fname) )
69
return 1; /* Writing to stdout is always okay */
72
71
if( access( fname, F_OK ) )
73
72
return 1; /* does not exist */
84
87
return 0; /* do not overwrite */
86
89
tty_printf(_("File `%s' exists. "), fname);
87
92
if( cpr_get_answer_is_yes("openfile.overwrite.okay",
88
_("Overwrite (y/N)? ")) )
93
_("Overwrite? (y/N) ")) )
95
* Strip know extensions from iname and return a newly allocated
100
* Strip known extensions from iname and return a newly allocated
96
101
* filename. Return NULL if we can't do that.
103
if( (!iname || (*iname=='-' && !iname[1]) ))
104
return xstrdup ("-");
108
if ( iobuf_is_pipe_filename (iname) )
106
111
n = strlen(iname);
107
112
if( n > 4 && ( !CMP_FILENAME(iname+n-4, EXTSEP_S "gpg")
108
113
|| !CMP_FILENAME(iname+n-4, EXTSEP_S "pgp")
109
114
|| !CMP_FILENAME(iname+n-4, EXTSEP_S "sig")
110
115
|| !CMP_FILENAME(iname+n-4, EXTSEP_S "asc") ) ) {
111
char *buf = xstrdup ( iname );
116
char *buf = xstrdup( iname );
115
120
else if( n > 5 && !CMP_FILENAME(iname+n-5, EXTSEP_S "sign") ) {
116
char *buf = xstrdup ( iname );
121
char *buf = xstrdup( iname );
127
* Ask for a outputfilename and use the given one as default.
128
* Return NULL if no file has been given or it is not possible to
131
/* Ask for an output filename; use the given one as default. Return
132
NULL if no file has been given or if it is not possible to ask the
133
user. NAME is the template len which might conatin enbedded Nuls.
134
NAMELEN is its actual length.
132
137
ask_outfile_name( const char *name, size_t namelen )
143
s = _("Enter new filename");
145
n = strlen(s) + namelen + 10;
146
defname = name && namelen? make_printable_string( name, namelen, 0): NULL;
147
prompt = xmalloc (n);
149
sprintf(prompt, "%s [%s]: ", s, defname );
151
sprintf(prompt, "%s: ", s );
152
fname = cpr_get("openfile.askoutname", prompt );
156
xfree ( fname ); fname = NULL;
157
fname = defname; defname = NULL;
148
defname = name && namelen? make_printable_string (name, namelen, 0) : NULL;
150
s = _("Enter new filename");
151
n = strlen(s) + (defname?strlen (defname):0) + 10;
152
prompt = xmalloc (n);
154
snprintf (prompt, n-1, "%s [%s]: ", s, defname );
156
snprintf (prompt, n-1, "%s: ", s );
157
tty_enable_completion(NULL);
158
fname = cpr_get ("openfile.askoutname", prompt );
160
tty_disable_completion ();
166
175
/****************
167
176
* Make an output filename for the inputfile INAME.
168
* Returns an iobuf_t and an errorcode
177
* Returns an IOBUF and an errorcode
169
178
* Mode 0 = use ".gpg"
174
open_outfile( const char *iname, int mode, iobuf_t *a )
183
open_outfile( const char *iname, int mode, IOBUF *a )
179
if( (!iname || (*iname=='-' && !iname[1])) && !opt.outfile ) {
180
if( !(*a = iobuf_create(NULL)) ) {
181
rc = gpg_error_from_errno (errno);
182
log_error(_("%s: can't open: %s\n"), "[stdout]", strerror(errno) );
188
if( iobuf_is_pipe_filename (iname) && !opt.outfile ) {
189
*a = iobuf_create(NULL);
191
rc = gpg_error_from_syserror ();
192
log_error(_("can't open `%s': %s\n"), "[stdout]", strerror(errno) );
184
194
else if( opt.verbose )
185
195
log_info(_("writing to stdout\n"));
249
if( !(*a = iobuf_create( name )) )
251
rc = gpg_error_from_errno (errno);
252
log_error(_("%s: can't create: %s\n"), name, strerror(errno) );
265
if (is_secured_filename (name) )
271
*a = iobuf_create( name );
274
rc = gpg_error_from_syserror ();
275
log_error(_("can't create `%s': %s\n"), name, strerror(errno) );
254
277
else if( opt.verbose )
255
278
log_info(_("writing to `%s'\n"), name );
284
iobuf_ioctl (*a,3,1,NULL); /* disable fd caching */
265
291
* Try to open a file without the extension ".sig" or ".asc"
266
292
* Return NULL if such a file is not available.
269
295
open_sigfile( const char *iname, progress_filter_context_t *pfx )
274
if( iname && !(*iname == '-' && !iname[1]) ) {
300
if( !iobuf_is_pipe_filename (iname) ) {
275
301
len = strlen(iname);
276
302
if( len > 4 && ( !strcmp(iname + len - 4, EXTSEP_S "sig")
277
303
|| ( len > 5 && !strcmp(iname + len - 5, EXTSEP_S "sign") )
278
304
|| !strcmp(iname + len - 4, EXTSEP_S "asc")) ) {
280
buf = xstrdup (iname);
306
buf = xstrdup(iname);
281
307
buf[len-(buf[len-1]=='n'?5:4)] = 0 ;
282
308
a = iobuf_open( buf );
309
if (a && is_secured_file (iobuf_get_fd (a)))
283
315
if( a && opt.verbose )
284
316
log_info(_("assuming signed data in `%s'\n"), buf );
286
318
handle_progress (pfx, a, buf);
308
340
if( opt.dry_run )
311
fname = xmalloc ( strlen(datadir) + strlen(destdir)
312
+ strlen (SKELEXT) + 15 );
343
fname = xmalloc( strlen(datadir) + strlen(destdir) + 15 );
313
344
strcpy(stpcpy(fname, datadir), DIRSEP_S "gpg-conf" SKELEXT );
314
345
src = fopen( fname, "r" );
346
if (src && is_secured_file (fileno (src)))
316
log_error(_("%s: can't open: %s\n"), fname, strerror(errno) );
353
log_info (_("can't open `%s': %s\n"), fname, strerror(errno) );
320
357
strcpy(stpcpy(fname, destdir), DIRSEP_S "gpg" EXTSEP_S "conf" );
321
358
oldmask=umask(077);
322
dst = fopen( fname, "w" );
359
if ( is_secured_filename (fname) )
365
dst = fopen( fname, "w" );
325
log_error(_("%s: can't create: %s\n"), fname, strerror(errno) );
368
log_info (_("can't create `%s': %s\n"), fname, strerror(errno) );
354
397
log_info (_("WARNING: options in `%s'"
355
398
" are not yet active during this run\n"),
362
try_make_homedir( const char *fname )
405
try_make_homedir (const char *fname)
364
const char *defhome = GNUPG_DEFAULT_HOMEDIR;
366
/* Create the directory only if the supplied directory name
367
* is the same as the default one. This way we avoid to create
368
* arbitrary directories when a non-default homedirectory is used.
369
* To cope with HOME, we do compare only the suffix if we see that
370
* the default homedir does start with a tilde.
372
if( opt.dry_run || opt.no_homedir_creation )
375
if ( ( *defhome == '~'
376
&& ( strlen(fname) >= strlen (defhome+1)
377
&& !strcmp(fname+strlen(fname)-strlen(defhome+1),
380
&& !compare_filenames( fname, defhome ) )
382
if( mkdir( fname, S_IRUSR|S_IWUSR|S_IXUSR ) )
383
log_fatal( _("%s: can't create directory: %s\n"),
384
fname, strerror(errno) );
385
else if( !opt.quiet )
386
log_info( _("%s: directory created\n"), fname );
387
copy_options_file( fname );
388
/* log_info(_("you have to start GnuPG again, " */
389
/* "so it can read the new configuration file\n") ); */
407
const char *defhome = standard_homedir ();
409
/* Create the directory only if the supplied directory name is the
410
same as the default one. This way we avoid to create arbitrary
411
directories when a non-default home directory is used. To cope
412
with HOME, we do compare only the suffix if we see that the
413
default homedir does start with a tilde. */
414
if ( opt.dry_run || opt.no_homedir_creation )
418
#ifdef HAVE_W32_SYSTEM
419
( !compare_filenames (fname, defhome) )
422
&& (strlen(fname) >= strlen (defhome+1)
423
&& !strcmp(fname+strlen(fname)-strlen(defhome+1), defhome+1 ) ))
424
|| (*defhome != '~' && !compare_filenames( fname, defhome ) )
428
if ( mkdir (fname, S_IRUSR|S_IWUSR|S_IXUSR) )
429
log_fatal ( _("can't create directory `%s': %s\n"),
430
fname, strerror(errno) );
431
else if (!opt.quiet )
432
log_info ( _("directory `%s' created\n"), fname );
433
copy_options_file( fname );