This file is indexed.

/usr/lib/python3/dist-packages/asyncssh/scp.py is in python3-asyncssh 1.11.1-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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
# Copyright (c) 2017 by Ron Frederick <ronf@timeheart.net>.
# All rights reserved.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v1.0 which accompanies this
# distribution and is available at:
#
#     http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
#     Ron Frederick - initial implementation, API, and documentation
#     Jonathan Slenders - proposed changes to allow SFTP server callbacks
#                         to be coroutines

"""SCP handlers"""

import argparse
import asyncio
import posixpath
import shlex
import stat

from .constants import DEFAULT_LANG
from .constants import FX_BAD_MESSAGE, FX_CONNECTION_LOST, FX_FAILURE

from .sftp import LocalFile, match_glob
from .sftp import SFTP_BLOCK_SIZE, SFTPAttrs, SFTPError, SFTPServerFile


def _parse_cd_args(args):
    """Parse arguments to an SCP copy or dir request"""

    try:
        permissions, size, name = args.split()
        return int(permissions, 8), int(size), name
    except ValueError:
        raise SCPError(FX_BAD_MESSAGE, 'Invalid copy or dir request') from None


def _parse_t_args(args):
    """Parse argument to an SCP time request"""

    try:
        atime, _, mtime, _ = args.split()
        return int(atime), int(mtime)
    except ValueError:
        raise SCPError(FX_BAD_MESSAGE, 'Invalid time request') from None


@asyncio.coroutine
def _parse_path(path):
    """Convert an SCP path into an SSHClientConnection and path"""

    from . import connect

    if isinstance(path, tuple):
        conn, path = path
    elif isinstance(path, str) and ':' in path:
        conn, path = path.split(':')
    elif isinstance(path, bytes) and b':' in path:
        conn, path = path.split(b':')
    elif isinstance(path, (str, bytes)):
        conn = None
    else:
        conn = path
        path = b'.'

    if isinstance(conn, (str, bytes)):
        close_conn = True
        conn = yield from connect(conn)
    elif isinstance(conn, tuple):
        close_conn = True
        conn = yield from connect(*conn)
    else:
        close_conn = False

    return conn, path, close_conn


@asyncio.coroutine
def _start_remote(conn, source, must_be_dir, preserve, recurse, path):
    """Start remote SCP server"""

    if isinstance(path, str):
        path = path.encode('utf-8')

    command = (b'scp ' + (b'-f ' if source else b'-t ') +
               (b'-d ' if must_be_dir else b'') +
               (b'-p ' if preserve else b'') +
               (b'-r ' if recurse else b'') + path)

    writer, reader, _ = yield from conn.open_session(command, encoding=None)

    return reader, writer


class SCPError(SFTPError):
    """SCP error"""

    def __init__(self, code, reason, path=None, fatal=False,
                 suppress_send=False, lang=DEFAULT_LANG):
        if isinstance(reason, bytes):
            reason = reason.decode('utf-8', errors='replace')

        if isinstance(path, bytes):
            path = path.decode('utf-8', errors='replace')

        if path:
            reason = reason + ': ' + path

        super().__init__(code, reason, lang)
        self.fatal = fatal
        self.suppress_send = suppress_send


class _SCPArgParser(argparse.ArgumentParser):
    """A parser for SCP arguments"""

    def __init__(self):
        super().__init__(add_help=False)

        group = self.add_mutually_exclusive_group(required=True)
        group.add_argument('-f', dest='source', action='store_true')
        group.add_argument('-t', dest='source', action='store_false')

        self.add_argument('-d', dest='must_be_dir', action='store_true')
        self.add_argument('-p', dest='preserve', action='store_true')
        self.add_argument('-r', dest='recurse', action='store_true')
        self.add_argument('-v', dest='verbose', action='store_true')

        self.add_argument('path')

    def error(self, message):
        raise ValueError(message)

    def parse(self, command):
        """Parse an SCP command"""

        return self.parse_args(shlex.split(command)[1:])


class _SCPHandler:
    """SCP handler"""

    def __init__(self, reader, writer, error_handler=None):
        self._reader = reader
        self._writer = writer
        self._error_handler = error_handler

    @asyncio.coroutine
    def await_response(self):
        """Wait for an SCP response"""

        result = yield from self._reader.read(1)

        if result != b'\0':
            reason = yield from self._reader.readline()

            if not result or not reason.endswith(b'\n'):
                raise SCPError(FX_CONNECTION_LOST, 'Connection lost',
                               fatal=True, suppress_send=True)

            if result not in b'\x01\x02':
                reason = result + reason

            return SCPError(FX_FAILURE, reason[:-1], fatal=result != b'\x01',
                            suppress_send=True)

        return None

    def send_request(self, *args):
        """Send an SCP request"""

        self._writer.write(b''.join(args) + b'\n')

    @asyncio.coroutine
    def make_request(self, *args):
        """Send an SCP request and wait for a response"""

        self.send_request(*args)

        exc = yield from self.await_response()

        if exc:
            raise exc

    @asyncio.coroutine
    def send_data(self, data):
        """Send SCP file data"""

        self._writer.write(data)
        yield from self._writer.drain()

    def send_ok(self):
        """Send an SCP OK response"""

        self._writer.write(b'\0')

    def send_error(self, exc):
        """Send an SCP error response"""

        if isinstance(exc, SFTPError):
            reason = exc.reason.encode('utf-8')
        elif isinstance(exc, OSError): # pragma: no branch (win32)
            reason = exc.strerror.encode('utf-8')

            if exc.filename:
                if isinstance(exc.filename, str): # pragma: no cover (win32)
                    exc.filename = exc.filename.encode('utf-8')

                reason += b': ' + exc.filename
        else: # pragma: no cover (win32)
            reason = str(exc).encode('utf-8')

        fatal = getattr(exc, 'fatal', False)

        self._writer.write((b'\x02' if fatal else b'\x01') +
                           b'scp: ' + reason + b'\n')

    @asyncio.coroutine
    def recv_request(self):
        """Receive SCP request"""

        request = yield from self._reader.readline()

        if not request:
            return None, None

        return request[:1], request[1:-1]


    @asyncio.coroutine
    def recv_data(self, n):
        """Receive SCP file data"""

        return (yield from self._reader.read(n))

    def handle_error(self, exc):
        """Handle an SCP error"""

        if isinstance(exc, BrokenPipeError):
            exc = SCPError(FX_CONNECTION_LOST, 'Connection lost',
                           fatal=True, suppress_send=True)

        if not getattr(exc, 'suppress_send', False):
            self.send_error(exc)

        if getattr(exc, 'fatal', False) or self._error_handler is None:
            raise exc from None
        elif self._error_handler:
            self._error_handler(exc)

    def close(self):
        """Close an SCP session"""

        self._writer.close()


class _SCPSource(_SCPHandler):
    """SCP handler for sending files"""

    def __init__(self, fs, reader, writer, preserve, recurse,
                 block_size=SFTP_BLOCK_SIZE, progress_handler=None,
                 error_handler=None):
        super().__init__(reader, writer, error_handler)

        self._fs = fs
        self._preserve = preserve
        self._recurse = recurse
        self._block_size = block_size
        self._progress_handler = progress_handler

    @asyncio.coroutine
    def _make_cd_request(self, action, attrs, size, path):
        """Make an SCP copy or dir request"""

        args = '%04o %d ' % (attrs.permissions & 0o7777, size)
        yield from self.make_request(action, args.encode('ascii'),
                                     posixpath.basename(path))

    @asyncio.coroutine
    def _make_t_request(self, attrs):
        """Make an SCP time request"""

        args = '%d 0 %d 0' % (attrs.atime, attrs.mtime)
        yield from self.make_request(b'T', args.encode('ascii'))

    @asyncio.coroutine
    def _send_file(self, srcpath, dstpath, attrs):
        """Send a file over SCP"""

        file_obj = yield from self._fs.open(srcpath, 'rb')
        size = attrs.size
        local_exc = None
        offset = 0

        try:
            yield from self._make_cd_request(b'C', attrs, size, srcpath)

            while offset < size:
                blocklen = min(size - offset, self._block_size)

                if local_exc:
                    data = blocklen * b'\0'
                else:
                    try:
                        data = yield from file_obj.read(blocklen, offset)

                        if not data:
                            raise SCPError(FX_FAILURE, 'Unexpected EOF')
                    except (OSError, SFTPError) as exc:
                        local_exc = exc

                yield from self.send_data(data)
                offset += len(data)

                if self._progress_handler:
                    self._progress_handler(srcpath, dstpath, offset, size)
        finally:
            yield from file_obj.close()

        if local_exc:
            self.send_error(local_exc)
            local_exc.suppress_send = True
        else:
            self.send_ok()

        remote_exc = yield from self.await_response()
        exc = remote_exc or local_exc

        if exc:
            raise exc

    @asyncio.coroutine
    def _send_dir(self, srcpath, dstpath, attrs):
        """Send directory over SCP"""

        yield from self._make_cd_request(b'D', attrs, 0, srcpath)

        for name in (yield from self._fs.listdir(srcpath)):
            if name in (b'.', b'..'):
                continue

            yield from self._send_files(posixpath.join(srcpath, name),
                                        posixpath.join(dstpath, name))

        yield from self.make_request(b'E')

    @asyncio.coroutine
    def _send_files(self, srcpath, dstpath):
        """Send files via SCP"""

        try:
            attrs = yield from self._fs.stat(srcpath)

            if self._preserve:
                yield from self._make_t_request(attrs)

            if self._recurse and stat.S_ISDIR(attrs.permissions):
                yield from self._send_dir(srcpath, dstpath, attrs)
            elif stat.S_ISREG(attrs.permissions):
                yield from self._send_file(srcpath, dstpath, attrs)
            else:
                raise SCPError(FX_FAILURE, 'Not a regular file', srcpath)
        except (OSError, SFTPError, ValueError) as exc:
            self.handle_error(exc)

    @asyncio.coroutine
    def run(self, srcpath):
        """Start SCP transfer"""

        try:
            if isinstance(srcpath, str):
                srcpath = srcpath.encode('utf-8')

            exc = yield from self.await_response()

            if exc:
                raise exc

            for path in (yield from match_glob(self._fs, srcpath)):
                yield from self._send_files(path, b'')
        except (OSError, SFTPError) as exc:
            self.handle_error(exc)
        finally:
            self.close()


class _SCPSink(_SCPHandler):
    """SCP handler for receiving files"""

    def __init__(self, fs, reader, writer, must_be_dir, preserve, recurse,
                 block_size=SFTP_BLOCK_SIZE, progress_handler=None,
                 error_handler=None):
        super().__init__(reader, writer, error_handler)

        self._fs = fs
        self._must_be_dir = must_be_dir
        self._preserve = preserve
        self._recurse = recurse
        self._block_size = block_size
        self._progress_handler = progress_handler

    @asyncio.coroutine
    def _recv_file(self, srcpath, dstpath, size):
        """Receive a file via SCP"""

        file_obj = yield from self._fs.open(dstpath, 'wb')
        local_exc = None
        offset = 0

        try:
            self.send_ok()

            while offset < size:
                blocklen = min(size - offset, self._block_size)
                data = yield from self.recv_data(blocklen)

                if not data:
                    raise SCPError(FX_CONNECTION_LOST, 'Connection lost',
                                   fatal=True, suppress_send=True)

                if not local_exc:
                    try:
                        yield from file_obj.write(data, offset)
                    except (OSError, SFTPError) as exc:
                        local_exc = exc

                offset += len(data)

                if self._progress_handler:
                    self._progress_handler(srcpath, dstpath, offset, size)
        finally:
            yield from file_obj.close()

        remote_exc = yield from self.await_response()

        if local_exc:
            self.send_error(local_exc)
            local_exc.suppress_send = True
        else:
            self.send_ok()

        exc = remote_exc or local_exc

        if exc:
            raise exc

    @asyncio.coroutine
    def _recv_dir(self, srcpath, dstpath):
        """Receive a directory over SCP"""

        if not self._recurse:
            raise SCPError(FX_BAD_MESSAGE, 'Directory received without recurse')

        if (yield from self._fs.exists(dstpath)):
            if not (yield from self._fs.isdir(dstpath)):
                raise SCPError(FX_FAILURE, 'Not a directory', dstpath)
        else:
            yield from self._fs.mkdir(dstpath)

        yield from self._recv_files(srcpath, dstpath)

    @asyncio.coroutine
    def _recv_files(self, srcpath, dstpath):
        """Receive files over SCP"""

        self.send_ok()

        attrs = SFTPAttrs()

        while True:
            action, args = yield from self.recv_request()

            if not action:
                break

            try:
                if action in b'\x01\x02':
                    raise SCPError(FX_FAILURE, args, fatal=action != b'\x01',
                                   suppress_send=True)
                elif action == b'T':
                    if self._preserve:
                        attrs.atime, attrs.mtime = _parse_t_args(args)

                    self.send_ok()
                elif action == b'E':
                    self.send_ok()
                elif action in b'CD':
                    try:
                        attrs.permissions, size, name = _parse_cd_args(args)

                        new_srcpath = posixpath.join(srcpath, name)

                        if (yield from self._fs.isdir(dstpath)):
                            new_dstpath = posixpath.join(dstpath, name)
                        else:
                            new_dstpath = dstpath

                        if action == b'D':
                            yield from self._recv_dir(new_srcpath, new_dstpath)
                        else:
                            yield from self._recv_file(new_srcpath,
                                                       new_dstpath, size)

                        if self._preserve:
                            yield from self._fs.setstat(new_dstpath, attrs)
                    finally:
                        attrs = SFTPAttrs()
                else:
                    raise SCPError(FX_BAD_MESSAGE, 'Unknown request')
            except (OSError, SFTPError) as exc:
                self.handle_error(exc)

    @asyncio.coroutine
    def run(self, dstpath):
        """Start SCP file receive"""

        try:
            if isinstance(dstpath, str):
                dstpath = dstpath.encode('utf-8')

            if self._must_be_dir and not (yield from self._fs.isdir(dstpath)):
                self.handle_error(SCPError(FX_FAILURE, 'Not a directory',
                                           dstpath))
            else:
                yield from self._recv_files(b'', dstpath)
        except (OSError, SFTPError, ValueError) as exc:
            self.handle_error(exc)
        finally:
            self.close()


class _SCPCopier:
    """SCP handler for remote-to-remote copies"""

    def __init__(self, src_reader, src_writer, dst_reader, dst_writer,
                 block_size=SFTP_BLOCK_SIZE, progress_handler=None,
                 error_handler=None):
        self._source = _SCPHandler(src_reader, src_writer)
        self._sink = _SCPHandler(dst_reader, dst_writer)
        self._block_size = block_size
        self._progress_handler = progress_handler
        self._error_handler = error_handler

    def _handle_error(self, exc):
        """Handle an SCP error"""

        if isinstance(exc, BrokenPipeError):
            exc = SCPError(FX_CONNECTION_LOST, 'Connection lost', fatal=True)

        if self._error_handler and not getattr(exc, 'fatal', False):
            self._error_handler(exc)
        else:
            raise exc

    @asyncio.coroutine
    def _forward_response(self, src, dst):
        """Forward an SCP response between two remote SCP servers"""

        # pylint: disable=no-self-use

        try:
            exc = yield from src.await_response()

            if exc:
                dst.send_error(exc)
                return exc
            else:
                dst.send_ok()
                return None
        except OSError as exc:
            return exc

    @asyncio.coroutine
    def _copy_file(self, path, size):
        """Copy a file from one remote SCP server to another"""

        offset = 0

        while offset < size:
            blocklen = min(size - offset, self._block_size)
            data = yield from self._source.recv_data(blocklen)

            if not data:
                raise SCPError(FX_CONNECTION_LOST, 'Connection lost',
                               fatal=True)

            yield from self._sink.send_data(data)
            offset += len(data)

            if self._progress_handler:
                self._progress_handler(path, path, offset, size)

        source_exc = yield from self._forward_response(self._source, self._sink)
        sink_exc = yield from self._forward_response(self._sink, self._source)

        exc = sink_exc or source_exc

        if exc:
            self._handle_error(exc)

    @asyncio.coroutine
    def run(self):
        """Start SCP remote-to-remote transfer"""

        paths = []

        try:
            exc = yield from self._forward_response(self._sink, self._source)

            if exc:
                self._handle_error(exc)

            while True:
                action, args = yield from self._source.recv_request()

                if not action:
                    break

                self._sink.send_request(action, args)

                if action in b'\x01\x02':
                    exc = SCPError(FX_FAILURE, args, fatal=action != b'\x01')
                    self._handle_error(exc)
                    continue

                exc = yield from self._forward_response(self._sink,
                                                        self._source)

                if exc:
                    self._handle_error(exc)
                    continue

                if action in b'CD':
                    _, size, name = _parse_cd_args(args)

                    if action == b'C':
                        path = b'/'.join(paths + [name])
                        yield from self._copy_file(path, size)
                    else:
                        paths.append(name)
                elif action == b'E':
                    if paths:
                        paths.pop()
                    else:
                        break
                elif action != b'T':
                    raise SCPError(FX_BAD_MESSAGE, 'Unknown SCP action')
        except (OSError, SFTPError) as exc:
            self._handle_error(exc)
        finally:
            self._source.close()
            self._sink.close()


@asyncio.coroutine
def scp(srcpaths, dstpath=None, *, preserve=False, recurse=False,
        block_size=SFTP_BLOCK_SIZE, progress_handler=None, error_handler=None):
    """Copy files using SCP

       This function is a coroutine which copies one or more files or
       directories using the SCP protocol. Source and destination paths
       can be string or bytes values to reference local files or can be
       a tuple of the form ``(conn, path)`` where ``conn`` is an open
       :class:`SSHClientConnection` to reference files and directories
       on a remote system.

       For convenience, a host name or tuple of the form ``(host, port)``
       can be provided in place of the :class:`SSHClientConnection` to
       request that a new SSH connection be opened to a host using
       default connect arguments. A string or bytes value of the form
       ``'host:path'`` may also be used in place of the ``(conn, path)``
       tuple to make a new connection to the requested host on the
       default SSH port.

       Either a single source path or a sequence of source paths can be
       provided, and each path can contain '*' and '?' wildcard characters
       which can be used to match multiple source files or directories.

       When copying a single file or directory, the destination path
       can be either the full path to copy data into or the path to an
       existing directory where the data should be placed. In the latter
       case, the base file name from the source path will be used as the
       destination name.

       When copying multiple files, the destination path must refer to
       a directory. If it doesn't already exist, a directory will be
       created with that name.

       If the destination path is an :class:`SSHClientConnection` without
       a path or the path provided is empty, files are copied into the
       default destination working directory.

       If preserve is ``True``, the access and modification times and
       permissions of the original files and directories are set on the
       copied files. However, do to the timing of when this information
       is sent, the preserved access time will be what was set on the
       source file before the copy begins. So, the access time on the
       source file will no longer match the destination after the
       transfer completes.

       If recurse is ``True`` and the source path points at a directory,
       the entire subtree under that directory is copied.

       Symbolic links found on the source will have the contents of their
       target copied rather than creating a destination symbolic link.
       When using this option during a recursive copy, one needs to watch
       out for links that result in loops. SCP does not provide a
       mechanism for preserving links. If you need this, consider using
       SFTP instead.

       The block_size value controls the size of read and write operations
       issued to copy the files. It defaults to 16 KB.

       If progress_handler is specified, it will be called after each
       block of a file is successfully copied. The arguments passed to
       this handler will be the relative path of the file being copied,
       bytes copied so far, and total bytes in the file being copied. If
       multiple source paths are provided or recurse is set to ``True``,
       the progress_handler will be called consecutively on each file
       being copied.

       If error_handler is specified and an error occurs during the copy,
       this handler will be called with the exception instead of it being
       raised. This is intended to primarily be used when multiple source
       paths are provided or when recurse is set to ``True``, to allow
       error information to be collected without aborting the copy of the
       remaining files. The error handler can raise an exception if it
       wants the copy to completely stop. Otherwise, after an error, the
       copy will continue starting with the next file.

       :param srcpaths:
           The paths of the source files or directories to copy
       :param dstpath: (optional)
           The path of the destination file or directory to copy into
       :param bool preserve: (optional)
           Whether or not to preserve the original file attributes
       :param bool recurse: (optional)
           Whether or not to recursively copy directories
       :param int block_size: (optional)
           The block size to use for file reads and writes
       :param callable progress_handler: (optional)
           The function to call to report copy progress
       :param callable error_handler: (optional)
           The function to call when an error occurs

       :raises: | :exc:`OSError` if a local file I/O error occurs
                | :exc:`SFTPError` if the server returns an error
                | :exc:`ValueError` if both source and destination are local

    """

    if (isinstance(srcpaths, (str, bytes)) or
            (isinstance(srcpaths, tuple) and len(srcpaths) == 2)):
        srcpaths = [srcpaths]

    must_be_dir = len(srcpaths) > 1

    dstconn, dstpath, close_dst = yield from _parse_path(dstpath)

    try:
        for srcpath in srcpaths:
            srcconn, srcpath, close_src = yield from _parse_path(srcpath)

            try:
                if srcconn and dstconn:
                    src_reader, src_writer = yield from _start_remote(
                        srcconn, True, must_be_dir, preserve, recurse, srcpath)

                    dst_reader, dst_writer = yield from _start_remote(
                        dstconn, False, must_be_dir, preserve, recurse, dstpath)

                    copier = _SCPCopier(src_reader, src_writer, dst_reader,
                                        dst_writer, block_size,
                                        progress_handler, error_handler)

                    yield from copier.run()
                elif srcconn:
                    reader, writer = yield from _start_remote(
                        srcconn, True, must_be_dir, preserve, recurse, srcpath)

                    sink = _SCPSink(LocalFile, reader, writer, must_be_dir,
                                    preserve, recurse, block_size,
                                    progress_handler, error_handler)

                    yield from sink.run(dstpath)
                elif dstconn:
                    reader, writer = yield from _start_remote(
                        dstconn, False, must_be_dir, preserve, recurse, dstpath)

                    source = _SCPSource(LocalFile, reader, writer,
                                        preserve, recurse, block_size,
                                        progress_handler, error_handler)

                    yield from source.run(srcpath)
                else:
                    raise ValueError('Local copy not supported')
            finally:
                if close_src:
                    srcconn.close()
                    yield from srcconn.wait_closed()
    finally:
        if close_dst:
            dstconn.close()
            yield from dstconn.wait_closed()


@asyncio.coroutine
def run_scp_server(sftp_server, command, stdin, stdout, stderr):
    """Return a handler for an SCP server session"""

    try:
        args = _SCPArgParser().parse(command)
    except ValueError as exc:
        stderr.write(b'scp: ' + str(exc).encode('utf-8') + b'\n')
        stderr.channel.exit(1)
        return

    fs = SFTPServerFile(sftp_server)

    if args.source:
        handler = _SCPSource(fs, stdin, stdout, args.preserve, args.recurse,
                             error_handler=False)
    else:
        handler = _SCPSink(fs, stdin, stdout, args.must_be_dir, args.preserve,
                           args.recurse, error_handler=False)

    try:
        yield from handler.run(args.path)
    finally:
        sftp_server.exit()