2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 * 2005 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 2 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
29 #include <sys/types.h>
43 #define SKELEXT ".skl"
45 #define SKELEXT EXTSEP_S "skel"
48 #if defined (HAVE_DRIVE_LETTERS) || defined (__riscos__)
49 #define CMP_FILENAME(a,b) ascii_strcasecmp( (a), (b) )
51 #define CMP_FILENAME(a,b) strcmp( (a), (b) )
54 #ifdef MKDIR_TAKES_ONE_ARG
56 #define mkdir(a,b) mkdir(a)
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 #ifndef HAVE_DOSISH_SYSTEM
77 if ( !strcmp ( fname
, "/dev/null" ) )
78 return 1; /* does not do any harm */
81 /* fixme: add some backup stuff in case of overwrite */
84 if( opt
.answer_no
|| opt
.batch
)
85 return 0; /* do not overwrite */
87 tty_printf(_("File `%s' exists. "), fname
);
90 if( cpr_get_answer_is_yes("openfile.overwrite.okay",
91 _("Overwrite? (y/N) ")) )
98 * Strip know extensions from iname and return a newly allocated
99 * filename. Return NULL if we can't do that.
102 make_outfile_name( const char *iname
)
106 if ( iobuf_is_pipe_filename (iname
) )
110 if( n
> 4 && ( !CMP_FILENAME(iname
+n
-4, EXTSEP_S
"gpg")
111 || !CMP_FILENAME(iname
+n
-4, EXTSEP_S
"pgp")
112 || !CMP_FILENAME(iname
+n
-4, EXTSEP_S
"sig")
113 || !CMP_FILENAME(iname
+n
-4, EXTSEP_S
"asc") ) ) {
114 char *buf
= xstrdup( iname
);
118 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
);
130 * Ask for a outputfilename and use the given one as default.
131 * Return NULL if no file has been given or it is not possible to
135 ask_outfile_name( const char *name
, size_t namelen
)
146 s
= _("Enter new filename");
148 n
= strlen(s
) + namelen
+ 10;
149 defname
= name
&& namelen
? make_printable_string( name
, namelen
, 0): NULL
;
152 sprintf(prompt
, "%s [%s]: ", s
, defname
);
154 sprintf(prompt
, "%s: ", s
);
155 tty_enable_completion(NULL
);
156 fname
= cpr_get("openfile.askoutname", prompt
);
158 tty_disable_completion();
161 xfree( fname
); fname
= NULL
;
162 fname
= defname
; defname
= NULL
;
172 * Make an output filename for the inputfile INAME.
173 * Returns an IOBUF and an errorcode
174 * Mode 0 = use ".gpg"
179 open_outfile( const char *iname
, int mode
, IOBUF
*a
)
184 if( iobuf_is_pipe_filename (iname
) && !opt
.outfile
) {
185 *a
= iobuf_create(NULL
);
187 rc
= gpg_error_from_errno (errno
);
188 log_error(_("can't open `%s': %s\n"), "[stdout]", strerror(errno
) );
190 else if( opt
.verbose
)
191 log_info(_("writing to stdout\n"));
199 else if( opt
.outfile
)
202 #ifdef USE_ONLY_8DOT3
203 if (opt
.mangle_dos_filenames
)
205 /* It is quite common DOS system to have only one dot in a
206 * a filename So if we have something like this, we simple
207 * replace the suffix execpt in cases where the suffix is
208 * larger than 3 characters and not the same as.
209 * We should really map the filenames to 8.3 but this tends to
210 * be more complicated and is probaly a duty of the filesystem
213 const char *newsfx
= mode
==1 ? ".asc" :
214 mode
==2 ? ".sig" : ".gpg";
216 buf
= xmalloc(strlen(iname
)+4+1);
218 dot
= strchr(buf
, '.' );
219 if ( dot
&& dot
> buf
&& dot
[1] && strlen(dot
) <= 4
220 && CMP_FILENAME(newsfx
, dot
) )
222 strcpy(dot
, newsfx
);
224 else if ( dot
&& !dot
[1] ) /* don't duplicate a dot */
225 strcpy( dot
, newsfx
+1 );
227 strcat ( buf
, newsfx
);
230 #endif /* USE_ONLY_8DOT3 */
232 buf
= xmalloc(strlen(iname
)+4+1);
233 strcpy(stpcpy(buf
,iname
), mode
==1 ? EXTSEP_S
"asc" :
234 mode
==2 ? EXTSEP_S
"sig" : EXTSEP_S
"gpg");
240 while( !overwrite_filep (name
) )
242 char *tmp
= ask_outfile_name (NULL
, 0);
246 rc
= gpg_error (GPG_ERR_EEXIST
);
255 if (is_secured_filename (name
) )
261 *a
= iobuf_create( name
);
264 rc
= gpg_error_from_errno (errno
);
265 log_error(_("can't create `%s': %s\n"), name
, strerror(errno
) );
267 else if( opt
.verbose
)
268 log_info(_("writing to `%s'\n"), name
);
274 iobuf_ioctl (*a
,3,1,NULL
); /* disable fd caching */
281 * Try to open a file without the extension ".sig" or ".asc"
282 * Return NULL if such a file is not available.
285 open_sigfile( const char *iname
, progress_filter_context_t
*pfx
)
290 if( !iobuf_is_pipe_filename (iname
) ) {
292 if( len
> 4 && ( !strcmp(iname
+ len
- 4, EXTSEP_S
"sig")
293 || ( len
> 5 && !strcmp(iname
+ len
- 5, EXTSEP_S
"sign") )
294 || !strcmp(iname
+ len
- 4, EXTSEP_S
"asc")) ) {
296 buf
= xstrdup(iname
);
297 buf
[len
-(buf
[len
-1]=='n'?5:4)] = 0 ;
298 a
= iobuf_open( buf
);
299 if (a
&& is_secured_file (iobuf_get_fd (a
)))
305 if( a
&& opt
.verbose
)
306 log_info(_("assuming signed data in `%s'\n"), buf
);
308 handle_progress (pfx
, a
, buf
);
316 * Copy the option file skeleton to the given directory.
319 copy_options_file( const char *destdir
)
321 const char *datadir
= GNUPG_DATADIR
;
333 fname
= xmalloc( strlen(datadir
) + strlen(destdir
) + 15 );
334 strcpy(stpcpy(fname
, datadir
), DIRSEP_S
"gpg-conf" SKELEXT
);
335 src
= fopen( fname
, "r" );
336 if (src
&& is_secured_file (fileno (src
)))
343 log_error(_("can't open `%s': %s\n"), fname
, strerror(errno
) );
347 strcpy(stpcpy(fname
, destdir
), DIRSEP_S
"gpg" EXTSEP_S
"conf" );
349 if ( is_secured_filename (fname
) )
355 dst
= fopen( fname
, "w" );
358 log_error(_("can't create `%s': %s\n"), fname
, strerror(errno
) );
364 while( (c
=getc(src
)) != EOF
) {
365 if( linefeeds
< 3 ) {
374 if (c
== ' ' || c
== '\t')
385 log_info(_("new configuration file `%s' created\n"), fname
);
387 log_info (_("WARNING: options in `%s'"
388 " are not yet active during this run\n"),
395 try_make_homedir( const char *fname
)
397 const char *defhome
= GNUPG_DEFAULT_HOMEDIR
;
399 /* Create the directory only if the supplied directory name
400 * is the same as the default one. This way we avoid to create
401 * arbitrary directories when a non-default homedirectory is used.
402 * To cope with HOME, we do compare only the suffix if we see that
403 * the default homedir does start with a tilde.
405 if( opt
.dry_run
|| opt
.no_homedir_creation
)
408 if ( ( *defhome
== '~'
409 && ( strlen(fname
) >= strlen (defhome
+1)
410 && !strcmp(fname
+strlen(fname
)-strlen(defhome
+1),
413 && !compare_filenames( fname
, defhome
) )
415 if( mkdir( fname
, S_IRUSR
|S_IWUSR
|S_IXUSR
) )
416 log_fatal( _("can't create directory `%s': %s\n"),
417 fname
, strerror(errno
) );
418 else if( !opt
.quiet
)
419 log_info( _("directory `%s' created\n"), fname
);
420 copy_options_file( fname
);
421 /* log_info(_("you have to start GnuPG again, " */
422 /* "so it can read the new configuration file\n") ); */