summaryrefslogtreecommitdiff
path: root/nova/console/xvp.py
blob: 9f379f79dc58a8332b13221da9abaa8fe1cc5632 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2010 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""XVP (Xenserver VNC Proxy) driver."""

import os
import signal

import jinja2
from oslo.config import cfg

from nova import context
from nova import db
from nova.openstack.common import excutils
from nova.openstack.common.gettextutils import _
from nova.openstack.common import log as logging
from nova.openstack.common import processutils
from nova import paths
from nova import utils


xvp_opts = [
    cfg.StrOpt('console_xvp_conf_template',
               default=paths.basedir_def('nova/console/xvp.conf.template'),
               help='XVP conf template'),
    cfg.StrOpt('console_xvp_conf',
               default='/etc/xvp.conf',
               help='Generated XVP conf file'),
    cfg.StrOpt('console_xvp_pid',
               default='/var/run/xvp.pid',
               help='XVP master process pid file'),
    cfg.StrOpt('console_xvp_log',
               default='/var/log/xvp.log',
               help='XVP log file'),
    cfg.IntOpt('console_xvp_multiplex_port',
               default=5900,
               help='Port for XVP to multiplex VNC connections on'),
    ]

CONF = cfg.CONF
CONF.register_opts(xvp_opts)
CONF.import_opt('host', 'nova.netconf')
LOG = logging.getLogger(__name__)


class XVPConsoleProxy(object):
    """Sets up XVP config, and manages XVP daemon."""

    def __init__(self):
        self.xvpconf_template = open(CONF.console_xvp_conf_template).read()
        self.host = CONF.host  # default, set by manager.
        super(XVPConsoleProxy, self).__init__()

    @property
    def console_type(self):
        return 'vnc+xvp'

    def get_port(self, context):
        """Get available port for consoles that need one."""
        #TODO(mdragon): implement port selection for non multiplex ports,
        #               we are not using that, but someone else may want
        #               it.
        return CONF.console_xvp_multiplex_port

    def setup_console(self, context, console):
        """Sets up actual proxies."""
        self._rebuild_xvp_conf(context.elevated())

    def teardown_console(self, context, console):
        """Tears down actual proxies."""
        self._rebuild_xvp_conf(context.elevated())

    def init_host(self):
        """Start up any config'ed consoles on start."""
        ctxt = context.get_admin_context()
        self._rebuild_xvp_conf(ctxt)

    def fix_pool_password(self, password):
        """Trim password to length, and encode."""
        return self._xvp_encrypt(password, is_pool_password=True)

    def fix_console_password(self, password):
        """Trim password to length, and encode."""
        return self._xvp_encrypt(password)

    def _rebuild_xvp_conf(self, context):
        LOG.debug(_('Rebuilding xvp conf'))
        pools = [pool for pool in
                 db.console_pool_get_all_by_host_type(context, self.host,
                                                       self.console_type)
                  if pool['consoles']]
        if not pools:
            LOG.debug('No console pools!')
            self._xvp_stop()
            return
        conf_data = {'multiplex_port': CONF.console_xvp_multiplex_port,
                     'pools': pools}
        tmpl_path, tmpl_file = os.path.split(CONF.injected_network_template)
        env = jinja2.Environment(loader=jinja2.FileSystemLoader(tmpl_path))
        env.filters['pass_encode'] = self.fix_console_password
        template = env.get_template(tmpl_file)
        self._write_conf(template.render(conf_data))
        self._xvp_restart()

    def _write_conf(self, config):
        try:
            LOG.debug(_('Re-wrote %s') % CONF.console_xvp_conf)
            with open(CONF.console_xvp_conf, 'w') as cfile:
                cfile.write(config)
        except IOError:
            with excutils.save_and_reraise_exception():
                LOG.exception(_("Failed to write configuration file"))

    def _xvp_stop(self):
        LOG.debug(_('Stopping xvp'))
        pid = self._xvp_pid()
        if not pid:
            return
        try:
            os.kill(pid, signal.SIGTERM)
        except OSError:
            #if it's already not running, no problem.
            pass

    def _xvp_start(self):
        if self._xvp_check_running():
            return
        LOG.debug(_('Starting xvp'))
        try:
            utils.execute('xvp',
                          '-p', CONF.console_xvp_pid,
                          '-c', CONF.console_xvp_conf,
                          '-l', CONF.console_xvp_log)
        except processutils.ProcessExecutionError as err:
            LOG.error(_('Error starting xvp: %s') % err)

    def _xvp_restart(self):
        LOG.debug(_('Restarting xvp'))
        if not self._xvp_check_running():
            LOG.debug(_('xvp not running...'))
            self._xvp_start()
        else:
            pid = self._xvp_pid()
            os.kill(pid, signal.SIGUSR1)

    def _xvp_pid(self):
        try:
            with open(CONF.console_xvp_pid, 'r') as pidfile:
                pid = int(pidfile.read())
        except IOError:
            return None
        except ValueError:
            return None
        return pid

    def _xvp_check_running(self):
        pid = self._xvp_pid()
        if not pid:
            return False
        try:
            os.kill(pid, 0)
        except OSError:
            return False
        return True

    def _xvp_encrypt(self, password, is_pool_password=False):
        """Call xvp to obfuscate passwords for config file.

        Args:
            - password: the password to encode, max 8 char for vm passwords,
                        and 16 chars for pool passwords. passwords will
                        be trimmed to max len before encoding.
            - is_pool_password: True if this this is the XenServer api password
                                False if it's a VM console password
              (xvp uses different keys and max lengths for pool passwords)

        Note that xvp's obfuscation should not be considered 'real' encryption.
        It simply DES encrypts the passwords with static keys plainly viewable
        in the xvp source code.

        """
        maxlen = 8
        flag = '-e'
        if is_pool_password:
            maxlen = 16
            flag = '-x'
        #xvp will blow up on passwords that are too long (mdragon)
        password = password[:maxlen]
        out, err = utils.execute('xvp', flag, process_input=password)
        if err:
            raise processutils.ProcessExecutionError(_("Failed to run xvp."))
        return out.strip()