]>
git.saurik.com Git - apt.git/blob - methods/gpgv.cc
1 #include <apt-pkg/error.h>
2 #include <apt-pkg/acquire-method.h>
3 #include <apt-pkg/strutl.h>
16 #define GNUPGPREFIX "[GNUPG:]"
17 #define GNUPGBADSIG "[GNUPG:] BADSIG"
18 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
19 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
20 #define GNUPGNODATA "[GNUPG:] NODATA"
22 class GPGVMethod
: public pkgAcqMethod
25 string
VerifyGetSigners(const char *file
, const char *outfile
,
26 vector
<string
> &GoodSigners
, vector
<string
> &BadSigners
,
27 vector
<string
> &NoPubKeySigners
);
30 virtual bool Fetch(FetchItem
*Itm
);
34 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
37 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
38 vector
<string
> &GoodSigners
,
39 vector
<string
> &BadSigners
,
40 vector
<string
> &NoPubKeySigners
)
42 // setup a (empty) stringstream for formating the return value
43 std::stringstream ret
;
46 if (_config
->FindB("Debug::Acquire::gpgv", false))
48 std::cerr
<< "inside VerifyGetSigners" << std::endl
;
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 std::cerr
<< "gpgv path: " << gpgvpath
<< std::endl
;
60 std::cerr
<< "Keyring path: " << pubringpath
<< std::endl
;
63 if (stat(pubringpath
.c_str(), &buff
) != 0)
65 ioprintf(ret
, _("Couldn't access keyring: '%s'"), strerror(errno
));
70 return "Couldn't create pipe";
76 return string("Couldn't spawn new process") + strerror(errno
);
80 const char *Args
[400];
83 Args
[i
++] = gpgvpath
.c_str();
84 Args
[i
++] = "--status-fd";
86 Args
[i
++] = "--keyring";
87 Args
[i
++] = pubringpath
.c_str();
89 Configuration::Item
const *Opts
;
90 Opts
= _config
->Tree("Acquire::gpgv::Options");
94 for (; Opts
!= 0; Opts
= Opts
->Next
)
96 if (Opts
->Value
.empty() == true)
98 Args
[i
++] = Opts
->Value
.c_str();
100 std::cerr
<< _("E: Argument list from Acquire::gpgv::Options too long. Exiting.") << std::endl
;
109 if (_config
->FindB("Debug::Acquire::gpgv", false))
111 std::cerr
<< "Preparing to exec: " << gpgvpath
;
112 for(unsigned int j
=0;Args
[j
] != NULL
; j
++)
113 std::cerr
<< " " << Args
[j
];
114 std::cerr
<< std::endl
;
116 int nullfd
= open("/dev/null", O_RDONLY
);
118 // Redirect output to /dev/null; we read from the status fd
119 dup2(nullfd
, STDOUT_FILENO
);
120 dup2(nullfd
, STDERR_FILENO
);
121 // Redirect the pipe to the status fd (3)
124 putenv((char *)"LANG=");
125 putenv((char *)"LC_ALL=");
126 putenv((char *)"LC_MESSAGES=");
127 execvp(gpgvpath
.c_str(), (char **)Args
);
133 pipein
= fdopen(fd
[0], "r");
135 // Loop over the output of gpgv, and check the signatures.
136 size_t buffersize
= 64;
137 char *buffer
= (char *) malloc(buffersize
);
138 size_t bufferoff
= 0;
143 // Read a line. Sigh.
144 while ((c
= getc(pipein
)) != EOF
&& c
!= '\n')
146 if (bufferoff
== buffersize
)
147 buffer
= (char *) realloc(buffer
, buffersize
*= 2);
148 *(buffer
+bufferoff
) = c
;
151 if (bufferoff
== 0 && c
== EOF
)
153 *(buffer
+bufferoff
) = '\0';
155 if (_config
->FindB("Debug::Acquire::gpgv", false))
156 std::cerr
<< "Read: " << buffer
<< std::endl
;
158 // Push the data into three separate vectors, which
159 // we later concatenate. They're kept separate so
160 // if we improve the apt method communication stuff later
161 // it will be better.
162 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
164 if (_config
->FindB("Debug::Acquire::gpgv", false))
165 std::cerr
<< "Got BADSIG! " << std::endl
;
166 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
169 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
171 if (_config
->FindB("Debug::Acquire::gpgv", false))
172 std::cerr
<< "Got NO_PUBKEY " << std::endl
;
173 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
175 if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
177 if (_config
->FindB("Debug::Acquire::gpgv", false))
178 std::cerr
<< "Got NODATA! " << std::endl
;
179 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
181 if (strncmp(buffer
, GNUPGVALIDSIG
, sizeof(GNUPGVALIDSIG
)-1) == 0)
183 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
184 char *p
= sig
+ sizeof("VALIDSIG");
185 while (*p
&& isxdigit(*p
))
188 if (_config
->FindB("Debug::Acquire::gpgv", false))
189 std::cerr
<< "Got VALIDSIG, key ID:" << sig
<< std::endl
;
190 GoodSigners
.push_back(string(sig
));
195 waitpid(pid
, &status
, 0);
196 if (_config
->FindB("Debug::Acquire::gpgv", false))
198 std::cerr
<< "gpgv exited\n";
201 if (WEXITSTATUS(status
) == 0)
203 if (GoodSigners
.empty())
204 return _("Internal error: Good signature, but could not determine key fingerprint?!");
207 else if (WEXITSTATUS(status
) == 1)
209 return _("At least one invalid signature was encountered.");
211 else if (WEXITSTATUS(status
) == 111)
213 ioprintf(ret
, _("Could not execute '%s' to verify signature (is gnupg installed?)"), gpgvpath
.c_str());
218 return _("Unknown error executing gpgv");
222 bool GPGVMethod::Fetch(FetchItem
*Itm
)
225 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
227 vector
<string
> GoodSigners
;
228 vector
<string
> BadSigners
;
229 vector
<string
> NoPubKeySigners
;
232 Res
.Filename
= Itm
->DestFile
;
235 // Run gpgv on file, extract contents and get the key ID of the signer
236 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(),
237 GoodSigners
, BadSigners
, NoPubKeySigners
);
238 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
241 // In this case, something bad probably happened, so we just go
242 // with what the other method gave us for an error message.
243 if (BadSigners
.empty() && NoPubKeySigners
.empty())
247 if (!BadSigners
.empty())
249 errmsg
+= _("The following signatures were invalid:\n");
250 for (vector
<string
>::iterator I
= BadSigners
.begin();
251 I
!= BadSigners
.end(); I
++)
252 errmsg
+= (*I
+ "\n");
254 if (!NoPubKeySigners
.empty())
256 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
257 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
258 I
!= NoPubKeySigners
.end(); I
++)
259 errmsg
+= (*I
+ "\n");
262 // this is only fatal if we have no good sigs or if we have at
263 // least one bad signature. good signatures and NoPubKey signatures
264 // happen easily when a file is signed with multiple signatures
265 if(GoodSigners
.empty() or !BadSigners
.empty())
266 return _error
->Error(errmsg
.c_str());
269 // Transfer the modification times
271 if (stat(Path
.c_str(),&Buf
) != 0)
272 return _error
->Errno("stat",_("Failed to stat %s"), Path
.c_str());
274 struct utimbuf TimeBuf
;
275 TimeBuf
.actime
= Buf
.st_atime
;
276 TimeBuf
.modtime
= Buf
.st_mtime
;
277 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
278 return _error
->Errno("utime",_("Failed to set modification time"));
280 if (stat(Itm
->DestFile
.c_str(),&Buf
) != 0)
281 return _error
->Errno("stat",_("Failed to stat"));
283 // Return a Done response
284 Res
.LastModified
= Buf
.st_mtime
;
285 Res
.Size
= Buf
.st_size
;
286 // Just pass the raw output up, because passing it as a real data
287 // structure is too difficult with the method stuff. We keep it
288 // as three separate vectors for future extensibility.
289 Res
.GPGVOutput
= GoodSigners
;
290 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
291 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
294 if (_config
->FindB("Debug::Acquire::gpgv", false))
296 std::cerr
<< "gpgv succeeded\n";
305 setlocale(LC_ALL
, "");