This file is indexed.

/usr/lib/python2.7/dist-packages/txwinrm/shell.py is in python-txwinrm 1.3.3-1.

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

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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
##############################################################################
#
# Copyright (C) Zenoss, Inc. 2013, all rights reserved.
#
# This content is made available according to terms specified in the LICENSE
# file at the top-level directory of this package.
#
##############################################################################

import re
import logging
import shlex
import base64
import csv
from itertools import izip
from pprint import pformat
from cStringIO import StringIO
from twisted.internet import reactor, defer, task
from twisted.internet.error import TimeoutError
from xml.etree import cElementTree as ET
from xml.etree import ElementTree
from . import constants as c
from .util import create_etree_request_sender, get_datetime, RequestError
from .enumerate import create_parser_and_factory

log = logging.getLogger('winrm')
_MAX_REQUESTS_PER_COMMAND = 9999
_MAX_RETRIES = 3


class CommandResponse(object):

    def __init__(self, stdout, stderr, exit_code):
        self._stdout = stdout
        self._stderr = stderr
        self._exit_code = exit_code

    @property
    def stdout(self):
        return self._stdout

    @property
    def stderr(self):
        return self._stderr

    @property
    def exit_code(self):
        return self._exit_code

    def __repr__(self):
        return pformat(dict(
            stdout=self.stdout, stderr=self.stderr, exit_code=self.exit_code))


def _build_ps_command_line_elem(ps_command, ps_script):
    """Build PowerShell command line elements without splitting
    the actual ps script into arguments. using _build_command_line_elem
    with a ps script splits the script into separate arguments.  Remote
    Windows shell inserts spaces when reconstituting the script.

    ps_command - powershell command with arguments as string
        e.g. 'powershell -NoLogo -NonInteractive -NoProfile -Command'
    ps_script - script to be run in powershell as single line string
        e.g. "& {get-counter -counter \"\memory\pages output/sec\" }"
    """
    command_line_parts = shlex.split(ps_command, posix=False)
    # ensure '-command' is last
    if command_line_parts[-1:][0].lower() != '-command':
        index = 0
        for option in command_line_parts:
            if option.lower() == '-command':
                command_line_parts.pop(index)
                break
            index += 1
        command_line_parts.append(option)
    prefix = "rsp"
    ET.register_namespace(prefix, c.XML_NS_MSRSP)
    command_line_elem = ET.Element('{%s}CommandLine' % c.XML_NS_MSRSP)
    command_elem = ET.Element('{%s}Command' % c.XML_NS_MSRSP)
    command_elem.text = command_line_parts[0]
    command_line_elem.append(command_elem)
    for arguments_text in command_line_parts[1:]:
        arguments_elem = ET.Element('{%s}Arguments' % c.XML_NS_MSRSP)
        arguments_elem.text = arguments_text
        command_line_elem.append(arguments_elem)
    arguments_elem = ET.Element('{%s}Arguments' % c.XML_NS_MSRSP)
    arguments_elem.text = ps_script
    command_line_elem.append(arguments_elem)
    tree = ET.ElementTree(command_line_elem)
    str_io = StringIO()
    tree.write(str_io, encoding='utf-8')
    return str_io.getvalue()


def _build_command_line_elem(command_line):
    command_line_parts = shlex.split(command_line, posix=False)
    prefix = "rsp"
    ET.register_namespace(prefix, c.XML_NS_MSRSP)
    command_line_elem = ET.Element('{%s}CommandLine' % c.XML_NS_MSRSP)
    command_elem = ET.Element('{%s}Command' % c.XML_NS_MSRSP)
    command_elem.text = command_line_parts[0]
    command_line_elem.append(command_elem)
    for arguments_text in command_line_parts[1:]:
        arguments_elem = ET.Element('{%s}Arguments' % c.XML_NS_MSRSP)
        arguments_elem.text = arguments_text
        command_line_elem.append(arguments_elem)
    tree = ET.ElementTree(command_line_elem)
    str_io = StringIO()
    tree.write(str_io, encoding='utf-8')
    return str_io.getvalue()


def _stripped_lines(stream_parts):
    results = []
    for line in ''.join(stream_parts).splitlines():
        if line.strip():
            results.append(line.strip())
    return results


def _find_shell_id(elem):
    xpath = './/{%s}Selector[@Name="ShellId"]' % c.XML_NS_WS_MAN
    return elem.findtext(xpath).strip()


def _find_command_id(elem):
    xpath = './/{%s}CommandId' % c.XML_NS_MSRSP
    return elem.findtext(xpath).strip()


def _find_stream(elem, command_id, stream_name):
    xpath = './/{%s}Stream[@Name="%s"][@CommandId="%s"]' \
        % (c.XML_NS_MSRSP, stream_name, command_id)
    for elem in elem.findall(xpath):
        if elem.text is not None:
            yield base64.decodestring(elem.text).decode('utf-8-sig')


def _find_exit_code(elem, command_id):
    command_state_xpath = './/{%s}CommandState[@CommandId="%s"]' \
        % (c.XML_NS_MSRSP, command_id)
    command_state_elem = elem.find(command_state_xpath)
    if command_state_elem is not None:
        exit_code_xpath = './/{%s}ExitCode' % c.XML_NS_MSRSP
        exit_code_text = command_state_elem.findtext(exit_code_xpath)
        return None if exit_code_text is None else int(exit_code_text)


class SingleShotCommand(object):

    def __init__(self, sender):
        self._sender = sender

    @defer.inlineCallbacks
    def run_command(self, command_line):
        """
        Run commands in a remote shell like the winrs application on Windows.
        Accepts multiple commands. Returns a dictionary with the following
        structure:
            CommandResponse
                .stdout = [<non-empty, stripped line>, ...]
                .stderr = [<non-empty, stripped line>, ...]
                .exit_code = <int>
        """
        shell_id = yield self._create_shell()
        try:
            cmd_response = yield self._run_command(shell_id, command_line)
        except TimeoutError:
            yield self._sender.close_connections()
        yield self._delete_shell(shell_id)
        yield self._sender.close_connections()
        defer.returnValue(cmd_response)

    @defer.inlineCallbacks
    def _create_shell(self):
        elem = yield self._sender.send_request('create')
        defer.returnValue(_find_shell_id(elem))

    @defer.inlineCallbacks
    def _run_command(self, shell_id, command_line):
        command_line_elem = _build_command_line_elem(command_line)
        command_elem = yield self._sender.send_request(
            'command', shell_id=shell_id, command_line_elem=command_line_elem,
            timeout=self._sender._sender._conn_info.timeout)
        command_id = _find_command_id(command_elem)
        stdout_parts = []
        stderr_parts = []
        for i in xrange(_MAX_REQUESTS_PER_COMMAND):
            receive_elem = yield self._sender.send_request(
                'receive', shell_id=shell_id, command_id=command_id)
            stdout_parts.extend(
                _find_stream(receive_elem, command_id, 'stdout'))
            stderr_parts.extend(
                _find_stream(receive_elem, command_id, 'stderr'))
            exit_code = _find_exit_code(receive_elem, command_id)
            if exit_code is not None:
                break
        else:
            raise Exception("Reached max requests per command.")
        yield self._sender.send_request(
            'signal',
            shell_id=shell_id,
            command_id=command_id,
            signal_code=c.SHELL_SIGNAL_TERMINATE)
        stdout = _stripped_lines(stdout_parts)
        stderr = _stripped_lines(stderr_parts)
        defer.returnValue(CommandResponse(stdout, stderr, exit_code))

    @defer.inlineCallbacks
    def _delete_shell(self, shell_id):
        yield self._sender.send_request('delete', shell_id=shell_id)


def create_single_shot_command(conn_info):
    sender = create_etree_request_sender(conn_info)
    return SingleShotCommand(sender)


def _find_enum_context(elem):
    e_context = None
    xpath = './/{{{}}}EnumerationContext'.format(c.XML_NS_ENUMERATION)
    ctxt_elem = elem.find(xpath)
    if ctxt_elem is not None:
        e_context = ctxt_elem.text.split(':')[1]
    return e_context


def _find_shell_ids(elem):
    ids = []
    xpath = './/{{{}}}ShellId'.format(c.XML_NS_MSRSP)
    shells = elem.findall(xpath)
    for shell in shells:
        ids.append(shell.text)
    return ids


@defer.inlineCallbacks
def _get_active_shells(request_sender):
    elem = yield request_sender.send_request('enum_shells')
    enum_context = _find_enum_context(elem)
    if enum_context is None:
        defer.returnValue(None)
    response = yield request_sender.send_request('pull_shells', uuid=enum_context)
    body = ElementTree.tostring(response)
    parser, factory = create_parser_and_factory()
    parser.feed(body)
    defer.returnValue(factory.items)


@defer.inlineCallbacks
def _get_active_shell(request_sender, conn_info, min_runtime=600):
    """Sift through existing shells to find what should be the oldest active shell
    created by our user.  Compare against minimum runtime so we grab the oldest
    shell.  something less than min_runtime could have been created by a different
    client.  sender can be RequestSender or WinRMClient.  conn_info is a
    txwinrm.util.ConnectionInfo instance
    """
    shells = yield _get_active_shells(request_sender)
    active_shell = None

    user_domain = conn_info.username.split('@')
    try:
        # get domain user as netbios
        user = (user_domain[1].split('.')[0] + '\\' + user_domain[0]).lower()
    except IndexError:
        # local user, no netbios
        # user_domain[0] will always exist
        user = user_domain[0].lower()

    def get_runtime(runtime):
        # return total runtime in seconds
        # ShellRunTime is specified as P<days>DT<hours>H<minutes>M<seconds>S
        # e.g. P1DT1H1M1S is a runtime of of 1 day, 1 hour, 1 minute, and 1 second
        # we'll calculate the total number of seconds a shell has been running using
        # these numbers
        try:
            rt_match = re.match('P(?P<d>\d+)DT(?P<h>\d+)H(?P<m>\d+)M(?P<s>\d+)S', runtime)
            return int(rt_match.group('s')) + (int(rt_match.group('m')) * 60) + (int(rt_match.group('h')) * 3600) + (int(rt_match.group('d')) * 86400)
        except Exception:
            return 0

    for shell in shells:
        if user == shell.Owner.lower():
            runtime = get_runtime(shell.ShellRunTime)
            if runtime > min_runtime:
                # found possible candidate, test against previous
                if active_shell is not None:
                    prev_runtime = get_runtime(active_shell.ShellRunTime)
                    if runtime > prev_runtime:
                        active_shell = shell
                else:
                    active_shell = shell
    defer.returnValue(active_shell)


class LongRunningCommand(object):

    def __init__(self, sender, min_runtime=600):
        self._sender = sender
        self._shell_id = None
        self._command_id = None
        self._exit_code = None

        # attach to shell with a minimum runtime of x seconds to know
        # that our user created the shell
        self._min_runtime = min_runtime

    @defer.inlineCallbacks
    def is_shell_active(self, shell_id):
        if shell_id is None:
            defer.returnValue(False)
        elem = yield self._sender.send_request('enum_shells')
        enum_context = _find_enum_context(elem)
        if enum_context is None:
            defer.returnValue(False)
        elem = yield self._sender.send_request('pull_shells', uuid=enum_context)
        shell_ids = _find_shell_ids(elem)
        if shell_id in shell_ids:
            defer.returnValue(True)
        else:
            defer.returnValue(False)

    @defer.inlineCallbacks
    def start(self, command_line, ps_script=None):
        elem = yield self._sender.send_request('create')
        self._shell_id = _find_shell_id(elem)
        if ps_script is not None:
            log.debug("LongRunningCommand run_command: {0}".format(command_line + ps_script))
            command_line_elem = _build_ps_command_line_elem(command_line, ps_script)
        else:
            log.debug("LongRunningCommand run_command: {0}".format(command_line))
            command_line_elem = _build_command_line_elem(command_line)
        log.debug('LongRunningCommand run_command: sending command request '
                  '(shell_id={0}, command_line_elem={1})'.format(
                      self._shell_id, command_line_elem))
        try:
            command_elem = yield self._sender.send_request(
                'command', shell_id=self._shell_id,
                command_line_elem=command_line_elem,
                timeout=self._sender._sender._conn_info.timeout)
        except TimeoutError:
            yield self._sender.close_connections()
            raise
        self._command_id = _find_command_id(command_elem)
        defer.returnValue(self._command_id)

    @defer.inlineCallbacks
    def receive(self):
        try:
            receive_elem = yield self._sender.send_request(
                'receive',
                shell_id=self._shell_id,
                command_id=self._command_id)
        except TimeoutError:
            # could be simple network problem, reconnect and try again
            yield self._sender.close_connections()
            try:
                receive_elem = yield self._sender.send_request(
                    'receive',
                    shell_id=self._shell_id,
                    command_id=self._command_id)
            except TimeoutError:
                yield self._sender.close_connections()
            except Exception:
                raise
        stdout_parts = _find_stream(receive_elem, self._command_id, 'stdout')
        stderr_parts = _find_stream(receive_elem, self._command_id, 'stderr')
        self._exit_code = _find_exit_code(receive_elem, self._command_id)
        stdout = _stripped_lines(stdout_parts)
        stderr = _stripped_lines(stderr_parts)
        defer.returnValue((stdout, stderr))

    @defer.inlineCallbacks
    def stop(self):
        for _ in xrange(_MAX_RETRIES):
            try:
                yield self._sender.send_request(
                    'signal',
                    shell_id=self._shell_id,
                    command_id=self._command_id,
                    signal_code=c.SHELL_SIGNAL_CTRL_C)
                break
            except TimeoutError:
                # we may need to reset the connection and try again
                yield self._sender.close_connections()
            except Exception:
                pass
        try:
            stdout, stderr = yield self.receive()
        except TimeoutError:
            # close_connections done in receive() for TimeoutError
            raise
        except RequestError:
            yield self._sender.send_request('delete', shell_id=self._shell_id)
            yield self._sender.close_connections()
            self._shell_id = None
            defer.returnValue(CommandResponse([], [], 0))
        try:
            yield self._sender.send_request(
                'signal',
                shell_id=self._shell_id,
                command_id=self._command_id,
                signal_code=c.SHELL_SIGNAL_TERMINATE)
        except RequestError:
            pass
        yield self._sender.send_request('delete', shell_id=self._shell_id)
        yield self._sender.close_connections()
        self._shell_id = None
        defer.returnValue(CommandResponse(stdout, stderr, self._exit_code))


def create_long_running_command(conn_info):
    sender = create_etree_request_sender(conn_info)
    return LongRunningCommand(sender)


@defer.inlineCallbacks
def create_long_running_shell(conn_info):
    results = {}

    sender = create_etree_request_sender(conn_info)
    elem = yield sender.send_request('create')
    shell_id = _find_shell_id(elem)

    results['sender'] = sender
    results['shell_id'] = shell_id

    defer.returnValue(results)


@defer.inlineCallbacks
def retrieve_long_running_shell(sender, shell_id, command_line):

    stdout_parts = []
    stderr_parts = []
    exit_code = None

    command_line_elem = _build_command_line_elem(command_line)

    command_elem = yield sender.send_request(
        'command', shell_id=shell_id,
        command_line_elem=command_line_elem)
    command_id = _find_command_id(command_elem)

    for i in xrange(3):
        receive_elem = yield sender.send_request(
            'receive',
            shell_id=shell_id,
            command_id=command_id)
        stdout_parts.extend(
            _find_stream(receive_elem, command_id, 'stdout'))
        stderr_parts.extend(
            _find_stream(receive_elem, command_id, 'stderr'))
        exit_code = _find_exit_code(receive_elem, command_id)

    stdout = _stripped_lines(stdout_parts)
    stderr = _stripped_lines(stderr_parts)

    defer.returnValue(CommandResponse(stdout, stderr, exit_code))


class Typeperf(object):

    def __init__(self, long_running_command):
        self._long_running_command = long_running_command
        self._counters = None
        self._row_count = 0

    @defer.inlineCallbacks
    def start(self, counters, time_between_samples=1):
        self._counters = counters
        self._row_count = 0
        quoted_counters = ['"{0}"'.format(c) for c in counters]
        command_line = 'typeperf {0} -si {1}'.format(
            ' '.join(quoted_counters), time_between_samples)
        yield self._long_running_command.start(command_line)

    @defer.inlineCallbacks
    def receive(self):
        """
        Returns a pair, (<dictionary>, <stripped stderr lines>), where the
        dictionary is {<counter>: [(<datetime>, <float>)]}"""
        stdout, stderr = yield self._long_running_command.receive()
        dct = {}
        for counter in self._counters:
            dct[counter] = []
        for row in csv.reader(stdout):
            self._row_count += 1
            if self._row_count == 1:
                continue
            try:
                timestamp = get_datetime(row[0])
            except ValueError as e:
                log.debug('Typeperf receive {0}. {1}'.format(row, e))
                continue
            for counter, value in izip(self._counters, row[1:]):
                dct[counter].append((timestamp, float(value)))
        defer.returnValue((dct, stderr))

    @defer.inlineCallbacks
    def stop(self):
        yield self._long_running_command.stop()
        self._counters = None
        self._row_count = 0


def create_typeperf(conn_info):
    long_running_command = create_long_running_command(conn_info)
    return Typeperf(long_running_command)


class RemoteShell(object):

    _PROMPT_PATTERN = re.compile(r'[A-Z]:\\.*>$')
    _READ_DELAY = 0.2

    def __init__(self, sender, include_exit_codes=False):
        self._sender = sender
        self._include_exit_codes = include_exit_codes
        self._reset()

    def __del__(self):
        self.delete()

    @property
    def prompt(self):
        return self._prompt

    @defer.inlineCallbacks
    def create(self):
        if self._shell_id is not None:
            self.delete()
        log.debug("RemoteShell create: sending create request")
        elem = yield self._sender.send_request('create')
        self._shell_id = _find_shell_id(elem)
        command_line_elem = _build_command_line_elem('cmd')
        log.debug('RemoteShell create: sending command request (shell_id={0}, '
                  'command_line_elem={1})'.format(
                    self._shell_id, command_line_elem))
        command_elem = yield self._sender.send_request(
            'command', shell_id=self._shell_id,
            command_line_elem=command_line_elem,
            timeout=self._sender._sender._conn_info.timeout)
        self._command_id = _find_command_id(command_elem)
        self._deferred_receiving = self._start_receiving()
        stdout = []
        stderr = []
        while self._prompt is None:
            out, err = yield task.deferLater(
                reactor, self._READ_DELAY, self._get_output)
            stderr.extend(err)
            for line in out:
                if self._PROMPT_PATTERN.match(line):
                    self._prompt = line
                else:
                    stdout.append(line)
        defer.returnValue(CommandResponse(stdout, stderr, None))

    @defer.inlineCallbacks
    def run_command(self, command):
        stdout, stderr = yield self._run_command(command)
        if self._include_exit_codes:
            o2, e2 = yield self._run_command('echo %errorlevel%')
            exit_code = o2[0]
        else:
            exit_code = None
        defer.returnValue(CommandResponse(stdout, stderr, exit_code))

    @defer.inlineCallbacks
    def delete(self):
        if self._shell_id is None:
            return
        self.run_command('exit')
        exit_code = yield self._deferred_receiving
        yield self._sender.send_request(
            'signal',
            shell_id=self._shell_id,
            command_id=self._command_id,
            signal_code=c.SHELL_SIGNAL_TERMINATE)
        yield self._sender.send_request('delete', shell_id=self._shell_id)
        stdout, stderr = self._get_output()
        self._reset()
        defer.returnValue(CommandResponse(stdout, stderr, exit_code))

    def _reset(self):
        self._shell_id = None
        self._command_id = None
        self._deferred_receiving = None
        self._prompt = None
        self._stdout_parts = []
        self._stderr_parts = []

    @defer.inlineCallbacks
    def _start_receiving(self):
        exit_code = None
        while exit_code is None:
            receive_elem = yield task.deferLater(
                reactor, self._READ_DELAY, self._sender.send_request,
                'receive', shell_id=self._shell_id,
                command_id=self._command_id)
            self._stdout_parts.extend(
                _find_stream(receive_elem, self._command_id, 'stdout'))
            self._stderr_parts.extend(
                _find_stream(receive_elem, self._command_id, 'stderr'))
            exit_code = _find_exit_code(receive_elem, self._command_id)
        defer.returnValue(exit_code)

    def _get_output(self):
        stdout = _stripped_lines(self._stdout_parts)
        stderr = _stripped_lines(self._stderr_parts)
        del self._stdout_parts[:]
        del self._stderr_parts[:]
        return stdout, stderr

    @defer.inlineCallbacks
    def _run_command(self, command):
        base64_encoded_command = base64.encodestring('{0}\r\n'.format(command))
        yield self._sender.send_request(
            'send',
            shell_id=self._shell_id,
            command_id=self._command_id,
            base64_encoded_command=base64_encoded_command)
        stdout = []
        stderr = []
        for i in xrange(_MAX_REQUESTS_PER_COMMAND):
            out, err = yield task.deferLater(
                reactor, self._READ_DELAY, self._get_output)
            stderr.extend(err)
            if not out:
                continue
            stdout.extend(out[:-1])
            if out[-1] == self._prompt:
                break
            stdout.append(out[-1])
        else:
            raise Exception("Reached max requests per command.")
        defer.returnValue((stdout, stderr))


def create_remote_shell(conn_info, include_exit_codes=False):
    sender = create_etree_request_sender(conn_info)
    return RemoteShell(sender, include_exit_codes)