~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to g10/openfile.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* openfile.c
2
 
 * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
 
3
 *               2005 Free Software Foundation, Inc.
3
4
 *
4
5
 * This file is part of GnuPG.
5
6
 *
6
7
 * GnuPG is free software; you can redistribute it and/or modify
7
8
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * the Free Software Foundation; either version 3 of the License, or
9
10
 * (at your option) any later version.
10
11
 *
11
12
 * GnuPG is distributed in the hope that it will be useful,
14
15
 * GNU General Public License for more details.
15
16
 *
16
17
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
19
 */
20
20
 
21
21
#include <config.h>
31
31
 
32
32
#include "gpg.h"
33
33
#include "util.h"
34
 
#include "memory.h"
35
34
#include "ttyio.h"
36
35
#include "options.h"
37
36
#include "main.h"
66
65
int
67
66
overwrite_filep( const char *fname )
68
67
{
69
 
    if( !fname || (*fname == '-' && !fname[1]) )
70
 
        return 1; /* writing to stdout is always okay */
 
68
    if( iobuf_is_pipe_filename (fname) )
 
69
        return 1; /* Writing to stdout is always okay */
71
70
 
72
71
    if( access( fname, F_OK ) )
73
72
        return 1; /* does not exist */
76
75
    if ( !strcmp ( fname, "/dev/null" ) )
77
76
        return 1; /* does not do any harm */
78
77
#endif
 
78
#ifdef HAVE_W32_SYSTEM
 
79
    if ( !strcmp ( fname, "nul" ) )
 
80
        return 1;
 
81
#endif
79
82
 
80
83
    /* fixme: add some backup stuff in case of overwrite */
81
84
    if( opt.answer_yes )
84
87
        return 0;  /* do not overwrite */
85
88
 
86
89
    tty_printf(_("File `%s' exists. "), fname);
 
90
    if( cpr_enabled () )
 
91
        tty_printf ("\n");
87
92
    if( cpr_get_answer_is_yes("openfile.overwrite.okay",
88
 
                               _("Overwrite (y/N)? ")) )
 
93
                               _("Overwrite? (y/N) ")) )
89
94
        return 1;
90
95
    return 0;
91
96
}
92
97
 
93
98
 
94
99
/****************
95
 
 * Strip know extensions from iname and return a newly allocated
 
100
 * Strip known extensions from iname and return a newly allocated
96
101
 * filename.  Return NULL if we can't do that.
97
102
 */
98
103
char *
100
105
{
101
106
    size_t n;
102
107
 
103
 
    if( (!iname || (*iname=='-' && !iname[1]) ))
104
 
        return xstrdup ("-");
 
108
    if ( iobuf_is_pipe_filename (iname) )
 
109
        return xstrdup("-");
105
110
 
106
111
    n = strlen(iname);
107
112
    if( n > 4 && (    !CMP_FILENAME(iname+n-4, EXTSEP_S "gpg")
108
113
                   || !CMP_FILENAME(iname+n-4, EXTSEP_S "pgp")
109
114
                   || !CMP_FILENAME(iname+n-4, EXTSEP_S "sig")
110
115
                   || !CMP_FILENAME(iname+n-4, EXTSEP_S "asc") ) ) {
111
 
        char *buf = xstrdup ( iname );
 
116
        char *buf = xstrdup( iname );
112
117
        buf[n-4] = 0;
113
118
        return buf;
114
119
    }
115
120
    else if( n > 5 && !CMP_FILENAME(iname+n-5, EXTSEP_S "sign") ) {
116
 
        char *buf = xstrdup ( iname );
 
121
        char *buf = xstrdup( iname );
117
122
        buf[n-5] = 0;
118
123
        return buf;
119
124
    }
123
128
}
124
129
 
125
130
 
126
 
/****************
127
 
 * Ask for a outputfilename and use the given one as default.
128
 
 * Return NULL if no file has been given or it is not possible to
129
 
 * ask the user.
 
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.
130
135
 */
131
136
char *
132
137
ask_outfile_name( const char *name, size_t namelen )
133
138
{
134
 
    size_t n;
135
 
    const char *s;
136
 
    char *prompt;
137
 
    char *fname;
138
 
    char *defname;
139
 
 
140
 
    if( opt.batch )
141
 
        return NULL;
142
 
 
143
 
    s = _("Enter new filename");
144
 
 
145
 
    n = strlen(s) + namelen + 10;
146
 
    defname = name && namelen? make_printable_string( name, namelen, 0): NULL;
147
 
    prompt = xmalloc (n);
148
 
    if( defname )
149
 
        sprintf(prompt, "%s [%s]: ", s, defname );
150
 
    else
151
 
        sprintf(prompt, "%s: ", s );
152
 
    fname = cpr_get("openfile.askoutname", prompt );
153
 
    cpr_kill_prompt();
154
 
    xfree (prompt);
155
 
    if( !*fname ) {
156
 
        xfree ( fname ); fname = NULL;
157
 
        fname = defname; defname = NULL;
 
139
  size_t n;
 
140
  const char *s;
 
141
  char *prompt;
 
142
  char *fname;
 
143
  char *defname;
 
144
 
 
145
  if ( opt.batch )
 
146
    return NULL;
 
147
  
 
148
  defname = name && namelen? make_printable_string (name, namelen, 0) : NULL;
 
149
 
 
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 ) 
 
163
    {
 
164
      xfree (fname); 
 
165
      fname = defname;
 
166
      defname = NULL;
158
167
    }
159
 
    xfree (defname);
160
 
    if (fname)
161
 
        trim_spaces (fname);
162
 
    return fname;
 
168
  xfree (defname);
 
169
  if (fname)
 
170
    trim_spaces (fname);
 
171
  return fname;
163
172
}
164
173
 
165
174
 
166
175
/****************
167
176
 * Make an output filename for the inputfile INAME.
168
 
 * Returns an iobuf_t and an errorcode
 
177
 * Returns an IOBUF and an errorcode
169
178
 * Mode 0 = use ".gpg"
170
179
 *      1 = use ".asc"
171
180
 *      2 = use ".sig"
172
181
 */
173
182
int
174
 
open_outfile( const char *iname, int mode, iobuf_t *a )
 
183
open_outfile( const char *iname, int mode, IOBUF *a )
175
184
{
176
185
  int rc = 0;
177
186
 
178
187
  *a = NULL;
179
 
  if( (!iname || (*iname=='-' && !iname[1])) && !opt.outfile ) {
180
 
    if( !(*a = iobuf_create(NULL)) ) {
181
 
      rc = gpg_error_from_errno (errno);
182
 
      log_error(_("%s: can't open: %s\n"), "[stdout]", strerror(errno) );
 
188
  if( iobuf_is_pipe_filename (iname) && !opt.outfile ) {
 
189
    *a = iobuf_create(NULL);
 
190
    if( !*a ) {
 
191
      rc = gpg_error_from_syserror ();
 
192
      log_error(_("can't open `%s': %s\n"), "[stdout]", strerror(errno) );
183
193
    }
184
194
    else if( opt.verbose )
185
195
      log_info(_("writing to stdout\n"));
188
198
    char *buf = NULL;
189
199
    const char *name;
190
200
    
191
 
    if( opt.dry_run )
192
 
      name = "/dev/null";
 
201
    if ( opt.dry_run )
 
202
      {
 
203
#ifdef HAVE_W32_SYSTEM
 
204
        name = "nul";
 
205
#else
 
206
        name = "/dev/null";
 
207
#endif
 
208
      }
193
209
    else if( opt.outfile )
194
210
      name = opt.outfile;
195
211
    else {
207
223
          const char *newsfx = mode==1 ? ".asc" :
208
224
                               mode==2 ? ".sig" : ".gpg";
209
225
          
210
 
          buf = xmalloc (strlen(iname)+4+1);
 
226
          buf = xmalloc(strlen(iname)+4+1);
211
227
          strcpy(buf,iname);
212
228
          dot = strchr(buf, '.' );
213
229
          if ( dot && dot > buf && dot[1] && strlen(dot) <= 4
223
239
      if (!buf)
224
240
#endif /* USE_ONLY_8DOT3 */
225
241
        {
226
 
          buf = xmalloc (strlen(iname)+4+1);
 
242
          buf = xmalloc(strlen(iname)+4+1);
227
243
          strcpy(stpcpy(buf,iname), mode==1 ? EXTSEP_S "asc" :
228
244
                                   mode==2 ? EXTSEP_S "sig" : EXTSEP_S "gpg");
229
245
        }
237
253
        if ( !tmp || !*tmp )
238
254
          {
239
255
            xfree (tmp);
240
 
            rc = GPG_ERR_EEXIST;
 
256
            rc = gpg_error (GPG_ERR_EEXIST);
241
257
            break;
242
258
          }
243
259
        xfree (buf);
246
262
    
247
263
    if( !rc )
248
264
      {
249
 
        if( !(*a = iobuf_create( name )) )
250
 
          {
251
 
            rc = gpg_error_from_errno (errno);
252
 
            log_error(_("%s: can't create: %s\n"), name, strerror(errno) );
 
265
        if (is_secured_filename (name) )
 
266
          {
 
267
            *a = NULL;
 
268
            errno = EPERM;
 
269
          }
 
270
        else
 
271
          *a = iobuf_create( name );
 
272
        if( !*a )
 
273
          {
 
274
            rc = gpg_error_from_syserror ();
 
275
            log_error(_("can't create `%s': %s\n"), name, strerror(errno) );
253
276
          }
254
277
        else if( opt.verbose )
255
278
          log_info(_("writing to `%s'\n"), name );
256
279
      }
257
 
    xfree (buf);
 
280
    xfree(buf);
258
281
  }
259
282
 
 
283
  if (*a)
 
284
    iobuf_ioctl (*a,3,1,NULL); /* disable fd caching */
 
285
 
260
286
  return rc;
261
287
}
262
288
 
265
291
 * Try to open a file without the extension ".sig" or ".asc"
266
292
 * Return NULL if such a file is not available.
267
293
 */
268
 
iobuf_t
 
294
IOBUF
269
295
open_sigfile( const char *iname, progress_filter_context_t *pfx )
270
296
{
271
 
    iobuf_t a = NULL;
 
297
    IOBUF a = NULL;
272
298
    size_t len;
273
299
 
274
 
    if( iname && !(*iname == '-' && !iname[1]) ) {
 
300
    if( !iobuf_is_pipe_filename (iname) ) {
275
301
        len = strlen(iname);
276
302
        if( len > 4 && ( !strcmp(iname + len - 4, EXTSEP_S "sig")
277
303
                        || ( len > 5 && !strcmp(iname + len - 5, EXTSEP_S "sign") )
278
304
                        || !strcmp(iname + len - 4, EXTSEP_S "asc")) ) {
279
305
            char *buf;
280
 
            buf = xstrdup (iname);
 
306
            buf = xstrdup(iname);
281
307
            buf[len-(buf[len-1]=='n'?5:4)] = 0 ;
282
308
            a = iobuf_open( buf );
 
309
            if (a && is_secured_file (iobuf_get_fd (a)))
 
310
              {
 
311
                iobuf_close (a);
 
312
                a = NULL;
 
313
                errno = EPERM;
 
314
              }
283
315
            if( a && opt.verbose )
284
316
                log_info(_("assuming signed data in `%s'\n"), buf );
285
317
            if (a && pfx)
286
318
              handle_progress (pfx, a, buf);
287
 
            xfree (buf);
 
319
            xfree(buf);
288
320
        }
289
321
    }
290
322
    return a;
296
328
static void
297
329
copy_options_file( const char *destdir )
298
330
{
299
 
    const char *datadir = GNUPG_DATADIR;
 
331
    const char *datadir = gnupg_datadir ();
300
332
    char *fname;
301
333
    FILE *src, *dst;
302
334
    int linefeeds=0;
308
340
    if( opt.dry_run )
309
341
        return;
310
342
 
311
 
    fname = xmalloc ( strlen(datadir) + strlen(destdir) 
312
 
                      + strlen (SKELEXT) + 15 );
 
343
    fname = xmalloc( strlen(datadir) + strlen(destdir) + 15 );
313
344
    strcpy(stpcpy(fname, datadir), DIRSEP_S "gpg-conf" SKELEXT );
314
345
    src = fopen( fname, "r" );
 
346
    if (src && is_secured_file (fileno (src)))
 
347
      {
 
348
        fclose (src);
 
349
        src = NULL;
 
350
        errno = EPERM;
 
351
      }
315
352
    if( !src ) {
316
 
        log_error(_("%s: can't open: %s\n"), fname, strerror(errno) );
317
 
        xfree (fname);
 
353
        log_info (_("can't open `%s': %s\n"), fname, strerror(errno) );
 
354
        xfree(fname);
318
355
        return;
319
356
    }
320
357
    strcpy(stpcpy(fname, destdir), DIRSEP_S "gpg" EXTSEP_S "conf" );
321
358
    oldmask=umask(077);
322
 
    dst = fopen( fname, "w" );
 
359
    if ( is_secured_filename (fname) )
 
360
      {
 
361
        dst = NULL;
 
362
        errno = EPERM;
 
363
      }
 
364
    else
 
365
      dst = fopen( fname, "w" );
323
366
    umask(oldmask);
324
367
    if( !dst ) {
325
 
        log_error(_("%s: can't create: %s\n"), fname, strerror(errno) );
 
368
        log_info (_("can't create `%s': %s\n"), fname, strerror(errno) );
326
369
        fclose( src );
327
 
        xfree (fname);
 
370
        xfree(fname);
328
371
        return;
329
372
    }
330
373
 
354
397
        log_info (_("WARNING: options in `%s'"
355
398
                    " are not yet active during this run\n"),
356
399
                  fname);
357
 
    xfree (fname);
 
400
    xfree(fname);
358
401
}
359
402
 
360
403
 
361
404
void
362
 
try_make_homedir( const char *fname )
 
405
try_make_homedir (const char *fname)
363
406
{
364
 
    const char *defhome = GNUPG_DEFAULT_HOMEDIR;
365
 
 
366
 
    /* Create the directory only if the supplied directory name
367
 
     * is the same as the default one.  This way we avoid to create
368
 
     * arbitrary directories when a non-default homedirectory is used.
369
 
     * To cope with HOME, we do compare only the suffix if we see that
370
 
     * the default homedir does start with a tilde.
371
 
     */
372
 
    if( opt.dry_run || opt.no_homedir_creation )
373
 
        return;
374
 
 
375
 
    if ( ( *defhome == '~'
376
 
           && ( strlen(fname) >= strlen (defhome+1)
377
 
                && !strcmp(fname+strlen(fname)-strlen(defhome+1),
378
 
                           defhome+1 ) ))
379
 
         || ( *defhome != '~'
380
 
              && !compare_filenames( fname, defhome ) )
381
 
        ) {
382
 
        if( mkdir( fname, S_IRUSR|S_IWUSR|S_IXUSR ) )
383
 
            log_fatal( _("%s: can't create directory: %s\n"),
384
 
                                        fname,  strerror(errno) );
385
 
        else if( !opt.quiet )
386
 
            log_info( _("%s: directory created\n"), fname );
387
 
        copy_options_file( fname );
388
 
/*      log_info(_("you have to start GnuPG again, " */
389
 
/*                 "so it can read the new configuration file\n") ); */
390
 
/*      g10_exit(1); */
 
407
  const char *defhome = standard_homedir ();
 
408
 
 
409
  /* Create the directory only if the supplied directory name is the
 
410
     same as the default one.  This way we avoid to create arbitrary
 
411
     directories when a non-default home directory is used.  To cope
 
412
     with HOME, we do compare only the suffix if we see that the
 
413
     default homedir does start with a tilde.  */
 
414
  if ( opt.dry_run || opt.no_homedir_creation )
 
415
    return;
 
416
 
 
417
  if (
 
418
#ifdef HAVE_W32_SYSTEM
 
419
      ( !compare_filenames (fname, defhome) )
 
420
#else
 
421
      ( *defhome == '~'
 
422
        && (strlen(fname) >= strlen (defhome+1)
 
423
            && !strcmp(fname+strlen(fname)-strlen(defhome+1), defhome+1 ) ))
 
424
      || (*defhome != '~'  && !compare_filenames( fname, defhome ) )
 
425
#endif
 
426
      )
 
427
    {
 
428
      if ( mkdir (fname, S_IRUSR|S_IWUSR|S_IXUSR) )
 
429
        log_fatal ( _("can't create directory `%s': %s\n"),
 
430
                    fname, strerror(errno) );
 
431
      else if (!opt.quiet )
 
432
        log_info ( _("directory `%s' created\n"), fname );
 
433
      copy_options_file( fname );
 
434
      
391
435
    }
392
436
}