Implement the server comamnd DECRYPT.
[gnupg.git] / g10 / openfile.c
blob9f41273e0e6467279f85ccee41f081ea9af76545
1 /* openfile.c
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/>.
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
32 #include "gpg.h"
33 #include "util.h"
34 #include "ttyio.h"
35 #include "options.h"
36 #include "main.h"
37 #include "status.h"
38 #include "i18n.h"
40 #ifdef USE_ONLY_8DOT3
41 #define SKELEXT ".skl"
42 #else
43 #define SKELEXT EXTSEP_S "skel"
44 #endif
46 #ifdef HAVE_W32_SYSTEM
47 #define NAME_OF_DEV_NULL "nul"
48 #else
49 #define NAME_OF_DEV_NULL "/dev/null"
50 #endif
53 #if defined (HAVE_DRIVE_LETTERS) || defined (__riscos__)
54 #define CMP_FILENAME(a,b) ascii_strcasecmp( (a), (b) )
55 #else
56 #define CMP_FILENAME(a,b) strcmp( (a), (b) )
57 #endif
59 #ifdef MKDIR_TAKES_ONE_ARG
60 #undef mkdir
61 #define mkdir(a,b) mkdir(a)
62 #endif
64 /* FIXME: Implement opt.interactive. */
66 /****************
67 * Check whether FNAME exists and ask if it's okay to overwrite an
68 * existing one.
69 * Returns: True: it's okay to overwrite or the file does not exist
70 * False: Do not overwrite
72 int
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. */
84 if (opt.answer_yes)
85 return 1;
86 if (opt.answer_no || opt.batch)
87 return 0; /* Do not overwrite. */
89 tty_printf (_("File `%s' exists. "), fname);
90 if (cpr_enabled ())
91 tty_printf ("\n");
92 if (cpr_get_answer_is_yes ("openfile.overwrite.okay",
93 _("Overwrite? (y/N) ")) )
94 return 1;
95 return 0;
99 /****************
100 * Strip known extensions from iname and return a newly allocated
101 * filename. Return NULL if we can't do that.
103 char *
104 make_outfile_name( const char *iname )
106 size_t n;
108 if ( iobuf_is_pipe_filename (iname) )
109 return xstrdup("-");
111 n = strlen(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 );
117 buf[n-4] = 0;
118 return buf;
120 else if( n > 5 && !CMP_FILENAME(iname+n-5, EXTSEP_S "sign") ) {
121 char *buf = xstrdup( iname );
122 buf[n-5] = 0;
123 return buf;
126 log_info(_("%s: unknown suffix\n"), iname );
127 return NULL;
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.
136 char *
137 ask_outfile_name( const char *name, size_t namelen )
139 size_t n;
140 const char *s;
141 char *prompt;
142 char *fname;
143 char *defname;
145 if ( opt.batch )
146 return 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);
153 if (defname)
154 snprintf (prompt, n-1, "%s [%s]: ", s, defname );
155 else
156 snprintf (prompt, n-1, "%s: ", s );
157 tty_enable_completion(NULL);
158 fname = cpr_get ("openfile.askoutname", prompt );
159 cpr_kill_prompt ();
160 tty_disable_completion ();
161 xfree (prompt);
162 if ( !*fname )
164 xfree (fname);
165 fname = defname;
166 defname = NULL;
168 xfree (defname);
169 if (fname)
170 trim_spaces (fname);
171 return fname;
175 /****************
176 * Make an output filename for the inputfile INAME.
177 * Returns an IOBUF and an errorcode
178 * Mode 0 = use ".gpg"
179 * 1 = use ".asc"
180 * 2 = use ".sig"
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)
189 int rc = 0;
191 *a = NULL;
192 if (inp_fd != -1)
194 char xname[64];
195 int fd2;
197 fd2 = dup (inp_fd);
198 if (fd2 == -1)
199 *a = NULL;
200 else
201 *a = iobuf_fdopen (fd2, "wb");
202 if (!*a)
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);
217 if ( !*a )
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"));
225 else
227 char *buf = NULL;
228 const char *name;
230 if (opt.dry_run)
231 name = NAME_OF_DEV_NULL;
232 else if (opt.outfile)
233 name = opt.outfile;
234 else
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. */
245 char *dot;
246 const char *newsfx;
248 newsfx = (mode==1 ? ".asc" :
249 mode==2 ? ".sig" : ".gpg");
251 buf = xmalloc (strlen(iname)+4+1);
252 strcpy (buf, iname);
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);
259 else
260 strcat (buf, newsfx);
262 if (!buf)
263 #endif /* USE_ONLY_8DOT3 */
265 buf = xstrconcat (iname,
266 (mode==1 ? EXTSEP_S "asc" :
267 mode==2 ? EXTSEP_S "sig" : EXTSEP_S "gpg"),
268 NULL);
270 name = buf;
273 rc = 0;
274 while ( !overwrite_filep (name) )
276 char *tmp = ask_outfile_name (NULL, 0);
277 if ( !tmp || !*tmp )
279 xfree (tmp);
280 rc = gpg_error (GPG_ERR_EEXIST);
281 break;
283 xfree (buf);
284 name = buf = tmp;
287 if ( !rc )
289 if (is_secured_filename (name) )
291 *a = NULL;
292 errno = EPERM;
294 else
295 *a = iobuf_create (name);
296 if (!*a)
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 );
304 xfree(buf);
307 if (*a)
308 iobuf_ioctl (*a, 3, 1, NULL); /* Disable fd caching. */
310 return rc;
314 /****************
315 * Try to open a file without the extension ".sig" or ".asc"
316 * Return NULL if such a file is not available.
318 IOBUF
319 open_sigfile( const char *iname, progress_filter_context_t *pfx )
321 IOBUF a = NULL;
322 size_t len;
324 if( !iobuf_is_pipe_filename (iname) ) {
325 len = strlen(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")) ) {
329 char *buf;
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)))
335 iobuf_close (a);
336 a = NULL;
337 errno = EPERM;
339 if( a && opt.verbose )
340 log_info(_("assuming signed data in `%s'\n"), buf );
341 if (a && pfx)
342 handle_progress (pfx, a, buf);
343 xfree(buf);
346 return a;
349 /****************
350 * Copy the option file skeleton to the given directory.
352 static void
353 copy_options_file( const char *destdir )
355 const char *datadir = gnupg_datadir ();
356 char *fname;
357 FILE *src, *dst;
358 int linefeeds=0;
359 int c;
360 mode_t oldmask;
361 int esc = 0;
362 int any_option = 0;
364 if( opt.dry_run )
365 return;
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)))
372 fclose (src);
373 src = NULL;
374 errno = EPERM;
376 if( !src ) {
377 log_info (_("can't open `%s': %s\n"), fname, strerror(errno) );
378 xfree(fname);
379 return;
381 strcpy(stpcpy(fname, destdir), DIRSEP_S "gpg" EXTSEP_S "conf" );
382 oldmask=umask(077);
383 if ( is_secured_filename (fname) )
385 dst = NULL;
386 errno = EPERM;
388 else
389 dst = fopen( fname, "w" );
390 umask(oldmask);
391 if( !dst ) {
392 log_info (_("can't create `%s': %s\n"), fname, strerror(errno) );
393 fclose( src );
394 xfree(fname);
395 return;
398 while( (c=getc(src)) != EOF ) {
399 if( linefeeds < 3 ) {
400 if( c == '\n' )
401 linefeeds++;
403 else {
404 putc( c, dst );
405 if (c== '\n')
406 esc = 1;
407 else if (esc == 1) {
408 if (c == ' ' || c == '\t')
410 else if (c == '#')
411 esc = 2;
412 else
413 any_option = 1;
417 fclose( dst );
418 fclose( src );
419 log_info(_("new configuration file `%s' created\n"), fname );
420 if (any_option)
421 log_info (_("WARNING: options in `%s'"
422 " are not yet active during this run\n"),
423 fname);
424 xfree(fname);
428 void
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 )
439 return;
441 if (
442 #ifdef HAVE_W32_SYSTEM
443 ( !compare_filenames (fname, defhome) )
444 #else
445 ( *defhome == '~'
446 && (strlen(fname) >= strlen (defhome+1)
447 && !strcmp(fname+strlen(fname)-strlen(defhome+1), defhome+1 ) ))
448 || (*defhome != '~' && !compare_filenames( fname, defhome ) )
449 #endif
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 );