~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-updates

« back to all changes in this revision

Viewing changes to g10/decrypt.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
/* decrypt.c - verify signed data
2
 
 * Copyright (C) 1998,1999,2000,2001,2002,2003 Free Software Foundation, Inc.
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
 
3
 *               2007 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>
25
25
#include <errno.h>
26
26
#include <assert.h>
27
27
 
 
28
#include "gpg.h"
28
29
#include "options.h"
29
30
#include "packet.h"
30
 
#include "errors.h"
 
31
#include "status.h"
31
32
#include "iobuf.h"
32
33
#include "keydb.h"
33
 
#include "memory.h"
34
34
#include "util.h"
35
35
#include "main.h"
36
36
#include "status.h"
49
49
int
50
50
decrypt_message( const char *filename )
51
51
{
52
 
    iobuf_t fp;
53
 
    armor_filter_context_t afx;
54
 
    progress_filter_context_t pfx;
 
52
    IOBUF fp;
 
53
    armor_filter_context_t *afx = NULL;
 
54
    progress_filter_context_t *pfx;
55
55
    int rc;
56
 
    int no_out=0;
57
 
 
58
 
    /* open the message file */
 
56
    int no_out = 0;
 
57
 
 
58
    pfx = new_progress_context ();
 
59
 
 
60
    /* Open the message file.  */
59
61
    fp = iobuf_open(filename);
 
62
    if (fp && is_secured_file (iobuf_get_fd (fp)))
 
63
      {
 
64
        iobuf_close (fp);
 
65
        fp = NULL;
 
66
        errno = EPERM;
 
67
      }
60
68
    if( !fp ) {
61
 
        rc = gpg_error_from_errno (errno);
62
 
        log_error(_("can't open `%s'\n"), print_fname_stdin(filename));
 
69
        rc = gpg_error_from_syserror ();
 
70
        log_error (_("can't open `%s': %s\n"), print_fname_stdin(filename),
 
71
                   gpg_strerror (rc));
 
72
        release_progress_context (pfx);
63
73
        return rc;
64
74
    }
65
75
 
66
 
    handle_progress (&pfx, fp, filename);
 
76
    handle_progress (pfx, fp, filename);
67
77
 
68
78
    if( !opt.no_armor ) {
69
79
        if( use_armor_filter( fp ) ) {
70
 
            memset( &afx, 0, sizeof afx);
71
 
            iobuf_push_filter( fp, armor_filter, &afx );
 
80
            afx = new_armor_context ();
 
81
            push_armor_filter ( afx, fp );
72
82
        }
73
83
    }
74
84
 
80
90
    if( no_out )
81
91
       opt.outfile = NULL;
82
92
    iobuf_close(fp);
 
93
    release_armor_context (afx);
 
94
    release_progress_context (pfx);
83
95
    return rc;
84
96
}
85
97
 
86
98
void
87
 
decrypt_messages(int nfiles, char **files)
 
99
decrypt_messages(int nfiles, char *files[])
88
100
{
89
 
  iobuf_t fp;
90
 
  armor_filter_context_t afx;  
91
 
  progress_filter_context_t pfx;
 
101
  IOBUF fp;
 
102
  armor_filter_context_t *afx = NULL;  
 
103
  progress_filter_context_t *pfx;
92
104
  char *p, *output = NULL;
93
 
  int rc = 0;
 
105
  int rc=0,use_stdin=0;
 
106
  unsigned int lno=0;
94
107
  
95
108
  if (opt.outfile)
96
109
    {
97
110
      log_error(_("--output doesn't work for this command\n"));
98
111
      return;
99
 
        
100
112
    }
101
113
 
102
 
  while (nfiles--)
 
114
  pfx = new_progress_context ();
 
115
 
 
116
  if(!nfiles)
 
117
    use_stdin=1;
 
118
 
 
119
  for(;;)
103
120
    {
104
 
      print_file_status(STATUS_FILE_START, *files, 3);      
105
 
      output = make_outfile_name(*files);
 
121
      char line[2048];
 
122
      char *filename=NULL;
 
123
 
 
124
      if(use_stdin)
 
125
        {
 
126
          if(fgets(line, DIM(line), stdin))
 
127
            {
 
128
              lno++;
 
129
              if (!*line || line[strlen(line)-1] != '\n')
 
130
                log_error("input line %u too long or missing LF\n", lno);
 
131
              else
 
132
                {
 
133
                  line[strlen(line)-1] = '\0';
 
134
                  filename=line;
 
135
                }
 
136
            }
 
137
        }
 
138
      else
 
139
        {
 
140
          if(nfiles)
 
141
            {
 
142
              filename=*files;
 
143
              nfiles--;
 
144
              files++;
 
145
            }
 
146
        }
 
147
 
 
148
      if(filename==NULL)
 
149
        break;
 
150
 
 
151
      print_file_status(STATUS_FILE_START, filename, 3);      
 
152
      output = make_outfile_name(filename);
106
153
      if (!output)
107
154
        goto next_file;
108
 
      fp = iobuf_open(*files);
 
155
      fp = iobuf_open(filename);
 
156
      if (fp)
 
157
        iobuf_ioctl (fp,3,1,NULL); /* disable fd caching */
 
158
      if (fp && is_secured_file (iobuf_get_fd (fp)))
 
159
        {
 
160
          iobuf_close (fp);
 
161
          fp = NULL;
 
162
          errno = EPERM;
 
163
        }
109
164
      if (!fp)
110
165
        {
111
 
          log_error(_("can't open `%s'\n"), print_fname_stdin(*files));
 
166
          log_error(_("can't open `%s'\n"), print_fname_stdin(filename));
112
167
          goto next_file;
113
168
        }
114
169
 
115
 
      handle_progress (&pfx, fp, *files);
 
170
      handle_progress (pfx, fp, filename);
116
171
 
117
172
      if (!opt.no_armor)
118
173
        {
119
174
          if (use_armor_filter(fp))
120
175
            {
121
 
              memset(&afx, 0, sizeof afx);
122
 
              iobuf_push_filter(fp, armor_filter, &afx);
 
176
              afx = new_armor_context ();
 
177
              push_armor_filter ( afx, fp );
123
178
            }
124
179
        }
125
180
      rc = proc_packets(NULL, fp);
126
181
      iobuf_close(fp);
127
182
      if (rc)
128
 
        log_error("%s: decryption failed: %s\n", print_fname_stdin(*files),
129
 
                  gpg_strerror (rc));
 
183
        log_error("%s: decryption failed: %s\n", print_fname_stdin(filename),
 
184
                  g10_errstr(rc));
130
185
      p = get_last_passphrase();
131
186
      set_next_passphrase(p);
132
187
      xfree (p);
134
189
    next_file:
135
190
      /* Note that we emit file_done even after an error. */
136
191
      write_status( STATUS_FILE_DONE );
137
 
      xfree (output);
138
 
      files++;
 
192
      xfree(output);
 
193
      reset_literals_seen();
139
194
    }
 
195
 
140
196
  set_next_passphrase(NULL);  
 
197
  release_armor_context (afx);
 
198
  release_progress_context (pfx);
141
199
}
142