This file is indexed.

/usr/bin/autopkgtest-virt-ssh is in autopkgtest 5.3.1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#!/usr/bin/python3
#
# autopkgtest-virt-ssh is part of autopkgtest
# autopkgtest is a tool for testing Debian binary packages
#
# autopkgtest is Copyright (C) 2006-2015 Canonical Ltd.
#
# Authors:
#    Jean-Baptiste Lallement <jean-baptiste.lallement@canonical.com>
#    Martin Pitt <martin.pitt@ubuntu.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# See the file CREDITS for a full list of credits information (often
# installed as /usr/share/doc/autopkgtest/CREDITS).

import sys
import os
import argparse
import tempfile
import shutil
import pipes
import time
import subprocess
import socket

sys.path.insert(0, '/usr/share/autopkgtest/lib')
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(
    os.path.abspath(__file__))), 'lib'))

import VirtSubproc
import adtlog

capabilities = []
args = None
workdir = None
sshcmd = None
sshconfig = {'identity': None,
             'login': None,
             'password': None,
             'port': None,
             'options': None,
             'hostname': None,
             'capabilities': None,
             'extraopts': None}
# Note: Running in jenkins might require -tt
sshopts = '-q -o BatchMode=yes -o UserKnownHostsFile=/dev/null '\
          '-o StrictHostKeyChecking=no -o CheckHostIP=no '\
          '-o ControlMaster=auto -o ControlPersist=60 '\
          '-o ControlPath=%s/ssh_control-%%r@%%h:%%p'


# Tests or builds sometimes leak background processes which might still be
# connected to ssh's stdout/err; we need to kill these after the main program
# (build or test script) finishes, otherwise we get eternal hangs as ssh waits
# until nothing is connected to its tty any more. So we run this in ssh,
# wrapping the actual command.
# We also want to avoid exiting with 255 as that's ssh's exit code if something
# with the connection failed; so we translate that to 253.
terminal_kill_wrapper = '''#!/bin/bash
set -u
PPPID=$(cut -f4 -d' ' /proc/$PPID/stat)
%s
"$@"; RC=$?
set -e
myout=$(readlink /proc/$$/fd/1)
myerr=$(readlink /proc/$$/fd/2)
myout="${myout/[/\\\\[}"; myout="${myout/]/\\\\]}"
myerr="${myerr/[/\\\\[}"; myerr="${myerr/]/\\\\]}"

# determine processes which have myout or myerr open
PS=$(ls -l /proc/[0-9]*/fd/* 2>/dev/null | sed -nr '\#('"$myout"'|'"$myerr"')# { s#^.*/proc/([0-9]+)/.*$#\\1#; p}'|sort -u)

KILL=""
for pid in $PS; do
    [ $pid -ne $$ ] && [ $pid -ne $PPID ] && [ $pid -ne $PPPID ] || continue
    [ -r /proc/$pid/comm ] && [ "$(< /proc/$pid/comm)" != sshd ] || continue
    #echo "XXXautopkgtest-ssh-wrapper($$ $PPID $PPPID $myout $myerr): killing $pid (`cat /proc/$pid/cmdline`)" >&2
    KILL="$KILL $pid"
done

[ -z "$KILL" ] || kill -9 $KILL || true
[ $RC != 255 ] || RC=253
exit $RC
'''


cleanup_paths = []  # paths on the device which we created


def parse_args():
    global args, capabilities

    parser = argparse.ArgumentParser()

    parser.add_argument('-d', '--debug', action='store_true',
                        help='Enable debugging output')
    parser.add_argument('-H', '--hostname',
                        help='hostname with optional user: [user@]hostname')
    parser.add_argument('-i', '--identity',
                        help='Selects a file from which the identity '
                        '(private key) for public key authentication is '
                        'read')
    parser.add_argument('-l', '--login',
                        help='Specifies the user to log in as on the '
                        'remote machine.')
    parser.add_argument('-P', '--password',
                        help='Specifies the sudo password on the remote host.'
                        ' It can be the password in clear text or a file '
                        'containing the password.')
    parser.add_argument('-p', '--port', type=str,
                        help='ssh port to use to connect to the host')
    parser.add_argument('-o', '--options',
                        help='Passed verbatim to ssh; see man ssh_config')
    parser.add_argument('-r', '--reboot', action='store_true',
                        help='Indicate that testbed supports reboot')
    parser.add_argument('--capability', action='append',
                        help='Indicate that testbed supports given capability.'
                        ' Can be specified multiple times. Never use this on '
                        'precious testbeds!')
    parser.add_argument('-s', '--setup-script',
                        help='Setup script to prepare testbed and ssh connection')
    parser.add_argument('--timeout-ssh', metavar='SECS', type=int, default=300,
                        help='Timeout for waiting for the ssh connection, in '
                        'seconds (default: %(default)s)')
    parser.add_argument('scriptargs', nargs=argparse.REMAINDER,
                        help='Additional arguments to pass to the setup '
                        'script for configuration')

    args = parser.parse_args()
    if args.debug:
        adtlog.verbosity = 2
    adtlog.debug(str(args))

    # shortcut for shipped scripts
    if args.setup_script and not os.path.exists(args.setup_script):
        shipped_script = os.path.join('/usr/share/autopkgtest/ssh-setup',
                                      args.setup_script)
        if os.path.exists(shipped_script):
            args.setup_script = shipped_script

    # turn --password file path into the actual password
    if args.password and os.path.exists(args.password):
        with open(args.password) as f:
            args.password = f.read().strip()

    if args.reboot:
        capabilities.append('reboot')

    if args.capability:
        capabilities += args.capability


def execute_setup_script(command, fail_ok=False, print_stderr=True):
    '''Run the --setup-script, if given.

    Arguments passed after -- to the main program are passed verbatim to the
    setup script.  The output of the script must be of the form key=value and
    is parsed to populate sshconfig. Command line options always override the
    values from the setup script.

    :param command: Command to execute. The command must match a function in
                    the ssh script
    :param fail_ok: If True, failures will not cause bombing
    :return: A tuple (return code, stdout, stderr). stdout and stderr may be
             None, for example if the script fails and fail_ok is True.
    '''
    global sshconfig, args
    out = None
    err = None

    if args.setup_script:
        fpath = args.setup_script
        if not os.path.isfile(fpath):
            VirtSubproc.bomb('File not found: %s' % fpath)
        elif not os.access(fpath, os.X_OK):
            VirtSubproc.bomb('File is not executable: %s' % fpath)

        if args.scriptargs and args.scriptargs[0] == '--':
            del args.scriptargs[0]
        cmd = [args.setup_script, command] + args.scriptargs
        if args.login:
            cmd += ['-l', args.login]
        if sshconfig.get('extraopts'):
            cmd += sshconfig['extraopts'].split(' ')

        adtlog.debug('Executing setup script: %s' % ' '.join(cmd))
        (status, out, err) = VirtSubproc.execute_timeout(
            None,
            1800,
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        if print_stderr:
            # Keep outputting the error on stderr as well as capturing it
            sys.stderr.write(err)
        if status != 0:
            err = 'setup script failed with code %i: %s' % (status,
                                                            ' '.join(cmd))
            if fail_ok:
                adtlog.debug(err)
                return (status, out, err)
            else:
                execute_setup_script('debug-failure', fail_ok=True)
                VirtSubproc.bomb(err)

        if command in ['open', 'revert']:
            for k, v in dict([s.split('=', 1) for s in out.splitlines()
                              if s and '=' in s]).items():
                sshconfig[k] = v
            adtlog.debug('got sshconfig from script %s: %s' % (command, sshconfig))

    # Command line arguments take priority
    for param in sshconfig:
        a = getattr(args, param, None)
        if a is not None:
            sshconfig[param] = a

    return (0, out, err)


def host_setup(command):
    '''Prepare remote host for ssh connection and return its configuration

    When a --setup-script is passed, execute it and return its configuration.
    The configuration of the remote side can be overloaded by options on the
    command line.

    command should either be "open" or "revert".

    Sets the global sshcmd accordingly.
    '''
    global workdir, sshcmd

    try:
        if workdir is None:
            workdir = tempfile.mkdtemp(prefix='autopkgtest-ssh.')
            os.chmod(workdir, 0o755)
        execute_setup_script(command)
        build_sshcmd()
        wait_for_ssh(sshcmd, timeout=args.timeout_ssh)
        build_auxverb()
    except Exception:
        # Clean up on failure
        hook_cleanup()
        raise
    adtlog.debug('host set up for %s; ssh command: %s' % (command, sshcmd))


def build_sshcmd():
    '''Generate sshcmd from sshconfig'''

    global sshconfig, sshcmd, capabilities, workdir

    sshcmd = ['ssh'] + (sshopts % workdir).split()
    for param in sshconfig:
        if not sshconfig[param]:
            continue

        if param == 'identity':
            sshcmd += ['-i', sshconfig[param]]
        elif param == 'login':
            sshcmd += ['-l', sshconfig[param]]
            if sshconfig[param] != 'root':
                capabilities.append('suggested-normal-user=' + sshconfig[param])
        elif param == 'port':
            sshcmd += ['-p', sshconfig[param]]
        elif param == 'options':
            sshcmd += sshconfig[param].split()
        elif param == 'hostname':
            sshcmd += [sshconfig[param]]
        elif param == 'capabilities':
            capabilities += sshconfig[param].replace(',', ' ').split()
            # Remove duplicates
            capabilities = list(set(capabilities))
        elif param == 'password':
            if not args.password:
                args.password = sshconfig[param]  # forward to can_sudo
        elif param == 'extraopts':
            # Do nothing but don't print a warning. It will be passed back as
            # is to the ssh setup script
            pass
        else:
            adtlog.warning('Ignoring invalid parameter: %s' % param)


def wait_for_ssh(ssh_cmd, timeout=300):
    '''Wait until testbed responds to ssh'''

    cmd = ssh_cmd + ['/bin/true']
    start = time.time()
    elapsed = 0
    delay = 3

    while elapsed < timeout:
        try:
            rc = VirtSubproc.execute_timeout(None, 30, cmd)[0]
            if rc == 0:
                adtlog.debug('ssh connection established.')
                break
        except VirtSubproc.Timeout:
            pass
        adtlog.warning('ssh connection failed. Retrying in %d seconds...'
                       % delay)
        time.sleep(delay)
        elapsed = time.time() - start
    else:
        execute_setup_script('debug-failure', fail_ok=True)
        VirtSubproc.bomb('Timed out on waiting for ssh connection')


def build_auxverb():
    '''Generate auxverb from sshconfig'''

    global sshconfig, sshcmd, capabilities, workdir

    if sshconfig['login'] != 'root':
        (sudocmd, askpass) = can_sudo(sshcmd)
    else:
        (sudocmd, askpass) = (None, None)
    if sudocmd:
        if 'root-on-testbed' not in capabilities:
            capabilities.append('root-on-testbed')
    else:
        if 'root-on-testbed' in capabilities:
            capabilities.remove('root-on-testbed')

    extra_cmd = ''
    if askpass:
        extra_cmd += 'export SUDO_ASKPASS=%s\n' % askpass

    # create remote wrapper
    rc = VirtSubproc.execute_timeout(
        terminal_kill_wrapper % extra_cmd, 30, sshcmd +
        ['rm -f /tmp/autopkgtest-run-wrapper; set -C; cat > /tmp/autopkgtest-run-wrapper; chmod 755 /tmp/autopkgtest-run-wrapper'],
        stdin=subprocess.PIPE)[0]
    if rc != 0:
        VirtSubproc.bomb('Failed to create /tmp/autopkgtest-run-wrapper')

    # create local auxverb script
    auxverb = os.path.join(workdir, 'runcmd')
    with open(auxverb, 'w') as f:
        f.write('''#!/bin/bash
exec %s -- %s /tmp/autopkgtest-run-wrapper $(printf '%%q ' "${@%% }")
''' % (" ".join(sshcmd), sudocmd or ''))
    os.chmod(auxverb, 0o755)
    VirtSubproc.auxverb = [auxverb]


def can_sudo(ssh_cmd):
    '''Determine if the user can sudo

    :param ssh_cmd: ssh command to connect to the host
    :returns: (sudo_command, askpass_path); (None, None) if user cannot sudo
    '''
    global cleanup_paths

    sudocmd = None

    # if we have a password, use that
    if args.password:
        cmd = 'F=`mktemp --tmpdir sudo_askpass.XXXX`;' \
              '/bin/echo -e "#!/bin/sh\necho \'%s\'" > $F;' \
              'chmod u+x $F; sync; echo $F' % args.password
        askpass = VirtSubproc.check_exec(
            ssh_cmd + ['/bin/sh', '-ec', pipes.quote(cmd)],
            outp=True, timeout=30).strip()
        adtlog.debug('created SUDO_ASKPASS from specified password')
        cleanup_paths.append(askpass)

        sudocmd = 'SUDO_ASKPASS=%s sudo -A' % askpass
        cmd = ssh_cmd + ['--'] + sudocmd.split() + ['/bin/true']
        (rc, _, err) = VirtSubproc.execute_timeout(None, 30, cmd,
                                                   stderr=subprocess.PIPE)
        if rc == 0:
            if err:
                VirtSubproc.bomb('sudo failed with stderr: "%s"' % err)
            adtlog.debug('can_sudo: askpass works')
            return (sudocmd, askpass)
        else:
            adtlog.warning('specified sudo password fails, no root available')
            pass

    # otherwise, test if we can do it without password (NOPASSWD sudo option)
    sudocmd = "sudo -n"
    cmd = ssh_cmd + ['--'] + sudocmd.split() + ['/bin/true']
    (rc, _, err) = VirtSubproc.execute_timeout(None, 30, cmd,
                                               stderr=subprocess.PIPE)
    if rc == 0:
        if err:
            VirtSubproc.bomb('sudo failed with stderr: "%s"' % err)
        adtlog.debug('can_sudo: sudo without password works')
        return (sudocmd, None)
    else:
        adtlog.debug('can_sudo: sudo without password does not work')

    return (None, None)


def hook_debug_fail():
    # Don't print stderr; if we're being called for the hook, we assume the
    # caller is going to do that.
    (status, out, err) = execute_setup_script('debug-failure',
                                              fail_ok=True,
                                              print_stderr=False)
    return err


def hook_open():
    host_setup('open')


def hook_downtmp(path):
    return VirtSubproc.downtmp_mktemp(path)


def hook_revert():
    host_setup('revert')


def wait_port_down(host, port, timeout):
    '''Wait until host:port stops responding'''

    VirtSubproc.timeout_start(timeout)
    while True:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            res = s.connect_ex((host, port))
            adtlog.debug('wait_port_down() connect: %s' % os.strerror(res))
            if res != 0:
                break
            # connect might succeed with port forwarding (e. g. QEMU)
            try:
                r = s.recv(1, socket.MSG_WAITALL)
                adtlog.debug('wait_port_down() recv: "%s"' % str(r))
                if not r:
                    break
            except OSError:
                break
            time.sleep(0.1)
        finally:
            s.close()
    VirtSubproc.timeout_stop()


def hook_wait_reboot():
    global sshcmd

    if args.setup_script:
        (rc, _, _) = execute_setup_script('wait-reboot', fail_ok=True)
    else:
        # if we don't have a setup script, use the fallback below
        rc = 1

    if rc != 0:
        adtlog.debug('setup script wait-reboot failed, waiting for ssh to go down...')
        VirtSubproc.execute_timeout(None, 10, sshcmd + ['-O', 'exit'])
        # wait for ssh/the machine to go down; we can't just call ssh sleep
        # for this, this often hangs when sshd is being shut down
        if sshconfig['port'] is not None:
            port = int(sshconfig['port'])
        else:
            port = 22
        try:
            wait_port_down(sshconfig['hostname'], port, 300)
        except VirtSubproc.Timeout:
            execute_setup_script('debug-failure', fail_ok=True)
            VirtSubproc.bomb('timed out waiting for testbed to reboot')

    build_sshcmd()
    wait_for_ssh(sshcmd, timeout=args.timeout_ssh)
    build_auxverb()


def hook_cleanup():
    global capabilities, workdir, cleanup_paths, sshcmd

    try:
        VirtSubproc.downtmp_remove()
        if cleanup_paths:
            VirtSubproc.check_exec(['rm', '-rf'] + cleanup_paths, downp=True,
                                   timeout=VirtSubproc.copy_timeout)
    except VirtSubproc.Timeout:
        adtlog.error('Removing temporary files on testbed timed out')
        # still do the remaining cleanup

    execute_setup_script('cleanup')

    capabilities = [c for c in capabilities if not c.startswith('downtmp-host')]

    # terminate ssh connection muxer; it inherits our stderr (which causes an
    # eternal hang of tee processes), and we are going to remove the socket dir
    # anyway
    if sshcmd:
        VirtSubproc.execute_timeout(None, 10, sshcmd + ['-O', 'exit'])

    if workdir:
        shutil.rmtree(workdir, ignore_errors=True)
        workdir = None


def hook_capabilities():
    return capabilities


parse_args()
VirtSubproc.main()