| 1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
# This script is run by a VM host (eg "west") to prepare itself for testing |
| 4 |
|
| 5 |
import os |
| 6 |
import sys |
| 7 |
import socket |
| 8 |
import shutil |
| 9 |
import subprocess |
| 10 |
import pexpect |
| 11 |
import glob |
| 12 |
from pathlib import Path |
| 13 |
import re |
| 14 |
import argparse |
| 15 |
from enum import Enum |
| 16 |
|
| 17 |
test_hosts = ["east", "west", "road", "north", "nic"] |
| 18 |
|
| 19 |
def path_touch(dst, mode=None): |
| 20 |
if mode: #it is seems unhappy with mode=None |
| 21 |
Path(dst).touch(mode) |
| 22 |
else: |
| 23 |
Path(dst).touch() |
| 24 |
|
| 25 |
def rsync_ap(src, dst, timer=20): |
| 26 |
|
| 27 |
cmd = "/usr/bin/rsync --delete -q -aP" |
| 28 |
cmd += " %s %s" % (src, dst) |
| 29 |
|
| 30 |
try: |
| 31 |
output = subprocess.check_output(cmd, shell=True, timeout=timer, stderr=subprocess.STDOUT) |
| 32 |
except subprocess.TimeoutExpired: |
| 33 |
print( "EXCEPTION TIMEOUT ? cmd %s , cwd %s" % (os.getcwd(), cmd)) |
| 34 |
except subprocess.CalledProcessError as e: |
| 35 |
print ("EXCEPTION ? cwd %s , %s %s" % (os.getcwd(), cmd, e.output)) |
| 36 |
|
| 37 |
def mount_bind(src, dst, mode=None, touch_src_file=False, mkdir_src=False, wipe_old_dst=False, fatal=True): |
| 38 |
|
| 39 |
if touch_src_file and mkdir_src: |
| 40 |
print("conflicting options touch_src_file and mkdir_src"); |
| 41 |
|
| 42 |
if mkdir_src and not os.path.isdir(src) and not touch_src_file: |
| 43 |
os.makedirs(src) |
| 44 |
|
| 45 |
if touch_src_file : |
| 46 |
path_touch(src, mode) |
| 47 |
|
| 48 |
if wipe_old_dst: |
| 49 |
wipe_old(dst) |
| 50 |
|
| 51 |
if os.path.isdir(src): |
| 52 |
if not os.path.isdir(dst) and not os.path.islink(dst): |
| 53 |
os.makedirs(dst) |
| 54 |
elif os.path.isfile(src): |
| 55 |
if not os.path.isfile(dst) and not os.path.islink(dst): |
| 56 |
path_touch(dst, mode) |
| 57 |
else: |
| 58 |
mode_str = '' |
| 59 |
if mode: |
| 60 |
mode_str = "mode 0o%o" % mode |
| 61 |
print("mount_bind unknown action src=%s dst=%s %s" |
| 62 |
% (src, dst, mode_str)) |
| 63 |
if fatal: |
| 64 |
sys.exit(1) |
| 65 |
return True |
| 66 |
|
| 67 |
cmd = ['/bin/mount', '--bind', src, dst] |
| 68 |
o = subprocess.run(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, |
| 69 |
encoding='utf-8') |
| 70 |
if o.returncode: |
| 71 |
print("mount_bind failed %s" % cmd) |
| 72 |
if fatal: |
| 73 |
sys.exit(1) |
| 74 |
|
| 75 |
return o.returncode |
| 76 |
|
| 77 |
def lsw_init_dir(src, dst): |
| 78 |
if args.namespace: |
| 79 |
return mount_bind(src, dst, mkdir_src=True, wipe_old_dst=True, fatal=True) |
| 80 |
else: |
| 81 |
os.makedirs(dst, exist_ok=True) |
| 82 |
return |
| 83 |
|
| 84 |
def lsw_cp_file(src, dst, mode=None, nsbasepath=''): |
| 85 |
|
| 86 |
if args.namespace: |
| 87 |
ns_dst="%s/%s" %(nsbasepath, dst) #copy to local NS/hostname/<path> then mount |
| 88 |
shutil.copy(src, ns_dst) |
| 89 |
if mode: |
| 90 |
os.chmod(ns_dst, mode) |
| 91 |
mount_bind(ns_dst, dst) |
| 92 |
else: |
| 93 |
shutil.copy(src, dst) |
| 94 |
if mode: |
| 95 |
os.chmod(dst, mode) |
| 96 |
return |
| 97 |
|
| 98 |
def umount(dst): |
| 99 |
if os.path.islink(dst): |
| 100 |
os.unlink(dst) |
| 101 |
else: |
| 102 |
cmd = "umount %s" % dst |
| 103 |
subprocess.run(cmd, shell=True, capture_output=True, encoding="ascii", |
| 104 |
check=True) |
| 105 |
|
| 106 |
def umount_all(dst): |
| 107 |
o = subprocess.run("mount", shell=True, capture_output=True, encoding="ascii", check=True) |
| 108 |
for line in o.stdout.splitlines(): |
| 109 |
mdst = line.split(' ')[2] |
| 110 |
if mdst == dst: |
| 111 |
umount(dst) |
| 112 |
|
| 113 |
def wipe_old(dst): |
| 114 |
if args.namespace: |
| 115 |
umount_all(dst) |
| 116 |
elif os.path.islink(dst) or os.path.isfile(dst): |
| 117 |
os.unlink(dst) |
| 118 |
elif os.path.isdir(dst): |
| 119 |
shutil.rmtree(dst) |
| 120 |
# else: #no existing file |
| 121 |
# print("cannot wipe unknown type %s" % dst) |
| 122 |
|
| 123 |
def shell(command, out=False): |
| 124 |
"""Run command as a sub-process and report failures""" |
| 125 |
if args.verbose: |
| 126 |
print(command) |
| 127 |
status, output = subprocess.getstatusoutput(command) |
| 128 |
if status: |
| 129 |
print(("command '%s' failed with status %d: %s" % |
| 130 |
(command, status, output))) |
| 131 |
elif (args.verbose or out) and output: |
| 132 |
print(output) |
| 133 |
return output |
| 134 |
|
| 135 |
def choose_config_file(hostname, testpath, config_path, strict): |
| 136 |
(config_absdir, config_file) = os.path.split(config_path) |
| 137 |
if os.path.isabs(config_absdir): |
| 138 |
config_dir = config_absdir[1:] # drop slash; better? |
| 139 |
else: |
| 140 |
config_dir = config_absdir |
| 141 |
# extension contains the dot |
| 142 |
(config_base, extension) = os.path.splitext(config_file) |
| 143 |
|
| 144 |
# pass 1: look in the test directory, reject ambiguous files. |
| 145 |
# When exact, don't match HOSTNAME.EXTENSION. |
| 146 |
paths = [] |
| 147 |
paths.append((False, os.path.join(testpath, hostname + "." + config_file))) |
| 148 |
paths.append((True, os.path.join(testpath, hostname + extension))) |
| 149 |
paths.append((None, os.path.join(testpath, config_file))) |
| 150 |
path = None |
| 151 |
for fuzzy, p in paths: |
| 152 |
if os.path.isfile(p): |
| 153 |
if strict is True and fuzzy is True: |
| 154 |
# strict so ignore fuzzy match |
| 155 |
continue |
| 156 |
if strict is False and fuzzy is False: |
| 157 |
# non-strict should not match HOSTNAME.CONFIG_FILE |
| 158 |
sys.exit("config file %s conflicts with %s" % (p, hostname + extension)) |
| 159 |
if path: |
| 160 |
sys.exit("conflicting files %s %s in test directory" % (p, path)) |
| 161 |
path = p |
| 162 |
if path: |
| 163 |
return path |
| 164 |
|
| 165 |
# pass 2: look in base configs |
| 166 |
if userland in ("libreswan", "openswan", "strongswan"): |
| 167 |
# what about BSD? |
| 168 |
paths = [] |
| 169 |
paths.append(os.path.join("/testing/baseconfigs", hostname, config_dir, config_file)) |
| 170 |
paths.append(os.path.join("/testing/baseconfigs/all/", config_dir, config_file)) |
| 171 |
for path in paths: |
| 172 |
if os.path.isfile(path): |
| 173 |
return path |
| 174 |
|
| 175 |
# dump all the paths? |
| 176 |
return None |
| 177 |
|
| 178 |
def get_configsetup(key=None): |
| 179 |
# without --config /dev/null most tests would pass. |
| 180 |
# However, the test that follows a test with broken config will fail |
| 181 |
# And during a testrun the worker that hit error may not recover. |
| 182 |
cmd = '$(ipsec addconn --configsetup --config /dev/null | grep ' + key + ') && echo $' + key |
| 183 |
o = subprocess.run(cmd, shell=True, capture_output=True, encoding="ascii", check=True,) |
| 184 |
return o.stdout.strip().replace("'", '') |
| 185 |
|
| 186 |
def copy_config_file(hostname, testpath, config_path, strict=False, optional=False, nsprefix=""): |
| 187 |
src = choose_config_file(hostname, testpath, config_path, strict) |
| 188 |
if src: |
| 189 |
# config_absdir has a leading / so below is always valid |
| 190 |
dst = nsprefix + config_path |
| 191 |
dstdir = os.path.dirname(dst) |
| 192 |
if not os.path.isdir(dstdir): |
| 193 |
os.mkdir(dstdir) |
| 194 |
# When NS DST=<host>/NS/<test>/CONFIG_PATH |
| 195 |
shutil.copy(src, dst) |
| 196 |
# Now mount DST=<host>/NS/<test>/CONFIG_PATH over CONFIGI_PATH |
| 197 |
if nsprefix: # non-None and non-Empty |
| 198 |
mount_bind(dst, config_path); |
| 199 |
elif not optional: |
| 200 |
print("ERROR: %s's %s not found" % (hostname, config_path)) |
| 201 |
|
| 202 |
# prevent double installs from going unnoticed |
| 203 |
if os.path.isfile("/usr/libexec/ipsec/pluto"): |
| 204 |
if os.path.isfile("/usr/local/libexec/ipsec/pluto"): |
| 205 |
sys.exit("\n\n---------------------------------------------------------------------\n" |
| 206 |
"ABORT: found a swan userland in the base system as well as /usr/local\n" |
| 207 |
"---------------------------------------------------------------------\n") |
| 208 |
|
| 209 |
parser = argparse.ArgumentParser(description='swan-prep arguments') |
| 210 |
exclusive_grp_dnsserver = parser.add_mutually_exclusive_group() |
| 211 |
|
| 212 |
parser.add_argument('--testpath', '-t', action='store', |
| 213 |
default=os.getcwd(), help="Test directory full path %s " % os.getcwd()) |
| 214 |
parser.add_argument('--hostname', '-H', action='store', |
| 215 |
default='', help='The name of the host to prepare as') |
| 216 |
# we should get this from the testparams.sh file? |
| 217 |
parser.add_argument('--userland', '-u', action='store', |
| 218 |
default='libreswan', help='which userland to prepare') |
| 219 |
parser.add_argument('--strongswan-version', action='store', |
| 220 |
default='', help='strongswan version expect') |
| 221 |
|
| 222 |
class Keys(Enum): |
| 223 |
X509 = 'x509' |
| 224 |
HOST = 'host' |
| 225 |
NONE = 'none' |
| 226 |
parser.add_argument('--keys', |
| 227 |
choices=[Keys.X509, Keys.HOST, Keys.NONE], default=None, |
| 228 |
help='create an NSS database containing X.509, host, or no keys') |
| 229 |
parser.add_argument('--nokeys', |
| 230 |
action='store_const', dest='keys', const=Keys.NONE, |
| 231 |
help='create an empty NSS database') |
| 232 |
parser.add_argument('--hostkeys', |
| 233 |
action='store_const', dest='keys', const=Keys.HOST, |
| 234 |
help="create an NSS database containing the host keys") |
| 235 |
parser.add_argument('--x509keys', '-x', '--x509', |
| 236 |
action='store_const', dest='keys', const=Keys.X509, |
| 237 |
help='create an NSS database containing X.509 certificates and private keys') |
| 238 |
|
| 239 |
nsspassword="s3cret" |
| 240 |
parser.add_argument('--nsspw', action='store_true', |
| 241 |
help='set the security password on the NSS database to '+nsspassword) |
| 242 |
|
| 243 |
parser.add_argument('--x509name', '-X', action='store', default="", |
| 244 |
help='create X509 NSS file by importing test certs') |
| 245 |
parser.add_argument('--certchain', '-c', action='store_true', |
| 246 |
help='import the ca-chain test certificates into the NSS database') |
| 247 |
|
| 248 |
parser.add_argument('--fips', '-f', action='store_true', |
| 249 |
help='prepare /etc/ipsec.d for running in FIPS mode') |
| 250 |
|
| 251 |
exclusive_grp_dnsserver.add_argument('--dnssec', '-d', action='store_true', |
| 252 |
help='start nsd and unbound for DNSSEC - meant only for nic') |
| 253 |
|
| 254 |
parser.add_argument('--46', '--64', action='store_true', |
| 255 |
help='Do not disable IPv6. Default is disable IPv6 ', dest='ipv46', default=False) |
| 256 |
parser.add_argument('--verbose', '-v', action='store_true', |
| 257 |
help='more verbose') |
| 258 |
parser.add_argument('--namespace', action='store_true', |
| 259 |
default='', help='Running inside name sapace') |
| 260 |
|
| 261 |
args = parser.parse_args() |
| 262 |
|
| 263 |
if args.hostname: |
| 264 |
hostname = args.hostname |
| 265 |
else: |
| 266 |
hostname = socket.gethostname() |
| 267 |
|
| 268 |
if "." in hostname: |
| 269 |
hostname = hostname.split(".")[0] |
| 270 |
|
| 271 |
# Validate this is sane? |
| 272 |
testpath = args.testpath |
| 273 |
if not os.path.isdir(testpath): |
| 274 |
sys.exit("Unknown or bad testpath '%s'" % args.testname) |
| 275 |
|
| 276 |
testname = os.path.basename(testpath) |
| 277 |
|
| 278 |
# dnssec is only run on nic, but nic isn't supposed to run swan-prep |
| 279 |
# ever :/ when using namespaces, nic has no eth0 |
| 280 |
if not args.dnssec: |
| 281 |
o = subprocess.run(['ip' , '-o', 'link' , 'show', 'dev', 'eth0'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="ascii") |
| 282 |
ifname = o.stdout.split(' ')[1] |
| 283 |
if o.returncode: |
| 284 |
print("cannot find eht0. test guest expect eth0. --no-eth0 to disable this check\n%s\n%s" % i (o.output, o.stderr)) |
| 285 |
sys.exit(1) |
| 286 |
|
| 287 |
# nsrun set magic string SWAN_PLUTOTEST=YES in the environment |
| 288 |
# to identify namespace |
| 289 |
# echo $SWAN_PLUTOTEST |
| 290 |
if "SWAN_PLUTOTEST" in os.environ: |
| 291 |
args.namespace = True |
| 292 |
elif not args.namespace: |
| 293 |
o = subprocess.run('ip netns identify', shell=True, capture_output=True, encoding="ascii", check=True,) |
| 294 |
namespace = o.stdout |
| 295 |
namespace_expect = hostname + '-' + testname |
| 296 |
if re.search(hostname, namespace): |
| 297 |
args.namespace = True |
| 298 |
|
| 299 |
resolve_conf = "/etc/resolv.conf" |
| 300 |
etc_hosts = "/etc/hosts" |
| 301 |
|
| 302 |
if hostname != "nic": |
| 303 |
nssdir = get_configsetup("nssdir") |
| 304 |
ipsecdir = get_configsetup("ipsecdir") |
| 305 |
configfile = get_configsetup("configfile") |
| 306 |
secretsfile = get_configsetup("secretsfile") |
| 307 |
|
| 308 |
if args.namespace: |
| 309 |
nsbasepath ="%s/NS/%s" % (testpath, hostname) #will create testpath/NS/hostname/* |
| 310 |
if not os.path.isdir(nsbasepath): |
| 311 |
os.makedirs(nsbasepath) |
| 312 |
else: |
| 313 |
nsbasepath = '' |
| 314 |
|
| 315 |
# Setup pluto.log softlink and bindmount in namespace |
| 316 |
if hostname != "nic": |
| 317 |
subprocess.run(["ipsec", "stop"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="ascii") # gentle try before wiping files and killall |
| 318 |
|
| 319 |
outputdir = "%s/OUTPUT/" % testpath |
| 320 |
if not os.path.isdir(outputdir): |
| 321 |
os.mkdir(outputdir) |
| 322 |
os.chmod(outputdir, 0o777) |
| 323 |
|
| 324 |
if args.userland in ("libreswan", "openswan"): |
| 325 |
dname = "pluto" |
| 326 |
elif args.userland == "strongswan": |
| 327 |
dname = "charon" |
| 328 |
else: |
| 329 |
dname = "iked" |
| 330 |
|
| 331 |
daemonlogfile = "%s/%s.%s.log" % (outputdir, hostname, dname) |
| 332 |
tmplink = "/tmp/%s.log" % dname |
| 333 |
wipe_old(tmplink) |
| 334 |
|
| 335 |
Path(daemonlogfile).touch(mode=0o777) |
| 336 |
|
| 337 |
if args.namespace : |
| 338 |
mount_bind(daemonlogfile, tmplink, touch_src_file=True) |
| 339 |
else : |
| 340 |
os.symlink(daemonlogfile, tmplink) |
| 341 |
|
| 342 |
if args.userland: |
| 343 |
if not args.userland in ("libreswan", "strongswan", "openswan"): |
| 344 |
sys.exit("swan-prep: unknown userland type '%s'" % args.userland) |
| 345 |
userland = args.userland |
| 346 |
else: |
| 347 |
userland = "libreswan" |
| 348 |
|
| 349 |
# print "swan-prep running on %s for test %s with userland |
| 350 |
# %s"%(hostname,testname,userland) |
| 351 |
|
| 352 |
if hostname != "nic": |
| 353 |
# wipe or unmount any old configs |
| 354 |
# do not clean pid files or files opened by pluto, charon .. yet. |
| 355 |
wipe_old(configfile) |
| 356 |
wipe_old(secretsfile) |
| 357 |
wipe_old(nssdir) |
| 358 |
if nssdir != ipsecdir: |
| 359 |
wipe_old(ipsecdir) |
| 360 |
wipe_old("/etc/strongswan") |
| 361 |
|
| 362 |
if args.namespace: |
| 363 |
wipe_old(resolve_conf) |
| 364 |
wipe_old(etc_hosts) |
| 365 |
|
| 366 |
# if using systemd, ensure we don't restart pluto on crash |
| 367 |
if os.path.isfile("/lib/systemd/system/ipsec.service") and not args.namespace: |
| 368 |
service = "".join(open("/lib/systemd/system/ipsec.service").readlines()) |
| 369 |
if "Restart=always" in service: |
| 370 |
fp = open("/lib/systemd/system/ipsec.service", "w") |
| 371 |
fp.write("".join(service).replace("Restart=always", "Restart=no")) |
| 372 |
# always reload to avoid "service is masked" errors |
| 373 |
subprocess.getoutput("/bin/systemctl daemon-reload") |
| 374 |
|
| 375 |
# AA_201902 what happens to audit log namespace |
| 376 |
# we have to cleanup the audit log or we could get entries from previous test |
| 377 |
if os.path.isfile("/var/log/audit/audit.log"): |
| 378 |
fp = open("/var/log/audit/audit.log", "w") |
| 379 |
fp.close() |
| 380 |
if os.path.isfile("/lib/systemd/system/auditd.service"): |
| 381 |
subprocess.getoutput("/bin/systemctl restart auditd.service") |
| 382 |
|
| 383 |
# disable some daemons that could cause noise packets getting encrypted |
| 384 |
if not args.namespace : |
| 385 |
subprocess.getoutput("/bin/systemctl stop chronyd") |
| 386 |
subprocess.getoutput("/bin/systemctl stop sssd") |
| 387 |
|
| 388 |
# work around for ACQUIRE losing sub-type, causing false positive test results |
| 389 |
subprocess.getoutput('sysctl net.ipv4.ping_group_range="1 0"') |
| 390 |
|
| 391 |
# ensure cores are just dropped, not sent to aobrt-hook-ccpp or systemd. |
| 392 |
# setting /proc/sys/kernel/core_pattern to "core" or a pattern does not |
| 393 |
# work. And you cannot use shell redirection ">". So we hack it with "tee" |
| 394 |
pattern = "|/usr/bin/tee /tmp/core.%h.%e.%p" |
| 395 |
fp = open("/proc/sys/kernel/core_pattern", "w") |
| 396 |
fp.write(pattern) |
| 397 |
fp.close() |
| 398 |
|
| 399 |
if hostname != "nic": |
| 400 |
dst = "/etc/strongswan/" if userland == "strongswan" else ipsecdir |
| 401 |
src = "%s%s" % (nsbasepath, dst) |
| 402 |
lsw_init_dir(src, dst) |
| 403 |
|
| 404 |
if args.userland in ("libreswan", "openswan"): |
| 405 |
dst = "/run/pluto" |
| 406 |
if nssdir and ipsecdir != nssdir: |
| 407 |
src = "%s%s" % (nsbasepath, nssdir) |
| 408 |
lsw_init_dir(src, nssdir) |
| 409 |
elif args.userland in ("strongswan"): |
| 410 |
dst="/run/strongswan" |
| 411 |
elif args.namespace: |
| 412 |
print("Unknown run directory fatal in namespace") |
| 413 |
sys.exit(1) |
| 414 |
src = "%s%s" % (nsbasepath, dst) |
| 415 |
lsw_init_dir(src, dst) |
| 416 |
|
| 417 |
if args.userland in ("libreswan", "openswan"): |
| 418 |
|
| 419 |
# fill in any missing dirs |
| 420 |
for dir in ("/etc/ipsec.d/policies", "/etc/ipsec.d/cacerts", "/etc/ipsec.d/crls", "/etc/ipsec.d/certs", "/etc/ipsec.d/private"): |
| 421 |
if not os.path.isdir(dir): |
| 422 |
os.mkdir(dir) |
| 423 |
if "private" in dir: |
| 424 |
os.chmod(dir, 0o700) |
| 425 |
else: |
| 426 |
oldfiles = glob.glob("%s/*.der"%dir) + glob.glob("%s/*.crt"%dir) + glob.glob("%s/*.pem"%dir) + glob.glob("%s/*.key"%dir) |
| 427 |
for oldfile in oldfiles: |
| 428 |
os.unlink(oldfile) |
| 429 |
|
| 430 |
copy_config_file(hostname, testpath, "/etc/ipsec.conf", nsprefix=nsbasepath) |
| 431 |
copy_config_file(hostname, testpath, "/etc/ipsec.secrets", nsprefix=nsbasepath) |
| 432 |
|
| 433 |
elif args.userland == "strongswan": |
| 434 |
|
| 435 |
# check version and spew all over test output |
| 436 |
output = subprocess.getoutput("strongswan version") |
| 437 |
strongswan_version = "U5.9.14" |
| 438 |
if args.strongswan_version: |
| 439 |
strongswan_version = args.strongswan_version |
| 440 |
if not strongswan_version in output: |
| 441 |
print("strongswan %s must be installed" % (strongswan_version)) |
| 442 |
print("") |
| 443 |
print(output) |
| 444 |
|
| 445 |
# required to write log file in /tmp |
| 446 |
subprocess.getoutput("setenforce 0") |
| 447 |
|
| 448 |
for directory in ("ipsec.d/", "ipsec.d/aacerts/", "ipsec.d/ocspcerts/", "ipsec.d/cacerts/", "ipsec.d/private/", "ipsec.d/certs/"): |
| 449 |
dst = os.path.join("/etc/strongswan", directory) |
| 450 |
if os.path.isdir(dst): |
| 451 |
shutil.rmtree(dst) |
| 452 |
os.mkdir(dst) |
| 453 |
copy_config_file(hostname, testpath, "/etc/strongswan/strongswan.conf", strict=True) |
| 454 |
copy_config_file(hostname, testpath, "/etc/strongswan/ipsec.conf") |
| 455 |
copy_config_file(hostname, testpath, "/etc/strongswan/ipsec.secrets") |
| 456 |
copy_config_file(hostname, testpath, "/etc/strongswan/swanctl/swanctl.conf", strict=True, optional=True) |
| 457 |
|
| 458 |
# test specific files |
| 459 |
xl2tpdconf = "%s/%s.xl2tpd.conf" % (testpath, hostname) |
| 460 |
pppoptions = "%s/%s.ppp-options.xl2tpd" % (testpath, hostname) |
| 461 |
chapfile = "%s/chap-secrets" % testpath |
| 462 |
xauthpasswd = "%s/%s.passwd" % (testpath, hostname) |
| 463 |
|
| 464 |
if os.path.isfile(xl2tpdconf): |
| 465 |
lsw_cp_file(xl2tpdconf, "/etc/xl2tpd/xl2tpd.conf", nsbasepath=nsbasepath) |
| 466 |
if os.path.isfile(pppoptions): |
| 467 |
lsw_cp_file(pppoptions, "/etc/ppp/options.xl2tpd", nsbasepath=nsbasepath) |
| 468 |
if os.path.isfile(chapfile): |
| 469 |
lsw_cp_file(chapfile, "/etc/ppp/chap-secrets", nsbasepath=nsbasepath) |
| 470 |
if os.path.isfile(xauthpasswd): |
| 471 |
lsw_cp_file(xauthpasswd, "/etc/ipsec.d/passwd", nsbasepath=nsbasepath) |
| 472 |
|
| 473 |
# restore /etc/hosts to original - some tests make changes |
| 474 |
lsw_cp_file("/testing/baseconfigs/all/etc/hosts", "/etc/hosts", nsbasepath=nsbasepath) |
| 475 |
resolv = "/testing/baseconfigs/all/etc/resolv.conf" |
| 476 |
if os.path.isfile("/testing/baseconfigs/%s/etc/resolv.conf" % hostname): |
| 477 |
resolv = "/testing/baseconfigs/%s/etc/resolv.conf" % hostname |
| 478 |
else: |
| 479 |
resolv = "/testing/baseconfigs/all/etc/resolv.conf" |
| 480 |
dst = resolve_conf |
| 481 |
if not args.namespace and os.path.islink(dst): # on fedora 22 it is link first remove the link |
| 482 |
os.unlink(dst) |
| 483 |
lsw_cp_file(resolv, "/etc/resolv.conf", nsbasepath=nsbasepath) |
| 484 |
|
| 485 |
sysconfigd = "%s/etc/sysconfig" % (nsbasepath) |
| 486 |
if not os.path.isdir(sysconfigd): |
| 487 |
os.makedirs(sysconfigd) |
| 488 |
|
| 489 |
if args.userland in ("libreswan", "openswan") and hostname != "nic" and not args.namespace: |
| 490 |
if args.fips and args.userland in ("libreswan", "openswan"): |
| 491 |
# the test also requires using a modutil cmd which we cannot run here |
| 492 |
shutil.copyfile("/testing/baseconfigs/all/etc/sysconfig/pluto.fips", "/etc/sysconfig/pluto") |
| 493 |
else: |
| 494 |
lsw_cp_file("/testing/baseconfigs/all/etc/sysconfig/pluto", "/etc/sysconfig/pluto", |
| 495 |
nsbasepath=nsbasepath) |
| 496 |
if os.path.isfile("/etc/system-fips"): |
| 497 |
wipe_old("/etc/system-fips") # would work? would remove file from real system? AA_201902 |
| 498 |
|
| 499 |
# Set up NSS DB |
| 500 |
# |
| 501 |
# Stuff above will have emptied and re-created the directory. |
| 502 |
|
| 503 |
if args.userland in ("libreswan", "openswan") and hostname != "nic": |
| 504 |
|
| 505 |
# Determine the NSS password and save |
| 506 |
if args.nsspw or args.fips: |
| 507 |
# Save the raw password so it can be passed to NSS commands. |
| 508 |
util_pw = " -f /run/pluto/nsspw" |
| 509 |
p12cmd_pw = " -k /run/pluto/nsspw" |
| 510 |
with open("/run/pluto/nsspw", "w") as f: |
| 511 |
f.write(nsspassword) |
| 512 |
f.write("\n") |
| 513 |
# Store the password in "$NSSDIR/nsspassword" so that pluto |
| 514 |
# can use it to open the the NSS DB |
| 515 |
if args.nsspw or args.fips: |
| 516 |
with open(ipsecdir + "/nsspassword", "w") as f: |
| 517 |
if args.nsspw: |
| 518 |
f.write("NSS Certificate DB:" + nsspassword + "\n") |
| 519 |
if args.fips: |
| 520 |
f.write("NSS FIPS 140-2 Certificate DB:" + nsspassword + "\n") |
| 521 |
else: |
| 522 |
util_pw = "" |
| 523 |
p12cmd_pw = " -K ''" |
| 524 |
|
| 525 |
match args.keys: |
| 526 |
case Keys.HOST: |
| 527 |
# This brings in the pre-generated nss *.db files that contain |
| 528 |
# raw host keys. Basic testing tends to use pre-shared keys |
| 529 |
# and not these files. |
| 530 |
print("Creating NSS database containing host keys") |
| 531 |
for dbfile in ("cert9.db", "key4.db"): |
| 532 |
src = os.path.join("/testing/baseconfigs", hostname, "etc/ipsec.d", dbfile) |
| 533 |
dst = os.path.join(nssdir, dbfile) |
| 534 |
shutil.copyfile(src, dst) |
| 535 |
if not args.namespace: |
| 536 |
os.chown(dst, 0, 0) |
| 537 |
case Keys.X509: |
| 538 |
print("Preparing X.509 files") |
| 539 |
#print("Creating NSS database containing X.509 keys and certificates") |
| 540 |
shell("/usr/bin/certutil -N --empty-password -d sql:" + nssdir) |
| 541 |
case Keys.NONE: |
| 542 |
print("Creating empty NSS database") |
| 543 |
shell("/usr/bin/certutil -N --empty-password -d sql:" + nssdir) |
| 544 |
|
| 545 |
# If needed set a password (this will upgrade any existing |
| 546 |
# database) |
| 547 |
|
| 548 |
if args.nsspw or args.fips: |
| 549 |
with open("/tmp/pw", "w") as f: |
| 550 |
f.write("\n") |
| 551 |
shell("/usr/bin/certutil -W -f /tmp/pw -@ /run/pluto/nsspw -d sql:" + nssdir, out=True) |
| 552 |
|
| 553 |
# Switch on fips in the NSS db |
| 554 |
if args.fips: |
| 555 |
shell("/usr/bin/modutil -dbdir sql:" + nssdir + " -fips true -force", out=True) |
| 556 |
|
| 557 |
# this section is getting rough. could use a nice refactoring |
| 558 |
if args.keys == Keys.X509: |
| 559 |
|
| 560 |
if not os.path.isfile("/testing/x509/real/mainca/root.p12"): |
| 561 |
print("\n\n---------------------------------------------------------------------\n" |
| 562 |
"WARNING: no root.p12 file, did you run './kvm keys'?\n" |
| 563 |
"---------------------------------------------------------------------\n") |
| 564 |
|
| 565 |
pw = "-w /testing/x509/nss-pw" |
| 566 |
|
| 567 |
if args.certchain: |
| 568 |
icanum = 2 |
| 569 |
ica_p = hostname + "_chain_int_" |
| 570 |
ee = hostname + "_chain_endcert" |
| 571 |
|
| 572 |
# a note about DB trusts |
| 573 |
# 'CT,,' is our root's trust. T is important!! it is for verifying "SSLClient" x509 KU |
| 574 |
# ',,' is an intermediate's trust |
| 575 |
# 'P,,' (trusted peer) is nic's for OCSP |
| 576 |
# 'u,u,u' will be end cert trusts that are p12 imported (with privkey) |
| 577 |
|
| 578 |
# mainca and nic import |
| 579 |
shell("/testing/x509/import.sh real/mainca/root.cert") |
| 580 |
shell("/testing/x509/import.sh real/mainca/nic.end.cert") |
| 581 |
|
| 582 |
# install ee |
| 583 |
shell("/usr/bin/pk12util -i /testing/x509/pkcs12/%s.p12 -d sql:%s %s%s" % |
| 584 |
(ee, nssdir, pw, p12cmd_pw)) |
| 585 |
|
| 586 |
# install intermediates |
| 587 |
for i in range(1, icanum + 1): |
| 588 |
acrt = ica_p + str(i) |
| 589 |
shell("/usr/bin/certutil -A -n %s -t ',,' -d sql:%s -a -i /testing/x509/certs/%s.crt%s" % |
| 590 |
(acrt, nssdir, acrt, util_pw)) |
| 591 |
|
| 592 |
elif args.x509name: |
| 593 |
|
| 594 |
# is pw needed? Perhaps when FIPS, but has it been set? |
| 595 |
|
| 596 |
# import then fix trust |
| 597 |
shell("/usr/bin/pk12util -i /testing/x509/pkcs12/mainca/%s.p12 -d sql:%s %s%s" % |
| 598 |
(args.x509name, nssdir, pw, p12cmd_pw)) |
| 599 |
shell("/usr/bin/certutil -M -n 'Libreswan test CA for mainca - Libreswan' -d sql:%s -t 'CT,,'%s" % (nssdir, util_pw)) |
| 600 |
|
| 601 |
# pre-import certs for all other hosts |
| 602 |
for certname in ("west", "east", "road", "north", "nic"): |
| 603 |
if not hostname in certname: |
| 604 |
shell("/testing/x509/import.sh real/mainca/%s.end.cert" % (certname)) |
| 605 |
|
| 606 |
else: |
| 607 |
|
| 608 |
# is pw needed? Perahps when FIPS, but has it been set? |
| 609 |
|
| 610 |
# import then fix trust |
| 611 |
shell("/testing/x509/import.sh real/mainca/%s.all.p12" % (hostname)) |
| 612 |
|
| 613 |
# pre-import certs for all other hosts |
| 614 |
for certname in ("west", "east", "road", "north", "nic"): |
| 615 |
if not hostname in certname: |
| 616 |
shell("/testing/x509/import.sh real/mainca/%s.end.cert" % (certname)) |
| 617 |
|
| 618 |
# Don't enable FIPS mode until after NSS DBS are created. See: |
| 619 |
# https://bugzilla.mozilla.org/show_bug.cgi?id=1531267 |
| 620 |
if args.fips: |
| 621 |
fp = open("/etc/system-fips", "w") |
| 622 |
fp.close() |
| 623 |
shell("/testing/guestbin/fipson") |
| 624 |
|
| 625 |
if args.namespace: |
| 626 |
# good idea for namespace. KVM sysctl.conf get copid. |
| 627 |
subprocess.getoutput("sysctl -p /testing/baseconfigs/all/etc/sysctl.conf") |
| 628 |
|
| 629 |
if hostname != "nic" and not args.ipv46: |
| 630 |
subprocess.getoutput("sysctl net.ipv6.conf.all.disable_ipv6=1") |
| 631 |
subprocess.getoutput("sysctl net.ipv6.conf.default.disable_ipv6=1") |
| 632 |
|
| 633 |
if args.dnssec: |
| 634 |
if args.namespace: |
| 635 |
dst = "/etc/nsd" |
| 636 |
src = "%s%s" % (nsbasepath, dst) |
| 637 |
lsw_init_dir(src, dst) |
| 638 |
|
| 639 |
dst ="/run/nsd" |
| 640 |
src = "%s%s" % (nsbasepath, dst) |
| 641 |
lsw_init_dir(src, dst) |
| 642 |
|
| 643 |
src = '/testing/baseconfigs/all/etc/nsd' |
| 644 |
dst = '/etc/' |
| 645 |
rsync_ap(src, dst); |
| 646 |
else: |
| 647 |
rsync_ap('/testing/baseconfigs/all/etc/nsd/conf.d', '/etc/nsd') |
| 648 |
rsync_ap('/testing/baseconfigs/all/etc/nsd/server.d', '/etc/nsd') |
| 649 |
|
| 650 |
if args.dnssec: |
| 651 |
# nsd listen on port 5353 and unbound listen on port 53 |
| 652 |
subprocess.getoutput("sed -i 's/port: 53$/port: 5353/' /etc/nsd/server.d/nsd-server-libreswan.conf") |
| 653 |
else: |
| 654 |
#nsd listen on port 53 |
| 655 |
subprocess.getoutput("sed -i 's/port: 5353$/port: 53/' /etc/nsd/server.d/nsd-server-libreswan.conf") |
| 656 |
|
| 657 |
if args.namespace: |
| 658 |
cmd = "../../guestbin/nsd-start.sh start" |
| 659 |
output = subprocess.check_output(cmd, shell=True, timeout=20, stderr=subprocess.STDOUT) |
| 660 |
else: |
| 661 |
subprocess.getoutput("systemctl start nsd") |
| 662 |
|
| 663 |
# now unbound |
| 664 |
if args.dnssec: |
| 665 |
if args.namespace: |
| 666 |
dst = "/etc/unbound" |
| 667 |
src = "%s%s" % (nsbasepath, dst) |
| 668 |
|
| 669 |
if mount_bind(src, dst, mkdir_src=True, wipe_old_dst=True): |
| 670 |
sys.exit(1) |
| 671 |
|
| 672 |
dst ="/run/unbound" |
| 673 |
src = "%s%s" % (nsbasepath, dst) |
| 674 |
if mount_bind(src, dst, mkdir_src=True, wipe_old_dst=True): |
| 675 |
sys.exit(1) |
| 676 |
|
| 677 |
if mount_bind(src, dst, mkdir_src=True, wipe_old_dst=True): |
| 678 |
print("failed to mount_bind src=%s dst %s" % (src, dst)) |
| 679 |
sys.exit(1) |
| 680 |
|
| 681 |
src = '/testing/baseconfigs/all/etc/unbound' |
| 682 |
dst = '/etc/' |
| 683 |
rsync_ap(src, dst); |
| 684 |
|
| 685 |
if args.namespace: |
| 686 |
cmd = "../../guestbin/unbound-start.sh restart" |
| 687 |
subprocess.check_output(cmd, shell=True, timeout=120, stderr=subprocess.STDOUT) |
| 688 |
else: |
| 689 |
subprocess.getoutput("systemctl start unbound") |
| 690 |
|
| 691 |
if not os.path.isfile("/root/.gdbinit"): |
| 692 |
fp = open("/root/.gdbinit", "w") |
| 693 |
fp.write("set auto-load safe-path /") |
| 694 |
fp.close() |
| 695 |
|
| 696 |
subprocess.getoutput("iptables -F"); |
| 697 |
subprocess.getoutput("iptables -X"); |
| 698 |
|
| 699 |
if not args.namespace and hostname != "nic": # inside namespace this would kill pluto from other ns |
| 700 |
# shouldn't happen early on? now wiped run time files pid etc. |
| 701 |
# this is probably a last resort? may be a more gentle attempt at the beginning. |
| 702 |
# |
| 703 |
# final prep - this kills any running userland |
| 704 |
subprocess.call(["systemctl", "stop", "ipsec"]) |
| 705 |
# for some reason this fails to stop strongswan? |
| 706 |
subprocess.call(["systemctl", "stop", "strongswan"]) |
| 707 |
# python has no pidof - just outsource to the shell, thanks python! |
| 708 |
for dname in ( "pluto", "charon", "starter", "iked" ): |
| 709 |
try: |
| 710 |
if args.verbose: |
| 711 |
print ("INFO found daemon running stop it %s" % dname) |
| 712 |
subprocess.check_output(["killall", "-9", dname], stderr=subprocess.STDOUT) |
| 713 |
except: |
| 714 |
pass |
| 715 |
|
| 716 |
if os.path.isfile("/usr/sbin/getenforce"): |
| 717 |
selinux = subprocess.getoutput("/usr/sbin/getenforce") |
| 718 |
if os.path.isfile("/usr/sbin/restorecon") and selinux == 'Enforcing': |
| 719 |
subprocess.getoutput("restorecon -Rv /etc/ipsec.* /var/lib/ipsec /usr/local/libexec/ipsec /usr/local/sbin/ipsec") |
| 720 |
|
| 721 |
for pidfile in ("/run/pluto/pluto.pid", "/run/strongswan/charon.pid", "/run/spmd.pid", ): |
| 722 |
if os.path.isfile(pidfile): |
| 723 |
os.unlink(pidfile) |
| 724 |
|
| 725 |
if not args.namespace: |
| 726 |
# remove stacks so test can start the stack it needs. |
| 727 |
subprocess.getoutput("ipsec _stackmanager stop") |