2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 * 2005, 2009 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
41 #define SKELEXT ".skl"
43 #define SKELEXT EXTSEP_S "skel"
46 #ifdef HAVE_W32_SYSTEM
47 #define NAME_OF_DEV_NULL "nul"
49 #define NAME_OF_DEV_NULL "/dev/null"
53 #if defined (HAVE_DRIVE_LETTERS) || defined (__riscos__)
54 #define CMP_FILENAME(a,b) ascii_strcasecmp( (a), (b) )
56 #define CMP_FILENAME(a,b) strcmp( (a), (b) )
59 #ifdef MKDIR_TAKES_ONE_ARG
61 #define mkdir(a,b) mkdir(a)
64 /* FIXME: Implement opt.interactive. */
67 * Check whether FNAME exists and ask if it's okay to overwrite an
69 * Returns: True: it's okay to overwrite or the file does not exist
70 * False: Do not overwrite
73 overwrite_filep( const char *fname
)
75 if ( iobuf_is_pipe_filename (fname
) )
76 return 1; /* Writing to stdout is always okay. */
78 if ( access( fname
, F_OK
) )
79 return 1; /* Does not exist. */
81 if ( !compare_filenames (fname
, NAME_OF_DEV_NULL
) )
82 return 1; /* Does not do any harm. */
86 if (opt
.answer_no
|| opt
.batch
)
87 return 0; /* Do not overwrite. */
89 tty_printf (_("File `%s' exists. "), fname
);
92 if (cpr_get_answer_is_yes ("openfile.overwrite.okay",
93 _("Overwrite? (y/N) ")) )
100 * Strip known extensions from iname and return a newly allocated
101 * filename. Return NULL if we can't do that.
104 make_outfile_name( const char *iname
)
108 if ( iobuf_is_pipe_filename (iname
) )
112 if( n
> 4 && ( !CMP_FILENAME(iname
+n
-4, EXTSEP_S
"gpg")
113 || !CMP_FILENAME(iname
+n
-4, EXTSEP_S
"pgp")
114 || !CMP_FILENAME(iname
+n
-4, EXTSEP_S
"sig")
115 || !CMP_FILENAME(iname
+n
-4, EXTSEP_S
"asc") ) ) {
116 char *buf
= xstrdup( iname
);
120 else if( n
> 5 && !CMP_FILENAME(iname
+n
-5, EXTSEP_S
"sign") ) {
121 char *buf
= xstrdup( iname
);
126 log_info(_("%s: unknown suffix\n"), iname
);
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.
137 ask_outfile_name( const char *name
, size_t namelen
)
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 ();
176 * Make an output filename for the inputfile INAME.
177 * Returns an IOBUF and an errorcode
178 * Mode 0 = use ".gpg"
182 * If INP_FD is not -1 the function simply creates an IOBUF for that
183 * file descriptor and ignorea INAME and MODE. Note that INP_FD won't
184 * be closed if the returned IOBUF is closed.
187 open_outfile (int inp_fd
, const char *iname
, int mode
, iobuf_t
*a
)
201 *a
= iobuf_fdopen (fd2
, "wb");
204 rc
= gpg_error_from_syserror ();
205 snprintf (xname
, sizeof xname
, "[fd %d]", inp_fd
);
206 log_error (_("can't open `%s': %s\n"), xname
, gpg_strerror (rc
));
208 else if (opt
.verbose
)
210 snprintf (xname
, sizeof xname
, "[fd %d]", inp_fd
);
211 log_info (_("writing to `%s'\n"), xname
);
214 else if (iobuf_is_pipe_filename (iname
) && !opt
.outfile
)
216 *a
= iobuf_create(NULL
);
219 rc
= gpg_error_from_syserror ();
220 log_error (_("can't open `%s': %s\n"), "[stdout]", strerror(errno
) );
222 else if ( opt
.verbose
)
223 log_info (_("writing to stdout\n"));
231 name
= NAME_OF_DEV_NULL
;
232 else if (opt
.outfile
)
236 #ifdef USE_ONLY_8DOT3
237 if (opt
.mangle_dos_filenames
)
239 /* It is quite common for DOS systems to have only one
240 dot in a filename. If we have something like this,
241 we simple replace the suffix except in cases where
242 the suffix is larger than 3 characters and not the
243 same as the new one. We don't map the filenames to
244 8.3 because this is a duty of the file system. */
248 newsfx
= (mode
==1 ? ".asc" :
249 mode
==2 ? ".sig" : ".gpg");
251 buf
= xmalloc (strlen(iname
)+4+1);
253 dot
= strchr (buf
, '.' );
254 if ( dot
&& dot
> buf
&& dot
[1] && strlen(dot
) <= 4
255 && CMP_FILENAME (newsfx
, dot
) )
256 strcpy (dot
, newsfx
);
257 else if (dot
&& !dot
[1]) /* Do not duplicate a dot. */
258 strcpy (dot
, newsfx
+1);
260 strcat (buf
, newsfx
);
263 #endif /* USE_ONLY_8DOT3 */
265 buf
= xstrconcat (iname
,
266 (mode
==1 ? EXTSEP_S
"asc" :
267 mode
==2 ? EXTSEP_S
"sig" : EXTSEP_S
"gpg"),
274 while ( !overwrite_filep (name
) )
276 char *tmp
= ask_outfile_name (NULL
, 0);
280 rc
= gpg_error (GPG_ERR_EEXIST
);
289 if (is_secured_filename (name
) )
295 *a
= iobuf_create (name
);
298 rc
= gpg_error_from_syserror ();
299 log_error(_("can't create `%s': %s\n"), name
, strerror(errno
) );
301 else if( opt
.verbose
)
302 log_info (_("writing to `%s'\n"), name
);
308 iobuf_ioctl (*a
, 3, 1, NULL
); /* Disable fd caching. */
315 * Try to open a file without the extension ".sig" or ".asc"
316 * Return NULL if such a file is not available.
319 open_sigfile( const char *iname
, progress_filter_context_t
*pfx
)
324 if( !iobuf_is_pipe_filename (iname
) ) {
326 if( len
> 4 && ( !strcmp(iname
+ len
- 4, EXTSEP_S
"sig")
327 || ( len
> 5 && !strcmp(iname
+ len
- 5, EXTSEP_S
"sign") )
328 || !strcmp(iname
+ len
- 4, EXTSEP_S
"asc")) ) {
330 buf
= xstrdup(iname
);
331 buf
[len
-(buf
[len
-1]=='n'?5:4)] = 0 ;
332 a
= iobuf_open( buf
);
333 if (a
&& is_secured_file (iobuf_get_fd (a
)))
339 if( a
&& opt
.verbose
)
340 log_info(_("assuming signed data in `%s'\n"), buf
);
342 handle_progress (pfx
, a
, buf
);
350 * Copy the option file skeleton to the given directory.
353 copy_options_file( const char *destdir
)
355 const char *datadir
= gnupg_datadir ();
367 fname
= xmalloc( strlen(datadir
) + strlen(destdir
) + 15 );
368 strcpy(stpcpy(fname
, datadir
), DIRSEP_S
"gpg-conf" SKELEXT
);
369 src
= fopen( fname
, "r" );
370 if (src
&& is_secured_file (fileno (src
)))
377 log_info (_("can't open `%s': %s\n"), fname
, strerror(errno
) );
381 strcpy(stpcpy(fname
, destdir
), DIRSEP_S
"gpg" EXTSEP_S
"conf" );
383 if ( is_secured_filename (fname
) )
389 dst
= fopen( fname
, "w" );
392 log_info (_("can't create `%s': %s\n"), fname
, strerror(errno
) );
398 while( (c
=getc(src
)) != EOF
) {
399 if( linefeeds
< 3 ) {
408 if (c
== ' ' || c
== '\t')
419 log_info(_("new configuration file `%s' created\n"), fname
);
421 log_info (_("WARNING: options in `%s'"
422 " are not yet active during this run\n"),
429 try_make_homedir (const char *fname
)
431 const char *defhome
= standard_homedir ();
433 /* Create the directory only if the supplied directory name is the
434 same as the default one. This way we avoid to create arbitrary
435 directories when a non-default home directory is used. To cope
436 with HOME, we do compare only the suffix if we see that the
437 default homedir does start with a tilde. */
438 if ( opt
.dry_run
|| opt
.no_homedir_creation
)
442 #ifdef HAVE_W32_SYSTEM
443 ( !compare_filenames (fname
, defhome
) )
446 && (strlen(fname
) >= strlen (defhome
+1)
447 && !strcmp(fname
+strlen(fname
)-strlen(defhome
+1), defhome
+1 ) ))
448 || (*defhome
!= '~' && !compare_filenames( fname
, defhome
) )
452 if ( mkdir (fname
, S_IRUSR
|S_IWUSR
|S_IXUSR
) )
453 log_fatal ( _("can't create directory `%s': %s\n"),
454 fname
, strerror(errno
) );
455 else if (!opt
.quiet
)
456 log_info ( _("directory `%s' created\n"), fname
);
457 copy_options_file( fname
);