2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2009,
3 * 2010 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 <https://www.gnu.org/licenses/>.
26 #include <sys/types.h>
40 #define SKELEXT ".skl"
42 #define SKELEXT EXTSEP_S "skel"
45 #ifdef HAVE_W32_SYSTEM
46 #define NAME_OF_DEV_NULL "nul"
48 #define NAME_OF_DEV_NULL "/dev/null"
52 #if defined (HAVE_DRIVE_LETTERS) || defined (__riscos__)
53 #define CMP_FILENAME(a,b) ascii_strcasecmp( (a), (b) )
55 #define CMP_FILENAME(a,b) strcmp( (a), (b) )
59 /* FIXME: Implement opt.interactive. */
62 * Check whether FNAME exists and ask if it's okay to overwrite an
64 * Returns: True: it's okay to overwrite or the file does not exist
65 * False: Do not overwrite
68 overwrite_filep( const char *fname )
70 if ( iobuf_is_pipe_filename (fname) )
71 return 1; /* Writing to stdout is always okay. */
73 if ( access( fname, F_OK ) )
74 return 1; /* Does not exist. */
76 if ( !compare_filenames (fname, NAME_OF_DEV_NULL) )
77 return 1; /* Does not do any harm. */
81 if (opt.answer_no || opt.batch)
82 return 0; /* Do not overwrite. */
84 tty_printf (_("File '%s' exists. "), fname);
87 if (cpr_get_answer_is_yes ("openfile.overwrite.okay",
88 _("Overwrite? (y/N) ")) )
95 * Strip known extensions from iname and return a newly allocated
96 * filename. Return NULL if we can't do that.
99 make_outfile_name (const char *iname)
103 if (iobuf_is_pipe_filename (iname))
104 return xstrdup ("-");
107 if (n > 4 && (!CMP_FILENAME(iname+n-4, EXTSEP_S GPGEXT_GPG)
108 || !CMP_FILENAME(iname+n-4, EXTSEP_S "pgp")
109 || !CMP_FILENAME(iname+n-4, EXTSEP_S "sig")
110 || !CMP_FILENAME(iname+n-4, EXTSEP_S "asc")))
112 char *buf = xstrdup (iname);
116 else if (n > 5 && !CMP_FILENAME(iname+n-5, EXTSEP_S "sign"))
118 char *buf = xstrdup (iname);
123 log_info (_("%s: unknown suffix\n"), iname);
128 /* Ask for an output filename; use the given one as default. Return
129 NULL if no file has been given or if it is not possible to ask the
130 user. NAME is the template len which might conatin enbedded Nuls.
131 NAMELEN is its actual length.
134 ask_outfile_name( const char *name, size_t namelen )
145 defname = name && namelen? make_printable_string (name, namelen, 0) : NULL;
147 s = _("Enter new filename");
148 n = strlen(s) + (defname?strlen (defname):0) + 10;
149 prompt = xmalloc (n);
151 snprintf (prompt, n, "%s [%s]: ", s, defname );
153 snprintf (prompt, n, "%s: ", s );
154 tty_enable_completion(NULL);
155 fname = cpr_get ("openfile.askoutname", prompt );
157 tty_disable_completion ();
173 * Make an output filename for the inputfile INAME.
174 * Returns an IOBUF and an errorcode
175 * Mode 0 = use ".gpg"
180 * If INP_FD is not -1 the function simply creates an IOBUF for that
181 * file descriptor and ignore INAME and MODE. Note that INP_FD won't
182 * be closed if the returned IOBUF is closed. With RESTRICTEDPERM a
183 * file will be created with mode 700 if possible.
186 open_outfile (int inp_fd, const char *iname, int mode, int restrictedperm,
196 *a = iobuf_fdopen_nc (inp_fd, "wb");
199 rc = gpg_error_from_syserror ();
200 snprintf (xname, sizeof xname, "[fd %d]", inp_fd);
201 log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (rc));
203 else if (opt.verbose)
205 snprintf (xname, sizeof xname, "[fd %d]", inp_fd);
206 log_info (_("writing to '%s'\n"), xname);
209 else if (iobuf_is_pipe_filename (iname) && !opt.outfile)
211 *a = iobuf_create (NULL, 0);
214 rc = gpg_error_from_syserror ();
215 log_error (_("can't open '%s': %s\n"), "[stdout]", strerror(errno) );
217 else if ( opt.verbose )
218 log_info (_("writing to stdout\n"));
226 name = NAME_OF_DEV_NULL;
227 else if (opt.outfile)
231 #ifdef USE_ONLY_8DOT3
232 if (opt.mangle_dos_filenames)
234 /* It is quite common for DOS systems to have only one
235 dot in a filename. If we have something like this,
236 we simple replace the suffix except in cases where
237 the suffix is larger than 3 characters and not the
238 same as the new one. We don't map the filenames to
239 8.3 because this is a duty of the file system. */
243 newsfx = (mode==1 ? ".asc" :
245 mode==3 ? ".rev" : ".gpg");
247 buf = xmalloc (strlen(iname)+4+1);
249 dot = strchr (buf, '.' );
250 if ( dot && dot > buf && dot[1] && strlen(dot) <= 4
251 && CMP_FILENAME (newsfx, dot) )
252 strcpy (dot, newsfx);
253 else if (dot && !dot[1]) /* Do not duplicate a dot. */
254 strcpy (dot, newsfx+1);
256 strcat (buf, newsfx);
259 #endif /* USE_ONLY_8DOT3 */
261 buf = xstrconcat (iname,
262 (mode==1 ? EXTSEP_S "asc" :
263 mode==2 ? EXTSEP_S "sig" :
264 mode==3 ? EXTSEP_S "rev" :
265 /* */ EXTSEP_S GPGEXT_GPG),
272 while ( !overwrite_filep (name) )
274 char *tmp = ask_outfile_name (NULL, 0);
278 rc = gpg_error (GPG_ERR_EEXIST);
287 if (is_secured_filename (name) )
290 gpg_err_set_errno (EPERM);
293 *a = iobuf_create (name, restrictedperm);
296 rc = gpg_error_from_syserror ();
297 log_error(_("can't create '%s': %s\n"), name, strerror(errno) );
299 else if( opt.verbose )
300 log_info (_("writing to '%s'\n"), name );
306 iobuf_ioctl (*a, IOBUF_IOCTL_NO_CACHE, 1, NULL);
312 /* Find a matching data file for the signature file SIGFILENAME and
313 return it as a malloced string. If no matching data file is found,
316 get_matching_datafile (const char *sigfilename)
321 if (iobuf_is_pipe_filename (sigfilename))
324 len = strlen (sigfilename);
326 && (!strcmp (sigfilename + len - 4, EXTSEP_S "sig")
327 || (len > 5 && !strcmp(sigfilename + len - 5, EXTSEP_S "sign"))
328 || !strcmp(sigfilename + len - 4, EXTSEP_S "asc")))
331 fname = xstrdup (sigfilename);
332 fname[len-(fname[len-1]=='n'?5:4)] = 0 ;
333 if (access (fname, R_OK ))
335 /* Not found or other error. */
346 * Try to open a file without the extension ".sig" or ".asc"
347 * Return NULL if such a file is not available.
350 open_sigfile (const char *sigfilename, progress_filter_context_t *pfx)
355 buf = get_matching_datafile (sigfilename);
358 a = iobuf_open (buf);
359 if (a && is_secured_file (iobuf_get_fd (a)))
363 gpg_err_set_errno (EPERM);
366 log_info (_("assuming signed data in '%s'\n"), buf);
368 handle_progress (pfx, a, buf);
377 * Copy the option file skeleton for NAME to the given directory.
378 * Returns true if the new option file has any option.
381 copy_options_file (const char *destdir, const char *name)
383 const char *datadir = gnupg_datadir ();
395 fname = xstrconcat (datadir, DIRSEP_S, name, "-conf", SKELEXT, NULL);
396 src = fopen (fname, "r");
397 if (src && is_secured_file (fileno (src)))
401 gpg_err_set_errno (EPERM);
405 log_info (_("can't open '%s': %s\n"), fname, strerror(errno));
410 fname = xstrconcat (destdir, DIRSEP_S, name, EXTSEP_S, "conf", NULL);
412 oldmask = umask (077);
413 if (is_secured_filename (fname))
416 gpg_err_set_errno (EPERM);
419 dst = fopen( fname, "w" );
424 log_info (_("can't create '%s': %s\n"), fname, strerror(errno) );
430 while ((c = getc (src)) != EOF)
444 if (c == ' ' || c == '\t')
457 log_info (_("new configuration file '%s' created\n"), fname);
464 try_make_homedir (const char *fname)
466 const char *defhome = standard_homedir ();
468 /* Create the directory only if the supplied directory name is the
469 same as the default one. This way we avoid to create arbitrary
470 directories when a non-default home directory is used. To cope
471 with HOME, we do compare only the suffix if we see that the
472 default homedir does start with a tilde. */
473 if ( opt.dry_run || opt.no_homedir_creation )
477 #ifdef HAVE_W32_SYSTEM
478 ( !compare_filenames (fname, defhome) )
481 && (strlen(fname) >= strlen (defhome+1)
482 && !strcmp(fname+strlen(fname)-strlen(defhome+1), defhome+1 ) ))
483 || (*defhome != '~' && !compare_filenames( fname, defhome ) )
487 if (gnupg_mkdir (fname, "-rwx"))
488 log_fatal ( _("can't create directory '%s': %s\n"),
489 fname, strerror(errno) );
490 else if (!opt.quiet )
491 log_info ( _("directory '%s' created\n"), fname );
493 /* Note that we also copy a dirmngr.conf file here. This is
494 because gpg is likely the first invoked tool and thus creates
496 copy_options_file (fname, DIRMNGR_NAME);
497 if (copy_options_file (fname, GPG_NAME))
498 log_info (_("WARNING: options in '%s'"
499 " are not yet active during this run\n"),
505 /* Get and if needed create a string with the directory used to store
506 openpgp revocations. */
508 get_openpgp_revocdir (const char *home)
513 fname = make_filename (home, GNUPG_OPENPGP_REVOC_DIR, NULL);
514 if (stat (fname, &statbuf) && errno == ENOENT)
516 if (gnupg_mkdir (fname, "-rwx"))
517 log_error (_("can't create directory '%s': %s\n"),
518 fname, strerror (errno) );
520 log_info (_("directory '%s' created\n"), fname);