]>
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>
14 #define GNUPGPREFIX "[GNUPG:]"
15 #define GNUPGBADSIG "[GNUPG:] BADSIG"
16 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
17 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
19 class GPGVMethod
: public pkgAcqMethod
22 const char *VerifyGetSigners(const char *file
, const char *outfile
,
23 vector
<string
> &GoodSigners
, vector
<string
> &BadSigners
,
24 vector
<string
> &NoPubKeySigners
);
27 virtual bool Fetch(FetchItem
*Itm
);
31 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
34 const char *GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
35 vector
<string
> &GoodSigners
,
36 vector
<string
> &BadSigners
,
37 vector
<string
> &NoPubKeySigners
)
39 if (_config
->FindB("Debug::Acquire::gpgv", false))
41 std::cerr
<< "inside VerifyGetSigners" << std::endl
;
48 string gpgvpath
= _config
->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
49 string pubringpath
= _config
->Find("Apt::GPGV::TrustedKeyring", "/etc/apt/trusted.gpg");
50 if (_config
->FindB("Debug::Acquire::gpgv", false))
52 std::cerr
<< "gpgv path: " << gpgvpath
<< std::endl
;
53 std::cerr
<< "Keyring path: " << pubringpath
<< std::endl
;
56 if (stat(pubringpath
.c_str(), &buff
) != 0)
57 return (string("Couldn't access keyring: ") + strerror(errno
)).c_str();
61 return "Couldn't create pipe";
67 return (string("Couldn't spawn new process") + strerror(errno
)).c_str();
71 if (_config
->FindB("Debug::Acquire::gpgv", false))
73 std::cerr
<< "Preparing to exec: " << gpgvpath
74 << " --status-fd 3 --keyring " << pubringpath
75 << " " << file
<< " " << outfile
<< std::endl
;
77 int nullfd
= open("/dev/null", O_RDONLY
);
79 // Redirect output to /dev/null; we read from the status fd
80 dup2(nullfd
, STDOUT_FILENO
);
81 dup2(nullfd
, STDERR_FILENO
);
82 // Redirect the pipe to the status fd (3)
87 putenv("LC_MESSAGES=");
88 execlp(gpgvpath
.c_str(), gpgvpath
.c_str(), "--status-fd", "3", "--keyring",
89 pubringpath
.c_str(), file
, outfile
, NULL
);
95 pipein
= fdopen(fd
[0], "r");
97 // Loop over the output of gpgv, and check the signatures.
98 size_t buffersize
= 64;
99 char *buffer
= (char *) malloc(buffersize
);
100 size_t bufferoff
= 0;
105 // Read a line. Sigh.
106 while ((c
= getc(pipein
)) != EOF
&& c
!= '\n')
108 if (bufferoff
== buffersize
)
109 buffer
= (char *) realloc(buffer
, buffersize
*= 2);
110 *(buffer
+bufferoff
) = c
;
113 if (bufferoff
== 0 && c
== EOF
)
115 *(buffer
+bufferoff
) = '\0';
117 if (_config
->FindB("Debug::Acquire::gpgv", false))
118 std::cerr
<< "Read: " << buffer
<< std::endl
;
120 // Push the data into three separate vectors, which
121 // we later concatenate. They're kept separate so
122 // if we improve the apt method communication stuff later
123 // it will be better.
124 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
126 if (_config
->FindB("Debug::Acquire::gpgv", false))
127 std::cerr
<< "Got BADSIG! " << std::endl
;
128 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
131 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
133 if (_config
->FindB("Debug::Acquire::gpgv", false))
134 std::cerr
<< "Got NO_PUBKEY " << std::endl
;
135 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
138 if (strncmp(buffer
, GNUPGVALIDSIG
, sizeof(GNUPGVALIDSIG
)-1) == 0)
140 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
141 char *p
= sig
+ sizeof("VALIDSIG");
142 while (*p
&& isxdigit(*p
))
145 if (_config
->FindB("Debug::Acquire::gpgv", false))
146 std::cerr
<< "Got VALIDSIG, key ID:" << sig
<< std::endl
;
147 GoodSigners
.push_back(string(sig
));
152 waitpid(pid
, &status
, 0);
153 if (_config
->FindB("Debug::Acquire::gpgv", false))
155 std::cerr
<<"gpgv exited\n";
158 if (WEXITSTATUS(status
) == 0)
160 if (GoodSigners
.empty())
161 return "Internal error: Good signature, but could not determine key fingerprint?!";
164 else if (WEXITSTATUS(status
) == 1)
166 return "At least one invalid signature was encountered.";
168 else if (WEXITSTATUS(status
) == 111)
170 return (string("Could not execute ") + gpgvpath
+
171 string(" to verify signature (is gnupg installed?)")).c_str();
175 return "Unknown error executing gpgv";
179 bool GPGVMethod::Fetch(FetchItem
*Itm
)
182 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
184 vector
<string
> GoodSigners
;
185 vector
<string
> BadSigners
;
186 vector
<string
> NoPubKeySigners
;
189 Res
.Filename
= Itm
->DestFile
;
192 // Run gpgv on file, extract contents and get the key ID of the signer
193 const char *msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(),
194 GoodSigners
, BadSigners
, NoPubKeySigners
);
195 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
198 // In this case, something bad probably happened, so we just go
199 // with what the other method gave us for an error message.
200 if (BadSigners
.empty() && NoPubKeySigners
.empty())
204 if (!BadSigners
.empty())
206 errmsg
+= "The following signatures were invalid:\n";
207 for (vector
<string
>::iterator I
= BadSigners
.begin();
208 I
!= BadSigners
.end(); I
++)
209 errmsg
+= (*I
+ "\n");
211 if (!NoPubKeySigners
.empty())
213 errmsg
+= "The following signatures couldn't be verified because the public key is not available:\n";
214 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
215 I
!= NoPubKeySigners
.end(); I
++)
216 errmsg
+= (*I
+ "\n");
219 return _error
->Error(errmsg
.c_str());
222 // Transfer the modification times
224 if (stat(Path
.c_str(),&Buf
) != 0)
225 return _error
->Errno("stat","Failed to stat %s", Path
.c_str());
227 struct utimbuf TimeBuf
;
228 TimeBuf
.actime
= Buf
.st_atime
;
229 TimeBuf
.modtime
= Buf
.st_mtime
;
230 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
231 return _error
->Errno("utime","Failed to set modification time");
233 if (stat(Itm
->DestFile
.c_str(),&Buf
) != 0)
234 return _error
->Errno("stat","Failed to stat");
236 // Return a Done response
237 Res
.LastModified
= Buf
.st_mtime
;
238 Res
.Size
= Buf
.st_size
;
239 // Just pass the raw output up, because passing it as a real data
240 // structure is too difficult with the method stuff. We keep it
241 // as three separate vectors for future extensibility.
242 Res
.GPGVOutput
= GoodSigners
;
243 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
244 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
247 if (_config
->FindB("Debug::Acquire::gpgv", false))
249 std::cerr
<<"gpgv suceeded\n";