1
 
# Copyright (C) 2005 by Canonical Ltd
 
2
 
#   Authors: Robert Collins <robert.collins@canonical.com>
 
4
 
# This program is free software; you can redistribute it and/or modify
 
5
 
# it under the terms of the GNU General Public License as published by
 
6
 
# the Free Software Foundation; either version 2 of the License, or
 
7
 
# (at your option) any later version.
 
9
 
# This program is distributed in the hope that it will be useful,
 
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 
# GNU General Public License for more details.
 
14
 
# You should have received a copy of the GNU General Public License
 
15
 
# along with this program; if not, write to the Free Software
 
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 
"""GPG signing and checking logic."""
 
32
 
class DisabledGPGStrategy(object):
 
33
 
    """A GPG Strategy that makes everything fail."""
 
35
 
    def __init__(self, ignored):
 
36
 
        """Real strategies take a configuration."""
 
38
 
    def sign(self, content):
 
39
 
        raise errors.SigningFailed('Signing is disabled.')
 
42
 
class LoopbackGPGStrategy(object):
 
43
 
    """A GPG Strategy that acts like 'cat' - data is just passed through."""
 
45
 
    def __init__(self, ignored):
 
46
 
        """Real strategies take a configuration."""
 
48
 
    def sign(self, content):
 
53
 
    tty = os.environ.get('TTY')
 
55
 
        os.environ['GPG_TTY'] = tty
 
56
 
        trace.mutter('setting GPG_TTY=%s', tty)
 
58
 
        # This is not quite worthy of a warning, because some people
 
59
 
        # don't need GPG_TTY to be set. But it is worthy of a big mark
 
60
 
        # in ~/.bzr.log, so that people can debug it if it happens to them
 
61
 
        trace.mutter('** Env var TTY empty, cannot set GPG_TTY.'
 
65
 
class GPGStrategy(object):
 
66
 
    """GPG Signing and checking facilities."""
 
68
 
    def _command_line(self):
 
69
 
        return [self._config.gpg_signing_command(), '--clearsign']
 
71
 
    def __init__(self, config):
 
74
 
    def sign(self, content):
 
75
 
        ui.ui_factory.clear_term()
 
77
 
        preexec_fn = _set_gpg_tty
 
78
 
        if sys.platform == 'win32':
 
79
 
            # Win32 doesn't support preexec_fn, but wouldn't support TTY anyway.
 
82
 
            process = subprocess.Popen(self._command_line(),
 
83
 
                                       stdin=subprocess.PIPE,
 
84
 
                                       stdout=subprocess.PIPE,
 
85
 
                                       preexec_fn=preexec_fn)
 
87
 
                result = process.communicate(content)[0]
 
88
 
                if process.returncode is None:
 
90
 
                if process.returncode != 0:
 
91
 
                    raise errors.SigningFailed(self._command_line())
 
94
 
                if e.errno == errno.EPIPE:
 
95
 
                    raise errors.SigningFailed(self._command_line())
 
99
 
            # bad subprocess parameters, should never happen.
 
102
 
            if e.errno == errno.ENOENT:
 
103
 
                # gpg is not installed
 
104
 
                raise errors.SigningFailed(self._command_line())