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 <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) )
60 /* FIXME: Implement opt.interactive. */
63 * Check whether FNAME exists and ask if it's okay to overwrite an
65 * Returns: True: it's okay to overwrite or the file does not exist
66 * False: Do not overwrite
69 overwrite_filep( const char *fname )
71 if ( iobuf_is_pipe_filename (fname) )
72 return 1; /* Writing to stdout is always okay. */
74 if ( access( fname, F_OK ) )
75 return 1; /* Does not exist. */
77 if ( !compare_filenames (fname, NAME_OF_DEV_NULL) )
78 return 1; /* Does not do any harm. */
82 if (opt.answer_no || opt.batch)
83 return 0; /* Do not overwrite. */
85 tty_printf (_("File '%s' exists. "), fname);
88 if (cpr_get_answer_is_yes ("openfile.overwrite.okay",
89 _("Overwrite? (y/N) ")) )
96 * Strip known extensions from iname and return a newly allocated
97 * filename. Return NULL if we can't do that.
100 make_outfile_name (const char *iname)
104 if (iobuf_is_pipe_filename (iname))
105 return xstrdup ("-");
108 if (n > 4 && (!CMP_FILENAME(iname+n-4, EXTSEP_S GPGEXT_GPG)
109 || !CMP_FILENAME(iname+n-4, EXTSEP_S "pgp")
110 || !CMP_FILENAME(iname+n-4, EXTSEP_S "sig")
111 || !CMP_FILENAME(iname+n-4, EXTSEP_S "asc")))
113 char *buf = xstrdup (iname);
117 else if (n > 5 && !CMP_FILENAME(iname+n-5, EXTSEP_S "sign"))
119 char *buf = xstrdup (iname);
124 log_info (_("%s: unknown suffix\n"), iname);
129 /* Ask for an output filename; use the given one as default. Return
130 NULL if no file has been given or if it is not possible to ask the
131 user. NAME is the template len which might conatin enbedded Nuls.
132 NAMELEN is its actual length.
135 ask_outfile_name( const char *name, size_t namelen )
146 defname = name && namelen? make_printable_string (name, namelen, 0) : NULL;
148 s = _("Enter new filename");
149 n = strlen(s) + (defname?strlen (defname):0) + 10;
150 prompt = xmalloc (n);
152 snprintf (prompt, n-1, "%s [%s]: ", s, defname );
154 snprintf (prompt, n-1, "%s: ", s );
155 tty_enable_completion(NULL);
156 fname = cpr_get ("openfile.askoutname", prompt );
158 tty_disable_completion ();
174 * Make an output filename for the inputfile INAME.
175 * Returns an IOBUF and an errorcode
176 * Mode 0 = use ".gpg"
181 * If INP_FD is not -1 the function simply creates an IOBUF for that
182 * file descriptor and ignore INAME and MODE. Note that INP_FD won't
183 * be closed if the returned IOBUF is closed. With RESTRICTEDPERM a
184 * file will be created with mode 700 if possible.
187 open_outfile (int inp_fd, const char *iname, int mode, int restrictedperm,
197 *a = iobuf_fdopen_nc (inp_fd, "wb");
200 rc = gpg_error_from_syserror ();
201 snprintf (xname, sizeof xname, "[fd %d]", inp_fd);
202 log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (rc));
204 else if (opt.verbose)
206 snprintf (xname, sizeof xname, "[fd %d]", inp_fd);
207 log_info (_("writing to '%s'\n"), xname);
210 else if (iobuf_is_pipe_filename (iname) && !opt.outfile)
212 *a = iobuf_create (NULL, 0);
215 rc = gpg_error_from_syserror ();
216 log_error (_("can't open '%s': %s\n"), "[stdout]", strerror(errno) );
218 else if ( opt.verbose )
219 log_info (_("writing to stdout\n"));
227 name = NAME_OF_DEV_NULL;
228 else if (opt.outfile)
232 #ifdef USE_ONLY_8DOT3
233 if (opt.mangle_dos_filenames)
235 /* It is quite common for DOS systems to have only one
236 dot in a filename. If we have something like this,
237 we simple replace the suffix except in cases where
238 the suffix is larger than 3 characters and not the
239 same as the new one. We don't map the filenames to
240 8.3 because this is a duty of the file system. */
244 newsfx = (mode==1 ? ".asc" :
246 mode==3 ? ".rev" : ".gpg");
248 buf = xmalloc (strlen(iname)+4+1);
250 dot = strchr (buf, '.' );
251 if ( dot && dot > buf && dot[1] && strlen(dot) <= 4
252 && CMP_FILENAME (newsfx, dot) )
253 strcpy (dot, newsfx);
254 else if (dot && !dot[1]) /* Do not duplicate a dot. */
255 strcpy (dot, newsfx+1);
257 strcat (buf, newsfx);
260 #endif /* USE_ONLY_8DOT3 */
262 buf = xstrconcat (iname,
263 (mode==1 ? EXTSEP_S "asc" :
264 mode==2 ? EXTSEP_S "sig" :
265 mode==3 ? EXTSEP_S "rev" :
266 /* */ EXTSEP_S GPGEXT_GPG),
273 while ( !overwrite_filep (name) )
275 char *tmp = ask_outfile_name (NULL, 0);
279 rc = gpg_error (GPG_ERR_EEXIST);
288 if (is_secured_filename (name) )
291 gpg_err_set_errno (EPERM);
294 *a = iobuf_create (name, restrictedperm);
297 rc = gpg_error_from_syserror ();
298 log_error(_("can't create '%s': %s\n"), name, strerror(errno) );
300 else if( opt.verbose )
301 log_info (_("writing to '%s'\n"), name );
307 iobuf_ioctl (*a, IOBUF_IOCTL_NO_CACHE, 1, NULL);
313 /* Find a matching data file for the signature file SIGFILENAME and
314 return it as a malloced string. If no matching data file is found,
317 get_matching_datafile (const char *sigfilename)
322 if (iobuf_is_pipe_filename (sigfilename))
325 len = strlen (sigfilename);
327 && (!strcmp (sigfilename + len - 4, EXTSEP_S "sig")
328 || (len > 5 && !strcmp(sigfilename + len - 5, EXTSEP_S "sign"))
329 || !strcmp(sigfilename + len - 4, EXTSEP_S "asc")))
332 fname = xstrdup (sigfilename);
333 fname[len-(fname[len-1]=='n'?5:4)] = 0 ;
334 if (access (fname, R_OK ))
336 /* Not found or other error. */
347 * Try to open a file without the extension ".sig" or ".asc"
348 * Return NULL if such a file is not available.
351 open_sigfile (const char *sigfilename, progress_filter_context_t *pfx)
356 buf = get_matching_datafile (sigfilename);
359 a = iobuf_open (buf);
360 if (a && is_secured_file (iobuf_get_fd (a)))
364 gpg_err_set_errno (EPERM);
367 log_info (_("assuming signed data in '%s'\n"), buf);
369 handle_progress (pfx, a, buf);
378 * Copy the option file skeleton to the given directory.
381 copy_options_file (const char *destdir)
383 const char *datadir = gnupg_datadir ();
395 fname = xmalloc (strlen(datadir) + strlen(destdir) + 15);
396 strcpy (stpcpy(fname, datadir), DIRSEP_S "gpg-conf" SKELEXT);
397 src = fopen (fname, "r");
398 if (src && is_secured_file (fileno (src)))
402 gpg_err_set_errno (EPERM);
406 log_info (_("can't open '%s': %s\n"), fname, strerror(errno));
410 strcpy (stpcpy (fname, destdir), DIRSEP_S GPGEXT_GPG EXTSEP_S "conf");
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);
459 log_info (_("WARNING: options in '%s'"
460 " are not yet active during this run\n"),
467 try_make_homedir (const char *fname)
469 const char *defhome = standard_homedir ();
471 /* Create the directory only if the supplied directory name is the
472 same as the default one. This way we avoid to create arbitrary
473 directories when a non-default home directory is used. To cope
474 with HOME, we do compare only the suffix if we see that the
475 default homedir does start with a tilde. */
476 if ( opt.dry_run || opt.no_homedir_creation )
480 #ifdef HAVE_W32_SYSTEM
481 ( !compare_filenames (fname, defhome) )
484 && (strlen(fname) >= strlen (defhome+1)
485 && !strcmp(fname+strlen(fname)-strlen(defhome+1), defhome+1 ) ))
486 || (*defhome != '~' && !compare_filenames( fname, defhome ) )
490 if (gnupg_mkdir (fname, "-rwx"))
491 log_fatal ( _("can't create directory '%s': %s\n"),
492 fname, strerror(errno) );
493 else if (!opt.quiet )
494 log_info ( _("directory '%s' created\n"), fname );
495 copy_options_file( fname );
500 /* Get and if needed create a string with the directory used to store
501 openpgp revocations. */
503 get_openpgp_revocdir (const char *home)
508 fname = make_filename (home, GNUPG_OPENPGP_REVOC_DIR, NULL);
509 if (stat (fname, &statbuf) && errno == ENOENT)
511 if (gnupg_mkdir (fname, "-rwx"))
512 log_error (_("can't create directory '%s': %s\n"),
513 fname, strerror (errno) );
515 log_info (_("directory '%s' created\n"), fname);