Mercurial > hg > rc1
comparison vendor/pear/crypt_gpg/Crypt/GPG/Engine.php @ 0:1e000243b222
vanilla 1.3.3 distro, I hope
author | Charlie Root |
---|---|
date | Thu, 04 Jan 2018 15:50:29 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1e000243b222 |
---|---|
1 <?php | |
2 | |
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ | |
4 | |
5 /** | |
6 * Crypt_GPG is a package to use GPG from PHP | |
7 * | |
8 * This file contains an engine that handles GPG subprocess control and I/O. | |
9 * PHP's process manipulation functions are used to handle the GPG subprocess. | |
10 * | |
11 * PHP version 5 | |
12 * | |
13 * LICENSE: | |
14 * | |
15 * This library is free software; you can redistribute it and/or modify | |
16 * it under the terms of the GNU Lesser General Public License as | |
17 * published by the Free Software Foundation; either version 2.1 of the | |
18 * License, or (at your option) any later version. | |
19 * | |
20 * This library is distributed in the hope that it will be useful, | |
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
23 * Lesser General Public License for more details. | |
24 * | |
25 * You should have received a copy of the GNU Lesser General Public | |
26 * License along with this library; if not, see | |
27 * <http://www.gnu.org/licenses/> | |
28 * | |
29 * @category Encryption | |
30 * @package Crypt_GPG | |
31 * @author Nathan Fredrickson <nathan@silverorange.com> | |
32 * @author Michael Gauthier <mike@silverorange.com> | |
33 * @copyright 2005-2013 silverorange | |
34 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 | |
35 * @link http://pear.php.net/package/Crypt_GPG | |
36 * @link http://www.gnupg.org/ | |
37 */ | |
38 | |
39 /** | |
40 * Crypt_GPG base class. | |
41 */ | |
42 require_once 'Crypt/GPG.php'; | |
43 | |
44 /** | |
45 * GPG exception classes. | |
46 */ | |
47 require_once 'Crypt/GPG/Exceptions.php'; | |
48 | |
49 /** | |
50 * Status/Error handler class. | |
51 */ | |
52 require_once 'Crypt/GPG/ProcessHandler.php'; | |
53 | |
54 /** | |
55 * Process control methods. | |
56 */ | |
57 require_once 'Crypt/GPG/ProcessControl.php'; | |
58 | |
59 /** | |
60 * Information about a created signature | |
61 */ | |
62 require_once 'Crypt/GPG/SignatureCreationInfo.php'; | |
63 | |
64 /** | |
65 * Standard PEAR exception is used if GPG binary is not found. | |
66 */ | |
67 require_once 'PEAR/Exception.php'; | |
68 | |
69 // {{{ class Crypt_GPG_Engine | |
70 | |
71 /** | |
72 * Native PHP Crypt_GPG I/O engine | |
73 * | |
74 * This class is used internally by Crypt_GPG and does not need be used | |
75 * directly. See the {@link Crypt_GPG} class for end-user API. | |
76 * | |
77 * This engine uses PHP's native process control functions to directly control | |
78 * the GPG process. The GPG executable is required to be on the system. | |
79 * | |
80 * All data is passed to the GPG subprocess using file descriptors. This is the | |
81 * most secure method of passing data to the GPG subprocess. | |
82 * | |
83 * @category Encryption | |
84 * @package Crypt_GPG | |
85 * @author Nathan Fredrickson <nathan@silverorange.com> | |
86 * @author Michael Gauthier <mike@silverorange.com> | |
87 * @copyright 2005-2013 silverorange | |
88 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 | |
89 * @link http://pear.php.net/package/Crypt_GPG | |
90 * @link http://www.gnupg.org/ | |
91 */ | |
92 class Crypt_GPG_Engine | |
93 { | |
94 // {{{ constants | |
95 | |
96 /** | |
97 * Size of data chunks that are sent to and retrieved from the IPC pipes. | |
98 * | |
99 * The value of 65536 has been chosen empirically | |
100 * as the one with best performance. | |
101 * | |
102 * @see https://pear.php.net/bugs/bug.php?id=21077 | |
103 */ | |
104 const CHUNK_SIZE = 65536; | |
105 | |
106 /** | |
107 * Standard input file descriptor. This is used to pass data to the GPG | |
108 * process. | |
109 */ | |
110 const FD_INPUT = 0; | |
111 | |
112 /** | |
113 * Standard output file descriptor. This is used to receive normal output | |
114 * from the GPG process. | |
115 */ | |
116 const FD_OUTPUT = 1; | |
117 | |
118 /** | |
119 * Standard output file descriptor. This is used to receive error output | |
120 * from the GPG process. | |
121 */ | |
122 const FD_ERROR = 2; | |
123 | |
124 /** | |
125 * GPG status output file descriptor. The status file descriptor outputs | |
126 * detailed information for many GPG commands. See the second section of | |
127 * the file <b>doc/DETAILS</b> in the | |
128 * {@link http://www.gnupg.org/download/ GPG package} for a detailed | |
129 * description of GPG's status output. | |
130 */ | |
131 const FD_STATUS = 3; | |
132 | |
133 /** | |
134 * Command input file descriptor. This is used for methods requiring | |
135 * passphrases. | |
136 */ | |
137 const FD_COMMAND = 4; | |
138 | |
139 /** | |
140 * Extra message input file descriptor. This is used for passing signed | |
141 * data when verifying a detached signature. | |
142 */ | |
143 const FD_MESSAGE = 5; | |
144 | |
145 /** | |
146 * Minimum version of GnuPG that is supported. | |
147 */ | |
148 const MIN_VERSION = '1.0.2'; | |
149 | |
150 // }}} | |
151 // {{{ private class properties | |
152 | |
153 /** | |
154 * Whether or not to use strict mode | |
155 * | |
156 * When set to true, any clock problems (e.g. keys generate in future) | |
157 * are errors, otherwise they are just warnings. | |
158 * | |
159 * Strict mode is disabled by default. | |
160 * | |
161 * @var boolean | |
162 * @see Crypt_GPG_Engine::__construct() | |
163 */ | |
164 private $_strict = false; | |
165 | |
166 /** | |
167 * Whether or not to use debugging mode | |
168 * | |
169 * When set to true, every GPG command is echoed before it is run. Sensitive | |
170 * data is always handled using pipes and is not specified as part of the | |
171 * command. As a result, sensitive data is never displayed when debug is | |
172 * enabled. Sensitive data includes private key data and passphrases. | |
173 * | |
174 * This can be set to a callable function where first argument is the | |
175 * debug line to process. | |
176 * | |
177 * Debugging is off by default. | |
178 * | |
179 * @var mixed | |
180 * @see Crypt_GPG_Engine::__construct() | |
181 */ | |
182 private $_debug = false; | |
183 | |
184 /** | |
185 * Location of GPG binary | |
186 * | |
187 * @var string | |
188 * @see Crypt_GPG_Engine::__construct() | |
189 * @see Crypt_GPG_Engine::_getBinary() | |
190 */ | |
191 private $_binary = ''; | |
192 | |
193 /** | |
194 * Location of GnuPG agent binary | |
195 * | |
196 * Only used for GnuPG 2.x | |
197 * | |
198 * @var string | |
199 * @see Crypt_GPG_Engine::__construct() | |
200 * @see Crypt_GPG_Engine::_getAgent() | |
201 */ | |
202 private $_agent = ''; | |
203 | |
204 /** | |
205 * Location of GnuPG conf binary | |
206 * | |
207 * Only used for GnuPG 2.1.x | |
208 * | |
209 * @var string | |
210 * @see Crypt_GPG_Engine::__construct() | |
211 * @see Crypt_GPG_Engine::_getGPGConf() | |
212 */ | |
213 private $_gpgconf = null; | |
214 | |
215 /** | |
216 * Directory containing the GPG key files | |
217 * | |
218 * This property only contains the path when the <i>homedir</i> option | |
219 * is specified in the constructor. | |
220 * | |
221 * @var string | |
222 * @see Crypt_GPG_Engine::__construct() | |
223 */ | |
224 private $_homedir = ''; | |
225 | |
226 /** | |
227 * File path of the public keyring | |
228 * | |
229 * This property only contains the file path when the <i>public_keyring</i> | |
230 * option is specified in the constructor. | |
231 * | |
232 * If the specified file path starts with <kbd>~/</kbd>, the path is | |
233 * relative to the <i>homedir</i> if specified, otherwise to | |
234 * <kbd>~/.gnupg</kbd>. | |
235 * | |
236 * @var string | |
237 * @see Crypt_GPG_Engine::__construct() | |
238 */ | |
239 private $_publicKeyring = ''; | |
240 | |
241 /** | |
242 * File path of the private (secret) keyring | |
243 * | |
244 * This property only contains the file path when the <i>private_keyring</i> | |
245 * option is specified in the constructor. | |
246 * | |
247 * If the specified file path starts with <kbd>~/</kbd>, the path is | |
248 * relative to the <i>homedir</i> if specified, otherwise to | |
249 * <kbd>~/.gnupg</kbd>. | |
250 * | |
251 * @var string | |
252 * @see Crypt_GPG_Engine::__construct() | |
253 */ | |
254 private $_privateKeyring = ''; | |
255 | |
256 /** | |
257 * File path of the trust database | |
258 * | |
259 * This property only contains the file path when the <i>trust_db</i> | |
260 * option is specified in the constructor. | |
261 * | |
262 * If the specified file path starts with <kbd>~/</kbd>, the path is | |
263 * relative to the <i>homedir</i> if specified, otherwise to | |
264 * <kbd>~/.gnupg</kbd>. | |
265 * | |
266 * @var string | |
267 * @see Crypt_GPG_Engine::__construct() | |
268 */ | |
269 private $_trustDb = ''; | |
270 | |
271 /** | |
272 * Array of pipes used for communication with the GPG binary | |
273 * | |
274 * This is an array of file descriptor resources. | |
275 * | |
276 * @var array | |
277 */ | |
278 private $_pipes = array(); | |
279 | |
280 /** | |
281 * Array of pipes used for communication with the gpg-agent binary | |
282 * | |
283 * This is an array of file descriptor resources. | |
284 * | |
285 * @var array | |
286 */ | |
287 private $_agentPipes = array(); | |
288 | |
289 /** | |
290 * Array of currently opened pipes | |
291 * | |
292 * This array is used to keep track of remaining opened pipes so they can | |
293 * be closed when the GPG subprocess is finished. This array is a subset of | |
294 * the {@link Crypt_GPG_Engine::$_pipes} array and contains opened file | |
295 * descriptor resources. | |
296 * | |
297 * @var array | |
298 * @see Crypt_GPG_Engine::_closePipe() | |
299 */ | |
300 private $_openPipes = array(); | |
301 | |
302 /** | |
303 * A handle for the GPG process | |
304 * | |
305 * @var resource | |
306 */ | |
307 private $_process = null; | |
308 | |
309 /** | |
310 * A handle for the gpg-agent process | |
311 * | |
312 * @var resource | |
313 */ | |
314 private $_agentProcess = null; | |
315 | |
316 /** | |
317 * GPG agent daemon socket and PID for running gpg-agent | |
318 * | |
319 * @var string | |
320 */ | |
321 private $_agentInfo = null; | |
322 | |
323 /** | |
324 * Whether or not the operating system is Darwin (OS X) | |
325 * | |
326 * @var boolean | |
327 */ | |
328 private $_isDarwin = false; | |
329 | |
330 /** | |
331 * Message digest algorithm. | |
332 * | |
333 * @var string | |
334 */ | |
335 private $_digest_algo = null; | |
336 | |
337 /** | |
338 * Symmetric cipher algorithm. | |
339 * | |
340 * @var string | |
341 */ | |
342 private $_cipher_algo = null; | |
343 | |
344 /** | |
345 * Commands to be sent to GPG's command input stream | |
346 * | |
347 * @var string | |
348 * @see Crypt_GPG_Engine::sendCommand() | |
349 */ | |
350 private $_commandBuffer = ''; | |
351 | |
352 /** | |
353 * A status/error handler | |
354 * | |
355 * @var Crypt_GPG_ProcessHanler | |
356 */ | |
357 private $_processHandler = null; | |
358 | |
359 /** | |
360 * Array of status line handlers | |
361 * | |
362 * @var array | |
363 * @see Crypt_GPG_Engine::addStatusHandler() | |
364 */ | |
365 private $_statusHandlers = array(); | |
366 | |
367 /** | |
368 * Array of error line handlers | |
369 * | |
370 * @var array | |
371 * @see Crypt_GPG_Engine::addErrorHandler() | |
372 */ | |
373 private $_errorHandlers = array(); | |
374 | |
375 /** | |
376 * The input source | |
377 * | |
378 * This is data to send to GPG. Either a string or a stream resource. | |
379 * | |
380 * @var string|resource | |
381 * @see Crypt_GPG_Engine::setInput() | |
382 */ | |
383 private $_input = null; | |
384 | |
385 /** | |
386 * The extra message input source | |
387 * | |
388 * Either a string or a stream resource. | |
389 * | |
390 * @var string|resource | |
391 * @see Crypt_GPG_Engine::setMessage() | |
392 */ | |
393 private $_message = null; | |
394 | |
395 /** | |
396 * The output location | |
397 * | |
398 * This is where the output from GPG is sent. Either a string or a stream | |
399 * resource. | |
400 * | |
401 * @var string|resource | |
402 * @see Crypt_GPG_Engine::setOutput() | |
403 */ | |
404 private $_output = ''; | |
405 | |
406 /** | |
407 * The GPG operation to execute | |
408 * | |
409 * @var string | |
410 * @see Crypt_GPG_Engine::setOperation() | |
411 */ | |
412 private $_operation; | |
413 | |
414 /** | |
415 * Arguments for the current operation | |
416 * | |
417 * @var array | |
418 * @see Crypt_GPG_Engine::setOperation() | |
419 */ | |
420 private $_arguments = array(); | |
421 | |
422 /** | |
423 * The version number of the GPG binary | |
424 * | |
425 * @var string | |
426 * @see Crypt_GPG_Engine::getVersion() | |
427 */ | |
428 private $_version = ''; | |
429 | |
430 // }}} | |
431 // {{{ __construct() | |
432 | |
433 /** | |
434 * Creates a new GPG engine | |
435 * | |
436 * Available options are: | |
437 * | |
438 * - <kbd>string homedir</kbd> - the directory where the GPG | |
439 * keyring files are stored. If not | |
440 * specified, Crypt_GPG uses the | |
441 * default of <kbd>~/.gnupg</kbd>. | |
442 * - <kbd>string publicKeyring</kbd> - the file path of the public | |
443 * keyring. Use this if the public | |
444 * keyring is not in the homedir, or | |
445 * if the keyring is in a directory | |
446 * not writable by the process | |
447 * invoking GPG (like Apache). Then | |
448 * you can specify the path to the | |
449 * keyring with this option | |
450 * (/foo/bar/pubring.gpg), and specify | |
451 * a writable directory (like /tmp) | |
452 * using the <i>homedir</i> option. | |
453 * - <kbd>string privateKeyring</kbd> - the file path of the private | |
454 * keyring. Use this if the private | |
455 * keyring is not in the homedir, or | |
456 * if the keyring is in a directory | |
457 * not writable by the process | |
458 * invoking GPG (like Apache). Then | |
459 * you can specify the path to the | |
460 * keyring with this option | |
461 * (/foo/bar/secring.gpg), and specify | |
462 * a writable directory (like /tmp) | |
463 * using the <i>homedir</i> option. | |
464 * - <kbd>string trustDb</kbd> - the file path of the web-of-trust | |
465 * database. Use this if the trust | |
466 * database is not in the homedir, or | |
467 * if the database is in a directory | |
468 * not writable by the process | |
469 * invoking GPG (like Apache). Then | |
470 * you can specify the path to the | |
471 * trust database with this option | |
472 * (/foo/bar/trustdb.gpg), and specify | |
473 * a writable directory (like /tmp) | |
474 * using the <i>homedir</i> option. | |
475 * - <kbd>string binary</kbd> - the location of the GPG binary. If | |
476 * not specified, the driver attempts | |
477 * to auto-detect the GPG binary | |
478 * location using a list of known | |
479 * default locations for the current | |
480 * operating system. The option | |
481 * <kbd>gpgBinary</kbd> is a | |
482 * deprecated alias for this option. | |
483 * - <kbd>string agent</kbd> - the location of the GnuPG agent | |
484 * binary. The gpg-agent is only | |
485 * used for GnuPG 2.x. If not | |
486 * specified, the engine attempts | |
487 * to auto-detect the gpg-agent | |
488 * binary location using a list of | |
489 * know default locations for the | |
490 * current operating system. | |
491 * - <kbd>string|false gpgconf</kbd> - the location of the GnuPG conf | |
492 * binary. The gpgconf is only | |
493 * used for GnuPG >= 2.1. If not | |
494 * specified, the engine attempts | |
495 * to auto-detect the location using | |
496 * a list of know default locations. | |
497 * When set to FALSE `gpgconf --kill` | |
498 * will not be executed via destructor. | |
499 * - <kbd>string digest-algo</kbd> - Sets the message digest algorithm. | |
500 * - <kbd>string cipher-algo</kbd> - Sets the symmetric cipher. | |
501 * - <kbd>boolean strict</kbd> - In strict mode clock problems on | |
502 * subkeys and signatures are not ignored | |
503 * (--ignore-time-conflict | |
504 * and --ignore-valid-from options) | |
505 * - <kbd>mixed debug</kbd> - whether or not to use debug mode. | |
506 * When debug mode is on, all | |
507 * communication to and from the GPG | |
508 * subprocess is logged. This can be | |
509 * useful to diagnose errors when | |
510 * using Crypt_GPG. | |
511 * | |
512 * @param array $options optional. An array of options used to create the | |
513 * GPG object. All options are optional and are | |
514 * represented as key-value pairs. | |
515 * | |
516 * @throws Crypt_GPG_FileException if the <kbd>homedir</kbd> does not exist | |
517 * and cannot be created. This can happen if <kbd>homedir</kbd> is | |
518 * not specified, Crypt_GPG is run as the web user, and the web | |
519 * user has no home directory. This exception is also thrown if any | |
520 * of the options <kbd>publicKeyring</kbd>, | |
521 * <kbd>privateKeyring</kbd> or <kbd>trustDb</kbd> options are | |
522 * specified but the files do not exist or are are not readable. | |
523 * This can happen if the user running the Crypt_GPG process (for | |
524 * example, the Apache user) does not have permission to read the | |
525 * files. | |
526 * | |
527 * @throws PEAR_Exception if the provided <kbd>binary</kbd> is invalid, or | |
528 * if no <kbd>binary</kbd> is provided and no suitable binary could | |
529 * be found. | |
530 * | |
531 * @throws PEAR_Exception if the provided <kbd>agent</kbd> is invalid, or | |
532 * if no <kbd>agent</kbd> is provided and no suitable gpg-agent | |
533 * cound be found. | |
534 */ | |
535 public function __construct(array $options = array()) | |
536 { | |
537 $this->_isDarwin = (strncmp(strtoupper(PHP_OS), 'DARWIN', 6) === 0); | |
538 | |
539 // get homedir | |
540 if (array_key_exists('homedir', $options)) { | |
541 $this->_homedir = (string)$options['homedir']; | |
542 } else { | |
543 if (extension_loaded('posix')) { | |
544 // note: this requires the package OS dep exclude 'windows' | |
545 $info = posix_getpwuid(posix_getuid()); | |
546 $this->_homedir = $info['dir'].'/.gnupg'; | |
547 } else { | |
548 if (isset($_SERVER['HOME'])) { | |
549 $this->_homedir = $_SERVER['HOME']; | |
550 } else { | |
551 $this->_homedir = getenv('HOME'); | |
552 } | |
553 } | |
554 | |
555 if ($this->_homedir === false) { | |
556 throw new Crypt_GPG_FileException( | |
557 'Could not locate homedir. Please specify the homedir ' . | |
558 'to use with the \'homedir\' option when instantiating ' . | |
559 'the Crypt_GPG object.' | |
560 ); | |
561 } | |
562 } | |
563 | |
564 // attempt to create homedir if it does not exist | |
565 if (!is_dir($this->_homedir)) { | |
566 if (@mkdir($this->_homedir, 0777, true)) { | |
567 // Set permissions on homedir. Parent directories are created | |
568 // with 0777, homedir is set to 0700. | |
569 chmod($this->_homedir, 0700); | |
570 } else { | |
571 throw new Crypt_GPG_FileException( | |
572 'The \'homedir\' "' . $this->_homedir . '" is not ' . | |
573 'readable or does not exist and cannot be created. This ' . | |
574 'can happen if \'homedir\' is not specified in the ' . | |
575 'Crypt_GPG options, Crypt_GPG is run as the web user, ' . | |
576 'and the web user has no home directory.', | |
577 0, | |
578 $this->_homedir | |
579 ); | |
580 } | |
581 } | |
582 | |
583 // check homedir permissions (See Bug #19833) | |
584 if (!is_executable($this->_homedir)) { | |
585 throw new Crypt_GPG_FileException( | |
586 'The \'homedir\' "' . $this->_homedir . '" is not enterable ' . | |
587 'by the current user. Please check the permissions on your ' . | |
588 'homedir and make sure the current user can both enter and ' . | |
589 'write to the directory.', | |
590 0, | |
591 $this->_homedir | |
592 ); | |
593 } | |
594 if (!is_writeable($this->_homedir)) { | |
595 throw new Crypt_GPG_FileException( | |
596 'The \'homedir\' "' . $this->_homedir . '" is not writable ' . | |
597 'by the current user. Please check the permissions on your ' . | |
598 'homedir and make sure the current user can both enter and ' . | |
599 'write to the directory.', | |
600 0, | |
601 $this->_homedir | |
602 ); | |
603 } | |
604 | |
605 // get binary | |
606 if (array_key_exists('binary', $options)) { | |
607 $this->_binary = (string)$options['binary']; | |
608 } elseif (array_key_exists('gpgBinary', $options)) { | |
609 // deprecated alias | |
610 $this->_binary = (string)$options['gpgBinary']; | |
611 } else { | |
612 $this->_binary = $this->_getBinary(); | |
613 } | |
614 | |
615 if ($this->_binary == '' || !is_executable($this->_binary)) { | |
616 throw new PEAR_Exception( | |
617 'GPG binary not found. If you are sure the GPG binary is ' . | |
618 'installed, please specify the location of the GPG binary ' . | |
619 'using the \'binary\' driver option.' | |
620 ); | |
621 } | |
622 | |
623 // get agent | |
624 if (array_key_exists('agent', $options)) { | |
625 $this->_agent = (string)$options['agent']; | |
626 | |
627 if ($this->_agent && !is_executable($this->_agent)) { | |
628 throw new PEAR_Exception( | |
629 'Specified gpg-agent binary is not executable.' | |
630 ); | |
631 } | |
632 } else { | |
633 $this->_agent = $this->_getAgent(); | |
634 } | |
635 | |
636 if (array_key_exists('gpgconf', $options)) { | |
637 $this->_gpgconf = $options['gpgconf']; | |
638 | |
639 if ($this->_gpgconf && !is_executable($this->_gpgconf)) { | |
640 throw new PEAR_Exception( | |
641 'Specified gpgconf binary is not executable.' | |
642 ); | |
643 } | |
644 } | |
645 | |
646 /* | |
647 * Note: | |
648 * | |
649 * Normally, GnuPG expects keyrings to be in the homedir and expects | |
650 * to be able to write temporary files in the homedir. Sometimes, | |
651 * keyrings are not in the homedir, or location of the keyrings does | |
652 * not allow writing temporary files. In this case, the <i>homedir</i> | |
653 * option by itself is not enough to specify the keyrings because GnuPG | |
654 * can not write required temporary files. Additional options are | |
655 * provided so you can specify the location of the keyrings separately | |
656 * from the homedir. | |
657 */ | |
658 | |
659 // get public keyring | |
660 if (array_key_exists('publicKeyring', $options)) { | |
661 $this->_publicKeyring = (string)$options['publicKeyring']; | |
662 if (!is_readable($this->_publicKeyring)) { | |
663 throw new Crypt_GPG_FileException( | |
664 'The \'publicKeyring\' "' . $this->_publicKeyring . | |
665 '" does not exist or is not readable. Check the location ' . | |
666 'and ensure the file permissions are correct.', | |
667 0, $this->_publicKeyring | |
668 ); | |
669 } | |
670 } | |
671 | |
672 // get private keyring | |
673 if (array_key_exists('privateKeyring', $options)) { | |
674 $this->_privateKeyring = (string)$options['privateKeyring']; | |
675 if (!is_readable($this->_privateKeyring)) { | |
676 throw new Crypt_GPG_FileException( | |
677 'The \'privateKeyring\' "' . $this->_privateKeyring . | |
678 '" does not exist or is not readable. Check the location ' . | |
679 'and ensure the file permissions are correct.', | |
680 0, $this->_privateKeyring | |
681 ); | |
682 } | |
683 } | |
684 | |
685 // get trust database | |
686 if (array_key_exists('trustDb', $options)) { | |
687 $this->_trustDb = (string)$options['trustDb']; | |
688 if (!is_readable($this->_trustDb)) { | |
689 throw new Crypt_GPG_FileException( | |
690 'The \'trustDb\' "' . $this->_trustDb . | |
691 '" does not exist or is not readable. Check the location ' . | |
692 'and ensure the file permissions are correct.', | |
693 0, $this->_trustDb | |
694 ); | |
695 } | |
696 } | |
697 | |
698 if (array_key_exists('debug', $options)) { | |
699 $this->_debug = $options['debug']; | |
700 } | |
701 | |
702 $this->_strict = !empty($options['strict']); | |
703 | |
704 if (!empty($options['digest-algo'])) { | |
705 $this->_digest_algo = $options['digest-algo']; | |
706 } | |
707 | |
708 if (!empty($options['cipher-algo'])) { | |
709 $this->_cipher_algo = $options['cipher-algo']; | |
710 } | |
711 } | |
712 | |
713 // }}} | |
714 // {{{ __destruct() | |
715 | |
716 /** | |
717 * Closes open GPG subprocesses when this object is destroyed | |
718 * | |
719 * Subprocesses should never be left open by this class unless there is | |
720 * an unknown error and unexpected script termination occurs. | |
721 */ | |
722 public function __destruct() | |
723 { | |
724 $this->_closeSubprocess(); | |
725 $this->_closeIdleAgents(); | |
726 } | |
727 | |
728 // }}} | |
729 // {{{ addErrorHandler() | |
730 | |
731 /** | |
732 * Adds an error handler method | |
733 * | |
734 * The method is run every time a new error line is received from the GPG | |
735 * subprocess. The handler method must accept the error line to be handled | |
736 * as its first parameter. | |
737 * | |
738 * @param callback $callback the callback method to use. | |
739 * @param array $args optional. Additional arguments to pass as | |
740 * parameters to the callback method. | |
741 * | |
742 * @return void | |
743 */ | |
744 public function addErrorHandler($callback, array $args = array()) | |
745 { | |
746 $this->_errorHandlers[] = array( | |
747 'callback' => $callback, | |
748 'args' => $args | |
749 ); | |
750 } | |
751 | |
752 // }}} | |
753 // {{{ addStatusHandler() | |
754 | |
755 /** | |
756 * Adds a status handler method | |
757 * | |
758 * The method is run every time a new status line is received from the | |
759 * GPG subprocess. The handler method must accept the status line to be | |
760 * handled as its first parameter. | |
761 * | |
762 * @param callback $callback the callback method to use. | |
763 * @param array $args optional. Additional arguments to pass as | |
764 * parameters to the callback method. | |
765 * | |
766 * @return void | |
767 */ | |
768 public function addStatusHandler($callback, array $args = array()) | |
769 { | |
770 $this->_statusHandlers[] = array( | |
771 'callback' => $callback, | |
772 'args' => $args | |
773 ); | |
774 } | |
775 | |
776 // }}} | |
777 // {{{ sendCommand() | |
778 | |
779 /** | |
780 * Sends a command to the GPG subprocess over the command file-descriptor | |
781 * pipe | |
782 * | |
783 * @param string $command the command to send. | |
784 * | |
785 * @return void | |
786 * | |
787 * @sensitive $command | |
788 */ | |
789 public function sendCommand($command) | |
790 { | |
791 if (array_key_exists(self::FD_COMMAND, $this->_openPipes)) { | |
792 $this->_commandBuffer .= $command . PHP_EOL; | |
793 } | |
794 } | |
795 | |
796 // }}} | |
797 // {{{ reset() | |
798 | |
799 /** | |
800 * Resets the GPG engine, preparing it for a new operation | |
801 * | |
802 * @return void | |
803 * | |
804 * @see Crypt_GPG_Engine::run() | |
805 * @see Crypt_GPG_Engine::setOperation() | |
806 */ | |
807 public function reset() | |
808 { | |
809 $this->_operation = ''; | |
810 $this->_arguments = array(); | |
811 $this->_input = null; | |
812 $this->_message = null; | |
813 $this->_output = ''; | |
814 $this->_commandBuffer = ''; | |
815 | |
816 $this->_statusHandlers = array(); | |
817 $this->_errorHandlers = array(); | |
818 | |
819 if ($this->_debug) { | |
820 $this->addStatusHandler(array($this, '_handleDebugStatus')); | |
821 $this->addErrorHandler(array($this, '_handleDebugError')); | |
822 } | |
823 | |
824 $this->_processHandler = new Crypt_GPG_ProcessHandler($this); | |
825 | |
826 $this->addStatusHandler(array($this->_processHandler, 'handleStatus')); | |
827 $this->addErrorHandler(array($this->_processHandler, 'handleError')); | |
828 } | |
829 | |
830 // }}} | |
831 // {{{ run() | |
832 | |
833 /** | |
834 * Runs the current GPG operation. | |
835 * | |
836 * This creates and manages the GPG subprocess. | |
837 * This will close input/output file handles. | |
838 * | |
839 * The operation must be set with {@link Crypt_GPG_Engine::setOperation()} | |
840 * before this method is called. | |
841 * | |
842 * @return void | |
843 * | |
844 * @throws Crypt_GPG_InvalidOperationException if no operation is specified. | |
845 * @throws Crypt_GPG_Exception if an unknown or unexpected error occurs. | |
846 * | |
847 * @see Crypt_GPG_Engine::reset() | |
848 * @see Crypt_GPG_Engine::setOperation() | |
849 */ | |
850 public function run() | |
851 { | |
852 if ($this->_operation === '') { | |
853 throw new Crypt_GPG_InvalidOperationException( | |
854 'No GPG operation specified. Use Crypt_GPG_Engine::setOperation() ' . | |
855 'before calling Crypt_GPG_Engine::run().' | |
856 ); | |
857 } | |
858 | |
859 $this->_openSubprocess(); | |
860 $this->_process(); | |
861 $this->_closeSubprocess(); | |
862 } | |
863 | |
864 // }}} | |
865 // {{{ setInput() | |
866 | |
867 /** | |
868 * Sets the input source for the current GPG operation | |
869 * | |
870 * @param string|resource &$input either a reference to the string | |
871 * containing the input data or an open | |
872 * stream resource containing the input | |
873 * data. | |
874 * | |
875 * @return void | |
876 */ | |
877 public function setInput(&$input) | |
878 { | |
879 $this->_input =& $input; | |
880 } | |
881 | |
882 // }}} | |
883 // {{{ setMessage() | |
884 | |
885 /** | |
886 * Sets the message source for the current GPG operation | |
887 * | |
888 * Detached signature data should be specified here. | |
889 * | |
890 * @param string|resource &$message either a reference to the string | |
891 * containing the message data or an open | |
892 * stream resource containing the message | |
893 * data. | |
894 * | |
895 * @return void | |
896 */ | |
897 public function setMessage(&$message) | |
898 { | |
899 $this->_message =& $message; | |
900 } | |
901 | |
902 // }}} | |
903 // {{{ setOutput() | |
904 | |
905 /** | |
906 * Sets the output destination for the current GPG operation | |
907 * | |
908 * @param string|resource &$output either a reference to the string in | |
909 * which to store GPG output or an open | |
910 * stream resource to which the output data | |
911 * should be written. | |
912 * | |
913 * @return void | |
914 */ | |
915 public function setOutput(&$output) | |
916 { | |
917 $this->_output =& $output; | |
918 } | |
919 | |
920 // }}} | |
921 // {{{ setOperation() | |
922 | |
923 /** | |
924 * Sets the operation to perform | |
925 * | |
926 * @param string $operation the operation to perform. This should be one | |
927 * of GPG's operations. For example, | |
928 * <kbd>--encrypt</kbd>, <kbd>--decrypt</kbd>, | |
929 * <kbd>--sign</kbd>, etc. | |
930 * @param array $arguments optional. Additional arguments for the GPG | |
931 * subprocess. See the GPG manual for specific | |
932 * values. | |
933 * | |
934 * @return void | |
935 * | |
936 * @see Crypt_GPG_Engine::reset() | |
937 * @see Crypt_GPG_Engine::run() | |
938 */ | |
939 public function setOperation($operation, array $arguments = array()) | |
940 { | |
941 $this->_operation = $operation; | |
942 $this->_arguments = $arguments; | |
943 | |
944 $this->_processHandler->setOperation($operation); | |
945 } | |
946 | |
947 // }}} | |
948 // {{{ setPins() | |
949 | |
950 /** | |
951 * Sets the PINENTRY_USER_DATA environment variable with the currently | |
952 * added keys and passphrases | |
953 * | |
954 * Keys and passphrases are stored as an indexed array of passphrases | |
955 * in JSON encoded to a flat string. | |
956 * | |
957 * For GnuPG 2.x this is how passphrases are passed. For GnuPG 1.x the | |
958 * environment variable is set but not used. | |
959 * | |
960 * @param array $keys the internal key array to use. | |
961 * | |
962 * @return void | |
963 */ | |
964 public function setPins(array $keys) | |
965 { | |
966 $envKeys = array(); | |
967 | |
968 foreach ($keys as $keyId => $key) { | |
969 $envKeys[$keyId] = is_array($key) ? $key['passphrase'] : $key; | |
970 } | |
971 | |
972 $_ENV['PINENTRY_USER_DATA'] = json_encode($envKeys); | |
973 } | |
974 | |
975 // }}} | |
976 // {{{ getVersion() | |
977 | |
978 /** | |
979 * Gets the version of the GnuPG binary | |
980 * | |
981 * @return string a version number string containing the version of GnuPG | |
982 * being used. This value is suitable to use with PHP's | |
983 * version_compare() function. | |
984 * | |
985 * @throws Crypt_GPG_Exception if an unknown or unexpected error occurs. | |
986 * Use the <kbd>debug</kbd> option and file a bug report if these | |
987 * exceptions occur. | |
988 * | |
989 * @throws Crypt_GPG_UnsupportedException if the provided binary is not | |
990 * GnuPG or if the GnuPG version is less than 1.0.2. | |
991 */ | |
992 public function getVersion() | |
993 { | |
994 if ($this->_version == '') { | |
995 $options = array( | |
996 'homedir' => $this->_homedir, | |
997 'binary' => $this->_binary, | |
998 'debug' => $this->_debug, | |
999 'agent' => $this->_agent, | |
1000 ); | |
1001 | |
1002 $engine = new self($options); | |
1003 $info = ''; | |
1004 | |
1005 // Set a garbage version so we do not end up looking up the version | |
1006 // recursively. | |
1007 $engine->_version = '1.0.0'; | |
1008 | |
1009 $engine->reset(); | |
1010 $engine->setOutput($info); | |
1011 $engine->setOperation('--version'); | |
1012 $engine->run(); | |
1013 | |
1014 $matches = array(); | |
1015 $expression = '#gpg \(GnuPG[A-Za-z0-9/]*?\) (\S+)#'; | |
1016 | |
1017 if (preg_match($expression, $info, $matches) === 1) { | |
1018 $this->_version = $matches[1]; | |
1019 } else { | |
1020 throw new Crypt_GPG_Exception( | |
1021 'No GnuPG version information provided by the binary "' . | |
1022 $this->_binary . '". Are you sure it is GnuPG?' | |
1023 ); | |
1024 } | |
1025 | |
1026 if (version_compare($this->_version, self::MIN_VERSION, 'lt')) { | |
1027 throw new Crypt_GPG_Exception( | |
1028 'The version of GnuPG being used (' . $this->_version . | |
1029 ') is not supported by Crypt_GPG. The minimum version ' . | |
1030 'required by Crypt_GPG is ' . self::MIN_VERSION | |
1031 ); | |
1032 } | |
1033 } | |
1034 | |
1035 | |
1036 return $this->_version; | |
1037 } | |
1038 | |
1039 // }}} | |
1040 // {{{ getProcessData() | |
1041 | |
1042 /** | |
1043 * Get data from the last process execution. | |
1044 * | |
1045 * @param string $name Data element name (e.g. 'SignatureInfo') | |
1046 * | |
1047 * @return mixed | |
1048 * @see Crypt_GPG_ProcessHandler::getData() | |
1049 */ | |
1050 public function getProcessData($name) | |
1051 { | |
1052 if ($this->_processHandler) { | |
1053 switch ($name) { | |
1054 case 'SignatureInfo': | |
1055 if ($data = $this->_processHandler->getData('SigCreated')) { | |
1056 return new Crypt_GPG_SignatureCreationInfo($data); | |
1057 } | |
1058 break; | |
1059 | |
1060 case 'Signatures': | |
1061 return (array) $this->_processHandler->getData('Signatures'); | |
1062 | |
1063 default: | |
1064 return $this->_processHandler->getData($name); | |
1065 } | |
1066 } | |
1067 } | |
1068 | |
1069 // }}} | |
1070 // {{{ setProcessData() | |
1071 | |
1072 /** | |
1073 * Set some data for the process execution. | |
1074 * | |
1075 * @param string $name Data element name (e.g. 'Handle') | |
1076 * @param mixed $value Data value | |
1077 * | |
1078 * @return void | |
1079 */ | |
1080 public function setProcessData($name, $value) | |
1081 { | |
1082 if ($this->_processHandler) { | |
1083 $this->_processHandler->setData($name, $value); | |
1084 } | |
1085 } | |
1086 | |
1087 // }}} | |
1088 // {{{ _handleDebugStatus() | |
1089 | |
1090 /** | |
1091 * Displays debug output for status lines | |
1092 * | |
1093 * @param string $line the status line to handle. | |
1094 * | |
1095 * @return void | |
1096 */ | |
1097 private function _handleDebugStatus($line) | |
1098 { | |
1099 $this->_debug('STATUS: ' . $line); | |
1100 } | |
1101 | |
1102 // }}} | |
1103 // {{{ _handleDebugError() | |
1104 | |
1105 /** | |
1106 * Displays debug output for error lines | |
1107 * | |
1108 * @param string $line the error line to handle. | |
1109 * | |
1110 * @return void | |
1111 */ | |
1112 private function _handleDebugError($line) | |
1113 { | |
1114 $this->_debug('ERROR: ' . $line); | |
1115 } | |
1116 | |
1117 // }}} | |
1118 // {{{ _process() | |
1119 | |
1120 /** | |
1121 * Performs internal streaming operations for the subprocess using either | |
1122 * strings or streams as input / output points | |
1123 * | |
1124 * This is the main I/O loop for streaming to and from the GPG subprocess. | |
1125 * | |
1126 * The implementation of this method is verbose mainly for performance | |
1127 * reasons. Adding streams to a lookup array and looping the array inside | |
1128 * the main I/O loop would be siginficantly slower for large streams. | |
1129 * | |
1130 * @return void | |
1131 * | |
1132 * @throws Crypt_GPG_Exception if there is an error selecting streams for | |
1133 * reading or writing. If this occurs, please file a bug report at | |
1134 * http://pear.php.net/bugs/report.php?package=Crypt_GPG. | |
1135 */ | |
1136 private function _process() | |
1137 { | |
1138 $this->_debug('BEGIN PROCESSING'); | |
1139 | |
1140 $this->_commandBuffer = ''; // buffers input to GPG | |
1141 $messageBuffer = ''; // buffers input to GPG | |
1142 $inputBuffer = ''; // buffers input to GPG | |
1143 $outputBuffer = ''; // buffers output from GPG | |
1144 $statusBuffer = ''; // buffers output from GPG | |
1145 $errorBuffer = ''; // buffers output from GPG | |
1146 $inputComplete = false; // input stream is completely buffered | |
1147 $messageComplete = false; // message stream is completely buffered | |
1148 | |
1149 if (is_string($this->_input)) { | |
1150 $inputBuffer = $this->_input; | |
1151 $inputComplete = true; | |
1152 } | |
1153 | |
1154 if (is_string($this->_message)) { | |
1155 $messageBuffer = $this->_message; | |
1156 $messageComplete = true; | |
1157 } | |
1158 | |
1159 if (is_string($this->_output)) { | |
1160 $outputBuffer =& $this->_output; | |
1161 } | |
1162 | |
1163 // convenience variables | |
1164 $fdInput = $this->_pipes[self::FD_INPUT]; | |
1165 $fdOutput = $this->_pipes[self::FD_OUTPUT]; | |
1166 $fdError = $this->_pipes[self::FD_ERROR]; | |
1167 $fdStatus = $this->_pipes[self::FD_STATUS]; | |
1168 $fdCommand = $this->_pipes[self::FD_COMMAND]; | |
1169 $fdMessage = $this->_pipes[self::FD_MESSAGE]; | |
1170 | |
1171 // select loop delay in milliseconds | |
1172 $delay = 0; | |
1173 $inputPosition = 0; | |
1174 $eolLength = mb_strlen(PHP_EOL, '8bit'); | |
1175 | |
1176 while (true) { | |
1177 $inputStreams = array(); | |
1178 $outputStreams = array(); | |
1179 $exceptionStreams = array(); | |
1180 | |
1181 // set up input streams | |
1182 if (is_resource($this->_input) && !$inputComplete) { | |
1183 if (feof($this->_input)) { | |
1184 $inputComplete = true; | |
1185 } else { | |
1186 $inputStreams[] = $this->_input; | |
1187 } | |
1188 } | |
1189 | |
1190 // close GPG input pipe if there is no more data | |
1191 if ($inputBuffer == '' && $inputComplete) { | |
1192 $this->_debug('=> closing GPG input pipe'); | |
1193 $this->_closePipe(self::FD_INPUT); | |
1194 } | |
1195 | |
1196 if (is_resource($this->_message) && !$messageComplete) { | |
1197 if (feof($this->_message)) { | |
1198 $messageComplete = true; | |
1199 } else { | |
1200 $inputStreams[] = $this->_message; | |
1201 } | |
1202 } | |
1203 | |
1204 // close GPG message pipe if there is no more data | |
1205 if ($messageBuffer == '' && $messageComplete) { | |
1206 $this->_debug('=> closing GPG message pipe'); | |
1207 $this->_closePipe(self::FD_MESSAGE); | |
1208 } | |
1209 | |
1210 if (!feof($fdOutput)) { | |
1211 $inputStreams[] = $fdOutput; | |
1212 } | |
1213 | |
1214 if (!feof($fdStatus)) { | |
1215 $inputStreams[] = $fdStatus; | |
1216 } | |
1217 | |
1218 if (!feof($fdError)) { | |
1219 $inputStreams[] = $fdError; | |
1220 } | |
1221 | |
1222 // set up output streams | |
1223 if ($outputBuffer != '' && is_resource($this->_output)) { | |
1224 $outputStreams[] = $this->_output; | |
1225 } | |
1226 | |
1227 if ($this->_commandBuffer != '' && is_resource($fdCommand)) { | |
1228 $outputStreams[] = $fdCommand; | |
1229 } | |
1230 | |
1231 if ($messageBuffer != '' && is_resource($fdMessage)) { | |
1232 $outputStreams[] = $fdMessage; | |
1233 } | |
1234 | |
1235 if ($inputBuffer != '' && is_resource($fdInput)) { | |
1236 $outputStreams[] = $fdInput; | |
1237 } | |
1238 | |
1239 // no streams left to read or write, we're all done | |
1240 if (count($inputStreams) === 0 && count($outputStreams) === 0) { | |
1241 break; | |
1242 } | |
1243 | |
1244 $this->_debug('selecting streams'); | |
1245 | |
1246 $ready = stream_select( | |
1247 $inputStreams, | |
1248 $outputStreams, | |
1249 $exceptionStreams, | |
1250 null | |
1251 ); | |
1252 | |
1253 $this->_debug('=> got ' . $ready); | |
1254 | |
1255 if ($ready === false) { | |
1256 throw new Crypt_GPG_Exception( | |
1257 'Error selecting stream for communication with GPG ' . | |
1258 'subprocess. Please file a bug report at: ' . | |
1259 'http://pear.php.net/bugs/report.php?package=Crypt_GPG' | |
1260 ); | |
1261 } | |
1262 | |
1263 if ($ready === 0) { | |
1264 throw new Crypt_GPG_Exception( | |
1265 'stream_select() returned 0. This can not happen! Please ' . | |
1266 'file a bug report at: ' . | |
1267 'http://pear.php.net/bugs/report.php?package=Crypt_GPG' | |
1268 ); | |
1269 } | |
1270 | |
1271 // write input (to GPG) | |
1272 if (in_array($fdInput, $outputStreams, true)) { | |
1273 $this->_debug('GPG is ready for input'); | |
1274 | |
1275 $chunk = mb_substr($inputBuffer, $inputPosition, self::CHUNK_SIZE, '8bit'); | |
1276 $length = mb_strlen($chunk, '8bit'); | |
1277 | |
1278 $this->_debug( | |
1279 '=> about to write ' . $length . ' bytes to GPG input' | |
1280 ); | |
1281 | |
1282 $length = fwrite($fdInput, $chunk, $length); | |
1283 if ($length === 0) { | |
1284 // If we wrote 0 bytes it was either EAGAIN or EPIPE. Since | |
1285 // the pipe was seleted for writing, we assume it was EPIPE. | |
1286 // There's no way to get the actual error code in PHP. See | |
1287 // PHP Bug #39598. https://bugs.php.net/bug.php?id=39598 | |
1288 $this->_debug('=> broken pipe on GPG input'); | |
1289 $this->_debug('=> closing pipe GPG input'); | |
1290 $this->_closePipe(self::FD_INPUT); | |
1291 } else { | |
1292 $this->_debug('=> wrote ' . $length . ' bytes'); | |
1293 // Move the position pointer, don't modify $inputBuffer (#21081) | |
1294 if (is_string($this->_input)) { | |
1295 $inputPosition += $length; | |
1296 } else { | |
1297 $inputPosition = 0; | |
1298 $inputBuffer = mb_substr($inputBuffer, $length, null, '8bit'); | |
1299 } | |
1300 } | |
1301 } | |
1302 | |
1303 // read input (from PHP stream) | |
1304 // If the buffer is too big wait until it's smaller, we don't want | |
1305 // to use too much memory | |
1306 if (in_array($this->_input, $inputStreams, true) | |
1307 && mb_strlen($inputBuffer, '8bit') < self::CHUNK_SIZE | |
1308 ) { | |
1309 $this->_debug('input stream is ready for reading'); | |
1310 $this->_debug( | |
1311 '=> about to read ' . self::CHUNK_SIZE . | |
1312 ' bytes from input stream' | |
1313 ); | |
1314 | |
1315 $chunk = fread($this->_input, self::CHUNK_SIZE); | |
1316 $length = mb_strlen($chunk, '8bit'); | |
1317 $inputBuffer .= $chunk; | |
1318 | |
1319 $this->_debug('=> read ' . $length . ' bytes'); | |
1320 } | |
1321 | |
1322 // write message (to GPG) | |
1323 if (in_array($fdMessage, $outputStreams, true)) { | |
1324 $this->_debug('GPG is ready for message data'); | |
1325 | |
1326 $chunk = mb_substr($messageBuffer, 0, self::CHUNK_SIZE, '8bit'); | |
1327 $length = mb_strlen($chunk, '8bit'); | |
1328 | |
1329 $this->_debug( | |
1330 '=> about to write ' . $length . ' bytes to GPG message' | |
1331 ); | |
1332 | |
1333 $length = fwrite($fdMessage, $chunk, $length); | |
1334 if ($length === 0) { | |
1335 // If we wrote 0 bytes it was either EAGAIN or EPIPE. Since | |
1336 // the pipe was seleted for writing, we assume it was EPIPE. | |
1337 // There's no way to get the actual error code in PHP. See | |
1338 // PHP Bug #39598. https://bugs.php.net/bug.php?id=39598 | |
1339 $this->_debug('=> broken pipe on GPG message'); | |
1340 $this->_debug('=> closing pipe GPG message'); | |
1341 $this->_closePipe(self::FD_MESSAGE); | |
1342 } else { | |
1343 $this->_debug('=> wrote ' . $length . ' bytes'); | |
1344 $messageBuffer = mb_substr($messageBuffer, $length, null, '8bit'); | |
1345 } | |
1346 } | |
1347 | |
1348 // read message (from PHP stream) | |
1349 if (in_array($this->_message, $inputStreams, true)) { | |
1350 $this->_debug('message stream is ready for reading'); | |
1351 $this->_debug( | |
1352 '=> about to read ' . self::CHUNK_SIZE . | |
1353 ' bytes from message stream' | |
1354 ); | |
1355 | |
1356 $chunk = fread($this->_message, self::CHUNK_SIZE); | |
1357 $length = mb_strlen($chunk, '8bit'); | |
1358 $messageBuffer .= $chunk; | |
1359 | |
1360 $this->_debug('=> read ' . $length . ' bytes'); | |
1361 } | |
1362 | |
1363 // read output (from GPG) | |
1364 if (in_array($fdOutput, $inputStreams, true)) { | |
1365 $this->_debug('GPG output stream ready for reading'); | |
1366 $this->_debug( | |
1367 '=> about to read ' . self::CHUNK_SIZE . | |
1368 ' bytes from GPG output' | |
1369 ); | |
1370 | |
1371 $chunk = fread($fdOutput, self::CHUNK_SIZE); | |
1372 $length = mb_strlen($chunk, '8bit'); | |
1373 $outputBuffer .= $chunk; | |
1374 | |
1375 $this->_debug('=> read ' . $length . ' bytes'); | |
1376 } | |
1377 | |
1378 // write output (to PHP stream) | |
1379 if (in_array($this->_output, $outputStreams, true)) { | |
1380 $this->_debug('output stream is ready for data'); | |
1381 | |
1382 $chunk = mb_substr($outputBuffer, 0, self::CHUNK_SIZE, '8bit'); | |
1383 $length = mb_strlen($chunk, '8bit'); | |
1384 | |
1385 $this->_debug( | |
1386 '=> about to write ' . $length . ' bytes to output stream' | |
1387 ); | |
1388 | |
1389 $length = fwrite($this->_output, $chunk, $length); | |
1390 $outputBuffer = mb_substr($outputBuffer, $length, null, '8bit'); | |
1391 | |
1392 $this->_debug('=> wrote ' . $length . ' bytes'); | |
1393 } | |
1394 | |
1395 // read error (from GPG) | |
1396 if (in_array($fdError, $inputStreams, true)) { | |
1397 $this->_debug('GPG error stream ready for reading'); | |
1398 $this->_debug( | |
1399 '=> about to read ' . self::CHUNK_SIZE . | |
1400 ' bytes from GPG error' | |
1401 ); | |
1402 | |
1403 $chunk = fread($fdError, self::CHUNK_SIZE); | |
1404 $length = mb_strlen($chunk, '8bit'); | |
1405 $errorBuffer .= $chunk; | |
1406 | |
1407 $this->_debug('=> read ' . $length . ' bytes'); | |
1408 | |
1409 // pass lines to error handlers | |
1410 while (($pos = strpos($errorBuffer, PHP_EOL)) !== false) { | |
1411 $line = mb_substr($errorBuffer, 0, $pos, '8bit'); | |
1412 foreach ($this->_errorHandlers as $handler) { | |
1413 array_unshift($handler['args'], $line); | |
1414 call_user_func_array( | |
1415 $handler['callback'], | |
1416 $handler['args'] | |
1417 ); | |
1418 | |
1419 array_shift($handler['args']); | |
1420 } | |
1421 | |
1422 $errorBuffer = mb_substr($errorBuffer, $pos + $eolLength, null, '8bit'); | |
1423 } | |
1424 } | |
1425 | |
1426 // read status (from GPG) | |
1427 if (in_array($fdStatus, $inputStreams, true)) { | |
1428 $this->_debug('GPG status stream ready for reading'); | |
1429 $this->_debug( | |
1430 '=> about to read ' . self::CHUNK_SIZE . | |
1431 ' bytes from GPG status' | |
1432 ); | |
1433 | |
1434 $chunk = fread($fdStatus, self::CHUNK_SIZE); | |
1435 $length = mb_strlen($chunk, '8bit'); | |
1436 $statusBuffer .= $chunk; | |
1437 | |
1438 $this->_debug('=> read ' . $length . ' bytes'); | |
1439 | |
1440 // pass lines to status handlers | |
1441 while (($pos = strpos($statusBuffer, PHP_EOL)) !== false) { | |
1442 $line = mb_substr($statusBuffer, 0, $pos, '8bit'); | |
1443 // only pass lines beginning with magic prefix | |
1444 if (mb_substr($line, 0, 9, '8bit') == '[GNUPG:] ') { | |
1445 $line = mb_substr($line, 9, null, '8bit'); | |
1446 foreach ($this->_statusHandlers as $handler) { | |
1447 array_unshift($handler['args'], $line); | |
1448 call_user_func_array( | |
1449 $handler['callback'], | |
1450 $handler['args'] | |
1451 ); | |
1452 | |
1453 array_shift($handler['args']); | |
1454 } | |
1455 } | |
1456 | |
1457 $statusBuffer = mb_substr($statusBuffer, $pos + $eolLength, null, '8bit'); | |
1458 } | |
1459 } | |
1460 | |
1461 // write command (to GPG) | |
1462 if (in_array($fdCommand, $outputStreams, true)) { | |
1463 $this->_debug('GPG is ready for command data'); | |
1464 | |
1465 // send commands | |
1466 $chunk = mb_substr($this->_commandBuffer, 0, self::CHUNK_SIZE, '8bit'); | |
1467 $length = mb_strlen($chunk, '8bit'); | |
1468 | |
1469 $this->_debug( | |
1470 '=> about to write ' . $length . ' bytes to GPG command' | |
1471 ); | |
1472 | |
1473 $length = fwrite($fdCommand, $chunk, $length); | |
1474 if ($length === 0) { | |
1475 // If we wrote 0 bytes it was either EAGAIN or EPIPE. Since | |
1476 // the pipe was seleted for writing, we assume it was EPIPE. | |
1477 // There's no way to get the actual error code in PHP. See | |
1478 // PHP Bug #39598. https://bugs.php.net/bug.php?id=39598 | |
1479 $this->_debug('=> broken pipe on GPG command'); | |
1480 $this->_debug('=> closing pipe GPG command'); | |
1481 $this->_closePipe(self::FD_COMMAND); | |
1482 } else { | |
1483 $this->_debug('=> wrote ' . $length); | |
1484 $this->_commandBuffer = mb_substr($this->_commandBuffer, $length, null, '8bit'); | |
1485 } | |
1486 } | |
1487 | |
1488 if (count($outputStreams) === 0 || count($inputStreams) === 0) { | |
1489 // we have an I/O imbalance, increase the select loop delay | |
1490 // to smooth things out | |
1491 $delay += 10; | |
1492 } else { | |
1493 // things are running smoothly, decrease the delay | |
1494 $delay -= 8; | |
1495 $delay = max(0, $delay); | |
1496 } | |
1497 | |
1498 if ($delay > 0) { | |
1499 usleep($delay); | |
1500 } | |
1501 | |
1502 } // end loop while streams are open | |
1503 | |
1504 $this->_debug('END PROCESSING'); | |
1505 } | |
1506 | |
1507 // }}} | |
1508 // {{{ _openSubprocess() | |
1509 | |
1510 /** | |
1511 * Opens an internal GPG subprocess for the current operation | |
1512 * | |
1513 * Opens a GPG subprocess, then connects the subprocess to some pipes. Sets | |
1514 * the private class property {@link Crypt_GPG_Engine::$_process} to | |
1515 * the new subprocess. | |
1516 * | |
1517 * @return void | |
1518 * | |
1519 * @throws Crypt_GPG_OpenSubprocessException if the subprocess could not be | |
1520 * opened. | |
1521 * | |
1522 * @see Crypt_GPG_Engine::setOperation() | |
1523 * @see Crypt_GPG_Engine::_closeSubprocess() | |
1524 * @see Crypt_GPG_Engine::$_process | |
1525 */ | |
1526 private function _openSubprocess() | |
1527 { | |
1528 $version = $this->getVersion(); | |
1529 | |
1530 // log versions, but not when looking for the version number | |
1531 if ($version !== '1.0.0') { | |
1532 $this->_debug('USING GPG ' . $version . ' with PHP ' . PHP_VERSION); | |
1533 } | |
1534 | |
1535 // Binary operations will not work on Windows with PHP < 5.2.6. This is | |
1536 // in case stream_select() ever works on Windows. | |
1537 $rb = (version_compare(PHP_VERSION, '5.2.6') < 0) ? 'r' : 'rb'; | |
1538 $wb = (version_compare(PHP_VERSION, '5.2.6') < 0) ? 'w' : 'wb'; | |
1539 | |
1540 $env = $_ENV; | |
1541 | |
1542 // Newer versions of GnuPG return localized results. Crypt_GPG only | |
1543 // works with English, so set the locale to 'C' for the subprocess. | |
1544 $env['LC_ALL'] = 'C'; | |
1545 | |
1546 // If using GnuPG 2.x < 2.1.13 start the gpg-agent | |
1547 if (version_compare($version, '2.0.0', 'ge') | |
1548 && version_compare($version, '2.1.13', 'lt') | |
1549 ) { | |
1550 if (!$this->_agent) { | |
1551 throw new Crypt_GPG_OpenSubprocessException( | |
1552 'Unable to open gpg-agent subprocess (gpg-agent not found). ' . | |
1553 'Please specify location of the gpg-agent binary ' . | |
1554 'using the \'agent\' driver option.' | |
1555 ); | |
1556 } | |
1557 | |
1558 $agentArguments = array( | |
1559 '--daemon', | |
1560 '--options /dev/null', // ignore any saved options | |
1561 '--csh', // output is easier to parse | |
1562 '--keep-display', // prevent passing --display to pinentry | |
1563 '--no-grab', | |
1564 '--ignore-cache-for-signing', | |
1565 '--pinentry-touch-file /dev/null', | |
1566 '--disable-scdaemon', | |
1567 '--no-use-standard-socket', | |
1568 '--pinentry-program ' . escapeshellarg($this->_getPinEntry()) | |
1569 ); | |
1570 | |
1571 if ($this->_homedir) { | |
1572 $agentArguments[] = '--homedir ' . | |
1573 escapeshellarg($this->_homedir); | |
1574 } | |
1575 | |
1576 if ($version21 = version_compare($version, '2.1.0', 'ge')) { | |
1577 // This is needed to get socket file location in stderr output | |
1578 // Note: This does not help when the agent already is running | |
1579 $agentArguments[] = '--verbose'; | |
1580 } | |
1581 | |
1582 $agentCommandLine = $this->_agent . ' ' . implode(' ', $agentArguments); | |
1583 | |
1584 $agentDescriptorSpec = array( | |
1585 self::FD_INPUT => array('pipe', $rb), // stdin | |
1586 self::FD_OUTPUT => array('pipe', $wb), // stdout | |
1587 self::FD_ERROR => array('pipe', $wb) // stderr | |
1588 ); | |
1589 | |
1590 $this->_debug('OPENING GPG-AGENT SUBPROCESS WITH THE FOLLOWING COMMAND:'); | |
1591 $this->_debug($agentCommandLine); | |
1592 | |
1593 $this->_agentProcess = proc_open( | |
1594 $agentCommandLine, | |
1595 $agentDescriptorSpec, | |
1596 $this->_agentPipes, | |
1597 null, | |
1598 $env, | |
1599 array('binary_pipes' => true) | |
1600 ); | |
1601 | |
1602 if (!is_resource($this->_agentProcess)) { | |
1603 throw new Crypt_GPG_OpenSubprocessException( | |
1604 'Unable to open gpg-agent subprocess.', | |
1605 0, | |
1606 $agentCommandLine | |
1607 ); | |
1608 } | |
1609 | |
1610 // Get GPG_AGENT_INFO and set environment variable for gpg process. | |
1611 // This is a blocking read, but is only 1 line. | |
1612 $agentInfo = fread($this->_agentPipes[self::FD_OUTPUT], self::CHUNK_SIZE); | |
1613 | |
1614 // For GnuPG 2.1 we need to read both stderr and stdout | |
1615 if ($version21) { | |
1616 $agentInfo .= "\n" . fread($this->_agentPipes[self::FD_ERROR], self::CHUNK_SIZE); | |
1617 } | |
1618 | |
1619 if ($agentInfo) { | |
1620 foreach (explode("\n", $agentInfo) as $line) { | |
1621 if ($version21) { | |
1622 if (preg_match('/listening on socket \'([^\']+)/', $line, $m)) { | |
1623 $this->_agentInfo = $m[1]; | |
1624 } else if (preg_match('/gpg-agent\[([0-9]+)\].* started/', $line, $m)) { | |
1625 $this->_agentInfo .= ':' . $m[1] . ':1'; | |
1626 } | |
1627 } else if (preg_match('/GPG_AGENT_INFO[=\s]([^;]+)/', $line, $m)) { | |
1628 $this->_agentInfo = $m[1]; | |
1629 break; | |
1630 } | |
1631 } | |
1632 } | |
1633 | |
1634 $this->_debug('GPG-AGENT-INFO: ' . $this->_agentInfo); | |
1635 | |
1636 $env['GPG_AGENT_INFO'] = $this->_agentInfo; | |
1637 | |
1638 // gpg-agent daemon is started, we can close the launching process | |
1639 $this->_closeAgentLaunchProcess(); | |
1640 | |
1641 // Terminate processes if something went wrong | |
1642 register_shutdown_function(array($this, '__destruct')); | |
1643 } | |
1644 | |
1645 // "Register" GPGConf existence for _closeIdleAgents() | |
1646 if (version_compare($version, '2.1.0', 'ge')) { | |
1647 if ($this->_gpgconf === null) { | |
1648 $this->_gpgconf = $this->_getGPGConf(); | |
1649 } | |
1650 } else { | |
1651 $this->_gpgconf = false; | |
1652 } | |
1653 | |
1654 $commandLine = $this->_binary; | |
1655 | |
1656 $defaultArguments = array( | |
1657 '--status-fd ' . escapeshellarg(self::FD_STATUS), | |
1658 '--command-fd ' . escapeshellarg(self::FD_COMMAND), | |
1659 '--no-secmem-warning', | |
1660 '--no-tty', | |
1661 '--no-default-keyring', // ignored if keying files are not specified | |
1662 '--no-options' // prevent creation of ~/.gnupg directory | |
1663 ); | |
1664 | |
1665 if (version_compare($version, '1.0.7', 'ge')) { | |
1666 if (version_compare($version, '2.0.0', 'lt')) { | |
1667 $defaultArguments[] = '--no-use-agent'; | |
1668 } | |
1669 $defaultArguments[] = '--no-permission-warning'; | |
1670 } | |
1671 | |
1672 if (version_compare($version, '1.4.2', 'ge')) { | |
1673 $defaultArguments[] = '--exit-on-status-write-error'; | |
1674 } | |
1675 | |
1676 if (version_compare($version, '1.3.2', 'ge')) { | |
1677 $defaultArguments[] = '--trust-model always'; | |
1678 } else { | |
1679 $defaultArguments[] = '--always-trust'; | |
1680 } | |
1681 | |
1682 // Since 2.1.13 we can use "loopback mode" instead of gpg-agent | |
1683 if (version_compare($version, '2.1.13', 'ge')) { | |
1684 $defaultArguments[] = '--pinentry-mode loopback'; | |
1685 } | |
1686 | |
1687 if (!$this->_strict) { | |
1688 $defaultArguments[] = '--ignore-time-conflict'; | |
1689 $defaultArguments[] = '--ignore-valid-from'; | |
1690 } | |
1691 | |
1692 if (!empty($this->_digest_algo)) { | |
1693 $defaultArguments[] = '--digest-algo ' . escapeshellarg($this->_digest_algo); | |
1694 $defaultArguments[] = '--s2k-digest-algo ' . escapeshellarg($this->_digest_algo); | |
1695 } | |
1696 | |
1697 if (!empty($this->_cipher_algo)) { | |
1698 $defaultArguments[] = '--cipher-algo ' . escapeshellarg($this->_cipher_algo); | |
1699 $defaultArguments[] = '--s2k-cipher-algo ' . escapeshellarg($this->_cipher_algo); | |
1700 } | |
1701 | |
1702 $arguments = array_merge($defaultArguments, $this->_arguments); | |
1703 | |
1704 if ($this->_homedir) { | |
1705 $arguments[] = '--homedir ' . escapeshellarg($this->_homedir); | |
1706 | |
1707 // the random seed file makes subsequent actions faster so only | |
1708 // disable it if we have to. | |
1709 if (!is_writeable($this->_homedir)) { | |
1710 $arguments[] = '--no-random-seed-file'; | |
1711 } | |
1712 } | |
1713 | |
1714 if ($this->_publicKeyring) { | |
1715 $arguments[] = '--keyring ' . escapeshellarg($this->_publicKeyring); | |
1716 } | |
1717 | |
1718 if ($this->_privateKeyring) { | |
1719 $arguments[] = '--secret-keyring ' . | |
1720 escapeshellarg($this->_privateKeyring); | |
1721 } | |
1722 | |
1723 if ($this->_trustDb) { | |
1724 $arguments[] = '--trustdb-name ' . escapeshellarg($this->_trustDb); | |
1725 } | |
1726 | |
1727 $commandLine .= ' ' . implode(' ', $arguments) . ' ' . | |
1728 $this->_operation; | |
1729 | |
1730 $descriptorSpec = array( | |
1731 self::FD_INPUT => array('pipe', $rb), // stdin | |
1732 self::FD_OUTPUT => array('pipe', $wb), // stdout | |
1733 self::FD_ERROR => array('pipe', $wb), // stderr | |
1734 self::FD_STATUS => array('pipe', $wb), // status | |
1735 self::FD_COMMAND => array('pipe', $rb), // command | |
1736 self::FD_MESSAGE => array('pipe', $rb) // message | |
1737 ); | |
1738 | |
1739 $this->_debug('OPENING GPG SUBPROCESS WITH THE FOLLOWING COMMAND:'); | |
1740 $this->_debug($commandLine); | |
1741 | |
1742 $this->_process = proc_open( | |
1743 $commandLine, | |
1744 $descriptorSpec, | |
1745 $this->_pipes, | |
1746 null, | |
1747 $env, | |
1748 array('binary_pipes' => true) | |
1749 ); | |
1750 | |
1751 if (!is_resource($this->_process)) { | |
1752 throw new Crypt_GPG_OpenSubprocessException( | |
1753 'Unable to open GPG subprocess.', 0, $commandLine | |
1754 ); | |
1755 } | |
1756 | |
1757 // Set streams as non-blocking. See Bug #18618. | |
1758 foreach ($this->_pipes as $pipe) { | |
1759 stream_set_blocking($pipe, 0); | |
1760 stream_set_write_buffer($pipe, self::CHUNK_SIZE); | |
1761 stream_set_chunk_size($pipe, self::CHUNK_SIZE); | |
1762 stream_set_read_buffer($pipe, self::CHUNK_SIZE); | |
1763 } | |
1764 | |
1765 $this->_openPipes = $this->_pipes; | |
1766 } | |
1767 | |
1768 // }}} | |
1769 // {{{ _closeSubprocess() | |
1770 | |
1771 /** | |
1772 * Closes the internal GPG subprocess | |
1773 * | |
1774 * Closes the internal GPG subprocess. Sets the private class property | |
1775 * {@link Crypt_GPG_Engine::$_process} to null. | |
1776 * | |
1777 * @return void | |
1778 * | |
1779 * @see Crypt_GPG_Engine::_openSubprocess() | |
1780 * @see Crypt_GPG_Engine::$_process | |
1781 */ | |
1782 private function _closeSubprocess() | |
1783 { | |
1784 // clear PINs from environment if they were set | |
1785 $_ENV['PINENTRY_USER_DATA'] = null; | |
1786 | |
1787 if (is_resource($this->_process)) { | |
1788 $this->_debug('CLOSING GPG SUBPROCESS'); | |
1789 | |
1790 // close remaining open pipes | |
1791 foreach (array_keys($this->_openPipes) as $pipeNumber) { | |
1792 $this->_closePipe($pipeNumber); | |
1793 } | |
1794 | |
1795 $status = proc_get_status($this->_process); | |
1796 $exitCode = proc_close($this->_process); | |
1797 | |
1798 // proc_close() can return -1 in some cases, | |
1799 // get the real exit code from the process status | |
1800 if ($exitCode < 0 && $status && !$status['running']) { | |
1801 $exitCode = $status['exitcode']; | |
1802 } | |
1803 | |
1804 if ($exitCode > 0) { | |
1805 $this->_debug( | |
1806 '=> subprocess returned an unexpected exit code: ' . | |
1807 $exitCode | |
1808 ); | |
1809 } | |
1810 | |
1811 $this->_process = null; | |
1812 $this->_pipes = array(); | |
1813 | |
1814 // close file handles before throwing an exception | |
1815 if (is_resource($this->_input)) { | |
1816 fclose($this->_input); | |
1817 } | |
1818 | |
1819 if (is_resource($this->_output)) { | |
1820 fclose($this->_output); | |
1821 } | |
1822 | |
1823 $this->_processHandler->throwException($exitCode); | |
1824 } | |
1825 | |
1826 $this->_closeAgentLaunchProcess(); | |
1827 | |
1828 if ($this->_agentInfo !== null) { | |
1829 $parts = explode(':', $this->_agentInfo, 3); | |
1830 | |
1831 if (!empty($parts[1])) { | |
1832 $this->_debug('STOPPING GPG-AGENT DAEMON'); | |
1833 | |
1834 $process = new Crypt_GPG_ProcessControl($parts[1]); | |
1835 | |
1836 // terminate agent daemon | |
1837 $process->terminate(); | |
1838 | |
1839 while ($process->isRunning()) { | |
1840 usleep(10000); // 10 ms | |
1841 $process->terminate(); | |
1842 } | |
1843 | |
1844 $this->_debug('GPG-AGENT DAEMON STOPPED'); | |
1845 } | |
1846 | |
1847 $this->_agentInfo = null; | |
1848 } | |
1849 } | |
1850 | |
1851 // }}} | |
1852 // {{{ _closeAgentLaunchProcess() | |
1853 | |
1854 /** | |
1855 * Closes a the internal GPG-AGENT subprocess | |
1856 * | |
1857 * Closes the internal GPG-AGENT subprocess. Sets the private class property | |
1858 * {@link Crypt_GPG_Engine::$_agentProcess} to null. | |
1859 * | |
1860 * @return void | |
1861 * | |
1862 * @see Crypt_GPG_Engine::_openSubprocess() | |
1863 * @see Crypt_GPG_Engine::$_agentProcess | |
1864 */ | |
1865 private function _closeAgentLaunchProcess() | |
1866 { | |
1867 if (is_resource($this->_agentProcess)) { | |
1868 $this->_debug('CLOSING GPG-AGENT LAUNCH PROCESS'); | |
1869 | |
1870 // close agent pipes | |
1871 foreach ($this->_agentPipes as $pipe) { | |
1872 fflush($pipe); | |
1873 fclose($pipe); | |
1874 } | |
1875 | |
1876 // close agent launching process | |
1877 proc_close($this->_agentProcess); | |
1878 | |
1879 $this->_agentProcess = null; | |
1880 $this->_agentPipes = array(); | |
1881 | |
1882 $this->_debug('GPG-AGENT LAUNCH PROCESS CLOSED'); | |
1883 } | |
1884 } | |
1885 | |
1886 // }}} | |
1887 // {{{ _closePipe() | |
1888 | |
1889 /** | |
1890 * Closes an opened pipe used to communicate with the GPG subprocess | |
1891 * | |
1892 * If the pipe is already closed, it is ignored. If the pipe is open, it | |
1893 * is flushed and then closed. | |
1894 * | |
1895 * @param integer $pipeNumber the file descriptor number of the pipe to | |
1896 * close. | |
1897 * | |
1898 * @return void | |
1899 */ | |
1900 private function _closePipe($pipeNumber) | |
1901 { | |
1902 $pipeNumber = intval($pipeNumber); | |
1903 if (array_key_exists($pipeNumber, $this->_openPipes)) { | |
1904 fflush($this->_openPipes[$pipeNumber]); | |
1905 fclose($this->_openPipes[$pipeNumber]); | |
1906 unset($this->_openPipes[$pipeNumber]); | |
1907 } | |
1908 } | |
1909 | |
1910 // }}} | |
1911 // {{{ _closeIdleAgents() | |
1912 | |
1913 /** | |
1914 * Forces automatically started gpg-agent process to cleanup and exit | |
1915 * within a minute. | |
1916 * | |
1917 * This is needed in GnuPG 2.1 where agents are started | |
1918 * automatically by gpg process, not our code. | |
1919 * | |
1920 * @return void | |
1921 */ | |
1922 private function _closeIdleAgents() | |
1923 { | |
1924 if ($this->_gpgconf) { | |
1925 // before 2.1.13 --homedir wasn't supported, use env variable | |
1926 $env = array('GNUPGHOME' => $this->_homedir); | |
1927 $cmd = $this->_gpgconf . ' --kill gpg-agent'; | |
1928 | |
1929 if ($process = proc_open($cmd, array(), $pipes, null, $env)) { | |
1930 proc_close($process); | |
1931 } | |
1932 } | |
1933 } | |
1934 | |
1935 // }}} | |
1936 // {{{ _getBinary() | |
1937 | |
1938 /** | |
1939 * Gets the name of the GPG binary for the current operating system | |
1940 * | |
1941 * This method is called if the '<kbd>binary</kbd>' option is <i>not</i> | |
1942 * specified when creating this driver. | |
1943 * | |
1944 * @return string the name of the GPG binary for the current operating | |
1945 * system. If no suitable binary could be found, an empty | |
1946 * string is returned. | |
1947 */ | |
1948 private function _getBinary() | |
1949 { | |
1950 if ($binary = $this->_findBinary('gpg')) { | |
1951 return $binary; | |
1952 } | |
1953 | |
1954 return $this->_findBinary('gpg2'); | |
1955 } | |
1956 | |
1957 // }}} | |
1958 // {{{ _getAgent() | |
1959 | |
1960 /** | |
1961 * Gets the name of the GPG-AGENT binary for the current operating system | |
1962 * | |
1963 * @return string the name of the GPG-AGENT binary for the current operating | |
1964 * system. If no suitable binary could be found, an empty | |
1965 * string is returned. | |
1966 */ | |
1967 private function _getAgent() | |
1968 { | |
1969 return $this->_findBinary('gpg-agent'); | |
1970 } | |
1971 | |
1972 // }}} | |
1973 // {{{ _getGPGConf() | |
1974 | |
1975 /** | |
1976 * Gets the name of the GPGCONF binary for the current operating system | |
1977 * | |
1978 * @return string the name of the GPGCONF binary for the current operating | |
1979 * system. If no suitable binary could be found, an empty | |
1980 * string is returned. | |
1981 */ | |
1982 private function _getGPGConf() | |
1983 { | |
1984 return $this->_findBinary('gpgconf'); | |
1985 } | |
1986 | |
1987 // }}} | |
1988 // {{{ _findBinary() | |
1989 | |
1990 /** | |
1991 * Gets the location of a binary for the current operating system | |
1992 * | |
1993 * @param string $name Name of a binary program | |
1994 * | |
1995 * @return string The location of the binary for the current operating | |
1996 * system. If no suitable binary could be found, an empty | |
1997 * string is returned. | |
1998 */ | |
1999 private function _findBinary($name) | |
2000 { | |
2001 $binary = ''; | |
2002 | |
2003 if ($this->_isDarwin) { | |
2004 $locations = array( | |
2005 '/opt/local/bin/', // MacPorts | |
2006 '/usr/local/bin/', // Mac GPG | |
2007 '/sw/bin/', // Fink | |
2008 '/usr/bin/' | |
2009 ); | |
2010 } else { | |
2011 $locations = array( | |
2012 '/usr/bin/', | |
2013 '/usr/local/bin/' | |
2014 ); | |
2015 } | |
2016 | |
2017 foreach ($locations as $location) { | |
2018 if (is_executable($location . $name)) { | |
2019 $binary = $location . $name; | |
2020 break; | |
2021 } | |
2022 } | |
2023 | |
2024 return $binary; | |
2025 } | |
2026 | |
2027 // }}} | |
2028 // {{{ _getPinEntry() | |
2029 | |
2030 /** | |
2031 * Gets the location of the PinEntry script | |
2032 * | |
2033 * @return string the location of the PinEntry script. | |
2034 */ | |
2035 private function _getPinEntry() | |
2036 { | |
2037 // Find PinEntry program depending on the way how the package is installed | |
2038 $ds = DIRECTORY_SEPARATOR; | |
2039 $root = __DIR__ . $ds . '..' . $ds . '..' . $ds; | |
2040 $paths = array( | |
2041 '@bin-dir@', // PEAR | |
2042 $root . 'scripts', // Git | |
2043 $root . 'bin', // Composer | |
2044 ); | |
2045 | |
2046 foreach ($paths as $path) { | |
2047 if (file_exists($path . $ds . 'crypt-gpg-pinentry')) { | |
2048 return $path . $ds . 'crypt-gpg-pinentry'; | |
2049 } | |
2050 } | |
2051 } | |
2052 | |
2053 // }}} | |
2054 // {{{ _debug() | |
2055 | |
2056 /** | |
2057 * Displays debug text if debugging is turned on | |
2058 * | |
2059 * Debugging text is prepended with a debug identifier and echoed to stdout. | |
2060 * | |
2061 * @param string $text the debugging text to display. | |
2062 * | |
2063 * @return void | |
2064 */ | |
2065 private function _debug($text) | |
2066 { | |
2067 if ($this->_debug) { | |
2068 if (php_sapi_name() === 'cli') { | |
2069 foreach (explode(PHP_EOL, $text) as $line) { | |
2070 echo "Crypt_GPG DEBUG: ", $line, PHP_EOL; | |
2071 } | |
2072 } else if (is_callable($this->_debug)) { | |
2073 call_user_func($this->_debug, $text); | |
2074 } else { | |
2075 // running on a web server, format debug output nicely | |
2076 foreach (explode(PHP_EOL, $text) as $line) { | |
2077 echo "Crypt_GPG DEBUG: <strong>", htmlspecialchars($line), | |
2078 '</strong><br />', PHP_EOL; | |
2079 } | |
2080 } | |
2081 } | |
2082 } | |
2083 | |
2084 // }}} | |
2085 } | |
2086 | |
2087 // }}} | |
2088 | |
2089 ?> |