~ubuntu-branches/debian/experimental/apt/experimental

« back to all changes in this revision

Viewing changes to methods/gpgv.cc

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2010-02-18 22:07:23 UTC
  • mfrom: (9.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100218220723-zb7zdh6fmsmp30tr
Tags: 0.7.26~exp2
fix crash when LANGUAGE is not set

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <apt-pkg/contrib/error.h>
 
1
#include <apt-pkg/error.h>
2
2
#include <apt-pkg/acquire-method.h>
3
 
#include <apt-pkg/contrib/strutl.h>
 
3
#include <apt-pkg/strutl.h>
 
4
#include <apt-pkg/fileutl.h>
4
5
#include <apti18n.h>
5
6
 
6
 
#include <sys/stat.h>
7
 
#include <unistd.h>
8
7
#include <utime.h>
9
8
#include <stdio.h>
10
9
#include <fcntl.h>
17
16
#define GNUPGBADSIG "[GNUPG:] BADSIG"
18
17
#define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
19
18
#define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
 
19
#define GNUPGGOODSIG "[GNUPG:] GOODSIG"
 
20
#define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
 
21
#define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
20
22
#define GNUPGNODATA "[GNUPG:] NODATA"
21
23
 
22
24
class GPGVMethod : public pkgAcqMethod
23
25
{
24
26
   private:
25
27
   string VerifyGetSigners(const char *file, const char *outfile,
26
 
                                vector<string> &GoodSigners, vector<string> &BadSigners,
 
28
                                vector<string> &GoodSigners, 
 
29
                                vector<string> &BadSigners,
 
30
                                vector<string> &WorthlessSigners,
27
31
                                vector<string> &NoPubKeySigners);
28
32
   
29
33
   protected:
37
41
string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
38
42
                                         vector<string> &GoodSigners,
39
43
                                         vector<string> &BadSigners,
 
44
                                         vector<string> &WorthlessSigners,
40
45
                                         vector<string> &NoPubKeySigners)
41
46
{
 
47
   bool const Debug = _config->FindB("Debug::Acquire::gpgv", false);
42
48
   // setup a (empty) stringstream for formating the return value
43
49
   std::stringstream ret;
44
50
   ret.str("");
45
51
 
46
 
   if (_config->FindB("Debug::Acquire::gpgv", false))
47
 
   {
48
 
      std::cerr << "inside VerifyGetSigners" << std::endl;
49
 
   }
 
52
   if (Debug == true)
 
53
      std::clog << "inside VerifyGetSigners" << std::endl;
 
54
 
50
55
   pid_t pid;
51
56
   int fd[2];
52
57
   FILE *pipein;
53
58
   int status;
54
 
   struct stat buff;
55
 
   string gpgvpath = _config->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
56
 
   string pubringpath = _config->Find("APT::GPGV::TrustedKeyring", "/etc/apt/trusted.gpg");
57
 
   if (_config->FindB("Debug::Acquire::gpgv", false))
 
59
   string const gpgvpath = _config->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
 
60
   // FIXME: remove support for deprecated APT::GPGV setting
 
61
   string const trustedFile = _config->FindFile("Dir::Etc::Trusted",
 
62
                        _config->Find("APT::GPGV::TrustedKeyring", "/etc/apt/trusted.gpg").c_str());
 
63
   string const trustedPath = _config->FindDir("Dir::Etc::TrustedParts", "/etc/apt/trusted.gpg.d");
 
64
   if (Debug == true)
58
65
   {
59
 
      std::cerr << "gpgv path: " << gpgvpath << std::endl;
60
 
      std::cerr << "Keyring path: " << pubringpath << std::endl;
 
66
      std::clog << "gpgv path: " << gpgvpath << std::endl;
 
67
      std::clog << "Keyring file: " << trustedFile << std::endl;
 
68
      std::clog << "Keyring path: " << trustedPath << std::endl;
61
69
   }
62
70
 
63
 
   if (stat(pubringpath.c_str(), &buff) != 0) 
 
71
   vector<string> keyrings = GetListOfFilesInDir(trustedPath, "gpg", false);
 
72
   if (FileExists(trustedFile) == true)
 
73
      keyrings.push_back(trustedFile);
 
74
 
 
75
   if (keyrings.empty() == true)
64
76
   {
65
 
      ioprintf(ret, _("Couldn't access keyring: '%s'"), strerror(errno)); 
 
77
      // TRANSLATOR: %s is the trusted keyring parts directory
 
78
      ioprintf(ret, _("No keyring installed in %s."), trustedPath.c_str());
66
79
      return ret.str();
67
80
   }
 
81
 
68
82
   if (pipe(fd) < 0)
69
 
   {
70
83
      return "Couldn't create pipe";
71
 
   }
72
84
 
73
85
   pid = fork();
74
86
   if (pid < 0)
75
 
   {
76
87
      return string("Couldn't spawn new process") + strerror(errno);
77
 
   }
78
88
   else if (pid == 0)
79
89
   {
80
90
      const char *Args[400];
84
94
      Args[i++] = "--status-fd";
85
95
      Args[i++] = "3";
86
96
      Args[i++] = "--ignore-time-conflict";
87
 
      Args[i++] = "--keyring";
88
 
      Args[i++] = pubringpath.c_str();
 
97
      for (vector<string>::const_iterator K = keyrings.begin();
 
98
           K != keyrings.end(); ++K)
 
99
      {
 
100
         Args[i++] = "--keyring";
 
101
         Args[i++] = K->c_str();
 
102
         // check overflow (minus a bit of extra space at the end)
 
103
         if(i >= sizeof(Args)/sizeof(char*)-5) {
 
104
            std::clog << _("E: Too many keyrings should be passed to gpgv. Exiting.") << std::endl;
 
105
            exit(111);
 
106
         }
 
107
      }
89
108
 
90
109
      Configuration::Item const *Opts;
91
110
      Opts = _config->Tree("Acquire::gpgv::Options");
97
116
            if (Opts->Value.empty() == true)
98
117
               continue;
99
118
            Args[i++] = Opts->Value.c_str();
100
 
            if(i >= 395) { 
101
 
               std::cerr << _("E: Argument list from Acquire::gpgv::Options too long. Exiting.") << std::endl;
 
119
            // check overflow (minus a bit of extra space at the end)
 
120
            if(i >= sizeof(Args)/sizeof(char*)-5) { 
 
121
               std::clog << _("E: Argument list from Acquire::gpgv::Options too long. Exiting.") << std::endl;
102
122
               exit(111);
103
123
            }
104
124
         }
107
127
      Args[i++] = outfile;
108
128
      Args[i++] = NULL;
109
129
 
110
 
      if (_config->FindB("Debug::Acquire::gpgv", false))
 
130
      if (Debug == true)
111
131
      {
112
 
         std::cerr << "Preparing to exec: " << gpgvpath;
 
132
         std::clog << "Preparing to exec: " << gpgvpath;
113
133
         for(unsigned int j=0;Args[j] != NULL; j++)
114
 
            std::cerr << " " << Args[j];
115
 
         std::cerr << std::endl;
 
134
            std::clog << " " << Args[j];
 
135
         std::clog << std::endl;
116
136
      }
117
 
      int nullfd = open("/dev/null", O_RDONLY);
 
137
      int const nullfd = open("/dev/null", O_RDONLY);
118
138
      close(fd[0]);
119
139
      // Redirect output to /dev/null; we read from the status fd
120
140
      dup2(nullfd, STDOUT_FILENO); 
153
173
         break;
154
174
      *(buffer+bufferoff) = '\0';
155
175
      bufferoff = 0;
156
 
      if (_config->FindB("Debug::Acquire::gpgv", false))
157
 
         std::cerr << "Read: " << buffer << std::endl;
 
176
      if (Debug == true)
 
177
         std::clog << "Read: " << buffer << std::endl;
158
178
 
159
179
      // Push the data into three separate vectors, which
160
180
      // we later concatenate.  They're kept separate so
162
182
      // it will be better.
163
183
      if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0)
164
184
      {
165
 
         if (_config->FindB("Debug::Acquire::gpgv", false))
166
 
            std::cerr << "Got BADSIG! " << std::endl;
 
185
         if (Debug == true)
 
186
            std::clog << "Got BADSIG! " << std::endl;
167
187
         BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
168
188
      }
169
189
      
170
190
      if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0)
171
191
      {
172
 
         if (_config->FindB("Debug::Acquire::gpgv", false))
173
 
            std::cerr << "Got NO_PUBKEY " << std::endl;
 
192
         if (Debug == true)
 
193
            std::clog << "Got NO_PUBKEY " << std::endl;
174
194
         NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
175
195
      }
176
196
      if (strncmp(buffer, GNUPGNODATA, sizeof(GNUPGBADSIG)-1) == 0)
177
197
      {
178
 
         if (_config->FindB("Debug::Acquire::gpgv", false))
179
 
            std::cerr << "Got NODATA! " << std::endl;
 
198
         if (Debug == true)
 
199
            std::clog << "Got NODATA! " << std::endl;
180
200
         BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
181
201
      }
182
 
      if (strncmp(buffer, GNUPGVALIDSIG, sizeof(GNUPGVALIDSIG)-1) == 0)
 
202
      if (strncmp(buffer, GNUPGKEYEXPIRED, sizeof(GNUPGKEYEXPIRED)-1) == 0)
 
203
      {
 
204
         if (Debug == true)
 
205
            std::clog << "Got KEYEXPIRED! " << std::endl;
 
206
         WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
 
207
      }
 
208
      if (strncmp(buffer, GNUPGREVKEYSIG, sizeof(GNUPGREVKEYSIG)-1) == 0)
 
209
      {
 
210
         if (Debug == true)
 
211
            std::clog << "Got REVKEYSIG! " << std::endl;
 
212
         WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
 
213
      }
 
214
      if (strncmp(buffer, GNUPGGOODSIG, sizeof(GNUPGGOODSIG)-1) == 0)
183
215
      {
184
216
         char *sig = buffer + sizeof(GNUPGPREFIX);
185
 
         char *p = sig + sizeof("VALIDSIG");
 
217
         char *p = sig + sizeof("GOODSIG");
186
218
         while (*p && isxdigit(*p)) 
187
219
            p++;
188
220
         *p = 0;
189
 
         if (_config->FindB("Debug::Acquire::gpgv", false))
190
 
            std::cerr << "Got VALIDSIG, key ID:" << sig << std::endl;
 
221
         if (Debug == true)
 
222
            std::clog << "Got GOODSIG, key ID:" << sig << std::endl;
191
223
         GoodSigners.push_back(string(sig));
192
224
      }
193
225
   }
194
226
   fclose(pipein);
195
227
 
196
228
   waitpid(pid, &status, 0);
197
 
   if (_config->FindB("Debug::Acquire::gpgv", false))
 
229
   if (Debug == true)
198
230
   {
199
 
      std::cerr << "gpgv exited\n";
 
231
      std::clog << "gpgv exited\n";
200
232
   }
201
233
   
202
234
   if (WEXITSTATUS(status) == 0)
227
259
   string keyID;
228
260
   vector<string> GoodSigners;
229
261
   vector<string> BadSigners;
 
262
   // a worthless signature is a expired or revoked one
 
263
   vector<string> WorthlessSigners;
230
264
   vector<string> NoPubKeySigners;
231
265
   
232
266
   FetchResult Res;
235
269
 
236
270
   // Run gpgv on file, extract contents and get the key ID of the signer
237
271
   string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(),
238
 
                              GoodSigners, BadSigners, NoPubKeySigners);
 
272
                                 GoodSigners, BadSigners, WorthlessSigners,
 
273
                                 NoPubKeySigners);
239
274
   if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty())
240
275
   {
241
276
      string errmsg;
242
277
      // In this case, something bad probably happened, so we just go
243
278
      // with what the other method gave us for an error message.
244
 
      if (BadSigners.empty() && NoPubKeySigners.empty())
 
279
      if (BadSigners.empty() && WorthlessSigners.empty() && NoPubKeySigners.empty())
245
280
         errmsg = msg;
246
281
      else
247
282
      {
252
287
                 I != BadSigners.end(); I++)
253
288
               errmsg += (*I + "\n");
254
289
         }
 
290
         if (!WorthlessSigners.empty())
 
291
         {
 
292
            errmsg += _("The following signatures were invalid:\n");
 
293
            for (vector<string>::iterator I = WorthlessSigners.begin();
 
294
                 I != WorthlessSigners.end(); I++)
 
295
               errmsg += (*I + "\n");
 
296
         }
255
297
         if (!NoPubKeySigners.empty())
256
298
         {
257
299
             errmsg += _("The following signatures couldn't be verified because the public key is not available:\n");
264
306
      // least one bad signature. good signatures and NoPubKey signatures
265
307
      // happen easily when a file is signed with multiple signatures
266
308
      if(GoodSigners.empty() or !BadSigners.empty())
267
 
         return _error->Error(errmsg.c_str());
 
309
         return _error->Error("%s", errmsg.c_str());
268
310
   }
269
311
      
270
312
   // Just pass the raw output up, because passing it as a real data
277
319
 
278
320
   if (_config->FindB("Debug::Acquire::gpgv", false))
279
321
   {
280
 
      std::cerr << "gpgv succeeded\n";
 
322
      std::clog << "gpgv succeeded\n";
281
323
   }
282
324
 
283
325
   return true;