| 1 |
# Copyright (c) 2015 OpenStack Foundation. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
import configparser as ConfigParser |
| 17 |
import errno |
| 18 |
import os |
| 19 |
import subprocess |
| 20 |
import sys |
| 21 |
|
| 22 |
from neutron.common import config |
| 23 |
from neutron_lib.utils import helpers |
| 24 |
from oslo_config import cfg |
| 25 |
from oslo_log import log as logging |
| 26 |
from oslo_rootwrap import wrapper |
| 27 |
|
| 28 |
from neutron_vpnaas._i18n import _ |
| 29 |
|
| 30 |
LOG = logging.getLogger(__name__) |
| 31 |
|
| 32 |
|
| 33 |
def setup_conf(): |
| 34 |
cli_opts = [ |
| 35 |
cfg.DictOpt('mount_paths', |
| 36 |
required=True, |
| 37 |
help=_('Dict of paths to bind-mount (source:target) ' |
| 38 |
'prior to launch subprocess.')), |
| 39 |
cfg.ListOpt( |
| 40 |
'cmd', |
| 41 |
required=True, |
| 42 |
help=_('Command line to execute as a subprocess ' |
| 43 |
'provided as comma-separated list of arguments.')), |
| 44 |
cfg.StrOpt('rootwrap_config', default='/etc/neutron/rootwrap.conf', |
| 45 |
help=_('Rootwrap configuration file.')), |
| 46 |
] |
| 47 |
conf = cfg.CONF |
| 48 |
conf.register_cli_opts(cli_opts) |
| 49 |
return conf |
| 50 |
|
| 51 |
|
| 52 |
def execute(cmd): |
| 53 |
if not cmd: |
| 54 |
return |
| 55 |
cmd = list(map(str, cmd)) |
| 56 |
LOG.debug("Running command: %s", cmd) |
| 57 |
env = os.environ.copy() |
| 58 |
with subprocess.Popen(cmd, |
| 59 |
stdin=subprocess.PIPE, |
| 60 |
stdout=subprocess.PIPE, |
| 61 |
stderr=subprocess.PIPE, |
| 62 |
close_fds=True, env=env) as obj: |
| 63 |
_stdout, _stderr = obj.communicate() |
| 64 |
_stdout = helpers.safe_decode_utf8(_stdout) |
| 65 |
_stderr = helpers.safe_decode_utf8(_stderr) |
| 66 |
msg = ('Command: %(cmd)s Exit code: %(returncode)s ' |
| 67 |
'Stdout: %(stdout)s Stderr: %(stderr)s' % |
| 68 |
{'cmd': cmd, |
| 69 |
'returncode': obj.returncode, |
| 70 |
'stdout': _stdout, |
| 71 |
'stderr': _stderr}) |
| 72 |
LOG.debug(msg) |
| 73 |
obj.stdin.close() |
| 74 |
# Pass the output to calling process |
| 75 |
sys.stdout.write(msg) |
| 76 |
sys.stdout.flush() |
| 77 |
return obj.returncode |
| 78 |
|
| 79 |
|
| 80 |
def filter_command(command, rootwrap_config): |
| 81 |
# Load rootwrap configuration |
| 82 |
try: |
| 83 |
rawconfig = ConfigParser.RawConfigParser() |
| 84 |
rawconfig.read(rootwrap_config) |
| 85 |
rw_config = wrapper.RootwrapConfig(rawconfig) |
| 86 |
except ValueError as exc: |
| 87 |
LOG.error('Incorrect value in %(config)s: %(exc)s', |
| 88 |
{'config': rootwrap_config, 'exc': exc}) |
| 89 |
sys.exit(errno.EINVAL) |
| 90 |
except ConfigParser.Error: |
| 91 |
LOG.error('Incorrect configuration file: %(config)s', |
| 92 |
{'config': rootwrap_config}) |
| 93 |
sys.exit(errno.EINVAL) |
| 94 |
|
| 95 |
# Check if command matches any of the loaded filters |
| 96 |
filters = wrapper.load_filters(rw_config.filters_path) |
| 97 |
try: |
| 98 |
wrapper.match_filter(filters, command, exec_dirs=rw_config.exec_dirs) |
| 99 |
except wrapper.FilterMatchNotExecutable as exc: |
| 100 |
LOG.error('Command %(command)s is not executable: ' |
| 101 |
'%(path)s (filter match = %(name)s)', |
| 102 |
{'command': command, |
| 103 |
'path': exc.match.exec_path, |
| 104 |
'name': exc.match.name}) |
| 105 |
sys.exit(errno.EINVAL) |
| 106 |
except wrapper.NoFilterMatched: |
| 107 |
LOG.error('Unauthorized command: %(cmd)s (no filter matched)', |
| 108 |
{'cmd': command}) |
| 109 |
sys.exit(errno.EPERM) |
| 110 |
|
| 111 |
|
| 112 |
def execute_with_mount(): |
| 113 |
config.register_common_config_options() |
| 114 |
conf = setup_conf() |
| 115 |
conf() |
| 116 |
config.setup_logging() |
| 117 |
if not conf.cmd: |
| 118 |
LOG.error('No command provided, exiting') |
| 119 |
return errno.EINVAL |
| 120 |
|
| 121 |
if not conf.mount_paths: |
| 122 |
LOG.error('No mount path provided, exiting') |
| 123 |
return errno.EINVAL |
| 124 |
|
| 125 |
# Both sudoers and rootwrap.conf will not exist in the directory /etc |
| 126 |
# after bind-mount, so we can't use utils.execute(conf.cmd, |
| 127 |
# run_as_root=True). That's why we have to check here if cmd matches |
| 128 |
# CommandFilter |
| 129 |
filter_command(conf.cmd, conf.rootwrap_config) |
| 130 |
|
| 131 |
# Make sure the process is running in net namespace invoked by ip |
| 132 |
# netns exec(/proc/[pid]/ns/net) which is since Linux 3.0, |
| 133 |
# as we can't check mount namespace(/proc/[pid]/ns/mnt) |
| 134 |
# which is since Linux 3.8. For more detail please refer the link |
| 135 |
# http://man7.org/linux/man-pages/man7/namespaces.7.html |
| 136 |
if os.path.samefile(os.path.join('/proc/1/ns/net'), |
| 137 |
os.path.join('/proc', str(os.getpid()), 'ns/net')): |
| 138 |
LOG.error('Cannot run without netns, exiting') |
| 139 |
return errno.EINVAL |
| 140 |
|
| 141 |
for path, new_path in conf.mount_paths.items(): |
| 142 |
if not os.path.isdir(new_path): |
| 143 |
# Sometimes all directories are not ready |
| 144 |
LOG.debug('%s is not directory', new_path) |
| 145 |
continue |
| 146 |
if os.path.isdir(path) and os.path.isabs(path): |
| 147 |
return_code = execute(['mount', '--bind', new_path, path]) |
| 148 |
if return_code == 0: |
| 149 |
LOG.info('%(new_path)s has been ' |
| 150 |
'bind-mounted in %(path)s', |
| 151 |
{'new_path': new_path, 'path': path}) |
| 152 |
else: |
| 153 |
LOG.error('Failed to bind-mount ' |
| 154 |
'%(new_path)s in %(path)s', |
| 155 |
{'new_path': new_path, 'path': path}) |
| 156 |
return execute(conf.cmd) |
| 157 |
|
| 158 |
|
| 159 |
def main(): |
| 160 |
sys.exit(execute_with_mount()) |
| 161 |
|
| 162 |
|
| 163 |
if __name__ == "__main__": |
| 164 |
main() |