Fabian Peter Hammerle 5 years ago
commit
911278b036
3 changed files with 131 additions and 0 deletions
  1. 6 0
      LICENSE
  2. 92 0
      pyftpd-sink
  3. 33 0
      setup.py

+ 6 - 0
LICENSE

@@ -0,0 +1,6 @@
+Copyright (c) 2018 Fabian Peter Hammerle
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 92 - 0
pyftpd-sink

@@ -0,0 +1,92 @@
+#!/usr/bin/env python3
+
+import hashlib
+import os
+import pyftpdlib.authorizers
+import pyftpdlib.handlers
+import pyftpdlib.servers
+
+
+class SHA256Authorizer(pyftpdlib.authorizers.DummyAuthorizer):
+
+    def validate_authentication(self, username, password, handler):
+        # pyftpdlib/authorizers.py", line 110, in add_user
+        #   dic = {'pwd': str(password),
+        # so we can not compare against .digest()
+        password_hash = hashlib.sha256(password.encode()).hexdigest()
+        if self.user_table[username]['pwd'] != password_hash.lower():
+            raise pyftpdlib.authorizers.AuthenticationFailed()
+
+
+def serve(root_dir_path, username, password_sha256_hexdigest, control_port, passive_port):
+    assert os.path.isdir(root_dir_path), root_dir_path
+    authorizer = SHA256Authorizer()
+    authorizer.add_user(
+        username,
+        password_sha256_hexdigest.lower(),
+        homedir=root_dir_path,
+        # https://pyftpdlib.readthedocs.io/en/latest/api.html#pyftpdlib.authorizers.DummyAuthorizer.add_user
+        # e: change dir
+        # m: mkdir
+        # w: write
+        perm='emw',
+        msg_login='renshi ni hen gaoxing',
+        msg_quit='zaijian',
+    )
+    handler = pyftpdlib.handlers.FTPHandler
+    handler.authorizer = authorizer
+    handler.banner = 'ni hao'
+    # handler.masquerade_address = '192.168.1.1'
+    handler.passive_ports = (passive_port,)
+    server = pyftpdlib.servers.FTPServer((None, control_port), handler)
+    # apparently requires +1 for unknown reasons
+    server.max_cons = 1 + 1
+    server.serve_forever()
+
+
+def _init_argparser():
+    import argparse
+    argparser = argparse.ArgumentParser()
+    argparser.add_argument(
+        '--root', '--root-dir',
+        metavar='path',
+        dest='root_dir_path',
+        required=True,
+    )
+    argparser.add_argument(
+        '--user', '--username',
+        metavar='username',
+        dest='username',
+        required=True,
+    )
+    argparser.add_argument(
+        '--pwd-hash', '--password-hash',
+        metavar='sha256_hexdigest',
+        dest='password_sha256_hexdigest',
+        required=True,
+    )
+    argparser.add_argument(
+        '--ctrl-port', '--control-port',
+        metavar='port',
+        dest='control_port',
+        default=2121,
+    )
+    argparser.add_argument(
+        '--pasv-port', '--passive-port',
+        metavar='port',
+        dest='passive_port',
+        default=62121,
+    )
+    return argparser
+
+
+def main(argv):
+    argparser = _init_argparser()
+    args = argparser.parse_args(argv[1:])
+    serve(**vars(args))
+    return 0
+
+
+if __name__ == "__main__":
+    import sys
+    sys.exit(main(sys.argv))

+ 33 - 0
setup.py

@@ -0,0 +1,33 @@
+from setuptools import setup
+
+setup(
+    name='pyftpd-sink',
+    version='0.1',
+    # description='',
+    author='Fabian Peter Hammerle',
+    author_email='fabian@hammerle.me',
+    url='https://git.hammerle.me/fphammerle/pyftpd-sink',
+    license='MIT',
+    keywords=[
+        'ftp',
+        'server',
+    ],
+    classifiers=[
+        'Development Status :: 4 - Beta',
+        'Intended Audience :: System Administrators',
+        'License :: OSI Approved :: MIT License',
+        'Programming Language :: Python',
+        'Topic :: Internet :: File Transfer Protocol (FTP)',
+        'Programming Language :: Python',
+        'Topic :: System :: Systems Administration',
+        'Topic :: Utilities',
+    ],
+    packages=[],
+    scripts=[
+        'pyftpd-sink',
+    ],
+    install_requires=[
+        'pyftpdlib>=1.5.4',
+    ],
+    tests_require=[],
+)